1 /* GTK - The GIMP Toolkit
2 * gtkrecentchoosermenu.c - Recently used items menu widget
3 * Copyright (C) 2005, Emmanuele Bassi
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #include "gtkrecentmanager.h"
26 #include "gtkrecentfilter.h"
27 #include "gtkrecentchooser.h"
28 #include "gtkrecentchooserutils.h"
29 #include "gtkrecentchooserprivate.h"
30 #include "gtkrecentchoosermenu.h"
33 #include "gtkicontheme.h"
34 #include "gtkiconfactory.h"
36 #include "gtksettings.h"
37 #include "gtkmenushell.h"
38 #include "gtkmenuitem.h"
39 #include "gtkimagemenuitem.h"
40 #include "gtkseparatormenuitem.h"
44 #include "gtktooltip.h"
45 #include "gtkactivatable.h"
46 #include "gtktypebuiltins.h"
47 #include "gtkprivate.h"
50 struct _GtkRecentChooserMenuPrivate
52 /* the recent manager object */
53 GtkRecentManager *manager;
55 /* size of the icons of the menu items */
58 /* max size of the menu item label */
61 gint first_recent_item_pos;
62 GtkWidget *placeholder;
64 /* RecentChooser properties */
66 guint show_private : 1;
67 guint show_not_found : 1;
72 guint show_numbers : 1;
74 GtkRecentSortType sort_type;
75 GtkRecentSortFunc sort_func;
77 GDestroyNotify sort_data_destroy;
80 GtkRecentFilter *current_filter;
82 guint local_manager : 1;
83 gulong manager_changed_id;
92 /* activatable properties */
93 PROP_ACTIVATABLE_RELATED_ACTION,
94 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
98 #define FALLBACK_ICON_SIZE 32
99 #define FALLBACK_ITEM_LIMIT 10
100 #define DEFAULT_LABEL_WIDTH 30
102 #define GTK_RECENT_CHOOSER_MENU_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RECENT_CHOOSER_MENU, GtkRecentChooserMenuPrivate))
104 static void gtk_recent_chooser_menu_finalize (GObject *object);
105 static void gtk_recent_chooser_menu_dispose (GObject *object);
106 static GObject *gtk_recent_chooser_menu_constructor (GType type,
107 guint n_construct_properties,
108 GObjectConstructParam *construct_params);
110 static void gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface);
112 static void gtk_recent_chooser_menu_set_property (GObject *object,
116 static void gtk_recent_chooser_menu_get_property (GObject *object,
121 static gboolean gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
124 static gchar * gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser);
125 static gboolean gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
128 static void gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
130 static void gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser);
131 static void gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser);
132 static GList * gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser);
133 static GtkRecentManager *gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser);
134 static void gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser *chooser,
135 GtkRecentSortFunc sort_func,
137 GDestroyNotify data_destroy);
138 static void gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
139 GtkRecentFilter *filter);
140 static void gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
141 GtkRecentFilter *filter);
142 static GSList * gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser);
143 static void gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
144 GtkRecentFilter *filter);
146 static void gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu);
147 static void gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
150 static void set_recent_manager (GtkRecentChooserMenu *menu,
151 GtkRecentManager *manager);
153 static void chooser_set_sort_type (GtkRecentChooserMenu *menu,
154 GtkRecentSortType sort_type);
156 static gint get_icon_size_for_widget (GtkWidget *widget);
158 static void item_activate_cb (GtkWidget *widget,
160 static void manager_changed_cb (GtkRecentManager *manager,
163 static void gtk_recent_chooser_activatable_iface_init (GtkActivatableIface *iface);
164 static void gtk_recent_chooser_update (GtkActivatable *activatable,
166 const gchar *property_name);
167 static void gtk_recent_chooser_sync_action_properties (GtkActivatable *activatable,
170 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserMenu,
171 gtk_recent_chooser_menu,
173 G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
174 gtk_recent_chooser_iface_init)
175 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
176 gtk_recent_chooser_activatable_iface_init))
180 gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface)
182 iface->set_current_uri = gtk_recent_chooser_menu_set_current_uri;
183 iface->get_current_uri = gtk_recent_chooser_menu_get_current_uri;
184 iface->select_uri = gtk_recent_chooser_menu_select_uri;
185 iface->unselect_uri = gtk_recent_chooser_menu_unselect_uri;
186 iface->select_all = gtk_recent_chooser_menu_select_all;
187 iface->unselect_all = gtk_recent_chooser_menu_unselect_all;
188 iface->get_items = gtk_recent_chooser_menu_get_items;
189 iface->get_recent_manager = gtk_recent_chooser_menu_get_recent_manager;
190 iface->set_sort_func = gtk_recent_chooser_menu_set_sort_func;
191 iface->add_filter = gtk_recent_chooser_menu_add_filter;
192 iface->remove_filter = gtk_recent_chooser_menu_remove_filter;
193 iface->list_filters = gtk_recent_chooser_menu_list_filters;
197 gtk_recent_chooser_activatable_iface_init (GtkActivatableIface *iface)
199 iface->update = gtk_recent_chooser_update;
200 iface->sync_action_properties = gtk_recent_chooser_sync_action_properties;
204 gtk_recent_chooser_menu_class_init (GtkRecentChooserMenuClass *klass)
206 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
208 gobject_class->constructor = gtk_recent_chooser_menu_constructor;
209 gobject_class->dispose = gtk_recent_chooser_menu_dispose;
210 gobject_class->finalize = gtk_recent_chooser_menu_finalize;
211 gobject_class->set_property = gtk_recent_chooser_menu_set_property;
212 gobject_class->get_property = gtk_recent_chooser_menu_get_property;
214 _gtk_recent_chooser_install_properties (gobject_class);
217 * GtkRecentChooserMenu:show-numbers
219 * Whether the first ten items in the menu should be prepended by
220 * a number acting as a unique mnemonic.
224 g_object_class_install_property (gobject_class,
226 g_param_spec_boolean ("show-numbers",
228 P_("Whether the items should be displayed with a number"),
230 GTK_PARAM_READWRITE));
233 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
234 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
236 g_type_class_add_private (klass, sizeof (GtkRecentChooserMenuPrivate));
240 gtk_recent_chooser_menu_init (GtkRecentChooserMenu *menu)
242 GtkRecentChooserMenuPrivate *priv;
244 priv = GTK_RECENT_CHOOSER_MENU_GET_PRIVATE (menu);
248 priv->show_icons= TRUE;
249 priv->show_numbers = FALSE;
250 priv->show_tips = FALSE;
251 priv->show_not_found = TRUE;
252 priv->show_private = FALSE;
253 priv->local_only = TRUE;
255 priv->limit = FALLBACK_ITEM_LIMIT;
256 priv->sort_type = GTK_RECENT_SORT_NONE;
257 priv->icon_size = FALLBACK_ICON_SIZE;
258 priv->label_width = DEFAULT_LABEL_WIDTH;
260 priv->first_recent_item_pos = -1;
261 priv->placeholder = NULL;
263 priv->current_filter = NULL;
267 gtk_recent_chooser_menu_finalize (GObject *object)
269 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
270 GtkRecentChooserMenuPrivate *priv = menu->priv;
272 priv->manager = NULL;
274 if (priv->sort_data_destroy)
276 priv->sort_data_destroy (priv->sort_data);
277 priv->sort_data_destroy = NULL;
280 priv->sort_data = NULL;
281 priv->sort_func = NULL;
283 G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->finalize (object);
287 gtk_recent_chooser_menu_dispose (GObject *object)
289 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
290 GtkRecentChooserMenuPrivate *priv = menu->priv;
292 if (priv->manager_changed_id)
295 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
297 priv->manager_changed_id = 0;
300 if (priv->populate_id)
302 g_source_remove (priv->populate_id);
303 priv->populate_id = 0;
306 if (priv->current_filter)
308 g_object_unref (priv->current_filter);
309 priv->current_filter = NULL;
312 G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->dispose (object);
316 gtk_recent_chooser_menu_constructor (GType type,
318 GObjectConstructParam *params)
320 GtkRecentChooserMenu *menu;
321 GtkRecentChooserMenuPrivate *priv;
322 GObjectClass *parent_class;
325 parent_class = G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class);
326 object = parent_class->constructor (type, n_params, params);
327 menu = GTK_RECENT_CHOOSER_MENU (object);
330 g_assert (priv->manager);
332 /* we create a placeholder menuitem, to be used in case
333 * the menu is empty. this placeholder will stay around
334 * for the entire lifetime of the menu, and we just hide it
335 * when it's not used. we have to do this, and do it here,
336 * because we need a marker for the beginning of the recent
337 * items list, so that we can insert the new items at the
338 * right place when idly populating the menu in case the
339 * user appended or prepended custom menu items to the
340 * recent chooser menu widget.
342 priv->placeholder = gtk_menu_item_new_with_label (_("No items found"));
343 gtk_widget_set_sensitive (priv->placeholder, FALSE);
344 g_object_set_data (G_OBJECT (priv->placeholder),
345 "gtk-recent-menu-placeholder",
346 GINT_TO_POINTER (TRUE));
348 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), priv->placeholder, 0);
349 gtk_widget_set_no_show_all (priv->placeholder, TRUE);
350 gtk_widget_show (priv->placeholder);
352 /* (re)populate the menu */
353 gtk_recent_chooser_menu_populate (menu);
359 gtk_recent_chooser_menu_set_property (GObject *object,
364 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
365 GtkRecentChooserMenuPrivate *priv = menu->priv;
369 case PROP_SHOW_NUMBERS:
370 priv->show_numbers = g_value_get_boolean (value);
372 case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
373 set_recent_manager (menu, g_value_get_object (value));
375 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
376 priv->show_private = g_value_get_boolean (value);
378 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
379 priv->show_not_found = g_value_get_boolean (value);
381 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
382 gtk_recent_chooser_menu_set_show_tips (menu, g_value_get_boolean (value));
384 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
385 priv->show_icons = g_value_get_boolean (value);
387 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
388 g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
390 G_OBJECT_TYPE_NAME (object));
392 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
393 priv->local_only = g_value_get_boolean (value);
395 case GTK_RECENT_CHOOSER_PROP_LIMIT:
396 priv->limit = g_value_get_int (value);
398 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
399 chooser_set_sort_type (menu, g_value_get_enum (value));
401 case GTK_RECENT_CHOOSER_PROP_FILTER:
402 gtk_recent_chooser_menu_set_current_filter (menu, g_value_get_object (value));
404 case PROP_ACTIVATABLE_RELATED_ACTION:
405 _gtk_recent_chooser_set_related_action (GTK_RECENT_CHOOSER (menu), g_value_get_object (value));
407 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
408 _gtk_recent_chooser_set_use_action_appearance (GTK_RECENT_CHOOSER (menu), g_value_get_boolean (value));
411 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
417 gtk_recent_chooser_menu_get_property (GObject *object,
422 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
423 GtkRecentChooserMenuPrivate *priv = menu->priv;
427 case PROP_SHOW_NUMBERS:
428 g_value_set_boolean (value, priv->show_numbers);
430 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
431 g_value_set_boolean (value, priv->show_tips);
433 case GTK_RECENT_CHOOSER_PROP_LIMIT:
434 g_value_set_int (value, priv->limit);
436 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
437 g_value_set_boolean (value, priv->local_only);
439 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
440 g_value_set_enum (value, priv->sort_type);
442 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
443 g_value_set_boolean (value, priv->show_private);
445 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
446 g_value_set_boolean (value, priv->show_not_found);
448 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
449 g_value_set_boolean (value, priv->show_icons);
451 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
452 g_value_set_boolean (value, FALSE);
454 case GTK_RECENT_CHOOSER_PROP_FILTER:
455 g_value_set_object (value, priv->current_filter);
457 case PROP_ACTIVATABLE_RELATED_ACTION:
458 g_value_set_object (value, _gtk_recent_chooser_get_related_action (GTK_RECENT_CHOOSER (menu)));
460 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
461 g_value_set_boolean (value, _gtk_recent_chooser_get_use_action_appearance (GTK_RECENT_CHOOSER (menu)));
464 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
470 gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
474 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
476 GtkWidget *menu_item = NULL;
477 gboolean found = FALSE;
479 children = gtk_container_get_children (GTK_CONTAINER (menu));
481 for (l = children; l != NULL; l = l->next)
485 menu_item = GTK_WIDGET (l->data);
487 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
491 if (strcmp (uri, gtk_recent_info_get_uri (info)) == 0)
493 gtk_menu_shell_activate_item (GTK_MENU_SHELL (menu),
502 g_list_free (children);
506 g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
507 GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
508 _("No recently used resource found with URI `%s'"),
516 gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser)
518 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
519 GtkWidget *menu_item;
522 menu_item = gtk_menu_get_active (GTK_MENU (menu));
526 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
530 return g_strdup (gtk_recent_info_get_uri (info));
534 gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
538 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
540 GtkWidget *menu_item = NULL;
541 gboolean found = FALSE;
543 children = gtk_container_get_children (GTK_CONTAINER (menu));
544 for (l = children; l != NULL; l = l->next)
548 menu_item = GTK_WIDGET (l->data);
550 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
554 if (0 == strcmp (uri, gtk_recent_info_get_uri (info)))
558 g_list_free (children);
562 g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
563 GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
564 _("No recently used resource found with URI `%s'"),
570 gtk_menu_shell_select_item (GTK_MENU_SHELL (menu), menu_item);
577 gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
580 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
582 gtk_menu_shell_deselect (GTK_MENU_SHELL (menu));
586 gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser)
588 g_warning (_("This function is not implemented for "
589 "widgets of class '%s'"),
590 g_type_name (G_OBJECT_TYPE (chooser)));
594 gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser)
596 g_warning (_("This function is not implemented for "
597 "widgets of class '%s'"),
598 g_type_name (G_OBJECT_TYPE (chooser)));
602 gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser *chooser,
603 GtkRecentSortFunc sort_func,
605 GDestroyNotify data_destroy)
607 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
608 GtkRecentChooserMenuPrivate *priv = menu->priv;
610 if (priv->sort_data_destroy)
612 priv->sort_data_destroy (priv->sort_data);
613 priv->sort_data_destroy = NULL;
616 priv->sort_func = NULL;
617 priv->sort_data = NULL;
618 priv->sort_data_destroy = NULL;
622 priv->sort_func = sort_func;
623 priv->sort_data = sort_data;
624 priv->sort_data_destroy = data_destroy;
629 chooser_set_sort_type (GtkRecentChooserMenu *menu,
630 GtkRecentSortType sort_type)
632 if (menu->priv->sort_type == sort_type)
635 menu->priv->sort_type = sort_type;
640 gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser)
642 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
643 GtkRecentChooserMenuPrivate *priv = menu->priv;
645 return _gtk_recent_chooser_get_items (chooser,
646 priv->current_filter,
651 static GtkRecentManager *
652 gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser)
654 GtkRecentChooserMenuPrivate *priv;
656 priv = GTK_RECENT_CHOOSER_MENU (chooser)->priv;
658 return priv->manager;
662 gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
663 GtkRecentFilter *filter)
665 GtkRecentChooserMenu *menu;
667 menu = GTK_RECENT_CHOOSER_MENU (chooser);
669 gtk_recent_chooser_menu_set_current_filter (menu, filter);
673 gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
674 GtkRecentFilter *filter)
676 GtkRecentChooserMenu *menu;
678 menu = GTK_RECENT_CHOOSER_MENU (chooser);
680 if (filter == menu->priv->current_filter)
682 g_object_unref (menu->priv->current_filter);
683 menu->priv->current_filter = NULL;
685 g_object_notify (G_OBJECT (menu), "filter");
690 gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser)
692 GtkRecentChooserMenu *menu;
693 GSList *retval = NULL;
695 menu = GTK_RECENT_CHOOSER_MENU (chooser);
697 if (menu->priv->current_filter)
698 retval = g_slist_prepend (retval, menu->priv->current_filter);
704 gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
705 GtkRecentFilter *filter)
707 GtkRecentChooserMenuPrivate *priv;
711 if (priv->current_filter)
712 g_object_unref (G_OBJECT (priv->current_filter));
716 priv->current_filter = filter;
717 g_object_ref_sink (priv->current_filter);
720 gtk_recent_chooser_menu_populate (menu);
722 g_object_notify (G_OBJECT (menu), "filter");
725 /* taken from libeel/eel-strings.c */
727 escape_underscores (const gchar *string)
738 for (p = string; *p != '\0'; p++)
739 underscores += (*p == '_');
741 if (underscores == 0)
742 return g_strdup (string);
744 escaped = g_new (char, strlen (string) + underscores + 1);
745 for (p = string, q = escaped; *p != '\0'; p++, q++)
747 /* Add an extra underscore. */
760 gtk_recent_chooser_menu_add_tip (GtkRecentChooserMenu *menu,
764 GtkRecentChooserMenuPrivate *priv;
767 g_assert (info != NULL);
768 g_assert (item != NULL);
772 path = gtk_recent_info_get_uri_display (info);
775 gchar *tip_text = g_strdup_printf (_("Open '%s'"), path);
777 gtk_widget_set_tooltip_text (item, tip_text);
778 gtk_widget_set_has_tooltip (item, priv->show_tips);
786 gtk_recent_chooser_menu_create_item (GtkRecentChooserMenu *menu,
790 GtkRecentChooserMenuPrivate *priv;
792 GtkWidget *item, *image, *label;
795 g_assert (info != NULL);
799 if (priv->show_numbers)
801 gchar *name, *escaped;
803 name = g_strdup (gtk_recent_info_get_display_name (info));
805 name = g_strdup (_("Unknown item"));
807 escaped = escape_underscores (name);
809 /* avoid clashing mnemonics */
811 /* This is the label format that is used for the first 10 items
812 * in a recent files menu. The %d is the number of the item,
813 * the %s is the name of the item. Please keep the _ in front
814 * of the number to give these menu items a mnemonic.
816 text = g_strdup_printf (C_("recent menu label", "_%d. %s"), count, escaped);
818 /* This is the format that is used for items in a recent files menu.
819 * The %d is the number of the item, the %s is the name of the item.
821 text = g_strdup_printf (C_("recent menu label", "%d. %s"), count, escaped);
823 item = gtk_image_menu_item_new_with_mnemonic (text);
830 text = g_strdup (gtk_recent_info_get_display_name (info));
831 item = gtk_image_menu_item_new_with_label (text);
836 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item),
839 /* ellipsize the menu item label, in case the recent document
840 * display name is huge.
842 label = GTK_BIN (item)->child;
843 if (GTK_IS_LABEL (label))
845 gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
846 gtk_label_set_max_width_chars (GTK_LABEL (label), priv->label_width);
849 if (priv->show_icons)
851 icon = gtk_recent_info_get_icon (info, priv->icon_size);
853 image = gtk_image_new_from_pixbuf (icon);
854 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
855 g_object_unref (icon);
858 g_signal_connect (item, "activate",
859 G_CALLBACK (item_activate_cb),
866 gtk_recent_chooser_menu_insert_item (GtkRecentChooserMenu *menu,
870 GtkRecentChooserMenuPrivate *priv = menu->priv;
873 if (priv->first_recent_item_pos == -1)
877 children = gtk_container_get_children (GTK_CONTAINER (menu));
879 for (real_position = 0, l = children;
881 real_position += 1, l = l->next)
883 GObject *child = l->data;
884 gboolean is_placeholder = FALSE;
887 GPOINTER_TO_INT (g_object_get_data (child, "gtk-recent-menu-placeholder"));
893 g_list_free (children);
894 priv->first_recent_item_pos = real_position;
897 real_position = priv->first_recent_item_pos;
899 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), menuitem,
900 real_position + position);
901 gtk_widget_show (menuitem);
904 /* removes the items we own from the menu */
906 gtk_recent_chooser_menu_dispose_items (GtkRecentChooserMenu *menu)
910 children = gtk_container_get_children (GTK_CONTAINER (menu));
911 for (l = children; l != NULL; l = l->next)
913 GtkWidget *menu_item = GTK_WIDGET (l->data);
914 gboolean has_mark = FALSE;
916 /* check for our mark, in order to remove just the items we own */
918 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item), "gtk-recent-menu-mark"));
924 /* destroy the attached RecentInfo struct, if found */
925 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
927 g_object_set_data_full (G_OBJECT (menu_item), "gtk-recent-info",
930 /* and finally remove the item from the menu */
931 gtk_container_remove (GTK_CONTAINER (menu), menu_item);
935 /* recalculate the position of the first recent item */
936 menu->priv->first_recent_item_pos = -1;
938 g_list_free (children);
946 gint displayed_items;
947 GtkRecentChooserMenu *menu;
948 GtkWidget *placeholder;
952 idle_populate_func (gpointer data)
954 MenuPopulateData *pdata;
955 GtkRecentChooserMenuPrivate *priv;
960 pdata = (MenuPopulateData *) data;
961 priv = pdata->menu->priv;
965 pdata->items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (pdata->menu));
968 /* show the placeholder here */
969 gtk_widget_show (pdata->placeholder);
970 pdata->displayed_items = 1;
971 priv->populate_id = 0;
976 gtk_widget_hide (pdata->placeholder);
978 pdata->n_items = g_list_length (pdata->items);
979 pdata->loaded_items = 0;
982 info = g_list_nth_data (pdata->items, pdata->loaded_items);
983 item = gtk_recent_chooser_menu_create_item (pdata->menu,
985 pdata->displayed_items);
987 goto check_and_return;
989 gtk_recent_chooser_menu_add_tip (pdata->menu, info, item);
990 gtk_recent_chooser_menu_insert_item (pdata->menu, item,
991 pdata->displayed_items);
993 pdata->displayed_items += 1;
995 /* mark the menu item as one of our own */
996 g_object_set_data (G_OBJECT (item),
997 "gtk-recent-menu-mark",
998 GINT_TO_POINTER (TRUE));
1000 /* attach the RecentInfo object to the menu item, and own a reference
1001 * to it, so that it will be destroyed with the menu item when it's
1002 * not needed anymore.
1004 g_object_set_data_full (G_OBJECT (item), "gtk-recent-info",
1005 gtk_recent_info_ref (info),
1006 (GDestroyNotify) gtk_recent_info_unref);
1009 pdata->loaded_items += 1;
1011 if (pdata->loaded_items == pdata->n_items)
1013 g_list_foreach (pdata->items, (GFunc) gtk_recent_info_unref, NULL);
1014 g_list_free (pdata->items);
1016 priv->populate_id = 0;
1027 idle_populate_clean_up (gpointer data)
1029 MenuPopulateData *pdata = data;
1031 if (pdata->menu->priv->populate_id == 0)
1033 /* show the placeholder in case no item survived
1034 * the filtering process in the idle loop
1036 if (!pdata->displayed_items)
1037 gtk_widget_show (pdata->placeholder);
1038 g_object_unref (pdata->placeholder);
1040 g_slice_free (MenuPopulateData, data);
1045 gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu)
1047 MenuPopulateData *pdata;
1048 GtkRecentChooserMenuPrivate *priv = menu->priv;
1050 if (menu->priv->populate_id)
1053 pdata = g_slice_new (MenuPopulateData);
1054 pdata->items = NULL;
1056 pdata->loaded_items = 0;
1057 pdata->displayed_items = 0;
1059 pdata->placeholder = g_object_ref (priv->placeholder);
1061 priv->icon_size = get_icon_size_for_widget (GTK_WIDGET (menu));
1063 /* remove our menu items first */
1064 gtk_recent_chooser_menu_dispose_items (menu);
1066 priv->populate_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
1069 idle_populate_clean_up);
1072 /* bounce activate signal from the recent menu item widget
1073 * to the recent menu widget
1076 item_activate_cb (GtkWidget *widget,
1079 GtkRecentChooser *chooser = GTK_RECENT_CHOOSER (user_data);
1081 _gtk_recent_chooser_item_activated (chooser);
1084 /* we force a redraw if the manager changes when we are showing */
1086 manager_changed_cb (GtkRecentManager *manager,
1089 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (user_data);
1091 gtk_recent_chooser_menu_populate (menu);
1095 set_recent_manager (GtkRecentChooserMenu *menu,
1096 GtkRecentManager *manager)
1098 GtkRecentChooserMenuPrivate *priv = menu->priv;
1102 if (priv->manager_changed_id)
1104 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
1105 priv->manager_changed_id = 0;
1108 if (priv->populate_id)
1110 g_source_remove (priv->populate_id);
1111 priv->populate_id = 0;
1114 priv->manager = NULL;
1118 priv->manager = manager;
1120 priv->manager = gtk_recent_manager_get_default ();
1123 priv->manager_changed_id = g_signal_connect (priv->manager, "changed",
1124 G_CALLBACK (manager_changed_cb),
1129 get_icon_size_for_widget (GtkWidget *widget)
1131 GtkSettings *settings;
1134 if (gtk_widget_has_screen (widget))
1135 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
1137 settings = gtk_settings_get_default ();
1139 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1141 return MAX (width, height);
1143 return FALLBACK_ICON_SIZE;
1147 foreach_set_shot_tips (GtkWidget *widget,
1150 GtkRecentChooserMenu *menu = user_data;
1151 GtkRecentChooserMenuPrivate *priv = menu->priv;
1154 /* toggle the tooltip only on the items we create */
1156 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "gtk-recent-menu-mark"));
1159 gtk_widget_set_has_tooltip (widget, priv->show_tips);
1163 gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
1166 GtkRecentChooserMenuPrivate *priv = menu->priv;
1168 if (priv->show_tips == show_tips)
1171 priv->show_tips = show_tips;
1172 gtk_container_foreach (GTK_CONTAINER (menu), foreach_set_shot_tips, menu);
1176 gtk_recent_chooser_update (GtkActivatable *activatable,
1178 const gchar *property_name)
1180 if (strcmp (property_name, "sensitive") == 0)
1181 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1183 _gtk_recent_chooser_update (activatable, action, property_name);
1187 gtk_recent_chooser_sync_action_properties (GtkActivatable *activatable,
1190 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1192 _gtk_recent_chooser_sync_action_properties (activatable, action);
1201 * gtk_recent_chooser_menu_new:
1203 * Creates a new #GtkRecentChooserMenu widget.
1205 * This kind of widget shows the list of recently used resources as
1206 * a menu, each item as a menu item. Each item inside the menu might
1207 * have an icon, representing its MIME type, and a number, for mnemonic
1210 * This widget implements the #GtkRecentChooser interface.
1212 * This widget creates its own #GtkRecentManager object. See the
1213 * gtk_recent_chooser_menu_new_for_manager() function to know how to create
1214 * a #GtkRecentChooserMenu widget bound to another #GtkRecentManager object.
1216 * Return value: a new #GtkRecentChooserMenu
1221 gtk_recent_chooser_menu_new (void)
1223 return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1224 "recent-manager", NULL,
1229 * gtk_recent_chooser_menu_new_for_manager:
1230 * @manager: a #GtkRecentManager
1232 * Creates a new #GtkRecentChooserMenu widget using @manager as
1233 * the underlying recently used resources manager.
1235 * This is useful if you have implemented your own recent manager,
1236 * or if you have a customized instance of a #GtkRecentManager
1237 * object or if you wish to share a common #GtkRecentManager object
1238 * among multiple #GtkRecentChooser widgets.
1240 * Return value: a new #GtkRecentChooserMenu, bound to @manager.
1245 gtk_recent_chooser_menu_new_for_manager (GtkRecentManager *manager)
1247 g_return_val_if_fail (manager == NULL || GTK_IS_RECENT_MANAGER (manager), NULL);
1249 return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1250 "recent-manager", manager,
1255 * gtk_recent_chooser_menu_get_show_numbers:
1256 * @menu: a #GtkRecentChooserMenu
1258 * Returns the value set by gtk_recent_chooser_menu_set_show_numbers().
1260 * Return value: %TRUE if numbers should be shown.
1265 gtk_recent_chooser_menu_get_show_numbers (GtkRecentChooserMenu *menu)
1267 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu), FALSE);
1269 return menu->priv->show_numbers;
1273 * gtk_recent_chooser_menu_set_show_numbers:
1274 * @menu: a #GtkRecentChooserMenu
1275 * @show_numbers: whether to show numbers
1277 * Sets whether a number should be added to the items of @menu. The
1278 * numbers are shown to provide a unique character for a mnemonic to
1279 * be used inside ten menu item's label. Only the first the items
1280 * get a number to avoid clashes.
1285 gtk_recent_chooser_menu_set_show_numbers (GtkRecentChooserMenu *menu,
1286 gboolean show_numbers)
1288 g_return_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu));
1290 if (menu->priv->show_numbers == show_numbers)
1293 menu->priv->show_numbers = show_numbers;
1294 g_object_notify (G_OBJECT (menu), "show-numbers");
1297 #define __GTK_RECENT_CHOOSER_MENU_C__
1298 #include "gtkaliasdef.c"