1 /* GTK - The GIMP Toolkit
2 * gtkrecentchooserdialog.c: Recent files selector dialog
3 * Copyright (C) 2006 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, see <http://www.gnu.org/licenses/>.
21 #include "gtkrecentchooserdialog.h"
22 #include "gtkrecentchooserwidget.h"
23 #include "gtkrecentchooserutils.h"
24 #include "gtkrecentmanager.h"
25 #include "gtktypebuiltins.h"
31 * SECTION:gtkrecentchooserdialog
32 * @Short_description: Displays recently used files in a dialog
33 * @Title: GtkRecentChooserDialog
34 * @See_also:#GtkRecentChooser, #GtkDialog
36 * #GtkRecentChooserDialog is a dialog box suitable for displaying the recently
37 * used documents. This widgets works by putting a #GtkRecentChooserWidget inside
38 * a #GtkDialog. It exposes the #GtkRecentChooserIface interface, so you can use
39 * all the #GtkRecentChooser functions on the recent chooser dialog as well as
40 * those for #GtkDialog.
42 * Note that #GtkRecentChooserDialog does not have any methods of its own.
43 * Instead, you should use the functions that work on a #GtkRecentChooser.
45 * <example id="gtkrecentchooser-typical-usage">
46 * <title>Typical usage</title>
47 * In the simplest of cases, you can use the following code to use
48 * a #GtkRecentChooserDialog to select a recently used file:
52 * dialog = gtk_recent_chooser_dialog_new ("Recent Documents",
54 * GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
55 * GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
58 * if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
60 * GtkRecentInfo *info;
62 * info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (dialog));
63 * open_file (gtk_recent_info_get_uri (info));
64 * gtk_recent_info_unref (info);
67 * gtk_widget_destroy (dialog);
71 * Recently used files are supported since GTK+ 2.10.
75 struct _GtkRecentChooserDialogPrivate
77 GtkRecentManager *manager;
82 #define GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE(obj) (GTK_RECENT_CHOOSER_DIALOG (obj)->priv)
84 static void gtk_recent_chooser_dialog_class_init (GtkRecentChooserDialogClass *klass);
85 static void gtk_recent_chooser_dialog_init (GtkRecentChooserDialog *dialog);
86 static void gtk_recent_chooser_dialog_finalize (GObject *object);
88 static GObject *gtk_recent_chooser_dialog_constructor (GType type,
89 guint n_construct_properties,
90 GObjectConstructParam *construct_params);
92 static void gtk_recent_chooser_dialog_set_property (GObject *object,
96 static void gtk_recent_chooser_dialog_get_property (GObject *object,
101 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserDialog,
102 gtk_recent_chooser_dialog,
104 G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
105 _gtk_recent_chooser_delegate_iface_init))
108 gtk_recent_chooser_dialog_class_init (GtkRecentChooserDialogClass *klass)
110 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
112 gobject_class->set_property = gtk_recent_chooser_dialog_set_property;
113 gobject_class->get_property = gtk_recent_chooser_dialog_get_property;
114 gobject_class->constructor = gtk_recent_chooser_dialog_constructor;
115 gobject_class->finalize = gtk_recent_chooser_dialog_finalize;
117 _gtk_recent_chooser_install_properties (gobject_class);
119 g_type_class_add_private (klass, sizeof (GtkRecentChooserDialogPrivate));
123 gtk_recent_chooser_dialog_init (GtkRecentChooserDialog *dialog)
125 GtkWidget *content_area, *action_area;
127 GtkRecentChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
128 GTK_TYPE_RECENT_CHOOSER_DIALOG,
129 GtkRecentChooserDialogPrivate);
130 GtkDialog *rc_dialog = GTK_DIALOG (dialog);
134 content_area = gtk_dialog_get_content_area (rc_dialog);
135 action_area = gtk_dialog_get_action_area (rc_dialog);
137 gtk_container_set_border_width (GTK_CONTAINER (rc_dialog), 5);
138 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
139 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
143 /* we intercept the GtkRecentChooser::item_activated signal and try to
144 * make the dialog emit a valid response signal
147 gtk_recent_chooser_item_activated_cb (GtkRecentChooser *chooser,
150 GtkDialog *rc_dialog;
151 GtkRecentChooserDialog *dialog;
152 GtkWidget *action_area;
155 dialog = GTK_RECENT_CHOOSER_DIALOG (user_data);
156 rc_dialog = GTK_DIALOG (dialog);
158 if (gtk_window_activate_default (GTK_WINDOW (dialog)))
161 action_area = gtk_dialog_get_action_area (rc_dialog);
162 children = gtk_container_get_children (GTK_CONTAINER (action_area));
164 for (l = children; l; l = l->next)
169 widget = GTK_WIDGET (l->data);
170 response_id = gtk_dialog_get_response_for_widget (rc_dialog, widget);
172 if (response_id == GTK_RESPONSE_ACCEPT ||
173 response_id == GTK_RESPONSE_OK ||
174 response_id == GTK_RESPONSE_YES ||
175 response_id == GTK_RESPONSE_APPLY)
177 g_list_free (children);
179 gtk_dialog_response (GTK_DIALOG (dialog), response_id);
185 g_list_free (children);
189 gtk_recent_chooser_dialog_constructor (GType type,
190 guint n_construct_properties,
191 GObjectConstructParam *construct_params)
193 GtkRecentChooserDialogPrivate *priv;
194 GtkWidget *content_area;
197 object = G_OBJECT_CLASS (gtk_recent_chooser_dialog_parent_class)->constructor (type,
198 n_construct_properties,
200 priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
202 gtk_widget_push_composite_child ();
205 priv->chooser = g_object_new (GTK_TYPE_RECENT_CHOOSER_WIDGET,
206 "recent-manager", priv->manager,
209 priv->chooser = g_object_new (GTK_TYPE_RECENT_CHOOSER_WIDGET, NULL);
211 g_signal_connect (priv->chooser, "item-activated",
212 G_CALLBACK (gtk_recent_chooser_item_activated_cb),
215 content_area = gtk_dialog_get_content_area (GTK_DIALOG (object));
217 gtk_container_set_border_width (GTK_CONTAINER (priv->chooser), 5);
218 gtk_box_pack_start (GTK_BOX (content_area),
219 priv->chooser, TRUE, TRUE, 0);
220 gtk_widget_show (priv->chooser);
222 _gtk_recent_chooser_set_delegate (GTK_RECENT_CHOOSER (object),
223 GTK_RECENT_CHOOSER (priv->chooser));
225 gtk_widget_pop_composite_child ();
231 gtk_recent_chooser_dialog_set_property (GObject *object,
236 GtkRecentChooserDialogPrivate *priv;
238 priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
242 case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
243 priv->manager = g_value_get_object (value);
246 g_object_set_property (G_OBJECT (priv->chooser), pspec->name, value);
252 gtk_recent_chooser_dialog_get_property (GObject *object,
257 GtkRecentChooserDialogPrivate *priv;
259 priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
261 g_object_get_property (G_OBJECT (priv->chooser), pspec->name, value);
265 gtk_recent_chooser_dialog_finalize (GObject *object)
267 GtkRecentChooserDialog *dialog = GTK_RECENT_CHOOSER_DIALOG (object);
269 dialog->priv->manager = NULL;
271 G_OBJECT_CLASS (gtk_recent_chooser_dialog_parent_class)->finalize (object);
275 gtk_recent_chooser_dialog_new_valist (const gchar *title,
277 GtkRecentManager *manager,
278 const gchar *first_button_text,
282 const char *button_text = first_button_text;
285 result = g_object_new (GTK_TYPE_RECENT_CHOOSER_DIALOG,
287 "recent-manager", manager,
291 gtk_window_set_transient_for (GTK_WINDOW (result), parent);
295 response_id = va_arg (varargs, gint);
296 gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
297 button_text = va_arg (varargs, const gchar *);
304 * gtk_recent_chooser_dialog_new:
305 * @title: (allow-none): Title of the dialog, or %NULL
306 * @parent: (allow-none): Transient parent of the dialog, or %NULL,
307 * @first_button_text: (allow-none): stock ID or text to go in the first button, or %NULL
308 * @...: response ID for the first button, then additional (button, id)
309 * pairs, ending with %NULL
311 * Creates a new #GtkRecentChooserDialog. This function is analogous to
312 * gtk_dialog_new_with_buttons().
314 * Return value: a new #GtkRecentChooserDialog
319 gtk_recent_chooser_dialog_new (const gchar *title,
321 const gchar *first_button_text,
327 va_start (varargs, first_button_text);
328 result = gtk_recent_chooser_dialog_new_valist (title,
339 * gtk_recent_chooser_dialog_new_for_manager:
340 * @title: (allow-none): Title of the dialog, or %NULL
341 * @parent: (allow-none): Transient parent of the dialog, or %NULL,
342 * @manager: a #GtkRecentManager
343 * @first_button_text: (allow-none): stock ID or text to go in the first button, or %NULL
344 * @...: response ID for the first button, then additional (button, id)
345 * pairs, ending with %NULL
347 * Creates a new #GtkRecentChooserDialog with a specified recent manager.
349 * This is useful if you have implemented your own recent manager, or if you
350 * have a customized instance of a #GtkRecentManager object.
352 * Return value: a new #GtkRecentChooserDialog
357 gtk_recent_chooser_dialog_new_for_manager (const gchar *title,
359 GtkRecentManager *manager,
360 const gchar *first_button_text,
366 va_start (varargs, first_button_text);
367 result = gtk_recent_chooser_dialog_new_valist (title,