]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentchooserdialog.c
configure.ac: Indeed the minimum required version is 2.29.4, not 2.29.2
[~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 static void gtk_recent_chooser_dialog_map       (GtkWidget *widget);
104 static void gtk_recent_chooser_dialog_unmap     (GtkWidget *widget);
105
106 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserDialog,
107                          gtk_recent_chooser_dialog,
108                          GTK_TYPE_DIALOG,
109                          G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
110                                                 _gtk_recent_chooser_delegate_iface_init))
111
112 static void
113 gtk_recent_chooser_dialog_class_init (GtkRecentChooserDialogClass *klass)
114 {
115   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
116   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
117   
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;
122   
123   widget_class->map = gtk_recent_chooser_dialog_map;
124   widget_class->unmap = gtk_recent_chooser_dialog_unmap;
125   
126   _gtk_recent_chooser_install_properties (gobject_class);
127   
128   g_type_class_add_private (klass, sizeof (GtkRecentChooserDialogPrivate));
129 }
130
131 static void
132 gtk_recent_chooser_dialog_init (GtkRecentChooserDialog *dialog)
133 {
134   GtkWidget *content_area, *action_area;
135
136   GtkRecentChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
137                                                                      GTK_TYPE_RECENT_CHOOSER_DIALOG,
138                                                                      GtkRecentChooserDialogPrivate);
139   GtkDialog *rc_dialog = GTK_DIALOG (dialog);
140   
141   dialog->priv = priv;
142
143   content_area = gtk_dialog_get_content_area (rc_dialog);
144   action_area = gtk_dialog_get_action_area (rc_dialog);
145
146   gtk_container_set_border_width (GTK_CONTAINER (rc_dialog), 5);
147   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
148   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
149
150 }
151
152 /* we intercept the GtkRecentChooser::item_activated signal and try to
153  * make the dialog emit a valid response signal
154  */
155 static void
156 gtk_recent_chooser_item_activated_cb (GtkRecentChooser *chooser,
157                                       gpointer          user_data)
158 {
159   GtkDialog *rc_dialog;
160   GtkRecentChooserDialog *dialog;
161   GtkWidget *action_area;
162   GList *children, *l;
163
164   dialog = GTK_RECENT_CHOOSER_DIALOG (user_data);
165   rc_dialog = GTK_DIALOG (dialog);
166
167   if (gtk_window_activate_default (GTK_WINDOW (dialog)))
168     return;
169
170   action_area = gtk_dialog_get_action_area (rc_dialog);
171   children = gtk_container_get_children (GTK_CONTAINER (action_area));
172   
173   for (l = children; l; l = l->next)
174     {
175       GtkWidget *widget;
176       gint response_id;
177       
178       widget = GTK_WIDGET (l->data);
179       response_id = gtk_dialog_get_response_for_widget (rc_dialog, widget);
180       
181       if (response_id == GTK_RESPONSE_ACCEPT ||
182           response_id == GTK_RESPONSE_OK     ||
183           response_id == GTK_RESPONSE_YES    ||
184           response_id == GTK_RESPONSE_APPLY)
185         {
186           g_list_free (children);
187           
188           gtk_dialog_response (GTK_DIALOG (dialog), response_id);
189
190           return;
191         }
192     }
193   
194   g_list_free (children);
195 }
196
197 static GObject *
198 gtk_recent_chooser_dialog_constructor (GType                  type,
199                                        guint                  n_construct_properties,
200                                        GObjectConstructParam *construct_params)
201 {
202   GtkRecentChooserDialogPrivate *priv;
203   GtkWidget *content_area;
204   GObject *object;
205
206   object = G_OBJECT_CLASS (gtk_recent_chooser_dialog_parent_class)->constructor (type,
207                                                                                  n_construct_properties,
208                                                                                  construct_params);
209   priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
210   
211   gtk_widget_push_composite_child ();
212   
213   if (priv->manager)
214     priv->chooser = g_object_new (GTK_TYPE_RECENT_CHOOSER_WIDGET,
215                                   "recent-manager", priv->manager,
216                                   NULL);
217   else
218     priv->chooser = g_object_new (GTK_TYPE_RECENT_CHOOSER_WIDGET, NULL);
219   
220   g_signal_connect (priv->chooser, "item-activated",
221                     G_CALLBACK (gtk_recent_chooser_item_activated_cb),
222                     object);
223
224   content_area = gtk_dialog_get_content_area (GTK_DIALOG (object));
225
226   gtk_container_set_border_width (GTK_CONTAINER (priv->chooser), 5);
227   gtk_box_pack_start (GTK_BOX (content_area),
228                       priv->chooser, TRUE, TRUE, 0);
229   gtk_widget_show (priv->chooser);
230   
231   _gtk_recent_chooser_set_delegate (GTK_RECENT_CHOOSER (object),
232                                     GTK_RECENT_CHOOSER (priv->chooser));
233   
234   gtk_widget_pop_composite_child ();
235   
236   return object;
237 }
238
239 static void
240 gtk_recent_chooser_dialog_set_property (GObject      *object,
241                                         guint         prop_id,
242                                         const GValue *value,
243                                         GParamSpec   *pspec)
244 {
245   GtkRecentChooserDialogPrivate *priv;
246   
247   priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
248   
249   switch (prop_id)
250     {
251     case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
252       priv->manager = g_value_get_object (value);
253       break;
254     default:
255       g_object_set_property (G_OBJECT (priv->chooser), pspec->name, value);
256       break;
257     }
258 }
259
260 static void
261 gtk_recent_chooser_dialog_get_property (GObject      *object,
262                                         guint         prop_id,
263                                         GValue       *value,
264                                         GParamSpec   *pspec)
265 {
266   GtkRecentChooserDialogPrivate *priv;
267   
268   priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
269   
270   g_object_get_property (G_OBJECT (priv->chooser), pspec->name, value);
271 }
272
273 static void
274 gtk_recent_chooser_dialog_finalize (GObject *object)
275 {
276   GtkRecentChooserDialog *dialog = GTK_RECENT_CHOOSER_DIALOG (object);
277  
278   dialog->priv->manager = NULL;
279   
280   G_OBJECT_CLASS (gtk_recent_chooser_dialog_parent_class)->finalize (object);
281 }
282
283 static void
284 gtk_recent_chooser_dialog_map (GtkWidget *widget)
285 {
286   GtkRecentChooserDialog *dialog = GTK_RECENT_CHOOSER_DIALOG (widget);
287   GtkRecentChooserDialogPrivate *priv = dialog->priv;
288   
289   if (!gtk_widget_get_mapped (priv->chooser))
290     gtk_widget_map (priv->chooser);
291
292   GTK_WIDGET_CLASS (gtk_recent_chooser_dialog_parent_class)->map (widget);
293 }
294
295 static void
296 gtk_recent_chooser_dialog_unmap (GtkWidget *widget)
297 {
298   GtkRecentChooserDialog *dialog = GTK_RECENT_CHOOSER_DIALOG (widget);
299   GtkRecentChooserDialogPrivate *priv = dialog->priv;
300   
301   GTK_WIDGET_CLASS (gtk_recent_chooser_dialog_parent_class)->unmap (widget);
302   
303   gtk_widget_unmap (priv->chooser);
304 }
305
306 static GtkWidget *
307 gtk_recent_chooser_dialog_new_valist (const gchar      *title,
308                                       GtkWindow        *parent,
309                                       GtkRecentManager *manager,
310                                       const gchar      *first_button_text,
311                                       va_list           varargs)
312 {
313   GtkWidget *result;
314   const char *button_text = first_button_text;
315   gint response_id;
316   
317   result = g_object_new (GTK_TYPE_RECENT_CHOOSER_DIALOG,
318                          "title", title,
319                          "recent-manager", manager,
320                          NULL);
321   
322   if (parent)
323     gtk_window_set_transient_for (GTK_WINDOW (result), parent);
324   
325   while (button_text)
326     {
327       response_id = va_arg (varargs, gint);
328       gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
329       button_text = va_arg (varargs, const gchar *);
330     }
331   
332   return result;
333 }
334
335 /**
336  * gtk_recent_chooser_dialog_new:
337  * @title: (allow-none): Title of the dialog, or %NULL
338  * @parent: (allow-none): Transient parent of the dialog, or %NULL,
339  * @first_button_text: (allow-none): stock ID or text to go in the first button, or %NULL
340  * @Varargs: response ID for the first button, then additional (button, id)
341  *   pairs, ending with %NULL
342  *
343  * Creates a new #GtkRecentChooserDialog.  This function is analogous to
344  * gtk_dialog_new_with_buttons().
345  *
346  * Return value: a new #GtkRecentChooserDialog
347  *
348  * Since: 2.10
349  */
350 GtkWidget *
351 gtk_recent_chooser_dialog_new (const gchar *title,
352                                GtkWindow   *parent,
353                                const gchar *first_button_text,
354                                ...)
355 {
356   GtkWidget *result;
357   va_list varargs;
358   
359   va_start (varargs, first_button_text);
360   result = gtk_recent_chooser_dialog_new_valist (title,
361                                                  parent,
362                                                  NULL,
363                                                  first_button_text,
364                                                  varargs);
365   va_end (varargs);
366   
367   return result;
368 }
369
370 /**
371  * gtk_recent_chooser_dialog_new_for_manager:
372  * @title: (allow-none): Title of the dialog, or %NULL
373  * @parent: (allow-none): Transient parent of the dialog, or %NULL,
374  * @manager: a #GtkRecentManager
375  * @first_button_text: (allow-none): stock ID or text to go in the first button, or %NULL
376  * @Varargs: response ID for the first button, then additional (button, id)
377  *   pairs, ending with %NULL
378  *
379  * Creates a new #GtkRecentChooserDialog with a specified recent manager.
380  *
381  * This is useful if you have implemented your own recent manager, or if you
382  * have a customized instance of a #GtkRecentManager object.
383  *
384  * Return value: a new #GtkRecentChooserDialog
385  *
386  * Since: 2.10
387  */
388 GtkWidget *
389 gtk_recent_chooser_dialog_new_for_manager (const gchar      *title,
390                                            GtkWindow        *parent,
391                                            GtkRecentManager *manager,
392                                            const gchar      *first_button_text,
393                                            ...)
394 {
395   GtkWidget *result;
396   va_list varargs;
397   
398   va_start (varargs, first_button_text);
399   result = gtk_recent_chooser_dialog_new_valist (title,
400                                                  parent,
401                                                  manager,
402                                                  first_button_text,
403                                                  varargs);
404   va_end (varargs);
405   
406   return result;
407 }