4c61bc0424e45c54fa272ee7ca970d4e711d6cbf
[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 gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data);
39 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
40
41 gchar *filesel_select_file(const gchar *title, const gchar *file)
42 {
43         static gchar *filename = NULL;
44         static gchar *cwd = NULL;
45
46         filesel_create(title);
47
48         manage_window_set_transient(GTK_WINDOW(filesel));
49
50         if (filename) {
51                 g_free(filename);
52                 filename = NULL;
53         }
54
55         if (!cwd)
56                 cwd = g_strconcat(startup_dir, G_DIR_SEPARATOR_S, NULL);
57
58         gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), cwd);
59
60         if (file)
61                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),
62                                                 file);
63
64         gtk_widget_show(filesel);
65
66         gtk_main();
67
68         if (filesel_ack) {
69                 gchar *str;
70
71                 str = gtk_file_selection_get_filename
72                         (GTK_FILE_SELECTION(filesel));
73                 if (str && str[0] != '\0') {
74                         gchar *dir;
75
76                         filename = g_strdup(str);
77                         dir = g_dirname(str);
78                         g_free(cwd);
79                         cwd = g_strconcat(dir, G_DIR_SEPARATOR_S, NULL);
80                         g_free(dir);
81                 }
82         }
83
84         manage_window_focus_out(filesel, NULL, NULL);
85         gtk_widget_destroy(filesel);
86         GTK_EVENTS_FLUSH();
87
88         return filename;
89 }
90
91 static void filesel_create(const gchar *title)
92 {
93         filesel = gtk_file_selection_new(title);
94         gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button),
95                            "clicked", GTK_SIGNAL_FUNC(filesel_ok_cb),
96                            NULL);
97         gtk_signal_connect
98                 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button),
99                  "clicked", GTK_SIGNAL_FUNC(filesel_cancel_cb),
100                  NULL);
101         gtk_signal_connect(GTK_OBJECT(filesel), "delete_event",
102                            GTK_SIGNAL_FUNC(delete_event), 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 gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
126 {
127         filesel_cancel_cb(NULL, NULL);
128         return TRUE;
129 }
130
131 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
132 {
133         if (event && event->keyval == GDK_Escape)
134                 filesel_cancel_cb(NULL, NULL);
135 }