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
, wxT("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
[] = { wxT("open"), wxT("save") };
162 m_radioFilePickerMode
= new wxRadioBox(this, wxID_ANY
, wxT("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, wxT("&FilePicker style"));
168 m_chkFileTextCtrl
= CreateCheckBoxAndAddToSizer(filebox
, wxT("With textctrl"), false);
169 m_chkFileOverwritePrompt
= CreateCheckBoxAndAddToSizer(filebox
, wxT("Overwrite prompt"), false);
170 m_chkFileMustExist
= CreateCheckBoxAndAddToSizer(filebox
, wxT("File must exist"), false);
171 m_chkFileChangeDir
= CreateCheckBoxAndAddToSizer(filebox
, wxT("Change working dir"), false);
172 boxleft
->Add(filebox
, 0, wxALL
|wxGROW
, 5);
174 boxleft
->Add(new wxButton(this, PickerPage_Reset
, wxT("&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 wxString path
= "/home/robert/wxDesigner.tar.gz";
203 // pass an empty string as initial file
204 m_filePicker
= new wxFilePickerCtrl(this, PickerPage_File
,
206 wxT("Hello!"), wxT("*"),
207 wxDefaultPosition
, wxDefaultSize
,
211 long FilePickerWidgetsPage::GetPickerStyle()
215 if ( m_chkFileTextCtrl
->GetValue() )
216 style
|= wxFLP_USE_TEXTCTRL
;
218 if ( m_chkFileOverwritePrompt
->GetValue() )
219 style
|= wxFLP_OVERWRITE_PROMPT
;
221 if ( m_chkFileMustExist
->GetValue() )
222 style
|= wxFLP_FILE_MUST_EXIST
;
224 if ( m_chkFileChangeDir
->GetValue() )
225 style
|= wxFLP_CHANGE_DIR
;
227 if (m_radioFilePickerMode
->GetSelection() == FilePickerMode_Open
)
235 void FilePickerWidgetsPage::RecreatePicker()
239 m_sizer
->Insert(1, m_filePicker
, 0, wxALIGN_CENTER
||wxALL
, 5);
244 void FilePickerWidgetsPage::Reset()
246 m_radioFilePickerMode
->SetSelection((wxFLP_DEFAULT_STYLE
& wxFLP_OPEN
) ?
247 FilePickerMode_Open
: FilePickerMode_Save
);
248 m_chkFileTextCtrl
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_USE_TEXTCTRL
) != 0);
249 m_chkFileOverwritePrompt
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_OVERWRITE_PROMPT
) != 0);
250 m_chkFileMustExist
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_FILE_MUST_EXIST
) != 0);
251 m_chkFileChangeDir
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_CHANGE_DIR
) != 0);
253 UpdateFilePickerMode();
256 void FilePickerWidgetsPage::UpdateFilePickerMode()
258 switch (m_radioFilePickerMode
->GetSelection())
260 case FilePickerMode_Open
:
261 m_chkFileOverwritePrompt
->SetValue(false);
262 m_chkFileOverwritePrompt
->Disable();
263 m_chkFileMustExist
->Enable();
265 case FilePickerMode_Save
:
266 m_chkFileMustExist
->SetValue(false);
267 m_chkFileMustExist
->Disable();
268 m_chkFileOverwritePrompt
->Enable();
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
278 void FilePickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
285 void FilePickerWidgetsPage::OnFileChange(wxFileDirPickerEvent
& event
)
287 wxLogMessage(wxT("The file changed to '%s' ! The current working directory is '%s'"),
288 event
.GetPath().c_str(), wxGetCwd().c_str());
291 void FilePickerWidgetsPage::OnCheckBox(wxCommandEvent
&event
)
293 if (event
.GetEventObject() == m_chkFileTextCtrl
||
294 event
.GetEventObject() == m_chkFileOverwritePrompt
||
295 event
.GetEventObject() == m_chkFileMustExist
||
296 event
.GetEventObject() == m_chkFileChangeDir
)
299 if (event
.GetEventObject() == m_radioFilePickerMode
)
301 UpdateFilePickerMode();
306 #endif // wxUSE_FILEPICKERCTRL