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