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