1 /* GTK - The GIMP Toolkit
2 * gtkrecentchoosermenu.c - Recently used items menu widget
3 * Copyright (C) 2005, Emmanuele Bassi
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #include "gtkrecentmanager.h"
26 #include "gtkrecentfilter.h"
27 #include "gtkrecentchooser.h"
28 #include "gtkrecentchooserutils.h"
29 #include "gtkrecentchooserprivate.h"
30 #include "gtkrecentchoosermenu.h"
33 #include "gtkicontheme.h"
34 #include "gtkiconfactory.h"
36 #include "gtksettings.h"
37 #include "gtkmenushell.h"
38 #include "gtkmenuitem.h"
39 #include "gtkimagemenuitem.h"
40 #include "gtkseparatormenuitem.h"
44 #include "gtktooltip.h"
45 #include "gtkactivatable.h"
46 #include "gtktypebuiltins.h"
47 #include "gtkprivate.h"
50 struct _GtkRecentChooserMenuPrivate
52 /* the recent manager object */
53 GtkRecentManager *manager;
55 /* size of the icons of the menu items */
58 /* max size of the menu item label */
61 gint first_recent_item_pos;
62 GtkWidget *placeholder;
64 /* RecentChooser properties */
66 guint show_private : 1;
67 guint show_not_found : 1;
72 guint show_numbers : 1;
74 GtkRecentSortType sort_type;
75 GtkRecentSortFunc sort_func;
77 GDestroyNotify sort_data_destroy;
80 GtkRecentFilter *current_filter;
82 guint local_manager : 1;
83 gulong manager_changed_id;
92 /* activatable properties */
93 PROP_ACTIVATABLE_RELATED_ACTION,
94 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
98 #define FALLBACK_ICON_SIZE 32
99 #define FALLBACK_ITEM_LIMIT 10
100 #define DEFAULT_LABEL_WIDTH 30
102 #define GTK_RECENT_CHOOSER_MENU_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RECENT_CHOOSER_MENU, GtkRecentChooserMenuPrivate))
104 static void gtk_recent_chooser_menu_finalize (GObject *object);
105 static void gtk_recent_chooser_menu_dispose (GObject *object);
106 static GObject *gtk_recent_chooser_menu_constructor (GType type,
107 guint n_construct_properties,
108 GObjectConstructParam *construct_params);
110 static void gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface);
112 static void gtk_recent_chooser_menu_set_property (GObject *object,
116 static void gtk_recent_chooser_menu_get_property (GObject *object,
121 static gboolean gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
124 static gchar * gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser);
125 static gboolean gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
128 static void gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
130 static void gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser);
131 static void gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser);
132 static GList * gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser);
133 static GtkRecentManager *gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser);
134 static void gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser *chooser,
135 GtkRecentSortFunc sort_func,
137 GDestroyNotify data_destroy);
138 static void gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
139 GtkRecentFilter *filter);
140 static void gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
141 GtkRecentFilter *filter);
142 static GSList * gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser);
143 static void gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
144 GtkRecentFilter *filter);
146 static void gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu);
147 static void gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
150 static void set_recent_manager (GtkRecentChooserMenu *menu,
151 GtkRecentManager *manager);
153 static void chooser_set_sort_type (GtkRecentChooserMenu *menu,
154 GtkRecentSortType sort_type);
156 static gint get_icon_size_for_widget (GtkWidget *widget);
158 static void item_activate_cb (GtkWidget *widget,
160 static void manager_changed_cb (GtkRecentManager *manager,
163 static void gtk_recent_chooser_activatable_iface_init (GtkActivatableIface *iface);
164 static void gtk_recent_chooser_activatable_update (GtkActivatable *activatable,
166 const gchar *property_name);
167 static void gtk_recent_chooser_activatable_reset (GtkActivatable *activatable,
170 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserMenu,
171 gtk_recent_chooser_menu,
173 G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
174 gtk_recent_chooser_iface_init)
175 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
176 gtk_recent_chooser_activatable_iface_init))
180 gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface)
182 iface->set_current_uri = gtk_recent_chooser_menu_set_current_uri;
183 iface->get_current_uri = gtk_recent_chooser_menu_get_current_uri;
184 iface->select_uri = gtk_recent_chooser_menu_select_uri;
185 iface->unselect_uri = gtk_recent_chooser_menu_unselect_uri;
186 iface->select_all = gtk_recent_chooser_menu_select_all;
187 iface->unselect_all = gtk_recent_chooser_menu_unselect_all;
188 iface->get_items = gtk_recent_chooser_menu_get_items;
189 iface->get_recent_manager = gtk_recent_chooser_menu_get_recent_manager;
190 iface->set_sort_func = gtk_recent_chooser_menu_set_sort_func;
191 iface->add_filter = gtk_recent_chooser_menu_add_filter;
192 iface->remove_filter = gtk_recent_chooser_menu_remove_filter;
193 iface->list_filters = gtk_recent_chooser_menu_list_filters;
197 gtk_recent_chooser_activatable_iface_init (GtkActivatableIface *iface)
200 iface->update = gtk_recent_chooser_activatable_update;
201 iface->reset = gtk_recent_chooser_activatable_reset;
205 gtk_recent_chooser_menu_class_init (GtkRecentChooserMenuClass *klass)
207 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
209 gobject_class->constructor = gtk_recent_chooser_menu_constructor;
210 gobject_class->dispose = gtk_recent_chooser_menu_dispose;
211 gobject_class->finalize = gtk_recent_chooser_menu_finalize;
212 gobject_class->set_property = gtk_recent_chooser_menu_set_property;
213 gobject_class->get_property = gtk_recent_chooser_menu_get_property;
215 _gtk_recent_chooser_install_properties (gobject_class);
218 * GtkRecentChooserMenu:show-numbers
220 * Whether the first ten items in the menu should be prepended by
221 * a number acting as a unique mnemonic.
225 g_object_class_install_property (gobject_class,
227 g_param_spec_boolean ("show-numbers",
229 P_("Whether the items should be displayed with a number"),
231 GTK_PARAM_READWRITE));
234 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
235 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
237 g_type_class_add_private (klass, sizeof (GtkRecentChooserMenuPrivate));
241 gtk_recent_chooser_menu_init (GtkRecentChooserMenu *menu)
243 GtkRecentChooserMenuPrivate *priv;
245 priv = GTK_RECENT_CHOOSER_MENU_GET_PRIVATE (menu);
249 priv->show_icons= TRUE;
250 priv->show_numbers = FALSE;
251 priv->show_tips = FALSE;
252 priv->show_not_found = TRUE;
253 priv->show_private = FALSE;
254 priv->local_only = TRUE;
256 priv->limit = FALLBACK_ITEM_LIMIT;
257 priv->sort_type = GTK_RECENT_SORT_NONE;
258 priv->icon_size = FALLBACK_ICON_SIZE;
259 priv->label_width = DEFAULT_LABEL_WIDTH;
261 priv->first_recent_item_pos = -1;
262 priv->placeholder = NULL;
264 priv->current_filter = NULL;
268 gtk_recent_chooser_menu_finalize (GObject *object)
270 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
271 GtkRecentChooserMenuPrivate *priv = menu->priv;
273 priv->manager = NULL;
275 if (priv->sort_data_destroy)
277 priv->sort_data_destroy (priv->sort_data);
278 priv->sort_data_destroy = NULL;
281 priv->sort_data = NULL;
282 priv->sort_func = NULL;
284 G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->finalize (object);
288 gtk_recent_chooser_menu_dispose (GObject *object)
290 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
291 GtkRecentChooserMenuPrivate *priv = menu->priv;
293 if (priv->manager_changed_id)
296 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
298 priv->manager_changed_id = 0;
301 if (priv->populate_id)
303 g_source_remove (priv->populate_id);
304 priv->populate_id = 0;
307 if (priv->current_filter)
309 g_object_unref (priv->current_filter);
310 priv->current_filter = NULL;
313 G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->dispose (object);
317 gtk_recent_chooser_menu_constructor (GType type,
319 GObjectConstructParam *params)
321 GtkRecentChooserMenu *menu;
322 GtkRecentChooserMenuPrivate *priv;
323 GObjectClass *parent_class;
326 parent_class = G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class);
327 object = parent_class->constructor (type, n_params, params);
328 menu = GTK_RECENT_CHOOSER_MENU (object);
331 g_assert (priv->manager);
333 /* we create a placeholder menuitem, to be used in case
334 * the menu is empty. this placeholder will stay around
335 * for the entire lifetime of the menu, and we just hide it
336 * when it's not used. we have to do this, and do it here,
337 * because we need a marker for the beginning of the recent
338 * items list, so that we can insert the new items at the
339 * right place when idly populating the menu in case the
340 * user appended or prepended custom menu items to the
341 * recent chooser menu widget.
343 priv->placeholder = gtk_menu_item_new_with_label (_("No items found"));
344 gtk_widget_set_sensitive (priv->placeholder, FALSE);
345 g_object_set_data (G_OBJECT (priv->placeholder),
346 "gtk-recent-menu-placeholder",
347 GINT_TO_POINTER (TRUE));
349 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), priv->placeholder, 0);
350 gtk_widget_set_no_show_all (priv->placeholder, TRUE);
351 gtk_widget_show (priv->placeholder);
353 /* (re)populate the menu */
354 gtk_recent_chooser_menu_populate (menu);
360 gtk_recent_chooser_menu_set_property (GObject *object,
365 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
366 GtkRecentChooserMenuPrivate *priv = menu->priv;
370 case PROP_SHOW_NUMBERS:
371 priv->show_numbers = g_value_get_boolean (value);
373 case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
374 set_recent_manager (menu, g_value_get_object (value));
376 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
377 priv->show_private = g_value_get_boolean (value);
379 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
380 priv->show_not_found = g_value_get_boolean (value);
382 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
383 gtk_recent_chooser_menu_set_show_tips (menu, g_value_get_boolean (value));
385 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
386 priv->show_icons = g_value_get_boolean (value);
388 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
389 g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
391 G_OBJECT_TYPE_NAME (object));
393 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
394 priv->local_only = g_value_get_boolean (value);
396 case GTK_RECENT_CHOOSER_PROP_LIMIT:
397 priv->limit = g_value_get_int (value);
399 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
400 chooser_set_sort_type (menu, g_value_get_enum (value));
402 case GTK_RECENT_CHOOSER_PROP_FILTER:
403 gtk_recent_chooser_menu_set_current_filter (menu, g_value_get_object (value));
405 case PROP_ACTIVATABLE_RELATED_ACTION:
406 _gtk_recent_chooser_set_related_action (GTK_RECENT_CHOOSER (menu), g_value_get_object (value));
408 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
409 _gtk_recent_chooser_set_use_action_appearance (GTK_RECENT_CHOOSER (menu), g_value_get_boolean (value));
412 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
418 gtk_recent_chooser_menu_get_property (GObject *object,
423 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
424 GtkRecentChooserMenuPrivate *priv = menu->priv;
428 case PROP_SHOW_NUMBERS:
429 g_value_set_boolean (value, priv->show_numbers);
431 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
432 g_value_set_boolean (value, priv->show_tips);
434 case GTK_RECENT_CHOOSER_PROP_LIMIT:
435 g_value_set_int (value, priv->limit);
437 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
438 g_value_set_boolean (value, priv->local_only);
440 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
441 g_value_set_enum (value, priv->sort_type);
443 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
444 g_value_set_boolean (value, priv->show_private);
446 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
447 g_value_set_boolean (value, priv->show_not_found);
449 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
450 g_value_set_boolean (value, priv->show_icons);
452 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
453 g_value_set_boolean (value, FALSE);
455 case GTK_RECENT_CHOOSER_PROP_FILTER:
456 g_value_set_object (value, priv->current_filter);
458 case PROP_ACTIVATABLE_RELATED_ACTION:
459 g_value_set_object (value, _gtk_recent_chooser_get_related_action (GTK_RECENT_CHOOSER (menu)));
461 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
462 g_value_set_boolean (value, _gtk_recent_chooser_get_use_action_appearance (GTK_RECENT_CHOOSER (menu)));
465 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
471 gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
475 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
477 GtkWidget *menu_item = NULL;
478 gboolean found = FALSE;
480 children = gtk_container_get_children (GTK_CONTAINER (menu));
482 for (l = children; l != NULL; l = l->next)
486 menu_item = GTK_WIDGET (l->data);
488 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
492 if (strcmp (uri, gtk_recent_info_get_uri (info)) == 0)
494 gtk_menu_shell_activate_item (GTK_MENU_SHELL (menu),
503 g_list_free (children);
507 g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
508 GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
509 _("No recently used resource found with URI `%s'"),
517 gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser)
519 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
520 GtkWidget *menu_item;
523 menu_item = gtk_menu_get_active (GTK_MENU (menu));
527 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
531 return g_strdup (gtk_recent_info_get_uri (info));
535 gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
539 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
541 GtkWidget *menu_item = NULL;
542 gboolean found = FALSE;
544 children = gtk_container_get_children (GTK_CONTAINER (menu));
545 for (l = children; l != NULL; l = l->next)
549 menu_item = GTK_WIDGET (l->data);
551 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
555 if (0 == strcmp (uri, gtk_recent_info_get_uri (info)))
559 g_list_free (children);
563 g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
564 GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
565 _("No recently used resource found with URI `%s'"),
571 gtk_menu_shell_select_item (GTK_MENU_SHELL (menu), menu_item);
578 gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
581 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
583 gtk_menu_shell_deselect (GTK_MENU_SHELL (menu));
587 gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser)
589 g_warning (_("This function is not implemented for "
590 "widgets of class '%s'"),
591 g_type_name (G_OBJECT_TYPE (chooser)));
595 gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser)
597 g_warning (_("This function is not implemented for "
598 "widgets of class '%s'"),
599 g_type_name (G_OBJECT_TYPE (chooser)));
603 gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser *chooser,
604 GtkRecentSortFunc sort_func,
606 GDestroyNotify data_destroy)
608 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
609 GtkRecentChooserMenuPrivate *priv = menu->priv;
611 if (priv->sort_data_destroy)
613 priv->sort_data_destroy (priv->sort_data);
614 priv->sort_data_destroy = NULL;
617 priv->sort_func = NULL;
618 priv->sort_data = NULL;
619 priv->sort_data_destroy = NULL;
623 priv->sort_func = sort_func;
624 priv->sort_data = sort_data;
625 priv->sort_data_destroy = data_destroy;
630 chooser_set_sort_type (GtkRecentChooserMenu *menu,
631 GtkRecentSortType sort_type)
633 if (menu->priv->sort_type == sort_type)
636 menu->priv->sort_type = sort_type;
641 gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser)
643 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
644 GtkRecentChooserMenuPrivate *priv = menu->priv;
646 return _gtk_recent_chooser_get_items (chooser,
647 priv->current_filter,
652 static GtkRecentManager *
653 gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser)
655 GtkRecentChooserMenuPrivate *priv;
657 priv = GTK_RECENT_CHOOSER_MENU (chooser)->priv;
659 return priv->manager;
663 gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
664 GtkRecentFilter *filter)
666 GtkRecentChooserMenu *menu;
668 menu = GTK_RECENT_CHOOSER_MENU (chooser);
670 gtk_recent_chooser_menu_set_current_filter (menu, filter);
674 gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
675 GtkRecentFilter *filter)
677 GtkRecentChooserMenu *menu;
679 menu = GTK_RECENT_CHOOSER_MENU (chooser);
681 if (filter == menu->priv->current_filter)
683 g_object_unref (menu->priv->current_filter);
684 menu->priv->current_filter = NULL;
686 g_object_notify (G_OBJECT (menu), "filter");
691 gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser)
693 GtkRecentChooserMenu *menu;
694 GSList *retval = NULL;
696 menu = GTK_RECENT_CHOOSER_MENU (chooser);
698 if (menu->priv->current_filter)
699 retval = g_slist_prepend (retval, menu->priv->current_filter);
705 gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
706 GtkRecentFilter *filter)
708 GtkRecentChooserMenuPrivate *priv;
712 if (priv->current_filter)
713 g_object_unref (G_OBJECT (priv->current_filter));
717 priv->current_filter = filter;
718 g_object_ref_sink (priv->current_filter);
721 gtk_recent_chooser_menu_populate (menu);
723 g_object_notify (G_OBJECT (menu), "filter");
726 /* taken from libeel/eel-strings.c */
728 escape_underscores (const gchar *string)
739 for (p = string; *p != '\0'; p++)
740 underscores += (*p == '_');
742 if (underscores == 0)
743 return g_strdup (string);
745 escaped = g_new (char, strlen (string) + underscores + 1);
746 for (p = string, q = escaped; *p != '\0'; p++, q++)
748 /* Add an extra underscore. */
761 gtk_recent_chooser_menu_add_tip (GtkRecentChooserMenu *menu,
765 GtkRecentChooserMenuPrivate *priv;
768 g_assert (info != NULL);
769 g_assert (item != NULL);
773 path = gtk_recent_info_get_uri_display (info);
776 gchar *tip_text = g_strdup_printf (_("Open '%s'"), path);
778 gtk_widget_set_tooltip_text (item, tip_text);
779 gtk_widget_set_has_tooltip (item, priv->show_tips);
787 gtk_recent_chooser_menu_create_item (GtkRecentChooserMenu *menu,
791 GtkRecentChooserMenuPrivate *priv;
793 GtkWidget *item, *image, *label;
796 g_assert (info != NULL);
800 if (priv->show_numbers)
802 gchar *name, *escaped;
804 name = g_strdup (gtk_recent_info_get_display_name (info));
806 name = g_strdup (_("Unknown item"));
808 escaped = escape_underscores (name);
810 /* avoid clashing mnemonics */
812 /* This is the label format that is used for the first 10 items
813 * in a recent files menu. The %d is the number of the item,
814 * the %s is the name of the item. Please keep the _ in front
815 * of the number to give these menu items a mnemonic.
817 text = g_strdup_printf (C_("recent menu label", "_%d. %s"), count, escaped);
819 /* This is the format that is used for items in a recent files menu.
820 * The %d is the number of the item, the %s is the name of the item.
822 text = g_strdup_printf (C_("recent menu label", "%d. %s"), count, escaped);
824 item = gtk_image_menu_item_new_with_mnemonic (text);
831 text = g_strdup (gtk_recent_info_get_display_name (info));
832 item = gtk_image_menu_item_new_with_label (text);
837 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item),
840 /* ellipsize the menu item label, in case the recent document
841 * display name is huge.
843 label = GTK_BIN (item)->child;
844 if (GTK_IS_LABEL (label))
846 gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
847 gtk_label_set_max_width_chars (GTK_LABEL (label), priv->label_width);
850 if (priv->show_icons)
852 icon = gtk_recent_info_get_icon (info, priv->icon_size);
854 image = gtk_image_new_from_pixbuf (icon);
855 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
856 g_object_unref (icon);
859 g_signal_connect (item, "activate",
860 G_CALLBACK (item_activate_cb),
867 gtk_recent_chooser_menu_insert_item (GtkRecentChooserMenu *menu,
871 GtkRecentChooserMenuPrivate *priv = menu->priv;
874 if (priv->first_recent_item_pos == -1)
878 children = gtk_container_get_children (GTK_CONTAINER (menu));
880 for (real_position = 0, l = children;
882 real_position += 1, l = l->next)
884 GObject *child = l->data;
885 gboolean is_placeholder = FALSE;
888 GPOINTER_TO_INT (g_object_get_data (child, "gtk-recent-menu-placeholder"));
894 g_list_free (children);
895 priv->first_recent_item_pos = real_position;
898 real_position = priv->first_recent_item_pos;
900 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), menuitem,
901 real_position + position);
902 gtk_widget_show (menuitem);
905 /* removes the items we own from the menu */
907 gtk_recent_chooser_menu_dispose_items (GtkRecentChooserMenu *menu)
911 children = gtk_container_get_children (GTK_CONTAINER (menu));
912 for (l = children; l != NULL; l = l->next)
914 GtkWidget *menu_item = GTK_WIDGET (l->data);
915 gboolean has_mark = FALSE;
917 /* check for our mark, in order to remove just the items we own */
919 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item), "gtk-recent-menu-mark"));
925 /* destroy the attached RecentInfo struct, if found */
926 info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
928 g_object_set_data_full (G_OBJECT (menu_item), "gtk-recent-info",
931 /* and finally remove the item from the menu */
932 gtk_container_remove (GTK_CONTAINER (menu), menu_item);
936 /* recalculate the position of the first recent item */
937 menu->priv->first_recent_item_pos = -1;
939 g_list_free (children);
947 gint displayed_items;
948 GtkRecentChooserMenu *menu;
949 GtkWidget *placeholder;
953 idle_populate_func (gpointer data)
955 MenuPopulateData *pdata;
956 GtkRecentChooserMenuPrivate *priv;
961 pdata = (MenuPopulateData *) data;
962 priv = pdata->menu->priv;
966 pdata->items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (pdata->menu));
969 /* show the placeholder here */
970 gtk_widget_show (pdata->placeholder);
971 pdata->displayed_items = 1;
972 priv->populate_id = 0;
977 gtk_widget_hide (pdata->placeholder);
979 pdata->n_items = g_list_length (pdata->items);
980 pdata->loaded_items = 0;
983 info = g_list_nth_data (pdata->items, pdata->loaded_items);
984 item = gtk_recent_chooser_menu_create_item (pdata->menu,
986 pdata->displayed_items);
988 goto check_and_return;
990 gtk_recent_chooser_menu_add_tip (pdata->menu, info, item);
991 gtk_recent_chooser_menu_insert_item (pdata->menu, item,
992 pdata->displayed_items);
994 pdata->displayed_items += 1;
996 /* mark the menu item as one of our own */
997 g_object_set_data (G_OBJECT (item),
998 "gtk-recent-menu-mark",
999 GINT_TO_POINTER (TRUE));
1001 /* attach the RecentInfo object to the menu item, and own a reference
1002 * to it, so that it will be destroyed with the menu item when it's
1003 * not needed anymore.
1005 g_object_set_data_full (G_OBJECT (item), "gtk-recent-info",
1006 gtk_recent_info_ref (info),
1007 (GDestroyNotify) gtk_recent_info_unref);
1010 pdata->loaded_items += 1;
1012 if (pdata->loaded_items == pdata->n_items)
1014 g_list_foreach (pdata->items, (GFunc) gtk_recent_info_unref, NULL);
1015 g_list_free (pdata->items);
1017 priv->populate_id = 0;
1028 idle_populate_clean_up (gpointer data)
1030 MenuPopulateData *pdata = data;
1032 if (pdata->menu->priv->populate_id == 0)
1034 /* show the placeholder in case no item survived
1035 * the filtering process in the idle loop
1037 if (!pdata->displayed_items)
1038 gtk_widget_show (pdata->placeholder);
1039 g_object_unref (pdata->placeholder);
1041 g_slice_free (MenuPopulateData, data);
1046 gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu)
1048 MenuPopulateData *pdata;
1049 GtkRecentChooserMenuPrivate *priv = menu->priv;
1051 if (menu->priv->populate_id)
1054 pdata = g_slice_new (MenuPopulateData);
1055 pdata->items = NULL;
1057 pdata->loaded_items = 0;
1058 pdata->displayed_items = 0;
1060 pdata->placeholder = g_object_ref (priv->placeholder);
1062 priv->icon_size = get_icon_size_for_widget (GTK_WIDGET (menu));
1064 /* remove our menu items first */
1065 gtk_recent_chooser_menu_dispose_items (menu);
1067 priv->populate_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
1070 idle_populate_clean_up);
1073 /* bounce activate signal from the recent menu item widget
1074 * to the recent menu widget
1077 item_activate_cb (GtkWidget *widget,
1080 GtkRecentChooser *chooser = GTK_RECENT_CHOOSER (user_data);
1082 _gtk_recent_chooser_item_activated (chooser);
1085 /* we force a redraw if the manager changes when we are showing */
1087 manager_changed_cb (GtkRecentManager *manager,
1090 GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (user_data);
1092 gtk_recent_chooser_menu_populate (menu);
1096 set_recent_manager (GtkRecentChooserMenu *menu,
1097 GtkRecentManager *manager)
1099 GtkRecentChooserMenuPrivate *priv = menu->priv;
1103 if (priv->manager_changed_id)
1105 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
1106 priv->manager_changed_id = 0;
1109 if (priv->populate_id)
1111 g_source_remove (priv->populate_id);
1112 priv->populate_id = 0;
1115 priv->manager = NULL;
1119 priv->manager = manager;
1121 priv->manager = gtk_recent_manager_get_default ();
1124 priv->manager_changed_id = g_signal_connect (priv->manager, "changed",
1125 G_CALLBACK (manager_changed_cb),
1130 get_icon_size_for_widget (GtkWidget *widget)
1132 GtkSettings *settings;
1135 if (gtk_widget_has_screen (widget))
1136 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
1138 settings = gtk_settings_get_default ();
1140 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1142 return MAX (width, height);
1144 return FALLBACK_ICON_SIZE;
1148 foreach_set_shot_tips (GtkWidget *widget,
1151 GtkRecentChooserMenu *menu = user_data;
1152 GtkRecentChooserMenuPrivate *priv = menu->priv;
1155 /* toggle the tooltip only on the items we create */
1157 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "gtk-recent-menu-mark"));
1160 gtk_widget_set_has_tooltip (widget, priv->show_tips);
1164 gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
1167 GtkRecentChooserMenuPrivate *priv = menu->priv;
1169 if (priv->show_tips == show_tips)
1172 priv->show_tips = show_tips;
1173 gtk_container_foreach (GTK_CONTAINER (menu), foreach_set_shot_tips, menu);
1177 gtk_recent_chooser_activatable_update (GtkActivatable *activatable,
1179 const gchar *property_name)
1181 if (strcmp (property_name, "sensitive") == 0)
1182 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1184 _gtk_recent_chooser_activatable_update (activatable, action, property_name);
1188 gtk_recent_chooser_activatable_reset (GtkActivatable *activatable,
1191 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1193 _gtk_recent_chooser_activatable_reset (activatable, action);
1202 * gtk_recent_chooser_menu_new:
1204 * Creates a new #GtkRecentChooserMenu widget.
1206 * This kind of widget shows the list of recently used resources as
1207 * a menu, each item as a menu item. Each item inside the menu might
1208 * have an icon, representing its MIME type, and a number, for mnemonic
1211 * This widget implements the #GtkRecentChooser interface.
1213 * This widget creates its own #GtkRecentManager object. See the
1214 * gtk_recent_chooser_menu_new_for_manager() function to know how to create
1215 * a #GtkRecentChooserMenu widget bound to another #GtkRecentManager object.
1217 * Return value: a new #GtkRecentChooserMenu
1222 gtk_recent_chooser_menu_new (void)
1224 return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1225 "recent-manager", NULL,
1230 * gtk_recent_chooser_menu_new_for_manager:
1231 * @manager: a #GtkRecentManager
1233 * Creates a new #GtkRecentChooserMenu widget using @manager as
1234 * the underlying recently used resources manager.
1236 * This is useful if you have implemented your own recent manager,
1237 * or if you have a customized instance of a #GtkRecentManager
1238 * object or if you wish to share a common #GtkRecentManager object
1239 * among multiple #GtkRecentChooser widgets.
1241 * Return value: a new #GtkRecentChooserMenu, bound to @manager.
1246 gtk_recent_chooser_menu_new_for_manager (GtkRecentManager *manager)
1248 g_return_val_if_fail (manager == NULL || GTK_IS_RECENT_MANAGER (manager), NULL);
1250 return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1251 "recent-manager", manager,
1256 * gtk_recent_chooser_menu_get_show_numbers:
1257 * @menu: a #GtkRecentChooserMenu
1259 * Returns the value set by gtk_recent_chooser_menu_set_show_numbers().
1261 * Return value: %TRUE if numbers should be shown.
1266 gtk_recent_chooser_menu_get_show_numbers (GtkRecentChooserMenu *menu)
1268 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu), FALSE);
1270 return menu->priv->show_numbers;
1274 * gtk_recent_chooser_menu_set_show_numbers:
1275 * @menu: a #GtkRecentChooserMenu
1276 * @show_numbers: whether to show numbers
1278 * Sets whether a number should be added to the items of @menu. The
1279 * numbers are shown to provide a unique character for a mnemonic to
1280 * be used inside ten menu item's label. Only the first the items
1281 * get a number to avoid clashes.
1286 gtk_recent_chooser_menu_set_show_numbers (GtkRecentChooserMenu *menu,
1287 gboolean show_numbers)
1289 g_return_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu));
1291 if (menu->priv->show_numbers == show_numbers)
1294 menu->priv->show_numbers = show_numbers;
1295 g_object_notify (G_OBJECT (menu), "show-numbers");
1298 #define __GTK_RECENT_CHOOSER_MENU_C__
1299 #include "gtkaliasdef.c"