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