compilation fix when not using PCH
[wxWidgets.git] / src / gtk / dirdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dirdlg.cpp
3 // Purpose: native implementation of wxDirDialog
4 // Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp, Francesco Montorsi
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13
14
15 /*
16 NOTE: the GtkFileChooser interface can be used both for wxFileDialog and for wxDirDialog.
17 Thus following code is very similar (even if not identic) to src/gtk/filedlg.cpp
18 If you find a problem in this code, remember to check also that file !
19 */
20
21
22
23 #if wxUSE_DIRDLG
24
25 #include "wx/dirdlg.h"
26
27 #ifndef WX_PRECOMP
28 #include "wx/intl.h"
29 #include "wx/filedlg.h"
30 #endif
31
32 #ifdef __WXGTK24__
33
34 #include <gtk/gtk.h>
35 #include "wx/gtk/private.h"
36
37 #include <unistd.h> // chdir
38
39 #include "wx/filename.h" // wxFilename
40 #include "wx/tokenzr.h" // wxStringTokenizer
41 #include "wx/filefn.h" // ::wxGetCwd
42 #include "wx/msgdlg.h" // wxMessageDialog
43
44 //-----------------------------------------------------------------------------
45 // idle system
46 //-----------------------------------------------------------------------------
47
48 extern void wxapp_install_idle_handler();
49
50 //-----------------------------------------------------------------------------
51 // "clicked" for OK-button
52 //-----------------------------------------------------------------------------
53
54 extern "C" {
55 static void gtk_filedialog_ok_callback(GtkWidget *widget, wxDirDialog *dialog)
56 {
57 int style = dialog->GetStyle();
58 gchar* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
59
60 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
61 #if GTK_CHECK_VERSION(2,7,3)
62 if(gtk_check_version(2,7,3) != NULL)
63 #endif
64 if ((style & wxSAVE) && (style & wxOVERWRITE_PROMPT))
65 {
66 if ( g_file_test(filename, G_FILE_TEST_EXISTS) )
67 {
68 wxString msg;
69
70 msg.Printf(
71 _("File '%s' already exists, do you really want to overwrite it?"),
72 wxString(wxConvFileName->cMB2WX(filename)).c_str());
73
74 wxMessageDialog dlg(dialog, msg, _("Confirm"),
75 wxYES_NO | wxICON_QUESTION);
76 if (dlg.ShowModal() != wxID_YES)
77 {
78 g_free(filename);
79 return;
80 }
81 }
82 }
83
84 // change to the directory where the user went if asked
85 if (style & wxCHANGE_DIR)
86 {
87 // Use chdir to not care about filename encodings
88 gchar* folder = g_path_get_dirname(filename);
89 chdir(folder);
90 g_free(folder);
91 }
92
93 g_free(filename);
94
95 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
96 event.SetEventObject(dialog);
97 dialog->GetEventHandler()->ProcessEvent(event);
98 }
99 }
100
101 //-----------------------------------------------------------------------------
102 // "clicked" for Cancel-button
103 //-----------------------------------------------------------------------------
104
105 extern "C" {
106 static void gtk_filedialog_cancel_callback(GtkWidget *WXUNUSED(w),
107 wxDirDialog *dialog)
108 {
109 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
110 event.SetEventObject(dialog);
111 dialog->GetEventHandler()->ProcessEvent(event);
112 }
113 }
114
115 extern "C" {
116 static void gtk_filedialog_response_callback(GtkWidget *w,
117 gint response,
118 wxDirDialog *dialog)
119 {
120 wxapp_install_idle_handler();
121
122 if (response == GTK_RESPONSE_ACCEPT)
123 gtk_filedialog_ok_callback(w, dialog);
124 else if (response == GTK_RESPONSE_CANCEL)
125 gtk_filedialog_cancel_callback(w, dialog);
126 else // "delete"
127 {
128 gtk_filedialog_cancel_callback(w, dialog);
129 dialog->m_destroyed_by_delete = true;
130 }
131 }
132 }
133
134 #endif // __WXGTK24__
135
136 //-----------------------------------------------------------------------------
137 // wxDirDialog
138 //-----------------------------------------------------------------------------
139
140 IMPLEMENT_DYNAMIC_CLASS(wxDirDialog,wxGenericDirDialog)
141
142 BEGIN_EVENT_TABLE(wxDirDialog,wxGenericDirDialog)
143 EVT_BUTTON(wxID_OK, wxDirDialog::OnFakeOk)
144 END_EVENT_TABLE()
145
146 wxDirDialog::wxDirDialog(wxWindow* parent, const wxString& title,
147 const wxString& defaultPath, long style,
148 const wxPoint& pos, const wxSize& sz,
149 const wxString& name)
150 {
151 #ifdef __WXGTK24__
152 if (!gtk_check_version(2,4,0))
153 {
154 m_message = title;
155 m_needParent = false;
156 m_destroyed_by_delete = false;
157
158 if (!PreCreation(parent, pos, wxDefaultSize) ||
159 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
160 wxDefaultValidator, wxT("filedialog")))
161 {
162 wxFAIL_MSG( wxT("wxDirDialog creation failed") );
163 return;
164 }
165
166 GtkFileChooserAction gtk_action;
167 GtkWindow* gtk_parent = NULL;
168 if (parent)
169 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
170
171 gtk_action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
172 if (style & wxDD_NEW_DIR_BUTTON)
173 gtk_action = GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
174
175 m_widget = gtk_file_chooser_dialog_new(
176 wxGTK_CONV(m_message),
177 gtk_parent,
178 gtk_action,
179 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
180 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
181 NULL);
182
183 // local-only property could be set to false to allow non-local files to be loaded.
184 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
185 // and the GtkFileChooserDialog should probably also be created with a backend,
186 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
187 // Currently local-only is kept as the default - true:
188 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
189
190 g_signal_connect(G_OBJECT(m_widget), "response",
191 GTK_SIGNAL_FUNC(gtk_filedialog_response_callback), (gpointer)this);
192
193 if ( !defaultPath.empty() )
194 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget),
195 wxConvFileName->cWX2MB(defaultPath) );
196 }
197 else
198 #endif
199 wxGenericDirDialog::Create(parent, title, defaultPath, style, pos, sz, name);
200 }
201
202 wxDirDialog::~wxDirDialog()
203 {
204 #ifdef __WXGTK24__
205 if (!gtk_check_version(2,4,0))
206 {
207 if (m_destroyed_by_delete)
208 m_widget = NULL;
209 }
210 #endif
211 }
212
213 void wxDirDialog::OnFakeOk( wxCommandEvent &event )
214 {
215 #ifdef __WXGTK24__
216 if (!gtk_check_version(2,4,0))
217 wxDialog::OnOK( event );
218 else
219 #endif
220 wxGenericDirDialog::OnOK( event );
221 }
222
223 int wxDirDialog::ShowModal()
224 {
225 #ifdef __WXGTK24__
226 if (!gtk_check_version(2,4,0))
227 return wxDialog::ShowModal();
228 else
229 #endif
230 return wxGenericDirDialog::ShowModal();
231 }
232
233 bool wxDirDialog::Show( bool show )
234 {
235 #ifdef __WXGTK24__
236 if (!gtk_check_version(2,4,0))
237 return wxDialog::Show( show );
238 else
239 #endif
240 return wxGenericDirDialog::Show( show );
241 }
242
243 void wxDirDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags )
244 {
245 if (!m_wxwindow)
246 return;
247 else
248 wxGenericDirDialog::DoSetSize( x, y, width, height, sizeFlags );
249 }
250
251 void wxDirDialog::SetPath(const wxString& dir)
252 {
253 #ifdef __WXGTK24__
254 if (!gtk_check_version(2,4,0))
255 {
256 if (wxDirExists(dir))
257 {
258 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(dir));
259 }
260 }
261 else
262 #endif
263 wxGenericDirDialog::SetPath( dir );
264 }
265
266 wxString wxDirDialog::GetPath() const
267 {
268 #ifdef __WXGTK24__
269 if (!gtk_check_version(2,4,0))
270 return wxConvFileName->cMB2WX( gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(m_widget) ) );
271 else
272 #endif
273 return wxGenericDirDialog::GetPath();
274 }
275
276 #endif // wxUSE_DIRDLG