]> icculus.org git repositories - divverent/netradiant.git/blob - libs/gtkutil/filechooser.cpp
correctly support [ and ] keys
[divverent/netradiant.git] / libs / gtkutil / filechooser.cpp
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #include "filechooser.h"
23
24 #include "ifiletypes.h"
25
26 #include <list>
27 #include <vector>
28 #include <gtk/gtkwidget.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtkfilechooser.h>
31 #include <gtk/gtkfilechooserdialog.h>
32 #include <gtk/gtkstock.h>
33
34 #include "string/string.h"
35 #include "stream/stringstream.h"
36 #include "container/array.h"
37 #include "os/path.h"
38 #include "os/file.h"
39
40 #include "messagebox.h"
41
42
43 struct filetype_pair_t
44 {
45   filetype_pair_t()
46     : m_moduleName("")
47   {
48   }
49   filetype_pair_t(const char* moduleName, filetype_t type)
50     : m_moduleName(moduleName), m_type(type)
51   {
52   }
53   const char* m_moduleName;
54   filetype_t m_type;
55 };
56
57 class FileTypeList : public IFileTypeList
58 {
59   struct filetype_copy_t
60   {
61     filetype_copy_t(const filetype_pair_t& other)
62       : m_moduleName(other.m_moduleName), m_name(other.m_type.name), m_pattern(other.m_type.pattern)
63     {
64     }
65     CopiedString m_moduleName;
66     CopiedString m_name;
67     CopiedString m_pattern;
68   };
69
70   typedef std::list<filetype_copy_t> Types;
71   Types m_types;
72 public:
73
74   typedef Types::const_iterator const_iterator;
75   const_iterator begin() const
76   {
77     return m_types.begin();
78   }
79   const_iterator end() const
80   {
81     return m_types.end();
82   }
83
84   std::size_t size() const
85   {
86     return m_types.size();
87   }
88
89   void addType(const char* moduleName, filetype_t type)
90   {
91     m_types.push_back(filetype_pair_t(moduleName, type));
92   }
93 };
94
95
96 class GTKMasks
97 {
98   const FileTypeList& m_types;
99 public:
100   std::vector<CopiedString> m_filters;
101   std::vector<CopiedString> m_masks;
102
103   GTKMasks(const FileTypeList& types) : m_types(types)
104   {
105     m_masks.reserve(m_types.size());
106     for(FileTypeList::const_iterator i = m_types.begin(); i != m_types.end(); ++i)
107     {
108       std::size_t len = strlen((*i).m_name.c_str()) + strlen((*i).m_pattern.c_str()) + 3;
109       StringOutputStream buffer(len + 1); // length + null char
110
111       buffer << (*i).m_name.c_str() << " <" << (*i).m_pattern.c_str() << ">";
112
113       m_masks.push_back(buffer.c_str());
114     }
115
116     m_filters.reserve(m_types.size());
117     for(FileTypeList::const_iterator i = m_types.begin(); i != m_types.end(); ++i)
118     {
119       m_filters.push_back((*i).m_pattern);
120     }
121   }
122
123   filetype_pair_t GetTypeForGTKMask(const char *mask) const
124   {
125     std::vector<CopiedString>::const_iterator j = m_masks.begin();
126     for(FileTypeList::const_iterator i = m_types.begin(); i != m_types.end(); ++i, ++j)
127     {
128       if(string_equal((*j).c_str(), mask))
129       {
130         return filetype_pair_t((*i).m_moduleName.c_str(), filetype_t((*i).m_name.c_str(), (*i).m_pattern.c_str()));
131       }
132     }
133     return filetype_pair_t();
134   }
135
136 };
137
138 static char g_file_dialog_file[1024];
139
140 const char* file_dialog_show(GtkWidget* parent, bool open, const char* title, const char* path, const char* pattern)
141 {
142   filetype_t type;
143
144   if(pattern == 0)
145   {
146     pattern = "*";
147   }
148
149   FileTypeList typelist;
150   GlobalFiletypes().getTypeList(pattern, &typelist);
151
152   GTKMasks masks(typelist);
153
154   if (title == 0)
155     title = open ? "Open File" : "Save File";
156     
157   GtkWidget* dialog;
158   if (open)
159   {
160     dialog = gtk_file_chooser_dialog_new(title,
161                                         GTK_WINDOW(parent),
162                                         GTK_FILE_CHOOSER_ACTION_OPEN,
163                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
164                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
165                                         NULL);
166   }
167   else
168   {
169     dialog = gtk_file_chooser_dialog_new(title,
170                                         GTK_WINDOW(parent),
171                                         GTK_FILE_CHOOSER_ACTION_SAVE,
172                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
173                                         GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
174                                         NULL);
175     gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "unnamed");
176   }
177
178   gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
179   gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
180
181   // we expect an actual path below, if the path is 0 we might crash
182   if (path != 0 && !string_empty(path))
183   {
184     ASSERT_MESSAGE(path_is_absolute(path), "file_dialog_show: path not absolute: " << makeQuoted(path));
185
186     Array<char> new_path(strlen(path)+1);
187
188     // copy path, replacing dir separators as appropriate
189     Array<char>::iterator w = new_path.begin();
190     for(const char* r = path; *r != '\0'; ++r)
191     {
192       *w++ = (*r == '/') ? G_DIR_SEPARATOR : *r;
193     }
194     // remove separator from end of path if required
195     if(*(w-1) == G_DIR_SEPARATOR)
196     {
197       --w;
198     }
199     // terminate string
200     *w = '\0';
201
202     gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), new_path.data());
203   }
204
205   // we should add all important paths as shortcut folder...
206   // gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog), "/tmp/", NULL);
207
208   
209   for(std::size_t i = 0; i < masks.m_filters.size(); ++i)
210   {
211     GtkFileFilter* filter = gtk_file_filter_new();
212     gtk_file_filter_add_pattern(filter, masks.m_filters[i].c_str());
213     gtk_file_filter_set_name(filter, masks.m_masks[i].c_str());
214     gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
215   }
216
217   if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
218   {
219     strcpy(g_file_dialog_file, gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)));
220
221     if(!string_equal(pattern, "*"))
222     {
223       GtkFileFilter* filter = gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog));
224       if(filter != 0) // no filter set? some file-chooser implementations may allow the user to set no filter, which we treat as 'all files'
225       {
226         type = masks.GetTypeForGTKMask(gtk_file_filter_get_name(filter)).m_type;
227         // last ext separator
228         const char* extension = path_get_extension(g_file_dialog_file);
229         // no extension
230         if(string_empty(extension))
231         {
232           strcat(g_file_dialog_file, type.pattern+1);
233         }
234         else
235         {
236           strcpy(g_file_dialog_file + (extension - g_file_dialog_file), type.pattern+2);
237         }
238       }
239     }
240
241     // convert back to unix format
242     for(char* w = g_file_dialog_file; *w!='\0'; w++)
243     {
244       if(*w=='\\') 
245       {
246         *w = '/';
247       }
248     }
249   }
250   else
251   {
252     g_file_dialog_file[0] = '\0';
253   }
254
255   gtk_widget_destroy(dialog);
256
257   // don't return an empty filename
258   if(g_file_dialog_file[0] == '\0') return NULL;
259
260   return g_file_dialog_file;
261 }
262
263 char* dir_dialog(GtkWidget* parent, const char* title, const char* path)
264 {
265   GtkWidget* dialog = gtk_file_chooser_dialog_new(title,
266                                         GTK_WINDOW(parent),
267                                         GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
268                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
269                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
270                                         NULL);
271
272   gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
273   gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
274
275   if(!string_empty(path))
276   {
277     gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), path);
278   }
279
280   char* filename = 0;
281   if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
282   {
283     filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
284   }
285
286   gtk_widget_destroy(dialog);
287
288   return filename;
289 }
290
291 const char* file_dialog(GtkWidget* parent, bool open, const char* title, const char* path, const char* pattern)
292 {
293   for(;;)
294   {
295     const char* file = file_dialog_show(parent, open, title, path, pattern);
296
297     if(open
298       || file == 0
299       || !file_exists(file)
300       || gtk_MessageBox(parent, "The file specified already exists.\nDo you want to replace it?", title, eMB_NOYES, eMB_ICONQUESTION) == eIDYES)
301     {
302       return file;
303     }
304   }
305 }