]> Pileus Git - ~andy/gtk/commitdiff
app-chooser-button: add gtk_app_chooser_button_set_active_custom_item()
authorCosimo Cecchi <cosimoc@gnome.org>
Wed, 1 Dec 2010 11:12:03 +0000 (12:12 +0100)
committerCosimo Cecchi <cosimoc@gnome.org>
Wed, 1 Dec 2010 16:04:22 +0000 (17:04 +0100)
This allows to pre-select a custom item.

gtk/gtkappchooserbutton.c
gtk/gtkappchooserbutton.h
tests/testappchooserbutton.c

index b510764ab18ab87fa222b52ce8efb8166e03d129..d9cf438b0591c7b2b4353400c1985757df487229 100644 (file)
@@ -556,6 +556,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,
   real_insert_custom_item (self, name, label, icon, TRUE, &iter);
 }
 
+/**
+ * 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
index 61a88b11cebce570574b0f59de15888f33c638f9..67fc5de5d5785d0c40fa071d5062d37f0ffe5504 100644 (file)
@@ -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);
index 751604bd352f3c4994d85135c146bb9d52003a87..0416af72c56e9f8b1b8b4d630d15e4a3b8523d1d 100644 (file)
@@ -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",