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"
32 struct _GtkRecentChooserDialogPrivate
34 GtkRecentManager *manager;
39 #define GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE(obj) (GTK_RECENT_CHOOSER_DIALOG (obj)->priv)
41 static void gtk_recent_chooser_dialog_class_init (GtkRecentChooserDialogClass *klass);
42 static void gtk_recent_chooser_dialog_init (GtkRecentChooserDialog *dialog);
43 static void gtk_recent_chooser_dialog_finalize (GObject *object);
45 static GObject *gtk_recent_chooser_dialog_constructor (GType type,
46 guint n_construct_properties,
47 GObjectConstructParam *construct_params);
49 static void gtk_recent_chooser_dialog_set_property (GObject *object,
53 static void gtk_recent_chooser_dialog_get_property (GObject *object,
58 static void gtk_recent_chooser_dialog_map (GtkWidget *widget);
59 static void gtk_recent_chooser_dialog_unmap (GtkWidget *widget);
61 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserDialog,
62 gtk_recent_chooser_dialog,
64 G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
65 _gtk_recent_chooser_delegate_iface_init))
68 gtk_recent_chooser_dialog_class_init (GtkRecentChooserDialogClass *klass)
70 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
71 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
73 gobject_class->set_property = gtk_recent_chooser_dialog_set_property;
74 gobject_class->get_property = gtk_recent_chooser_dialog_get_property;
75 gobject_class->constructor = gtk_recent_chooser_dialog_constructor;
76 gobject_class->finalize = gtk_recent_chooser_dialog_finalize;
78 widget_class->map = gtk_recent_chooser_dialog_map;
79 widget_class->unmap = gtk_recent_chooser_dialog_unmap;
81 _gtk_recent_chooser_install_properties (gobject_class);
83 g_type_class_add_private (klass, sizeof (GtkRecentChooserDialogPrivate));
87 gtk_recent_chooser_dialog_init (GtkRecentChooserDialog *dialog)
89 GtkRecentChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
90 GTK_TYPE_RECENT_CHOOSER_DIALOG,
91 GtkRecentChooserDialogPrivate);
92 GtkDialog *rc_dialog = GTK_DIALOG (dialog);
96 gtk_dialog_set_has_separator (rc_dialog, FALSE);
97 gtk_container_set_border_width (GTK_CONTAINER (rc_dialog), 5);
98 gtk_box_set_spacing (GTK_BOX (rc_dialog->vbox), 2); /* 2 * 5 + 2 = 12 */
99 gtk_container_set_border_width (GTK_CONTAINER (rc_dialog->action_area), 5);
103 /* we intercept the GtkRecentChooser::item_activated signal and try to
104 * make the dialog emit a valid response signal
107 gtk_recent_chooser_item_activated_cb (GtkRecentChooser *chooser,
110 GtkRecentChooserDialog *dialog;
113 dialog = GTK_RECENT_CHOOSER_DIALOG (user_data);
115 if (gtk_window_activate_default (GTK_WINDOW (dialog)))
118 children = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area));
120 for (l = children; l; l = l->next)
125 widget = GTK_WIDGET (l->data);
126 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
128 if (response_id == GTK_RESPONSE_ACCEPT ||
129 response_id == GTK_RESPONSE_OK ||
130 response_id == GTK_RESPONSE_YES ||
131 response_id == GTK_RESPONSE_APPLY)
133 g_list_free (children);
135 gtk_dialog_response (GTK_DIALOG (dialog), response_id);
141 g_list_free (children);
145 gtk_recent_chooser_dialog_constructor (GType type,
146 guint n_construct_properties,
147 GObjectConstructParam *construct_params)
150 GtkRecentChooserDialogPrivate *priv;
152 object = G_OBJECT_CLASS (gtk_recent_chooser_dialog_parent_class)->constructor (type,
153 n_construct_properties,
155 priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
157 gtk_widget_push_composite_child ();
160 priv->chooser = g_object_new (GTK_TYPE_RECENT_CHOOSER_WIDGET,
161 "recent-manager", priv->manager,
164 priv->chooser = g_object_new (GTK_TYPE_RECENT_CHOOSER_WIDGET, NULL);
166 g_signal_connect (priv->chooser, "item-activated",
167 G_CALLBACK (gtk_recent_chooser_item_activated_cb),
170 gtk_container_set_border_width (GTK_CONTAINER (priv->chooser), 5);
171 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (object)->vbox),
172 priv->chooser, TRUE, TRUE, 0);
173 gtk_widget_show (priv->chooser);
175 _gtk_recent_chooser_set_delegate (GTK_RECENT_CHOOSER (object),
176 GTK_RECENT_CHOOSER (priv->chooser));
178 gtk_widget_pop_composite_child ();
184 gtk_recent_chooser_dialog_set_property (GObject *object,
189 GtkRecentChooserDialogPrivate *priv;
191 priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
195 case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
196 priv->manager = g_value_get_object (value);
199 g_object_set_property (G_OBJECT (priv->chooser), pspec->name, value);
205 gtk_recent_chooser_dialog_get_property (GObject *object,
210 GtkRecentChooserDialogPrivate *priv;
212 priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
214 g_object_get_property (G_OBJECT (priv->chooser), pspec->name, value);
218 gtk_recent_chooser_dialog_finalize (GObject *object)
220 GtkRecentChooserDialog *dialog = GTK_RECENT_CHOOSER_DIALOG (object);
222 dialog->priv->manager = NULL;
224 G_OBJECT_CLASS (gtk_recent_chooser_dialog_parent_class)->finalize (object);
228 gtk_recent_chooser_dialog_map (GtkWidget *widget)
230 GtkRecentChooserDialog *dialog = GTK_RECENT_CHOOSER_DIALOG (widget);
231 GtkRecentChooserDialogPrivate *priv = dialog->priv;
233 if (!gtk_widget_get_mapped (priv->chooser))
234 gtk_widget_map (priv->chooser);
236 GTK_WIDGET_CLASS (gtk_recent_chooser_dialog_parent_class)->map (widget);
240 gtk_recent_chooser_dialog_unmap (GtkWidget *widget)
242 GtkRecentChooserDialog *dialog = GTK_RECENT_CHOOSER_DIALOG (widget);
243 GtkRecentChooserDialogPrivate *priv = dialog->priv;
245 GTK_WIDGET_CLASS (gtk_recent_chooser_dialog_parent_class)->unmap (widget);
247 gtk_widget_unmap (priv->chooser);
251 gtk_recent_chooser_dialog_new_valist (const gchar *title,
253 GtkRecentManager *manager,
254 const gchar *first_button_text,
258 const char *button_text = first_button_text;
261 result = g_object_new (GTK_TYPE_RECENT_CHOOSER_DIALOG,
263 "recent-manager", manager,
267 gtk_window_set_transient_for (GTK_WINDOW (result), parent);
271 response_id = va_arg (varargs, gint);
272 gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
273 button_text = va_arg (varargs, const gchar *);
280 * gtk_recent_chooser_dialog_new:
281 * @title: (allow-none): Title of the dialog, or %NULL
282 * @parent: (allow-none): Transient parent of the dialog, or %NULL,
283 * @first_button_text: (allow-none): stock ID or text to go in the first button, or %NULL
284 * @Varargs: response ID for the first button, then additional (button, id)
285 * pairs, ending with %NULL
287 * Creates a new #GtkRecentChooserDialog. This function is analogous to
288 * gtk_dialog_new_with_buttons().
290 * Return value: a new #GtkRecentChooserDialog
295 gtk_recent_chooser_dialog_new (const gchar *title,
297 const gchar *first_button_text,
303 va_start (varargs, first_button_text);
304 result = gtk_recent_chooser_dialog_new_valist (title,
315 * gtk_recent_chooser_dialog_new_for_manager:
316 * @title: (allow-none): Title of the dialog, or %NULL
317 * @parent: (allow-none): Transient parent of the dialog, or %NULL,
318 * @manager: a #GtkRecentManager
319 * @first_button_text: (allow-none): stock ID or text to go in the first button, or %NULL
320 * @Varargs: response ID for the first button, then additional (button, id)
321 * pairs, ending with %NULL
323 * Creates a new #GtkRecentChooserDialog with a specified recent manager.
325 * This is useful if you have implemented your own recent manager, or if you
326 * have a customized instance of a #GtkRecentManager object.
328 * Return value: a new #GtkRecentChooserDialog
333 gtk_recent_chooser_dialog_new_for_manager (const gchar *title,
335 GtkRecentManager *manager,
336 const gchar *first_button_text,
342 va_start (varargs, first_button_text);
343 result = gtk_recent_chooser_dialog_new_valist (title,
353 #define __GTK_RECENT_CHOOSER_DIALOG_C__
354 #include "gtkaliasdef.c"