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