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