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

34 35
#include "gdkkeysyms.h"

36 37 38 39 40 41 42 43 44 45 46
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;
};

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

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

63 64 65 66
struct _GtkFileChooserEntry
{
  GtkEntry parent_instance;

67 68
  GtkFileChooserAction action;

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

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

79 80
  LoadCompleteAction load_complete_action;

81 82
  GtkListStore *completion_store;

83 84
  guint start_autocompletion_idle_id;

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

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

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

102 103
#define COMPLETION_FEEDBACK_TIMEOUT_MS 2000

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

static void     gtk_file_chooser_entry_finalize       (GObject          *object);
107
static void     gtk_file_chooser_entry_dispose        (GObject          *object);
108
static void     gtk_file_chooser_entry_grab_focus     (GtkWidget        *widget);
109
static void     gtk_file_chooser_entry_unmap          (GtkWidget        *widget);
110 111
static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
							GdkEventKey *event);
112 113
static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget       *widget,
							GdkEventFocus   *event);
114
static void     gtk_file_chooser_entry_activate       (GtkEntry         *entry);
115 116 117 118
static void     gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
						       const gchar *new_text,
						       gint         new_text_length,
						       gint        *position);
119 120 121 122 123 124 125 126
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);
127 128 129 130 131 132 133 134 135 136 137 138 139

#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

140 141 142 143 144 145 146 147
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);
148 149
static char    *maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
						GFile               *file,
150 151
						gchar               *display_name,
						gboolean            *appended);
152

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

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

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

Matthias Clasen's avatar
Matthias Clasen committed
170 171
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
172
						gtk_file_chooser_entry_iface_init))
173 174

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

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

184
  widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
185
  widget_class->unmap = gtk_file_chooser_entry_unmap;
186
  widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
187
  widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
188 189

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

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;
198 199 200
  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;
201 202 203
}

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

209 210
  chooser_entry->local_only = TRUE;

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

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

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

  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);

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

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

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

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

247 248 249 250 251 252 253 254 255
  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);

256 257
  g_free (chooser_entry->file_part);

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

261 262 263 264 265 266 267 268 269 270 271 272
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;
    }
}

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
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;
    }
}

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

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

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

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

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

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

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

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

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

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

  /* 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);

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

360 361 362
  return TRUE;
}

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

  chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);

378 379 380 381
  /* 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)
382 383 384 385
    {
      return FALSE;
    }

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

392
  /* If we have an empty file_part, then we're at the root of a directory.  In
393 394
   * 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.
395
   */
396
  /* Additionally, support for gnome .hidden files would be sweet, too */
397
  if (chooser_entry->file_part[0] == '\000')
398
    {
399 400 401 402 403
      if (name[0] == '.')
	result = FALSE;
      else
	result = TRUE;
      g_free (name);
404

405 406
      return result;
    }
407 408


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

412 413 414 415 416 417 418 419 420 421 422 423 424 425
#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

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

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

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

  remove_completion_feedback (chooser_entry);
442 443
}

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

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

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

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

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

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

  return display_name;
}

491 492 493 494 495 496 497 498 499 500 501 502
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);
}

503 504 505 506
/* 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.
 */
507
static gboolean
508 509
find_common_prefix (GtkFileChooserEntry *chooser_entry,
		    gchar               **common_prefix_ret,
510
		    GFile               **unique_file_ret,
511
		    gboolean             *is_complete_not_unique_ret,
512
		    gboolean             *prefix_expands_the_file_part_ret,
513
		    GError              **error)
514
{
515
  GtkEditable *editable;
516
  GtkTreeIter iter;
517
  gboolean parsed;
518
  gboolean valid;
519
  char *text_up_to_cursor;
520
  GFile *parsed_folder_file;
521
  char *parsed_file_part;
522

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

528 529 530 531
  editable = GTK_EDITABLE (chooser_entry);

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

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

  g_free (text_up_to_cursor);

  if (!parsed)
542
    return FALSE;
543

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

548
  g_object_unref (parsed_folder_file);
549 550 551

  /* First pass: find the common prefix */

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

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

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

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

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

583 584
	      *p = '\0';

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

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

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
  /* 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--;

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

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

      /* 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);
629 630
    }

631
  g_free (parsed_file_part);
632 633

  return TRUE;
634 635
}

636 637 638 639 640 641 642 643 644
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));
645
  if (cursor_pos < gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)))
646 647 648 649 650 651 652 653 654 655 656 657 658
    {
      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;
}

659
typedef enum {
660 661 662
  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 */
663
  NOTHING_INSERTED_UNIQUE,	/* what the user typed is already completed, and is a unique match */
664 665 666
  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 */
667 668
} CommonPrefixResult;

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

683 684
  clear_completions (chooser_entry);

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

688
  error = NULL;
689
  if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
690
    {
691 692 693 694 695 696
      /* 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)
697 698
	{
	  beep (chooser_entry);
699
	  pop_up_completion_feedback (chooser_entry, _("Invalid path"));
700 701 702 703 704 705 706 707
	}

      g_error_free (error);

      return INVALID_INPUT;
    }

  have_result = FALSE;
708

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

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

723
      g_object_unref (unique_file);
724

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

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

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

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

750
      pos = chooser_entry->file_part_pos;
751

752 753 754 755 756 757 758 759 760
      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;
761

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

778
      g_free (common_prefix);
779 780 781 782

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

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

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

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

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

812 813
  remove_completion_feedback (chooser_entry);

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

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

  clear_completions (chooser_entry);
}

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

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

838
  clear_completions_if_not_in_change (chooser_entry);
839 840
}

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

  parent_editable_iface->set_position (editable, position);

849
  clear_completions_if_not_in_change (chooser_entry);
850 851
}

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

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

861
  clear_completions_if_not_in_change (chooser_entry);
862 863
}

864 865 866 867 868 869 870
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));
}

871 872 873 874 875 876 877 878 879 880
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);
}

881 882 883 884 885 886 887 888
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);
889
  GtkAllocation allocation;
890

891 892 893 894
  gtk_widget_get_allocation (chooser_entry->completion_feedback_window, &allocation);

  gtk_paint_flat_box (gtk_widget_get_style (chooser_entry->completion_feedback_window),
                      gtk_widget_get_window (chooser_entry->completion_feedback_window),
895 896 897 898 899 900
		      GTK_STATE_NORMAL,
		      GTK_SHADOW_OUT,
		      NULL,
		      chooser_entry->completion_feedback_window,
		      "tooltip",
		      0, 0,
901
                      allocation.width, allocation.height);
902 903 904 905

  return FALSE;
}

906 907 908
static void
set_invisible_mouse_cursor (GdkWindow *window)
{
909
  GdkDisplay *display;
910 911
  GdkCursor *cursor;

912 913
  display = gdk_drawable_get_display (window);
  cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
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.
   */
928
  set_invisible_mouse_cursor (gtk_widget_get_window (widget));
929 930
}

931 932 933 934 935
static void
create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
{
  /* Stolen from gtk_tooltip_init() */

936
  GtkStyle *style;
937 938
  GtkWidget *alignment;

939 940 941 942 943 944 945
  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");

946
  alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
947
  style = gtk_widget_get_style (chooser_entry->completion_feedback_window);
948
  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
949 950
                             style->ythickness, style->ythickness,
                             style->xthickness, style->xthickness);
951 952 953
  gtk_container_add (GTK_CONTAINER (chooser_entry->completion_feedback_window), alignment);
  gtk_widget_show (alignment);

954
  g_signal_connect (chooser_entry->completion_feedback_window, "expose-event",
955
		    G_CALLBACK (completion_feedback_window_expose_event_cb), chooser_entry);
956 957 958
  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 */
959 960

  chooser_entry->completion_feedback_label = gtk_label_new (NULL);
961 962
  gtk_container_add (GTK_CONTAINER (alignment), chooser_entry->completion_feedback_label);
  gtk_widget_show (chooser_entry->completion_feedback_label);
963 964
}

965 966 967 968 969 970 971 972 973 974 975 976 977 978
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)
{
979 980
  if (chooser_entry->completion_feedback_timeout_id != 0)
    g_source_remove (chooser_entry->completion_feedback_timeout_id);
981 982 983 984 985 986

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

987
/* Gets the x position of the text cursor in the entry, in widget coordinates */
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
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;
}

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

  GtkRequisition feedback_req;
1020
  GtkWidget *widget = GTK_WIDGET (chooser_entry);
1021
  gint entry_x, entry_y;
1022
  gint cursor_x;
1023
  GtkAllocation entry_allocation;
1024
  int feedback_x, feedback_y;
1025 1026 1027

  gtk_widget_size_request (chooser_entry->completion_feedback_window, &feedback_req);

1028 1029
  gdk_window_get_origin (gtk_widget_get_window (widget), &entry_x, &entry_y);
  gtk_widget_get_allocation (widget, &entry_allocation);
1030

1031
  get_entry_cursor_x (chooser_entry, &cursor_x);
1032

1033
  /* FIXME: fit to the screen if we bump on the screen's edge */
1034
  /* cheap "half M-width", use height as approximation of character em-size */
1035 1036
  feedback_x = entry_x + cursor_x + entry_allocation.height / 2;
  feedback_y = entry_y + (entry_allocation.height - feedback_req.height) / 2;
1037

1038
  gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1039
  gtk_widget_show (chooser_entry->completion_feedback_window);
1040

1041
  install_completion_feedback_timer (chooser_entry);
1042 1043
}

1044 1045 1046 1047
static void
pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
			    const gchar         *feedback)
{
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
  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)
{
1059 1060 1061
  if (chooser_entry->completion_feedback_window)
    gtk_widget_destroy (chooser_entry->completion_feedback_window);

1062 1063
  chooser_entry->completion_feedback_window = NULL;
  chooser_entry->completion_feedback_label = NULL;
1064

1065 1066 1067 1068 1069
  if (chooser_entry->completion_feedback_timeout_id != 0)
    {
      g_source_remove (chooser_entry->completion_feedback_timeout_id);
      chooser_entry->completion_feedback_timeout_id = 0;
    }
1070 1071
}

1072 1073 1074
static void
explicitly_complete (GtkFileChooserEntry *chooser_entry)
{
1075 1076
  CommonPrefixResult result;

1077
  g_assert (chooser_entry->current_folder != NULL);
1078
  g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1079 1080 1081

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

  switch (result)
    {
1091 1092
    case INVALID_INPUT:
      /* We already beeped in append_common_prefix(); do nothing here */
1093 1094
      break;

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

1103 1104 1105 1106
    case NOTHING_INSERTED_COMPLETE:
      /* FIXME: pop up the suggestion window or scroll it */
      break;

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

1114 1115 1116 1117 1118
    case COMPLETED:
      /* Nothing to do */
      break;

    case COMPLETED_UNIQUE:
1119
      /* Nothing to do */
1120 1121
      break;

1122
    case COMPLETE_BUT_NOT_UNIQUE:
Matthias Clasen's avatar
Matthias Clasen committed
1123 1124 1125 1126
      /* 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
       */
1127
      pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1128 1129 1130 1131 1132
      break;

    default:
      g_assert_not_reached ();
    }
1133 1134 1135 1136 1137
}

static void
start_explicit_completion (GtkFileChooserEntry *chooser_entry)
{
1138 1139 1140
  RefreshStatus status;
  gboolean is_error;
  char *feedback_msg;
1141

1142 1143 1144 1145 1146
  status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);

  is_error = FALSE;

  switch (status)
1147
    {
1148 1149
    case REFRESH_OK:
      g_assert (chooser_entry->current_folder_file != NULL);
1150

1151 1152
      if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
	explicitly_complete (chooser_entry);
1153 1154 1155
      else
	{
	  chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1156

1157 1158
	  /* Translators: this text is shown while the system is searching
	   * for possible completions for filenames in a file chooser entry. */
1159 1160
	  pop_up_completion_feedback (chooser_entry, _("Completing..."));
	}
1161

1162
      break;
1163

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

    case REFRESH_INCOMPLETE_HOSTNAME:
      is_error = TRUE;

      if (chooser_entry->local_only)
1175 1176 1177 1178 1179 1180 1181 1182
	{
	  /* 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");
	}
1183 1184 1185
      else
	{
	  /* Another option is to complete the hostname based on the remote volumes that are mounted */
1186 1187 1188 1189

	  /* 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]") */
1190 1191 1192 1193 1194 1195 1196
	  feedback_msg = _("Incomplete hostname; end it with '/'");
	}

      break;

    case REFRESH_NONEXISTENT:
      is_error = TRUE;
1197 1198 1199 1200

      /* 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 */
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
      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 ();
1211 1212 1213
      return;
    }

1214
  if (is_error)
1215
    {
1216
      g_assert (chooser_entry->current_folder_file == NULL);
1217

1218 1219 1220
      beep (chooser_entry);
      pop_up_completion_feedback (chooser_entry, feedback_msg);
      chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1221 1222 1223
    }
}

1224
static gboolean
1225 1226
gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
					GdkEventKey *event)
1227
{
1228 1229 1230
  GtkFileChooserEntry *chooser_entry;
  GtkEditable *editable;
  GtkEntry *entry;
1231
  GdkModifierType state;
1232 1233 1234 1235 1236
  gboolean control_pressed;

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

1238
  if (!chooser_entry->eat_tabs)
1239
    return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1240

1241 1242
  control_pressed = FALSE;

1243
  if (gtk_get_current_event_state (&state))
1244
    {
1245 1246 1247 1248 1249 1250
      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. */
1251
  if (event->keyval == GDK_Tab && !control_pressed)
1252
    {
1253
      if (chooser_entry->has_completion)
1254
	gtk_editable_set_position (editable, gtk_entry_get_text_length (entry));
1255
      else
1256
	start_explicit_completion (chooser_entry);
1257

1258
      return TRUE;
1259 1260 1261 1262
     }

  return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);

1263 1264
}

1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
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);
}

1276
static void
1277
commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1278 1279 1280
{
  if (chooser_entry->has_completion)
    {
1281
      gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1282
				 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1283
    }
1284

1285
  /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1286
  refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1287 1288 1289 1290 1291 1292
}

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

1294
  commit_completion_and_refresh (chooser_entry);
Matthias Clasen's avatar
Matthias Clasen committed
1295
  GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1296 1297
}