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 <gdk/gdkscreen.h>
27 #include "gtkrecentmanager.h"
28 #include "gtkrecentfilter.h"
29 #include "gtkrecentchooser.h"
30 #include "gtkrecentchooserutils.h"
31 #include "gtkrecentchooserprivate.h"
32 #include "gtkrecentchoosermenu.h"
35 #include "gtkicontheme.h"
36 #include "gtkiconfactory.h"
38 #include "gtksettings.h"
39 #include "gtkmenushell.h"
40 #include "gtkmenuitem.h"
41 #include "gtkimagemenuitem.h"
42 #include "gtkseparatormenuitem.h"
46 #include "gtkobject.h"
47 #include "gtktooltips.h"
48 #include "gtktypebuiltins.h"
49 #include "gtkprivate.h"
52 struct _GtkRecentChooserMenuPrivate
54 /* the recent manager object */
55 GtkRecentManager *manager;
57 /* size of the icons of the menu items */
60 /* max size of the menu item label */
63 gint first_recent_item_pos;
64 GtkWidget *placeholder;
66 /* RecentChooser properties */
68 guint show_private : 1;
69 guint show_not_found : 1;
74 guint show_numbers : 1;
76 GtkRecentSortType sort_type;
77 GtkRecentSortFunc sort_func;
79 GDestroyNotify sort_data_destroy;
82 GtkRecentFilter *current_filter;
84 guint local_manager : 1;
85 gulong manager_changed_id;
89 /* tooltips for our bookmark items*/
90 GtkTooltips *tooltips;
99 #define FALLBACK_ICON_SIZE 32
100 #define FALLBACK_ITEM_LIMIT 10
101 #define DEFAULT_LABEL_WIDTH 30
103 #define GTK_RECENT_CHOOSER_MENU_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RECENT_CHOOSER_MENU, GtkRecentChooserMenuPrivate))
105 static void gtk_recent_chooser_menu_finalize (GObject *object);
106 static void gtk_recent_chooser_menu_dispose (GObject *object);
107 static GObject *gtk_recent_chooser_menu_constructor (GType type,
108 guint n_construct_properties,
109 GObjectConstructParam *construct_params);
111 static void gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface);
113 static void gtk_recent_chooser_menu_set_property (GObject *object,
117 static void gtk_recent_chooser_menu_get_property (GObject *object,
122 static gboolean gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
125 static gchar * gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser);
126 static gboolean gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
129 static void gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
131 static void gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser);
132 static void gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser);
133 static GList * gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser);
134 static GtkRecentManager *gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser);
135 static void gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser *chooser,
136 GtkRecentSortFunc sort_func,
138 GDestroyNotify data_destroy);
139 static void gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
140 GtkRecentFilter *filter);
141 static void gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
142 GtkRecentFilter *filter);
143 static GSList * gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser);
144 static void gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
145 GtkRecentFilter *filter);
147 static void gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu);
148 static void gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
151 static void set_recent_manager (GtkRecentChooserMenu *menu,
152 GtkRecentManager *manager);
154 static void chooser_set_sort_type (GtkRecentChooserMenu *menu,
155 GtkRecentSortType sort_type);
157 static gint get_icon_size_for_widget (GtkWidget *widget);
159 static void item_activate_cb (GtkWidget *widget,
161 static void manager_changed_cb (GtkRecentManager *manager,
164 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserMenu,
165 gtk_recent_chooser_menu,
167 G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
168 gtk_recent_chooser_iface_init))
172 gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface)
174 iface->set_current_uri = gtk_recent_chooser_menu_set_current_uri;
175 iface->get_current_uri = gtk_recent_chooser_menu_get_current_uri;
176 iface->select_uri = gtk_recent_chooser_menu_select_uri;
177 iface->unselect_uri = gtk_recent_chooser_menu_unselect_uri;
178 iface->select_all = gtk_recent_chooser_menu_select_all;
179 iface->unselect_all = gtk_recent_chooser_menu_unselect_all;
180 iface->get_items = gtk_recent_chooser_menu_get_items;
181 iface->get_recent_manager = gtk_recent_chooser_menu_get_recent_manager;
182 iface->set_sort_func = gtk_recent_chooser_menu_set_sort_func;
183 iface->add_filter = gtk_recent_chooser_menu_add_filter;
184 iface->remove_filter = gtk_recent_chooser_menu_remove_filter;
185 iface->list_filters = gtk_recent_chooser_menu_list_filters;
189 gtk_recent_chooser_menu_class_init (GtkRecentChooserMenuClass *klass)
191 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
193 gobject_class->constructor = gtk_recent_chooser_menu_constructor;
194 gobject_class->dispose = gtk_recent_chooser_menu_dispose;
195 gobject_class->finalize = gtk_recent_chooser_menu_finalize;
196 gobject_class->set_property = gtk_recent_chooser_menu_set_property;
197 gobject_class->get_property = gtk_recent_chooser_menu_get_property;
199 _gtk_recent_chooser_install_properties (gobject_class);
202 * GtkRecentChooserMenu:show-numbers
204 * Whether the first ten items in the menu should be prepended by
205 * a number acting as a unique mnemonic.
209 g_object_class_install_property (gobject_class,
211 g_param_spec_boolean ("show-numbers",
213 P_("Whether the items should be displayed with a number"),
215 GTK_PARAM_READWRITE));
217 g_type_class_add_private (klass, sizeof (GtkRecentChooserMenuPrivate));
221 gtk_recent_chooser_menu_init (GtkRecentChooserMenu *menu)
223 GtkRecentChooserMenuPrivate *priv;
225 priv = GTK_RECENT_CHOOSER_MENU_GET_PRIVATE (menu);
229 priv->show_icons= TRUE;
230 priv->show_numbers = FALSE;
231 priv->show_tips = FALSE;
232 priv->show_not_found = TRUE;
233 priv->show_private = FALSE;
234 priv->local_only = TRUE;
236 priv->limit = FALLBACK_ITEM_LIMIT;
238 priv->sort_type = GTK_RECENT_SORT_NONE;
240 priv->icon_size = FALLBACK_ICON_SIZE;
242 priv->label_width = DEFAULT_LABEL_WIDTH;
244 priv->first_recent_item_pos = -1;
245 priv->placeholder = NULL;
247 priv->current_filter = NULL;
249 priv->tooltips = gtk_tooltips_new ();
250 g_object_ref_sink (priv->tooltips);
254 gtk_recent_chooser_menu_finalize (GObject *object)
256 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
257 GtkRecentChooserMenuPrivate *priv = menu->priv;
259 priv->manager = NULL;
261 if (priv->sort_data_destroy)
263 priv->sort_data_destroy (priv->sort_data);
264 priv->sort_data_destroy = NULL;
267 priv->sort_data = NULL;
268 priv->sort_func = NULL;
270 G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->finalize (object);
274 gtk_recent_chooser_menu_dispose (GObject *object)
276 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
277 GtkRecentChooserMenuPrivate *priv = menu->priv;
279 if (priv->manager_changed_id)
282 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
284 priv->manager_changed_id = 0;
287 if (priv->populate_id)
289 g_source_remove (priv->populate_id);
290 priv->populate_id = 0;
295 g_object_unref (priv->tooltips);
296 priv->tooltips = NULL;
299 if (priv->current_filter)
301 g_object_unref (priv->current_filter);
302 priv->current_filter = NULL;
305 G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->dispose (object);
309 gtk_recent_chooser_menu_constructor (GType type,
311 GObjectConstructParam *params)
313 GtkRecentChooserMenu *menu;
314 GtkRecentChooserMenuPrivate *priv;
315 GObjectClass *parent_class;
318 parent_class = G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class);
319 object = parent_class->constructor (type, n_params, params);
320 menu = GTK_RECENT_CHOOSER_MENU (object);
323 g_assert (priv->manager);
325 /* we create a placeholder menuitem, to be used in case
326 * the menu is empty. this placeholder will stay around
327 * for the entire lifetime of the menu, and we just hide it
328 * when it's not used. we have to do this, and do it here,
329 * because we need a marker for the beginning of the recent
330 * items list, so that we can insert the new items at the
331 * right place when idly populating the menu in case the
332 * user appended or prepended custom menu items to the
333 * recent chooser menu widget.
335 priv->placeholder = gtk_menu_item_new_with_label (_("No items found"));
336 gtk_widget_set_sensitive (priv->placeholder, FALSE);
337 g_object_set_data (G_OBJECT (priv->placeholder),
338 "gtk-recent-menu-placeholder",
339 GINT_TO_POINTER (TRUE));
341 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), priv->placeholder, 0);
342 gtk_widget_show (priv->placeholder);
344 /* (re)populate the menu */
345 gtk_recent_chooser_menu_populate (menu);
351 gtk_recent_chooser_menu_set_property (GObject *object,
356 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
357 GtkRecentChooserMenuPrivate *priv = menu->priv;
361 case PROP_SHOW_NUMBERS:
362 priv->show_numbers = g_value_get_boolean (value);
364 case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
365 set_recent_manager (menu, g_value_get_object (value));
367 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
368 priv->show_private = g_value_get_boolean (value);
370 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
371 priv->show_not_found = g_value_get_boolean (value);
373 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
374 gtk_recent_chooser_menu_set_show_tips (menu, g_value_get_boolean (value));
376 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
377 priv->show_icons = g_value_get_boolean (value);
379 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
380 g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
382 G_OBJECT_TYPE_NAME (object));
384 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
385 priv->local_only = g_value_get_boolean (value);
387 case GTK_RECENT_CHOOSER_PROP_LIMIT:
388 priv->limit = g_value_get_int (value);
390 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
391 chooser_set_sort_type (menu, g_value_get_enum (value));
393 case GTK_RECENT_CHOOSER_PROP_FILTER:
394 gtk_recent_chooser_menu_set_current_filter (menu, g_value_get_object (value));
397 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
403 gtk_recent_chooser_menu_get_property (GObject *object,
408 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
409 GtkRecentChooserMenuPrivate *priv = menu->priv;
413 case PROP_SHOW_NUMBERS:
414 g_value_set_boolean (value, priv->show_numbers);
416 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
417 g_value_set_boolean (value, priv->show_tips);
419 case GTK_RECENT_CHOOSER_PROP_LIMIT:
420 g_value_set_int (value, priv->limit);
422 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
423 g_value_set_boolean (value, priv->local_only);
425 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
426 g_value_set_enum (value, priv->sort_type);
428 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
429 g_value_set_boolean (value, priv->show_private);
431 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
432 g_value_set_boolean (value, priv->show_not_found);
434 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
435 g_value_set_boolean (value, priv->show_icons);
437 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
438 g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
440 G_OBJECT_TYPE_NAME (object));
442 case GTK_RECENT_CHOOSER_PROP_FILTER:
443 g_value_set_object (value, priv->current_filter);
446 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
452 gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
456 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
458 GtkWidget *menu_item = NULL;
459 gboolean found = FALSE;
461 children = gtk_container_get_children (GTK_CONTAINER (menu));
463 for (l = children; l != NULL; l = l->next)
467 menu_item = GTK_WIDGET (l->data);
469 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
473 if (strcmp (uri, gtk_recent_info_get_uri (info)) == 0)
475 gtk_menu_shell_activate_item (GTK_MENU_SHELL (menu),
484 g_list_free (children);
488 g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
489 GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
490 _("No recently used resource found with URI `%s'"),
498 gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser)
500 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
501 GtkWidget *menu_item;
504 menu_item = gtk_menu_get_active (GTK_MENU (menu));
508 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
512 return g_strdup (gtk_recent_info_get_uri (info));
516 gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
520 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
522 GtkWidget *menu_item = NULL;
523 gboolean found = FALSE;
525 children = gtk_container_get_children (GTK_CONTAINER (menu));
526 for (l = children; l != NULL; l = l->next)
530 menu_item = GTK_WIDGET (l->data);
532 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
536 if (0 == strcmp (uri, gtk_recent_info_get_uri (info)))
540 g_list_free (children);
544 g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
545 GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
546 _("No recently used resource found with URI `%s'"),
552 gtk_menu_shell_select_item (GTK_MENU_SHELL (menu), menu_item);
559 gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
562 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
564 gtk_menu_shell_deselect (GTK_MENU_SHELL (menu));
568 gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser)
570 g_warning (_("This function is not implemented for "
571 "widgets of class '%s'"),
572 g_type_name (G_OBJECT_TYPE (chooser)));
576 gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser)
578 g_warning (_("This function is not implemented for "
579 "widgets of class '%s'"),
580 g_type_name (G_OBJECT_TYPE (chooser)));
584 gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser *chooser,
585 GtkRecentSortFunc sort_func,
587 GDestroyNotify data_destroy)
589 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
590 GtkRecentChooserMenuPrivate *priv = menu->priv;
592 if (priv->sort_data_destroy)
594 priv->sort_data_destroy (priv->sort_data);
595 priv->sort_data_destroy = NULL;
598 priv->sort_func = NULL;
599 priv->sort_data = NULL;
600 priv->sort_data_destroy = NULL;
604 priv->sort_func = sort_func;
605 priv->sort_data = sort_data;
606 priv->sort_data_destroy = data_destroy;
611 chooser_set_sort_type (GtkRecentChooserMenu *menu,
612 GtkRecentSortType sort_type)
614 if (menu->priv->sort_type == sort_type)
617 menu->priv->sort_type = sort_type;
622 gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser)
624 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
625 GtkRecentChooserMenuPrivate *priv = menu->priv;
627 return _gtk_recent_chooser_get_items (chooser,
628 priv->current_filter,
633 static GtkRecentManager *
634 gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser)
636 GtkRecentChooserMenuPrivate *priv;
638 priv = GTK_RECENT_CHOOSER_MENU (chooser)->priv;
640 return priv->manager;
644 gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
645 GtkRecentFilter *filter)
647 GtkRecentChooserMenu *menu;
649 menu = GTK_RECENT_CHOOSER_MENU (chooser);
651 gtk_recent_chooser_menu_set_current_filter (menu, filter);
655 gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
656 GtkRecentFilter *filter)
658 GtkRecentChooserMenu *menu;
660 menu = GTK_RECENT_CHOOSER_MENU (chooser);
662 if (filter == menu->priv->current_filter)
664 g_object_unref (menu->priv->current_filter);
665 menu->priv->current_filter = NULL;
667 g_object_notify (G_OBJECT (menu), "filter");
672 gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser)
674 GtkRecentChooserMenu *menu;
675 GSList *retval = NULL;
677 menu = GTK_RECENT_CHOOSER_MENU (chooser);
679 if (menu->priv->current_filter)
680 retval = g_slist_prepend (retval, menu->priv->current_filter);
686 gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
687 GtkRecentFilter *filter)
689 GtkRecentChooserMenuPrivate *priv;
693 if (priv->current_filter)
694 g_object_unref (G_OBJECT (priv->current_filter));
698 priv->current_filter = filter;
699 g_object_ref_sink (priv->current_filter);
702 gtk_recent_chooser_menu_populate (menu);
704 g_object_notify (G_OBJECT (menu), "filter");
707 /* taken from libeel/eel-strings.c */
709 escape_underscores (const gchar *string)
720 for (p = string; *p != '\0'; p++)
721 underscores += (*p == '_');
723 if (underscores == 0)
724 return g_strdup (string);
726 escaped = g_new (char, strlen (string) + underscores + 1);
727 for (p = string, q = escaped; *p != '\0'; p++, q++)
729 /* Add an extra underscore. */
742 gtk_recent_chooser_menu_add_tip (GtkRecentChooserMenu *menu,
746 GtkRecentChooserMenuPrivate *priv;
747 gchar *path, *tip_text;
749 g_assert (info != NULL);
750 g_assert (item != NULL);
757 path = gtk_recent_info_get_uri_display (info);
760 tip_text = g_strdup_printf (_("Open '%s'"), path);
762 gtk_tooltips_set_tip (priv->tooltips,
773 gtk_recent_chooser_menu_create_item (GtkRecentChooserMenu *menu,
777 GtkRecentChooserMenuPrivate *priv;
779 GtkWidget *item, *image, *label;
782 g_assert (info != NULL);
786 if (priv->show_numbers)
788 gchar *name, *escaped;
790 name = g_strdup (gtk_recent_info_get_display_name (info));
792 name = g_strdup (_("Unknown item"));
794 escaped = escape_underscores (name);
796 /* avoid clashing mnemonics */
798 text = g_strdup_printf ("_%d. %s", count, escaped);
800 text = g_strdup_printf ("%d. %s", count, escaped);
802 item = gtk_image_menu_item_new_with_mnemonic (text);
809 text = g_strdup (gtk_recent_info_get_display_name (info));
810 item = gtk_image_menu_item_new_with_label (text);
815 /* ellipsize the menu item label, in case the recent document
816 * display name is huge.
818 label = GTK_BIN (item)->child;
819 if (GTK_IS_LABEL (label))
821 gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
822 gtk_label_set_max_width_chars (GTK_LABEL (label), priv->label_width);
825 if (priv->show_icons)
827 icon = gtk_recent_info_get_icon (info, priv->icon_size);
829 image = gtk_image_new_from_pixbuf (icon);
830 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
831 g_object_unref (icon);
834 g_signal_connect (item, "activate",
835 G_CALLBACK (item_activate_cb),
842 gtk_recent_chooser_menu_insert_item (GtkRecentChooserMenu *menu,
846 GtkRecentChooserMenuPrivate *priv = menu->priv;
849 if (priv->first_recent_item_pos == -1)
853 children = gtk_container_get_children (GTK_CONTAINER (menu));
855 for (real_position = 0, l = children;
857 real_position += 1, l = l->next)
859 GObject *child = l->data;
860 gboolean is_placeholder = FALSE;
863 GPOINTER_TO_INT (g_object_get_data (child, "gtk-recent-menu-placeholder"));
869 g_list_free (children);
870 priv->first_recent_item_pos = real_position;
873 real_position = priv->first_recent_item_pos;
875 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), menuitem,
876 real_position + position);
877 gtk_widget_show (menuitem);
880 /* removes the items we own from the menu */
882 gtk_recent_chooser_menu_dispose_items (GtkRecentChooserMenu *menu)
886 children = gtk_container_get_children (GTK_CONTAINER (menu));
887 for (l = children; l != NULL; l = l->next)
889 GtkWidget *menu_item = GTK_WIDGET (l->data);
890 gboolean has_mark = FALSE;
892 /* check for our mark, in order to remove just the items we own */
894 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item), "gtk-recent-menu-mark"));
900 /* destroy the attached RecentInfo struct, if found */
901 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
903 g_object_set_data_full (G_OBJECT (menu_item), "gtk-recent-info",
906 /* and finally remove the item from the menu */
907 gtk_container_remove (GTK_CONTAINER (menu), menu_item);
911 /* recalculate the position of the first recent item */
912 menu->priv->first_recent_item_pos = -1;
914 g_list_free (children);
922 gint displayed_items;
923 GtkRecentChooserMenu *menu;
924 GtkWidget *placeholder;
928 idle_populate_func (gpointer data)
930 MenuPopulateData *pdata;
931 GtkRecentChooserMenuPrivate *priv;
936 pdata = (MenuPopulateData *) data;
937 priv = pdata->menu->priv;
939 priv->populate_id = 0;
943 pdata->items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (pdata->menu));
946 /* show the placeholder here */
947 gtk_widget_show (pdata->placeholder);
948 pdata->displayed_items = 1;
953 pdata->n_items = g_list_length (pdata->items);
954 pdata->loaded_items = 0;
957 info = g_list_nth_data (pdata->items, pdata->loaded_items);
958 item = gtk_recent_chooser_menu_create_item (pdata->menu,
960 pdata->displayed_items);
962 goto check_and_return;
964 gtk_recent_chooser_menu_add_tip (pdata->menu, info, item);
965 gtk_recent_chooser_menu_insert_item (pdata->menu, item,
966 pdata->displayed_items);
968 pdata->displayed_items += 1;
970 /* mark the menu item as one of our own */
971 g_object_set_data (G_OBJECT (item),
972 "gtk-recent-menu-mark",
973 GINT_TO_POINTER (TRUE));
975 /* attach the RecentInfo object to the menu item, and own a reference
976 * to it, so that it will be destroyed with the menu item when it's
977 * not needed anymore.
979 g_object_set_data_full (G_OBJECT (item), "gtk-recent-info",
980 gtk_recent_info_ref (info),
981 (GDestroyNotify) gtk_recent_info_unref);
984 pdata->loaded_items += 1;
986 if (pdata->loaded_items == pdata->n_items)
988 g_list_foreach (pdata->items, (GFunc) gtk_recent_info_unref, NULL);
989 g_list_free (pdata->items);
1000 idle_populate_clean_up (gpointer data)
1002 MenuPopulateData *pdata = data;
1004 /* show the placeholder in case no item survived
1005 * the filtering process in the idle loop
1007 if (!pdata->displayed_items)
1008 gtk_widget_show (pdata->placeholder);
1009 g_object_unref (pdata->placeholder);
1011 g_slice_free (MenuPopulateData, data);
1015 gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu)
1017 MenuPopulateData *pdata;
1018 GtkRecentChooserMenuPrivate *priv = menu->priv;
1020 if (menu->priv->populate_id)
1023 pdata = g_slice_new (MenuPopulateData);
1024 pdata->items = NULL;
1026 pdata->loaded_items = 0;
1027 pdata->displayed_items = 0;
1029 pdata->placeholder = g_object_ref (priv->placeholder);
1031 priv->icon_size = get_icon_size_for_widget (GTK_WIDGET (menu));
1033 /* remove our menu items first and hide the placeholder */
1034 gtk_recent_chooser_menu_dispose_items (menu);
1035 gtk_widget_hide (priv->placeholder);
1037 priv->populate_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
1040 idle_populate_clean_up);
1043 /* bounce activate signal from the recent menu item widget
1044 * to the recent menu widget
1047 item_activate_cb (GtkWidget *widget,
1050 GtkRecentChooser *chooser = GTK_RECENT_CHOOSER (user_data);
1052 _gtk_recent_chooser_item_activated (chooser);
1055 /* we force a redraw if the manager changes when we are showing */
1057 manager_changed_cb (GtkRecentManager *manager,
1060 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (user_data);
1062 gtk_recent_chooser_menu_populate (menu);
1066 set_recent_manager (GtkRecentChooserMenu *menu,
1067 GtkRecentManager *manager)
1069 GtkRecentChooserMenuPrivate *priv = menu->priv;
1073 if (priv->manager_changed_id)
1075 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
1076 priv->manager_changed_id = 0;
1079 if (priv->populate_id)
1081 g_source_remove (priv->populate_id);
1082 priv->populate_id = 0;
1085 priv->manager = NULL;
1089 priv->manager = manager;
1091 priv->manager = gtk_recent_manager_get_default ();
1094 priv->manager_changed_id = g_signal_connect (priv->manager, "changed",
1095 G_CALLBACK (manager_changed_cb),
1100 get_icon_size_for_widget (GtkWidget *widget)
1102 GtkSettings *settings;
1105 if (gtk_widget_has_screen (widget))
1106 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
1108 settings = gtk_settings_get_default ();
1110 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1112 return MAX (width, height);
1114 return FALLBACK_ICON_SIZE;
1118 gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
1121 if (menu->priv->show_tips == show_tips)
1124 g_assert (menu->priv->tooltips != NULL);
1127 gtk_tooltips_enable (menu->priv->tooltips);
1129 gtk_tooltips_disable (menu->priv->tooltips);
1131 menu->priv->show_tips = show_tips;
1139 * gtk_recent_chooser_menu_new:
1141 * Creates a new #GtkRecentChooserMenu widget.
1143 * This kind of widget shows the list of recently used resources as
1144 * a menu, each item as a menu item. Each item inside the menu might
1145 * have an icon, representing its MIME type, and a number, for mnemonic
1148 * This widget implements the #GtkRecentChooser interface.
1150 * This widget creates its own #GtkRecentManager object. See the
1151 * gtk_recent_chooser_menu_new_for_manager() function to know how to create
1152 * a #GtkRecentChooserMenu widget bound to another #GtkRecentManager object.
1154 * Return value: a new #GtkRecentChooserMenu
1159 gtk_recent_chooser_menu_new (void)
1161 return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1162 "recent-manager", NULL,
1167 * gtk_recent_chooser_menu_new_for_manager:
1168 * @manager: a #GtkRecentManager
1170 * Creates a new #GtkRecentChooserMenu widget using @manager as
1171 * the underlying recently used resources manager.
1173 * This is useful if you have implemented your own recent manager,
1174 * or if you have a customized instance of a #GtkRecentManager
1175 * object or if you wish to share a common #GtkRecentManager object
1176 * among multiple #GtkRecentChooser widgets.
1178 * Return value: a new #GtkRecentChooserMenu, bound to @manager.
1183 gtk_recent_chooser_menu_new_for_manager (GtkRecentManager *manager)
1185 g_return_val_if_fail (manager == NULL || GTK_IS_RECENT_MANAGER (manager), NULL);
1187 return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1188 "recent-manager", manager,
1193 * gtk_recent_chooser_menu_get_show_numbers:
1194 * @menu: a #GtkRecentChooserMenu
1196 * Returns the value set by gtk_recent_chooser_menu_set_show_numbers().
1198 * Return value: %TRUE if numbers should be shown.
1203 gtk_recent_chooser_menu_get_show_numbers (GtkRecentChooserMenu *menu)
1205 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu), FALSE);
1207 return menu->priv->show_numbers;
1211 * gtk_recent_chooser_menu_set_show_numbers:
1212 * @menu: a #GtkRecentChooserMenu
1213 * @show_numbers: whether to show numbers
1215 * Sets whether a number should be added to the items of @menu. The
1216 * numbers are shown to provide a unique character for a mnemonic to
1217 * be used inside ten menu item's label. Only the first the items
1218 * get a number to avoid clashes.
1223 gtk_recent_chooser_menu_set_show_numbers (GtkRecentChooserMenu *menu,
1224 gboolean show_numbers)
1226 g_return_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu));
1228 if (menu->priv->show_numbers == show_numbers)
1231 menu->priv->show_numbers = show_numbers;
1232 g_object_notify (G_OBJECT (menu), "show-numbers");
1235 #define __GTK_RECENT_CHOOSER_MENU_C__
1236 #include "gtkaliasdef.c"