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