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