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