[gtk+/wip/combo: 9/11] Change api around



commit 232580a734ceb5fc43c70285f7f1ff913760c3ed
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Dec 25 23:50:49 2014 -0500

    Change api around
    
    Rename add/remove to add_item/remove_item, flip the order of
    text and sort in the argument list, and add a group argument
    at the end. Add a gtk_combo_add_group function that lets one
    specify display text and sort key for a group.

 docs/reference/gtk/gtk3-sections.txt |    5 +-
 gtk/gtkcombo.c                       |  162 ++++++++++++++++++++++++----------
 gtk/gtkcombo.h                       |   15 ++--
 3 files changed, 125 insertions(+), 57 deletions(-)
---
diff --git a/docs/reference/gtk/gtk3-sections.txt b/docs/reference/gtk/gtk3-sections.txt
index 7c68edc..88d56f8 100644
--- a/docs/reference/gtk/gtk3-sections.txt
+++ b/docs/reference/gtk/gtk3-sections.txt
@@ -982,10 +982,11 @@ gtk_combo_box_text_get_type
 <TITLE>GtkCombo</TITLE>
 GtkCombo
 gtk_combo_new
+gtk_combo_add_item
+gtk_combo_remove_item
+gtk_combo_add_group
 gtk_combo_get_active
 gtk_combo_set_active
-gtk_combo_add
-gtk_combo_remove
 gtk_combo_get_placeholder
 gtk_combo_set_placeholder
 gtk_combo_get_allow_custom
diff --git a/gtk/gtkcombo.c b/gtk/gtkcombo.c
index 1fbc50c..684a31d 100644
--- a/gtk/gtkcombo.c
+++ b/gtk/gtkcombo.c
@@ -51,15 +51,19 @@
  *
  * To create a GtkCombo, use gtk_combo_new().
  *
- * You can add items to a Gtkcombo using gtk_combo_add() and remove them
- * with gtk_combo_remove(). Each item has an id that is returned as the
- * value of the #GtkCombo:active property when the item is currently
+ * You can add items to a GtkCombo using gtk_combo_add_item() and remove
+ * them with gtk_combo_remove_item(). Each item has an ID that is returned
+ * as the value of the #GtkCombo:active property when the item is currently
  * selected, an optional text that is used to display the item, and an
  * optional sort key that is used to sort the items.
  *
  * If you want to allow the user to enter custom values, use
  * gtk_combo_set_allow_custom().
  *
+ * Items can optionally be grouped, by specifying a group id as the last
+ * argument to gtk_combo_add_item(). Groups can have display text and sort
+ * keys that are different from the group id, by using gtk_combo_box_add_group().
+ *
  * # GtkCombo as GtkBuildable
  *
  * The GtkCombo implementation of the GtkBuildable interface supports
@@ -315,6 +319,10 @@ static void     custom_header_func   (GtkListBoxRow  *row,
                                       gpointer        data);
 static GtkWidget *group_get_list     (GtkCombo       *combo,
                                       const gchar    *group);
+static GtkWidget *ensure_group       (GtkCombo       *combo,
+                                      const gchar    *group,
+                                      const gchar    *text,
+                                      const gchar    *sort);
 
 static void     gtk_combo_buildable_init (GtkBuildableIface *iface);
 
@@ -601,7 +609,7 @@ item_end_element (GMarkupParseContext *context,
           g_string_assign (data->string, translated);
         }
 
-      gtk_combo_add (GTK_COMBO (data->object), data->id, data->sort, data->string->str);
+      gtk_combo_add_item (GTK_COMBO (data->object), data->id, data->string->str, data->sort, NULL);
     }
 
   data->translatable = FALSE;
@@ -732,6 +740,32 @@ list_get_show_more_item (GtkWidget *list)
   return (GtkWidget*)g_object_get_data (G_OBJECT (list), "show-more-item");
 }
 
+static gboolean
+is_group_row (GtkWidget *row)
+{
+  const gchar *group;
+
+  group = g_object_get_data (G_OBJECT (row), "group");
+  if (group && g_strcmp0 (group, "list") != 0)
+    return TRUE;
+
+  return FALSE;
+}
+
+static const gchar *
+group_row_get_sort (GtkWidget *row)
+{
+  const gchar *sort;
+  GtkWidget *label;
+
+  sort = g_object_get_data (G_OBJECT (row), "sort");
+  if (sort)
+    return sort;
+
+  label = g_object_get_data (G_OBJECT (row), "label");
+  return gtk_label_get_label (GTK_LABEL (label));
+}
+
 static gint
 list_sort_func (GtkListBoxRow *row1,
                 GtkListBoxRow *row2,
@@ -739,20 +773,28 @@ list_sort_func (GtkListBoxRow *row1,
 {
   GtkCombo *combo = data;
   GtkWidget *show_more;
+  const gchar *sort1;
+  const gchar *sort2;
 
   if (row1 == row2)
     return 0;
 
-  if (GTK_IS_COMBO_ROW (row1) && GTK_IS_COMBO_ROW (row2))
-    {
-      const gchar *sort1;
-      const gchar *sort2;
+  if (GTK_IS_COMBO_ROW (row1))
+    sort1 = gtk_combo_row_get_sort (GTK_COMBO_ROW (row1));
+  else if (is_group_row (GTK_WIDGET (row1)))
+    sort1 = group_row_get_sort (GTK_WIDGET (row1));
+  else
+    sort1 = NULL;
 
-      sort1 = gtk_combo_row_get_sort (GTK_COMBO_ROW (row1));
-      sort2 = gtk_combo_row_get_sort (GTK_COMBO_ROW (row2));
+  if (GTK_IS_COMBO_ROW (row2))
+    sort2 = gtk_combo_row_get_sort (GTK_COMBO_ROW (row2));
+  else if (is_group_row (GTK_WIDGET (row2)))
+    sort2 = group_row_get_sort (GTK_WIDGET (row2));
+  else
+    sort2 = NULL;
 
-      return g_strcmp0 (sort1, sort2);
-    }
+  if (sort1 && sort2)
+    return g_strcmp0 (sort1, sort2);
 
   show_more = list_get_show_more_item (gtk_widget_get_parent (GTK_WIDGET (row1)));
 
@@ -868,8 +910,8 @@ find_row_in_group (GtkWidget *row,
 static void
 add_to_list (GtkWidget   *list,
              const gchar *id,
-             const gchar *sort,
-             const gchar *text)
+             const gchar *text,
+             const gchar *sort)
 {
   GtkWidget *row;
   ForeachData data;
@@ -1077,7 +1119,7 @@ custom_entry_done (GtkWidget *widget,
   text = gtk_entry_get_text (GTK_ENTRY (combo->custom_entry));
   if (text[0] != '\0')
     {
-      gtk_combo_add (combo, text, text, text);
+      gtk_combo_add_item (combo, text, NULL, NULL, NULL);
       gtk_combo_set_active (combo, text);
       gtk_entry_set_text (GTK_ENTRY (combo->custom_entry), "");
       gtk_widget_hide (combo->popover);
@@ -1360,36 +1402,44 @@ gtk_combo_set_active (GtkCombo    *combo,
 }
 
 /**
- * gtk_combo_add:
+ * gtk_combo_add_item:
  * @combo: a #GtkCombo
  * @id: the ID for the item to add
- * @sort: (allow-none): a sort key for the item
  * @text: (allow-none): the text to display for the item
+ * @sort: (allow-none): a sort key for the item
+ * @group: (allow-none): the group for the item
  *
  * Adds an item to the combo.
  *
- * If an item with this ID already exists, its sort key
- * and display text will be updated with the new values.
+ * If an item with this ID already exists, its display text
+ * and sort key will be updated with the new values.
  *
- * If @sort is %NULL, the item will be sorted according to @text.
  * If @text is %NULL, the @id will be used to display the item.
+ * If @sort is %NULL, the item will be sorted according to @text.
  *
  * Since: 3.16
  */
 void
-gtk_combo_add (GtkCombo    *combo,
-               const gchar *id,
-               const gchar *sort,
-               const gchar *text)
+gtk_combo_add_item (GtkCombo    *combo,
+                    const gchar *id,
+                    const gchar *text,
+                    const gchar *sort,
+                    const gchar *group)
 {
+  GtkWidget *list;
+
   g_return_if_fail (GTK_IS_COMBO (combo));
 
-  add_to_list (combo->list, id, sort, text);
-  collapse (combo, combo->list);
+  if (group)
+    list = ensure_group (combo, group, NULL, NULL);
+  else
+    list = combo->list;
+  add_to_list (list, id, text, sort);
+  collapse (combo, list);
 }
 
 /**
- * gtk_combo_remove:
+ * gtk_combo_remove_item:
  * @combo: a #GtkCombo
  * @id: the ID of the item to remove
  *
@@ -1401,8 +1451,8 @@ gtk_combo_add (GtkCombo    *combo,
  * Since: 3.16
  */
 void
-gtk_combo_remove (GtkCombo    *combo,
-                  const gchar *id)
+gtk_combo_remove_item (GtkCombo    *combo,
+                       const gchar *id)
 {
   g_return_if_fail (GTK_IS_COMBO (combo));
 
@@ -1411,8 +1461,6 @@ gtk_combo_remove (GtkCombo    *combo,
 
   if (g_strcmp0 (id, combo->active) == 0)
     set_active (combo, NULL);
-
-  collapse (combo, combo->list);
 }
 
 /**
@@ -1519,20 +1567,22 @@ group_get_list (GtkCombo    *combo,
 
 static GtkWidget *
 ensure_group (GtkCombo    *combo,
-              const gchar *group)
+              const gchar *group,
+              const gchar *text,
+              const gchar *sort)
 {
   GtkWidget *tab;
-  GtkWidget *list;
+  GtkWidget *item;
+  GtkWidget *label;
 
   tab = gtk_stack_get_child_by_name (GTK_STACK (combo->stack), group);
   if (tab == NULL)
     {
+      GtkWidget *list;
       GtkWidget *frame;
       GtkWidget *header;
       GtkWidget *box;
       GtkWidget *image;
-      GtkWidget *label;
-      GtkWidget *item;
       GtkWidget *scrolled_window;
 
       tab = frame = gtk_frame_new (NULL);
@@ -1583,7 +1633,6 @@ ensure_group (GtkCombo    *combo,
       gtk_list_box_set_sort_func (GTK_LIST_BOX (list), list_sort_func, combo, NULL);
       gtk_list_box_set_header_func (GTK_LIST_BOX (list), list_header_func, combo, NULL);
 
-
       item = gtk_list_box_row_new ();
       gtk_widget_show (item);
       box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
@@ -1604,27 +1653,44 @@ ensure_group (GtkCombo    *combo,
 
       gtk_container_add (GTK_CONTAINER (item), box);
       gtk_list_box_insert (GTK_LIST_BOX (combo->list), item, -1);
+
       g_object_set_data (G_OBJECT (item), "group", (gpointer)group);
       g_object_set_data (G_OBJECT (item), "check", image);
+      g_object_set_data (G_OBJECT (item), "label", label);
+      g_object_set_data (G_OBJECT (tab), "item", item);
     }
 
-  list = (GtkWidget*)g_object_get_data (G_OBJECT (tab), "list");
+  item = (GtkWidget*)g_object_get_data (G_OBJECT (tab), "item");
+  label = (GtkWidget*)g_object_get_data (G_OBJECT (item), "label");
+  if (text)
+    gtk_label_set_label (GTK_LABEL (label), text);
+  if (sort)
+    g_object_set_data_full (G_OBJECT (item), "sort", g_strdup (sort), g_free);
+
+  gtk_list_box_invalidate_sort (GTK_LIST_BOX (combo->list));
+  gtk_list_box_invalidate_filter (GTK_LIST_BOX (combo->list));
 
-  return list;
+  return (GtkWidget*)g_object_get_data (G_OBJECT (tab), "list");
 }
 
+/**
+ * gtk_combo_add_group:
+ * @combo: a #GtkCombo
+ * @group: a group ID
+ * @text: (allow-none): An optional display text for the group
+ * @sort: (allow-none): An optional sort key for the group
+ *
+ * Associates a display text and sort key with a group of items.
+ *
+ * Since: 3.16
+ */
 void
-gtk_combo_add_with_group (GtkCombo    *combo,
-                          const gchar *group,
-                          const gchar *id,
-                          const gchar *sort,
-                          const gchar *text)
+gtk_combo_add_group (GtkCombo    *combo,
+                     const gchar *group,
+                     const gchar *text,
+                     const gchar *sort)
 {
-  GtkWidget *list;
-
   g_return_if_fail (GTK_IS_COMBO (combo));
 
-  list = ensure_group (combo, group);
-  add_to_list (list, id, sort, text);
-  collapse (combo, list);
+  ensure_group (combo, group, text, sort);
 }
diff --git a/gtk/gtkcombo.h b/gtk/gtkcombo.h
index 3046734..aaa9a85 100644
--- a/gtk/gtkcombo.h
+++ b/gtk/gtkcombo.h
@@ -51,12 +51,14 @@ void          gtk_combo_set_active       (GtkCombo    *combo,
                                           const gchar *id);
 
 GDK_AVAILABLE_IN_3_16
-void          gtk_combo_add              (GtkCombo    *combo,
+void          gtk_combo_add_item         (GtkCombo    *combo,
                                           const gchar *id,
+                                          const gchar *text,
                                           const gchar *sort,
-                                          const gchar *text);
+                                          const gchar *group);
+
 GDK_AVAILABLE_IN_3_16
-void          gtk_combo_remove           (GtkCombo    *combo,
+void          gtk_combo_remove_item      (GtkCombo    *combo,
                                           const gchar *id);
 
 GDK_AVAILABLE_IN_3_16
@@ -73,11 +75,10 @@ void          gtk_combo_set_allow_custom (GtkCombo    *combo,
                                           gboolean     allow);
 
 GDK_AVAILABLE_IN_3_16
-void          gtk_combo_add_with_group   (GtkCombo    *combo,
+void          gtk_combo_add_group        (GtkCombo    *combo,
                                           const gchar *group,
-                                          const gchar *id,
-                                          const gchar *sort,
-                                          const gchar *text);
+                                          const gchar *text,
+                                          const gchar *sort);
 
 G_END_DECLS
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]