Statistics
| Revision:

root / src / filesel.c @ 1

History | View | Annotate | Download (3.88 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2003 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/gtkentry.h>
25
#include <gtk/gtkmain.h>
26
#include <gtk/gtksignal.h>
27

    
28
#include "main.h"
29
#include "filesel.h"
30
#include "manage_window.h"
31
#include "gtkutils.h"
32

    
33
static GtkWidget *filesel;
34
static gboolean filesel_ack;
35
static gboolean filesel_fin;
36

    
37
static void filesel_create(const gchar *title);
38
static void filesel_ok_cb(GtkWidget *widget, gpointer data);
39
static void filesel_cancel_cb(GtkWidget *widget, gpointer data);
40
static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data);
41
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
42

    
43
gchar *filesel_select_file(const gchar *title, const gchar *file)
44
{
45
        static gchar *filename = NULL;
46
        static gchar *cwd = NULL;
47

    
48
        filesel_create(title);
49

    
50
        manage_window_set_transient(GTK_WINDOW(filesel));
51

    
52
        if (filename) {
53
                g_free(filename);
54
                filename = NULL;
55
        }
56

    
57
        if (!cwd)
58
                cwd = g_strconcat(startup_dir, G_DIR_SEPARATOR_S, NULL);
59

    
60
        gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), cwd);
61

    
62
        if (file) {
63
                gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),
64
                                                file);
65
                gtk_editable_select_region
66
                        (GTK_EDITABLE(GTK_FILE_SELECTION(filesel)->selection_entry),
67
                         0, -1);
68
        }
69

    
70
        gtk_widget_show(filesel);
71

    
72
        filesel_ack = filesel_fin = FALSE;
73

    
74
        while (filesel_fin == FALSE)
75
                gtk_main_iteration();
76

    
77
        if (filesel_ack) {
78
                const gchar *str;
79

    
80
                str = gtk_file_selection_get_filename
81
                        (GTK_FILE_SELECTION(filesel));
82
                if (str && str[0] != '\0') {
83
                        gchar *dir;
84

    
85
                        filename = g_strdup(str);
86
                        dir = g_dirname(str);
87
                        g_free(cwd);
88
                        cwd = g_strconcat(dir, G_DIR_SEPARATOR_S, NULL);
89
                        g_free(dir);
90
                }
91
        }
92

    
93
        manage_window_focus_out(filesel, NULL, NULL);
94
        gtk_widget_destroy(filesel);
95
        GTK_EVENTS_FLUSH();
96

    
97
        return filename;
98
}
99

    
100
static void filesel_create(const gchar *title)
101
{
102
        filesel = gtk_file_selection_new(title);
103
        gtk_window_set_position(GTK_WINDOW(filesel), GTK_WIN_POS_CENTER);
104
        gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
105
        gtk_window_set_wmclass
106
                (GTK_WINDOW(filesel), "file_selection", "Sylpheed");
107

    
108
        g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button),
109
                         "clicked", G_CALLBACK(filesel_ok_cb),
110
                         NULL);
111
        g_signal_connect
112
                (G_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button),
113
                 "clicked", G_CALLBACK(filesel_cancel_cb),
114
                 NULL);
115
        g_signal_connect(G_OBJECT(filesel), "delete_event",
116
                         G_CALLBACK(delete_event), NULL);
117
        g_signal_connect(G_OBJECT(filesel), "key_press_event",
118
                         G_CALLBACK(key_pressed), NULL);
119
        MANAGE_WINDOW_SIGNALS_CONNECT(filesel);
120
}
121

    
122
static void filesel_ok_cb(GtkWidget *widget, gpointer data)
123
{
124
        filesel_ack = TRUE;
125
        filesel_fin = TRUE;
126
}
127

    
128
static void filesel_cancel_cb(GtkWidget *widget, gpointer data)
129
{
130
        filesel_ack = FALSE;
131
        filesel_fin = TRUE;
132
}
133

    
134
static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
135
{
136
        filesel_cancel_cb(NULL, NULL);
137
        return TRUE;
138
}
139

    
140
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
141
{
142
        if (event && event->keyval == GDK_Escape)
143
                filesel_cancel_cb(NULL, NULL);
144
        return FALSE;
145
}