]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/filepicker.cpp
Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / src / gtk / filepicker.cpp
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 // Copyright: (c) Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 // ----------------------------------------------------------------------------
13 // headers
14 // ----------------------------------------------------------------------------
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #if wxUSE_FILEPICKERCTRL
20
21 #include "wx/filepicker.h"
22 #include "wx/tooltip.h"
23
24 #include <gtk/gtk.h>
25 #include "wx/gtk/private.h"
26
27 // ============================================================================
28 // implementation
29 // ============================================================================
30
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 // 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
46 if (!(style & wxFLP_SAVE) && !(style & wxFLP_USE_TEXTCTRL))
47 {
48 // VERY IMPORTANT: this code is identical to relative code in wxDirButton;
49 // if you find a problem here, fix it also in wxDirButton !
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
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()
62 SetWindowStyle(style);
63 m_path = path;
64 m_message = message;
65 m_wildcard = wildcard;
66 if ((m_dialog = CreateDialog()) == NULL)
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 // use as label the currently selected file
82 m_widget = gtk_file_chooser_button_new_with_dialog( m_dialog->m_widget );
83 g_object_ref(m_widget);
84
85 // we need to know when the dialog has been dismissed clicking OK...
86 // NOTE: the "clicked" signal is not available for a GtkFileChooserButton
87 // thus we are forced to use wxFileDialog's event
88 m_dialog->Connect(wxEVT_BUTTON,
89 wxCommandEventHandler(wxFileButton::OnDialogOK),
90 NULL, this);
91
92 m_parent->DoAddChild( this );
93
94 PostCreation(size);
95 SetInitialSize(size);
96 }
97 else
98 return wxGenericFileButton::Create(parent, id, label, path, message, wildcard,
99 pos, size, style, validator, name);
100 return true;
101 }
102
103 wxFileButton::~wxFileButton()
104 {
105 if ( m_dialog )
106 {
107 // We need to delete the C++ dialog object here but we shouldn't delete
108 // its widget which is used by our GtkFileChooserButton and will be
109 // deleted by it when it is itself destroyed in our base class dtor. So
110 // take the widget ownership away from the dialog to avoid GTK+ errors
111 // that would happen if GtkFileChooserButton tried to access the
112 // already destroyed dialog widget.
113 g_object_unref(m_dialog->m_widget);
114 m_dialog->m_widget = NULL;
115 delete m_dialog;
116 }
117 }
118
119 void wxFileButton::OnDialogOK(wxCommandEvent& ev)
120 {
121 // the wxFileDialog associated with the GtkFileChooserButton has been closed
122 // using the OK button, thus the selected file has changed...
123 if (ev.GetId() == wxID_OK)
124 {
125 // ...update our path
126 UpdatePathFromDialog(m_dialog);
127
128 // ...and fire an event
129 wxFileDirPickerEvent event(wxEVT_FILEPICKER_CHANGED, this, GetId(), m_path);
130 HandleWindowEvent(event);
131 }
132 }
133
134 void wxFileButton::SetPath(const wxString &str)
135 {
136 m_path = str;
137
138 if (m_dialog)
139 UpdateDialogPath(m_dialog);
140 }
141
142 void wxFileButton::SetInitialDirectory(const wxString& dir)
143 {
144 if (m_dialog)
145 {
146 // Only change the directory if the default file name doesn't have any
147 // directory in it, otherwise it takes precedence.
148 if ( m_path.find_first_of(wxFileName::GetPathSeparators()) ==
149 wxString::npos )
150 {
151 static_cast<wxFileDialog*>(m_dialog)->SetDirectory(dir);
152 }
153 }
154 else
155 wxGenericFileButton::SetInitialDirectory(dir);
156 }
157
158 #endif // wxUSE_FILEPICKERCTRL
159
160 #if wxUSE_DIRPICKERCTRL
161
162 #ifdef __UNIX__
163 #include <unistd.h> // chdir
164 #endif
165
166 //-----------------------------------------------------------------------------
167 // "current-folder-changed"
168 //-----------------------------------------------------------------------------
169
170 extern "C" {
171 static void gtk_dirbutton_currentfolderchanged_callback(GtkFileChooserButton *widget,
172 wxDirButton *p)
173 {
174 // update the m_path member of the wxDirButtonGTK
175 // unless the path was changed by wxDirButton::SetPath()
176 if (p->m_bIgnoreNextChange)
177 {
178 p->m_bIgnoreNextChange=false;
179 return;
180 }
181 wxASSERT(p);
182
183 // NB: it's important to use gtk_file_chooser_get_filename instead of
184 // gtk_file_chooser_get_current_folder (see GTK docs) !
185 wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget)));
186 p->GTKUpdatePath(filename);
187
188 // since GtkFileChooserButton when used to pick directories also uses a combobox,
189 // maybe that the current folder has been changed but not through the GtkFileChooserDialog
190 // and thus the 'gtk_filedialog_ok_callback' could have not been called...
191 // thus we need to make sure the current working directory is updated if wxDIRP_CHANGE_DIR
192 // style was given.
193 if (p->HasFlag(wxDIRP_CHANGE_DIR))
194 chdir(filename);
195
196 // ...and fire an event
197 wxFileDirPickerEvent event(wxEVT_DIRPICKER_CHANGED, p, p->GetId(), p->GetPath());
198 p->HandleWindowEvent(event);
199 }
200 }
201
202
203 //-----------------------------------------------------------------------------
204 // wxDirButtonGTK
205 //-----------------------------------------------------------------------------
206
207 IMPLEMENT_DYNAMIC_CLASS(wxDirButton, wxButton)
208
209 bool wxDirButton::Create( wxWindow *parent, wxWindowID id,
210 const wxString &label, const wxString &path,
211 const wxString &message, const wxString &wildcard,
212 const wxPoint &pos, const wxSize &size,
213 long style, const wxValidator& validator,
214 const wxString &name )
215 {
216 if (!(style & wxDIRP_USE_TEXTCTRL))
217 {
218 // VERY IMPORTANT: this code is identic to relative code in wxFileButton;
219 // if you find a problem here, fix it also in wxFileButton !
220
221 if (!PreCreation( parent, pos, size ) ||
222 !wxControl::CreateBase(parent, id, pos, size, style & wxWINDOW_STYLE_MASK,
223 validator, name))
224 {
225 wxFAIL_MSG( wxT("wxDirButtonGTK creation failed") );
226 return false;
227 }
228
229 // create the dialog associated with this button
230 SetWindowStyle(style);
231 m_message = message;
232 m_wildcard = wildcard;
233 if ((m_dialog = CreateDialog()) == NULL)
234 return false;
235 SetPath(path);
236
237 // little trick used to avoid problems when there are other GTK windows 'grabbed':
238 // GtkFileChooserDialog won't be responsive to user events if there is another
239 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
240 // in modal mode in the application - see wxDialogGTK::ShowModal).
241 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
242 // is clicked and then remove it as soon as the user closes the dialog itself.
243 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
244 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
245 // hidden simply using its "show" and "hide" events - clean & simple :)
246 g_signal_connect(m_dialog->m_widget, "show", G_CALLBACK(gtk_grab_add), NULL);
247 g_signal_connect(m_dialog->m_widget, "hide", G_CALLBACK(gtk_grab_remove), NULL);
248
249
250 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
251 // use as label the currently selected file
252 m_widget = gtk_file_chooser_button_new_with_dialog( m_dialog->m_widget );
253 g_object_ref(m_widget);
254
255 // GtkFileChooserButton signals
256 g_signal_connect(m_widget, "current-folder-changed",
257 G_CALLBACK(gtk_dirbutton_currentfolderchanged_callback), this);
258
259 m_parent->DoAddChild( this );
260
261 PostCreation(size);
262 SetInitialSize(size);
263 }
264 else
265 return wxGenericDirButton::Create(parent, id, label, path, message, wildcard,
266 pos, size, style, validator, name);
267 return true;
268 }
269
270 wxDirButton::~wxDirButton()
271 {
272 delete m_dialog;
273 }
274
275 void wxDirButton::GTKUpdatePath(const char *gtkpath)
276 {
277 m_path = wxString::FromUTF8(gtkpath);
278 }
279 void wxDirButton::SetPath(const wxString& str)
280 {
281 if ( m_path == str )
282 {
283 // don't do anything and especially don't set m_bIgnoreNextChange
284 return;
285 }
286
287 m_path = str;
288
289 // wxDirButton uses the "current-folder-changed" signal which is triggered also
290 // when we set the path on the dialog associated with this button; thus we need
291 // to set the following flag to avoid sending a wxFileDirPickerEvent from this
292 // function (which would be inconsistent with wxFileButton's behaviour and in
293 // general with all wxWidgets control-manipulation functions which do not send events).
294 m_bIgnoreNextChange = true;
295
296 if (m_dialog)
297 UpdateDialogPath(m_dialog);
298 }
299
300 void wxDirButton::SetInitialDirectory(const wxString& dir)
301 {
302 if (m_dialog)
303 {
304 if (m_path.empty())
305 static_cast<wxDirDialog*>(m_dialog)->SetPath(dir);
306 }
307 else
308 wxGenericDirButton::SetInitialDirectory(dir);
309 }
310
311 #endif // wxUSE_DIRPICKERCTRL