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/tooltip.h"
28 #include <unistd.h> // chdir
31 // ============================================================================
33 // ============================================================================
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 IMPLEMENT_DYNAMIC_CLASS(wxFileButton
, wxButton
)
41 bool wxFileButton::Create( wxWindow
*parent
, wxWindowID id
,
42 const wxString
&label
, const wxString
&path
,
43 const wxString
&message
, const wxString
&wildcard
,
44 const wxPoint
&pos
, const wxSize
&size
,
45 long style
, const wxValidator
& validator
,
46 const wxString
&name
)
48 if (!gtk_check_version(2,6,0))
50 // VERY IMPORTANT: this code is identic to relative code in wxFileButton;
51 // if you find a problem here, fix it also in wxFileButton !
55 if (!PreCreation( parent
, pos
, size
) ||
56 !wxControl::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
59 wxFAIL_MSG( wxT("wxFileButton creation failed") );
63 // create the dialog associated with this button
64 // NB: unlike generic implementation, native GTK implementation needs to create
65 // the filedialog here as it needs to use gtk_file_chooser_button_new_with_dialog()
66 SetWindowStyle(style
);
69 m_wildcard
= wildcard
;
70 if ((m_dialog
= CreateDialog()) == NULL
)
73 // little trick used to avoid problems when there are other GTK windows 'grabbed':
74 // GtkFileChooserDialog won't be responsive to user events if there is another
75 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
76 // in modal mode in the application - see wxDialogGTK::ShowModal).
77 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
78 // is clicked and then remove it as soon as the user closes the dialog itself.
79 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
80 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
81 // hidden simply using its "show" and "hide" events - clean & simple :)
82 g_signal_connect(m_dialog
->m_widget
, "show", G_CALLBACK(gtk_grab_add
), NULL
);
83 g_signal_connect(m_dialog
->m_widget
, "hide", G_CALLBACK(gtk_grab_remove
), NULL
);
85 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
86 // use as label the currently selected file
87 m_widget
= gtk_file_chooser_button_new_with_dialog( m_dialog
->m_widget
);
88 gtk_widget_show( GTK_WIDGET(m_widget
) );
90 // we need to know when the dialog has been dismissed clicking OK...
91 // NOTE: the "clicked" signal is not available for a GtkFileChooserButton
92 // thus we are forced to use wxFileDialog's event
93 m_dialog
->Connect(wxEVT_COMMAND_BUTTON_CLICKED
,
94 wxCommandEventHandler(wxFileButton::OnDialogOK
),
97 m_parent
->DoAddChild( this );
103 return wxGenericFileButton::Create(parent
, id
, label
, path
, message
, wildcard
,
104 pos
, size
, style
, validator
, name
);
108 wxFileButton::~wxFileButton()
110 // GtkFileChooserButton will automatically destroy the
111 // GtkFileChooserDialog associated with m_dialog.
112 // Thus we have to set its m_widget to NULL to avoid
113 // double destruction on same widget
114 m_dialog
->m_widget
= NULL
;
117 void wxFileButton::OnDialogOK(wxCommandEvent
& ev
)
119 // the wxFileDialog associated with the GtkFileChooserButton has been closed
120 // using the OK button, thus the selected file has changed...
121 if (ev
.GetId() == wxID_OK
)
123 // ...update our path
124 UpdatePathFromDialog(m_dialog
);
126 // ...and fire an event
127 wxFileDirPickerEvent
event(wxEVT_COMMAND_FILEPICKER_CHANGED
, this, GetId(), m_path
);
128 GetEventHandler()->ProcessEvent(event
);
132 #endif // wxUSE_FILEPICKERCTRL && defined(__WXGTK26__)
137 #if wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)
139 //-----------------------------------------------------------------------------
140 // "current-folder-changed"
141 //-----------------------------------------------------------------------------
144 static void gtk_dirbutton_currentfolderchanged_callback(GtkFileChooserButton
*widget
,
147 // update the m_path member of the wxDirButtonGTK
150 // NB: it's important to use gtk_file_chooser_get_filename instead of
151 // gtk_file_chooser_get_current_folder (see GTK docs) !
152 gchar
* filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
153 p
->UpdatePath(filename
);
155 // since GtkFileChooserButton when used to pick directories also uses a combobox,
156 // maybe that the current folder has been changed but not through the GtkFileChooserDialog
157 // and thus the 'gtk_filedialog_ok_callback' could have not been called...
158 // thus we need to make sure the current working directory is updated if wxDIRP_CHANGE_DIR
160 if (p
->HasFlag(wxDIRP_CHANGE_DIR
))
164 // ...and fire an event
165 wxFileDirPickerEvent
event(wxEVT_COMMAND_DIRPICKER_CHANGED
, p
, p
->GetId(), p
->GetPath());
166 p
->GetEventHandler()->ProcessEvent(event
);
171 //-----------------------------------------------------------------------------
173 //-----------------------------------------------------------------------------
175 IMPLEMENT_DYNAMIC_CLASS(wxDirButton
, wxButton
)
177 bool wxDirButton::Create( wxWindow
*parent
, wxWindowID id
,
178 const wxString
&label
, const wxString
&path
,
179 const wxString
&message
, const wxString
&wildcard
,
180 const wxPoint
&pos
, const wxSize
&size
,
181 long style
, const wxValidator
& validator
,
182 const wxString
&name
)
184 if (!gtk_check_version(2,6,0))
186 // VERY IMPORTANT: this code is identic to relative code in wxFileButton;
187 // if you find a problem here, fix it also in wxFileButton !
191 if (!PreCreation( parent
, pos
, size
) ||
192 !wxControl::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
195 wxFAIL_MSG( wxT("wxDirButtonGTK creation failed") );
199 // create the dialog associated with this button
200 SetWindowStyle(style
);
202 m_wildcard
= wildcard
;
203 if ((m_dialog
= CreateDialog()) == NULL
)
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
);
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
);
223 gtk_widget_show( GTK_WIDGET(m_widget
) );
225 // GtkFileChooserButton signals
226 g_signal_connect(m_widget
, "current-folder-changed",
227 G_CALLBACK(gtk_dirbutton_currentfolderchanged_callback
), this);
229 m_parent
->DoAddChild( this );
235 return wxGenericDirButton::Create(parent
, id
, label
, path
, message
, wildcard
,
236 pos
, size
, style
, validator
, name
);
240 wxDirButton::~wxDirButton()
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
;
249 #endif // wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)