]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentchooserdefault.c
gtk/gtkrecentchooserdefault.c: use accessor functions to access GtkWidget
[~andy/gtk] / gtk / gtkrecentchooserdefault.c
1 /* GTK - The GIMP Toolkit
2  * gtkrecentchooserdefault.c
3  * Copyright (C) 2005-2006, 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 #include <time.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31
32 #include "gtkstock.h"
33 #include "gtkicontheme.h"
34 #include "gtkiconfactory.h"
35 #include "gtksettings.h"
36 #include "gtktreeview.h"
37 #include "gtkliststore.h"
38 #include "gtkbutton.h"
39 #include "gtkcelllayout.h"
40 #include "gtkcellrendererpixbuf.h"
41 #include "gtkcellrenderertext.h"
42 #include "gtkcheckmenuitem.h"
43 #include "gtkclipboard.h"
44 #include "gtkcombobox.h"
45 #include "gtkentry.h"
46 #include "gtkeventbox.h"
47 #include "gtkexpander.h"
48 #include "gtkframe.h"
49 #include "gtkhbox.h"
50 #include "gtkhpaned.h"
51 #include "gtkimage.h"
52 #include "gtkimagemenuitem.h"
53 #include "gtkintl.h"
54 #include "gtklabel.h"
55 #include "gtkmenuitem.h"
56 #include "gtkmessagedialog.h"
57 #include "gtkscrolledwindow.h"
58 #include "gtkseparatormenuitem.h"
59 #include "gtksizegroup.h"
60 #include "gtktable.h"
61 #include "gtktreemodelsort.h"
62 #include "gtktreemodelfilter.h"
63 #include "gtktreeselection.h"
64 #include "gtktreestore.h"
65 #include "gtktooltip.h"
66 #include "gtktypebuiltins.h"
67 #include "gtkvbox.h"
68 #include "gtkactivatable.h"
69
70 #include "gtkrecentmanager.h"
71 #include "gtkrecentfilter.h"
72 #include "gtkrecentchooser.h"
73 #include "gtkrecentchooserprivate.h"
74 #include "gtkrecentchooserutils.h"
75 #include "gtkrecentchooserdefault.h"
76
77 #include "gtkprivate.h"
78
79
80 enum 
81 {
82   PROP_0,
83
84   /* activatable properties */
85   PROP_ACTIVATABLE_RELATED_ACTION,
86   PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
87 };
88
89
90 struct _GtkRecentChooserDefault
91 {
92   GtkVBox parent_instance;
93   
94   GtkRecentManager *manager;
95   gulong manager_changed_id;
96   guint local_manager : 1;
97   
98   gint icon_size;
99
100   /* RecentChooser properties */
101   gint limit;  
102   GtkRecentSortType sort_type;
103   guint show_private : 1;
104   guint show_not_found : 1;
105   guint select_multiple : 1;
106   guint show_tips : 1;
107   guint show_icons : 1;
108   guint local_only : 1;
109
110   guint limit_set : 1;
111   
112   GSList *filters;
113   GtkRecentFilter *current_filter;
114   GtkWidget *filter_combo_hbox;
115   GtkWidget *filter_combo;
116   
117   GtkRecentSortFunc sort_func;
118   gpointer sort_data;
119   GDestroyNotify sort_data_destroy;
120
121   GtkIconTheme *icon_theme;
122   
123   GtkWidget *recent_view;
124   GtkListStore *recent_store;
125   GtkTreeViewColumn *icon_column;
126   GtkTreeViewColumn *meta_column;
127   GtkCellRenderer *meta_renderer;
128   GtkTreeSelection *selection;
129   
130   GtkWidget *recent_popup_menu;
131   GtkWidget *recent_popup_menu_copy_item;
132   GtkWidget *recent_popup_menu_remove_item;
133   GtkWidget *recent_popup_menu_clear_item;
134   GtkWidget *recent_popup_menu_show_private_item;
135  
136   guint load_id;
137   GList *recent_items;
138   gint n_recent_items;
139   gint loaded_items;
140   guint load_state;
141 };
142
143 typedef struct _GtkRecentChooserDefaultClass
144 {
145   GtkVBoxClass parent_class;
146 } GtkRecentChooserDefaultClass;
147
148 enum {
149   RECENT_URI_COLUMN,
150   RECENT_DISPLAY_NAME_COLUMN,
151   RECENT_INFO_COLUMN,
152     
153   N_RECENT_COLUMNS
154 };
155
156 enum {
157   LOAD_EMPTY,    /* initial state: the model is empty */
158   LOAD_PRELOAD,  /* the model is loading and not inserted in the tree yet */
159   LOAD_LOADING,  /* the model is fully loaded but not inserted */
160   LOAD_FINISHED  /* the model is fully loaded and inserted */
161 };
162
163 /* Icon size for if we can't get it from the theme */
164 #define FALLBACK_ICON_SIZE  48
165 #define FALLBACK_ITEM_LIMIT 20
166
167 #define NUM_CHARS 40
168 #define NUM_LINES 9
169
170 \f
171
172 /* GObject */
173 static void     _gtk_recent_chooser_default_class_init  (GtkRecentChooserDefaultClass *klass);
174 static void     _gtk_recent_chooser_default_init        (GtkRecentChooserDefault      *impl);
175 static GObject *gtk_recent_chooser_default_constructor  (GType                         type,
176                                                          guint                         n_construct_prop,
177                                                          GObjectConstructParam        *construct_params);
178 static void     gtk_recent_chooser_default_finalize     (GObject                      *object);
179 static void     gtk_recent_chooser_default_dispose      (GObject                      *object);
180 static void     gtk_recent_chooser_default_set_property (GObject                      *object,
181                                                          guint                         prop_id,
182                                                          const GValue                 *value,
183                                                          GParamSpec                   *pspec);
184 static void     gtk_recent_chooser_default_get_property (GObject                      *object,
185                                                          guint                         prop_id,
186                                                          GValue                       *value,
187                                                          GParamSpec                   *pspec);
188
189 /* GtkRecentChooserIface */
190 static void              gtk_recent_chooser_iface_init                 (GtkRecentChooserIface  *iface);
191 static gboolean          gtk_recent_chooser_default_set_current_uri    (GtkRecentChooser       *chooser,
192                                                                         const gchar            *uri,
193                                                                         GError                **error);
194 static gchar *           gtk_recent_chooser_default_get_current_uri    (GtkRecentChooser       *chooser);
195 static gboolean          gtk_recent_chooser_default_select_uri         (GtkRecentChooser       *chooser,
196                                                                         const gchar            *uri,
197                                                                         GError                **error);
198 static void              gtk_recent_chooser_default_unselect_uri       (GtkRecentChooser       *chooser,
199                                                                         const gchar            *uri);
200 static void              gtk_recent_chooser_default_select_all         (GtkRecentChooser       *chooser);
201 static void              gtk_recent_chooser_default_unselect_all       (GtkRecentChooser       *chooser);
202 static GList *           gtk_recent_chooser_default_get_items          (GtkRecentChooser       *chooser);
203 static GtkRecentManager *gtk_recent_chooser_default_get_recent_manager (GtkRecentChooser       *chooser);
204 static void              gtk_recent_chooser_default_set_sort_func      (GtkRecentChooser       *chooser,
205                                                                         GtkRecentSortFunc       sort_func,
206                                                                         gpointer                sort_data,
207                                                                         GDestroyNotify          data_destroy);
208 static void              gtk_recent_chooser_default_add_filter         (GtkRecentChooser       *chooser,
209                                                                         GtkRecentFilter        *filter);
210 static void              gtk_recent_chooser_default_remove_filter      (GtkRecentChooser       *chooser,
211                                                                         GtkRecentFilter        *filter);
212 static GSList *          gtk_recent_chooser_default_list_filters       (GtkRecentChooser       *chooser);
213
214
215 static void gtk_recent_chooser_default_map      (GtkWidget *widget);
216 static void gtk_recent_chooser_default_show_all (GtkWidget *widget);
217
218 static void set_current_filter        (GtkRecentChooserDefault *impl,
219                                        GtkRecentFilter         *filter);
220
221 static GtkIconTheme *get_icon_theme_for_widget (GtkWidget   *widget);
222 static gint          get_icon_size_for_widget  (GtkWidget   *widget,
223                                                 GtkIconSize  icon_size);
224 static gint          get_recent_files_limit    (GtkWidget   *widget);
225
226 static void reload_recent_items (GtkRecentChooserDefault *impl);
227 static void chooser_set_model   (GtkRecentChooserDefault *impl);
228
229 static void set_recent_manager (GtkRecentChooserDefault *impl,
230                                 GtkRecentManager        *manager);
231
232 static void chooser_set_sort_type (GtkRecentChooserDefault *impl,
233                                    GtkRecentSortType        sort_type);
234
235 static void recent_manager_changed_cb (GtkRecentManager  *manager,
236                                        gpointer           user_data);
237 static void recent_icon_data_func     (GtkTreeViewColumn *tree_column,
238                                        GtkCellRenderer   *cell,
239                                        GtkTreeModel      *model,
240                                        GtkTreeIter       *iter,
241                                        gpointer           user_data);
242 static void recent_meta_data_func     (GtkTreeViewColumn *tree_column,
243                                        GtkCellRenderer   *cell,
244                                        GtkTreeModel      *model,
245                                        GtkTreeIter       *iter,
246                                        gpointer           user_data);
247
248 static void selection_changed_cb      (GtkTreeSelection  *z,
249                                        gpointer           user_data);
250 static void row_activated_cb          (GtkTreeView       *tree_view,
251                                        GtkTreePath       *tree_path,
252                                        GtkTreeViewColumn *tree_column,
253                                        gpointer           user_data);
254 static void filter_combo_changed_cb   (GtkComboBox       *combo_box,
255                                        gpointer           user_data);
256
257 static void remove_all_activated_cb   (GtkMenuItem       *menu_item,
258                                        gpointer           user_data);
259 static void remove_item_activated_cb  (GtkMenuItem       *menu_item,
260                                        gpointer           user_data);
261 static void show_private_toggled_cb   (GtkCheckMenuItem  *menu_item,
262                                        gpointer           user_data);
263
264 static gboolean recent_view_popup_menu_cb   (GtkWidget      *widget,
265                                              gpointer        user_data);
266 static gboolean recent_view_button_press_cb (GtkWidget      *widget,
267                                              GdkEventButton *event,
268                                              gpointer        user_data);
269
270 static void     recent_view_drag_begin_cb         (GtkWidget        *widget,
271                                                    GdkDragContext   *context,
272                                                    gpointer          user_data);
273 static void     recent_view_drag_data_get_cb      (GtkWidget        *widget,
274                                                    GdkDragContext   *context,
275                                                    GtkSelectionData *selection_data,
276                                                    guint             info,
277                                                    guint32           time_,
278                                                    gpointer          data);
279 static gboolean recent_view_query_tooltip_cb      (GtkWidget        *widget,
280                                                    gint              x,
281                                                    gint              y,
282                                                    gboolean          keyboard_tip,
283                                                    GtkTooltip       *tooltip,
284                                                    gpointer          user_data);
285
286 static void gtk_recent_chooser_activatable_iface_init (GtkActivatableIface  *iface);
287 static void gtk_recent_chooser_update                 (GtkActivatable       *activatable,
288                                                        GtkAction            *action,
289                                                        const gchar          *property_name);
290 static void gtk_recent_chooser_sync_action_properties (GtkActivatable       *activatable,
291                                                        GtkAction            *action);
292
293 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserDefault,
294                          _gtk_recent_chooser_default,
295                          GTK_TYPE_VBOX,
296                          G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
297                                                 gtk_recent_chooser_iface_init)
298                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
299                                                 gtk_recent_chooser_activatable_iface_init))
300
301
302 \f
303
304 static void
305 gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface)
306 {
307   iface->set_current_uri = gtk_recent_chooser_default_set_current_uri;
308   iface->get_current_uri = gtk_recent_chooser_default_get_current_uri;
309   iface->select_uri = gtk_recent_chooser_default_select_uri;
310   iface->unselect_uri = gtk_recent_chooser_default_unselect_uri;
311   iface->select_all = gtk_recent_chooser_default_select_all;
312   iface->unselect_all = gtk_recent_chooser_default_unselect_all;
313   iface->get_items = gtk_recent_chooser_default_get_items;
314   iface->get_recent_manager = gtk_recent_chooser_default_get_recent_manager;
315   iface->set_sort_func = gtk_recent_chooser_default_set_sort_func;
316   iface->add_filter = gtk_recent_chooser_default_add_filter;
317   iface->remove_filter = gtk_recent_chooser_default_remove_filter;
318   iface->list_filters = gtk_recent_chooser_default_list_filters;
319 }
320
321 static void
322 gtk_recent_chooser_activatable_iface_init (GtkActivatableIface *iface)
323
324 {
325   iface->update = gtk_recent_chooser_update;
326   iface->sync_action_properties = gtk_recent_chooser_sync_action_properties;
327 }
328
329 static void
330 _gtk_recent_chooser_default_class_init (GtkRecentChooserDefaultClass *klass)
331 {
332   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
333   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
334
335   gobject_class->constructor = gtk_recent_chooser_default_constructor;
336   gobject_class->set_property = gtk_recent_chooser_default_set_property;
337   gobject_class->get_property = gtk_recent_chooser_default_get_property;
338   gobject_class->dispose = gtk_recent_chooser_default_dispose;
339   gobject_class->finalize = gtk_recent_chooser_default_finalize;
340   
341   widget_class->map = gtk_recent_chooser_default_map;
342   widget_class->show_all = gtk_recent_chooser_default_show_all;
343   
344   _gtk_recent_chooser_install_properties (gobject_class);
345
346   g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
347   g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
348 }
349
350 static void
351 _gtk_recent_chooser_default_init (GtkRecentChooserDefault *impl)
352 {
353   gtk_box_set_spacing (GTK_BOX (impl), 6);
354
355   /* by default, we use the global manager */
356   impl->local_manager = FALSE;
357   
358   impl->limit = FALLBACK_ITEM_LIMIT;
359   impl->sort_type = GTK_RECENT_SORT_NONE;
360
361   impl->show_icons = TRUE;
362   impl->show_private = FALSE;
363   impl->show_not_found = TRUE;
364   impl->show_tips = FALSE;
365   impl->select_multiple = FALSE;
366   impl->local_only = TRUE;
367   
368   impl->icon_size = FALLBACK_ICON_SIZE;
369   impl->icon_theme = NULL;
370   
371   impl->current_filter = NULL;
372
373   impl->recent_items = NULL;
374   impl->n_recent_items = 0;
375   impl->loaded_items = 0;
376   
377   impl->load_state = LOAD_EMPTY;
378 }
379
380 static GObject *
381 gtk_recent_chooser_default_constructor (GType                  type,
382                                         guint                  n_params,
383                                         GObjectConstructParam *params)
384 {
385   GObjectClass *parent_class;
386   GtkRecentChooserDefault *impl;
387   GObject *object;
388   GtkWidget *scrollw;
389   GtkCellRenderer *renderer;
390
391   parent_class = G_OBJECT_CLASS (_gtk_recent_chooser_default_parent_class);
392   object = parent_class->constructor (type, n_params, params);
393   impl = GTK_RECENT_CHOOSER_DEFAULT (object);
394   
395   g_assert (impl->manager);
396   
397   gtk_widget_push_composite_child ();
398
399   impl->limit = get_recent_files_limit (GTK_WIDGET (impl));
400   
401   scrollw = gtk_scrolled_window_new (NULL, NULL);
402   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrollw),
403                                        GTK_SHADOW_IN);
404   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollw),
405                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
406   gtk_box_pack_start (GTK_BOX (impl), scrollw, TRUE, TRUE, 0);
407   gtk_widget_show (scrollw);
408   
409   impl->recent_view = gtk_tree_view_new ();
410   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->recent_view), FALSE);
411   g_signal_connect (impl->recent_view, "row-activated",
412                     G_CALLBACK (row_activated_cb), impl);
413   g_signal_connect (impl->recent_view, "popup-menu",
414                     G_CALLBACK (recent_view_popup_menu_cb), impl);
415   g_signal_connect (impl->recent_view, "button-press-event",
416                     G_CALLBACK (recent_view_button_press_cb), impl);
417   g_signal_connect (impl->recent_view, "drag-begin",
418                     G_CALLBACK (recent_view_drag_begin_cb), impl);
419   g_signal_connect (impl->recent_view, "drag-data-get",
420                     G_CALLBACK (recent_view_drag_data_get_cb), impl);
421
422   g_object_set (impl->recent_view, "has-tooltip", TRUE, NULL);
423   g_signal_connect (impl->recent_view, "query-tooltip",
424                     G_CALLBACK (recent_view_query_tooltip_cb), impl);
425
426   g_object_set_data (G_OBJECT (impl->recent_view),
427                      "GtkRecentChooserDefault", impl);
428   
429   gtk_container_add (GTK_CONTAINER (scrollw), impl->recent_view);
430   gtk_widget_show (impl->recent_view);
431   
432   impl->icon_column = gtk_tree_view_column_new ();
433   gtk_tree_view_column_set_expand (impl->icon_column, FALSE);
434   gtk_tree_view_column_set_resizable (impl->icon_column, FALSE);
435   
436   renderer = gtk_cell_renderer_pixbuf_new ();
437   gtk_tree_view_column_pack_start (impl->icon_column, renderer, FALSE);
438   gtk_tree_view_column_set_cell_data_func (impl->icon_column,
439                                            renderer,
440                                            recent_icon_data_func,
441                                            impl,
442                                            NULL);
443   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->recent_view),
444                                impl->icon_column);
445   
446   impl->meta_column = gtk_tree_view_column_new ();
447   gtk_tree_view_column_set_expand (impl->meta_column, TRUE);
448   gtk_tree_view_column_set_resizable (impl->meta_column, FALSE);
449   
450   impl->meta_renderer = gtk_cell_renderer_text_new ();
451   g_object_set (G_OBJECT (impl->meta_renderer),
452                 "ellipsize", PANGO_ELLIPSIZE_END,
453                 NULL);
454   gtk_tree_view_column_pack_start (impl->meta_column, impl->meta_renderer, TRUE);
455   gtk_tree_view_column_set_cell_data_func (impl->meta_column,
456                                            impl->meta_renderer,
457                                            recent_meta_data_func,
458                                            impl,
459                                            NULL);
460   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->recent_view),
461                                impl->meta_column);
462   
463   impl->selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->recent_view));
464   gtk_tree_selection_set_mode (impl->selection, GTK_SELECTION_SINGLE);
465   g_signal_connect (impl->selection, "changed", G_CALLBACK (selection_changed_cb), impl);
466
467   /* drag and drop */
468   gtk_drag_source_set (impl->recent_view,
469                        GDK_BUTTON1_MASK,
470                        NULL, 0,
471                        GDK_ACTION_COPY);
472   gtk_drag_source_add_uri_targets (impl->recent_view);
473
474   impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
475   
476   impl->filter_combo = gtk_combo_box_new_text ();
477   gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
478   g_signal_connect (impl->filter_combo, "changed",
479                     G_CALLBACK (filter_combo_changed_cb), impl);
480   gtk_widget_set_tooltip_text (impl->filter_combo,
481                                _("Select which type of documents are shown"));
482   
483   gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox),
484                     impl->filter_combo,
485                     FALSE, FALSE, 0);
486   gtk_widget_show (impl->filter_combo);
487   
488   gtk_box_pack_end (GTK_BOX (impl), impl->filter_combo_hbox, FALSE, FALSE, 0);
489   
490   gtk_widget_pop_composite_child ();
491   
492   impl->recent_store = gtk_list_store_new (N_RECENT_COLUMNS,
493                                            G_TYPE_STRING,       /* uri */
494                                            G_TYPE_STRING,       /* display_name */
495                                            GTK_TYPE_RECENT_INFO /* info */);
496   
497   return object;
498 }
499
500 static void
501 gtk_recent_chooser_default_set_property (GObject      *object,
502                                          guint         prop_id,
503                                          const GValue *value,
504                                          GParamSpec   *pspec)
505 {
506   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);
507   
508   switch (prop_id)
509     {
510     case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
511       set_recent_manager (impl, g_value_get_object (value));
512       break;
513     case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
514       impl->show_private = g_value_get_boolean (value);
515       if (impl->recent_popup_menu_show_private_item)
516         {
517           GtkCheckMenuItem *item = GTK_CHECK_MENU_ITEM (impl->recent_popup_menu_show_private_item);
518           g_signal_handlers_block_by_func (item, G_CALLBACK (show_private_toggled_cb), impl);
519           gtk_check_menu_item_set_active (item, impl->show_private);
520           g_signal_handlers_unblock_by_func (item, G_CALLBACK (show_private_toggled_cb), impl);
521         }
522       reload_recent_items (impl);
523       break;
524     case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
525       impl->show_not_found = g_value_get_boolean (value);
526       reload_recent_items (impl);
527       break;
528     case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
529       impl->show_tips = g_value_get_boolean (value);
530       break;
531     case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
532       impl->show_icons = g_value_get_boolean (value);
533       gtk_tree_view_column_set_visible (impl->icon_column, impl->show_icons);
534       break;
535     case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
536       impl->select_multiple = g_value_get_boolean (value);
537       
538       if (impl->select_multiple)
539         gtk_tree_selection_set_mode (impl->selection, GTK_SELECTION_MULTIPLE);
540       else
541         gtk_tree_selection_set_mode (impl->selection, GTK_SELECTION_SINGLE);
542       break;
543     case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
544       impl->local_only = g_value_get_boolean (value);
545       reload_recent_items (impl);
546       break;
547     case GTK_RECENT_CHOOSER_PROP_LIMIT:
548       impl->limit = g_value_get_int (value);
549       impl->limit_set = TRUE;
550       reload_recent_items (impl);
551       break;
552     case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
553       chooser_set_sort_type (impl, g_value_get_enum (value));
554       break;
555     case GTK_RECENT_CHOOSER_PROP_FILTER:
556       set_current_filter (impl, g_value_get_object (value));
557       break;
558     case PROP_ACTIVATABLE_RELATED_ACTION:
559       _gtk_recent_chooser_set_related_action (GTK_RECENT_CHOOSER (impl), g_value_get_object (value));
560       break;
561     case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE: 
562       _gtk_recent_chooser_set_use_action_appearance (GTK_RECENT_CHOOSER (impl), g_value_get_boolean (value));
563       break;
564     default:
565       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
566       break;
567     }
568 }
569
570 static void
571 gtk_recent_chooser_default_get_property (GObject    *object,
572                                          guint       prop_id,
573                                          GValue     *value,
574                                          GParamSpec *pspec)
575 {
576   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);
577   
578   switch (prop_id)
579     {
580     case GTK_RECENT_CHOOSER_PROP_LIMIT:
581       g_value_set_int (value, impl->limit);
582       break;
583     case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
584       g_value_set_enum (value, impl->sort_type);
585       break;
586     case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
587       g_value_set_boolean (value, impl->show_private);
588       break;
589     case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
590       g_value_set_boolean (value, impl->show_icons);
591       break;
592     case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
593       g_value_set_boolean (value, impl->show_not_found);
594       break;
595     case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
596       g_value_set_boolean (value, impl->show_tips);
597       break;
598     case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
599       g_value_set_boolean (value, impl->local_only);
600       break;
601     case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
602       g_value_set_boolean (value, impl->select_multiple);
603       break;
604     case GTK_RECENT_CHOOSER_PROP_FILTER:
605       g_value_set_object (value, impl->current_filter);
606       break;
607     case PROP_ACTIVATABLE_RELATED_ACTION:
608       g_value_set_object (value, _gtk_recent_chooser_get_related_action (GTK_RECENT_CHOOSER (impl)));
609       break;
610     case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE: 
611       g_value_set_boolean (value, _gtk_recent_chooser_get_use_action_appearance (GTK_RECENT_CHOOSER (impl)));
612       break;
613     default:
614       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
615       break;
616     }
617 }
618
619 static void
620 gtk_recent_chooser_default_dispose (GObject *object)
621 {
622   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);
623
624   if (impl->load_id)
625     {
626       g_source_remove (impl->load_id);
627       impl->load_state = LOAD_EMPTY;
628       impl->load_id = 0;
629     }
630
631   if (impl->recent_items)
632     {
633       g_list_foreach (impl->recent_items, (GFunc) gtk_recent_info_unref, NULL);
634       g_list_free (impl->recent_items);
635       impl->recent_items = NULL;
636     }
637
638   if (impl->manager && impl->manager_changed_id)
639     {
640       g_signal_handler_disconnect (impl->manager, impl->manager_changed_id);
641       impl->manager_changed_id = 0;
642     }
643
644   if (impl->filters)
645     {
646       g_slist_foreach (impl->filters, (GFunc) g_object_unref, NULL);
647       g_slist_free (impl->filters);
648       impl->filters = NULL;
649     }
650   
651   if (impl->current_filter)
652     {
653       g_object_unref (impl->current_filter);
654       impl->current_filter = NULL;
655     }
656
657   if (impl->recent_store)
658     {
659       g_object_unref (impl->recent_store);
660       impl->recent_store = NULL;
661     }
662
663   G_OBJECT_CLASS (_gtk_recent_chooser_default_parent_class)->dispose (object);
664 }
665
666 static void
667 gtk_recent_chooser_default_finalize (GObject *object)
668 {
669   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);
670
671   impl->manager = NULL; 
672   
673   if (impl->sort_data_destroy)
674     {
675       impl->sort_data_destroy (impl->sort_data);
676       impl->sort_data_destroy = NULL;
677     }
678   
679   impl->sort_data = NULL;
680   impl->sort_func = NULL;
681   
682   G_OBJECT_CLASS (_gtk_recent_chooser_default_parent_class)->finalize (object);
683 }
684
685 /* override GtkWidget::show_all since we have internal widgets we wish to keep
686  * hidden unless we decide otherwise, like the filter combo box.
687  */
688 static void
689 gtk_recent_chooser_default_show_all (GtkWidget *widget)
690 {
691   gtk_widget_show (widget);
692 }
693
694
695
696 /* Shows an error dialog set as transient for the specified window */
697 static void
698 error_message_with_parent (GtkWindow   *parent,
699                            const gchar *msg,
700                            const gchar *detail)
701 {
702   GtkWidget *dialog;
703
704   dialog = gtk_message_dialog_new (parent,
705                                    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
706                                    GTK_MESSAGE_ERROR,
707                                    GTK_BUTTONS_OK,
708                                    "%s",
709                                    msg);
710   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
711                                             "%s", detail);
712
713   if (parent->group)
714     gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
715
716   gtk_dialog_run (GTK_DIALOG (dialog));
717   gtk_widget_destroy (dialog);
718 }
719
720 /* Returns a toplevel GtkWindow, or NULL if none */
721 static GtkWindow *
722 get_toplevel (GtkWidget *widget)
723 {
724   GtkWidget *toplevel;
725
726   toplevel = gtk_widget_get_toplevel (widget);
727   if (!gtk_widget_is_toplevel (toplevel))
728     return NULL;
729   else
730     return GTK_WINDOW (toplevel);
731 }
732
733 /* Shows an error dialog for the file chooser */
734 static void
735 error_message (GtkRecentChooserDefault *impl,
736                const gchar             *msg,
737                const gchar             *detail)
738 {
739   error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
740 }
741
742 static void
743 set_busy_cursor (GtkRecentChooserDefault *impl,
744                  gboolean                 show_busy_cursor)
745 {
746   GtkWindow *toplevel;
747   GdkDisplay *display;
748   GdkCursor *cursor;
749
750   toplevel = get_toplevel (GTK_WIDGET (impl));
751   if (!toplevel || !gtk_widget_get_realized (GTK_WIDGET (toplevel)))
752     return;
753   
754   display = gtk_widget_get_display (GTK_WIDGET (toplevel));
755   
756   cursor = NULL;
757   if (show_busy_cursor)
758     cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
759
760   gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (toplevel)),
761                          cursor);
762   gdk_display_flush (display);
763
764   if (cursor)
765     gdk_cursor_unref (cursor);
766 }
767
768 static void
769 chooser_set_model (GtkRecentChooserDefault *impl)
770 {
771   g_assert (impl->recent_store != NULL);
772   g_assert (impl->load_state == LOAD_LOADING);
773
774   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->recent_view),
775                            GTK_TREE_MODEL (impl->recent_store));
776   gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->recent_view));
777   gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->recent_view), TRUE);
778   gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->recent_view),
779                                    RECENT_DISPLAY_NAME_COLUMN);
780
781   impl->load_state = LOAD_FINISHED;
782 }
783
784 static gboolean
785 load_recent_items (gpointer user_data)
786 {
787   GtkRecentChooserDefault *impl;
788   GtkRecentInfo *info;
789   GtkTreeIter iter;
790   const gchar *uri, *name;
791   gboolean retval;
792   
793   impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
794   
795   g_assert ((impl->load_state == LOAD_EMPTY) ||
796             (impl->load_state == LOAD_PRELOAD));
797   
798   /* store the items for multiple runs */
799   if (!impl->recent_items)
800     {
801       impl->recent_items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (impl));
802       if (!impl->recent_items)
803         {
804           impl->load_state = LOAD_FINISHED;
805           
806           return FALSE;
807         }
808         
809       impl->n_recent_items = g_list_length (impl->recent_items);
810       impl->loaded_items = 0;
811       impl->load_state = LOAD_PRELOAD;
812     }
813   
814   info = (GtkRecentInfo *) g_list_nth_data (impl->recent_items,
815                                             impl->loaded_items);
816   g_assert (info);
817
818   uri = gtk_recent_info_get_uri (info);
819   name = gtk_recent_info_get_display_name (info);
820   
821   /* at this point, everything goes inside the model; operations on the
822    * visualization of items inside the model are done in the cell data
823    * funcs (remember that there are two of those: one for the icon and
824    * one for the text), while the filtering is done only when a filter
825    * is actually loaded. */
826   gtk_list_store_append (impl->recent_store, &iter);
827   gtk_list_store_set (impl->recent_store, &iter,
828                       RECENT_URI_COLUMN, uri,           /* uri  */
829                       RECENT_DISPLAY_NAME_COLUMN, name, /* display_name */
830                       RECENT_INFO_COLUMN, info,         /* info */
831                       -1);
832   
833   impl->loaded_items += 1;
834
835   if (impl->loaded_items == impl->n_recent_items)
836     {
837       /* we have finished loading, so we remove the items cache */
838       impl->load_state = LOAD_LOADING;
839       
840       g_list_foreach (impl->recent_items,
841                       (GFunc) gtk_recent_info_unref,
842                       NULL);
843       g_list_free (impl->recent_items);
844       
845       impl->recent_items = NULL;
846       impl->n_recent_items = 0;
847       impl->loaded_items = 0;
848
849       /* load the filled up model */
850       chooser_set_model (impl);
851
852       retval = FALSE;
853     }
854   else
855     {
856       /* we did not finish, so continue loading */
857       retval = TRUE;
858     }
859   
860   return retval;
861 }
862
863 static void
864 cleanup_after_load (gpointer user_data)
865 {
866   GtkRecentChooserDefault *impl;
867   
868   impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
869
870   if (impl->load_id != 0)
871     {
872       g_assert ((impl->load_state == LOAD_EMPTY) ||
873                 (impl->load_state == LOAD_PRELOAD) ||
874                 (impl->load_state == LOAD_LOADING) ||
875                 (impl->load_state == LOAD_FINISHED));
876       
877       /* we have officialy finished loading all the items,
878        * so we can reset the state machine
879        */
880       g_source_remove (impl->load_id);
881       impl->load_id = 0;
882       impl->load_state = LOAD_EMPTY;
883     }
884   else
885     g_assert ((impl->load_state == LOAD_EMPTY) ||
886               (impl->load_state == LOAD_LOADING) ||
887               (impl->load_state == LOAD_FINISHED));
888
889   set_busy_cursor (impl, FALSE);
890 }
891
892 /* clears the current model and reloads the recently used resources */
893 static void
894 reload_recent_items (GtkRecentChooserDefault *impl)
895 {
896   GtkWidget *widget;
897
898   /* reload is already in progress - do not disturb */
899   if (impl->load_id)
900     return;
901   
902   widget = GTK_WIDGET (impl);
903
904   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->recent_view), NULL);
905   gtk_list_store_clear (impl->recent_store);
906   
907   if (!impl->icon_theme)
908     impl->icon_theme = get_icon_theme_for_widget (widget);
909
910   impl->icon_size = get_icon_size_for_widget (widget,
911                                               GTK_ICON_SIZE_BUTTON);
912
913   if (!impl->limit_set)
914     impl->limit = get_recent_files_limit (widget);
915
916   set_busy_cursor (impl, TRUE);
917
918   impl->load_state = LOAD_EMPTY;
919   impl->load_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
920                                              load_recent_items,
921                                              impl,
922                                              cleanup_after_load);
923 }
924
925 /* taken form gtkfilechooserdialog.c */
926 static void
927 set_default_size (GtkRecentChooserDefault *impl)
928 {
929   GtkWidget *widget;
930   gint width, height;
931   gint font_size;
932   GdkScreen *screen;
933   gint monitor_num;
934   GtkRequisition req;
935   GdkRectangle monitor;
936
937   widget = GTK_WIDGET (impl);
938
939   /* Size based on characters and the icon size */
940   font_size = pango_font_description_get_size (gtk_widget_get_style (widget)->font_desc);
941   font_size = PANGO_PIXELS (font_size);
942
943   width = impl->icon_size + font_size * NUM_CHARS;
944   height = (impl->icon_size + font_size) * NUM_LINES;
945
946   /* Use at least the requisition size... */
947   gtk_widget_size_request (widget, &req);
948   width = MAX (width, req.width);
949   height = MAX (height, req.height);
950
951   /* ... but no larger than the monitor */
952   screen = gtk_widget_get_screen (widget);
953   monitor_num = gdk_screen_get_monitor_at_window (screen,
954                                                   gtk_widget_get_window (widget));
955
956   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
957
958   width = MIN (width, monitor.width * 3 / 4);
959   height = MIN (height, monitor.height * 3 / 4);
960
961   /* Set size */
962   gtk_widget_set_size_request (impl->recent_view, width, height);
963 }
964
965 static void
966 gtk_recent_chooser_default_map (GtkWidget *widget)
967 {
968   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (widget);
969
970   GTK_WIDGET_CLASS (_gtk_recent_chooser_default_parent_class)->map (widget);
971
972   /* reloads everything */
973   reload_recent_items (impl);
974
975   set_default_size (impl);
976 }
977
978 static void
979 recent_icon_data_func (GtkTreeViewColumn *tree_column,
980                        GtkCellRenderer   *cell,
981                        GtkTreeModel      *model,
982                        GtkTreeIter       *iter,
983                        gpointer           user_data)
984 {
985   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
986   GtkRecentInfo *info = NULL;
987   GdkPixbuf *pixbuf;
988   
989   gtk_tree_model_get (model, iter,
990                       RECENT_INFO_COLUMN, &info,
991                       -1);
992   g_assert (info != NULL);
993   
994   pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
995   
996   g_object_set (cell,
997                 "pixbuf", pixbuf,
998                 NULL);
999   
1000   if (pixbuf)  
1001     g_object_unref (pixbuf);
1002
1003   gtk_recent_info_unref (info);
1004 }
1005
1006 static void
1007 recent_meta_data_func (GtkTreeViewColumn *tree_column,
1008                        GtkCellRenderer   *cell,
1009                        GtkTreeModel      *model,
1010                        GtkTreeIter       *iter,
1011                        gpointer           user_data)
1012 {
1013   GtkRecentInfo *info = NULL;
1014   gchar *name;
1015   
1016   gtk_tree_model_get (model, iter,
1017                       RECENT_DISPLAY_NAME_COLUMN, &name,
1018                       RECENT_INFO_COLUMN, &info,
1019                       -1);
1020   g_assert (info != NULL);
1021   
1022   if (!name)
1023     name = gtk_recent_info_get_short_name (info);
1024
1025   g_object_set (cell, "text", name, NULL);
1026   
1027   g_free (name);
1028   gtk_recent_info_unref (info);
1029 }
1030
1031
1032 static gchar *
1033 gtk_recent_chooser_default_get_current_uri (GtkRecentChooser *chooser)
1034 {
1035   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1036   
1037   g_assert (impl->selection != NULL);
1038   
1039   if (!impl->select_multiple)
1040     {
1041       GtkTreeModel *model;
1042       GtkTreeIter iter;
1043       gchar *uri = NULL;
1044       
1045       if (!gtk_tree_selection_get_selected (impl->selection, &model, &iter))
1046         return NULL;
1047       
1048       gtk_tree_model_get (model, &iter, RECENT_URI_COLUMN, &uri, -1);
1049       
1050       return uri;
1051     }
1052   
1053   return NULL;
1054 }
1055
1056 typedef struct
1057 {
1058   guint found : 1;
1059   guint do_select : 1;
1060   guint do_activate : 1;
1061   
1062   gchar *uri;
1063   
1064   GtkRecentChooserDefault *impl;
1065 } SelectURIData;
1066
1067 static gboolean
1068 scan_for_uri_cb (GtkTreeModel *model,
1069                  GtkTreePath  *path,
1070                  GtkTreeIter  *iter,
1071                  gpointer      user_data)
1072 {
1073   SelectURIData *select_data = (SelectURIData *) user_data;
1074   gchar *uri = NULL;
1075   
1076   if (!select_data)
1077     return TRUE;
1078   
1079   if (select_data->found)
1080     return TRUE;
1081   
1082   gtk_tree_model_get (model, iter, RECENT_URI_COLUMN, &uri, -1);
1083   if (!uri)
1084     return FALSE;
1085   
1086   if (strcmp (uri, select_data->uri) == 0)
1087     {
1088       select_data->found = TRUE;
1089       
1090       if (select_data->do_activate)
1091         gtk_tree_view_row_activated (GTK_TREE_VIEW (select_data->impl->recent_view),
1092                                      path,
1093                                      select_data->impl->meta_column);
1094       
1095       if (select_data->do_select)
1096         gtk_tree_selection_select_path (select_data->impl->selection, path);
1097       else
1098         gtk_tree_selection_unselect_path (select_data->impl->selection, path);
1099
1100       g_free (uri);
1101       
1102       return TRUE;
1103     }
1104
1105   g_free (uri);
1106   
1107   return FALSE;
1108 }
1109
1110 static gboolean
1111 gtk_recent_chooser_default_set_current_uri (GtkRecentChooser  *chooser,
1112                                             const gchar       *uri,
1113                                             GError           **error)
1114 {
1115   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1116   SelectURIData *data;
1117   
1118   data = g_new0 (SelectURIData, 1);
1119   data->uri = g_strdup (uri);
1120   data->impl = impl;
1121   data->found = FALSE;
1122   data->do_activate = TRUE;
1123   data->do_select = TRUE;
1124   
1125   gtk_tree_model_foreach (GTK_TREE_MODEL (impl->recent_store),
1126                           scan_for_uri_cb,
1127                           data);
1128   
1129   if (!data->found)
1130     {
1131       g_free (data->uri);
1132       g_free (data);
1133       
1134       g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
1135                    GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
1136                    _("No item for URI '%s' found"),
1137                    uri);
1138       return FALSE;
1139     }
1140   
1141   g_free (data->uri);
1142   g_free (data);
1143
1144   return TRUE;
1145 }
1146
1147 static gboolean
1148 gtk_recent_chooser_default_select_uri (GtkRecentChooser  *chooser,
1149                                        const gchar       *uri,
1150                                        GError           **error)
1151 {
1152   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1153   SelectURIData *data;
1154   
1155   data = g_new0 (SelectURIData, 1);
1156   data->uri = g_strdup (uri);
1157   data->impl = impl;
1158   data->found = FALSE;
1159   data->do_activate = FALSE;
1160   data->do_select = TRUE;
1161   
1162   gtk_tree_model_foreach (GTK_TREE_MODEL (impl->recent_store),
1163                           scan_for_uri_cb,
1164                           data);
1165   
1166   if (!data->found)
1167     {
1168       g_free (data->uri);
1169       g_free (data);
1170       
1171       g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
1172                    GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
1173                    _("No item for URI '%s' found"),
1174                    uri);
1175       return FALSE;
1176     }
1177   
1178   g_free (data->uri);
1179   g_free (data);
1180
1181   return TRUE;
1182 }
1183
1184 static void
1185 gtk_recent_chooser_default_unselect_uri (GtkRecentChooser *chooser,
1186                                          const gchar      *uri)
1187 {
1188   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1189   SelectURIData *data;
1190   
1191   data = g_new0 (SelectURIData, 1);
1192   data->uri = g_strdup (uri);
1193   data->impl = impl;
1194   data->found = FALSE;
1195   data->do_activate = FALSE;
1196   data->do_select = FALSE;
1197   
1198   gtk_tree_model_foreach (GTK_TREE_MODEL (impl->recent_store),
1199                           scan_for_uri_cb,
1200                           data);
1201   
1202   g_free (data->uri);
1203   g_free (data);
1204 }
1205
1206 static void
1207 gtk_recent_chooser_default_select_all (GtkRecentChooser *chooser)
1208 {
1209   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1210   
1211   if (!impl->select_multiple)
1212     return;
1213   
1214   gtk_tree_selection_select_all (impl->selection);
1215 }
1216
1217 static void
1218 gtk_recent_chooser_default_unselect_all (GtkRecentChooser *chooser)
1219 {
1220   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1221   
1222   gtk_tree_selection_unselect_all (impl->selection);
1223 }
1224
1225 static void
1226 gtk_recent_chooser_default_set_sort_func (GtkRecentChooser  *chooser,
1227                                           GtkRecentSortFunc  sort_func,
1228                                           gpointer           sort_data,
1229                                           GDestroyNotify     data_destroy)
1230 {
1231   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1232   
1233   if (impl->sort_data_destroy)
1234     {
1235       impl->sort_data_destroy (impl->sort_data);
1236       impl->sort_data_destroy = NULL;
1237     }
1238       
1239   impl->sort_func = NULL;
1240   impl->sort_data = NULL;
1241   
1242   if (sort_func)
1243     {
1244       impl->sort_func = sort_func;
1245       impl->sort_data = sort_data;
1246       impl->sort_data_destroy = data_destroy;
1247     }
1248 }
1249
1250 static GList *
1251 gtk_recent_chooser_default_get_items (GtkRecentChooser *chooser)
1252 {
1253   GtkRecentChooserDefault *impl;
1254
1255   impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1256
1257   return _gtk_recent_chooser_get_items (chooser,
1258                                         impl->current_filter,
1259                                         impl->sort_func,
1260                                         impl->sort_data);
1261 }
1262
1263 static GtkRecentManager *
1264 gtk_recent_chooser_default_get_recent_manager (GtkRecentChooser *chooser)
1265 {
1266   return GTK_RECENT_CHOOSER_DEFAULT (chooser)->manager;
1267 }
1268
1269 static void
1270 show_filters (GtkRecentChooserDefault *impl,
1271               gboolean                 show)
1272 {
1273   if (show)
1274     gtk_widget_show (impl->filter_combo_hbox);
1275   else
1276     gtk_widget_hide (impl->filter_combo_hbox);
1277 }
1278
1279 static void
1280 gtk_recent_chooser_default_add_filter (GtkRecentChooser *chooser,
1281                                        GtkRecentFilter  *filter)
1282 {
1283   GtkRecentChooserDefault *impl;
1284   const gchar *name;
1285
1286   impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1287   
1288   if (g_slist_find (impl->filters, filter))
1289     {
1290       g_warning ("gtk_recent_chooser_add_filter() called on filter already in list\n");
1291       return;
1292     }
1293   
1294   g_object_ref_sink (filter);
1295   impl->filters = g_slist_append (impl->filters, filter);
1296   
1297   /* display new filter */
1298   name = gtk_recent_filter_get_name (filter);
1299   if (!name)
1300     name = _("Untitled filter");
1301     
1302   gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
1303   
1304   if (!g_slist_find (impl->filters, impl->current_filter))
1305     set_current_filter (impl, filter);
1306   
1307   show_filters (impl, TRUE);
1308 }
1309
1310 static void
1311 gtk_recent_chooser_default_remove_filter (GtkRecentChooser *chooser,
1312                                           GtkRecentFilter  *filter)
1313 {
1314   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1315   GtkTreeModel *model;
1316   GtkTreeIter iter;
1317   gint filter_idx;
1318   
1319   filter_idx = g_slist_index (impl->filters, filter);
1320   
1321   if (filter_idx < 0)
1322     {
1323       g_warning ("gtk_recent_chooser_remove_filter() called on filter not in list\n");
1324       return;  
1325     }
1326   
1327   impl->filters = g_slist_remove (impl->filters, filter);
1328   
1329   if (filter == impl->current_filter)
1330     {
1331       if (impl->filters)
1332         set_current_filter (impl, impl->filters->data);
1333       else
1334         set_current_filter (impl, NULL);
1335     }
1336   
1337   model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
1338   gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_idx);
1339   gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
1340   
1341   g_object_unref (filter);
1342   
1343   if (!impl->filters)
1344     show_filters (impl, FALSE);
1345 }
1346
1347 static GSList *
1348 gtk_recent_chooser_default_list_filters (GtkRecentChooser *chooser)
1349 {
1350   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1351   
1352   return g_slist_copy (impl->filters);
1353 }
1354
1355 static void
1356 set_current_filter (GtkRecentChooserDefault *impl,
1357                     GtkRecentFilter         *filter)
1358 {
1359   if (impl->current_filter != filter)
1360     {
1361       gint filter_idx;
1362       
1363       filter_idx = g_slist_index (impl->filters, filter);
1364       if (impl->filters && filter && filter_idx < 0)
1365         return;
1366       
1367       if (impl->current_filter)
1368         g_object_unref (impl->current_filter);
1369       
1370       impl->current_filter = filter;
1371       
1372       if (impl->current_filter)     
1373         {
1374           g_object_ref_sink (impl->current_filter);
1375         }
1376       
1377       if (impl->filters)
1378         gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
1379                                   filter_idx);
1380       
1381       if (impl->recent_store)
1382         reload_recent_items (impl);
1383
1384       g_object_notify (G_OBJECT (impl), "filter");
1385     }
1386 }
1387
1388 static void
1389 chooser_set_sort_type (GtkRecentChooserDefault *impl,
1390                        GtkRecentSortType        sort_type)
1391 {
1392   if (impl->sort_type != sort_type)
1393     {
1394       impl->sort_type = sort_type;
1395       reload_recent_items (impl);
1396
1397       g_object_notify (G_OBJECT (impl), "sort-type");
1398     }
1399 }
1400
1401
1402 static GtkIconTheme *
1403 get_icon_theme_for_widget (GtkWidget *widget)
1404 {
1405   if (gtk_widget_has_screen (widget))
1406     return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1407
1408   return gtk_icon_theme_get_default ();
1409 }
1410
1411 static gint
1412 get_icon_size_for_widget (GtkWidget   *widget,
1413                           GtkIconSize  icon_size)
1414 {
1415   GtkSettings *settings;
1416   gint width, height;
1417
1418   if (gtk_widget_has_screen (widget))
1419     settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
1420   else
1421     settings = gtk_settings_get_default ();
1422
1423   if (gtk_icon_size_lookup_for_settings (settings, icon_size,
1424                                          &width, &height))
1425     return MAX (width, height);
1426
1427   return FALLBACK_ICON_SIZE;
1428 }
1429
1430 static gint
1431 get_recent_files_limit (GtkWidget *widget)
1432 {
1433   GtkSettings *settings;
1434   gint limit;
1435
1436   if (gtk_widget_has_screen (widget))
1437     settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
1438   else
1439     settings = gtk_settings_get_default ();
1440   
1441   g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
1442
1443   return limit;
1444 }
1445
1446 static void
1447 recent_manager_changed_cb (GtkRecentManager *manager,
1448                            gpointer          user_data)
1449 {
1450   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1451
1452   reload_recent_items (impl);
1453 }
1454
1455 static void
1456 selection_changed_cb (GtkTreeSelection *selection,
1457                       gpointer          user_data)
1458 {
1459   _gtk_recent_chooser_selection_changed (GTK_RECENT_CHOOSER (user_data));
1460 }
1461
1462 static void
1463 row_activated_cb (GtkTreeView       *tree_view,
1464                   GtkTreePath       *tree_path,
1465                   GtkTreeViewColumn *tree_column,
1466                   gpointer           user_data)
1467 {
1468   _gtk_recent_chooser_item_activated (GTK_RECENT_CHOOSER (user_data));
1469 }
1470
1471 static void
1472 filter_combo_changed_cb (GtkComboBox *combo_box,
1473                          gpointer     user_data)
1474 {
1475   GtkRecentChooserDefault *impl;
1476   gint new_index;
1477   GtkRecentFilter *filter;
1478   
1479   impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1480   
1481   new_index = gtk_combo_box_get_active (combo_box);
1482   filter = g_slist_nth_data (impl->filters, new_index);
1483   
1484   set_current_filter (impl, filter);
1485 }
1486
1487 static GdkPixbuf *
1488 get_drag_pixbuf (GtkRecentChooserDefault *impl)
1489 {
1490   GtkRecentInfo *info;
1491   GdkPixbuf *retval;
1492   gint size;
1493   
1494   g_assert (GTK_IS_RECENT_CHOOSER_DEFAULT (impl));
1495
1496   info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (impl));
1497   if (!info)
1498     return NULL;
1499
1500   size = get_icon_size_for_widget (GTK_WIDGET (impl), GTK_ICON_SIZE_DND);
1501
1502   retval = gtk_recent_info_get_icon (info, size);
1503   gtk_recent_info_unref (info);
1504
1505   return retval;
1506 }
1507
1508 static void
1509 recent_view_drag_begin_cb (GtkWidget      *widget,
1510                            GdkDragContext *context,
1511                            gpointer        user_data)
1512 {
1513   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1514   GdkPixbuf *pixbuf;
1515
1516   pixbuf = get_drag_pixbuf (impl);
1517   if (pixbuf)
1518     {
1519       gtk_drag_set_icon_pixbuf (context, pixbuf, 0, 0);
1520       g_object_unref (pixbuf);
1521     }
1522   else
1523     gtk_drag_set_icon_default (context);
1524 }
1525
1526 typedef struct
1527 {
1528   gchar **uri_list;
1529   gsize next_pos;
1530 } DragData;
1531
1532 static void
1533 append_uri_to_urilist (GtkTreeModel *model,
1534                        GtkTreePath  *path,
1535                        GtkTreeIter  *iter,
1536                        gpointer      user_data)
1537 {
1538   DragData *drag_data = (DragData *) user_data;
1539   GtkTreeModel *child_model;
1540   GtkTreeIter child_iter;
1541   gchar *uri = NULL;
1542   gsize pos;
1543
1544   child_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
1545   gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model),
1546                                                     &child_iter,
1547                                                     iter);
1548   gtk_tree_model_get (child_model, &child_iter,
1549                       RECENT_URI_COLUMN, &uri,
1550                       -1);
1551   g_assert (uri != NULL);
1552
1553   pos = drag_data->next_pos;
1554   drag_data->uri_list[pos] = g_strdup (uri);
1555   drag_data->next_pos = pos + 1;
1556 }
1557
1558 static void
1559 recent_view_drag_data_get_cb (GtkWidget        *widget,
1560                               GdkDragContext   *context,
1561                               GtkSelectionData *selection_data,
1562                               guint             info,
1563                               guint32           time_,
1564                               gpointer          data)
1565 {
1566   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (data);
1567   DragData drag_data;
1568   gsize n_uris;
1569   
1570   n_uris = gtk_tree_selection_count_selected_rows (impl->selection);
1571   if (n_uris == 0)
1572     return;
1573
1574   drag_data.uri_list = g_new0 (gchar *, n_uris + 1);
1575   drag_data.next_pos = 0;
1576   
1577   gtk_tree_selection_selected_foreach (impl->selection,
1578                                        append_uri_to_urilist,
1579                                        &drag_data);
1580   
1581   gtk_selection_data_set_uris (selection_data, drag_data.uri_list);
1582
1583   g_strfreev (drag_data.uri_list);
1584 }
1585
1586 static gboolean
1587 recent_view_query_tooltip_cb (GtkWidget  *widget,
1588                               gint        x,
1589                               gint        y,
1590                               gboolean    keyboard_tip,
1591                               GtkTooltip *tooltip,
1592                               gpointer    user_data)
1593 {
1594   GtkRecentChooserDefault *impl = user_data;
1595   GtkTreeView *tree_view;
1596   GtkTreeIter iter;
1597   GtkTreePath *path = NULL;
1598   GtkRecentInfo *info = NULL;
1599   gchar *uri_display;
1600
1601   if (!impl->show_tips)
1602     return FALSE;
1603
1604   tree_view = GTK_TREE_VIEW (impl->recent_view);
1605
1606   gtk_tree_view_get_tooltip_context (tree_view,
1607                                      &x, &y,
1608                                      keyboard_tip,
1609                                      NULL, &path, NULL);
1610   if (!path)
1611     return FALSE;
1612
1613   if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_store), &iter, path))
1614     {
1615       gtk_tree_path_free (path);
1616       return FALSE;
1617     }
1618
1619   gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_store), &iter,
1620                       RECENT_INFO_COLUMN, &info,
1621                       -1);
1622
1623   uri_display = gtk_recent_info_get_uri_display (info);
1624   
1625   gtk_tooltip_set_text (tooltip, uri_display);
1626   gtk_tree_view_set_tooltip_row (tree_view, tooltip, path);
1627
1628   g_free (uri_display);
1629   gtk_tree_path_free (path);
1630   gtk_recent_info_unref (info);
1631
1632   return TRUE;
1633 }
1634
1635 static void
1636 remove_selected_from_list (GtkRecentChooserDefault *impl)
1637 {
1638   gchar *uri;
1639   GError *err;
1640   
1641   if (impl->select_multiple)
1642     return;
1643   
1644   uri = gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (impl));
1645   if (!uri)
1646     return;
1647   
1648   err = NULL;
1649   if (!gtk_recent_manager_remove_item (impl->manager, uri, &err))
1650     {
1651       gchar *msg;
1652    
1653       msg = g_strdup (_("Could not remove item"));
1654       error_message (impl, msg, err->message);
1655       
1656       g_free (msg);
1657       g_error_free (err);
1658     }
1659   
1660   g_free (uri);
1661 }
1662
1663 static void
1664 copy_activated_cb (GtkMenuItem *menu_item,
1665                    gpointer     user_data)
1666 {
1667   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1668   GtkRecentInfo *info;
1669   gchar *utf8_uri;
1670
1671   info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (impl));
1672   if (!info)
1673     return;
1674
1675   utf8_uri = gtk_recent_info_get_uri_display (info);
1676   
1677   gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (impl),
1678                                                     GDK_SELECTION_CLIPBOARD),
1679                           utf8_uri, -1);
1680
1681   gtk_recent_info_unref (info);
1682   g_free (utf8_uri);
1683 }
1684
1685 static void
1686 remove_all_activated_cb (GtkMenuItem *menu_item,
1687                          gpointer     user_data)
1688 {
1689   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1690   GError *err = NULL;
1691   
1692   gtk_recent_manager_purge_items (impl->manager, &err);
1693   if (err)
1694     {
1695        gchar *msg;
1696
1697        msg = g_strdup (_("Could not clear list"));
1698
1699        error_message (impl, msg, err->message);
1700        
1701        g_free (msg);
1702        g_error_free (err);
1703     }
1704 }
1705
1706 static void
1707 remove_item_activated_cb (GtkMenuItem *menu_item,
1708                           gpointer     user_data)
1709 {
1710   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1711   
1712   remove_selected_from_list (impl);
1713 }
1714
1715 static void
1716 show_private_toggled_cb (GtkCheckMenuItem *menu_item,
1717                          gpointer          user_data)
1718 {
1719   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1720   
1721   g_object_set (G_OBJECT (impl),
1722                 "show-private", gtk_check_menu_item_get_active (menu_item),
1723                 NULL);
1724 }
1725
1726 static void
1727 recent_popup_menu_detach_cb (GtkWidget *attach_widget,
1728                              GtkMenu   *menu)
1729 {
1730   GtkRecentChooserDefault *impl;
1731   
1732   impl = g_object_get_data (G_OBJECT (attach_widget), "GtkRecentChooserDefault");
1733   g_assert (GTK_IS_RECENT_CHOOSER_DEFAULT (impl));
1734   
1735   impl->recent_popup_menu = NULL;
1736   impl->recent_popup_menu_remove_item = NULL;
1737   impl->recent_popup_menu_copy_item = NULL;
1738   impl->recent_popup_menu_clear_item = NULL;
1739   impl->recent_popup_menu_show_private_item = NULL;
1740 }
1741
1742 static void
1743 recent_view_menu_ensure_state (GtkRecentChooserDefault *impl)
1744 {
1745   gint count;
1746   
1747   g_assert (GTK_IS_RECENT_CHOOSER_DEFAULT (impl));
1748   g_assert (impl->recent_popup_menu != NULL);
1749
1750   if (!impl->manager)
1751     count = 0;
1752   else
1753     g_object_get (G_OBJECT (impl->manager), "size", &count, NULL);
1754
1755   if (count == 0)
1756     {
1757       gtk_widget_set_sensitive (impl->recent_popup_menu_remove_item, FALSE);
1758       gtk_widget_set_sensitive (impl->recent_popup_menu_copy_item, FALSE);
1759       gtk_widget_set_sensitive (impl->recent_popup_menu_clear_item, FALSE);
1760       gtk_widget_set_sensitive (impl->recent_popup_menu_show_private_item, FALSE);
1761     }
1762 }
1763
1764 static void
1765 recent_view_menu_build (GtkRecentChooserDefault *impl)
1766 {
1767   GtkWidget *item;
1768   
1769   if (impl->recent_popup_menu)
1770     {
1771       recent_view_menu_ensure_state (impl);
1772       
1773       return;
1774     }
1775   
1776   impl->recent_popup_menu = gtk_menu_new ();
1777   gtk_menu_attach_to_widget (GTK_MENU (impl->recent_popup_menu),
1778                              impl->recent_view,
1779                              recent_popup_menu_detach_cb);
1780   
1781   item = gtk_image_menu_item_new_with_mnemonic (_("Copy _Location"));
1782   impl->recent_popup_menu_copy_item = item;
1783   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1784                                  gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU));
1785   g_signal_connect (item, "activate",
1786                     G_CALLBACK (copy_activated_cb), impl);
1787   gtk_widget_show (item);
1788   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1789
1790   item = gtk_separator_menu_item_new ();
1791   gtk_widget_show (item);
1792   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1793   
1794   item = gtk_image_menu_item_new_with_mnemonic (_("_Remove From List"));
1795   impl->recent_popup_menu_remove_item = item;
1796   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1797                                  gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
1798   g_signal_connect (item, "activate",
1799                     G_CALLBACK (remove_item_activated_cb), impl);
1800   gtk_widget_show (item);
1801   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1802
1803   item = gtk_image_menu_item_new_with_mnemonic (_("_Clear List"));
1804   impl->recent_popup_menu_clear_item = item;
1805   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1806                                  gtk_image_new_from_stock (GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU));
1807   g_signal_connect (item, "activate",
1808                     G_CALLBACK (remove_all_activated_cb), impl);
1809   
1810   gtk_widget_show (item);
1811   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1812   
1813   item = gtk_separator_menu_item_new ();
1814   gtk_widget_show (item);
1815   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1816   
1817   item = gtk_check_menu_item_new_with_mnemonic (_("Show _Private Resources"));
1818   impl->recent_popup_menu_show_private_item = item;
1819   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), impl->show_private);
1820   g_signal_connect (item, "toggled",
1821                     G_CALLBACK (show_private_toggled_cb), impl);
1822   gtk_widget_show (item);
1823   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1824   
1825   recent_view_menu_ensure_state (impl);
1826 }
1827
1828 /* taken from gtkfilechooserdefault.c */
1829 static void
1830 popup_position_func (GtkMenu   *menu,
1831                      gint      *x,
1832                      gint      *y,
1833                      gboolean  *push_in,
1834                      gpointer   user_data)
1835 {
1836   GtkAllocation allocation;
1837   GtkWidget *widget = GTK_WIDGET (user_data);
1838   GdkScreen *screen = gtk_widget_get_screen (widget);
1839   GtkRequisition req;
1840   gint monitor_num;
1841   GdkRectangle monitor;
1842
1843   if (G_UNLIKELY (!gtk_widget_get_realized (widget)))
1844     return;
1845
1846   gdk_window_get_origin (gtk_widget_get_window (widget),
1847                          x, y);
1848
1849   gtk_widget_size_request (GTK_WIDGET (menu), &req);
1850
1851   gtk_widget_get_allocation (widget, &allocation);
1852   *x += (allocation.width - req.width) / 2;
1853   *y += (allocation.height - req.height) / 2;
1854
1855   monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
1856   gtk_menu_set_monitor (menu, monitor_num);
1857   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1858
1859   *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
1860   *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
1861
1862   *push_in = FALSE;
1863 }
1864
1865
1866 static void
1867 recent_view_menu_popup (GtkRecentChooserDefault *impl,
1868                         GdkEventButton          *event)
1869 {
1870   recent_view_menu_build (impl);
1871   
1872   if (event)
1873     gtk_menu_popup (GTK_MENU (impl->recent_popup_menu),
1874                     NULL, NULL, NULL, NULL,
1875                     event->button, event->time);
1876   else
1877     {
1878       gtk_menu_popup (GTK_MENU (impl->recent_popup_menu),
1879                       NULL, NULL,
1880                       popup_position_func, impl->recent_view,
1881                       0, GDK_CURRENT_TIME);
1882       gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->recent_popup_menu),
1883                                    FALSE);
1884     }
1885 }
1886
1887 static gboolean
1888 recent_view_popup_menu_cb (GtkWidget *widget,
1889                            gpointer   user_data)
1890 {
1891   recent_view_menu_popup (GTK_RECENT_CHOOSER_DEFAULT (user_data), NULL);
1892   return TRUE;
1893 }
1894
1895 static gboolean
1896 recent_view_button_press_cb (GtkWidget      *widget,
1897                              GdkEventButton *event,
1898                              gpointer        user_data)
1899 {
1900   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1901   
1902   if (event->button == 3)
1903     {
1904       GtkTreePath *path;
1905       gboolean res;
1906
1907       if (event->window != gtk_tree_view_get_bin_window (GTK_TREE_VIEW (impl->recent_view)))
1908         return FALSE;
1909
1910       res = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (impl->recent_view),
1911                                            event->x, event->y,
1912                                            &path,
1913                                            NULL, NULL, NULL);
1914       if (!res)
1915         return FALSE;
1916
1917       /* select the path before creating the popup menu */
1918       gtk_tree_selection_select_path (impl->selection, path);
1919       gtk_tree_path_free (path);
1920       
1921       recent_view_menu_popup (impl, event);
1922
1923       return TRUE;
1924     }
1925   
1926   return FALSE;
1927 }
1928
1929 static void
1930 set_recent_manager (GtkRecentChooserDefault *impl,
1931                     GtkRecentManager        *manager)
1932 {
1933   if (impl->manager)
1934     {
1935       if (impl->manager_changed_id)
1936         {
1937           g_signal_handler_disconnect (impl, impl->manager_changed_id);
1938           impl->manager_changed_id = 0;
1939         }
1940
1941       impl->manager = NULL;
1942     }
1943   
1944   if (manager)
1945     impl->manager = manager;
1946   else
1947     impl->manager = gtk_recent_manager_get_default ();
1948   
1949   if (impl->manager)
1950     {
1951       impl->manager_changed_id = g_signal_connect (impl->manager, "changed",
1952                                                    G_CALLBACK (recent_manager_changed_cb),
1953                                                    impl);
1954     }
1955 }
1956
1957 static void
1958 gtk_recent_chooser_update (GtkActivatable *activatable,
1959                            GtkAction      *action,
1960                            const gchar    *property_name)
1961 {
1962   if (strcmp (property_name, "visible") == 0)
1963     {
1964       if (gtk_action_is_visible (action))
1965         gtk_widget_show (GTK_WIDGET (activatable));
1966       else
1967         gtk_widget_hide (GTK_WIDGET (activatable));
1968     }
1969
1970   if (strcmp (property_name, "sensitive") == 0)
1971     gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1972
1973   _gtk_recent_chooser_update (activatable, action, property_name);
1974 }
1975
1976
1977 static void 
1978 gtk_recent_chooser_sync_action_properties (GtkActivatable *activatable,
1979                                            GtkAction      *action)
1980 {
1981   if (action)
1982     {
1983       if (gtk_action_is_visible (action))
1984         gtk_widget_show (GTK_WIDGET (activatable));
1985       else
1986         gtk_widget_hide (GTK_WIDGET (activatable));
1987       
1988       gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1989     }
1990
1991   _gtk_recent_chooser_sync_action_properties (activatable, action);
1992 }
1993
1994
1995 GtkWidget *
1996 _gtk_recent_chooser_default_new (GtkRecentManager *manager)
1997 {
1998   return g_object_new (GTK_TYPE_RECENT_CHOOSER_DEFAULT,
1999                        "recent-manager", manager,
2000                        NULL);
2001 }