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