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