2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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 gchar *filesel_oldfilename;
39 static void filesel_create(const gchar *title, gboolean multiple_files);
40 static void filesel_ok_cb(GtkWidget *widget, gpointer data);
41 static void filesel_cancel_cb(GtkWidget *widget, gpointer data);
42 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data);
43 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
45 static void filesel_file_list_select_row_multi(GtkCList *clist, gint row, gint col,
46 GdkEventButton *event, gpointer userdata);
47 static void filesel_file_list_select_row_single(GtkCList *clist, gint row, gint col,
48 GdkEventButton *event, gpointer userdata);
50 static void filesel_dir_list_select_row_multi(GtkCList *clist, gint row, gint col,
51 GdkEventButton *event, gpointer userdata);
52 static void filesel_dir_list_select_row_single(GtkCList *clist, gint row, gint col,
53 GdkEventButton *event, gpointer userdata);
55 static GList *filesel_get_multiple_filenames(void);
57 gchar *filesel_select_file(const gchar *title, const gchar *file)
59 static gchar *filename = NULL;
60 static gchar *cwd = NULL;
62 filesel_create(title, FALSE);
64 manage_window_set_transient(GTK_WINDOW(filesel));
72 cwd = g_strconcat(startup_dir, G_DIR_SEPARATOR_S, NULL);
74 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), cwd);
77 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),
79 filesel_oldfilename = g_strdup(file);
81 filesel_oldfilename = NULL;
84 gtk_widget_show(filesel);
91 str = gtk_file_selection_get_filename
92 (GTK_FILE_SELECTION(filesel));
93 if (str && str[0] != '\0') {
96 filename = g_strdup(str);
99 cwd = g_strconcat(dir, G_DIR_SEPARATOR_S, NULL);
104 if (filesel_oldfilename)
105 g_free(filesel_oldfilename);
107 manage_window_focus_out(filesel, NULL, NULL);
108 gtk_widget_destroy(filesel);
114 GList *filesel_select_multiple_files(const gchar *title, const gchar *file)
116 /* ALF - sorry for the exuberant code duping... need to
118 static gchar *filename = NULL;
119 static gchar *cwd = NULL;
122 filesel_create(title, TRUE);
124 manage_window_set_transient(GTK_WINDOW(filesel));
132 cwd = g_strconcat(startup_dir, G_DIR_SEPARATOR_S, NULL);
134 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), cwd);
137 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),
139 gtk_widget_show(filesel);
146 list = filesel_get_multiple_filenames();
149 fname = gtk_file_selection_get_filename (GTK_FILE_SELECTION(filesel));
150 list = g_list_append (list, g_strdup(fname));
154 manage_window_focus_out(filesel, NULL, NULL);
155 gtk_widget_destroy(filesel);
161 static void filesel_create(const gchar *title, gboolean multiple_files)
163 filesel = gtk_file_selection_new(title);
164 gtk_window_set_position(GTK_WINDOW(filesel), GTK_WIN_POS_CENTER);
165 gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
166 gtk_window_set_wmclass
167 (GTK_WINDOW(filesel), "file_selection", "Sylpheed");
169 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button),
170 "clicked", GTK_SIGNAL_FUNC(filesel_ok_cb),
173 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button),
174 "clicked", GTK_SIGNAL_FUNC(filesel_cancel_cb),
176 gtk_signal_connect(GTK_OBJECT(filesel), "delete_event",
177 GTK_SIGNAL_FUNC(delete_event), NULL);
178 gtk_signal_connect(GTK_OBJECT(filesel), "key_press_event",
179 GTK_SIGNAL_FUNC(key_pressed), NULL);
180 MANAGE_WINDOW_SIGNALS_CONNECT(filesel);
182 if (multiple_files) {
183 gtk_clist_set_selection_mode
184 (GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list),
185 GTK_SELECTION_MULTIPLE);
186 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
188 GTK_SIGNAL_FUNC(filesel_file_list_select_row_multi),
190 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
192 GTK_SIGNAL_FUNC(filesel_file_list_select_row_multi),
194 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->dir_list),
196 GTK_SIGNAL_FUNC(filesel_dir_list_select_row_multi),
199 gtk_signal_connect_after(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
201 GTK_SIGNAL_FUNC(filesel_file_list_select_row_single),
203 gtk_signal_connect_after(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->dir_list),
205 GTK_SIGNAL_FUNC(filesel_dir_list_select_row_single),
210 static void filesel_ok_cb(GtkWidget *widget, gpointer data)
216 static void filesel_cancel_cb(GtkWidget *widget, gpointer data)
222 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
224 filesel_cancel_cb(NULL, NULL);
225 if (filesel_oldfilename) {
226 g_free(filesel_oldfilename);
227 filesel_oldfilename = NULL;
232 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
234 if (event && event->keyval == GDK_Escape)
235 filesel_cancel_cb(NULL, NULL);
238 /* handle both "select_row" and "unselect_row". note that we're using the
239 * entry box to put there the selected file names in. we're not using these
240 * entry box to get the selected file names. instead we use the clist selection.
241 * the entry box is used only to retrieve dir name. */
242 static void filesel_file_list_select_row_multi(GtkCList *clist, gint row, gint col,
243 GdkEventButton *event, gpointer userdata)
245 /* simple implementation in which we clear the file entry and refill it */
246 GList *list = clist->selection;
247 GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
249 gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
251 #define INVALID_FILENAME_CHARS " "
252 for (; list; list = list->next) {
253 gint row = GPOINTER_TO_INT(list->data);
254 gchar *text = NULL, *tmp;
256 if (!gtk_clist_get_text(clist, row, 0, &text))
259 /* NOTE: quick glance in source code of GtkCList
260 * reveals we should not free the returned 'text' */
262 tmp = g_strconcat(text, " ", NULL);
264 gtk_entry_append_text(entry, text);
267 #undef INVALID_FILENAME_CHARS
270 static void filesel_dir_list_select_row_multi(GtkCList *clist, gint row, gint col,
271 GdkEventButton *event, gpointer userdata)
273 GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
274 GtkCList *file_list = GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list);
276 /* if dir list is selected we clean everything */
277 gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
278 gtk_clist_unselect_all(file_list);
281 static void filesel_file_list_select_row_single(GtkCList *clist, gint row, gint col,
282 GdkEventButton *event, gpointer userdata)
286 if(gtk_clist_get_text(clist, row, 0, &text)) {
287 filesel_oldfilename = g_strdup(text);
288 debug_print("%s\n", filesel_oldfilename);
290 filesel_oldfilename = NULL;
294 static void filesel_dir_list_select_row_single(GtkCList *clist, gint row, gint col,
295 GdkEventButton *event, gpointer userdata)
298 GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
300 buf = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
301 if(filesel_oldfilename && !(*buf)) {
302 gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
303 gtk_entry_append_text(entry, filesel_oldfilename);
308 static GList *filesel_get_multiple_filenames(void)
310 /* as noted before we are not using the entry text when selecting
311 * multiple files. to much hassle to parse out invalid chars (chars
312 * that need to be escaped). instead we use the file_list. the
313 * entry is only useful for extracting the current directory. */
314 GtkCList *file_list = GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list);
315 GList *list = NULL, *sel_list;
319 g_return_val_if_fail(file_list->selection != NULL, NULL);
321 tmp = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel));
323 cwd = g_dirname(tmp);
325 g_return_val_if_fail(cwd != NULL, NULL);
327 /* only quick way to check the end of a multi byte string for our
330 separator = 0 == g_strncasecmp(cwd, G_DIR_SEPARATOR_S, strlen(G_DIR_SEPARATOR_S))
334 /* fetch the selected file names */
335 for (sel_list = file_list->selection; sel_list; sel_list = sel_list->next) {
336 gint sel = GPOINTER_TO_INT(sel_list->data);
337 gchar *sel_text = NULL;
340 gtk_clist_get_text(file_list, sel, 0, &sel_text);
341 if (!sel_text) continue;
342 sel_text = g_strdup(sel_text);
345 fname = g_strconcat(cwd, sel_text, NULL);
347 fname = g_strconcat(cwd, G_DIR_SEPARATOR_S, sel_text, NULL);
349 list = g_list_append(list, fname);