2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <gdk/gdkkeysyms.h>
22 #include <gtk/gtkwidget.h>
23 #include <gtk/gtkfilesel.h>
24 #include <gtk/gtkentry.h>
25 #include <gtk/gtkmain.h>
26 #include <gtk/gtksignal.h>
27 #include <gtk/gtkeditable.h>
31 #include "manage_window.h"
35 static GtkWidget *filesel;
36 static gboolean filesel_ack;
37 static gboolean filesel_fin;
38 static gchar *filesel_oldfilename;
40 static void filesel_create(const gchar *title, gboolean multiple_files);
41 static void filesel_ok_cb(GtkWidget *widget, gpointer data);
42 static void filesel_cancel_cb(GtkWidget *widget, gpointer data);
43 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data);
44 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
46 static void filesel_file_list_select_row_multi(GtkCList *clist, gint row, gint col,
47 GdkEventButton *event, gpointer userdata);
48 static void filesel_file_list_select_row_single(GtkCList *clist, gint row, gint col,
49 GdkEventButton *event, gpointer userdata);
51 static void filesel_dir_list_select_row_multi(GtkCList *clist, gint row, gint col,
52 GdkEventButton *event, gpointer userdata);
53 static void filesel_dir_list_select_row_single(GtkCList *clist, gint row, gint col,
54 GdkEventButton *event, gpointer userdata);
56 static GList *filesel_get_multiple_filenames(void);
58 gchar *filesel_select_file(const gchar *title, const gchar *file)
60 static gchar *filename = NULL;
61 static gchar *cwd = NULL;
63 filesel_create(title, FALSE);
65 manage_window_set_transient(GTK_WINDOW(filesel));
73 cwd = g_strconcat(sylpheed_get_startup_dir(), G_DIR_SEPARATOR_S, NULL);
75 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), cwd);
78 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),
80 filesel_oldfilename = g_strdup(file);
82 filesel_oldfilename = NULL;
85 gtk_widget_show(filesel);
87 filesel_ack = filesel_fin = FALSE;
89 while (filesel_fin == FALSE)
95 str = gtk_file_selection_get_filename
96 (GTK_FILE_SELECTION(filesel));
97 if (str && str[0] != '\0') {
100 filename = g_strdup(str);
101 dir = g_dirname(str);
103 cwd = g_strconcat(dir, G_DIR_SEPARATOR_S, NULL);
108 if (filesel_oldfilename)
109 g_free(filesel_oldfilename);
111 manage_window_focus_out(filesel, NULL, NULL);
112 gtk_widget_destroy(filesel);
118 GList *filesel_select_multiple_files(const gchar *title, const gchar *file)
120 /* ALF - sorry for the exuberant code duping... need to
122 static gchar *filename = NULL;
123 static gchar *cwd = NULL;
126 filesel_create(title, TRUE);
128 manage_window_set_transient(GTK_WINDOW(filesel));
136 cwd = g_strconcat(sylpheed_get_startup_dir(), G_DIR_SEPARATOR_S, NULL);
138 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), cwd);
141 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),
143 gtk_widget_show(filesel);
145 filesel_ack = filesel_fin = FALSE;
147 while (filesel_fin == FALSE)
148 gtk_main_iteration();
153 list = filesel_get_multiple_filenames();
156 fname = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel));
157 list = g_list_append(list, g_strdup(fname));
161 manage_window_focus_out(filesel, NULL, NULL);
162 gtk_widget_destroy(filesel);
168 static void filesel_create(const gchar *title, gboolean multiple_files)
170 filesel = gtk_file_selection_new(title);
171 gtk_window_set_position(GTK_WINDOW(filesel), GTK_WIN_POS_CENTER);
172 gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
173 gtk_window_set_wmclass
174 (GTK_WINDOW(filesel), "file_selection", "Sylpheed");
176 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button),
177 "clicked", GTK_SIGNAL_FUNC(filesel_ok_cb),
180 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button),
181 "clicked", GTK_SIGNAL_FUNC(filesel_cancel_cb),
183 gtk_signal_connect(GTK_OBJECT(filesel), "delete_event",
184 GTK_SIGNAL_FUNC(delete_event), NULL);
185 gtk_signal_connect(GTK_OBJECT(filesel), "key_press_event",
186 GTK_SIGNAL_FUNC(key_pressed), NULL);
187 MANAGE_WINDOW_SIGNALS_CONNECT(filesel);
189 if (multiple_files) {
190 gtk_clist_set_selection_mode
191 (GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list),
192 GTK_SELECTION_MULTIPLE);
193 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
195 GTK_SIGNAL_FUNC(filesel_file_list_select_row_multi),
197 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
199 GTK_SIGNAL_FUNC(filesel_file_list_select_row_multi),
201 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->dir_list),
203 GTK_SIGNAL_FUNC(filesel_dir_list_select_row_multi),
206 gtk_signal_connect_after(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
208 GTK_SIGNAL_FUNC(filesel_file_list_select_row_single),
210 gtk_signal_connect_after(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->dir_list),
212 GTK_SIGNAL_FUNC(filesel_dir_list_select_row_single),
217 static void filesel_ok_cb(GtkWidget *widget, gpointer data)
223 static void filesel_cancel_cb(GtkWidget *widget, gpointer data)
229 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
231 filesel_cancel_cb(NULL, NULL);
232 if (filesel_oldfilename) {
233 g_free(filesel_oldfilename);
234 filesel_oldfilename = NULL;
239 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
241 if (event && event->keyval == GDK_Escape)
242 filesel_cancel_cb(NULL, NULL);
245 /* handle both "select_row" and "unselect_row". note that we're using the
246 * entry box to put there the selected file names in. we're not using these
247 * entry box to get the selected file names. instead we use the clist selection.
248 * the entry box is used only to retrieve dir name. */
249 static void filesel_file_list_select_row_multi(GtkCList *clist, gint row, gint col,
250 GdkEventButton *event, gpointer userdata)
252 /* simple implementation in which we clear the file entry and refill it */
253 GList *list = clist->selection;
254 GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
256 gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
258 #define INVALID_FILENAME_CHARS " "
259 for (; list; list = list->next) {
260 gint row = GPOINTER_TO_INT(list->data);
261 gchar *text = NULL, *tmp;
263 if (!gtk_clist_get_text(clist, row, 0, &text))
266 /* NOTE: quick glance in source code of GtkCList
267 * reveals we should not free the returned 'text' */
269 tmp = g_strconcat(text, " ", NULL);
271 gtk_entry_append_text(entry, text);
274 #undef INVALID_FILENAME_CHARS
277 static void filesel_dir_list_select_row_multi(GtkCList *clist, gint row, gint col,
278 GdkEventButton *event, gpointer userdata)
280 GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
281 GtkCList *file_list = GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list);
283 /* if dir list is selected we clean everything */
284 gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
285 gtk_clist_unselect_all(file_list);
288 static void filesel_file_list_select_row_single(GtkCList *clist, gint row, gint col,
289 GdkEventButton *event, gpointer userdata)
293 if(gtk_clist_get_text(clist, row, 0, &text)) {
294 filesel_oldfilename = g_strdup(text);
295 debug_print("%s\n", filesel_oldfilename);
297 filesel_oldfilename = NULL;
301 static void filesel_dir_list_select_row_single(GtkCList *clist, gint row, gint col,
302 GdkEventButton *event, gpointer userdata)
305 GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
307 buf = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
308 if(filesel_oldfilename && !(*buf)) {
309 gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
310 gtk_entry_append_text(entry, filesel_oldfilename);
315 static GList *filesel_get_multiple_filenames(void)
317 /* as noted before we are not using the entry text when selecting
318 * multiple files. to much hassle to parse out invalid chars (chars
319 * that need to be escaped). instead we use the file_list. the
320 * entry is only useful for extracting the current directory. */
321 GtkCList *file_list = GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list);
322 GList *list = NULL, *sel_list;
326 g_return_val_if_fail(file_list->selection != NULL, NULL);
328 tmp = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel));
330 cwd = g_dirname(tmp);
332 g_return_val_if_fail(cwd != NULL, NULL);
334 /* only quick way to check the end of a multi byte string for our
337 separator = 0 == g_strncasecmp(cwd, G_DIR_SEPARATOR_S, strlen(G_DIR_SEPARATOR_S))
341 /* fetch the selected file names */
342 for (sel_list = file_list->selection; sel_list; sel_list = sel_list->next) {
343 gint sel = GPOINTER_TO_INT(sel_list->data);
344 gchar *sel_text = NULL;
347 gtk_clist_get_text(file_list, sel, 0, &sel_text);
348 if (!sel_text) continue;
349 sel_text = g_strdup(sel_text);
352 fname = g_strconcat(cwd, sel_text, NULL);
354 fname = g_strconcat(cwd, G_DIR_SEPARATOR_S, sel_text, NULL);
356 list = g_list_append(list, fname);