]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentchoosermenu.c
Remove a surplus GDK_THREADS_LEAVE() and avoid a potential use after free
[~andy/gtk] / gtk / gtkrecentchoosermenu.c
1 /* GTK - The GIMP Toolkit
2  * gtkrecentchoosermenu.c - Recently used items menu widget
3  * Copyright (C) 2005, 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 <string.h>
24
25 #include <gdk/gdkscreen.h>
26
27 #include "gtkrecentmanager.h"
28 #include "gtkrecentfilter.h"
29 #include "gtkrecentchooser.h"
30 #include "gtkrecentchooserutils.h"
31 #include "gtkrecentchooserprivate.h"
32 #include "gtkrecentchoosermenu.h"
33
34 #include "gtkstock.h"
35 #include "gtkicontheme.h"
36 #include "gtkiconfactory.h"
37 #include "gtkintl.h"
38 #include "gtksettings.h"
39 #include "gtkmenushell.h"
40 #include "gtkmenuitem.h"
41 #include "gtkimagemenuitem.h"
42 #include "gtkseparatormenuitem.h"
43 #include "gtkmenu.h"
44 #include "gtkimage.h"
45 #include "gtklabel.h"
46 #include "gtkobject.h"
47 #include "gtktooltips.h"
48 #include "gtktypebuiltins.h"
49 #include "gtkprivate.h"
50 #include "gtkalias.h"
51
52 struct _GtkRecentChooserMenuPrivate
53 {
54   /* the recent manager object */
55   GtkRecentManager *manager;
56   
57   /* size of the icons of the menu items */  
58   gint icon_size;
59
60   /* max size of the menu item label */
61   gint label_width;
62
63   gint first_recent_item_pos;
64   GtkWidget *placeholder;
65
66   /* RecentChooser properties */
67   gint limit;  
68   guint show_private : 1;
69   guint show_not_found : 1;
70   guint show_tips : 1;
71   guint show_icons : 1;
72   guint local_only : 1;
73   
74   guint show_numbers : 1;
75   
76   GtkRecentSortType sort_type;
77   GtkRecentSortFunc sort_func;
78   gpointer sort_data;
79   GDestroyNotify sort_data_destroy;
80   
81   GSList *filters;
82   GtkRecentFilter *current_filter;
83  
84   guint local_manager : 1;
85   gulong manager_changed_id;
86
87   gulong populate_id;
88
89   /* tooltips for our bookmark items*/
90   GtkTooltips *tooltips;
91 };
92
93 enum {
94   PROP_0,
95
96   PROP_SHOW_NUMBERS
97 };
98
99 #define FALLBACK_ICON_SIZE      32
100 #define FALLBACK_ITEM_LIMIT     10
101 #define DEFAULT_LABEL_WIDTH     30
102
103 #define GTK_RECENT_CHOOSER_MENU_GET_PRIVATE(obj)        (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RECENT_CHOOSER_MENU, GtkRecentChooserMenuPrivate))
104
105 static void     gtk_recent_chooser_menu_finalize    (GObject                   *object);
106 static void     gtk_recent_chooser_menu_dispose     (GObject                   *object);
107 static GObject *gtk_recent_chooser_menu_constructor (GType                      type,
108                                                      guint                      n_construct_properties,
109                                                      GObjectConstructParam     *construct_params);
110
111 static void gtk_recent_chooser_iface_init      (GtkRecentChooserIface     *iface);
112
113 static void gtk_recent_chooser_menu_set_property (GObject      *object,
114                                                   guint         prop_id,
115                                                   const GValue *value,
116                                                   GParamSpec   *pspec);
117 static void gtk_recent_chooser_menu_get_property (GObject      *object,
118                                                   guint         prop_id,
119                                                   GValue       *value,
120                                                   GParamSpec   *pspec);
121
122 static gboolean          gtk_recent_chooser_menu_set_current_uri    (GtkRecentChooser  *chooser,
123                                                                      const gchar       *uri,
124                                                                      GError           **error);
125 static gchar *           gtk_recent_chooser_menu_get_current_uri    (GtkRecentChooser  *chooser);
126 static gboolean          gtk_recent_chooser_menu_select_uri         (GtkRecentChooser  *chooser,
127                                                                      const gchar       *uri,
128                                                                      GError           **error);
129 static void              gtk_recent_chooser_menu_unselect_uri       (GtkRecentChooser  *chooser,
130                                                                      const gchar       *uri);
131 static void              gtk_recent_chooser_menu_select_all         (GtkRecentChooser  *chooser);
132 static void              gtk_recent_chooser_menu_unselect_all       (GtkRecentChooser  *chooser);
133 static GList *           gtk_recent_chooser_menu_get_items          (GtkRecentChooser  *chooser);
134 static GtkRecentManager *gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser  *chooser);
135 static void              gtk_recent_chooser_menu_set_sort_func      (GtkRecentChooser  *chooser,
136                                                                      GtkRecentSortFunc  sort_func,
137                                                                      gpointer           sort_data,
138                                                                      GDestroyNotify     data_destroy);
139 static void              gtk_recent_chooser_menu_add_filter         (GtkRecentChooser  *chooser,
140                                                                      GtkRecentFilter   *filter);
141 static void              gtk_recent_chooser_menu_remove_filter      (GtkRecentChooser  *chooser,
142                                                                      GtkRecentFilter   *filter);
143 static GSList *          gtk_recent_chooser_menu_list_filters       (GtkRecentChooser  *chooser);
144 static void              gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
145                                                                      GtkRecentFilter      *filter);
146
147 static void              gtk_recent_chooser_menu_populate           (GtkRecentChooserMenu *menu);
148 static void              gtk_recent_chooser_menu_set_show_tips      (GtkRecentChooserMenu *menu,
149                                                                      gboolean              show_tips);
150
151 static void     set_recent_manager (GtkRecentChooserMenu *menu,
152                                     GtkRecentManager     *manager);
153
154 static void     chooser_set_sort_type (GtkRecentChooserMenu *menu,
155                                        GtkRecentSortType     sort_type);
156
157 static gint     get_icon_size_for_widget (GtkWidget *widget);
158
159 static void     item_activate_cb   (GtkWidget        *widget,
160                                     gpointer          user_data);
161 static void     manager_changed_cb (GtkRecentManager *manager,
162                                     gpointer          user_data);
163
164 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserMenu,
165                          gtk_recent_chooser_menu,
166                          GTK_TYPE_MENU,
167                          G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
168                                                 gtk_recent_chooser_iface_init))
169
170
171 static void
172 gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface)
173 {
174   iface->set_current_uri = gtk_recent_chooser_menu_set_current_uri;
175   iface->get_current_uri = gtk_recent_chooser_menu_get_current_uri;
176   iface->select_uri = gtk_recent_chooser_menu_select_uri;
177   iface->unselect_uri = gtk_recent_chooser_menu_unselect_uri;
178   iface->select_all = gtk_recent_chooser_menu_select_all;
179   iface->unselect_all = gtk_recent_chooser_menu_unselect_all;
180   iface->get_items = gtk_recent_chooser_menu_get_items;
181   iface->get_recent_manager = gtk_recent_chooser_menu_get_recent_manager;
182   iface->set_sort_func = gtk_recent_chooser_menu_set_sort_func;
183   iface->add_filter = gtk_recent_chooser_menu_add_filter;
184   iface->remove_filter = gtk_recent_chooser_menu_remove_filter;
185   iface->list_filters = gtk_recent_chooser_menu_list_filters;
186 }
187
188 static void
189 gtk_recent_chooser_menu_class_init (GtkRecentChooserMenuClass *klass)
190 {
191   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
192
193   gobject_class->constructor = gtk_recent_chooser_menu_constructor;
194   gobject_class->dispose = gtk_recent_chooser_menu_dispose;
195   gobject_class->finalize = gtk_recent_chooser_menu_finalize;
196   gobject_class->set_property = gtk_recent_chooser_menu_set_property;
197   gobject_class->get_property = gtk_recent_chooser_menu_get_property;
198
199   _gtk_recent_chooser_install_properties (gobject_class);
200
201   /**
202    * GtkRecentChooserMenu:show-numbers
203    *
204    * Whether the first ten items in the menu should be prepended by
205    * a number acting as a unique mnemonic.
206    *
207    * Since: 2.10
208    */
209   g_object_class_install_property (gobject_class,
210                                    PROP_SHOW_NUMBERS,
211                                    g_param_spec_boolean ("show-numbers",
212                                                          P_("Show Numbers"),
213                                                          P_("Whether the items should be displayed with a number"),
214                                                          FALSE,
215                                                          GTK_PARAM_READWRITE));
216   
217   g_type_class_add_private (klass, sizeof (GtkRecentChooserMenuPrivate));
218 }
219
220 static void
221 gtk_recent_chooser_menu_init (GtkRecentChooserMenu *menu)
222 {
223   GtkRecentChooserMenuPrivate *priv;
224   
225   priv = GTK_RECENT_CHOOSER_MENU_GET_PRIVATE (menu);
226   
227   menu->priv = priv;
228   
229   priv->show_icons= TRUE;
230   priv->show_numbers = FALSE;
231   priv->show_tips = FALSE;
232   priv->show_not_found = TRUE;
233   priv->show_private = FALSE;
234   priv->local_only = TRUE;
235   
236   priv->limit = FALLBACK_ITEM_LIMIT;
237
238   priv->sort_type = GTK_RECENT_SORT_NONE;
239   
240   priv->icon_size = FALLBACK_ICON_SIZE;
241   
242   priv->label_width = DEFAULT_LABEL_WIDTH;
243   
244   priv->first_recent_item_pos = -1;
245   priv->placeholder = NULL;
246
247   priv->current_filter = NULL;
248     
249   priv->tooltips = gtk_tooltips_new ();
250   g_object_ref_sink (priv->tooltips);
251 }
252
253 static void
254 gtk_recent_chooser_menu_finalize (GObject *object)
255 {
256   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
257   GtkRecentChooserMenuPrivate *priv = menu->priv;
258   
259   priv->manager = NULL;
260   
261   if (priv->sort_data_destroy)
262     {
263       priv->sort_data_destroy (priv->sort_data);
264       priv->sort_data_destroy = NULL;
265     }
266
267   priv->sort_data = NULL;
268   priv->sort_func = NULL;
269   
270   G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->finalize (object);
271 }
272
273 static void
274 gtk_recent_chooser_menu_dispose (GObject *object)
275 {
276   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
277   GtkRecentChooserMenuPrivate *priv = menu->priv;
278
279   if (priv->manager_changed_id)
280     {
281       if (priv->manager)
282         g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
283
284       priv->manager_changed_id = 0;
285     }
286   
287   if (priv->populate_id)
288     {
289       g_source_remove (priv->populate_id);
290       priv->populate_id = 0;
291     }
292
293   if (priv->tooltips)
294     {
295       g_object_unref (priv->tooltips);
296       priv->tooltips = NULL;
297     }
298  
299   if (priv->current_filter)
300     {
301       g_object_unref (priv->current_filter);
302       priv->current_filter = NULL;
303     }
304   
305   G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->dispose (object);
306 }
307
308 static GObject *
309 gtk_recent_chooser_menu_constructor (GType                  type,
310                                      guint                  n_params,
311                                      GObjectConstructParam *params)
312 {
313   GtkRecentChooserMenu *menu;
314   GtkRecentChooserMenuPrivate *priv;
315   GObjectClass *parent_class;
316   GObject *object;
317   
318   parent_class = G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class);
319   object = parent_class->constructor (type, n_params, params);
320   menu = GTK_RECENT_CHOOSER_MENU (object);
321   priv = menu->priv;
322   
323   g_assert (priv->manager);
324
325   /* we create a placeholder menuitem, to be used in case
326    * the menu is empty. this placeholder will stay around
327    * for the entire lifetime of the menu, and we just hide it
328    * when it's not used. we have to do this, and do it here,
329    * because we need a marker for the beginning of the recent
330    * items list, so that we can insert the new items at the
331    * right place when idly populating the menu in case the
332    * user appended or prepended custom menu items to the
333    * recent chooser menu widget.
334    */
335   priv->placeholder = gtk_menu_item_new_with_label (_("No items found"));
336   gtk_widget_set_sensitive (priv->placeholder, FALSE);
337   g_object_set_data (G_OBJECT (priv->placeholder),
338                      "gtk-recent-menu-placeholder",
339                      GINT_TO_POINTER (TRUE));
340
341   gtk_menu_shell_insert (GTK_MENU_SHELL (menu), priv->placeholder, 0);
342   gtk_widget_show (priv->placeholder);
343
344   /* (re)populate the menu */
345   gtk_recent_chooser_menu_populate (menu);
346
347   return object;
348 }
349
350 static void
351 gtk_recent_chooser_menu_set_property (GObject      *object,
352                                       guint         prop_id,
353                                       const GValue *value,
354                                       GParamSpec   *pspec)
355 {
356   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
357   GtkRecentChooserMenuPrivate *priv = menu->priv;
358   
359   switch (prop_id)
360     {
361     case PROP_SHOW_NUMBERS:
362       priv->show_numbers = g_value_get_boolean (value);
363       break;
364     case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
365       set_recent_manager (menu, g_value_get_object (value));
366       break;
367     case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
368       priv->show_private = g_value_get_boolean (value);
369       break;
370     case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
371       priv->show_not_found = g_value_get_boolean (value);
372       break;
373     case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
374       gtk_recent_chooser_menu_set_show_tips (menu, g_value_get_boolean (value));
375       break;
376     case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
377       priv->show_icons = g_value_get_boolean (value);
378       break;
379     case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
380       g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
381                  G_STRFUNC,
382                  G_OBJECT_TYPE_NAME (object));
383       break;
384     case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
385       priv->local_only = g_value_get_boolean (value);
386       break;
387     case GTK_RECENT_CHOOSER_PROP_LIMIT:
388       priv->limit = g_value_get_int (value);
389       break;
390     case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
391       chooser_set_sort_type (menu, g_value_get_enum (value));
392       break;
393     case GTK_RECENT_CHOOSER_PROP_FILTER:
394       gtk_recent_chooser_menu_set_current_filter (menu, g_value_get_object (value));
395       break;
396     default:
397       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
398       break;
399     }
400 }
401
402 static void
403 gtk_recent_chooser_menu_get_property (GObject    *object,
404                                       guint       prop_id,
405                                       GValue     *value,
406                                       GParamSpec *pspec)
407 {
408   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
409   GtkRecentChooserMenuPrivate *priv = menu->priv;
410   
411   switch (prop_id)
412     {
413     case PROP_SHOW_NUMBERS:
414       g_value_set_boolean (value, priv->show_numbers);
415       break;
416     case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
417       g_value_set_boolean (value, priv->show_tips);
418       break;
419     case GTK_RECENT_CHOOSER_PROP_LIMIT:
420       g_value_set_int (value, priv->limit);
421       break;
422     case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
423       g_value_set_boolean (value, priv->local_only);
424       break;
425     case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
426       g_value_set_enum (value, priv->sort_type);
427       break;
428     case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
429       g_value_set_boolean (value, priv->show_private);
430       break;
431     case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
432       g_value_set_boolean (value, priv->show_not_found);
433       break;
434     case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
435       g_value_set_boolean (value, priv->show_icons);
436       break;
437     case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
438       g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
439                  G_STRFUNC,
440                  G_OBJECT_TYPE_NAME (object));
441       break;
442     case GTK_RECENT_CHOOSER_PROP_FILTER:
443       g_value_set_object (value, priv->current_filter);
444       break;
445     default:
446       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
447       break;
448     }
449 }
450
451 static gboolean
452 gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser  *chooser,
453                                          const gchar       *uri,
454                                          GError           **error)
455 {
456   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
457   GList *children, *l;
458   GtkWidget *menu_item = NULL;
459   gboolean found = FALSE;
460   
461   children = gtk_container_get_children (GTK_CONTAINER (menu));
462   
463   for (l = children; l != NULL; l = l->next)
464     {
465       GtkRecentInfo *info;
466       
467       menu_item = GTK_WIDGET (l->data);
468       
469       info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
470       if (!info)
471         continue;
472       
473       if (strcmp (uri, gtk_recent_info_get_uri (info)) == 0)
474         {
475           gtk_menu_shell_activate_item (GTK_MENU_SHELL (menu),
476                                         menu_item,
477                                         TRUE);
478           found = TRUE;
479
480           break;
481         }
482     }
483
484   g_list_free (children);
485   
486   if (!found)  
487     {
488       g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
489                    GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
490                    _("No recently used resource found with URI `%s'"),
491                    uri);
492     }
493   
494   return found;
495 }
496
497 static gchar *
498 gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser  *chooser)
499 {
500   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
501   GtkWidget *menu_item;
502   GtkRecentInfo *info;
503   
504   menu_item = gtk_menu_get_active (GTK_MENU (menu));
505   if (!menu_item)
506     return NULL;
507   
508   info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
509   if (!info)
510     return NULL;
511   
512   return g_strdup (gtk_recent_info_get_uri (info));
513 }
514
515 static gboolean
516 gtk_recent_chooser_menu_select_uri (GtkRecentChooser  *chooser,
517                                     const gchar       *uri,
518                                     GError           **error)
519 {
520   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
521   GList *children, *l;
522   GtkWidget *menu_item = NULL;
523   gboolean found = FALSE;
524   
525   children = gtk_container_get_children (GTK_CONTAINER (menu));
526   for (l = children; l != NULL; l = l->next)
527     {
528       GtkRecentInfo *info;
529       
530       menu_item = GTK_WIDGET (l->data);
531       
532       info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
533       if (!info)
534         continue;
535       
536       if (0 == strcmp (uri, gtk_recent_info_get_uri (info)))
537         found = TRUE;
538     }
539
540   g_list_free (children);
541   
542   if (!found)  
543     {
544       g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
545                    GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
546                    _("No recently used resource found with URI `%s'"),
547                    uri);
548       return FALSE;
549     }
550   else
551     {
552       gtk_menu_shell_select_item (GTK_MENU_SHELL (menu), menu_item);
553
554       return TRUE;
555     }
556 }
557
558 static void
559 gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
560                                        const gchar     *uri)
561 {
562   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
563   
564   gtk_menu_shell_deselect (GTK_MENU_SHELL (menu));
565 }
566
567 static void
568 gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser)
569 {
570   g_warning (_("This function is not implemented for "
571                "widgets of class '%s'"),
572              g_type_name (G_OBJECT_TYPE (chooser)));
573 }
574
575 static void
576 gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser)
577 {
578   g_warning (_("This function is not implemented for "
579                "widgets of class '%s'"),
580              g_type_name (G_OBJECT_TYPE (chooser)));
581 }
582
583 static void
584 gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser  *chooser,
585                                        GtkRecentSortFunc  sort_func,
586                                        gpointer           sort_data,
587                                        GDestroyNotify     data_destroy)
588 {
589   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
590   GtkRecentChooserMenuPrivate *priv = menu->priv;
591   
592   if (priv->sort_data_destroy)
593     {
594       priv->sort_data_destroy (priv->sort_data);
595       priv->sort_data_destroy = NULL;
596     }
597       
598   priv->sort_func = NULL;
599   priv->sort_data = NULL;
600   priv->sort_data_destroy = NULL;
601   
602   if (sort_func)
603     {
604       priv->sort_func = sort_func;
605       priv->sort_data = sort_data;
606       priv->sort_data_destroy = data_destroy;
607     }
608 }
609
610 static void
611 chooser_set_sort_type (GtkRecentChooserMenu *menu,
612                        GtkRecentSortType     sort_type)
613 {
614   if (menu->priv->sort_type == sort_type)
615     return;
616
617   menu->priv->sort_type = sort_type;
618 }
619
620
621 static GList *
622 gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser)
623 {
624   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
625   GtkRecentChooserMenuPrivate *priv = menu->priv;
626
627   return _gtk_recent_chooser_get_items (chooser,
628                                         priv->current_filter,
629                                         priv->sort_func,
630                                         priv->sort_data);
631 }
632
633 static GtkRecentManager *
634 gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser)
635 {
636   GtkRecentChooserMenuPrivate *priv;
637  
638   priv = GTK_RECENT_CHOOSER_MENU (chooser)->priv;
639   
640   return priv->manager;
641 }
642
643 static void
644 gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
645                                     GtkRecentFilter  *filter)
646 {
647   GtkRecentChooserMenu *menu;
648
649   menu = GTK_RECENT_CHOOSER_MENU (chooser);
650   
651   gtk_recent_chooser_menu_set_current_filter (menu, filter);
652 }
653
654 static void
655 gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
656                                        GtkRecentFilter  *filter)
657 {
658   GtkRecentChooserMenu *menu;
659
660   menu = GTK_RECENT_CHOOSER_MENU (chooser);
661   
662   if (filter == menu->priv->current_filter)
663     {
664       g_object_unref (menu->priv->current_filter);
665       menu->priv->current_filter = NULL;
666
667       g_object_notify (G_OBJECT (menu), "filter");
668     }
669 }
670
671 static GSList *
672 gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser)
673 {
674   GtkRecentChooserMenu *menu;
675   GSList *retval = NULL;
676
677   menu = GTK_RECENT_CHOOSER_MENU (chooser);
678  
679   if (menu->priv->current_filter)
680     retval = g_slist_prepend (retval, menu->priv->current_filter);
681
682   return retval;
683 }
684
685 static void
686 gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
687                                             GtkRecentFilter      *filter)
688 {
689   GtkRecentChooserMenuPrivate *priv;
690
691   priv = menu->priv;
692   
693   if (priv->current_filter)
694     g_object_unref (G_OBJECT (priv->current_filter));
695   
696   if (filter)
697     {
698       priv->current_filter = filter;
699       g_object_ref_sink (priv->current_filter);
700     }
701
702   gtk_recent_chooser_menu_populate (menu);
703   
704   g_object_notify (G_OBJECT (menu), "filter");
705 }
706
707 /* taken from libeel/eel-strings.c */
708 static gchar *
709 escape_underscores (const gchar *string)
710 {
711   gint underscores;
712   const gchar *p;
713   gchar *q;
714   gchar *escaped;
715
716   if (!string)
717     return NULL;
718         
719   underscores = 0;
720   for (p = string; *p != '\0'; p++)
721     underscores += (*p == '_');
722
723   if (underscores == 0)
724     return g_strdup (string);
725
726   escaped = g_new (char, strlen (string) + underscores + 1);
727   for (p = string, q = escaped; *p != '\0'; p++, q++)
728     {
729       /* Add an extra underscore. */
730       if (*p == '_')
731         *q++ = '_';
732       
733       *q = *p;
734     }
735   
736   *q = '\0';
737         
738   return escaped;
739 }
740
741 static void
742 gtk_recent_chooser_menu_add_tip (GtkRecentChooserMenu *menu,
743                                  GtkRecentInfo        *info,
744                                  GtkWidget            *item)
745 {
746   GtkRecentChooserMenuPrivate *priv;
747   gchar *path, *tip_text;
748
749   g_assert (info != NULL);
750   g_assert (item != NULL);
751
752   priv = menu->priv;
753   
754   if (!priv->tooltips)
755     return;
756   
757   path = gtk_recent_info_get_uri_display (info);
758   if (path)
759     {
760       tip_text = g_strdup_printf (_("Open '%s'"), path);
761
762       gtk_tooltips_set_tip (priv->tooltips,
763                             item,
764                             tip_text,
765                             NULL);
766
767       g_free (path);
768       g_free (tip_text);
769     }
770 }
771
772 static GtkWidget *
773 gtk_recent_chooser_menu_create_item (GtkRecentChooserMenu *menu,
774                                      GtkRecentInfo        *info,
775                                      gint                  count)
776 {
777   GtkRecentChooserMenuPrivate *priv;
778   gchar *text;
779   GtkWidget *item, *image, *label;
780   GdkPixbuf *icon;
781
782   g_assert (info != NULL);
783
784   priv = menu->priv;
785
786   if (priv->show_numbers)
787     {
788       gchar *name, *escaped;
789       
790       name = g_strdup (gtk_recent_info_get_display_name (info));
791       if (!name)
792         name = g_strdup (_("Unknown item"));
793       
794       escaped = escape_underscores (name);
795       
796       /* avoid clashing mnemonics */
797       if (count <= 10)
798         text = g_strdup_printf ("_%d. %s", count, escaped);
799       else
800         text = g_strdup_printf ("%d. %s", count, escaped);
801       
802       item = gtk_image_menu_item_new_with_mnemonic (text);
803       
804       g_free (escaped);
805       g_free (name);
806     }
807   else
808     {
809       text = g_strdup (gtk_recent_info_get_display_name (info));
810       item = gtk_image_menu_item_new_with_label (text);
811     }
812
813   g_free (text);
814
815   /* ellipsize the menu item label, in case the recent document
816    * display name is huge.
817    */
818   label = GTK_BIN (item)->child;
819   if (GTK_IS_LABEL (label))
820     {
821       gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
822       gtk_label_set_max_width_chars (GTK_LABEL (label), priv->label_width);
823     }
824   
825   if (priv->show_icons)
826     {
827       icon = gtk_recent_info_get_icon (info, priv->icon_size);
828         
829       image = gtk_image_new_from_pixbuf (icon);
830       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
831       g_object_unref (icon);
832     }
833
834   g_signal_connect (item, "activate",
835                     G_CALLBACK (item_activate_cb),
836                     menu);
837
838   return item;
839 }
840
841 static void
842 gtk_recent_chooser_menu_insert_item (GtkRecentChooserMenu *menu,
843                                      GtkWidget            *menuitem,
844                                      gint                  position)
845 {
846   GtkRecentChooserMenuPrivate *priv = menu->priv;
847   gint real_position;
848
849   if (priv->first_recent_item_pos == -1)
850     {
851       GList *children, *l;
852
853       children = gtk_container_get_children (GTK_CONTAINER (menu));
854
855       for (real_position = 0, l = children;
856            l != NULL;
857            real_position += 1, l = l->next)
858         {
859           GObject *child = l->data;
860           gboolean is_placeholder = FALSE;
861
862           is_placeholder =
863             GPOINTER_TO_INT (g_object_get_data (child, "gtk-recent-menu-placeholder"));
864
865           if (is_placeholder)
866             break;
867         }
868
869       g_list_free (children);
870       priv->first_recent_item_pos = real_position;
871     }
872   else
873     real_position = priv->first_recent_item_pos;
874
875   gtk_menu_shell_insert (GTK_MENU_SHELL (menu), menuitem,
876                          real_position + position);
877   gtk_widget_show (menuitem);
878 }
879
880 /* removes the items we own from the menu */
881 static void
882 gtk_recent_chooser_menu_dispose_items (GtkRecentChooserMenu *menu)
883 {
884   GList *children, *l;
885  
886   children = gtk_container_get_children (GTK_CONTAINER (menu));
887   for (l = children; l != NULL; l = l->next)
888     {
889       GtkWidget *menu_item = GTK_WIDGET (l->data);
890       gboolean has_mark = FALSE;
891       
892       /* check for our mark, in order to remove just the items we own */
893       has_mark =
894         GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item), "gtk-recent-menu-mark"));
895
896       if (has_mark)
897         {
898           GtkRecentInfo *info;
899           
900           /* destroy the attached RecentInfo struct, if found */
901           info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
902           if (info)
903             g_object_set_data_full (G_OBJECT (menu_item), "gtk-recent-info",
904                                     NULL, NULL);
905           
906           /* and finally remove the item from the menu */
907           gtk_container_remove (GTK_CONTAINER (menu), menu_item);
908         }
909     }
910
911   /* recalculate the position of the first recent item */
912   menu->priv->first_recent_item_pos = -1;
913
914   g_list_free (children);
915 }
916
917 typedef struct
918 {
919   GList *items;
920   gint n_items;
921   gint loaded_items;
922   gint displayed_items;
923   GtkRecentChooserMenu *menu;
924   GtkWidget *placeholder;
925 } MenuPopulateData;
926
927 static gboolean
928 idle_populate_func (gpointer data)
929 {
930   MenuPopulateData *pdata;
931   GtkRecentChooserMenuPrivate *priv;
932   GtkRecentInfo *info;
933   gboolean retval;
934   GtkWidget *item;
935
936   pdata = (MenuPopulateData *) data;
937   priv = pdata->menu->priv;
938
939   priv->populate_id = 0;
940
941   if (!pdata->items)
942     {
943       pdata->items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (pdata->menu));
944       if (!pdata->items)
945         {
946           /* show the placeholder here */
947           gtk_widget_show (pdata->placeholder);
948           pdata->displayed_items = 1;
949
950           return FALSE;
951         }
952       
953       pdata->n_items = g_list_length (pdata->items);
954       pdata->loaded_items = 0;
955     }
956
957   info = g_list_nth_data (pdata->items, pdata->loaded_items);
958   item = gtk_recent_chooser_menu_create_item (pdata->menu,
959                                               info,
960                                               pdata->displayed_items);
961   if (!item)
962     goto check_and_return;
963       
964   gtk_recent_chooser_menu_add_tip (pdata->menu, info, item);
965   gtk_recent_chooser_menu_insert_item (pdata->menu, item,
966                                        pdata->displayed_items);
967   
968   pdata->displayed_items += 1;
969       
970   /* mark the menu item as one of our own */
971   g_object_set_data (G_OBJECT (item),
972                      "gtk-recent-menu-mark",
973                      GINT_TO_POINTER (TRUE));
974       
975   /* attach the RecentInfo object to the menu item, and own a reference
976    * to it, so that it will be destroyed with the menu item when it's
977    * not needed anymore.
978    */
979   g_object_set_data_full (G_OBJECT (item), "gtk-recent-info",
980                           gtk_recent_info_ref (info),
981                           (GDestroyNotify) gtk_recent_info_unref);
982   
983 check_and_return:
984   pdata->loaded_items += 1;
985
986   if (pdata->loaded_items == pdata->n_items)
987     {
988       g_list_foreach (pdata->items, (GFunc) gtk_recent_info_unref, NULL);
989       g_list_free (pdata->items);
990
991       retval = FALSE;
992     }
993   else
994     retval = TRUE;
995
996   return retval;
997 }
998
999 static void
1000 idle_populate_clean_up (gpointer data)
1001 {
1002   MenuPopulateData *pdata = data;
1003
1004   /* show the placeholder in case no item survived
1005    * the filtering process in the idle loop
1006    */
1007   if (!pdata->displayed_items)
1008     gtk_widget_show (pdata->placeholder);
1009   g_object_unref (pdata->placeholder);
1010
1011   g_slice_free (MenuPopulateData, data);
1012 }
1013
1014 static void
1015 gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu)
1016 {
1017   MenuPopulateData *pdata;
1018   GtkRecentChooserMenuPrivate *priv = menu->priv;
1019
1020   if (menu->priv->populate_id)
1021     return;
1022
1023   pdata = g_slice_new (MenuPopulateData);
1024   pdata->items = NULL;
1025   pdata->n_items = 0;
1026   pdata->loaded_items = 0;
1027   pdata->displayed_items = 0;
1028   pdata->menu = menu;
1029   pdata->placeholder = g_object_ref (priv->placeholder);
1030
1031   priv->icon_size = get_icon_size_for_widget (GTK_WIDGET (menu));
1032   
1033   /* remove our menu items first and hide the placeholder */
1034   gtk_recent_chooser_menu_dispose_items (menu);
1035   gtk_widget_hide (priv->placeholder);
1036   
1037   priv->populate_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
1038                                                  idle_populate_func,
1039                                                  pdata,
1040                                                  idle_populate_clean_up);
1041 }
1042
1043 /* bounce activate signal from the recent menu item widget 
1044  * to the recent menu widget
1045  */
1046 static void
1047 item_activate_cb (GtkWidget *widget,
1048                   gpointer   user_data)
1049 {
1050   GtkRecentChooser *chooser = GTK_RECENT_CHOOSER (user_data);
1051   
1052   _gtk_recent_chooser_item_activated (chooser);
1053 }
1054
1055 /* we force a redraw if the manager changes when we are showing */
1056 static void
1057 manager_changed_cb (GtkRecentManager *manager,
1058                     gpointer          user_data)
1059 {
1060   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (user_data);
1061
1062   gtk_recent_chooser_menu_populate (menu);
1063 }
1064
1065 static void
1066 set_recent_manager (GtkRecentChooserMenu *menu,
1067                     GtkRecentManager     *manager)
1068 {
1069   GtkRecentChooserMenuPrivate *priv = menu->priv;
1070
1071   if (priv->manager)
1072     {
1073       if (priv->manager_changed_id)
1074         {
1075           g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
1076           priv->manager_changed_id = 0;
1077         }
1078
1079       if (priv->populate_id)
1080         {
1081           g_source_remove (priv->populate_id);
1082           priv->populate_id = 0;
1083         }
1084
1085       priv->manager = NULL;
1086     }
1087   
1088   if (manager)
1089     priv->manager = manager;
1090   else
1091     priv->manager = gtk_recent_manager_get_default ();
1092   
1093   if (priv->manager)
1094     priv->manager_changed_id = g_signal_connect (priv->manager, "changed",
1095                                                  G_CALLBACK (manager_changed_cb),
1096                                                  menu);
1097 }
1098
1099 static gint
1100 get_icon_size_for_widget (GtkWidget *widget)
1101 {
1102   GtkSettings *settings;
1103   gint width, height;
1104
1105   if (gtk_widget_has_screen (widget))
1106     settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
1107   else
1108     settings = gtk_settings_get_default ();
1109
1110   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1111                                          &width, &height))
1112     return MAX (width, height);
1113
1114   return FALLBACK_ICON_SIZE;
1115 }
1116
1117 static void
1118 gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
1119                                        gboolean              show_tips)
1120 {
1121   if (menu->priv->show_tips == show_tips)
1122     return;
1123   
1124   g_assert (menu->priv->tooltips != NULL);
1125   
1126   if (show_tips)
1127     gtk_tooltips_enable (menu->priv->tooltips);
1128   else
1129     gtk_tooltips_disable (menu->priv->tooltips);
1130   
1131   menu->priv->show_tips = show_tips;
1132 }
1133
1134 /*
1135  * Public API
1136  */
1137
1138 /**
1139  * gtk_recent_chooser_menu_new:
1140  *
1141  * Creates a new #GtkRecentChooserMenu widget.
1142  *
1143  * This kind of widget shows the list of recently used resources as
1144  * a menu, each item as a menu item.  Each item inside the menu might
1145  * have an icon, representing its MIME type, and a number, for mnemonic
1146  * access.
1147  *
1148  * This widget implements the #GtkRecentChooser interface.
1149  *
1150  * This widget creates its own #GtkRecentManager object.  See the
1151  * gtk_recent_chooser_menu_new_for_manager() function to know how to create
1152  * a #GtkRecentChooserMenu widget bound to another #GtkRecentManager object.
1153  *
1154  * Return value: a new #GtkRecentChooserMenu
1155  *
1156  * Since: 2.10
1157  */
1158 GtkWidget *
1159 gtk_recent_chooser_menu_new (void)
1160 {
1161   return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1162                        "recent-manager", NULL,
1163                        NULL);
1164 }
1165
1166 /**
1167  * gtk_recent_chooser_menu_new_for_manager:
1168  * @manager: a #GtkRecentManager
1169  *
1170  * Creates a new #GtkRecentChooserMenu widget using @manager as
1171  * the underlying recently used resources manager.
1172  *
1173  * This is useful if you have implemented your own recent manager,
1174  * or if you have a customized instance of a #GtkRecentManager
1175  * object or if you wish to share a common #GtkRecentManager object
1176  * among multiple #GtkRecentChooser widgets.
1177  *
1178  * Return value: a new #GtkRecentChooserMenu, bound to @manager.
1179  *
1180  * Since: 2.10
1181  */
1182 GtkWidget *
1183 gtk_recent_chooser_menu_new_for_manager (GtkRecentManager *manager)
1184 {
1185   g_return_val_if_fail (manager == NULL || GTK_IS_RECENT_MANAGER (manager), NULL);
1186   
1187   return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1188                        "recent-manager", manager,
1189                        NULL);
1190 }
1191
1192 /**
1193  * gtk_recent_chooser_menu_get_show_numbers:
1194  * @menu: a #GtkRecentChooserMenu
1195  *
1196  * Returns the value set by gtk_recent_chooser_menu_set_show_numbers().
1197  * 
1198  * Return value: %TRUE if numbers should be shown.
1199  *
1200  * Since: 2.10
1201  */
1202 gboolean
1203 gtk_recent_chooser_menu_get_show_numbers (GtkRecentChooserMenu *menu)
1204 {
1205   g_return_val_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu), FALSE);
1206
1207   return menu->priv->show_numbers;
1208 }
1209
1210 /**
1211  * gtk_recent_chooser_menu_set_show_numbers:
1212  * @menu: a #GtkRecentChooserMenu
1213  * @show_numbers: whether to show numbers
1214  *
1215  * Sets whether a number should be added to the items of @menu.  The
1216  * numbers are shown to provide a unique character for a mnemonic to
1217  * be used inside ten menu item's label.  Only the first the items
1218  * get a number to avoid clashes.
1219  *
1220  * Since: 2.10
1221  */
1222 void
1223 gtk_recent_chooser_menu_set_show_numbers (GtkRecentChooserMenu *menu,
1224                                           gboolean              show_numbers)
1225 {
1226   g_return_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu));
1227
1228   if (menu->priv->show_numbers == show_numbers)
1229     return;
1230
1231   menu->priv->show_numbers = show_numbers;
1232   g_object_notify (G_OBJECT (menu), "show-numbers");
1233 }
1234
1235 #define __GTK_RECENT_CHOOSER_MENU_C__
1236 #include "gtkaliasdef.c"