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