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, see <http://www.gnu.org/licenses/>.
23 #include "gtkrecentmanager.h"
24 #include "gtkrecentfilter.h"
25 #include "gtkrecentchooser.h"
26 #include "gtkrecentchooserutils.h"
27 #include "gtkrecentchooserprivate.h"
28 #include "gtkrecentchoosermenu.h"
31 #include "gtkicontheme.h"
32 #include "gtkiconfactory.h"
34 #include "gtksettings.h"
35 #include "gtkmenushell.h"
36 #include "gtkmenuitem.h"
37 #include "gtkimagemenuitem.h"
38 #include "gtkseparatormenuitem.h"
42 #include "gtktooltip.h"
43 #include "gtkactivatable.h"
44 #include "gtktypebuiltins.h"
45 #include "gtkprivate.h"
48 * SECTION:gtkrecentchoosermenu
49 * @Short_description: Displays recently used files in a menu
50 * @Title: GtkRecentChooserMenu
51 * @See_also:#GtkRecentChooser
53 * #GtkRecentChooserMenu is a widget suitable for displaying recently used files
54 * inside a menu. It can be used to set a sub-menu of a #GtkMenuItem using
55 * gtk_menu_item_set_submenu(), or as the menu of a #GtkMenuToolButton.
57 * Note that #GtkRecentChooserMenu does not have any methods of its own. Instead,
58 * you should use the functions that work on a #GtkRecentChooser.
60 * Note also that #GtkRecentChooserMenu does not support multiple filters, as it
61 * has no way to let the user choose between them as the #GtkRecentChooserWidget
62 * and #GtkRecentChooserDialog widgets do. Thus using gtk_recent_chooser_add_filter()
63 * on a #GtkRecentChooserMenu widget will yield the same effects as using
64 * gtk_recent_chooser_set_filter(), replacing any currently set filter
65 * with the supplied filter; gtk_recent_chooser_remove_filter() will remove
66 * any currently set #GtkRecentFilter object and will unset the current filter;
67 * gtk_recent_chooser_list_filters() will return a list containing a single
68 * #GtkRecentFilter object.
70 * Recently used files are supported since GTK+ 2.10.
74 struct _GtkRecentChooserMenuPrivate
76 /* the recent manager object */
77 GtkRecentManager *manager;
79 /* max size of the menu item label */
82 gint first_recent_item_pos;
83 GtkWidget *placeholder;
85 /* RecentChooser properties */
87 guint show_private : 1;
88 guint show_not_found : 1;
93 guint show_numbers : 1;
95 GtkRecentSortType sort_type;
96 GtkRecentSortFunc sort_func;
98 GDestroyNotify sort_data_destroy;
101 GtkRecentFilter *current_filter;
103 guint local_manager : 1;
104 gulong manager_changed_id;
113 /* activatable properties */
114 PROP_ACTIVATABLE_RELATED_ACTION,
115 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
119 #define FALLBACK_ITEM_LIMIT 10
120 #define DEFAULT_LABEL_WIDTH 30
123 static void gtk_recent_chooser_menu_finalize (GObject *object);
124 static void gtk_recent_chooser_menu_dispose (GObject *object);
125 static GObject *gtk_recent_chooser_menu_constructor (GType type,
126 guint n_construct_properties,
127 GObjectConstructParam *construct_params);
129 static void gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface);
131 static void gtk_recent_chooser_menu_set_property (GObject *object,
135 static void gtk_recent_chooser_menu_get_property (GObject *object,
140 static gboolean gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
143 static gchar * gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser);
144 static gboolean gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
147 static void gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
149 static void gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser);
150 static void gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser);
151 static GList * gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser);
152 static GtkRecentManager *gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser);
153 static void gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser *chooser,
154 GtkRecentSortFunc sort_func,
156 GDestroyNotify data_destroy);
157 static void gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
158 GtkRecentFilter *filter);
159 static void gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
160 GtkRecentFilter *filter);
161 static GSList * gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser);
162 static void gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
163 GtkRecentFilter *filter);
165 static void gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu);
166 static void gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
169 static void set_recent_manager (GtkRecentChooserMenu *menu,
170 GtkRecentManager *manager);
172 static void chooser_set_sort_type (GtkRecentChooserMenu *menu,
173 GtkRecentSortType sort_type);
175 static void item_activate_cb (GtkWidget *widget,
177 static void manager_changed_cb (GtkRecentManager *manager,
180 static void gtk_recent_chooser_activatable_iface_init (GtkActivatableIface *iface);
181 static void gtk_recent_chooser_update (GtkActivatable *activatable,
183 const gchar *property_name);
184 static void gtk_recent_chooser_sync_action_properties (GtkActivatable *activatable,
187 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserMenu,
188 gtk_recent_chooser_menu,
190 G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
191 gtk_recent_chooser_iface_init)
192 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
193 gtk_recent_chooser_activatable_iface_init))
197 gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface)
199 iface->set_current_uri = gtk_recent_chooser_menu_set_current_uri;
200 iface->get_current_uri = gtk_recent_chooser_menu_get_current_uri;
201 iface->select_uri = gtk_recent_chooser_menu_select_uri;
202 iface->unselect_uri = gtk_recent_chooser_menu_unselect_uri;
203 iface->select_all = gtk_recent_chooser_menu_select_all;
204 iface->unselect_all = gtk_recent_chooser_menu_unselect_all;
205 iface->get_items = gtk_recent_chooser_menu_get_items;
206 iface->get_recent_manager = gtk_recent_chooser_menu_get_recent_manager;
207 iface->set_sort_func = gtk_recent_chooser_menu_set_sort_func;
208 iface->add_filter = gtk_recent_chooser_menu_add_filter;
209 iface->remove_filter = gtk_recent_chooser_menu_remove_filter;
210 iface->list_filters = gtk_recent_chooser_menu_list_filters;
214 gtk_recent_chooser_activatable_iface_init (GtkActivatableIface *iface)
216 iface->update = gtk_recent_chooser_update;
217 iface->sync_action_properties = gtk_recent_chooser_sync_action_properties;
221 gtk_recent_chooser_menu_class_init (GtkRecentChooserMenuClass *klass)
223 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
225 gobject_class->constructor = gtk_recent_chooser_menu_constructor;
226 gobject_class->dispose = gtk_recent_chooser_menu_dispose;
227 gobject_class->finalize = gtk_recent_chooser_menu_finalize;
228 gobject_class->set_property = gtk_recent_chooser_menu_set_property;
229 gobject_class->get_property = gtk_recent_chooser_menu_get_property;
231 _gtk_recent_chooser_install_properties (gobject_class);
234 * GtkRecentChooserMenu:show-numbers:
236 * Whether the first ten items in the menu should be prepended by
237 * a number acting as a unique mnemonic.
241 g_object_class_install_property (gobject_class,
243 g_param_spec_boolean ("show-numbers",
245 P_("Whether the items should be displayed with a number"),
247 GTK_PARAM_READWRITE));
250 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
251 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
253 g_type_class_add_private (klass, sizeof (GtkRecentChooserMenuPrivate));
257 gtk_recent_chooser_menu_init (GtkRecentChooserMenu *menu)
259 GtkRecentChooserMenuPrivate *priv;
261 menu->priv = G_TYPE_INSTANCE_GET_PRIVATE (menu,
262 GTK_TYPE_RECENT_CHOOSER_MENU,
263 GtkRecentChooserMenuPrivate);
267 priv->show_icons= TRUE;
268 priv->show_numbers = FALSE;
269 priv->show_tips = FALSE;
270 priv->show_not_found = TRUE;
271 priv->show_private = FALSE;
272 priv->local_only = TRUE;
274 priv->limit = FALLBACK_ITEM_LIMIT;
275 priv->sort_type = GTK_RECENT_SORT_NONE;
276 priv->label_width = DEFAULT_LABEL_WIDTH;
278 priv->first_recent_item_pos = -1;
279 priv->placeholder = NULL;
281 priv->current_filter = NULL;
285 gtk_recent_chooser_menu_finalize (GObject *object)
287 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
288 GtkRecentChooserMenuPrivate *priv = menu->priv;
290 priv->manager = NULL;
292 if (priv->sort_data_destroy)
294 priv->sort_data_destroy (priv->sort_data);
295 priv->sort_data_destroy = NULL;
298 priv->sort_data = NULL;
299 priv->sort_func = NULL;
301 G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->finalize (object);
305 gtk_recent_chooser_menu_dispose (GObject *object)
307 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
308 GtkRecentChooserMenuPrivate *priv = menu->priv;
310 if (priv->manager_changed_id)
313 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
315 priv->manager_changed_id = 0;
318 if (priv->populate_id)
320 g_source_remove (priv->populate_id);
321 priv->populate_id = 0;
324 if (priv->current_filter)
326 g_object_unref (priv->current_filter);
327 priv->current_filter = NULL;
330 G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->dispose (object);
334 gtk_recent_chooser_menu_constructor (GType type,
336 GObjectConstructParam *params)
338 GtkRecentChooserMenu *menu;
339 GtkRecentChooserMenuPrivate *priv;
340 GObjectClass *parent_class;
343 parent_class = G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class);
344 object = parent_class->constructor (type, n_params, params);
345 menu = GTK_RECENT_CHOOSER_MENU (object);
348 g_assert (priv->manager);
350 /* we create a placeholder menuitem, to be used in case
351 * the menu is empty. this placeholder will stay around
352 * for the entire lifetime of the menu, and we just hide it
353 * when it's not used. we have to do this, and do it here,
354 * because we need a marker for the beginning of the recent
355 * items list, so that we can insert the new items at the
356 * right place when idly populating the menu in case the
357 * user appended or prepended custom menu items to the
358 * recent chooser menu widget.
360 priv->placeholder = gtk_menu_item_new_with_label (_("No items found"));
361 gtk_widget_set_sensitive (priv->placeholder, FALSE);
362 g_object_set_data (G_OBJECT (priv->placeholder),
363 "gtk-recent-menu-placeholder",
364 GINT_TO_POINTER (TRUE));
366 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), priv->placeholder, 0);
367 gtk_widget_set_no_show_all (priv->placeholder, TRUE);
368 gtk_widget_show (priv->placeholder);
370 /* (re)populate the menu */
371 gtk_recent_chooser_menu_populate (menu);
377 gtk_recent_chooser_menu_set_property (GObject *object,
382 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
383 GtkRecentChooserMenuPrivate *priv = menu->priv;
387 case PROP_SHOW_NUMBERS:
388 priv->show_numbers = g_value_get_boolean (value);
390 case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
391 set_recent_manager (menu, g_value_get_object (value));
393 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
394 priv->show_private = g_value_get_boolean (value);
396 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
397 priv->show_not_found = g_value_get_boolean (value);
399 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
400 gtk_recent_chooser_menu_set_show_tips (menu, g_value_get_boolean (value));
402 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
403 priv->show_icons = g_value_get_boolean (value);
405 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
406 g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
408 G_OBJECT_TYPE_NAME (object));
410 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
411 priv->local_only = g_value_get_boolean (value);
413 case GTK_RECENT_CHOOSER_PROP_LIMIT:
414 priv->limit = g_value_get_int (value);
416 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
417 chooser_set_sort_type (menu, g_value_get_enum (value));
419 case GTK_RECENT_CHOOSER_PROP_FILTER:
420 gtk_recent_chooser_menu_set_current_filter (menu, g_value_get_object (value));
422 case PROP_ACTIVATABLE_RELATED_ACTION:
423 _gtk_recent_chooser_set_related_action (GTK_RECENT_CHOOSER (menu), g_value_get_object (value));
425 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
426 _gtk_recent_chooser_set_use_action_appearance (GTK_RECENT_CHOOSER (menu), g_value_get_boolean (value));
429 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
435 gtk_recent_chooser_menu_get_property (GObject *object,
440 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
441 GtkRecentChooserMenuPrivate *priv = menu->priv;
445 case PROP_SHOW_NUMBERS:
446 g_value_set_boolean (value, priv->show_numbers);
448 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
449 g_value_set_boolean (value, priv->show_tips);
451 case GTK_RECENT_CHOOSER_PROP_LIMIT:
452 g_value_set_int (value, priv->limit);
454 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
455 g_value_set_boolean (value, priv->local_only);
457 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
458 g_value_set_enum (value, priv->sort_type);
460 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
461 g_value_set_boolean (value, priv->show_private);
463 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
464 g_value_set_boolean (value, priv->show_not_found);
466 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
467 g_value_set_boolean (value, priv->show_icons);
469 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
470 g_value_set_boolean (value, FALSE);
472 case GTK_RECENT_CHOOSER_PROP_FILTER:
473 g_value_set_object (value, priv->current_filter);
475 case PROP_ACTIVATABLE_RELATED_ACTION:
476 g_value_set_object (value, _gtk_recent_chooser_get_related_action (GTK_RECENT_CHOOSER (menu)));
478 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
479 g_value_set_boolean (value, _gtk_recent_chooser_get_use_action_appearance (GTK_RECENT_CHOOSER (menu)));
482 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
488 gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
492 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
494 GtkWidget *menu_item = NULL;
495 gboolean found = FALSE;
497 children = gtk_container_get_children (GTK_CONTAINER (menu));
499 for (l = children; l != NULL; l = l->next)
503 menu_item = GTK_WIDGET (l->data);
505 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
509 if (strcmp (uri, gtk_recent_info_get_uri (info)) == 0)
511 gtk_menu_shell_activate_item (GTK_MENU_SHELL (menu),
520 g_list_free (children);
524 g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
525 GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
526 _("No recently used resource found with URI `%s'"),
534 gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser)
536 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
537 GtkWidget *menu_item;
540 menu_item = gtk_menu_get_active (GTK_MENU (menu));
544 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
548 return g_strdup (gtk_recent_info_get_uri (info));
552 gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
556 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
558 GtkWidget *menu_item = NULL;
559 gboolean found = FALSE;
561 children = gtk_container_get_children (GTK_CONTAINER (menu));
562 for (l = children; l != NULL; l = l->next)
566 menu_item = GTK_WIDGET (l->data);
568 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
572 if (0 == strcmp (uri, gtk_recent_info_get_uri (info)))
576 g_list_free (children);
580 g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
581 GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
582 _("No recently used resource found with URI `%s'"),
588 gtk_menu_shell_select_item (GTK_MENU_SHELL (menu), menu_item);
595 gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
598 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
600 gtk_menu_shell_deselect (GTK_MENU_SHELL (menu));
604 gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser)
606 g_warning ("This function is not implemented for "
607 "widgets of class '%s'",
608 g_type_name (G_OBJECT_TYPE (chooser)));
612 gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser)
614 g_warning ("This function is not implemented for "
615 "widgets of class '%s'",
616 g_type_name (G_OBJECT_TYPE (chooser)));
620 gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser *chooser,
621 GtkRecentSortFunc sort_func,
623 GDestroyNotify data_destroy)
625 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
626 GtkRecentChooserMenuPrivate *priv = menu->priv;
628 if (priv->sort_data_destroy)
630 priv->sort_data_destroy (priv->sort_data);
631 priv->sort_data_destroy = NULL;
634 priv->sort_func = NULL;
635 priv->sort_data = NULL;
636 priv->sort_data_destroy = NULL;
640 priv->sort_func = sort_func;
641 priv->sort_data = sort_data;
642 priv->sort_data_destroy = data_destroy;
647 chooser_set_sort_type (GtkRecentChooserMenu *menu,
648 GtkRecentSortType sort_type)
650 if (menu->priv->sort_type == sort_type)
653 menu->priv->sort_type = sort_type;
658 gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser)
660 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
661 GtkRecentChooserMenuPrivate *priv = menu->priv;
663 return _gtk_recent_chooser_get_items (chooser,
664 priv->current_filter,
669 static GtkRecentManager *
670 gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser)
672 GtkRecentChooserMenuPrivate *priv;
674 priv = GTK_RECENT_CHOOSER_MENU (chooser)->priv;
676 return priv->manager;
680 gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
681 GtkRecentFilter *filter)
683 GtkRecentChooserMenu *menu;
685 menu = GTK_RECENT_CHOOSER_MENU (chooser);
687 gtk_recent_chooser_menu_set_current_filter (menu, filter);
691 gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
692 GtkRecentFilter *filter)
694 GtkRecentChooserMenu *menu;
696 menu = GTK_RECENT_CHOOSER_MENU (chooser);
698 if (filter == menu->priv->current_filter)
700 g_object_unref (menu->priv->current_filter);
701 menu->priv->current_filter = NULL;
703 g_object_notify (G_OBJECT (menu), "filter");
708 gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser)
710 GtkRecentChooserMenu *menu;
711 GSList *retval = NULL;
713 menu = GTK_RECENT_CHOOSER_MENU (chooser);
715 if (menu->priv->current_filter)
716 retval = g_slist_prepend (retval, menu->priv->current_filter);
722 gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
723 GtkRecentFilter *filter)
725 GtkRecentChooserMenuPrivate *priv;
729 if (priv->current_filter)
730 g_object_unref (G_OBJECT (priv->current_filter));
732 priv->current_filter = filter;
734 if (priv->current_filter)
735 g_object_ref_sink (priv->current_filter);
737 gtk_recent_chooser_menu_populate (menu);
739 g_object_notify (G_OBJECT (menu), "filter");
742 /* taken from libeel/eel-strings.c */
744 escape_underscores (const gchar *string)
755 for (p = string; *p != '\0'; p++)
756 underscores += (*p == '_');
758 if (underscores == 0)
759 return g_strdup (string);
761 escaped = g_new (char, strlen (string) + underscores + 1);
762 for (p = string, q = escaped; *p != '\0'; p++, q++)
764 /* Add an extra underscore. */
777 gtk_recent_chooser_menu_add_tip (GtkRecentChooserMenu *menu,
781 GtkRecentChooserMenuPrivate *priv;
784 g_assert (info != NULL);
785 g_assert (item != NULL);
789 path = gtk_recent_info_get_uri_display (info);
792 gchar *tip_text = g_strdup_printf (_("Open '%s'"), path);
794 gtk_widget_set_tooltip_text (item, tip_text);
795 gtk_widget_set_has_tooltip (item, priv->show_tips);
803 gtk_recent_chooser_menu_create_item (GtkRecentChooserMenu *menu,
807 GtkRecentChooserMenuPrivate *priv;
809 GtkWidget *item, *image, *label;
812 g_assert (info != NULL);
816 if (priv->show_numbers)
818 gchar *name, *escaped;
820 name = g_strdup (gtk_recent_info_get_display_name (info));
822 name = g_strdup (_("Unknown item"));
824 escaped = escape_underscores (name);
826 /* avoid clashing mnemonics */
828 /* This is the label format that is used for the first 10 items
829 * in a recent files menu. The %d is the number of the item,
830 * the %s is the name of the item. Please keep the _ in front
831 * of the number to give these menu items a mnemonic.
833 text = g_strdup_printf (C_("recent menu label", "_%d. %s"), count, escaped);
835 /* This is the format that is used for items in a recent files menu.
836 * The %d is the number of the item, the %s is the name of the item.
838 text = g_strdup_printf (C_("recent menu label", "%d. %s"), count, escaped);
840 item = gtk_image_menu_item_new_with_mnemonic (text);
847 text = g_strdup (gtk_recent_info_get_display_name (info));
848 item = gtk_image_menu_item_new_with_label (text);
853 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item),
856 /* ellipsize the menu item label, in case the recent document
857 * display name is huge.
859 label = gtk_bin_get_child (GTK_BIN (item));
860 if (GTK_IS_LABEL (label))
862 gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
863 gtk_label_set_max_width_chars (GTK_LABEL (label), priv->label_width);
866 if (priv->show_icons)
868 icon = gtk_recent_info_get_gicon (info);
870 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_MENU);
871 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
872 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
874 g_object_unref (icon);
877 g_signal_connect (item, "activate",
878 G_CALLBACK (item_activate_cb),
885 gtk_recent_chooser_menu_insert_item (GtkRecentChooserMenu *menu,
889 GtkRecentChooserMenuPrivate *priv = menu->priv;
892 if (priv->first_recent_item_pos == -1)
896 children = gtk_container_get_children (GTK_CONTAINER (menu));
898 for (real_position = 0, l = children;
900 real_position += 1, l = l->next)
902 GObject *child = l->data;
903 gboolean is_placeholder = FALSE;
906 GPOINTER_TO_INT (g_object_get_data (child, "gtk-recent-menu-placeholder"));
912 g_list_free (children);
913 priv->first_recent_item_pos = real_position;
916 real_position = priv->first_recent_item_pos;
918 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), menuitem,
919 real_position + position);
920 gtk_widget_show (menuitem);
923 /* removes the items we own from the menu */
925 gtk_recent_chooser_menu_dispose_items (GtkRecentChooserMenu *menu)
929 children = gtk_container_get_children (GTK_CONTAINER (menu));
930 for (l = children; l != NULL; l = l->next)
932 GtkWidget *menu_item = GTK_WIDGET (l->data);
933 gboolean has_mark = FALSE;
935 /* check for our mark, in order to remove just the items we own */
937 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item), "gtk-recent-menu-mark"));
943 /* destroy the attached RecentInfo struct, if found */
944 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
946 g_object_set_data_full (G_OBJECT (menu_item), "gtk-recent-info",
949 /* and finally remove the item from the menu */
950 gtk_container_remove (GTK_CONTAINER (menu), menu_item);
954 /* recalculate the position of the first recent item */
955 menu->priv->first_recent_item_pos = -1;
957 g_list_free (children);
965 gint displayed_items;
966 GtkRecentChooserMenu *menu;
967 GtkWidget *placeholder;
970 static MenuPopulateData *
971 create_menu_populate_data (GtkRecentChooserMenu *menu)
973 MenuPopulateData *pdata;
975 pdata = g_slice_new (MenuPopulateData);
978 pdata->loaded_items = 0;
979 pdata->displayed_items = 0;
981 pdata->placeholder = g_object_ref (menu->priv->placeholder);
987 free_menu_populate_data (MenuPopulateData *pdata)
989 if (pdata->placeholder)
990 g_object_unref (pdata->placeholder);
991 g_slice_free (MenuPopulateData, pdata);
995 idle_populate_func (gpointer data)
997 MenuPopulateData *pdata;
998 GtkRecentChooserMenuPrivate *priv;
1003 pdata = (MenuPopulateData *) data;
1004 priv = pdata->menu->priv;
1008 pdata->items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (pdata->menu));
1011 /* show the placeholder here */
1012 gtk_widget_show (pdata->placeholder);
1013 pdata->displayed_items = 1;
1014 priv->populate_id = 0;
1019 gtk_widget_hide (pdata->placeholder);
1021 pdata->n_items = g_list_length (pdata->items);
1022 pdata->loaded_items = 0;
1025 info = g_list_nth_data (pdata->items, pdata->loaded_items);
1026 item = gtk_recent_chooser_menu_create_item (pdata->menu,
1028 pdata->displayed_items);
1030 goto check_and_return;
1032 gtk_recent_chooser_menu_add_tip (pdata->menu, info, item);
1033 gtk_recent_chooser_menu_insert_item (pdata->menu, item,
1034 pdata->displayed_items);
1036 pdata->displayed_items += 1;
1038 /* mark the menu item as one of our own */
1039 g_object_set_data (G_OBJECT (item),
1040 "gtk-recent-menu-mark",
1041 GINT_TO_POINTER (TRUE));
1043 /* attach the RecentInfo object to the menu item, and own a reference
1044 * to it, so that it will be destroyed with the menu item when it's
1045 * not needed anymore.
1047 g_object_set_data_full (G_OBJECT (item), "gtk-recent-info",
1048 gtk_recent_info_ref (info),
1049 (GDestroyNotify) gtk_recent_info_unref);
1052 pdata->loaded_items += 1;
1054 if (pdata->loaded_items == pdata->n_items)
1056 g_list_free_full (pdata->items, (GDestroyNotify) gtk_recent_info_unref);
1058 priv->populate_id = 0;
1069 idle_populate_clean_up (gpointer data)
1071 MenuPopulateData *pdata = data;
1073 if (pdata->menu->priv->populate_id == 0)
1075 /* show the placeholder in case no item survived
1076 * the filtering process in the idle loop
1078 if (!pdata->displayed_items)
1079 gtk_widget_show (pdata->placeholder);
1082 free_menu_populate_data (pdata);
1086 gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu)
1088 MenuPopulateData *pdata;
1089 GtkRecentChooserMenuPrivate *priv = menu->priv;
1091 if (priv->populate_id)
1094 pdata = create_menu_populate_data (menu);
1096 /* remove our menu items first */
1097 gtk_recent_chooser_menu_dispose_items (menu);
1099 priv->populate_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
1102 idle_populate_clean_up);
1105 /* bounce activate signal from the recent menu item widget
1106 * to the recent menu widget
1109 item_activate_cb (GtkWidget *widget,
1112 GtkRecentChooser *chooser = GTK_RECENT_CHOOSER (user_data);
1114 _gtk_recent_chooser_item_activated (chooser);
1117 /* we force a redraw if the manager changes when we are showing */
1119 manager_changed_cb (GtkRecentManager *manager,
1122 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (user_data);
1124 gtk_recent_chooser_menu_populate (menu);
1128 set_recent_manager (GtkRecentChooserMenu *menu,
1129 GtkRecentManager *manager)
1131 GtkRecentChooserMenuPrivate *priv = menu->priv;
1135 if (priv->manager_changed_id)
1137 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
1138 priv->manager_changed_id = 0;
1141 if (priv->populate_id)
1143 g_source_remove (priv->populate_id);
1144 priv->populate_id = 0;
1147 priv->manager = NULL;
1151 priv->manager = manager;
1153 priv->manager = gtk_recent_manager_get_default ();
1156 priv->manager_changed_id = g_signal_connect (priv->manager, "changed",
1157 G_CALLBACK (manager_changed_cb),
1162 foreach_set_shot_tips (GtkWidget *widget,
1165 GtkRecentChooserMenu *menu = user_data;
1166 GtkRecentChooserMenuPrivate *priv = menu->priv;
1169 /* toggle the tooltip only on the items we create */
1171 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "gtk-recent-menu-mark"));
1174 gtk_widget_set_has_tooltip (widget, priv->show_tips);
1178 gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
1181 GtkRecentChooserMenuPrivate *priv = menu->priv;
1183 if (priv->show_tips == show_tips)
1186 priv->show_tips = show_tips;
1187 gtk_container_foreach (GTK_CONTAINER (menu), foreach_set_shot_tips, menu);
1191 gtk_recent_chooser_update (GtkActivatable *activatable,
1193 const gchar *property_name)
1195 if (strcmp (property_name, "sensitive") == 0)
1196 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1198 _gtk_recent_chooser_update (activatable, action, property_name);
1202 gtk_recent_chooser_sync_action_properties (GtkActivatable *activatable,
1205 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1207 _gtk_recent_chooser_sync_action_properties (activatable, action);
1216 * gtk_recent_chooser_menu_new:
1218 * Creates a new #GtkRecentChooserMenu widget.
1220 * This kind of widget shows the list of recently used resources as
1221 * a menu, each item as a menu item. Each item inside the menu might
1222 * have an icon, representing its MIME type, and a number, for mnemonic
1225 * This widget implements the #GtkRecentChooser interface.
1227 * This widget creates its own #GtkRecentManager object. See the
1228 * gtk_recent_chooser_menu_new_for_manager() function to know how to create
1229 * a #GtkRecentChooserMenu widget bound to another #GtkRecentManager object.
1231 * Return value: a new #GtkRecentChooserMenu
1236 gtk_recent_chooser_menu_new (void)
1238 return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1239 "recent-manager", NULL,
1244 * gtk_recent_chooser_menu_new_for_manager:
1245 * @manager: a #GtkRecentManager
1247 * Creates a new #GtkRecentChooserMenu widget using @manager as
1248 * the underlying recently used resources manager.
1250 * This is useful if you have implemented your own recent manager,
1251 * or if you have a customized instance of a #GtkRecentManager
1252 * object or if you wish to share a common #GtkRecentManager object
1253 * among multiple #GtkRecentChooser widgets.
1255 * Return value: a new #GtkRecentChooserMenu, bound to @manager.
1260 gtk_recent_chooser_menu_new_for_manager (GtkRecentManager *manager)
1262 g_return_val_if_fail (manager == NULL || GTK_IS_RECENT_MANAGER (manager), NULL);
1264 return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1265 "recent-manager", manager,
1270 * gtk_recent_chooser_menu_get_show_numbers:
1271 * @menu: a #GtkRecentChooserMenu
1273 * Returns the value set by gtk_recent_chooser_menu_set_show_numbers().
1275 * Return value: %TRUE if numbers should be shown.
1280 gtk_recent_chooser_menu_get_show_numbers (GtkRecentChooserMenu *menu)
1282 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu), FALSE);
1284 return menu->priv->show_numbers;
1288 * gtk_recent_chooser_menu_set_show_numbers:
1289 * @menu: a #GtkRecentChooserMenu
1290 * @show_numbers: whether to show numbers
1292 * Sets whether a number should be added to the items of @menu. The
1293 * numbers are shown to provide a unique character for a mnemonic to
1294 * be used inside ten menu item's label. Only the first the items
1295 * get a number to avoid clashes.
1300 gtk_recent_chooser_menu_set_show_numbers (GtkRecentChooserMenu *menu,
1301 gboolean show_numbers)
1303 g_return_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu));
1305 if (menu->priv->show_numbers == show_numbers)
1308 menu->priv->show_numbers = show_numbers;
1309 g_object_notify (G_OBJECT (menu), "show-numbers");