[gtk+/app-chooser-api-add] app-chooser-button: add gtk_app_chooser_button_set_active_custom_item()



commit 0293ee30efe8aeeec88abe5a6ab6578ee5bafb7d
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Wed Dec 1 12:12:03 2010 +0100

    app-chooser-button: add gtk_app_chooser_button_set_active_custom_item()
    
    This allows to pre-select a custom item.

 gtk/gtkappchooserbutton.c    |   61 ++++++++++++++++++++++++++++++++++++++++++
 gtk/gtkappchooserbutton.h    |    2 +
 tests/testappchooserbutton.c |    3 ++
 3 files changed, 66 insertions(+), 0 deletions(-)
---
diff --git a/gtk/gtkappchooserbutton.c b/gtk/gtkappchooserbutton.c
index 639520c..b1b7bbe 100644
--- a/gtk/gtkappchooserbutton.c
+++ b/gtk/gtkappchooserbutton.c
@@ -555,6 +555,37 @@ gtk_app_chooser_button_init (GtkAppChooserButton *self)
                            g_free, NULL);
 }
 
+static gboolean
+app_chooser_button_iter_from_custom_name (GtkAppChooserButton *self,
+                                          const gchar *name,
+                                          GtkTreeIter *set_me)
+{
+  GtkTreeIter iter;
+  gchar *custom_name = NULL;
+
+  if (!gtk_tree_model_get_iter_first
+      (GTK_TREE_MODEL (self->priv->store), &iter))
+    return FALSE;
+
+  do {
+    gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
+                        COLUMN_NAME, &custom_name,
+                        -1);
+
+    if (g_strcmp0 (custom_name, name) == 0)
+      {
+        g_free (custom_name);
+        *set_me = iter;
+
+        return TRUE;
+      }
+
+    g_free (custom_name);
+  } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (self->priv->store), &iter));
+
+  return FALSE;
+}
+
 static void
 real_insert_custom_item (GtkAppChooserButton *self,
                          const gchar *name,
@@ -670,6 +701,36 @@ gtk_app_chooser_button_append_custom_item (GtkAppChooserButton *self,
 }
 
 /**
+ * gtk_app_chooser_button_select_custom_item:
+ * @self: a #GtkAppChooserButton
+ * @name: the name of the custom item
+ *
+ * Selects a custom item previously added with
+ * gtk_app_chooser_button_append_custom_item().
+ *
+ * Since: 3.0
+ */
+void
+gtk_app_chooser_button_set_active_custom_item (GtkAppChooserButton *self,
+                                               const gchar         *name)
+{
+  GtkTreeIter iter;
+
+  g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
+  g_return_if_fail (name != NULL);
+
+  if (g_hash_table_lookup (self->priv->custom_item_names, name) == NULL ||
+      !app_chooser_button_iter_from_custom_name (self, name, &iter))
+    {
+      g_warning ("Can't find the item named %s in the app chooser.",
+                 name);
+      return;
+    }
+
+  gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), &iter);
+}
+
+/**
  * gtk_app_chooser_button_get_show_dialog_item:
  * @self: a #GtkAppChooserButton
  *
diff --git a/gtk/gtkappchooserbutton.h b/gtk/gtkappchooserbutton.h
index 61a88b1..67fc5de 100644
--- a/gtk/gtkappchooserbutton.h
+++ b/gtk/gtkappchooserbutton.h
@@ -68,6 +68,8 @@ void        gtk_app_chooser_button_append_custom_item (GtkAppChooserButton *self
                                                        const gchar         *name,
                                                        const gchar         *label,
                                                        GIcon               *icon);
+void     gtk_app_chooser_button_set_active_custom_item (GtkAppChooserButton *self,
+                                                        const gchar         *name);
 
 void     gtk_app_chooser_button_set_show_dialog_item  (GtkAppChooserButton *self,
                                                        gboolean             setting);
diff --git a/tests/testappchooserbutton.c b/tests/testappchooserbutton.c
index 751604b..0416af7 100644
--- a/tests/testappchooserbutton.c
+++ b/tests/testappchooserbutton.c
@@ -122,6 +122,9 @@ main (int argc,
   /* test refresh on a combo */
   gtk_app_chooser_refresh (GTK_APP_CHOOSER (combobox));
 
+  gtk_app_chooser_button_set_active_custom_item (GTK_APP_CHOOSER_BUTTON (combobox),
+                                                 CUSTOM_ITEM);
+
   gtk_widget_show_all (toplevel);
 
   g_signal_connect (toplevel, "delete-event",



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