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