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
22 #include "wx/filepicker.h"
23 #include "wx/tooltip.h"
26 #include "wx/gtk/private.h"
28 // ============================================================================
30 // ============================================================================
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 IMPLEMENT_DYNAMIC_CLASS(wxFileButton
, wxButton
)
38 bool wxFileButton::Create( wxWindow
*parent
, wxWindowID id
,
39 const wxString
&label
, const wxString
&path
,
40 const wxString
&message
, const wxString
&wildcard
,
41 const wxPoint
&pos
, const wxSize
&size
,
42 long style
, const wxValidator
& validator
,
43 const wxString
&name
)
45 // we can't use the native button for wxFLP_SAVE pickers as it can only
46 // open existing files and there is no way to create a new file using it
47 if (!(style
& wxFLP_SAVE
) && !(style
& wxFLP_USE_TEXTCTRL
))
49 // VERY IMPORTANT: this code is identical to relative code in wxDirButton;
50 // if you find a problem here, fix it also in wxDirButton !
52 if (!PreCreation( parent
, pos
, size
) ||
53 !wxControl::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
56 wxFAIL_MSG( wxT("wxFileButton creation failed") );
60 // create the dialog associated with this button
61 // NB: unlike generic implementation, native GTK implementation needs to create
62 // the filedialog here as it needs to use gtk_file_chooser_button_new_with_dialog()
63 SetWindowStyle(style
);
66 m_wildcard
= wildcard
;
67 if ((m_dialog
= CreateDialog()) == NULL
)
70 // little trick used to avoid problems when there are other GTK windows 'grabbed':
71 // GtkFileChooserDialog won't be responsive to user events if there is another
72 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
73 // in modal mode in the application - see wxDialogGTK::ShowModal).
74 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
75 // is clicked and then remove it as soon as the user closes the dialog itself.
76 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
77 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
78 // hidden simply using its "show" and "hide" events - clean & simple :)
79 g_signal_connect(m_dialog
->m_widget
, "show", G_CALLBACK(gtk_grab_add
), NULL
);
80 g_signal_connect(m_dialog
->m_widget
, "hide", G_CALLBACK(gtk_grab_remove
), NULL
);
82 // use as label the currently selected file
83 m_widget
= gtk_file_chooser_button_new_with_dialog( m_dialog
->m_widget
);
84 g_object_ref(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_BUTTON
,
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()
108 // We need to delete the C++ dialog object here but we shouldn't delete
109 // its widget which is used by our GtkFileChooserButton and will be
110 // deleted by it when it is itself destroyed in our base class dtor. So
111 // take the widget ownership away from the dialog to avoid GTK+ errors
112 // that would happen if GtkFileChooserButton tried to access the
113 // already destroyed dialog widget.
114 g_object_unref(m_dialog
->m_widget
);
115 m_dialog
->m_widget
= NULL
;
120 void wxFileButton::OnDialogOK(wxCommandEvent
& ev
)
122 // the wxFileDialog associated with the GtkFileChooserButton has been closed
123 // using the OK button, thus the selected file has changed...
124 if (ev
.GetId() == wxID_OK
)
126 // ...update our path
127 UpdatePathFromDialog(m_dialog
);
129 // ...and fire an event
130 wxFileDirPickerEvent
event(wxEVT_FILEPICKER_CHANGED
, this, GetId(), m_path
);
131 HandleWindowEvent(event
);
135 void wxFileButton::SetPath(const wxString
&str
)
140 UpdateDialogPath(m_dialog
);
143 void wxFileButton::SetInitialDirectory(const wxString
& dir
)
147 // Only change the directory if the default file name doesn't have any
148 // directory in it, otherwise it takes precedence.
149 if ( m_path
.find_first_of(wxFileName::GetPathSeparators()) ==
152 static_cast<wxFileDialog
*>(m_dialog
)->SetDirectory(dir
);
156 wxGenericFileButton::SetInitialDirectory(dir
);
159 #endif // wxUSE_FILEPICKERCTRL
161 #if wxUSE_DIRPICKERCTRL
164 #include <unistd.h> // chdir
167 //-----------------------------------------------------------------------------
168 // "current-folder-changed"
169 //-----------------------------------------------------------------------------
172 static void gtk_dirbutton_currentfolderchanged_callback(GtkFileChooserButton
*widget
,
175 // update the m_path member of the wxDirButtonGTK
176 // unless the path was changed by wxDirButton::SetPath()
177 if (p
->m_bIgnoreNextChange
)
179 p
->m_bIgnoreNextChange
=false;
184 // NB: it's important to use gtk_file_chooser_get_filename instead of
185 // gtk_file_chooser_get_current_folder (see GTK docs) !
186 wxGtkString
filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
)));
187 p
->GTKUpdatePath(filename
);
189 // since GtkFileChooserButton when used to pick directories also uses a combobox,
190 // maybe that the current folder has been changed but not through the GtkFileChooserDialog
191 // and thus the 'gtk_filedialog_ok_callback' could have not been called...
192 // thus we need to make sure the current working directory is updated if wxDIRP_CHANGE_DIR
194 if (p
->HasFlag(wxDIRP_CHANGE_DIR
))
197 // ...and fire an event
198 wxFileDirPickerEvent
event(wxEVT_DIRPICKER_CHANGED
, p
, p
->GetId(), p
->GetPath());
199 p
->HandleWindowEvent(event
);
204 //-----------------------------------------------------------------------------
206 //-----------------------------------------------------------------------------
208 IMPLEMENT_DYNAMIC_CLASS(wxDirButton
, wxButton
)
210 bool wxDirButton::Create( wxWindow
*parent
, wxWindowID id
,
211 const wxString
&label
, const wxString
&path
,
212 const wxString
&message
, const wxString
&wildcard
,
213 const wxPoint
&pos
, const wxSize
&size
,
214 long style
, const wxValidator
& validator
,
215 const wxString
&name
)
217 if (!(style
& wxDIRP_USE_TEXTCTRL
))
219 // VERY IMPORTANT: this code is identic to relative code in wxFileButton;
220 // if you find a problem here, fix it also in wxFileButton !
222 if (!PreCreation( parent
, pos
, size
) ||
223 !wxControl::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
226 wxFAIL_MSG( wxT("wxDirButtonGTK creation failed") );
230 // create the dialog associated with this button
231 SetWindowStyle(style
);
233 m_wildcard
= wildcard
;
234 if ((m_dialog
= CreateDialog()) == NULL
)
238 // little trick used to avoid problems when there are other GTK windows 'grabbed':
239 // GtkFileChooserDialog won't be responsive to user events if there is another
240 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
241 // in modal mode in the application - see wxDialogGTK::ShowModal).
242 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
243 // is clicked and then remove it as soon as the user closes the dialog itself.
244 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
245 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
246 // hidden simply using its "show" and "hide" events - clean & simple :)
247 g_signal_connect(m_dialog
->m_widget
, "show", G_CALLBACK(gtk_grab_add
), NULL
);
248 g_signal_connect(m_dialog
->m_widget
, "hide", G_CALLBACK(gtk_grab_remove
), NULL
);
251 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
252 // use as label the currently selected file
253 m_widget
= gtk_file_chooser_button_new_with_dialog( m_dialog
->m_widget
);
254 g_object_ref(m_widget
);
256 // GtkFileChooserButton signals
257 g_signal_connect(m_widget
, "current-folder-changed",
258 G_CALLBACK(gtk_dirbutton_currentfolderchanged_callback
), this);
260 m_parent
->DoAddChild( this );
263 SetInitialSize(size
);
266 return wxGenericDirButton::Create(parent
, id
, label
, path
, message
, wildcard
,
267 pos
, size
, style
, validator
, name
);
271 wxDirButton::~wxDirButton()
276 void wxDirButton::GTKUpdatePath(const char *gtkpath
)
278 m_path
= wxString::FromUTF8(gtkpath
);
280 void wxDirButton::SetPath(const wxString
& str
)
284 // don't do anything and especially don't set m_bIgnoreNextChange
290 // wxDirButton uses the "current-folder-changed" signal which is triggered also
291 // when we set the path on the dialog associated with this button; thus we need
292 // to set the following flag to avoid sending a wxFileDirPickerEvent from this
293 // function (which would be inconsistent with wxFileButton's behaviour and in
294 // general with all wxWidgets control-manipulation functions which do not send events).
295 m_bIgnoreNextChange
= true;
298 UpdateDialogPath(m_dialog
);
301 void wxDirButton::SetInitialDirectory(const wxString
& dir
)
306 static_cast<wxDirDialog
*>(m_dialog
)->SetPath(dir
);
309 wxGenericDirButton::SetInitialDirectory(dir
);
312 #endif // wxUSE_DIRPICKERCTRL