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