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 // License: wxWindows license
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
,
115 wxRadioBox
*m_radioFilePickerMode
;
120 DECLARE_EVENT_TABLE()
121 DECLARE_WIDGETS_PAGE(FilePickerWidgetsPage
)
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 BEGIN_EVENT_TABLE(FilePickerWidgetsPage
, WidgetsPage
)
129 EVT_BUTTON(PickerPage_Reset
, FilePickerWidgetsPage::OnButtonReset
)
131 EVT_FILEPICKER_CHANGED(PickerPage_File
, FilePickerWidgetsPage::OnFileChange
)
133 EVT_CHECKBOX(wxID_ANY
, FilePickerWidgetsPage::OnCheckBox
)
134 EVT_RADIOBOX(wxID_ANY
, FilePickerWidgetsPage::OnCheckBox
)
137 // ============================================================================
139 // ============================================================================
141 #if defined(__WXGTK24__)
142 #define FAMILY_CTRLS NATIVE_CTRLS
144 #define FAMILY_CTRLS GENERIC_CTRLS
147 IMPLEMENT_WIDGETS_PAGE(FilePickerWidgetsPage
, _T("FilePicker"),
148 PICKER_CTRLS
| FAMILY_CTRLS
);
150 FilePickerWidgetsPage::FilePickerWidgetsPage(WidgetsBookCtrl
*book
,
151 wxImageList
*imaglist
)
152 : WidgetsPage(book
, imaglist
, filepicker_xpm
)
156 void FilePickerWidgetsPage::CreateContent()
159 wxSizer
*boxleft
= new wxBoxSizer(wxVERTICAL
);
161 static const wxString mode
[] = { _T("open"), _T("save") };
162 m_radioFilePickerMode
= new wxRadioBox(this, wxID_ANY
, _T("wxFilePicker mode"),
163 wxDefaultPosition
, wxDefaultSize
,
164 WXSIZEOF(mode
), mode
);
165 boxleft
->Add(m_radioFilePickerMode
, 0, wxALL
|wxGROW
, 5);
167 wxStaticBoxSizer
*filebox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&FilePicker style"));
168 m_chkFileTextCtrl
= CreateCheckBoxAndAddToSizer(filebox
, _T("With textctrl"), false);
169 m_chkFileOverwritePrompt
= CreateCheckBoxAndAddToSizer(filebox
, _T("Overwrite prompt"), false);
170 m_chkFileMustExist
= CreateCheckBoxAndAddToSizer(filebox
, _T("File must exist"), false);
171 m_chkFileChangeDir
= CreateCheckBoxAndAddToSizer(filebox
, _T("Change working dir"), false);
172 boxleft
->Add(filebox
, 0, wxALL
|wxGROW
, 5);
174 boxleft
->Add(new wxButton(this, PickerPage_Reset
, _T("&Reset")),
175 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
177 Reset(); // set checkboxes state
184 m_sizer
= new wxBoxSizer(wxVERTICAL
);
185 m_sizer
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
186 m_sizer
->Add(m_filePicker
, 0, wxALIGN_CENTER
|wxALL
, 5);
187 m_sizer
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
190 wxSizer
*sz
= new wxBoxSizer(wxHORIZONTAL
);
191 sz
->Add(boxleft
, 0, wxGROW
|wxALL
, 5);
192 sz
->Add(m_sizer
, 1, wxGROW
|wxALL
, 5);
197 void FilePickerWidgetsPage::CreatePicker()
201 // pass an empty string as initial file
202 m_filePicker
= new wxFilePickerCtrl(this, PickerPage_File
,
204 wxT("Hello!"), wxT("*"),
205 wxDefaultPosition
, wxDefaultSize
,
209 long FilePickerWidgetsPage::GetPickerStyle()
213 if ( m_chkFileTextCtrl
->GetValue() )
214 style
|= wxFLP_USE_TEXTCTRL
;
216 if ( m_chkFileOverwritePrompt
->GetValue() )
217 style
|= wxFLP_OVERWRITE_PROMPT
;
219 if ( m_chkFileMustExist
->GetValue() )
220 style
|= wxFLP_FILE_MUST_EXIST
;
222 if ( m_chkFileChangeDir
->GetValue() )
223 style
|= wxFLP_CHANGE_DIR
;
225 if (m_radioFilePickerMode
->GetSelection() == FilePickerMode_Open
)
233 void FilePickerWidgetsPage::RecreatePicker()
237 m_sizer
->Insert(1, m_filePicker
, 0, wxALIGN_CENTER
||wxALL
, 5);
242 void FilePickerWidgetsPage::Reset()
244 m_radioFilePickerMode
->SetSelection((wxFLP_DEFAULT_STYLE
& wxFLP_OPEN
) ?
245 FilePickerMode_Open
: FilePickerMode_Save
);
246 m_chkFileTextCtrl
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_USE_TEXTCTRL
) != 0);
247 m_chkFileOverwritePrompt
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_OVERWRITE_PROMPT
) != 0);
248 m_chkFileMustExist
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_FILE_MUST_EXIST
) != 0);
249 m_chkFileChangeDir
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_CHANGE_DIR
) != 0);
251 UpdateFilePickerMode();
254 void FilePickerWidgetsPage::UpdateFilePickerMode()
256 switch (m_radioFilePickerMode
->GetSelection())
258 case FilePickerMode_Open
:
259 m_chkFileOverwritePrompt
->SetValue(false);
260 m_chkFileOverwritePrompt
->Disable();
261 m_chkFileMustExist
->Enable();
263 case FilePickerMode_Save
:
264 m_chkFileMustExist
->SetValue(false);
265 m_chkFileMustExist
->Disable();
266 m_chkFileOverwritePrompt
->Enable();
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
276 void FilePickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
283 void FilePickerWidgetsPage::OnFileChange(wxFileDirPickerEvent
& event
)
285 wxLogMessage(wxT("The file changed to '%s' ! The current working directory is '%s'"),
286 event
.GetPath().c_str(), wxGetCwd().c_str());
289 void FilePickerWidgetsPage::OnCheckBox(wxCommandEvent
&event
)
291 if (event
.GetEventObject() == m_chkFileTextCtrl
||
292 event
.GetEventObject() == m_chkFileOverwritePrompt
||
293 event
.GetEventObject() == m_chkFileMustExist
||
294 event
.GetEventObject() == m_chkFileChangeDir
)
297 if (event
.GetEventObject() == m_radioFilePickerMode
)
299 UpdateFilePickerMode();
304 #endif // wxUSE_FILEPICKERCTRL