add Summary->Next/Prev labeled message
[claws.git] / src / filesel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include <glib.h>
21 #include <gdk/gdkkeysyms.h>
22 #include <gtk/gtkwidget.h>
23 #include <gtk/gtkfilesel.h>
24 #include <gtk/gtkmain.h>
25 #include <gtk/gtksignal.h>
26 #include <gtk/gtkeditable.h>
27 #include <gtk/gtkentry.h>
28
29 #include "main.h"
30 #include "filesel.h"
31 #include "manage_window.h"
32 #include "gtkutils.h"
33
34 static GtkWidget *filesel;
35 static gboolean filesel_ack;
36 static gchar *filesel_oldfilename = NULL;
37
38 static void filesel_create(const gchar *title, gboolean multiple_files);
39 static void filesel_ok_cb(GtkWidget *widget, gpointer data);
40 static void filesel_cancel_cb(GtkWidget *widget, gpointer data);
41 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data);
42 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
43
44 static void filesel_file_list_select_row_multi(GtkCList *clist, gint row, gint col,
45                                                GdkEventButton *event, gpointer userdata);
46 static void filesel_file_list_select_row_single(GtkCList *clist, gint row, gint col,
47                                                 GdkEventButton *event, gpointer userdata);
48
49 static void filesel_dir_list_select_row_multi(GtkCList *clist, gint row, gint col,
50                                               GdkEventButton *event, gpointer userdata);
51 static void filesel_dir_list_select_row_single(GtkCList *clist, gint row, gint col,
52                                                GdkEventButton *event, gpointer userdata);
53
54 static GList *filesel_get_multiple_filenames(void);
55
56 gchar *filesel_select_file(const gchar *title, const gchar *file)
57 {
58         static gchar *filename = NULL;
59         static gchar *cwd = NULL;
60
61         filesel_create(title, FALSE);
62
63         manage_window_set_transient(GTK_WINDOW(filesel));
64
65         if (filename) {
66                 g_free(filename);
67                 filename = NULL;
68         }
69
70         if (!cwd)
71                 cwd = g_strconcat(startup_dir, G_DIR_SEPARATOR_S, NULL);
72
73         gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), cwd);
74
75         if (file) {
76                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),
77                                                 file);
78                 filesel_oldfilename = g_strdup(file);
79         }
80
81         gtk_widget_show(filesel);
82
83         gtk_main();
84
85         if (filesel_ack) {
86                 gchar *str;
87
88                 str = gtk_file_selection_get_filename
89                         (GTK_FILE_SELECTION(filesel));
90                 if (str && str[0] != '\0') {
91                         gchar *dir;
92
93                         filename = g_strdup(str);
94                         dir = g_dirname(str);
95                         g_free(cwd);
96                         cwd = g_strconcat(dir, G_DIR_SEPARATOR_S, NULL);
97                         g_free(dir);
98                 }
99         }
100
101         manage_window_focus_out(filesel, NULL, NULL);
102         gtk_widget_destroy(filesel);
103         GTK_EVENTS_FLUSH();
104
105         return filename;
106 }
107
108 GList *filesel_select_multiple_files(const gchar *title, const gchar *file)
109 {
110         /* ALF - sorry for the exuberant code duping... need to 
111          * be cleaned up. */
112         static gchar *filename = NULL;
113         static gchar *cwd = NULL;
114         GList        *list = NULL;
115
116         filesel_create(title, TRUE);
117
118         manage_window_set_transient(GTK_WINDOW(filesel));
119
120         if (filename) {
121                 g_free(filename);
122                 filename = NULL;
123         }
124
125         if (!cwd)
126                 cwd = g_strconcat(startup_dir, G_DIR_SEPARATOR_S, NULL);
127
128         gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), cwd);
129
130         if (file)
131                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),
132                                                 file);
133         gtk_widget_show(filesel);
134
135         gtk_main();
136
137         if (filesel_ack)
138                 list = filesel_get_multiple_filenames();                
139
140         manage_window_focus_out(filesel, NULL, NULL);
141         gtk_widget_destroy(filesel);
142         GTK_EVENTS_FLUSH();
143
144         return list;
145 }
146
147 static void filesel_create(const gchar *title, gboolean multiple_files)
148 {
149         filesel = gtk_file_selection_new(title);
150         gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button),
151                            "clicked", GTK_SIGNAL_FUNC(filesel_ok_cb),
152                            NULL);
153         gtk_signal_connect
154                 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button),
155                  "clicked", GTK_SIGNAL_FUNC(filesel_cancel_cb),
156                  NULL);
157         gtk_signal_connect(GTK_OBJECT(filesel), "delete_event",
158                            GTK_SIGNAL_FUNC(delete_event), NULL);
159         gtk_signal_connect(GTK_OBJECT(filesel), "key_press_event",
160                            GTK_SIGNAL_FUNC(key_pressed), NULL);
161         gtk_signal_connect(GTK_OBJECT(filesel), "focus_in_event",
162                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
163         gtk_signal_connect(GTK_OBJECT(filesel), "focus_out_event",
164                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
165
166         gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
167
168         if (multiple_files) {
169                 gtk_clist_set_selection_mode
170                         (GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list),
171                          GTK_SELECTION_MULTIPLE);
172                 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
173                                    "select_row", 
174                                    GTK_SIGNAL_FUNC(filesel_file_list_select_row_multi),
175                                    NULL);
176                 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
177                                    "unselect_row",
178                                    GTK_SIGNAL_FUNC(filesel_file_list_select_row_multi),
179                                    NULL);
180                 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->dir_list),
181                                    "select_row",
182                                    GTK_SIGNAL_FUNC(filesel_dir_list_select_row_multi),
183                                    NULL);
184         } else {
185                 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
186                                    "select_row", 
187                                    GTK_SIGNAL_FUNC(filesel_file_list_select_row_single),
188                                    NULL);
189                 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->dir_list),
190                                    "select_row",
191                                    GTK_SIGNAL_FUNC(filesel_dir_list_select_row_single),
192                                    NULL);
193         }
194 }
195
196 static void filesel_ok_cb(GtkWidget *widget, gpointer data)
197 {
198         filesel_ack = TRUE;
199         g_free(filesel_oldfilename);
200         gtk_main_quit();
201 }
202
203 static void filesel_cancel_cb(GtkWidget *widget, gpointer data)
204 {
205         filesel_ack = FALSE;
206         g_free(filesel_oldfilename);
207         gtk_main_quit();
208 }
209
210 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
211 {
212         filesel_cancel_cb(NULL, NULL);
213         g_free(filesel_oldfilename);
214         return TRUE;
215 }
216
217 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
218 {
219         if (event && event->keyval == GDK_Escape)
220                 filesel_cancel_cb(NULL, NULL);
221 }
222
223 /* handle both "select_row" and "unselect_row". note that we're using the
224  * entry box to put there the selected file names in. we're not using these
225  * entry box to get the selected file names. instead we use the clist selection.
226  * the entry box is used only to retrieve dir name. */
227 static void filesel_file_list_select_row_multi(GtkCList *clist, gint row, gint col,
228                                                GdkEventButton *event, gpointer userdata)
229 {
230         /* simple implementation in which we clear the file entry and refill it */
231         GList    *list  = clist->selection;
232         GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
233
234         gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
235
236 #define INVALID_FILENAME_CHARS     " "
237         for (; list; list = list->next) {
238                 gint row = GPOINTER_TO_INT(list->data);
239                 gchar *text = NULL, *tmp;
240
241                 if (!gtk_clist_get_text(clist, row, 0, &text))
242                         break;
243
244                 /* NOTE: quick glance in source code of GtkCList
245                  * reveals we should not free the returned 'text' */
246                 
247                 tmp = g_strconcat(text, " ", NULL);
248                 text = tmp;
249                 gtk_entry_append_text(entry, text); 
250                 g_free(text);
251         }
252 #undef INVALID_FILENAME_CHARS
253 }
254
255 static void filesel_dir_list_select_row_multi(GtkCList *clist, gint row, gint col,
256                                               GdkEventButton *event, gpointer userdata)
257 {
258         GtkEntry *entry     = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
259         GtkCList *file_list = GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list);
260
261         /* if dir list is selected we clean everything */
262         gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
263         gtk_clist_unselect_all(file_list);
264 }
265
266 static void filesel_file_list_select_row_single(GtkCList *clist, gint row, gint col,
267                                                 GdkEventButton *event, gpointer userdata)
268 {
269         GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
270
271         g_free(filesel_oldfilename);
272         filesel_oldfilename = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
273 }
274
275 static void filesel_dir_list_select_row_single(GtkCList *clist, gint row, gint col,
276                                                GdkEventButton *event, gpointer userdata)
277 {
278         GtkEntry *entry     = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
279
280         gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
281         if(filesel_oldfilename) {
282                 gtk_entry_append_text(entry, filesel_oldfilename);
283         }
284 }
285
286 static GList *filesel_get_multiple_filenames(void)
287 {
288         /* as noted before we are not using the entry text when selecting
289          * multiple files. to much hassle to parse out invalid chars (chars
290          * that need to be escaped). instead we use the file_list. the
291          * entry is only useful for extracting the current directory. */
292         GtkCList *file_list  = GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list);
293         GtkEntry *file_entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
294         GList    *list = NULL, *sel_list;
295         gchar    *cwd, *tmp;     
296         gboolean  separator;
297
298         g_return_val_if_fail(file_list->selection != NULL, NULL);
299
300         tmp = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel));
301         tmp = g_strdup(tmp);
302         cwd = g_dirname(tmp);
303         g_free(tmp);
304         g_return_val_if_fail(cwd != NULL, NULL);
305
306         /* only quick way to check the end of a multi byte string for our
307          * separator... */
308         g_strreverse(cwd);
309         separator = 0 == g_strncasecmp(cwd, G_DIR_SEPARATOR_S, strlen(G_DIR_SEPARATOR_S))
310                 ?  TRUE : FALSE;
311         g_strreverse(cwd);
312
313         /* fetch the selected file names */
314         for (sel_list = file_list->selection; sel_list; sel_list = sel_list->next) {
315                 gint   sel = GPOINTER_TO_INT(sel_list->data);
316                 gchar *sel_text = NULL;
317                 gchar *fname = NULL;
318                 
319                 gtk_clist_get_text(file_list, sel, 0, &sel_text);
320                 if (!sel_text) continue;
321                 sel_text = g_strdup(sel_text);
322
323                 if (separator)
324                         fname = g_strconcat(cwd, sel_text, NULL);
325                 else
326                         fname = g_strconcat(cwd, G_DIR_SEPARATOR_S, sel_text, NULL);
327                 
328                 list = g_list_append(list, fname);
329                 g_free(sel_text);
330         }
331         
332         return list;
333 }
334