]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentchooserdialog.c
Deprecate widget flag: GTK_WIDGET_MAPPED
[~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 #include "gtkalias.h"
29
30 #include <stdarg.h>
31
32 struct _GtkRecentChooserDialogPrivate
33 {
34   GtkRecentManager *manager;
35   
36   GtkWidget *chooser;
37 };
38
39 #define GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE(obj)      (GTK_RECENT_CHOOSER_DIALOG (obj)->priv)
40
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);
44
45 static GObject *gtk_recent_chooser_dialog_constructor (GType                  type,
46                                                        guint                  n_construct_properties,
47                                                        GObjectConstructParam *construct_params);
48
49 static void gtk_recent_chooser_dialog_set_property (GObject      *object,
50                                                     guint         prop_id,
51                                                     const GValue *value,
52                                                     GParamSpec   *pspec);
53 static void gtk_recent_chooser_dialog_get_property (GObject      *object,
54                                                     guint         prop_id,
55                                                     GValue       *value,
56                                                     GParamSpec   *pspec);
57
58 static void gtk_recent_chooser_dialog_map       (GtkWidget *widget);
59 static void gtk_recent_chooser_dialog_unmap     (GtkWidget *widget);
60
61 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserDialog,
62                          gtk_recent_chooser_dialog,
63                          GTK_TYPE_DIALOG,
64                          G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
65                                                 _gtk_recent_chooser_delegate_iface_init))
66
67 static void
68 gtk_recent_chooser_dialog_class_init (GtkRecentChooserDialogClass *klass)
69 {
70   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
71   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
72   
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;
77   
78   widget_class->map = gtk_recent_chooser_dialog_map;
79   widget_class->unmap = gtk_recent_chooser_dialog_unmap;
80   
81   _gtk_recent_chooser_install_properties (gobject_class);
82   
83   g_type_class_add_private (klass, sizeof (GtkRecentChooserDialogPrivate));
84 }
85
86 static void
87 gtk_recent_chooser_dialog_init (GtkRecentChooserDialog *dialog)
88 {
89   GtkRecentChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
90                                                                      GTK_TYPE_RECENT_CHOOSER_DIALOG,
91                                                                      GtkRecentChooserDialogPrivate);
92   GtkDialog *rc_dialog = GTK_DIALOG (dialog);
93   
94   dialog->priv = priv;
95
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);
100
101 }
102
103 /* we intercept the GtkRecentChooser::item_activated signal and try to
104  * make the dialog emit a valid response signal
105  */
106 static void
107 gtk_recent_chooser_item_activated_cb (GtkRecentChooser *chooser,
108                                       gpointer          user_data)
109 {
110   GtkRecentChooserDialog *dialog;
111   GList *children, *l;
112
113   dialog = GTK_RECENT_CHOOSER_DIALOG (user_data);
114
115   if (gtk_window_activate_default (GTK_WINDOW (dialog)))
116     return;
117   
118   children = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area));
119   
120   for (l = children; l; l = l->next)
121     {
122       GtkWidget *widget;
123       gint response_id;
124       
125       widget = GTK_WIDGET (l->data);
126       response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
127       
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)
132         {
133           g_list_free (children);
134           
135           gtk_dialog_response (GTK_DIALOG (dialog), response_id);
136
137           return;
138         }
139     }
140   
141   g_list_free (children);
142 }
143
144 static GObject *
145 gtk_recent_chooser_dialog_constructor (GType                  type,
146                                        guint                  n_construct_properties,
147                                        GObjectConstructParam *construct_params)
148 {
149   GObject *object;
150   GtkRecentChooserDialogPrivate *priv;
151   
152   object = G_OBJECT_CLASS (gtk_recent_chooser_dialog_parent_class)->constructor (type,
153                                                                                  n_construct_properties,
154                                                                                  construct_params);
155   priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
156   
157   gtk_widget_push_composite_child ();
158   
159   if (priv->manager)
160     priv->chooser = g_object_new (GTK_TYPE_RECENT_CHOOSER_WIDGET,
161                                   "recent-manager", priv->manager,
162                                   NULL);
163   else
164     priv->chooser = g_object_new (GTK_TYPE_RECENT_CHOOSER_WIDGET, NULL);
165   
166   g_signal_connect (priv->chooser, "item-activated",
167                     G_CALLBACK (gtk_recent_chooser_item_activated_cb),
168                     object);
169
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);
174   
175   _gtk_recent_chooser_set_delegate (GTK_RECENT_CHOOSER (object),
176                                     GTK_RECENT_CHOOSER (priv->chooser));
177   
178   gtk_widget_pop_composite_child ();
179   
180   return object;
181 }
182
183 static void
184 gtk_recent_chooser_dialog_set_property (GObject      *object,
185                                         guint         prop_id,
186                                         const GValue *value,
187                                         GParamSpec   *pspec)
188 {
189   GtkRecentChooserDialogPrivate *priv;
190   
191   priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
192   
193   switch (prop_id)
194     {
195     case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
196       priv->manager = g_value_get_object (value);
197       break;
198     default:
199       g_object_set_property (G_OBJECT (priv->chooser), pspec->name, value);
200       break;
201     }
202 }
203
204 static void
205 gtk_recent_chooser_dialog_get_property (GObject      *object,
206                                         guint         prop_id,
207                                         GValue       *value,
208                                         GParamSpec   *pspec)
209 {
210   GtkRecentChooserDialogPrivate *priv;
211   
212   priv = GTK_RECENT_CHOOSER_DIALOG_GET_PRIVATE (object);
213   
214   g_object_get_property (G_OBJECT (priv->chooser), pspec->name, value);
215 }
216
217 static void
218 gtk_recent_chooser_dialog_finalize (GObject *object)
219 {
220   GtkRecentChooserDialog *dialog = GTK_RECENT_CHOOSER_DIALOG (object);
221  
222   dialog->priv->manager = NULL;
223   
224   G_OBJECT_CLASS (gtk_recent_chooser_dialog_parent_class)->finalize (object);
225 }
226
227 static void
228 gtk_recent_chooser_dialog_map (GtkWidget *widget)
229 {
230   GtkRecentChooserDialog *dialog = GTK_RECENT_CHOOSER_DIALOG (widget);
231   GtkRecentChooserDialogPrivate *priv = dialog->priv;
232   
233   if (!gtk_widget_get_mapped (priv->chooser))
234     gtk_widget_map (priv->chooser);
235
236   GTK_WIDGET_CLASS (gtk_recent_chooser_dialog_parent_class)->map (widget);
237 }
238
239 static void
240 gtk_recent_chooser_dialog_unmap (GtkWidget *widget)
241 {
242   GtkRecentChooserDialog *dialog = GTK_RECENT_CHOOSER_DIALOG (widget);
243   GtkRecentChooserDialogPrivate *priv = dialog->priv;
244   
245   GTK_WIDGET_CLASS (gtk_recent_chooser_dialog_parent_class)->unmap (widget);
246   
247   gtk_widget_unmap (priv->chooser);
248 }
249
250 static GtkWidget *
251 gtk_recent_chooser_dialog_new_valist (const gchar      *title,
252                                       GtkWindow        *parent,
253                                       GtkRecentManager *manager,
254                                       const gchar      *first_button_text,
255                                       va_list           varargs)
256 {
257   GtkWidget *result;
258   const char *button_text = first_button_text;
259   gint response_id;
260   
261   result = g_object_new (GTK_TYPE_RECENT_CHOOSER_DIALOG,
262                          "title", title,
263                          "recent-manager", manager,
264                          NULL);
265   
266   if (parent)
267     gtk_window_set_transient_for (GTK_WINDOW (result), parent);
268   
269   while (button_text)
270     {
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 *);
274     }
275   
276   return result;
277 }
278
279 /**
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
286  *
287  * Creates a new #GtkRecentChooserDialog.  This function is analogous to
288  * gtk_dialog_new_with_buttons().
289  *
290  * Return value: a new #GtkRecentChooserDialog
291  *
292  * Since: 2.10
293  */
294 GtkWidget *
295 gtk_recent_chooser_dialog_new (const gchar *title,
296                                GtkWindow   *parent,
297                                const gchar *first_button_text,
298                                ...)
299 {
300   GtkWidget *result;
301   va_list varargs;
302   
303   va_start (varargs, first_button_text);
304   result = gtk_recent_chooser_dialog_new_valist (title,
305                                                  parent,
306                                                  NULL,
307                                                  first_button_text,
308                                                  varargs);
309   va_end (varargs);
310   
311   return result;
312 }
313
314 /**
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
322  *
323  * Creates a new #GtkRecentChooserDialog with a specified recent manager.
324  *
325  * This is useful if you have implemented your own recent manager, or if you
326  * have a customized instance of a #GtkRecentManager object.
327  *
328  * Return value: a new #GtkRecentChooserDialog
329  *
330  * Since: 2.10
331  */
332 GtkWidget *
333 gtk_recent_chooser_dialog_new_for_manager (const gchar      *title,
334                                            GtkWindow        *parent,
335                                            GtkRecentManager *manager,
336                                            const gchar      *first_button_text,
337                                            ...)
338 {
339   GtkWidget *result;
340   va_list varargs;
341   
342   va_start (varargs, first_button_text);
343   result = gtk_recent_chooser_dialog_new_valist (title,
344                                                  parent,
345                                                  manager,
346                                                  first_button_text,
347                                                  varargs);
348   va_end (varargs);
349   
350   return result;
351 }
352
353 #define __GTK_RECENT_CHOOSER_DIALOG_C__
354 #include "gtkaliasdef.c"