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