Merge remote-tracking branch 'github/master'
[xonotic/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         filetype_pair_t( const char* moduleName, filetype_t type )
49                 : m_moduleName( moduleName ), m_type( type ){
50         }
51         const char* m_moduleName;
52         filetype_t m_type;
53 };
54
55 class FileTypeList : public IFileTypeList
56 {
57 struct filetype_copy_t
58 {
59         filetype_copy_t( const filetype_pair_t& other )
60                 : m_moduleName( other.m_moduleName ), m_name( other.m_type.name ), m_pattern( other.m_type.pattern ){
61         }
62         CopiedString m_moduleName;
63         CopiedString m_name;
64         CopiedString m_pattern;
65 };
66
67 typedef std::list<filetype_copy_t> Types;
68 Types m_types;
69 public:
70
71 typedef Types::const_iterator const_iterator;
72 const_iterator begin() const {
73         return m_types.begin();
74 }
75 const_iterator end() const {
76         return m_types.end();
77 }
78
79 std::size_t size() const {
80         return m_types.size();
81 }
82
83 void addType( const char* moduleName, filetype_t type ){
84         m_types.push_back( filetype_pair_t( moduleName, type ) );
85 }
86 };
87
88
89 class GTKMasks
90 {
91 const FileTypeList& m_types;
92 public:
93 std::vector<CopiedString> m_filters;
94 std::vector<CopiedString> m_masks;
95
96 GTKMasks( const FileTypeList& types ) : m_types( types ){
97         m_masks.reserve( m_types.size() );
98         for ( FileTypeList::const_iterator i = m_types.begin(); i != m_types.end(); ++i )
99         {
100                 std::size_t len = strlen( ( *i ).m_name.c_str() ) + strlen( ( *i ).m_pattern.c_str() ) + 3;
101                 StringOutputStream buffer( len + 1 ); // length + null char
102
103                 buffer << ( *i ).m_name.c_str() << " <" << ( *i ).m_pattern.c_str() << ">";
104
105                 m_masks.push_back( buffer.c_str() );
106         }
107
108         m_filters.reserve( m_types.size() );
109         for ( FileTypeList::const_iterator i = m_types.begin(); i != m_types.end(); ++i )
110         {
111                 m_filters.push_back( ( *i ).m_pattern );
112         }
113 }
114
115 filetype_pair_t GetTypeForGTKMask( const char *mask ) const {
116         std::vector<CopiedString>::const_iterator j = m_masks.begin();
117         for ( FileTypeList::const_iterator i = m_types.begin(); i != m_types.end(); ++i, ++j )
118         {
119                 if ( string_equal( ( *j ).c_str(), mask ) ) {
120                         return filetype_pair_t( ( *i ).m_moduleName.c_str(), filetype_t( ( *i ).m_name.c_str(), ( *i ).m_pattern.c_str() ) );
121                 }
122         }
123         return filetype_pair_t();
124 }
125
126 };
127
128 static char g_file_dialog_file[1024];
129
130 const char* file_dialog_show( GtkWidget* parent, bool open, const char* title, const char* path, const char* pattern, bool want_load, bool want_import, bool want_save ){
131         filetype_t type;
132
133         if ( pattern == 0 ) {
134                 pattern = "*";
135         }
136
137         FileTypeList typelist;
138         GlobalFiletypes().getTypeList( pattern, &typelist, want_load, want_import, want_save );
139
140         GTKMasks masks( typelist );
141
142         if ( title == 0 ) {
143                 title = open ? "Open File" : "Save File";
144         }
145
146         GtkWidget* dialog;
147         if ( open ) {
148                 dialog = gtk_file_chooser_dialog_new( title,
149                                                                                           GTK_WINDOW( parent ),
150                                                                                           GTK_FILE_CHOOSER_ACTION_OPEN,
151                                                                                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
152                                                                                           GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
153                                                                                           NULL );
154         }
155         else
156         {
157                 dialog = gtk_file_chooser_dialog_new( title,
158                                                                                           GTK_WINDOW( parent ),
159                                                                                           GTK_FILE_CHOOSER_ACTION_SAVE,
160                                                                                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
161                                                                                           GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
162                                                                                           NULL );
163                 gtk_file_chooser_set_current_name( GTK_FILE_CHOOSER( dialog ), "unnamed" );
164         }
165
166         gtk_window_set_modal( GTK_WINDOW( dialog ), TRUE );
167         gtk_window_set_position( GTK_WINDOW( dialog ), GTK_WIN_POS_CENTER_ON_PARENT );
168
169         // we expect an actual path below, if the path is 0 we might crash
170         if ( path != 0 && !string_empty( path ) ) {
171                 ASSERT_MESSAGE( path_is_absolute( path ), "file_dialog_show: path not absolute: " << makeQuoted( path ) );
172
173                 Array<char> new_path( strlen( path ) + 1 );
174
175                 // copy path, replacing dir separators as appropriate
176                 Array<char>::iterator w = new_path.begin();
177                 for ( const char* r = path; *r != '\0'; ++r )
178                 {
179                         *w++ = ( *r == '/' ) ? G_DIR_SEPARATOR : *r;
180                 }
181                 // remove separator from end of path if required
182                 if ( *( w - 1 ) == G_DIR_SEPARATOR ) {
183                         --w;
184                 }
185                 // terminate string
186                 *w = '\0';
187
188                 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER( dialog ), new_path.data() );
189         }
190
191         // we should add all important paths as shortcut folder...
192         // gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog), "/tmp/", NULL);
193
194
195         for ( std::size_t i = 0; i < masks.m_filters.size(); ++i )
196         {
197                 GtkFileFilter* filter = gtk_file_filter_new();
198                 gtk_file_filter_add_pattern( filter, masks.m_filters[i].c_str() );
199                 gtk_file_filter_set_name( filter, masks.m_masks[i].c_str() );
200                 gtk_file_chooser_add_filter( GTK_FILE_CHOOSER( dialog ), filter );
201         }
202
203         if ( gtk_dialog_run( GTK_DIALOG( dialog ) ) == GTK_RESPONSE_ACCEPT ) {
204                 strcpy( g_file_dialog_file, gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( dialog ) ) );
205
206                 if ( !string_equal( pattern, "*" ) ) {
207                         GtkFileFilter* filter = gtk_file_chooser_get_filter( GTK_FILE_CHOOSER( dialog ) );
208                         if ( filter != 0 ) { // no filter set? some file-chooser implementations may allow the user to set no filter, which we treat as 'all files'
209                                 type = masks.GetTypeForGTKMask( gtk_file_filter_get_name( filter ) ).m_type;
210                                 // last ext separator
211                                 const char* extension = path_get_extension( g_file_dialog_file );
212                                 // no extension
213                                 if ( string_empty( extension ) ) {
214                                         strcat( g_file_dialog_file, type.pattern + 1 );
215                                 }
216                                 else
217                                 {
218                                         strcpy( g_file_dialog_file + ( extension - g_file_dialog_file ), type.pattern + 2 );
219                                 }
220                         }
221                 }
222
223                 // convert back to unix format
224                 for ( char* w = g_file_dialog_file; *w != '\0'; w++ )
225                 {
226                         if ( *w == '\\' ) {
227                                 *w = '/';
228                         }
229                 }
230         }
231         else
232         {
233                 g_file_dialog_file[0] = '\0';
234         }
235
236         gtk_widget_destroy( dialog );
237
238         // don't return an empty filename
239         if ( g_file_dialog_file[0] == '\0' ) {
240                 return NULL;
241         }
242
243         return g_file_dialog_file;
244 }
245
246 char* dir_dialog( GtkWidget* parent, const char* title, const char* path ){
247         GtkWidget* dialog = gtk_file_chooser_dialog_new( title,
248                                                                                                          GTK_WINDOW( parent ),
249                                                                                                          GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
250                                                                                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
251                                                                                                          GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
252                                                                                                          NULL );
253
254         gtk_window_set_modal( GTK_WINDOW( dialog ), TRUE );
255         gtk_window_set_position( GTK_WINDOW( dialog ), GTK_WIN_POS_CENTER_ON_PARENT );
256
257         if ( !string_empty( path ) ) {
258                 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER( dialog ), path );
259         }
260
261         char* filename = 0;
262         if ( gtk_dialog_run( GTK_DIALOG( dialog ) ) == GTK_RESPONSE_ACCEPT ) {
263                 filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( dialog ) );
264         }
265
266         gtk_widget_destroy( dialog );
267
268         return filename;
269 }
270
271 const char* file_dialog( GtkWidget* parent, bool open, const char* title, const char* path, const char* pattern, bool want_load, bool want_import, bool want_save ){
272         for (;; )
273         {
274                 const char* file = file_dialog_show( parent, open, title, path, pattern, want_load, want_import, want_save );
275
276                 if ( open
277                          || file == 0
278                          || !file_exists( file )
279                          || gtk_MessageBox( parent, "The file specified already exists.\nDo you want to replace it?", title, eMB_NOYES, eMB_ICONQUESTION ) == eIDYES ) {
280                         return file;
281                 }
282         }
283 }