]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/filedlg.cpp
allow customization of individual grid lines appearance (patch 1496015)
[wxWidgets.git] / src / gtk / filedlg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
9b5f1895 2// Name: src/gtk/filedlg.cpp
9755e73b 3// Purpose: native implementation of wxFileDialog
f8bc53eb 4// Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp
a81258be 5// Id: $Id$
f8bc53eb 6// Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
ff3e84ff 13#if wxUSE_FILEDLG && defined(__WXGTK24__)
9755e73b 14
c801d85f 15#include "wx/filedlg.h"
4e1901b7 16
88a7a4e1
WS
17#ifndef WX_PRECOMP
18 #include "wx/intl.h"
246c5004 19 #include "wx/msgdlg.h"
88a7a4e1
WS
20#endif
21
f8bc53eb 22#include <gtk/gtk.h>
9755e73b 23#include "wx/gtk/private.h"
83624f79 24
f8bc53eb
JS
25#include <unistd.h> // chdir
26
f8bc53eb
JS
27#include "wx/filename.h" // wxFilename
28#include "wx/tokenzr.h" // wxStringTokenizer
29#include "wx/filefn.h" // ::wxGetCwd
f8bc53eb 30
acfd422a
RR
31//-----------------------------------------------------------------------------
32// idle system
33//-----------------------------------------------------------------------------
34
35extern void wxapp_install_idle_handler();
acfd422a 36
291a8f20
RR
37//-----------------------------------------------------------------------------
38// "clicked" for OK-button
c801d85f
KB
39//-----------------------------------------------------------------------------
40
865bb325 41extern "C" {
9755e73b 42static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog)
c801d85f 43{
ff3e84ff 44 int style = dialog->GetWindowStyle();
f8bc53eb 45 gchar* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
035b704a 46
b5ab6d19
MR
47 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
48#if GTK_CHECK_VERSION(2,7,3)
49 if(gtk_check_version(2,7,3) != NULL)
50#endif
ff3e84ff 51 if ((style & wxFD_SAVE) && (style & wxFD_OVERWRITE_PROMPT))
bfc6fde4 52 {
f8bc53eb 53 if ( g_file_test(filename, G_FILE_TEST_EXISTS) )
0e1399b3
VZ
54 {
55 wxString msg;
f8bc53eb 56
9755e73b 57 msg.Printf(
f8bc53eb
JS
58 _("File '%s' already exists, do you really want to overwrite it?"),
59 wxString(wxConvFileName->cMB2WX(filename)).c_str());
0e1399b3 60
9755e73b
VS
61 wxMessageDialog dlg(dialog, msg, _("Confirm"),
62 wxYES_NO | wxICON_QUESTION);
63 if (dlg.ShowModal() != wxID_YES)
0e1399b3
VZ
64 return;
65 }
83624f79 66 }
035b704a 67
3f6638b8 68 // change to the directory where the user went if asked
ff3e84ff 69 if (style & wxFD_CHANGE_DIR)
3f6638b8 70 {
f8bc53eb
JS
71 // Use chdir to not care about filename encodings
72 gchar* folder = g_path_get_dirname(filename);
73 chdir(folder);
74 g_free(folder);
3f6638b8
VZ
75 }
76
f8bc53eb 77 g_free(filename);
27b2dd53 78
bfc6fde4 79 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
9755e73b
VS
80 event.SetEventObject(dialog);
81 dialog->GetEventHandler()->ProcessEvent(event);
ff7b1510 82}
865bb325 83}
c801d85f 84
291a8f20
RR
85//-----------------------------------------------------------------------------
86// "clicked" for Cancel-button
87//-----------------------------------------------------------------------------
88
865bb325 89extern "C" {
a552d120 90static void gtk_filedialog_cancel_callback(GtkWidget *w, wxFileDialog *dialog)
c801d85f 91{
bfc6fde4 92 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
9755e73b
VS
93 event.SetEventObject(dialog);
94 dialog->GetEventHandler()->ProcessEvent(event);
95}
865bb325 96}
9755e73b 97
865bb325 98extern "C" {
9755e73b 99static void gtk_filedialog_response_callback(GtkWidget *w,
f8bc53eb 100 gint response,
9755e73b
VS
101 wxFileDialog *dialog)
102{
103 wxapp_install_idle_handler();
27b2dd53 104
a7334928 105 if (response == GTK_RESPONSE_ACCEPT)
9755e73b 106 gtk_filedialog_ok_callback(w, dialog);
a552d120 107 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
c2740a5a 108 gtk_filedialog_cancel_callback(w, dialog);
ff7b1510 109}
865bb325
VZ
110}
111
c801d85f 112
291a8f20
RR
113//-----------------------------------------------------------------------------
114// wxFileDialog
115//-----------------------------------------------------------------------------
116
4e1901b7
RR
117IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxGenericFileDialog)
118
119BEGIN_EVENT_TABLE(wxFileDialog,wxGenericFileDialog)
f8bc53eb 120 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
4e1901b7 121END_EVENT_TABLE()
c801d85f 122
9755e73b
VS
123wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
124 const wxString& defaultDir,
125 const wxString& defaultFileName,
126 const wxString& wildCard,
ff3e84ff
VZ
127 long style, const wxPoint& pos,
128 const wxSize& sz,
129 const wxString& name)
4e1901b7 130 : wxGenericFileDialog(parent, message, defaultDir, defaultFileName,
ff3e84ff 131 wildCard, style, pos, sz, name, true )
c801d85f 132{
77f70672
RR
133 if (!gtk_check_version(2,4,0))
134 {
27b2dd53 135 m_needParent = false;
83624f79 136
77f70672
RR
137 if (!PreCreation(parent, pos, wxDefaultSize) ||
138 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
9755e73b 139 wxDefaultValidator, wxT("filedialog")))
77f70672
RR
140 {
141 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
142 return;
143 }
3f6638b8 144
77f70672
RR
145 GtkFileChooserAction gtk_action;
146 GtkWindow* gtk_parent = NULL;
147 if (parent)
29a7bba6 148 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
27b2dd53 149
fbfb8bcc 150 const gchar* ok_btn_stock;
ff3e84ff 151 if ( style & wxFD_SAVE )
77f70672
RR
152 {
153 gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
154 ok_btn_stock = GTK_STOCK_SAVE;
155 }
156 else
157 {
158 gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
159 ok_btn_stock = GTK_STOCK_OPEN;
160 }
f8bc53eb 161
77f70672 162 m_widget = gtk_file_chooser_dialog_new(
9755e73b
VS
163 wxGTK_CONV(m_message),
164 gtk_parent,
165 gtk_action,
166 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
167 ok_btn_stock, GTK_RESPONSE_ACCEPT,
168 NULL);
169
c1dfe97c
MR
170 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
171
ff3e84ff 172 if ( style & wxFD_MULTIPLE )
f8bc53eb 173 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget), true);
27b2dd53 174
a552d120
VZ
175 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically destroys
176 // the dialog when the user press ESC on the dialog: in that case a second call to
177 // ShowModal() would result in a bunch of Gtk-CRITICAL errors...
178 g_signal_connect (G_OBJECT(m_widget),
179 "delete_event",
180 G_CALLBACK (gtk_widget_hide_on_delete),
181 (gpointer)this);
182
f8bc53eb
JS
183 // local-only property could be set to false to allow non-local files to be loaded.
184 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
185 // and the GtkFileChooserDialog should probably also be created with a backend,
186 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
187 // Currently local-only is kept as the default - true:
188 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
27b2dd53 189
3fe39b0c
MR
190 g_signal_connect (m_widget, "response",
191 G_CALLBACK (gtk_filedialog_response_callback), this);
27b2dd53 192
77f70672 193 SetWildcard(wildCard);
f8bc53eb 194
ff3e84ff 195 if ( style & wxFD_SAVE )
f8bc53eb 196 {
da865fdd 197 if ( !defaultDir.empty() )
f8bc53eb
JS
198 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
199 wxConvFileName->cWX2MB(defaultDir));
200
201 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget),
202 wxConvFileName->cWX2MB(defaultFileName));
b5ab6d19
MR
203
204#if GTK_CHECK_VERSION(2,7,3)
e031f1df 205 if ((style & wxFD_OVERWRITE_PROMPT) && !gtk_check_version(2,7,3))
b5ab6d19
MR
206 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget), TRUE);
207#endif
f8bc53eb
JS
208 }
209 else
210 {
da865fdd 211 if ( !defaultFileName.empty() )
f8bc53eb
JS
212 {
213 wxString dir;
da865fdd 214 if ( defaultDir.empty() )
f8bc53eb
JS
215 dir = ::wxGetCwd();
216 else
217 dir = defaultDir;
218
219 gtk_file_chooser_set_filename(
220 GTK_FILE_CHOOSER(m_widget),
221 wxConvFileName->cWX2MB( wxFileName(dir, defaultFileName).GetFullPath() ) );
222 }
da865fdd 223 else if ( !defaultDir.empty() )
f8bc53eb
JS
224 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget),
225 wxConvFileName->cWX2MB(defaultDir) );
226 }
77f70672
RR
227 }
228 else
77f70672 229 wxGenericFileDialog::Create( parent, message, defaultDir, defaultFileName, wildCard, style, pos );
9755e73b 230}
0e1399b3 231
4e1901b7
RR
232void wxFileDialog::OnFakeOk( wxCommandEvent &event )
233{
77f70672
RR
234 if (!gtk_check_version(2,4,0))
235 wxDialog::OnOK( event );
236 else
77f70672 237 wxGenericFileDialog::OnListOk( event );
4e1901b7
RR
238}
239
240int wxFileDialog::ShowModal()
241{
77f70672
RR
242 if (!gtk_check_version(2,4,0))
243 return wxDialog::ShowModal();
244 else
77f70672 245 return wxGenericFileDialog::ShowModal();
4e1901b7
RR
246}
247
248bool wxFileDialog::Show( bool show )
249{
77f70672
RR
250 if (!gtk_check_version(2,4,0))
251 return wxDialog::Show( show );
252 else
77f70672 253 return wxGenericFileDialog::Show( show );
9755e73b 254}
0e1399b3 255
5b2e23bf
RR
256void wxFileDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags )
257{
258 if (!m_wxwindow)
259 return;
260 else
261 wxGenericFileDialog::DoSetSize( x, y, width, height, sizeFlags );
262}
263
f8bc53eb
JS
264wxString wxFileDialog::GetPath() const
265{
f8bc53eb
JS
266 if (!gtk_check_version(2,4,0))
267 return wxConvFileName->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget)));
268 else
f8bc53eb
JS
269 return wxGenericFileDialog::GetPath();
270}
271
27b2dd53 272void wxFileDialog::GetFilenames(wxArrayString& files) const
9755e73b 273{
77f70672 274 if (!gtk_check_version(2,4,0))
9755e73b 275 {
77f70672 276 GetPaths(files);
f8bc53eb 277 for (size_t n = 0; n < files.GetCount(); ++n )
9755e73b 278 {
f8bc53eb
JS
279 wxFileName file(files[n]);
280 files[n] = file.GetFullName();
9755e73b 281 }
9755e73b 282 }
77f70672 283 else
77f70672 284 wxGenericFileDialog::GetFilenames( files );
9755e73b 285}
76840ed0 286
27b2dd53 287void wxFileDialog::GetPaths(wxArrayString& paths) const
9755e73b 288{
77f70672 289 if (!gtk_check_version(2,4,0))
9755e73b 290 {
27b2dd53 291 paths.Empty();
f8bc53eb 292 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget)))
9755e73b 293 {
f8bc53eb 294 GSList *gpathsi = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget));
77f70672
RR
295 GSList *gpaths = gpathsi;
296 while (gpathsi)
297 {
f8bc53eb
JS
298 wxString file(wxConvFileName->cMB2WX((gchar*) gpathsi->data));
299 paths.Add(file);
77f70672
RR
300 g_free(gpathsi->data);
301 gpathsi = gpathsi->next;
302 }
0832aaaf 303
f8bc53eb 304 g_slist_free(gpaths);
9755e73b 305 }
f8bc53eb
JS
306 else
307 paths.Add(GetPath());
9755e73b
VS
308 }
309 else
77f70672 310 wxGenericFileDialog::GetPaths( paths );
9755e73b 311}
035b704a 312
9755e73b
VS
313void wxFileDialog::SetMessage(const wxString& message)
314{
77f70672
RR
315 if (!gtk_check_version(2,4,0))
316 {
317 m_message = message;
318 SetTitle(message);
319 }
320 else
77f70672 321 wxGenericFileDialog::SetMessage( message );
9755e73b
VS
322}
323
76840ed0
RR
324void wxFileDialog::SetPath(const wxString& path)
325{
77f70672
RR
326 if (!gtk_check_version(2,4,0))
327 {
328 if (path.empty()) return;
329
f8bc53eb 330 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(path));
77f70672
RR
331 }
332 else
77f70672 333 wxGenericFileDialog::SetPath( path );
76840ed0
RR
334}
335
9755e73b
VS
336void wxFileDialog::SetDirectory(const wxString& dir)
337{
77f70672 338 if (!gtk_check_version(2,4,0))
76840ed0 339 {
da865fdd 340 if (wxDirExists(dir))
77f70672 341 {
f8bc53eb 342 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(dir));
77f70672 343 }
76840ed0 344 }
77f70672 345 else
77f70672 346 wxGenericFileDialog::SetDirectory( dir );
9755e73b
VS
347}
348
f8bc53eb
JS
349wxString wxFileDialog::GetDirectory() const
350{
f8bc53eb
JS
351 if (!gtk_check_version(2,4,0))
352 return wxConvFileName->cMB2WX(
353 gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER(m_widget) ) );
354 else
f8bc53eb
JS
355 return wxGenericFileDialog::GetDirectory();
356}
357
9755e73b
VS
358void wxFileDialog::SetFilename(const wxString& name)
359{
77f70672
RR
360 if (!gtk_check_version(2,4,0))
361 {
ff3e84ff 362 if (HasFlag(wxFD_SAVE))
f8bc53eb
JS
363 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(name));
364 else
365 SetPath(wxFileName(GetDirectory(), name).GetFullPath());
77f70672
RR
366 }
367 else
77f70672 368 wxGenericFileDialog::SetFilename( name );
9755e73b 369}
035b704a 370
f8bc53eb
JS
371wxString wxFileDialog::GetFilename() const
372{
f8bc53eb
JS
373 if (!gtk_check_version(2,4,0))
374 return wxFileName(
375 wxConvFileName->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget))) ).GetFullName();
376 else
f8bc53eb
JS
377 return wxGenericFileDialog::GetFilename();
378}
379
9755e73b
VS
380void wxFileDialog::SetWildcard(const wxString& wildCard)
381{
77f70672 382 if (!gtk_check_version(2,4,0))
9755e73b 383 {
77f70672
RR
384 // parse filters
385 wxArrayString wildDescriptions, wildFilters;
f8bc53eb 386 if (!wxParseCommonDialogsFilter(wildCard, wildDescriptions, wildFilters))
77f70672 387 {
f8bc53eb 388 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
77f70672
RR
389 }
390 else
9755e73b 391 {
f8bc53eb
JS
392 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
393 m_wildCard = wildCard;
394
395 GtkFileChooser* chooser = GTK_FILE_CHOOSER(m_widget);
396
397 // empty current filter list:
398 GSList* ifilters = gtk_file_chooser_list_filters(chooser);
399 GSList* filters = ifilters;
400
401 while (ifilters)
402 {
403 gtk_file_chooser_remove_filter(chooser,GTK_FILE_FILTER(ifilters->data));
404 ifilters = ifilters->next;
405 }
406 g_slist_free(filters);
407
77f70672 408 // add parsed to GtkChooser
f8bc53eb 409 for (size_t n = 0; n < wildFilters.GetCount(); ++n)
9755e73b 410 {
77f70672 411 GtkFileFilter* filter = gtk_file_filter_new();
f8bc53eb
JS
412 gtk_file_filter_set_name(filter, wxGTK_CONV(wildDescriptions[n]));
413
414 wxStringTokenizer exttok(wildFilters[n], wxT(";"));
415 while (exttok.HasMoreTokens())
77f70672 416 {
f8bc53eb
JS
417 wxString token = exttok.GetNextToken();
418 gtk_file_filter_add_pattern(filter, wxGTK_CONV(token));
77f70672 419 }
27b2dd53 420
77f70672
RR
421 gtk_file_chooser_add_filter(chooser, filter);
422 }
f8bc53eb
JS
423
424 // Reset the filter index
425 SetFilterIndex(0);
9755e73b
VS
426 }
427 }
77f70672 428 else
77f70672 429 wxGenericFileDialog::SetWildcard( wildCard );
9755e73b 430}
a3622daa 431
9755e73b
VS
432void wxFileDialog::SetFilterIndex(int filterIndex)
433{
ff3e84ff 434
77f70672 435 if (!gtk_check_version(2,4,0))
9755e73b 436 {
f8bc53eb 437 gpointer filter;
77f70672 438 GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget);
f8bc53eb
JS
439 GSList *filters = gtk_file_chooser_list_filters(chooser);
440
441 filter = g_slist_nth_data(filters, filterIndex);
442
443 if (filter != NULL)
9755e73b 444 {
f8bc53eb
JS
445 gtk_file_chooser_set_filter(chooser, GTK_FILE_FILTER(filter));
446 }
447 else
448 {
449 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
9755e73b 450 }
f8bc53eb 451
77f70672 452 g_slist_free(filters);
9755e73b 453 }
77f70672 454 else
77f70672 455 wxGenericFileDialog::SetFilterIndex( filterIndex );
4e1901b7
RR
456}
457
f8bc53eb 458int wxFileDialog::GetFilterIndex() const
4e1901b7 459{
f8bc53eb 460 if (!gtk_check_version(2,4,0))
4e1901b7 461 {
f8bc53eb
JS
462 GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget);
463 GtkFileFilter *filter = gtk_file_chooser_get_filter(chooser);
464 GSList *filters = gtk_file_chooser_list_filters(chooser);
465 gint index = g_slist_index(filters, filter);
466 g_slist_free(filters);
3502e687 467
f8bc53eb 468 if (index == -1)
9755e73b 469 {
f8bc53eb
JS
470 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
471 return 0;
9755e73b 472 }
f8bc53eb
JS
473 else
474 return index;
9755e73b 475 }
f8bc53eb 476 else
f8bc53eb 477 return wxGenericFileDialog::GetFilterIndex();
9755e73b 478}
3f6638b8 479
ff3e84ff 480#endif // wxUSE_FILEDLG && __WXGTK24__