27093fc55d3956a0d2394d06317c41ba70a74546
[claws.git] / src / filesel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 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
27 #include "main.h"
28 #include "filesel.h"
29 #include "manage_window.h"
30 #include "gtkutils.h"
31
32 static GtkWidget *filesel;
33 static gboolean filesel_ack;
34
35 static void filesel_create(const gchar *title);
36 static void filesel_ok_cb(GtkWidget *widget, gpointer data);
37 static void filesel_cancel_cb(GtkWidget *widget, gpointer data);
38 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
39
40 gchar *filesel_select_file(const gchar *title, const gchar *file)
41 {
42         static gchar *filename = NULL;
43         static gchar *cwd = NULL;
44
45         filesel_create(title);
46
47         manage_window_set_transient(GTK_WINDOW(filesel));
48
49         if (filename) {
50                 g_free(filename);
51                 filename = NULL;
52         }
53
54         if (!cwd)
55                 cwd = g_strconcat(startup_dir, G_DIR_SEPARATOR_S, NULL);
56
57         gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), cwd);
58
59         if (file)
60                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),
61                                                 file);
62
63         gtk_widget_show(filesel);
64
65         gtk_main();
66
67         if (filesel_ack) {
68                 gchar *str;
69
70                 str = gtk_file_selection_get_filename
71                         (GTK_FILE_SELECTION(filesel));
72                 if (str && str[0] != '\0') {
73                         gchar *dir;
74
75                         filename = g_strdup(str);
76                         dir = g_dirname(str);
77                         g_free(cwd);
78                         cwd = g_strconcat(dir, G_DIR_SEPARATOR_S, NULL);
79                         g_free(dir);
80                 }
81         }
82
83         manage_window_focus_out(filesel, NULL, NULL);
84         gtk_widget_destroy(filesel);
85         GTK_EVENTS_FLUSH();
86
87         return filename;
88 }
89
90 static void filesel_create(const gchar *title)
91 {
92         filesel = gtk_file_selection_new(title);
93         gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button),
94                            "clicked", GTK_SIGNAL_FUNC(filesel_ok_cb),
95                            NULL);
96         gtk_signal_connect
97                 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button),
98                  "clicked", GTK_SIGNAL_FUNC(filesel_cancel_cb),
99                  NULL);
100         gtk_signal_connect(GTK_OBJECT(filesel), "delete_event",
101                            GTK_SIGNAL_FUNC(filesel_cancel_cb),
102                            NULL);
103         gtk_signal_connect(GTK_OBJECT(filesel), "key_press_event",
104                            GTK_SIGNAL_FUNC(key_pressed), NULL);
105         gtk_signal_connect(GTK_OBJECT(filesel), "focus_in_event",
106                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
107         gtk_signal_connect(GTK_OBJECT(filesel), "focus_out_event",
108                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
109
110         gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
111 }
112
113 static void filesel_ok_cb(GtkWidget *widget, gpointer data)
114 {
115         filesel_ack = TRUE;
116         gtk_main_quit();
117 }
118
119 static void filesel_cancel_cb(GtkWidget *widget, gpointer data)
120 {
121         filesel_ack = FALSE;
122         gtk_main_quit();
123 }
124
125 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
126 {
127         if (event && event->keyval == GDK_Escape)
128                 filesel_cancel_cb(NULL, NULL);
129 }