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"
24 #include "wx/gtk/private.h"
27 #include "wx/tooltip.h"
33 #include <unistd.h> // chdir
36 // ============================================================================
38 // ============================================================================
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 IMPLEMENT_DYNAMIC_CLASS(wxFileButton
, wxButton
)
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
)
53 if (!gtk_check_version(2,6,0))
55 // VERY IMPORTANT: this code is identic to relative code in wxFileButton;
56 // if you find a problem here, fix it also in wxFileButton !
60 if (!PreCreation( parent
, pos
, size
) ||
61 !wxControl::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
64 wxFAIL_MSG( wxT("wxFileButton creation failed") );
68 // create the dialog associated with this button
69 // NB: unlike generic implementation, native GTK implementation needs to create
70 // the filedialog here as it needs to use gtk_file_chooser_button_new_with_dialog()
71 SetWindowStyle(style
);
74 m_wildcard
= wildcard
;
75 if ((m_dialog
= CreateDialog()) == NULL
)
78 // little trick used to avoid problems when there are other GTK windows 'grabbed':
79 // GtkFileChooserDialog won't be responsive to user events if there is another
80 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
81 // in modal mode in the application - see wxDialogGTK::ShowModal).
82 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
83 // is clicked and then remove it as soon as the user closes the dialog itself.
84 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
85 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
86 // hidden simply using its "show" and "hide" events - clean & simple :)
87 g_signal_connect(m_dialog
->m_widget
, "show", G_CALLBACK(gtk_grab_add
), NULL
);
88 g_signal_connect(m_dialog
->m_widget
, "hide", G_CALLBACK(gtk_grab_remove
), NULL
);
90 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
91 // use as label the currently selected file
92 m_widget
= gtk_file_chooser_button_new_with_dialog( m_dialog
->m_widget
);
93 gtk_widget_show( GTK_WIDGET(m_widget
) );
95 // we need to know when the dialog has been dismissed clicking OK...
96 // NOTE: the "clicked" signal is not available for a GtkFileChooserButton
97 // thus we are forced to use wxFileDialog's event
98 m_dialog
->Connect(wxEVT_COMMAND_BUTTON_CLICKED
,
99 wxCommandEventHandler(wxFileButton::OnDialogOK
),
102 m_parent
->DoAddChild( this );
108 return wxGenericFileButton::Create(parent
, id
, label
, path
, message
, wildcard
,
109 pos
, size
, style
, validator
, name
);
113 wxFileButton::~wxFileButton()
115 // GtkFileChooserButton will automatically destroy the
116 // GtkFileChooserDialog associated with m_dialog.
117 // Thus we have to set its m_widget to NULL to avoid
118 // double destruction on same widget
119 m_dialog
->m_widget
= NULL
;
122 void wxFileButton::OnDialogOK(wxCommandEvent
& ev
)
124 // the wxFileDialog associated with the GtkFileChooserButton has been closed
125 // using the OK button, thus the selected file has changed...
126 if (ev
.GetId() == wxID_OK
)
128 // ...update our path
129 UpdatePathFromDialog(m_dialog
);
131 // ...and fire an event
132 wxFileDirPickerEvent
event(wxEVT_COMMAND_FILEPICKER_CHANGED
, this, GetId(), m_path
);
133 GetEventHandler()->ProcessEvent(event
);
137 #endif // wxUSE_FILEPICKERCTRL && defined(__WXGTK26__)
142 #if wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)
144 //-----------------------------------------------------------------------------
145 // "current-folder-changed"
146 //-----------------------------------------------------------------------------
149 static void gtk_dirbutton_currentfolderchanged_callback(GtkFileChooserButton
*widget
,
152 // update the m_path member of the wxDirButtonGTK
155 // NB: it's important to use gtk_file_chooser_get_filename instead of
156 // gtk_file_chooser_get_current_folder (see GTK docs) !
157 gchar
* filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
158 p
->UpdatePath(filename
);
160 // since GtkFileChooserButton when used to pick directories also uses a combobox,
161 // maybe that the current folder has been changed but not through the GtkFileChooserDialog
162 // and thus the 'gtk_filedialog_ok_callback' could have not been called...
163 // thus we need to make sure the current working directory is updated if wxDIRP_CHANGE_DIR
165 if (p
->HasFlag(wxDIRP_CHANGE_DIR
))
169 // ...and fire an event
170 wxFileDirPickerEvent
event(wxEVT_COMMAND_DIRPICKER_CHANGED
, p
, p
->GetId(), p
->GetPath());
171 p
->GetEventHandler()->ProcessEvent(event
);
176 //-----------------------------------------------------------------------------
178 //-----------------------------------------------------------------------------
180 IMPLEMENT_DYNAMIC_CLASS(wxDirButton
, wxButton
)
182 bool wxDirButton::Create( wxWindow
*parent
, wxWindowID id
,
183 const wxString
&label
, const wxString
&path
,
184 const wxString
&message
, const wxString
&wildcard
,
185 const wxPoint
&pos
, const wxSize
&size
,
186 long style
, const wxValidator
& validator
,
187 const wxString
&name
)
189 if (!gtk_check_version(2,6,0))
191 // VERY IMPORTANT: this code is identic to relative code in wxFileButton;
192 // if you find a problem here, fix it also in wxFileButton !
196 if (!PreCreation( parent
, pos
, size
) ||
197 !wxControl::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
200 wxFAIL_MSG( wxT("wxDirButtonGTK creation failed") );
204 // create the dialog associated with this button
205 SetWindowStyle(style
);
207 m_wildcard
= wildcard
;
208 if ((m_dialog
= CreateDialog()) == NULL
)
211 // little trick used to avoid problems when there are other GTK windows 'grabbed':
212 // GtkFileChooserDialog won't be responsive to user events if there is another
213 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
214 // in modal mode in the application - see wxDialogGTK::ShowModal).
215 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
216 // is clicked and then remove it as soon as the user closes the dialog itself.
217 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
218 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
219 // hidden simply using its "show" and "hide" events - clean & simple :)
220 g_signal_connect(m_dialog
->m_widget
, "show", G_CALLBACK(gtk_grab_add
), NULL
);
221 g_signal_connect(m_dialog
->m_widget
, "hide", G_CALLBACK(gtk_grab_remove
), NULL
);
224 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
225 // use as label the currently selected file
226 m_widget
= gtk_file_chooser_button_new_with_dialog( m_dialog
->m_widget
);
228 gtk_widget_show( GTK_WIDGET(m_widget
) );
230 // GtkFileChooserButton signals
231 g_signal_connect(m_widget
, "current-folder-changed",
232 G_CALLBACK(gtk_dirbutton_currentfolderchanged_callback
), this);
234 m_parent
->DoAddChild( this );
240 return wxGenericDirButton::Create(parent
, id
, label
, path
, message
, wildcard
,
241 pos
, size
, style
, validator
, name
);
245 wxDirButton::~wxDirButton()
247 // GtkFileChooserButton will automatically destroy the
248 // GtkFileChooserDialog associated with m_dialog.
249 // Thus we have to set its m_widget to NULL to avoid
250 // double destruction on same widget
251 m_dialog
->m_widget
= NULL
;
254 #endif // wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)