]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentchoosermenu.c
More action-related fixes
[~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   /* ellipsize the menu item label, in case the recent document
838    * display name is huge.
839    */
840   label = GTK_BIN (item)->child;
841   if (GTK_IS_LABEL (label))
842     {
843       gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
844       gtk_label_set_max_width_chars (GTK_LABEL (label), priv->label_width);
845     }
846   
847   if (priv->show_icons)
848     {
849       icon = gtk_recent_info_get_icon (info, priv->icon_size);
850         
851       image = gtk_image_new_from_pixbuf (icon);
852       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
853       g_object_unref (icon);
854     }
855
856   g_signal_connect (item, "activate",
857                     G_CALLBACK (item_activate_cb),
858                     menu);
859
860   return item;
861 }
862
863 static void
864 gtk_recent_chooser_menu_insert_item (GtkRecentChooserMenu *menu,
865                                      GtkWidget            *menuitem,
866                                      gint                  position)
867 {
868   GtkRecentChooserMenuPrivate *priv = menu->priv;
869   gint real_position;
870
871   if (priv->first_recent_item_pos == -1)
872     {
873       GList *children, *l;
874
875       children = gtk_container_get_children (GTK_CONTAINER (menu));
876
877       for (real_position = 0, l = children;
878            l != NULL;
879            real_position += 1, l = l->next)
880         {
881           GObject *child = l->data;
882           gboolean is_placeholder = FALSE;
883
884           is_placeholder =
885             GPOINTER_TO_INT (g_object_get_data (child, "gtk-recent-menu-placeholder"));
886
887           if (is_placeholder)
888             break;
889         }
890
891       g_list_free (children);
892       priv->first_recent_item_pos = real_position;
893     }
894   else
895     real_position = priv->first_recent_item_pos;
896
897   gtk_menu_shell_insert (GTK_MENU_SHELL (menu), menuitem,
898                          real_position + position);
899   gtk_widget_show (menuitem);
900 }
901
902 /* removes the items we own from the menu */
903 static void
904 gtk_recent_chooser_menu_dispose_items (GtkRecentChooserMenu *menu)
905 {
906   GList *children, *l;
907  
908   children = gtk_container_get_children (GTK_CONTAINER (menu));
909   for (l = children; l != NULL; l = l->next)
910     {
911       GtkWidget *menu_item = GTK_WIDGET (l->data);
912       gboolean has_mark = FALSE;
913       
914       /* check for our mark, in order to remove just the items we own */
915       has_mark =
916         GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item), "gtk-recent-menu-mark"));
917
918       if (has_mark)
919         {
920           GtkRecentInfo *info;
921           
922           /* destroy the attached RecentInfo struct, if found */
923           info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
924           if (info)
925             g_object_set_data_full (G_OBJECT (menu_item), "gtk-recent-info",
926                                     NULL, NULL);
927           
928           /* and finally remove the item from the menu */
929           gtk_container_remove (GTK_CONTAINER (menu), menu_item);
930         }
931     }
932
933   /* recalculate the position of the first recent item */
934   menu->priv->first_recent_item_pos = -1;
935
936   g_list_free (children);
937 }
938
939 typedef struct
940 {
941   GList *items;
942   gint n_items;
943   gint loaded_items;
944   gint displayed_items;
945   GtkRecentChooserMenu *menu;
946   GtkWidget *placeholder;
947 } MenuPopulateData;
948
949 static gboolean
950 idle_populate_func (gpointer data)
951 {
952   MenuPopulateData *pdata;
953   GtkRecentChooserMenuPrivate *priv;
954   GtkRecentInfo *info;
955   gboolean retval;
956   GtkWidget *item;
957
958   pdata = (MenuPopulateData *) data;
959   priv = pdata->menu->priv;
960
961   if (!pdata->items)
962     {
963       pdata->items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (pdata->menu));
964       if (!pdata->items)
965         {
966           /* show the placeholder here */
967           gtk_widget_show (pdata->placeholder);
968           pdata->displayed_items = 1;
969           priv->populate_id = 0;
970
971           return FALSE;
972         }
973       else
974         gtk_widget_hide (pdata->placeholder);
975       
976       pdata->n_items = g_list_length (pdata->items);
977       pdata->loaded_items = 0;
978     }
979
980   info = g_list_nth_data (pdata->items, pdata->loaded_items);
981   item = gtk_recent_chooser_menu_create_item (pdata->menu,
982                                               info,
983                                               pdata->displayed_items);
984   if (!item)
985     goto check_and_return;
986       
987   gtk_recent_chooser_menu_add_tip (pdata->menu, info, item);
988   gtk_recent_chooser_menu_insert_item (pdata->menu, item,
989                                        pdata->displayed_items);
990   
991   pdata->displayed_items += 1;
992       
993   /* mark the menu item as one of our own */
994   g_object_set_data (G_OBJECT (item),
995                      "gtk-recent-menu-mark",
996                      GINT_TO_POINTER (TRUE));
997       
998   /* attach the RecentInfo object to the menu item, and own a reference
999    * to it, so that it will be destroyed with the menu item when it's
1000    * not needed anymore.
1001    */
1002   g_object_set_data_full (G_OBJECT (item), "gtk-recent-info",
1003                           gtk_recent_info_ref (info),
1004                           (GDestroyNotify) gtk_recent_info_unref);
1005   
1006 check_and_return:
1007   pdata->loaded_items += 1;
1008
1009   if (pdata->loaded_items == pdata->n_items)
1010     {
1011       g_list_foreach (pdata->items, (GFunc) gtk_recent_info_unref, NULL);
1012       g_list_free (pdata->items);
1013
1014       priv->populate_id = 0;
1015
1016       retval = FALSE;
1017     }
1018   else
1019     retval = TRUE;
1020
1021   return retval;
1022 }
1023
1024 static void
1025 idle_populate_clean_up (gpointer data)
1026 {
1027   MenuPopulateData *pdata = data;
1028
1029   if (pdata->menu->priv->populate_id == 0)
1030     {
1031       /* show the placeholder in case no item survived
1032        * the filtering process in the idle loop
1033        */
1034       if (!pdata->displayed_items)
1035         gtk_widget_show (pdata->placeholder);
1036       g_object_unref (pdata->placeholder);
1037
1038       g_slice_free (MenuPopulateData, data);
1039     }
1040 }
1041
1042 static void
1043 gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu)
1044 {
1045   MenuPopulateData *pdata;
1046   GtkRecentChooserMenuPrivate *priv = menu->priv;
1047
1048   if (menu->priv->populate_id)
1049     return;
1050
1051   pdata = g_slice_new (MenuPopulateData);
1052   pdata->items = NULL;
1053   pdata->n_items = 0;
1054   pdata->loaded_items = 0;
1055   pdata->displayed_items = 0;
1056   pdata->menu = menu;
1057   pdata->placeholder = g_object_ref (priv->placeholder);
1058
1059   priv->icon_size = get_icon_size_for_widget (GTK_WIDGET (menu));
1060   
1061   /* remove our menu items first */
1062   gtk_recent_chooser_menu_dispose_items (menu);
1063   
1064   priv->populate_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
1065                                                  idle_populate_func,
1066                                                  pdata,
1067                                                  idle_populate_clean_up);
1068 }
1069
1070 /* bounce activate signal from the recent menu item widget 
1071  * to the recent menu widget
1072  */
1073 static void
1074 item_activate_cb (GtkWidget *widget,
1075                   gpointer   user_data)
1076 {
1077   GtkRecentChooser *chooser = GTK_RECENT_CHOOSER (user_data);
1078   
1079   _gtk_recent_chooser_item_activated (chooser);
1080 }
1081
1082 /* we force a redraw if the manager changes when we are showing */
1083 static void
1084 manager_changed_cb (GtkRecentManager *manager,
1085                     gpointer          user_data)
1086 {
1087   GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (user_data);
1088
1089   gtk_recent_chooser_menu_populate (menu);
1090 }
1091
1092 static void
1093 set_recent_manager (GtkRecentChooserMenu *menu,
1094                     GtkRecentManager     *manager)
1095 {
1096   GtkRecentChooserMenuPrivate *priv = menu->priv;
1097
1098   if (priv->manager)
1099     {
1100       if (priv->manager_changed_id)
1101         {
1102           g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
1103           priv->manager_changed_id = 0;
1104         }
1105
1106       if (priv->populate_id)
1107         {
1108           g_source_remove (priv->populate_id);
1109           priv->populate_id = 0;
1110         }
1111
1112       priv->manager = NULL;
1113     }
1114   
1115   if (manager)
1116     priv->manager = manager;
1117   else
1118     priv->manager = gtk_recent_manager_get_default ();
1119   
1120   if (priv->manager)
1121     priv->manager_changed_id = g_signal_connect (priv->manager, "changed",
1122                                                  G_CALLBACK (manager_changed_cb),
1123                                                  menu);
1124 }
1125
1126 static gint
1127 get_icon_size_for_widget (GtkWidget *widget)
1128 {
1129   GtkSettings *settings;
1130   gint width, height;
1131
1132   if (gtk_widget_has_screen (widget))
1133     settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
1134   else
1135     settings = gtk_settings_get_default ();
1136
1137   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1138                                          &width, &height))
1139     return MAX (width, height);
1140
1141   return FALLBACK_ICON_SIZE;
1142 }
1143
1144 static void
1145 foreach_set_shot_tips (GtkWidget *widget,
1146                        gpointer   user_data)
1147 {
1148   GtkRecentChooserMenu *menu = user_data;
1149   GtkRecentChooserMenuPrivate *priv = menu->priv;
1150   gboolean has_mark;
1151
1152   /* toggle the tooltip only on the items we create */
1153   has_mark =
1154     GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "gtk-recent-menu-mark"));
1155
1156   if (has_mark)
1157     gtk_widget_set_has_tooltip (widget, priv->show_tips);
1158 }
1159
1160 static void
1161 gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
1162                                        gboolean              show_tips)
1163 {
1164   GtkRecentChooserMenuPrivate *priv = menu->priv;
1165
1166   if (priv->show_tips == show_tips)
1167     return;
1168   
1169   priv->show_tips = show_tips;
1170   gtk_container_foreach (GTK_CONTAINER (menu), foreach_set_shot_tips, menu);
1171 }
1172
1173 static void 
1174 gtk_recent_chooser_activatable_update (GtkActivatable       *activatable,
1175                                        GtkAction            *action,
1176                                        const gchar          *property_name)
1177 {
1178   if (strcmp (property_name, "sensitive") == 0)
1179     gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1180
1181   _gtk_recent_chooser_activatable_update (activatable, action, property_name);
1182 }
1183
1184 static void 
1185 gtk_recent_chooser_activatable_reset (GtkActivatable       *activatable,
1186                                       GtkAction            *action)
1187 {
1188   gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1189
1190   _gtk_recent_chooser_activatable_reset (activatable, action);
1191 }
1192
1193
1194 /*
1195  * Public API
1196  */
1197
1198 /**
1199  * gtk_recent_chooser_menu_new:
1200  *
1201  * Creates a new #GtkRecentChooserMenu widget.
1202  *
1203  * This kind of widget shows the list of recently used resources as
1204  * a menu, each item as a menu item.  Each item inside the menu might
1205  * have an icon, representing its MIME type, and a number, for mnemonic
1206  * access.
1207  *
1208  * This widget implements the #GtkRecentChooser interface.
1209  *
1210  * This widget creates its own #GtkRecentManager object.  See the
1211  * gtk_recent_chooser_menu_new_for_manager() function to know how to create
1212  * a #GtkRecentChooserMenu widget bound to another #GtkRecentManager object.
1213  *
1214  * Return value: a new #GtkRecentChooserMenu
1215  *
1216  * Since: 2.10
1217  */
1218 GtkWidget *
1219 gtk_recent_chooser_menu_new (void)
1220 {
1221   return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1222                        "recent-manager", NULL,
1223                        NULL);
1224 }
1225
1226 /**
1227  * gtk_recent_chooser_menu_new_for_manager:
1228  * @manager: a #GtkRecentManager
1229  *
1230  * Creates a new #GtkRecentChooserMenu widget using @manager as
1231  * the underlying recently used resources manager.
1232  *
1233  * This is useful if you have implemented your own recent manager,
1234  * or if you have a customized instance of a #GtkRecentManager
1235  * object or if you wish to share a common #GtkRecentManager object
1236  * among multiple #GtkRecentChooser widgets.
1237  *
1238  * Return value: a new #GtkRecentChooserMenu, bound to @manager.
1239  *
1240  * Since: 2.10
1241  */
1242 GtkWidget *
1243 gtk_recent_chooser_menu_new_for_manager (GtkRecentManager *manager)
1244 {
1245   g_return_val_if_fail (manager == NULL || GTK_IS_RECENT_MANAGER (manager), NULL);
1246   
1247   return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
1248                        "recent-manager", manager,
1249                        NULL);
1250 }
1251
1252 /**
1253  * gtk_recent_chooser_menu_get_show_numbers:
1254  * @menu: a #GtkRecentChooserMenu
1255  *
1256  * Returns the value set by gtk_recent_chooser_menu_set_show_numbers().
1257  * 
1258  * Return value: %TRUE if numbers should be shown.
1259  *
1260  * Since: 2.10
1261  */
1262 gboolean
1263 gtk_recent_chooser_menu_get_show_numbers (GtkRecentChooserMenu *menu)
1264 {
1265   g_return_val_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu), FALSE);
1266
1267   return menu->priv->show_numbers;
1268 }
1269
1270 /**
1271  * gtk_recent_chooser_menu_set_show_numbers:
1272  * @menu: a #GtkRecentChooserMenu
1273  * @show_numbers: whether to show numbers
1274  *
1275  * Sets whether a number should be added to the items of @menu.  The
1276  * numbers are shown to provide a unique character for a mnemonic to
1277  * be used inside ten menu item's label.  Only the first the items
1278  * get a number to avoid clashes.
1279  *
1280  * Since: 2.10
1281  */
1282 void
1283 gtk_recent_chooser_menu_set_show_numbers (GtkRecentChooserMenu *menu,
1284                                           gboolean              show_numbers)
1285 {
1286   g_return_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu));
1287
1288   if (menu->priv->show_numbers == show_numbers)
1289     return;
1290
1291   menu->priv->show_numbers = show_numbers;
1292   g_object_notify (G_OBJECT (menu), "show-numbers");
1293 }
1294
1295 #define __GTK_RECENT_CHOOSER_MENU_C__
1296 #include "gtkaliasdef.c"