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