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