1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/filepicker.cpp
3 // Purpose: implementation of wxFileButton and wxDirButton
4 // Author: Francesco Montorsi
5 // Modified By:
6 // Created: 15/04/2006
7 // Copyright: (c) Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 // ----------------------------------------------------------------------------
13 // headers
14 // ----------------------------------------------------------------------------
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #if wxUSE_FILEPICKERCTRL
20
21 #ifndef WX_PRECOMP
22 #include "wx/log.h"
23 #endif
24
25 #include "wx/filepicker.h"
26 #include "wx/tooltip.h"
27
28 #include <gtk/gtk.h>
29 #include "wx/gtk/private.h"
30
31 // ============================================================================
32 // implementation
33 // ============================================================================
34
35 //-----------------------------------------------------------------------------
36 // wxFileButton
37 //-----------------------------------------------------------------------------
38
IMPLEMENT_DYNAMIC_CLASS(wxFileButton,wxButton)39 IMPLEMENT_DYNAMIC_CLASS(wxFileButton, wxButton)
40
41 bool wxFileButton::Create( wxWindow *parent, wxWindowID id,
42 const wxString &label, const wxString &path,
43 const wxString &message, const wxString &wildcard,
44 const wxPoint &pos, const wxSize &size,
45 long style, const wxValidator& validator,
46 const wxString &name )
47 {
48 // we can't use the native button for wxFLP_SAVE pickers as it can only
49 // open existing files and there is no way to create a new file using it
50 if (!(style & wxFLP_SAVE) && !(style & wxFLP_USE_TEXTCTRL))
51 {
52 // VERY IMPORTANT: this code is identical to relative code in wxDirButton;
53 // if you find a problem here, fix it also in wxDirButton !
54
55 if (!PreCreation( parent, pos, size ) ||
56 !wxControl::CreateBase(parent, id, pos, size, style & wxWINDOW_STYLE_MASK,
57 validator, name))
58 {
59 wxFAIL_MSG( wxT("wxFileButton creation failed") );
60 return false;
61 }
62
63 // create the dialog associated with this button
64 // NB: unlike generic implementation, native GTK implementation needs to create
65 // the filedialog here as it needs to use gtk_file_chooser_button_new_with_dialog()
66 SetWindowStyle(style);
67 m_path = path;
68 m_message = message;
69 m_wildcard = wildcard;
70 if ((m_dialog = CreateDialog()) == NULL)
71 return false;
72
73 // little trick used to avoid problems when there are other GTK windows 'grabbed':
74 // GtkFileChooserDialog won't be responsive to user events if there is another
75 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
76 // in modal mode in the application - see wxDialogGTK::ShowModal).
77 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
78 // is clicked and then remove it as soon as the user closes the dialog itself.
79 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
80 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
81 // hidden simply using its "show" and "hide" events - clean & simple :)
82 g_signal_connect(m_dialog->m_widget, "show", G_CALLBACK(gtk_grab_add), NULL);
83 g_signal_connect(m_dialog->m_widget, "hide", G_CALLBACK(gtk_grab_remove), NULL);
84
85 // use as label the currently selected file
86 m_widget = gtk_file_chooser_button_new_with_dialog( m_dialog->m_widget );
87 g_object_ref(m_widget);
88
89 // we need to know when the dialog has been dismissed clicking OK...
90 // NOTE: the "clicked" signal is not available for a GtkFileChooserButton
91 // thus we are forced to use wxFileDialog's event
92 m_dialog->Connect(wxEVT_BUTTON,
93 wxCommandEventHandler(wxFileButton::OnDialogOK),
94 NULL, this);
95
96 m_parent->DoAddChild( this );
97
98 PostCreation(size);
99 SetInitialSize(size);
100 }
101 else
102 return wxGenericFileButton::Create(parent, id, label, path, message, wildcard,
103 pos, size, style, validator, name);
104 return true;
105 }
106
~wxFileButton()107 wxFileButton::~wxFileButton()
108 {
109 if ( m_dialog )
110 {
111 // when m_dialog is deleted, it will destroy the widget it is sharing
112 // with GtkFileChooserButton, which results in a bunch of Gtk-CRITICAL
113 // errors from GtkFileChooserButton. To avoid this, call gtk_widget_destroy()
114 // on GtkFileChooserButton first (our base dtor will do it again, but
115 // that does no harm). m_dialog holds a reference to the shared widget,
116 // so it won't go away until m_dialog base dtor unrefs it.
117 gtk_widget_destroy(m_widget);
118 delete m_dialog;
119 }
120 }
121
OnDialogOK(wxCommandEvent & ev)122 void wxFileButton::OnDialogOK(wxCommandEvent& ev)
123 {
124 // the wxFileDialog associated with the GtkFileChooserButton has been closed
125 // using the OK button, thus the selected file has changed...
126 if (ev.GetId() == wxID_OK)
127 {
128 // ...update our path
129 UpdatePathFromDialog(m_dialog);
130
131 // ...and fire an event
132 wxFileDirPickerEvent event(wxEVT_FILEPICKER_CHANGED, this, GetId(), m_path);
133 HandleWindowEvent(event);
134 }
135 }
136
SetPath(const wxString & str)137 void wxFileButton::SetPath(const wxString &str)
138 {
139 m_path = str;
140
141 if (m_dialog)
142 UpdateDialogPath(m_dialog);
143 }
144
SetInitialDirectory(const wxString & dir)145 void wxFileButton::SetInitialDirectory(const wxString& dir)
146 {
147 if (m_dialog)
148 {
149 // Only change the directory if the default file name doesn't have any
150 // directory in it, otherwise it takes precedence.
151 if ( m_path.find_first_of(wxFileName::GetPathSeparators()) ==
152 wxString::npos )
153 {
154 static_cast<wxFileDialog*>(m_dialog)->SetDirectory(dir);
155 }
156 }
157 else
158 wxGenericFileButton::SetInitialDirectory(dir);
159 }
160
DoApplyWidgetStyle(GtkRcStyle *)161 void wxFileButton::DoApplyWidgetStyle(GtkRcStyle*)
162 {
163 }
164 #endif // wxUSE_FILEPICKERCTRL
165
166 #if wxUSE_DIRPICKERCTRL
167
168 #ifdef __UNIX__
169 #include <unistd.h> // chdir
170 #endif
171
172 //-----------------------------------------------------------------------------
173 // "file-set"
174 //-----------------------------------------------------------------------------
175
176 extern "C" {
file_set(GtkFileChooser * widget,wxDirButton * p)177 static void file_set(GtkFileChooser* widget, wxDirButton* p)
178 {
179 // NB: it's important to use gtk_file_chooser_get_filename instead of
180 // gtk_file_chooser_get_current_folder (see GTK docs) !
181 wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget)));
182 p->GTKUpdatePath(filename);
183
184 // since GtkFileChooserButton when used to pick directories also uses a combobox,
185 // maybe that the current folder has been changed but not through the GtkFileChooserDialog
186 // and thus the 'gtk_filedialog_ok_callback' could have not been called...
187 // thus we need to make sure the current working directory is updated if wxDIRP_CHANGE_DIR
188 // style was given.
189 if (p->HasFlag(wxDIRP_CHANGE_DIR))
190 {
191 if ( chdir(filename) != 0 )
192 {
193 wxLogSysError(_("Changing current directory to \"%s\" failed"),
194 wxString::FromUTF8(filename));
195 }
196 }
197
198 // ...and fire an event
199 wxFileDirPickerEvent event(wxEVT_DIRPICKER_CHANGED, p, p->GetId(), p->GetPath());
200 p->HandleWindowEvent(event);
201 }
202 }
203
204 //-----------------------------------------------------------------------------
205 // "selection-changed"
206 //-----------------------------------------------------------------------------
207
208 extern "C" {
selection_changed(GtkFileChooser * chooser,wxDirButton * win)209 static void selection_changed(GtkFileChooser* chooser, wxDirButton* win)
210 {
211 char* filename = gtk_file_chooser_get_filename(chooser);
212
213 if (wxString::FromUTF8(filename) == win->GetPath())
214 win->m_bIgnoreNextChange = false;
215 else if (!win->m_bIgnoreNextChange)
216 file_set(chooser, win);
217
218 g_free(filename);
219 }
220 }
221
222 //-----------------------------------------------------------------------------
223 // wxDirButtonGTK
224 //-----------------------------------------------------------------------------
225
IMPLEMENT_DYNAMIC_CLASS(wxDirButton,wxButton)226 IMPLEMENT_DYNAMIC_CLASS(wxDirButton, wxButton)
227
228 bool wxDirButton::Create( wxWindow *parent, wxWindowID id,
229 const wxString &label, const wxString &path,
230 const wxString &message, const wxString &wildcard,
231 const wxPoint &pos, const wxSize &size,
232 long style, const wxValidator& validator,
233 const wxString &name )
234 {
235 if (!(style & wxDIRP_USE_TEXTCTRL))
236 {
237 // VERY IMPORTANT: this code is identic to relative code in wxFileButton;
238 // if you find a problem here, fix it also in wxFileButton !
239
240 if (!PreCreation( parent, pos, size ) ||
241 !wxControl::CreateBase(parent, id, pos, size, style & wxWINDOW_STYLE_MASK,
242 validator, name))
243 {
244 wxFAIL_MSG( wxT("wxDirButtonGTK creation failed") );
245 return false;
246 }
247
248 // create the dialog associated with this button
249 SetWindowStyle(style);
250 m_message = message;
251 m_wildcard = wildcard;
252 if ((m_dialog = CreateDialog()) == NULL)
253 return false;
254
255 // little trick used to avoid problems when there are other GTK windows 'grabbed':
256 // GtkFileChooserDialog won't be responsive to user events if there is another
257 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
258 // in modal mode in the application - see wxDialogGTK::ShowModal).
259 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
260 // is clicked and then remove it as soon as the user closes the dialog itself.
261 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
262 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
263 // hidden simply using its "show" and "hide" events - clean & simple :)
264 g_signal_connect(m_dialog->m_widget, "show", G_CALLBACK(gtk_grab_add), NULL);
265 g_signal_connect(m_dialog->m_widget, "hide", G_CALLBACK(gtk_grab_remove), NULL);
266
267
268 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
269 // use as label the currently selected file
270 m_widget = gtk_file_chooser_button_new_with_dialog( m_dialog->m_widget );
271 g_object_ref(m_widget);
272 SetPath(path);
273
274 #ifdef __WXGTK3__
275 if (gtk_check_version(3,8,0) == NULL)
276 g_signal_connect(m_widget, "file_set", G_CALLBACK(file_set), this);
277 else
278 #endif
279 {
280 // prior to GTK+ 3.8 neither "file-set" nor "current-folder-changed" will be
281 // emitted when the user selects one of the special folders from the combobox
282 g_signal_connect(m_widget, "selection_changed",
283 G_CALLBACK(selection_changed), this);
284 }
285
286 m_parent->DoAddChild( this );
287
288 PostCreation(size);
289 SetInitialSize(size);
290 }
291 else
292 return wxGenericDirButton::Create(parent, id, label, path, message, wildcard,
293 pos, size, style, validator, name);
294 return true;
295 }
296
~wxDirButton()297 wxDirButton::~wxDirButton()
298 {
299 if (m_dialog)
300 {
301 // see ~wxFileButton() comment
302 gtk_widget_destroy(m_widget);
303 delete m_dialog;
304 }
305 }
306
GTKUpdatePath(const char * gtkpath)307 void wxDirButton::GTKUpdatePath(const char *gtkpath)
308 {
309 m_path = wxString::FromUTF8(gtkpath);
310 }
SetPath(const wxString & str)311 void wxDirButton::SetPath(const wxString& str)
312 {
313 if ( m_path == str )
314 return;
315
316 m_path = str;
317
318 m_bIgnoreNextChange = true;
319
320 if (GTK_IS_FILE_CHOOSER(m_widget))
321 gtk_file_chooser_set_filename((GtkFileChooser*)m_widget, str.utf8_str());
322 else if (m_dialog)
323 UpdateDialogPath(m_dialog);
324 }
325
SetInitialDirectory(const wxString & dir)326 void wxDirButton::SetInitialDirectory(const wxString& dir)
327 {
328 if (m_dialog)
329 {
330 if (m_path.empty())
331 static_cast<wxDirDialog*>(m_dialog)->SetPath(dir);
332 }
333 else
334 wxGenericDirButton::SetInitialDirectory(dir);
335 }
336
DoApplyWidgetStyle(GtkRcStyle *)337 void wxDirButton::DoApplyWidgetStyle(GtkRcStyle*)
338 {
339 }
340 #endif // wxUSE_DIRPICKERCTRL
341