]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentchoosermenu.c
Provide a fast path returning NULL when the limit is set to 0.
[~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 #include "gtkalias.h"
49
50 struct _GtkRecentChooserMenuPrivate
51 {
52   /* the recent manager object */
53   GtkRecentManager *manager;
54   
55   /* size of the icons of the menu items */  
56   gint icon_size;
57
58   /* RecentChooser properties */
59   gint limit;  
60   guint show_private : 1;
61   guint show_not_found : 1;
62   guint show_tips : 1;
63   guint show_icons : 1;
64   guint local_only : 1;
65   
66   guint show_numbers : 1;
67   
68   GtkRecentSortType sort_type;
69   GtkRecentSortFunc sort_func;
70   gpointer sort_data;
71   GDestroyNotify sort_data_destroy;
72   
73   GSList *filters;
74   GtkRecentFilter *current_filter;
75  
76   guint local_manager : 1;
77   gulong manager_changed_id;
78
79   gulong populate_id;
80
81   /* tooltips for our bookmark items*/
82   GtkTooltips *tooltips;
83 };
84
85 enum {
86   PROP_0,
87
88   PROP_SHOW_NUMBERS
89 };
90
91 #define FALLBACK_ICON_SIZE      32
92 #define FALLBACK_ITEM_LIMIT     10
93
94 #define GTK_RECENT_CHOOSER_MENU_GET_PRIVATE(obj)        (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RECENT_CHOOSER_MENU, GtkRecentChooserMenuPrivate))
95
96 static void     gtk_recent_chooser_menu_finalize    (GObject                   *object);
97 static GObject *gtk_recent_chooser_menu_constructor (GType                      type,
98                                                      guint                      n_construct_properties,
99                                                      GObjectConstructParam     *construct_params);
100
101 static void gtk_recent_chooser_iface_init      (GtkRecentChooserIface     *iface);
102
103 static void gtk_recent_chooser_menu_set_property (GObject      *object,
104                                                   guint         prop_id,
105                                                   const GValue *value,
106                                                   GParamSpec   *pspec);
107 static void gtk_recent_chooser_menu_get_property (GObject      *object,
108                                                   guint         prop_id,
109                                                   GValue       *value,
110                                                   GParamSpec   *pspec);
111
112 static gboolean          gtk_recent_chooser_menu_set_current_uri    (GtkRecentChooser  *chooser,
113                                                                      const gchar       *uri,
114                                                                      GError           **error);
115 static gchar *           gtk_recent_chooser_menu_get_current_uri    (GtkRecentChooser  *chooser);
116 static gboolean          gtk_recent_chooser_menu_select_uri         (GtkRecentChooser  *chooser,
117                                                                      const gchar       *uri,
118                                                                      GError           **error);
119 static void              gtk_recent_chooser_menu_unselect_uri       (GtkRecentChooser  *chooser,
120                                                                      const gchar       *uri);
121 static void              gtk_recent_chooser_menu_select_all         (GtkRecentChooser  *chooser);
122 static void              gtk_recent_chooser_menu_unselect_all       (GtkRecentChooser  *chooser);
123 static GList *           gtk_recent_chooser_menu_get_items          (GtkRecentChooser  *chooser);
124 static GtkRecentManager *gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser  *chooser);
125 static void              gtk_recent_chooser_menu_set_sort_func      (GtkRecentChooser  *chooser,
126                                                                      GtkRecentSortFunc  sort_func,
127                                                                      gpointer           sort_data,
128                                                                      GDestroyNotify     data_destroy);
129 static void              gtk_recent_chooser_menu_add_filter         (GtkRecentChooser  *chooser,
130                                                                      GtkRecentFilter   *filter);
131 static void              gtk_recent_chooser_menu_remove_filter      (GtkRecentChooser  *chooser,
132                                                                      GtkRecentFilter   *filter);
133 static GSList *          gtk_recent_chooser_menu_list_filters       (GtkRecentChooser  *chooser);
134 static void              gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
135                                                                      GtkRecentFilter      *filter);
136
137 static void              gtk_recent_chooser_menu_populate           (GtkRecentChooserMenu *menu);
138 static void              gtk_recent_chooser_menu_set_show_tips      (GtkRecentChooserMenu *menu,
139                                                                      gboolean              show_tips);
140
141 static void     set_recent_manager (GtkRecentChooserMenu *menu,
142                                     GtkRecentManager     *manager);
143
144 static void     chooser_set_sort_type (GtkRecentChooserMenu *menu,
145                                        GtkRecentSortType     sort_type);
146
147 static gint     get_icon_size_for_widget (GtkWidget *widget);
148
149 static void     item_activate_cb   (GtkWidget        *widget,
150                                     gpointer          user_data);
151 static void     manager_changed_cb (GtkRecentManager *manager,
152                                     gpointer          user_data);
153
154 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserMenu,
155                          gtk_recent_chooser_menu,
156                          GTK_TYPE_MENU,
157                          G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
158                                                 gtk_recent_chooser_iface_init))
159
160
161 static void
162 gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface)
163 {
164   iface->set_current_uri = gtk_recent_chooser_menu_set_current_uri;
165   iface->get_current_uri = gtk_recent_chooser_menu_get_current_uri;
166   iface->select_uri = gtk_recent_chooser_menu_select_uri;
167   iface->unselect_uri = gtk_recent_chooser_menu_unselect_uri;
168   iface->select_all = gtk_recent_chooser_menu_select_all;
169   iface->unselect_all = gtk_recent_chooser_menu_unselect_all;
170   iface->get_items = gtk_recent_chooser_menu_get_items;
171   iface->get_recent_manager = gtk_recent_chooser_menu_get_recent_manager;
172   iface->set_sort_func = gtk_recent_chooser_menu_set_sort_func;
173   iface->add_filter = gtk_recent_chooser_menu_add_filter;
174   iface->remove_filter = gtk_recent_chooser_menu_remove_filter;
175   iface->list_filters = gtk_recent_chooser_menu_list_filters;
176 }
177
178 static void
179 gtk_recent_chooser_menu_class_init (GtkRecentChooserMenuClass *klass)
180 {
181   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
182
183   gobject_class->constructor = gtk_recent_chooser_menu_constructor;
184   gobject_class->finalize = gtk_recent_chooser_menu_finalize;
185   gobject_class->set_property = gtk_recent_chooser_menu_set_property;
186   gobject_class->get_property = gtk_recent_chooser_menu_get_property;
187
188   _gtk_recent_chooser_install_properties (gobject_class);
189
190   /**
191    * GtkRecentChooserMenu:show-numbers
192    *
193    * Whether the first ten items in the menu should be prepended by
194    * a number acting as a unique mnemonic.
195    *
196    * Since: 2.10
197    */
198   g_object_class_install_property (gobject_class,
199                                    PROP_SHOW_NUMBERS,
200                                    g_param_spec_boolean ("show-numbers",
201                                                          P_("Show Numbers"),
202                                                          P_("Whether the items should be displayed with a number"),
203                                                          FALSE,
204                                                          G_PARAM_READWRITE));
205   
206   g_type_class_add_private (klass, sizeof (GtkRecentChooserMenuPrivate));
207 }
208
209 static void
210 gtk_recent_chooser_menu_init (GtkRecentChooserMenu *menu)
211 {
212   GtkRecentChooserMenuPrivate *priv;
213   
214   priv = GTK_RECENT_CHOOSER_MENU_GET_PRIVATE (menu);
215   
216   menu->priv = priv;
217   
218   priv->show_icons= TRUE;
219   priv->show_numbers = FALSE;
220   priv->show_tips = FALSE;
221   priv->show_not_found = TRUE;
222   priv->show_private = FALSE;
223   priv->local_only = TRUE;
224   
225   priv->limit = FALLBACK_ITEM_LIMIT;
226
227   priv->sort_type = GTK_RECENT_SORT_NONE;
228   
229   priv->icon_size = FALLBACK_ICON_SIZE;
230   
231   priv->current_filter = NULL;
232     
233   priv->tooltips = gtk_tooltips_new ();
234   g_object_ref_sink (priv->tooltips);
235 }
236
237 static void
238 gtk_recent_chooser_menu_finalize (GObject *object)
239 {
240   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
241   GtkRecentChooserMenuPrivate *priv = menu->priv;
242   
243   g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
244   priv->manager_changed_id = 0;
245
246   if (priv->populate_id)
247     g_source_remove (priv->populate_id);
248
249   priv->manager = NULL;
250   
251   if (priv->sort_data_destroy)
252     {
253       priv->sort_data_destroy (priv->sort_data);
254       
255       priv->sort_data_destroy = NULL;
256       priv->sort_data = NULL;
257       priv->sort_func = NULL;
258     }
259   
260   if (priv->tooltips)
261     g_object_unref (priv->tooltips);
262   
263   if (priv->current_filter)
264     g_object_unref (priv->current_filter);
265   
266   G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->finalize (object);
267 }
268
269 static GObject *
270 gtk_recent_chooser_menu_constructor (GType                  type,
271                                      guint                  n_construct_properties,
272                                      GObjectConstructParam *construct_params)
273 {
274   GtkRecentChooserMenu *menu;
275   GObject *object;
276   
277   object = G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->constructor (type,
278                                                                                n_construct_properties,
279                                                                                construct_params);
280   menu = GTK_RECENT_CHOOSER_MENU (object);
281   
282   g_assert (menu->priv->manager);
283   
284   return object;
285 }
286
287 static void
288 gtk_recent_chooser_menu_set_property (GObject      *object,
289                                       guint         prop_id,
290                                       const GValue *value,
291                                       GParamSpec   *pspec)
292 {
293   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
294   
295   switch (prop_id)
296     {
297     case PROP_SHOW_NUMBERS:
298       menu->priv->show_numbers = g_value_get_boolean (value);
299       break;
300     case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
301       set_recent_manager (menu, g_value_get_object (value));
302       break;
303     case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
304       menu->priv->show_private = g_value_get_boolean (value);
305       break;
306     case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
307       menu->priv->show_not_found = g_value_get_boolean (value);
308       break;
309     case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
310       gtk_recent_chooser_menu_set_show_tips (menu, g_value_get_boolean (value));
311       break;
312     case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
313       menu->priv->show_icons = g_value_get_boolean (value);
314       break;
315     case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
316       g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
317                  G_STRFUNC,
318                  G_OBJECT_TYPE_NAME (object));
319       break;
320     case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
321       menu->priv->local_only = g_value_get_boolean (value);
322       break;
323     case GTK_RECENT_CHOOSER_PROP_LIMIT:
324       menu->priv->limit = g_value_get_int (value);
325       break;
326     case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
327       chooser_set_sort_type (menu, g_value_get_enum (value));
328       break;
329     case GTK_RECENT_CHOOSER_PROP_FILTER:
330       gtk_recent_chooser_menu_set_current_filter (menu, g_value_get_object (value));
331       break;
332     default:
333       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
334       break;
335     }
336 }
337
338 static void
339 gtk_recent_chooser_menu_get_property (GObject    *object,
340                                       guint       prop_id,
341                                       GValue     *value,
342                                       GParamSpec *pspec)
343 {
344   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
345   
346   switch (prop_id)
347     {
348     case PROP_SHOW_NUMBERS:
349       g_value_set_boolean (value, menu->priv->show_numbers);
350       break;
351     case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
352       g_value_set_boolean (value, menu->priv->show_tips);
353       break;
354     case GTK_RECENT_CHOOSER_PROP_LIMIT:
355       g_value_set_int (value, menu->priv->limit);
356       break;
357     case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
358       g_value_set_boolean (value, menu->priv->local_only);
359       break;
360     case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
361       g_value_set_enum (value, menu->priv->sort_type);
362       break;
363     case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
364       g_value_set_boolean (value, menu->priv->show_private);
365       break;
366     case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
367       g_value_set_boolean (value, menu->priv->show_not_found);
368       break;
369     case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
370       g_value_set_boolean (value, menu->priv->show_icons);
371       break;
372     case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
373       g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
374                  G_STRFUNC,
375                  G_OBJECT_TYPE_NAME (object));
376       break;
377     case GTK_RECENT_CHOOSER_PROP_FILTER:
378       g_value_set_object (value, menu->priv->current_filter);
379       break;
380     default:
381       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
382       break;
383     }
384 }
385
386 static gboolean
387 gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser  *chooser,
388                                          const gchar       *uri,
389                                          GError           **error)
390 {
391   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
392   GList *children, *l;
393   GtkWidget *menu_item = NULL;
394   gboolean found = FALSE;
395   
396   children = gtk_container_get_children (GTK_CONTAINER (menu));
397   for (l = children; l != NULL; l = l->next)
398     {
399       GtkRecentInfo *info;
400       
401       menu_item = GTK_WIDGET (l->data);
402       
403       info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
404       if (!info)
405         continue;
406       
407       if (strcmp (uri, gtk_recent_info_get_uri (info)) == 0)
408         {
409           gtk_menu_shell_activate_item (GTK_MENU_SHELL (menu),
410                                         menu_item,
411                                         TRUE);
412           found = TRUE;
413
414           break;
415         }
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     }
427   
428   return found;
429 }
430
431 static gchar *
432 gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser  *chooser)
433 {
434   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
435   GtkWidget *menu_item;
436   GtkRecentInfo *info;
437   
438   menu_item = gtk_menu_get_active (GTK_MENU (menu));
439   if (!menu_item)
440     return NULL;
441   
442   info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
443   if (!info)
444     return NULL;
445   
446   return g_strdup (gtk_recent_info_get_uri (info));
447 }
448
449 static gboolean
450 gtk_recent_chooser_menu_select_uri (GtkRecentChooser  *chooser,
451                                     const gchar       *uri,
452                                     GError           **error)
453 {
454   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
455   GList *children, *l;
456   GtkWidget *menu_item = NULL;
457   gboolean found = FALSE;
458   
459   children = gtk_container_get_children (GTK_CONTAINER (menu));
460   for (l = children; l != NULL; l = l->next)
461     {
462       GtkRecentInfo *info;
463       
464       menu_item = GTK_WIDGET (l->data);
465       
466       info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
467       if (!info)
468         continue;
469       
470       if (0 == strcmp (uri, gtk_recent_info_get_uri (info)))
471         found = TRUE;
472     }
473
474   g_list_free (children);
475   
476   if (!found)  
477     {
478       g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
479                    GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
480                    _("No recently used resource found with URI `%s'"),
481                    uri);
482       return FALSE;
483     }
484   else
485     {
486       gtk_menu_shell_select_item (GTK_MENU_SHELL (menu), menu_item);
487
488       return TRUE;
489     }
490 }
491
492 static void
493 gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
494                                        const gchar     *uri)
495 {
496   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
497   
498   gtk_menu_shell_deselect (GTK_MENU_SHELL (menu));
499 }
500
501 static void
502 gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser)
503 {
504   g_warning (_("This function is not implemented for "
505                "widgets of class '%s'"),
506              g_type_name (G_OBJECT_TYPE (chooser)));
507 }
508
509 static void
510 gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser)
511 {
512   g_warning (_("This function is not implemented for "
513                "widgets of class '%s'"),
514              g_type_name (G_OBJECT_TYPE (chooser)));
515 }
516
517 static void
518 gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser  *chooser,
519                                        GtkRecentSortFunc  sort_func,
520                                        gpointer           sort_data,
521                                        GDestroyNotify     data_destroy)
522 {
523   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
524   GtkRecentChooserMenuPrivate *priv = menu->priv;
525   
526   if (priv->sort_data_destroy)
527     {
528       priv->sort_data_destroy (priv->sort_data);
529       
530       priv->sort_func = NULL;
531       priv->sort_data = NULL;
532       priv->sort_data_destroy = NULL;
533     }
534   
535   if (sort_func)
536     {
537       priv->sort_func = sort_func;
538       priv->sort_data = sort_data;
539       priv->sort_data_destroy = data_destroy;
540     }
541 }
542
543 static gint
544 sort_recent_items_mru (GtkRecentInfo *a,
545                        GtkRecentInfo *b,
546                        gpointer       unused)
547 {
548   g_assert (a != NULL && b != NULL);
549   
550   return (gtk_recent_info_get_modified (a) < gtk_recent_info_get_modified (b));
551 }
552
553 static gint
554 sort_recent_items_lru (GtkRecentInfo *a,
555                        GtkRecentInfo *b,
556                        gpointer       unused)
557 {
558   g_assert (a != NULL && b != NULL);
559   
560   return (gtk_recent_info_get_modified (a) > gtk_recent_info_get_modified (b));
561 }
562
563 /* our proxy sorting function */
564 static gint
565 sort_recent_items_proxy (gpointer *a,
566                          gpointer *b,
567                          gpointer  user_data)
568 {
569   GtkRecentInfo *info_a = (GtkRecentInfo *) a;
570   GtkRecentInfo *info_b = (GtkRecentInfo *) b;
571   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (user_data);
572
573   if (menu->priv->sort_func)
574     return (* menu->priv->sort_func) (info_a,
575                                       info_b,
576                                       menu->priv->sort_data);
577   
578   /* fallback */
579   return 0;
580 }
581
582 static void
583 chooser_set_sort_type (GtkRecentChooserMenu *menu,
584                        GtkRecentSortType     sort_type)
585 {
586   if (menu->priv->sort_type == sort_type)
587     return;
588
589   menu->priv->sort_type = sort_type;
590 }
591
592
593 static GList *
594 gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser)
595 {
596   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
597   GtkRecentChooserMenuPrivate *priv;
598   gint limit;
599   GtkRecentSortType sort_type;
600   GList *items;
601   GCompareDataFunc compare_func;
602   gint length;
603   
604   priv = menu->priv;
605   
606   if (!priv->manager)
607     return NULL;
608   
609   limit = gtk_recent_chooser_get_limit (chooser);
610   if (limit == 0)
611     return NULL;
612
613   sort_type = gtk_recent_chooser_get_sort_type (chooser);
614   switch (sort_type)
615     {
616     case GTK_RECENT_SORT_NONE:
617       compare_func = NULL;
618       break;
619     case GTK_RECENT_SORT_MRU:
620       compare_func = (GCompareDataFunc) sort_recent_items_mru;
621       break;
622     case GTK_RECENT_SORT_LRU:
623       compare_func = (GCompareDataFunc) sort_recent_items_lru;
624       break;
625     case GTK_RECENT_SORT_CUSTOM:
626       compare_func = (GCompareDataFunc) sort_recent_items_proxy;
627       break;
628     default:
629       g_assert_not_reached ();
630       break;
631     }
632   
633   items = gtk_recent_manager_get_items (priv->manager);
634   if (!items)
635     return NULL;
636   
637   if (compare_func)  
638     items = g_list_sort_with_data (items, compare_func, menu);
639  
640   length = g_list_length (items);
641   if ((limit != -1) && (length > limit))
642     {
643       GList *clamp, *l;
644
645       clamp = g_list_nth (items, limit - 1);
646       if (!clamp)
647         return items;
648
649       l = clamp->next;
650       clamp->next = NULL;      
651       
652       g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
653       g_list_free (l);
654     }
655
656   return items;
657 }
658
659 static GtkRecentManager *
660 gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser)
661 {
662   GtkRecentChooserMenuPrivate *priv;
663  
664   priv = GTK_RECENT_CHOOSER_MENU (chooser)->priv;
665   
666   return priv->manager;
667 }
668
669 static void
670 gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
671                                     GtkRecentFilter  *filter)
672 {
673   GtkRecentChooserMenu *menu;
674
675   menu = GTK_RECENT_CHOOSER_MENU (chooser);
676   
677   gtk_recent_chooser_menu_set_current_filter (menu, filter);
678 }
679
680 static void
681 gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
682                                        GtkRecentFilter  *filter)
683 {
684   GtkRecentChooserMenu *menu;
685
686   menu = GTK_RECENT_CHOOSER_MENU (chooser);
687   
688   if (filter == menu->priv->current_filter)
689     {
690       g_object_unref (menu->priv->current_filter);
691       menu->priv->current_filter = NULL;
692
693       g_object_notify (G_OBJECT (menu), "filter");
694     }
695 }
696
697 static GSList *
698 gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser)
699 {
700   GtkRecentChooserMenu *menu;
701   GSList *retval = NULL;
702
703   menu = GTK_RECENT_CHOOSER_MENU (chooser);
704  
705   if (menu->priv->current_filter)
706     retval = g_slist_prepend (retval, menu->priv->current_filter);
707
708   return retval;
709 }
710
711 static void
712 gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
713                                             GtkRecentFilter      *filter)
714 {
715   GtkRecentChooserMenuPrivate *priv;
716
717   priv = menu->priv;
718   
719   if (priv->current_filter)
720     g_object_unref (G_OBJECT (priv->current_filter));
721   
722   if (filter)
723     {
724       priv->current_filter = filter;
725       g_object_ref_sink (priv->current_filter);
726     }
727   
728   g_object_notify (G_OBJECT (menu), "filter");
729 }
730
731 static gboolean
732 get_is_recent_filtered (GtkRecentChooserMenu *menu,
733                         GtkRecentInfo        *info)
734 {
735   GtkRecentChooserMenuPrivate *priv;
736   GtkRecentFilter *current_filter;
737   GtkRecentFilterInfo filter_info;
738   GtkRecentFilterFlags needed;
739   gboolean retval;
740
741   g_assert (info != NULL);
742
743   priv = menu->priv;
744   
745   if (!priv->current_filter)
746     return FALSE;
747   
748   current_filter = priv->current_filter;
749   needed = gtk_recent_filter_get_needed (current_filter);
750   
751   filter_info.contains = GTK_RECENT_FILTER_URI | GTK_RECENT_FILTER_MIME_TYPE;
752   
753   filter_info.uri = gtk_recent_info_get_uri (info);
754   filter_info.mime_type = gtk_recent_info_get_mime_type (info);
755   
756   if (needed & GTK_RECENT_FILTER_DISPLAY_NAME)
757     {
758       filter_info.display_name = gtk_recent_info_get_display_name (info);
759       filter_info.contains |= GTK_RECENT_FILTER_DISPLAY_NAME;
760     }
761   else
762     filter_info.uri = NULL;
763   
764   if (needed & GTK_RECENT_FILTER_APPLICATION)
765     {
766       filter_info.applications = (const gchar **) gtk_recent_info_get_applications (info, NULL);
767       filter_info.contains |= GTK_RECENT_FILTER_APPLICATION;
768     }
769   else
770     filter_info.applications = NULL;
771
772   if (needed & GTK_RECENT_FILTER_GROUP)
773     {
774       filter_info.groups = (const gchar **) gtk_recent_info_get_groups (info, NULL);
775       filter_info.contains |= GTK_RECENT_FILTER_GROUP;
776     }
777   else
778     filter_info.groups = NULL;
779   
780   if (needed & GTK_RECENT_FILTER_AGE)
781     {
782       filter_info.age = gtk_recent_info_get_age (info);
783       filter_info.contains |= GTK_RECENT_FILTER_AGE;
784     }
785   else
786     filter_info.age = -1;
787   
788   retval = gtk_recent_filter_filter (current_filter, &filter_info);
789   
790   /* this we own */
791   if (filter_info.applications)
792     g_strfreev ((gchar **) filter_info.applications);
793   if (filter_info.groups)
794     g_strfreev ((gchar **) filter_info.groups);
795   
796   return !retval;
797 }
798
799 /* taken from libeel/eel-strings.c */
800 static gchar *
801 escape_underscores (const gchar *string)
802 {
803   gint underscores;
804   const gchar *p;
805   gchar *q;
806   gchar *escaped;
807
808   if (!string)
809     return NULL;
810         
811   underscores = 0;
812   for (p = string; *p != '\0'; p++)
813     underscores += (*p == '_');
814
815   if (underscores == 0)
816     return g_strdup (string);
817
818   escaped = g_new (char, strlen (string) + underscores + 1);
819   for (p = string, q = escaped; *p != '\0'; p++, q++)
820     {
821       /* Add an extra underscore. */
822       if (*p == '_')
823         *q++ = '_';
824       
825       *q = *p;
826     }
827   
828   *q = '\0';
829         
830   return escaped;
831 }
832
833 static void
834 gtk_recent_chooser_menu_add_tip (GtkRecentChooserMenu *menu,
835                                  GtkRecentInfo        *info,
836                                  GtkWidget            *item)
837 {
838   GtkRecentChooserMenuPrivate *priv;
839   gchar *path, *tip_text;
840
841   g_assert (info != NULL);
842   g_assert (item != NULL);
843
844   priv = menu->priv;
845   
846   if (!priv->tooltips)
847     return;
848   
849   path = gtk_recent_info_get_uri_display (info);
850   
851   tip_text = g_strdup_printf (_("Open '%s'"), path);
852  
853   gtk_tooltips_set_tip (priv->tooltips,
854                         item,
855                         tip_text,
856                         NULL);
857
858   g_free (path);  
859   g_free (tip_text);
860 }
861
862 static GtkWidget *
863 gtk_recent_chooser_menu_create_item (GtkRecentChooserMenu *menu,
864                                      GtkRecentInfo        *info,
865                                      gint                  count)
866 {
867   GtkRecentChooserMenuPrivate *priv;
868   gchar *label;
869   GtkWidget *item, *image;
870   GdkPixbuf *icon;
871
872   g_assert (info != NULL);
873
874   priv = menu->priv;
875
876   if (priv->show_numbers)
877     {
878       gchar *name, *escaped;
879       
880       name = g_strdup (gtk_recent_info_get_display_name (info));
881       if (!name)
882         name = g_strdup (_("Unknown item"));
883       
884       escaped = escape_underscores (name);
885       
886       /* avoid clashing mnemonics */
887       if (count <= 10)
888         label = g_strdup_printf ("_%d. %s", count, escaped);
889       else
890         label = g_strdup_printf ("%d. %s", count, escaped);
891       
892       item = gtk_image_menu_item_new_with_mnemonic (label);
893       
894       g_free (escaped);
895       g_free (name);
896     }
897   else
898     {
899       label = g_strdup (gtk_recent_info_get_display_name (info));
900       item = gtk_image_menu_item_new_with_label (label);
901     }
902   
903   if (priv->show_icons)
904     {
905       icon = gtk_recent_info_get_icon (info, priv->icon_size);
906         
907       image = gtk_image_new_from_pixbuf (icon);
908       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
909     }
910
911   g_signal_connect (item, "activate",
912                     G_CALLBACK (item_activate_cb),
913                     menu);
914
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"