]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/filepicker.cpp
Commit pickers-fixes.patch added to 1472329 (Francesco Montorsi)
[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
24 #include "wx/gtk/private.h"
25
26 #if wxUSE_TOOLTIPS
27 #include "wx/tooltip.h"
28 #endif
29
30 #include <gdk/gdk.h>
31 #include <gtk/gtk.h>
32
33 #include <unistd.h> // chdir
34
35
36 // ============================================================================
37 // implementation
38 // ============================================================================
39
40 //-----------------------------------------------------------------------------
41 // wxFileButton
42 //-----------------------------------------------------------------------------
43
44 IMPLEMENT_DYNAMIC_CLASS(wxFileButton, wxButton)
45
46 bool wxFileButton::Create( wxWindow *parent, wxWindowID id,
47 const wxString &label, const wxString &path,
48 const wxString &message, const wxString &wildcard,
49 const wxPoint &pos, const wxSize &size,
50 long style, const wxValidator& validator,
51 const wxString &name )
52 {
53 if (!gtk_check_version(2,6,0))
54 {
55 // VERY IMPORTANT: this code is identic to relative code in wxFileButton;
56 // if you find a problem here, fix it also in wxFileButton !
57
58 m_needParent = true;
59
60 if (!PreCreation( parent, pos, size ) ||
61 !wxControl::CreateBase(parent, id, pos, size, style & wxWINDOW_STYLE_MASK,
62 validator, name))
63 {
64 wxFAIL_MSG( wxT("wxFileButton creation failed") );
65 return false;
66 }
67
68 // create the dialog associated with this button
69 // NB: unlike generic implementation, native GTK implementation needs to create
70 // the filedialog here as it needs to use gtk_file_chooser_button_new_with_dialog()
71 SetWindowStyle(style);
72 m_path = path;
73 m_message = message;
74 m_wildcard = wildcard;
75 if ((m_dialog = CreateDialog()) == NULL)
76 return false;
77
78 // little trick used to avoid problems when there are other GTK windows 'grabbed':
79 // GtkFileChooserDialog won't be responsive to user events if there is another
80 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
81 // in modal mode in the application - see wxDialogGTK::ShowModal).
82 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
83 // is clicked and then remove it as soon as the user closes the dialog itself.
84 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
85 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
86 // hidden simply using its "show" and "hide" events - clean & simple :)
87 g_signal_connect(m_dialog->m_widget, "show", G_CALLBACK(gtk_grab_add), NULL);
88 g_signal_connect(m_dialog->m_widget, "hide", G_CALLBACK(gtk_grab_remove), NULL);
89
90 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
91 // use as label the currently selected file
92 m_widget = gtk_file_chooser_button_new_with_dialog( m_dialog->m_widget );
93 gtk_widget_show( GTK_WIDGET(m_widget) );
94
95 // we need to know when the dialog has been dismissed clicking OK...
96 // NOTE: the "clicked" signal is not available for a GtkFileChooserButton
97 // thus we are forced to use wxFileDialog's event
98 m_dialog->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
99 wxCommandEventHandler(wxFileButton::OnDialogOK),
100 NULL, this);
101
102 m_parent->DoAddChild( this );
103
104 PostCreation(size);
105 SetBestSize(size);
106 }
107 else
108 return wxGenericFileButton::Create(parent, id, label, path, message, wildcard,
109 pos, size, style, validator, name);
110 return true;
111 }
112
113 wxFileButton::~wxFileButton()
114 {
115 // GtkFileChooserButton will automatically destroy the
116 // GtkFileChooserDialog associated with m_dialog.
117 // Thus we have to set its m_widget to NULL to avoid
118 // double destruction on same widget
119 m_dialog->m_widget = NULL;
120 }
121
122 void wxFileButton::OnDialogOK(wxCommandEvent& ev)
123 {
124 // the wxFileDialog associated with the GtkFileChooserButton has been closed
125 // using the OK button, thus the selected file has changed...
126 if (ev.GetId() == wxID_OK)
127 {
128 // ...update our path
129 UpdatePathFromDialog(m_dialog);
130
131 // ...and fire an event
132 wxFileDirPickerEvent event(wxEVT_COMMAND_FILEPICKER_CHANGED, this, GetId(), m_path);
133 GetEventHandler()->ProcessEvent(event);
134 }
135 }
136
137 #endif // wxUSE_FILEPICKERCTRL && defined(__WXGTK26__)
138
139
140
141
142 #if wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)
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 wxASSERT(p);
154
155 // NB: it's important to use gtk_file_chooser_get_filename instead of
156 // gtk_file_chooser_get_current_folder (see GTK docs) !
157 gchar* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
158 p->UpdatePath(filename);
159
160 // since GtkFileChooserButton when used to pick directories also uses a combobox,
161 // maybe that the current folder has been changed but not through the GtkFileChooserDialog
162 // and thus the 'gtk_filedialog_ok_callback' could have not been called...
163 // thus we need to make sure the current working directory is updated if wxDIRP_CHANGE_DIR
164 // style was given.
165 if (p->HasFlag(wxDIRP_CHANGE_DIR))
166 chdir(filename);
167 g_free(filename);
168
169 // ...and fire an event
170 wxFileDirPickerEvent event(wxEVT_COMMAND_DIRPICKER_CHANGED, p, p->GetId(), p->GetPath());
171 p->GetEventHandler()->ProcessEvent(event);
172 }
173 }
174
175
176 //-----------------------------------------------------------------------------
177 // wxDirButtonGTK
178 //-----------------------------------------------------------------------------
179
180 IMPLEMENT_DYNAMIC_CLASS(wxDirButton, wxButton)
181
182 bool wxDirButton::Create( wxWindow *parent, wxWindowID id,
183 const wxString &label, const wxString &path,
184 const wxString &message, const wxString &wildcard,
185 const wxPoint &pos, const wxSize &size,
186 long style, const wxValidator& validator,
187 const wxString &name )
188 {
189 if (!gtk_check_version(2,6,0))
190 {
191 // VERY IMPORTANT: this code is identic to relative code in wxFileButton;
192 // if you find a problem here, fix it also in wxFileButton !
193
194 m_needParent = true;
195
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);
206 m_message = message;
207 m_wildcard = wildcard;
208 if ((m_dialog = CreateDialog()) == NULL)
209 return false;
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 );
227
228 gtk_widget_show( GTK_WIDGET(m_widget) );
229
230 // GtkFileChooserButton signals
231 g_signal_connect(m_widget, "current-folder-changed",
232 G_CALLBACK(gtk_dirbutton_currentfolderchanged_callback), this);
233
234 m_parent->DoAddChild( this );
235
236 PostCreation(size);
237 SetBestSize(size);
238 }
239 else
240 return wxGenericDirButton::Create(parent, id, label, path, message, wildcard,
241 pos, size, style, validator, name);
242 return true;
243 }
244
245 wxDirButton::~wxDirButton()
246 {
247 // GtkFileChooserButton will automatically destroy the
248 // GtkFileChooserDialog associated with m_dialog.
249 // Thus we have to set its m_widget to NULL to avoid
250 // double destruction on same widget
251 m_dialog->m_widget = NULL;
252 }
253
254 #endif // wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)