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