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