1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: filepicker.cpp
4 // Purpose: Part of the widgets sample showing wx*PickerCtrl
5 // Author: Francesco Montorsi
8 // Copyright: (c) 2006 Francesco Montorsi
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_FILEPICKERCTRL
29 // for all others, include the necessary headers
33 #include "wx/radiobox.h"
36 #include "wx/artprov.h"
38 #include "wx/stattext.h"
39 #include "wx/checkbox.h"
40 #include "wx/imaglist.h"
42 #include "wx/filepicker.h"
45 #include "icons/filepicker.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
53 FilePickerMode_Open
= 0,
60 PickerPage_Reset
= wxID_HIGHEST
,
65 // ----------------------------------------------------------------------------
66 // FilePickerWidgetsPage
67 // ----------------------------------------------------------------------------
69 class FilePickerWidgetsPage
: public WidgetsPage
72 FilePickerWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
73 virtual ~FilePickerWidgetsPage(){};
75 virtual wxControl
*GetWidget() const { return m_filePicker
; }
76 virtual void RecreateWidget() { RecreatePicker(); }
78 // lazy creation of the content
79 virtual void CreateContent();
83 // called only once at first construction
86 // called to recreate an existing control
87 void RecreatePicker();
89 // restore the checkboxes state to the initial values
92 // get the initial style for the picker of the given kind
93 long GetPickerStyle();
95 // update filepicker radiobox
96 void UpdateFilePickerMode();
98 // the pickers and the relative event handlers
99 void OnFileChange(wxFileDirPickerEvent
&ev
);
100 void OnCheckBox(wxCommandEvent
&ev
);
101 void OnButtonReset(wxCommandEvent
&ev
);
105 wxFilePickerCtrl
*m_filePicker
;
111 wxCheckBox
*m_chkFileTextCtrl
,
112 *m_chkFileOverwritePrompt
,
116 wxRadioBox
*m_radioFilePickerMode
;
121 DECLARE_EVENT_TABLE()
122 DECLARE_WIDGETS_PAGE(FilePickerWidgetsPage
)
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 BEGIN_EVENT_TABLE(FilePickerWidgetsPage
, WidgetsPage
)
130 EVT_BUTTON(PickerPage_Reset
, FilePickerWidgetsPage::OnButtonReset
)
132 EVT_FILEPICKER_CHANGED(PickerPage_File
, FilePickerWidgetsPage::OnFileChange
)
134 EVT_CHECKBOX(wxID_ANY
, FilePickerWidgetsPage::OnCheckBox
)
135 EVT_RADIOBOX(wxID_ANY
, FilePickerWidgetsPage::OnCheckBox
)
138 // ============================================================================
140 // ============================================================================
142 #if defined(__WXGTK24__)
143 #define FAMILY_CTRLS NATIVE_CTRLS
145 #define FAMILY_CTRLS GENERIC_CTRLS
148 IMPLEMENT_WIDGETS_PAGE(FilePickerWidgetsPage
, wxT("FilePicker"),
149 PICKER_CTRLS
| FAMILY_CTRLS
);
151 FilePickerWidgetsPage::FilePickerWidgetsPage(WidgetsBookCtrl
*book
,
152 wxImageList
*imaglist
)
153 : WidgetsPage(book
, imaglist
, filepicker_xpm
)
157 void FilePickerWidgetsPage::CreateContent()
160 wxSizer
*boxleft
= new wxBoxSizer(wxVERTICAL
);
162 static const wxString mode
[] = { wxT("open"), wxT("save") };
163 m_radioFilePickerMode
= new wxRadioBox(this, wxID_ANY
, wxT("wxFilePicker mode"),
164 wxDefaultPosition
, wxDefaultSize
,
165 WXSIZEOF(mode
), mode
);
166 boxleft
->Add(m_radioFilePickerMode
, 0, wxALL
|wxGROW
, 5);
168 wxStaticBoxSizer
*filebox
= new wxStaticBoxSizer(wxVERTICAL
, this, wxT("&FilePicker style"));
169 m_chkFileTextCtrl
= CreateCheckBoxAndAddToSizer(filebox
, wxT("With textctrl"), false);
170 m_chkFileOverwritePrompt
= CreateCheckBoxAndAddToSizer(filebox
, wxT("Overwrite prompt"), false);
171 m_chkFileMustExist
= CreateCheckBoxAndAddToSizer(filebox
, wxT("File must exist"), false);
172 m_chkFileChangeDir
= CreateCheckBoxAndAddToSizer(filebox
, wxT("Change working dir"), false);
173 m_chkSmall
= CreateCheckBoxAndAddToSizer(filebox
, "&Small version", false);
175 boxleft
->Add(filebox
, 0, wxALL
|wxGROW
, 5);
177 boxleft
->Add(new wxButton(this, PickerPage_Reset
, wxT("&Reset")),
178 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
180 Reset(); // set checkboxes state
187 m_sizer
= new wxBoxSizer(wxVERTICAL
);
188 m_sizer
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
189 m_sizer
->Add(m_filePicker
, 0, wxEXPAND
|wxALL
, 5);
190 m_sizer
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
193 wxSizer
*sz
= new wxBoxSizer(wxHORIZONTAL
);
194 sz
->Add(boxleft
, 0, wxGROW
|wxALL
, 5);
195 sz
->Add(m_sizer
, 1, wxGROW
|wxALL
, 5);
200 void FilePickerWidgetsPage::CreatePicker()
204 // pass an empty string as initial file
205 m_filePicker
= new wxFilePickerCtrl(this, PickerPage_File
,
207 wxT("Hello!"), wxT("*"),
208 wxDefaultPosition
, wxDefaultSize
,
212 long FilePickerWidgetsPage::GetPickerStyle()
216 if ( m_chkFileTextCtrl
->GetValue() )
217 style
|= wxFLP_USE_TEXTCTRL
;
219 if ( m_chkFileOverwritePrompt
->GetValue() )
220 style
|= wxFLP_OVERWRITE_PROMPT
;
222 if ( m_chkFileMustExist
->GetValue() )
223 style
|= wxFLP_FILE_MUST_EXIST
;
225 if ( m_chkFileChangeDir
->GetValue() )
226 style
|= wxFLP_CHANGE_DIR
;
228 if ( m_chkSmall
->GetValue() )
229 style
|= wxFLP_SMALL
;
231 if (m_radioFilePickerMode
->GetSelection() == FilePickerMode_Open
)
239 void FilePickerWidgetsPage::RecreatePicker()
243 m_sizer
->Insert(1, m_filePicker
, 0, wxEXPAND
|wxALL
, 5);
248 void FilePickerWidgetsPage::Reset()
250 m_radioFilePickerMode
->SetSelection((wxFLP_DEFAULT_STYLE
& wxFLP_OPEN
) ?
251 FilePickerMode_Open
: FilePickerMode_Save
);
252 m_chkFileTextCtrl
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_USE_TEXTCTRL
) != 0);
253 m_chkFileOverwritePrompt
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_OVERWRITE_PROMPT
) != 0);
254 m_chkFileMustExist
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_FILE_MUST_EXIST
) != 0);
255 m_chkFileChangeDir
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_CHANGE_DIR
) != 0);
256 m_chkSmall
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_SMALL
) != 0);
258 UpdateFilePickerMode();
261 void FilePickerWidgetsPage::UpdateFilePickerMode()
263 switch (m_radioFilePickerMode
->GetSelection())
265 case FilePickerMode_Open
:
266 m_chkFileOverwritePrompt
->SetValue(false);
267 m_chkFileOverwritePrompt
->Disable();
268 m_chkFileMustExist
->Enable();
270 case FilePickerMode_Save
:
271 m_chkFileMustExist
->SetValue(false);
272 m_chkFileMustExist
->Disable();
273 m_chkFileOverwritePrompt
->Enable();
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 void FilePickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
290 void FilePickerWidgetsPage::OnFileChange(wxFileDirPickerEvent
& event
)
292 wxLogMessage(wxT("The file changed to '%s' ! The current working directory is '%s'"),
293 event
.GetPath().c_str(), wxGetCwd().c_str());
296 void FilePickerWidgetsPage::OnCheckBox(wxCommandEvent
&event
)
298 if (event
.GetEventObject() == m_chkFileTextCtrl
||
299 event
.GetEventObject() == m_chkFileOverwritePrompt
||
300 event
.GetEventObject() == m_chkFileMustExist
||
301 event
.GetEventObject() == m_chkFileChangeDir
||
302 event
.GetEventObject() == m_chkSmall
)
305 if (event
.GetEventObject() == m_radioFilePickerMode
)
307 UpdateFilePickerMode();
312 #endif // wxUSE_FILEPICKERCTRL