gtkfilechooserentry.c 56.9 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
						gchar               *display_name);
150

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

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

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

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

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

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

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

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

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;
196 197 198
  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;
199 200 201
}

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

207 208
  chooser_entry->local_only = TRUE;

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

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

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

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

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

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

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

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

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

254 255
  g_free (chooser_entry->file_part);

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

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

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

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

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

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

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

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

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

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

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

341
  display_name = maybe_append_separator_to_file (chooser_entry, file, display_name);
342

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

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

354
  g_object_unref (file);
355 356
  g_free (display_name);

357 358 359
  return TRUE;
}

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

  chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);

375 376 377 378
  /* 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)
379 380 381 382
    {
      return FALSE;
    }

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

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

402 403
      return result;
    }
404 405


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

409 410 411 412 413 414 415 416 417 418 419 420 421 422
#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

423
  result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
424

425 426
  g_free (norm_file_part);
  g_free (norm_name);
427
  g_free (name);
428
  
429 430 431
  return result;
}

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

  remove_completion_feedback (chooser_entry);
439 440
}

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

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

466
      info = _gtk_folder_get_info (chooser_entry->current_folder, file);
467 468 469

      if (info)
	{
470
	  if (_gtk_file_info_consider_as_directory (info))
471 472
	    {
	      gchar *tmp = display_name;
473
	      display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
474 475
	      g_free (tmp);
	    }
476 477

	  g_object_unref (info);
478 479 480 481 482 483
	}
    }

  return display_name;
}

484 485 486 487 488 489 490 491 492 493 494 495
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);
}

496 497 498 499
/* 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.
 */
500
static gboolean
501 502
find_common_prefix (GtkFileChooserEntry *chooser_entry,
		    gchar               **common_prefix_ret,
503
		    GFile               **unique_file_ret,
504
		    gboolean             *is_complete_not_unique_ret,
505
		    gboolean             *prefix_expands_the_file_part_ret,
506
		    GError              **error)
507
{
508
  GtkEditable *editable;
509
  GtkTreeIter iter;
510
  gboolean parsed;
511
  gboolean valid;
512
  char *text_up_to_cursor;
513
  GFile *parsed_folder_file;
514
  char *parsed_file_part;
515

516
  *common_prefix_ret = NULL;
517
  *unique_file_ret = NULL;
518
  *is_complete_not_unique_ret = FALSE;
519
  *prefix_expands_the_file_part_ret = FALSE;
520

521 522 523 524
  editable = GTK_EDITABLE (chooser_entry);

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

525 526 527 528 529 530
  parsed = _gtk_file_system_parse (chooser_entry->file_system,
				   chooser_entry->base_folder,
				   text_up_to_cursor,
				   &parsed_folder_file,
				   &parsed_file_part,
				   error);
531 532 533 534

  g_free (text_up_to_cursor);

  if (!parsed)
535
    return FALSE;
536

537 538 539
  g_assert (parsed_folder_file != NULL
	    && chooser_entry->current_folder != NULL
	    && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
540

541
  g_object_unref (parsed_folder_file);
542 543 544

  /* First pass: find the common prefix */

545
  valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
546 547 548 549

  while (valid)
    {
      gchar *display_name;
550
      GFile *file;
551 552 553 554

      gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
			  &iter,
			  DISPLAY_NAME_COLUMN, &display_name,
555
			  FILE_COLUMN, &file,
556 557
			  -1);

558
      if (g_str_has_prefix (display_name, parsed_file_part))
559
	{
560
	  if (!*common_prefix_ret)
561
	    {
562
	      *common_prefix_ret = trim_dir_separator_suffix (display_name);
563
	      *unique_file_ret = g_object_ref (file);
564 565 566
	    }
	  else
	    {
567
	      gchar *p = *common_prefix_ret;
568
	      const gchar *q = display_name;
569

570 571 572 573 574
	      while (*p && *p == *q)
		{
		  p++;
		  q++;
		}
575

576 577
	      *p = '\0';

578 579 580 581 582
	      if (*unique_file_ret)
		{
		  g_object_unref (*unique_file_ret);
		  *unique_file_ret = NULL;
		}
583 584 585 586
	    }
	}

      g_free (display_name);
587
      g_object_unref (file);
588
      valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
589
    }
590

591 592 593 594 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--;

	  if (strncmp (*common_prefix_ret, display_name, len) == 0)
	    *is_complete_not_unique_ret = TRUE;

	  g_free (display_name);
	  valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
	}
618 619 620 621

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

624
  g_free (parsed_file_part);
625 626

  return TRUE;
627 628
}

629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
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));
  if (cursor_pos < GTK_ENTRY (chooser_entry)->text_length)
    {
      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;
}

652
typedef enum {
653 654 655
  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 */
656
  NOTHING_INSERTED_UNIQUE,	/* what the user typed is already completed, and is a unique match */
657 658 659
  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 */
660 661
} CommonPrefixResult;

662
/* Finds a common prefix based on the contents of the entry and mandatorily appends it */
663
static CommonPrefixResult
664
append_common_prefix (GtkFileChooserEntry *chooser_entry,
665 666
		      gboolean             highlight,
		      gboolean             show_errors)
667 668
{
  gchar *common_prefix;
669
  GFile *unique_file;
670
  gboolean is_complete_not_unique;
671
  gboolean prefix_expands_the_file_part;
672 673 674
  GError *error;
  CommonPrefixResult result;
  gboolean have_result;
675

676 677
  clear_completions (chooser_entry);

678 679 680
  if (chooser_entry->completion_store == NULL)
    return NO_MATCH;

681
  error = NULL;
682
  if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
683
    {
684 685 686 687 688 689
      /* 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)
690 691
	{
	  beep (chooser_entry);
692
	  pop_up_completion_feedback (chooser_entry, _("Invalid path"));
693 694 695 696 697 698 699 700
	}

      g_error_free (error);

      return INVALID_INPUT;
    }

  have_result = FALSE;
701

702
  if (unique_file)
703
    {
704
      if (!char_after_cursor_is_directory_separator (chooser_entry))
705 706
	common_prefix = maybe_append_separator_to_file (chooser_entry,
							unique_file,
707 708
							common_prefix);

709
      g_object_unref (unique_file);
710 711

      if (common_prefix)
712 713 714 715 716 717
	{
	  if (prefix_expands_the_file_part)
	    result = COMPLETED_UNIQUE;
	  else
	    result = NOTHING_INSERTED_UNIQUE;
	}
718 719 720 721
      else
	result = INVALID_INPUT;

      have_result = TRUE;
722
    }
723 724
  else
    {
725 726 727 728 729
      if (is_complete_not_unique)
	{
	  result = COMPLETE_BUT_NOT_UNIQUE;
	  have_result = TRUE;
	}
730
    }
731 732 733

  if (common_prefix)
    {
734
      gint cursor_pos;
735 736 737
      gint common_prefix_len;
      gint pos;

738
      cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
739 740
      common_prefix_len = g_utf8_strlen (common_prefix, -1);

741
      pos = chooser_entry->file_part_pos;
742

743 744 745 746 747 748 749 750 751
      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;
752

753 754 755 756
	  if (highlight)
	    {
	      gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
					  cursor_pos,
757
					  pos); /* equivalent to cursor_pos + common_prefix_len); */
758 759 760 761
	      chooser_entry->has_completion = TRUE;
	    }
	  else
	    gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
762
	}
763
      else if (!have_result)
764 765 766 767
	{
	  result = NOTHING_INSERTED_COMPLETE;
	  have_result = TRUE;
	}
768

769
      g_free (common_prefix);
770 771 772 773

      if (have_result)
	return result;
      else
774
	return COMPLETED;
775 776 777 778 779 780
    }
  else
    {
      if (have_result)
	return result;
      else
781
	return NO_MATCH;
782
    }
783 784
}

785 786 787 788 789
static void
gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
				       const gchar *new_text,
				       gint         new_text_length,
				       gint        *position)
790
{
791 792 793
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
  gint old_text_len;
  gint insert_pos;
794

795 796
  old_text_len = GTK_ENTRY (chooser_entry)->text_length;
  insert_pos = *position;
797

798
  parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
799

800 801
  if (chooser_entry->in_change)
    return;
802

803 804
  remove_completion_feedback (chooser_entry);

805 806
  if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
       || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
807 808
      && insert_pos == old_text_len)
    install_start_autocompletion_idle (chooser_entry);
809 810
}

811 812 813 814 815 816 817 818 819
static void
clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
{
  if (chooser_entry->in_change)
    return;

  clear_completions (chooser_entry);
}

820 821 822 823
static void
gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
				       gint         start_pos,
				       gint         end_pos)
824
{
825 826 827
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);

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

829
  clear_completions_if_not_in_change (chooser_entry);
830 831
}

832
static void
833 834
gtk_file_chooser_entry_set_position (GtkEditable *editable,
				     gint         position)
835
{
836 837 838 839
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);

  parent_editable_iface->set_position (editable, position);

840
  clear_completions_if_not_in_change (chooser_entry);
841 842
}

843
static void
844 845 846
gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
					     gint         start_pos,
					     gint         end_pos)
847
{
848 849
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);

850 851
  parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);

852
  clear_completions_if_not_in_change (chooser_entry);
853 854
}

855 856 857 858 859 860 861
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));
}

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

872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
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;
}

895 896 897
static void
set_invisible_mouse_cursor (GdkWindow *window)
{
898
  GdkDisplay *display;
899 900
  GdkCursor *cursor;

901 902
  display = gdk_drawable_get_display (window);
  cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919

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

920 921 922 923 924
static void
create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
{
  /* Stolen from gtk_tooltip_init() */

925 926
  GtkWidget *alignment;

927 928 929 930 931 932 933
  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");

934 935 936 937 938 939 940 941 942
  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);

943
  g_signal_connect (chooser_entry->completion_feedback_window, "expose-event",
944
		    G_CALLBACK (completion_feedback_window_expose_event_cb), chooser_entry);
945 946 947
  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 */
948 949

  chooser_entry->completion_feedback_label = gtk_label_new (NULL);
950 951
  gtk_container_add (GTK_CONTAINER (alignment), chooser_entry->completion_feedback_label);
  gtk_widget_show (chooser_entry->completion_feedback_label);
952 953
}

954 955 956 957 958 959 960 961 962 963 964 965 966 967
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)
{
968 969
  if (chooser_entry->completion_feedback_timeout_id != 0)
    g_source_remove (chooser_entry->completion_feedback_timeout_id);
970 971 972 973 974 975

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

976
/* Gets the x position of the text cursor in the entry, in widget coordinates */
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
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;
}

1003 1004 1005 1006 1007 1008 1009
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;
1010
  gint cursor_x;
1011
  GtkAllocation *entry_allocation;
1012
  int feedback_x, feedback_y;
1013 1014 1015 1016 1017 1018

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

1019
  get_entry_cursor_x (chooser_entry, &cursor_x);
1020

1021 1022
  /* 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" */
1023 1024
  feedback_y = entry_y + (entry_allocation->height - feedback_req.height) / 2;

1025
  gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1026
  gtk_widget_show (chooser_entry->completion_feedback_window);
1027

1028
  install_completion_feedback_timer (chooser_entry);
1029 1030
}

1031 1032 1033 1034
static void
pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
			    const gchar         *feedback)
{
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
  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)
{
1046 1047 1048
  if (chooser_entry->completion_feedback_window)
    gtk_widget_destroy (chooser_entry->completion_feedback_window);

1049 1050
  chooser_entry->completion_feedback_window = NULL;
  chooser_entry->completion_feedback_label = NULL;
1051

1052 1053 1054 1055 1056
  if (chooser_entry->completion_feedback_timeout_id != 0)
    {
      g_source_remove (chooser_entry->completion_feedback_timeout_id);
      chooser_entry->completion_feedback_timeout_id = 0;
    }
1057 1058
}

1059 1060 1061
static void
explicitly_complete (GtkFileChooserEntry *chooser_entry)
{
1062 1063
  CommonPrefixResult result;

1064
  g_assert (chooser_entry->current_folder != NULL);
1065
  g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1066 1067 1068

  /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
   *
1069
   * - If there is a common prefix, insert it (done)
1070
   * - If there is no common prefix, pop up the suggestion window
1071
   * - If there are no matches at all, beep and bring up a tooltip (done)
1072 1073
   * - If the suggestion window is already up, scroll it
   */
1074 1075 1076 1077
  result = append_common_prefix (chooser_entry, FALSE, TRUE);

  switch (result)
    {
1078 1079
    case INVALID_INPUT:
      /* We already beeped in append_common_prefix(); do nothing here */
1080 1081
      break;

1082
    case NO_MATCH:
1083
      beep (chooser_entry);
Matthias Clasen's avatar
Matthias Clasen committed
1084 1085 1086
      /* translators: this text is shown when there are no completions 
       * for something the user typed in a file chooser entry
       */
1087
      pop_up_completion_feedback (chooser_entry, _("No match"));
1088 1089
      break;

1090 1091 1092 1093
    case NOTHING_INSERTED_COMPLETE:
      /* FIXME: pop up the suggestion window or scroll it */
      break;

1094
    case NOTHING_INSERTED_UNIQUE:
Matthias Clasen's avatar
Matthias Clasen committed
1095 1096 1097
      /* translators: this text is shown when there is exactly one completion 
       * for something the user typed in a file chooser entry
       */
1098 1099 1100
      pop_up_completion_feedback (chooser_entry, _("Sole completion"));
      break;

1101 1102 1103 1104 1105
    case COMPLETED:
      /* Nothing to do */
      break;

    case COMPLETED_UNIQUE:
1106
      /* Nothing to do */
1107 1108
      break;

1109
    case COMPLETE_BUT_NOT_UNIQUE:
Matthias Clasen's avatar
Matthias Clasen committed
1110 1111 1112 1113
      /* 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
       */
1114
      pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1115 1116 1117 1118 1119
      break;

    default:
      g_assert_not_reached ();
    }
1120 1121 1122 1123 1124
}

static void
start_explicit_completion (GtkFileChooserEntry *chooser_entry)
{
1125 1126 1127
  RefreshStatus status;
  gboolean is_error;
  char *feedback_msg;
1128

1129 1130 1131 1132 1133
  status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);

  is_error = FALSE;

  switch (status)
1134
    {
1135 1136
    case REFRESH_OK:
      g_assert (chooser_entry->current_folder_file != NULL);
1137

1138 1139
      if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
	explicitly_complete (chooser_entry);
1140 1141 1142
      else
	{
	  chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1143

1144 1145
	  /* Translators: this text is shown while the system is searching
	   * for possible completions for filenames in a file chooser entry. */
1146 1147
	  pop_up_completion_feedback (chooser_entry, _("Completing..."));
	}
1148

1149
      break;
1150

1151 1152
    case REFRESH_INVALID_INPUT:
      is_error = TRUE;
1153 1154
      /* Translators: this is shown in the feedback for Tab-completion in a file
       * chooser's text entry, when the user enters an invalid path. */
1155 1156 1157 1158 1159 1160 1161
      feedback_msg = _("Invalid path");
      break;

    case REFRESH_INCOMPLETE_HOSTNAME:
      is_error = TRUE;

      if (chooser_entry->local_only)
1162 1163 1164 1165 1166 1167 1168 1169
	{
	  /* 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");
	}
1170 1171 1172
      else
	{
	  /* Another option is to complete the hostname based on the remote volumes that are mounted */
1173 1174 1175 1176

	  /* 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]") */
1177 1178 1179 1180 1181 1182 1183
	  feedback_msg = _("Incomplete hostname; end it with '/'");
	}

      break;

    case REFRESH_NONEXISTENT:
      is_error = TRUE;
1184 1185 1186 1187

      /* 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 */
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
      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 ();
1198 1199 1200
      return;
    }

1201
  if (is_error)
1202
    {
1203
      g_assert (chooser_entry->current_folder_file == NULL);
1204

1205 1206 1207
      beep (chooser_entry);
      pop_up_completion_feedback (chooser_entry, feedback_msg);
      chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1208 1209 1210
    }
}

1211 1212 1213 1214
static gboolean
gtk_file_chooser_entry_focus (GtkWidget        *widget,
			      GtkDirectionType  direction)
{
1215 1216 1217
  GtkFileChooserEntry *chooser_entry;
  GtkEditable *editable;
  GtkEntry *entry;
1218
  GdkModifierType state;
1219 1220 1221 1222 1223
  gboolean control_pressed;

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

1225
  if (!chooser_entry->eat_tabs)
Matthias Clasen's avatar
Matthias Clasen committed
1226
    return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
1227

1228 1229
  control_pressed = FALSE;

1230
  if (gtk_get_current_event_state (&state))
1231
    {
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
      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) &&
      (GTK_WIDGET_HAS_FOCUS (widget)) &&
      (! control_pressed))
    {
1242
      if (chooser_entry->has_completion)
1243
	gtk_editable_set_position (editable, entry->text_length);
1244
      else
1245
	start_explicit_completion (chooser_entry);
1246

1247 1248 1249
      return TRUE;
    }
  else
Matthias Clasen's avatar
Matthias Clasen committed
1250
    return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
1251 1252
}

1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
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);
}

1264
static void
1265
commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1266 1267 1268
{
  if (chooser_entry->has_completion)
    {
1269 1270
      gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
				 GTK_ENTRY (chooser_entry)->text_length);
1271
    }
1272

1273
  /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1274
  refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1275 1276 1277 1278 1279 1280
}

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

1282
  commit_completion_and_refresh (chooser_entry);
Matthias Clasen's avatar
Matthias Clasen committed
1283
  GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1284 1285
}

1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
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;
}

1297 1298 1299 1300
/* Fills the completion store from the contents of the current folder */
static void
populate_completion_store (GtkFileChooserEntry *chooser_entry)
{
1301
  GSList *files;
1302 1303
  GSList *tmp_list;

1304
  discard_completion_store (chooser_entry);
1305

1306
  files = _gtk_folder_list_children (chooser_entry->current_folder);
1307

1308 1309
  chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
							G_TYPE_STRING,
1310
							G_TYPE_FILE);
1311

1312
  for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1313
    {
1314 1315 1316 1317
      GFileInfo *info;
      GFile *file;

      file = tmp_list->data;
1318

1319
      info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1320 1321 1322

      if (info)
	{
1323
	  gchar *display_name = g_strdup (g_file_info_get_display_name (info));
1324 1325
	  GtkTreeIter iter;

1326
          display_name = maybe_append_separator_to_file (chooser_entry, file, display_name);
1327 1328 1329 1330

	  gtk_list_store_append (chooser_entry->completion_store, &iter);
	  gtk_list_store_set (chooser_entry->completion_store, &iter,
			      DISPLAY_NAME_COLUMN, display_name,
1331
			      FILE_COLUMN, file,
1332 1333
			      -1);

1334
	  g_object_unref (info);
1335 1336 1337 1338
          g_free (display_name);
	}
    }

1339 1340
  g_slist_foreach (files, (GFunc) g_object_unref, NULL);
  g_slist_free (files);
1341 1342 1343