2 * gtkappchooserbutton.h: an app-chooser combobox
4 * Copyright (C) 2010 Red Hat, Inc.
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.
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.
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.
21 * Authors: Cosimo Cecchi <ccecchi@redhat.com>
25 * SECTION:gtkappchooserbutton
26 * @Title: GtkAppChooserButton
27 * @Short_description: A button to launch an application chooser dialog
29 * The #GtkAppChooserButton is a widget that lets the user select
30 * an application. It implements the #GtkAppChooser interface.
34 #include "gtkappchooserbutton.h"
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"
45 #include "gtkmarshalers.h"
48 PROP_CONTENT_TYPE = 1,
49 PROP_SHOW_DIALOG_ITEM,
54 SIGNAL_CUSTOM_ITEM_ACTIVATED,
68 #define CUSTOM_ITEM_OTHER_APP "gtk-internal-item-other-app"
70 static void app_chooser_iface_init (GtkAppChooserIface *iface);
72 static void real_insert_custom_item (GtkAppChooserButton *self,
79 static void real_insert_separator (GtkAppChooserButton *self,
83 static guint signals[NUM_SIGNALS] = { 0, };
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));
89 struct _GtkAppChooserButtonPrivate {
94 gboolean show_dialog_item;
97 GHashTable *custom_item_names;
101 row_separator_func (GtkTreeModel *model,
107 gtk_tree_model_get (model, iter,
108 COLUMN_SEPARATOR, &separator,
115 get_first_iter (GtkListStore *store,
120 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), iter))
122 /* the model is empty, append */
123 gtk_list_store_append (store, iter);
127 gtk_list_store_insert_before (store, &iter2, iter);
133 GtkAppChooserButton *self;
139 select_app_data_free (SelectAppData *data)
141 g_clear_object (&data->self);
142 g_clear_object (&data->info);
144 g_slice_free (SelectAppData, data);
148 select_application_func_cb (GtkTreeModel *model,
153 SelectAppData *data = user_data;
154 GAppInfo *app_to_match = data->info, *app = NULL;
157 gtk_tree_model_get (model, iter,
158 COLUMN_APP_INFO, &app,
159 COLUMN_CUSTOM, &custom,
162 /* cutsom items are always after GAppInfos, so iterating further here
168 if (g_app_info_equal (app, app_to_match))
170 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (data->self), iter);
178 gtk_app_chooser_button_select_application (GtkAppChooserButton *self,
183 data = g_slice_new0 (SelectAppData);
184 data->self = g_object_ref (self);
185 data->info = g_object_ref (info);
187 gtk_tree_model_foreach (GTK_TREE_MODEL (self->priv->store),
188 select_application_func_cb, data);
190 select_app_data_free (data);
194 other_application_dialog_response_cb (GtkDialog *dialog,
198 GtkAppChooserButton *self = user_data;
201 if (response_id != GTK_RESPONSE_OK)
203 /* reset the active item, otherwise we are stuck on
204 * 'Other application...'
206 gtk_combo_box_set_active (GTK_COMBO_BOX (self), self->priv->last_active);
207 gtk_widget_destroy (GTK_WIDGET (dialog));
211 info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (dialog));
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);
217 g_object_unref (info);
221 other_application_item_activated_cb (GtkAppChooserButton *self)
223 GtkWidget *dialog, *widget;
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);
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);
234 widget = gtk_app_chooser_dialog_get_widget (GTK_APP_CHOOSER_DIALOG (dialog));
235 g_object_set (widget,
236 "show-fallback", TRUE,
239 gtk_widget_show (dialog);
241 g_signal_connect (dialog, "response",
242 G_CALLBACK (other_application_dialog_response_cb), self);
246 gtk_app_chooser_button_ensure_dialog_item (GtkAppChooserButton *self,
247 GtkTreeIter *prev_iter)
249 GtkTreeIter iter, iter2;
251 if (!self->priv->show_dialog_item)
254 if (prev_iter == NULL)
255 gtk_list_store_append (self->priv->store, &iter);
257 gtk_list_store_insert_after (self->priv->store, &iter, prev_iter);
259 real_insert_separator (self, FALSE, &iter);
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,
269 gtk_app_chooser_button_populate (GtkAppChooserButton *self)
271 GList *recommended_apps = NULL, *l;
273 GtkTreeIter iter, iter2;
275 gboolean cycled_recommended;
278 recommended_apps = g_app_info_get_recommended_for_type (self->priv->content_type);
280 cycled_recommended = FALSE;
282 for (l = recommended_apps; l != NULL; l = l->next)
286 icon = g_app_info_get_icon (app);
289 icon = g_themed_icon_new ("application-x-executable");
293 if (cycled_recommended)
295 gtk_list_store_insert_after (self->priv->store, &iter2, &iter);
300 get_first_iter (self->priv->store, &iter);
301 cycled_recommended = TRUE;
304 gtk_list_store_set (self->priv->store, &iter,
305 COLUMN_APP_INFO, app,
306 COLUMN_LABEL, g_app_info_get_name (app),
308 COLUMN_CUSTOM, FALSE,
311 g_object_unref (icon);
314 if (!cycled_recommended)
315 gtk_app_chooser_button_ensure_dialog_item (self, NULL);
317 gtk_app_chooser_button_ensure_dialog_item (self, &iter);
319 gtk_combo_box_set_active (GTK_COMBO_BOX (self), 0);
323 gtk_app_chooser_button_build_ui (GtkAppChooserButton *self)
325 GtkCellRenderer *cell;
328 self->priv->store = gtk_list_store_new (NUM_COLUMNS,
330 G_TYPE_STRING, /* name */
331 G_TYPE_STRING, /* label */
333 G_TYPE_BOOLEAN, /* separator */
334 G_TYPE_BOOLEAN); /* custom */
336 gtk_combo_box_set_model (GTK_COMBO_BOX (self),
337 GTK_TREE_MODEL (self->priv->store));
339 area = gtk_cell_layout_get_area (GTK_CELL_LAYOUT (self));
341 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
342 row_separator_func, NULL, NULL);
344 cell = gtk_cell_renderer_pixbuf_new ();
345 gtk_cell_area_add_with_properties (area, cell,
350 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self), cell,
351 "gicon", COLUMN_ICON,
354 cell = gtk_cell_renderer_text_new ();
355 gtk_cell_area_add_with_properties (area, cell,
359 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self), cell,
360 "text", COLUMN_LABEL,
363 gtk_app_chooser_button_populate (self);
367 gtk_app_chooser_button_remove_non_custom (GtkAppChooserButton *self)
371 gboolean custom, res;
373 model = GTK_TREE_MODEL (self->priv->store);
375 if (!gtk_tree_model_get_iter_first (model, &iter))
379 gtk_tree_model_get (model, &iter,
380 COLUMN_CUSTOM, &custom,
383 res = gtk_tree_model_iter_next (model, &iter);
385 res = gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
390 gtk_app_chooser_button_changed (GtkComboBox *object)
392 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
398 if (!gtk_combo_box_get_active_iter (object, &iter))
401 gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
403 COLUMN_CUSTOM, &custom,
410 name_quark = g_quark_from_string (name);
411 g_signal_emit (self, signals[SIGNAL_CUSTOM_ITEM_ACTIVATED], name_quark, name);
412 self->priv->last_active = gtk_combo_box_get_active (object);
416 /* trigger the dialog internally */
417 other_application_item_activated_cb (self);
423 self->priv->last_active = gtk_combo_box_get_active (object);
427 gtk_app_chooser_button_refresh (GtkAppChooser *object)
429 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
431 gtk_app_chooser_button_remove_non_custom (self);
432 gtk_app_chooser_button_populate (self);
436 gtk_app_chooser_button_get_app_info (GtkAppChooser *object)
438 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
442 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter))
445 gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
446 COLUMN_APP_INFO, &info,
453 gtk_app_chooser_button_constructed (GObject *obj)
455 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
457 if (G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->constructed != NULL)
458 G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->constructed (obj);
460 g_assert (self->priv->content_type != NULL);
462 gtk_app_chooser_button_build_ui (self);
466 gtk_app_chooser_button_set_property (GObject *obj,
471 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
475 case PROP_CONTENT_TYPE:
476 self->priv->content_type = g_value_dup_string (value);
478 case PROP_SHOW_DIALOG_ITEM:
479 gtk_app_chooser_button_set_show_dialog_item (self, g_value_get_boolean (value));
482 gtk_app_chooser_button_set_heading (self, g_value_get_string (value));
485 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
491 gtk_app_chooser_button_get_property (GObject *obj,
496 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
500 case PROP_CONTENT_TYPE:
501 g_value_set_string (value, self->priv->content_type);
503 case PROP_SHOW_DIALOG_ITEM:
504 g_value_set_boolean (value, self->priv->show_dialog_item);
507 g_value_set_string (value, self->priv->heading);
510 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
516 gtk_app_chooser_button_finalize (GObject *obj)
518 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
520 g_hash_table_destroy (self->priv->custom_item_names);
521 g_free (self->priv->content_type);
522 g_free (self->priv->heading);
524 G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->finalize (obj);
528 app_chooser_iface_init (GtkAppChooserIface *iface)
530 iface->get_app_info = gtk_app_chooser_button_get_app_info;
531 iface->refresh = gtk_app_chooser_button_refresh;
535 gtk_app_chooser_button_class_init (GtkAppChooserButtonClass *klass)
537 GObjectClass *oclass = G_OBJECT_CLASS (klass);
538 GtkComboBoxClass *combo_class = GTK_COMBO_BOX_CLASS (klass);
541 oclass->set_property = gtk_app_chooser_button_set_property;
542 oclass->get_property = gtk_app_chooser_button_get_property;
543 oclass->finalize = gtk_app_chooser_button_finalize;
544 oclass->constructed = gtk_app_chooser_button_constructed;
546 combo_class->changed = gtk_app_chooser_button_changed;
548 g_object_class_override_property (oclass, PROP_CONTENT_TYPE, "content-type");
551 * GtkAppChooserButton:show-dialog-item:
553 * The #GtkAppChooserButton:show-dialog-item property determines whether the dropdown menu
554 * should show an item that triggers a #GtkAppChooserDialog when clicked.
557 g_param_spec_boolean ("show-dialog-item",
558 P_("Include an 'Other...' item"),
559 P_("Whether the combobox should include an item that triggers a GtkAppChooserDialog"),
561 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
562 g_object_class_install_property (oclass, PROP_SHOW_DIALOG_ITEM, pspec);
565 * GtkAppChooserButton:heading:
567 * The text to show at the top of the dialog that can be
568 * opened from the button. The string may contain Pango markup.
570 pspec = g_param_spec_string ("heading",
572 P_("The text to show at the top of the dialog"),
574 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
575 g_object_class_install_property (oclass, PROP_HEADING, pspec);
579 * GtkAppChooserButton::custom-item-activated:
580 * @self: the object which received the signal
581 * @item_name: the name of the activated item
583 * Emitted when a custom item, previously added with
584 * gtk_app_chooser_button_append_custom_item(), is activated from the
587 signals[SIGNAL_CUSTOM_ITEM_ACTIVATED] =
588 g_signal_new ("custom-item-activated",
589 GTK_TYPE_APP_CHOOSER_BUTTON,
590 G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
591 G_STRUCT_OFFSET (GtkAppChooserButtonClass, custom_item_activated),
593 _gtk_marshal_VOID__STRING,
597 g_type_class_add_private (klass, sizeof (GtkAppChooserButtonPrivate));
601 gtk_app_chooser_button_init (GtkAppChooserButton *self)
603 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_APP_CHOOSER_BUTTON,
604 GtkAppChooserButtonPrivate);
605 self->priv->custom_item_names =
606 g_hash_table_new_full (g_str_hash, g_str_equal,
611 app_chooser_button_iter_from_custom_name (GtkAppChooserButton *self,
616 gchar *custom_name = NULL;
618 if (!gtk_tree_model_get_iter_first
619 (GTK_TREE_MODEL (self->priv->store), &iter))
623 gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
624 COLUMN_NAME, &custom_name,
627 if (g_strcmp0 (custom_name, name) == 0)
629 g_free (custom_name);
635 g_free (custom_name);
636 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (self->priv->store), &iter));
642 real_insert_custom_item (GtkAppChooserButton *self,
651 if (g_hash_table_lookup (self->priv->custom_item_names,
654 g_warning ("Attempting to add custom item %s to GtkAppChooserButton, "
655 "when there's already an item with the same name", name);
659 g_hash_table_insert (self->priv->custom_item_names,
660 g_strdup (name), GINT_TO_POINTER (1));
663 gtk_list_store_set (self->priv->store, iter,
667 COLUMN_CUSTOM, custom,
668 COLUMN_SEPARATOR, FALSE,
673 real_insert_separator (GtkAppChooserButton *self,
677 gtk_list_store_set (self->priv->store, iter,
678 COLUMN_CUSTOM, custom,
679 COLUMN_SEPARATOR, TRUE,
684 * gtk_app_chooser_button_new:
685 * @content_type: the content type to show applications for
687 * Creates a new #GtkAppChooserButton for applications
688 * that can handle content of the given type.
690 * Returns: a newly created #GtkAppChooserButton
695 gtk_app_chooser_button_new (const gchar *content_type)
697 g_return_val_if_fail (content_type != NULL, NULL);
699 return g_object_new (GTK_TYPE_APP_CHOOSER_BUTTON,
700 "content-type", content_type,
705 * gtk_app_chooser_button_append_separator:
706 * @self: a #GtkAppChooserButton
708 * Appends a separator to the list of applications that is shown
714 gtk_app_chooser_button_append_separator (GtkAppChooserButton *self)
718 g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
720 gtk_list_store_append (self->priv->store, &iter);
721 real_insert_separator (self, TRUE, &iter);
725 * gtk_app_chooser_button_append_custom_item:
726 * @self: a #GtkAppChooserButton
727 * @name: the name of the custom item
728 * @label: the label for the custom item
729 * @icon: the icon for the custom item
731 * Appends a custom item to the list of applications that is shown
732 * in the popup; the item name must be unique per-widget.
733 * Clients can use the provided name as a detail for the ::custom-item-activated
734 * signal, to add a callback for the activation of a particular
735 * custom item in the list.
736 * See also gtk_app_chooser_button_append_separator().
741 gtk_app_chooser_button_append_custom_item (GtkAppChooserButton *self,
748 g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
749 g_return_if_fail (name != NULL);
751 gtk_list_store_append (self->priv->store, &iter);
752 real_insert_custom_item (self, name, label, icon, TRUE, &iter);
756 * gtk_app_chooser_button_set_active_custom_item:
757 * @self: a #GtkAppChooserButton
758 * @name: the name of the custom item
760 * Selects a custom item previously added with
761 * gtk_app_chooser_button_append_custom_item().
763 * Use gtk_app_chooser_refresh() to bring the selection
764 * to its initial state.
769 gtk_app_chooser_button_set_active_custom_item (GtkAppChooserButton *self,
774 g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
775 g_return_if_fail (name != NULL);
777 if (g_hash_table_lookup (self->priv->custom_item_names, name) == NULL ||
778 !app_chooser_button_iter_from_custom_name (self, name, &iter))
780 g_warning ("Can't find the item named %s in the app chooser.",
785 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), &iter);
789 * gtk_app_chooser_button_get_show_dialog_item:
790 * @self: a #GtkAppChooserButton
792 * Returns the current value of the #GtkAppChooserButton:show-dialog-item
795 * Returns: the value of #GtkAppChooserButton:show-dialog-item
800 gtk_app_chooser_button_get_show_dialog_item (GtkAppChooserButton *self)
802 g_return_val_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self), FALSE);
804 return self->priv->show_dialog_item;
808 * gtk_app_chooser_button_set_show_dialog_item:
809 * @self: a #GtkAppChooserButton
810 * @setting: the new value for #GtkAppChooserButton:show-dialog-item
812 * Sets whether the dropdown menu of this button should show an
813 * entry to trigger a #GtkAppChooserDialog.
818 gtk_app_chooser_button_set_show_dialog_item (GtkAppChooserButton *self,
821 if (self->priv->show_dialog_item != setting)
823 self->priv->show_dialog_item = setting;
825 g_object_notify (G_OBJECT (self), "show-dialog-item");
827 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));
832 * gtk_app_chooser_button_set_heading:
833 * @self: a #GtkAppChooserButton
834 * @heading: a string containing Pango markup
836 * Sets the text to display at the top of the dialog.
837 * If the heading is not set, the dialog displays a default text.
840 gtk_app_chooser_button_set_heading (GtkAppChooserButton *self,
841 const gchar *heading)
843 g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
845 g_free (self->priv->heading);
846 self->priv->heading = g_strdup (heading);
848 g_object_notify (G_OBJECT (self), "heading");
852 * gtk_app_chooser_button_get_heading:
853 * @self: a #GtkAppChooserButton
855 * Returns the text to display at the top of the dialog.
857 * Returns: the text to display at the top of the dialog, or %NULL, in which
858 * case a default text is displayed
861 gtk_app_chooser_button_get_heading (GtkAppChooserButton *self)
863 g_return_val_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self), NULL);
865 return self->priv->heading;