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 * SECTION:gtkrecentchoosermenu
51 * @Short_description: Displays recently used files in a menu
52 * @Title: GtkRecentChooserMenu
53 * @See_also:#GtkRecentChooser
55 * #GtkRecentChooserMenu is a widget suitable for displaying recently used files
56 * inside a menu. It can be used to set a sub-menu of a #GtkMenuItem using
57 * gtk_menu_item_set_submenu(), or as the menu of a #GtkMenuToolButton.
59 * Note that #GtkRecentChooserMenu does not have any methods of its own. Instead,
60 * you should use the functions that work on a #GtkRecentChooser.
62 * Note also that #GtkRecentChooserMenu does not support multiple filters, as it
63 * has no way to let the user choose between them as the #GtkRecentChooserWidget
64 * and #GtkRecentChooserDialog widgets do. Thus using gtk_recent_chooser_add_filter()
65 * on a #GtkRecentChooserMenu widget will yield the same effects as using
66 * gtk_recent_chooser_set_filter(), replacing any currently set filter
67 * with the supplied filter; gtk_recent_chooser_remove_filter() will remove
68 * any currently set #GtkRecentFilter object and will unset the current filter;
69 * gtk_recent_chooser_list_filters() will return a list containing a single
70 * #GtkRecentFilter object.
72 * Recently used files are supported since GTK+ 2.10.
76 struct _GtkRecentChooserMenuPrivate
78 /* the recent manager object */
79 GtkRecentManager *manager;
81 /* max size of the menu item label */
84 gint first_recent_item_pos;
85 GtkWidget *placeholder;
87 /* RecentChooser properties */
89 guint show_private : 1;
90 guint show_not_found : 1;
95 guint show_numbers : 1;
97 GtkRecentSortType sort_type;
98 GtkRecentSortFunc sort_func;
100 GDestroyNotify sort_data_destroy;
103 GtkRecentFilter *current_filter;
105 guint local_manager : 1;
106 gulong manager_changed_id;
115 /* activatable properties */
116 PROP_ACTIVATABLE_RELATED_ACTION,
117 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
121 #define FALLBACK_ITEM_LIMIT 10
122 #define DEFAULT_LABEL_WIDTH 30
125 static void gtk_recent_chooser_menu_finalize (GObject *object);
126 static void gtk_recent_chooser_menu_dispose (GObject *object);
127 static GObject *gtk_recent_chooser_menu_constructor (GType type,
128 guint n_construct_properties,
129 GObjectConstructParam *construct_params);
131 static void gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface);
133 static void gtk_recent_chooser_menu_set_property (GObject *object,
137 static void gtk_recent_chooser_menu_get_property (GObject *object,
142 static gboolean gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
145 static gchar * gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser);
146 static gboolean gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
149 static void gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
151 static void gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser);
152 static void gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser);
153 static GList * gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser);
154 static GtkRecentManager *gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser);
155 static void gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser *chooser,
156 GtkRecentSortFunc sort_func,
158 GDestroyNotify data_destroy);
159 static void gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
160 GtkRecentFilter *filter);
161 static void gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
162 GtkRecentFilter *filter);
163 static GSList * gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser);
164 static void gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
165 GtkRecentFilter *filter);
167 static void gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu);
168 static void gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
171 static void set_recent_manager (GtkRecentChooserMenu *menu,
172 GtkRecentManager *manager);
174 static void chooser_set_sort_type (GtkRecentChooserMenu *menu,
175 GtkRecentSortType sort_type);
177 static void item_activate_cb (GtkWidget *widget,
179 static void manager_changed_cb (GtkRecentManager *manager,
182 static void gtk_recent_chooser_activatable_iface_init (GtkActivatableIface *iface);
183 static void gtk_recent_chooser_update (GtkActivatable *activatable,
185 const gchar *property_name);
186 static void gtk_recent_chooser_sync_action_properties (GtkActivatable *activatable,
189 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserMenu,
190 gtk_recent_chooser_menu,
192 G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
193 gtk_recent_chooser_iface_init)
194 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
195 gtk_recent_chooser_activatable_iface_init))
199 gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface)
201 iface->set_current_uri = gtk_recent_chooser_menu_set_current_uri;
202 iface->get_current_uri = gtk_recent_chooser_menu_get_current_uri;
203 iface->select_uri = gtk_recent_chooser_menu_select_uri;
204 iface->unselect_uri = gtk_recent_chooser_menu_unselect_uri;
205 iface->select_all = gtk_recent_chooser_menu_select_all;
206 iface->unselect_all = gtk_recent_chooser_menu_unselect_all;
207 iface->get_items = gtk_recent_chooser_menu_get_items;
208 iface->get_recent_manager = gtk_recent_chooser_menu_get_recent_manager;
209 iface->set_sort_func = gtk_recent_chooser_menu_set_sort_func;
210 iface->add_filter = gtk_recent_chooser_menu_add_filter;
211 iface->remove_filter = gtk_recent_chooser_menu_remove_filter;
212 iface->list_filters = gtk_recent_chooser_menu_list_filters;
216 gtk_recent_chooser_activatable_iface_init (GtkActivatableIface *iface)
218 iface->update = gtk_recent_chooser_update;
219 iface->sync_action_properties = gtk_recent_chooser_sync_action_properties;
223 gtk_recent_chooser_menu_class_init (GtkRecentChooserMenuClass *klass)
225 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
227 gobject_class->constructor = gtk_recent_chooser_menu_constructor;
228 gobject_class->dispose = gtk_recent_chooser_menu_dispose;
229 gobject_class->finalize = gtk_recent_chooser_menu_finalize;
230 gobject_class->set_property = gtk_recent_chooser_menu_set_property;
231 gobject_class->get_property = gtk_recent_chooser_menu_get_property;
233 _gtk_recent_chooser_install_properties (gobject_class);
236 * GtkRecentChooserMenu:show-numbers
238 * Whether the first ten items in the menu should be prepended by
239 * a number acting as a unique mnemonic.
243 g_object_class_install_property (gobject_class,
245 g_param_spec_boolean ("show-numbers",
247 P_("Whether the items should be displayed with a number"),
249 GTK_PARAM_READWRITE));
252 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
253 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
255 g_type_class_add_private (klass, sizeof (GtkRecentChooserMenuPrivate));
259 gtk_recent_chooser_menu_init (GtkRecentChooserMenu *menu)
261 GtkRecentChooserMenuPrivate *priv;
263 menu->priv = G_TYPE_INSTANCE_GET_PRIVATE (menu,
264 GTK_TYPE_RECENT_CHOOSER_MENU,
265 GtkRecentChooserMenuPrivate);
269 priv->show_icons= TRUE;
270 priv->show_numbers = FALSE;
271 priv->show_tips = FALSE;
272 priv->show_not_found = TRUE;
273 priv->show_private = FALSE;
274 priv->local_only = TRUE;
276 priv->limit = FALLBACK_ITEM_LIMIT;
277 priv->sort_type = GTK_RECENT_SORT_NONE;
278 priv->label_width = DEFAULT_LABEL_WIDTH;
280 priv->first_recent_item_pos = -1;
281 priv->placeholder = NULL;
283 priv->current_filter = NULL;
287 gtk_recent_chooser_menu_finalize (GObject *object)
289 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
290 GtkRecentChooserMenuPrivate *priv = menu->priv;
292 priv->manager = NULL;
294 if (priv->sort_data_destroy)
296 priv->sort_data_destroy (priv->sort_data);
297 priv->sort_data_destroy = NULL;
300 priv->sort_data = NULL;
301 priv->sort_func = NULL;
303 G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->finalize (object);
307 gtk_recent_chooser_menu_dispose (GObject *object)
309 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
310 GtkRecentChooserMenuPrivate *priv = menu->priv;
312 if (priv->manager_changed_id)
315 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
317 priv->manager_changed_id = 0;
320 if (priv->populate_id)
322 g_source_remove (priv->populate_id);
323 priv->populate_id = 0;
326 if (priv->current_filter)
328 g_object_unref (priv->current_filter);
329 priv->current_filter = NULL;
332 G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->dispose (object);
336 gtk_recent_chooser_menu_constructor (GType type,
338 GObjectConstructParam *params)
340 GtkRecentChooserMenu *menu;
341 GtkRecentChooserMenuPrivate *priv;
342 GObjectClass *parent_class;
345 parent_class = G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class);
346 object = parent_class->constructor (type, n_params, params);
347 menu = GTK_RECENT_CHOOSER_MENU (object);
350 g_assert (priv->manager);
352 /* we create a placeholder menuitem, to be used in case
353 * the menu is empty. this placeholder will stay around
354 * for the entire lifetime of the menu, and we just hide it
355 * when it's not used. we have to do this, and do it here,
356 * because we need a marker for the beginning of the recent
357 * items list, so that we can insert the new items at the
358 * right place when idly populating the menu in case the
359 * user appended or prepended custom menu items to the
360 * recent chooser menu widget.
362 priv->placeholder = gtk_menu_item_new_with_label (_("No items found"));
363 gtk_widget_set_sensitive (priv->placeholder, FALSE);
364 g_object_set_data (G_OBJECT (priv->placeholder),
365 "gtk-recent-menu-placeholder",
366 GINT_TO_POINTER (TRUE));
368 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), priv->placeholder, 0);
369 gtk_widget_set_no_show_all (priv->placeholder, TRUE);
370 gtk_widget_show (priv->placeholder);
372 /* (re)populate the menu */
373 gtk_recent_chooser_menu_populate (menu);
379 gtk_recent_chooser_menu_set_property (GObject *object,
384 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
385 GtkRecentChooserMenuPrivate *priv = menu->priv;
389 case PROP_SHOW_NUMBERS:
390 priv->show_numbers = g_value_get_boolean (value);
392 case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
393 set_recent_manager (menu, g_value_get_object (value));
395 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
396 priv->show_private = g_value_get_boolean (value);
398 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
399 priv->show_not_found = g_value_get_boolean (value);
401 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
402 gtk_recent_chooser_menu_set_show_tips (menu, g_value_get_boolean (value));
404 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
405 priv->show_icons = g_value_get_boolean (value);
407 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
408 g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
410 G_OBJECT_TYPE_NAME (object));
412 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
413 priv->local_only = g_value_get_boolean (value);
415 case GTK_RECENT_CHOOSER_PROP_LIMIT:
416 priv->limit = g_value_get_int (value);
418 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
419 chooser_set_sort_type (menu, g_value_get_enum (value));
421 case GTK_RECENT_CHOOSER_PROP_FILTER:
422 gtk_recent_chooser_menu_set_current_filter (menu, g_value_get_object (value));
424 case PROP_ACTIVATABLE_RELATED_ACTION:
425 _gtk_recent_chooser_set_related_action (GTK_RECENT_CHOOSER (menu), g_value_get_object (value));
427 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
428 _gtk_recent_chooser_set_use_action_appearance (GTK_RECENT_CHOOSER (menu), g_value_get_boolean (value));
431 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
437 gtk_recent_chooser_menu_get_property (GObject *object,
442 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
443 GtkRecentChooserMenuPrivate *priv = menu->priv;
447 case PROP_SHOW_NUMBERS:
448 g_value_set_boolean (value, priv->show_numbers);
450 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
451 g_value_set_boolean (value, priv->show_tips);
453 case GTK_RECENT_CHOOSER_PROP_LIMIT:
454 g_value_set_int (value, priv->limit);
456 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
457 g_value_set_boolean (value, priv->local_only);
459 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
460 g_value_set_enum (value, priv->sort_type);
462 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
463 g_value_set_boolean (value, priv->show_private);
465 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
466 g_value_set_boolean (value, priv->show_not_found);
468 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
469 g_value_set_boolean (value, priv->show_icons);
471 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
472 g_value_set_boolean (value, FALSE);
474 case GTK_RECENT_CHOOSER_PROP_FILTER:
475 g_value_set_object (value, priv->current_filter);
477 case PROP_ACTIVATABLE_RELATED_ACTION:
478 g_value_set_object (value, _gtk_recent_chooser_get_related_action (GTK_RECENT_CHOOSER (menu)));
480 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
481 g_value_set_boolean (value, _gtk_recent_chooser_get_use_action_appearance (GTK_RECENT_CHOOSER (menu)));
484 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
490 gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
494 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
496 GtkWidget *menu_item = NULL;
497 gboolean found = FALSE;
499 children = gtk_container_get_children (GTK_CONTAINER (menu));
501 for (l = children; l != NULL; l = l->next)
505 menu_item = GTK_WIDGET (l->data);
507 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
511 if (strcmp (uri, gtk_recent_info_get_uri (info)) == 0)
513 gtk_menu_shell_activate_item (GTK_MENU_SHELL (menu),
522 g_list_free (children);
526 g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
527 GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
528 _("No recently used resource found with URI `%s'"),
536 gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser)
538 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
539 GtkWidget *menu_item;
542 menu_item = gtk_menu_get_active (GTK_MENU (menu));
546 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
550 return g_strdup (gtk_recent_info_get_uri (info));
554 gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
558 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
560 GtkWidget *menu_item = NULL;
561 gboolean found = FALSE;
563 children = gtk_container_get_children (GTK_CONTAINER (menu));
564 for (l = children; l != NULL; l = l->next)
568 menu_item = GTK_WIDGET (l->data);
570 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
574 if (0 == strcmp (uri, gtk_recent_info_get_uri (info)))
578 g_list_free (children);
582 g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
583 GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
584 _("No recently used resource found with URI `%s'"),
590 gtk_menu_shell_select_item (GTK_MENU_SHELL (menu), menu_item);
597 gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
600 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
602 gtk_menu_shell_deselect (GTK_MENU_SHELL (menu));
606 gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser)
608 g_warning ("This function is not implemented for "
609 "widgets of class '%s'",
610 g_type_name (G_OBJECT_TYPE (chooser)));
614 gtk_recent_chooser_menu_unselect_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_set_sort_func (GtkRecentChooser *chooser,
623 GtkRecentSortFunc sort_func,
625 GDestroyNotify data_destroy)
627 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
628 GtkRecentChooserMenuPrivate *priv = menu->priv;
630 if (priv->sort_data_destroy)
632 priv->sort_data_destroy (priv->sort_data);
633 priv->sort_data_destroy = NULL;
636 priv->sort_func = NULL;
637 priv->sort_data = NULL;
638 priv->sort_data_destroy = NULL;
642 priv->sort_func = sort_func;
643 priv->sort_data = sort_data;
644 priv->sort_data_destroy = data_destroy;
649 chooser_set_sort_type (GtkRecentChooserMenu *menu,
650 GtkRecentSortType sort_type)
652 if (menu->priv->sort_type == sort_type)
655 menu->priv->sort_type = sort_type;
660 gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser)
662 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
663 GtkRecentChooserMenuPrivate *priv = menu->priv;
665 return _gtk_recent_chooser_get_items (chooser,
666 priv->current_filter,
671 static GtkRecentManager *
672 gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser)
674 GtkRecentChooserMenuPrivate *priv;
676 priv = GTK_RECENT_CHOOSER_MENU (chooser)->priv;
678 return priv->manager;
682 gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
683 GtkRecentFilter *filter)
685 GtkRecentChooserMenu *menu;
687 menu = GTK_RECENT_CHOOSER_MENU (chooser);
689 gtk_recent_chooser_menu_set_current_filter (menu, filter);
693 gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
694 GtkRecentFilter *filter)
696 GtkRecentChooserMenu *menu;
698 menu = GTK_RECENT_CHOOSER_MENU (chooser);
700 if (filter == menu->priv->current_filter)
702 g_object_unref (menu->priv->current_filter);
703 menu->priv->current_filter = NULL;
705 g_object_notify (G_OBJECT (menu), "filter");
710 gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser)
712 GtkRecentChooserMenu *menu;
713 GSList *retval = NULL;
715 menu = GTK_RECENT_CHOOSER_MENU (chooser);
717 if (menu->priv->current_filter)
718 retval = g_slist_prepend (retval, menu->priv->current_filter);
724 gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
725 GtkRecentFilter *filter)
727 GtkRecentChooserMenuPrivate *priv;
731 if (priv->current_filter)
732 g_object_unref (G_OBJECT (priv->current_filter));
734 priv->current_filter = filter;
736 if (priv->current_filter)
737 g_object_ref_sink (priv->current_filter);
739 gtk_recent_chooser_menu_populate (menu);
741 g_object_notify (G_OBJECT (menu), "filter");
744 /* taken from libeel/eel-strings.c */
746 escape_underscores (const gchar *string)
757 for (p = string; *p != '\0'; p++)
758 underscores += (*p == '_');
760 if (underscores == 0)
761 return g_strdup (string);
763 escaped = g_new (char, strlen (string) + underscores + 1);
764 for (p = string, q = escaped; *p != '\0'; p++, q++)
766 /* Add an extra underscore. */
779 gtk_recent_chooser_menu_add_tip (GtkRecentChooserMenu *menu,
783 GtkRecentChooserMenuPrivate *priv;
786 g_assert (info != NULL);
787 g_assert (item != NULL);
791 path = gtk_recent_info_get_uri_display (info);
794 gchar *tip_text = g_strdup_printf (_("Open '%s'"), path);
796 gtk_widget_set_tooltip_text (item, tip_text);
797 gtk_widget_set_has_tooltip (item, priv->show_tips);
805 gtk_recent_chooser_menu_create_item (GtkRecentChooserMenu *menu,
809 GtkRecentChooserMenuPrivate *priv;
811 GtkWidget *item, *image, *label;
814 g_assert (info != NULL);
818 if (priv->show_numbers)
820 gchar *name, *escaped;
822 name = g_strdup (gtk_recent_info_get_display_name (info));
824 name = g_strdup (_("Unknown item"));
826 escaped = escape_underscores (name);
828 /* avoid clashing mnemonics */
830 /* This is the label format that is used for the first 10 items
831 * in a recent files menu. The %d is the number of the item,
832 * the %s is the name of the item. Please keep the _ in front
833 * of the number to give these menu items a mnemonic.
835 text = g_strdup_printf (C_("recent menu label", "_%d. %s"), count, escaped);
837 /* This is the format that is used for items in a recent files menu.
838 * The %d is the number of the item, the %s is the name of the item.
840 text = g_strdup_printf (C_("recent menu label", "%d. %s"), count, escaped);
842 item = gtk_image_menu_item_new_with_mnemonic (text);
849 text = g_strdup (gtk_recent_info_get_display_name (info));
850 item = gtk_image_menu_item_new_with_label (text);
855 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item),
858 /* ellipsize the menu item label, in case the recent document
859 * display name is huge.
861 label = gtk_bin_get_child (GTK_BIN (item));
862 if (GTK_IS_LABEL (label))
864 gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
865 gtk_label_set_max_width_chars (GTK_LABEL (label), priv->label_width);
868 if (priv->show_icons)
870 icon = gtk_recent_info_get_gicon (info);
872 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_MENU);
873 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
874 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
876 g_object_unref (icon);
879 g_signal_connect (item, "activate",
880 G_CALLBACK (item_activate_cb),
887 gtk_recent_chooser_menu_insert_item (GtkRecentChooserMenu *menu,
891 GtkRecentChooserMenuPrivate *priv = menu->priv;
894 if (priv->first_recent_item_pos == -1)
898 children = gtk_container_get_children (GTK_CONTAINER (menu));
900 for (real_position = 0, l = children;
902 real_position += 1, l = l->next)
904 GObject *child = l->data;
905 gboolean is_placeholder = FALSE;
908 GPOINTER_TO_INT (g_object_get_data (child, "gtk-recent-menu-placeholder"));
914 g_list_free (children);
915 priv->first_recent_item_pos = real_position;
918 real_position = priv->first_recent_item_pos;
920 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), menuitem,
921 real_position + position);
922 gtk_widget_show (menuitem);
925 /* removes the items we own from the menu */
927 gtk_recent_chooser_menu_dispose_items (GtkRecentChooserMenu *menu)
931 children = gtk_container_get_children (GTK_CONTAINER (menu));
932 for (l = children; l != NULL; l = l->next)
934 GtkWidget *menu_item = GTK_WIDGET (l->data);
935 gboolean has_mark = FALSE;
937 /* check for our mark, in order to remove just the items we own */
939 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item), "gtk-recent-menu-mark"));
945 /* destroy the attached RecentInfo struct, if found */
946 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
948 g_object_set_data_full (G_OBJECT (menu_item), "gtk-recent-info",
951 /* and finally remove the item from the menu */
952 gtk_container_remove (GTK_CONTAINER (menu), menu_item);
956 /* recalculate the position of the first recent item */
957 menu->priv->first_recent_item_pos = -1;
959 g_list_free (children);
967 gint displayed_items;
968 GtkRecentChooserMenu *menu;
969 GtkWidget *placeholder;
972 static MenuPopulateData *
973 create_menu_populate_data (GtkRecentChooserMenu *menu)
975 MenuPopulateData *pdata;
977 pdata = g_slice_new (MenuPopulateData);
980 pdata->loaded_items = 0;
981 pdata->displayed_items = 0;
983 pdata->placeholder = g_object_ref (menu->priv->placeholder);
989 free_menu_populate_data (MenuPopulateData *pdata)
991 if (pdata->placeholder)
992 g_object_unref (pdata->placeholder);
993 g_slice_free (MenuPopulateData, pdata);
997 idle_populate_func (gpointer data)
999 MenuPopulateData *pdata;
1000 GtkRecentChooserMenuPrivate *priv;
1001 GtkRecentInfo *info;
1005 pdata = (MenuPopulateData *) data;
1006 priv = pdata->menu->priv;
1010 pdata->items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (pdata->menu));
1013 /* show the placeholder here */
1014 gtk_widget_show (pdata->placeholder);
1015 pdata->displayed_items = 1;
1016 priv->populate_id = 0;
1021 gtk_widget_hide (pdata->placeholder);
1023 pdata->n_items = g_list_length (pdata->items);
1024 pdata->loaded_items = 0;
1027 info = g_list_nth_data (pdata->items, pdata->loaded_items);
1028 item = gtk_recent_chooser_menu_create_item (pdata->menu,
1030 pdata->displayed_items);
1032 goto check_and_return;
1034 gtk_recent_chooser_menu_add_tip (pdata->menu, info, item);
1035 gtk_recent_chooser_menu_insert_item (pdata->menu, item,
1036 pdata->displayed_items);
1038 pdata->displayed_items += 1;
1040 /* mark the menu item as one of our own */
1041 g_object_set_data (G_OBJECT (item),
1042 "gtk-recent-menu-mark",
1043 GINT_TO_POINTER (TRUE));
1045 /* attach the RecentInfo object to the menu item, and own a reference
1046 * to it, so that it will be destroyed with the menu item when it's
1047 * not needed anymore.
1049 g_object_set_data_full (G_OBJECT (item), "gtk-recent-info",
1050 gtk_recent_info_ref (info),
1051 (GDestroyNotify) gtk_recent_info_unref);
1054 pdata->loaded_items += 1;
1056 if (pdata->loaded_items == pdata->n_items)
1058 g_list_foreach (pdata->items, (GFunc) gtk_recent_info_unref, NULL);
1059 g_list_free (pdata->items);
1061 priv->populate_id = 0;
1072 idle_populate_clean_up (gpointer data)
1074 MenuPopulateData *pdata = data;
1076 if (pdata->menu->priv->populate_id == 0)
1078 /* show the placeholder in case no item survived
1079 * the filtering process in the idle loop
1081 if (!pdata->displayed_items)
1082 gtk_widget_show (pdata->placeholder);
1085 free_menu_populate_data (pdata);
1089 gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu)
1091 MenuPopulateData *pdata;
1092 GtkRecentChooserMenuPrivate *priv = menu->priv;
1094 if (priv->populate_id)
1097 pdata = create_menu_populate_data (menu);
1099 /* remove our menu items first */
1100 gtk_recent_chooser_menu_dispose_items (menu);
1102 priv->populate_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
1105 idle_populate_clean_up);
1108 /* bounce activate signal from the recent menu item widget
1109 * to the recent menu widget
1112 item_activate_cb (GtkWidget *widget,
1115 GtkRecentChooser *chooser = GTK_RECENT_CHOOSER (user_data);
1117 _gtk_recent_chooser_item_activated (chooser);
1120 /* we force a redraw if the manager changes when we are showing */
1122 manager_changed_cb (GtkRecentManager *manager,
1125 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (user_data);
1127 gtk_recent_chooser_menu_populate (menu);
1131 set_recent_manager (GtkRecentChooserMenu *menu,
1132 GtkRecentManager *manager)
1134 GtkRecentChooserMenuPrivate *priv = menu->priv;
1138 if (priv->manager_changed_id)
1140 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
1141 priv->manager_changed_id = 0;
1144 if (priv->populate_id)
1146 g_source_remove (priv->populate_id);
1147 priv->populate_id = 0;
1150 priv->manager = NULL;
1154 priv->manager = manager;
1156 priv->manager = gtk_recent_manager_get_default ();
1159 priv->manager_changed_id = g_signal_connect (priv->manager, "changed",
1160 G_CALLBACK (manager_changed_cb),
1165 foreach_set_shot_tips (GtkWidget *widget,
1168 GtkRecentChooserMenu *menu = user_data;
1169 GtkRecentChooserMenuPrivate *priv = menu->priv;
1172 /* toggle the tooltip only on the items we create */
1174 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "gtk-recent-menu-mark"));
1177 gtk_widget_set_has_tooltip (widget, priv->show_tips);
1181 gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
1184 GtkRecentChooserMenuPrivate *priv = menu->priv;
1186 if (priv->show_tips == show_tips)
1189 priv->show_tips = show_tips;
1190 gtk_container_foreach (GTK_CONTAINER (menu), foreach_set_shot_tips, menu);
1194 gtk_recent_chooser_update (GtkActivatable *activatable,
1196 const gchar *property_name)
1198 if (strcmp (property_name, "sensitive") == 0)
1199 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1201 _gtk_recent_chooser_update (activatable, action, property_name);
1205 gtk_recent_chooser_sync_action_properties (GtkActivatable *activatable,
1208 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1210 _gtk_recent_chooser_sync_action_properties (activatable, action);
1219 * gtk_recent_chooser_menu_new:
1221 * Creates a new #GtkRecentChooserMenu widget.
1223 * This kind of widget shows the list of recently used resources as
1224 * a menu, each item as a menu item. Each item inside the menu might
1225 * have an icon, representing its MIME type, and a number, for mnemonic
1228 * This widget implements the #GtkRecentChooser interface.
1230 * This widget creates its own #GtkRecentManager object. See the
1231 * gtk_recent_chooser_menu_new_for_manager() function to know how to create
1232 * a #GtkRecentChooserMenu widget bound to another #GtkRecentManager object.
1234 * Return value: a new #GtkRecentChooserMenu
1239 gtk_recent_chooser_menu_new (void)
1241 return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1242 "recent-manager", NULL,
1247 * gtk_recent_chooser_menu_new_for_manager:
1248 * @manager: a #GtkRecentManager
1250 * Creates a new #GtkRecentChooserMenu widget using @manager as
1251 * the underlying recently used resources manager.
1253 * This is useful if you have implemented your own recent manager,
1254 * or if you have a customized instance of a #GtkRecentManager
1255 * object or if you wish to share a common #GtkRecentManager object
1256 * among multiple #GtkRecentChooser widgets.
1258 * Return value: a new #GtkRecentChooserMenu, bound to @manager.
1263 gtk_recent_chooser_menu_new_for_manager (GtkRecentManager *manager)
1265 g_return_val_if_fail (manager == NULL || GTK_IS_RECENT_MANAGER (manager), NULL);
1267 return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1268 "recent-manager", manager,
1273 * gtk_recent_chooser_menu_get_show_numbers:
1274 * @menu: a #GtkRecentChooserMenu
1276 * Returns the value set by gtk_recent_chooser_menu_set_show_numbers().
1278 * Return value: %TRUE if numbers should be shown.
1283 gtk_recent_chooser_menu_get_show_numbers (GtkRecentChooserMenu *menu)
1285 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu), FALSE);
1287 return menu->priv->show_numbers;
1291 * gtk_recent_chooser_menu_set_show_numbers:
1292 * @menu: a #GtkRecentChooserMenu
1293 * @show_numbers: whether to show numbers
1295 * Sets whether a number should be added to the items of @menu. The
1296 * numbers are shown to provide a unique character for a mnemonic to
1297 * be used inside ten menu item's label. Only the first the items
1298 * get a number to avoid clashes.
1303 gtk_recent_chooser_menu_set_show_numbers (GtkRecentChooserMenu *menu,
1304 gboolean show_numbers)
1306 g_return_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu));
1308 if (menu->priv->show_numbers == show_numbers)
1311 menu->priv->show_numbers = show_numbers;
1312 g_object_notify (G_OBJECT (menu), "show-numbers");