]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentchoosermenu.c
9ad1cb10b4eb66648bd7c3fd1dcbabee0b34d7a1
[~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 (strcmp (uri, gtk_recent_info_get_uri (info)) == 0)
415         {
416           gtk_menu_shell_activate_item (GTK_MENU_SHELL (menu),
417                                         menu_item,
418                                         TRUE);
419           found = TRUE;
420
421           break;
422         }
423     }
424
425   g_list_free (children);
426   
427   if (!found)  
428     {
429       g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
430                    GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
431                    _("No recently used resource found with URI `%s'"),
432                    uri);
433     }
434   
435   return found;
436 }
437
438 static gchar *
439 gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser  *chooser)
440 {
441   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
442   GtkWidget *menu_item;
443   GtkRecentInfo *info;
444   
445   menu_item = gtk_menu_get_active (GTK_MENU (menu));
446   if (!menu_item)
447     return NULL;
448   
449   info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
450   if (!info)
451     return NULL;
452   
453   return g_strdup (gtk_recent_info_get_uri (info));
454 }
455
456 static gboolean
457 gtk_recent_chooser_menu_select_uri (GtkRecentChooser  *chooser,
458                                     const gchar       *uri,
459                                     GError           **error)
460 {
461   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
462   GList *children, *l;
463   GtkWidget *menu_item = NULL;
464   gboolean found = FALSE;
465   
466   children = gtk_container_get_children (GTK_CONTAINER (menu));
467   for (l = children; l != NULL; l = l->next)
468     {
469       GtkRecentInfo *info;
470       
471       menu_item = GTK_WIDGET (l->data);
472       
473       info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
474       if (!info)
475         continue;
476       
477       if (0 == strcmp (uri, gtk_recent_info_get_uri (info)))
478         found = TRUE;
479     }
480
481   g_list_free (children);
482   
483   if (!found)  
484     {
485       g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
486                    GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
487                    _("No recently used resource found with URI `%s'"),
488                    uri);
489       return FALSE;
490     }
491   else
492     {
493       gtk_menu_shell_select_item (GTK_MENU_SHELL (menu), menu_item);
494
495       return TRUE;
496     }
497 }
498
499 static void
500 gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
501                                        const gchar     *uri)
502 {
503   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
504   
505   gtk_menu_shell_deselect (GTK_MENU_SHELL (menu));
506 }
507
508 static void
509 gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser)
510 {
511   g_warning (_("This function is not implemented for "
512                "widgets of class '%s'"),
513              g_type_name (G_OBJECT_TYPE (chooser)));
514 }
515
516 static void
517 gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser)
518 {
519   g_warning (_("This function is not implemented for "
520                "widgets of class '%s'"),
521              g_type_name (G_OBJECT_TYPE (chooser)));
522 }
523
524 static void
525 gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser  *chooser,
526                                        GtkRecentSortFunc  sort_func,
527                                        gpointer           sort_data,
528                                        GDestroyNotify     data_destroy)
529 {
530   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
531   GtkRecentChooserMenuPrivate *priv = menu->priv;
532   
533   if (priv->sort_data_destroy)
534     {
535       priv->sort_data_destroy (priv->sort_data);
536       
537       priv->sort_func = NULL;
538       priv->sort_data = NULL;
539       priv->sort_data_destroy = NULL;
540     }
541   
542   if (sort_func)
543     {
544       priv->sort_func = sort_func;
545       priv->sort_data = sort_data;
546       priv->sort_data_destroy = data_destroy;
547     }
548 }
549
550 static gint
551 sort_recent_items_mru (GtkRecentInfo *a,
552                        GtkRecentInfo *b,
553                        gpointer       unused)
554 {
555   g_assert (a != NULL && b != NULL);
556   
557   return (gtk_recent_info_get_modified (a) < gtk_recent_info_get_modified (b));
558 }
559
560 static gint
561 sort_recent_items_lru (GtkRecentInfo *a,
562                        GtkRecentInfo *b,
563                        gpointer       unused)
564 {
565   g_assert (a != NULL && b != NULL);
566   
567   return (gtk_recent_info_get_modified (a) > gtk_recent_info_get_modified (b));
568 }
569
570 /* our proxy sorting function */
571 static gint
572 sort_recent_items_proxy (gpointer *a,
573                          gpointer *b,
574                          gpointer  user_data)
575 {
576   GtkRecentInfo *info_a = (GtkRecentInfo *) a;
577   GtkRecentInfo *info_b = (GtkRecentInfo *) b;
578   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (user_data);
579
580   if (menu->priv->sort_func)
581     return (* menu->priv->sort_func) (info_a,
582                                       info_b,
583                                       menu->priv->sort_data);
584   
585   /* fallback */
586   return 0;
587 }
588
589 static void
590 chooser_set_sort_type (GtkRecentChooserMenu *menu,
591                        GtkRecentSortType     sort_type)
592 {
593   if (menu->priv->sort_type == sort_type)
594     return;
595
596   menu->priv->sort_type = sort_type;
597 }
598
599
600 static GList *
601 gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser)
602 {
603   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
604   GtkRecentChooserMenuPrivate *priv;
605   gint limit;
606   GtkRecentSortType sort_type;
607   GList *items;
608   GCompareDataFunc compare_func;
609   gint length;
610   
611   priv = menu->priv;
612   
613   if (!priv->manager)
614     return NULL;
615   
616   limit = gtk_recent_chooser_get_limit (chooser);
617   sort_type = gtk_recent_chooser_get_sort_type (chooser);
618
619   switch (sort_type)
620     {
621     case GTK_RECENT_SORT_NONE:
622       compare_func = NULL;
623       break;
624     case GTK_RECENT_SORT_MRU:
625       compare_func = (GCompareDataFunc) sort_recent_items_mru;
626       break;
627     case GTK_RECENT_SORT_LRU:
628       compare_func = (GCompareDataFunc) sort_recent_items_lru;
629       break;
630     case GTK_RECENT_SORT_CUSTOM:
631       compare_func = (GCompareDataFunc) sort_recent_items_proxy;
632       break;
633     default:
634       g_assert_not_reached ();
635       break;
636     }
637   
638   items = gtk_recent_manager_get_items (priv->manager);
639   if (!items)
640     return NULL;
641   
642   if (compare_func)  
643     items = g_list_sort_with_data (items, compare_func, menu);
644  
645   length = g_list_length (items);
646   if ((limit != -1) && (length > limit))
647     {
648       GList *clamp, *l;
649
650       clamp = g_list_nth (items, limit - 1);
651
652       l = clamp->next;
653       clamp->next = NULL;      
654       
655       g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
656       g_list_free (l);
657     }
658
659   return items;
660 }
661
662 static GtkRecentManager *
663 gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser)
664 {
665   GtkRecentChooserMenuPrivate *priv;
666  
667   priv = GTK_RECENT_CHOOSER_MENU (chooser)->priv;
668   
669   return priv->manager;
670 }
671
672 static void
673 gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
674                                     GtkRecentFilter  *filter)
675 {
676   GtkRecentChooserMenu *menu;
677
678   menu = GTK_RECENT_CHOOSER_MENU (chooser);
679   
680   gtk_recent_chooser_menu_set_current_filter (menu, filter);
681 }
682
683 static void
684 gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
685                                        GtkRecentFilter  *filter)
686 {
687   GtkRecentChooserMenu *menu;
688
689   menu = GTK_RECENT_CHOOSER_MENU (chooser);
690   
691   if (filter == menu->priv->current_filter)
692     {
693       g_object_unref (menu->priv->current_filter);
694       menu->priv->current_filter = NULL;
695
696       g_object_notify (G_OBJECT (menu), "filter");
697     }
698 }
699
700 static GSList *
701 gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser)
702 {
703   GtkRecentChooserMenu *menu;
704   GSList *retval = NULL;
705
706   menu = GTK_RECENT_CHOOSER_MENU (chooser);
707  
708   if (menu->priv->current_filter)
709     retval = g_slist_prepend (retval, menu->priv->current_filter);
710
711   return retval;
712 }
713
714 static void
715 gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
716                                             GtkRecentFilter      *filter)
717 {
718   GtkRecentChooserMenuPrivate *priv;
719
720   priv = menu->priv;
721   
722   if (priv->current_filter)
723     g_object_unref (G_OBJECT (priv->current_filter));
724   
725   if (filter)
726     {
727       priv->current_filter = filter;
728       g_object_ref_sink (priv->current_filter);
729     }
730   
731   g_object_notify (G_OBJECT (menu), "filter");
732 }
733
734 static gboolean
735 get_is_recent_filtered (GtkRecentChooserMenu *menu,
736                         GtkRecentInfo        *info)
737 {
738   GtkRecentChooserMenuPrivate *priv;
739   GtkRecentFilter *current_filter;
740   GtkRecentFilterInfo filter_info;
741   GtkRecentFilterFlags needed;
742   gboolean retval;
743
744   g_assert (info != NULL);
745
746   priv = menu->priv;
747   
748   if (!priv->current_filter)
749     return FALSE;
750   
751   current_filter = priv->current_filter;
752   needed = gtk_recent_filter_get_needed (current_filter);
753   
754   filter_info.contains = GTK_RECENT_FILTER_URI | GTK_RECENT_FILTER_MIME_TYPE;
755   
756   filter_info.uri = gtk_recent_info_get_uri (info);
757   filter_info.mime_type = gtk_recent_info_get_mime_type (info);
758   
759   if (needed & GTK_RECENT_FILTER_DISPLAY_NAME)
760     {
761       filter_info.display_name = gtk_recent_info_get_display_name (info);
762       filter_info.contains |= GTK_RECENT_FILTER_DISPLAY_NAME;
763     }
764   else
765     filter_info.uri = NULL;
766   
767   if (needed & GTK_RECENT_FILTER_APPLICATION)
768     {
769       filter_info.applications = (const gchar **) gtk_recent_info_get_applications (info, NULL);
770       filter_info.contains |= GTK_RECENT_FILTER_APPLICATION;
771     }
772   else
773     filter_info.applications = NULL;
774
775   if (needed & GTK_RECENT_FILTER_GROUP)
776     {
777       filter_info.groups = (const gchar **) gtk_recent_info_get_groups (info, NULL);
778       filter_info.contains |= GTK_RECENT_FILTER_GROUP;
779     }
780   else
781     filter_info.groups = NULL;
782   
783   if (needed & GTK_RECENT_FILTER_AGE)
784     {
785       filter_info.age = gtk_recent_info_get_age (info);
786       filter_info.contains |= GTK_RECENT_FILTER_AGE;
787     }
788   else
789     filter_info.age = -1;
790   
791   retval = gtk_recent_filter_filter (current_filter, &filter_info);
792   
793   /* this we own */
794   if (filter_info.applications)
795     g_strfreev ((gchar **) filter_info.applications);
796   if (filter_info.groups)
797     g_strfreev ((gchar **) filter_info.groups);
798   
799   return !retval;
800 }
801
802 /* taken from libeel/eel-strings.c */
803 static gchar *
804 escape_underscores (const gchar *string)
805 {
806   gint underscores;
807   const gchar *p;
808   gchar *q;
809   gchar *escaped;
810
811   if (!string)
812     return NULL;
813         
814   underscores = 0;
815   for (p = string; *p != '\0'; p++)
816     underscores += (*p == '_');
817
818   if (underscores == 0)
819     return g_strdup (string);
820
821   escaped = g_new (char, strlen (string) + underscores + 1);
822   for (p = string, q = escaped; *p != '\0'; p++, q++)
823     {
824       /* Add an extra underscore. */
825       if (*p == '_')
826         *q++ = '_';
827       
828       *q = *p;
829     }
830   
831   *q = '\0';
832         
833   return escaped;
834 }
835
836 static void
837 gtk_recent_chooser_menu_add_tip (GtkRecentChooserMenu *menu,
838                                  GtkRecentInfo        *info,
839                                  GtkWidget            *item)
840 {
841   GtkRecentChooserMenuPrivate *priv;
842   gchar *path, *tip_text;
843
844   g_assert (info != NULL);
845   g_assert (item != NULL);
846
847   priv = menu->priv;
848   
849   if (!priv->tooltips)
850     return;
851   
852   path = gtk_recent_info_get_uri_display (info);
853   
854   tip_text = g_strdup_printf (_("Open '%s'"), path);
855  
856   gtk_tooltips_set_tip (priv->tooltips,
857                         item,
858                         tip_text,
859                         NULL);
860
861   g_free (path);  
862   g_free (tip_text);
863 }
864
865 static GtkWidget *
866 gtk_recent_chooser_menu_create_item (GtkRecentChooserMenu *menu,
867                                      GtkRecentInfo        *info,
868                                      gint                  count)
869 {
870   GtkRecentChooserMenuPrivate *priv;
871   gchar *label;
872   GtkWidget *item, *image;
873   GdkPixbuf *icon;
874
875   g_assert (info != NULL);
876
877   priv = menu->priv;
878
879   if (priv->show_numbers)
880     {
881       gchar *name, *escaped;
882       
883       name = g_strdup (gtk_recent_info_get_display_name (info));
884       if (!name)
885         name = g_strdup (_("Unknown item"));
886       
887       escaped = escape_underscores (name);
888       
889       /* avoid clashing mnemonics */
890       if (count <= 10)
891         label = g_strdup_printf ("_%d. %s", count, escaped);
892       else
893         label = g_strdup_printf ("%d. %s", count, escaped);
894       
895       item = gtk_image_menu_item_new_with_mnemonic (label);
896       
897       g_free (escaped);
898       g_free (name);
899     }
900   else
901     {
902       label = g_strdup (gtk_recent_info_get_display_name (info));
903       item = gtk_image_menu_item_new_with_label (label);
904     }
905   
906   if (priv->show_icons)
907     {
908       icon = gtk_recent_info_get_icon (info, priv->icon_size);
909         
910       image = gtk_image_new_from_pixbuf (icon);
911       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
912     }
913
914   g_signal_connect (item, "activate",
915                     G_CALLBACK (item_activate_cb),
916                     menu);
917
918 out:
919   g_free (label);
920
921   return item;
922 }
923
924 /* removes the items we own from the menu */
925 static void
926 gtk_recent_chooser_menu_dispose_items (GtkRecentChooserMenu *menu)
927 {
928   GList *children, *l;
929  
930   children = gtk_container_get_children (GTK_CONTAINER (menu));
931   for (l = children; l != NULL; l = l->next)
932     {
933       GtkWidget *menu_item = GTK_WIDGET (l->data);
934       gint mark = 0;
935       
936       /* check for our mark, in order to remove just the items we own */
937       mark = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item),
938                                                  "gtk-recent-menu-mark"));
939       if (mark == 1)
940         {
941           GtkRecentInfo *info;
942           
943           /* destroy the attached RecentInfo struct, if found */
944           info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
945           if (info)
946             g_object_set_data_full (G_OBJECT (menu_item), "gtk-recent-info",
947                                     NULL, NULL);
948           
949           /* and finally remove the item from the menu */
950           gtk_container_remove (GTK_CONTAINER (menu), menu_item);
951         }
952     }
953
954   g_list_free (children);
955 }
956
957 typedef struct
958 {
959   GList *items;
960   gint n_items;
961   gint loaded_items;
962   GtkRecentChooserMenu *menu;
963 } MenuPopulateData;
964
965 static gboolean
966 idle_populate_func (gpointer data)
967 {
968   MenuPopulateData *pdata;
969   GtkRecentChooserMenuPrivate *priv;
970   GtkRecentInfo *info;
971   gboolean retval;
972   GtkWidget *item;
973
974   GDK_THREADS_ENTER ();
975
976   pdata = (MenuPopulateData *) data;
977   priv = pdata->menu->priv;
978
979   priv->populate_id = 0;
980
981   if (!pdata->items)
982     {
983       pdata->items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (pdata->menu));
984       if (!pdata->items)
985         {
986           item = gtk_menu_item_new_with_label (_("No items found"));
987           gtk_widget_set_sensitive (item, FALSE);
988       
989           /* we also mark this item, so that it gets removed when rebuilding
990            * the menu on the next map event
991            */
992           g_object_set_data (G_OBJECT (item), "gtk-recent-menu-mark",
993                              GINT_TO_POINTER (1));
994       
995           gtk_menu_shell_prepend (GTK_MENU_SHELL (pdata->menu), item);
996           gtk_widget_show (item);
997
998           /* no items: add a placeholder menu */
999           GDK_THREADS_LEAVE ();
1000
1001           return FALSE;
1002         }
1003       
1004       /* reverse the list */
1005       pdata->items = g_list_reverse (pdata->items);
1006       
1007       pdata->n_items = g_list_length (pdata->items);
1008       pdata->loaded_items = 0;
1009     }
1010
1011   info = g_list_nth_data (pdata->items, pdata->loaded_items);
1012
1013   /* skip non-local items on request */
1014   if (priv->local_only &&
1015       !gtk_recent_info_is_local (info))
1016     {
1017       goto check_and_return;
1018     }
1019       
1020   /* skip private items on request */
1021   if (!priv->show_private &&
1022       gtk_recent_info_get_private_hint (info))
1023     {
1024       goto check_and_return;
1025     }
1026
1027   /* skip non-existing items on request */
1028   if (!priv->show_not_found &&
1029       !gtk_recent_info_exists (info))
1030     {
1031       goto check_and_return;
1032     }
1033   /* filter items based on the currently set filter object */
1034   if (get_is_recent_filtered (pdata->menu, info))
1035     {
1036       goto check_and_return;
1037     }
1038
1039   item = gtk_recent_chooser_menu_create_item (pdata->menu,
1040                                               info,
1041                                               pdata->loaded_items);
1042   if (!item)
1043     goto check_and_return;
1044       
1045   gtk_recent_chooser_menu_add_tip (pdata->menu, info, item);
1046       
1047   /* FIXME
1048    *
1049    * We should really place our items taking into account user
1050    * defined menu items; this would also remove the need of
1051    * reverting the scan order.
1052    */
1053   gtk_menu_shell_prepend (GTK_MENU_SHELL (pdata->menu), item);
1054   gtk_widget_show (item);
1055       
1056   /* mark the menu item as one of our own */
1057   g_object_set_data (G_OBJECT (item), "gtk-recent-menu-mark",
1058                      GINT_TO_POINTER (1));
1059       
1060   /* attach the RecentInfo object to the menu item, and own a reference
1061    * to it, so that it will be destroyed with the menu item when it's
1062    * not needed anymore.
1063    */
1064   g_object_set_data_full (G_OBJECT (item), "gtk-recent-info",
1065                           gtk_recent_info_ref (info),
1066                           (GDestroyNotify) gtk_recent_info_unref);
1067   
1068 check_and_return:
1069   pdata->loaded_items += 1;
1070
1071   if (pdata->loaded_items == pdata->n_items)
1072     {
1073       g_list_foreach (pdata->items, (GFunc) gtk_recent_info_unref, NULL);
1074       g_list_free (pdata->items);
1075
1076       retval = FALSE;
1077     }
1078   else
1079     retval = TRUE;
1080
1081   GDK_THREADS_LEAVE ();
1082
1083   return retval;
1084 }
1085
1086 static void
1087 idle_populate_clean_up (gpointer data)
1088 {
1089   g_slice_free (MenuPopulateData, data);
1090 }
1091
1092 static void
1093 gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu)
1094 {
1095   MenuPopulateData *pdata;
1096
1097   if (menu->priv->populate_id)
1098     return;
1099
1100   pdata = g_slice_new (MenuPopulateData);
1101   pdata->items = NULL;
1102   pdata->n_items = 0;
1103   pdata->loaded_items = 0;
1104   pdata->menu = menu;
1105
1106   menu->priv->icon_size = get_icon_size_for_widget (GTK_WIDGET (menu));
1107   
1108   /* dispose our menu items first */
1109   gtk_recent_chooser_menu_dispose_items (menu);
1110   
1111   menu->priv->populate_id = g_idle_add_full (G_PRIORITY_HIGH_IDLE + 30,
1112                                              idle_populate_func,
1113                                              pdata,
1114                                              idle_populate_clean_up);
1115 }
1116
1117 /* bounce activate signal from the recent menu item widget 
1118  * to the recent menu widget
1119  */
1120 static void
1121 item_activate_cb (GtkWidget *widget,
1122                   gpointer   user_data)
1123 {
1124   GtkRecentChooser *chooser = GTK_RECENT_CHOOSER (user_data);
1125   
1126   _gtk_recent_chooser_item_activated (chooser);
1127 }
1128
1129 /* we force a redraw if the manager changes when we are showing */
1130 static void
1131 manager_changed_cb (GtkRecentManager *manager,
1132                     gpointer          user_data)
1133 {
1134   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (user_data);
1135
1136   gtk_recent_chooser_menu_populate (menu);
1137 }
1138
1139 static void
1140 set_recent_manager (GtkRecentChooserMenu *menu,
1141                     GtkRecentManager     *manager)
1142 {
1143   if (menu->priv->manager)
1144     {
1145       g_signal_handler_disconnect (menu, menu->priv->manager_changed_id);
1146       menu->priv->manager = NULL;
1147     }
1148   
1149   if (manager)
1150     menu->priv->manager = manager;
1151   else
1152     menu->priv->manager = gtk_recent_manager_get_default ();
1153   
1154   if (menu->priv->manager)
1155     menu->priv->manager_changed_id = g_signal_connect (menu->priv->manager, "changed",
1156                                                        G_CALLBACK (manager_changed_cb),
1157                                                        menu);
1158   /* (re)populate the menu */
1159   gtk_recent_chooser_menu_populate (menu);
1160 }
1161
1162 static gint
1163 get_icon_size_for_widget (GtkWidget *widget)
1164 {
1165   GtkSettings *settings;
1166   gint width, height;
1167
1168   if (gtk_widget_has_screen (widget))
1169     settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
1170   else
1171     settings = gtk_settings_get_default ();
1172
1173   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1174                                          &width, &height))
1175     return MAX (width, height);
1176
1177   return FALLBACK_ICON_SIZE;
1178 }
1179
1180 static void
1181 gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
1182                                        gboolean              show_tips)
1183 {
1184   if (menu->priv->show_tips == show_tips)
1185     return;
1186   
1187   g_assert (menu->priv->tooltips != NULL);
1188   
1189   if (show_tips)
1190     gtk_tooltips_enable (menu->priv->tooltips);
1191   else
1192     gtk_tooltips_disable (menu->priv->tooltips);
1193   
1194   menu->priv->show_tips = show_tips;
1195 }
1196
1197 /*
1198  * Public API
1199  */
1200
1201 /**
1202  * gtk_recent_chooser_menu_new:
1203  *
1204  * Creates a new #GtkRecentChooserMenu widget.
1205  *
1206  * This kind of widget shows the list of recently used resources as
1207  * a menu, each item as a menu item.  Each item inside the menu might
1208  * have an icon, representing its MIME type, and a number, for mnemonic
1209  * access.
1210  *
1211  * This widget implements the #GtkRecentChooser interface.
1212  *
1213  * This widget creates its own #GtkRecentManager object.  See the
1214  * gtk_recent_chooser_menu_new_for_manager() function to know how to create
1215  * a #GtkRecentChooserMenu widget bound to another #GtkRecentManager object.
1216  *
1217  * Return value: a new #GtkRecentChooserMenu
1218  *
1219  * Since: 2.10
1220  */
1221 GtkWidget *
1222 gtk_recent_chooser_menu_new (void)
1223 {
1224   return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1225                        "recent-manager", NULL,
1226                        NULL);
1227 }
1228
1229 /**
1230  * gtk_recent_chooser_menu_new_for_manager:
1231  * @manager: a #GtkRecentManager
1232  *
1233  * Creates a new #GtkRecentChooserMenu widget using @manager as
1234  * the underlying recently used resources manager.
1235  *
1236  * This is useful if you have implemented your own recent manager,
1237  * or if you have a customized instance of a #GtkRecentManager
1238  * object or if you wish to share a common #GtkRecentManager object
1239  * among multiple #GtkRecentChooser widgets.
1240  *
1241  * Return value: a new #GtkRecentChooserMenu, bound to @manager.
1242  *
1243  * Since: 2.10
1244  */
1245 GtkWidget *
1246 gtk_recent_chooser_menu_new_for_manager (GtkRecentManager *manager)
1247 {
1248   g_return_val_if_fail (manager == NULL || GTK_IS_RECENT_MANAGER (manager), NULL);
1249   
1250   return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1251                        "recent-manager", manager,
1252                        NULL);
1253 }
1254
1255 /**
1256  * gtk_recent_chooser_menu_get_show_numbers:
1257  * @menu: a #GtkRecentChooserMenu
1258  *
1259  * Returns the value set by gtk_recent_chooser_menu_set_show_numbers().
1260  * 
1261  * Return value: %TRUE if numbers should be shown.
1262  *
1263  * Since: 2.10
1264  */
1265 gboolean
1266 gtk_recent_chooser_menu_get_show_numbers (GtkRecentChooserMenu *menu)
1267 {
1268   g_return_val_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu), FALSE);
1269
1270   return menu->priv->show_numbers;
1271 }
1272
1273 /**
1274  * gtk_recent_chooser_menu_set_show_numbers:
1275  * @menu: a #GtkRecentChooserMenu
1276  * @show_numbers: whether to show numbers
1277  *
1278  * Sets whether a number should be added to the items of @menu.  The
1279  * numbers are shown to provide a unique character for a mnemonic to
1280  * be used inside the menu item's label.  Only the first the items
1281  * get a number to avoid clashes.
1282  *
1283  * Since: 2.10
1284  */
1285 void
1286 gtk_recent_chooser_menu_set_show_numbers (GtkRecentChooserMenu *menu,
1287                                           gboolean              show_numbers)
1288 {
1289   g_return_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu));
1290
1291   if (menu->priv->show_numbers == show_numbers)
1292     return;
1293
1294   menu->priv->show_numbers = show_numbers;
1295   g_object_notify (G_OBJECT (menu), "show-numbers");
1296 }
1297
1298 #define __GTK_RECENT_CHOOSER_MENU_C__
1299 #include "gtkaliasdef.c"