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 #include "wx/gtk/private.h"
21 #include "wx/filepicker.h"
25 #include "wx/tooltip.h"
31 #include <unistd.h> // chdir
34 // ============================================================================
36 // ============================================================================
38 #if wxUSE_FILEPICKERCTRL && defined(__WXGTK26__)
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 SetWindowStyle(style
);
71 if (!CreateDialog(message
, wildcard
))
74 // little trick used to avoid problems when there are other GTK windows 'grabbed':
75 // GtkFileChooserDialog won't be responsive to user events if there is another
76 // window which called gtk_grab_add (and this happens if e.g. a wxDialog is running
77 // in modal mode in the application - see wxDialogGTK::ShowModal).
78 // An idea could be to put the grab on the m_dialog->m_widget when the GtkFileChooserButton
79 // is clicked and then remove it as soon as the user closes the dialog itself.
80 // Unfortunately there's no way to hook in the 'clicked' event of the GtkFileChooserButton,
81 // thus we add grab on m_dialog->m_widget when it's shown and remove it when it's
82 // hidden simply using its "show" and "hide" events - clean & simple :)
83 g_signal_connect(m_dialog
->m_widget
, "show", G_CALLBACK(gtk_grab_add
), NULL
);
84 g_signal_connect(m_dialog
->m_widget
, "hide", G_CALLBACK(gtk_grab_remove
), NULL
);
86 // NOTE: we deliberately ignore the given label as GtkFileChooserButton
87 // use as label the currently selected file
88 m_widget
= gtk_file_chooser_button_new_with_dialog( m_dialog
->m_widget
);
89 gtk_widget_show( GTK_WIDGET(m_widget
) );
91 // we need to know when the dialog has been dismissed clicking OK...
92 // NOTE: the "clicked" signal is not available for a GtkFileChooserButton
93 // thus we are forced to use wxFileDialog's event
94 m_dialog
->Connect(wxEVT_COMMAND_BUTTON_CLICKED
,
95 wxCommandEventHandler(wxFileButton::OnDialogOK
),
98 m_parent
->DoAddChild( this );
104 return wxGenericFileButton::Create(parent
, id
, label
, path
, message
, wildcard
,
105 pos
, size
, style
, validator
, name
);
109 wxFileButton::~wxFileButton()
111 // GtkFileChooserButton will automatically destroy the
112 // GtkFileChooserDialog associated with m_dialog.
113 // Thus we have to set its m_widget to NULL to avoid
114 // double destruction on same widget
115 m_dialog
->m_widget
= NULL
;
118 void wxFileButton::OnDialogOK(wxCommandEvent
& ev
)
120 // the wxFileDialog associated with the GtkFileChooserButton has been closed
121 // using the OK button, thus the selected file has changed...
122 if (ev
.GetId() == wxID_OK
)
124 // ...update our path
125 UpdatePathFromDialog();
127 // ...and fire an event
128 wxFileDirPickerEvent
event(wxEVT_COMMAND_FILEPICKER_CHANGED
, this, GetId(), m_path
);
129 GetEventHandler()->ProcessEvent(event
);
133 #endif // wxUSE_FILEPICKERCTRL && defined(__WXGTK26__)
138 #if wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)
140 //-----------------------------------------------------------------------------
141 // "current-folder-changed"
142 //-----------------------------------------------------------------------------
145 static void gtk_dirbutton_currentfolderchanged_callback(GtkFileChooserButton
*widget
,
148 // update the m_path member of the wxDirButtonGTK
151 // NB: it's important to use gtk_file_chooser_get_filename instead of
152 // gtk_file_chooser_get_current_folder (see GTK docs) !
153 gchar
* filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
154 p
->UpdatePath(filename
);
156 // since GtkFileChooserButton when used to pick directories also uses a combobox,
157 // maybe that the current folder has been changed but not through the GtkFileChooserDialog
158 // and thus the 'gtk_filedialog_ok_callback' could have not been called...
159 // thus we need to make sure the current working directory is updated if wxDIRP_CHANGE_DIR
161 if (p
->HasFlag(wxDIRP_CHANGE_DIR
))
165 // ...and fire an event
166 wxFileDirPickerEvent
event(wxEVT_COMMAND_DIRPICKER_CHANGED
, p
, p
->GetId(), p
->GetPath());
167 p
->GetEventHandler()->ProcessEvent(event
);
172 //-----------------------------------------------------------------------------
174 //-----------------------------------------------------------------------------
176 IMPLEMENT_DYNAMIC_CLASS(wxDirButton
, wxButton
)
178 bool wxDirButton::Create( wxWindow
*parent
, wxWindowID id
,
179 const wxString
&label
, const wxString
&path
,
180 const wxString
&message
, const wxString
&wildcard
,
181 const wxPoint
&pos
, const wxSize
&size
,
182 long style
, const wxValidator
& validator
,
183 const wxString
&name
)
185 if (!gtk_check_version(2,6,0))
187 // VERY IMPORTANT: this code is identic to relative code in wxFileButton;
188 // if you find a problem here, fix it also in wxFileButton !
192 if (!PreCreation( parent
, pos
, size
) ||
193 !wxControl::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
196 wxFAIL_MSG( wxT("wxDirButtonGTK creation failed") );
200 // create the dialog associated with this button
201 SetWindowStyle(style
);
203 if (!CreateDialog(message
, wildcard
))
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__)