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, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
25 #include "gtkrecentaction.h"
26 #include "gtkimagemenuitem.h"
27 #include "gtkmenutoolbutton.h"
28 #include "gtkrecentchooser.h"
29 #include "gtkrecentchoosermenu.h"
30 #include "gtkrecentchooserutils.h"
31 #include "gtkrecentchooserprivate.h"
32 #include "gtkprivate.h"
35 #define FALLBACK_ITEM_LIMIT 10
37 #define GTK_RECENT_ACTION_GET_PRIVATE(obj) \
38 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
39 GTK_TYPE_RECENT_ACTION, \
40 GtkRecentActionPrivate))
42 struct _GtkRecentActionPrivate
44 GtkRecentManager *manager;
45 guint manager_changed_id;
47 guint show_numbers : 1;
49 /* RecentChooser properties */
50 guint show_private : 1;
51 guint show_not_found : 1;
58 GtkRecentSortType sort_type;
59 GtkRecentSortFunc sort_func;
61 GDestroyNotify data_destroy;
63 GtkRecentFilter *current_filter;
66 GtkRecentChooser *current_chooser;
76 static void gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface);
78 G_DEFINE_TYPE_WITH_CODE (GtkRecentAction,
81 G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
82 gtk_recent_chooser_iface_init));
85 gtk_recent_action_set_current_uri (GtkRecentChooser *chooser,
89 GtkRecentAction *action = GTK_RECENT_ACTION (chooser);
90 GtkRecentActionPrivate *priv = action->priv;
93 for (l = priv->choosers; l; l = l->next)
95 GtkRecentChooser *recent_chooser = l->data;
97 if (!gtk_recent_chooser_set_current_uri (recent_chooser, uri, error))
105 gtk_recent_action_get_current_uri (GtkRecentChooser *chooser)
107 GtkRecentAction *recent_action = GTK_RECENT_ACTION (chooser);
108 GtkRecentActionPrivate *priv = recent_action->priv;
110 if (priv->current_chooser)
111 return gtk_recent_chooser_get_current_uri (priv->current_chooser);
117 gtk_recent_action_select_uri (GtkRecentChooser *chooser,
121 GtkRecentAction *action = GTK_RECENT_ACTION (chooser);
122 GtkRecentActionPrivate *priv = action->priv;
125 for (l = priv->choosers; l; l = l->next)
127 GtkRecentChooser *recent_chooser = l->data;
129 if (!gtk_recent_chooser_select_uri (recent_chooser, uri, error))
137 gtk_recent_action_unselect_uri (GtkRecentChooser *chooser,
140 GtkRecentAction *action = GTK_RECENT_ACTION (chooser);
141 GtkRecentActionPrivate *priv = action->priv;
144 for (l = priv->choosers; l; l = l->next)
146 GtkRecentChooser *chooser = l->data;
148 gtk_recent_chooser_unselect_uri (chooser, uri);
153 gtk_recent_action_select_all (GtkRecentChooser *chooser)
155 g_warning (_("This function is not implemented for "
156 "widgets of class '%s'"),
157 g_type_name (G_OBJECT_TYPE (chooser)));
161 gtk_recent_action_unselect_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)));
170 gtk_recent_action_get_items (GtkRecentChooser *chooser)
172 GtkRecentAction *action = GTK_RECENT_ACTION (chooser);
173 GtkRecentActionPrivate *priv = action->priv;
175 return _gtk_recent_chooser_get_items (chooser,
176 priv->current_filter,
181 static GtkRecentManager *
182 gtk_recent_action_get_recent_manager (GtkRecentChooser *chooser)
184 return GTK_RECENT_ACTION_GET_PRIVATE (chooser)->manager;
188 gtk_recent_action_set_sort_func (GtkRecentChooser *chooser,
189 GtkRecentSortFunc sort_func,
191 GDestroyNotify data_destroy)
193 GtkRecentAction *action = GTK_RECENT_ACTION (chooser);
194 GtkRecentActionPrivate *priv = action->priv;
197 if (priv->data_destroy)
199 priv->data_destroy (priv->sort_data);
200 priv->data_destroy = NULL;
203 priv->sort_func = NULL;
204 priv->sort_data = NULL;
208 priv->sort_func = sort_func;
209 priv->sort_data = sort_data;
210 priv->data_destroy = data_destroy;
213 for (l = priv->choosers; l; l = l->next)
215 GtkRecentChooser *chooser_menu = l->data;
217 gtk_recent_chooser_set_sort_func (chooser_menu, priv->sort_func,
224 set_current_filter (GtkRecentAction *action,
225 GtkRecentFilter *filter)
227 GtkRecentActionPrivate *priv = action->priv;
230 g_object_ref (action);
232 if (priv->current_filter)
233 g_object_unref (priv->current_filter);
235 priv->current_filter = filter;
237 if (priv->current_filter)
238 g_object_ref_sink (priv->current_filter);
240 for (l = priv->choosers; l; l = l->next)
242 GtkRecentChooser *chooser = l->data;
244 gtk_recent_chooser_set_filter (chooser, 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 GtkRecentActionPrivate *priv = GTK_RECENT_ACTION_GET_PRIVATE (chooser);
258 if (priv->current_filter != filter)
259 set_current_filter (GTK_RECENT_ACTION (chooser), filter);
263 gtk_recent_action_remove_filter (GtkRecentChooser *chooser,
264 GtkRecentFilter *filter)
266 GtkRecentActionPrivate *priv = GTK_RECENT_ACTION_GET_PRIVATE (chooser);
268 if (priv->current_filter == filter)
269 set_current_filter (GTK_RECENT_ACTION (chooser), NULL);
273 gtk_recent_action_list_filters (GtkRecentChooser *chooser)
275 GSList *retval = NULL;
276 GtkRecentFilter *current_filter;
278 current_filter = GTK_RECENT_ACTION_GET_PRIVATE (chooser)->current_filter;
279 retval = g_slist_prepend (retval, current_filter);
286 gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface)
288 iface->set_current_uri = gtk_recent_action_set_current_uri;
289 iface->get_current_uri = gtk_recent_action_get_current_uri;
290 iface->select_uri = gtk_recent_action_select_uri;
291 iface->unselect_uri = gtk_recent_action_unselect_uri;
292 iface->select_all = gtk_recent_action_select_all;
293 iface->unselect_all = gtk_recent_action_unselect_all;
294 iface->get_items = gtk_recent_action_get_items;
295 iface->get_recent_manager = gtk_recent_action_get_recent_manager;
296 iface->set_sort_func = gtk_recent_action_set_sort_func;
297 iface->add_filter = gtk_recent_action_add_filter;
298 iface->remove_filter = gtk_recent_action_remove_filter;
299 iface->list_filters = gtk_recent_action_list_filters;
303 gtk_recent_action_activate (GtkAction *action)
305 /* we have probably been invoked by a menu tool button or by a
306 * direct call of gtk_action_activate(); since no item has been
307 * selected, we must unset the current recent chooser pointer
309 GTK_RECENT_ACTION_GET_PRIVATE (action)->current_chooser = NULL;
313 delegate_selection_changed (GtkRecentAction *action,
314 GtkRecentChooser *chooser)
316 GtkRecentActionPrivate *priv = action->priv;
318 priv->current_chooser = chooser;
320 g_signal_emit_by_name (action, "selection-changed");
324 delegate_item_activated (GtkRecentAction *action,
325 GtkRecentChooser *chooser)
327 GtkRecentActionPrivate *priv = action->priv;
329 priv->current_chooser = chooser;
331 g_signal_emit_by_name (action, "item-activated");
335 gtk_recent_action_connect_proxy (GtkAction *action,
338 GtkRecentAction *recent_action = GTK_RECENT_ACTION (action);
339 GtkRecentActionPrivate *priv = recent_action->priv;
341 if (GTK_IS_RECENT_CHOOSER (widget) &&
342 !g_slist_find (priv->choosers, widget))
344 g_object_set (G_OBJECT (widget),
345 "show-private", priv->show_private,
346 "show-not-found", priv->show_not_found,
347 "show-tips", priv->show_tips,
348 "show-icons", priv->show_icons,
349 "show-numbers", priv->show_numbers,
350 "limit", priv->limit,
351 "sort-type", priv->sort_type,
352 "filter", priv->current_filter,
353 "local-only", priv->local_only,
358 gtk_recent_chooser_set_sort_func (GTK_RECENT_CHOOSER (widget),
364 g_signal_connect_swapped (widget, "selection_changed",
365 G_CALLBACK (delegate_selection_changed),
367 g_signal_connect_swapped (widget, "item-activated",
368 G_CALLBACK (delegate_item_activated),
372 GTK_ACTION_CLASS (gtk_recent_action_parent_class)->connect_proxy (action, widget);
376 gtk_recent_action_disconnect_proxy (GtkAction *action,
379 GtkRecentAction *recent_action = GTK_RECENT_ACTION (action);
380 GtkRecentActionPrivate *priv = recent_action->priv;
382 /* if it was one of the recent choosers we created, remove it
385 if (g_slist_find (priv->choosers, widget))
386 priv->choosers = g_slist_remove (priv->choosers, widget);
388 GTK_ACTION_CLASS (gtk_recent_action_parent_class)->disconnect_proxy (action, widget);
392 gtk_recent_action_create_menu (GtkAction *action)
394 GtkRecentAction *recent_action = GTK_RECENT_ACTION (action);
395 GtkRecentActionPrivate *priv = recent_action->priv;
398 widget = g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
399 "show-private", priv->show_private,
400 "show-not-found", priv->show_not_found,
401 "show-tips", priv->show_tips,
402 "show-icons", priv->show_icons,
403 "show-numbers", priv->show_numbers,
404 "limit", priv->limit,
405 "sort-type", priv->sort_type,
406 "recent-manager", priv->manager,
407 "filter", priv->current_filter,
408 "local-only", priv->local_only,
413 gtk_recent_chooser_set_sort_func (GTK_RECENT_CHOOSER (widget),
419 g_signal_connect_swapped (widget, "selection_changed",
420 G_CALLBACK (delegate_selection_changed),
422 g_signal_connect_swapped (widget, "item-activated",
423 G_CALLBACK (delegate_item_activated),
426 /* keep track of the choosers we create */
427 priv->choosers = g_slist_prepend (priv->choosers, widget);
433 gtk_recent_action_create_menu_item (GtkAction *action)
438 menu = gtk_recent_action_create_menu (action);
439 menuitem = g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
440 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), menu);
441 gtk_widget_show (menu);
447 gtk_recent_action_create_tool_item (GtkAction *action)
452 menu = gtk_recent_action_create_menu (action);
453 toolitem = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
454 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (toolitem), menu);
455 gtk_widget_show (menu);
461 manager_changed_cb (GtkRecentManager *manager,
464 /* do we need to propagate the signal to all the proxies? guess not */
468 set_recent_manager (GtkRecentAction *action,
469 GtkRecentManager *manager)
471 GtkRecentActionPrivate *priv = action->priv;
475 if (priv->manager_changed_id)
477 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
478 priv->manager_changed_id = 0;
481 priv->manager = NULL;
485 priv->manager = NULL;
487 priv->manager = gtk_recent_manager_get_default ();
490 priv->manager_changed_id = g_signal_connect (priv->manager, "changed",
491 G_CALLBACK (manager_changed_cb),
496 gtk_recent_action_finalize (GObject *gobject)
498 GtkRecentAction *action = GTK_RECENT_ACTION (gobject);
499 GtkRecentActionPrivate *priv = action->priv;
501 priv->manager = NULL;
503 if (priv->data_destroy)
505 priv->data_destroy (priv->sort_data);
506 priv->data_destroy = NULL;
509 priv->sort_data = NULL;
510 priv->sort_func = NULL;
512 g_slist_free (priv->choosers);
514 G_OBJECT_CLASS (gtk_recent_action_parent_class)->finalize (gobject);
518 gtk_recent_action_dispose (GObject *gobject)
520 GtkRecentAction *action = GTK_RECENT_ACTION (gobject);
521 GtkRecentActionPrivate *priv = action->priv;
523 if (priv->manager_changed_id)
526 g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
528 priv->manager_changed_id = 0;
531 if (priv->current_filter)
533 g_object_unref (priv->current_filter);
534 priv->current_filter = NULL;
537 G_OBJECT_CLASS (gtk_recent_action_parent_class)->dispose (gobject);
541 gtk_recent_action_set_property (GObject *gobject,
546 GtkRecentAction *action = GTK_RECENT_ACTION (gobject);
547 GtkRecentActionPrivate *priv = action->priv;
552 case PROP_SHOW_NUMBERS:
553 priv->show_numbers = g_value_get_boolean (value);
555 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
556 priv->show_private = g_value_get_boolean (value);
558 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
559 priv->show_not_found = g_value_get_boolean (value);
561 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
562 priv->show_tips = g_value_get_boolean (value);
564 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
565 priv->show_icons = g_value_get_boolean (value);
567 case GTK_RECENT_CHOOSER_PROP_LIMIT:
568 priv->limit = g_value_get_int (value);
570 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
571 priv->local_only = g_value_get_boolean (value);
573 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
574 priv->sort_type = g_value_get_enum (value);
576 case GTK_RECENT_CHOOSER_PROP_FILTER:
577 /* this already iterates over the choosers list */
578 set_current_filter (action, g_value_get_object (value));
580 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
581 g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
583 G_OBJECT_TYPE_NAME (gobject));
585 case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
586 /* this is a construct-only property; we set the recent-manager
587 * of the choosers with this value when we create them, so there's
588 * no need to iterate later.
590 set_recent_manager (action, g_value_get_object (value));
593 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
597 /* propagate the properties to the proxies we have created */
598 for (l = priv->choosers; l != NULL; l = l->next)
600 GObject *proxy = l->data;
602 g_object_set_property (proxy, pspec->name, value);
607 gtk_recent_action_get_property (GObject *gobject,
612 GtkRecentActionPrivate *priv = GTK_RECENT_ACTION_GET_PRIVATE (gobject);
616 case PROP_SHOW_NUMBERS:
617 g_value_set_boolean (value, priv->show_numbers);
619 case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
620 g_value_set_boolean (value, priv->show_private);
622 case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
623 g_value_set_boolean (value, priv->show_not_found);
625 case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
626 g_value_set_boolean (value, priv->show_tips);
628 case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
629 g_value_set_boolean (value, priv->show_icons);
631 case GTK_RECENT_CHOOSER_PROP_LIMIT:
632 g_value_set_int (value, priv->limit);
634 case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
635 g_value_set_boolean (value, priv->local_only);
637 case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
638 g_value_set_enum (value, priv->sort_type);
640 case GTK_RECENT_CHOOSER_PROP_FILTER:
641 g_value_set_object (value, priv->current_filter);
643 case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
644 g_value_set_boolean (value, FALSE);
647 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
653 gtk_recent_action_class_init (GtkRecentActionClass *klass)
655 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
656 GtkActionClass *action_class = GTK_ACTION_CLASS (klass);
658 g_type_class_add_private (klass, sizeof (GtkRecentActionPrivate));
660 gobject_class->finalize = gtk_recent_action_finalize;
661 gobject_class->dispose = gtk_recent_action_dispose;
662 gobject_class->set_property = gtk_recent_action_set_property;
663 gobject_class->get_property = gtk_recent_action_get_property;
665 action_class->activate = gtk_recent_action_activate;
666 action_class->connect_proxy = gtk_recent_action_connect_proxy;
667 action_class->disconnect_proxy = gtk_recent_action_disconnect_proxy;
668 action_class->create_menu_item = gtk_recent_action_create_menu_item;
669 action_class->create_tool_item = gtk_recent_action_create_tool_item;
670 action_class->create_menu = gtk_recent_action_create_menu;
671 action_class->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
672 action_class->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
674 _gtk_recent_chooser_install_properties (gobject_class);
676 g_object_class_install_property (gobject_class,
678 g_param_spec_boolean ("show-numbers",
680 P_("Whether the items should be displayed with a number"),
687 gtk_recent_action_init (GtkRecentAction *action)
689 GtkRecentActionPrivate *priv;
691 action->priv = priv = GTK_RECENT_ACTION_GET_PRIVATE (action);
693 priv->show_numbers = FALSE;
694 priv->show_icons = TRUE;
695 priv->show_tips = FALSE;
696 priv->show_not_found = TRUE;
697 priv->show_private = FALSE;
698 priv->local_only = TRUE;
700 priv->limit = FALLBACK_ITEM_LIMIT;
702 priv->sort_type = GTK_RECENT_SORT_NONE;
703 priv->sort_func = NULL;
704 priv->sort_data = NULL;
705 priv->data_destroy = NULL;
707 priv->current_filter = NULL;
709 priv->manager = NULL;
710 priv->manager_changed_id = 0;
714 * gtk_recent_action_new:
715 * @name: a unique name for the action
716 * @label: the label displayed in menu items and on buttons, or %NULL
717 * @tooltip: a tooltip for the action, or %NULL
718 * @stock_id: the stock icon to display in widgets representing the
721 * Creates a new #GtkRecentAction object. To add the action to
722 * a #GtkActionGroup and set the accelerator for the action,
723 * call gtk_action_group_add_action_with_accel().
725 * Return value: the newly created #GtkRecentAction.
730 gtk_recent_action_new (const gchar *name,
732 const gchar *tooltip,
733 const gchar *stock_id)
735 g_return_val_if_fail (name != NULL, NULL);
737 return g_object_new (GTK_TYPE_RECENT_ACTION,
741 "stock-id", stock_id,
746 * gtk_recent_action_new_for_manager:
747 * @name: a unique name for the action
748 * @label: the label displayed in menu items and on buttons, or %NULL
749 * @tooltip: a tooltip for the action, or %NULL
750 * @stock_id: the stock icon to display in widgets representing the
752 * @manager: a #GtkRecentManager, or %NULL for using the default
755 * Creates a new #GtkRecentAction object. To add the action to
756 * a #GtkActionGroup and set the accelerator for the action,
757 * call gtk_action_group_add_action_with_accel().
759 * Return value: the newly created #GtkRecentAction
764 gtk_recent_action_new_for_manager (const gchar *name,
766 const gchar *tooltip,
767 const gchar *stock_id,
768 GtkRecentManager *manager)
770 g_return_val_if_fail (name != NULL, NULL);
771 g_return_val_if_fail (manager == NULL || GTK_IS_RECENT_MANAGER (manager), NULL);
773 return g_object_new (GTK_TYPE_RECENT_ACTION,
777 "stock-id", stock_id,
778 "recent-manager", manager,
783 * gtk_recent_action_get_show_numbers:
784 * @action: a #GtkRecentAction
786 * Returns the value set by gtk_recent_chooser_menu_set_show_numbers().
788 * Return value: %TRUE if numbers should be shown.
793 gtk_recent_action_get_show_numbers (GtkRecentAction *action)
795 g_return_val_if_fail (GTK_IS_RECENT_ACTION (action), FALSE);
797 return action->priv->show_numbers;
801 * gtk_recent_action_set_show_numbers:
802 * @action: a #GtkRecentAction
803 * @show_numbers: %TRUE if the shown items should be numbered
805 * Sets whether a number should be added to the items shown by the
806 * widgets representing @action. The numbers are shown to provide
807 * a unique character for a mnemonic to be used inside the menu item's
808 * label. Only the first ten items get a number to avoid clashes.
813 gtk_recent_action_set_show_numbers (GtkRecentAction *action,
814 gboolean show_numbers)
816 GtkRecentActionPrivate *priv;
818 g_return_if_fail (GTK_IS_RECENT_ACTION (action));
822 if (priv->show_numbers != show_numbers)
824 g_object_ref (action);
826 priv->show_numbers = show_numbers;
828 g_object_notify (G_OBJECT (action), "show-numbers");
829 g_object_unref (action);
833 #define __GTK_RECENT_ACTION_C__
834 #include "gtkaliasdef.c"