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