added wx{Colour|File|Dir|Font}PickerCtrl (patch 1472329 by Francesco)
[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 #include "wx/gtk/private.h"
21 #include "wx/filepicker.h"
22
23
24 #if wxUSE_TOOLTIPS
25 #include "wx/tooltip.h"
26 #endif
27
28 #include <gdk/gdk.h>
29 #include <gtk/gtk.h>
30
31 #include <unistd.h> // chdir
32
33
34 // ============================================================================
35 // implementation
36 // ============================================================================
37
38 #if wxUSE_FILEPICKERCTRL && defined(__WXGTK26__)
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 SetWindowStyle(style);
70 m_path = path;
71 if (!CreateDialog(message, wildcard))
72 return false;
73
74 // little trick used to avoid problems when there are other GTK windows 'grabbed':
75 // GtkFileChooserDialog won't be responsive to user events if there is another
76 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
77 // in modal mode in the application - see wxDialogGTK::ShowModal).
78 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
79 // is clicked and then remove it as soon as the user closes the dialog itself.
80 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
81 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
82 // hidden simply using its "show" and "hide" events - clean & simple :)
83 g_signal_connect(m_dialog->m_widget, "show", G_CALLBACK(gtk_grab_add), NULL);
84 g_signal_connect(m_dialog->m_widget, "hide", G_CALLBACK(gtk_grab_remove), NULL);
85
86 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
87 // use as label the currently selected file
88 m_widget = gtk_file_chooser_button_new_with_dialog( m_dialog->m_widget );
89 gtk_widget_show( GTK_WIDGET(m_widget) );
90
91 // we need to know when the dialog has been dismissed clicking OK...
92 // NOTE: the "clicked" signal is not available for a GtkFileChooserButton
93 // thus we are forced to use wxFileDialog's event
94 m_dialog->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
95 wxCommandEventHandler(wxFileButton::OnDialogOK),
96 NULL, this);
97
98 m_parent->DoAddChild( this );
99
100 PostCreation(size);
101 SetBestSize(size);
102 }
103 else
104 return wxGenericFileButton::Create(parent, id, label, path, message, wildcard,
105 pos, size, style, validator, name);
106 return true;
107 }
108
109 wxFileButton::~wxFileButton()
110 {
111 // GtkFileChooserButton will automatically destroy the
112 // GtkFileChooserDialog associated with m_dialog.
113 // Thus we have to set its m_widget to NULL to avoid
114 // double destruction on same widget
115 m_dialog->m_widget = NULL;
116 }
117
118 void wxFileButton::OnDialogOK(wxCommandEvent& ev)
119 {
120 // the wxFileDialog associated with the GtkFileChooserButton has been closed
121 // using the OK button, thus the selected file has changed...
122 if (ev.GetId() == wxID_OK)
123 {
124 // ...update our path
125 UpdatePathFromDialog();
126
127 // ...and fire an event
128 wxFileDirPickerEvent event(wxEVT_COMMAND_FILEPICKER_CHANGED, this, GetId(), m_path);
129 GetEventHandler()->ProcessEvent(event);
130 }
131 }
132
133 #endif // wxUSE_FILEPICKERCTRL && defined(__WXGTK26__)
134
135
136
137
138 #if wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)
139
140 //-----------------------------------------------------------------------------
141 // "current-folder-changed"
142 //-----------------------------------------------------------------------------
143
144 extern "C" {
145 static void gtk_dirbutton_currentfolderchanged_callback(GtkFileChooserButton *widget,
146 wxDirButton *p)
147 {
148 // update the m_path member of the wxDirButtonGTK
149 wxASSERT(p);
150
151 // NB: it's important to use gtk_file_chooser_get_filename instead of
152 // gtk_file_chooser_get_current_folder (see GTK docs) !
153 gchar* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
154 p->UpdatePath(filename);
155
156 // since GtkFileChooserButton when used to pick directories also uses a combobox,
157 // maybe that the current folder has been changed but not through the GtkFileChooserDialog
158 // and thus the 'gtk_filedialog_ok_callback' could have not been called...
159 // thus we need to make sure the current working directory is updated if wxDIRP_CHANGE_DIR
160 // style was given.
161 if (p->HasFlag(wxDIRP_CHANGE_DIR))
162 chdir(filename);
163 g_free(filename);
164
165 // ...and fire an event
166 wxFileDirPickerEvent event(wxEVT_COMMAND_DIRPICKER_CHANGED, p, p->GetId(), p->GetPath());
167 p->GetEventHandler()->ProcessEvent(event);
168 }
169 }
170
171
172 //-----------------------------------------------------------------------------
173 // wxDirButtonGTK
174 //-----------------------------------------------------------------------------
175
176 IMPLEMENT_DYNAMIC_CLASS(wxDirButton, wxButton)
177
178 bool wxDirButton::Create( wxWindow *parent, wxWindowID id,
179 const wxString &label, const wxString &path,
180 const wxString &message, const wxString &wildcard,
181 const wxPoint &pos, const wxSize &size,
182 long style, const wxValidator& validator,
183 const wxString &name )
184 {
185 if (!gtk_check_version(2,6,0))
186 {
187 // VERY IMPORTANT: this code is identic to relative code in wxFileButton;
188 // if you find a problem here, fix it also in wxFileButton !
189
190 m_needParent = true;
191
192 if (!PreCreation( parent, pos, size ) ||
193 !wxControl::CreateBase(parent, id, pos, size, style & wxWINDOW_STYLE_MASK,
194 validator, name))
195 {
196 wxFAIL_MSG( wxT("wxDirButtonGTK creation failed") );
197 return false;
198 }
199
200 // create the dialog associated with this button
201 SetWindowStyle(style);
202 m_path = path;
203 if (!CreateDialog(message, wildcard))
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__)