]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/filepicker.cpp
More comments about non-native GTH widgets.
[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 wxDirButton;
49 // if you find a problem here, fix it also in wxDirButton !
50
51 m_needParent = true;
52 m_acceptsFocus = true;
53
54 if (!PreCreation( parent, pos, size ) ||
55 !wxControl::CreateBase(parent, id, pos, size, style & wxWINDOW_STYLE_MASK,
56 validator, name))
57 {
58 wxFAIL_MSG( wxT("wxFileButton creation failed") );
59 return false;
60 }
61
62 // create the dialog associated with this button
63 // NB: unlike generic implementation, native GTK implementation needs to create
64 // the filedialog here as it needs to use gtk_file_chooser_button_new_with_dialog()
65 SetWindowStyle(style);
66 m_path = path;
67 m_message = message;
68 m_wildcard = wildcard;
69 if ((m_dialog = CreateDialog()) == NULL)
70 return false;
71
72 // little trick used to avoid problems when there are other GTK windows 'grabbed':
73 // GtkFileChooserDialog won't be responsive to user events if there is another
74 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
75 // in modal mode in the application - see wxDialogGTK::ShowModal).
76 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
77 // is clicked and then remove it as soon as the user closes the dialog itself.
78 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
79 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
80 // hidden simply using its "show" and "hide" events - clean & simple :)
81 g_signal_connect(m_dialog->m_widget, "show", G_CALLBACK(gtk_grab_add), NULL);
82 g_signal_connect(m_dialog->m_widget, "hide", G_CALLBACK(gtk_grab_remove), NULL);
83
84 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
85 // use as label the currently selected file
86 m_widget = gtk_file_chooser_button_new_with_dialog( m_dialog->m_widget );
87 gtk_widget_show( GTK_WIDGET(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_COMMAND_BUTTON_CLICKED,
93 wxCommandEventHandler(wxFileButton::OnDialogOK),
94 NULL, this);
95
96 m_parent->DoAddChild( this );
97
98 PostCreation(size);
99 SetBestSize(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
107 wxFileButton::~wxFileButton()
108 {
109 // GtkFileChooserButton will automatically destroy the
110 // GtkFileChooserDialog associated with m_dialog.
111 // Thus we have to set its m_widget to NULL to avoid
112 // double destruction on same widget
113 m_dialog->m_widget = NULL;
114 }
115
116 void wxFileButton::OnDialogOK(wxCommandEvent& ev)
117 {
118 // the wxFileDialog associated with the GtkFileChooserButton has been closed
119 // using the OK button, thus the selected file has changed...
120 if (ev.GetId() == wxID_OK)
121 {
122 // ...update our path
123 UpdatePathFromDialog(m_dialog);
124
125 // ...and fire an event
126 wxFileDirPickerEvent event(wxEVT_COMMAND_FILEPICKER_CHANGED, this, GetId(), m_path);
127 GetEventHandler()->ProcessEvent(event);
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 m_acceptsFocus = true;
204
205 if (!PreCreation( parent, pos, size ) ||
206 !wxControl::CreateBase(parent, id, pos, size, style & wxWINDOW_STYLE_MASK,
207 validator, name))
208 {
209 wxFAIL_MSG( wxT("wxDirButtonGTK creation failed") );
210 return false;
211 }
212
213 // create the dialog associated with this button
214 SetWindowStyle(style);
215 m_message = message;
216 m_wildcard = wildcard;
217 if ((m_dialog = CreateDialog()) == NULL)
218 return false;
219 SetPath(path);
220
221 // little trick used to avoid problems when there are other GTK windows 'grabbed':
222 // GtkFileChooserDialog won't be responsive to user events if there is another
223 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
224 // in modal mode in the application - see wxDialogGTK::ShowModal).
225 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
226 // is clicked and then remove it as soon as the user closes the dialog itself.
227 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
228 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
229 // hidden simply using its "show" and "hide" events - clean & simple :)
230 g_signal_connect(m_dialog->m_widget, "show", G_CALLBACK(gtk_grab_add), NULL);
231 g_signal_connect(m_dialog->m_widget, "hide", G_CALLBACK(gtk_grab_remove), NULL);
232
233
234 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
235 // use as label the currently selected file
236 m_widget = gtk_file_chooser_button_new_with_dialog( m_dialog->m_widget );
237
238 gtk_widget_show( GTK_WIDGET(m_widget) );
239
240 // GtkFileChooserButton signals
241 g_signal_connect(m_widget, "current-folder-changed",
242 G_CALLBACK(gtk_dirbutton_currentfolderchanged_callback), this);
243
244 m_parent->DoAddChild( this );
245
246 PostCreation(size);
247 SetBestSize(size);
248 }
249 else
250 return wxGenericDirButton::Create(parent, id, label, path, message, wildcard,
251 pos, size, style, validator, name);
252 return true;
253 }
254
255 wxDirButton::~wxDirButton()
256 {
257 // GtkFileChooserButton will automatically destroy the
258 // GtkFileChooserDialog associated with m_dialog.
259 // Thus we have to set its m_widget to NULL to avoid
260 // double destruction on same widget
261 m_dialog->m_widget = NULL;
262 }
263
264 void wxDirButton::SetPath(const wxString &str)
265 {
266 m_path = str;
267
268 // wxDirButton uses the "current-folder-changed" signal which is triggered also
269 // when we set the path on the dialog associated with this button; thus we need
270 // to set the following flag to avoid sending a wxFileDirPickerEvent from this
271 // function (which would be inconsistent with wxFileButton's behaviour and in
272 // general with all wxWidgets control-manipulation functions which do not send events).
273 m_bIgnoreNextChange = true;
274
275 UpdateDialogPath(m_dialog);
276 }
277
278 #endif // wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)