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