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