1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/filepicker.cpp
3 // Purpose: implementation of wxFileButton and wxDirButton
4 // Author: Francesco Montorsi
8 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
20 #if wxUSE_FILEPICKERCTRL && defined(__WXGTK26__)
22 #include "wx/filepicker.h"
23 #include "wx/tooltip.h"
25 #include "wx/gtk/private.h"
27 // ============================================================================
29 // ============================================================================
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 IMPLEMENT_DYNAMIC_CLASS(wxFileButton
, wxButton
)
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
)
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) )
48 // VERY IMPORTANT: this code is identic to relative code in wxDirButton;
49 // if you find a problem here, fix it also in wxDirButton !
51 if (!PreCreation( parent
, pos
, size
) ||
52 !wxControl::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
55 wxFAIL_MSG( wxT("wxFileButton creation failed") );
59 // create the dialog associated with this button
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()
62 SetWindowStyle(style
);
65 m_wildcard
= wildcard
;
66 if ((m_dialog
= CreateDialog()) == NULL
)
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
);
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
);
84 gtk_widget_show(m_widget
);
86 // we need to know when the dialog has been dismissed clicking OK...
87 // NOTE: the "clicked" signal is not available for a GtkFileChooserButton
88 // thus we are forced to use wxFileDialog's event
89 m_dialog
->Connect(wxEVT_COMMAND_BUTTON_CLICKED
,
90 wxCommandEventHandler(wxFileButton::OnDialogOK
),
93 m_parent
->DoAddChild( this );
99 return wxGenericFileButton::Create(parent
, id
, label
, path
, message
, wildcard
,
100 pos
, size
, style
, validator
, name
);
104 wxFileButton::~wxFileButton()
106 // GtkFileChooserButton will automatically destroy the
107 // GtkFileChooserDialog associated with m_dialog.
108 // Thus we have to set its m_widget to NULL to avoid
109 // double destruction on same widget
111 m_dialog
->m_widget
= NULL
;
114 void wxFileButton::OnDialogOK(wxCommandEvent
& ev
)
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
)
120 // ...update our path
121 UpdatePathFromDialog(m_dialog
);
123 // ...and fire an event
124 wxFileDirPickerEvent
event(wxEVT_COMMAND_FILEPICKER_CHANGED
, this, GetId(), m_path
);
125 HandleWindowEvent(event
);
129 void wxFileButton::SetPath(const wxString
&str
)
133 UpdateDialogPath(m_dialog
);
136 #endif // wxUSE_FILEPICKERCTRL && defined(__WXGTK26__)
141 #if wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)
143 #include <unistd.h> // chdir
145 //-----------------------------------------------------------------------------
146 // "current-folder-changed"
147 //-----------------------------------------------------------------------------
150 static void gtk_dirbutton_currentfolderchanged_callback(GtkFileChooserButton
*widget
,
153 // update the m_path member of the wxDirButtonGTK
154 // unless the path was changed by wxDirButton::SetPath()
155 if (p
->m_bIgnoreNextChange
)
157 p
->m_bIgnoreNextChange
=false;
162 // NB: it's important to use gtk_file_chooser_get_filename instead of
163 // gtk_file_chooser_get_current_folder (see GTK docs) !
164 wxGtkString
filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
)));
165 p
->UpdatePath(filename
);
167 // since GtkFileChooserButton when used to pick directories also uses a combobox,
168 // maybe that the current folder has been changed but not through the GtkFileChooserDialog
169 // and thus the 'gtk_filedialog_ok_callback' could have not been called...
170 // thus we need to make sure the current working directory is updated if wxDIRP_CHANGE_DIR
172 if (p
->HasFlag(wxDIRP_CHANGE_DIR
))
175 // ...and fire an event
176 wxFileDirPickerEvent
event(wxEVT_COMMAND_DIRPICKER_CHANGED
, p
, p
->GetId(), p
->GetPath());
177 p
->HandleWindowEvent(event
);
182 //-----------------------------------------------------------------------------
184 //-----------------------------------------------------------------------------
186 IMPLEMENT_DYNAMIC_CLASS(wxDirButton
, wxButton
)
188 bool wxDirButton::Create( wxWindow
*parent
, wxWindowID id
,
189 const wxString
&label
, const wxString
&path
,
190 const wxString
&message
, const wxString
&wildcard
,
191 const wxPoint
&pos
, const wxSize
&size
,
192 long style
, const wxValidator
& validator
,
193 const wxString
&name
)
195 if (!gtk_check_version(2,6,0))
197 // VERY IMPORTANT: this code is identic to relative code in wxFileButton;
198 // if you find a problem here, fix it also in wxFileButton !
200 if (!PreCreation( parent
, pos
, size
) ||
201 !wxControl::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
204 wxFAIL_MSG( wxT("wxDirButtonGTK creation failed") );
208 // create the dialog associated with this button
209 SetWindowStyle(style
);
211 m_wildcard
= wildcard
;
212 if ((m_dialog
= CreateDialog()) == NULL
)
216 // little trick used to avoid problems when there are other GTK windows 'grabbed':
217 // GtkFileChooserDialog won't be responsive to user events if there is another
218 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
219 // in modal mode in the application - see wxDialogGTK::ShowModal).
220 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
221 // is clicked and then remove it as soon as the user closes the dialog itself.
222 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
223 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
224 // hidden simply using its "show" and "hide" events - clean & simple :)
225 g_signal_connect(m_dialog
->m_widget
, "show", G_CALLBACK(gtk_grab_add
), NULL
);
226 g_signal_connect(m_dialog
->m_widget
, "hide", G_CALLBACK(gtk_grab_remove
), NULL
);
229 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
230 // use as label the currently selected file
231 m_widget
= gtk_file_chooser_button_new_with_dialog( m_dialog
->m_widget
);
233 gtk_widget_show(m_widget
);
235 // GtkFileChooserButton signals
236 g_signal_connect(m_widget
, "current-folder-changed",
237 G_CALLBACK(gtk_dirbutton_currentfolderchanged_callback
), this);
239 m_parent
->DoAddChild( this );
242 SetInitialSize(size
);
245 return wxGenericDirButton::Create(parent
, id
, label
, path
, message
, wildcard
,
246 pos
, size
, style
, validator
, name
);
250 wxDirButton::~wxDirButton()
252 // GtkFileChooserButton will automatically destroy the
253 // GtkFileChooserDialog associated with m_dialog.
254 // Thus we have to set its m_widget to NULL to avoid
255 // double destruction on same widget
257 m_dialog
->m_widget
= NULL
;
260 void wxDirButton::SetPath(const wxString
&str
)
264 // wxDirButton uses the "current-folder-changed" signal which is triggered also
265 // when we set the path on the dialog associated with this button; thus we need
266 // to set the following flag to avoid sending a wxFileDirPickerEvent from this
267 // function (which would be inconsistent with wxFileButton's behaviour and in
268 // general with all wxWidgets control-manipulation functions which do not send events).
269 m_bIgnoreNextChange
= true;
272 UpdateDialogPath(m_dialog
);
275 #endif // wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)