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 if (!gtk_check_version(2,6,0))
46 // VERY IMPORTANT: this code is identic to relative code in wxDirButton;
47 // if you find a problem here, fix it also in wxDirButton !
49 if (!PreCreation( parent
, pos
, size
) ||
50 !wxControl::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
53 wxFAIL_MSG( wxT("wxFileButton creation failed") );
57 // create the dialog associated with this button
58 // NB: unlike generic implementation, native GTK implementation needs to create
59 // the filedialog here as it needs to use gtk_file_chooser_button_new_with_dialog()
60 SetWindowStyle(style
);
63 m_wildcard
= wildcard
;
64 if ((m_dialog
= CreateDialog()) == NULL
)
67 // little trick used to avoid problems when there are other GTK windows 'grabbed':
68 // GtkFileChooserDialog won't be responsive to user events if there is another
69 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
70 // in modal mode in the application - see wxDialogGTK::ShowModal).
71 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
72 // is clicked and then remove it as soon as the user closes the dialog itself.
73 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
74 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
75 // hidden simply using its "show" and "hide" events - clean & simple :)
76 g_signal_connect(m_dialog
->m_widget
, "show", G_CALLBACK(gtk_grab_add
), NULL
);
77 g_signal_connect(m_dialog
->m_widget
, "hide", G_CALLBACK(gtk_grab_remove
), NULL
);
79 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
80 // use as label the currently selected file
81 m_widget
= gtk_file_chooser_button_new_with_dialog( m_dialog
->m_widget
);
82 gtk_widget_show(m_widget
);
84 // we need to know when the dialog has been dismissed clicking OK...
85 // NOTE: the "clicked" signal is not available for a GtkFileChooserButton
86 // thus we are forced to use wxFileDialog's event
87 m_dialog
->Connect(wxEVT_COMMAND_BUTTON_CLICKED
,
88 wxCommandEventHandler(wxFileButton::OnDialogOK
),
91 m_parent
->DoAddChild( this );
97 return wxGenericFileButton::Create(parent
, id
, label
, path
, message
, wildcard
,
98 pos
, size
, style
, validator
, name
);
102 wxFileButton::~wxFileButton()
104 // GtkFileChooserButton will automatically destroy the
105 // GtkFileChooserDialog associated with m_dialog.
106 // Thus we have to set its m_widget to NULL to avoid
107 // double destruction on same widget
109 m_dialog
->m_widget
= NULL
;
112 void wxFileButton::OnDialogOK(wxCommandEvent
& ev
)
114 // the wxFileDialog associated with the GtkFileChooserButton has been closed
115 // using the OK button, thus the selected file has changed...
116 if (ev
.GetId() == wxID_OK
)
118 // ...update our path
119 UpdatePathFromDialog(m_dialog
);
121 // ...and fire an event
122 wxFileDirPickerEvent
event(wxEVT_COMMAND_FILEPICKER_CHANGED
, this, GetId(), m_path
);
123 GetEventHandler()->ProcessEvent(event
);
127 void wxFileButton::SetPath(const wxString
&str
)
131 UpdateDialogPath(m_dialog
);
134 #endif // wxUSE_FILEPICKERCTRL && defined(__WXGTK26__)
139 #if wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)
141 #include <unistd.h> // chdir
143 //-----------------------------------------------------------------------------
144 // "current-folder-changed"
145 //-----------------------------------------------------------------------------
148 static void gtk_dirbutton_currentfolderchanged_callback(GtkFileChooserButton
*widget
,
151 // update the m_path member of the wxDirButtonGTK
152 // unless the path was changed by wxDirButton::SetPath()
153 if (p
->m_bIgnoreNextChange
)
155 p
->m_bIgnoreNextChange
=false;
160 // NB: it's important to use gtk_file_chooser_get_filename instead of
161 // gtk_file_chooser_get_current_folder (see GTK docs) !
162 wxGtkString
filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
)));
163 p
->UpdatePath(filename
);
165 // since GtkFileChooserButton when used to pick directories also uses a combobox,
166 // maybe that the current folder has been changed but not through the GtkFileChooserDialog
167 // and thus the 'gtk_filedialog_ok_callback' could have not been called...
168 // thus we need to make sure the current working directory is updated if wxDIRP_CHANGE_DIR
170 if (p
->HasFlag(wxDIRP_CHANGE_DIR
))
173 // ...and fire an event
174 wxFileDirPickerEvent
event(wxEVT_COMMAND_DIRPICKER_CHANGED
, p
, p
->GetId(), p
->GetPath());
175 p
->GetEventHandler()->ProcessEvent(event
);
180 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
184 IMPLEMENT_DYNAMIC_CLASS(wxDirButton
, wxButton
)
186 bool wxDirButton::Create( wxWindow
*parent
, wxWindowID id
,
187 const wxString
&label
, const wxString
&path
,
188 const wxString
&message
, const wxString
&wildcard
,
189 const wxPoint
&pos
, const wxSize
&size
,
190 long style
, const wxValidator
& validator
,
191 const wxString
&name
)
193 if (!gtk_check_version(2,6,0))
195 // VERY IMPORTANT: this code is identic to relative code in wxFileButton;
196 // if you find a problem here, fix it also in wxFileButton !
198 if (!PreCreation( parent
, pos
, size
) ||
199 !wxControl::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
202 wxFAIL_MSG( wxT("wxDirButtonGTK creation failed") );
206 // create the dialog associated with this button
207 SetWindowStyle(style
);
209 m_wildcard
= wildcard
;
210 if ((m_dialog
= CreateDialog()) == NULL
)
214 // little trick used to avoid problems when there are other GTK windows 'grabbed':
215 // GtkFileChooserDialog won't be responsive to user events if there is another
216 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
217 // in modal mode in the application - see wxDialogGTK::ShowModal).
218 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
219 // is clicked and then remove it as soon as the user closes the dialog itself.
220 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
221 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
222 // hidden simply using its "show" and "hide" events - clean & simple :)
223 g_signal_connect(m_dialog
->m_widget
, "show", G_CALLBACK(gtk_grab_add
), NULL
);
224 g_signal_connect(m_dialog
->m_widget
, "hide", G_CALLBACK(gtk_grab_remove
), NULL
);
227 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
228 // use as label the currently selected file
229 m_widget
= gtk_file_chooser_button_new_with_dialog( m_dialog
->m_widget
);
231 gtk_widget_show(m_widget
);
233 // GtkFileChooserButton signals
234 g_signal_connect(m_widget
, "current-folder-changed",
235 G_CALLBACK(gtk_dirbutton_currentfolderchanged_callback
), this);
237 m_parent
->DoAddChild( this );
240 SetInitialSize(size
);
243 return wxGenericDirButton::Create(parent
, id
, label
, path
, message
, wildcard
,
244 pos
, size
, style
, validator
, name
);
248 wxDirButton::~wxDirButton()
250 // GtkFileChooserButton will automatically destroy the
251 // GtkFileChooserDialog associated with m_dialog.
252 // Thus we have to set its m_widget to NULL to avoid
253 // double destruction on same widget
255 m_dialog
->m_widget
= NULL
;
258 void wxDirButton::SetPath(const wxString
&str
)
262 // wxDirButton uses the "current-folder-changed" signal which is triggered also
263 // when we set the path on the dialog associated with this button; thus we need
264 // to set the following flag to avoid sending a wxFileDirPickerEvent from this
265 // function (which would be inconsistent with wxFileButton's behaviour and in
266 // general with all wxWidgets control-manipulation functions which do not send events).
267 m_bIgnoreNextChange
= true;
270 UpdateDialogPath(m_dialog
);
273 #endif // wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)