]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkappchooserwidget.c
filechooserbutton: Update the button/combo at construction time
[~andy/gtk] / gtk / gtkappchooserwidget.c
index 95510e9a35fa7e5fa43bbb3160ff7f7a8552d716..36875dfb0df3b3fa580a4d2dd79f366f1d2215a8 100644 (file)
@@ -15,9 +15,7 @@
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public
- * License along with the Gnome Library; see the file COPYING.LIB.  If not,
- * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  *
  * Authors: Dave Camp <dave@novell.com>
  *          Alexander Larsson <alexl@redhat.com>
 #include <glib/gi18n-lib.h>
 #include <gio/gio.h>
 
+/**
+ * SECTION:gtkappchooserwidget
+ * @Title: GtkAppChooserWidget
+ * @Short_description: Application chooser widget that can be embedded in other widgets
+ *
+ * #GtkAppChooserWidget is a widget for selecting applications.
+ * It is the main building block for #GtkAppChooserDialog. Most
+ * applications only need to use the latter; but you can use
+ * this widget as part of a larger widget if you have special needs.
+ *
+ * #GtkAppChooserWidget offers detailed control over what applications
+ * are shown, using the
+ * #GtkAppChooserWidget:show-default,
+ * #GtkAppChooserWidget:show-recommended,
+ * #GtkAppChooserWidget:show-fallback,
+ * #GtkAppChooserWidget:show-other and
+ * #GtkAppChooserWidget:show-all
+ * properties. See the #GtkAppChooser documentation for more information
+ * about these groups of applications.
+ *
+ * To keep track of the selected application, use the
+ * #GtkAppChooserWidget::application-selected and #GtkAppChooserWidget::application-activated signals.
+ */
+
 struct _GtkAppChooserWidgetPrivate {
   GAppInfo *selected_app_info;
 
   gchar *content_type;
   gchar *default_text;
-  gboolean show_default;
-  gboolean show_recommended;
-  gboolean show_fallback;
-  gboolean show_other;
-  gboolean show_all;
+
+  guint show_default     : 1;
+  guint show_recommended : 1;
+  guint show_fallback    : 1;
+  guint show_other       : 1;
+  guint show_all         : 1;
 
   GtkWidget *program_list;
   GtkListStore *program_list_store;
@@ -183,7 +206,7 @@ widget_button_press_event_cb (GtkWidget      *widget,
 {
   GtkAppChooserWidget *self = user_data;
 
-  if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
+  if (event->button == GDK_BUTTON_SECONDARY && event->type == GDK_BUTTON_PRESS)
     {
       GAppInfo *info;
       GtkWidget *menu;
@@ -402,13 +425,13 @@ gtk_app_chooser_sort_func (GtkTreeModel *model,
   /* they're both recommended/falback or not, so if one is a heading, wins */
   if (a_heading)
     {
-      return -1;
+      retval = -1;
       goto out;
     }
 
   if (b_heading)
     {
-      return 1;
+      retval = 1;
       goto out;
     }
 
@@ -513,6 +536,9 @@ gtk_app_chooser_widget_add_section (GtkAppChooserWidget *self,
           !g_app_info_supports_files (app))
         continue;
 
+      if (!g_app_info_should_show (app))
+        continue;
+
       if (g_list_find_custom (exclude_apps, app,
                               (GCompareFunc) compare_apps_func))
         continue;
@@ -530,11 +556,9 @@ gtk_app_chooser_widget_add_section (GtkAppChooserWidget *self,
           heading_added = TRUE;
         }
 
-      app_string = g_markup_printf_escaped ("<b>%s</b>\n%s",
-                                            g_app_info_get_display_name (app) != NULL ?
-                                            g_app_info_get_display_name (app) : "",
-                                            g_app_info_get_description (app) != NULL ?
-                                            g_app_info_get_description (app) : "");
+      app_string = g_markup_printf_escaped ("%s",
+                                            g_app_info_get_name (app) != NULL ?
+                                            g_app_info_get_name (app) : "");
 
       icon = g_app_info_get_icon (app);
       unref_icon = FALSE;
@@ -548,7 +572,7 @@ gtk_app_chooser_widget_add_section (GtkAppChooserWidget *self,
       gtk_list_store_set (self->priv->program_list_store, &iter,
                           COLUMN_APP_INFO, app,
                           COLUMN_GICON, icon,
-                          COLUMN_NAME, g_app_info_get_display_name (app),
+                          COLUMN_NAME, g_app_info_get_name (app),
                           COLUMN_DESC, app_string,
                           COLUMN_EXEC, g_app_info_get_executable (app),
                           COLUMN_HEADING, FALSE,
@@ -590,11 +614,9 @@ gtk_app_chooser_add_default (GtkAppChooserWidget *self,
 
   g_free (string);
 
-  string = g_markup_printf_escaped ("<b>%s</b>\n%s",
-                                    g_app_info_get_display_name (app) != NULL ?
-                                    g_app_info_get_display_name (app) : "",
-                                    g_app_info_get_description (app) != NULL ?
-                                    g_app_info_get_description (app) : "");
+  string = g_markup_printf_escaped ("%s",
+                                    g_app_info_get_name (app) != NULL ?
+                                    g_app_info_get_name (app) : "");
 
   icon = g_app_info_get_icon (app);
   if (icon == NULL)
@@ -607,7 +629,7 @@ gtk_app_chooser_add_default (GtkAppChooserWidget *self,
   gtk_list_store_set (self->priv->program_list_store, &iter,
                       COLUMN_APP_INFO, app,
                       COLUMN_GICON, icon,
-                      COLUMN_NAME, g_app_info_get_display_name (app),
+                      COLUMN_NAME, g_app_info_get_name (app),
                       COLUMN_DESC, string,
                       COLUMN_EXEC, g_app_info_get_executable (app),
                       COLUMN_HEADING, FALSE,
@@ -623,13 +645,15 @@ gtk_app_chooser_add_default (GtkAppChooserWidget *self,
 static void
 add_no_applications_label (GtkAppChooserWidget *self)
 {
-  gchar *text = NULL, *desc;
+  gchar *text = NULL, *desc = NULL;
   const gchar *string;
   GtkTreeIter iter;
 
   if (self->priv->default_text == NULL)
     {
-      desc = g_content_type_get_description (self->priv->content_type);
+      if (self->priv->content_type)
+       desc = g_content_type_get_description (self->priv->content_type);
+
       string = text = g_strdup_printf (_("No applications available to open \"%s\""),
                                        desc);
       g_free (desc);
@@ -699,7 +723,7 @@ gtk_app_chooser_widget_real_add_items (GtkAppChooserWidget *self)
   if (self->priv->show_all)
     show_headings = FALSE;
 
-  if (self->priv->show_default)
+  if (self->priv->show_default && self->priv->content_type)
     {
       default_app = g_app_info_get_default_for_type (self->priv->content_type, FALSE);
 
@@ -711,9 +735,11 @@ gtk_app_chooser_widget_real_add_items (GtkAppChooserWidget *self)
         }
     }
 
-  if (self->priv->show_recommended || self->priv->show_all)
+#ifndef G_OS_WIN32
+  if ((self->priv->content_type && self->priv->show_recommended) || self->priv->show_all)
     {
-      recommended_apps = g_app_info_get_recommended_for_type (self->priv->content_type);
+      if (self->priv->content_type)
+       recommended_apps = g_app_info_get_recommended_for_type (self->priv->content_type);
 
       apps_added |= gtk_app_chooser_widget_add_section (self, _("Recommended Applications"),
                                                         show_headings,
@@ -725,9 +751,10 @@ gtk_app_chooser_widget_real_add_items (GtkAppChooserWidget *self)
                                     g_list_copy (recommended_apps));
     }
 
-  if (self->priv->show_fallback || self->priv->show_all)
+  if ((self->priv->content_type && self->priv->show_fallback) || self->priv->show_all)
     {
-      fallback_apps = g_app_info_get_fallback_for_type (self->priv->content_type);
+      if (self->priv->content_type)
+       fallback_apps = g_app_info_get_fallback_for_type (self->priv->content_type);
 
       apps_added |= gtk_app_chooser_widget_add_section (self, _("Related Applications"),
                                                         show_headings,
@@ -737,6 +764,7 @@ gtk_app_chooser_widget_real_add_items (GtkAppChooserWidget *self)
       exclude_apps = g_list_concat (exclude_apps,
                                     g_list_copy (fallback_apps));
     }
+#endif
 
   if (self->priv->show_other || self->priv->show_all)
     {
@@ -844,7 +872,7 @@ gtk_app_chooser_widget_add_items (GtkAppChooserWidget *self)
                                        "gicon", COLUMN_GICON,
                                        NULL);
   g_object_set (renderer,
-                "stock-size", GTK_ICON_SIZE_DIALOG,
+                "stock-size", GTK_ICON_SIZE_MENU,
                 NULL);
 
   /* app name renderer */
@@ -944,8 +972,6 @@ gtk_app_chooser_widget_constructed (GObject *object)
 {
   GtkAppChooserWidget *self = GTK_APP_CHOOSER_WIDGET (object);
 
-  g_assert (self->priv->content_type != NULL);
-
   if (G_OBJECT_CLASS (gtk_app_chooser_widget_parent_class)->constructed != NULL)
     G_OBJECT_CLASS (gtk_app_chooser_widget_parent_class)->constructed (object);
 
@@ -968,11 +994,7 @@ gtk_app_chooser_widget_dispose (GObject *object)
 {
   GtkAppChooserWidget *self = GTK_APP_CHOOSER_WIDGET (object);
 
-  if (self->priv->selected_app_info != NULL)
-    {
-      g_object_unref (self->priv->selected_app_info);
-      self->priv->selected_app_info = NULL;
-    }
+  g_clear_object (&self->priv->selected_app_info);
 
   G_OBJECT_CLASS (gtk_app_chooser_widget_parent_class)->dispose (object);
 }
@@ -1010,9 +1032,10 @@ gtk_app_chooser_widget_class_init (GtkAppChooserWidgetClass *klass)
   /**
    * GtkAppChooserWidget:show-recommended:
    *
-   * The #GtkAppChooserWidget:show-recommended property determines whether the app chooser
-   * should show a section for recommended applications. If %FALSE, the
-   * recommended applications are listed among the other applications.
+   * The #GtkAppChooserWidget:show-recommended property determines
+   * whether the app chooser should show a section for recommended
+   * applications. If %FALSE, the recommended applications are listed
+   * among the other applications.
    */
   pspec = g_param_spec_boolean ("show-recommended",
                                 P_("Show recommended apps"),
@@ -1024,9 +1047,10 @@ gtk_app_chooser_widget_class_init (GtkAppChooserWidgetClass *klass)
   /**
    * GtkAppChooserWidget:show-fallback:
    *
-   * The #GtkAppChooserWidget:show-fallback property determines whether the app chooser
-   * should show a section for related applications. If %FALSE, the
-   * related applications are listed among the other applications.
+   * The #GtkAppChooserWidget:show-fallback property determines whether
+   * the app chooser should show a section for fallback applications.
+   * If %FALSE, the fallback applications are listed among the other
+   * applications.
    */
   pspec = g_param_spec_boolean ("show-fallback",
                                 P_("Show fallback apps"),
@@ -1038,8 +1062,8 @@ gtk_app_chooser_widget_class_init (GtkAppChooserWidgetClass *klass)
   /**
    * GtkAppChooserWidget:show-other:
    *
-   * The #GtkAppChooserWidget:show-other property determines whether the app chooser
-   * should show a section for other applications.
+   * The #GtkAppChooserWidget:show-other property determines whether
+   * the app chooser should show a section for other applications.
    */
   pspec = g_param_spec_boolean ("show-other",
                                 P_("Show other apps"),
@@ -1051,9 +1075,9 @@ gtk_app_chooser_widget_class_init (GtkAppChooserWidgetClass *klass)
   /**
    * GtkAppChooserWidget:show-all:
    *
-   * If the #GtkAppChooserWidget:show-all property is %TRUE, the app chooser presents
-   * all applications in a single list, without subsections for
-   * default, recommended or related applications.
+   * If the #GtkAppChooserWidget:show-all property is %TRUE, the app
+   * chooser presents all applications in a single list, without
+   * subsections for default, recommended or related applications.
    */
   pspec = g_param_spec_boolean ("show-all",
                                 P_("Show all apps"),
@@ -1065,8 +1089,9 @@ gtk_app_chooser_widget_class_init (GtkAppChooserWidgetClass *klass)
   /**
    * GtkAppChooserWidget:default-text:
    *
-   * The #GtkAppChooserWidget:default-text property determines the text that appears
-   * in the widget when there are no applications for the given content type.
+   * The #GtkAppChooserWidget:default-text property determines the text
+   * that appears in the widget when there are no applications for the
+   * given content type.
    * See also gtk_app_chooser_widget_set_default_text().
    */
   pspec = g_param_spec_string ("default-text",
@@ -1099,6 +1124,7 @@ gtk_app_chooser_widget_class_init (GtkAppChooserWidgetClass *klass)
    * @application: the activated #GAppInfo
    *
    * Emitted when an application item is activated from the widget's list.
+   *
    * This usually happens when the user double clicks an item, or an item
    * is selected and the user presses one of the keys Space, Shift+Space,
    * Return or Enter.
@@ -1121,8 +1147,8 @@ gtk_app_chooser_widget_class_init (GtkAppChooserWidgetClass *klass)
    *
    * Emitted when a context menu is about to popup over an application item.
    * Clients can insert menu items into the provided #GtkMenu object in the
-   * callback of this signal; the context menu will be shown over the item if
-   * at least one item has been added to the menu.
+   * callback of this signal; the context menu will be shown over the item
+   * if at least one item has been added to the menu.
    */
   signals[SIGNAL_POPULATE_POPUP] =
     g_signal_new ("populate-popup",
@@ -1164,7 +1190,7 @@ gtk_app_chooser_widget_init (GtkAppChooserWidget *self)
   gtk_widget_show (self->priv->program_list);
 
   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->program_list));
-  gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
+  gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
   gtk_tree_selection_set_select_function (selection, gtk_app_chooser_selection_func,
                                           self, NULL);
   g_signal_connect_swapped (selection, "changed",