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