gtkrecentchooserdefault.c 58.3 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 * gtkrecentchooserdefault.c
 * Copyright (C) 2005-2006, Emmanuele Bassi
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "gtkstock.h"
#include "gtkicontheme.h"
#include "gtkiconfactory.h"
#include "gtksettings.h"
#include "gtktreeview.h"
#include "gtkliststore.h"
#include "gtkbutton.h"
#include "gtkcelllayout.h"
#include "gtkcellrendererpixbuf.h"
#include "gtkcellrenderertext.h"
#include "gtkcheckmenuitem.h"
#include "gtkclipboard.h"
44
#include "gtkcomboboxtext.h"
45 46 47 48
#include "gtkentry.h"
#include "gtkeventbox.h"
#include "gtkexpander.h"
#include "gtkframe.h"
Matthias Clasen's avatar
Matthias Clasen committed
49 50
#include "gtkbox.h"
#include "gtkpaned.h"
51 52 53 54 55 56 57 58 59
#include "gtkimage.h"
#include "gtkimagemenuitem.h"
#include "gtkintl.h"
#include "gtklabel.h"
#include "gtkmenuitem.h"
#include "gtkmessagedialog.h"
#include "gtkscrolledwindow.h"
#include "gtkseparatormenuitem.h"
#include "gtksizegroup.h"
60
#include "gtksizerequest.h"
61 62 63 64 65
#include "gtktable.h"
#include "gtktreemodelsort.h"
#include "gtktreemodelfilter.h"
#include "gtktreeselection.h"
#include "gtktreestore.h"
66
#include "gtktooltip.h"
67
#include "gtktypebuiltins.h"
68
#include "gtkorientable.h"
69
#include "gtkactivatable.h"
70 71 72 73 74 75 76 77

#include "gtkrecentmanager.h"
#include "gtkrecentfilter.h"
#include "gtkrecentchooser.h"
#include "gtkrecentchooserprivate.h"
#include "gtkrecentchooserutils.h"
#include "gtkrecentchooserdefault.h"

78
#include "gtkprivate.h"
79

80

81 82 83 84 85 86 87 88 89
enum 
{
  PROP_0,

  /* activatable properties */
  PROP_ACTIVATABLE_RELATED_ACTION,
  PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
};

90 91 92

struct _GtkRecentChooserDefault
{
93 94
  GtkBox parent_instance;

95 96 97
  GtkRecentManager *manager;
  gulong manager_changed_id;
  guint local_manager : 1;
98

99 100 101
  gint icon_size;

  /* RecentChooser properties */
102
  gint limit;
103 104 105 106 107 108 109
  GtkRecentSortType sort_type;
  guint show_private : 1;
  guint show_not_found : 1;
  guint select_multiple : 1;
  guint show_tips : 1;
  guint show_icons : 1;
  guint local_only : 1;
110 111

  guint limit_set : 1;
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  
  GSList *filters;
  GtkRecentFilter *current_filter;
  GtkWidget *filter_combo_hbox;
  GtkWidget *filter_combo;
  
  GtkRecentSortFunc sort_func;
  gpointer sort_data;
  GDestroyNotify sort_data_destroy;

  GtkIconTheme *icon_theme;
  
  GtkWidget *recent_view;
  GtkListStore *recent_store;
  GtkTreeViewColumn *icon_column;
  GtkTreeViewColumn *meta_column;
  GtkCellRenderer *meta_renderer;
  GtkTreeSelection *selection;
  
  GtkWidget *recent_popup_menu;
  GtkWidget *recent_popup_menu_copy_item;
  GtkWidget *recent_popup_menu_remove_item;
  GtkWidget *recent_popup_menu_clear_item;
  GtkWidget *recent_popup_menu_show_private_item;
 
  guint load_id;
  GList *recent_items;
  gint n_recent_items;
  gint loaded_items;
  guint load_state;
};

typedef struct _GtkRecentChooserDefaultClass
{
146
  GtkBoxClass parent_class;
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
} GtkRecentChooserDefaultClass;

enum {
  RECENT_URI_COLUMN,
  RECENT_DISPLAY_NAME_COLUMN,
  RECENT_INFO_COLUMN,
  N_RECENT_COLUMNS
};

enum {
  LOAD_EMPTY,    /* initial state: the model is empty */
  LOAD_PRELOAD,  /* the model is loading and not inserted in the tree yet */
  LOAD_LOADING,  /* the model is fully loaded but not inserted */
  LOAD_FINISHED  /* the model is fully loaded and inserted */
};

/* Icon size for if we can't get it from the theme */
#define FALLBACK_ICON_SIZE  48
#define FALLBACK_ITEM_LIMIT 20

#define NUM_CHARS 40
#define NUM_LINES 9



/* GObject */
173 174
static void     _gtk_recent_chooser_default_class_init  (GtkRecentChooserDefaultClass *klass);
static void     _gtk_recent_chooser_default_init        (GtkRecentChooserDefault      *impl);
175 176 177 178
static GObject *gtk_recent_chooser_default_constructor  (GType                         type,
						         guint                         n_construct_prop,
						         GObjectConstructParam        *construct_params);
static void     gtk_recent_chooser_default_finalize     (GObject                      *object);
179
static void     gtk_recent_chooser_default_dispose      (GObject                      *object);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
static void     gtk_recent_chooser_default_set_property (GObject                      *object,
						         guint                         prop_id,
						         const GValue                 *value,
						         GParamSpec                   *pspec);
static void     gtk_recent_chooser_default_get_property (GObject                      *object,
						         guint                         prop_id,
						         GValue                       *value,
						         GParamSpec                   *pspec);

/* GtkRecentChooserIface */
static void              gtk_recent_chooser_iface_init                 (GtkRecentChooserIface  *iface);
static gboolean          gtk_recent_chooser_default_set_current_uri    (GtkRecentChooser       *chooser,
								        const gchar            *uri,
								        GError                **error);
static gchar *           gtk_recent_chooser_default_get_current_uri    (GtkRecentChooser       *chooser);
static gboolean          gtk_recent_chooser_default_select_uri         (GtkRecentChooser       *chooser,
								        const gchar            *uri,
								        GError                **error);
static void              gtk_recent_chooser_default_unselect_uri       (GtkRecentChooser       *chooser,
								        const gchar            *uri);
static void              gtk_recent_chooser_default_select_all         (GtkRecentChooser       *chooser);
static void              gtk_recent_chooser_default_unselect_all       (GtkRecentChooser       *chooser);
static GList *           gtk_recent_chooser_default_get_items          (GtkRecentChooser       *chooser);
static GtkRecentManager *gtk_recent_chooser_default_get_recent_manager (GtkRecentChooser       *chooser);
static void              gtk_recent_chooser_default_set_sort_func      (GtkRecentChooser       *chooser,
									GtkRecentSortFunc       sort_func,
									gpointer                sort_data,
									GDestroyNotify          data_destroy);
static void              gtk_recent_chooser_default_add_filter         (GtkRecentChooser       *chooser,
								        GtkRecentFilter        *filter);
static void              gtk_recent_chooser_default_remove_filter      (GtkRecentChooser       *chooser,
								        GtkRecentFilter        *filter);
static GSList *          gtk_recent_chooser_default_list_filters       (GtkRecentChooser       *chooser);


static void gtk_recent_chooser_default_map      (GtkWidget *widget);
static void gtk_recent_chooser_default_show_all (GtkWidget *widget);

static void set_current_filter        (GtkRecentChooserDefault *impl,
				       GtkRecentFilter         *filter);

static GtkIconTheme *get_icon_theme_for_widget (GtkWidget   *widget);
static gint          get_icon_size_for_widget  (GtkWidget   *widget,
						GtkIconSize  icon_size);
224
static gint          get_recent_files_limit    (GtkWidget   *widget);
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

static void reload_recent_items (GtkRecentChooserDefault *impl);
static void chooser_set_model   (GtkRecentChooserDefault *impl);

static void set_recent_manager (GtkRecentChooserDefault *impl,
				GtkRecentManager        *manager);

static void chooser_set_sort_type (GtkRecentChooserDefault *impl,
				   GtkRecentSortType        sort_type);

static void recent_manager_changed_cb (GtkRecentManager  *manager,
			               gpointer           user_data);
static void recent_icon_data_func     (GtkTreeViewColumn *tree_column,
				       GtkCellRenderer   *cell,
				       GtkTreeModel      *model,
				       GtkTreeIter       *iter,
				       gpointer           user_data);
static void recent_meta_data_func     (GtkTreeViewColumn *tree_column,
				       GtkCellRenderer   *cell,
				       GtkTreeModel      *model,
				       GtkTreeIter       *iter,
				       gpointer           user_data);

static void selection_changed_cb      (GtkTreeSelection  *z,
				       gpointer           user_data);
static void row_activated_cb          (GtkTreeView       *tree_view,
				       GtkTreePath       *tree_path,
				       GtkTreeViewColumn *tree_column,
				       gpointer           user_data);
static void filter_combo_changed_cb   (GtkComboBox       *combo_box,
				       gpointer           user_data);

static void remove_all_activated_cb   (GtkMenuItem       *menu_item,
				       gpointer           user_data);
static void remove_item_activated_cb  (GtkMenuItem       *menu_item,
				       gpointer           user_data);
static void show_private_toggled_cb   (GtkCheckMenuItem  *menu_item,
				       gpointer           user_data);

static gboolean recent_view_popup_menu_cb   (GtkWidget      *widget,
					     gpointer        user_data);
static gboolean recent_view_button_press_cb (GtkWidget      *widget,
					     GdkEventButton *event,
					     gpointer        user_data);

static void     recent_view_drag_begin_cb         (GtkWidget        *widget,
						   GdkDragContext   *context,
						   gpointer          user_data);
static void     recent_view_drag_data_get_cb      (GtkWidget        *widget,
						   GdkDragContext   *context,
						   GtkSelectionData *selection_data,
						   guint             info,
						   guint32           time_,
						   gpointer          data);
279 280 281 282 283 284 285
static gboolean recent_view_query_tooltip_cb      (GtkWidget        *widget,
                                                   gint              x,
                                                   gint              y,
                                                   gboolean          keyboard_tip,
                                                   GtkTooltip       *tooltip,
                                                   gpointer          user_data);

286
static void gtk_recent_chooser_activatable_iface_init (GtkActivatableIface  *iface);
287
static void gtk_recent_chooser_update                 (GtkActivatable       *activatable,
288 289
						       GtkAction            *action,
						       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_BOX,
296
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
297 298 299
				 		gtk_recent_chooser_iface_init)
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
				 		gtk_recent_chooser_activatable_iface_init))
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320




static void
gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface)
{
  iface->set_current_uri = gtk_recent_chooser_default_set_current_uri;
  iface->get_current_uri = gtk_recent_chooser_default_get_current_uri;
  iface->select_uri = gtk_recent_chooser_default_select_uri;
  iface->unselect_uri = gtk_recent_chooser_default_unselect_uri;
  iface->select_all = gtk_recent_chooser_default_select_all;
  iface->unselect_all = gtk_recent_chooser_default_unselect_all;
  iface->get_items = gtk_recent_chooser_default_get_items;
  iface->get_recent_manager = gtk_recent_chooser_default_get_recent_manager;
  iface->set_sort_func = gtk_recent_chooser_default_set_sort_func;
  iface->add_filter = gtk_recent_chooser_default_add_filter;
  iface->remove_filter = gtk_recent_chooser_default_remove_filter;
  iface->list_filters = gtk_recent_chooser_default_list_filters;
}

321 322
static void
gtk_recent_chooser_activatable_iface_init (GtkActivatableIface *iface)
323

324 325 326
{
  iface->update = gtk_recent_chooser_update;
  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 333 334
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

335
  gobject_class->constructor = gtk_recent_chooser_default_constructor;
336 337
  gobject_class->set_property = gtk_recent_chooser_default_set_property;
  gobject_class->get_property = gtk_recent_chooser_default_get_property;
338 339
  gobject_class->dispose = gtk_recent_chooser_default_dispose;
  gobject_class->finalize = gtk_recent_chooser_default_finalize;
340 341 342 343 344
  
  widget_class->map = gtk_recent_chooser_default_map;
  widget_class->show_all = gtk_recent_chooser_default_show_all;
  
  _gtk_recent_chooser_install_properties (gobject_class);
345 346 347

  g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
  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 356 357
  gtk_orientable_set_orientation (GTK_ORIENTABLE (impl),
                                  GTK_ORIENTATION_VERTICAL);

358 359
  /* by default, we use the global manager */
  impl->local_manager = FALSE;
360

361 362 363 364 365
  impl->limit = FALLBACK_ITEM_LIMIT;
  impl->sort_type = GTK_RECENT_SORT_NONE;

  impl->show_icons = TRUE;
  impl->show_private = FALSE;
366
  impl->show_not_found = TRUE;
367
  impl->show_tips = FALSE;
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
  impl->select_multiple = FALSE;
  impl->local_only = TRUE;
  
  impl->icon_size = FALLBACK_ICON_SIZE;
  impl->icon_theme = NULL;
  
  impl->current_filter = NULL;

  impl->recent_items = NULL;
  impl->n_recent_items = 0;
  impl->loaded_items = 0;
  
  impl->load_state = LOAD_EMPTY;
}

static GObject *
gtk_recent_chooser_default_constructor (GType                  type,
385 386
				        guint                  n_params,
				        GObjectConstructParam *params)
387
{
388
  GObjectClass *parent_class;
389 390 391 392 393
  GtkRecentChooserDefault *impl;
  GObject *object;
  GtkWidget *scrollw;
  GtkCellRenderer *renderer;

394 395
  parent_class = G_OBJECT_CLASS (_gtk_recent_chooser_default_parent_class);
  object = parent_class->constructor (type, n_params, params);
396 397 398 399 400
  impl = GTK_RECENT_CHOOSER_DEFAULT (object);
  
  g_assert (impl->manager);
  
  gtk_widget_push_composite_child ();
401 402

  impl->limit = get_recent_files_limit (GTK_WIDGET (impl));
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
  
  scrollw = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrollw),
  				       GTK_SHADOW_IN);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollw),
  				  GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
  gtk_box_pack_start (GTK_BOX (impl), scrollw, TRUE, TRUE, 0);
  gtk_widget_show (scrollw);
  
  impl->recent_view = gtk_tree_view_new ();
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->recent_view), FALSE);
  g_signal_connect (impl->recent_view, "row-activated",
                    G_CALLBACK (row_activated_cb), impl);
  g_signal_connect (impl->recent_view, "popup-menu",
  		    G_CALLBACK (recent_view_popup_menu_cb), impl);
  g_signal_connect (impl->recent_view, "button-press-event",
  		    G_CALLBACK (recent_view_button_press_cb), impl);
420
  g_signal_connect (impl->recent_view, "drag-begin",
421
		    G_CALLBACK (recent_view_drag_begin_cb), impl);
422
  g_signal_connect (impl->recent_view, "drag-data-get",
423 424
		    G_CALLBACK (recent_view_drag_data_get_cb), impl);

425 426 427 428
  g_object_set (impl->recent_view, "has-tooltip", TRUE, NULL);
  g_signal_connect (impl->recent_view, "query-tooltip",
                    G_CALLBACK (recent_view_query_tooltip_cb), impl);

429 430
  g_object_set_data (G_OBJECT (impl->recent_view),
                     "GtkRecentChooserDefault", impl);
431 432 433 434 435 436 437 438 439
  
  gtk_container_add (GTK_CONTAINER (scrollw), impl->recent_view);
  gtk_widget_show (impl->recent_view);
  
  impl->icon_column = gtk_tree_view_column_new ();
  gtk_tree_view_column_set_expand (impl->icon_column, FALSE);
  gtk_tree_view_column_set_resizable (impl->icon_column, FALSE);
  
  renderer = gtk_cell_renderer_pixbuf_new ();
440
  g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  gtk_tree_view_column_pack_start (impl->icon_column, renderer, FALSE);
  gtk_tree_view_column_set_cell_data_func (impl->icon_column,
  					   renderer,
  					   recent_icon_data_func,
  					   impl,
  					   NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW (impl->recent_view),
                               impl->icon_column);
  
  impl->meta_column = gtk_tree_view_column_new ();
  gtk_tree_view_column_set_expand (impl->meta_column, TRUE);
  gtk_tree_view_column_set_resizable (impl->meta_column, FALSE);
  
  impl->meta_renderer = gtk_cell_renderer_text_new ();
  g_object_set (G_OBJECT (impl->meta_renderer),
                "ellipsize", PANGO_ELLIPSIZE_END,
                NULL);
  gtk_tree_view_column_pack_start (impl->meta_column, impl->meta_renderer, TRUE);
  gtk_tree_view_column_set_cell_data_func (impl->meta_column,
  					   impl->meta_renderer,
  					   recent_meta_data_func,
  					   impl,
  					   NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW (impl->recent_view),
                               impl->meta_column);
  
  impl->selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->recent_view));
  gtk_tree_selection_set_mode (impl->selection, GTK_SELECTION_SINGLE);
  g_signal_connect (impl->selection, "changed", G_CALLBACK (selection_changed_cb), impl);

  /* drag and drop */
  gtk_drag_source_set (impl->recent_view,
		       GDK_BUTTON1_MASK,
474
		       NULL, 0,
475
		       GDK_ACTION_COPY);
476
  gtk_drag_source_add_uri_targets (impl->recent_view);
477

478 479
  impl->filter_combo_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);

480
  impl->filter_combo = gtk_combo_box_text_new ();
481 482 483
  gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
  g_signal_connect (impl->filter_combo, "changed",
                    G_CALLBACK (filter_combo_changed_cb), impl);
484 485
  gtk_widget_set_tooltip_text (impl->filter_combo,
		               _("Select which type of documents are shown"));
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
  
  gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox),
                    impl->filter_combo,
                    FALSE, FALSE, 0);
  gtk_widget_show (impl->filter_combo);
  
  gtk_box_pack_end (GTK_BOX (impl), impl->filter_combo_hbox, FALSE, FALSE, 0);
  
  gtk_widget_pop_composite_child ();
  
  impl->recent_store = gtk_list_store_new (N_RECENT_COLUMNS,
  					   G_TYPE_STRING,       /* uri */
  					   G_TYPE_STRING,       /* display_name */
  					   GTK_TYPE_RECENT_INFO /* info */);
  
  return object;
}

static void
gtk_recent_chooser_default_set_property (GObject      *object,
				         guint         prop_id,
					 const GValue *value,
					 GParamSpec   *pspec)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);
  
  switch (prop_id)
    {
    case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
      set_recent_manager (impl, g_value_get_object (value));
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
      impl->show_private = g_value_get_boolean (value);
      if (impl->recent_popup_menu_show_private_item)
	{
          GtkCheckMenuItem *item = GTK_CHECK_MENU_ITEM (impl->recent_popup_menu_show_private_item);
	  g_signal_handlers_block_by_func (item, G_CALLBACK (show_private_toggled_cb), impl);
          gtk_check_menu_item_set_active (item, impl->show_private);
	  g_signal_handlers_unblock_by_func (item, G_CALLBACK (show_private_toggled_cb), impl);
        }
526
      reload_recent_items (impl);
527 528 529
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
      impl->show_not_found = g_value_get_boolean (value);
530
      reload_recent_items (impl);
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
      impl->show_tips = g_value_get_boolean (value);
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
      impl->show_icons = g_value_get_boolean (value);
      gtk_tree_view_column_set_visible (impl->icon_column, impl->show_icons);
      break;
    case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
      impl->select_multiple = g_value_get_boolean (value);
      
      if (impl->select_multiple)
        gtk_tree_selection_set_mode (impl->selection, GTK_SELECTION_MULTIPLE);
      else
        gtk_tree_selection_set_mode (impl->selection, GTK_SELECTION_SINGLE);
      break;
    case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
      impl->local_only = g_value_get_boolean (value);
549
      reload_recent_items (impl);
550 551 552
      break;
    case GTK_RECENT_CHOOSER_PROP_LIMIT:
      impl->limit = g_value_get_int (value);
553
      impl->limit_set = TRUE;
554
      reload_recent_items (impl);
555 556 557 558 559 560 561
      break;
    case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
      chooser_set_sort_type (impl, g_value_get_enum (value));
      break;
    case GTK_RECENT_CHOOSER_PROP_FILTER:
      set_current_filter (impl, g_value_get_object (value));
      break;
562 563 564 565 566 567
    case PROP_ACTIVATABLE_RELATED_ACTION:
      _gtk_recent_chooser_set_related_action (GTK_RECENT_CHOOSER (impl), g_value_get_object (value));
      break;
    case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE: 
      _gtk_recent_chooser_set_use_action_appearance (GTK_RECENT_CHOOSER (impl), g_value_get_boolean (value));
      break;
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_recent_chooser_default_get_property (GObject    *object,
					 guint       prop_id,
					 GValue     *value,
					 GParamSpec *pspec)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);
  
  switch (prop_id)
    {
    case GTK_RECENT_CHOOSER_PROP_LIMIT:
      g_value_set_int (value, impl->limit);
      break;
    case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
      g_value_set_enum (value, impl->sort_type);
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
      g_value_set_boolean (value, impl->show_private);
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
      g_value_set_boolean (value, impl->show_icons);
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
      g_value_set_boolean (value, impl->show_not_found);
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
      g_value_set_boolean (value, impl->show_tips);
      break;
    case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
      g_value_set_boolean (value, impl->local_only);
      break;
    case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
      g_value_set_boolean (value, impl->select_multiple);
      break;
    case GTK_RECENT_CHOOSER_PROP_FILTER:
      g_value_set_object (value, impl->current_filter);
      break;
611 612 613 614 615 616
    case PROP_ACTIVATABLE_RELATED_ACTION:
      g_value_set_object (value, _gtk_recent_chooser_get_related_action (GTK_RECENT_CHOOSER (impl)));
      break;
    case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE: 
      g_value_set_boolean (value, _gtk_recent_chooser_get_use_action_appearance (GTK_RECENT_CHOOSER (impl)));
      break;
617 618 619 620 621 622 623
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
624
gtk_recent_chooser_default_dispose (GObject *object)
625 626 627
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);

628 629 630
  if (impl->load_id)
    {
      g_source_remove (impl->load_id);
631
      impl->load_state = LOAD_EMPTY;
632 633 634
      impl->load_id = 0;
    }

635 636
  if (impl->recent_items)
    {
637
      g_list_foreach (impl->recent_items, (GFunc) gtk_recent_info_unref, NULL);
638 639 640 641
      g_list_free (impl->recent_items);
      impl->recent_items = NULL;
    }

642
  if (impl->manager && impl->manager_changed_id)
643 644 645 646 647 648 649
    {
      g_signal_handler_disconnect (impl->manager, impl->manager_changed_id);
      impl->manager_changed_id = 0;
    }

  if (impl->filters)
    {
650 651 652
      g_slist_foreach (impl->filters, (GFunc) g_object_unref, NULL);
      g_slist_free (impl->filters);
      impl->filters = NULL;
653 654 655
    }
  
  if (impl->current_filter)
656 657 658 659
    {
      g_object_unref (impl->current_filter);
      impl->current_filter = NULL;
    }
660 661

  if (impl->recent_store)
662 663 664 665
    {
      g_object_unref (impl->recent_store);
      impl->recent_store = NULL;
    }
666

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
  G_OBJECT_CLASS (_gtk_recent_chooser_default_parent_class)->dispose (object);
}

static void
gtk_recent_chooser_default_finalize (GObject *object)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);

  impl->manager = NULL; 
  
  if (impl->sort_data_destroy)
    {
      impl->sort_data_destroy (impl->sort_data);
      impl->sort_data_destroy = NULL;
    }
  
  impl->sort_data = NULL;
  impl->sort_func = NULL;
685
  
686
  G_OBJECT_CLASS (_gtk_recent_chooser_default_parent_class)->finalize (object);
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
}

/* override GtkWidget::show_all since we have internal widgets we wish to keep
 * hidden unless we decide otherwise, like the filter combo box.
 */
static void
gtk_recent_chooser_default_show_all (GtkWidget *widget)
{
  gtk_widget_show (widget);
}



/* Shows an error dialog set as transient for the specified window */
static void
error_message_with_parent (GtkWindow   *parent,
			   const gchar *msg,
			   const gchar *detail)
{
  GtkWidget *dialog;

  dialog = gtk_message_dialog_new (parent,
				   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
				   GTK_MESSAGE_ERROR,
				   GTK_BUTTONS_OK,
				   "%s",
				   msg);
  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
					    "%s", detail);

717 718 719
  if (gtk_window_has_group (parent))
    gtk_window_group_add_window (gtk_window_get_group (parent),
                                 GTK_WINDOW (dialog));
720 721 722 723 724 725 726 727 728 729 730 731

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

/* Returns a toplevel GtkWindow, or NULL if none */
static GtkWindow *
get_toplevel (GtkWidget *widget)
{
  GtkWidget *toplevel;

  toplevel = gtk_widget_get_toplevel (widget);
732
  if (!gtk_widget_is_toplevel (toplevel))
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
    return NULL;
  else
    return GTK_WINDOW (toplevel);
}

/* Shows an error dialog for the file chooser */
static void
error_message (GtkRecentChooserDefault *impl,
	       const gchar             *msg,
	       const gchar             *detail)
{
  error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
}

static void
set_busy_cursor (GtkRecentChooserDefault *impl,
		 gboolean                 show_busy_cursor)
{
  GtkWindow *toplevel;
  GdkDisplay *display;
  GdkCursor *cursor;

  toplevel = get_toplevel (GTK_WIDGET (impl));
756
  if (!toplevel || !gtk_widget_get_realized (GTK_WIDGET (toplevel)))
757 758 759 760 761 762 763 764
    return;
  
  display = gtk_widget_get_display (GTK_WIDGET (toplevel));
  
  cursor = NULL;
  if (show_busy_cursor)
    cursor = gdk_cursor_new_for_display (display, GDK_WATCH);

765 766
  gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (toplevel)),
                         cursor);
767 768 769
  gdk_display_flush (display);

  if (cursor)
770
    g_object_unref (cursor);
771 772 773 774 775 776 777
}

static void
chooser_set_model (GtkRecentChooserDefault *impl)
{
  g_assert (impl->recent_store != NULL);
  g_assert (impl->load_state == LOAD_LOADING);
778

779
  gtk_tree_view_set_model (GTK_TREE_VIEW (impl->recent_view),
780
                           GTK_TREE_MODEL (impl->recent_store));
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
  gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->recent_view));
  gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->recent_view), TRUE);
  gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->recent_view),
  				   RECENT_DISPLAY_NAME_COLUMN);

  impl->load_state = LOAD_FINISHED;
}

static gboolean
load_recent_items (gpointer user_data)
{
  GtkRecentChooserDefault *impl;
  GtkRecentInfo *info;
  GtkTreeIter iter;
  const gchar *uri, *name;
  gboolean retval;
  
  impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
  
  g_assert ((impl->load_state == LOAD_EMPTY) ||
            (impl->load_state == LOAD_PRELOAD));
  
  /* store the items for multiple runs */
  if (!impl->recent_items)
    {
      impl->recent_items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (impl));
      if (!impl->recent_items)
        {
	  impl->load_state = LOAD_FINISHED;
          
          return FALSE;
        }
        
      impl->n_recent_items = g_list_length (impl->recent_items);
      impl->loaded_items = 0;
      impl->load_state = LOAD_PRELOAD;
    }
  
  info = (GtkRecentInfo *) g_list_nth_data (impl->recent_items,
                                            impl->loaded_items);
  g_assert (info);

  uri = gtk_recent_info_get_uri (info);
  name = gtk_recent_info_get_display_name (info);
  
  /* at this point, everything goes inside the model; operations on the
   * visualization of items inside the model are done in the cell data
   * funcs (remember that there are two of those: one for the icon and
   * one for the text), while the filtering is done only when a filter
   * is actually loaded. */
  gtk_list_store_append (impl->recent_store, &iter);
  gtk_list_store_set (impl->recent_store, &iter,
  		      RECENT_URI_COLUMN, uri,           /* uri  */
  		      RECENT_DISPLAY_NAME_COLUMN, name, /* display_name */
  		      RECENT_INFO_COLUMN, info,         /* info */
  		      -1);
  
  impl->loaded_items += 1;

  if (impl->loaded_items == impl->n_recent_items)
    {
      /* we have finished loading, so we remove the items cache */
      impl->load_state = LOAD_LOADING;
      
      g_list_foreach (impl->recent_items,
      		      (GFunc) gtk_recent_info_unref,
      		      NULL);
      g_list_free (impl->recent_items);
      
      impl->recent_items = NULL;
      impl->n_recent_items = 0;
      impl->loaded_items = 0;
853

854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
      /* load the filled up model */
      chooser_set_model (impl);

      retval = FALSE;
    }
  else
    {
      /* we did not finish, so continue loading */
      retval = TRUE;
    }
  
  return retval;
}

static void
cleanup_after_load (gpointer user_data)
{
  GtkRecentChooserDefault *impl;
  
  impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);

  if (impl->load_id != 0)
    {
877 878
      g_assert ((impl->load_state == LOAD_EMPTY) ||
                (impl->load_state == LOAD_PRELOAD) ||
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
		(impl->load_state == LOAD_LOADING) ||
		(impl->load_state == LOAD_FINISHED));
      
      /* we have officialy finished loading all the items,
       * so we can reset the state machine
       */
      g_source_remove (impl->load_id);
      impl->load_id = 0;
      impl->load_state = LOAD_EMPTY;
    }
  else
    g_assert ((impl->load_state == LOAD_EMPTY) ||
	      (impl->load_state == LOAD_LOADING) ||
	      (impl->load_state == LOAD_FINISHED));

  set_busy_cursor (impl, FALSE);
}

/* clears the current model and reloads the recently used resources */
static void
reload_recent_items (GtkRecentChooserDefault *impl)
{
901 902
  GtkWidget *widget;

903 904 905 906
  /* reload is already in progress - do not disturb */
  if (impl->load_id)
    return;
  
907 908
  widget = GTK_WIDGET (impl);

909 910 911 912
  gtk_tree_view_set_model (GTK_TREE_VIEW (impl->recent_view), NULL);
  gtk_list_store_clear (impl->recent_store);
  
  if (!impl->icon_theme)
913
    impl->icon_theme = get_icon_theme_for_widget (widget);
914

915
  impl->icon_size = get_icon_size_for_widget (widget,
916 917
		  			      GTK_ICON_SIZE_BUTTON);

918 919 920
  if (!impl->limit_set)
    impl->limit = get_recent_files_limit (widget);

921 922 923
  set_busy_cursor (impl, TRUE);

  impl->load_state = LOAD_EMPTY;
924
  impl->load_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
925 926 927
                                             load_recent_items,
                                             impl,
                                             cleanup_after_load);
928 929 930 931 932 933
}

/* taken form gtkfilechooserdialog.c */
static void
set_default_size (GtkRecentChooserDefault *impl)
{
934
  GtkScrolledWindow *scrollw;
935 936 937 938 939 940 941
  GtkWidget *widget;
  gint width, height;
  gint font_size;
  GdkScreen *screen;
  gint monitor_num;
  GtkRequisition req;
  GdkRectangle monitor;
942 943
  GtkStyleContext *context;
  GtkStateFlags state;
944 945

  widget = GTK_WIDGET (impl);
946 947
  context = gtk_widget_get_style_context (widget);
  state = gtk_widget_get_state_flags (widget);
948 949

  /* Size based on characters and the icon size */
950
  font_size = pango_font_description_get_size (gtk_style_context_get_font (context, state));
951 952 953 954 955 956
  font_size = PANGO_PIXELS (font_size);

  width = impl->icon_size + font_size * NUM_CHARS;
  height = (impl->icon_size + font_size) * NUM_LINES;

  /* Use at least the requisition size... */
957
  gtk_widget_get_preferred_size (widget, &req, NULL);
958 959 960 961 962
  width = MAX (width, req.width);
  height = MAX (height, req.height);

  /* ... but no larger than the monitor */
  screen = gtk_widget_get_screen (widget);
963 964
  monitor_num = gdk_screen_get_monitor_at_window (screen,
                                                  gtk_widget_get_window (widget));
965 966 967 968 969 970 971

  gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);

  width = MIN (width, monitor.width * 3 / 4);
  height = MIN (height, monitor.height * 3 / 4);

  /* Set size */
972 973 974
  scrollw = GTK_SCROLLED_WINDOW (gtk_widget_get_parent (impl->recent_view));
  gtk_scrolled_window_set_min_content_width (scrollw, width);
  gtk_scrolled_window_set_min_content_height (scrollw, height);
975 976 977 978 979 980
}

static void
gtk_recent_chooser_default_map (GtkWidget *widget)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (widget);
981 982

  GTK_WIDGET_CLASS (_gtk_recent_chooser_default_parent_class)->map (widget);
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997

  /* reloads everything */
  reload_recent_items (impl);

  set_default_size (impl);
}

static void
recent_icon_data_func (GtkTreeViewColumn *tree_column,
		       GtkCellRenderer   *cell,
		       GtkTreeModel      *model,
		       GtkTreeIter       *iter,
		       gpointer           user_data)
{
  GtkRecentInfo *info = NULL;
998 999 1000
  GIcon *icon;

  gtk_tree_model_get (model, iter, RECENT_INFO_COLUMN, &info, -1);
1001
  g_assert (info != NULL);
1002 1003 1004 1005 1006 1007

  icon = gtk_recent_info_get_gicon (info);
  g_object_set (cell, "gicon", icon, NULL);

  if (icon != NULL)
    g_object_unref (icon);
1008 1009

  gtk_recent_info_unref (info);
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
}

static void
recent_meta_data_func (GtkTreeViewColumn *tree_column,
		       GtkCellRenderer   *cell,
		       GtkTreeModel      *model,
		       GtkTreeIter       *iter,
		       gpointer           user_data)
{
  GtkRecentInfo *info = NULL;
1020
  gchar *name;
1021 1022 1023 1024 1025 1026 1027 1028 1029
  
  gtk_tree_model_get (model, iter,
                      RECENT_DISPLAY_NAME_COLUMN, &name,
                      RECENT_INFO_COLUMN, &info,
                      -1);
  g_assert (info != NULL);
  
  if (!name)
    name = gtk_recent_info_get_short_name (info);
1030

1031
  g_object_set (cell, "text", name, NULL);
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
  
  g_free (name);
  gtk_recent_info_unref (info);
}


static gchar *
gtk_recent_chooser_default_get_current_uri (GtkRecentChooser *chooser)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  
  g_assert (impl->selection != NULL);
  
  if (!impl->select_multiple)
    {
      GtkTreeModel *model;
      GtkTreeIter iter;
      gchar *uri = NULL;
      
      if (!gtk_tree_selection_get_selected (impl->selection, &model, &iter))
        return NULL;
      
      gtk_tree_model_get (model, &iter, RECENT_URI_COLUMN, &uri, -1);
      
      return uri;
    }
  
  return NULL;
}

typedef struct
{
  guint found : 1;
  guint do_select : 1;
  guint do_activate : 1;
  
  gchar *uri;
  
  GtkRecentChooserDefault *impl;
} SelectURIData;

static gboolean
scan_for_uri_cb (GtkTreeModel *model,
		 GtkTreePath  *path,
		 GtkTreeIter  *iter,
		 gpointer      user_data)
{
  SelectURIData *select_data = (SelectURIData *) user_data;
1080
  gchar *uri = NULL;
1081 1082 1083 1084 1085 1086 1087 1088
  
  if (!select_data)
    return TRUE;
  
  if (select_data->found)
    return TRUE;
  
  gtk_tree_model_get (model, iter, RECENT_URI_COLUMN, &uri, -1);
1089 1090 1091 1092
  if (!uri)
    return FALSE;
  
  if (strcmp (uri, select_data->uri) == 0)
1093 1094 1095 1096
    {
      select_data->found = TRUE;
      
      if (select_data->do_activate)
1097 1098 1099
        gtk_tree_view_row_activated (GTK_TREE_VIEW (select_data->impl->recent_view),
        			     path,
        			     select_data->impl->meta_column);
1100 1101
      
      if (select_data->do_select)
1102
        gtk_tree_selection_select_path (select_data->impl->selection, path);
1103
      else
1104 1105 1106
        gtk_tree_selection_unselect_path (select_data->impl->selection, path);

      g_free (uri);
1107 1108 1109
      
      return TRUE;
    }
1110 1111

  g_free (uri);
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
  
  return FALSE;
}

static gboolean
gtk_recent_chooser_default_set_current_uri (GtkRecentChooser  *chooser,
					    const gchar       *uri,
					    GError           **error)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  SelectURIData *data;
  
  data = g_new0 (SelectURIData, 1);
  data->uri = g_strdup (uri);
  data->impl = impl;
  data->found = FALSE;
  data->do_activate = TRUE;
  data->do_select = TRUE;
  
  gtk_tree_model_foreach (GTK_TREE_MODEL (impl->recent_store),
  			  scan_for_uri_cb,
  			  data);
  
  if (!data->found)
    {
      g_free (data->uri);
      g_free (data);
      
      g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
      		   GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
      		   _("No item for URI '%s' found"),
      		   uri);
      return FALSE;
    }
  
  g_free (data->uri);
  g_free (data);

  return TRUE;
}

static gboolean
gtk_recent_chooser_default_select_uri (GtkRecentChooser  *chooser,
				       const gchar       *uri,
				       GError           **error)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  SelectURIData *data;
  
  data = g_new0 (SelectURIData, 1);
  data->uri = g_strdup (uri);
  data->impl = impl;
  data->found = FALSE;
  data->do_activate = FALSE;
  data->do_select = TRUE;
  
  gtk_tree_model_foreach (GTK_TREE_MODEL (impl->recent_store),
  			  scan_for_uri_cb,
  			  data);
  
  if (!data->found)
    {
      g_free (data->uri);
      g_free (data);
      
      g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
      		   GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
      		   _("No item for URI '%s' found"),
      		   uri);
      return FALSE;
    }
  
  g_free (data->uri);
  g_free (data);

  return TRUE;
}

static void
gtk_recent_chooser_default_unselect_uri (GtkRecentChooser *chooser,
					 const gchar      *uri)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  SelectURIData *data;
  
  data = g_new0 (SelectURIData, 1);
  data->uri = g_strdup (uri);
  data->impl = impl;
  data->found = FALSE;
  data->do_activate = FALSE;
  data->do_select = FALSE;
  
  gtk_tree_model_foreach (GTK_TREE_MODEL (impl->recent_store),
  			  scan_for_uri_cb,
  			  data);
  
  g_free (data->uri);
  g_free (data);
}

static void
gtk_recent_chooser_default_select_all (GtkRecentChooser *chooser)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  
  if (!impl->select_multiple)
    return;
  
  gtk_tree_selection_select_all (impl->selection);
}

static void
gtk_recent_chooser_default_unselect_all (GtkRecentChooser *chooser)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  
  gtk_tree_selection_unselect_all (impl->selection);
}

static void
gtk_recent_chooser_default_set_sort_func (GtkRecentChooser  *chooser,
					  GtkRecentSortFunc  sort_func,
					  gpointer           sort_data,
					  GDestroyNotify     data_destroy)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  
  if (impl->sort_data_destroy)
    {
      impl->sort_data_destroy (impl->sort_data);
      impl->sort_data_destroy = NULL;
    }
1244 1245 1246
      
  impl->sort_func = NULL;
  impl->sort_data = NULL;
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
  
  if (sort_func)
    {
      impl->sort_func = sort_func;
      impl->sort_data = sort_data;
      impl->sort_data_destroy = data_destroy;
    }
}

static GList *
gtk_recent_chooser_default_get_items (GtkRecentChooser *chooser)
{
  GtkRecentChooserDefault *impl;

1261
  impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1262

1263
  return _gtk_recent_chooser_get_items (chooser,
1264
                                        impl->current_filter,
1265 1266
                                        impl->sort_func,
                                        impl->sort_data);
1267 1268 1269 1270 1271
}

static GtkRecentManager *
gtk_recent_chooser_default_get_recent_manager (GtkRecentChooser *chooser)
{
1272
  return GTK_RECENT_CHOOSER_DEFAULT (chooser)->manager;
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
}

static void
show_filters (GtkRecentChooserDefault *impl,
              gboolean                 show)
{
  if (show)
    gtk_widget_show (impl->filter_combo_hbox);
  else
    gtk_widget_hide (impl->filter_combo_hbox);
}

static void
gtk_recent_chooser_default_add_filter (GtkRecentChooser *chooser,
				       GtkRecentFilter  *filter)
{
  GtkRecentChooserDefault *impl;
  const gchar *name;

  impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  
  if (g_slist_find (impl->filters, filter))
    {
      g_warning ("gtk_recent_chooser_add_filter() called on filter already in list\n");
      return;
    }
  
  g_object_ref_sink (filter);
  impl->filters = g_slist_append (impl->filters, filter);
  
  /* display new filter */
  name = gtk_recent_filter_get_name (filter);
  if (!name)
1306
    name = _("Untitled filter");
1307 1308 1309

  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->filter_combo), name);

1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
  if (!g_slist_find (impl->filters, impl->current_filter))
    set_current_filter (impl, filter);
  
  show_filters (impl, TRUE);
}

static void
gtk_recent_chooser_default_remove_filter (GtkRecentChooser *chooser,
					  GtkRecentFilter  *filter)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  GtkTreeModel *model;
  GtkTreeIter iter;
  gint filter_idx;
  
  filter_idx = g_slist_index (impl->filters, filter);
  
  if (filter_idx < 0)
    {
      g_warning ("gtk_recent_chooser_remove_filter() called on filter not in list\n");
      return;  
    }
  
  impl->filters = g_slist_remove (impl->filters, filter);
  
  if (filter == impl->current_filter)
    {
      if (impl->filters)
        set_current_filter (impl, impl->filters->data);
      else
        set_current_filter (impl, NULL);
    }
  
  model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
  gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_idx);
  gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
  
  g_object_unref (filter);
  
  if (!impl->filters)
    show_filters (impl, FALSE);
}

static GSList *
gtk_recent_chooser_default_list_filters (GtkRecentChooser *chooser)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  
  return g_slist_copy (impl->filters);
}

static void
set_current_filter (GtkRecentChooserDefault *impl,
		    GtkRecentFilter         *filter)
{
  if (impl->current_filter != filter)
    {
      gint filter_idx;
      
      filter_idx = g_slist_index (impl->filters, filter);
      if (impl->filters && filter && filter_idx < 0)
        return;
      
      if (impl->current_filter)
        g_object_unref (impl->current_filter);
      
      impl->current_filter = filter;
      
      if (impl->current_filter)     
        {
          g_object_ref_sink (impl->current_filter);
        }
      
      if (impl->filters)
        gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
                                  filter_idx);
      
1387 1388 1389
      if (impl->recent_store)
        reload_recent_items (impl);

1390 1391 1392 1393
      g_object_notify (G_OBJECT (impl), "filter");
    }
}

1394 1395 1396 1397
static void
chooser_set_sort_type (GtkRecentChooserDefault *impl,
		       GtkRecentSortType        sort_type)
{
1398 1399 1400 1401
  if (impl->sort_type != sort_type)
    {
      impl->sort_type = sort_type;
      reload_recent_items (impl);
1402

1403 1404
      g_object_notify (G_OBJECT (impl), "sort-type");
    }
1405 1406 1407
}


1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
static GtkIconTheme *
get_icon_theme_for_widget (GtkWidget *widget)
{
  if (gtk_widget_has_screen (widget))
    return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));

  return gtk_icon_theme_get_default ();
}

static gint
get_icon_size_for_widget (GtkWidget   *widget,
			  GtkIconSize  icon_size)
{
  GtkSettings *settings;
  gint width, height;

  if (gtk_widget_has_screen (widget))
    settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
  else
    settings = gtk_settings_get_default ();

  if (gtk_icon_size_lookup_for_settings (settings, icon_size,
                                         &width, &height))
    return MAX (width, height);

  return FALLBACK_ICON_SIZE;
}

1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
static gint
get_recent_files_limit (GtkWidget *widget)
{
  GtkSettings *settings;
  gint limit;

  if (gtk_widget_has_screen (widget))
    settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
  else
    settings = gtk_settings_get_default ();
  
  g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);

  return limit;
}
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572

static void
recent_manager_changed_cb (GtkRecentManager *manager,
			   gpointer          user_data)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);

  reload_recent_items (impl);
}

static void
selection_changed_cb (GtkTreeSelection *selection,
		      gpointer          user_data)
{
  _gtk_recent_chooser_selection_changed (GTK_RECENT_CHOOSER (user_data));
}

static void
row_activated_cb (GtkTreeView       *tree_view,
		  GtkTreePath       *tree_path,
		  GtkTreeViewColumn *tree_column,
		  gpointer           user_data)
{
  _gtk_recent_chooser_item_activated (GTK_RECENT_CHOOSER (user_data));
}

static void
filter_combo_changed_cb (GtkComboBox *combo_box,
			 gpointer     user_data)
{
  GtkRecentChooserDefault *impl;
  gint new_index;
  GtkRecentFilter *filter;
  
  impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
  
  new_index = gtk_combo_box_get_active (combo_box);
  filter = g_slist_nth_data (impl->filters, new_index);
  
  set_current_filter (impl, filter);
}

static GdkPixbuf *
get_drag_pixbuf (GtkRecentChooserDefault *impl)
{
  GtkRecentInfo *info;
  GdkPixbuf *retval;
  gint size;
  
  g_assert (GTK_IS_RECENT_CHOOSER_DEFAULT (impl));

  info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (impl));
  if (!info)
    return NULL;

  size = get_icon_size_for_widget (GTK_WIDGET (impl), GTK_ICON_SIZE_DND);

  retval = gtk_recent_info_get_icon (info, size);
  gtk_recent_info_unref (info);

  return retval;
}

static void
recent_view_drag_begin_cb (GtkWidget      *widget,
			   GdkDragContext *context,
			   gpointer        user_data)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
  GdkPixbuf *pixbuf;

  pixbuf = get_drag_pixbuf (impl);
  if (pixbuf)
    {
      gtk_drag_set_icon_pixbuf (context, pixbuf, 0, 0);
      g_object_unref (pixbuf);
    }
  else
    gtk_drag_set_icon_default (context);
}

typedef struct
{
  gchar **uri_list;
  gsize next_pos;
} DragData;

static void
append_uri_to_urilist (GtkTreeModel *model,
		       GtkTreePath  *path,
		       GtkTreeIter  *iter,
		       gpointer      user_data)
{
  DragData *drag_data = (DragData *) user_data;
  GtkTreeModel *child_model;
  GtkTreeIter child_iter;
  gchar *uri = NULL;
  gsize pos;

  child_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
  gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model),
  						    &child_iter,
  						    iter);
  gtk_tree_model_get (child_model, &child_iter,
  		      RECENT_URI_COLUMN, &uri,
  		      -1);
  g_assert (uri != NULL);

  pos = drag_data->next_pos;
  drag_data->uri_list[pos] = g_strdup (uri);
  drag_data->next_pos = pos + 1;
}

static void
recent_view_drag_data_get_cb (GtkWidget        *widget,
			      GdkDragContext   *context,
			      GtkSelectionData *selection_data,
			      guint             info,
			      guint32           time_,
			      gpointer          data)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (data);
1573
  DragData drag_data;
1574 1575 1576 1577
  gsize n_uris;
  
  n_uris = gtk_tree_selection_count_selected_rows (impl->selection);
  if (n_uris == 0)
1578
    return;
1579

1580 1581
  drag_data.uri_list = g_new0 (gchar *, n_uris + 1);
  drag_data.next_pos = 0;
1582 1583 1584
  
  gtk_tree_selection_selected_foreach (impl->selection,
      				       append_uri_to_urilist,
1585
      				       &drag_data);
1586
  
1587
  gtk_selection_data_set_uris (selection_data, drag_data.uri_list);
1588

1589
  g_strfreev (drag_data.uri_list);
1590 1591
}

1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
static gboolean
recent_view_query_tooltip_cb (GtkWidget  *widget,
                              gint        x,
                              gint        y,
                              gboolean    keyboard_tip,
                              GtkTooltip *tooltip,
                              gpointer    user_data)
{
  GtkRecentChooserDefault *impl = user_data;
  GtkTreeView *tree_view;
  GtkTreeIter iter;
  GtkTreePath *path = NULL;
  GtkRecentInfo *info = NULL;
  gchar *uri_display;
1606

1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
  if (!impl->show_tips)
    return FALSE;

  tree_view = GTK_TREE_VIEW (impl->recent_view);

  gtk_tree_view_get_tooltip_context (tree_view,
                                     &x, &y,
                                     keyboard_tip,
                                     NULL, &path, NULL);
  if (!path)
    return FALSE;

  if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_store), &iter, path))
    {
      gtk_tree_path_free (path);
      return FALSE;
    }

  gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_store), &iter,
                      RECENT_INFO_COLUMN, &info,
                      -1);

  uri_display = gtk_recent_info_get_uri_display (info);
  
  gtk_tooltip_set_text (tooltip, uri_display);
  gtk_tree_view_set_tooltip_row (tree_view, tooltip, path);

  g_free (uri_display);
  gtk_tree_path_free (path);
  gtk_recent_info_unref (info);

  return TRUE;
}
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658

static void
remove_selected_from_list (GtkRecentChooserDefault *impl)
{
  gchar *uri;
  GError *err;
  
  if (impl->select_multiple)
    return;
  
  uri = gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (impl));
  if (!uri)
    return;
  
  err = NULL;
  if (!gtk_recent_manager_remove_item (impl->manager, uri, &err))
    {
      gchar *msg;
   
1659
      msg = g_strdup (_("Could not remove item"));
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
      error_message (impl, msg, err->message);
      
      g_free (msg);
      g_error_free (err);
    }
  
  g_free (uri);
}

static void
copy_activated_cb (GtkMenuItem *menu_item,
		   gpointer     user_data)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
  GtkRecentInfo *info;
  gchar *utf8_uri;

  info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (impl));
  if (!info)
    return;

  utf8_uri = gtk_recent_info_get_uri_display (info);
  
  gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (impl),
			  			    GDK_SELECTION_CLIPBOARD),
                          utf8_uri, -1);

1687
  gtk_recent_info_unref (info);
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841
  g_free (utf8_uri);
}

static void
remove_all_activated_cb (GtkMenuItem *menu_item,
			 gpointer     user_data)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
  GError *err = NULL;
  
  gtk_recent_manager_purge_items (impl->manager, &err);
  if (err)
    {
       gchar *msg;

       msg = g_strdup (_("Could not clear list"));

       error_message (impl, msg, err->message);
       
       g_free (msg);
       g_error_free (err);
    }
}

static void
remove_item_activated_cb (GtkMenuItem *menu_item,
			  gpointer     user_data)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
  
  remove_selected_from_list (impl);
}

static void
show_private_toggled_cb (GtkCheckMenuItem *menu_item,
			 gpointer          user_data)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
  
  g_object_set (G_OBJECT (impl),
  		"show-private", gtk_check_menu_item_get_active (menu_item),
  		NULL);
}

static void
recent_popup_menu_detach_cb (GtkWidget *attach_widget,
			     GtkMenu   *menu)
{
  GtkRecentChooserDefault *impl;
  
  impl = g_object_get_data (G_OBJECT (attach_widget), "GtkRecentChooserDefault");
  g_assert (GTK_IS_RECENT_CHOOSER_DEFAULT (impl));
  
  impl->recent_popup_menu = NULL;
  impl->recent_popup_menu_remove_item = NULL;
  impl->recent_popup_menu_copy_item = NULL;
  impl->recent_popup_menu_clear_item = NULL;
  impl->recent_popup_menu_show_private_item = NULL;
}

static void
recent_view_menu_ensure_state (GtkRecentChooserDefault *impl)
{
  gint count;
  
  g_assert (GTK_IS_RECENT_CHOOSER_DEFAULT (impl));
  g_assert (impl->recent_popup_menu != NULL);

  if (!impl->manager)
    count = 0;
  else
    g_object_get (G_OBJECT (impl->manager), "size", &count, NULL);

  if (count == 0)
    {
      gtk_widget_set_sensitive (impl->recent_popup_menu_remove_item, FALSE);
      gtk_widget_set_sensitive (impl->recent_popup_menu_copy_item, FALSE);
      gtk_widget_set_sensitive (impl->recent_popup_menu_clear_item, FALSE);
      gtk_widget_set_sensitive (impl->recent_popup_menu_show_private_item, FALSE);
    }
}

static void
recent_view_menu_build (GtkRecentChooserDefault *impl)
{
  GtkWidget *item;
  
  if (impl->recent_popup_menu)
    {
      recent_view_menu_ensure_state (impl);
      
      return;
    }
  
  impl->recent_popup_menu = gtk_menu_new ();
  gtk_menu_attach_to_widget (GTK_MENU (impl->recent_popup_menu),
  			     impl->recent_view,
  			     recent_popup_menu_detach_cb);
  
  item = gtk_image_menu_item_new_with_mnemonic (_("Copy _Location"));
  impl->recent_popup_menu_copy_item = item;
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
		  		 gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU));
  g_signal_connect (item, "activate",
		    G_CALLBACK (copy_activated_cb), impl);
  gtk_widget_show (item);
  gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);

  item = gtk_separator_menu_item_new ();
  gtk_widget_show (item);
  gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
  
  item = gtk_image_menu_item_new_with_mnemonic (_("_Remove From List"));
  impl->recent_popup_menu_remove_item = item;
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
  				 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
  g_signal_connect (item, "activate",
  		    G_CALLBACK (remove_item_activated_cb), impl);
  gtk_widget_show (item);
  gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);

  item = gtk_image_menu_item_new_with_mnemonic (_("_Clear List"));
  impl->recent_popup_menu_clear_item = item;
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
  				 gtk_image_new_from_stock (GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU));
  g_signal_connect (item, "activate",
		    G_CALLBACK (remove_all_activated_cb), impl);
  
  gtk_widget_show (item);
  gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
  
  item = gtk_separator_menu_item_new ();
  gtk_widget_show (item);
  gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
  
  item = gtk_check_menu_item_new_with_mnemonic (_("Show _Private Resources"));
  impl->recent_popup_menu_show_private_item = item;
  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), impl->show_private);
  g_signal_connect (item, "toggled",
  		    G_CALLBACK (show_private_toggled_cb), impl);
  gtk_widget_show (item);
  gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
  
  recent_view_menu_ensure_state (impl);
}

/* taken from gtkfilechooserdefault.c */
static void
popup_position_func (GtkMenu   *menu,
                     gint      *x,
                     gint      *y,
                     gboolean  *push_in,
                     gpointer	user_data)
{
1842
  GtkAllocation allocation;
1843 1844 1845 1846 1847 1848
  GtkWidget *widget = GTK_WIDGET (user_data);
  GdkScreen *screen = gtk_widget_get_screen (widget);
  GtkRequisition req;
  gint monitor_num;
  GdkRectangle monitor;

1849
  if (G_UNLIKELY (!gtk_widget_get_realized (widget)))
1850 1851
    return;

1852 1853
  gdk_window_get_origin (gtk_widget_get_window (widget),
                         x, y);
1854

1855 1856
  gtk_widget_get_preferred_size (GTK_WIDGET (menu),
                                 &req, NULL);
1857

1858 1859 1860
  gtk_widget_get_allocation (widget, &allocation);
  *x += (allocation.width - req.width) / 2;
  *y += (allocation.height - req.height) / 2;
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941

  monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
  gtk_menu_set_monitor (menu, monitor_num);
  gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);

  *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
  *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));

  *push_in = FALSE;
}


static void
recent_view_menu_popup (GtkRecentChooserDefault *impl,
			GdkEventButton          *event)
{
  recent_view_menu_build (impl);
  
  if (event)
    gtk_menu_popup (GTK_MENU (impl->recent_popup_menu),
    		    NULL, NULL, NULL, NULL,
    		    event->button, event->time);
  else
    {
      gtk_menu_popup (GTK_MENU (impl->recent_popup_menu),
      		      NULL, NULL,
      		      popup_position_func, impl->recent_view,
      		      0, GDK_CURRENT_TIME);
      gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->recent_popup_menu),
      				   FALSE);
    }
}

static gboolean
recent_view_popup_menu_cb (GtkWidget *widget,
			   gpointer   user_data)
{
  recent_view_menu_popup (GTK_RECENT_CHOOSER_DEFAULT (user_data), NULL);
  return TRUE;
}

static gboolean
recent_view_button_press_cb (GtkWidget      *widget,
			     GdkEventButton *event,
			     gpointer        user_data)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
  
  if (event->button == 3)
    {
      GtkTreePath *path;
      gboolean res;

      if (event->window != gtk_tree_view_get_bin_window (GTK_TREE_VIEW (impl->recent_view)))
        return FALSE;

      res = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (impl->recent_view),
		      			   event->x, event->y,
					   &path,
					   NULL, NULL, NULL);
      if (!res)
        return FALSE;

      /* select the path before creating the popup menu */
      gtk_tree_selection_select_path (impl->selection, path);
      gtk_tree_path_free (path);
      
      recent_view_menu_popup (impl, event);

      return TRUE;
    }
  
  return FALSE;
}

static void
set_recent_manager (GtkRecentChooserDefault *impl,
		    GtkRecentManager        *manager)
{
  if (impl->manager)
    {
1942 1943 1944 1945 1946
      if (impl->manager_changed_id)
        {
          g_signal_handler_disconnect (impl, impl->manager_changed_id);
          impl->manager_changed_id = 0;
        }
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956

      impl->manager = NULL;
    }
  
  if (manager)
    impl->manager = manager;
  else
    impl->manager = gtk_recent_manager_get_default ();
  
  if (impl->manager)
1957 1958 1959 1960 1961
    {
      impl->manager_changed_id = g_signal_connect (impl->manager, "changed",
                                                   G_CALLBACK (recent_manager_changed_cb),
                                                   impl);
    }
1962 1963
}

1964 1965 1966 1967
static void
gtk_recent_chooser_update (GtkActivatable *activatable,
			   GtkAction      *action,
			   const gchar    *property_name)
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
{
  if (strcmp (property_name, "visible") == 0)
    {
      if (gtk_action_is_visible (action))
	gtk_widget_show (GTK_WIDGET (activatable));
      else
	gtk_widget_hide (GTK_WIDGET (activatable));
    }

  if (strcmp (property_name, "sensitive") == 0)
    gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));

1980
  _gtk_recent_chooser_update (activatable, action, property_name);
1981 1982 1983 1984
}


static void 
1985 1986
gtk_recent_chooser_sync_action_properties (GtkActivatable *activatable,
				           GtkAction      *action)
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
{
  if (action)
    {
      if (gtk_action_is_visible (action))
	gtk_widget_show (GTK_WIDGET (activatable));
      else
	gtk_widget_hide (GTK_WIDGET (activatable));
      
      gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
    }

1998
  _gtk_recent_chooser_sync_action_properties (activatable, action);
1999 2000 2001
}


2002 2003 2004 2005 2006 2007 2008
GtkWidget *
_gtk_recent_chooser_default_new (GtkRecentManager *manager)
{
  return g_object_new (GTK_TYPE_RECENT_CHOOSER_DEFAULT,
  		       "recent-manager", manager,
  		       NULL);
}