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