1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #include <sys/types.h>
32 #ifdef HAVE_SYS_PARAM_H
33 #include <sys/param.h>
45 #include <glib.h> /* Include early to get G_OS_WIN32 etc */
46 #include <glib/gstdio.h>
48 #if defined(G_PLATFORM_WIN32)
53 #endif /* G_PLATFORM_WIN32 */
55 #include "gdk/gdkkeysyms.h"
57 #undef GTK_DISABLE_DEPRECATED /* GtkOptionMenu */
59 #include "gtkbutton.h"
60 #include "gtkcellrenderertext.h"
62 #include "gtkfilesel.h"
67 #include "gtkliststore.h"
69 #include "gtkprivate.h"
70 #include "gtkscrolledwindow.h"
72 #include "gtktreeselection.h"
73 #include "gtktreeview.h"
76 #include "gtkmenuitem.h"
77 #include "gtkdialog.h"
78 #include "gtkmessagedialog.h"
80 #include "gtkeventbox.h"
81 #include "gtkoptionmenu.h"
84 #include "gtkhpaned.h"
92 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
94 #endif /* G_OS_WIN32 */
97 #include <sys/cygwin.h> /* For cygwin_conv_to_posix_path */
100 #define DIR_LIST_WIDTH 180
101 #define DIR_LIST_HEIGHT 180
102 #define FILE_LIST_WIDTH 180
103 #define FILE_LIST_HEIGHT 180
105 /* The Hurd doesn't define either PATH_MAX or MAXPATHLEN, so we put this
106 * in here, since the rest of the code in the file does require some
111 # define MAXPATHLEN PATH_MAX
113 # define MAXPATHLEN 2048
117 /* I've put this here so it doesn't get confused with the
118 * file completion interface */
119 typedef struct _HistoryCallbackArg HistoryCallbackArg;
121 struct _HistoryCallbackArg
124 GtkWidget *menu_item;
128 typedef struct _CompletionState CompletionState;
129 typedef struct _CompletionDir CompletionDir;
130 typedef struct _CompletionDirSent CompletionDirSent;
131 typedef struct _CompletionDirEntry CompletionDirEntry;
132 typedef struct _CompletionUserDir CompletionUserDir;
133 typedef struct _PossibleCompletion PossibleCompletion;
135 /* Non-external file completion decls and structures */
137 /* A contant telling PRCS how many directories to cache. Its actually
138 * kept in a list, so the geometry isn't important. */
139 #define CMPL_DIRECTORY_CACHE_SIZE 10
141 /* A constant used to determine whether a substring was an exact
142 * match by first_diff_index()
144 #define PATTERN_MATCH -1
145 #define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
146 #define CMPL_ERRNO_DID_NOT_CONVERT ((1<<16)-2)
148 /* This structure contains all the useful information about a directory
149 * for the purposes of filename completion. These structures are cached
150 * in the CompletionState struct. CompletionDir's are reference counted.
152 struct _CompletionDirSent
154 #ifndef G_PLATFORM_WIN32
161 struct _CompletionDirEntry *entries;
164 struct _CompletionDir
166 CompletionDirSent *sent;
171 struct _CompletionDir *cmpl_parent;
176 /* This structure contains pairs of directory entry names with a flag saying
177 * whether or not they are a valid directory. NOTE: This information is used
178 * to provide the caller with information about whether to update its completions
179 * or try to open a file. Since directories are cached by the directory mtime,
180 * a symlink which points to an invalid file (which will not be a directory),
181 * will not be reevaluated if that file is created, unless the containing
182 * directory is touched. I consider this case to be worth ignoring (josh).
184 struct _CompletionDirEntry
191 struct _CompletionUserDir
197 struct _PossibleCompletion
199 /* accessible fields, all are accessed externally by functions
203 gint is_a_completion;
204 gboolean is_directory;
211 struct _CompletionState
213 gint last_valid_char;
215 gint updated_text_len;
216 gint updated_text_alloc;
217 gboolean re_complete;
219 gchar *user_dir_name_buffer;
220 gint user_directories_len;
222 gchar *last_completion_text;
224 gint user_completion_index; /* if >= 0, currently completing ~user */
226 struct _CompletionDir *completion_dir; /* directory completing from */
227 struct _CompletionDir *active_completion_dir;
229 struct _PossibleCompletion the_completion;
231 struct _CompletionDir *reference_dir; /* initial directory */
233 GList* directory_storage;
234 GList* directory_sent_storage;
236 struct _CompletionUserDir *user_directories;
254 /* File completion functions which would be external, were they used
255 * outside of this file.
258 static CompletionState* cmpl_init_state (void);
259 static void cmpl_free_state (CompletionState *cmpl_state);
260 static gint cmpl_state_okay (CompletionState* cmpl_state);
261 static const gchar* cmpl_strerror (gint);
263 static PossibleCompletion* cmpl_completion_matches(gchar *text_to_complete,
264 gchar **remaining_text,
265 CompletionState *cmpl_state);
267 /* Returns a name for consideration, possibly a completion, this name
268 * will be invalid after the next call to cmpl_next_completion.
270 static char* cmpl_this_completion (PossibleCompletion*);
272 /* True if this completion matches the given text. Otherwise, this
273 * output can be used to have a list of non-completions.
275 static gint cmpl_is_a_completion (PossibleCompletion*);
277 /* True if the completion is a directory
279 static gboolean cmpl_is_directory (PossibleCompletion*);
281 /* Obtains the next completion, or NULL
283 static PossibleCompletion* cmpl_next_completion (CompletionState*);
285 /* Updating completions: the return value of cmpl_updated_text() will
286 * be text_to_complete completed as much as possible after the most
287 * recent call to cmpl_completion_matches. For the present
288 * application, this is the suggested replacement for the user's input
289 * string. You must CALL THIS AFTER ALL cmpl_text_completions have
292 static gchar* cmpl_updated_text (CompletionState* cmpl_state);
294 /* After updating, to see if the completion was a directory, call
295 * this. If it was, you should consider re-calling completion_matches.
297 static gboolean cmpl_updated_dir (CompletionState* cmpl_state);
299 /* Current location: if using file completion, return the current
300 * directory, from which file completion begins. More specifically,
301 * the cwd concatenated with all exact completions up to the last
302 * directory delimiter('/').
304 static gchar* cmpl_reference_position (CompletionState* cmpl_state);
307 /* This doesn't work currently and would require changes
308 * to fnmatch.c to get working.
310 /* backing up: if cmpl_completion_matches returns NULL, you may query
311 * the index of the last completable character into cmpl_updated_text.
313 static gint cmpl_last_valid_char (CompletionState* cmpl_state);
316 /* When the user selects a non-directory, call cmpl_completion_fullname
317 * to get the full name of the selected file.
319 static gchar* cmpl_completion_fullname (const gchar*, CompletionState* cmpl_state);
322 /* Directory operations. */
323 static CompletionDir* open_ref_dir (gchar* text_to_complete,
324 gchar** remaining_text,
325 CompletionState* cmpl_state);
326 #ifndef G_PLATFORM_WIN32
327 static gboolean check_dir (gchar *dir_name,
329 gboolean *stat_subdirs);
331 static CompletionDir* open_dir (gchar* dir_name,
332 CompletionState* cmpl_state);
334 static CompletionDir* open_user_dir (const gchar* text_to_complete,
335 CompletionState *cmpl_state);
337 static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
338 CompletionState *cmpl_state);
339 static CompletionDirSent* open_new_dir (gchar* dir_name,
341 gboolean stat_subdirs);
342 static gint correct_dir_fullname (CompletionDir* cmpl_dir);
343 static gint correct_parent (CompletionDir* cmpl_dir,
345 #ifndef G_PLATFORM_WIN32
346 static gchar* find_parent_dir_fullname (gchar* dirname);
348 static CompletionDir* attach_dir (CompletionDirSent* sent,
350 CompletionState *cmpl_state);
351 static void free_dir_sent (CompletionDirSent* sent);
352 static void free_dir (CompletionDir *dir);
353 static void prune_memory_usage(CompletionState *cmpl_state);
355 /* Completion operations */
357 static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
358 CompletionState *cmpl_state);
360 static PossibleCompletion* attempt_file_completion(CompletionState *cmpl_state);
361 static CompletionDir* find_completion_dir(gchar* text_to_complete,
362 gchar** remaining_text,
363 CompletionState* cmpl_state);
364 static PossibleCompletion* append_completion_text(gchar* text,
365 CompletionState* cmpl_state);
367 static gint get_pwdb(CompletionState* cmpl_state);
368 static gint compare_user_dir(const void* a, const void* b);
370 static gint first_diff_index(gchar* pat, gchar* text);
371 static gint compare_cmpl_dir(const void* a, const void* b);
372 static void update_cmpl(PossibleCompletion* poss,
373 CompletionState* cmpl_state);
375 static void gtk_file_selection_set_property (GObject *object,
379 static void gtk_file_selection_get_property (GObject *object,
383 static void gtk_file_selection_finalize (GObject *object);
384 static void gtk_file_selection_destroy (GtkObject *object);
385 static void gtk_file_selection_map (GtkWidget *widget);
386 static gint gtk_file_selection_key_press (GtkWidget *widget,
389 static gint gtk_file_selection_insert_text (GtkWidget *widget,
390 const gchar *new_text,
391 gint new_text_length,
394 static void gtk_file_selection_update_fileops (GtkFileSelection *filesel);
396 static void gtk_file_selection_file_activate (GtkTreeView *tree_view,
398 GtkTreeViewColumn *column,
400 static void gtk_file_selection_file_changed (GtkTreeSelection *selection,
402 static void gtk_file_selection_dir_activate (GtkTreeView *tree_view,
404 GtkTreeViewColumn *column,
407 static void gtk_file_selection_populate (GtkFileSelection *fs,
409 gboolean try_complete,
410 gboolean reset_entry);
411 static void gtk_file_selection_abort (GtkFileSelection *fs);
413 static void gtk_file_selection_update_history_menu (GtkFileSelection *fs,
416 static void gtk_file_selection_create_dir (GtkWidget *widget, gpointer data);
417 static void gtk_file_selection_delete_file (GtkWidget *widget, gpointer data);
418 static void gtk_file_selection_rename_file (GtkWidget *widget, gpointer data);
420 static void free_selected_names (GPtrArray *names);
422 #ifndef G_PLATFORM_WIN32
424 #define compare_utf8_filenames(a, b) strcmp(a, b)
425 #define compare_sys_filenames(a, b) strcmp(a, b)
430 compare_utf8_filenames (const gchar *a,
433 gchar *a_folded, *b_folded;
436 a_folded = g_utf8_strdown (a, -1);
437 b_folded = g_utf8_strdown (b, -1);
439 retval = strcmp (a_folded, b_folded);
448 compare_sys_filenames (const gchar *a,
451 gchar *a_utf8, *b_utf8;
454 a_utf8 = g_filename_to_utf8 (a, -1, NULL, NULL, NULL);
455 b_utf8 = g_filename_to_utf8 (b, -1, NULL, NULL, NULL);
457 retval = compare_utf8_filenames (a_utf8, b_utf8);
467 /* Saves errno when something cmpl does fails. */
468 static gint cmpl_errno;
472 * Take the path currently in the file selection
473 * entry field and translate as necessary from
474 * a Win32 style to Cygwin style path. For
475 * instance translate:
476 * x:\somepath\file.jpg
478 * /cygdrive/x/somepath/file.jpg
480 * Replace the path in the selection text field.
481 * Return a boolean value concerning whether a
482 * translation had to be made.
485 translate_win32_path (GtkFileSelection *filesel)
489 gchar newPath[MAX_PATH];
492 * Retrieve the current path
494 path = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
496 cygwin_conv_to_posix_path (path, newPath);
497 updated = (strcmp (path, newPath) != 0);
500 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), newPath);
506 G_DEFINE_TYPE (GtkFileSelection, gtk_file_selection, GTK_TYPE_DIALOG)
509 gtk_file_selection_class_init (GtkFileSelectionClass *class)
511 GObjectClass *gobject_class;
512 GtkObjectClass *object_class;
513 GtkWidgetClass *widget_class;
515 gobject_class = (GObjectClass*) class;
516 object_class = (GtkObjectClass*) class;
517 widget_class = (GtkWidgetClass*) class;
519 gobject_class->finalize = gtk_file_selection_finalize;
520 gobject_class->set_property = gtk_file_selection_set_property;
521 gobject_class->get_property = gtk_file_selection_get_property;
523 g_object_class_install_property (gobject_class,
525 g_param_spec_string ("filename",
527 P_("The currently selected filename"),
529 GTK_PARAM_READWRITE));
530 g_object_class_install_property (gobject_class,
532 g_param_spec_boolean ("show-fileops",
533 P_("Show file operations"),
534 P_("Whether buttons for creating/manipulating files should be displayed"),
536 GTK_PARAM_READWRITE));
537 g_object_class_install_property (gobject_class,
538 PROP_SELECT_MULTIPLE,
539 g_param_spec_boolean ("select-multiple",
540 P_("Select Multiple"),
541 P_("Whether to allow multiple files to be selected"),
543 GTK_PARAM_READWRITE));
544 object_class->destroy = gtk_file_selection_destroy;
545 widget_class->map = gtk_file_selection_map;
548 static void gtk_file_selection_set_property (GObject *object,
553 GtkFileSelection *filesel;
555 filesel = GTK_FILE_SELECTION (object);
560 gtk_file_selection_set_filename (filesel,
561 g_value_get_string (value));
563 case PROP_SHOW_FILEOPS:
564 if (g_value_get_boolean (value))
565 gtk_file_selection_show_fileop_buttons (filesel);
567 gtk_file_selection_hide_fileop_buttons (filesel);
569 case PROP_SELECT_MULTIPLE:
570 gtk_file_selection_set_select_multiple (filesel, g_value_get_boolean (value));
573 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
578 static void gtk_file_selection_get_property (GObject *object,
583 GtkFileSelection *filesel;
585 filesel = GTK_FILE_SELECTION (object);
590 g_value_set_string (value,
591 gtk_file_selection_get_filename(filesel));
594 case PROP_SHOW_FILEOPS:
595 /* This is a little bit hacky, but doing otherwise would require
596 * adding a field to the object.
598 g_value_set_boolean (value, (filesel->fileop_c_dir &&
599 filesel->fileop_del_file &&
600 filesel->fileop_ren_file));
602 case PROP_SELECT_MULTIPLE:
603 g_value_set_boolean (value, gtk_file_selection_get_select_multiple (filesel));
606 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
612 grab_default (GtkWidget *widget)
614 gtk_widget_grab_default (widget);
619 gtk_file_selection_init (GtkFileSelection *filesel)
621 GtkWidget *entry_vbox;
623 GtkWidget *list_hbox, *list_container;
624 GtkWidget *pulldown_hbox;
625 GtkWidget *scrolled_win;
631 GtkTreeViewColumn *column;
633 gtk_widget_push_composite_child ();
635 dialog = GTK_DIALOG (filesel);
637 filesel->cmpl_state = cmpl_init_state ();
639 /* The dialog-sized vertical box */
640 filesel->main_vbox = dialog->vbox;
641 gtk_container_set_border_width (GTK_CONTAINER (filesel), 10);
643 /* The horizontal box containing create, rename etc. buttons */
644 filesel->button_area = gtk_hbutton_box_new ();
645 gtk_button_box_set_layout (GTK_BUTTON_BOX (filesel->button_area), GTK_BUTTONBOX_START);
646 gtk_box_set_spacing (GTK_BOX (filesel->button_area), 0);
647 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area,
649 gtk_widget_show (filesel->button_area);
651 gtk_file_selection_show_fileop_buttons (filesel);
653 /* hbox for pulldown menu */
654 pulldown_hbox = gtk_hbox_new (TRUE, 5);
655 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
656 gtk_widget_show (pulldown_hbox);
659 filesel->history_pulldown = gtk_option_menu_new ();
660 gtk_widget_show (filesel->history_pulldown);
661 gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
664 /* The horizontal box containing the directory and file listboxes */
666 spacer = gtk_hbox_new (FALSE, 0);
667 gtk_widget_set_size_request (spacer, -1, 5);
668 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), spacer, FALSE, FALSE, 0);
669 gtk_widget_show (spacer);
671 list_hbox = gtk_hbox_new (FALSE, 5);
672 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
673 gtk_widget_show (list_hbox);
675 list_container = g_object_new (GTK_TYPE_HPANED,
681 list_container = list_hbox;
683 spacer = gtk_hbox_new (FALSE, 0);
684 gtk_widget_set_size_request (spacer, -1, 5);
685 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), spacer, FALSE, FALSE, 0);
686 gtk_widget_show (spacer);
688 /* The directories list */
690 model = gtk_list_store_new (1, G_TYPE_STRING);
691 filesel->dir_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
692 g_object_unref (model);
694 column = gtk_tree_view_column_new_with_attributes (_("Folders"),
695 gtk_cell_renderer_text_new (),
698 label = gtk_label_new_with_mnemonic (_("Fol_ders"));
699 gtk_label_set_mnemonic_widget (GTK_LABEL (label), filesel->dir_list);
700 gtk_widget_show (label);
701 gtk_tree_view_column_set_widget (column, label);
702 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
703 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel->dir_list), column);
705 gtk_widget_set_size_request (filesel->dir_list,
706 DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
707 g_signal_connect (filesel->dir_list, "row_activated",
708 G_CALLBACK (gtk_file_selection_dir_activate), filesel);
710 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list)); */
712 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
713 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
714 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->dir_list);
715 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
716 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
717 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 0);
718 if (GTK_IS_PANED (list_container))
719 gtk_paned_pack1 (GTK_PANED (list_container), scrolled_win, TRUE, TRUE);
721 gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
722 gtk_widget_show (filesel->dir_list);
723 gtk_widget_show (scrolled_win);
726 model = gtk_list_store_new (1, G_TYPE_STRING);
727 filesel->file_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
728 g_object_unref (model);
730 column = gtk_tree_view_column_new_with_attributes (_("Files"),
731 gtk_cell_renderer_text_new (),
734 label = gtk_label_new_with_mnemonic (_("_Files"));
735 gtk_label_set_mnemonic_widget (GTK_LABEL (label), filesel->file_list);
736 gtk_widget_show (label);
737 gtk_tree_view_column_set_widget (column, label);
738 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
739 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel->file_list), column);
741 gtk_widget_set_size_request (filesel->file_list,
742 FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
743 g_signal_connect (filesel->file_list, "row_activated",
744 G_CALLBACK (gtk_file_selection_file_activate), filesel);
745 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list)), "changed",
746 G_CALLBACK (gtk_file_selection_file_changed), filesel);
748 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list)); */
750 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
751 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
752 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->file_list);
753 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
754 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
755 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 0);
756 gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
757 gtk_widget_show (filesel->file_list);
758 gtk_widget_show (scrolled_win);
760 /* action area for packing buttons into. */
761 filesel->action_area = gtk_hbox_new (TRUE, 0);
762 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
764 gtk_widget_show (filesel->action_area);
766 /* The OK/Cancel button area */
768 /* The Cancel button */
769 filesel->cancel_button = gtk_dialog_add_button (dialog,
771 GTK_RESPONSE_CANCEL);
773 filesel->ok_button = gtk_dialog_add_button (dialog,
777 gtk_dialog_set_alternative_button_order (dialog,
782 gtk_widget_grab_default (filesel->ok_button);
784 /* The selection entry widget */
785 entry_vbox = gtk_vbox_new (FALSE, 2);
786 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 2);
787 gtk_widget_show (entry_vbox);
789 eventbox = gtk_event_box_new ();
790 filesel->selection_text = label = gtk_label_new ("");
791 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
792 gtk_container_add (GTK_CONTAINER (eventbox), label);
793 gtk_box_pack_start (GTK_BOX (entry_vbox), eventbox, FALSE, FALSE, 0);
794 gtk_widget_show (label);
795 gtk_widget_show (eventbox);
797 filesel->selection_entry = gtk_entry_new ();
798 g_signal_connect (filesel->selection_entry, "key_press_event",
799 G_CALLBACK (gtk_file_selection_key_press), filesel);
800 g_signal_connect (filesel->selection_entry, "insert_text",
801 G_CALLBACK (gtk_file_selection_insert_text), NULL);
802 g_signal_connect_swapped (filesel->selection_entry, "changed",
803 G_CALLBACK (gtk_file_selection_update_fileops), filesel);
804 g_signal_connect_swapped (filesel->selection_entry, "focus_in_event",
805 G_CALLBACK (grab_default),
807 g_signal_connect_swapped (filesel->selection_entry, "activate",
808 G_CALLBACK (gtk_button_clicked),
811 gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
812 gtk_widget_show (filesel->selection_entry);
814 gtk_label_set_mnemonic_widget (GTK_LABEL (filesel->selection_text),
815 filesel->selection_entry);
817 if (!cmpl_state_okay (filesel->cmpl_state))
821 g_snprintf (err_buf, sizeof (err_buf), _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno));
823 gtk_label_set_text (GTK_LABEL (filesel->selection_text), err_buf);
827 gtk_file_selection_populate (filesel, "", FALSE, TRUE);
830 gtk_widget_grab_focus (filesel->selection_entry);
832 gtk_widget_pop_composite_child ();
836 dnd_really_drop (GtkWidget *dialog, gint response_id, GtkFileSelection *fs)
840 if (response_id == GTK_RESPONSE_YES)
842 filename = g_object_get_data (G_OBJECT (dialog), "gtk-fs-dnd-filename");
844 gtk_file_selection_set_filename (fs, filename);
847 gtk_widget_destroy (dialog);
851 filenames_dropped (GtkWidget *widget,
852 GdkDragContext *context,
855 GtkSelectionData *selection_data,
860 char *filename = NULL;
862 const char *this_hostname;
863 GError *error = NULL;
865 uris = gtk_selection_data_get_uris (selection_data);
866 if (!uris || !uris[0])
872 filename = g_filename_from_uri (uris[0], &hostname, &error);
877 g_warning ("Error getting dropped filename: %s\n",
879 g_error_free (error);
883 this_hostname = g_get_host_name ();
885 if ((hostname == NULL) ||
886 (strcmp (hostname, this_hostname) == 0) ||
887 (strcmp (hostname, "localhost") == 0))
888 gtk_file_selection_set_filename (GTK_FILE_SELECTION (widget),
893 gchar *filename_utf8;
895 /* Conversion back to UTF-8 should always succeed for the result
896 * of g_filename_from_uri()
898 filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
899 g_assert (filename_utf8);
901 dialog = gtk_message_dialog_new (GTK_WINDOW (widget),
902 GTK_DIALOG_DESTROY_WITH_PARENT,
903 GTK_MESSAGE_QUESTION,
905 _("The file \"%s\" resides on another machine (called %s) and may not be available to this program.\n"
906 "Are you sure that you want to select it?"), filename_utf8, hostname);
907 g_free (filename_utf8);
909 g_object_set_data_full (G_OBJECT (dialog), I_("gtk-fs-dnd-filename"), g_strdup (filename), g_free);
911 g_signal_connect_data (dialog, "response",
912 (GCallback) dnd_really_drop,
915 gtk_widget_show (dialog);
923 filenames_drag_get (GtkWidget *widget,
924 GdkDragContext *context,
925 GtkSelectionData *selection_data,
928 GtkFileSelection *filesel)
931 gchar *filename_utf8;
933 file = gtk_file_selection_get_filename (filesel);
937 if (gtk_targets_include_uri (&selection_data->target, 1))
940 const char *hostname;
944 hostname = g_get_host_name ();
947 file_uri = g_filename_to_uri (file, hostname, &error);
950 g_warning ("Error getting filename: %s\n",
952 g_error_free (error);
958 gtk_selection_data_set_uris (selection_data, uris);
964 filename_utf8 = g_filename_to_utf8 (file, -1, NULL, NULL, NULL);
968 gtk_selection_data_set_text (selection_data, filename_utf8, -1);
969 g_free (filename_utf8);
973 file_selection_setup_dnd (GtkFileSelection *filesel)
977 gtk_drag_dest_set (GTK_WIDGET (filesel),
978 GTK_DEST_DEFAULT_ALL,
981 gtk_drag_dest_add_uri_targets (GTK_WIDGET (filesel));
983 g_signal_connect (filesel, "drag_data_received",
984 G_CALLBACK (filenames_dropped), NULL);
986 eventbox = gtk_widget_get_parent (filesel->selection_text);
987 gtk_drag_source_set (eventbox,
991 gtk_drag_source_add_uri_targets (eventbox);
992 gtk_drag_source_add_text_targets (eventbox);
994 g_signal_connect (eventbox, "drag_data_get",
995 G_CALLBACK (filenames_drag_get), filesel);
999 gtk_file_selection_new (const gchar *title)
1001 GtkFileSelection *filesel;
1003 filesel = g_object_new (GTK_TYPE_FILE_SELECTION, NULL);
1004 gtk_window_set_title (GTK_WINDOW (filesel), title);
1005 gtk_dialog_set_has_separator (GTK_DIALOG (filesel), FALSE);
1007 file_selection_setup_dnd (filesel);
1009 return GTK_WIDGET (filesel);
1013 gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel)
1015 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1017 /* delete, create directory, and rename */
1018 if (!filesel->fileop_c_dir)
1020 filesel->fileop_c_dir = gtk_button_new_with_mnemonic (_("_New Folder"));
1021 g_signal_connect (filesel->fileop_c_dir, "clicked",
1022 G_CALLBACK (gtk_file_selection_create_dir),
1024 gtk_box_pack_start (GTK_BOX (filesel->button_area),
1025 filesel->fileop_c_dir, TRUE, TRUE, 0);
1026 gtk_widget_show (filesel->fileop_c_dir);
1029 if (!filesel->fileop_del_file)
1031 filesel->fileop_del_file = gtk_button_new_with_mnemonic (_("De_lete File"));
1032 g_signal_connect (filesel->fileop_del_file, "clicked",
1033 G_CALLBACK (gtk_file_selection_delete_file),
1035 gtk_box_pack_start (GTK_BOX (filesel->button_area),
1036 filesel->fileop_del_file, TRUE, TRUE, 0);
1037 gtk_widget_show (filesel->fileop_del_file);
1040 if (!filesel->fileop_ren_file)
1042 filesel->fileop_ren_file = gtk_button_new_with_mnemonic (_("_Rename File"));
1043 g_signal_connect (filesel->fileop_ren_file, "clicked",
1044 G_CALLBACK (gtk_file_selection_rename_file),
1046 gtk_box_pack_start (GTK_BOX (filesel->button_area),
1047 filesel->fileop_ren_file, TRUE, TRUE, 0);
1048 gtk_widget_show (filesel->fileop_ren_file);
1051 gtk_file_selection_update_fileops (filesel);
1053 g_object_notify (G_OBJECT (filesel), "show-fileops");
1057 gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
1059 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1061 if (filesel->fileop_ren_file)
1063 gtk_widget_destroy (filesel->fileop_ren_file);
1064 filesel->fileop_ren_file = NULL;
1067 if (filesel->fileop_del_file)
1069 gtk_widget_destroy (filesel->fileop_del_file);
1070 filesel->fileop_del_file = NULL;
1073 if (filesel->fileop_c_dir)
1075 gtk_widget_destroy (filesel->fileop_c_dir);
1076 filesel->fileop_c_dir = NULL;
1078 g_object_notify (G_OBJECT (filesel), "show-fileops");
1084 * gtk_file_selection_set_filename:
1085 * @filesel: a #GtkFileSelection.
1086 * @filename: a string to set as the default file name.
1088 * Sets a default path for the file requestor. If @filename includes a
1089 * directory path, then the requestor will open with that path as its
1090 * current working directory.
1092 * This has the consequence that in order to open the requestor with a
1093 * working directory and an empty filename, @filename must have a trailing
1094 * directory separator.
1096 * The encoding of @filename is preferred GLib file name encoding, which
1097 * may not be UTF-8. See g_filename_from_utf8().
1100 gtk_file_selection_set_filename (GtkFileSelection *filesel,
1101 const gchar *filename)
1104 const char *name, *last_slash;
1105 char *filename_utf8;
1107 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1108 g_return_if_fail (filename != NULL);
1110 filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
1111 g_return_if_fail (filename_utf8 != NULL);
1113 last_slash = strrchr (filename_utf8, G_DIR_SEPARATOR);
1117 buf = g_strdup ("");
1118 name = filename_utf8;
1122 buf = g_strdup (filename_utf8);
1123 buf[last_slash - filename_utf8 + 1] = 0;
1124 name = last_slash + 1;
1127 gtk_file_selection_populate (filesel, buf, FALSE, TRUE);
1129 if (filesel->selection_entry)
1130 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
1132 g_object_notify (G_OBJECT (filesel), "filename");
1134 g_free (filename_utf8);
1138 * gtk_file_selection_get_filename:
1139 * @filesel: a #GtkFileSelection
1141 * This function returns the selected filename in the GLib file name
1142 * encoding. To convert to UTF-8, call g_filename_to_utf8(). The
1143 * returned string points to a statically allocated buffer and should
1144 * be copied if you plan to keep it around.
1146 * If no file is selected then the selected directory path is returned.
1148 * Return value: currently-selected filename in the on-disk encoding.
1150 G_CONST_RETURN gchar*
1151 gtk_file_selection_get_filename (GtkFileSelection *filesel)
1153 static const gchar nothing[2] = "";
1154 static GString *something;
1158 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
1160 #ifdef G_WITH_CYGWIN
1161 translate_win32_path (filesel);
1163 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
1166 gchar *fullname = cmpl_completion_fullname (text, filesel->cmpl_state);
1167 sys_filename = g_filename_from_utf8 (fullname, -1, NULL, NULL, NULL);
1172 something = g_string_new (sys_filename);
1174 g_string_assign (something, sys_filename);
1175 g_free (sys_filename);
1177 return something->str;
1184 gtk_file_selection_complete (GtkFileSelection *filesel,
1185 const gchar *pattern)
1187 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1188 g_return_if_fail (pattern != NULL);
1190 if (filesel->selection_entry)
1191 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
1192 gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE, TRUE);
1196 gtk_file_selection_destroy (GtkObject *object)
1198 GtkFileSelection *filesel;
1200 HistoryCallbackArg *callback_arg;
1202 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
1204 filesel = GTK_FILE_SELECTION (object);
1206 if (filesel->fileop_dialog)
1208 gtk_widget_destroy (filesel->fileop_dialog);
1209 filesel->fileop_dialog = NULL;
1212 if (filesel->history_list)
1214 list = filesel->history_list;
1217 callback_arg = list->data;
1218 g_free (callback_arg->directory);
1219 g_free (callback_arg);
1222 g_list_free (filesel->history_list);
1223 filesel->history_list = NULL;
1226 if (filesel->cmpl_state)
1228 cmpl_free_state (filesel->cmpl_state);
1229 filesel->cmpl_state = NULL;
1232 if (filesel->selected_names)
1234 free_selected_names (filesel->selected_names);
1235 filesel->selected_names = NULL;
1238 if (filesel->last_selected)
1240 g_free (filesel->last_selected);
1241 filesel->last_selected = NULL;
1244 GTK_OBJECT_CLASS (gtk_file_selection_parent_class)->destroy (object);
1248 gtk_file_selection_map (GtkWidget *widget)
1250 GtkFileSelection *filesel = GTK_FILE_SELECTION (widget);
1252 /* Refresh the contents */
1253 gtk_file_selection_populate (filesel, "", FALSE, FALSE);
1255 GTK_WIDGET_CLASS (gtk_file_selection_parent_class)->map (widget);
1259 gtk_file_selection_finalize (GObject *object)
1261 GtkFileSelection *filesel = GTK_FILE_SELECTION (object);
1263 g_free (filesel->fileop_file);
1265 G_OBJECT_CLASS (gtk_file_selection_parent_class)->finalize (object);
1268 /* Begin file operations callbacks */
1271 gtk_file_selection_fileop_error (GtkFileSelection *fs,
1272 gchar *error_message)
1276 g_return_if_fail (error_message != NULL);
1279 dialog = gtk_message_dialog_new (GTK_WINDOW (fs),
1280 GTK_DIALOG_DESTROY_WITH_PARENT,
1283 "%s", error_message);
1285 /* yes, we free it */
1286 g_free (error_message);
1288 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1290 g_signal_connect_swapped (dialog, "response",
1291 G_CALLBACK (gtk_widget_destroy),
1294 gtk_widget_show (dialog);
1298 gtk_file_selection_fileop_destroy (GtkWidget *widget,
1301 GtkFileSelection *fs = data;
1303 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1305 fs->fileop_dialog = NULL;
1309 entry_is_empty (GtkEntry *entry)
1311 const gchar *text = gtk_entry_get_text (entry);
1313 return *text == '\0';
1317 gtk_file_selection_fileop_entry_changed (GtkEntry *entry,
1320 gtk_widget_set_sensitive (button, !entry_is_empty (entry));
1324 gtk_file_selection_create_dir_confirmed (GtkWidget *widget,
1327 GtkFileSelection *fs = data;
1328 const gchar *dirname;
1331 gchar *sys_full_path;
1333 GError *error = NULL;
1334 CompletionState *cmpl_state;
1336 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1338 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1339 cmpl_state = (CompletionState*) fs->cmpl_state;
1340 path = cmpl_reference_position (cmpl_state);
1342 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
1343 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1346 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1347 buf = g_strdup_printf (_("The folder name \"%s\" contains symbols that are not allowed in filenames"), dirname);
1349 buf = g_strdup_printf (_("Error creating folder '%s': %s"),
1350 dirname, error->message);
1351 gtk_file_selection_fileop_error (fs, buf);
1352 g_error_free (error);
1356 if (g_mkdir (sys_full_path, 0777) < 0)
1358 buf = g_strdup_printf (_("Error creating folder '%s': %s"),
1359 dirname, g_strerror (errno));
1360 gtk_file_selection_fileop_error (fs, buf);
1365 g_free (sys_full_path);
1367 gtk_widget_destroy (fs->fileop_dialog);
1368 gtk_file_selection_populate (fs, "", FALSE, FALSE);
1372 gtk_file_selection_create_dir (GtkWidget *widget,
1375 GtkFileSelection *fs = data;
1381 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1383 if (fs->fileop_dialog)
1387 dialog = gtk_dialog_new ();
1388 fs->fileop_dialog = dialog;
1389 g_signal_connect (dialog, "destroy",
1390 G_CALLBACK (gtk_file_selection_fileop_destroy),
1392 gtk_window_set_title (GTK_WINDOW (dialog), _("New Folder"));
1393 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1394 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fs));
1396 /* If file dialog is grabbed, grab option dialog */
1397 /* When option dialog is closed, file dialog will be grabbed again */
1398 if (GTK_WINDOW (fs)->modal)
1399 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1401 vbox = gtk_vbox_new (FALSE, 0);
1402 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1403 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1405 gtk_widget_show( vbox);
1407 label = gtk_label_new_with_mnemonic (_("_Folder name:"));
1408 gtk_misc_set_alignment(GTK_MISC (label), 0.0, 0.0);
1409 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1410 gtk_widget_show (label);
1412 /* The directory entry widget */
1413 fs->fileop_entry = gtk_entry_new ();
1414 gtk_label_set_mnemonic_widget (GTK_LABEL (label), fs->fileop_entry);
1415 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1417 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1418 gtk_entry_set_activates_default (GTK_ENTRY (fs->fileop_entry), TRUE);
1419 gtk_widget_show (fs->fileop_entry);
1422 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1423 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
1424 g_signal_connect_swapped (button, "clicked",
1425 G_CALLBACK (gtk_widget_destroy),
1428 gtk_widget_grab_focus (fs->fileop_entry);
1430 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1431 _("C_reate"), GTK_RESPONSE_OK);
1432 gtk_widget_set_sensitive (button, FALSE);
1433 g_signal_connect (button, "clicked",
1434 G_CALLBACK (gtk_file_selection_create_dir_confirmed),
1436 g_signal_connect (fs->fileop_entry, "changed",
1437 G_CALLBACK (gtk_file_selection_fileop_entry_changed),
1440 gtk_widget_grab_default (button);
1442 gtk_widget_show (dialog);
1446 gtk_file_selection_delete_file_response (GtkDialog *dialog,
1450 GtkFileSelection *fs = data;
1451 CompletionState *cmpl_state;
1454 gchar *sys_full_path;
1455 GError *error = NULL;
1458 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1460 if (response_id != GTK_RESPONSE_OK)
1462 gtk_widget_destroy (GTK_WIDGET (dialog));
1466 cmpl_state = (CompletionState*) fs->cmpl_state;
1467 path = cmpl_reference_position (cmpl_state);
1469 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1470 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1473 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1474 buf = g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"),
1477 buf = g_strdup_printf (_("Error deleting file '%s': %s"),
1478 fs->fileop_file, error->message);
1480 gtk_file_selection_fileop_error (fs, buf);
1481 g_error_free (error);
1485 if (g_unlink (sys_full_path) < 0)
1487 buf = g_strdup_printf (_("Error deleting file '%s': %s"),
1488 fs->fileop_file, g_strerror (errno));
1489 gtk_file_selection_fileop_error (fs, buf);
1494 g_free (sys_full_path);
1496 gtk_widget_destroy (fs->fileop_dialog);
1497 gtk_file_selection_populate (fs, "", FALSE, TRUE);
1501 gtk_file_selection_delete_file (GtkWidget *widget,
1504 GtkFileSelection *fs = data;
1506 const gchar *filename;
1508 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1510 if (fs->fileop_dialog)
1513 #ifdef G_WITH_CYGWIN
1514 translate_win32_path (fs);
1517 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1518 if (strlen (filename) < 1)
1521 g_free (fs->fileop_file);
1522 fs->fileop_file = g_strdup (filename);
1525 fs->fileop_dialog = dialog =
1526 gtk_message_dialog_new (GTK_WINDOW (fs),
1527 GTK_WINDOW (fs)->modal ? GTK_DIALOG_MODAL : 0,
1528 GTK_MESSAGE_QUESTION,
1530 _("Really delete file \"%s\"?"), filename);
1532 g_signal_connect (dialog, "destroy",
1533 G_CALLBACK (gtk_file_selection_fileop_destroy),
1535 gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
1536 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1539 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
1540 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1541 GTK_STOCK_DELETE, GTK_RESPONSE_OK,
1544 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
1546 g_signal_connect (dialog, "response",
1547 G_CALLBACK (gtk_file_selection_delete_file_response),
1550 gtk_widget_show (dialog);
1554 gtk_file_selection_rename_file_confirmed (GtkWidget *widget,
1557 GtkFileSelection *fs = data;
1561 gchar *new_filename;
1562 gchar *old_filename;
1563 gchar *sys_new_filename;
1564 gchar *sys_old_filename;
1565 CompletionState *cmpl_state;
1566 GError *error = NULL;
1568 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1570 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1571 cmpl_state = (CompletionState*) fs->cmpl_state;
1572 path = cmpl_reference_position (cmpl_state);
1574 new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
1575 old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1577 sys_new_filename = g_filename_from_utf8 (new_filename, -1, NULL, NULL, &error);
1580 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1581 buf = g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"), new_filename);
1583 buf = g_strdup_printf (_("Error renaming file to \"%s\": %s"),
1584 new_filename, error->message);
1585 gtk_file_selection_fileop_error (fs, buf);
1586 g_error_free (error);
1590 sys_old_filename = g_filename_from_utf8 (old_filename, -1, NULL, NULL, &error);
1593 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1594 buf = g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"), old_filename);
1596 buf = g_strdup_printf (_("Error renaming file \"%s\": %s"),
1597 old_filename, error->message);
1598 gtk_file_selection_fileop_error (fs, buf);
1599 g_error_free (error);
1603 if (g_rename (sys_old_filename, sys_new_filename) < 0)
1605 buf = g_strdup_printf (_("Error renaming file \"%s\" to \"%s\": %s"),
1606 sys_old_filename, sys_new_filename,
1607 g_strerror (errno));
1608 gtk_file_selection_fileop_error (fs, buf);
1612 gtk_file_selection_populate (fs, "", FALSE, FALSE);
1613 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), file);
1616 g_free (sys_old_filename);
1619 g_free (new_filename);
1620 g_free (old_filename);
1621 g_free (sys_new_filename);
1623 gtk_widget_destroy (fs->fileop_dialog);
1627 gtk_file_selection_rename_file (GtkWidget *widget,
1630 GtkFileSelection *fs = data;
1637 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1639 if (fs->fileop_dialog)
1642 g_free (fs->fileop_file);
1643 fs->fileop_file = g_strdup (gtk_entry_get_text (GTK_ENTRY (fs->selection_entry)));
1644 if (strlen (fs->fileop_file) < 1)
1648 fs->fileop_dialog = dialog = gtk_dialog_new ();
1649 g_signal_connect (dialog, "destroy",
1650 G_CALLBACK (gtk_file_selection_fileop_destroy),
1652 gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
1653 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1654 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fs));
1656 /* If file dialog is grabbed, grab option dialog */
1657 /* When option dialog closed, file dialog will be grabbed again */
1658 if (GTK_WINDOW (fs)->modal)
1659 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1661 vbox = gtk_vbox_new (FALSE, 0);
1662 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1663 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1665 gtk_widget_show(vbox);
1667 buf = g_strdup_printf (_("Rename file \"%s\" to:"), fs->fileop_file);
1668 label = gtk_label_new (buf);
1669 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1670 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1671 gtk_widget_show (label);
1674 /* New filename entry */
1675 fs->fileop_entry = gtk_entry_new ();
1676 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1678 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1679 gtk_entry_set_activates_default (GTK_ENTRY (fs->fileop_entry), TRUE);
1680 gtk_widget_show (fs->fileop_entry);
1682 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1683 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1684 0, strlen (fs->fileop_file));
1687 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1688 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
1689 g_signal_connect_swapped (button, "clicked",
1690 G_CALLBACK (gtk_widget_destroy),
1693 gtk_widget_grab_focus (fs->fileop_entry);
1695 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1696 _("_Rename"), GTK_RESPONSE_OK);
1697 g_signal_connect (button, "clicked",
1698 G_CALLBACK (gtk_file_selection_rename_file_confirmed),
1700 g_signal_connect (fs->fileop_entry, "changed",
1701 G_CALLBACK (gtk_file_selection_fileop_entry_changed),
1704 gtk_widget_grab_default (button);
1706 gtk_widget_show (dialog);
1710 gtk_file_selection_insert_text (GtkWidget *widget,
1711 const gchar *new_text,
1712 gint new_text_length,
1718 filename = g_filename_from_utf8 (new_text, new_text_length, NULL, NULL, NULL);
1722 gdk_display_beep (gtk_widget_get_display (widget));
1723 g_signal_stop_emission_by_name (widget, "insert_text");
1733 gtk_file_selection_update_fileops (GtkFileSelection *fs)
1737 if (!fs->selection_entry)
1740 sensitive = !entry_is_empty (GTK_ENTRY (fs->selection_entry));
1742 if (fs->fileop_del_file)
1743 gtk_widget_set_sensitive (fs->fileop_del_file, sensitive);
1745 if (fs->fileop_ren_file)
1746 gtk_widget_set_sensitive (fs->fileop_ren_file, sensitive);
1750 gtk_file_selection_key_press (GtkWidget *widget,
1754 GtkFileSelection *fs;
1757 g_return_val_if_fail (widget != NULL, FALSE);
1758 g_return_val_if_fail (event != NULL, FALSE);
1760 if ((event->keyval == GDK_Tab || event->keyval == GDK_KP_Tab) &&
1761 (event->state & gtk_accelerator_get_default_mod_mask ()) == 0)
1763 fs = GTK_FILE_SELECTION (user_data);
1764 #ifdef G_WITH_CYGWIN
1765 translate_win32_path (fs);
1767 text = g_strdup (gtk_entry_get_text (GTK_ENTRY (fs->selection_entry)));
1769 gtk_file_selection_populate (fs, text, TRUE, TRUE);
1780 gtk_file_selection_history_callback (GtkWidget *widget,
1783 GtkFileSelection *fs = data;
1784 HistoryCallbackArg *callback_arg;
1787 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1789 list = fs->history_list;
1792 callback_arg = list->data;
1794 if (callback_arg->menu_item == widget)
1796 gtk_file_selection_populate (fs, callback_arg->directory, FALSE, FALSE);
1805 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1806 gchar *current_directory)
1808 HistoryCallbackArg *callback_arg;
1809 GtkWidget *menu_item;
1815 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1816 g_return_if_fail (current_directory != NULL);
1818 list = fs->history_list;
1820 if (fs->history_menu)
1823 callback_arg = list->data;
1824 g_free (callback_arg->directory);
1825 g_free (callback_arg);
1828 g_list_free (fs->history_list);
1829 fs->history_list = NULL;
1831 gtk_widget_destroy (fs->history_menu);
1834 fs->history_menu = gtk_menu_new ();
1836 current_dir = g_strdup (current_directory);
1838 dir_len = strlen (current_dir);
1840 for (i = dir_len; i >= 0; i--)
1842 /* the i == dir_len is to catch the full path for the first
1844 if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
1846 /* another small hack to catch the full path */
1848 current_dir[i + 1] = '\0';
1849 #ifdef G_WITH_CYGWIN
1850 if (!strcmp (current_dir, "//"))
1853 menu_item = gtk_menu_item_new_with_label (current_dir);
1855 callback_arg = g_new (HistoryCallbackArg, 1);
1856 callback_arg->menu_item = menu_item;
1858 /* since the autocompletion gets confused if you don't
1859 * supply a trailing '/' on a dir entry, set the full
1860 * (current) path to "" which just refreshes the filesel */
1863 callback_arg->directory = g_strdup ("");
1867 callback_arg->directory = g_strdup (current_dir);
1870 fs->history_list = g_list_append (fs->history_list, callback_arg);
1872 g_signal_connect (menu_item, "activate",
1873 G_CALLBACK (gtk_file_selection_history_callback),
1875 gtk_menu_shell_append (GTK_MENU_SHELL (fs->history_menu), menu_item);
1876 gtk_widget_show (menu_item);
1880 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1882 g_free (current_dir);
1886 get_real_filename (gchar *filename,
1889 #ifdef G_WITH_CYGWIN
1890 /* Check to see if the selection was a drive selector */
1891 if (isalpha (filename[0]) && (filename[1] == ':'))
1893 gchar temp_filename[MAX_PATH];
1896 cygwin_conv_to_posix_path (filename, temp_filename);
1898 /* we need trailing '/'. */
1899 len = strlen (temp_filename);
1900 if (len > 0 && temp_filename[len-1] != '/')
1902 temp_filename[len] = '/';
1903 temp_filename[len+1] = '\0';
1909 return g_strdup (temp_filename);
1911 #endif /* G_WITH_CYGWIN */
1916 gtk_file_selection_file_activate (GtkTreeView *tree_view,
1918 GtkTreeViewColumn *column,
1921 GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
1922 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
1926 gtk_tree_model_get_iter (model, &iter, path);
1927 gtk_tree_model_get (model, &iter, FILE_COLUMN, &filename, -1);
1928 filename = get_real_filename (filename, TRUE);
1929 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1930 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1936 gtk_file_selection_dir_activate (GtkTreeView *tree_view,
1938 GtkTreeViewColumn *column,
1941 GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
1942 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
1946 gtk_tree_model_get_iter (model, &iter, path);
1947 gtk_tree_model_get (model, &iter, DIR_COLUMN, &filename, -1);
1948 filename = get_real_filename (filename, TRUE);
1949 gtk_file_selection_populate (fs, filename, FALSE, FALSE);
1953 #ifdef G_PLATFORM_WIN32
1956 win32_gtk_add_drives_to_dir_list (GtkListStore *model)
1960 char formatBuffer[128];
1963 /* Get the drives string */
1964 GetLogicalDriveStrings (sizeof (buffer), buffer);
1966 /* Add the drives as necessary */
1968 while (*textPtr != '\0')
1970 /* Ignore floppies (?) */
1971 if (GetDriveType (textPtr) != DRIVE_REMOVABLE)
1973 /* Build the actual displayable string */
1974 g_snprintf (formatBuffer, sizeof (formatBuffer), "%c:\\", toupper (textPtr[0]));
1976 /* Add to the list */
1977 gtk_list_store_append (model, &iter);
1978 gtk_list_store_set (model, &iter, DIR_COLUMN, formatBuffer, -1);
1980 textPtr += (strlen (textPtr) + 1);
1986 escape_underscores (const gchar *str)
1988 GString *result = g_string_new (NULL);
1992 g_string_append_c (result, '_');
1994 g_string_append_c (result, *str);
1998 return g_string_free (result, FALSE);
2002 gtk_file_selection_populate (GtkFileSelection *fs,
2004 gboolean try_complete,
2005 gboolean reset_entry)
2007 CompletionState *cmpl_state;
2008 PossibleCompletion* poss;
2010 GtkListStore *dir_model;
2011 GtkListStore *file_model;
2013 gchar* rem_path = rel_path;
2015 gint did_recurse = FALSE;
2016 gint possible_count = 0;
2018 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
2020 cmpl_state = (CompletionState*) fs->cmpl_state;
2021 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
2023 if (!cmpl_state_okay (cmpl_state))
2025 /* Something went wrong. */
2026 gtk_file_selection_abort (fs);
2030 g_assert (cmpl_state->reference_dir);
2032 dir_model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs->dir_list)));
2033 file_model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs->file_list)));
2035 gtk_list_store_clear (dir_model);
2036 gtk_list_store_clear (file_model);
2038 /* Set the dir list to include ./ and ../ */
2039 gtk_list_store_append (dir_model, &iter);
2040 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, "." G_DIR_SEPARATOR_S, -1);
2041 gtk_list_store_append (dir_model, &iter);
2042 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, ".." G_DIR_SEPARATOR_S, -1);
2046 if (cmpl_is_a_completion (poss))
2048 possible_count += 1;
2050 filename = cmpl_this_completion (poss);
2052 if (cmpl_is_directory (poss))
2054 if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
2055 strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
2057 gtk_list_store_append (dir_model, &iter);
2058 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, filename, -1);
2063 gtk_list_store_append (file_model, &iter);
2064 gtk_list_store_set (file_model, &iter, DIR_COLUMN, filename, -1);
2068 poss = cmpl_next_completion (cmpl_state);
2071 #ifdef G_PLATFORM_WIN32
2072 /* For Windows, add drives as potential selections */
2073 win32_gtk_add_drives_to_dir_list (dir_model);
2076 /* File lists are set. */
2078 g_assert (cmpl_state->reference_dir);
2083 /* User is trying to complete filenames, so advance the user's input
2084 * string to the updated_text, which is the common leading substring
2085 * of all possible completions, and if its a directory attempt
2086 * attempt completions in it. */
2088 if (cmpl_updated_text (cmpl_state)[0])
2091 if (cmpl_updated_dir (cmpl_state))
2093 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
2097 gtk_file_selection_populate (fs, dir_name, TRUE, TRUE);
2103 if (fs->selection_entry)
2104 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
2105 cmpl_updated_text (cmpl_state));
2110 if (fs->selection_entry)
2111 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
2114 else if (reset_entry)
2116 if (fs->selection_entry)
2117 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
2122 if (fs->selection_entry && try_complete)
2123 gtk_editable_set_position (GTK_EDITABLE (fs->selection_entry), -1);
2125 if (fs->selection_entry)
2127 char *escaped = escape_underscores (cmpl_reference_position (cmpl_state));
2128 sel_text = g_strconcat (_("_Selection: "), escaped, NULL);
2131 gtk_label_set_text_with_mnemonic (GTK_LABEL (fs->selection_text), sel_text);
2135 if (fs->history_pulldown)
2137 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
2144 gtk_file_selection_abort (GtkFileSelection *fs)
2148 g_snprintf (err_buf, sizeof (err_buf), _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno));
2150 /* BEEP gdk_beep(); */
2152 if (fs->selection_entry)
2153 gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
2157 * gtk_file_selection_set_select_multiple:
2158 * @filesel: a #GtkFileSelection
2159 * @select_multiple: whether or not the user is allowed to select multiple
2160 * files in the file list.
2162 * Sets whether the user is allowed to select multiple files in the file list.
2163 * Use gtk_file_selection_get_selections () to get the list of selected files.
2166 gtk_file_selection_set_select_multiple (GtkFileSelection *filesel,
2167 gboolean select_multiple)
2169 GtkTreeSelection *sel;
2170 GtkSelectionMode mode;
2172 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
2174 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list));
2176 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE;
2178 if (mode != gtk_tree_selection_get_mode (sel))
2180 gtk_tree_selection_set_mode (sel, mode);
2182 g_object_notify (G_OBJECT (filesel), "select-multiple");
2187 * gtk_file_selection_get_select_multiple:
2188 * @filesel: a #GtkFileSelection
2190 * Determines whether or not the user is allowed to select multiple files in
2191 * the file list. See gtk_file_selection_set_select_multiple().
2193 * Return value: %TRUE if the user is allowed to select multiple files in the
2197 gtk_file_selection_get_select_multiple (GtkFileSelection *filesel)
2199 GtkTreeSelection *sel;
2201 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), FALSE);
2203 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list));
2204 return (gtk_tree_selection_get_mode (sel) == GTK_SELECTION_MULTIPLE);
2208 multiple_changed_foreach (GtkTreeModel *model,
2213 GPtrArray *names = data;
2216 gtk_tree_model_get (model, iter, FILE_COLUMN, &filename, -1);
2218 g_ptr_array_add (names, filename);
2222 free_selected_names (GPtrArray *names)
2226 for (i = 0; i < names->len; i++)
2227 g_free (g_ptr_array_index (names, i));
2229 g_ptr_array_free (names, TRUE);
2233 gtk_file_selection_file_changed (GtkTreeSelection *selection,
2236 GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
2237 GPtrArray *new_names;
2242 new_names = g_ptr_array_sized_new (8);
2244 gtk_tree_selection_selected_foreach (selection,
2245 multiple_changed_foreach,
2248 /* nothing selected */
2249 if (new_names->len == 0)
2251 g_ptr_array_free (new_names, TRUE);
2253 if (fs->selected_names != NULL)
2255 free_selected_names (fs->selected_names);
2256 fs->selected_names = NULL;
2259 goto maybe_clear_entry;
2262 if (new_names->len != 1)
2264 GPtrArray *old_names = fs->selected_names;
2266 if (old_names != NULL)
2268 /* A common case is selecting a range of files from top to bottom,
2269 * so quickly check for that to avoid looping over the entire list
2271 if (compare_utf8_filenames (g_ptr_array_index (old_names, old_names->len - 1),
2272 g_ptr_array_index (new_names, new_names->len - 1)) != 0)
2273 index = new_names->len - 1;
2276 gint i = 0, j = 0, cmp;
2278 /* do a quick diff, stopping at the first file not in the
2281 while (i < old_names->len && j < new_names->len)
2283 cmp = compare_utf8_filenames (g_ptr_array_index (old_names, i),
2284 g_ptr_array_index (new_names, j));
2301 /* we ran off the end of the old list */
2302 if (index == -1 && i < new_names->len)
2308 /* A phantom anchor still exists at the point where the last item
2309 * was selected, which is used for subsequent range selections.
2310 * So search up from there.
2312 if (fs->last_selected &&
2313 compare_utf8_filenames (fs->last_selected,
2314 g_ptr_array_index (new_names, 0)) == 0)
2315 index = new_names->len - 1;
2323 if (fs->selected_names != NULL)
2324 free_selected_names (fs->selected_names);
2326 fs->selected_names = new_names;
2330 g_free (fs->last_selected);
2332 fs->last_selected = g_strdup (g_ptr_array_index (new_names, index));
2333 filename = get_real_filename (fs->last_selected, FALSE);
2335 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
2337 if (filename != fs->last_selected)
2345 entry = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
2346 if ((entry != NULL) && (fs->last_selected != NULL) &&
2347 (compare_utf8_filenames (entry, fs->last_selected) == 0))
2348 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
2352 * gtk_file_selection_get_selections:
2353 * @filesel: a #GtkFileSelection
2355 * Retrieves the list of file selections the user has made in the dialog box.
2356 * This function is intended for use when the user can select multiple files
2359 * The filenames are in the GLib file name encoding. To convert to
2360 * UTF-8, call g_filename_to_utf8() on each string.
2362 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2363 * g_strfreev() to free it.
2366 gtk_file_selection_get_selections (GtkFileSelection *filesel)
2370 gchar *filename, *dirname;
2371 gchar *current, *buf;
2373 gboolean unselected_entry;
2375 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), NULL);
2377 filename = g_strdup (gtk_file_selection_get_filename (filesel));
2379 if (strlen (filename) == 0)
2385 names = filesel->selected_names;
2388 selections = g_new (gchar *, names->len + 2);
2390 selections = g_new (gchar *, 2);
2393 unselected_entry = TRUE;
2397 dirname = g_path_get_dirname (filename);
2399 if ((names->len >= 1) &&
2400 (strcmp (gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry)), "") == 0))
2401 { /* multiple files are selected and last selection was removed via ctrl click */
2403 dirname = g_strdup (filename); /* as gtk_file_selection_get_filename returns dir
2404 if no file is selected */
2405 unselected_entry = FALSE;
2408 for (i = 0; i < names->len; i++)
2410 buf = g_filename_from_utf8 (g_ptr_array_index (names, i), -1,
2412 current = g_build_filename (dirname, buf, NULL);
2415 selections[count++] = current;
2417 if (unselected_entry && compare_sys_filenames (current, filename) == 0)
2418 unselected_entry = FALSE;
2424 if (unselected_entry)
2425 selections[count++] = filename;
2429 selections[count] = NULL;
2434 /**********************************************************************/
2435 /* External Interface */
2436 /**********************************************************************/
2438 /* The four completion state selectors
2441 cmpl_updated_text (CompletionState *cmpl_state)
2443 return cmpl_state->updated_text;
2447 cmpl_updated_dir (CompletionState *cmpl_state)
2449 return cmpl_state->re_complete;
2453 cmpl_reference_position (CompletionState *cmpl_state)
2455 return cmpl_state->reference_dir->fullname;
2459 /* This doesn't work currently and would require changes
2460 * to fnmatch.c to get working.
2463 cmpl_last_valid_char (CompletionState *cmpl_state)
2465 return cmpl_state->last_valid_char;
2470 cmpl_completion_fullname (const gchar *text,
2471 CompletionState *cmpl_state)
2473 if (!cmpl_state_okay (cmpl_state))
2475 return g_strdup ("");
2477 else if (g_path_is_absolute (text))
2479 return g_strdup (text);
2482 else if (text[0] == '~')
2487 dir = open_user_dir (text, cmpl_state);
2491 slash = strchr (text, G_DIR_SEPARATOR);
2493 /* slash may be NULL, that works too */
2494 return g_strconcat (dir->fullname, slash, NULL);
2499 return g_build_filename (cmpl_state->reference_dir->fullname,
2504 /* The three completion selectors
2507 cmpl_this_completion (PossibleCompletion* pc)
2513 cmpl_is_directory (PossibleCompletion* pc)
2515 return pc->is_directory;
2519 cmpl_is_a_completion (PossibleCompletion* pc)
2521 return pc->is_a_completion;
2524 /**********************************************************************/
2525 /* Construction, deletion */
2526 /**********************************************************************/
2528 /* Get the nearest parent of the current directory for which
2529 * we can convert the filename into UTF-8. With paranoia.
2530 * Returns "." when all goes wrong.
2533 get_current_dir_utf8 (void)
2535 gchar *dir = g_get_current_dir ();
2536 gchar *dir_utf8 = NULL;
2542 dir_utf8 = g_filename_to_utf8 (dir, -1, NULL, NULL, NULL);
2546 last_slash = strrchr (dir, G_DIR_SEPARATOR);
2547 if (!last_slash) /* g_get_current_dir() wasn't absolute! */
2550 if (last_slash + 1 == g_path_skip_root (dir)) /* Parent directory is a root directory */
2552 if (last_slash[1] == '\0') /* Root misencoded! */
2555 last_slash[1] = '\0';
2558 last_slash[0] = '\0';
2560 g_assert (last_slash);
2565 return dir_utf8 ? dir_utf8 : g_strdup (".");
2568 static CompletionState*
2569 cmpl_init_state (void)
2572 CompletionState *new_state;
2575 new_state = g_new (CompletionState, 1);
2577 utf8_cwd = get_current_dir_utf8 ();
2581 new_state->reference_dir = NULL;
2582 new_state->completion_dir = NULL;
2583 new_state->active_completion_dir = NULL;
2584 new_state->directory_storage = NULL;
2585 new_state->directory_sent_storage = NULL;
2586 new_state->last_valid_char = 0;
2587 new_state->updated_text = g_new (gchar, MAXPATHLEN);
2588 new_state->updated_text_alloc = MAXPATHLEN;
2589 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
2590 new_state->the_completion.text_alloc = MAXPATHLEN;
2591 new_state->user_dir_name_buffer = NULL;
2592 new_state->user_directories = NULL;
2594 new_state->reference_dir = open_dir (utf8_cwd, new_state);
2596 if (!new_state->reference_dir)
2598 /* Directories changing from underneath us, grumble */
2599 strcpy (utf8_cwd, G_DIR_SEPARATOR_S);
2609 cmpl_free_dir_list (GList* dp0)
2615 free_dir (dp->data);
2623 cmpl_free_dir_sent_list (GList* dp0)
2629 free_dir_sent (dp->data);
2637 cmpl_free_state (CompletionState* cmpl_state)
2639 g_return_if_fail (cmpl_state != NULL);
2641 cmpl_free_dir_list (cmpl_state->directory_storage);
2642 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
2644 g_free (cmpl_state->user_dir_name_buffer);
2645 g_free (cmpl_state->user_directories);
2646 g_free (cmpl_state->the_completion.text);
2647 g_free (cmpl_state->updated_text);
2649 g_free (cmpl_state);
2653 free_dir (CompletionDir* dir)
2655 g_free (dir->cmpl_text);
2656 g_free (dir->fullname);
2661 free_dir_sent (CompletionDirSent* sent)
2664 for (i = 0; i < sent->entry_count; i++)
2666 g_free (sent->entries[i].entry_name);
2667 g_free (sent->entries[i].sort_key);
2669 g_free (sent->entries);
2674 prune_memory_usage (CompletionState *cmpl_state)
2676 GList* cdsl = cmpl_state->directory_sent_storage;
2677 GList* cdl = cmpl_state->directory_storage;
2681 for (; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
2686 cmpl_free_dir_sent_list (cdsl->next);
2690 cmpl_state->directory_storage = NULL;
2693 if (cdl->data == cmpl_state->reference_dir)
2694 cmpl_state->directory_storage = g_list_prepend (NULL, cdl->data);
2696 free_dir (cdl->data);
2703 /**********************************************************************/
2704 /* The main entrances. */
2705 /**********************************************************************/
2707 static PossibleCompletion*
2708 cmpl_completion_matches (gchar *text_to_complete,
2709 gchar **remaining_text,
2710 CompletionState *cmpl_state)
2715 PossibleCompletion *poss;
2717 prune_memory_usage (cmpl_state);
2719 g_assert (text_to_complete != NULL);
2721 cmpl_state->user_completion_index = -1;
2722 cmpl_state->last_completion_text = text_to_complete;
2723 cmpl_state->the_completion.text[0] = 0;
2724 cmpl_state->last_valid_char = 0;
2725 cmpl_state->updated_text_len = -1;
2726 cmpl_state->updated_text[0] = 0;
2727 cmpl_state->re_complete = FALSE;
2730 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2732 if (text_to_complete[0] == '~' && !first_slash)
2734 /* Text starts with ~ and there is no slash, show all the
2735 * home directory completions.
2737 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
2739 update_cmpl (poss, cmpl_state);
2744 cmpl_state->reference_dir =
2745 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
2747 if (!cmpl_state->reference_dir)
2750 cmpl_state->completion_dir =
2751 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
2753 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
2755 if (!cmpl_state->completion_dir)
2758 cmpl_state->completion_dir->cmpl_index = -1;
2759 cmpl_state->completion_dir->cmpl_parent = NULL;
2760 cmpl_state->completion_dir->cmpl_text = g_strdup (*remaining_text);
2762 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
2764 cmpl_state->reference_dir = cmpl_state->completion_dir;
2766 poss = attempt_file_completion (cmpl_state);
2768 update_cmpl (poss, cmpl_state);
2773 static PossibleCompletion*
2774 cmpl_next_completion (CompletionState* cmpl_state)
2776 PossibleCompletion* poss = NULL;
2778 cmpl_state->the_completion.text[0] = 0;
2781 if (cmpl_state->user_completion_index >= 0)
2782 poss = attempt_homedir_completion (cmpl_state->last_completion_text, cmpl_state);
2784 poss = attempt_file_completion (cmpl_state);
2786 poss = attempt_file_completion (cmpl_state);
2789 update_cmpl (poss, cmpl_state);
2794 /**********************************************************************/
2795 /* Directory Operations */
2796 /**********************************************************************/
2798 /* Open the directory where completion will begin from, if possible. */
2799 static CompletionDir*
2800 open_ref_dir (gchar *text_to_complete,
2801 gchar **remaining_text,
2802 CompletionState *cmpl_state)
2805 CompletionDir *new_dir;
2807 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2809 #ifdef G_WITH_CYGWIN
2810 if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
2813 g_snprintf (root_dir, sizeof (root_dir), "//%c", text_to_complete[2]);
2815 new_dir = open_dir (root_dir, cmpl_state);
2818 *remaining_text = text_to_complete + 4;
2826 else if (text_to_complete[0] == '~')
2828 new_dir = open_user_dir (text_to_complete, cmpl_state);
2833 *remaining_text = first_slash + 1;
2835 *remaining_text = text_to_complete + strlen (text_to_complete);
2843 else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2845 gchar *tmp = g_strdup (text_to_complete);
2849 while (*p && *p != '*' && *p != '?')
2853 p = strrchr (tmp, G_DIR_SEPARATOR);
2856 if (p + 1 == g_path_skip_root (tmp))
2860 new_dir = open_dir (tmp, cmpl_state);
2863 *remaining_text = text_to_complete +
2864 ((p == g_path_skip_root (tmp)) ? (p - tmp) : (p + 1 - tmp));
2868 /* If no possible candidates, use the cwd */
2869 gchar *utf8_curdir = get_current_dir_utf8 ();
2871 new_dir = open_dir (utf8_curdir, cmpl_state);
2874 *remaining_text = text_to_complete;
2876 g_free (utf8_curdir);
2883 *remaining_text = text_to_complete;
2885 new_dir = open_dir (cmpl_state->reference_dir->fullname, cmpl_state);
2890 new_dir->cmpl_index = -1;
2891 new_dir->cmpl_parent = NULL;
2899 /* open a directory by user name */
2900 static CompletionDir*
2901 open_user_dir (const gchar *text_to_complete,
2902 CompletionState *cmpl_state)
2904 CompletionDir *result;
2908 g_assert (text_to_complete && text_to_complete[0] == '~');
2910 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2913 cmp_len = first_slash - text_to_complete - 1;
2915 cmp_len = strlen (text_to_complete + 1);
2920 const gchar *homedir = g_get_home_dir ();
2921 gchar *utf8_homedir = g_filename_to_utf8 (homedir, -1, NULL, NULL, NULL);
2924 result = open_dir (utf8_homedir, cmpl_state);
2928 g_free (utf8_homedir);
2933 gchar* copy = g_new (char, cmp_len + 1);
2937 strncpy (copy, text_to_complete + 1, cmp_len);
2939 pwd = getpwnam (copy);
2946 utf8_dir = g_filename_to_utf8 (pwd->pw_dir, -1, NULL, NULL, NULL);
2947 result = open_dir (utf8_dir, cmpl_state);
2955 /* open a directory relative to the current relative directory */
2956 static CompletionDir*
2957 open_relative_dir (gchar *dir_name,
2959 CompletionState *cmpl_state)
2961 CompletionDir *result;
2964 path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
2965 g_string_assign (path, dir->fullname);
2967 if (dir->fullname_len > 1
2968 && path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
2969 g_string_append_c (path, G_DIR_SEPARATOR);
2970 g_string_append (path, dir_name);
2972 result = open_dir (path->str, cmpl_state);
2974 g_string_free (path, TRUE);
2979 /* after the cache lookup fails, really open a new directory */
2980 static CompletionDirSent*
2981 open_new_dir (gchar *dir_name,
2983 gboolean stat_subdirs)
2985 CompletionDirSent *sent;
2988 GError *error = NULL;
2989 gint entry_count = 0;
2992 struct stat ent_sbuf;
2994 gchar *sys_dir_name;
2996 sent = g_new (CompletionDirSent, 1);
2997 #ifndef G_PLATFORM_WIN32
2998 sent->mtime = sbuf->st_mtime;
2999 sent->inode = sbuf->st_ino;
3000 sent->device = sbuf->st_dev;
3002 path = g_string_sized_new (2*MAXPATHLEN + 10);
3004 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
3007 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3011 directory = g_dir_open (sys_dir_name, 0, &error);
3014 cmpl_errno = error->code; /* ??? */
3015 g_free (sys_dir_name);
3019 while ((dirent = g_dir_read_name (directory)) != NULL)
3021 entry_count += 2; /* For ".",".." */
3023 sent->entries = g_new (CompletionDirEntry, entry_count);
3024 sent->entry_count = entry_count;
3026 g_dir_rewind (directory);
3028 for (i = 0; i < entry_count; i += 1)
3030 GError *error = NULL;
3038 dirent = g_dir_read_name (directory);
3039 if (!dirent) /* Directory changed */
3043 sent->entries[n_entries].entry_name = g_filename_to_utf8 (dirent, -1, NULL, NULL, &error);
3044 if (sent->entries[n_entries].entry_name == NULL
3045 || !g_utf8_validate (sent->entries[n_entries].entry_name, -1, NULL))
3047 gchar *escaped_str = g_strescape (dirent, NULL);
3048 g_message (_("The filename \"%s\" couldn't be converted to UTF-8. "
3049 "(try setting the environment variable G_FILENAME_ENCODING): %s"),
3051 error->message ? error->message : _("Invalid UTF-8"));
3052 g_free (escaped_str);
3053 g_clear_error (&error);
3056 g_clear_error (&error);
3058 sent->entries[n_entries].sort_key = g_utf8_collate_key (sent->entries[n_entries].entry_name, -1);
3060 g_string_assign (path, sys_dir_name);
3061 if (path->str[path->len-1] != G_DIR_SEPARATOR)
3063 g_string_append_c (path, G_DIR_SEPARATOR);
3065 g_string_append (path, dirent);
3069 /* Here we know path->str is a "system charset" string */
3070 if (g_stat (path->str, &ent_sbuf) >= 0 && S_ISDIR (ent_sbuf.st_mode))
3071 sent->entries[n_entries].is_dir = TRUE;
3073 /* stat may fail, and we don't mind, since it could be a
3074 * dangling symlink. */
3075 sent->entries[n_entries].is_dir = FALSE;
3078 sent->entries[n_entries].is_dir = 1;
3082 sent->entry_count = n_entries;
3084 g_free (sys_dir_name);
3085 g_string_free (path, TRUE);
3086 qsort (sent->entries, sent->entry_count, sizeof (CompletionDirEntry), compare_cmpl_dir);
3088 g_dir_close (directory);
3093 #ifndef G_PLATFORM_WIN32
3096 check_dir (gchar *dir_name,
3097 struct stat *result,
3098 gboolean *stat_subdirs)
3100 /* A list of directories that we know only contain other directories.
3101 * Trying to stat every file in these directories would be very
3106 const gchar name[5];
3108 struct stat statbuf;
3109 } no_stat_dirs[] = {
3110 { "/afs", FALSE, { 0 } },
3111 { "/net", FALSE, { 0 } }
3114 static const gint n_no_stat_dirs = G_N_ELEMENTS (no_stat_dirs);
3115 static gboolean initialized = FALSE;
3116 gchar *sys_dir_name;
3122 for (i = 0; i < n_no_stat_dirs; i++)
3124 if (g_stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
3125 no_stat_dirs[i].present = TRUE;
3129 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
3132 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3136 if (g_stat (sys_dir_name, result) < 0)
3138 g_free (sys_dir_name);
3142 g_free (sys_dir_name);
3144 *stat_subdirs = TRUE;
3145 for (i = 0; i < n_no_stat_dirs; i++)
3147 if (no_stat_dirs[i].present &&
3148 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
3149 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
3151 *stat_subdirs = FALSE;
3161 /* open a directory by absolute pathname */
3162 static CompletionDir*
3163 open_dir (gchar *dir_name,
3164 CompletionState *cmpl_state)
3166 #ifndef G_PLATFORM_WIN32
3168 gboolean stat_subdirs;
3171 CompletionDirSent *sent;
3173 #ifndef G_PLATFORM_WIN32
3174 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
3177 cdsl = cmpl_state->directory_sent_storage;
3183 if (sent->inode == sbuf.st_ino &&
3184 sent->mtime == sbuf.st_mtime &&
3185 sent->device == sbuf.st_dev)
3186 return attach_dir (sent, dir_name, cmpl_state);
3191 sent = open_new_dir (dir_name, &sbuf, stat_subdirs);
3193 sent = open_new_dir (dir_name, NULL, TRUE);
3198 cmpl_state->directory_sent_storage =
3199 g_list_prepend (cmpl_state->directory_sent_storage, sent);
3201 return attach_dir (sent, dir_name, cmpl_state);
3207 static CompletionDir*
3208 attach_dir (CompletionDirSent *sent,
3210 CompletionState *cmpl_state)
3212 CompletionDir* new_dir;
3214 new_dir = g_new (CompletionDir, 1);
3216 cmpl_state->directory_storage =
3217 g_list_prepend (cmpl_state->directory_storage, new_dir);
3219 new_dir->sent = sent;
3220 new_dir->fullname = g_strdup (dir_name);
3221 new_dir->fullname_len = strlen (dir_name);
3222 new_dir->cmpl_text = NULL;
3228 correct_dir_fullname (CompletionDir* cmpl_dir)
3230 gint length = strlen (cmpl_dir->fullname);
3231 gchar *first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3232 gchar *sys_filename;
3235 /* Does it end with /. (\.) ? */
3237 strcmp (cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
3239 /* Is it just the root directory (on a drive) ? */
3240 if (cmpl_dir->fullname + length - 2 == first_slash)
3242 cmpl_dir->fullname[length - 1] = 0;
3243 cmpl_dir->fullname_len = length - 1;
3248 cmpl_dir->fullname[length - 2] = 0;
3252 /* Ends with /./ (\.\)? */
3253 else if (length >= 3 &&
3254 strcmp (cmpl_dir->fullname + length - 3,
3255 G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
3256 cmpl_dir->fullname[length - 2] = 0;
3258 /* Ends with /.. (\..) ? */
3259 else if (length >= 3 &&
3260 strcmp (cmpl_dir->fullname + length - 3,
3261 G_DIR_SEPARATOR_S "..") == 0)
3263 /* Is it just /.. (X:\..)? */
3264 if (cmpl_dir->fullname + length - 3 == first_slash)
3266 cmpl_dir->fullname[length - 2] = 0;
3267 cmpl_dir->fullname_len = length - 2;
3271 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3274 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3278 if (g_stat (sys_filename, &sbuf) < 0)
3280 g_free (sys_filename);
3284 g_free (sys_filename);
3286 cmpl_dir->fullname[length - 3] = 0;
3288 if (!correct_parent (cmpl_dir, &sbuf))
3292 /* Ends with /../ (\..\)? */
3293 else if (length >= 4 &&
3294 strcmp (cmpl_dir->fullname + length - 4,
3295 G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
3297 /* Is it just /../ (X:\..\)? */
3298 if (cmpl_dir->fullname + length - 4 == first_slash)
3300 cmpl_dir->fullname[length - 3] = 0;
3301 cmpl_dir->fullname_len = length - 3;
3305 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3308 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3312 if (g_stat (sys_filename, &sbuf) < 0)
3314 g_free (sys_filename);
3318 g_free (sys_filename);
3320 cmpl_dir->fullname[length - 4] = 0;
3322 if (!correct_parent (cmpl_dir, &sbuf))
3326 cmpl_dir->fullname_len = strlen (cmpl_dir->fullname);
3332 correct_parent (CompletionDir *cmpl_dir,
3338 #ifndef G_PLATFORM_WIN32
3341 gchar *sys_filename;
3344 last_slash = strrchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3345 g_assert (last_slash);
3346 first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3348 /* Clever (?) way to check for top-level directory that works also on
3349 * Win32, where there is a drive letter and colon prefixed...
3351 if (last_slash != first_slash)
3361 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3364 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3366 last_slash[0] = G_DIR_SEPARATOR;
3370 if (g_stat (sys_filename, &parbuf) < 0)
3372 g_free (sys_filename);
3375 last_slash[0] = G_DIR_SEPARATOR;
3378 g_free (sys_filename);
3380 #ifndef G_PLATFORM_WIN32 /* No inode numbers on Win32 */
3381 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
3382 /* it wasn't a link */
3388 last_slash[0] = G_DIR_SEPARATOR;
3390 /* it was a link, have to figure it out the hard way */
3392 new_name = find_parent_dir_fullname (cmpl_dir->fullname);
3397 g_free (cmpl_dir->fullname);
3399 cmpl_dir->fullname = new_name;
3405 #ifndef G_PLATFORM_WIN32
3408 find_parent_dir_fullname (gchar* dirname)
3410 gchar *sys_orig_dir;
3415 sys_orig_dir = g_get_current_dir ();
3416 sys_dirname = g_filename_from_utf8 (dirname, -1, NULL, NULL, NULL);
3419 g_free (sys_orig_dir);
3420 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3424 if (chdir (sys_dirname) != 0 || chdir ("..") != 0)
3427 chdir (sys_orig_dir);
3428 g_free (sys_dirname);
3429 g_free (sys_orig_dir);
3432 g_free (sys_dirname);
3434 sys_cwd = g_get_current_dir ();
3435 result = g_filename_to_utf8 (sys_cwd, -1, NULL, NULL, NULL);
3438 if (chdir (sys_orig_dir) != 0)
3441 g_free (sys_orig_dir);
3445 g_free (sys_orig_dir);
3451 /**********************************************************************/
3452 /* Completion Operations */
3453 /**********************************************************************/
3457 static PossibleCompletion*
3458 attempt_homedir_completion (gchar *text_to_complete,
3459 CompletionState *cmpl_state)
3463 if (!cmpl_state->user_dir_name_buffer &&
3464 !get_pwdb (cmpl_state))
3467 cmpl_state->user_completion_index += 1;
3469 while (cmpl_state->user_completion_index < cmpl_state->user_directories_len)
3471 index = first_diff_index (text_to_complete + 1,
3472 cmpl_state->user_directories
3473 [cmpl_state->user_completion_index].login);
3480 if (cmpl_state->last_valid_char < (index + 1))
3481 cmpl_state->last_valid_char = index + 1;
3482 cmpl_state->user_completion_index += 1;
3486 cmpl_state->the_completion.is_a_completion = 1;
3487 cmpl_state->the_completion.is_directory = TRUE;
3489 append_completion_text ("~", cmpl_state);
3491 append_completion_text (cmpl_state->
3492 user_directories[cmpl_state->user_completion_index].login,
3495 return append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3498 if (text_to_complete[1]
3499 || cmpl_state->user_completion_index > cmpl_state->user_directories_len)
3501 cmpl_state->user_completion_index = -1;
3506 cmpl_state->user_completion_index += 1;
3507 cmpl_state->the_completion.is_a_completion = 1;
3508 cmpl_state->the_completion.is_directory = TRUE;
3510 return append_completion_text ("~" G_DIR_SEPARATOR_S, cmpl_state);
3516 #ifdef G_PLATFORM_WIN32
3517 /* FIXME: determine whether we should casefold all Unicode letters
3518 * here, too (and in in first_diff_index() walk through the strings with
3519 * g_utf8_next_char()), or if this folding isn't actually needed at
3522 #define FOLD(c) (tolower(c))
3527 /* returns the index (>= 0) of the first differing character,
3528 * PATTERN_MATCH if the completion matches */
3530 first_diff_index (gchar *pat,
3535 while (*pat && *text && FOLD (*text) == FOLD (*pat))
3545 return PATTERN_MATCH;
3548 static PossibleCompletion*
3549 append_completion_text (gchar *text,
3550 CompletionState *cmpl_state)
3554 if (!cmpl_state->the_completion.text)
3557 len = strlen (text) + strlen (cmpl_state->the_completion.text) + 1;
3559 if (cmpl_state->the_completion.text_alloc > len)
3561 strcat (cmpl_state->the_completion.text, text);
3562 return &cmpl_state->the_completion;
3568 cmpl_state->the_completion.text_alloc = i;
3570 cmpl_state->the_completion.text = (gchar*) g_realloc (cmpl_state->the_completion.text, i);
3572 if (!cmpl_state->the_completion.text)
3576 strcat (cmpl_state->the_completion.text, text);
3577 return &cmpl_state->the_completion;
3581 static CompletionDir*
3582 find_completion_dir (gchar *text_to_complete,
3583 gchar **remaining_text,
3584 CompletionState *cmpl_state)
3586 gchar* first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
3587 CompletionDir* dir = cmpl_state->reference_dir;
3588 CompletionDir* next;
3589 *remaining_text = text_to_complete;
3593 gint len = first_slash - *remaining_text;
3595 gchar *found_name = NULL; /* Quiet gcc */
3597 gchar* pat_buf = g_new (gchar, len + 1);
3599 strncpy (pat_buf, *remaining_text, len);
3602 for (i = 0; i < dir->sent->entry_count; i += 1)
3604 if (dir->sent->entries[i].is_dir &&
3605 _gtk_fnmatch (pat_buf, dir->sent->entries[i].entry_name, TRUE))
3615 found_name = dir->sent->entries[i].entry_name;
3622 /* Perhaps we are trying to open an automount directory */
3623 found_name = pat_buf;
3626 next = open_relative_dir (found_name, dir, cmpl_state);
3634 next->cmpl_parent = dir;
3638 if (!correct_dir_fullname (dir))
3644 *remaining_text = first_slash + 1;
3645 first_slash = strchr (*remaining_text, G_DIR_SEPARATOR);
3654 update_cmpl (PossibleCompletion *poss,
3655 CompletionState *cmpl_state)
3659 if (!poss || !cmpl_is_a_completion (poss))
3662 cmpl_len = strlen (cmpl_this_completion (poss));
3664 if (cmpl_state->updated_text_alloc < cmpl_len + 1)
3666 cmpl_state->updated_text_alloc = 2*cmpl_len;
3667 cmpl_state->updated_text =
3668 (gchar*)g_realloc (cmpl_state->updated_text,
3669 cmpl_state->updated_text_alloc);
3672 if (cmpl_state->updated_text_len < 0)
3674 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3675 cmpl_state->updated_text_len = cmpl_len;
3676 cmpl_state->re_complete = cmpl_is_directory (poss);
3678 else if (cmpl_state->updated_text_len == 0)
3680 cmpl_state->re_complete = FALSE;
3685 first_diff_index (cmpl_state->updated_text,
3686 cmpl_this_completion (poss));
3688 cmpl_state->re_complete = FALSE;
3690 if (first_diff == PATTERN_MATCH)
3693 if (first_diff > cmpl_state->updated_text_len)
3694 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3696 cmpl_state->updated_text_len = first_diff;
3697 cmpl_state->updated_text[first_diff] = 0;
3701 static PossibleCompletion*
3702 attempt_file_completion (CompletionState *cmpl_state)
3704 gchar *pat_buf, *first_slash;
3705 CompletionDir *dir = cmpl_state->active_completion_dir;
3707 dir->cmpl_index += 1;
3709 if (dir->cmpl_index == dir->sent->entry_count)
3711 if (dir->cmpl_parent == NULL)
3713 cmpl_state->active_completion_dir = NULL;
3719 cmpl_state->active_completion_dir = dir->cmpl_parent;
3721 return attempt_file_completion (cmpl_state);
3725 g_assert (dir->cmpl_text);
3727 first_slash = strchr (dir->cmpl_text, G_DIR_SEPARATOR);
3731 gint len = first_slash - dir->cmpl_text;
3733 pat_buf = g_new (gchar, len + 1);
3734 strncpy (pat_buf, dir->cmpl_text, len);
3739 gint len = strlen (dir->cmpl_text);
3741 pat_buf = g_new (gchar, len + 2);
3742 strcpy (pat_buf, dir->cmpl_text);
3743 /* Don't append a * if the user entered one herself.
3744 * This way one can complete *.h and don't get matches
3745 * on any .help files, for instance.
3747 if (strchr (pat_buf, '*') == NULL)
3748 strcpy (pat_buf + len, "*");
3753 if (dir->sent->entries[dir->cmpl_index].is_dir)
3755 if (_gtk_fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name, TRUE))
3757 CompletionDir* new_dir;
3759 new_dir = open_relative_dir (dir->sent->entries[dir->cmpl_index].entry_name,
3768 new_dir->cmpl_parent = dir;
3770 new_dir->cmpl_index = -1;
3771 new_dir->cmpl_text = g_strdup (first_slash + 1);
3773 cmpl_state->active_completion_dir = new_dir;
3776 return attempt_file_completion (cmpl_state);
3781 return attempt_file_completion (cmpl_state);
3787 return attempt_file_completion (cmpl_state);
3792 if (dir->cmpl_parent != NULL)
3794 append_completion_text (dir->fullname +
3795 strlen (cmpl_state->completion_dir->fullname) + 1,
3797 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3800 append_completion_text (dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
3802 cmpl_state->the_completion.is_a_completion =
3803 _gtk_fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name, TRUE);
3805 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
3806 if (dir->sent->entries[dir->cmpl_index].is_dir)
3807 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3810 return &cmpl_state->the_completion;
3817 get_pwdb (CompletionState* cmpl_state)
3819 struct passwd *pwd_ptr;
3822 gint len = 0, i, count = 0;
3824 if (cmpl_state->user_dir_name_buffer)
3828 while ((pwd_ptr = getpwent ()) != NULL)
3830 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3831 len += strlen (utf8);
3833 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3834 len += strlen (utf8);
3842 cmpl_state->user_dir_name_buffer = g_new (gchar, len);
3843 cmpl_state->user_directories = g_new (CompletionUserDir, count);
3844 cmpl_state->user_directories_len = count;
3846 buf_ptr = cmpl_state->user_dir_name_buffer;
3848 for (i = 0; i < count; i += 1)
3850 pwd_ptr = getpwent ();
3857 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3858 strcpy (buf_ptr, utf8);
3861 cmpl_state->user_directories[i].login = buf_ptr;
3863 buf_ptr += strlen (buf_ptr);
3866 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3867 strcpy (buf_ptr, utf8);
3870 cmpl_state->user_directories[i].homedir = buf_ptr;
3872 buf_ptr += strlen (buf_ptr);
3876 qsort (cmpl_state->user_directories,
3877 cmpl_state->user_directories_len,
3878 sizeof (CompletionUserDir),
3887 g_free (cmpl_state->user_dir_name_buffer);
3888 g_free (cmpl_state->user_directories);
3890 cmpl_state->user_dir_name_buffer = NULL;
3891 cmpl_state->user_directories = NULL;
3897 compare_user_dir (const void *a,
3900 return strcmp ((((CompletionUserDir*)a))->login,
3901 (((CompletionUserDir*)b))->login);
3907 compare_cmpl_dir (const void *a,
3911 return strcmp (((CompletionDirEntry*)a)->sort_key,
3912 (((CompletionDirEntry*)b))->sort_key);
3916 cmpl_state_okay (CompletionState* cmpl_state)
3918 return cmpl_state && cmpl_state->reference_dir;
3922 cmpl_strerror (gint err)
3924 if (err == CMPL_ERRNO_TOO_LONG)
3925 return _("Name too long");
3926 else if (err == CMPL_ERRNO_DID_NOT_CONVERT)
3927 return _("Couldn't convert filename");
3929 return g_strerror (err);
3934 /* DLL ABI stability backward compatibility versions */
3936 #undef gtk_file_selection_get_filename
3938 G_CONST_RETURN gchar*
3939 gtk_file_selection_get_filename (GtkFileSelection *filesel)
3941 static gchar retval[MAXPATHLEN*2+1];
3944 tem = g_locale_from_utf8 (gtk_file_selection_get_filename_utf8 (filesel),
3945 -1, NULL, NULL, NULL);
3947 strncpy (retval, tem, sizeof (retval) - 1);
3948 retval[sizeof (retval) - 1] = '\0';
3954 #undef gtk_file_selection_set_filename
3957 gtk_file_selection_set_filename (GtkFileSelection *filesel,
3958 const gchar *filename)
3960 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
3961 gtk_file_selection_set_filename_utf8 (filesel, utf8_filename);
3962 g_free (utf8_filename);
3965 #undef gtk_file_selection_get_selections
3968 gtk_file_selection_get_selections (GtkFileSelection *filesel)
3971 gchar **selections = gtk_file_selection_get_selections_utf8 (filesel);
3973 if (selections != NULL)
3974 while (selections[i] != NULL)
3976 gchar *tem = selections[i];
3977 selections[i] = g_locale_from_utf8 (selections[i],
3978 -1, NULL, NULL, NULL);
3986 #endif /* G_OS_WIN32 */
3988 #define __GTK_FILESEL_C__
3989 #include "gtkaliasdef.c"