1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/filedlg.cpp
3 // Purpose: native implementation of wxFileDialog
4 // Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp
6 // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "filedlggtk.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
17 // Include setup.h to get wxUSE flags for compilers that do not support precompilation of headers
22 #include "wx/filedlg.h"
27 #include "wx/gtk/private.h"
29 #include <unistd.h> // chdir
32 #include "wx/filename.h" // wxFilename
33 #include "wx/tokenzr.h" // wxStringTokenizer
34 #include "wx/filefn.h" // ::wxGetCwd
35 #include "wx/msgdlg.h" // wxMessageDialog
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 extern void wxapp_install_idle_handler();
43 //-----------------------------------------------------------------------------
44 // "clicked" for OK-button
45 //-----------------------------------------------------------------------------
48 static void gtk_filedialog_ok_callback(GtkWidget
*widget
, wxFileDialog
*dialog
)
50 int style
= dialog
->GetStyle();
51 gchar
* filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
53 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
54 #if GTK_CHECK_VERSION(2,7,3)
55 if(gtk_check_version(2,7,3) != NULL
)
57 if ((style
& wxSAVE
) && (style
& wxOVERWRITE_PROMPT
))
59 if ( g_file_test(filename
, G_FILE_TEST_EXISTS
) )
64 _("File '%s' already exists, do you really want to overwrite it?"),
65 wxString(wxConvFileName
->cMB2WX(filename
)).c_str());
67 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
68 wxYES_NO
| wxICON_QUESTION
);
69 if (dlg
.ShowModal() != wxID_YES
)
74 // change to the directory where the user went if asked
75 if (style
& wxCHANGE_DIR
)
77 // Use chdir to not care about filename encodings
78 gchar
* folder
= g_path_get_dirname(filename
);
85 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
86 event
.SetEventObject(dialog
);
87 dialog
->GetEventHandler()->ProcessEvent(event
);
91 //-----------------------------------------------------------------------------
92 // "clicked" for Cancel-button
93 //-----------------------------------------------------------------------------
96 static void gtk_filedialog_cancel_callback(GtkWidget
*WXUNUSED(w
),
99 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
100 event
.SetEventObject(dialog
);
101 dialog
->GetEventHandler()->ProcessEvent(event
);
106 static void gtk_filedialog_response_callback(GtkWidget
*w
,
108 wxFileDialog
*dialog
)
110 wxapp_install_idle_handler();
112 if (response
== GTK_RESPONSE_ACCEPT
)
113 gtk_filedialog_ok_callback(w
, dialog
);
114 else if (response
== GTK_RESPONSE_CANCEL
)
115 gtk_filedialog_cancel_callback(w
, dialog
);
118 gtk_filedialog_cancel_callback(w
, dialog
);
119 dialog
->m_destroyed_by_delete
= true;
124 #endif // __WXGTK24__
126 //-----------------------------------------------------------------------------
128 //-----------------------------------------------------------------------------
130 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxGenericFileDialog
)
132 BEGIN_EVENT_TABLE(wxFileDialog
,wxGenericFileDialog
)
133 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
136 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
137 const wxString
& defaultDir
,
138 const wxString
& defaultFileName
,
139 const wxString
& wildCard
,
140 long style
, const wxPoint
& pos
)
141 : wxGenericFileDialog(parent
, message
, defaultDir
, defaultFileName
,
142 wildCard
, style
, pos
, true )
145 if (!gtk_check_version(2,4,0))
147 wxASSERT_MSG( !( (style
& wxSAVE
) && (style
& wxMULTIPLE
) ), wxT("wxFileDialog - wxMULTIPLE used on a save dialog" ) );
148 m_needParent
= false;
149 m_destroyed_by_delete
= false;
151 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
152 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
153 wxDefaultValidator
, wxT("filedialog")))
155 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
159 GtkFileChooserAction gtk_action
;
160 GtkWindow
* gtk_parent
= NULL
;
162 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
165 if ( style
& wxSAVE
)
167 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
168 ok_btn_stock
= GTK_STOCK_SAVE
;
172 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
173 ok_btn_stock
= GTK_STOCK_OPEN
;
176 m_widget
= gtk_file_chooser_dialog_new(
177 wxGTK_CONV(m_message
),
180 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
181 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
184 if ( style
& wxMULTIPLE
)
185 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), true);
187 // local-only property could be set to false to allow non-local files to be loaded.
188 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
189 // and the GtkFileChooserDialog should probably also be created with a backend,
190 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
191 // Currently local-only is kept as the default - true:
192 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
194 g_signal_connect(G_OBJECT(m_widget
), "response",
195 GTK_SIGNAL_FUNC(gtk_filedialog_response_callback
), (gpointer
)this);
197 SetWildcard(wildCard
);
199 if ( style
& wxSAVE
)
201 if ( !defaultDir
.empty() )
202 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
203 wxConvFileName
->cWX2MB(defaultDir
));
205 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
206 wxConvFileName
->cWX2MB(defaultFileName
));
208 #if GTK_CHECK_VERSION(2,7,3)
209 if (!gtk_check_version(2,7,3))
210 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget
), TRUE
);
215 if ( !defaultFileName
.empty() )
218 if ( defaultDir
.empty() )
223 gtk_file_chooser_set_filename(
224 GTK_FILE_CHOOSER(m_widget
),
225 wxConvFileName
->cWX2MB( wxFileName(dir
, defaultFileName
).GetFullPath() ) );
227 else if ( !defaultDir
.empty() )
228 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget
),
229 wxConvFileName
->cWX2MB(defaultDir
) );
234 wxGenericFileDialog::Create( parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
);
237 wxFileDialog::~wxFileDialog()
240 if (!gtk_check_version(2,4,0))
242 if (m_destroyed_by_delete
)
248 void wxFileDialog::OnFakeOk( wxCommandEvent
&event
)
251 if (!gtk_check_version(2,4,0))
252 wxDialog::OnOK( event
);
255 wxGenericFileDialog::OnListOk( event
);
258 int wxFileDialog::ShowModal()
261 if (!gtk_check_version(2,4,0))
262 return wxDialog::ShowModal();
265 return wxGenericFileDialog::ShowModal();
268 bool wxFileDialog::Show( bool show
)
271 if (!gtk_check_version(2,4,0))
272 return wxDialog::Show( show
);
275 return wxGenericFileDialog::Show( show
);
278 void wxFileDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
283 wxGenericFileDialog::DoSetSize( x
, y
, width
, height
, sizeFlags
);
286 wxString
wxFileDialog::GetPath() const
289 if (!gtk_check_version(2,4,0))
290 return wxConvFileName
->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
)));
293 return wxGenericFileDialog::GetPath();
296 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
299 if (!gtk_check_version(2,4,0))
302 for (size_t n
= 0; n
< files
.GetCount(); ++n
)
304 wxFileName
file(files
[n
]);
305 files
[n
] = file
.GetFullName();
310 wxGenericFileDialog::GetFilenames( files
);
313 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
316 if (!gtk_check_version(2,4,0))
319 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget
)))
321 GSList
*gpathsi
= gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget
));
322 GSList
*gpaths
= gpathsi
;
325 wxString
file(wxConvFileName
->cMB2WX((gchar
*) gpathsi
->data
));
327 g_free(gpathsi
->data
);
328 gpathsi
= gpathsi
->next
;
331 g_slist_free(gpaths
);
334 paths
.Add(GetPath());
338 wxGenericFileDialog::GetPaths( paths
);
341 void wxFileDialog::SetMessage(const wxString
& message
)
344 if (!gtk_check_version(2,4,0))
351 wxGenericFileDialog::SetMessage( message
);
354 void wxFileDialog::SetPath(const wxString
& path
)
357 if (!gtk_check_version(2,4,0))
359 if (path
.empty()) return;
361 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(path
));
365 wxGenericFileDialog::SetPath( path
);
368 void wxFileDialog::SetDirectory(const wxString
& dir
)
371 if (!gtk_check_version(2,4,0))
373 if (wxDirExists(dir
))
375 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(dir
));
380 wxGenericFileDialog::SetDirectory( dir
);
383 wxString
wxFileDialog::GetDirectory() const
386 if (!gtk_check_version(2,4,0))
387 return wxConvFileName
->cMB2WX(
388 gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER(m_widget
) ) );
391 return wxGenericFileDialog::GetDirectory();
394 void wxFileDialog::SetFilename(const wxString
& name
)
397 if (!gtk_check_version(2,4,0))
399 if (GetStyle() & wxSAVE
)
400 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(name
));
402 SetPath(wxFileName(GetDirectory(), name
).GetFullPath());
406 wxGenericFileDialog::SetFilename( name
);
409 wxString
wxFileDialog::GetFilename() const
412 if (!gtk_check_version(2,4,0))
414 wxConvFileName
->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
))) ).GetFullName();
417 return wxGenericFileDialog::GetFilename();
420 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
423 if (!gtk_check_version(2,4,0))
426 wxArrayString wildDescriptions
, wildFilters
;
427 if (!wxParseCommonDialogsFilter(wildCard
, wildDescriptions
, wildFilters
))
429 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
433 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
434 m_wildCard
= wildCard
;
436 GtkFileChooser
* chooser
= GTK_FILE_CHOOSER(m_widget
);
438 // empty current filter list:
439 GSList
* ifilters
= gtk_file_chooser_list_filters(chooser
);
440 GSList
* filters
= ifilters
;
444 gtk_file_chooser_remove_filter(chooser
,GTK_FILE_FILTER(ifilters
->data
));
445 ifilters
= ifilters
->next
;
447 g_slist_free(filters
);
449 // add parsed to GtkChooser
450 for (size_t n
= 0; n
< wildFilters
.GetCount(); ++n
)
452 GtkFileFilter
* filter
= gtk_file_filter_new();
453 gtk_file_filter_set_name(filter
, wxGTK_CONV(wildDescriptions
[n
]));
455 wxStringTokenizer
exttok(wildFilters
[n
], wxT(";"));
456 while (exttok
.HasMoreTokens())
458 wxString token
= exttok
.GetNextToken();
459 gtk_file_filter_add_pattern(filter
, wxGTK_CONV(token
));
462 gtk_file_chooser_add_filter(chooser
, filter
);
465 // Reset the filter index
471 wxGenericFileDialog::SetWildcard( wildCard
);
474 void wxFileDialog::SetFilterIndex(int filterIndex
)
477 if (!gtk_check_version(2,4,0))
480 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
481 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
483 filter
= g_slist_nth_data(filters
, filterIndex
);
487 gtk_file_chooser_set_filter(chooser
, GTK_FILE_FILTER(filter
));
491 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
494 g_slist_free(filters
);
498 wxGenericFileDialog::SetFilterIndex( filterIndex
);
501 int wxFileDialog::GetFilterIndex() const
504 if (!gtk_check_version(2,4,0))
506 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
507 GtkFileFilter
*filter
= gtk_file_chooser_get_filter(chooser
);
508 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
509 gint index
= g_slist_index(filters
, filter
);
510 g_slist_free(filters
);
514 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
522 return wxGenericFileDialog::GetFilterIndex();
525 #endif // wxUSE_FILEDLG