-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_get_text_length (GTK_ENTRY (chooser_entry)))
- {
- 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;
-}
-
-typedef enum {
- 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 */
- NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
- 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 */
-} CommonPrefixResult;
-
-/* Finds a common prefix based on the contents of the entry and mandatorily appends it */
-static CommonPrefixResult
-append_common_prefix (GtkFileChooserEntry *chooser_entry,
- gboolean highlight,
- gboolean show_errors)
-{
- gchar *common_prefix;
- GFile *unique_file;
- gboolean is_complete_not_unique;
- gboolean prefix_expands_the_file_part;
- GError *error;
- CommonPrefixResult result = NO_MATCH;
- gboolean have_result;
-
- clear_completions (chooser_entry);
-
- if (chooser_entry->completion_store == NULL)
- return NO_MATCH;
-
- error = NULL;
- if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
- {
- /* 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)
- {
- beep (chooser_entry);
- pop_up_completion_feedback (chooser_entry, _("Invalid path"));
- }
-
- g_error_free (error);
-
- return INVALID_INPUT;
- }
-
- have_result = FALSE;
-
- if (unique_file)
- {
- if (!char_after_cursor_is_directory_separator (chooser_entry))
- {
- gboolean appended;
-
- common_prefix = maybe_append_separator_to_file (chooser_entry,
- unique_file,
- common_prefix,
- &appended);
- if (appended)
- prefix_expands_the_file_part = TRUE;
- }
-
- g_object_unref (unique_file);
-
- if (prefix_expands_the_file_part)
- result = COMPLETED_UNIQUE;
- else
- result = NOTHING_INSERTED_UNIQUE;
-
- have_result = TRUE;
- }
- else
- {
- if (is_complete_not_unique)
- {
- result = COMPLETE_BUT_NOT_UNIQUE;
- have_result = TRUE;
- }
- }
-
- if (common_prefix)
- {
- gint cursor_pos;
- gint common_prefix_len;
- gint pos;
-
- cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
- common_prefix_len = g_utf8_strlen (common_prefix, -1);
-
- pos = chooser_entry->file_part_pos;
-
- 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;
-
- if (highlight)
- {
- gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
- cursor_pos,
- pos); /* equivalent to cursor_pos + common_prefix_len); */
- chooser_entry->has_completion = TRUE;
- }
- else
- gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
- }
- else if (!have_result)
- {
- result = NOTHING_INSERTED_COMPLETE;
- have_result = TRUE;
- }
-
- g_free (common_prefix);
-
- if (have_result)
- return result;
- else
- return COMPLETED;
- }
- else
- {
- if (have_result)
- return result;
- else
- return NO_MATCH;
- }
-}
-
-static void
-gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
- const gchar *new_text,
- gint new_text_length,
- gint *position)
-{
- GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
- gint old_text_len;
- gint insert_pos;
-
- old_text_len = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
- insert_pos = *position;
-
- parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
-
- if (chooser_entry->in_change)
- return;
-
- remove_completion_feedback (chooser_entry);
-
- if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
- || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
- && insert_pos == old_text_len)
- install_start_autocompletion_idle (chooser_entry);
-}
-
-static void
-clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
-{
- if (chooser_entry->in_change)
- return;
-
- clear_completions (chooser_entry);
-}
-
-static void
-gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
- gint start_pos,
- gint end_pos)
-{
- GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
-
- parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
-
- clear_completions_if_not_in_change (chooser_entry);
-}
-
-static void
-gtk_file_chooser_entry_set_position (GtkEditable *editable,
- gint position)
-{
- GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
-
- parent_editable_iface->set_position (editable, position);
-
- clear_completions_if_not_in_change (chooser_entry);
-}
-
-static void
-gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
- gint start_pos,
- gint end_pos)
-{
- GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
-
- parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
-
- clear_completions_if_not_in_change (chooser_entry);
-}
-
-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));
-}
-
-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);
-}
-
-static gboolean
-completion_feedback_window_draw_cb (GtkWidget *widget,
- cairo_t *cr,
- gpointer data)
-{
- /* Stolen from gtk_tooltip_paint_window() */
-
- GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
-
- gtk_paint_flat_box (gtk_widget_get_style (chooser_entry->completion_feedback_window),
- cr,
- GTK_STATE_NORMAL,
- GTK_SHADOW_OUT,
- chooser_entry->completion_feedback_window,
- "tooltip",
- 0, 0,
- gtk_widget_get_allocated_width (widget),
- gtk_widget_get_allocated_height (widget));
-
- return FALSE;
-}
-