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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include "gtkrecentchooserdialog.h"
24 #include "gtkrecentchooserwidget.h"
25 #include "gtkrecentchooserutils.h"
26 #include "gtkrecentmanager.h"
27 #include "gtktypebuiltins.h"
33 * SECTION:gtkrecentchooserdialog
34 * @Short_description: Displays recently used files in a dialog
35 * @Title: GtkRecentChooserDialog
36 * @See_also:#GtkRecentChooser, #GtkDialog
38 * #GtkRecentChooserDialog is a dialog box suitable for displaying the recently
39 * used documents. This widgets works by putting a #GtkRecentChooserWidget inside
40 * a #GtkDialog. It exposes the #GtkRecentChooserIface interface, so you can use
41 * all the #GtkRecentChooser functions on the recent chooser dialog as well as
42 * those for #GtkDialog.
44 * Note that #GtkRecentChooserDialog does not have any methods of its own.
45 * Instead, you should use the functions that work on a #GtkRecentChooser.
47 * <example id="gtkrecentchooser-typical-usage">
48 * <title>Typical usage</title>
49 * In the simplest of cases, you can use the following code to use
50 * a #GtkRecentChooserDialog to select a recently used file:
54 * dialog = gtk_recent_chooser_dialog_new ("Recent Documents",
56 * GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
57 * GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
60 * if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
62 * GtkRecentInfo *info;
64 * info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (dialog));
65 * open_file (gtk_recent_info_get_uri (info));
66 * gtk_recent_info_unref (info);
69 * gtk_widget_destroy (dialog);
73 * Recently used files are supported since GTK+ 2.10.
77 struct _GtkRecentChooserDialogPrivate
79 GtkRecentManager *manager;
84 #define GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE(obj) (GTK_RECENT_CHOOSER_DIALOG (obj)->priv)
86 static void gtk_recent_chooser_dialog_class_init (GtkRecentChooserDialogClass *klass);
87 static void gtk_recent_chooser_dialog_init (GtkRecentChooserDialog *dialog);
88 static void gtk_recent_chooser_dialog_finalize (GObject *object);
90 static GObject *gtk_recent_chooser_dialog_constructor (GType type,
91 guint n_construct_properties,
92 GObjectConstructParam *construct_params);
94 static void gtk_recent_chooser_dialog_set_property (GObject *object,
98 static void gtk_recent_chooser_dialog_get_property (GObject *object,
103 static void gtk_recent_chooser_dialog_map (GtkWidget *widget);
104 static void gtk_recent_chooser_dialog_unmap (GtkWidget *widget);
106 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserDialog,
107 gtk_recent_chooser_dialog,
109 G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
110 _gtk_recent_chooser_delegate_iface_init))
113 gtk_recent_chooser_dialog_class_init (GtkRecentChooserDialogClass *klass)
115 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
116 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
118 gobject_class->set_property = gtk_recent_chooser_dialog_set_property;
119 gobject_class->get_property = gtk_recent_chooser_dialog_get_property;
120 gobject_class->constructor = gtk_recent_chooser_dialog_constructor;
121 gobject_class->finalize = gtk_recent_chooser_dialog_finalize;
123 widget_class->map = gtk_recent_chooser_dialog_map;
124 widget_class->unmap = gtk_recent_chooser_dialog_unmap;
126 _gtk_recent_chooser_install_properties (gobject_class);
128 g_type_class_add_private (klass, sizeof (GtkRecentChooserDialogPrivate));
132 gtk_recent_chooser_dialog_init (GtkRecentChooserDialog *dialog)
134 GtkWidget *content_area, *action_area;
136 GtkRecentChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
137 GTK_TYPE_RECENT_CHOOSER_DIALOG,
138 GtkRecentChooserDialogPrivate);
139 GtkDialog *rc_dialog = GTK_DIALOG (dialog);
143 content_area = gtk_dialog_get_content_area (rc_dialog);
144 action_area = gtk_dialog_get_action_area (rc_dialog);
146 gtk_dialog_set_has_separator (rc_dialog, FALSE);
147 gtk_container_set_border_width (GTK_CONTAINER (rc_dialog), 5);
148 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
149 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
153 /* we intercept the GtkRecentChooser::item_activated signal and try to
154 * make the dialog emit a valid response signal
157 gtk_recent_chooser_item_activated_cb (GtkRecentChooser *chooser,
160 GtkDialog *rc_dialog;
161 GtkRecentChooserDialog *dialog;
162 GtkWidget *action_area;
165 dialog = GTK_RECENT_CHOOSER_DIALOG (user_data);
166 rc_dialog = GTK_DIALOG (dialog);
168 if (gtk_window_activate_default (GTK_WINDOW (dialog)))
171 action_area = gtk_dialog_get_action_area (rc_dialog);
172 children = gtk_container_get_children (GTK_CONTAINER (action_area));
174 for (l = children; l; l = l->next)
179 widget = GTK_WIDGET (l->data);
180 response_id = gtk_dialog_get_response_for_widget (rc_dialog, widget);
182 if (response_id == GTK_RESPONSE_ACCEPT ||
183 response_id == GTK_RESPONSE_OK ||
184 response_id == GTK_RESPONSE_YES ||
185 response_id == GTK_RESPONSE_APPLY)
187 g_list_free (children);
189 gtk_dialog_response (GTK_DIALOG (dialog), response_id);
195 g_list_free (children);
199 gtk_recent_chooser_dialog_constructor (GType type,
200 guint n_construct_properties,
201 GObjectConstructParam *construct_params)
203 GtkRecentChooserDialogPrivate *priv;
204 GtkWidget *content_area;
207 object = G_OBJECT_CLASS (gtk_recent_chooser_dialog_parent_class)->constructor (type,
208 n_construct_properties,
210 priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
212 gtk_widget_push_composite_child ();
215 priv->chooser = g_object_new (GTK_TYPE_RECENT_CHOOSER_WIDGET,
216 "recent-manager", priv->manager,
219 priv->chooser = g_object_new (GTK_TYPE_RECENT_CHOOSER_WIDGET, NULL);
221 g_signal_connect (priv->chooser, "item-activated",
222 G_CALLBACK (gtk_recent_chooser_item_activated_cb),
225 content_area = gtk_dialog_get_content_area (GTK_DIALOG (object));
227 gtk_container_set_border_width (GTK_CONTAINER (priv->chooser), 5);
228 gtk_box_pack_start (GTK_BOX (content_area),
229 priv->chooser, TRUE, TRUE, 0);
230 gtk_widget_show (priv->chooser);
232 _gtk_recent_chooser_set_delegate (GTK_RECENT_CHOOSER (object),
233 GTK_RECENT_CHOOSER (priv->chooser));
235 gtk_widget_pop_composite_child ();
241 gtk_recent_chooser_dialog_set_property (GObject *object,
246 GtkRecentChooserDialogPrivate *priv;
248 priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
252 case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
253 priv->manager = g_value_get_object (value);
256 g_object_set_property (G_OBJECT (priv->chooser), pspec->name, value);
262 gtk_recent_chooser_dialog_get_property (GObject *object,
267 GtkRecentChooserDialogPrivate *priv;
269 priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
271 g_object_get_property (G_OBJECT (priv->chooser), pspec->name, value);
275 gtk_recent_chooser_dialog_finalize (GObject *object)
277 GtkRecentChooserDialog *dialog = GTK_RECENT_CHOOSER_DIALOG (object);
279 dialog->priv->manager = NULL;
281 G_OBJECT_CLASS (gtk_recent_chooser_dialog_parent_class)->finalize (object);
285 gtk_recent_chooser_dialog_map (GtkWidget *widget)
287 GtkRecentChooserDialog *dialog = GTK_RECENT_CHOOSER_DIALOG (widget);
288 GtkRecentChooserDialogPrivate *priv = dialog->priv;
290 if (!gtk_widget_get_mapped (priv->chooser))
291 gtk_widget_map (priv->chooser);
293 GTK_WIDGET_CLASS (gtk_recent_chooser_dialog_parent_class)->map (widget);
297 gtk_recent_chooser_dialog_unmap (GtkWidget *widget)
299 GtkRecentChooserDialog *dialog = GTK_RECENT_CHOOSER_DIALOG (widget);
300 GtkRecentChooserDialogPrivate *priv = dialog->priv;
302 GTK_WIDGET_CLASS (gtk_recent_chooser_dialog_parent_class)->unmap (widget);
304 gtk_widget_unmap (priv->chooser);
308 gtk_recent_chooser_dialog_new_valist (const gchar *title,
310 GtkRecentManager *manager,
311 const gchar *first_button_text,
315 const char *button_text = first_button_text;
318 result = g_object_new (GTK_TYPE_RECENT_CHOOSER_DIALOG,
320 "recent-manager", manager,
324 gtk_window_set_transient_for (GTK_WINDOW (result), parent);
328 response_id = va_arg (varargs, gint);
329 gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
330 button_text = va_arg (varargs, const gchar *);
337 * gtk_recent_chooser_dialog_new:
338 * @title: (allow-none): Title of the dialog, or %NULL
339 * @parent: (allow-none): Transient parent of the dialog, or %NULL,
340 * @first_button_text: (allow-none): stock ID or text to go in the first button, or %NULL
341 * @Varargs: response ID for the first button, then additional (button, id)
342 * pairs, ending with %NULL
344 * Creates a new #GtkRecentChooserDialog. This function is analogous to
345 * gtk_dialog_new_with_buttons().
347 * Return value: a new #GtkRecentChooserDialog
352 gtk_recent_chooser_dialog_new (const gchar *title,
354 const gchar *first_button_text,
360 va_start (varargs, first_button_text);
361 result = gtk_recent_chooser_dialog_new_valist (title,
372 * gtk_recent_chooser_dialog_new_for_manager:
373 * @title: (allow-none): Title of the dialog, or %NULL
374 * @parent: (allow-none): Transient parent of the dialog, or %NULL,
375 * @manager: a #GtkRecentManager
376 * @first_button_text: (allow-none): stock ID or text to go in the first button, or %NULL
377 * @Varargs: response ID for the first button, then additional (button, id)
378 * pairs, ending with %NULL
380 * Creates a new #GtkRecentChooserDialog with a specified recent manager.
382 * This is useful if you have implemented your own recent manager, or if you
383 * have a customized instance of a #GtkRecentManager object.
385 * Return value: a new #GtkRecentChooserDialog
390 gtk_recent_chooser_dialog_new_for_manager (const gchar *title,
392 GtkRecentManager *manager,
393 const gchar *first_button_text,
399 va_start (varargs, first_button_text);
400 result = gtk_recent_chooser_dialog_new_valist (title,