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