gtkfilechooserbutton.c 90 KB
Newer Older
1
/* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 3

/* GTK+: gtkfilechooserbutton.c
4
 *
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (c) 2004 James M. Cape <jcape@ignore-your.tv>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
18
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 20
 */

21
#include "config.h"
22 23 24

#include <sys/types.h>
#include <sys/stat.h>
25
#ifdef HAVE_UNISTD_H
26
#include <unistd.h>
27
#endif
28 29 30 31

#include <string.h>

#include "gtkintl.h"
32
#include "gtkbutton.h"
Matthias Clasen's avatar
Matthias Clasen committed
33
#include "gtkcelllayout.h"
Matthias Clasen's avatar
Matthias Clasen committed
34
#include "gtkcellrenderertext.h"
Matthias Clasen's avatar
Matthias Clasen committed
35
#include "gtkcellrendererpixbuf.h"
36
#include "gtkcombobox.h"
37
#include "gtkcssiconthemevalueprivate.h"
38
#include "gtkdnd.h"
39
#include "gtkdragdest.h"
40
#include "gtkicontheme.h"
41
#include "deprecated/gtkiconfactory.h"
42 43
#include "gtkimage.h"
#include "gtklabel.h"
44
#include "gtkliststore.h"
45
#include "deprecated/gtkstock.h"
46
#include "gtktreemodelfilter.h"
47
#include "gtkseparator.h"
48
#include "gtkfilechooserdialog.h"
49
#include "gtkfilechoosernative.h"
50 51
#include "gtkfilechooserprivate.h"
#include "gtkfilechooserutils.h"
52
#include "gtkmarshalers.h"
53 54 55

#include "gtkfilechooserbutton.h"

56 57
#include "gtkorientable.h"

58
#include "gtktypebuiltins.h"
59
#include "gtkprivate.h"
60
#include "gtksettings.h"
61 62
#include "gtkstylecontextprivate.h"
#include "gtkbitmaskprivate.h"
63 64 65 66 67 68 69 70 71 72 73 74 75 76

/**
 * SECTION:gtkfilechooserbutton
 * @Short_description: A button to launch a file selection dialog
 * @Title: GtkFileChooserButton
 * @See_also:#GtkFileChooserDialog
 *
 * The #GtkFileChooserButton is a widget that lets the user select a
 * file.  It implements the #GtkFileChooser interface.  Visually, it is a
 * file name with a button to bring up a #GtkFileChooserDialog.
 * The user can then use that dialog to change the file associated with
 * that button.  This widget does not support setting the
 * #GtkFileChooser:select-multiple property to %TRUE.
 *
77 78
 * ## Create a button to let the user select a file in /etc
 *
79
 * |[<!-- language="C" -->
80 81 82 83 84 85 86 87
 * {
 *   GtkWidget *button;
 *
 *   button = gtk_file_chooser_button_new (_("Select a file"),
 *                                         GTK_FILE_CHOOSER_ACTION_OPEN);
 *   gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (button),
 *                                        "/etc");
 * }
88
 * ]|
89
 *
90
 * The #GtkFileChooserButton supports the #GtkFileChooserActions
91 92
 * %GTK_FILE_CHOOSER_ACTION_OPEN and %GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER.
 *
93 94 95 96 97 98
 * > The #GtkFileChooserButton will ellipsize the label, and will thus
 * > request little horizontal space.  To give the button more space,
 * > you should call gtk_widget_get_preferred_size(),
 * > gtk_file_chooser_button_set_width_chars(), or pack the button in
 * > such a way that other interface elements give space to the
 * > widget.
99 100 101
 */


102 103 104 105
/* **************** *
 *  Private Macros  *
 * **************** */

106
#define FALLBACK_ICON_SIZE	16
107
#define DEFAULT_TITLE		N_("Select a File")
108
#define DESKTOP_DISPLAY_NAME	N_("Desktop")
109
#define FALLBACK_DISPLAY_NAME	N_("(None)") /* this string is used in gtk+/gtk/tests/filechooser.c - change it there if you change it here */
110

111 112 113 114 115 116 117 118 119 120 121 122

/* ********************** *
 *  Private Enumerations  *
 * ********************** */

/* Property IDs */
enum
{
  PROP_0,

  PROP_DIALOG,
  PROP_TITLE,
123
  PROP_WIDTH_CHARS
124 125
};

126 127 128 129 130 131 132
/* Signals */
enum
{
  FILE_SET,
  LAST_SIGNAL
};

133 134 135 136
/* TreeModel Columns
 *
 * keep in line with the store defined in gtkfilechooserbutton.ui
 */
137 138 139 140 141 142
enum
{
  ICON_COLUMN,
  DISPLAY_NAME_COLUMN,
  TYPE_COLUMN,
  DATA_COLUMN,
143
  IS_FOLDER_COLUMN,
144
  CANCELLABLE_COLUMN,
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  NUM_COLUMNS
};

/* TreeModel Row Types */
typedef enum
{
  ROW_TYPE_SPECIAL,
  ROW_TYPE_VOLUME,
  ROW_TYPE_SHORTCUT,
  ROW_TYPE_BOOKMARK_SEPARATOR,
  ROW_TYPE_BOOKMARK,
  ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
  ROW_TYPE_CURRENT_FOLDER,
  ROW_TYPE_OTHER_SEPARATOR,
  ROW_TYPE_OTHER,
160
  ROW_TYPE_EMPTY_SELECTION,
161 162 163 164 165

  ROW_TYPE_INVALID = -1
}
RowType;

166 167 168 169 170 171 172

/* ******************** *
 *  Private Structures  *
 * ******************** */

struct _GtkFileChooserButtonPrivate
{
173 174 175
  GtkFileChooser *chooser;      /* Points to either dialog or native, depending on which is set */
  GtkWidget *dialog;            /* Set if you explicitly enable */
  GtkFileChooserNative *native; /* Otherwise this is set */
176
  GtkWidget *button;
177 178
  GtkWidget *image;
  GtkWidget *label;
179 180 181 182 183 184
  GtkWidget *combo_box;
  GtkCellRenderer *icon_cell;
  GtkCellRenderer *name_cell;

  GtkTreeModel *model;
  GtkTreeModel *filter_model;
185

186
  GtkFileSystem *fs;
187
  GFile *selection_while_inactive;
188
  GFile *current_folder_while_inactive;
189 190 191

  gulong fs_volumes_changed_id;

192 193 194
  GCancellable *dnd_select_folder_cancellable;
  GCancellable *update_button_cancellable;
  GSList *change_icon_theme_cancellables;
195

196 197
  GtkBookmarksManager *bookmarks_manager;

198
  gint icon_size;
199

200 201 202 203
  guint8 n_special;
  guint8 n_volumes;
  guint8 n_shortcuts;
  guint8 n_bookmarks;
204 205 206 207
  guint  has_bookmark_separator       : 1;
  guint  has_current_folder_separator : 1;
  guint  has_current_folder           : 1;
  guint  has_other_separator          : 1;
208

209
  /* Used for hiding/showing the dialog when the button is hidden */
210
  guint  active                       : 1;
211

212 213
  /* Whether the next async callback from GIO should emit the "selection-changed" signal */
  guint  is_changing_selection        : 1;
214 215 216 217 218 219 220 221 222
};


/* ************* *
 *  DnD Support  *
 * ************* */

enum
{
223 224
  TEXT_PLAIN,
  TEXT_URI_LIST
225 226
};

227

228 229 230 231
/* ********************* *
 *  Function Prototypes  *
 * ********************* */

232 233
/* GtkFileChooserIface Functions */
static void     gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface);
234 235 236 237
static gboolean gtk_file_chooser_button_set_current_folder (GtkFileChooser    *chooser,
							    GFile             *file,
							    GError           **error);
static GFile *gtk_file_chooser_button_get_current_folder (GtkFileChooser    *chooser);
238 239 240 241 242 243 244
static gboolean gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
						     GFile          *file,
						     GError        **error);
static void gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
						   GFile          *file);
static void gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser);
static GSList *gtk_file_chooser_button_get_files (GtkFileChooser *chooser);
245
static gboolean gtk_file_chooser_button_add_shortcut_folder     (GtkFileChooser      *chooser,
246
								 GFile               *file,
247 248
								 GError             **error);
static gboolean gtk_file_chooser_button_remove_shortcut_folder  (GtkFileChooser      *chooser,
249
								 GFile               *file,
250 251
								 GError             **error);

252
/* GObject Functions */
253
static void     gtk_file_chooser_button_constructed        (GObject          *object);
254
static void     gtk_file_chooser_button_set_property       (GObject          *object,
255
							    guint             param_id,
256 257 258
							    const GValue     *value,
							    GParamSpec       *pspec);
static void     gtk_file_chooser_button_get_property       (GObject          *object,
259
							    guint             param_id,
260 261
							    GValue           *value,
							    GParamSpec       *pspec);
262
static void     gtk_file_chooser_button_finalize           (GObject          *object);
263 264

/* GtkWidget Functions */
265
static void     gtk_file_chooser_button_destroy            (GtkWidget        *widget);
266 267 268 269 270
static void     gtk_file_chooser_button_drag_data_received (GtkWidget        *widget,
							    GdkDragContext   *context,
							    gint              x,
							    gint              y,
							    GtkSelectionData *data,
271
							    guint             type,
272 273
							    guint             drag_time);
static void     gtk_file_chooser_button_show_all           (GtkWidget        *widget);
274 275
static void     gtk_file_chooser_button_show               (GtkWidget        *widget);
static void     gtk_file_chooser_button_hide               (GtkWidget        *widget);
276
static void     gtk_file_chooser_button_map                (GtkWidget        *widget);
Matthias Clasen's avatar
Matthias Clasen committed
277 278
static gboolean gtk_file_chooser_button_mnemonic_activate  (GtkWidget        *widget,
							    gboolean          group_cycling);
279
static void     gtk_file_chooser_button_style_updated      (GtkWidget        *widget);
280 281
static void     gtk_file_chooser_button_screen_changed     (GtkWidget        *widget,
							    GdkScreen        *old_screen);
282 283
static void     gtk_file_chooser_button_state_flags_changed (GtkWidget       *widget,
                                                             GtkStateFlags    previous_state);
284 285

/* Utility Functions */
286
static GtkIconTheme *get_icon_theme               (GtkWidget            *widget);
287 288
static void          set_info_for_file_at_iter         (GtkFileChooserButton *fs,
							GFile                *file,
289
							GtkTreeIter          *iter);
290 291 292 293 294

static gint          model_get_type_position      (GtkFileChooserButton *button,
						   RowType               row_type);
static void          model_free_row_data          (GtkFileChooserButton *button,
						   GtkTreeIter          *iter);
295 296 297
static void          model_add_special            (GtkFileChooserButton *button);
static void          model_add_other              (GtkFileChooserButton *button);
static void          model_add_empty_selection    (GtkFileChooserButton *button);
298 299 300 301 302
static void          model_add_volumes            (GtkFileChooserButton *button,
						   GSList               *volumes);
static void          model_add_bookmarks          (GtkFileChooserButton *button,
						   GSList               *bookmarks);
static void          model_update_current_folder  (GtkFileChooserButton *button,
303
						   GFile                *file);
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
static void          model_remove_rows            (GtkFileChooserButton *button,
						   gint                  pos,
						   gint                  n_rows);

static gboolean      filter_model_visible_func    (GtkTreeModel         *model,
						   GtkTreeIter          *iter,
						   gpointer              user_data);

static gboolean      combo_box_row_separator_func (GtkTreeModel         *model,
						   GtkTreeIter          *iter,
						   gpointer              user_data);
static void          name_cell_data_func          (GtkCellLayout        *layout,
						   GtkCellRenderer      *cell,
						   GtkTreeModel         *model,
						   GtkTreeIter          *iter,
						   gpointer              user_data);
static void          open_dialog                  (GtkFileChooserButton *button);
static void          update_combo_box             (GtkFileChooserButton *button);
static void          update_label_and_image       (GtkFileChooserButton *button);

/* Child Object Callbacks */
static void     fs_volumes_changed_cb            (GtkFileSystem  *fs,
						  gpointer        user_data);
327
static void     bookmarks_changed_cb             (gpointer        user_data);
328 329 330

static void     combo_box_changed_cb             (GtkComboBox    *combo_box,
						  gpointer        user_data);
331 332 333
static void     combo_box_notify_popup_shown_cb  (GObject        *object,
						  GParamSpec     *pspec,
						  gpointer        user_data);
334 335 336 337

static void     button_clicked_cb                (GtkButton      *real_button,
						  gpointer        user_data);

338
static void     chooser_update_preview_cb        (GtkFileChooser *dialog,
339
						  gpointer        user_data);
340
static void     chooser_notify_cb                (GObject        *dialog,
341 342 343 344 345 346 347 348
						  GParamSpec     *pspec,
						  gpointer        user_data);
static gboolean dialog_delete_event_cb           (GtkWidget      *dialog,
						  GdkEvent       *event,
						  gpointer        user_data);
static void     dialog_response_cb               (GtkDialog      *dialog,
						  gint            response,
						  gpointer        user_data);
349 350 351
static void     native_response_cb               (GtkFileChooserNative *native,
                                                  gint            response,
                                                  gpointer        user_data);
352

353
static guint file_chooser_button_signals[LAST_SIGNAL] = { 0 };
354 355 356 357 358

/* ******************* *
 *  GType Declaration  *
 * ******************* */

359 360 361 362
G_DEFINE_TYPE_WITH_CODE (GtkFileChooserButton, gtk_file_chooser_button, GTK_TYPE_BOX,
                         G_ADD_PRIVATE (GtkFileChooserButton)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
                                                gtk_file_chooser_button_file_chooser_iface_init))
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377


/* ***************** *
 *  GType Functions  *
 * ***************** */

static void
gtk_file_chooser_button_class_init (GtkFileChooserButtonClass * class)
{
  GObjectClass *gobject_class;
  GtkWidgetClass *widget_class;

  gobject_class = G_OBJECT_CLASS (class);
  widget_class = GTK_WIDGET_CLASS (class);

378
  gobject_class->constructed = gtk_file_chooser_button_constructed;
379 380
  gobject_class->set_property = gtk_file_chooser_button_set_property;
  gobject_class->get_property = gtk_file_chooser_button_get_property;
381
  gobject_class->finalize = gtk_file_chooser_button_finalize;
382

383
  widget_class->destroy = gtk_file_chooser_button_destroy;
384 385
  widget_class->drag_data_received = gtk_file_chooser_button_drag_data_received;
  widget_class->show_all = gtk_file_chooser_button_show_all;
386 387
  widget_class->show = gtk_file_chooser_button_show;
  widget_class->hide = gtk_file_chooser_button_hide;
388
  widget_class->map = gtk_file_chooser_button_map;
389
  widget_class->style_updated = gtk_file_chooser_button_style_updated;
390
  widget_class->screen_changed = gtk_file_chooser_button_screen_changed;
Matthias Clasen's avatar
Matthias Clasen committed
391
  widget_class->mnemonic_activate = gtk_file_chooser_button_mnemonic_activate;
392
  widget_class->state_flags_changed = gtk_file_chooser_button_state_flags_changed;
393

394
  /**
395
   * GtkFileChooserButton::file-set:
396 397 398
   * @widget: the object which received the signal.
   *
   * The ::file-set signal is emitted when the user selects a file.
399
   *
400
   * Note that this signal is only emitted when the user
401
   * changes the file.
402 403 404 405 406 407
   *
   * Since: 2.12
   */
  file_chooser_button_signals[FILE_SET] =
    g_signal_new (I_("file-set"),
		  G_TYPE_FROM_CLASS (gobject_class),
408
		  G_SIGNAL_RUN_FIRST,
409 410
		  G_STRUCT_OFFSET (GtkFileChooserButtonClass, file_set),
		  NULL, NULL,
411
		  NULL,
412
		  G_TYPE_NONE, 0);
413

Matthias Clasen's avatar
Matthias Clasen committed
414 415
  /**
   * GtkFileChooserButton:dialog:
416
   *
Matthias Clasen's avatar
Matthias Clasen committed
417 418 419 420
   * Instance of the #GtkFileChooserDialog associated with the button.
   *
   * Since: 2.6
   */
421 422 423 424
  g_object_class_install_property (gobject_class, PROP_DIALOG,
				   g_param_spec_object ("dialog",
							P_("Dialog"),
							P_("The file chooser dialog to use."),
425
							GTK_TYPE_FILE_CHOOSER,
426
							(GTK_PARAM_WRITABLE |
427
							 G_PARAM_CONSTRUCT_ONLY)));
Matthias Clasen's avatar
Matthias Clasen committed
428 429 430

  /**
   * GtkFileChooserButton:title:
431
   *
Matthias Clasen's avatar
Matthias Clasen committed
432 433 434 435
   * Title to put on the #GtkFileChooserDialog associated with the button.
   *
   * Since: 2.6
   */
436 437 438 439
  g_object_class_install_property (gobject_class, PROP_TITLE,
				   g_param_spec_string ("title",
							P_("Title"),
							P_("The title of the file chooser dialog."),
440
							_(DEFAULT_TITLE),
441
							GTK_PARAM_READWRITE));
Matthias Clasen's avatar
Matthias Clasen committed
442 443

  /**
Matthias Clasen's avatar
Matthias Clasen committed
444
   * GtkFileChooserButton:width-chars:
445
   *
Matthias Clasen's avatar
Matthias Clasen committed
446 447 448 449
   * The width of the entry and label inside the button, in characters.
   *
   * Since: 2.6
   */
450 451 452 453 454
  g_object_class_install_property (gobject_class, PROP_WIDTH_CHARS,
				   g_param_spec_int ("width-chars",
						     P_("Width In Characters"),
						     P_("The desired width of the button widget, in characters."),
						     -1, G_MAXINT, -1,
455
						     GTK_PARAM_READWRITE));
456 457 458

  _gtk_file_chooser_install_properties (gobject_class);

459 460 461
  /* Bind class to template
   */
  gtk_widget_class_set_template_from_resource (widget_class,
462
					       "/org/gtk/libgtk/ui/gtkfilechooserbutton.ui");
463

464 465 466 467 468 469 470
  gtk_widget_class_bind_template_child_private (widget_class, GtkFileChooserButton, model);
  gtk_widget_class_bind_template_child_private (widget_class, GtkFileChooserButton, button);
  gtk_widget_class_bind_template_child_private (widget_class, GtkFileChooserButton, image);
  gtk_widget_class_bind_template_child_private (widget_class, GtkFileChooserButton, label);
  gtk_widget_class_bind_template_child_private (widget_class, GtkFileChooserButton, combo_box);
  gtk_widget_class_bind_template_child_private (widget_class, GtkFileChooserButton, icon_cell);
  gtk_widget_class_bind_template_child_private (widget_class, GtkFileChooserButton, name_cell);
471 472 473 474

  gtk_widget_class_bind_template_callback (widget_class, button_clicked_cb);
  gtk_widget_class_bind_template_callback (widget_class, combo_box_changed_cb);
  gtk_widget_class_bind_template_callback (widget_class, combo_box_notify_popup_shown_cb);
475 476

  gtk_widget_class_set_css_name (widget_class, "filechooserbutton");
477 478 479 480 481 482
}

static void
gtk_file_chooser_button_init (GtkFileChooserButton *button)
{
  GtkFileChooserButtonPrivate *priv;
483
  GtkTargetList *target_list;
484

485
  priv = button->priv = gtk_file_chooser_button_get_instance_private (button);
486

487 488
  priv->icon_size = FALLBACK_ICON_SIZE;

489 490
  gtk_widget_init_template (GTK_WIDGET (button));

491 492
  /* Bookmarks manager */
  priv->bookmarks_manager = _gtk_bookmarks_manager_new (bookmarks_changed_cb, button);
493 494 495 496
  gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->combo_box),
				      priv->name_cell, name_cell_data_func,
				      NULL, NULL);

497 498
  /* DnD */
  gtk_drag_dest_set (GTK_WIDGET (button),
499
                     (GTK_DEST_DEFAULT_ALL),
500
		     NULL, 0,
501
		     GDK_ACTION_COPY);
502 503 504 505 506
  target_list = gtk_target_list_new (NULL, 0);
  gtk_target_list_add_uri_targets (target_list, TEXT_URI_LIST);
  gtk_target_list_add_text_targets (target_list, TEXT_PLAIN);
  gtk_drag_dest_set_target_list (GTK_WIDGET (button), target_list);
  gtk_target_list_unref (target_list);
507 508 509
}


510 511 512 513 514 515 516 517
/* ******************************* *
 *  GtkFileChooserIface Functions  *
 * ******************************* */
static void
gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface)
{
  _gtk_file_chooser_delegate_iface_init (iface);

518 519
  iface->set_current_folder = gtk_file_chooser_button_set_current_folder;
  iface->get_current_folder = gtk_file_chooser_button_get_current_folder;
520 521 522 523
  iface->select_file = gtk_file_chooser_button_select_file;
  iface->unselect_file = gtk_file_chooser_button_unselect_file;
  iface->unselect_all = gtk_file_chooser_button_unselect_all;
  iface->get_files = gtk_file_chooser_button_get_files;
524 525 526 527
  iface->add_shortcut_folder = gtk_file_chooser_button_add_shortcut_folder;
  iface->remove_shortcut_folder = gtk_file_chooser_button_remove_shortcut_folder;
}

528 529 530 531 532 533 534 535 536 537 538 539
static void
emit_selection_changed_if_changing_selection (GtkFileChooserButton *button)
{
  GtkFileChooserButtonPrivate *priv = button->priv;

  if (priv->is_changing_selection)
    {
      priv->is_changing_selection = FALSE;
      g_signal_emit_by_name (button, "selection-changed");
    }
}

540 541 542 543 544 545 546 547
static gboolean
gtk_file_chooser_button_set_current_folder (GtkFileChooser    *chooser,
					    GFile             *file,
					    GError           **error)
{
  GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
  GtkFileChooserButtonPrivate *priv = button->priv;

548 549
  if (priv->current_folder_while_inactive)
    g_object_unref (priv->current_folder_while_inactive);
550

551
  priv->current_folder_while_inactive = g_object_ref (file);
552

553
  update_combo_box (button);
554

555
  g_signal_emit_by_name (button, "current-folder-changed");
556

557
  if (priv->active)
558
    gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->chooser), file, NULL);
559

560
  return TRUE;
561 562 563 564 565 566 567 568
}

static GFile *
gtk_file_chooser_button_get_current_folder (GtkFileChooser *chooser)
{
  GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
  GtkFileChooserButtonPrivate *priv = button->priv;

569 570
  if (priv->current_folder_while_inactive)
    return g_object_ref (priv->current_folder_while_inactive);
571
  else
572
    return NULL;
573 574
}

575 576 577 578 579 580 581 582
static gboolean
gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
				     GFile          *file,
				     GError        **error)
{
  GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
  GtkFileChooserButtonPrivate *priv = button->priv;

583 584
  if (priv->selection_while_inactive)
    g_object_unref (priv->selection_while_inactive);
585

586 587 588
  priv->selection_while_inactive = g_object_ref (file);

  priv->is_changing_selection = TRUE;
589

590 591
  update_label_and_image (button);
  update_combo_box (button);
592

593
  if (priv->active)
594
    gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->chooser), file, NULL);
595

596 597
  return TRUE;
}
598

599 600 601 602 603 604 605 606 607
static void
unselect_current_file (GtkFileChooserButton *button)
{
  GtkFileChooserButtonPrivate *priv = button->priv;

  if (priv->selection_while_inactive)
    {
      g_object_unref (priv->selection_while_inactive);
      priv->selection_while_inactive = NULL;
608
    }
609 610 611 612 613

  priv->is_changing_selection = TRUE;

  update_label_and_image (button);
  update_combo_box (button);
614 615 616 617 618 619 620 621 622
}

static void
gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
				       GFile          *file)
{
  GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
  GtkFileChooserButtonPrivate *priv = button->priv;

623 624
  if (g_file_equal (priv->selection_while_inactive, file))
    unselect_current_file (button);
625 626

  if (priv->active)
627
    gtk_file_chooser_unselect_file (GTK_FILE_CHOOSER (priv->chooser), file);
628 629 630 631 632 633 634 635
}

static void
gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser)
{
  GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
  GtkFileChooserButtonPrivate *priv = button->priv;

636
  unselect_current_file (button);
637 638

  if (priv->active)
639
    gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->chooser));
640 641
}

642 643
static GFile *
get_selected_file (GtkFileChooserButton *button)
644 645
{
  GtkFileChooserButtonPrivate *priv = button->priv;
646
  GFile *retval;
647

648 649 650 651
  retval = NULL;

  if (priv->selection_while_inactive)
    retval = priv->selection_while_inactive;
652
  else if (priv->chooser && gtk_file_chooser_get_action (priv->chooser) == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
653
    {
654
      /* If there is no "real" selection in SELECT_FOLDER mode, then we'll just return
655
       * the current folder, since that is what GtkFileChooserWidget would do.
656 657 658
       */
      if (priv->current_folder_while_inactive)
	retval = priv->current_folder_while_inactive;
659
    }
660

661 662 663 664
  if (retval)
    return g_object_ref (retval);
  else
    return NULL;
665 666 667 668 669 670
}

static GSList *
gtk_file_chooser_button_get_files (GtkFileChooser *chooser)
{
  GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
671
  GFile *file;
672

673 674 675 676 677
  file = get_selected_file (button);
  if (file)
    return g_slist_prepend (NULL, file);
  else
    return NULL;
678 679
}

680
static gboolean
681 682 683
gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser  *chooser,
					     GFile           *file,
					     GError         **error)
684 685 686 687 688 689
{
  GtkFileChooser *delegate;
  gboolean retval;

  delegate = g_object_get_qdata (G_OBJECT (chooser),
				 GTK_FILE_CHOOSER_DELEGATE_QUARK);
690
  retval = _gtk_file_chooser_add_shortcut_folder (delegate, file, error);
691 692 693

  if (retval)
    {
694 695
      GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
      GtkFileChooserButtonPrivate *priv = button->priv;
696 697 698
      GtkTreeIter iter;
      gint pos;

699
      pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
700 701 702 703
      pos += priv->n_shortcuts;

      gtk_list_store_insert (GTK_LIST_STORE (priv->model), &iter, pos);
      gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
704 705
			  ICON_COLUMN, NULL,
			  DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
706
			  TYPE_COLUMN, ROW_TYPE_SHORTCUT,
707
			  DATA_COLUMN, g_object_ref (file),
708
			  IS_FOLDER_COLUMN, FALSE,
709
			  -1);
710
      set_info_for_file_at_iter (button, file, &iter);
711 712 713 714 715 716 717 718 719
      priv->n_shortcuts++;

      gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
    }

  return retval;
}

static gboolean
720 721 722
gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser  *chooser,
						GFile           *file,
						GError         **error)
723 724 725 726 727 728 729
{
  GtkFileChooser *delegate;
  gboolean retval;

  delegate = g_object_get_qdata (G_OBJECT (chooser),
				 GTK_FILE_CHOOSER_DELEGATE_QUARK);

730
  retval = _gtk_file_chooser_remove_shortcut_folder (delegate, file, error);
731 732 733

  if (retval)
    {
734 735
      GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
      GtkFileChooserButtonPrivate *priv = button->priv;
736 737 738 739
      GtkTreeIter iter;
      gint pos;
      gchar type;

740
      pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
741
      gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
742 743 744 745 746 747 748 749 750 751 752

      do
	{
	  gpointer data;

	  gtk_tree_model_get (priv->model, &iter,
			      TYPE_COLUMN, &type,
			      DATA_COLUMN, &data,
			      -1);

	  if (type == ROW_TYPE_SHORTCUT &&
753
	      data && g_file_equal (data, file))
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
	    {
	      model_free_row_data (GTK_FILE_CHOOSER_BUTTON (chooser), &iter);
	      gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter);
	      priv->n_shortcuts--;
	      gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
	      update_combo_box (GTK_FILE_CHOOSER_BUTTON (chooser));
	      break;
	    }
	}
      while (type == ROW_TYPE_SHORTCUT &&
	     gtk_tree_model_iter_next (priv->model, &iter));
    }

  return retval;
}


771 772 773 774
/* ******************* *
 *  GObject Functions  *
 * ******************* */

775 776
static void
gtk_file_chooser_button_constructed (GObject *object)
777
{
778 779
  GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
  GtkFileChooserButtonPrivate *priv = button->priv;
780
  GSList *list;
781

782
  G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->constructed (object);
783 784 785

  if (!priv->dialog)
    {
786 787
      priv->native = gtk_file_chooser_native_new (NULL,
                                                  NULL,
788
						  GTK_FILE_CHOOSER_ACTION_OPEN,
789
						  NULL,
790
						  NULL);
791
      priv->chooser = GTK_FILE_CHOOSER (priv->native);
792
      gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
793 794 795

      g_signal_connect (priv->native, "response",
                        G_CALLBACK (native_response_cb), object);
796
    }
797
  else /* dialog set */
798
    {
799 800 801 802 803 804 805 806 807 808 809 810
      priv->chooser = GTK_FILE_CHOOSER (priv->dialog);

      if (!gtk_window_get_title (GTK_WINDOW (priv->dialog)))
        gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));

      g_signal_connect (priv->dialog, "delete-event",
                        G_CALLBACK (dialog_delete_event_cb), object);
      g_signal_connect (priv->dialog, "response",
                        G_CALLBACK (dialog_response_cb), object);

      g_object_add_weak_pointer (G_OBJECT (priv->dialog),
                                 (gpointer) (&priv->dialog));
811
    }
812

813 814
  g_signal_connect (priv->chooser, "notify",
                    G_CALLBACK (chooser_notify_cb), object);
815 816 817

  /* This is used, instead of the standard delegate, to ensure that signals are only
   * delegated when the OK button is pressed. */
818
  g_object_set_qdata (object, GTK_FILE_CHOOSER_DELEGATE_QUARK, priv->chooser);
819

820
  priv->fs =
821
    g_object_ref (_gtk_file_chooser_get_file_system (priv->chooser));
822

823
  model_add_special (button);
824

825
  list = _gtk_file_system_list_volumes (priv->fs);
826
  model_add_volumes (button, list);
827 828
  g_slist_free (list);

829
  list = _gtk_bookmarks_manager_list_bookmarks (priv->bookmarks_manager);
830
  model_add_bookmarks (button, list);
Matthias Clasen's avatar
Matthias Clasen committed
831
  g_slist_free_full (list, g_object_unref);
832

833
  model_add_other (button);
834

835 836
  model_add_empty_selection (button);

837 838 839 840 841 842 843 844 845 846
  priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
  gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
					  filter_model_visible_func,
					  object, NULL);

  gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo_box), priv->filter_model);
  gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (priv->combo_box),
					combo_box_row_separator_func,
					NULL, NULL);

847 848 849
  /* set up the action for a user-provided dialog, this also updates
   * the label, image and combobox
   */
850
  g_object_set (object,
851
		"action", gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->chooser)),
852
		NULL);
853 854 855 856 857

  priv->fs_volumes_changed_id =
    g_signal_connect (priv->fs, "volumes-changed",
		      G_CALLBACK (fs_volumes_changed_cb), object);

858 859
  update_label_and_image (button);
  update_combo_box (button);
860
}
861 862 863

static void
gtk_file_chooser_button_set_property (GObject      *object,
864
				      guint         param_id,
865 866 867
				      const GValue *value,
				      GParamSpec   *pspec)
{
868 869
  GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
  GtkFileChooserButtonPrivate *priv = button->priv;
870

871
  switch (param_id)
872 873
    {
    case PROP_DIALOG:
874 875
      /* Construct-only */
      priv->dialog = g_value_get_object (value);
876
      break;
877 878 879 880
    case PROP_WIDTH_CHARS:
      gtk_file_chooser_button_set_width_chars (GTK_FILE_CHOOSER_BUTTON (object),
					       g_value_get_int (value));
      break;
881
    case GTK_FILE_CHOOSER_PROP_ACTION:
882 883 884 885 886 887 888 889 890 891
      switch (g_value_get_enum (value))
	{
	case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
	case GTK_FILE_CHOOSER_ACTION_SAVE:
	  {
	    GEnumClass *eclass;
	    GEnumValue *eval;

	    eclass = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
	    eval = g_enum_get_value (eclass, g_value_get_enum (value));
892
	    g_warning ("%s: Choosers of type '%s' do not support '%s'.",
893 894
		       G_STRFUNC, G_OBJECT_TYPE_NAME (object), eval->value_name);

895
	    g_value_set_enum ((GValue *) value, GTK_FILE_CHOOSER_ACTION_OPEN);
896 897 898
	  }
	  break;
	}
899

900
      g_object_set_property (G_OBJECT (priv->chooser), pspec->name, value);
901
      update_label_and_image (GTK_FILE_CHOOSER_BUTTON (object));
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
      update_combo_box (GTK_FILE_CHOOSER_BUTTON (object));

      switch (g_value_get_enum (value))
	{
	case GTK_FILE_CHOOSER_ACTION_OPEN:
	  gtk_widget_hide (priv->combo_box);
	  gtk_widget_show (priv->button);
	  break;
	case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
	  gtk_widget_hide (priv->button);
	  gtk_widget_show (priv->combo_box);
	  break;
	default:
	  g_assert_not_reached ();
	  break;
	}
918 919 920 921 922 923 924 925 926
      break;

    case PROP_TITLE:
    case GTK_FILE_CHOOSER_PROP_FILTER:
    case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
    case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
    case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
    case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
    case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
927
    case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
928
    case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
929
      g_object_set_property (G_OBJECT (priv->chooser), pspec->name, value);
930 931
      break;

932
    case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
933
      g_object_set_property (G_OBJECT (priv->chooser), pspec->name, value);
934
      fs_volumes_changed_cb (priv->fs, button);
935
      bookmarks_changed_cb (button);
936 937
      break;

938
    case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
939
      g_warning ("%s: Choosers of type '%s' do not support selecting multiple files.",
940 941 942
		 G_STRFUNC, G_OBJECT_TYPE_NAME (object));
      break;
    default:
943
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
944 945 946 947 948 949
      break;
    }
}

static void
gtk_file_chooser_button_get_property (GObject    *object,
950
				      guint       param_id,
951 952 953
				      GValue     *value,
				      GParamSpec *pspec)
{
954 955
  GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
  GtkFileChooserButtonPrivate *priv = button->priv;
956 957

  switch (param_id)
958
    {
959 960
    case PROP_WIDTH_CHARS:
      g_value_set_int (value,
961
		       gtk_label_get_width_chars (GTK_LABEL (priv->label)));
962 963 964 965 966 967 968 969 970 971 972 973
      break;

    case PROP_TITLE:
    case GTK_FILE_CHOOSER_PROP_ACTION:
    case GTK_FILE_CHOOSER_PROP_FILTER:
    case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
    case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
    case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
    case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
    case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
    case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
    case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
974
    case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
975
    case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
976
      g_object_get_property (G_OBJECT (priv->chooser), pspec->name, value);
977 978 979
      break;

    default:
980
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
981 982 983 984
      break;
    }
}

985 986 987
static void
gtk_file_chooser_button_finalize (GObject *object)
{
988 989
  GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
  GtkFileChooserButtonPrivate *priv = button->priv;
990

991 992
  if (priv->selection_while_inactive)
    g_object_unref (priv->selection_while_inactive);
993

994 995 996
  if (priv->current_folder_while_inactive)
    g_object_unref (priv->current_folder_while_inactive);

997 998 999 1000 1001
  if (priv->model)
    {
      model_remove_rows (button, 0, gtk_tree_model_iter_n_children (priv->model, NULL));
      g_object_unref (priv->model);
    }
1002

1003
  G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize (object);
1004
}
1005 1006

/* ********************* *
1007
 *  GtkWidget Functions  *
1008 1009
 * ********************* */

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
static void
gtk_file_chooser_button_state_flags_changed (GtkWidget     *widget,
                                             GtkStateFlags  previous_state)
{
  GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
  GtkFileChooserButtonPrivate *priv = button->priv;
  GtkWidget *child;

  if (gtk_widget_get_visible (priv->button))
    child = priv->button;
  else
    child = priv->combo_box;

  if (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_DROP_ACTIVE)
    gtk_widget_set_state_flags (child, GTK_STATE_FLAG_DROP_ACTIVE, FALSE);
  else
    gtk_widget_unset_state_flags (child, GTK_STATE_FLAG_DROP_ACTIVE);

  GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->state_flags_changed (widget, previous_state);
}

1031
static void
1032
gtk_file_chooser_button_destroy (GtkWidget *widget)
1033
{
1034
  GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1035
  GtkFileChooserButtonPrivate *priv = button->priv;
1036 1037
  GtkTreeIter iter;
  GSList *l;
1038 1039

  if (priv->dialog != NULL)
1040 1041 1042 1043 1044
    {
      gtk_widget_destroy (priv->dialog);
      priv->dialog = NULL;
    }

1045 1046 1047 1048 1049
  if (priv->native)
    {
      gtk_native_dialog_destroy (GTK_NATIVE_DIALOG (priv->native));
      g_clear_object (&priv->native);
    }
1050 1051 1052

  priv->chooser = NULL;

1053
  if (priv->model && gtk_tree_model_get_iter_first (priv->model, &iter))
1054
    {
1055 1056 1057
      do
        model_free_row_data (button, &iter);
      while (gtk_tree_model_iter_next (priv->model, &iter));
1058 1059
    }

1060
  if (priv->dnd_select_folder_cancellable)
1061
    {
1062 1063
      g_cancellable_cancel (priv->dnd_select_folder_cancellable);
      priv->dnd_select_folder_cancellable = NULL;
1064 1065
    }

1066
  if (priv->update_button_cancellable)
1067
    {
1068 1069
      g_cancellable_cancel (priv->update_button_cancellable);
      priv->update_button_cancellable = NULL;
1070 1071
    }

1072
  if (priv->change_icon_theme_cancellables)
1073
    {
1074
      for (l = priv->change_icon_theme_cancellables; l; l = l->next)
1075
        {
1076 1077
	  GCancellable *cancellable = G_CANCELLABLE (l->data);
	  g_cancellable_cancel (cancellable);
1078
        }
1079 1080
      g_slist_free (priv->change_icon_theme_cancellables);
      priv->change_icon_theme_cancellables = NULL;
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
    }

  if (priv->filter_model)
    {
      g_object_unref (priv->filter_model);
      priv->filter_model = NULL;
    }

  if (priv->fs)
    {
      g_signal_handler_disconnect (priv->fs, priv->fs_volumes_changed_id);
      g_object_unref (priv->fs);
      priv->fs = NULL;
    }
1095

1096 1097 1098 1099 1100 1101
  if (priv->bookmarks_manager)
    {
      _gtk_bookmarks_manager_free (priv->bookmarks_manager);
      priv->bookmarks_manager = NULL;
    }

1102
  GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->destroy (widget);
1103 1104
}

1105 1106
struct DndSelectFolderData
{
1107
  GtkFileSystem *file_system;
1108 1109
  GtkFileChooserButton *button;
  GtkFileChooserAction action;
1110
  GFile *file;
1111 1112 1113 1114 1115 1116
  gchar **uris;
  guint i;
  gboolean selected;
};

static void
1117 1118 1119 1120
dnd_select_folder_get_info_cb (GCancellable *cancellable,
			       GFileInfo    *info,
			       const GError *error,
			       gpointer      user_data)
1121
{
1122
  gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1123 1124
  struct DndSelectFolderData *data = user_data;

1125
  if (cancellable != data->button->priv->dnd_select_folder_cancellable)
1126 1127
    {
      g_object_unref (data->button);
1128
      g_object_unref (data->file);
1129 1130 1131
      g_strfreev (data->uris);
      g_free (data);

1132
      g_object_unref (cancellable);
1133 1134 1135
      return;
    }

1136
  data->button->priv->dnd_select_folder_cancellable = NULL;
1137 1138 1139

  if (!cancelled && !error && info != NULL)
    {
1140 1141
      gboolean is_folder;

1142
      is_folder = _gtk_file_info_consider_as_directory (info);
1143 1144 1145 1146

      data->selected =
	(((data->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER && is_folder) ||
	  (data->action == GTK_FILE_CHOOSER_ACTION_OPEN && !is_folder)) &&
1147
	 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (data->button), data->file, NULL));
1148 1149 1150 1151 1152 1153
    }
  else
    data->selected = FALSE;

  if (data->selected || data->uris[++data->i] == NULL)
    {
1154 1155
      g_signal_emit (data->button, file_chooser_button_signals[FILE_SET], 0);

1156
      g_object_unref (data->button);
1157
      g_object_unref (data->file);
1158 1159 1160
      g_strfreev (data->uris);
      g_free (data);

1161
      g_object_unref (cancellable);
1162 1163 1164
      return;
    }

1165 1166
  if (data->file)
    g_object_unref (data->file);
1167

1168
  data->file = g_file_new_for_uri (data->uris[data->i]);
1169

1170
  data->button->priv->dnd_select_folder_cancellable =
1171 1172 1173
    _gtk_file_system_get_info (data->file_system, data->file,
			       "standard::type",
			       dnd_select_folder_get_info_cb, user_data);
1174

1175
  g_object_unref (cancellable);
1176 1177
}

1178 1179 1180 1181 1182 1183
static void
gtk_file_chooser_button_drag_data_received (GtkWidget	     *widget,
					    GdkDragContext   *context,
					    gint	      x,
					    gint	      y,
					    GtkSelectionData *data,
1184
					    guint	      type,
1185 1186
					    guint	      drag_time)
{
1187 1188
  GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
  GtkFileChooserButtonPrivate *priv = button->priv;
1189
  GFile *file;
1190
  gchar *text;
1191 1192

  if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received != NULL)
1193 1194 1195 1196 1197
    GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received (widget,
										 context,
										 x, y,
										 data, type,
										 drag_time);
1198

1199
  if (widget == NULL || context == NULL || data == NULL || gtk_selection_data_get_length (data) < 0)
1200 1201
    return;

1202
  switch (type)
1203 1204 1205 1206
    {
    case TEXT_URI_LIST:
      {
	gchar **uris;
1207
	struct DndSelectFolderData *info;
1208

1209
	uris = gtk_selection_data_get_uris (data);
1210

1211 1212 1213
	if (uris == NULL)
	  break;

1214 1215 1216 1217 1218
	info = g_new0 (struct DndSelectFolderData, 1);
	info->button = g_object_ref (button);
	info->i = 0;
	info->uris = uris;
	info->selected = FALSE;
1219
	info->file_system = priv->fs;
1220
	g_object_get (priv->chooser, "action", &info->action, NULL);
1221

1222
	info->file = g_file_new_for_uri (info->uris[info->i]);
1223

1224 1225
	if (priv->dnd_select_folder_cancellable)
	  g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1226

1227
	priv->dnd_select_folder_cancellable =
1228 1229 1230
	  _gtk_file_system_get_info (priv->fs, info->file,
				     "standard::type",
				     dnd_select_folder_get_info_cb, info);
1231 1232 1233 1234
      }
      break;

    case TEXT_PLAIN:
1235
      text = (char*) gtk_selection_data_get_text (data);
1236
      file = g_file_new_for_uri (text);
1237
      gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->chooser), file, NULL);
1238 1239
      g_object_unref (file);
      g_free (text);
1240
      g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
1241 1242 1243
      break;

    default:
1244 1245 1246
      break;
    }

1247
  gtk_drag_finish (context, TRUE, FALSE, drag_time);
1248 1249 1250 1251 1252 1253 1254 1255
}

static void
gtk_file_chooser_button_show_all (GtkWidget *widget)
{
  gtk_widget_show (widget);
}

1256 1257 1258
static void
gtk_file_chooser_button_show (GtkWidget *widget)
{
1259 1260
  GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
  GtkFileChooserButtonPrivate *priv = button->priv;
1261 1262

  if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show)
1263
    GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show (widget);
1264

1265
  if (priv->active)
1266
    open_dialog (GTK_FILE_CHOOSER_BUTTON (widget));
1267 1268 1269 1270 1271
}

static void
gtk_file_chooser_button_hide (GtkWidget *widget)
{
1272 1273
  GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
  GtkFileChooserButtonPrivate *priv = button->priv;
1274

1275 1276 1277 1278
  if (priv->dialog)
    gtk_widget_hide (priv->dialog);
  else
    gtk_native_dialog_hide (GTK_NATIVE_DIALOG (priv->native));
1279 1280

  if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide)
1281
    GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide (widget);
1282
}
1283

1284 1285 1286
static void
gtk_file_chooser_button_map (GtkWidget *widget)
{
1287
  GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map (widget);
1288 1289
}

Matthias Clasen's avatar
Matthias Clasen committed
1290 1291 1292 1293
static gboolean
gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
					   gboolean   group_cycling)
{
1294 1295
  GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
  GtkFileChooserButtonPrivate *priv = button->priv;
Matthias Clasen's avatar
Matthias Clasen committed
1296