picker controls improvements: fixes to valid paths recognition and event generation...
[wxWidgets.git] / src / gtk / filepicker.cpp
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 // Id: $Id$
8 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #if wxUSE_FILEPICKERCTRL && defined(__WXGTK26__)
21
22 #include "wx/filepicker.h"
23 #include "wx/tooltip.h"
24
25 #include <gtk/gtk.h>
26
27
28
29 // ============================================================================
30 // implementation
31 // ============================================================================
32
33 //-----------------------------------------------------------------------------
34 // wxFileButton
35 //-----------------------------------------------------------------------------
36
37 IMPLEMENT_DYNAMIC_CLASS(wxFileButton, wxButton)
38
39 bool wxFileButton::Create( wxWindow *parent, wxWindowID id,
40 const wxString &label, const wxString &path,
41 const wxString &message, const wxString &wildcard,
42 const wxPoint &pos, const wxSize &size,
43 long style, const wxValidator& validator,
44 const wxString &name )
45 {
46 if (!gtk_check_version(2,6,0))
47 {
48 // VERY IMPORTANT: this code is identic to relative code in wxFileButton;
49 // if you find a problem here, fix it also in wxFileButton !
50
51 m_needParent = true;
52
53 if (!PreCreation( parent, pos, size ) ||
54 !wxControl::CreateBase(parent, id, pos, size, style & wxWINDOW_STYLE_MASK,
55 validator, name))
56 {
57 wxFAIL_MSG( wxT("wxFileButton creation failed") );
58 return false;
59 }
60
61 // create the dialog associated with this button
62 // NB: unlike generic implementation, native GTK implementation needs to create
63 // the filedialog here as it needs to use gtk_file_chooser_button_new_with_dialog()
64 SetWindowStyle(style);
65 m_path = path;
66 m_message = message;
67 m_wildcard = wildcard;
68 if ((m_dialog = CreateDialog()) == NULL)
69 return false;
70
71 // little trick used to avoid problems when there are other GTK windows 'grabbed':
72 // GtkFileChooserDialog won't be responsive to user events if there is another
73 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
74 // in modal mode in the application - see wxDialogGTK::ShowModal).
75 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
76 // is clicked and then remove it as soon as the user closes the dialog itself.
77 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
78 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
79 // hidden simply using its "show" and "hide" events - clean & simple :)
80 g_signal_connect(m_dialog->m_widget, "show", G_CALLBACK(gtk_grab_add), NULL);
81 g_signal_connect(m_dialog->m_widget, "hide", G_CALLBACK(gtk_grab_remove), NULL);
82
83 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
84 // use as label the currently selected file
85 m_widget = gtk_file_chooser_button_new_with_dialog( m_dialog->m_widget );
86 gtk_widget_show( GTK_WIDGET(m_widget) );
87
88 // we need to know when the dialog has been dismissed clicking OK...
89 // NOTE: the "clicked" signal is not available for a GtkFileChooserButton
90 // thus we are forced to use wxFileDialog's event
91 m_dialog->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
92 wxCommandEventHandler(wxFileButton::OnDialogOK),
93 NULL, this);
94
95 m_parent->DoAddChild( this );
96
97 PostCreation(size);
98 SetBestSize(size);
99 }
100 else
101 return wxGenericFileButton::Create(parent, id, label, path, message, wildcard,
102 pos, size, style, validator, name);
103 return true;
104 }
105
106 wxFileButton::~wxFileButton()
107 {
108 // GtkFileChooserButton will automatically destroy the
109 // GtkFileChooserDialog associated with m_dialog.
110 // Thus we have to set its m_widget to NULL to avoid
111 // double destruction on same widget
112 m_dialog->m_widget = NULL;
113 }
114
115 void wxFileButton::OnDialogOK(wxCommandEvent& ev)
116 {
117 // the wxFileDialog associated with the GtkFileChooserButton has been closed
118 // using the OK button, thus the selected file has changed...
119 if (ev.GetId() == wxID_OK)
120 {
121 // ...update our path
122 UpdatePathFromDialog(m_dialog);
123
124 // ...and fire an event
125 wxFileDirPickerEvent event(wxEVT_COMMAND_FILEPICKER_CHANGED, this, GetId(), m_path);
126 GetEventHandler()->ProcessEvent(event);
127 }
128 }
129
130
131 void wxFileButton::SetPath(const wxString &str)
132 {
133 m_path = str;
134 UpdateDialogPath(m_dialog);
135 }
136
137 #endif // wxUSE_FILEPICKERCTRL && defined(__WXGTK26__)
138
139
140
141
142 #if wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)
143
144 #include <unistd.h> // chdir
145
146 //-----------------------------------------------------------------------------
147 // "current-folder-changed"
148 //-----------------------------------------------------------------------------
149
150 extern "C" {
151 static void gtk_dirbutton_currentfolderchanged_callback(GtkFileChooserButton *widget,
152 wxDirButton *p)
153 {
154 // update the m_path member of the wxDirButtonGTK
155 // unless the path was changed by wxDirButton::SetPath()
156 if (p->m_bIgnoreNextChange)
157 {
158 p->m_bIgnoreNextChange=false;
159 return;
160 }
161 wxASSERT(p);
162
163 // NB: it's important to use gtk_file_chooser_get_filename instead of
164 // gtk_file_chooser_get_current_folder (see GTK docs) !
165 gchar* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
166 p->UpdatePath(filename);
167
168 // since GtkFileChooserButton when used to pick directories also uses a combobox,
169 // maybe that the current folder has been changed but not through the GtkFileChooserDialog
170 // and thus the 'gtk_filedialog_ok_callback' could have not been called...
171 // thus we need to make sure the current working directory is updated if wxDIRP_CHANGE_DIR
172 // style was given.
173 if (p->HasFlag(wxDIRP_CHANGE_DIR))
174 chdir(filename);
175 g_free(filename);
176
177 // ...and fire an event
178 wxFileDirPickerEvent event(wxEVT_COMMAND_DIRPICKER_CHANGED, p, p->GetId(), p->GetPath());
179 p->GetEventHandler()->ProcessEvent(event);
180 }
181 }
182
183
184 //-----------------------------------------------------------------------------
185 // wxDirButtonGTK
186 //-----------------------------------------------------------------------------
187
188 IMPLEMENT_DYNAMIC_CLASS(wxDirButton, wxButton)
189
190 bool wxDirButton::Create( wxWindow *parent, wxWindowID id,
191 const wxString &label, const wxString &path,
192 const wxString &message, const wxString &wildcard,
193 const wxPoint &pos, const wxSize &size,
194 long style, const wxValidator& validator,
195 const wxString &name )
196 {
197 if (!gtk_check_version(2,6,0))
198 {
199 // VERY IMPORTANT: this code is identic to relative code in wxFileButton;
200 // if you find a problem here, fix it also in wxFileButton !
201
202 m_needParent = true;
203
204 if (!PreCreation( parent, pos, size ) ||
205 !wxControl::CreateBase(parent, id, pos, size, style & wxWINDOW_STYLE_MASK,
206 validator, name))
207 {
208 wxFAIL_MSG( wxT("wxDirButtonGTK creation failed") );
209 return false;
210 }
211
212 // create the dialog associated with this button
213 SetWindowStyle(style);
214 m_message = message;
215 m_wildcard = wildcard;
216 if ((m_dialog = CreateDialog()) == NULL)
217 return false;
218 SetPath(path);
219
220 // little trick used to avoid problems when there are other GTK windows 'grabbed':
221 // GtkFileChooserDialog won't be responsive to user events if there is another
222 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
223 // in modal mode in the application - see wxDialogGTK::ShowModal).
224 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
225 // is clicked and then remove it as soon as the user closes the dialog itself.
226 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
227 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
228 // hidden simply using its "show" and "hide" events - clean & simple :)
229 g_signal_connect(m_dialog->m_widget, "show", G_CALLBACK(gtk_grab_add), NULL);
230 g_signal_connect(m_dialog->m_widget, "hide", G_CALLBACK(gtk_grab_remove), NULL);
231
232
233 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
234 // use as label the currently selected file
235 m_widget = gtk_file_chooser_button_new_with_dialog( m_dialog->m_widget );
236
237 gtk_widget_show( GTK_WIDGET(m_widget) );
238
239 // GtkFileChooserButton signals
240 g_signal_connect(m_widget, "current-folder-changed",
241 G_CALLBACK(gtk_dirbutton_currentfolderchanged_callback), this);
242
243 m_parent->DoAddChild( this );
244
245 PostCreation(size);
246 SetBestSize(size);
247 }
248 else
249 return wxGenericDirButton::Create(parent, id, label, path, message, wildcard,
250 pos, size, style, validator, name);
251 return true;
252 }
253
254 wxDirButton::~wxDirButton()
255 {
256 // GtkFileChooserButton will automatically destroy the
257 // GtkFileChooserDialog associated with m_dialog.
258 // Thus we have to set its m_widget to NULL to avoid
259 // double destruction on same widget
260 m_dialog->m_widget = NULL;
261 }
262
263 void wxDirButton::SetPath(const wxString &str)
264 {
265 m_path = str;
266
267 // wxDirButton uses the "current-folder-changed" signal which is triggered also
268 // when we set the path on the dialog associated with this button; thus we need
269 // to set the following flag to avoid sending a wxFileDirPickerEvent from this
270 // function (which would be inconsistent with wxFileButton's behaviour and in
271 // general with all wxWidgets control-manipulation functions which do not send events).
272 m_bIgnoreNextChange = true;
273
274 UpdateDialogPath(m_dialog);
275 }
276
277 #endif // wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)