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();
44 //-----------------------------------------------------------------------------
45 // "clicked" for OK-button
46 //-----------------------------------------------------------------------------
49 static void gtk_filedialog_ok_callback(GtkWidget
*widget
, wxFileDialog
*dialog
)
51 int style
= dialog
->GetStyle();
52 gchar
* filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
54 if ((style
& wxSAVE
) && (style
& wxOVERWRITE_PROMPT
))
56 if ( g_file_test(filename
, G_FILE_TEST_EXISTS
) )
61 _("File '%s' already exists, do you really want to overwrite it?"),
62 wxString(wxConvFileName
->cMB2WX(filename
)).c_str());
64 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
65 wxYES_NO
| wxICON_QUESTION
);
66 if (dlg
.ShowModal() != wxID_YES
)
71 // change to the directory where the user went if asked
72 if (style
& wxCHANGE_DIR
)
74 // Use chdir to not care about filename encodings
75 gchar
* folder
= g_path_get_dirname(filename
);
82 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
83 event
.SetEventObject(dialog
);
84 dialog
->GetEventHandler()->ProcessEvent(event
);
88 //-----------------------------------------------------------------------------
89 // "clicked" for Cancel-button
90 //-----------------------------------------------------------------------------
93 static void gtk_filedialog_cancel_callback(GtkWidget
*WXUNUSED(w
),
96 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
97 event
.SetEventObject(dialog
);
98 dialog
->GetEventHandler()->ProcessEvent(event
);
103 static void gtk_filedialog_response_callback(GtkWidget
*w
,
105 wxFileDialog
*dialog
)
107 wxapp_install_idle_handler();
109 if (response
== GTK_RESPONSE_ACCEPT
)
110 gtk_filedialog_ok_callback(w
, dialog
);
111 else if (response
== GTK_RESPONSE_CANCEL
)
112 gtk_filedialog_cancel_callback(w
, dialog
);
115 gtk_filedialog_cancel_callback(w
, dialog
);
116 dialog
->m_destroyed_by_delete
= true;
121 #endif // __WXGTK24__
123 //-----------------------------------------------------------------------------
125 //-----------------------------------------------------------------------------
127 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxGenericFileDialog
)
129 BEGIN_EVENT_TABLE(wxFileDialog
,wxGenericFileDialog
)
130 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
133 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
134 const wxString
& defaultDir
,
135 const wxString
& defaultFileName
,
136 const wxString
& wildCard
,
137 long style
, const wxPoint
& pos
)
138 : wxGenericFileDialog(parent
, message
, defaultDir
, defaultFileName
,
139 wildCard
, style
, pos
, true )
142 if (!gtk_check_version(2,4,0))
144 wxASSERT_MSG( !( (style
& wxSAVE
) && (style
& wxMULTIPLE
) ), wxT("wxFileDialog - wxMULTIPLE used on a save dialog" ) );
145 m_needParent
= false;
146 m_destroyed_by_delete
= false;
148 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
149 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
150 wxDefaultValidator
, wxT("filedialog")))
152 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
156 GtkFileChooserAction gtk_action
;
157 GtkWindow
* gtk_parent
= NULL
;
159 gtk_parent
= GTK_WINDOW(parent
->m_widget
);
162 if ( style
& wxSAVE
)
164 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
165 ok_btn_stock
= GTK_STOCK_SAVE
;
169 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
170 ok_btn_stock
= GTK_STOCK_OPEN
;
173 m_widget
= gtk_file_chooser_dialog_new(
174 wxGTK_CONV(m_message
),
177 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
178 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
181 if ( style
& wxMULTIPLE
)
182 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), true);
184 // local-only property could be set to false to allow non-local files to be loaded.
185 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
186 // and the GtkFileChooserDialog should probably also be created with a backend,
187 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
188 // Currently local-only is kept as the default - true:
189 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
191 g_signal_connect(G_OBJECT(m_widget
), "response",
192 GTK_SIGNAL_FUNC(gtk_filedialog_response_callback
), (gpointer
)this);
194 SetWildcard(wildCard
);
196 if ( style
& wxSAVE
)
198 if ( !defaultDir
.empty() )
199 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
200 wxConvFileName
->cWX2MB(defaultDir
));
202 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
203 wxConvFileName
->cWX2MB(defaultFileName
));
207 if ( !defaultFileName
.empty() )
210 if ( defaultDir
.empty() )
215 gtk_file_chooser_set_filename(
216 GTK_FILE_CHOOSER(m_widget
),
217 wxConvFileName
->cWX2MB( wxFileName(dir
, defaultFileName
).GetFullPath() ) );
219 else if ( !defaultDir
.empty() )
220 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget
),
221 wxConvFileName
->cWX2MB(defaultDir
) );
226 wxGenericFileDialog::Create( parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
);
229 wxFileDialog::~wxFileDialog()
232 if (!gtk_check_version(2,4,0))
234 if (m_destroyed_by_delete
)
240 void wxFileDialog::OnFakeOk( wxCommandEvent
&event
)
243 if (!gtk_check_version(2,4,0))
244 wxDialog::OnOK( event
);
247 wxGenericFileDialog::OnListOk( event
);
250 int wxFileDialog::ShowModal()
253 if (!gtk_check_version(2,4,0))
254 return wxDialog::ShowModal();
257 return wxGenericFileDialog::ShowModal();
260 bool wxFileDialog::Show( bool show
)
263 if (!gtk_check_version(2,4,0))
264 return wxDialog::Show( show
);
267 return wxGenericFileDialog::Show( show
);
270 void wxFileDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
275 wxGenericFileDialog::DoSetSize( x
, y
, width
, height
, sizeFlags
);
278 wxString
wxFileDialog::GetPath() const
281 if (!gtk_check_version(2,4,0))
282 return wxConvFileName
->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
)));
285 return wxGenericFileDialog::GetPath();
288 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
291 if (!gtk_check_version(2,4,0))
294 for (size_t n
= 0; n
< files
.GetCount(); ++n
)
296 wxFileName
file(files
[n
]);
297 files
[n
] = file
.GetFullName();
302 wxGenericFileDialog::GetFilenames( files
);
305 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
308 if (!gtk_check_version(2,4,0))
311 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget
)))
313 GSList
*gpathsi
= gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget
));
314 GSList
*gpaths
= gpathsi
;
317 wxString
file(wxConvFileName
->cMB2WX((gchar
*) gpathsi
->data
));
319 g_free(gpathsi
->data
);
320 gpathsi
= gpathsi
->next
;
323 g_slist_free(gpaths
);
326 paths
.Add(GetPath());
330 wxGenericFileDialog::GetPaths( paths
);
333 void wxFileDialog::SetMessage(const wxString
& message
)
336 if (!gtk_check_version(2,4,0))
343 wxGenericFileDialog::SetMessage( message
);
346 void wxFileDialog::SetPath(const wxString
& path
)
349 if (!gtk_check_version(2,4,0))
351 if (path
.empty()) return;
353 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(path
));
357 wxGenericFileDialog::SetPath( path
);
360 void wxFileDialog::SetDirectory(const wxString
& dir
)
363 if (!gtk_check_version(2,4,0))
365 if (wxDirExists(dir
))
367 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(dir
));
372 wxGenericFileDialog::SetDirectory( dir
);
375 wxString
wxFileDialog::GetDirectory() const
378 if (!gtk_check_version(2,4,0))
379 return wxConvFileName
->cMB2WX(
380 gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER(m_widget
) ) );
383 return wxGenericFileDialog::GetDirectory();
386 void wxFileDialog::SetFilename(const wxString
& name
)
389 if (!gtk_check_version(2,4,0))
391 if (GetStyle() & wxSAVE
)
392 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(name
));
394 SetPath(wxFileName(GetDirectory(), name
).GetFullPath());
398 wxGenericFileDialog::SetFilename( name
);
401 wxString
wxFileDialog::GetFilename() const
404 if (!gtk_check_version(2,4,0))
406 wxConvFileName
->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
))) ).GetFullName();
409 return wxGenericFileDialog::GetFilename();
412 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
415 if (!gtk_check_version(2,4,0))
418 wxArrayString wildDescriptions
, wildFilters
;
419 if (!wxParseCommonDialogsFilter(wildCard
, wildDescriptions
, wildFilters
))
421 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
425 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
426 m_wildCard
= wildCard
;
428 GtkFileChooser
* chooser
= GTK_FILE_CHOOSER(m_widget
);
430 // empty current filter list:
431 GSList
* ifilters
= gtk_file_chooser_list_filters(chooser
);
432 GSList
* filters
= ifilters
;
436 gtk_file_chooser_remove_filter(chooser
,GTK_FILE_FILTER(ifilters
->data
));
437 ifilters
= ifilters
->next
;
439 g_slist_free(filters
);
441 // add parsed to GtkChooser
442 for (size_t n
= 0; n
< wildFilters
.GetCount(); ++n
)
444 GtkFileFilter
* filter
= gtk_file_filter_new();
445 gtk_file_filter_set_name(filter
, wxGTK_CONV(wildDescriptions
[n
]));
447 wxStringTokenizer
exttok(wildFilters
[n
], wxT(";"));
448 while (exttok
.HasMoreTokens())
450 wxString token
= exttok
.GetNextToken();
451 gtk_file_filter_add_pattern(filter
, wxGTK_CONV(token
));
454 gtk_file_chooser_add_filter(chooser
, filter
);
457 // Reset the filter index
463 wxGenericFileDialog::SetWildcard( wildCard
);
466 void wxFileDialog::SetFilterIndex(int filterIndex
)
469 if (!gtk_check_version(2,4,0))
472 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
473 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
475 filter
= g_slist_nth_data(filters
, filterIndex
);
479 gtk_file_chooser_set_filter(chooser
, GTK_FILE_FILTER(filter
));
483 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
486 g_slist_free(filters
);
490 wxGenericFileDialog::SetFilterIndex( filterIndex
);
493 int wxFileDialog::GetFilterIndex() const
496 if (!gtk_check_version(2,4,0))
498 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
499 GtkFileFilter
*filter
= gtk_file_chooser_get_filter(chooser
);
500 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
501 gint index
= g_slist_index(filters
, filter
);
502 g_slist_free(filters
);
506 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
514 return wxGenericFileDialog::GetFilterIndex();
517 #endif // wxUSE_FILEDLG