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