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