Add tests for wxSL_MIN_MAX_LABELS and wxSL_VALUE_LABEL
[wxWidgets.git] / samples / widgets / filepicker.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: filepicker.cpp
4 // Purpose: Part of the widgets sample showing wx*PickerCtrl
5 // Author: Francesco Montorsi
6 // Created: 20/6/2006
7 // Id: $Id$
8 // Copyright: (c) 2006 Francesco Montorsi
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_FILEPICKERCTRL
28
29 // for all others, include the necessary headers
30 #ifndef WX_PRECOMP
31 #include "wx/app.h"
32 #include "wx/log.h"
33 #include "wx/radiobox.h"
34 #endif
35
36 #include "wx/artprov.h"
37 #include "wx/sizer.h"
38 #include "wx/stattext.h"
39 #include "wx/checkbox.h"
40 #include "wx/imaglist.h"
41
42 #include "wx/filepicker.h"
43 #include "widgets.h"
44
45 #include "icons/filepicker.xpm"
46
47 // ----------------------------------------------------------------------------
48 // constants
49 // ----------------------------------------------------------------------------
50
51 enum
52 {
53 FilePickerMode_Open = 0,
54 FilePickerMode_Save
55 };
56
57 // control ids
58 enum
59 {
60 PickerPage_Reset = wxID_HIGHEST,
61 PickerPage_File
62 };
63
64
65 // ----------------------------------------------------------------------------
66 // FilePickerWidgetsPage
67 // ----------------------------------------------------------------------------
68
69 class FilePickerWidgetsPage : public WidgetsPage
70 {
71 public:
72 FilePickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
73 virtual ~FilePickerWidgetsPage(){};
74
75 virtual wxControl *GetWidget() const { return m_filePicker; }
76 virtual void RecreateWidget() { RecreatePicker(); }
77
78 // lazy creation of the content
79 virtual void CreateContent();
80
81 protected:
82
83 // called only once at first construction
84 void CreatePicker();
85
86 // called to recreate an existing control
87 void RecreatePicker();
88
89 // restore the checkboxes state to the initial values
90 void Reset();
91
92 // get the initial style for the picker of the given kind
93 long GetPickerStyle();
94
95 // update filepicker radiobox
96 void UpdateFilePickerMode();
97
98 // the pickers and the relative event handlers
99 void OnFileChange(wxFileDirPickerEvent &ev);
100 void OnCheckBox(wxCommandEvent &ev);
101 void OnButtonReset(wxCommandEvent &ev);
102
103
104 // the picker
105 wxFilePickerCtrl *m_filePicker;
106
107
108 // other controls
109 // --------------
110
111 wxCheckBox *m_chkFileTextCtrl,
112 *m_chkFileOverwritePrompt,
113 *m_chkFileMustExist,
114 *m_chkFileChangeDir;
115 wxRadioBox *m_radioFilePickerMode;
116
117 wxBoxSizer *m_sizer;
118
119 private:
120 DECLARE_EVENT_TABLE()
121 DECLARE_WIDGETS_PAGE(FilePickerWidgetsPage)
122 };
123
124 // ----------------------------------------------------------------------------
125 // event tables
126 // ----------------------------------------------------------------------------
127
128 BEGIN_EVENT_TABLE(FilePickerWidgetsPage, WidgetsPage)
129 EVT_BUTTON(PickerPage_Reset, FilePickerWidgetsPage::OnButtonReset)
130
131 EVT_FILEPICKER_CHANGED(PickerPage_File, FilePickerWidgetsPage::OnFileChange)
132
133 EVT_CHECKBOX(wxID_ANY, FilePickerWidgetsPage::OnCheckBox)
134 EVT_RADIOBOX(wxID_ANY, FilePickerWidgetsPage::OnCheckBox)
135 END_EVENT_TABLE()
136
137 // ============================================================================
138 // implementation
139 // ============================================================================
140
141 #if defined(__WXGTK24__)
142 #define FAMILY_CTRLS NATIVE_CTRLS
143 #else
144 #define FAMILY_CTRLS GENERIC_CTRLS
145 #endif
146
147 IMPLEMENT_WIDGETS_PAGE(FilePickerWidgetsPage, wxT("FilePicker"),
148 PICKER_CTRLS | FAMILY_CTRLS);
149
150 FilePickerWidgetsPage::FilePickerWidgetsPage(WidgetsBookCtrl *book,
151 wxImageList *imaglist)
152 : WidgetsPage(book, imaglist, filepicker_xpm)
153 {
154 }
155
156 void FilePickerWidgetsPage::CreateContent()
157 {
158 // left pane
159 wxSizer *boxleft = new wxBoxSizer(wxVERTICAL);
160
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);
166
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);
173
174 boxleft->Add(new wxButton(this, PickerPage_Reset, wxT("&Reset")),
175 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
176
177 Reset(); // set checkboxes state
178
179 // create pickers
180 m_filePicker = NULL;
181 CreatePicker();
182
183 // right pane
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
188
189 // global pane
190 wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
191 sz->Add(boxleft, 0, wxGROW|wxALL, 5);
192 sz->Add(m_sizer, 1, wxGROW|wxALL, 5);
193
194 SetSizer(sz);
195 }
196
197 void FilePickerWidgetsPage::CreatePicker()
198 {
199 delete m_filePicker;
200
201 wxString path = "/home/robert/wxDesigner.tar.gz";
202
203 // pass an empty string as initial file
204 m_filePicker = new wxFilePickerCtrl(this, PickerPage_File,
205 path,
206 wxT("Hello!"), wxT("*"),
207 wxDefaultPosition, wxDefaultSize,
208 GetPickerStyle());
209 }
210
211 long FilePickerWidgetsPage::GetPickerStyle()
212 {
213 long style = 0;
214
215 if ( m_chkFileTextCtrl->GetValue() )
216 style |= wxFLP_USE_TEXTCTRL;
217
218 if ( m_chkFileOverwritePrompt->GetValue() )
219 style |= wxFLP_OVERWRITE_PROMPT;
220
221 if ( m_chkFileMustExist->GetValue() )
222 style |= wxFLP_FILE_MUST_EXIST;
223
224 if ( m_chkFileChangeDir->GetValue() )
225 style |= wxFLP_CHANGE_DIR;
226
227 if (m_radioFilePickerMode->GetSelection() == FilePickerMode_Open)
228 style |= wxFLP_OPEN;
229 else
230 style |= wxFLP_SAVE;
231
232 return style;
233 }
234
235 void FilePickerWidgetsPage::RecreatePicker()
236 {
237 m_sizer->Remove(1);
238 CreatePicker();
239 m_sizer->Insert(1, m_filePicker, 0, wxALIGN_CENTER||wxALL, 5);
240
241 m_sizer->Layout();
242 }
243
244 void FilePickerWidgetsPage::Reset()
245 {
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);
252
253 UpdateFilePickerMode();
254 }
255
256 void FilePickerWidgetsPage::UpdateFilePickerMode()
257 {
258 switch (m_radioFilePickerMode->GetSelection())
259 {
260 case FilePickerMode_Open:
261 m_chkFileOverwritePrompt->SetValue(false);
262 m_chkFileOverwritePrompt->Disable();
263 m_chkFileMustExist->Enable();
264 break;
265 case FilePickerMode_Save:
266 m_chkFileMustExist->SetValue(false);
267 m_chkFileMustExist->Disable();
268 m_chkFileOverwritePrompt->Enable();
269 break;
270 }
271 }
272
273
274 // ----------------------------------------------------------------------------
275 // event handlers
276 // ----------------------------------------------------------------------------
277
278 void FilePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
279 {
280 Reset();
281
282 RecreatePicker();
283 }
284
285 void FilePickerWidgetsPage::OnFileChange(wxFileDirPickerEvent& event)
286 {
287 wxLogMessage(wxT("The file changed to '%s' ! The current working directory is '%s'"),
288 event.GetPath().c_str(), wxGetCwd().c_str());
289 }
290
291 void FilePickerWidgetsPage::OnCheckBox(wxCommandEvent &event)
292 {
293 if (event.GetEventObject() == m_chkFileTextCtrl ||
294 event.GetEventObject() == m_chkFileOverwritePrompt ||
295 event.GetEventObject() == m_chkFileMustExist ||
296 event.GetEventObject() == m_chkFileChangeDir)
297 RecreatePicker();
298
299 if (event.GetEventObject() == m_radioFilePickerMode)
300 {
301 UpdateFilePickerMode();
302 RecreatePicker();
303 }
304 }
305
306 #endif // wxUSE_FILEPICKERCTRL