]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkfilechooserentry.c
Fix the file chooser on Windows. I can't make it misbehave or crash any
[~andy/gtk] / gtk / gtkfilechooserentry.c
index 087005b36cf3cbd01240dc4f9faf7a9ef4d7e54e..6bc9d8a9a37e64c6bec3015fb5a5800379d176c4 100644 (file)
@@ -46,6 +46,7 @@ struct _GtkFileChooserEntry
   GtkFilePath *base_folder;
   GtkFilePath *current_folder_path;
   gchar *file_part;
+  gint file_part_pos;
   GSource *check_completion_idle;
   GSource *load_directory_idle;
 
@@ -55,7 +56,6 @@ struct _GtkFileChooserEntry
 
   guint has_completion : 1;
   guint in_change      : 1;
-  guint no_pop_down    : 1;
 };
 
 enum
@@ -95,11 +95,10 @@ static void     files_added_cb            (GtkFileSystem       *file_system,
 static void     files_deleted_cb          (GtkFileSystem       *file_system,
                                           GSList              *deleted_uris,
                                           GtkFileChooserEntry *chooser_entry);
-static char    *maybe_append_seperator_to_path (GtkFileChooserEntry *chooser_entry,
+static char    *maybe_append_separator_to_path (GtkFileChooserEntry *chooser_entry,
                                                GtkFilePath         *path,
                                                gchar               *display_name);
 
-
 static GObjectClass *parent_class;
 static GtkEditableClass *parent_editable_iface;
 
@@ -233,12 +232,8 @@ match_selected_callback (GtkEntryCompletion  *completion,
                         GtkFileChooserEntry *chooser_entry)
 {
   char *display_name;
-  gint total_len;
-  gint file_part_len;
-  gint selected_part_len;
   GtkFilePath *path;
   gint pos;
-  const gchar *text;
   
   gtk_tree_model_get (model, iter,
                      DISPLAY_NAME_COLUMN, &display_name,
@@ -253,31 +248,9 @@ match_selected_callback (GtkEntryCompletion  *completion,
       return FALSE;
     }
 
-  display_name = maybe_append_seperator_to_path (chooser_entry, path, display_name);
-
-  /* We have to jump through hoops to figure out where to append this to */
-  text = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
-  total_len = g_utf8_strlen (text, -1);
-  file_part_len = g_utf8_strlen (chooser_entry->file_part, -1);
-
-  selected_part_len = 0;
+  display_name = maybe_append_separator_to_path (chooser_entry, path, display_name);
 
-  if (chooser_entry->has_completion)
-    {
-      gint sel_start, sel_end;
-
-      /* Gosh, there should be a more efficient way of doing this... */
-      if (gtk_editable_get_selection_bounds (GTK_EDITABLE (chooser_entry),
-                                            &sel_start, &sel_end))
-       {
-         gchar *str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry),
-                                              sel_start, sel_end);
-         selected_part_len = g_utf8_strlen (str, -1);
-         g_free (str);
-       }
-    }
-
-  pos = total_len - (file_part_len + selected_part_len); 
+  pos = chooser_entry->file_part_pos;
 
   /* We don't set in_change here as we want to update the current_folder
    * variable */
@@ -291,10 +264,6 @@ match_selected_callback (GtkEntryCompletion  *completion,
   gtk_file_path_free (path);
   g_free (display_name);
 
-  /* We surpress the completion for a second.  It's confusing to click on an
-   * entry and have it pop back up immediately */
-  chooser_entry->no_pop_down = TRUE;
-
   return TRUE;
 }
 
@@ -321,11 +290,7 @@ completion_match_func (GtkEntryCompletion *comp,
       return FALSE;
     }
 
-  /* If this is set, than someone is surpressing the popdown temporarily */
-  if (chooser_entry->no_pop_down)
-    return FALSE;
-
-  gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, 0, &name, -1);
+  gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
   if (!name)
     {
       return FALSE; /* Uninitialized row, ugh */
@@ -351,6 +316,20 @@ completion_match_func (GtkEntryCompletion *comp,
   norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
   norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
 
+#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
+
   result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
 
   g_free (norm_file_part);
@@ -360,17 +339,18 @@ completion_match_func (GtkEntryCompletion *comp,
   return result;
 }
 
-/* This function will append a '/' character to paths to display_name iff the
- * path associated with it is a directory.  maybe_append_seperator_to_path will
- * g_free the display_name and return a new one if needed.  Otherwise, it will
- * return the old one.  You should be safe calling
+/* This function will append a directory separator to paths to
+ * display_name iff the path associated with it is a directory.
+ * maybe_append_separator_to_path will g_free the display_name and
+ * return a new one if needed.  Otherwise, it will return the old one.
+ * You should be safe calling
  *
- * display_name = maybe_append_seperator_to_path (entry, path, display_name);
+ * display_name = maybe_append_separator_to_path (entry, path, display_name);
  * ...
  * g_free (display_name);
  */
 static char *
-maybe_append_seperator_to_path (GtkFileChooserEntry *chooser_entry,
+maybe_append_separator_to_path (GtkFileChooserEntry *chooser_entry,
                                GtkFilePath         *path,
                                gchar               *display_name)
 {
@@ -386,7 +366,7 @@ maybe_append_seperator_to_path (GtkFileChooserEntry *chooser_entry,
          if (gtk_file_info_get_is_folder (info))
            {
              gchar *tmp = display_name;
-             display_name = g_strconcat (tmp, "/", NULL);
+             display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
              g_free (tmp);
            }
          
@@ -456,13 +436,14 @@ check_completion_callback (GtkFileChooserEntry *chooser_entry)
        }
 
       g_free (display_name);
+      gtk_file_path_free (path);
       valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store),
                                        &iter);
     }
 
   if (unique_path)
     {
-      common_prefix = maybe_append_seperator_to_path (chooser_entry,
+      common_prefix = maybe_append_separator_to_path (chooser_entry,
                                                      unique_path,
                                                      common_prefix);
       gtk_file_path_free (unique_path);
@@ -470,18 +451,16 @@ check_completion_callback (GtkFileChooserEntry *chooser_entry)
 
   if (common_prefix)
     {
-      gint total_len;
       gint file_part_len;
       gint common_prefix_len;
       gint pos;
 
-      total_len = g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry)), -1);
       file_part_len = g_utf8_strlen (chooser_entry->file_part, -1);
       common_prefix_len = g_utf8_strlen (common_prefix, -1);
 
       if (common_prefix_len > file_part_len)
        {
-         pos = total_len - file_part_len;
+         pos = chooser_entry->file_part_pos;
 
          chooser_entry->in_change = TRUE;
          gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
@@ -490,8 +469,8 @@ check_completion_callback (GtkFileChooserEntry *chooser_entry)
                                    common_prefix, -1, 
                                    &pos);
          gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
-                                     total_len,
-                                     total_len - file_part_len + common_prefix_len);
+                                     chooser_entry->file_part_pos + file_part_len,
+                                     chooser_entry->file_part_pos + common_prefix_len);
          chooser_entry->in_change = FALSE;
 
          chooser_entry->has_completion = TRUE;
@@ -609,19 +588,22 @@ load_directory_callback (GtkFileChooserEntry *chooser_entry)
   g_signal_connect (chooser_entry->current_folder, "files-removed",
                    G_CALLBACK (files_deleted_cb), chooser_entry);
   
-  gtk_file_folder_list_children (chooser_entry->current_folder,
-                                &child_paths,
-                                NULL); /* NULL-GError */
   chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
                                                        G_TYPE_STRING,
                                                        GTK_TYPE_FILE_PATH);
 
-  if (child_paths)
+  if (chooser_entry->file_part_pos != -1)
     {
-      update_current_folder_files (chooser_entry, child_paths);
-      add_completion_idle (chooser_entry);
-      gtk_file_paths_free (child_paths);
-  }
+      gtk_file_folder_list_children (chooser_entry->current_folder,
+                                    &child_paths,
+                                    NULL); /* NULL-GError */
+      if (child_paths)
+       {
+         update_current_folder_files (chooser_entry, child_paths);
+         add_completion_idle (chooser_entry);
+         gtk_file_paths_free (child_paths);
+       }
+    }
 
   gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
                                  GTK_TREE_MODEL (chooser_entry->completion_store));
@@ -692,13 +674,14 @@ gtk_file_chooser_entry_activate (GtkEntry *entry)
  */
 static void
 gtk_file_chooser_entry_maybe_update_directory (GtkFileChooserEntry *chooser_entry,
-                                              GtkFilePath         *folder_path)
+                                              GtkFilePath         *folder_path,
+                                              gboolean             force_reload)
 {
   gboolean queue_idle = FALSE;
 
   if (chooser_entry->current_folder_path)
     {
-      if (gtk_file_path_compare (folder_path, chooser_entry->current_folder_path) != 0)
+      if (gtk_file_path_compare (folder_path, chooser_entry->current_folder_path) != 0 || force_reload)
        {
          /* We changed our current directory.  We need to clear out the old
           * directory information.
@@ -755,6 +738,8 @@ gtk_file_chooser_entry_changed (GtkEditable *editable)
   const gchar *text;
   GtkFilePath *folder_path;
   gchar *file_part;
+  gsize total_len, file_part_len;
+  gint file_part_pos;
 
   if (chooser_entry->in_change)
     return;
@@ -769,14 +754,25 @@ gtk_file_chooser_entry_changed (GtkEditable *editable)
     {
       folder_path = gtk_file_path_copy (chooser_entry->base_folder);
       file_part = g_strdup ("");
+      file_part_pos = -1;
+    }
+  else
+    {
+      file_part_len = strlen (file_part);
+      total_len = strlen (text);
+      if (total_len > file_part_len)
+       file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
+      else
+       file_part_pos = 0;
     }
-
-  gtk_file_chooser_entry_maybe_update_directory (chooser_entry, folder_path);
 
   if (chooser_entry->file_part)
     g_free (chooser_entry->file_part);
 
   chooser_entry->file_part = file_part;
+  chooser_entry->file_part_pos = file_part_pos;
+
+  gtk_file_chooser_entry_maybe_update_directory (chooser_entry, folder_path, file_part_pos == -1);
 }
 
 static void
@@ -788,7 +784,6 @@ clear_completion_callback (GtkFileChooserEntry *chooser_entry,
       chooser_entry->has_completion = FALSE;
       gtk_file_chooser_entry_changed (GTK_EDITABLE (chooser_entry));
     }
-  chooser_entry->no_pop_down = FALSE;
 }
 
 /**