1 /* GTK - The GIMP Toolkit
2 * Recent chooser action for GtkUIManager
4 * Copyright (C) 2007, Emmanuele Bassi
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
23 #include "gtkrecentaction.h"
24 #include "gtkimagemenuitem.h"
25 #include "gtkmenutoolbutton.h"
26 #include "gtkrecentchooser.h"
27 #include "gtkrecentchoosermenu.h"
28 #include "gtkrecentchooserutils.h"
29 #include "gtkrecentchooserprivate.h"
30 #include "gtkprivate.h"
33 * SECTION:gtkrecentaction
34 * @Short_description: An action of which represents a list of recently used files
35 * @Title: GtkRecentAction
37 * A #GtkRecentAction represents a list of recently used files, which
38 * can be shown by widgets such as #GtkRecentChooserDialog or
39 * #GtkRecentChooserMenu.
41 * To construct a submenu showing recently used files, use a #GtkRecentAction
42 * as the action for a <menuitem>. To construct a menu toolbutton showing
43 * the recently used files in the popup menu, use a #GtkRecentAction as the
44 * action for a <toolitem> element.
48 #define FALLBACK_ITEM_LIMIT 10
51 struct _GtkRecentActionPrivate
53 GtkRecentManager *manager;
55 guint show_numbers : 1;
57 /* RecentChooser properties */
58 guint show_private : 1;
59 guint show_not_found : 1;
66 GtkRecentSortType sort_type;
67 GtkRecentSortFunc sort_func;
69 GDestroyNotify data_destroy;
71 GtkRecentFilter *current_filter;
74 GtkRecentChooser *current_chooser;
84 static void gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface);
86 G_DEFINE_TYPE_WITH_CODE (GtkRecentAction,
89 G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
90 gtk_recent_chooser_iface_init));
93 gtk_recent_action_set_current_uri (GtkRecentChooser *chooser,
97 GtkRecentAction *action = GTK_RECENT_ACTION (chooser);
98 GtkRecentActionPrivate *priv = action->priv;
101 for (l = priv->choosers; l; l = l->next)
103 GtkRecentChooser *recent_chooser = l->data;
105 if (!gtk_recent_chooser_set_current_uri (recent_chooser, uri, error))
113 gtk_recent_action_get_current_uri (GtkRecentChooser *chooser)
115 GtkRecentAction *recent_action = GTK_RECENT_ACTION (chooser);
116 GtkRecentActionPrivate *priv = recent_action->priv;
118 if (priv->current_chooser)
119 return gtk_recent_chooser_get_current_uri (priv->current_chooser);
125 gtk_recent_action_select_uri (GtkRecentChooser *chooser,
129 GtkRecentAction *action = GTK_RECENT_ACTION (chooser);
130 GtkRecentActionPrivate *priv = action->priv;
133 for (l = priv->choosers; l; l = l->next)
135 GtkRecentChooser *recent_chooser = l->data;
137 if (!gtk_recent_chooser_select_uri (recent_chooser, uri, error))
145 gtk_recent_action_unselect_uri (GtkRecentChooser *chooser,
148 GtkRecentAction *action = GTK_RECENT_ACTION (chooser);
149 GtkRecentActionPrivate *priv = action->priv;
152 for (l = priv->choosers; l; l = l->next)
154 GtkRecentChooser *chooser = l->data;
156 gtk_recent_chooser_unselect_uri (chooser, uri);
161 gtk_recent_action_select_all (GtkRecentChooser *chooser)
163 g_warning ("This function is not implemented for "
164 "widgets of class '%s'",
165 g_type_name (G_OBJECT_TYPE (chooser)));
169 gtk_recent_action_unselect_all (GtkRecentChooser *chooser)
171 g_warning ("This function is not implemented for "
172 "widgets of class '%s'",
173 g_type_name (G_OBJECT_TYPE (chooser)));
178 gtk_recent_action_get_items (GtkRecentChooser *chooser)
180 GtkRecentAction *action = GTK_RECENT_ACTION (chooser);
181 GtkRecentActionPrivate *priv = action->priv;
183 return _gtk_recent_chooser_get_items (chooser,
184 priv->current_filter,
189 static GtkRecentManager *
190 gtk_recent_action_get_recent_manager (GtkRecentChooser *chooser)
192 return GTK_RECENT_ACTION (chooser)->priv->manager;
196 gtk_recent_action_set_sort_func (GtkRecentChooser *chooser,
197 GtkRecentSortFunc sort_func,
199 GDestroyNotify data_destroy)
201 GtkRecentAction *action = GTK_RECENT_ACTION (chooser);
202 GtkRecentActionPrivate *priv = action->priv;
205 if (priv->data_destroy)
207 priv->data_destroy (priv->sort_data);
208 priv->data_destroy = NULL;
211 priv->sort_func = NULL;
212 priv->sort_data = NULL;
216 priv->sort_func = sort_func;
217 priv->sort_data = sort_data;
218 priv->data_destroy = data_destroy;
221 for (l = priv->choosers; l; l = l->next)
223 GtkRecentChooser *chooser_menu = l->data;
225 gtk_recent_chooser_set_sort_func (chooser_menu, priv->sort_func,
232 set_current_filter (GtkRecentAction *action,
233 GtkRecentFilter *filter)
235 GtkRecentActionPrivate *priv = action->priv;
237 g_object_ref (action);
239 if (priv->current_filter)
240 g_object_unref (priv->current_filter);
242 priv->current_filter = filter;
244 if (priv->current_filter)
245 g_object_ref_sink (priv->current_filter);
247 g_object_notify (G_OBJECT (action), "filter");
249 g_object_unref (action);
253 gtk_recent_action_add_filter (GtkRecentChooser *chooser,
254 GtkRecentFilter *filter)
256 GtkRecentAction *action = GTK_RECENT_ACTION (chooser);
257 GtkRecentActionPrivate *priv = action->priv;
259 if (priv->current_filter != filter)
260 set_current_filter (GTK_RECENT_ACTION (chooser), filter);
264 gtk_recent_action_remove_filter (GtkRecentChooser *chooser,
265 GtkRecentFilter *filter)
267 GtkRecentAction *action = GTK_RECENT_ACTION (chooser);
268 GtkRecentActionPrivate *priv = action->priv;
270 if (priv->current_filter == filter)
271 set_current_filter (GTK_RECENT_ACTION (chooser), NULL);
275 gtk_recent_action_list_filters (GtkRecentChooser *chooser)
277 GtkRecentAction *action = GTK_RECENT_ACTION (chooser);
278 GtkRecentActionPrivate *priv = action->priv;
279 GSList *retval = NULL;
280 GtkRecentFilter *current_filter;
282 current_filter = priv->current_filter;
283 retval = g_slist_prepend (retval, current_filter);
290 gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface)
292 iface->set_current_uri = gtk_recent_action_set_current_uri;
293 iface->get_current_uri = gtk_recent_action_get_current_uri;
294 iface->select_uri = gtk_recent_action_select_uri;
295 iface->unselect_uri = gtk_recent_action_unselect_uri;
296 iface->select_all = gtk_recent_action_select_all;
297 iface->unselect_all = gtk_recent_action_unselect_all;
298 iface->get_items = gtk_recent_action_get_items;
299 iface->get_recent_manager = gtk_recent_action_get_recent_manager;
300 iface->set_sort_func = gtk_recent_action_set_sort_func;
301 iface->add_filter = gtk_recent_action_add_filter;
302 iface->remove_filter = gtk_recent_action_remove_filter;
303 iface->list_filters = gtk_recent_action_list_filters;
307 gtk_recent_action_activate (GtkAction *action)
309 GtkRecentAction *recent_action = GTK_RECENT_ACTION (action);
310 GtkRecentActionPrivate *priv = recent_action->priv;
312 /* we have probably been invoked by a menu tool button or by a
313 * direct call of gtk_action_activate(); since no item has been
314 * selected, we must unset the current recent chooser pointer
316 priv->current_chooser = NULL;
320 delegate_selection_changed (GtkRecentAction *action,
321 GtkRecentChooser *chooser)
323 GtkRecentActionPrivate *priv = action->priv;
325 priv->current_chooser = chooser;
327 g_signal_emit_by_name (action, "selection-changed");
331 delegate_item_activated (GtkRecentAction *action,
332 GtkRecentChooser *chooser)
334 GtkRecentActionPrivate *priv = action->priv;
336 priv->current_chooser = chooser;
338 g_signal_emit_by_name (action, "item-activated");
342 gtk_recent_action_connect_proxy (GtkAction *action,
345 GtkRecentAction *recent_action = GTK_RECENT_ACTION (action);
346 GtkRecentActionPrivate *priv = recent_action->priv;
348 /* it can only be a recent chooser implementor anyway... */
349 if (GTK_IS_RECENT_CHOOSER (widget) &&
350 !g_slist_find (priv->choosers, widget))
354 gtk_recent_chooser_set_sort_func (GTK_RECENT_CHOOSER (widget),
360 g_signal_connect_swapped (widget, "selection_changed",
361 G_CALLBACK (delegate_selection_changed),
363 g_signal_connect_swapped (widget, "item-activated",
364 G_CALLBACK (delegate_item_activated),
368 if (GTK_ACTION_CLASS (gtk_recent_action_parent_class)->connect_proxy)
369 GTK_ACTION_CLASS (gtk_recent_action_parent_class)->connect_proxy (action, widget);
373 gtk_recent_action_disconnect_proxy (GtkAction *action,
376 GtkRecentAction *recent_action = GTK_RECENT_ACTION (action);
377 GtkRecentActionPrivate *priv = recent_action->priv;
379 /* if it was one of the recent choosers we created, remove it
382 if (g_slist_find (priv->choosers, widget))
383 priv->choosers = g_slist_remove (priv->choosers, widget);
385 if (GTK_ACTION_CLASS (gtk_recent_action_parent_class)->disconnect_proxy)
386 GTK_ACTION_CLASS (gtk_recent_action_parent_class)->disconnect_proxy (action, widget);
390 gtk_recent_action_create_menu (GtkAction *action)
392 GtkRecentAction *recent_action = GTK_RECENT_ACTION (action);
393 GtkRecentActionPrivate *priv = recent_action->priv;
396 widget = g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
397 "show-private", priv->show_private,
398 "show-not-found", priv->show_not_found,
399 "show-tips", priv->show_tips,
400 "show-icons", priv->show_icons,
401 "show-numbers", priv->show_numbers,
402 "limit", priv->limit,
403 "sort-type", priv->sort_type,
404 "recent-manager", priv->manager,
405 "filter", priv->current_filter,
406 "local-only", priv->local_only,
411 gtk_recent_chooser_set_sort_func (GTK_RECENT_CHOOSER (widget),
417 g_signal_connect_swapped (widget, "selection_changed",
418 G_CALLBACK (delegate_selection_changed),
420 g_signal_connect_swapped (widget, "item-activated",
421 G_CALLBACK (delegate_item_activated),
424 /* keep track of the choosers we create */
425 priv->choosers = g_slist_prepend (priv->choosers, widget);
431 gtk_recent_action_create_menu_item (GtkAction *action)
436 menu = gtk_recent_action_create_menu (action);
437 menuitem = g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
438 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), menu);
439 gtk_widget_show (menu);
445 gtk_recent_action_create_tool_item (GtkAction *action)
450 menu = gtk_recent_action_create_menu (action);
451 toolitem = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
452 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (toolitem), menu);
453 gtk_widget_show (menu);
459 set_recent_manager (GtkRecentAction *action,
460 GtkRecentManager *manager)
462 GtkRecentActionPrivate *priv = action->priv;
465 priv->manager = NULL;
467 priv->manager = gtk_recent_manager_get_default ();
471 gtk_recent_action_finalize (GObject *gobject)
473 GtkRecentAction *action = GTK_RECENT_ACTION (gobject);
474 GtkRecentActionPrivate *priv = action->priv;
476 priv->manager = NULL;
478 if (priv->data_destroy)
480 priv->data_destroy (priv->sort_data);
481 priv->data_destroy = NULL;
484 priv->sort_data = NULL;
485 priv->sort_func = NULL;
487 g_slist_free (priv->choosers);
489 G_OBJECT_CLASS (gtk_recent_action_parent_class)->finalize (gobject);
493 gtk_recent_action_dispose (GObject *gobject)
495 GtkRecentAction *action = GTK_RECENT_ACTION (gobject);
496 GtkRecentActionPrivate *priv = action->priv;
498 if (priv->current_filter)
500 g_object_unref (priv->current_filter);
501 priv->current_filter = NULL;
504 G_OBJECT_CLASS (gtk_recent_action_parent_class)->dispose (gobject);
508 gtk_recent_action_set_property (GObject *gobject,
513 GtkRecentAction *action = GTK_RECENT_ACTION (gobject);
514 GtkRecentActionPrivate *priv = action->priv;
518 case PROP_SHOW_NUMBERS:
519 priv->show_numbers = g_value_get_boolean (value);
521 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
522 priv->show_private = g_value_get_boolean (value);
524 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
525 priv->show_not_found = g_value_get_boolean (value);
527 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
528 priv->show_tips = g_value_get_boolean (value);
530 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
531 priv->show_icons = g_value_get_boolean (value);
533 case GTK_RECENT_CHOOSER_PROP_LIMIT:
534 priv->limit = g_value_get_int (value);
536 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
537 priv->local_only = g_value_get_boolean (value);
539 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
540 priv->sort_type = g_value_get_enum (value);
542 case GTK_RECENT_CHOOSER_PROP_FILTER:
543 set_current_filter (action, g_value_get_object (value));
545 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
546 g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
548 G_OBJECT_TYPE_NAME (gobject));
550 case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
551 set_recent_manager (action, g_value_get_object (value));
554 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
560 gtk_recent_action_get_property (GObject *gobject,
565 GtkRecentAction *action = GTK_RECENT_ACTION (gobject);
566 GtkRecentActionPrivate *priv = action->priv;
570 case PROP_SHOW_NUMBERS:
571 g_value_set_boolean (value, priv->show_numbers);
573 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
574 g_value_set_boolean (value, priv->show_private);
576 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
577 g_value_set_boolean (value, priv->show_not_found);
579 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
580 g_value_set_boolean (value, priv->show_tips);
582 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
583 g_value_set_boolean (value, priv->show_icons);
585 case GTK_RECENT_CHOOSER_PROP_LIMIT:
586 g_value_set_int (value, priv->limit);
588 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
589 g_value_set_boolean (value, priv->local_only);
591 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
592 g_value_set_enum (value, priv->sort_type);
594 case GTK_RECENT_CHOOSER_PROP_FILTER:
595 g_value_set_object (value, priv->current_filter);
597 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
598 g_value_set_boolean (value, FALSE);
601 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
607 gtk_recent_action_class_init (GtkRecentActionClass *klass)
609 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
610 GtkActionClass *action_class = GTK_ACTION_CLASS (klass);
612 g_type_class_add_private (klass, sizeof (GtkRecentActionPrivate));
614 gobject_class->finalize = gtk_recent_action_finalize;
615 gobject_class->dispose = gtk_recent_action_dispose;
616 gobject_class->set_property = gtk_recent_action_set_property;
617 gobject_class->get_property = gtk_recent_action_get_property;
619 action_class->activate = gtk_recent_action_activate;
620 action_class->connect_proxy = gtk_recent_action_connect_proxy;
621 action_class->disconnect_proxy = gtk_recent_action_disconnect_proxy;
622 action_class->create_menu_item = gtk_recent_action_create_menu_item;
623 action_class->create_tool_item = gtk_recent_action_create_tool_item;
624 action_class->create_menu = gtk_recent_action_create_menu;
625 action_class->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
626 action_class->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
628 _gtk_recent_chooser_install_properties (gobject_class);
630 g_object_class_install_property (gobject_class,
632 g_param_spec_boolean ("show-numbers",
634 P_("Whether the items should be displayed with a number"),
641 gtk_recent_action_init (GtkRecentAction *action)
643 GtkRecentActionPrivate *priv;
645 action->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (action,
646 GTK_TYPE_RECENT_ACTION,
647 GtkRecentActionPrivate);
649 priv->show_numbers = FALSE;
650 priv->show_icons = TRUE;
651 priv->show_tips = FALSE;
652 priv->show_not_found = TRUE;
653 priv->show_private = FALSE;
654 priv->local_only = TRUE;
656 priv->limit = FALLBACK_ITEM_LIMIT;
658 priv->sort_type = GTK_RECENT_SORT_NONE;
659 priv->sort_func = NULL;
660 priv->sort_data = NULL;
661 priv->data_destroy = NULL;
663 priv->current_filter = NULL;
665 priv->manager = NULL;
669 * gtk_recent_action_new:
670 * @name: a unique name for the action
671 * @label: (allow-none): the label displayed in menu items and on buttons,
673 * @tooltip: (allow-none): a tooltip for the action, or %NULL
674 * @stock_id: (allow-none): the stock icon to display in widgets representing
675 * the action, or %NULL
677 * Creates a new #GtkRecentAction object. To add the action to
678 * a #GtkActionGroup and set the accelerator for the action,
679 * call gtk_action_group_add_action_with_accel().
681 * Return value: the newly created #GtkRecentAction.
686 gtk_recent_action_new (const gchar *name,
688 const gchar *tooltip,
689 const gchar *stock_id)
691 g_return_val_if_fail (name != NULL, NULL);
693 return g_object_new (GTK_TYPE_RECENT_ACTION,
697 "stock-id", stock_id,
702 * gtk_recent_action_new_for_manager:
703 * @name: a unique name for the action
704 * @label: (allow-none): the label displayed in menu items and on buttons,
706 * @tooltip: (allow-none): a tooltip for the action, or %NULL
707 * @stock_id: (allow-none): the stock icon to display in widgets representing
708 * the action, or %NULL
709 * @manager: (allow-none): a #GtkRecentManager, or %NULL for using the default
712 * Creates a new #GtkRecentAction object. To add the action to
713 * a #GtkActionGroup and set the accelerator for the action,
714 * call gtk_action_group_add_action_with_accel().
716 * Return value: the newly created #GtkRecentAction
721 gtk_recent_action_new_for_manager (const gchar *name,
723 const gchar *tooltip,
724 const gchar *stock_id,
725 GtkRecentManager *manager)
727 g_return_val_if_fail (name != NULL, NULL);
728 g_return_val_if_fail (manager == NULL || GTK_IS_RECENT_MANAGER (manager), NULL);
730 return g_object_new (GTK_TYPE_RECENT_ACTION,
734 "stock-id", stock_id,
735 "recent-manager", manager,
740 * gtk_recent_action_get_show_numbers:
741 * @action: a #GtkRecentAction
743 * Returns the value set by gtk_recent_chooser_menu_set_show_numbers().
745 * Return value: %TRUE if numbers should be shown.
750 gtk_recent_action_get_show_numbers (GtkRecentAction *action)
752 g_return_val_if_fail (GTK_IS_RECENT_ACTION (action), FALSE);
754 return action->priv->show_numbers;
758 * gtk_recent_action_set_show_numbers:
759 * @action: a #GtkRecentAction
760 * @show_numbers: %TRUE if the shown items should be numbered
762 * Sets whether a number should be added to the items shown by the
763 * widgets representing @action. The numbers are shown to provide
764 * a unique character for a mnemonic to be used inside the menu item's
765 * label. Only the first ten items get a number to avoid clashes.
770 gtk_recent_action_set_show_numbers (GtkRecentAction *action,
771 gboolean show_numbers)
773 GtkRecentActionPrivate *priv;
775 g_return_if_fail (GTK_IS_RECENT_ACTION (action));
779 if (priv->show_numbers != show_numbers)
781 g_object_ref (action);
783 priv->show_numbers = show_numbers;
785 g_object_notify (G_OBJECT (action), "show-numbers");
786 g_object_unref (action);