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"
17 #if wxUSE_FILEDLG && defined(__WXGTK24__)
19 #include "wx/filedlg.h"
22 #include "wx/filename.h"
23 #include "wx/msgdlg.h"
26 #include "wx/gtk/private.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern void wxapp_install_idle_handler();
35 //-----------------------------------------------------------------------------
36 // "clicked" for OK-button
37 //-----------------------------------------------------------------------------
39 static void gtk_filedialog_ok_callback(GtkWidget
*widget
, wxFileDialog
*dialog
)
41 int style
= dialog
->GetStyle();
42 gchar
* text
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
43 wxString
filename(wxGTK_CONV_BACK(text
));
45 if ((style
& wxSAVE
) && (style
& wxOVERWRITE_PROMPT
))
47 if (wxFileExists(filename
))
51 _("File '%s' already exists, do you really want to overwrite it?"),
54 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
55 wxYES_NO
| wxICON_QUESTION
);
56 if (dlg
.ShowModal() != wxID_YES
)
60 else if ((style
& wxOPEN
) && ( style
& wxFILE_MUST_EXIST
))
62 if (!wxFileExists( filename
))
64 wxMessageDialog
dlg(dialog
,
65 _("Please choose an existing file."),
66 _("Error"), wxOK
| wxICON_ERROR
);
73 // change to the directory where the user went if asked
74 if (style
& wxCHANGE_DIR
)
77 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
79 if (cwd
!= wxGetCwd())
81 wxSetWorkingDirectory(cwd
);
85 dialog
->SetPath(filename
);
86 dialog
->UpdateFromDialog();
88 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
89 event
.SetEventObject(dialog
);
90 dialog
->GetEventHandler()->ProcessEvent(event
);
93 //-----------------------------------------------------------------------------
94 // "clicked" for Cancel-button
95 //-----------------------------------------------------------------------------
97 static void gtk_filedialog_cancel_callback(GtkWidget
*WXUNUSED(w
),
100 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
101 event
.SetEventObject(dialog
);
102 dialog
->GetEventHandler()->ProcessEvent(event
);
105 static void gtk_filedialog_response_callback(GtkWidget
*w
,
107 wxFileDialog
*dialog
)
109 wxapp_install_idle_handler();
111 if (response
== GTK_RESPONSE_ACCEPT
)
112 gtk_filedialog_ok_callback(w
, dialog
);
113 else if (response
== GTK_RESPONSE_CANCEL
)
114 gtk_filedialog_cancel_callback(w
, dialog
);
117 gtk_filedialog_cancel_callback(w
, dialog
);
118 dialog
->m_destroyed_by_delete
= TRUE
;
122 //-----------------------------------------------------------------------------
124 //-----------------------------------------------------------------------------
126 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxFileDialogBase
)
128 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
129 const wxString
& defaultDir
,
130 const wxString
& defaultFileName
,
131 const wxString
& wildCard
,
132 long style
, const wxPoint
& pos
)
133 : wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
,
134 wildCard
, style
, pos
)
136 m_needParent
= FALSE
;
137 m_destroyed_by_delete
= FALSE
;
139 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
140 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
141 wxDefaultValidator
, wxT("filedialog")))
143 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
147 bool multiple
= (style
& wxMULTIPLE
) == wxMULTIPLE
;
148 GtkFileChooserAction gtk_action
;
149 GtkWindow
* gtk_parent
= NULL
;
151 gtk_parent
= GTK_WINDOW(parent
->m_widget
);
154 if ((style
& wxSAVE
) == wxSAVE
)
156 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
157 ok_btn_stock
= GTK_STOCK_SAVE
;
161 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
162 ok_btn_stock
= GTK_STOCK_OPEN
;
164 m_widget
= gtk_file_chooser_dialog_new(
165 wxGTK_CONV(m_message
),
168 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
169 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
172 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), multiple
);
174 gtk_signal_connect(GTK_OBJECT(m_widget
),
176 GTK_SIGNAL_FUNC(gtk_filedialog_response_callback
),
180 if (!m_path
.empty() && m_path
.Last() != wxT('/'))
182 m_path
+= m_fileName
;
185 SetWildcard(wildCard
);
189 wxFileDialog::~wxFileDialog()
191 if (m_destroyed_by_delete
)
195 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
198 for (size_t n
= 0; n
< files
.GetCount(); n
++ )
201 wxSplitPath(files
[n
], NULL
, &name
, &ext
);
211 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
214 if (GetWindowStyle() & wxMULTIPLE
)
217 gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget
));
218 GSList
*gpaths
= gpathsi
;
221 wxString file
= wxGTK_CONV_BACK((gchar
*) gpathsi
->data
);
223 g_free(gpathsi
->data
);
224 gpathsi
= gpathsi
->next
;
227 g_slist_free(gpaths
);
231 paths
.Add(m_fileName
);
235 void wxFileDialog::SetMessage(const wxString
& message
)
241 void wxFileDialog::SetPath(const wxString
& path
)
243 if (path
.empty()) return;
246 m_path
= fn
.GetFullPath();
247 m_dir
= fn
.GetPath();
248 m_fileName
= fn
.GetFullName();
252 void wxFileDialog::SetDirectory(const wxString
& dir
)
254 if (wxDirExists(dir
))
257 m_path
= wxFileName(m_dir
, m_fileName
).GetFullPath();
262 void wxFileDialog::SetFilename(const wxString
& name
)
265 m_path
= wxFileName(m_dir
, m_fileName
).GetFullPath();
269 void wxFileDialog::UpdateDialog()
271 // set currently selected directory to match the path:
272 if (!m_dir
.empty() && wxDirExists(m_dir
))
274 // NB: This is important -- if we set directory only and not the path,
275 // then dialog will still remember old path set using previous
276 // call to gtk_chooser_set_filename. If the previous directory
277 // was a subdirectory of the directory we want to select now,
278 // the dialog would still contain directory selector controls
279 // for the subdirectory (with the parent directory selected),
280 // instead of showing only the parent directory as expected.
281 // This way, we force GtkFileChooser to really change the
282 // directory. Finally, it doesn't have to be done if filename
283 // is not empty because of the code that sets the filename below.
284 if (m_fileName
.empty())
285 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
),
288 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
292 // if the user set only the directory (e.g. by calling SetDirectory)
293 // and not the default filename, then we don't want to set the filename:
294 if (!m_fileName
.empty())
296 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
),
299 // pre-fill the filename when saving, too (there's no text entry
300 // control when opening a file, so it doesn't make sense to
301 // do this when opening files):
302 if (GetWindowStyle() & wxSAVE
)
304 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
305 wxGTK_CONV(m_fileName
));
310 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
312 m_wildCard
= wildCard
;
314 GtkFileChooser
* chooser
= GTK_FILE_CHOOSER(m_widget
);
316 // empty current filter list:
317 GSList
* ifilters
= gtk_file_chooser_list_filters(chooser
);
318 GSList
* filters
= ifilters
;
321 gtk_file_chooser_remove_filter(chooser
,GTK_FILE_FILTER(ifilters
->data
));
322 ifilters
= ifilters
->next
;
324 g_slist_free(filters
);
327 wxArrayString wildDescriptions
, wildFilters
;
328 if (!wxParseCommonDialogsFilter(m_wildCard
, wildDescriptions
, wildFilters
))
330 wxFAIL_MSG( wxT("Wrong file type description") );
334 // add parsed to GtkChooser
335 for (size_t n
= 0; n
< wildFilters
.GetCount(); n
++)
337 GtkFileFilter
* filter
= gtk_file_filter_new();
338 gtk_file_filter_set_name(filter
,wxGTK_CONV(wildDescriptions
[n
]));
339 wxString after
= wildFilters
[n
];
342 wxString ext
= after
.BeforeFirst(wxT(';'));
343 gtk_file_filter_add_pattern(filter
,wxGTK_CONV(ext
));
344 if (after
.Find(wxT(';')) == wxNOT_FOUND
)
346 after
= after
.AfterLast(wxT(';'));
348 while (!after
.empty());
350 gtk_file_chooser_add_filter(chooser
, filter
);
355 void wxFileDialog::SetFilterIndex(int filterIndex
)
357 m_filterIndex
= filterIndex
;
359 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
360 GSList
*fnode
= gtk_file_chooser_list_filters(chooser
);
361 GSList
*filters
= fnode
;
365 if (i
== filterIndex
)
367 gtk_file_chooser_set_filter(chooser
, GTK_FILE_FILTER(fnode
->data
));
374 g_slist_free(filters
);
377 void wxFileDialog::UpdateFromDialog()
379 // update filterIndex
380 GSList
*fnode
= gtk_file_chooser_list_filters(GTK_FILE_CHOOSER(m_widget
));
381 GSList
*filters
= fnode
;
382 GtkFileFilter
*current
=
383 gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(m_widget
));
389 if (fnode
->data
== (gpointer
)current
)
397 g_slist_free(filters
);
400 #endif // wxUSE_FILEDLG && defined(__WXGTK24__)