misc fixes for wxDirDialog; new wxDD_CHANGE_DIR flag (patch 1478051)
[wxWidgets.git] / src / gtk / dirdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dirdlg.cpp
3 // Purpose: native implementation of wxDirDialogGTK
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 wxDirDialogGTK.
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__ // only for GTK+ > 2.4 there is GtkFileChooserDialog
33
34 #include <gtk/gtk.h>
35 #include "wx/gtk/private.h"
36
37 #include <unistd.h> // chdir
38
39
40 //-----------------------------------------------------------------------------
41 // idle system
42 //-----------------------------------------------------------------------------
43
44 extern void wxapp_install_idle_handler();
45
46 //-----------------------------------------------------------------------------
47 // "clicked" for OK-button
48 //-----------------------------------------------------------------------------
49
50 extern "C" {
51 static void gtk_filedialog_ok_callback(GtkWidget *widget, wxDirDialogGTK *dialog)
52 {
53 gchar* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
54
55 // change to the directory where the user went if asked
56 if (dialog->HasFlag(wxDD_CHANGE_DIR))
57 chdir(filename);
58
59 g_free(filename);
60
61 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
62 event.SetEventObject(dialog);
63 dialog->GetEventHandler()->ProcessEvent(event);
64 }
65 }
66
67 //-----------------------------------------------------------------------------
68 // "clicked" for Cancel-button
69 //-----------------------------------------------------------------------------
70
71 extern "C" {
72 static void gtk_filedialog_cancel_callback(GtkWidget *WXUNUSED(w),
73 wxDirDialogGTK *dialog)
74 {
75 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
76 event.SetEventObject(dialog);
77 dialog->GetEventHandler()->ProcessEvent(event);
78 }
79 }
80
81 extern "C" {
82 static void gtk_filedialog_response_callback(GtkWidget *w,
83 gint response,
84 wxDirDialogGTK *dialog)
85 {
86 wxapp_install_idle_handler();
87
88 if (response == GTK_RESPONSE_ACCEPT)
89 gtk_filedialog_ok_callback(w, dialog);
90 else if (response == GTK_RESPONSE_CANCEL)
91 gtk_filedialog_cancel_callback(w, dialog);
92 else // "delete"
93 {
94 gtk_filedialog_cancel_callback(w, dialog);
95 dialog->m_destroyed_by_delete = true;
96 }
97 }
98 }
99
100 #endif // __WXGTK24__
101
102 //-----------------------------------------------------------------------------
103 // wxDirDialogGTK
104 //-----------------------------------------------------------------------------
105
106 IMPLEMENT_DYNAMIC_CLASS(wxDirDialogGTK,wxGenericDirDialog)
107
108 BEGIN_EVENT_TABLE(wxDirDialogGTK,wxGenericDirDialog)
109 EVT_BUTTON(wxID_OK, wxDirDialogGTK::OnFakeOk)
110 END_EVENT_TABLE()
111
112 wxDirDialogGTK::wxDirDialogGTK(wxWindow* parent, const wxString& title,
113 const wxString& defaultPath, long style,
114 const wxPoint& pos, const wxSize& sz,
115 const wxString& name)
116 {
117 #ifdef __WXGTK24__
118 if (!gtk_check_version(2,4,0))
119 {
120 m_message = title;
121 m_needParent = false;
122 m_destroyed_by_delete = false;
123
124 if (!PreCreation(parent, pos, wxDefaultSize) ||
125 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
126 wxDefaultValidator, wxT("filedialog")))
127 {
128 wxFAIL_MSG( wxT("wxDirDialogGTK creation failed") );
129 return;
130 }
131
132 GtkFileChooserAction gtk_action;
133 GtkWindow* gtk_parent = NULL;
134 if (parent)
135 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
136
137 gtk_action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
138 if (style & wxDD_NEW_DIR_BUTTON)
139 gtk_action = GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
140
141 m_widget = gtk_file_chooser_dialog_new(
142 wxGTK_CONV(m_message),
143 gtk_parent,
144 gtk_action,
145 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
146 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
147 NULL);
148
149 // local-only property could be set to false to allow non-local files to be loaded.
150 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
151 // and the GtkFileChooserDialog should probably also be created with a backend,
152 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
153 // Currently local-only is kept as the default - true:
154 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
155
156 g_signal_connect(G_OBJECT(m_widget), "response",
157 GTK_SIGNAL_FUNC(gtk_filedialog_response_callback), (gpointer)this);
158
159 if ( !defaultPath.empty() )
160 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget),
161 wxConvFileName->cWX2MB(defaultPath) );
162 }
163 else
164 #endif
165 wxGenericDirDialog::Create(parent, title, defaultPath, style, pos, sz, name);
166 }
167
168 wxDirDialogGTK::~wxDirDialogGTK()
169 {
170 #ifdef __WXGTK24__
171 if (!gtk_check_version(2,4,0))
172 {
173 if (m_destroyed_by_delete)
174 m_widget = NULL;
175 }
176 #endif
177 }
178
179 void wxDirDialogGTK::OnFakeOk( wxCommandEvent &event )
180 {
181 #ifdef __WXGTK24__
182 if (!gtk_check_version(2,4,0))
183 wxDialog::OnOK( event );
184 else
185 #endif
186 wxGenericDirDialog::OnOK( event );
187 }
188
189 int wxDirDialogGTK::ShowModal()
190 {
191 #ifdef __WXGTK24__
192 if (!gtk_check_version(2,4,0))
193 return wxDialog::ShowModal();
194 else
195 #endif
196 return wxGenericDirDialog::ShowModal();
197 }
198
199 bool wxDirDialogGTK::Show( bool show )
200 {
201 #ifdef __WXGTK24__
202 if (!gtk_check_version(2,4,0))
203 return wxDialog::Show( show );
204 else
205 #endif
206 return wxGenericDirDialog::Show( show );
207 }
208
209 void wxDirDialogGTK::DoSetSize(int x, int y, int width, int height, int sizeFlags )
210 {
211 if (!m_wxwindow)
212 return;
213 else
214 wxGenericDirDialog::DoSetSize( x, y, width, height, sizeFlags );
215 }
216
217 void wxDirDialogGTK::SetPath(const wxString& dir)
218 {
219 #ifdef __WXGTK24__
220 if (!gtk_check_version(2,4,0))
221 {
222 if (wxDirExists(dir))
223 {
224 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(dir));
225 }
226 }
227 else
228 #endif
229 wxGenericDirDialog::SetPath( dir );
230 }
231
232 wxString wxDirDialogGTK::GetPath() const
233 {
234 #ifdef __WXGTK24__
235 if (!gtk_check_version(2,4,0))
236 return wxConvFileName->cMB2WX( gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(m_widget) ) );
237 else
238 #endif
239 return wxGenericDirDialog::GetPath();
240 }
241
242 #endif // wxUSE_DIRDLG