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 gboolean 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;
62 gchar *startdir = NULL;
63 gchar *realfile = NULL;
65 filesel_create(title, FALSE);
67 manage_window_set_transient(GTK_WINDOW(filesel));
74 /* try to extract the directory and filename from file */
76 startdir = g_dirname(file);
77 realfile = g_basename(file);
82 /* use sylpheed's startup directory if we can't find anything useful */
83 if (!startdir || !strcmp(startdir, ".")) {
84 cwd = g_strconcat(sylpheed_get_startup_dir(), G_DIR_SEPARATOR_S, NULL);
86 cwd = g_strconcat(startdir, G_DIR_SEPARATOR_S, NULL);
89 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), cwd);
92 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),
94 filesel_oldfilename = g_strdup(realfile);
96 filesel_oldfilename = NULL;
99 gtk_widget_show(filesel);
101 filesel_ack = filesel_fin = FALSE;
103 while (filesel_fin == FALSE)
104 gtk_main_iteration();
109 str = gtk_file_selection_get_filename
110 (GTK_FILE_SELECTION(filesel));
111 if (str && str[0] != '\0') {
114 filename = g_strdup(str);
115 dir = g_dirname(str);
117 cwd = g_strconcat(dir, G_DIR_SEPARATOR_S, NULL);
122 if (filesel_oldfilename)
123 g_free(filesel_oldfilename);
128 manage_window_focus_out(filesel, NULL, NULL);
129 gtk_widget_destroy(filesel);
135 GList *filesel_select_multiple_files(const gchar *title, const gchar *file)
137 /* ALF - sorry for the exuberant code duping... need to
139 static gchar *filename = NULL;
140 static gchar *cwd = NULL;
143 filesel_create(title, TRUE);
145 manage_window_set_transient(GTK_WINDOW(filesel));
153 cwd = g_strconcat(sylpheed_get_startup_dir(), G_DIR_SEPARATOR_S, NULL);
155 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), cwd);
158 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),
160 gtk_widget_show(filesel);
162 filesel_ack = filesel_fin = FALSE;
164 while (filesel_fin == FALSE)
165 gtk_main_iteration();
168 const gchar *fname = NULL;
170 list = filesel_get_multiple_filenames();
173 fname = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel));
174 list = g_list_append(list, g_strdup(fname));
176 /* store dir of first entry */
177 if (NULL != (fname = list->data) && *fname) {
179 if (NULL != (dir = g_dirname(fname))) {
181 cwd = g_strconcat(dir, G_DIR_SEPARATOR_S, NULL);
187 manage_window_focus_out(filesel, NULL, NULL);
188 gtk_widget_destroy(filesel);
194 static void filesel_create(const gchar *title, gboolean multiple_files)
196 filesel = gtk_file_selection_new(title);
197 gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
198 gtk_window_set_wmclass
199 (GTK_WINDOW(filesel), "file_selection", "Sylpheed");
201 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button),
202 "clicked", G_CALLBACK(filesel_ok_cb),
205 (G_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button),
206 "clicked", G_CALLBACK(filesel_cancel_cb),
208 g_signal_connect(G_OBJECT(filesel), "delete_event",
209 G_CALLBACK(delete_event), NULL);
210 g_signal_connect(G_OBJECT(filesel), "key_press_event",
211 G_CALLBACK(key_pressed), NULL);
212 MANAGE_WINDOW_SIGNALS_CONNECT(filesel);
214 if (multiple_files) {
216 #warning GTK2 dir_list/file_list now GtkTreeView. selection handlers not working.
219 gtk_file_selection_set_select_multiple
220 (GTK_FILE_SELECTION(filesel), TRUE);
222 gtk_clist_set_selection_mode
223 (GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list),
224 GTK_SELECTION_MULTIPLE);
227 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
229 G_CALLBACK(filesel_file_list_select_row_multi),
231 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
233 G_CALLBACK(filesel_file_list_select_row_multi),
235 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(filesel)->dir_list),
237 G_CALLBACK(filesel_dir_list_select_row_multi),
240 g_signal_connect_after(G_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
242 G_CALLBACK(filesel_file_list_select_row_single),
244 g_signal_connect_after(G_OBJECT(GTK_FILE_SELECTION(filesel)->dir_list),
246 G_CALLBACK(filesel_dir_list_select_row_single),
251 static void filesel_ok_cb(GtkWidget *widget, gpointer data)
257 static void filesel_cancel_cb(GtkWidget *widget, gpointer data)
263 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
265 filesel_cancel_cb(NULL, NULL);
266 if (filesel_oldfilename) {
267 g_free(filesel_oldfilename);
268 filesel_oldfilename = NULL;
273 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
275 if (event && event->keyval == GDK_Escape)
276 filesel_cancel_cb(NULL, NULL);
280 /* handle both "select_row" and "unselect_row". note that we're using the
281 * entry box to put there the selected file names in. we're not using these
282 * entry box to get the selected file names. instead we use the clist selection.
283 * the entry box is used only to retrieve dir name. */
284 static void filesel_file_list_select_row_multi(GtkCList *clist, gint row, gint col,
285 GdkEventButton *event, gpointer userdata)
287 /* simple implementation in which we clear the file entry and refill it */
288 GList *list = clist->selection;
289 GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
291 gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
293 #define INVALID_FILENAME_CHARS " "
294 for (; list; list = list->next) {
295 gint row = GPOINTER_TO_INT(list->data);
296 gchar *text = NULL, *tmp;
298 if (!gtk_clist_get_text(clist, row, 0, &text))
301 /* NOTE: quick glance in source code of GtkCList
302 * reveals we should not free the returned 'text' */
304 tmp = g_strconcat(text, " ", NULL);
306 gtk_entry_append_text(entry, text);
309 #undef INVALID_FILENAME_CHARS
312 static void filesel_dir_list_select_row_multi(GtkCList *clist, gint row, gint col,
313 GdkEventButton *event, gpointer userdata)
315 GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
316 GtkCList *file_list = GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list);
318 /* if dir list is selected we clean everything */
319 gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
320 gtk_clist_unselect_all(file_list);
323 static void filesel_file_list_select_row_single(GtkCList *clist, gint row, gint col,
324 GdkEventButton *event, gpointer userdata)
328 if(gtk_clist_get_text(clist, row, 0, &text)) {
329 filesel_oldfilename = g_strdup(text);
330 debug_print("%s\n", filesel_oldfilename);
332 filesel_oldfilename = NULL;
336 static void filesel_dir_list_select_row_single(GtkCList *clist, gint row, gint col,
337 GdkEventButton *event, gpointer userdata)
340 GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
342 buf = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
343 if(filesel_oldfilename && !(*buf)) {
344 gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
345 gtk_entry_append_text(entry, filesel_oldfilename);
350 static GList *filesel_get_multiple_filenames(void)
353 /* as noted before we are not using the entry text when selecting
354 * multiple files. to much hassle to parse out invalid chars (chars
355 * that need to be escaped). instead we use the file_list. the
356 * entry is only useful for extracting the current directory. */
357 GtkCList *file_list = GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list);
358 GList *list = NULL, *sel_list;
362 g_return_val_if_fail(file_list->selection != NULL, NULL);
364 cwd = g_dirname(gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel)));
365 g_return_val_if_fail(cwd != NULL, NULL);
367 /* only quick way to check the end of a multi byte string for our
370 separator = 0 == g_strncasecmp(cwd, G_DIR_SEPARATOR_S, strlen(G_DIR_SEPARATOR_S))
374 /* fetch the selected file names */
375 for (sel_list = file_list->selection; sel_list; sel_list = sel_list->next) {
376 gint sel = GPOINTER_TO_INT(sel_list->data);
377 gchar *sel_text = NULL;
380 gtk_clist_get_text(file_list, sel, 0, &sel_text);
381 if (!sel_text) continue;
382 sel_text = g_strdup(sel_text);
385 fname = g_strconcat(cwd, sel_text, NULL);
387 fname = g_strconcat(cwd, G_DIR_SEPARATOR_S, sel_text, NULL);
389 list = g_list_append(list, fname);
397 /* GTK2 : gtk_file_selection_get_selections() returns newly allocated gchar**
398 * all entries have complete path / are non-utf8 / are free()d in caller
401 gchar **file_list, **list_ptr;
403 file_list = gtk_file_selection_get_selections (GTK_FILE_SELECTION(filesel));
405 /* fetch the selected file names */
406 for (list_ptr = file_list; list_ptr && *list_ptr; list_ptr++)
407 list = g_list_append(list, *list_ptr);