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