1 /* GTK - The GIMP Toolkit
2 * gtkrecentchooser.c - Abstract interface for recent file selectors GUIs
4 * Copyright (C) 2006, 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.
24 #include "gtkrecentchooser.h"
25 #include "gtkrecentchooserprivate.h"
26 #include "gtkrecentmanager.h"
28 #include "gtktypebuiltins.h"
29 #include "gtkprivate.h"
30 #include "gtkmarshalers.h"
42 static void gtk_recent_chooser_class_init (gpointer g_iface);
44 static guint chooser_signals[LAST_SIGNAL] = { 0, };
47 gtk_recent_chooser_get_type (void)
49 static GType chooser_type = 0;
53 static const GTypeInfo chooser_info =
55 sizeof (GtkRecentChooserIface),
57 NULL, /* base_finalize */
58 (GClassInitFunc) gtk_recent_chooser_class_init,
61 chooser_type = g_type_register_static (G_TYPE_INTERFACE,
62 I_("GtkRecentChooser"),
65 g_type_interface_add_prerequisite (chooser_type, GTK_TYPE_OBJECT);
72 gtk_recent_chooser_class_init (gpointer g_iface)
74 GType iface_type = G_TYPE_FROM_INTERFACE (g_iface);
77 * GtkRecentChooser::selection-changed
78 * @chooser: the object which received the signal
80 * This signal is emitted when there is a change in the set of
81 * selected recently used resources. This can happen when a user
82 * modifies the selection with the mouse or the keyboard, or when
83 * explicitely calling functions to change the selection.
87 chooser_signals[SELECTION_CHANGED] =
88 g_signal_new (I_("selection-changed"),
91 G_STRUCT_OFFSET (GtkRecentChooserIface, selection_changed),
93 g_cclosure_marshal_VOID__VOID,
97 * GtkRecentChooser::item-activated
98 * @chooser: the object which received the signal
100 * This signal is emitted when the user "activates" a recent item
101 * in the recent chooser. This can happen by double-clicking on an item
102 * in the recently used resources list, or by pressing
103 * <keycap>Enter</keycap>.
107 chooser_signals[ITEM_ACTIVATED] =
108 g_signal_new (I_("item-activated"),
111 G_STRUCT_OFFSET (GtkRecentChooserIface, item_activated),
113 g_cclosure_marshal_VOID__VOID,
116 g_object_interface_install_property (g_iface,
117 g_param_spec_object ("recent-manager",
118 P_("Recent Manager"),
119 P_("The RecentManager object to use"),
120 GTK_TYPE_RECENT_MANAGER,
121 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
122 g_object_interface_install_property (g_iface,
123 g_param_spec_boolean ("show-private",
125 P_("Whether the private items should be displayed"),
128 g_object_interface_install_property (g_iface,
129 g_param_spec_boolean ("show-tips",
131 P_("Whether there should be a tooltip on the item"),
134 g_object_interface_install_property (g_iface,
135 g_param_spec_boolean ("show-icons",
137 P_("Whether there should be an icon near the item"),
140 g_object_interface_install_property (g_iface,
141 g_param_spec_boolean ("show-not-found",
142 P_("Show Not Found"),
143 P_("Whether the items pointing to unavailable resources should be displayed"),
146 g_object_interface_install_property (g_iface,
147 g_param_spec_boolean ("select-multiple",
148 P_("Select Multiple"),
149 P_("Whether to allow multiple items to be selected"),
152 g_object_interface_install_property (g_iface,
153 g_param_spec_boolean ("local-only",
155 P_("Whether the selected resource(s) should be limited to local file: URIs"),
158 g_object_interface_install_property (g_iface,
159 g_param_spec_int ("limit",
161 P_("The maximum number of items to be displayed"),
166 g_object_interface_install_property (g_iface,
167 g_param_spec_enum ("sort-type",
169 P_("The sorting order of the items displayed"),
170 GTK_TYPE_RECENT_SORT_TYPE,
171 GTK_RECENT_SORT_NONE,
173 g_object_interface_install_property (g_iface,
174 g_param_spec_object ("filter",
176 P_("The current filter for selecting which resources are displayed"),
177 GTK_TYPE_RECENT_FILTER,
182 gtk_recent_chooser_error_quark (void)
184 return g_quark_from_static_string ("gtk-recent-chooser-error-quark");
188 * _gtk_recent_chooser_get_recent_manager:
189 * @chooser: a #GtkRecentChooser
191 * Gets the #GtkRecentManager used by @chooser.
193 * Return value: the recent manager for @chooser.
198 _gtk_recent_chooser_get_recent_manager (GtkRecentChooser *chooser)
200 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), NULL);
202 return GTK_RECENT_CHOOSER_GET_IFACE (chooser)->get_recent_manager (chooser);
206 * gtk_recent_chooser_set_show_private:
207 * @chooser: a #GtkRecentChooser
208 * @show_private: %TRUE to show private items, %FALSE otherwise
210 * Whether to show recently used resources marked registered as private.
215 gtk_recent_chooser_set_show_private (GtkRecentChooser *chooser,
216 gboolean show_private)
218 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
220 g_object_set (chooser, "show-private", show_private, NULL);
224 * gtk_recent_chooser_get_show_private:
225 * @chooser: a #GtkRecentChooser
227 * Returns whether @chooser should display recently used resources
228 * registered as private.
230 * Return value: %TRUE if the recent chooser should show private items,
236 gtk_recent_chooser_get_show_private (GtkRecentChooser *chooser)
238 gboolean show_private;
240 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
242 g_object_get (chooser, "show-private", &show_private, NULL);
248 * gtk_recent_chooser_set_show_not_found:
249 * @chooser: a #GtkRecentChooser
250 * @show_not_found: whether to show the local items we didn't find
252 * Sets whether @chooser should display the recently used resources that
253 * it didn't find. This only applies to local resources.
258 gtk_recent_chooser_set_show_not_found (GtkRecentChooser *chooser,
259 gboolean show_not_found)
261 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
263 g_object_set (chooser, "show-not-found", show_not_found, NULL);
267 * gtk_recent_chooser_get_show_not_found:
268 * @chooser: a #GtkRecentChooser
270 * Retrieves whether @chooser should show the recently used resources that
273 * Return value: %TRUE if the resources not found should be displayed, and
279 gtk_recent_chooser_get_show_not_found (GtkRecentChooser *chooser)
281 gboolean show_not_found;
283 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
285 g_object_get (chooser, "show-not-found", &show_not_found, NULL);
287 return show_not_found;
291 * gtk_recent_chooser_set_show_icons:
292 * @chooser: a #GtkRecentChooser
293 * @show_icons: whether to show an icon near the resource
295 * Sets whether @chooser should show an icon near the resource when
301 gtk_recent_chooser_set_show_icons (GtkRecentChooser *chooser,
304 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
306 g_object_set (chooser, "show-icons", show_icons, NULL);
310 * gtk_recent_chooser_get_show_icons:
311 * @chooser: a #GtkRecentChooser
313 * Retrieves whether @chooser should show an icon near the resource.
315 * Return value: %TRUE if the icons should be displayed, %FALSE otherwise.
320 gtk_recent_chooser_get_show_icons (GtkRecentChooser *chooser)
324 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
326 g_object_get (chooser, "show-icons", &show_icons, NULL);
332 * gtk_recent_chooser_set_select_multiple:
333 * @chooser: a #GtkRecentChooser
334 * @select_multiple: %TRUE if @chooser can select more than one item
336 * Sets whether @chooser can select multiple items.
341 gtk_recent_chooser_set_select_multiple (GtkRecentChooser *chooser,
342 gboolean select_multiple)
344 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
346 g_object_set (chooser, "select-multiple", select_multiple, NULL);
350 * gtk_recent_chooser_get_select_multiple:
351 * @chooser: a #GtkRecentChooser
353 * Gets whether @chooser can select multiple items.
355 * Return value: %TRUE if @chooser can select more than one item.
360 gtk_recent_chooser_get_select_multiple (GtkRecentChooser *chooser)
362 gboolean select_multiple;
364 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
366 g_object_get (chooser, "select-multiple", &select_multiple, NULL);
368 return select_multiple;
372 * gtk_recent_chooser_set_local_only:
373 * @chooser: a #GtkRecentChooser
374 * @local_only: %TRUE if only local files can be shown
376 * Sets whether only local resources, that is resources using the file:// URI
377 * scheme, should be shown in the recently used resources selector. If
378 * @local_only is %TRUE (the default) then the shown resources are guaranteed
379 * to be accessible through the operating system native file system.
384 gtk_recent_chooser_set_local_only (GtkRecentChooser *chooser,
387 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
389 g_object_set (chooser, "local-only", local_only, NULL);
393 * gtk_recent_chooser_get_local_only:
394 * @chooser: a #GtkRecentChooser
396 * Gets whether only local resources should be shown in the recently used
397 * resources selector. See gtk_recent_chooser_set_local_only()
399 * Return value: %TRUE if only local resources should be shown.
404 gtk_recent_chooser_get_local_only (GtkRecentChooser *chooser)
408 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
410 g_object_get (chooser, "local-only", &local_only, NULL);
416 * gtk_recent_chooser_set_limit:
417 * @chooser: a #GtkRecentChooser
418 * @limit: a positive integer, or -1 for all items
420 * Sets the number of items that should be returned by
421 * gtk_recent_chooser_get_items() and gtk_recent_chooser_get_uris().
426 gtk_recent_chooser_set_limit (GtkRecentChooser *chooser,
429 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
431 g_object_set (chooser, "limit", limit, NULL);
435 * gtk_recent_chooser_get_limit:
436 * @chooser: a #GtkRecentChooser
438 * Gets the number of items returned by gtk_recent_chooser_get_items()
439 * and gtk_recent_chooser_get_uris().
441 * Return value: A positive integer, or -1 meaning that all items are
447 gtk_recent_chooser_get_limit (GtkRecentChooser *chooser)
451 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), 10);
453 g_object_get (chooser, "limit", &limit, NULL);
459 * gtk_recent_chooser_set_show_tips:
460 * @chooser: a #GtkRecentChooser
461 * @show_tips: %TRUE if tooltips should be shown
463 * Sets whether to show a tooltips on the widget.
468 gtk_recent_chooser_set_show_tips (GtkRecentChooser *chooser,
471 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
473 g_object_set (chooser, "show-tips", show_tips, NULL);
477 * gtk_recent_chooser_get_show_tips:
478 * @chooser: a #GtkRecentChooser
480 * Gets whether @chooser should display tooltips.
482 * Return value: %TRUE if the recent chooser should show tooltips,
488 gtk_recent_chooser_get_show_tips (GtkRecentChooser *chooser)
492 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
494 g_object_get (chooser, "show-tips", &show_tips, NULL);
500 * gtk_recent_chooser_set_show_numbers:
501 * @chooser: a #GtkRecentChooser
502 * @show_numbers: %TRUE to show numbers, %FALSE otherwise
504 * Whether to show recently used resources prepended by a unique number.
509 gtk_recent_chooser_set_show_numbers (GtkRecentChooser *chooser,
510 gboolean show_numbers)
512 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
514 g_object_set (chooser, "show-numbers", show_numbers, NULL);
518 * gtk_recent_chooser_get_show_numbers:
519 * @chooser: a #GtkRecentChooser
521 * Returns whether @chooser should display recently used resources
522 * prepended by a unique number.
524 * Return value: %TRUE if the recent chooser should show display numbers,
530 gtk_recent_chooser_get_show_numbers (GtkRecentChooser *chooser)
532 gboolean show_numbers;
534 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
536 g_object_get (chooser, "show-numbers", &show_numbers, NULL);
543 * gtk_recent_chooser_set_sort_type:
544 * @chooser: a #GtkRecentChooser
545 * @sort_type: sort order that the chooser should use
547 * Changes the sorting order of the recently used resources list displayed by
553 gtk_recent_chooser_set_sort_type (GtkRecentChooser *chooser,
554 GtkRecentSortType sort_type)
556 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
558 g_object_set (chooser, "sort-type", sort_type, NULL);
562 * gtk_recent_chooser_get_sort_type:
563 * @chooser: a #GtkRecentChooser
565 * Gets the value set by gtk_recent_chooser_set_sort_type().
567 * Return value: the sorting order of the @chooser.
572 gtk_recent_chooser_get_sort_type (GtkRecentChooser *chooser)
574 GtkRecentSortType sort_type;
576 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), GTK_RECENT_SORT_NONE);
578 g_object_get (chooser, "sort-type", &sort_type, NULL);
584 * gtk_recent_chooser_set_sort_func:
585 * @chooser: a #GtkRecentChooser
586 * @sort_func: the comparison function
587 * @sort_data: user data to pass to @sort_func, or %NULL
588 * @data_destroy: destroy notifier for @sort_data, or %NULL
590 * Sets the comparison function used when sorting to be @sort_func. If
591 * the @chooser has the sort type set to #GTK_RECENT_SORT_CUSTOM then
592 * the chooser will sort using this function.
594 * To the comparison function will be passed two #GtkRecentInfo structs and
595 * @sort_data; @sort_func should return a positive integer if the first
596 * item comes before the second, zero if the two items are equal and
597 * a negative integer if the first item comes after the second.
602 gtk_recent_chooser_set_sort_func (GtkRecentChooser *chooser,
603 GtkRecentSortFunc sort_func,
605 GDestroyNotify data_destroy)
607 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
609 GTK_RECENT_CHOOSER_GET_IFACE (chooser)->set_sort_func (chooser,
616 * gtk_recent_chooser_set_current_uri:
617 * @chooser: a #GtkRecentChooser
619 * @error: return location for a #GError, or %NULL
621 * Sets @uri as the current URI for @chooser.
623 * Return value: %TRUE if the URI was found.
628 gtk_recent_chooser_set_current_uri (GtkRecentChooser *chooser,
632 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
634 return GTK_RECENT_CHOOSER_GET_IFACE (chooser)->set_current_uri (chooser, uri, error);
638 * gtk_recent_chooser_get_current_uri:
639 * @chooser: a #GtkRecentChooser
641 * Gets the URI currently selected by @chooser.
643 * Return value: a newly allocated string holding a URI.
648 gtk_recent_chooser_get_current_uri (GtkRecentChooser *chooser)
650 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), NULL);
652 return GTK_RECENT_CHOOSER_GET_IFACE (chooser)->get_current_uri (chooser);
656 * gtk_recent_chooser_get_current_item:
657 * @chooser: a #GtkRecentChooser
659 * Gets the #GtkRecentInfo currently selected by @chooser.
661 * Return value: a #GtkRecentInfo. Use gtk_recent_info_unref() when
662 * when you have finished using it.
667 gtk_recent_chooser_get_current_item (GtkRecentChooser *chooser)
669 GtkRecentManager *manager;
670 GtkRecentInfo *retval;
673 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), NULL);
675 uri = gtk_recent_chooser_get_current_uri (chooser);
679 manager = _gtk_recent_chooser_get_recent_manager (chooser);
680 retval = gtk_recent_manager_lookup_item (manager, uri, NULL);
687 * gtk_recent_chooser_select_uri:
688 * @chooser: a #GtkRecentChooser
690 * @error: return location for a #GError, or %NULL
692 * Selects @uri inside @chooser.
694 * Return value: %TRUE if @uri was found.
699 gtk_recent_chooser_select_uri (GtkRecentChooser *chooser,
703 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
705 return GTK_RECENT_CHOOSER_GET_IFACE (chooser)->select_uri (chooser, uri, error);
709 * gtk_recent_chooser_unselect_uri:
710 * @chooser: a #GtkRecentChooser
713 * Unselects @uri inside @chooser.
718 gtk_recent_chooser_unselect_uri (GtkRecentChooser *chooser,
721 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
723 GTK_RECENT_CHOOSER_GET_IFACE (chooser)->unselect_uri (chooser, uri);
727 * gtk_recent_chooser_select_all:
728 * @chooser: a #GtkRecentChooser
730 * Selects all the items inside @chooser, if the @chooser supports
731 * multiple selection.
736 gtk_recent_chooser_select_all (GtkRecentChooser *chooser)
738 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
740 GTK_RECENT_CHOOSER_GET_IFACE (chooser)->select_all (chooser);
744 * gtk_recent_chooser_unselect_all:
745 * @chooser: a #GtkRecentChooser
747 * Unselects all the items inside @chooser.
752 gtk_recent_chooser_unselect_all (GtkRecentChooser *chooser)
754 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
756 GTK_RECENT_CHOOSER_GET_IFACE (chooser)->unselect_all (chooser);
760 * gtk_recent_chooser_get_items:
761 * @chooser: a #GtkRecentChooser
763 * Gets the list of recently used resources in form of #GtkRecentInfo objects.
765 * The return value of this function is affected by the "sort-type" and
766 * "limit" properties of @chooser.
768 * Return value: A newly allocated list of #GtkRecentInfo objects. You should
769 * use gtk_recent_info_unref() on every item of the list, and then free
770 * the list itself using g_list_free().
775 gtk_recent_chooser_get_items (GtkRecentChooser *chooser)
777 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), NULL);
779 return GTK_RECENT_CHOOSER_GET_IFACE (chooser)->get_items (chooser);
783 * gtk_recent_chooser_get_uris:
784 * @chooser: a #GtkRecentChooser
785 * @length: return location for a the length of the URI list, or %NULL
787 * Gets the URI of the recently used resources.
789 * The return value of this function is affected by the "sort-type" and "limit"
790 * properties of @chooser.
792 * Since the returned array is %NULL terminated, @length may be %NULL.
794 * Return value: A newly allocated, %NULL terminated array of strings. Use
795 * g_strfreev() to free it.
800 gtk_recent_chooser_get_uris (GtkRecentChooser *chooser,
807 items = gtk_recent_chooser_get_items (chooser);
811 n_items = g_list_length (items);
812 retval = g_new0 (gchar *, n_items + 1);
814 for (l = items, i = 0; l != NULL; l = l->next)
816 GtkRecentInfo *info = (GtkRecentInfo *) l->data;
819 g_assert (info != NULL);
821 uri = gtk_recent_info_get_uri (info);
822 g_assert (uri != NULL);
824 retval[i++] = g_strdup (uri);
831 g_list_foreach (items,
832 (GFunc) gtk_recent_info_unref,
840 * gtk_recent_chooser_add_filter:
841 * @chooser: a #GtkRecentChooser
842 * @filter: a #GtkRecentFilter
844 * Adds @filter to the list of #GtkRecentFilter objects held by @chooser.
846 * If no previous filter objects were defined, this function will call
847 * gtk_recent_chooser_set_filter().
852 gtk_recent_chooser_add_filter (GtkRecentChooser *chooser,
853 GtkRecentFilter *filter)
855 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
857 GTK_RECENT_CHOOSER_GET_IFACE (chooser)->add_filter (chooser, filter);
861 * gtk_recent_chooser_remove_filter:
862 * @chooser: a #GtkRecentChooser
863 * @filter: a #GtkRecentFilter
865 * Removes @filter from the list of #GtkRecentFilter objects held by @chooser.
870 gtk_recent_chooser_remove_filter (GtkRecentChooser *chooser,
871 GtkRecentFilter *filter)
873 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
875 GTK_RECENT_CHOOSER_GET_IFACE (chooser)->remove_filter (chooser, filter);
879 * gtk_recent_chooser_list_filters:
880 * @chooser: a #GtkRecentChooser
882 * Gets the #GtkRecentFilter objects held by @chooser.
884 * Return value: A singly linked list of #GtkRecentFilter objects. You
885 * should just free the returned list using g_slist_free().
890 gtk_recent_chooser_list_filters (GtkRecentChooser *chooser)
892 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), NULL);
894 return GTK_RECENT_CHOOSER_GET_IFACE (chooser)->list_filters (chooser);
898 * gtk_recent_chooser_set_filter:
899 * @chooser: a #GtkRecentChooser
900 * @filter: a #GtkRecentFilter
902 * Sets @filter as the current #GtkRecentFilter object used by @chooser
903 * to affect the displayed recently used resources.
908 gtk_recent_chooser_set_filter (GtkRecentChooser *chooser,
909 GtkRecentFilter *filter)
911 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
913 g_object_set (G_OBJECT (chooser), "filter", filter, NULL);
917 * gtk_recent_chooser_get_filter:
918 * @chooser: a #GtkRecentChooser
920 * Gets the #GtkRecentFilter object currently used by @chooser to affect
921 * the display of the recently used resources.
923 * Return value: a #GtkRecentFilter object.
928 gtk_recent_chooser_get_filter (GtkRecentChooser *chooser)
930 GtkRecentFilter *filter;
932 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), NULL);
934 g_object_get (G_OBJECT (chooser), "filter", &filter, NULL);
936 /* we need this hack because g_object_get() increases the refcount
937 * of the returned object; see also gtk_file_chooser_get_filter()
938 * inside gtkfilechooser.c
941 g_object_unref (filter);
947 _gtk_recent_chooser_item_activated (GtkRecentChooser *chooser)
949 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
951 g_signal_emit (chooser, chooser_signals[ITEM_ACTIVATED], 0);
955 _gtk_recent_chooser_selection_changed (GtkRecentChooser *chooser)
957 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
959 g_signal_emit (chooser, chooser_signals[SELECTION_CHANGED], 0);
962 #define __GTK_RECENT_CHOOSER_C__
963 #include "gtkaliasdef.c"