gtkfilechooserentry.c 57.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
 * gtkfilechooserentry.c: Entry with filename completion
 * Copyright (C) 2003, Red Hat, Inc.
 *
 * 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.
 */

21
#include "config.h"
22 23
#include <string.h>

24
#include "gtkalignment.h"
25 26
#include "gtkcelllayout.h"
#include "gtkcellrenderertext.h"
27 28
#include "gtkentry.h"
#include "gtkfilechooserentry.h"
29
#include "gtklabel.h"
30
#include "gtkmain.h"
31
#include "gtkwindow.h"
32
#include "gtkintl.h"
33
#include "gtkalias.h"
34

35 36 37 38 39 40 41 42 43 44 45
typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;

#define GTK_FILE_CHOOSER_ENTRY_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
#define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
#define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))

struct _GtkFileChooserEntryClass
{
  GtkEntryClass parent_class;
};

46 47 48 49
/* Action to take when the current folder finishes loading (for explicit or automatic completion) */
typedef enum {
  LOAD_COMPLETE_NOTHING,
  LOAD_COMPLETE_AUTOCOMPLETE,
50
  LOAD_COMPLETE_EXPLICIT_COMPLETION
51 52
} LoadCompleteAction;

53 54 55 56 57 58 59 60 61
typedef enum
{
  REFRESH_OK,
  REFRESH_INVALID_INPUT,
  REFRESH_INCOMPLETE_HOSTNAME,
  REFRESH_NONEXISTENT,
  REFRESH_NOT_LOCAL
} RefreshStatus;

62 63 64 65
struct _GtkFileChooserEntry
{
  GtkEntry parent_instance;

66 67
  GtkFileChooserAction action;

68
  GtkFileSystem *file_system;
69 70
  GFile *base_folder;
  GFile *current_folder_file;
71
  gchar *file_part;
72
  gint file_part_pos;
73

74
  /* Folder being loaded or already loaded */
75 76
  GtkFolder *current_folder;
  GCancellable *load_folder_cancellable;
77

78 79
  LoadCompleteAction load_complete_action;

80 81
  GtkListStore *completion_store;

82 83
  guint start_autocompletion_idle_id;

84 85
  GtkWidget *completion_feedback_window;
  GtkWidget *completion_feedback_label;
86
  guint completion_feedback_timeout_id;
87

88
  guint has_completion : 1;
89
  guint in_change      : 1;
90
  guint eat_tabs       : 1;
91
  guint local_only     : 1;
92 93
};

94 95 96
enum
{
  DISPLAY_NAME_COLUMN,
97
  FILE_COLUMN,
98 99 100
  N_COLUMNS
};

101 102
#define COMPLETION_FEEDBACK_TIMEOUT_MS 2000

Matthias Clasen's avatar
Matthias Clasen committed
103
static void     gtk_file_chooser_entry_iface_init     (GtkEditableClass *iface);
104 105

static void     gtk_file_chooser_entry_finalize       (GObject          *object);
106
static void     gtk_file_chooser_entry_dispose        (GObject          *object);
107
static void     gtk_file_chooser_entry_grab_focus     (GtkWidget        *widget);
108
static void     gtk_file_chooser_entry_unmap          (GtkWidget        *widget);
109 110
static gboolean gtk_file_chooser_entry_focus          (GtkWidget        *widget,
						       GtkDirectionType  direction);
111 112
static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget       *widget,
							GdkEventFocus   *event);
113
static void     gtk_file_chooser_entry_activate       (GtkEntry         *entry);
114 115 116 117
static void     gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
						       const gchar *new_text,
						       gint         new_text_length,
						       gint        *position);
118 119 120 121 122 123 124 125
static void     gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
						       gint         start_pos,
						       gint         end_pos);
static void     gtk_file_chooser_entry_set_position (GtkEditable *editable,
						     gint         position);
static void     gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
							     gint         start_pos,
							     gint         end_pos);
126 127 128 129 130 131 132 133 134 135 136 137 138

#ifdef G_OS_WIN32
static gint     insert_text_callback      (GtkFileChooserEntry *widget,
					   const gchar         *new_text,
					   gint                 new_text_length,
					   gint                *position,
					   gpointer             user_data);
static void     delete_text_callback      (GtkFileChooserEntry *widget,
					   gint                 start_pos,
					   gint                 end_pos,
					   gpointer             user_data);
#endif

139 140 141 142 143 144 145 146
static gboolean match_selected_callback   (GtkEntryCompletion  *completion,
					   GtkTreeModel        *model,
					   GtkTreeIter         *iter,
					   GtkFileChooserEntry *chooser_entry);
static gboolean completion_match_func     (GtkEntryCompletion  *comp,
					   const char          *key,
					   GtkTreeIter         *iter,
					   gpointer             data);
147 148
static char    *maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
						GFile               *file,
149 150
						gchar               *display_name,
						gboolean            *appended);
151

152 153 154 155 156
typedef enum {
  REFRESH_UP_TO_CURSOR_POSITION,
  REFRESH_WHOLE_TEXT
} RefreshMode;

157
static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
158
						  RefreshMode refresh_mode);
159 160
static void finished_loading_cb (GtkFolder *folder,
				 gpointer   data);
161
static void autocomplete (GtkFileChooserEntry *chooser_entry);
162
static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
163
static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
164 165
static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
					const gchar         *feedback);
166

Owen Taylor's avatar
Owen Taylor committed
167
static GtkEditableClass *parent_editable_iface;
168

Matthias Clasen's avatar
Matthias Clasen committed
169 170
G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
Matthias Clasen's avatar
Matthias Clasen committed
171
						gtk_file_chooser_entry_iface_init))
172 173

static void
Matthias Clasen's avatar
Matthias Clasen committed
174
_gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
175 176 177
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
178
  GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
179 180

  gobject_class->finalize = gtk_file_chooser_entry_finalize;
181
  gobject_class->dispose = gtk_file_chooser_entry_dispose;
182

183
  widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
184
  widget_class->unmap = gtk_file_chooser_entry_unmap;
185
  widget_class->focus = gtk_file_chooser_entry_focus;
186
  widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
187 188

  entry_class->activate = gtk_file_chooser_entry_activate;
189 190 191 192 193 194 195 196
}

static void
gtk_file_chooser_entry_iface_init (GtkEditableClass *iface)
{
  parent_editable_iface = g_type_interface_peek_parent (iface);

  iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
197 198 199
  iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
  iface->set_position = gtk_file_chooser_entry_set_position;
  iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
200 201 202
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
203
_gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
204
{
205
  GtkEntryCompletion *comp;
206
  GtkCellRenderer *cell;
207

208 209
  chooser_entry->local_only = TRUE;

210 211
  g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);

212
  comp = gtk_entry_completion_new ();
213
  gtk_entry_completion_set_popup_single_match (comp, FALSE);
214

215 216 217 218
  gtk_entry_completion_set_match_func (comp,
				       completion_match_func,
				       chooser_entry,
				       NULL);
219 220 221 222 223 224 225 226

  cell = gtk_cell_renderer_text_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
                              cell, TRUE);
  gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
                                 cell,
                                 "text", 0);

227
  g_signal_connect (comp, "match-selected",
228
		    G_CALLBACK (match_selected_callback), chooser_entry);
229 230

  gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
231
  g_object_unref (comp);
232

233
#ifdef G_OS_WIN32
234
  g_signal_connect (chooser_entry, "insert-text",
235
		    G_CALLBACK (insert_text_callback), NULL);
236
  g_signal_connect (chooser_entry, "delete-text",
237 238
		    G_CALLBACK (delete_text_callback), NULL);
#endif
239 240 241 242 243 244 245
}

static void
gtk_file_chooser_entry_finalize (GObject *object)
{
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);

246 247 248 249 250 251 252 253 254
  if (chooser_entry->base_folder)
    g_object_unref (chooser_entry->base_folder);

  if (chooser_entry->current_folder)
    g_object_unref (chooser_entry->current_folder);

  if (chooser_entry->current_folder_file)
    g_object_unref (chooser_entry->current_folder_file);

255 256
  g_free (chooser_entry->file_part);

Matthias Clasen's avatar
Matthias Clasen committed
257
  G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
258 259
}

260 261 262 263 264 265 266 267 268 269 270 271
static void
discard_current_folder (GtkFileChooserEntry *chooser_entry)
{
  if (chooser_entry->current_folder)
    {
      g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
					    G_CALLBACK (finished_loading_cb), chooser_entry);
      g_object_unref (chooser_entry->current_folder);
      chooser_entry->current_folder = NULL;
    }
}

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
static void
discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
{
  if (chooser_entry->load_folder_cancellable)
    {
      g_cancellable_cancel (chooser_entry->load_folder_cancellable);
      chooser_entry->load_folder_cancellable = NULL;
    }

  if (chooser_entry->current_folder_file)
    {
      g_object_unref (chooser_entry->current_folder_file);
      chooser_entry->current_folder_file = NULL;
    }
}

288 289 290 291 292
static void
gtk_file_chooser_entry_dispose (GObject *object)
{
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);

293
  remove_completion_feedback (chooser_entry);
294
  discard_current_folder (chooser_entry);
295
  discard_loading_and_current_folder_file (chooser_entry);
296

297 298 299 300 301 302
  if (chooser_entry->start_autocompletion_idle_id != 0)
    {
      g_source_remove (chooser_entry->start_autocompletion_idle_id);
      chooser_entry->start_autocompletion_idle_id = 0;
    }

303
  if (chooser_entry->completion_store)
304 305 306 307 308
    {
      g_object_unref (chooser_entry->completion_store);
      chooser_entry->completion_store = NULL;
    }

309
  if (chooser_entry->file_system)
310 311 312 313
    {
      g_object_unref (chooser_entry->file_system);
      chooser_entry->file_system = NULL;
    }
314

Matthias Clasen's avatar
Matthias Clasen committed
315
  G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
316 317
}

318 319 320 321 322 323 324 325
/* Match functions for the GtkEntryCompletion */
static gboolean
match_selected_callback (GtkEntryCompletion  *completion,
			 GtkTreeModel        *model,
			 GtkTreeIter         *iter,
			 GtkFileChooserEntry *chooser_entry)
{
  char *display_name;
326
  GFile *file;
327
  gint pos;
328
  gboolean dummy;
329 330 331
  
  gtk_tree_model_get (model, iter,
		      DISPLAY_NAME_COLUMN, &display_name,
332
		      FILE_COLUMN, &file,
333 334
		      -1);

335
  if (!display_name || !file)
336
    {
337
      /* these shouldn't complain if passed NULL */
338
      g_object_unref (file);
339
      g_free (display_name);
340
      return FALSE;
341
    }
342

343
  display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
344

345
  pos = chooser_entry->file_part_pos;
346 347 348 349 350 351 352 353 354 355

  /* We don't set in_change here as we want to update the current_folder
   * variable */
  gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
			    pos, -1);
  gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
			    display_name, -1, 
			    &pos);
  gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);

356
  g_object_unref (file);
357 358
  g_free (display_name);

359 360 361
  return TRUE;
}

362 363 364
/* Match function for the GtkEntryCompletion */
static gboolean
completion_match_func (GtkEntryCompletion *comp,
365
		       const char         *key_unused,
366 367 368 369 370 371
		       GtkTreeIter        *iter,
		       gpointer            data)
{
  GtkFileChooserEntry *chooser_entry;
  char *name;
  gboolean result;
372 373
  char *norm_file_part;
  char *norm_name;
374 375 376

  chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);

377 378 379 380
  /* We ignore the key because it is the contents of the entry.  Instead, we
   * just use our precomputed file_part.
   */
  if (!chooser_entry->file_part)
381 382 383 384
    {
      return FALSE;
    }

385
  gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
386
  if (!name)
387 388 389
    {
      return FALSE; /* Uninitialized row, ugh */
    }
390

391
  /* If we have an empty file_part, then we're at the root of a directory.  In
392 393
   * that case, we want to match all non-dot files.  We might want to match
   * dot_files too if show_hidden is TRUE on the fileselector in the future.
394
   */
395
  /* Additionally, support for gnome .hidden files would be sweet, too */
396
  if (chooser_entry->file_part[0] == '\000')
397
    {
398 399 400 401 402
      if (name[0] == '.')
	result = FALSE;
      else
	result = TRUE;
      g_free (name);
403

404 405
      return result;
    }
406 407


408 409
  norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
  norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
410

411 412 413 414 415 416 417 418 419 420 421 422 423 424
#ifdef G_PLATFORM_WIN32
  {
    gchar *temp;

    temp = norm_file_part;
    norm_file_part = g_utf8_casefold (norm_file_part, -1);
    g_free (temp);

    temp = norm_name;
    norm_name = g_utf8_casefold (norm_name, -1);
    g_free (temp);
  }
#endif

425
  result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
426

427 428
  g_free (norm_file_part);
  g_free (norm_name);
429
  g_free (name);
430
  
431 432 433
  return result;
}

434 435 436 437 438
static void
clear_completions (GtkFileChooserEntry *chooser_entry)
{
  chooser_entry->has_completion = FALSE;
  chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
439 440

  remove_completion_feedback (chooser_entry);
441 442
}

443 444 445
static void
beep (GtkFileChooserEntry *chooser_entry)
{
446
  gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
447 448
}

449 450
/* This function will append a directory separator to paths to
 * display_name iff the path associated with it is a directory.
451
 * maybe_append_separator_to_file will g_free the display_name and
452 453
 * return a new one if needed.  Otherwise, it will return the old one.
 * You should be safe calling
454
 *
455
 * display_name = maybe_append_separator_to_file (entry, file, display_name, &appended);
456 457 458 459
 * ...
 * g_free (display_name);
 */
static char *
460 461
maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
				GFile               *file,
462 463
				gchar               *display_name,
				gboolean            *appended)
464
{
465 466
  *appended = FALSE;

467
  if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && file)
468
    {
469 470
      GFileInfo *info;

471
      info = _gtk_folder_get_info (chooser_entry->current_folder, file);
472 473 474

      if (info)
	{
475
	  if (_gtk_file_info_consider_as_directory (info))
476 477
	    {
	      gchar *tmp = display_name;
478
	      display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
479
	      *appended = TRUE;
480 481
	      g_free (tmp);
	    }
482 483

	  g_object_unref (info);
484 485 486 487 488 489
	}
    }

  return display_name;
}

490 491 492 493 494 495 496 497 498 499 500 501
static char *
trim_dir_separator_suffix (const char *str)
{
  int len;

  len = strlen (str);
  if (len > 0 && G_IS_DIR_SEPARATOR (str[len - 1]))
    return g_strndup (str, len - 1);
  else
    return g_strdup (str);
}

502 503 504 505
/* Determines if the completion model has entries with a common prefix relative
 * to the current contents of the entry.  Also, if there's one and only one such
 * path, stores it in unique_path_ret.
 */
506
static gboolean
507 508
find_common_prefix (GtkFileChooserEntry *chooser_entry,
		    gchar               **common_prefix_ret,
509
		    GFile               **unique_file_ret,
510
		    gboolean             *is_complete_not_unique_ret,
511
		    gboolean             *prefix_expands_the_file_part_ret,
512
		    GError              **error)
513
{
514
  GtkEditable *editable;
515
  GtkTreeIter iter;
516
  gboolean parsed;
517
  gboolean valid;
518
  char *text_up_to_cursor;
519
  GFile *parsed_folder_file;
520
  char *parsed_file_part;
521

522
  *common_prefix_ret = NULL;
523
  *unique_file_ret = NULL;
524
  *is_complete_not_unique_ret = FALSE;
525
  *prefix_expands_the_file_part_ret = FALSE;
526

527 528 529 530
  editable = GTK_EDITABLE (chooser_entry);

  text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));

531 532 533 534 535 536
  parsed = _gtk_file_system_parse (chooser_entry->file_system,
				   chooser_entry->base_folder,
				   text_up_to_cursor,
				   &parsed_folder_file,
				   &parsed_file_part,
				   error);
537 538 539 540

  g_free (text_up_to_cursor);

  if (!parsed)
541
    return FALSE;
542

543 544 545
  g_assert (parsed_folder_file != NULL
	    && chooser_entry->current_folder != NULL
	    && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
546

547
  g_object_unref (parsed_folder_file);
548 549 550

  /* First pass: find the common prefix */

551
  valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
552 553 554 555

  while (valid)
    {
      gchar *display_name;
556
      GFile *file;
557 558 559 560

      gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
			  &iter,
			  DISPLAY_NAME_COLUMN, &display_name,
561
			  FILE_COLUMN, &file,
562 563
			  -1);

564
      if (g_str_has_prefix (display_name, parsed_file_part))
565
	{
566
	  if (!*common_prefix_ret)
567
	    {
568
	      *common_prefix_ret = trim_dir_separator_suffix (display_name);
569
	      *unique_file_ret = g_object_ref (file);
570 571 572
	    }
	  else
	    {
573
	      gchar *p = *common_prefix_ret;
574
	      const gchar *q = display_name;
575

576 577 578 579 580
	      while (*p && *p == *q)
		{
		  p++;
		  q++;
		}
581

582 583
	      *p = '\0';

584 585 586 587 588
	      if (*unique_file_ret)
		{
		  g_object_unref (*unique_file_ret);
		  *unique_file_ret = NULL;
		}
589 590 591 592
	    }
	}

      g_free (display_name);
593
      g_object_unref (file);
594
      valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
595
    }
596

597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
  /* Second pass: see if the prefix we found is a complete match */

  if (*common_prefix_ret != NULL)
    {
      valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);

      while (valid)
	{
	  gchar *display_name;
	  int len;

	  gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
			      &iter,
			      DISPLAY_NAME_COLUMN, &display_name,
			      -1);
	  len = strlen (display_name);
	  g_assert (len > 0);

	  if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
	    len--;

618
	  if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
619 620 621 622 623
	    *is_complete_not_unique_ret = TRUE;

	  g_free (display_name);
	  valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
	}
624 625 626 627

      /* Finally:  Did we generate a new completion, or was the user's input already completed as far as it could go? */

      *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
628 629
    }

630
  g_free (parsed_file_part);
631 632

  return TRUE;
633 634
}

635 636 637 638 639 640 641 642 643
static gboolean
char_after_cursor_is_directory_separator (GtkFileChooserEntry *chooser_entry)
{
  int cursor_pos;
  gboolean result;

  result = FALSE;

  cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
644
  if (cursor_pos < gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)))
645 646 647 648 649 650 651 652 653 654 655 656 657
    {
      char *next_char_str;

      next_char_str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry), cursor_pos, cursor_pos + 1);
      if (G_IS_DIR_SEPARATOR (*next_char_str))
	result = TRUE;

      g_free (next_char_str);
    }

  return result;
}

658
typedef enum {
659 660 661
  INVALID_INPUT,		/* what the user typed is bogus */
  NO_MATCH,			/* no matches based on what the user typed */
  NOTHING_INSERTED_COMPLETE,	/* what the user typed is already completed as far as it will go */
662
  NOTHING_INSERTED_UNIQUE,	/* what the user typed is already completed, and is a unique match */
663 664 665
  COMPLETED,			/* completion inserted (ambiguous suffix) */
  COMPLETED_UNIQUE,		/* completion inserted, and it is a complete name and a unique match */
  COMPLETE_BUT_NOT_UNIQUE	/* completion inserted, it is a complete name but not unique */
666 667
} CommonPrefixResult;

668
/* Finds a common prefix based on the contents of the entry and mandatorily appends it */
669
static CommonPrefixResult
670
append_common_prefix (GtkFileChooserEntry *chooser_entry,
671 672
		      gboolean             highlight,
		      gboolean             show_errors)
673 674
{
  gchar *common_prefix;
675
  GFile *unique_file;
676
  gboolean is_complete_not_unique;
677
  gboolean prefix_expands_the_file_part;
678
  GError *error;
679
  CommonPrefixResult result = NO_MATCH;
680
  gboolean have_result;
681

682 683
  clear_completions (chooser_entry);

684 685 686
  if (chooser_entry->completion_store == NULL)
    return NO_MATCH;

687
  error = NULL;
688
  if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
689
    {
690 691 692 693 694 695
      /* If the user types an incomplete hostname ("http://foo" without a slash
       * after that), it's not an error.  We just don't want to pop up a
       * meaningless completion window in that state.
       */
      if (!g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME)
	  && show_errors)
696 697
	{
	  beep (chooser_entry);
698
	  pop_up_completion_feedback (chooser_entry, _("Invalid path"));
699 700 701 702 703 704 705 706
	}

      g_error_free (error);

      return INVALID_INPUT;
    }

  have_result = FALSE;
707

708
  if (unique_file)
709
    {
710
      if (!char_after_cursor_is_directory_separator (chooser_entry))
711 712 713 714 715 716 717 718 719 720
	{
	  gboolean appended;

	  common_prefix = maybe_append_separator_to_file (chooser_entry,
							  unique_file,
							  common_prefix,
							  &appended);
	  if (appended)
	    prefix_expands_the_file_part = TRUE;
	}
721

722
      g_object_unref (unique_file);
723

724 725
      if (prefix_expands_the_file_part)
	result = COMPLETED_UNIQUE;
726
      else
727
	result = NOTHING_INSERTED_UNIQUE;
728 729

      have_result = TRUE;
730
    }
731 732
  else
    {
733 734 735 736 737
      if (is_complete_not_unique)
	{
	  result = COMPLETE_BUT_NOT_UNIQUE;
	  have_result = TRUE;
	}
738
    }
739 740 741

  if (common_prefix)
    {
742
      gint cursor_pos;
743 744 745
      gint common_prefix_len;
      gint pos;

746
      cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
747 748
      common_prefix_len = g_utf8_strlen (common_prefix, -1);

749
      pos = chooser_entry->file_part_pos;
750

751 752 753 754 755 756 757 758 759
      if (prefix_expands_the_file_part)
	{
	  chooser_entry->in_change = TRUE;
	  gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
				    pos, cursor_pos);
	  gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
				    common_prefix, -1, 
				    &pos);
	  chooser_entry->in_change = FALSE;
760

761 762 763 764
	  if (highlight)
	    {
	      gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
					  cursor_pos,
765
					  pos); /* equivalent to cursor_pos + common_prefix_len); */
766 767 768 769
	      chooser_entry->has_completion = TRUE;
	    }
	  else
	    gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
770
	}
771
      else if (!have_result)
772 773 774 775
	{
	  result = NOTHING_INSERTED_COMPLETE;
	  have_result = TRUE;
	}
776

777
      g_free (common_prefix);
778 779 780 781

      if (have_result)
	return result;
      else
782
	return COMPLETED;
783 784 785 786 787 788
    }
  else
    {
      if (have_result)
	return result;
      else
789
	return NO_MATCH;
790
    }
791 792
}

793 794 795 796 797
static void
gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
				       const gchar *new_text,
				       gint         new_text_length,
				       gint        *position)
798
{
799 800 801
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
  gint old_text_len;
  gint insert_pos;
802

803
  old_text_len = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
804
  insert_pos = *position;
805

806
  parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
807

808 809
  if (chooser_entry->in_change)
    return;
810

811 812
  remove_completion_feedback (chooser_entry);

813 814
  if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
       || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
815 816
      && insert_pos == old_text_len)
    install_start_autocompletion_idle (chooser_entry);
817 818
}

819 820 821 822 823 824 825 826 827
static void
clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
{
  if (chooser_entry->in_change)
    return;

  clear_completions (chooser_entry);
}

828 829 830 831
static void
gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
				       gint         start_pos,
				       gint         end_pos)
832
{
833 834 835
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);

  parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
836

837
  clear_completions_if_not_in_change (chooser_entry);
838 839
}

840
static void
841 842
gtk_file_chooser_entry_set_position (GtkEditable *editable,
				     gint         position)
843
{
844 845 846 847
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);

  parent_editable_iface->set_position (editable, position);

848
  clear_completions_if_not_in_change (chooser_entry);
849 850
}

851
static void
852 853 854
gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
					     gint         start_pos,
					     gint         end_pos)
855
{
856 857
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);

858 859
  parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);

860
  clear_completions_if_not_in_change (chooser_entry);
861 862
}

863 864 865 866 867 868 869
static void
gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
{
  GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
  _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
}

870 871 872 873 874 875 876 877 878 879
static void
gtk_file_chooser_entry_unmap (GtkWidget *widget)
{
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);

  remove_completion_feedback (chooser_entry);

  GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
}

880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
static gboolean
completion_feedback_window_expose_event_cb (GtkWidget      *widget,
					    GdkEventExpose *event,
					    gpointer        data)
{
  /* Stolen from gtk_tooltip_paint_window() */

  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);

  gtk_paint_flat_box (chooser_entry->completion_feedback_window->style,
		      chooser_entry->completion_feedback_window->window,
		      GTK_STATE_NORMAL,
		      GTK_SHADOW_OUT,
		      NULL,
		      chooser_entry->completion_feedback_window,
		      "tooltip",
		      0, 0,
		      chooser_entry->completion_feedback_window->allocation.width,
		      chooser_entry->completion_feedback_window->allocation.height);

  return FALSE;
}

903 904 905
static void
set_invisible_mouse_cursor (GdkWindow *window)
{
906
  GdkDisplay *display;
907 908
  GdkCursor *cursor;

909 910
  display = gdk_drawable_get_display (window);
  cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927

  gdk_window_set_cursor (window, cursor);

  gdk_cursor_unref (cursor);
}

static void
completion_feedback_window_realize_cb (GtkWidget *widget,
				       gpointer data)
{
  /* We hide the mouse cursor inside the completion feedback window, since
   * GtkEntry hides the cursor when the user types.  We don't want the cursor to
   * come back if the completion feedback ends up where the mouse is.
   */
  set_invisible_mouse_cursor (widget->window);
}

928 929 930 931 932
static void
create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
{
  /* Stolen from gtk_tooltip_init() */

933 934
  GtkWidget *alignment;

935 936 937 938 939 940 941
  chooser_entry->completion_feedback_window = gtk_window_new (GTK_WINDOW_POPUP);
  gtk_window_set_type_hint (GTK_WINDOW (chooser_entry->completion_feedback_window),
			    GDK_WINDOW_TYPE_HINT_TOOLTIP);
  gtk_widget_set_app_paintable (chooser_entry->completion_feedback_window, TRUE);
  gtk_window_set_resizable (GTK_WINDOW (chooser_entry->completion_feedback_window), FALSE);
  gtk_widget_set_name (chooser_entry->completion_feedback_window, "gtk-tooltip");

942 943 944 945 946 947 948 949 950
  alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
			     chooser_entry->completion_feedback_window->style->ythickness,
			     chooser_entry->completion_feedback_window->style->ythickness,
			     chooser_entry->completion_feedback_window->style->xthickness,
			     chooser_entry->completion_feedback_window->style->xthickness);
  gtk_container_add (GTK_CONTAINER (chooser_entry->completion_feedback_window), alignment);
  gtk_widget_show (alignment);

951
  g_signal_connect (chooser_entry->completion_feedback_window, "expose-event",
952
		    G_CALLBACK (completion_feedback_window_expose_event_cb), chooser_entry);
953 954 955
  g_signal_connect (chooser_entry->completion_feedback_window, "realize",
		    G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
  /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
956 957

  chooser_entry->completion_feedback_label = gtk_label_new (NULL);
958 959
  gtk_container_add (GTK_CONTAINER (alignment), chooser_entry->completion_feedback_label);
  gtk_widget_show (chooser_entry->completion_feedback_label);
960 961
}

962 963 964 965 966 967 968 969 970 971 972 973 974 975
static gboolean
completion_feedback_timeout_cb (gpointer data)
{
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);

  chooser_entry->completion_feedback_timeout_id = 0;

  remove_completion_feedback (chooser_entry);
  return FALSE;
}

static void
install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
{
976 977
  if (chooser_entry->completion_feedback_timeout_id != 0)
    g_source_remove (chooser_entry->completion_feedback_timeout_id);
978 979 980 981 982 983

  chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
									   completion_feedback_timeout_cb,
									   chooser_entry);
}

984
/* Gets the x position of the text cursor in the entry, in widget coordinates */
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
static void
get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
		    gint                *x_ret)
{
  /* FIXME: see the docs for gtk_entry_get_layout_offsets().  As an exercise for
   * the reader, you have to implement support for the entry's scroll offset and
   * RTL layouts and all the fancy Pango stuff.
   */

  PangoLayout *layout;
  gint layout_x, layout_y;
  gint layout_index;
  PangoRectangle strong_pos;

  layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));

  gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);

  layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
						       GTK_ENTRY (chooser_entry)->current_pos);

  pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);

  *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
}

1011 1012 1013 1014 1015 1016 1017
static void
show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
{
  /* More or less stolen from gtk_tooltip_position() */

  GtkRequisition feedback_req;
  gint entry_x, entry_y;
1018
  gint cursor_x;
1019
  GtkAllocation *entry_allocation;
1020
  int feedback_x, feedback_y;
1021 1022 1023 1024 1025 1026

  gtk_widget_size_request (chooser_entry->completion_feedback_window, &feedback_req);

  gdk_window_get_origin (GTK_WIDGET (chooser_entry)->window, &entry_x, &entry_y);
  entry_allocation = &(GTK_WIDGET (chooser_entry)->allocation);

1027
  get_entry_cursor_x (chooser_entry, &cursor_x);
1028

1029 1030
  /* FIXME: fit to the screen if we bump on the screen's edge */
  feedback_x = entry_x + cursor_x + entry_allocation->height / 2; /* cheap "half M-width" */
1031 1032
  feedback_y = entry_y + (entry_allocation->height - feedback_req.height) / 2;

1033
  gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1034
  gtk_widget_show (chooser_entry->completion_feedback_window);
1035

1036
  install_completion_feedback_timer (chooser_entry);
1037 1038
}

1039 1040 1041 1042
static void
pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
			    const gchar         *feedback)
{
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
  if (chooser_entry->completion_feedback_window == NULL)
    create_completion_feedback_window (chooser_entry);

  gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);

  show_completion_feedback_window (chooser_entry);
}

static void
remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
{
1054 1055 1056
  if (chooser_entry->completion_feedback_window)
    gtk_widget_destroy (chooser_entry->completion_feedback_window);

1057 1058
  chooser_entry->completion_feedback_window = NULL;
  chooser_entry->completion_feedback_label = NULL;
1059

1060 1061 1062 1063 1064
  if (chooser_entry->completion_feedback_timeout_id != 0)
    {
      g_source_remove (chooser_entry->completion_feedback_timeout_id);
      chooser_entry->completion_feedback_timeout_id = 0;
    }
1065 1066
}

1067 1068 1069
static void
explicitly_complete (GtkFileChooserEntry *chooser_entry)
{
1070 1071
  CommonPrefixResult result;

1072
  g_assert (chooser_entry->current_folder != NULL);
1073
  g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1074 1075 1076

  /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
   *
1077
   * - If there is a common prefix, insert it (done)
1078
   * - If there is no common prefix, pop up the suggestion window
1079
   * - If there are no matches at all, beep and bring up a tooltip (done)
1080 1081
   * - If the suggestion window is already up, scroll it
   */
1082 1083 1084 1085
  result = append_common_prefix (chooser_entry, FALSE, TRUE);

  switch (result)
    {
1086 1087
    case INVALID_INPUT:
      /* We already beeped in append_common_prefix(); do nothing here */
1088 1089
      break;

1090
    case NO_MATCH:
1091
      beep (chooser_entry);
Matthias Clasen's avatar
Matthias Clasen committed
1092 1093 1094
      /* translators: this text is shown when there are no completions 
       * for something the user typed in a file chooser entry
       */
1095
      pop_up_completion_feedback (chooser_entry, _("No match"));
1096 1097
      break;

1098 1099 1100 1101
    case NOTHING_INSERTED_COMPLETE:
      /* FIXME: pop up the suggestion window or scroll it */
      break;

1102
    case NOTHING_INSERTED_UNIQUE:
Matthias Clasen's avatar
Matthias Clasen committed
1103 1104 1105
      /* translators: this text is shown when there is exactly one completion 
       * for something the user typed in a file chooser entry
       */
1106 1107 1108
      pop_up_completion_feedback (chooser_entry, _("Sole completion"));
      break;

1109 1110 1111 1112 1113
    case COMPLETED:
      /* Nothing to do */
      break;

    case COMPLETED_UNIQUE:
1114
      /* Nothing to do */
1115 1116
      break;

1117
    case COMPLETE_BUT_NOT_UNIQUE:
Matthias Clasen's avatar
Matthias Clasen committed
1118 1119 1120 1121
      /* translators: this text is shown when the text in a file chooser
       * entry is a complete filename, but could be continued to find
       * a longer match
       */
1122
      pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1123 1124 1125 1126 1127
      break;

    default:
      g_assert_not_reached ();
    }
1128 1129 1130 1131 1132
}

static void
start_explicit_completion (GtkFileChooserEntry *chooser_entry)
{
1133 1134 1135
  RefreshStatus status;
  gboolean is_error;
  char *feedback_msg;
1136

1137 1138 1139 1140 1141
  status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);

  is_error = FALSE;

  switch (status)
1142
    {
1143 1144
    case REFRESH_OK:
      g_assert (chooser_entry->current_folder_file != NULL);
1145

1146 1147
      if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
	explicitly_complete (chooser_entry);
1148 1149 1150
      else
	{
	  chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1151

1152 1153
	  /* Translators: this text is shown while the system is searching
	   * for possible completions for filenames in a file chooser entry. */
1154 1155
	  pop_up_completion_feedback (chooser_entry, _("Completing..."));
	}
1156

1157
      break;
1158

1159 1160
    case REFRESH_INVALID_INPUT:
      is_error = TRUE;
1161 1162
      /* Translators: this is shown in the feedback for Tab-completion in a file
       * chooser's text entry, when the user enters an invalid path. */
1163 1164 1165 1166 1167 1168 1169
      feedback_msg = _("Invalid path");
      break;

    case REFRESH_INCOMPLETE_HOSTNAME:
      is_error = TRUE;

      if (chooser_entry->local_only)
1170 1171 1172 1173 1174 1175 1176 1177
	{
	  /* hostnames in a local_only file chooser?  user error */

	  /* Translators: this is shown in the feedback for Tab-completion in a
	   * file chooser's text entry when the user enters something like
	   * "sftp://blahblah" in an app that only supports local filenames. */
	  feedback_msg = _("Only local files may be selected");
	}
1178 1179 1180
      else
	{
	  /* Another option is to complete the hostname based on the remote volumes that are mounted */
1181 1182 1183 1184

	  /* Translators: this is shown in the feedback for Tab-completion in a
	   * file chooser's text entry when the user hasn't entered the first '/'
	   * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1185 1186 1187 1188 1189 1190 1191
	  feedback_msg = _("Incomplete hostname; end it with '/'");
	}

      break;

    case REFRESH_NONEXISTENT:
      is_error = TRUE;
1192 1193 1194 1195

      /* Translators: this is shown in the feedback for Tab-completion in a file
       * chooser's text entry when the user enters a path that does not exist
       * and then hits Tab */
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
      feedback_msg = _("Path does not exist");
      break;

    case REFRESH_NOT_LOCAL:
      is_error = TRUE;
      feedback_msg = _("Only local files may be selected");
      break;

    default:
      g_assert_not_reached ();
1206 1207 1208
      return;
    }

1209
  if (is_error)
1210
    {
1211
      g_assert (chooser_entry->current_folder_file == NULL);
1212

1213 1214 1215
      beep (chooser_entry);
      pop_up_completion_feedback (chooser_entry, feedback_msg);
      chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1216 1217 1218
    }
}

1219 1220 1221 1222
static gboolean
gtk_file_chooser_entry_focus (GtkWidget        *widget,
			      GtkDirectionType  direction)
{
1223 1224 1225
  GtkFileChooserEntry *chooser_entry;
  GtkEditable *editable;
  GtkEntry *entry;
1226
  GdkModifierType state;
1227 1228 1229 1230 1231
  gboolean control_pressed;

  chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
  editable = GTK_EDITABLE (widget);
  entry = GTK_ENTRY (widget);
1232

1233
  if (!chooser_entry->eat_tabs)
Matthias Clasen's avatar
Matthias Clasen committed
1234
    return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
1235

1236 1237
  control_pressed = FALSE;

1238
  if (gtk_get_current_event_state (&state))
1239
    {
1240 1241 1242 1243 1244 1245 1246
      if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
	control_pressed = TRUE;
    }

  /* This is a bit evil -- it makes Tab never leave the entry. It basically
   * makes it 'safe' for people to hit. */
  if ((direction == GTK_DIR_TAB_FORWARD) &&
1247
      (gtk_widget_has_focus (widget)) &&
1248 1249
      (! control_pressed))
    {
1250
      if (chooser_entry->has_completion)
1251
	gtk_editable_set_position (editable, gtk_entry_get_text_length (entry));
1252
      else
1253
	start_explicit_completion (chooser_entry);
1254

1255 1256 1257
      return TRUE;
    }
  else
Matthias Clasen's avatar
Matthias Clasen committed
1258
    return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
1259 1260
}

1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
static gboolean
gtk_file_chooser_entry_focus_out_event (GtkWidget     *widget,
					GdkEventFocus *event)
{
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);

  chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
 
  return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
}

1272
static void
1273
commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1274 1275 1276
{
  if (chooser_entry->has_completion)
    {
1277
      gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1278
				 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1279
    }
1280

1281
  /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1282
  refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1283 1284 1285 1286 1287 1288
}

static void
gtk_file_chooser_entry_activate (GtkEntry *entry)
{
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1289

1290
  commit_completion_and_refresh (chooser_entry);
Matthias Clasen's avatar
Matthias Clasen committed
1291
  GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1292 1293
}

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
static void
discard_completion_store (GtkFileChooserEntry *chooser_entry)
{
  if (!chooser_entry->completion_store)
    return;

  gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
  g_object_unref (chooser_entry->completion_store);
  chooser_entry->completion_store = NULL;
}

1305 1306 1307 1308
/* Fills the completion store from the contents of the current folder */
static void
populate_completion_store (GtkFileChooserEntry *chooser_entry)
{
1309
  GSList *files;
1310 1311
  GSList *tmp_list;

1312
  discard_completion_store (chooser_entry);
1313

1314
  files = _gtk_folder_list_children (chooser_entry->current_folder);
1315

1316 1317
  chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
							G_TYPE_STRING,
1318
							G_TYPE_FILE);
1319

1320
  for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1321
    {
1322 1323 1324 1325
      GFileInfo *info;
      GFile *file;

      file = tmp_list->data;