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;
237 priv->sort_type = GTK_RECENT_SORT_NONE;
238 priv->icon_size = FALLBACK_ICON_SIZE;
239 priv->label_width = DEFAULT_LABEL_WIDTH;
241 priv->first_recent_item_pos = -1;
242 priv->placeholder = NULL;
244 priv->current_filter = NULL;
246 priv->tooltips = gtk_tooltips_new ();
247 g_object_ref_sink (priv->tooltips);
251 gtk_recent_chooser_menu_finalize (GObject *object)
253 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
254 GtkRecentChooserMenuPrivate *priv = menu->priv;
256 priv->manager = NULL;
258 if (priv->sort_data_destroy)
260 priv->sort_data_destroy (priv->sort_data);
261 priv->sort_data_destroy = NULL;
264 priv->sort_data = NULL;
265 priv->sort_func = NULL;
267 G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->finalize (object);
271 gtk_recent_chooser_menu_dispose (GObject *object)
273 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
274 GtkRecentChooserMenuPrivate *priv = menu->priv;
276 if (priv->manager_changed_id)
279 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
281 priv->manager_changed_id = 0;
284 if (priv->populate_id)
286 g_source_remove (priv->populate_id);
287 priv->populate_id = 0;
292 g_object_unref (priv->tooltips);
293 priv->tooltips = NULL;
296 if (priv->current_filter)
298 g_object_unref (priv->current_filter);
299 priv->current_filter = NULL;
302 G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->dispose (object);
306 gtk_recent_chooser_menu_constructor (GType type,
308 GObjectConstructParam *params)
310 GtkRecentChooserMenu *menu;
311 GtkRecentChooserMenuPrivate *priv;
312 GObjectClass *parent_class;
315 parent_class = G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class);
316 object = parent_class->constructor (type, n_params, params);
317 menu = GTK_RECENT_CHOOSER_MENU (object);
320 g_assert (priv->manager);
322 /* we create a placeholder menuitem, to be used in case
323 * the menu is empty. this placeholder will stay around
324 * for the entire lifetime of the menu, and we just hide it
325 * when it's not used. we have to do this, and do it here,
326 * because we need a marker for the beginning of the recent
327 * items list, so that we can insert the new items at the
328 * right place when idly populating the menu in case the
329 * user appended or prepended custom menu items to the
330 * recent chooser menu widget.
332 priv->placeholder = gtk_menu_item_new_with_label (_("No items found"));
333 gtk_widget_set_sensitive (priv->placeholder, FALSE);
334 g_object_set_data (G_OBJECT (priv->placeholder),
335 "gtk-recent-menu-placeholder",
336 GINT_TO_POINTER (TRUE));
338 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), priv->placeholder, 0);
339 gtk_widget_show (priv->placeholder);
341 /* (re)populate the menu */
342 gtk_recent_chooser_menu_populate (menu);
348 gtk_recent_chooser_menu_set_property (GObject *object,
353 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
354 GtkRecentChooserMenuPrivate *priv = menu->priv;
358 case PROP_SHOW_NUMBERS:
359 priv->show_numbers = g_value_get_boolean (value);
361 case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
362 set_recent_manager (menu, g_value_get_object (value));
364 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
365 priv->show_private = g_value_get_boolean (value);
367 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
368 priv->show_not_found = g_value_get_boolean (value);
370 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
371 gtk_recent_chooser_menu_set_show_tips (menu, g_value_get_boolean (value));
373 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
374 priv->show_icons = g_value_get_boolean (value);
376 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
377 g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
379 G_OBJECT_TYPE_NAME (object));
381 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
382 priv->local_only = g_value_get_boolean (value);
384 case GTK_RECENT_CHOOSER_PROP_LIMIT:
385 priv->limit = g_value_get_int (value);
387 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
388 chooser_set_sort_type (menu, g_value_get_enum (value));
390 case GTK_RECENT_CHOOSER_PROP_FILTER:
391 gtk_recent_chooser_menu_set_current_filter (menu, g_value_get_object (value));
394 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
400 gtk_recent_chooser_menu_get_property (GObject *object,
405 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
406 GtkRecentChooserMenuPrivate *priv = menu->priv;
410 case PROP_SHOW_NUMBERS:
411 g_value_set_boolean (value, priv->show_numbers);
413 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
414 g_value_set_boolean (value, priv->show_tips);
416 case GTK_RECENT_CHOOSER_PROP_LIMIT:
417 g_value_set_int (value, priv->limit);
419 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
420 g_value_set_boolean (value, priv->local_only);
422 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
423 g_value_set_enum (value, priv->sort_type);
425 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
426 g_value_set_boolean (value, priv->show_private);
428 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
429 g_value_set_boolean (value, priv->show_not_found);
431 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
432 g_value_set_boolean (value, priv->show_icons);
434 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
435 g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
437 G_OBJECT_TYPE_NAME (object));
439 case GTK_RECENT_CHOOSER_PROP_FILTER:
440 g_value_set_object (value, priv->current_filter);
443 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
449 gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
453 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
455 GtkWidget *menu_item = NULL;
456 gboolean found = FALSE;
458 children = gtk_container_get_children (GTK_CONTAINER (menu));
460 for (l = children; l != NULL; l = l->next)
464 menu_item = GTK_WIDGET (l->data);
466 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
470 if (strcmp (uri, gtk_recent_info_get_uri (info)) == 0)
472 gtk_menu_shell_activate_item (GTK_MENU_SHELL (menu),
481 g_list_free (children);
485 g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
486 GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
487 _("No recently used resource found with URI `%s'"),
495 gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser)
497 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
498 GtkWidget *menu_item;
501 menu_item = gtk_menu_get_active (GTK_MENU (menu));
505 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
509 return g_strdup (gtk_recent_info_get_uri (info));
513 gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
517 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
519 GtkWidget *menu_item = NULL;
520 gboolean found = FALSE;
522 children = gtk_container_get_children (GTK_CONTAINER (menu));
523 for (l = children; l != NULL; l = l->next)
527 menu_item = GTK_WIDGET (l->data);
529 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
533 if (0 == strcmp (uri, gtk_recent_info_get_uri (info)))
537 g_list_free (children);
541 g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
542 GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
543 _("No recently used resource found with URI `%s'"),
549 gtk_menu_shell_select_item (GTK_MENU_SHELL (menu), menu_item);
556 gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
559 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
561 gtk_menu_shell_deselect (GTK_MENU_SHELL (menu));
565 gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser)
567 g_warning (_("This function is not implemented for "
568 "widgets of class '%s'"),
569 g_type_name (G_OBJECT_TYPE (chooser)));
573 gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser)
575 g_warning (_("This function is not implemented for "
576 "widgets of class '%s'"),
577 g_type_name (G_OBJECT_TYPE (chooser)));
581 gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser *chooser,
582 GtkRecentSortFunc sort_func,
584 GDestroyNotify data_destroy)
586 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
587 GtkRecentChooserMenuPrivate *priv = menu->priv;
589 if (priv->sort_data_destroy)
591 priv->sort_data_destroy (priv->sort_data);
592 priv->sort_data_destroy = NULL;
595 priv->sort_func = NULL;
596 priv->sort_data = NULL;
597 priv->sort_data_destroy = NULL;
601 priv->sort_func = sort_func;
602 priv->sort_data = sort_data;
603 priv->sort_data_destroy = data_destroy;
608 chooser_set_sort_type (GtkRecentChooserMenu *menu,
609 GtkRecentSortType sort_type)
611 if (menu->priv->sort_type == sort_type)
614 menu->priv->sort_type = sort_type;
619 gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser)
621 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
622 GtkRecentChooserMenuPrivate *priv = menu->priv;
624 return _gtk_recent_chooser_get_items (chooser,
625 priv->current_filter,
630 static GtkRecentManager *
631 gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser)
633 GtkRecentChooserMenuPrivate *priv;
635 priv = GTK_RECENT_CHOOSER_MENU (chooser)->priv;
637 return priv->manager;
641 gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
642 GtkRecentFilter *filter)
644 GtkRecentChooserMenu *menu;
646 menu = GTK_RECENT_CHOOSER_MENU (chooser);
648 gtk_recent_chooser_menu_set_current_filter (menu, filter);
652 gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
653 GtkRecentFilter *filter)
655 GtkRecentChooserMenu *menu;
657 menu = GTK_RECENT_CHOOSER_MENU (chooser);
659 if (filter == menu->priv->current_filter)
661 g_object_unref (menu->priv->current_filter);
662 menu->priv->current_filter = NULL;
664 g_object_notify (G_OBJECT (menu), "filter");
669 gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser)
671 GtkRecentChooserMenu *menu;
672 GSList *retval = NULL;
674 menu = GTK_RECENT_CHOOSER_MENU (chooser);
676 if (menu->priv->current_filter)
677 retval = g_slist_prepend (retval, menu->priv->current_filter);
683 gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
684 GtkRecentFilter *filter)
686 GtkRecentChooserMenuPrivate *priv;
690 if (priv->current_filter)
691 g_object_unref (G_OBJECT (priv->current_filter));
695 priv->current_filter = filter;
696 g_object_ref_sink (priv->current_filter);
699 gtk_recent_chooser_menu_populate (menu);
701 g_object_notify (G_OBJECT (menu), "filter");
704 /* taken from libeel/eel-strings.c */
706 escape_underscores (const gchar *string)
717 for (p = string; *p != '\0'; p++)
718 underscores += (*p == '_');
720 if (underscores == 0)
721 return g_strdup (string);
723 escaped = g_new (char, strlen (string) + underscores + 1);
724 for (p = string, q = escaped; *p != '\0'; p++, q++)
726 /* Add an extra underscore. */
739 gtk_recent_chooser_menu_add_tip (GtkRecentChooserMenu *menu,
743 GtkRecentChooserMenuPrivate *priv;
744 gchar *path, *tip_text;
746 g_assert (info != NULL);
747 g_assert (item != NULL);
754 path = gtk_recent_info_get_uri_display (info);
757 tip_text = g_strdup_printf (_("Open '%s'"), path);
759 gtk_tooltips_set_tip (priv->tooltips,
770 gtk_recent_chooser_menu_create_item (GtkRecentChooserMenu *menu,
774 GtkRecentChooserMenuPrivate *priv;
776 GtkWidget *item, *image, *label;
779 g_assert (info != NULL);
783 if (priv->show_numbers)
785 gchar *name, *escaped;
787 name = g_strdup (gtk_recent_info_get_display_name (info));
789 name = g_strdup (_("Unknown item"));
791 escaped = escape_underscores (name);
793 /* avoid clashing mnemonics */
795 /* This is the label format that is used for the first 10 items
796 * in a recent files menu. The %d is the number of the item,
797 * the %s is the name of the item. Please keep the _ in front
798 * of the number to give these menu items a mnemonic.
800 * Don't include the prefix "recent menu label|" in the translation.
802 text = g_strdup_printf (Q_("recent menu label|_%d. %s"), count, escaped);
804 /* This is the format that is used for items in a recent files menu.
805 * The %d is the number of the item, the %s is the name of the item.
807 * Don't include the prefix "recent menu label|" in the translation.
809 text = g_strdup_printf (Q_("recent menu label|%d. %s"), count, escaped);
811 item = gtk_image_menu_item_new_with_mnemonic (text);
818 text = g_strdup (gtk_recent_info_get_display_name (info));
819 item = gtk_image_menu_item_new_with_label (text);
824 /* ellipsize the menu item label, in case the recent document
825 * display name is huge.
827 label = GTK_BIN (item)->child;
828 if (GTK_IS_LABEL (label))
830 gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
831 gtk_label_set_max_width_chars (GTK_LABEL (label), priv->label_width);
834 if (priv->show_icons)
836 icon = gtk_recent_info_get_icon (info, priv->icon_size);
838 image = gtk_image_new_from_pixbuf (icon);
839 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
840 g_object_unref (icon);
843 g_signal_connect (item, "activate",
844 G_CALLBACK (item_activate_cb),
851 gtk_recent_chooser_menu_insert_item (GtkRecentChooserMenu *menu,
855 GtkRecentChooserMenuPrivate *priv = menu->priv;
858 if (priv->first_recent_item_pos == -1)
862 children = gtk_container_get_children (GTK_CONTAINER (menu));
864 for (real_position = 0, l = children;
866 real_position += 1, l = l->next)
868 GObject *child = l->data;
869 gboolean is_placeholder = FALSE;
872 GPOINTER_TO_INT (g_object_get_data (child, "gtk-recent-menu-placeholder"));
878 g_list_free (children);
879 priv->first_recent_item_pos = real_position;
882 real_position = priv->first_recent_item_pos;
884 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), menuitem,
885 real_position + position);
886 gtk_widget_show (menuitem);
889 /* removes the items we own from the menu */
891 gtk_recent_chooser_menu_dispose_items (GtkRecentChooserMenu *menu)
895 children = gtk_container_get_children (GTK_CONTAINER (menu));
896 for (l = children; l != NULL; l = l->next)
898 GtkWidget *menu_item = GTK_WIDGET (l->data);
899 gboolean has_mark = FALSE;
901 /* check for our mark, in order to remove just the items we own */
903 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item), "gtk-recent-menu-mark"));
909 /* destroy the attached RecentInfo struct, if found */
910 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
912 g_object_set_data_full (G_OBJECT (menu_item), "gtk-recent-info",
915 /* and finally remove the item from the menu */
916 gtk_container_remove (GTK_CONTAINER (menu), menu_item);
920 /* recalculate the position of the first recent item */
921 menu->priv->first_recent_item_pos = -1;
923 g_list_free (children);
931 gint displayed_items;
932 GtkRecentChooserMenu *menu;
933 GtkWidget *placeholder;
937 idle_populate_func (gpointer data)
939 MenuPopulateData *pdata;
940 GtkRecentChooserMenuPrivate *priv;
945 pdata = (MenuPopulateData *) data;
946 priv = pdata->menu->priv;
948 priv->populate_id = 0;
952 pdata->items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (pdata->menu));
955 /* show the placeholder here */
956 gtk_widget_show (pdata->placeholder);
957 pdata->displayed_items = 1;
962 pdata->n_items = g_list_length (pdata->items);
963 pdata->loaded_items = 0;
966 info = g_list_nth_data (pdata->items, pdata->loaded_items);
967 item = gtk_recent_chooser_menu_create_item (pdata->menu,
969 pdata->displayed_items);
971 goto check_and_return;
973 gtk_recent_chooser_menu_add_tip (pdata->menu, info, item);
974 gtk_recent_chooser_menu_insert_item (pdata->menu, item,
975 pdata->displayed_items);
977 pdata->displayed_items += 1;
979 /* mark the menu item as one of our own */
980 g_object_set_data (G_OBJECT (item),
981 "gtk-recent-menu-mark",
982 GINT_TO_POINTER (TRUE));
984 /* attach the RecentInfo object to the menu item, and own a reference
985 * to it, so that it will be destroyed with the menu item when it's
986 * not needed anymore.
988 g_object_set_data_full (G_OBJECT (item), "gtk-recent-info",
989 gtk_recent_info_ref (info),
990 (GDestroyNotify) gtk_recent_info_unref);
993 pdata->loaded_items += 1;
995 if (pdata->loaded_items == pdata->n_items)
997 g_list_foreach (pdata->items, (GFunc) gtk_recent_info_unref, NULL);
998 g_list_free (pdata->items);
1009 idle_populate_clean_up (gpointer data)
1011 MenuPopulateData *pdata = data;
1013 /* show the placeholder in case no item survived
1014 * the filtering process in the idle loop
1016 if (!pdata->displayed_items)
1017 gtk_widget_show (pdata->placeholder);
1018 g_object_unref (pdata->placeholder);
1020 g_slice_free (MenuPopulateData, data);
1024 gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu)
1026 MenuPopulateData *pdata;
1027 GtkRecentChooserMenuPrivate *priv = menu->priv;
1029 if (menu->priv->populate_id)
1032 pdata = g_slice_new (MenuPopulateData);
1033 pdata->items = NULL;
1035 pdata->loaded_items = 0;
1036 pdata->displayed_items = 0;
1038 pdata->placeholder = g_object_ref (priv->placeholder);
1040 priv->icon_size = get_icon_size_for_widget (GTK_WIDGET (menu));
1042 /* remove our menu items first and hide the placeholder */
1043 gtk_recent_chooser_menu_dispose_items (menu);
1044 gtk_widget_hide (priv->placeholder);
1046 priv->populate_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
1049 idle_populate_clean_up);
1052 /* bounce activate signal from the recent menu item widget
1053 * to the recent menu widget
1056 item_activate_cb (GtkWidget *widget,
1059 GtkRecentChooser *chooser = GTK_RECENT_CHOOSER (user_data);
1061 _gtk_recent_chooser_item_activated (chooser);
1064 /* we force a redraw if the manager changes when we are showing */
1066 manager_changed_cb (GtkRecentManager *manager,
1069 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (user_data);
1071 gtk_recent_chooser_menu_populate (menu);
1075 set_recent_manager (GtkRecentChooserMenu *menu,
1076 GtkRecentManager *manager)
1078 GtkRecentChooserMenuPrivate *priv = menu->priv;
1082 if (priv->manager_changed_id)
1084 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
1085 priv->manager_changed_id = 0;
1088 if (priv->populate_id)
1090 g_source_remove (priv->populate_id);
1091 priv->populate_id = 0;
1094 priv->manager = NULL;
1098 priv->manager = manager;
1100 priv->manager = gtk_recent_manager_get_default ();
1103 priv->manager_changed_id = g_signal_connect (priv->manager, "changed",
1104 G_CALLBACK (manager_changed_cb),
1109 get_icon_size_for_widget (GtkWidget *widget)
1111 GtkSettings *settings;
1114 if (gtk_widget_has_screen (widget))
1115 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
1117 settings = gtk_settings_get_default ();
1119 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1121 return MAX (width, height);
1123 return FALLBACK_ICON_SIZE;
1127 gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
1130 if (menu->priv->show_tips == show_tips)
1133 g_assert (menu->priv->tooltips != NULL);
1136 gtk_tooltips_enable (menu->priv->tooltips);
1138 gtk_tooltips_disable (menu->priv->tooltips);
1140 menu->priv->show_tips = show_tips;
1148 * gtk_recent_chooser_menu_new:
1150 * Creates a new #GtkRecentChooserMenu widget.
1152 * This kind of widget shows the list of recently used resources as
1153 * a menu, each item as a menu item. Each item inside the menu might
1154 * have an icon, representing its MIME type, and a number, for mnemonic
1157 * This widget implements the #GtkRecentChooser interface.
1159 * This widget creates its own #GtkRecentManager object. See the
1160 * gtk_recent_chooser_menu_new_for_manager() function to know how to create
1161 * a #GtkRecentChooserMenu widget bound to another #GtkRecentManager object.
1163 * Return value: a new #GtkRecentChooserMenu
1168 gtk_recent_chooser_menu_new (void)
1170 return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1171 "recent-manager", NULL,
1176 * gtk_recent_chooser_menu_new_for_manager:
1177 * @manager: a #GtkRecentManager
1179 * Creates a new #GtkRecentChooserMenu widget using @manager as
1180 * the underlying recently used resources manager.
1182 * This is useful if you have implemented your own recent manager,
1183 * or if you have a customized instance of a #GtkRecentManager
1184 * object or if you wish to share a common #GtkRecentManager object
1185 * among multiple #GtkRecentChooser widgets.
1187 * Return value: a new #GtkRecentChooserMenu, bound to @manager.
1192 gtk_recent_chooser_menu_new_for_manager (GtkRecentManager *manager)
1194 g_return_val_if_fail (manager == NULL || GTK_IS_RECENT_MANAGER (manager), NULL);
1196 return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1197 "recent-manager", manager,
1202 * gtk_recent_chooser_menu_get_show_numbers:
1203 * @menu: a #GtkRecentChooserMenu
1205 * Returns the value set by gtk_recent_chooser_menu_set_show_numbers().
1207 * Return value: %TRUE if numbers should be shown.
1212 gtk_recent_chooser_menu_get_show_numbers (GtkRecentChooserMenu *menu)
1214 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu), FALSE);
1216 return menu->priv->show_numbers;
1220 * gtk_recent_chooser_menu_set_show_numbers:
1221 * @menu: a #GtkRecentChooserMenu
1222 * @show_numbers: whether to show numbers
1224 * Sets whether a number should be added to the items of @menu. The
1225 * numbers are shown to provide a unique character for a mnemonic to
1226 * be used inside ten menu item's label. Only the first the items
1227 * get a number to avoid clashes.
1232 gtk_recent_chooser_menu_set_show_numbers (GtkRecentChooserMenu *menu,
1233 gboolean show_numbers)
1235 g_return_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu));
1237 if (menu->priv->show_numbers == show_numbers)
1240 menu->priv->show_numbers = show_numbers;
1241 g_object_notify (G_OBJECT (menu), "show-numbers");
1244 #define __GTK_RECENT_CHOOSER_MENU_C__
1245 #include "gtkaliasdef.c"