]>
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 | ||
4e621d24 | 20 | #if wxUSE_FILEPICKERCTRL |
4ce7b1e4 | 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 | |
4e621d24 | 47 | if (!(style & wxFLP_SAVE) && !(style & wxFLP_USE_TEXTCTRL)) |
ec376c8f | 48 | { |
77d82770 | 49 | // VERY IMPORTANT: this code is identical to relative code in wxDirButton; |
a65ffcb2 | 50 | // if you find a problem here, fix it also in wxDirButton ! |
ec376c8f | 51 | |
ec376c8f VZ |
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 | |
556151f5 MW |
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() | |
ec376c8f VZ |
63 | SetWindowStyle(style); |
64 | m_path = path; | |
556151f5 MW |
65 | m_message = message; |
66 | m_wildcard = wildcard; | |
67 | if ((m_dialog = CreateDialog()) == NULL) | |
ec376c8f | 68 | return false; |
03647350 | 69 | |
ec376c8f VZ |
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 | ||
ec376c8f VZ |
82 | // use as label the currently selected file |
83 | m_widget = gtk_file_chooser_button_new_with_dialog( m_dialog->m_widget ); | |
9ff9d30c | 84 | g_object_ref(m_widget); |
ec376c8f VZ |
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 | |
ce7fe42e | 89 | m_dialog->Connect(wxEVT_BUTTON, |
ec376c8f VZ |
90 | wxCommandEventHandler(wxFileButton::OnDialogOK), |
91 | NULL, this); | |
92 | ||
93 | m_parent->DoAddChild( this ); | |
94 | ||
95 | PostCreation(size); | |
170acdc9 | 96 | SetInitialSize(size); |
ec376c8f VZ |
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 | { | |
05534268 | 106 | delete m_dialog; |
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 | |
ce7fe42e | 119 | wxFileDirPickerEvent event(wxEVT_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) | |
06a41924 VZ |
135 | { |
136 | // Only change the directory if the default file name doesn't have any | |
137 | // directory in it, otherwise it takes precedence. | |
138 | if ( m_path.find_first_of(wxFileName::GetPathSeparators()) == | |
139 | wxString::npos ) | |
140 | { | |
141 | static_cast<wxFileDialog*>(m_dialog)->SetDirectory(dir); | |
142 | } | |
143 | } | |
75cb911c VZ |
144 | else |
145 | wxGenericFileButton::SetInitialDirectory(dir); | |
146 | } | |
147 | ||
4e621d24 | 148 | #endif // wxUSE_FILEPICKERCTRL |
ec376c8f | 149 | |
4e621d24 | 150 | #if wxUSE_DIRPICKERCTRL |
ec376c8f | 151 | |
f04f570f | 152 | #ifdef __UNIX__ |
58772e49 | 153 | #include <unistd.h> // chdir |
f04f570f | 154 | #endif |
58772e49 | 155 | |
ec376c8f VZ |
156 | //----------------------------------------------------------------------------- |
157 | // "current-folder-changed" | |
158 | //----------------------------------------------------------------------------- | |
159 | ||
160 | extern "C" { | |
161 | static void gtk_dirbutton_currentfolderchanged_callback(GtkFileChooserButton *widget, | |
162 | wxDirButton *p) | |
163 | { | |
164 | // update the m_path member of the wxDirButtonGTK | |
58772e49 VZ |
165 | // unless the path was changed by wxDirButton::SetPath() |
166 | if (p->m_bIgnoreNextChange) | |
167 | { | |
168 | p->m_bIgnoreNextChange=false; | |
169 | return; | |
170 | } | |
ec376c8f VZ |
171 | wxASSERT(p); |
172 | ||
173 | // NB: it's important to use gtk_file_chooser_get_filename instead of | |
174 | // gtk_file_chooser_get_current_folder (see GTK docs) ! | |
e808cf8a | 175 | wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget))); |
77d82770 | 176 | p->GTKUpdatePath(filename); |
ec376c8f VZ |
177 | |
178 | // since GtkFileChooserButton when used to pick directories also uses a combobox, | |
179 | // maybe that the current folder has been changed but not through the GtkFileChooserDialog | |
180 | // and thus the 'gtk_filedialog_ok_callback' could have not been called... | |
181 | // thus we need to make sure the current working directory is updated if wxDIRP_CHANGE_DIR | |
182 | // style was given. | |
183 | if (p->HasFlag(wxDIRP_CHANGE_DIR)) | |
184 | chdir(filename); | |
ec376c8f VZ |
185 | |
186 | // ...and fire an event | |
ce7fe42e | 187 | wxFileDirPickerEvent event(wxEVT_DIRPICKER_CHANGED, p, p->GetId(), p->GetPath()); |
937013e0 | 188 | p->HandleWindowEvent(event); |
ec376c8f VZ |
189 | } |
190 | } | |
191 | ||
192 | ||
193 | //----------------------------------------------------------------------------- | |
194 | // wxDirButtonGTK | |
195 | //----------------------------------------------------------------------------- | |
196 | ||
197 | IMPLEMENT_DYNAMIC_CLASS(wxDirButton, wxButton) | |
198 | ||
199 | bool wxDirButton::Create( wxWindow *parent, wxWindowID id, | |
200 | const wxString &label, const wxString &path, | |
201 | const wxString &message, const wxString &wildcard, | |
202 | const wxPoint &pos, const wxSize &size, | |
203 | long style, const wxValidator& validator, | |
204 | const wxString &name ) | |
205 | { | |
4e621d24 | 206 | if (!(style & wxDIRP_USE_TEXTCTRL)) |
ec376c8f VZ |
207 | { |
208 | // VERY IMPORTANT: this code is identic to relative code in wxFileButton; | |
209 | // if you find a problem here, fix it also in wxFileButton ! | |
210 | ||
ec376c8f VZ |
211 | if (!PreCreation( parent, pos, size ) || |
212 | !wxControl::CreateBase(parent, id, pos, size, style & wxWINDOW_STYLE_MASK, | |
213 | validator, name)) | |
214 | { | |
215 | wxFAIL_MSG( wxT("wxDirButtonGTK creation failed") ); | |
216 | return false; | |
217 | } | |
218 | ||
219 | // create the dialog associated with this button | |
220 | SetWindowStyle(style); | |
556151f5 MW |
221 | m_message = message; |
222 | m_wildcard = wildcard; | |
223 | if ((m_dialog = CreateDialog()) == NULL) | |
ec376c8f | 224 | return false; |
58772e49 | 225 | SetPath(path); |
ec376c8f VZ |
226 | |
227 | // little trick used to avoid problems when there are other GTK windows 'grabbed': | |
228 | // GtkFileChooserDialog won't be responsive to user events if there is another | |
229 | // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running | |
230 | // in modal mode in the application - see wxDialogGTK::ShowModal). | |
231 | // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton | |
232 | // is clicked and then remove it as soon as the user closes the dialog itself. | |
233 | // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton, | |
234 | // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's | |
235 | // hidden simply using its "show" and "hide" events - clean & simple :) | |
236 | g_signal_connect(m_dialog->m_widget, "show", G_CALLBACK(gtk_grab_add), NULL); | |
237 | g_signal_connect(m_dialog->m_widget, "hide", G_CALLBACK(gtk_grab_remove), NULL); | |
238 | ||
239 | ||
240 | // NOTE: we deliberately ignore the given label as GtkFileChooserButton | |
241 | // use as label the currently selected file | |
242 | m_widget = gtk_file_chooser_button_new_with_dialog( m_dialog->m_widget ); | |
9ff9d30c | 243 | g_object_ref(m_widget); |
ec376c8f | 244 | |
ec376c8f VZ |
245 | // GtkFileChooserButton signals |
246 | g_signal_connect(m_widget, "current-folder-changed", | |
247 | G_CALLBACK(gtk_dirbutton_currentfolderchanged_callback), this); | |
248 | ||
249 | m_parent->DoAddChild( this ); | |
250 | ||
251 | PostCreation(size); | |
170acdc9 | 252 | SetInitialSize(size); |
ec376c8f VZ |
253 | } |
254 | else | |
255 | return wxGenericDirButton::Create(parent, id, label, path, message, wildcard, | |
256 | pos, size, style, validator, name); | |
257 | return true; | |
258 | } | |
259 | ||
260 | wxDirButton::~wxDirButton() | |
261 | { | |
05534268 | 262 | delete m_dialog; |
ec376c8f VZ |
263 | } |
264 | ||
77d82770 | 265 | void wxDirButton::GTKUpdatePath(const char *gtkpath) |
03647350 | 266 | { |
77d82770 RR |
267 | m_path = wxString::FromUTF8(gtkpath); |
268 | } | |
faaa88e7 | 269 | void wxDirButton::SetPath(const wxString& str) |
58772e49 | 270 | { |
faaa88e7 VZ |
271 | if ( m_path == str ) |
272 | { | |
273 | // don't do anything and especially don't set m_bIgnoreNextChange | |
274 | return; | |
275 | } | |
276 | ||
58772e49 VZ |
277 | m_path = str; |
278 | ||
279 | // wxDirButton uses the "current-folder-changed" signal which is triggered also | |
280 | // when we set the path on the dialog associated with this button; thus we need | |
281 | // to set the following flag to avoid sending a wxFileDirPickerEvent from this | |
282 | // function (which would be inconsistent with wxFileButton's behaviour and in | |
283 | // general with all wxWidgets control-manipulation functions which do not send events). | |
284 | m_bIgnoreNextChange = true; | |
285 | ||
dee059c4 JS |
286 | if (m_dialog) |
287 | UpdateDialogPath(m_dialog); | |
58772e49 VZ |
288 | } |
289 | ||
75cb911c VZ |
290 | void wxDirButton::SetInitialDirectory(const wxString& dir) |
291 | { | |
292 | if (m_dialog) | |
293 | { | |
294 | if (m_path.empty()) | |
295 | static_cast<wxDirDialog*>(m_dialog)->SetPath(dir); | |
296 | } | |
297 | else | |
298 | wxGenericDirButton::SetInitialDirectory(dir); | |
299 | } | |
300 | ||
4e621d24 | 301 | #endif // wxUSE_DIRPICKERCTRL |