1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/filedlg.cpp
3 // Purpose: native implementation of wxFileDialog
4 // Author: Robert Roebling, Zbigniew Zagorski
6 // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski
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"
19 #include "wx/filedlg.h"
22 #include "wx/filename.h"
23 #include "wx/msgdlg.h"
28 #include "wx/gtk/private.h"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern void wxapp_install_idle_handler();
37 //-----------------------------------------------------------------------------
38 // "clicked" for OK-button
39 //-----------------------------------------------------------------------------
42 static void gtk_filedialog_ok_callback(GtkWidget
*widget
, wxFileDialog
*dialog
)
44 int style
= dialog
->GetStyle();
45 gchar
* text
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
46 wxString
filename(wxGTK_CONV_BACK(text
));
48 if ((style
& wxSAVE
) && (style
& wxOVERWRITE_PROMPT
))
50 if (wxFileExists(filename
))
54 _("File '%s' already exists, do you really want to overwrite it?"),
57 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
58 wxYES_NO
| wxICON_QUESTION
);
59 if (dlg
.ShowModal() != wxID_YES
)
63 else if ((style
& wxOPEN
) && ( style
& wxFILE_MUST_EXIST
))
65 if (!wxFileExists( filename
))
67 wxMessageDialog
dlg(dialog
,
68 _("Please choose an existing file."),
69 _("Error"), wxOK
| wxICON_ERROR
);
76 // change to the directory where the user went if asked
77 if (style
& wxCHANGE_DIR
)
80 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
82 if (cwd
!= wxGetCwd())
84 wxSetWorkingDirectory(cwd
);
88 dialog
->SetPath(filename
);
89 dialog
->UpdateFromDialog();
91 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
92 event
.SetEventObject(dialog
);
93 dialog
->GetEventHandler()->ProcessEvent(event
);
97 //-----------------------------------------------------------------------------
98 // "clicked" for Cancel-button
99 //-----------------------------------------------------------------------------
102 static void gtk_filedialog_cancel_callback(GtkWidget
*WXUNUSED(w
),
103 wxFileDialog
*dialog
)
105 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
106 event
.SetEventObject(dialog
);
107 dialog
->GetEventHandler()->ProcessEvent(event
);
112 static void gtk_filedialog_response_callback(GtkWidget
*w
,
114 wxFileDialog
*dialog
)
116 wxapp_install_idle_handler();
118 if (response
== GTK_RESPONSE_ACCEPT
)
119 gtk_filedialog_ok_callback(w
, dialog
);
120 else if (response
== GTK_RESPONSE_CANCEL
)
121 gtk_filedialog_cancel_callback(w
, dialog
);
124 gtk_filedialog_cancel_callback(w
, dialog
);
125 dialog
->m_destroyed_by_delete
= true;
130 #endif // __WXGTK24__
132 //-----------------------------------------------------------------------------
134 //-----------------------------------------------------------------------------
136 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxGenericFileDialog
)
138 BEGIN_EVENT_TABLE(wxFileDialog
,wxGenericFileDialog
)
139 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
142 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
143 const wxString
& defaultDir
,
144 const wxString
& defaultFileName
,
145 const wxString
& wildCard
,
146 long style
, const wxPoint
& pos
)
147 : wxGenericFileDialog(parent
, message
, defaultDir
, defaultFileName
,
148 wildCard
, style
, pos
, true )
151 if (!gtk_check_version(2,4,0))
153 m_needParent
= false;
154 m_destroyed_by_delete
= false;
156 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
157 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
158 wxDefaultValidator
, wxT("filedialog")))
160 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
164 bool multiple
= (style
& wxMULTIPLE
) == wxMULTIPLE
;
165 GtkFileChooserAction gtk_action
;
166 GtkWindow
* gtk_parent
= NULL
;
168 gtk_parent
= GTK_WINDOW(parent
->m_widget
);
171 if ((style
& wxSAVE
) == wxSAVE
)
173 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
174 ok_btn_stock
= GTK_STOCK_SAVE
;
178 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
179 ok_btn_stock
= GTK_STOCK_OPEN
;
181 m_widget
= gtk_file_chooser_dialog_new(
182 wxGTK_CONV(m_message
),
185 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
186 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
189 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), multiple
);
191 gtk_signal_connect(GTK_OBJECT(m_widget
),
193 GTK_SIGNAL_FUNC(gtk_filedialog_response_callback
),
197 if (!m_path
.empty() && m_path
.Last() != wxT('/'))
199 m_path
+= m_fileName
;
202 SetWildcard(wildCard
);
207 wxGenericFileDialog::Create( parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
);
210 wxFileDialog::~wxFileDialog()
213 if (!gtk_check_version(2,4,0))
215 if (m_destroyed_by_delete
)
221 void wxFileDialog::OnFakeOk( wxCommandEvent
&event
)
224 if (!gtk_check_version(2,4,0))
225 wxDialog::OnOK( event
);
228 wxGenericFileDialog::OnListOk( event
);
231 int wxFileDialog::ShowModal()
234 if (!gtk_check_version(2,4,0))
235 return wxDialog::ShowModal();
238 return wxGenericFileDialog::ShowModal();
241 bool wxFileDialog::Show( bool show
)
244 if (!gtk_check_version(2,4,0))
245 return wxDialog::Show( show
);
248 return wxGenericFileDialog::Show( show
);
251 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
254 if (!gtk_check_version(2,4,0))
257 for (size_t n
= 0; n
< files
.GetCount(); n
++ )
260 wxSplitPath(files
[n
], NULL
, &name
, &ext
);
271 wxGenericFileDialog::GetFilenames( files
);
274 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
277 if (!gtk_check_version(2,4,0))
280 if (GetWindowStyle() & wxMULTIPLE
)
283 gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget
));
284 GSList
*gpaths
= gpathsi
;
287 wxString file
= wxGTK_CONV_BACK((gchar
*) gpathsi
->data
);
289 g_free(gpathsi
->data
);
290 gpathsi
= gpathsi
->next
;
293 g_slist_free(gpaths
);
297 paths
.Add(m_fileName
);
302 wxGenericFileDialog::GetPaths( paths
);
305 void wxFileDialog::SetMessage(const wxString
& message
)
308 if (!gtk_check_version(2,4,0))
315 wxGenericFileDialog::SetMessage( message
);
318 void wxFileDialog::SetPath(const wxString
& path
)
321 if (!gtk_check_version(2,4,0))
323 if (path
.empty()) return;
326 m_path
= fn
.GetFullPath();
327 m_dir
= fn
.GetPath();
328 m_fileName
= fn
.GetFullName();
333 wxGenericFileDialog::SetPath( path
);
336 void wxFileDialog::SetDirectory(const wxString
& dir
)
339 if (!gtk_check_version(2,4,0))
341 if (wxPathExists(dir
))
344 m_path
= wxFileName(m_dir
, m_fileName
).GetFullPath();
350 wxGenericFileDialog::SetDirectory( dir
);
353 void wxFileDialog::SetFilename(const wxString
& name
)
356 if (!gtk_check_version(2,4,0))
359 m_path
= wxFileName(m_dir
, m_fileName
).GetFullPath();
364 wxGenericFileDialog::SetFilename( name
);
367 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
370 if (!gtk_check_version(2,4,0))
372 m_wildCard
= wildCard
;
373 GtkFileChooser
* chooser
= GTK_FILE_CHOOSER(m_widget
);
375 // empty current filter list:
376 GSList
* ifilters
= gtk_file_chooser_list_filters(chooser
);
377 GSList
* filters
= ifilters
;
380 gtk_file_chooser_remove_filter(chooser
,GTK_FILE_FILTER(ifilters
->data
));
381 ifilters
= ifilters
->next
;
383 g_slist_free(filters
);
386 wxArrayString wildDescriptions
, wildFilters
;
387 if (!wxParseCommonDialogsFilter(m_wildCard
, wildDescriptions
, wildFilters
))
389 wxFAIL_MSG( wxT("Wrong file type description") );
393 // add parsed to GtkChooser
394 for (size_t n
= 0; n
< wildFilters
.GetCount(); n
++)
396 GtkFileFilter
* filter
= gtk_file_filter_new();
397 gtk_file_filter_set_name(filter
,wxGTK_CONV(wildDescriptions
[n
]));
398 wxString after
= wildFilters
[n
];
401 wxString ext
= after
.BeforeFirst(wxT(';'));
402 gtk_file_filter_add_pattern(filter
,wxGTK_CONV(ext
));
403 if (after
.Find(wxT(';')) == wxNOT_FOUND
)
405 after
= after
.AfterLast(wxT(';'));
407 while (!after
.empty());
409 gtk_file_chooser_add_filter(chooser
, filter
);
415 wxGenericFileDialog::SetWildcard( wildCard
);
418 void wxFileDialog::SetFilterIndex(int filterIndex
)
421 if (!gtk_check_version(2,4,0))
423 m_filterIndex
= filterIndex
;
425 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
426 GSList
*fnode
= gtk_file_chooser_list_filters(chooser
);
427 GSList
*filters
= fnode
;
431 if (i
== filterIndex
)
433 gtk_file_chooser_set_filter(chooser
, GTK_FILE_FILTER(fnode
->data
));
440 g_slist_free(filters
);
444 wxGenericFileDialog::SetFilterIndex( filterIndex
);
447 void wxFileDialog::UpdateDialog()
450 // set currently selected directory to match the path:
451 if (!m_dir
.empty() && wxPathExists(m_dir
))
453 // NB: This is important -- if we set directory only and not the path,
454 // then dialog will still remember old path set using previous
455 // call to gtk_chooser_set_filename. If the previous directory
456 // was a subdirectory of the directory we want to select now,
457 // the dialog would still contain directory selector controls
458 // for the subdirectory (with the parent directory selected),
459 // instead of showing only the parent directory as expected.
460 // This way, we force GtkFileChooser to really change the
461 // directory. Finally, it doesn't have to be done if filename
462 // is not empty because of the code that sets the filename below.
463 if (m_fileName
.empty())
464 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
),
467 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
471 // if the user set only the directory (e.g. by calling SetDirectory)
472 // and not the default filename, then we don't want to set the filename:
473 if (!m_fileName
.empty())
475 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
),
478 // pre-fill the filename when saving, too (there's no text entry
479 // control when opening a file, so it doesn't make sense to
480 // do this when opening files):
481 if (GetWindowStyle() & wxSAVE
)
483 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
484 wxGTK_CONV(m_fileName
));
490 void wxFileDialog::UpdateFromDialog()
493 // update filterIndex
494 GSList
*fnode
= gtk_file_chooser_list_filters(GTK_FILE_CHOOSER(m_widget
));
495 GSList
*filters
= fnode
;
496 GtkFileFilter
*current
=
497 gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(m_widget
));
503 if (fnode
->data
== (gpointer
)current
)
511 g_slist_free(filters
);
515 #endif // wxUSE_FILEDLG