]> Pileus Git - ~andy/gtk/blob - gtk/gtkappchooserbutton.c
Add appchooser docs
[~andy/gtk] / gtk / gtkappchooserbutton.c
1 /*
2  * gtkappchooserbutton.h: an app-chooser combobox
3  *
4  * Copyright (C) 2010 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with the Gnome Library; see the file COPYING.LIB.  If not,
18  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Authors: Cosimo Cecchi <ccecchi@redhat.com>
22  */
23
24 /**
25  * SECTION:gtkappchooserbutton
26  * @Title: GtkAppChooserButton
27  * @Short_description: A button to launch an application chooser dialog
28  *
29  * The #GtkAppChooserButton is a widget that lets the user select
30  * an application. It implements the #GtkAppChooser interface.
31  */
32 #include "config.h"
33
34 #include "gtkappchooserbutton.h"
35
36 #include "gtkappchooser.h"
37 #include "gtkappchooserdialog.h"
38 #include "gtkappchooserprivate.h"
39 #include "gtkcelllayout.h"
40 #include "gtkcellrendererpixbuf.h"
41 #include "gtkcellrenderertext.h"
42 #include "gtkcombobox.h"
43 #include "gtkdialog.h"
44 #include "gtkintl.h"
45 #include "gtkmarshalers.h"
46
47 enum {
48   PROP_CONTENT_TYPE = 1,
49   PROP_SHOW_DIALOG_ITEM,
50 };
51
52 enum {
53   SIGNAL_CUSTOM_ITEM_ACTIVATED,
54   NUM_SIGNALS
55 };
56
57 enum {
58   COLUMN_APP_INFO,
59   COLUMN_NAME,
60   COLUMN_LABEL,
61   COLUMN_ICON,
62   COLUMN_CUSTOM,
63   COLUMN_SEPARATOR,
64   NUM_COLUMNS,
65 };
66
67 #define CUSTOM_ITEM_OTHER_APP "gtk-internal-item-other-app"
68
69 static void app_chooser_iface_init (GtkAppChooserIface *iface);
70
71 static void real_insert_custom_item (GtkAppChooserButton *self,
72                                      const gchar *name,
73                                      const gchar *label,
74                                      GIcon *icon,
75                                      gboolean custom,
76                                      GtkTreeIter *iter);
77
78 static void real_insert_separator (GtkAppChooserButton *self,
79                                    gboolean custom,
80                                    GtkTreeIter *iter);
81
82 static guint signals[NUM_SIGNALS] = { 0, };
83
84 G_DEFINE_TYPE_WITH_CODE (GtkAppChooserButton, gtk_app_chooser_button, GTK_TYPE_COMBO_BOX,
85                          G_IMPLEMENT_INTERFACE (GTK_TYPE_APP_CHOOSER,
86                                                 app_chooser_iface_init));
87
88 struct _GtkAppChooserButtonPrivate {
89   GtkListStore *store;
90
91   gchar *content_type;
92   gboolean show_dialog_item;
93
94   GHashTable *custom_item_names;
95 };
96
97 static gboolean
98 row_separator_func (GtkTreeModel *model,
99                     GtkTreeIter  *iter,
100                     gpointer      user_data)
101 {
102   gboolean separator;
103
104   gtk_tree_model_get (model, iter,
105                       COLUMN_SEPARATOR, &separator,
106                       -1);
107
108   return separator;
109 }
110
111 static void
112 get_first_iter (GtkListStore *store,
113                 GtkTreeIter  *iter)
114 {
115   GtkTreeIter iter2;
116
117   if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), iter))
118     {
119       /* the model is empty, append */
120       gtk_list_store_append (store, iter);
121     }
122   else
123     {
124       gtk_list_store_insert_before (store, &iter2, iter);
125       *iter = iter2;
126     }
127 }
128
129 typedef struct {
130   GtkAppChooserButton *self;
131   GAppInfo *info;
132   gint active_index;
133 } SelectAppData;
134
135 static void
136 select_app_data_free (SelectAppData *data)
137 {
138   g_clear_object (&data->self);
139   g_clear_object (&data->info);
140
141   g_slice_free (SelectAppData, data);
142 }
143
144 static gboolean
145 select_application_func_cb (GtkTreeModel *model,
146                             GtkTreePath *path,
147                             GtkTreeIter *iter,
148                             gpointer user_data)
149 {
150   SelectAppData *data = user_data;
151   GAppInfo *app_to_match = data->info, *app = NULL;
152   gboolean custom;
153
154   gtk_tree_model_get (model, iter,
155                       COLUMN_APP_INFO, &app,
156                       COLUMN_CUSTOM, &custom,
157                       -1);
158
159   /* cutsom items are always after GAppInfos, so iterating further here
160    * is just useless.
161    */
162   if (custom)
163     return TRUE;
164
165   if (g_app_info_equal (app, app_to_match))
166     {
167       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (data->self), iter);
168       return TRUE;
169     }
170
171   return FALSE;
172 }
173
174 static void
175 gtk_app_chooser_button_select_application (GtkAppChooserButton *self,
176                                            GAppInfo *info)
177 {
178   SelectAppData *data;
179
180   data = g_slice_new0 (SelectAppData);
181   data->self = g_object_ref (self);
182   data->info = g_object_ref (info);
183
184   gtk_tree_model_foreach (GTK_TREE_MODEL (self->priv->store),
185                           select_application_func_cb, data);
186
187   select_app_data_free (data);
188 }
189
190 static void
191 other_application_dialog_response_cb (GtkDialog *dialog,
192                                       gint response_id,
193                                       gpointer user_data)
194 {
195   GtkAppChooserButton *self = user_data;
196   GAppInfo *info;
197
198   if (response_id != GTK_RESPONSE_OK)
199     {
200       /* reset the active item, otherwise we are stuck on
201        * 'Other application...'
202        */
203       gtk_combo_box_set_active (GTK_COMBO_BOX (self), 0);
204       gtk_widget_destroy (GTK_WIDGET (dialog));
205       return;
206     }
207
208   info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (dialog));
209
210   /* refresh the combobox to get the new application */
211   gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));
212   gtk_app_chooser_button_select_application (self, info);
213
214   g_object_unref (info);
215 }
216
217 static void
218 other_application_item_activated_cb (GtkAppChooserButton *self)
219 {
220   GtkWidget *dialog, *widget;
221   GtkWindow *toplevel;
222
223   toplevel = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (self)));
224   dialog = gtk_app_chooser_dialog_new_for_content_type (toplevel, GTK_DIALOG_DESTROY_WITH_PARENT,
225                                                         self->priv->content_type);
226   widget = gtk_app_chooser_dialog_get_widget (GTK_APP_CHOOSER_DIALOG (dialog));
227   g_object_set (widget,
228                 "show-fallback", TRUE,
229                 "show-other", TRUE,
230                 NULL);
231   gtk_widget_show (dialog);
232
233   g_signal_connect (dialog, "response",
234                     G_CALLBACK (other_application_dialog_response_cb), self);
235 }
236
237 static void
238 gtk_app_chooser_button_ensure_dialog_item (GtkAppChooserButton *self,
239                                            GtkTreeIter *prev_iter)
240 {
241   GIcon *icon;
242   GtkTreeIter iter, iter2;
243
244   if (!self->priv->show_dialog_item)
245     return;
246
247   icon = g_themed_icon_new ("application-x-executable");
248
249   if (prev_iter == NULL)
250     gtk_list_store_append (self->priv->store, &iter);
251   else
252     gtk_list_store_insert_after (self->priv->store, &iter, prev_iter);
253
254   real_insert_separator (self, FALSE, &iter);
255   iter2 = iter;
256
257   gtk_list_store_insert_after (self->priv->store, &iter, &iter2);
258   real_insert_custom_item (self, CUSTOM_ITEM_OTHER_APP,
259                            _("Other application..."), icon,
260                            FALSE, &iter);
261
262   g_object_unref (icon);
263 }
264
265 static void
266 gtk_app_chooser_button_populate (GtkAppChooserButton *self)
267 {
268   GList *recommended_apps = NULL, *l;
269   GAppInfo *app;
270   GtkTreeIter iter, iter2;
271   GIcon *icon;
272   gboolean cycled_recommended;
273
274   recommended_apps = g_app_info_get_recommended_for_type (self->priv->content_type);
275   cycled_recommended = FALSE;
276
277   for (l = recommended_apps; l != NULL; l = l->next)
278     {
279       app = l->data;
280
281       icon = g_app_info_get_icon (app);
282
283       if (icon == NULL)
284         icon = g_themed_icon_new ("application-x-executable");
285       else
286         g_object_ref (icon);
287
288       if (cycled_recommended)
289         {
290           gtk_list_store_insert_after (self->priv->store, &iter2, &iter);
291           iter = iter2;
292         }
293       else
294         {
295           get_first_iter (self->priv->store, &iter);
296           cycled_recommended = TRUE;
297         }
298
299       gtk_list_store_set (self->priv->store, &iter,
300                           COLUMN_APP_INFO, app,
301                           COLUMN_LABEL, g_app_info_get_display_name (app),
302                           COLUMN_ICON, icon,
303                           COLUMN_CUSTOM, FALSE,
304                           -1);
305
306       g_object_unref (icon);
307     }
308
309   if (!cycled_recommended)
310     gtk_app_chooser_button_ensure_dialog_item (self, NULL);
311   else
312     gtk_app_chooser_button_ensure_dialog_item (self, &iter);
313
314   gtk_combo_box_set_active (GTK_COMBO_BOX (self), 0);
315 }
316
317 static void
318 gtk_app_chooser_button_build_ui (GtkAppChooserButton *self)
319 {
320   GtkCellRenderer *cell;
321
322   self->priv->store = gtk_list_store_new (NUM_COLUMNS,
323                                           G_TYPE_APP_INFO,
324                                           G_TYPE_STRING, /* name */
325                                           G_TYPE_STRING, /* label */
326                                           G_TYPE_ICON,
327                                           G_TYPE_BOOLEAN, /* separator */
328                                           G_TYPE_BOOLEAN); /* custom */
329
330   gtk_combo_box_set_model (GTK_COMBO_BOX (self),
331                            GTK_TREE_MODEL (self->priv->store));
332
333   gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
334                                         row_separator_func, NULL, NULL);
335
336   cell = gtk_cell_renderer_pixbuf_new ();
337   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), cell, FALSE);
338   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self), cell,
339                                   "gicon", COLUMN_ICON,
340                                   NULL);
341
342   cell = gtk_cell_renderer_text_new ();
343   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), cell, TRUE);
344   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self), cell,
345                                   "text", COLUMN_LABEL,
346                                   NULL);
347   g_object_set (cell,
348                 "xpad", 6,
349                 NULL);
350
351   gtk_app_chooser_button_populate (self);
352 }
353
354 static void
355 gtk_app_chooser_button_remove_non_custom (GtkAppChooserButton *self)
356 {
357   GtkTreeModel *model;
358   GtkTreeIter iter;
359   gboolean custom, res;
360
361   model = GTK_TREE_MODEL (self->priv->store);
362
363   if (!gtk_tree_model_get_iter_first (model, &iter))
364     return;
365
366   do {
367     gtk_tree_model_get (model, &iter,
368                         COLUMN_CUSTOM, &custom,
369                         -1);
370     if (custom)
371       res = gtk_tree_model_iter_next (model, &iter);
372     else
373       res = gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
374   } while (res);
375 }
376
377 static void
378 gtk_app_chooser_button_changed (GtkComboBox *object)
379 {
380   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
381   GtkTreeIter iter;
382   gchar *name = NULL;
383   gboolean custom;
384   GQuark name_quark;
385
386   if (!gtk_combo_box_get_active_iter (object, &iter))
387     return;
388
389   gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
390                       COLUMN_NAME, &name,
391                       COLUMN_CUSTOM, &custom,
392                       -1);
393
394   if (name != NULL)
395     {
396       if (custom)
397         {
398           name_quark = g_quark_from_string (name);
399           g_signal_emit (self, signals[SIGNAL_CUSTOM_ITEM_ACTIVATED], name_quark, name);
400         }
401       else
402         {
403           /* trigger the dialog internally */
404           other_application_item_activated_cb (self);
405         }
406
407       g_free (name);
408     }
409 }
410
411 static void
412 gtk_app_chooser_button_refresh (GtkAppChooser *object)
413 {
414   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
415
416   gtk_app_chooser_button_remove_non_custom (self);
417   gtk_app_chooser_button_populate (self);
418 }
419
420 static GAppInfo *
421 gtk_app_chooser_button_get_app_info (GtkAppChooser *object)
422 {
423   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
424   GtkTreeIter iter;
425   GAppInfo *info;
426
427   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter))
428     return NULL;
429
430   gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
431                       COLUMN_APP_INFO, &info,
432                       -1);
433
434   return info;
435 }
436
437 static void
438 gtk_app_chooser_button_constructed (GObject *obj)
439 {
440   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
441
442   if (G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->constructed != NULL)
443     G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->constructed (obj);
444
445   g_assert (self->priv->content_type != NULL);
446
447   gtk_app_chooser_button_build_ui (self);
448 }
449
450 static void
451 gtk_app_chooser_button_set_property (GObject      *obj,
452                                      guint         property_id,
453                                      const GValue *value,
454                                      GParamSpec   *pspec)
455 {
456   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
457
458   switch (property_id)
459     {
460     case PROP_CONTENT_TYPE:
461       self->priv->content_type = g_value_dup_string (value);
462       break;
463     case PROP_SHOW_DIALOG_ITEM:
464       gtk_app_chooser_button_set_show_dialog_item (self, g_value_get_boolean (value));
465       break;
466     default:
467       G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
468       break;
469     }
470 }
471
472 static void
473 gtk_app_chooser_button_get_property (GObject    *obj,
474                                      guint       property_id,
475                                      GValue     *value,
476                                      GParamSpec *pspec)
477 {
478   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
479
480   switch (property_id)
481     {
482     case PROP_CONTENT_TYPE:
483       g_value_set_string (value, self->priv->content_type);
484       break;
485     case PROP_SHOW_DIALOG_ITEM:
486       g_value_set_boolean (value, self->priv->show_dialog_item);
487       break;
488     default:
489       G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
490       break;
491     }
492 }
493
494 static void
495 gtk_app_chooser_button_finalize (GObject *obj)
496 {
497   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
498
499   g_hash_table_destroy (self->priv->custom_item_names);
500   g_free (self->priv->content_type);
501
502   G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->finalize (obj);
503 }
504
505 static void
506 app_chooser_iface_init (GtkAppChooserIface *iface)
507 {
508   iface->get_app_info = gtk_app_chooser_button_get_app_info;
509   iface->refresh = gtk_app_chooser_button_refresh;
510 }
511
512 static void
513 gtk_app_chooser_button_class_init (GtkAppChooserButtonClass *klass)
514 {
515   GObjectClass *oclass = G_OBJECT_CLASS (klass);
516   GtkComboBoxClass *combo_class = GTK_COMBO_BOX_CLASS (klass);
517   GParamSpec *pspec;
518
519   oclass->set_property = gtk_app_chooser_button_set_property;
520   oclass->get_property = gtk_app_chooser_button_get_property;
521   oclass->finalize = gtk_app_chooser_button_finalize;
522   oclass->constructed = gtk_app_chooser_button_constructed;
523
524   combo_class->changed = gtk_app_chooser_button_changed;
525
526   g_object_class_override_property (oclass, PROP_CONTENT_TYPE, "content-type");
527
528   /**
529    * GtkAppChooserButton:show-dialog-item:
530    *
531    * The #GtkAppChooserButton:show-dialog-item property determines whether the dropdown menu
532    * should show an item that triggers a #GtkAppChooserDialog when clicked.
533    */
534   pspec =
535     g_param_spec_boolean ("show-dialog-item",
536                           P_("Include an 'Other...' item"),
537                           P_("Whether the combobox should include an item that triggers a GtkAppChooserDialog"),
538                           FALSE,
539                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
540   g_object_class_install_property (oclass, PROP_SHOW_DIALOG_ITEM, pspec);
541
542   /**
543    * GtkAppChooserButton::custom-item-activated:
544    * @self: the object which received the signal
545    * @item_name: the name of the activated item
546    *
547    * Emitted when a custom item, previously added with
548    * gtk_app_chooser_button_append_custom_item(), is activated from the
549    * dropdown menu.
550    */
551   signals[SIGNAL_CUSTOM_ITEM_ACTIVATED] =
552     g_signal_new ("custom-item-activated",
553                   GTK_TYPE_APP_CHOOSER_BUTTON,
554                   G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
555                   G_STRUCT_OFFSET (GtkAppChooserButtonClass, custom_item_activated),
556                   NULL, NULL,
557                   _gtk_marshal_VOID__STRING,
558                   G_TYPE_NONE,
559                   1, G_TYPE_STRING);
560
561   g_type_class_add_private (klass, sizeof (GtkAppChooserButtonPrivate));
562 }
563
564 static void
565 gtk_app_chooser_button_init (GtkAppChooserButton *self)
566 {
567   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_APP_CHOOSER_BUTTON,
568                                             GtkAppChooserButtonPrivate);
569   self->priv->custom_item_names =
570     g_hash_table_new_full (g_str_hash, g_str_equal,
571                            g_free, NULL);
572 }
573
574 static gboolean
575 app_chooser_button_iter_from_custom_name (GtkAppChooserButton *self,
576                                           const gchar *name,
577                                           GtkTreeIter *set_me)
578 {
579   GtkTreeIter iter;
580   gchar *custom_name = NULL;
581
582   if (!gtk_tree_model_get_iter_first
583       (GTK_TREE_MODEL (self->priv->store), &iter))
584     return FALSE;
585
586   do {
587     gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
588                         COLUMN_NAME, &custom_name,
589                         -1);
590
591     if (g_strcmp0 (custom_name, name) == 0)
592       {
593         g_free (custom_name);
594         *set_me = iter;
595
596         return TRUE;
597       }
598
599     g_free (custom_name);
600   } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (self->priv->store), &iter));
601
602   return FALSE;
603 }
604
605 static void
606 real_insert_custom_item (GtkAppChooserButton *self,
607                          const gchar *name,
608                          const gchar *label,
609                          GIcon *icon,
610                          gboolean custom,
611                          GtkTreeIter *iter)
612 {
613   if (custom)
614     {
615       if (g_hash_table_lookup (self->priv->custom_item_names,
616                                name) != NULL)
617         {
618           g_warning ("Attempting to add custom item %s to GtkAppChooserButton, "
619                      "when there's already an item with the same name", name);
620           return;
621         }
622
623       g_hash_table_insert (self->priv->custom_item_names,
624                            g_strdup (name), GINT_TO_POINTER (1));
625     }
626
627   gtk_list_store_set (self->priv->store, iter,
628                       COLUMN_NAME, name,
629                       COLUMN_LABEL, label,
630                       COLUMN_ICON, icon,
631                       COLUMN_CUSTOM, custom,
632                       COLUMN_SEPARATOR, FALSE,
633                       -1);
634 }
635
636 static void
637 real_insert_separator (GtkAppChooserButton *self,
638                        gboolean custom,
639                        GtkTreeIter *iter)
640 {
641   gtk_list_store_set (self->priv->store, iter,
642                       COLUMN_CUSTOM, custom,
643                       COLUMN_SEPARATOR, TRUE,
644                       -1);
645 }
646
647 /**
648  * gtk_app_chooser_button_new:
649  * @content_type: the content type to show applications for
650  *
651  * Creates a new #GtkAppChooserButton for applications
652  * that can handle content of the given type.
653  *
654  * Returns: a newly created #GtkAppChooserButton
655  *
656  * Since: 3.0
657  */
658 GtkWidget *
659 gtk_app_chooser_button_new (const gchar *content_type)
660 {
661   g_return_val_if_fail (content_type != NULL, NULL);
662
663   return g_object_new (GTK_TYPE_APP_CHOOSER_BUTTON,
664                        "content-type", content_type,
665                        NULL);
666 }
667
668 /**
669  * gtk_app_chooser_button_append_separator:
670  * @self: a #GtkAppChooserButton
671  *
672  * Appends a separator to the list of applications that is shown
673  * in the popup.
674  *
675  * Since: 3.0
676  */
677 void
678 gtk_app_chooser_button_append_separator (GtkAppChooserButton *self)
679 {
680   GtkTreeIter iter;
681
682   g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
683
684   gtk_list_store_append (self->priv->store, &iter);
685   real_insert_separator (self, TRUE, &iter);
686 }
687
688 /**
689  * gtk_app_chooser_button_append_custom_item:
690  * @self: a #GtkAppChooserButton
691  * @name: the name of the custom item
692  * @label: the label for the custom item
693  * @icon: the icon for the custom item
694  *
695  * Appends a custom item to the list of applications that is shown
696  * in the popup; the item name must be unique per-widget.
697  * Clients can use the provided name as a detail for the ::custom-item-activated
698  * signal, to add a callback for the activation of a particular
699  * custom item in the list.
700  * See also gtk_app_chooser_button_append_separator().
701  *
702  * Since: 3.0
703  */
704 void
705 gtk_app_chooser_button_append_custom_item (GtkAppChooserButton *self,
706                                            const gchar         *name,
707                                            const gchar         *label,
708                                            GIcon               *icon)
709 {
710   GtkTreeIter iter;
711
712   g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
713   g_return_if_fail (name != NULL);
714
715   gtk_list_store_append (self->priv->store, &iter);
716   real_insert_custom_item (self, name, label, icon, TRUE, &iter);
717 }
718
719 /**
720  * gtk_app_chooser_button_select_custom_item:
721  * @self: a #GtkAppChooserButton
722  * @name: the name of the custom item
723  *
724  * Selects a custom item previously added with
725  * gtk_app_chooser_button_append_custom_item().
726  * Use gtk_app_chooser_refresh() to bring the selection to its initial
727  * state.
728  *
729  * Since: 3.0
730  */
731 void
732 gtk_app_chooser_button_set_active_custom_item (GtkAppChooserButton *self,
733                                                const gchar         *name)
734 {
735   GtkTreeIter iter;
736
737   g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
738   g_return_if_fail (name != NULL);
739
740   if (g_hash_table_lookup (self->priv->custom_item_names, name) == NULL ||
741       !app_chooser_button_iter_from_custom_name (self, name, &iter))
742     {
743       g_warning ("Can't find the item named %s in the app chooser.",
744                  name);
745       return;
746     }
747
748   gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), &iter);
749 }
750
751 /**
752  * gtk_app_chooser_button_get_show_dialog_item:
753  * @self: a #GtkAppChooserButton
754  *
755  * Returns the current value of the #GtkAppChooserButton:show-dialog-item
756  * property.
757  *
758  * Returns: the value of #GtkAppChooserButton:show-dialog-item
759  *
760  * Since: 3.0
761  */
762 gboolean
763 gtk_app_chooser_button_get_show_dialog_item (GtkAppChooserButton *self)
764 {
765   g_return_val_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self), FALSE);
766
767   return self->priv->show_dialog_item;
768 }
769
770 /**
771  * gtk_app_chooser_button_set_show_dialog_item:
772  * @self: a #GtkAppChooserButton
773  * @setting: the new value for #GtkAppChooserButton:show-dialog-item
774  *
775  * Sets whether the dropdown menu of this button should show an
776  * entry to trigger a #GtkAppChooserDialog.
777  *
778  * Since: 3.0
779  */
780 void
781 gtk_app_chooser_button_set_show_dialog_item (GtkAppChooserButton *self,
782                                              gboolean setting)
783 {
784   if (self->priv->show_dialog_item != setting)
785     {
786       self->priv->show_dialog_item = setting;
787
788       g_object_notify (G_OBJECT (self), "show-dialog-item");
789
790       gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));
791     }
792 }