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 "filedlg.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 //-----------------------------------------------------------------------------
41 static void gtk_filedialog_ok_callback(GtkWidget
*widget
, wxFileDialog
*dialog
)
43 int style
= dialog
->GetStyle();
44 gchar
* text
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
45 wxString
filename(wxGTK_CONV_BACK(text
));
47 if ((style
& wxSAVE
) && (style
& wxOVERWRITE_PROMPT
))
49 if (wxFileExists(filename
))
53 _("File '%s' already exists, do you really want to overwrite it?"),
56 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
57 wxYES_NO
| wxICON_QUESTION
);
58 if (dlg
.ShowModal() != wxID_YES
)
62 else if ((style
& wxOPEN
) && ( style
& wxFILE_MUST_EXIST
))
64 if (!wxFileExists( filename
))
66 wxMessageDialog
dlg(dialog
,
67 _("Please choose an existing file."),
68 _("Error"), wxOK
| wxICON_ERROR
);
75 // change to the directory where the user went if asked
76 if (style
& wxCHANGE_DIR
)
79 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
81 if (cwd
!= wxGetCwd())
83 wxSetWorkingDirectory(cwd
);
87 dialog
->SetPath(filename
);
88 dialog
->UpdateFromDialog();
90 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
91 event
.SetEventObject(dialog
);
92 dialog
->GetEventHandler()->ProcessEvent(event
);
95 //-----------------------------------------------------------------------------
96 // "clicked" for Cancel-button
97 //-----------------------------------------------------------------------------
99 static void gtk_filedialog_cancel_callback(GtkWidget
*WXUNUSED(w
),
100 wxFileDialog
*dialog
)
102 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
103 event
.SetEventObject(dialog
);
104 dialog
->GetEventHandler()->ProcessEvent(event
);
107 static void gtk_filedialog_response_callback(GtkWidget
*w
,
109 wxFileDialog
*dialog
)
111 wxapp_install_idle_handler();
113 if (response
== GTK_RESPONSE_ACCEPT
)
114 gtk_filedialog_ok_callback(w
, dialog
);
115 else if (response
== GTK_RESPONSE_CANCEL
)
116 gtk_filedialog_cancel_callback(w
, dialog
);
119 gtk_filedialog_cancel_callback(w
, dialog
);
120 dialog
->m_destroyed_by_delete
= TRUE
;
125 //-----------------------------------------------------------------------------
127 //-----------------------------------------------------------------------------
129 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxGenericFileDialog
)
131 BEGIN_EVENT_TABLE(wxFileDialog
,wxGenericFileDialog
)
132 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
135 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
136 const wxString
& defaultDir
,
137 const wxString
& defaultFileName
,
138 const wxString
& wildCard
,
139 long style
, const wxPoint
& pos
)
140 : wxGenericFileDialog(parent
, message
, defaultDir
, defaultFileName
,
141 wildCard
, style
, pos
, true )
144 if (!gtk_check_version(2,4,0))
146 m_needParent
= FALSE
;
147 m_destroyed_by_delete
= FALSE
;
149 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
150 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
151 wxDefaultValidator
, wxT("filedialog")))
153 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
157 bool multiple
= (style
& wxMULTIPLE
) == wxMULTIPLE
;
158 GtkFileChooserAction gtk_action
;
159 GtkWindow
* gtk_parent
= NULL
;
161 gtk_parent
= GTK_WINDOW(parent
->m_widget
);
164 if ((style
& wxSAVE
) == wxSAVE
)
166 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
167 ok_btn_stock
= GTK_STOCK_SAVE
;
171 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
172 ok_btn_stock
= GTK_STOCK_OPEN
;
174 m_widget
= gtk_file_chooser_dialog_new(
175 wxGTK_CONV(m_message
),
178 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
179 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
182 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), multiple
);
184 gtk_signal_connect(GTK_OBJECT(m_widget
),
186 GTK_SIGNAL_FUNC(gtk_filedialog_response_callback
),
190 if (!m_path
.empty() && m_path
.Last() != wxT('/'))
192 m_path
+= m_fileName
;
195 SetWildcard(wildCard
);
200 wxGenericFileDialog::Create( parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
);
203 wxFileDialog::~wxFileDialog()
206 if (!gtk_check_version(2,4,0))
208 if (m_destroyed_by_delete
)
214 void wxFileDialog::OnFakeOk( wxCommandEvent
&event
)
217 if (!gtk_check_version(2,4,0))
218 wxDialog::OnOK( event
);
221 wxGenericFileDialog::OnListOk( event
);
224 int wxFileDialog::ShowModal()
227 if (!gtk_check_version(2,4,0))
228 return wxDialog::ShowModal();
231 return wxGenericFileDialog::ShowModal();
234 bool wxFileDialog::Show( bool show
)
237 if (!gtk_check_version(2,4,0))
238 return wxDialog::Show( show
);
241 return wxGenericFileDialog::Show( show
);
244 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
247 if (!gtk_check_version(2,4,0))
250 for (size_t n
= 0; n
< files
.GetCount(); n
++ )
253 wxSplitPath(files
[n
], NULL
, &name
, &ext
);
264 wxGenericFileDialog::GetFilenames( files
);
267 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
270 if (!gtk_check_version(2,4,0))
273 if (GetWindowStyle() & wxMULTIPLE
)
276 gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget
));
277 GSList
*gpaths
= gpathsi
;
280 wxString file
= wxGTK_CONV_BACK((gchar
*) gpathsi
->data
);
282 g_free(gpathsi
->data
);
283 gpathsi
= gpathsi
->next
;
286 g_slist_free(gpaths
);
290 paths
.Add(m_fileName
);
295 wxGenericFileDialog::GetPaths( paths
);
298 void wxFileDialog::SetMessage(const wxString
& message
)
301 if (!gtk_check_version(2,4,0))
308 wxGenericFileDialog::SetMessage( message
);
311 void wxFileDialog::SetPath(const wxString
& path
)
314 if (!gtk_check_version(2,4,0))
316 if (path
.empty()) return;
319 m_path
= fn
.GetFullPath();
320 m_dir
= fn
.GetPath();
321 m_fileName
= fn
.GetFullName();
326 wxGenericFileDialog::SetPath( path
);
329 void wxFileDialog::SetDirectory(const wxString
& dir
)
332 if (!gtk_check_version(2,4,0))
334 if (wxDirExists(dir
))
337 m_path
= wxFileName(m_dir
, m_fileName
).GetFullPath();
343 wxGenericFileDialog::SetDirectory( dir
);
346 void wxFileDialog::SetFilename(const wxString
& name
)
349 if (!gtk_check_version(2,4,0))
352 m_path
= wxFileName(m_dir
, m_fileName
).GetFullPath();
357 wxGenericFileDialog::SetFilename( name
);
360 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
363 if (!gtk_check_version(2,4,0))
365 m_wildCard
= wildCard
;
366 GtkFileChooser
* chooser
= GTK_FILE_CHOOSER(m_widget
);
368 // empty current filter list:
369 GSList
* ifilters
= gtk_file_chooser_list_filters(chooser
);
370 GSList
* filters
= ifilters
;
373 gtk_file_chooser_remove_filter(chooser
,GTK_FILE_FILTER(ifilters
->data
));
374 ifilters
= ifilters
->next
;
376 g_slist_free(filters
);
379 wxArrayString wildDescriptions
, wildFilters
;
380 if (!wxParseCommonDialogsFilter(m_wildCard
, wildDescriptions
, wildFilters
))
382 wxFAIL_MSG( wxT("Wrong file type description") );
386 // add parsed to GtkChooser
387 for (size_t n
= 0; n
< wildFilters
.GetCount(); n
++)
389 GtkFileFilter
* filter
= gtk_file_filter_new();
390 gtk_file_filter_set_name(filter
,wxGTK_CONV(wildDescriptions
[n
]));
391 wxString after
= wildFilters
[n
];
394 wxString ext
= after
.BeforeFirst(wxT(';'));
395 gtk_file_filter_add_pattern(filter
,wxGTK_CONV(ext
));
396 if (after
.Find(wxT(';')) == wxNOT_FOUND
)
398 after
= after
.AfterLast(wxT(';'));
400 while (!after
.empty());
402 gtk_file_chooser_add_filter(chooser
, filter
);
408 wxGenericFileDialog::SetWildcard( wildCard
);
411 void wxFileDialog::SetFilterIndex(int filterIndex
)
414 if (!gtk_check_version(2,4,0))
416 m_filterIndex
= filterIndex
;
418 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
419 GSList
*fnode
= gtk_file_chooser_list_filters(chooser
);
420 GSList
*filters
= fnode
;
424 if (i
== filterIndex
)
426 gtk_file_chooser_set_filter(chooser
, GTK_FILE_FILTER(fnode
->data
));
433 g_slist_free(filters
);
437 wxGenericFileDialog::SetFilterIndex( filterIndex
);
440 void wxFileDialog::UpdateDialog()
443 // set currently selected directory to match the path:
444 if (!m_dir
.empty() && wxDirExists(m_dir
))
446 // NB: This is important -- if we set directory only and not the path,
447 // then dialog will still remember old path set using previous
448 // call to gtk_chooser_set_filename. If the previous directory
449 // was a subdirectory of the directory we want to select now,
450 // the dialog would still contain directory selector controls
451 // for the subdirectory (with the parent directory selected),
452 // instead of showing only the parent directory as expected.
453 // This way, we force GtkFileChooser to really change the
454 // directory. Finally, it doesn't have to be done if filename
455 // is not empty because of the code that sets the filename below.
456 if (m_fileName
.empty())
457 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
),
460 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
464 // if the user set only the directory (e.g. by calling SetDirectory)
465 // and not the default filename, then we don't want to set the filename:
466 if (!m_fileName
.empty())
468 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
),
471 // pre-fill the filename when saving, too (there's no text entry
472 // control when opening a file, so it doesn't make sense to
473 // do this when opening files):
474 if (GetWindowStyle() & wxSAVE
)
476 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
477 wxGTK_CONV(m_fileName
));
483 void wxFileDialog::UpdateFromDialog()
486 // update filterIndex
487 GSList
*fnode
= gtk_file_chooser_list_filters(GTK_FILE_CHOOSER(m_widget
));
488 GSList
*filters
= fnode
;
489 GtkFileFilter
*current
=
490 gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(m_widget
));
496 if (fnode
->data
== (gpointer
)current
)
504 g_slist_free(filters
);
508 #endif // wxUSE_FILEDLG