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