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"
52 * SECTION:gtkrecentchoosermenu
53 * @Short_description: Displays recently used files in a menu
54 * @Title: GtkRecentChooserMenu
55 * @See_also:#GtkRecentChooser
57 * #GtkRecentChooserMenu is a widget suitable for displaying recently used files
58 * inside a menu. It can be used to set a sub-menu of a #GtkMenuItem using
59 * gtk_menu_item_set_submenu(), or as the menu of a #GtkMenuToolButton.
61 * Note that #GtkRecentChooserMenu does not have any methods of its own. Instead,
62 * you should use the functions that work on a #GtkRecentChooser.
64 * Note also that #GtkRecentChooserMenu does not support multiple filters, as it
65 * has no way to let the user choose between them as the #GtkRecentChooserWidget
66 * and #GtkRecentChooserDialog widgets do. Thus using gtk_recent_chooser_add_filter()
67 * on a #GtkRecentChooserMenu widget will yield the same effects as using
68 * gtk_recent_chooser_set_filter(), replacing any currently set filter
69 * with the supplied filter; gtk_recent_chooser_remove_filter() will remove
70 * any currently set #GtkRecentFilter object and will unset the current filter;
71 * gtk_recent_chooser_list_filters() will return a list containing a single
72 * #GtkRecentFilter object.
74 * Recently used files are supported since GTK+ 2.10.
78 struct _GtkRecentChooserMenuPrivate
80 /* the recent manager object */
81 GtkRecentManager *manager;
83 /* size of the icons of the menu items */
86 /* max size of the menu item label */
89 gint first_recent_item_pos;
90 GtkWidget *placeholder;
92 /* RecentChooser properties */
94 guint show_private : 1;
95 guint show_not_found : 1;
100 guint show_numbers : 1;
102 GtkRecentSortType sort_type;
103 GtkRecentSortFunc sort_func;
105 GDestroyNotify sort_data_destroy;
108 GtkRecentFilter *current_filter;
110 guint local_manager : 1;
111 gulong manager_changed_id;
120 /* activatable properties */
121 PROP_ACTIVATABLE_RELATED_ACTION,
122 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
126 #define FALLBACK_ICON_SIZE 32
127 #define FALLBACK_ITEM_LIMIT 10
128 #define DEFAULT_LABEL_WIDTH 30
130 #define GTK_RECENT_CHOOSER_MENU_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RECENT_CHOOSER_MENU, GtkRecentChooserMenuPrivate))
132 static void gtk_recent_chooser_menu_finalize (GObject *object);
133 static void gtk_recent_chooser_menu_dispose (GObject *object);
134 static GObject *gtk_recent_chooser_menu_constructor (GType type,
135 guint n_construct_properties,
136 GObjectConstructParam *construct_params);
138 static void gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface);
140 static void gtk_recent_chooser_menu_set_property (GObject *object,
144 static void gtk_recent_chooser_menu_get_property (GObject *object,
149 static gboolean gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
152 static gchar * gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser);
153 static gboolean gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
156 static void gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
158 static void gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser);
159 static void gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser);
160 static GList * gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser);
161 static GtkRecentManager *gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser);
162 static void gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser *chooser,
163 GtkRecentSortFunc sort_func,
165 GDestroyNotify data_destroy);
166 static void gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
167 GtkRecentFilter *filter);
168 static void gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
169 GtkRecentFilter *filter);
170 static GSList * gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser);
171 static void gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
172 GtkRecentFilter *filter);
174 static void gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu);
175 static void gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
178 static void set_recent_manager (GtkRecentChooserMenu *menu,
179 GtkRecentManager *manager);
181 static void chooser_set_sort_type (GtkRecentChooserMenu *menu,
182 GtkRecentSortType sort_type);
184 static gint get_icon_size_for_widget (GtkWidget *widget);
186 static void item_activate_cb (GtkWidget *widget,
188 static void manager_changed_cb (GtkRecentManager *manager,
191 static void gtk_recent_chooser_activatable_iface_init (GtkActivatableIface *iface);
192 static void gtk_recent_chooser_update (GtkActivatable *activatable,
194 const gchar *property_name);
195 static void gtk_recent_chooser_sync_action_properties (GtkActivatable *activatable,
198 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserMenu,
199 gtk_recent_chooser_menu,
201 G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
202 gtk_recent_chooser_iface_init)
203 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
204 gtk_recent_chooser_activatable_iface_init))
208 gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface)
210 iface->set_current_uri = gtk_recent_chooser_menu_set_current_uri;
211 iface->get_current_uri = gtk_recent_chooser_menu_get_current_uri;
212 iface->select_uri = gtk_recent_chooser_menu_select_uri;
213 iface->unselect_uri = gtk_recent_chooser_menu_unselect_uri;
214 iface->select_all = gtk_recent_chooser_menu_select_all;
215 iface->unselect_all = gtk_recent_chooser_menu_unselect_all;
216 iface->get_items = gtk_recent_chooser_menu_get_items;
217 iface->get_recent_manager = gtk_recent_chooser_menu_get_recent_manager;
218 iface->set_sort_func = gtk_recent_chooser_menu_set_sort_func;
219 iface->add_filter = gtk_recent_chooser_menu_add_filter;
220 iface->remove_filter = gtk_recent_chooser_menu_remove_filter;
221 iface->list_filters = gtk_recent_chooser_menu_list_filters;
225 gtk_recent_chooser_activatable_iface_init (GtkActivatableIface *iface)
227 iface->update = gtk_recent_chooser_update;
228 iface->sync_action_properties = gtk_recent_chooser_sync_action_properties;
232 gtk_recent_chooser_menu_class_init (GtkRecentChooserMenuClass *klass)
234 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
236 gobject_class->constructor = gtk_recent_chooser_menu_constructor;
237 gobject_class->dispose = gtk_recent_chooser_menu_dispose;
238 gobject_class->finalize = gtk_recent_chooser_menu_finalize;
239 gobject_class->set_property = gtk_recent_chooser_menu_set_property;
240 gobject_class->get_property = gtk_recent_chooser_menu_get_property;
242 _gtk_recent_chooser_install_properties (gobject_class);
245 * GtkRecentChooserMenu:show-numbers
247 * Whether the first ten items in the menu should be prepended by
248 * a number acting as a unique mnemonic.
252 g_object_class_install_property (gobject_class,
254 g_param_spec_boolean ("show-numbers",
256 P_("Whether the items should be displayed with a number"),
258 GTK_PARAM_READWRITE));
261 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
262 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
264 g_type_class_add_private (klass, sizeof (GtkRecentChooserMenuPrivate));
268 gtk_recent_chooser_menu_init (GtkRecentChooserMenu *menu)
270 GtkRecentChooserMenuPrivate *priv;
272 priv = GTK_RECENT_CHOOSER_MENU_GET_PRIVATE (menu);
276 priv->show_icons= TRUE;
277 priv->show_numbers = FALSE;
278 priv->show_tips = FALSE;
279 priv->show_not_found = TRUE;
280 priv->show_private = FALSE;
281 priv->local_only = TRUE;
283 priv->limit = FALLBACK_ITEM_LIMIT;
284 priv->sort_type = GTK_RECENT_SORT_NONE;
285 priv->icon_size = FALLBACK_ICON_SIZE;
286 priv->label_width = DEFAULT_LABEL_WIDTH;
288 priv->first_recent_item_pos = -1;
289 priv->placeholder = NULL;
291 priv->current_filter = NULL;
295 gtk_recent_chooser_menu_finalize (GObject *object)
297 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
298 GtkRecentChooserMenuPrivate *priv = menu->priv;
300 priv->manager = NULL;
302 if (priv->sort_data_destroy)
304 priv->sort_data_destroy (priv->sort_data);
305 priv->sort_data_destroy = NULL;
308 priv->sort_data = NULL;
309 priv->sort_func = NULL;
311 G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->finalize (object);
315 gtk_recent_chooser_menu_dispose (GObject *object)
317 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
318 GtkRecentChooserMenuPrivate *priv = menu->priv;
320 if (priv->manager_changed_id)
323 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
325 priv->manager_changed_id = 0;
328 if (priv->populate_id)
330 g_source_remove (priv->populate_id);
331 priv->populate_id = 0;
334 if (priv->current_filter)
336 g_object_unref (priv->current_filter);
337 priv->current_filter = NULL;
340 G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->dispose (object);
344 gtk_recent_chooser_menu_constructor (GType type,
346 GObjectConstructParam *params)
348 GtkRecentChooserMenu *menu;
349 GtkRecentChooserMenuPrivate *priv;
350 GObjectClass *parent_class;
353 parent_class = G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class);
354 object = parent_class->constructor (type, n_params, params);
355 menu = GTK_RECENT_CHOOSER_MENU (object);
358 g_assert (priv->manager);
360 /* we create a placeholder menuitem, to be used in case
361 * the menu is empty. this placeholder will stay around
362 * for the entire lifetime of the menu, and we just hide it
363 * when it's not used. we have to do this, and do it here,
364 * because we need a marker for the beginning of the recent
365 * items list, so that we can insert the new items at the
366 * right place when idly populating the menu in case the
367 * user appended or prepended custom menu items to the
368 * recent chooser menu widget.
370 priv->placeholder = gtk_menu_item_new_with_label (_("No items found"));
371 gtk_widget_set_sensitive (priv->placeholder, FALSE);
372 g_object_set_data (G_OBJECT (priv->placeholder),
373 "gtk-recent-menu-placeholder",
374 GINT_TO_POINTER (TRUE));
376 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), priv->placeholder, 0);
377 gtk_widget_set_no_show_all (priv->placeholder, TRUE);
378 gtk_widget_show (priv->placeholder);
380 /* (re)populate the menu */
381 gtk_recent_chooser_menu_populate (menu);
387 gtk_recent_chooser_menu_set_property (GObject *object,
392 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
393 GtkRecentChooserMenuPrivate *priv = menu->priv;
397 case PROP_SHOW_NUMBERS:
398 priv->show_numbers = g_value_get_boolean (value);
400 case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
401 set_recent_manager (menu, g_value_get_object (value));
403 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
404 priv->show_private = g_value_get_boolean (value);
406 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
407 priv->show_not_found = g_value_get_boolean (value);
409 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
410 gtk_recent_chooser_menu_set_show_tips (menu, g_value_get_boolean (value));
412 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
413 priv->show_icons = g_value_get_boolean (value);
415 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
416 g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
418 G_OBJECT_TYPE_NAME (object));
420 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
421 priv->local_only = g_value_get_boolean (value);
423 case GTK_RECENT_CHOOSER_PROP_LIMIT:
424 priv->limit = g_value_get_int (value);
426 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
427 chooser_set_sort_type (menu, g_value_get_enum (value));
429 case GTK_RECENT_CHOOSER_PROP_FILTER:
430 gtk_recent_chooser_menu_set_current_filter (menu, g_value_get_object (value));
432 case PROP_ACTIVATABLE_RELATED_ACTION:
433 _gtk_recent_chooser_set_related_action (GTK_RECENT_CHOOSER (menu), g_value_get_object (value));
435 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
436 _gtk_recent_chooser_set_use_action_appearance (GTK_RECENT_CHOOSER (menu), g_value_get_boolean (value));
439 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
445 gtk_recent_chooser_menu_get_property (GObject *object,
450 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
451 GtkRecentChooserMenuPrivate *priv = menu->priv;
455 case PROP_SHOW_NUMBERS:
456 g_value_set_boolean (value, priv->show_numbers);
458 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
459 g_value_set_boolean (value, priv->show_tips);
461 case GTK_RECENT_CHOOSER_PROP_LIMIT:
462 g_value_set_int (value, priv->limit);
464 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
465 g_value_set_boolean (value, priv->local_only);
467 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
468 g_value_set_enum (value, priv->sort_type);
470 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
471 g_value_set_boolean (value, priv->show_private);
473 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
474 g_value_set_boolean (value, priv->show_not_found);
476 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
477 g_value_set_boolean (value, priv->show_icons);
479 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
480 g_value_set_boolean (value, FALSE);
482 case GTK_RECENT_CHOOSER_PROP_FILTER:
483 g_value_set_object (value, priv->current_filter);
485 case PROP_ACTIVATABLE_RELATED_ACTION:
486 g_value_set_object (value, _gtk_recent_chooser_get_related_action (GTK_RECENT_CHOOSER (menu)));
488 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
489 g_value_set_boolean (value, _gtk_recent_chooser_get_use_action_appearance (GTK_RECENT_CHOOSER (menu)));
492 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
498 gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
502 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
504 GtkWidget *menu_item = NULL;
505 gboolean found = FALSE;
507 children = gtk_container_get_children (GTK_CONTAINER (menu));
509 for (l = children; l != NULL; l = l->next)
513 menu_item = GTK_WIDGET (l->data);
515 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
519 if (strcmp (uri, gtk_recent_info_get_uri (info)) == 0)
521 gtk_menu_shell_activate_item (GTK_MENU_SHELL (menu),
530 g_list_free (children);
534 g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
535 GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
536 _("No recently used resource found with URI `%s'"),
544 gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser)
546 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
547 GtkWidget *menu_item;
550 menu_item = gtk_menu_get_active (GTK_MENU (menu));
554 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
558 return g_strdup (gtk_recent_info_get_uri (info));
562 gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
566 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
568 GtkWidget *menu_item = NULL;
569 gboolean found = FALSE;
571 children = gtk_container_get_children (GTK_CONTAINER (menu));
572 for (l = children; l != NULL; l = l->next)
576 menu_item = GTK_WIDGET (l->data);
578 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
582 if (0 == strcmp (uri, gtk_recent_info_get_uri (info)))
586 g_list_free (children);
590 g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
591 GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
592 _("No recently used resource found with URI `%s'"),
598 gtk_menu_shell_select_item (GTK_MENU_SHELL (menu), menu_item);
605 gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
608 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
610 gtk_menu_shell_deselect (GTK_MENU_SHELL (menu));
614 gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser)
616 g_warning (_("This function is not implemented for "
617 "widgets of class '%s'"),
618 g_type_name (G_OBJECT_TYPE (chooser)));
622 gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser)
624 g_warning (_("This function is not implemented for "
625 "widgets of class '%s'"),
626 g_type_name (G_OBJECT_TYPE (chooser)));
630 gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser *chooser,
631 GtkRecentSortFunc sort_func,
633 GDestroyNotify data_destroy)
635 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
636 GtkRecentChooserMenuPrivate *priv = menu->priv;
638 if (priv->sort_data_destroy)
640 priv->sort_data_destroy (priv->sort_data);
641 priv->sort_data_destroy = NULL;
644 priv->sort_func = NULL;
645 priv->sort_data = NULL;
646 priv->sort_data_destroy = NULL;
650 priv->sort_func = sort_func;
651 priv->sort_data = sort_data;
652 priv->sort_data_destroy = data_destroy;
657 chooser_set_sort_type (GtkRecentChooserMenu *menu,
658 GtkRecentSortType sort_type)
660 if (menu->priv->sort_type == sort_type)
663 menu->priv->sort_type = sort_type;
668 gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser)
670 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
671 GtkRecentChooserMenuPrivate *priv = menu->priv;
673 return _gtk_recent_chooser_get_items (chooser,
674 priv->current_filter,
679 static GtkRecentManager *
680 gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser)
682 GtkRecentChooserMenuPrivate *priv;
684 priv = GTK_RECENT_CHOOSER_MENU (chooser)->priv;
686 return priv->manager;
690 gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
691 GtkRecentFilter *filter)
693 GtkRecentChooserMenu *menu;
695 menu = GTK_RECENT_CHOOSER_MENU (chooser);
697 gtk_recent_chooser_menu_set_current_filter (menu, filter);
701 gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
702 GtkRecentFilter *filter)
704 GtkRecentChooserMenu *menu;
706 menu = GTK_RECENT_CHOOSER_MENU (chooser);
708 if (filter == menu->priv->current_filter)
710 g_object_unref (menu->priv->current_filter);
711 menu->priv->current_filter = NULL;
713 g_object_notify (G_OBJECT (menu), "filter");
718 gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser)
720 GtkRecentChooserMenu *menu;
721 GSList *retval = NULL;
723 menu = GTK_RECENT_CHOOSER_MENU (chooser);
725 if (menu->priv->current_filter)
726 retval = g_slist_prepend (retval, menu->priv->current_filter);
732 gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
733 GtkRecentFilter *filter)
735 GtkRecentChooserMenuPrivate *priv;
739 if (priv->current_filter)
740 g_object_unref (G_OBJECT (priv->current_filter));
744 priv->current_filter = filter;
745 g_object_ref_sink (priv->current_filter);
748 gtk_recent_chooser_menu_populate (menu);
750 g_object_notify (G_OBJECT (menu), "filter");
753 /* taken from libeel/eel-strings.c */
755 escape_underscores (const gchar *string)
766 for (p = string; *p != '\0'; p++)
767 underscores += (*p == '_');
769 if (underscores == 0)
770 return g_strdup (string);
772 escaped = g_new (char, strlen (string) + underscores + 1);
773 for (p = string, q = escaped; *p != '\0'; p++, q++)
775 /* Add an extra underscore. */
788 gtk_recent_chooser_menu_add_tip (GtkRecentChooserMenu *menu,
792 GtkRecentChooserMenuPrivate *priv;
795 g_assert (info != NULL);
796 g_assert (item != NULL);
800 path = gtk_recent_info_get_uri_display (info);
803 gchar *tip_text = g_strdup_printf (_("Open '%s'"), path);
805 gtk_widget_set_tooltip_text (item, tip_text);
806 gtk_widget_set_has_tooltip (item, priv->show_tips);
814 gtk_recent_chooser_menu_create_item (GtkRecentChooserMenu *menu,
818 GtkRecentChooserMenuPrivate *priv;
820 GtkWidget *item, *image, *label;
823 g_assert (info != NULL);
827 if (priv->show_numbers)
829 gchar *name, *escaped;
831 name = g_strdup (gtk_recent_info_get_display_name (info));
833 name = g_strdup (_("Unknown item"));
835 escaped = escape_underscores (name);
837 /* avoid clashing mnemonics */
839 /* This is the label format that is used for the first 10 items
840 * in a recent files menu. The %d is the number of the item,
841 * the %s is the name of the item. Please keep the _ in front
842 * of the number to give these menu items a mnemonic.
844 text = g_strdup_printf (C_("recent menu label", "_%d. %s"), count, escaped);
846 /* This is the format that is used for items in a recent files menu.
847 * The %d is the number of the item, the %s is the name of the item.
849 text = g_strdup_printf (C_("recent menu label", "%d. %s"), count, escaped);
851 item = gtk_image_menu_item_new_with_mnemonic (text);
858 text = g_strdup (gtk_recent_info_get_display_name (info));
859 item = gtk_image_menu_item_new_with_label (text);
864 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item),
867 /* ellipsize the menu item label, in case the recent document
868 * display name is huge.
870 label = GTK_BIN (item)->child;
871 if (GTK_IS_LABEL (label))
873 gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
874 gtk_label_set_max_width_chars (GTK_LABEL (label), priv->label_width);
877 if (priv->show_icons)
879 icon = gtk_recent_info_get_icon (info, priv->icon_size);
881 image = gtk_image_new_from_pixbuf (icon);
882 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
883 g_object_unref (icon);
886 g_signal_connect (item, "activate",
887 G_CALLBACK (item_activate_cb),
894 gtk_recent_chooser_menu_insert_item (GtkRecentChooserMenu *menu,
898 GtkRecentChooserMenuPrivate *priv = menu->priv;
901 if (priv->first_recent_item_pos == -1)
905 children = gtk_container_get_children (GTK_CONTAINER (menu));
907 for (real_position = 0, l = children;
909 real_position += 1, l = l->next)
911 GObject *child = l->data;
912 gboolean is_placeholder = FALSE;
915 GPOINTER_TO_INT (g_object_get_data (child, "gtk-recent-menu-placeholder"));
921 g_list_free (children);
922 priv->first_recent_item_pos = real_position;
925 real_position = priv->first_recent_item_pos;
927 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), menuitem,
928 real_position + position);
929 gtk_widget_show (menuitem);
932 /* removes the items we own from the menu */
934 gtk_recent_chooser_menu_dispose_items (GtkRecentChooserMenu *menu)
938 children = gtk_container_get_children (GTK_CONTAINER (menu));
939 for (l = children; l != NULL; l = l->next)
941 GtkWidget *menu_item = GTK_WIDGET (l->data);
942 gboolean has_mark = FALSE;
944 /* check for our mark, in order to remove just the items we own */
946 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item), "gtk-recent-menu-mark"));
952 /* destroy the attached RecentInfo struct, if found */
953 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
955 g_object_set_data_full (G_OBJECT (menu_item), "gtk-recent-info",
958 /* and finally remove the item from the menu */
959 gtk_container_remove (GTK_CONTAINER (menu), menu_item);
963 /* recalculate the position of the first recent item */
964 menu->priv->first_recent_item_pos = -1;
966 g_list_free (children);
974 gint displayed_items;
975 GtkRecentChooserMenu *menu;
976 GtkWidget *placeholder;
980 idle_populate_func (gpointer data)
982 MenuPopulateData *pdata;
983 GtkRecentChooserMenuPrivate *priv;
988 pdata = (MenuPopulateData *) data;
989 priv = pdata->menu->priv;
993 pdata->items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (pdata->menu));
996 /* show the placeholder here */
997 gtk_widget_show (pdata->placeholder);
998 pdata->displayed_items = 1;
999 priv->populate_id = 0;
1004 gtk_widget_hide (pdata->placeholder);
1006 pdata->n_items = g_list_length (pdata->items);
1007 pdata->loaded_items = 0;
1010 info = g_list_nth_data (pdata->items, pdata->loaded_items);
1011 item = gtk_recent_chooser_menu_create_item (pdata->menu,
1013 pdata->displayed_items);
1015 goto check_and_return;
1017 gtk_recent_chooser_menu_add_tip (pdata->menu, info, item);
1018 gtk_recent_chooser_menu_insert_item (pdata->menu, item,
1019 pdata->displayed_items);
1021 pdata->displayed_items += 1;
1023 /* mark the menu item as one of our own */
1024 g_object_set_data (G_OBJECT (item),
1025 "gtk-recent-menu-mark",
1026 GINT_TO_POINTER (TRUE));
1028 /* attach the RecentInfo object to the menu item, and own a reference
1029 * to it, so that it will be destroyed with the menu item when it's
1030 * not needed anymore.
1032 g_object_set_data_full (G_OBJECT (item), "gtk-recent-info",
1033 gtk_recent_info_ref (info),
1034 (GDestroyNotify) gtk_recent_info_unref);
1037 pdata->loaded_items += 1;
1039 if (pdata->loaded_items == pdata->n_items)
1041 g_list_foreach (pdata->items, (GFunc) gtk_recent_info_unref, NULL);
1042 g_list_free (pdata->items);
1044 priv->populate_id = 0;
1055 idle_populate_clean_up (gpointer data)
1057 MenuPopulateData *pdata = data;
1059 if (pdata->menu->priv->populate_id == 0)
1061 /* show the placeholder in case no item survived
1062 * the filtering process in the idle loop
1064 if (!pdata->displayed_items)
1065 gtk_widget_show (pdata->placeholder);
1066 g_object_unref (pdata->placeholder);
1068 g_slice_free (MenuPopulateData, data);
1073 gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu)
1075 MenuPopulateData *pdata;
1076 GtkRecentChooserMenuPrivate *priv = menu->priv;
1078 if (menu->priv->populate_id)
1081 pdata = g_slice_new (MenuPopulateData);
1082 pdata->items = NULL;
1084 pdata->loaded_items = 0;
1085 pdata->displayed_items = 0;
1087 pdata->placeholder = g_object_ref (priv->placeholder);
1089 priv->icon_size = get_icon_size_for_widget (GTK_WIDGET (menu));
1091 /* remove our menu items first */
1092 gtk_recent_chooser_menu_dispose_items (menu);
1094 priv->populate_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
1097 idle_populate_clean_up);
1100 /* bounce activate signal from the recent menu item widget
1101 * to the recent menu widget
1104 item_activate_cb (GtkWidget *widget,
1107 GtkRecentChooser *chooser = GTK_RECENT_CHOOSER (user_data);
1109 _gtk_recent_chooser_item_activated (chooser);
1112 /* we force a redraw if the manager changes when we are showing */
1114 manager_changed_cb (GtkRecentManager *manager,
1117 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (user_data);
1119 gtk_recent_chooser_menu_populate (menu);
1123 set_recent_manager (GtkRecentChooserMenu *menu,
1124 GtkRecentManager *manager)
1126 GtkRecentChooserMenuPrivate *priv = menu->priv;
1130 if (priv->manager_changed_id)
1132 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
1133 priv->manager_changed_id = 0;
1136 if (priv->populate_id)
1138 g_source_remove (priv->populate_id);
1139 priv->populate_id = 0;
1142 priv->manager = NULL;
1146 priv->manager = manager;
1148 priv->manager = gtk_recent_manager_get_default ();
1151 priv->manager_changed_id = g_signal_connect (priv->manager, "changed",
1152 G_CALLBACK (manager_changed_cb),
1157 get_icon_size_for_widget (GtkWidget *widget)
1159 GtkSettings *settings;
1162 if (gtk_widget_has_screen (widget))
1163 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
1165 settings = gtk_settings_get_default ();
1167 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1169 return MAX (width, height);
1171 return FALLBACK_ICON_SIZE;
1175 foreach_set_shot_tips (GtkWidget *widget,
1178 GtkRecentChooserMenu *menu = user_data;
1179 GtkRecentChooserMenuPrivate *priv = menu->priv;
1182 /* toggle the tooltip only on the items we create */
1184 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "gtk-recent-menu-mark"));
1187 gtk_widget_set_has_tooltip (widget, priv->show_tips);
1191 gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
1194 GtkRecentChooserMenuPrivate *priv = menu->priv;
1196 if (priv->show_tips == show_tips)
1199 priv->show_tips = show_tips;
1200 gtk_container_foreach (GTK_CONTAINER (menu), foreach_set_shot_tips, menu);
1204 gtk_recent_chooser_update (GtkActivatable *activatable,
1206 const gchar *property_name)
1208 if (strcmp (property_name, "sensitive") == 0)
1209 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1211 _gtk_recent_chooser_update (activatable, action, property_name);
1215 gtk_recent_chooser_sync_action_properties (GtkActivatable *activatable,
1218 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1220 _gtk_recent_chooser_sync_action_properties (activatable, action);
1229 * gtk_recent_chooser_menu_new:
1231 * Creates a new #GtkRecentChooserMenu widget.
1233 * This kind of widget shows the list of recently used resources as
1234 * a menu, each item as a menu item. Each item inside the menu might
1235 * have an icon, representing its MIME type, and a number, for mnemonic
1238 * This widget implements the #GtkRecentChooser interface.
1240 * This widget creates its own #GtkRecentManager object. See the
1241 * gtk_recent_chooser_menu_new_for_manager() function to know how to create
1242 * a #GtkRecentChooserMenu widget bound to another #GtkRecentManager object.
1244 * Return value: a new #GtkRecentChooserMenu
1249 gtk_recent_chooser_menu_new (void)
1251 return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1252 "recent-manager", NULL,
1257 * gtk_recent_chooser_menu_new_for_manager:
1258 * @manager: a #GtkRecentManager
1260 * Creates a new #GtkRecentChooserMenu widget using @manager as
1261 * the underlying recently used resources manager.
1263 * This is useful if you have implemented your own recent manager,
1264 * or if you have a customized instance of a #GtkRecentManager
1265 * object or if you wish to share a common #GtkRecentManager object
1266 * among multiple #GtkRecentChooser widgets.
1268 * Return value: a new #GtkRecentChooserMenu, bound to @manager.
1273 gtk_recent_chooser_menu_new_for_manager (GtkRecentManager *manager)
1275 g_return_val_if_fail (manager == NULL || GTK_IS_RECENT_MANAGER (manager), NULL);
1277 return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1278 "recent-manager", manager,
1283 * gtk_recent_chooser_menu_get_show_numbers:
1284 * @menu: a #GtkRecentChooserMenu
1286 * Returns the value set by gtk_recent_chooser_menu_set_show_numbers().
1288 * Return value: %TRUE if numbers should be shown.
1293 gtk_recent_chooser_menu_get_show_numbers (GtkRecentChooserMenu *menu)
1295 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu), FALSE);
1297 return menu->priv->show_numbers;
1301 * gtk_recent_chooser_menu_set_show_numbers:
1302 * @menu: a #GtkRecentChooserMenu
1303 * @show_numbers: whether to show numbers
1305 * Sets whether a number should be added to the items of @menu. The
1306 * numbers are shown to provide a unique character for a mnemonic to
1307 * be used inside ten menu item's label. Only the first the items
1308 * get a number to avoid clashes.
1313 gtk_recent_chooser_menu_set_show_numbers (GtkRecentChooserMenu *menu,
1314 gboolean show_numbers)
1316 g_return_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu));
1318 if (menu->priv->show_numbers == show_numbers)
1321 menu->priv->show_numbers = show_numbers;
1322 g_object_notify (G_OBJECT (menu), "show-numbers");
1325 #define __GTK_RECENT_CHOOSER_MENU_C__
1326 #include "gtkaliasdef.c"