]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/filepicker.cpp
Go back to using a fixed value of 3 for lines/columns per action for mouse wheel...
[wxWidgets.git] / src / gtk / filepicker.cpp
CommitLineData
ec376c8f
VZ
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
ec376c8f
VZ
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
4e621d24 19#if wxUSE_FILEPICKERCTRL
4ce7b1e4 20
ec376c8f 21#include "wx/filepicker.h"
c757b5fe 22#include "wx/tooltip.h"
ec376c8f 23
9dc44eff 24#include <gtk/gtk.h>
e808cf8a 25#include "wx/gtk/private.h"
ec376c8f
VZ
26
27// ============================================================================
28// implementation
29// ============================================================================
30
ec376c8f
VZ
31//-----------------------------------------------------------------------------
32// wxFileButton
33//-----------------------------------------------------------------------------
34
35IMPLEMENT_DYNAMIC_CLASS(wxFileButton, wxButton)
36
37bool wxFileButton::Create( wxWindow *parent, wxWindowID id,
38 const wxString &label, const wxString &path,
39 const wxString &message, const wxString &wildcard,
40 const wxPoint &pos, const wxSize &size,
41 long style, const wxValidator& validator,
42 const wxString &name )
43{
4db37208
VZ
44 // we can't use the native button for wxFLP_SAVE pickers as it can only
45 // open existing files and there is no way to create a new file using it
4e621d24 46 if (!(style & wxFLP_SAVE) && !(style & wxFLP_USE_TEXTCTRL))
ec376c8f 47 {
77d82770 48 // VERY IMPORTANT: this code is identical to relative code in wxDirButton;
a65ffcb2 49 // if you find a problem here, fix it also in wxDirButton !
ec376c8f 50
ec376c8f
VZ
51 if (!PreCreation( parent, pos, size ) ||
52 !wxControl::CreateBase(parent, id, pos, size, style & wxWINDOW_STYLE_MASK,
53 validator, name))
54 {
55 wxFAIL_MSG( wxT("wxFileButton creation failed") );
56 return false;
57 }
58
59 // create the dialog associated with this button
556151f5
MW
60 // NB: unlike generic implementation, native GTK implementation needs to create
61 // the filedialog here as it needs to use gtk_file_chooser_button_new_with_dialog()
ec376c8f
VZ
62 SetWindowStyle(style);
63 m_path = path;
556151f5
MW
64 m_message = message;
65 m_wildcard = wildcard;
66 if ((m_dialog = CreateDialog()) == NULL)
ec376c8f 67 return false;
03647350 68
ec376c8f
VZ
69 // little trick used to avoid problems when there are other GTK windows 'grabbed':
70 // GtkFileChooserDialog won't be responsive to user events if there is another
71 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
72 // in modal mode in the application - see wxDialogGTK::ShowModal).
73 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
74 // is clicked and then remove it as soon as the user closes the dialog itself.
75 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
76 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
77 // hidden simply using its "show" and "hide" events - clean & simple :)
78 g_signal_connect(m_dialog->m_widget, "show", G_CALLBACK(gtk_grab_add), NULL);
79 g_signal_connect(m_dialog->m_widget, "hide", G_CALLBACK(gtk_grab_remove), NULL);
80
ec376c8f
VZ
81 // use as label the currently selected file
82 m_widget = gtk_file_chooser_button_new_with_dialog( m_dialog->m_widget );
9ff9d30c 83 g_object_ref(m_widget);
ec376c8f
VZ
84
85 // we need to know when the dialog has been dismissed clicking OK...
86 // NOTE: the "clicked" signal is not available for a GtkFileChooserButton
87 // thus we are forced to use wxFileDialog's event
ce7fe42e 88 m_dialog->Connect(wxEVT_BUTTON,
ec376c8f
VZ
89 wxCommandEventHandler(wxFileButton::OnDialogOK),
90 NULL, this);
91
92 m_parent->DoAddChild( this );
93
94 PostCreation(size);
170acdc9 95 SetInitialSize(size);
ec376c8f
VZ
96 }
97 else
98 return wxGenericFileButton::Create(parent, id, label, path, message, wildcard,
99 pos, size, style, validator, name);
100 return true;
101}
102
103wxFileButton::~wxFileButton()
104{
3ca5e2db
VZ
105 if ( m_dialog )
106 {
7eb84cdd
PC
107 // when m_dialog is deleted, it will destroy the widget it is sharing
108 // with GtkFileChooserButton, which results in a bunch of Gtk-CRITICAL
109 // errors from GtkFileChooserButton. To avoid this, call gtk_widget_destroy()
110 // on GtkFileChooserButton first (our base dtor will do it again, but
111 // that does no harm). m_dialog holds a reference to the shared widget,
112 // so it won't go away until m_dialog base dtor unrefs it.
113 gtk_widget_destroy(m_widget);
3ca5e2db
VZ
114 delete m_dialog;
115 }
ec376c8f
VZ
116}
117
118void wxFileButton::OnDialogOK(wxCommandEvent& ev)
119{
120 // the wxFileDialog associated with the GtkFileChooserButton has been closed
121 // using the OK button, thus the selected file has changed...
122 if (ev.GetId() == wxID_OK)
123 {
124 // ...update our path
556151f5 125 UpdatePathFromDialog(m_dialog);
ec376c8f
VZ
126
127 // ...and fire an event
ce7fe42e 128 wxFileDirPickerEvent event(wxEVT_FILEPICKER_CHANGED, this, GetId(), m_path);
937013e0 129 HandleWindowEvent(event);
ec376c8f
VZ
130 }
131}
132
58772e49
VZ
133void wxFileButton::SetPath(const wxString &str)
134{
135 m_path = str;
03647350 136
dee059c4
JS
137 if (m_dialog)
138 UpdateDialogPath(m_dialog);
58772e49
VZ
139}
140
75cb911c
VZ
141void wxFileButton::SetInitialDirectory(const wxString& dir)
142{
143 if (m_dialog)
06a41924
VZ
144 {
145 // Only change the directory if the default file name doesn't have any
146 // directory in it, otherwise it takes precedence.
147 if ( m_path.find_first_of(wxFileName::GetPathSeparators()) ==
148 wxString::npos )
149 {
150 static_cast<wxFileDialog*>(m_dialog)->SetDirectory(dir);
151 }
152 }
75cb911c
VZ
153 else
154 wxGenericFileButton::SetInitialDirectory(dir);
155}
156
4e621d24 157#endif // wxUSE_FILEPICKERCTRL
ec376c8f 158
4e621d24 159#if wxUSE_DIRPICKERCTRL
ec376c8f 160
f04f570f 161#ifdef __UNIX__
58772e49 162#include <unistd.h> // chdir
f04f570f 163#endif
58772e49 164
ec376c8f
VZ
165//-----------------------------------------------------------------------------
166// "current-folder-changed"
167//-----------------------------------------------------------------------------
168
169extern "C" {
170static void gtk_dirbutton_currentfolderchanged_callback(GtkFileChooserButton *widget,
171 wxDirButton *p)
172{
173 // update the m_path member of the wxDirButtonGTK
58772e49
VZ
174 // unless the path was changed by wxDirButton::SetPath()
175 if (p->m_bIgnoreNextChange)
176 {
177 p->m_bIgnoreNextChange=false;
178 return;
179 }
ec376c8f
VZ
180 wxASSERT(p);
181
182 // NB: it's important to use gtk_file_chooser_get_filename instead of
183 // gtk_file_chooser_get_current_folder (see GTK docs) !
e808cf8a 184 wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget)));
77d82770 185 p->GTKUpdatePath(filename);
ec376c8f
VZ
186
187 // since GtkFileChooserButton when used to pick directories also uses a combobox,
188 // maybe that the current folder has been changed but not through the GtkFileChooserDialog
189 // and thus the 'gtk_filedialog_ok_callback' could have not been called...
190 // thus we need to make sure the current working directory is updated if wxDIRP_CHANGE_DIR
191 // style was given.
192 if (p->HasFlag(wxDIRP_CHANGE_DIR))
193 chdir(filename);
ec376c8f
VZ
194
195 // ...and fire an event
ce7fe42e 196 wxFileDirPickerEvent event(wxEVT_DIRPICKER_CHANGED, p, p->GetId(), p->GetPath());
937013e0 197 p->HandleWindowEvent(event);
ec376c8f
VZ
198}
199}
200
201
202//-----------------------------------------------------------------------------
203// wxDirButtonGTK
204//-----------------------------------------------------------------------------
205
206IMPLEMENT_DYNAMIC_CLASS(wxDirButton, wxButton)
207
208bool wxDirButton::Create( wxWindow *parent, wxWindowID id,
209 const wxString &label, const wxString &path,
210 const wxString &message, const wxString &wildcard,
211 const wxPoint &pos, const wxSize &size,
212 long style, const wxValidator& validator,
213 const wxString &name )
214{
4e621d24 215 if (!(style & wxDIRP_USE_TEXTCTRL))
ec376c8f
VZ
216 {
217 // VERY IMPORTANT: this code is identic to relative code in wxFileButton;
218 // if you find a problem here, fix it also in wxFileButton !
219
ec376c8f
VZ
220 if (!PreCreation( parent, pos, size ) ||
221 !wxControl::CreateBase(parent, id, pos, size, style & wxWINDOW_STYLE_MASK,
222 validator, name))
223 {
224 wxFAIL_MSG( wxT("wxDirButtonGTK creation failed") );
225 return false;
226 }
227
228 // create the dialog associated with this button
229 SetWindowStyle(style);
556151f5
MW
230 m_message = message;
231 m_wildcard = wildcard;
232 if ((m_dialog = CreateDialog()) == NULL)
ec376c8f 233 return false;
58772e49 234 SetPath(path);
ec376c8f
VZ
235
236 // little trick used to avoid problems when there are other GTK windows 'grabbed':
237 // GtkFileChooserDialog won't be responsive to user events if there is another
238 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
239 // in modal mode in the application - see wxDialogGTK::ShowModal).
240 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
241 // is clicked and then remove it as soon as the user closes the dialog itself.
242 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
243 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
244 // hidden simply using its "show" and "hide" events - clean & simple :)
245 g_signal_connect(m_dialog->m_widget, "show", G_CALLBACK(gtk_grab_add), NULL);
246 g_signal_connect(m_dialog->m_widget, "hide", G_CALLBACK(gtk_grab_remove), NULL);
247
248
249 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
250 // use as label the currently selected file
251 m_widget = gtk_file_chooser_button_new_with_dialog( m_dialog->m_widget );
9ff9d30c 252 g_object_ref(m_widget);
ec376c8f 253
ec376c8f
VZ
254 // GtkFileChooserButton signals
255 g_signal_connect(m_widget, "current-folder-changed",
256 G_CALLBACK(gtk_dirbutton_currentfolderchanged_callback), this);
257
258 m_parent->DoAddChild( this );
259
260 PostCreation(size);
170acdc9 261 SetInitialSize(size);
ec376c8f
VZ
262 }
263 else
264 return wxGenericDirButton::Create(parent, id, label, path, message, wildcard,
265 pos, size, style, validator, name);
266 return true;
267}
268
269wxDirButton::~wxDirButton()
270{
7eb84cdd
PC
271 if (m_dialog)
272 {
273 // see ~wxFileButton() comment
274 gtk_widget_destroy(m_widget);
275 delete m_dialog;
276 }
ec376c8f
VZ
277}
278
77d82770 279void wxDirButton::GTKUpdatePath(const char *gtkpath)
03647350 280{
77d82770
RR
281 m_path = wxString::FromUTF8(gtkpath);
282}
faaa88e7 283void wxDirButton::SetPath(const wxString& str)
58772e49 284{
faaa88e7
VZ
285 if ( m_path == str )
286 {
287 // don't do anything and especially don't set m_bIgnoreNextChange
288 return;
289 }
290
58772e49
VZ
291 m_path = str;
292
293 // wxDirButton uses the "current-folder-changed" signal which is triggered also
294 // when we set the path on the dialog associated with this button; thus we need
295 // to set the following flag to avoid sending a wxFileDirPickerEvent from this
296 // function (which would be inconsistent with wxFileButton's behaviour and in
297 // general with all wxWidgets control-manipulation functions which do not send events).
298 m_bIgnoreNextChange = true;
299
dee059c4
JS
300 if (m_dialog)
301 UpdateDialogPath(m_dialog);
58772e49
VZ
302}
303
75cb911c
VZ
304void wxDirButton::SetInitialDirectory(const wxString& dir)
305{
306 if (m_dialog)
307 {
308 if (m_path.empty())
309 static_cast<wxDirDialog*>(m_dialog)->SetPath(dir);
310 }
311 else
312 wxGenericDirButton::SetInitialDirectory(dir);
313}
314
4e621d24 315#endif // wxUSE_DIRPICKERCTRL