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 "gtktooltip.h"
47 #include "gtktypebuiltins.h"
48 #include "gtkprivate.h"
51 struct _GtkRecentChooserMenuPrivate
53 /* the recent manager object */
54 GtkRecentManager *manager;
56 /* size of the icons of the menu items */
59 /* max size of the menu item label */
62 gint first_recent_item_pos;
63 GtkWidget *placeholder;
65 /* RecentChooser properties */
67 guint show_private : 1;
68 guint show_not_found : 1;
73 guint show_numbers : 1;
75 GtkRecentSortType sort_type;
76 GtkRecentSortFunc sort_func;
78 GDestroyNotify sort_data_destroy;
81 GtkRecentFilter *current_filter;
83 guint local_manager : 1;
84 gulong manager_changed_id;
95 #define FALLBACK_ICON_SIZE 32
96 #define FALLBACK_ITEM_LIMIT 10
97 #define DEFAULT_LABEL_WIDTH 30
99 #define GTK_RECENT_CHOOSER_MENU_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RECENT_CHOOSER_MENU, GtkRecentChooserMenuPrivate))
101 static void gtk_recent_chooser_menu_finalize (GObject *object);
102 static void gtk_recent_chooser_menu_dispose (GObject *object);
103 static GObject *gtk_recent_chooser_menu_constructor (GType type,
104 guint n_construct_properties,
105 GObjectConstructParam *construct_params);
107 static void gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface);
109 static void gtk_recent_chooser_menu_set_property (GObject *object,
113 static void gtk_recent_chooser_menu_get_property (GObject *object,
118 static gboolean gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
121 static gchar * gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser);
122 static gboolean gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
125 static void gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
127 static void gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser);
128 static void gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser);
129 static GList * gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser);
130 static GtkRecentManager *gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser);
131 static void gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser *chooser,
132 GtkRecentSortFunc sort_func,
134 GDestroyNotify data_destroy);
135 static void gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
136 GtkRecentFilter *filter);
137 static void gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
138 GtkRecentFilter *filter);
139 static GSList * gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser);
140 static void gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
141 GtkRecentFilter *filter);
143 static void gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu);
144 static void gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
147 static void set_recent_manager (GtkRecentChooserMenu *menu,
148 GtkRecentManager *manager);
150 static void chooser_set_sort_type (GtkRecentChooserMenu *menu,
151 GtkRecentSortType sort_type);
153 static gint get_icon_size_for_widget (GtkWidget *widget);
155 static void item_activate_cb (GtkWidget *widget,
157 static void manager_changed_cb (GtkRecentManager *manager,
160 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserMenu,
161 gtk_recent_chooser_menu,
163 G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
164 gtk_recent_chooser_iface_init))
168 gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface)
170 iface->set_current_uri = gtk_recent_chooser_menu_set_current_uri;
171 iface->get_current_uri = gtk_recent_chooser_menu_get_current_uri;
172 iface->select_uri = gtk_recent_chooser_menu_select_uri;
173 iface->unselect_uri = gtk_recent_chooser_menu_unselect_uri;
174 iface->select_all = gtk_recent_chooser_menu_select_all;
175 iface->unselect_all = gtk_recent_chooser_menu_unselect_all;
176 iface->get_items = gtk_recent_chooser_menu_get_items;
177 iface->get_recent_manager = gtk_recent_chooser_menu_get_recent_manager;
178 iface->set_sort_func = gtk_recent_chooser_menu_set_sort_func;
179 iface->add_filter = gtk_recent_chooser_menu_add_filter;
180 iface->remove_filter = gtk_recent_chooser_menu_remove_filter;
181 iface->list_filters = gtk_recent_chooser_menu_list_filters;
185 gtk_recent_chooser_menu_class_init (GtkRecentChooserMenuClass *klass)
187 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
189 gobject_class->constructor = gtk_recent_chooser_menu_constructor;
190 gobject_class->dispose = gtk_recent_chooser_menu_dispose;
191 gobject_class->finalize = gtk_recent_chooser_menu_finalize;
192 gobject_class->set_property = gtk_recent_chooser_menu_set_property;
193 gobject_class->get_property = gtk_recent_chooser_menu_get_property;
195 _gtk_recent_chooser_install_properties (gobject_class);
198 * GtkRecentChooserMenu:show-numbers
200 * Whether the first ten items in the menu should be prepended by
201 * a number acting as a unique mnemonic.
205 g_object_class_install_property (gobject_class,
207 g_param_spec_boolean ("show-numbers",
209 P_("Whether the items should be displayed with a number"),
211 GTK_PARAM_READWRITE));
213 g_type_class_add_private (klass, sizeof (GtkRecentChooserMenuPrivate));
217 gtk_recent_chooser_menu_init (GtkRecentChooserMenu *menu)
219 GtkRecentChooserMenuPrivate *priv;
221 priv = GTK_RECENT_CHOOSER_MENU_GET_PRIVATE (menu);
225 priv->show_icons= TRUE;
226 priv->show_numbers = FALSE;
227 priv->show_tips = FALSE;
228 priv->show_not_found = TRUE;
229 priv->show_private = FALSE;
230 priv->local_only = TRUE;
232 priv->limit = FALLBACK_ITEM_LIMIT;
233 priv->sort_type = GTK_RECENT_SORT_NONE;
234 priv->icon_size = FALLBACK_ICON_SIZE;
235 priv->label_width = DEFAULT_LABEL_WIDTH;
237 priv->first_recent_item_pos = -1;
238 priv->placeholder = NULL;
240 priv->current_filter = NULL;
244 gtk_recent_chooser_menu_finalize (GObject *object)
246 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
247 GtkRecentChooserMenuPrivate *priv = menu->priv;
249 priv->manager = NULL;
251 if (priv->sort_data_destroy)
253 priv->sort_data_destroy (priv->sort_data);
254 priv->sort_data_destroy = NULL;
257 priv->sort_data = NULL;
258 priv->sort_func = NULL;
260 G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->finalize (object);
264 gtk_recent_chooser_menu_dispose (GObject *object)
266 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
267 GtkRecentChooserMenuPrivate *priv = menu->priv;
269 if (priv->manager_changed_id)
272 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
274 priv->manager_changed_id = 0;
277 if (priv->populate_id)
279 g_source_remove (priv->populate_id);
280 priv->populate_id = 0;
283 if (priv->current_filter)
285 g_object_unref (priv->current_filter);
286 priv->current_filter = NULL;
289 G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->dispose (object);
293 gtk_recent_chooser_menu_constructor (GType type,
295 GObjectConstructParam *params)
297 GtkRecentChooserMenu *menu;
298 GtkRecentChooserMenuPrivate *priv;
299 GObjectClass *parent_class;
302 parent_class = G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class);
303 object = parent_class->constructor (type, n_params, params);
304 menu = GTK_RECENT_CHOOSER_MENU (object);
307 g_assert (priv->manager);
309 /* we create a placeholder menuitem, to be used in case
310 * the menu is empty. this placeholder will stay around
311 * for the entire lifetime of the menu, and we just hide it
312 * when it's not used. we have to do this, and do it here,
313 * because we need a marker for the beginning of the recent
314 * items list, so that we can insert the new items at the
315 * right place when idly populating the menu in case the
316 * user appended or prepended custom menu items to the
317 * recent chooser menu widget.
319 priv->placeholder = gtk_menu_item_new_with_label (_("No items found"));
320 gtk_widget_set_sensitive (priv->placeholder, FALSE);
321 g_object_set_data (G_OBJECT (priv->placeholder),
322 "gtk-recent-menu-placeholder",
323 GINT_TO_POINTER (TRUE));
325 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), priv->placeholder, 0);
326 gtk_widget_set_no_show_all (priv->placeholder, TRUE);
327 gtk_widget_show (priv->placeholder);
329 /* (re)populate the menu */
330 gtk_recent_chooser_menu_populate (menu);
336 gtk_recent_chooser_menu_set_property (GObject *object,
341 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
342 GtkRecentChooserMenuPrivate *priv = menu->priv;
346 case PROP_SHOW_NUMBERS:
347 priv->show_numbers = g_value_get_boolean (value);
349 case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
350 set_recent_manager (menu, g_value_get_object (value));
352 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
353 priv->show_private = g_value_get_boolean (value);
355 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
356 priv->show_not_found = g_value_get_boolean (value);
358 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
359 gtk_recent_chooser_menu_set_show_tips (menu, g_value_get_boolean (value));
361 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
362 priv->show_icons = g_value_get_boolean (value);
364 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
365 g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
367 G_OBJECT_TYPE_NAME (object));
369 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
370 priv->local_only = g_value_get_boolean (value);
372 case GTK_RECENT_CHOOSER_PROP_LIMIT:
373 priv->limit = g_value_get_int (value);
375 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
376 chooser_set_sort_type (menu, g_value_get_enum (value));
378 case GTK_RECENT_CHOOSER_PROP_FILTER:
379 gtk_recent_chooser_menu_set_current_filter (menu, g_value_get_object (value));
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
388 gtk_recent_chooser_menu_get_property (GObject *object,
393 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
394 GtkRecentChooserMenuPrivate *priv = menu->priv;
398 case PROP_SHOW_NUMBERS:
399 g_value_set_boolean (value, priv->show_numbers);
401 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
402 g_value_set_boolean (value, priv->show_tips);
404 case GTK_RECENT_CHOOSER_PROP_LIMIT:
405 g_value_set_int (value, priv->limit);
407 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
408 g_value_set_boolean (value, priv->local_only);
410 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
411 g_value_set_enum (value, priv->sort_type);
413 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
414 g_value_set_boolean (value, priv->show_private);
416 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
417 g_value_set_boolean (value, priv->show_not_found);
419 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
420 g_value_set_boolean (value, priv->show_icons);
422 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
423 g_value_set_boolean (value, FALSE);
425 case GTK_RECENT_CHOOSER_PROP_FILTER:
426 g_value_set_object (value, priv->current_filter);
429 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
435 gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
439 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
441 GtkWidget *menu_item = NULL;
442 gboolean found = FALSE;
444 children = gtk_container_get_children (GTK_CONTAINER (menu));
446 for (l = children; l != NULL; l = l->next)
450 menu_item = GTK_WIDGET (l->data);
452 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
456 if (strcmp (uri, gtk_recent_info_get_uri (info)) == 0)
458 gtk_menu_shell_activate_item (GTK_MENU_SHELL (menu),
467 g_list_free (children);
471 g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
472 GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
473 _("No recently used resource found with URI `%s'"),
481 gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser)
483 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
484 GtkWidget *menu_item;
487 menu_item = gtk_menu_get_active (GTK_MENU (menu));
491 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
495 return g_strdup (gtk_recent_info_get_uri (info));
499 gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
503 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
505 GtkWidget *menu_item = NULL;
506 gboolean found = FALSE;
508 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 (0 == strcmp (uri, gtk_recent_info_get_uri (info)))
523 g_list_free (children);
527 g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
528 GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
529 _("No recently used resource found with URI `%s'"),
535 gtk_menu_shell_select_item (GTK_MENU_SHELL (menu), menu_item);
542 gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
545 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
547 gtk_menu_shell_deselect (GTK_MENU_SHELL (menu));
551 gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser)
553 g_warning (_("This function is not implemented for "
554 "widgets of class '%s'"),
555 g_type_name (G_OBJECT_TYPE (chooser)));
559 gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser)
561 g_warning (_("This function is not implemented for "
562 "widgets of class '%s'"),
563 g_type_name (G_OBJECT_TYPE (chooser)));
567 gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser *chooser,
568 GtkRecentSortFunc sort_func,
570 GDestroyNotify data_destroy)
572 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
573 GtkRecentChooserMenuPrivate *priv = menu->priv;
575 if (priv->sort_data_destroy)
577 priv->sort_data_destroy (priv->sort_data);
578 priv->sort_data_destroy = NULL;
581 priv->sort_func = NULL;
582 priv->sort_data = NULL;
583 priv->sort_data_destroy = NULL;
587 priv->sort_func = sort_func;
588 priv->sort_data = sort_data;
589 priv->sort_data_destroy = data_destroy;
594 chooser_set_sort_type (GtkRecentChooserMenu *menu,
595 GtkRecentSortType sort_type)
597 if (menu->priv->sort_type == sort_type)
600 menu->priv->sort_type = sort_type;
605 gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser)
607 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
608 GtkRecentChooserMenuPrivate *priv = menu->priv;
610 return _gtk_recent_chooser_get_items (chooser,
611 priv->current_filter,
616 static GtkRecentManager *
617 gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser)
619 GtkRecentChooserMenuPrivate *priv;
621 priv = GTK_RECENT_CHOOSER_MENU (chooser)->priv;
623 return priv->manager;
627 gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
628 GtkRecentFilter *filter)
630 GtkRecentChooserMenu *menu;
632 menu = GTK_RECENT_CHOOSER_MENU (chooser);
634 gtk_recent_chooser_menu_set_current_filter (menu, filter);
638 gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
639 GtkRecentFilter *filter)
641 GtkRecentChooserMenu *menu;
643 menu = GTK_RECENT_CHOOSER_MENU (chooser);
645 if (filter == menu->priv->current_filter)
647 g_object_unref (menu->priv->current_filter);
648 menu->priv->current_filter = NULL;
650 g_object_notify (G_OBJECT (menu), "filter");
655 gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser)
657 GtkRecentChooserMenu *menu;
658 GSList *retval = NULL;
660 menu = GTK_RECENT_CHOOSER_MENU (chooser);
662 if (menu->priv->current_filter)
663 retval = g_slist_prepend (retval, menu->priv->current_filter);
669 gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
670 GtkRecentFilter *filter)
672 GtkRecentChooserMenuPrivate *priv;
676 if (priv->current_filter)
677 g_object_unref (G_OBJECT (priv->current_filter));
681 priv->current_filter = filter;
682 g_object_ref_sink (priv->current_filter);
685 gtk_recent_chooser_menu_populate (menu);
687 g_object_notify (G_OBJECT (menu), "filter");
690 /* taken from libeel/eel-strings.c */
692 escape_underscores (const gchar *string)
703 for (p = string; *p != '\0'; p++)
704 underscores += (*p == '_');
706 if (underscores == 0)
707 return g_strdup (string);
709 escaped = g_new (char, strlen (string) + underscores + 1);
710 for (p = string, q = escaped; *p != '\0'; p++, q++)
712 /* Add an extra underscore. */
725 gtk_recent_chooser_menu_add_tip (GtkRecentChooserMenu *menu,
729 GtkRecentChooserMenuPrivate *priv;
732 g_assert (info != NULL);
733 g_assert (item != NULL);
737 path = gtk_recent_info_get_uri_display (info);
740 gchar *tip_text = g_strdup_printf (_("Open '%s'"), path);
742 gtk_widget_set_tooltip_text (item, tip_text);
743 gtk_widget_set_has_tooltip (item, priv->show_tips);
751 gtk_recent_chooser_menu_create_item (GtkRecentChooserMenu *menu,
755 GtkRecentChooserMenuPrivate *priv;
757 GtkWidget *item, *image, *label;
760 g_assert (info != NULL);
764 if (priv->show_numbers)
766 gchar *name, *escaped;
768 name = g_strdup (gtk_recent_info_get_display_name (info));
770 name = g_strdup (_("Unknown item"));
772 escaped = escape_underscores (name);
774 /* avoid clashing mnemonics */
776 /* This is the label format that is used for the first 10 items
777 * in a recent files menu. The %d is the number of the item,
778 * the %s is the name of the item. Please keep the _ in front
779 * of the number to give these menu items a mnemonic.
781 * Don't include the prefix "recent menu label|" in the translation.
783 text = g_strdup_printf (Q_("recent menu label|_%d. %s"), count, escaped);
785 /* This is the format that is used for items in a recent files menu.
786 * The %d is the number of the item, the %s is the name of the item.
788 * Don't include the prefix "recent menu label|" in the translation.
790 text = g_strdup_printf (Q_("recent menu label|%d. %s"), count, escaped);
792 item = gtk_image_menu_item_new_with_mnemonic (text);
799 text = g_strdup (gtk_recent_info_get_display_name (info));
800 item = gtk_image_menu_item_new_with_label (text);
805 /* ellipsize the menu item label, in case the recent document
806 * display name is huge.
808 label = GTK_BIN (item)->child;
809 if (GTK_IS_LABEL (label))
811 gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
812 gtk_label_set_max_width_chars (GTK_LABEL (label), priv->label_width);
815 if (priv->show_icons)
817 icon = gtk_recent_info_get_icon (info, priv->icon_size);
819 image = gtk_image_new_from_pixbuf (icon);
820 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
821 g_object_unref (icon);
824 g_signal_connect (item, "activate",
825 G_CALLBACK (item_activate_cb),
832 gtk_recent_chooser_menu_insert_item (GtkRecentChooserMenu *menu,
836 GtkRecentChooserMenuPrivate *priv = menu->priv;
839 if (priv->first_recent_item_pos == -1)
843 children = gtk_container_get_children (GTK_CONTAINER (menu));
845 for (real_position = 0, l = children;
847 real_position += 1, l = l->next)
849 GObject *child = l->data;
850 gboolean is_placeholder = FALSE;
853 GPOINTER_TO_INT (g_object_get_data (child, "gtk-recent-menu-placeholder"));
859 g_list_free (children);
860 priv->first_recent_item_pos = real_position;
863 real_position = priv->first_recent_item_pos;
865 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), menuitem,
866 real_position + position);
867 gtk_widget_show (menuitem);
870 /* removes the items we own from the menu */
872 gtk_recent_chooser_menu_dispose_items (GtkRecentChooserMenu *menu)
876 children = gtk_container_get_children (GTK_CONTAINER (menu));
877 for (l = children; l != NULL; l = l->next)
879 GtkWidget *menu_item = GTK_WIDGET (l->data);
880 gboolean has_mark = FALSE;
882 /* check for our mark, in order to remove just the items we own */
884 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item), "gtk-recent-menu-mark"));
890 /* destroy the attached RecentInfo struct, if found */
891 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
893 g_object_set_data_full (G_OBJECT (menu_item), "gtk-recent-info",
896 /* and finally remove the item from the menu */
897 gtk_container_remove (GTK_CONTAINER (menu), menu_item);
901 /* recalculate the position of the first recent item */
902 menu->priv->first_recent_item_pos = -1;
904 g_list_free (children);
912 gint displayed_items;
913 GtkRecentChooserMenu *menu;
914 GtkWidget *placeholder;
918 idle_populate_func (gpointer data)
920 MenuPopulateData *pdata;
921 GtkRecentChooserMenuPrivate *priv;
926 pdata = (MenuPopulateData *) data;
927 priv = pdata->menu->priv;
931 pdata->items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (pdata->menu));
934 /* show the placeholder here */
935 gtk_widget_show (pdata->placeholder);
936 pdata->displayed_items = 1;
937 priv->populate_id = 0;
942 pdata->n_items = g_list_length (pdata->items);
943 pdata->loaded_items = 0;
946 info = g_list_nth_data (pdata->items, pdata->loaded_items);
947 item = gtk_recent_chooser_menu_create_item (pdata->menu,
949 pdata->displayed_items);
951 goto check_and_return;
953 gtk_recent_chooser_menu_add_tip (pdata->menu, info, item);
954 gtk_recent_chooser_menu_insert_item (pdata->menu, item,
955 pdata->displayed_items);
957 pdata->displayed_items += 1;
959 /* mark the menu item as one of our own */
960 g_object_set_data (G_OBJECT (item),
961 "gtk-recent-menu-mark",
962 GINT_TO_POINTER (TRUE));
964 /* attach the RecentInfo object to the menu item, and own a reference
965 * to it, so that it will be destroyed with the menu item when it's
966 * not needed anymore.
968 g_object_set_data_full (G_OBJECT (item), "gtk-recent-info",
969 gtk_recent_info_ref (info),
970 (GDestroyNotify) gtk_recent_info_unref);
973 pdata->loaded_items += 1;
975 if (pdata->loaded_items == pdata->n_items)
977 g_list_foreach (pdata->items, (GFunc) gtk_recent_info_unref, NULL);
978 g_list_free (pdata->items);
980 priv->populate_id = 0;
991 idle_populate_clean_up (gpointer data)
993 MenuPopulateData *pdata = data;
995 if (pdata->menu->priv->populate_id == 0)
997 /* show the placeholder in case no item survived
998 * the filtering process in the idle loop
1000 if (!pdata->displayed_items)
1001 gtk_widget_show (pdata->placeholder);
1002 g_object_unref (pdata->placeholder);
1004 g_slice_free (MenuPopulateData, data);
1009 gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu)
1011 MenuPopulateData *pdata;
1012 GtkRecentChooserMenuPrivate *priv = menu->priv;
1014 if (menu->priv->populate_id)
1017 pdata = g_slice_new (MenuPopulateData);
1018 pdata->items = NULL;
1020 pdata->loaded_items = 0;
1021 pdata->displayed_items = 0;
1023 pdata->placeholder = g_object_ref (priv->placeholder);
1025 priv->icon_size = get_icon_size_for_widget (GTK_WIDGET (menu));
1027 /* remove our menu items first and hide the placeholder */
1028 gtk_recent_chooser_menu_dispose_items (menu);
1029 gtk_widget_hide (priv->placeholder);
1031 priv->populate_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
1034 idle_populate_clean_up);
1037 /* bounce activate signal from the recent menu item widget
1038 * to the recent menu widget
1041 item_activate_cb (GtkWidget *widget,
1044 GtkRecentChooser *chooser = GTK_RECENT_CHOOSER (user_data);
1046 _gtk_recent_chooser_item_activated (chooser);
1049 /* we force a redraw if the manager changes when we are showing */
1051 manager_changed_cb (GtkRecentManager *manager,
1054 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (user_data);
1056 gtk_recent_chooser_menu_populate (menu);
1060 set_recent_manager (GtkRecentChooserMenu *menu,
1061 GtkRecentManager *manager)
1063 GtkRecentChooserMenuPrivate *priv = menu->priv;
1067 if (priv->manager_changed_id)
1069 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
1070 priv->manager_changed_id = 0;
1073 if (priv->populate_id)
1075 g_source_remove (priv->populate_id);
1076 priv->populate_id = 0;
1079 priv->manager = NULL;
1083 priv->manager = manager;
1085 priv->manager = gtk_recent_manager_get_default ();
1088 priv->manager_changed_id = g_signal_connect (priv->manager, "changed",
1089 G_CALLBACK (manager_changed_cb),
1094 get_icon_size_for_widget (GtkWidget *widget)
1096 GtkSettings *settings;
1099 if (gtk_widget_has_screen (widget))
1100 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
1102 settings = gtk_settings_get_default ();
1104 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1106 return MAX (width, height);
1108 return FALLBACK_ICON_SIZE;
1112 foreach_set_shot_tips (GtkWidget *widget,
1115 GtkRecentChooserMenu *menu = user_data;
1116 GtkRecentChooserMenuPrivate *priv = menu->priv;
1119 /* toggle the tooltip only on the items we create */
1121 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "gtk-recent-menu-mark"));
1124 gtk_widget_set_has_tooltip (widget, priv->show_tips);
1128 gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
1131 GtkRecentChooserMenuPrivate *priv = menu->priv;
1133 if (priv->show_tips == show_tips)
1136 priv->show_tips = show_tips;
1137 gtk_container_foreach (GTK_CONTAINER (menu), foreach_set_shot_tips, menu);
1145 * gtk_recent_chooser_menu_new:
1147 * Creates a new #GtkRecentChooserMenu widget.
1149 * This kind of widget shows the list of recently used resources as
1150 * a menu, each item as a menu item. Each item inside the menu might
1151 * have an icon, representing its MIME type, and a number, for mnemonic
1154 * This widget implements the #GtkRecentChooser interface.
1156 * This widget creates its own #GtkRecentManager object. See the
1157 * gtk_recent_chooser_menu_new_for_manager() function to know how to create
1158 * a #GtkRecentChooserMenu widget bound to another #GtkRecentManager object.
1160 * Return value: a new #GtkRecentChooserMenu
1165 gtk_recent_chooser_menu_new (void)
1167 return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1168 "recent-manager", NULL,
1173 * gtk_recent_chooser_menu_new_for_manager:
1174 * @manager: a #GtkRecentManager
1176 * Creates a new #GtkRecentChooserMenu widget using @manager as
1177 * the underlying recently used resources manager.
1179 * This is useful if you have implemented your own recent manager,
1180 * or if you have a customized instance of a #GtkRecentManager
1181 * object or if you wish to share a common #GtkRecentManager object
1182 * among multiple #GtkRecentChooser widgets.
1184 * Return value: a new #GtkRecentChooserMenu, bound to @manager.
1189 gtk_recent_chooser_menu_new_for_manager (GtkRecentManager *manager)
1191 g_return_val_if_fail (manager == NULL || GTK_IS_RECENT_MANAGER (manager), NULL);
1193 return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1194 "recent-manager", manager,
1199 * gtk_recent_chooser_menu_get_show_numbers:
1200 * @menu: a #GtkRecentChooserMenu
1202 * Returns the value set by gtk_recent_chooser_menu_set_show_numbers().
1204 * Return value: %TRUE if numbers should be shown.
1209 gtk_recent_chooser_menu_get_show_numbers (GtkRecentChooserMenu *menu)
1211 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu), FALSE);
1213 return menu->priv->show_numbers;
1217 * gtk_recent_chooser_menu_set_show_numbers:
1218 * @menu: a #GtkRecentChooserMenu
1219 * @show_numbers: whether to show numbers
1221 * Sets whether a number should be added to the items of @menu. The
1222 * numbers are shown to provide a unique character for a mnemonic to
1223 * be used inside ten menu item's label. Only the first the items
1224 * get a number to avoid clashes.
1229 gtk_recent_chooser_menu_set_show_numbers (GtkRecentChooserMenu *menu,
1230 gboolean show_numbers)
1232 g_return_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu));
1234 if (menu->priv->show_numbers == show_numbers)
1237 menu->priv->show_numbers = show_numbers;
1238 g_object_notify (G_OBJECT (menu), "show-numbers");
1241 #define __GTK_RECENT_CHOOSER_MENU_C__
1242 #include "gtkaliasdef.c"