]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentchooserdialog.c
5ee55e277bfdab502d5075d0f9dbb9a747155375
[~andy/gtk] / gtk / gtkrecentchooserdialog.c
1 /* GTK - The GIMP Toolkit
2  * gtkrecentchooserdialog.c: Recent files selector dialog
3  * Copyright (C) 2006 Emmanuele Bassi
4  * 
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.
9  *
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.
14  *
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.
19  */
20
21 #include "config.h"
22
23 #include "gtkrecentchooserdialog.h"
24 #include "gtkrecentchooserwidget.h"
25 #include "gtkrecentchooserutils.h"
26 #include "gtkrecentmanager.h"
27 #include "gtktypebuiltins.h"
28
29 #include <stdarg.h>
30
31
32 /**
33  * SECTION:gtkrecentchooserdialog
34  * @Short_description: Displays recently used files in a dialog
35  * @Title: GtkRecentChooserDialog
36  * @See_also:#GtkRecentChooser, #GtkDialog
37  *
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.
43  *
44  * Note that #GtkRecentChooserDialog does not have any methods of its own.
45  * Instead, you should use the functions that work on a #GtkRecentChooser.
46  *
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:
51  * <programlisting>
52  * GtkWidget *dialog;
53  *
54  * dialog = gtk_recent_chooser_dialog_new ("Recent Documents",
55  *                                         parent_window,
56  *                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
57  *                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
58  *                                         NULL);
59  *
60  * if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
61  *   {
62  *     GtkRecentInfo *info;
63  *
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);
67  *   }
68  *
69  * gtk_widget_destroy (dialog);
70  * </programlisting>
71  * </example>
72  *
73  * Recently used files are supported since GTK+ 2.10.
74  */
75
76
77 struct _GtkRecentChooserDialogPrivate
78 {
79   GtkRecentManager *manager;
80   
81   GtkWidget *chooser;
82 };
83
84 #define GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE(obj)      (GTK_RECENT_CHOOSER_DIALOG (obj)->priv)
85
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);
89
90 static GObject *gtk_recent_chooser_dialog_constructor (GType                  type,
91                                                        guint                  n_construct_properties,
92                                                        GObjectConstructParam *construct_params);
93
94 static void gtk_recent_chooser_dialog_set_property (GObject      *object,
95                                                     guint         prop_id,
96                                                     const GValue *value,
97                                                     GParamSpec   *pspec);
98 static void gtk_recent_chooser_dialog_get_property (GObject      *object,
99                                                     guint         prop_id,
100                                                     GValue       *value,
101                                                     GParamSpec   *pspec);
102
103 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserDialog,
104                          gtk_recent_chooser_dialog,
105                          GTK_TYPE_DIALOG,
106                          G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
107                                                 _gtk_recent_chooser_delegate_iface_init))
108
109 static void
110 gtk_recent_chooser_dialog_class_init (GtkRecentChooserDialogClass *klass)
111 {
112   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
113   
114   gobject_class->set_property = gtk_recent_chooser_dialog_set_property;
115   gobject_class->get_property = gtk_recent_chooser_dialog_get_property;
116   gobject_class->constructor = gtk_recent_chooser_dialog_constructor;
117   gobject_class->finalize = gtk_recent_chooser_dialog_finalize;
118   
119   _gtk_recent_chooser_install_properties (gobject_class);
120   
121   g_type_class_add_private (klass, sizeof (GtkRecentChooserDialogPrivate));
122 }
123
124 static void
125 gtk_recent_chooser_dialog_init (GtkRecentChooserDialog *dialog)
126 {
127   GtkWidget *content_area, *action_area;
128
129   GtkRecentChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
130                                                                      GTK_TYPE_RECENT_CHOOSER_DIALOG,
131                                                                      GtkRecentChooserDialogPrivate);
132   GtkDialog *rc_dialog = GTK_DIALOG (dialog);
133   
134   dialog->priv = priv;
135
136   content_area = gtk_dialog_get_content_area (rc_dialog);
137   action_area = gtk_dialog_get_action_area (rc_dialog);
138
139   gtk_container_set_border_width (GTK_CONTAINER (rc_dialog), 5);
140   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
141   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
142
143 }
144
145 /* we intercept the GtkRecentChooser::item_activated signal and try to
146  * make the dialog emit a valid response signal
147  */
148 static void
149 gtk_recent_chooser_item_activated_cb (GtkRecentChooser *chooser,
150                                       gpointer          user_data)
151 {
152   GtkDialog *rc_dialog;
153   GtkRecentChooserDialog *dialog;
154   GtkWidget *action_area;
155   GList *children, *l;
156
157   dialog = GTK_RECENT_CHOOSER_DIALOG (user_data);
158   rc_dialog = GTK_DIALOG (dialog);
159
160   if (gtk_window_activate_default (GTK_WINDOW (dialog)))
161     return;
162
163   action_area = gtk_dialog_get_action_area (rc_dialog);
164   children = gtk_container_get_children (GTK_CONTAINER (action_area));
165   
166   for (l = children; l; l = l->next)
167     {
168       GtkWidget *widget;
169       gint response_id;
170       
171       widget = GTK_WIDGET (l->data);
172       response_id = gtk_dialog_get_response_for_widget (rc_dialog, widget);
173       
174       if (response_id == GTK_RESPONSE_ACCEPT ||
175           response_id == GTK_RESPONSE_OK     ||
176           response_id == GTK_RESPONSE_YES    ||
177           response_id == GTK_RESPONSE_APPLY)
178         {
179           g_list_free (children);
180           
181           gtk_dialog_response (GTK_DIALOG (dialog), response_id);
182
183           return;
184         }
185     }
186   
187   g_list_free (children);
188 }
189
190 static GObject *
191 gtk_recent_chooser_dialog_constructor (GType                  type,
192                                        guint                  n_construct_properties,
193                                        GObjectConstructParam *construct_params)
194 {
195   GtkRecentChooserDialogPrivate *priv;
196   GtkWidget *content_area;
197   GObject *object;
198
199   object = G_OBJECT_CLASS (gtk_recent_chooser_dialog_parent_class)->constructor (type,
200                                                                                  n_construct_properties,
201                                                                                  construct_params);
202   priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
203   
204   gtk_widget_push_composite_child ();
205   
206   if (priv->manager)
207     priv->chooser = g_object_new (GTK_TYPE_RECENT_CHOOSER_WIDGET,
208                                   "recent-manager", priv->manager,
209                                   NULL);
210   else
211     priv->chooser = g_object_new (GTK_TYPE_RECENT_CHOOSER_WIDGET, NULL);
212   
213   g_signal_connect (priv->chooser, "item-activated",
214                     G_CALLBACK (gtk_recent_chooser_item_activated_cb),
215                     object);
216
217   content_area = gtk_dialog_get_content_area (GTK_DIALOG (object));
218
219   gtk_container_set_border_width (GTK_CONTAINER (priv->chooser), 5);
220   gtk_box_pack_start (GTK_BOX (content_area),
221                       priv->chooser, TRUE, TRUE, 0);
222   gtk_widget_show (priv->chooser);
223   
224   _gtk_recent_chooser_set_delegate (GTK_RECENT_CHOOSER (object),
225                                     GTK_RECENT_CHOOSER (priv->chooser));
226   
227   gtk_widget_pop_composite_child ();
228   
229   return object;
230 }
231
232 static void
233 gtk_recent_chooser_dialog_set_property (GObject      *object,
234                                         guint         prop_id,
235                                         const GValue *value,
236                                         GParamSpec   *pspec)
237 {
238   GtkRecentChooserDialogPrivate *priv;
239   
240   priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
241   
242   switch (prop_id)
243     {
244     case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
245       priv->manager = g_value_get_object (value);
246       break;
247     default:
248       g_object_set_property (G_OBJECT (priv->chooser), pspec->name, value);
249       break;
250     }
251 }
252
253 static void
254 gtk_recent_chooser_dialog_get_property (GObject      *object,
255                                         guint         prop_id,
256                                         GValue       *value,
257                                         GParamSpec   *pspec)
258 {
259   GtkRecentChooserDialogPrivate *priv;
260   
261   priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
262   
263   g_object_get_property (G_OBJECT (priv->chooser), pspec->name, value);
264 }
265
266 static void
267 gtk_recent_chooser_dialog_finalize (GObject *object)
268 {
269   GtkRecentChooserDialog *dialog = GTK_RECENT_CHOOSER_DIALOG (object);
270  
271   dialog->priv->manager = NULL;
272   
273   G_OBJECT_CLASS (gtk_recent_chooser_dialog_parent_class)->finalize (object);
274 }
275
276 static GtkWidget *
277 gtk_recent_chooser_dialog_new_valist (const gchar      *title,
278                                       GtkWindow        *parent,
279                                       GtkRecentManager *manager,
280                                       const gchar      *first_button_text,
281                                       va_list           varargs)
282 {
283   GtkWidget *result;
284   const char *button_text = first_button_text;
285   gint response_id;
286   
287   result = g_object_new (GTK_TYPE_RECENT_CHOOSER_DIALOG,
288                          "title", title,
289                          "recent-manager", manager,
290                          NULL);
291   
292   if (parent)
293     gtk_window_set_transient_for (GTK_WINDOW (result), parent);
294   
295   while (button_text)
296     {
297       response_id = va_arg (varargs, gint);
298       gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
299       button_text = va_arg (varargs, const gchar *);
300     }
301   
302   return result;
303 }
304
305 /**
306  * gtk_recent_chooser_dialog_new:
307  * @title: (allow-none): Title of the dialog, or %NULL
308  * @parent: (allow-none): Transient parent of the dialog, or %NULL,
309  * @first_button_text: (allow-none): stock ID or text to go in the first button, or %NULL
310  * @...: response ID for the first button, then additional (button, id)
311  *   pairs, ending with %NULL
312  *
313  * Creates a new #GtkRecentChooserDialog.  This function is analogous to
314  * gtk_dialog_new_with_buttons().
315  *
316  * Return value: a new #GtkRecentChooserDialog
317  *
318  * Since: 2.10
319  */
320 GtkWidget *
321 gtk_recent_chooser_dialog_new (const gchar *title,
322                                GtkWindow   *parent,
323                                const gchar *first_button_text,
324                                ...)
325 {
326   GtkWidget *result;
327   va_list varargs;
328   
329   va_start (varargs, first_button_text);
330   result = gtk_recent_chooser_dialog_new_valist (title,
331                                                  parent,
332                                                  NULL,
333                                                  first_button_text,
334                                                  varargs);
335   va_end (varargs);
336   
337   return result;
338 }
339
340 /**
341  * gtk_recent_chooser_dialog_new_for_manager:
342  * @title: (allow-none): Title of the dialog, or %NULL
343  * @parent: (allow-none): Transient parent of the dialog, or %NULL,
344  * @manager: a #GtkRecentManager
345  * @first_button_text: (allow-none): stock ID or text to go in the first button, or %NULL
346  * @...: response ID for the first button, then additional (button, id)
347  *   pairs, ending with %NULL
348  *
349  * Creates a new #GtkRecentChooserDialog with a specified recent manager.
350  *
351  * This is useful if you have implemented your own recent manager, or if you
352  * have a customized instance of a #GtkRecentManager object.
353  *
354  * Return value: a new #GtkRecentChooserDialog
355  *
356  * Since: 2.10
357  */
358 GtkWidget *
359 gtk_recent_chooser_dialog_new_for_manager (const gchar      *title,
360                                            GtkWindow        *parent,
361                                            GtkRecentManager *manager,
362                                            const gchar      *first_button_text,
363                                            ...)
364 {
365   GtkWidget *result;
366   va_list varargs;
367   
368   va_start (varargs, first_button_text);
369   result = gtk_recent_chooser_dialog_new_valist (title,
370                                                  parent,
371                                                  manager,
372                                                  first_button_text,
373                                                  varargs);
374   va_end (varargs);
375   
376   return result;
377 }