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"
34 #include "wx/textctrl.h"
37 #include "wx/artprov.h"
39 #include "wx/stattext.h"
40 #include "wx/checkbox.h"
41 #include "wx/imaglist.h"
43 #include "wx/filepicker.h"
46 #include "icons/filepicker.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
54 FilePickerMode_Open
= 0,
61 PickerPage_Reset
= wxID_HIGHEST
,
67 // ----------------------------------------------------------------------------
68 // FilePickerWidgetsPage
69 // ----------------------------------------------------------------------------
71 class FilePickerWidgetsPage
: public WidgetsPage
74 FilePickerWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
75 virtual ~FilePickerWidgetsPage(){};
77 virtual wxControl
*GetWidget() const { return m_filePicker
; }
78 virtual void RecreateWidget() { RecreatePicker(); }
80 // lazy creation of the content
81 virtual void CreateContent();
85 // called only once at first construction
88 // called to recreate an existing control
89 void RecreatePicker();
91 // restore the checkboxes state to the initial values
94 // get the initial style for the picker of the given kind
95 long GetPickerStyle();
97 // update filepicker radiobox
98 void UpdateFilePickerMode();
100 // the pickers and the relative event handlers
101 void OnFileChange(wxFileDirPickerEvent
&ev
);
102 void OnCheckBox(wxCommandEvent
&ev
);
103 void OnButtonReset(wxCommandEvent
&ev
);
104 void OnButtonSetDir(wxCommandEvent
&ev
);
108 wxFilePickerCtrl
*m_filePicker
;
114 wxCheckBox
*m_chkFileTextCtrl
,
115 *m_chkFileOverwritePrompt
,
119 wxRadioBox
*m_radioFilePickerMode
;
120 wxTextCtrl
*m_textInitialDir
;
125 DECLARE_EVENT_TABLE()
126 DECLARE_WIDGETS_PAGE(FilePickerWidgetsPage
)
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 BEGIN_EVENT_TABLE(FilePickerWidgetsPage
, WidgetsPage
)
134 EVT_BUTTON(PickerPage_Reset
, FilePickerWidgetsPage::OnButtonReset
)
135 EVT_BUTTON(PickerPage_SetDir
, FilePickerWidgetsPage::OnButtonSetDir
)
137 EVT_FILEPICKER_CHANGED(PickerPage_File
, FilePickerWidgetsPage::OnFileChange
)
139 EVT_CHECKBOX(wxID_ANY
, FilePickerWidgetsPage::OnCheckBox
)
140 EVT_RADIOBOX(wxID_ANY
, FilePickerWidgetsPage::OnCheckBox
)
143 // ============================================================================
145 // ============================================================================
147 #if defined(__WXGTK24__)
148 #define FAMILY_CTRLS NATIVE_CTRLS
150 #define FAMILY_CTRLS GENERIC_CTRLS
153 IMPLEMENT_WIDGETS_PAGE(FilePickerWidgetsPage
, wxT("FilePicker"),
154 PICKER_CTRLS
| FAMILY_CTRLS
);
156 FilePickerWidgetsPage::FilePickerWidgetsPage(WidgetsBookCtrl
*book
,
157 wxImageList
*imaglist
)
158 : WidgetsPage(book
, imaglist
, filepicker_xpm
)
162 void FilePickerWidgetsPage::CreateContent()
165 wxSizer
*boxleft
= new wxBoxSizer(wxVERTICAL
);
167 static const wxString mode
[] = { wxT("open"), wxT("save") };
168 m_radioFilePickerMode
= new wxRadioBox(this, wxID_ANY
, wxT("wxFilePicker mode"),
169 wxDefaultPosition
, wxDefaultSize
,
170 WXSIZEOF(mode
), mode
);
171 boxleft
->Add(m_radioFilePickerMode
, 0, wxALL
|wxGROW
, 5);
173 wxStaticBoxSizer
*filebox
= new wxStaticBoxSizer(wxVERTICAL
, this, wxT("&FilePicker style"));
174 m_chkFileTextCtrl
= CreateCheckBoxAndAddToSizer(filebox
, wxT("With textctrl"), false);
175 m_chkFileOverwritePrompt
= CreateCheckBoxAndAddToSizer(filebox
, wxT("Overwrite prompt"), false);
176 m_chkFileMustExist
= CreateCheckBoxAndAddToSizer(filebox
, wxT("File must exist"), false);
177 m_chkFileChangeDir
= CreateCheckBoxAndAddToSizer(filebox
, wxT("Change working dir"), false);
178 m_chkSmall
= CreateCheckBoxAndAddToSizer(filebox
, "&Small version", false);
180 boxleft
->Add(filebox
, 0, wxALL
|wxGROW
, 5);
182 boxleft
->Add(CreateSizerWithTextAndButton
185 "&Initial directory",
188 ), wxSizerFlags().Expand().Border());
190 boxleft
->AddSpacer(10);
192 boxleft
->Add(new wxButton(this, PickerPage_Reset
, wxT("&Reset")),
193 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
195 Reset(); // set checkboxes state
202 m_sizer
= new wxBoxSizer(wxVERTICAL
);
203 m_sizer
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
204 m_sizer
->Add(m_filePicker
, 0, wxEXPAND
|wxALL
, 5);
205 m_sizer
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
208 wxSizer
*sz
= new wxBoxSizer(wxHORIZONTAL
);
209 sz
->Add(boxleft
, 0, wxGROW
|wxALL
, 5);
210 sz
->Add(m_sizer
, 1, wxGROW
|wxALL
, 5);
215 void FilePickerWidgetsPage::CreatePicker()
219 // pass an empty string as initial file
220 m_filePicker
= new wxFilePickerCtrl(this, PickerPage_File
,
222 wxT("Hello!"), wxT("*"),
223 wxDefaultPosition
, wxDefaultSize
,
227 long FilePickerWidgetsPage::GetPickerStyle()
231 if ( m_chkFileTextCtrl
->GetValue() )
232 style
|= wxFLP_USE_TEXTCTRL
;
234 if ( m_chkFileOverwritePrompt
->GetValue() )
235 style
|= wxFLP_OVERWRITE_PROMPT
;
237 if ( m_chkFileMustExist
->GetValue() )
238 style
|= wxFLP_FILE_MUST_EXIST
;
240 if ( m_chkFileChangeDir
->GetValue() )
241 style
|= wxFLP_CHANGE_DIR
;
243 if ( m_chkSmall
->GetValue() )
244 style
|= wxFLP_SMALL
;
246 if (m_radioFilePickerMode
->GetSelection() == FilePickerMode_Open
)
254 void FilePickerWidgetsPage::RecreatePicker()
258 m_sizer
->Insert(1, m_filePicker
, 0, wxEXPAND
|wxALL
, 5);
263 void FilePickerWidgetsPage::Reset()
265 m_radioFilePickerMode
->SetSelection((wxFLP_DEFAULT_STYLE
& wxFLP_OPEN
) ?
266 FilePickerMode_Open
: FilePickerMode_Save
);
267 m_chkFileTextCtrl
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_USE_TEXTCTRL
) != 0);
268 m_chkFileOverwritePrompt
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_OVERWRITE_PROMPT
) != 0);
269 m_chkFileMustExist
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_FILE_MUST_EXIST
) != 0);
270 m_chkFileChangeDir
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_CHANGE_DIR
) != 0);
271 m_chkSmall
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_SMALL
) != 0);
273 UpdateFilePickerMode();
276 void FilePickerWidgetsPage::UpdateFilePickerMode()
278 switch (m_radioFilePickerMode
->GetSelection())
280 case FilePickerMode_Open
:
281 m_chkFileOverwritePrompt
->SetValue(false);
282 m_chkFileOverwritePrompt
->Disable();
283 m_chkFileMustExist
->Enable();
285 case FilePickerMode_Save
:
286 m_chkFileMustExist
->SetValue(false);
287 m_chkFileMustExist
->Disable();
288 m_chkFileOverwritePrompt
->Enable();
294 // ----------------------------------------------------------------------------
296 // ----------------------------------------------------------------------------
298 void FilePickerWidgetsPage::OnButtonSetDir(wxCommandEvent
& WXUNUSED(event
))
300 m_filePicker
->SetInitialDirectory(m_textInitialDir
->GetValue());
303 void FilePickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
310 void FilePickerWidgetsPage::OnFileChange(wxFileDirPickerEvent
& event
)
312 wxLogMessage(wxT("The file changed to '%s' ! The current working directory is '%s'"),
313 event
.GetPath().c_str(), wxGetCwd().c_str());
316 void FilePickerWidgetsPage::OnCheckBox(wxCommandEvent
&event
)
318 if (event
.GetEventObject() == m_chkFileTextCtrl
||
319 event
.GetEventObject() == m_chkFileOverwritePrompt
||
320 event
.GetEventObject() == m_chkFileMustExist
||
321 event
.GetEventObject() == m_chkFileChangeDir
||
322 event
.GetEventObject() == m_chkSmall
)
325 if (event
.GetEventObject() == m_radioFilePickerMode
)
327 UpdateFilePickerMode();
332 #endif // wxUSE_FILEPICKERCTRL