]>
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 | { | |
44 | if (!gtk_check_version(2,6,0)) | |
45 | { | |
a65ffcb2 VZ |
46 | // VERY IMPORTANT: this code is identic to relative code in wxDirButton; |
47 | // if you find a problem here, fix it also in wxDirButton ! | |
ec376c8f VZ |
48 | |
49 | m_needParent = true; | |
50 | ||
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 VZ |
67 | return false; |
68 | ||
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 | ||
81 | // NOTE: we deliberately ignore the given label as GtkFileChooserButton | |
82 | // use as label the currently selected file | |
83 | m_widget = gtk_file_chooser_button_new_with_dialog( m_dialog->m_widget ); | |
84 | gtk_widget_show( GTK_WIDGET(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); | |
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 | { | |
106 | // GtkFileChooserButton will automatically destroy the | |
107 | // GtkFileChooserDialog associated with m_dialog. | |
108 | // Thus we have to set its m_widget to NULL to avoid | |
109 | // double destruction on same widget | |
110 | m_dialog->m_widget = NULL; | |
111 | } | |
112 | ||
113 | void wxFileButton::OnDialogOK(wxCommandEvent& ev) | |
114 | { | |
115 | // the wxFileDialog associated with the GtkFileChooserButton has been closed | |
116 | // using the OK button, thus the selected file has changed... | |
117 | if (ev.GetId() == wxID_OK) | |
118 | { | |
119 | // ...update our path | |
556151f5 | 120 | UpdatePathFromDialog(m_dialog); |
ec376c8f VZ |
121 | |
122 | // ...and fire an event | |
123 | wxFileDirPickerEvent event(wxEVT_COMMAND_FILEPICKER_CHANGED, this, GetId(), m_path); | |
124 | GetEventHandler()->ProcessEvent(event); | |
125 | } | |
126 | } | |
127 | ||
58772e49 VZ |
128 | void wxFileButton::SetPath(const wxString &str) |
129 | { | |
130 | m_path = str; | |
131 | UpdateDialogPath(m_dialog); | |
132 | } | |
133 | ||
ec376c8f VZ |
134 | #endif // wxUSE_FILEPICKERCTRL && defined(__WXGTK26__) |
135 | ||
136 | ||
137 | ||
138 | ||
139 | #if wxUSE_DIRPICKERCTRL && defined(__WXGTK26__) | |
140 | ||
58772e49 VZ |
141 | #include <unistd.h> // chdir |
142 | ||
ec376c8f VZ |
143 | //----------------------------------------------------------------------------- |
144 | // "current-folder-changed" | |
145 | //----------------------------------------------------------------------------- | |
146 | ||
147 | extern "C" { | |
148 | static void gtk_dirbutton_currentfolderchanged_callback(GtkFileChooserButton *widget, | |
149 | wxDirButton *p) | |
150 | { | |
151 | // update the m_path member of the wxDirButtonGTK | |
58772e49 VZ |
152 | // unless the path was changed by wxDirButton::SetPath() |
153 | if (p->m_bIgnoreNextChange) | |
154 | { | |
155 | p->m_bIgnoreNextChange=false; | |
156 | return; | |
157 | } | |
ec376c8f VZ |
158 | wxASSERT(p); |
159 | ||
160 | // NB: it's important to use gtk_file_chooser_get_filename instead of | |
161 | // gtk_file_chooser_get_current_folder (see GTK docs) ! | |
e808cf8a | 162 | wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget))); |
ec376c8f VZ |
163 | p->UpdatePath(filename); |
164 | ||
165 | // since GtkFileChooserButton when used to pick directories also uses a combobox, | |
166 | // maybe that the current folder has been changed but not through the GtkFileChooserDialog | |
167 | // and thus the 'gtk_filedialog_ok_callback' could have not been called... | |
168 | // thus we need to make sure the current working directory is updated if wxDIRP_CHANGE_DIR | |
169 | // style was given. | |
170 | if (p->HasFlag(wxDIRP_CHANGE_DIR)) | |
171 | chdir(filename); | |
ec376c8f VZ |
172 | |
173 | // ...and fire an event | |
174 | wxFileDirPickerEvent event(wxEVT_COMMAND_DIRPICKER_CHANGED, p, p->GetId(), p->GetPath()); | |
175 | p->GetEventHandler()->ProcessEvent(event); | |
176 | } | |
177 | } | |
178 | ||
179 | ||
180 | //----------------------------------------------------------------------------- | |
181 | // wxDirButtonGTK | |
182 | //----------------------------------------------------------------------------- | |
183 | ||
184 | IMPLEMENT_DYNAMIC_CLASS(wxDirButton, wxButton) | |
185 | ||
186 | bool wxDirButton::Create( wxWindow *parent, wxWindowID id, | |
187 | const wxString &label, const wxString &path, | |
188 | const wxString &message, const wxString &wildcard, | |
189 | const wxPoint &pos, const wxSize &size, | |
190 | long style, const wxValidator& validator, | |
191 | const wxString &name ) | |
192 | { | |
193 | if (!gtk_check_version(2,6,0)) | |
194 | { | |
195 | // VERY IMPORTANT: this code is identic to relative code in wxFileButton; | |
196 | // if you find a problem here, fix it also in wxFileButton ! | |
197 | ||
198 | m_needParent = true; | |
199 | ||
200 | if (!PreCreation( parent, pos, size ) || | |
201 | !wxControl::CreateBase(parent, id, pos, size, style & wxWINDOW_STYLE_MASK, | |
202 | validator, name)) | |
203 | { | |
204 | wxFAIL_MSG( wxT("wxDirButtonGTK creation failed") ); | |
205 | return false; | |
206 | } | |
207 | ||
208 | // create the dialog associated with this button | |
209 | SetWindowStyle(style); | |
556151f5 MW |
210 | m_message = message; |
211 | m_wildcard = wildcard; | |
212 | if ((m_dialog = CreateDialog()) == NULL) | |
ec376c8f | 213 | return false; |
58772e49 | 214 | SetPath(path); |
ec376c8f VZ |
215 | |
216 | // little trick used to avoid problems when there are other GTK windows 'grabbed': | |
217 | // GtkFileChooserDialog won't be responsive to user events if there is another | |
218 | // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running | |
219 | // in modal mode in the application - see wxDialogGTK::ShowModal). | |
220 | // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton | |
221 | // is clicked and then remove it as soon as the user closes the dialog itself. | |
222 | // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton, | |
223 | // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's | |
224 | // hidden simply using its "show" and "hide" events - clean & simple :) | |
225 | g_signal_connect(m_dialog->m_widget, "show", G_CALLBACK(gtk_grab_add), NULL); | |
226 | g_signal_connect(m_dialog->m_widget, "hide", G_CALLBACK(gtk_grab_remove), NULL); | |
227 | ||
228 | ||
229 | // NOTE: we deliberately ignore the given label as GtkFileChooserButton | |
230 | // use as label the currently selected file | |
231 | m_widget = gtk_file_chooser_button_new_with_dialog( m_dialog->m_widget ); | |
232 | ||
233 | gtk_widget_show( GTK_WIDGET(m_widget) ); | |
234 | ||
235 | // GtkFileChooserButton signals | |
236 | g_signal_connect(m_widget, "current-folder-changed", | |
237 | G_CALLBACK(gtk_dirbutton_currentfolderchanged_callback), this); | |
238 | ||
239 | m_parent->DoAddChild( this ); | |
240 | ||
241 | PostCreation(size); | |
170acdc9 | 242 | SetInitialSize(size); |
ec376c8f VZ |
243 | } |
244 | else | |
245 | return wxGenericDirButton::Create(parent, id, label, path, message, wildcard, | |
246 | pos, size, style, validator, name); | |
247 | return true; | |
248 | } | |
249 | ||
250 | wxDirButton::~wxDirButton() | |
251 | { | |
252 | // GtkFileChooserButton will automatically destroy the | |
253 | // GtkFileChooserDialog associated with m_dialog. | |
254 | // Thus we have to set its m_widget to NULL to avoid | |
255 | // double destruction on same widget | |
256 | m_dialog->m_widget = NULL; | |
257 | } | |
258 | ||
58772e49 VZ |
259 | void wxDirButton::SetPath(const wxString &str) |
260 | { | |
261 | m_path = str; | |
262 | ||
263 | // wxDirButton uses the "current-folder-changed" signal which is triggered also | |
264 | // when we set the path on the dialog associated with this button; thus we need | |
265 | // to set the following flag to avoid sending a wxFileDirPickerEvent from this | |
266 | // function (which would be inconsistent with wxFileButton's behaviour and in | |
267 | // general with all wxWidgets control-manipulation functions which do not send events). | |
268 | m_bIgnoreNextChange = true; | |
269 | ||
270 | UpdateDialogPath(m_dialog); | |
271 | } | |
272 | ||
ec376c8f | 273 | #endif // wxUSE_DIRPICKERCTRL && defined(__WXGTK26__) |