]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/filepicker.cpp
Remove assert checking bitmap size in wxMenuItem drawing code in wxMSW.
[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 // Licence: wxWindows licence
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 #include "wx/textctrl.h"
35 #endif
36
37 #include "wx/artprov.h"
38 #include "wx/sizer.h"
39 #include "wx/stattext.h"
40 #include "wx/checkbox.h"
41 #include "wx/imaglist.h"
42
43 #include "wx/filepicker.h"
44 #include "widgets.h"
45
46 #include "icons/filepicker.xpm"
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 enum
53 {
54 FilePickerMode_Open = 0,
55 FilePickerMode_Save
56 };
57
58 // control ids
59 enum
60 {
61 PickerPage_Reset = wxID_HIGHEST,
62 PickerPage_File,
63 PickerPage_SetDir
64 };
65
66
67 // ----------------------------------------------------------------------------
68 // FilePickerWidgetsPage
69 // ----------------------------------------------------------------------------
70
71 class FilePickerWidgetsPage : public WidgetsPage
72 {
73 public:
74 FilePickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
75 virtual ~FilePickerWidgetsPage(){};
76
77 virtual wxControl *GetWidget() const { return m_filePicker; }
78 virtual void RecreateWidget() { RecreatePicker(); }
79
80 // lazy creation of the content
81 virtual void CreateContent();
82
83 protected:
84
85 // called only once at first construction
86 void CreatePicker();
87
88 // called to recreate an existing control
89 void RecreatePicker();
90
91 // restore the checkboxes state to the initial values
92 void Reset();
93
94 // get the initial style for the picker of the given kind
95 long GetPickerStyle();
96
97 // update filepicker radiobox
98 void UpdateFilePickerMode();
99
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);
105
106
107 // the picker
108 wxFilePickerCtrl *m_filePicker;
109
110
111 // other controls
112 // --------------
113
114 wxCheckBox *m_chkFileTextCtrl,
115 *m_chkFileOverwritePrompt,
116 *m_chkFileMustExist,
117 *m_chkFileChangeDir,
118 *m_chkSmall;
119 wxRadioBox *m_radioFilePickerMode;
120 wxTextCtrl *m_textInitialDir;
121
122 wxBoxSizer *m_sizer;
123
124 private:
125 DECLARE_EVENT_TABLE()
126 DECLARE_WIDGETS_PAGE(FilePickerWidgetsPage)
127 };
128
129 // ----------------------------------------------------------------------------
130 // event tables
131 // ----------------------------------------------------------------------------
132
133 BEGIN_EVENT_TABLE(FilePickerWidgetsPage, WidgetsPage)
134 EVT_BUTTON(PickerPage_Reset, FilePickerWidgetsPage::OnButtonReset)
135 EVT_BUTTON(PickerPage_SetDir, FilePickerWidgetsPage::OnButtonSetDir)
136
137 EVT_FILEPICKER_CHANGED(PickerPage_File, FilePickerWidgetsPage::OnFileChange)
138
139 EVT_CHECKBOX(wxID_ANY, FilePickerWidgetsPage::OnCheckBox)
140 EVT_RADIOBOX(wxID_ANY, FilePickerWidgetsPage::OnCheckBox)
141 END_EVENT_TABLE()
142
143 // ============================================================================
144 // implementation
145 // ============================================================================
146
147 #if defined(__WXGTK24__)
148 #define FAMILY_CTRLS NATIVE_CTRLS
149 #else
150 #define FAMILY_CTRLS GENERIC_CTRLS
151 #endif
152
153 IMPLEMENT_WIDGETS_PAGE(FilePickerWidgetsPage, wxT("FilePicker"),
154 PICKER_CTRLS | FAMILY_CTRLS);
155
156 FilePickerWidgetsPage::FilePickerWidgetsPage(WidgetsBookCtrl *book,
157 wxImageList *imaglist)
158 : WidgetsPage(book, imaglist, filepicker_xpm)
159 {
160 }
161
162 void FilePickerWidgetsPage::CreateContent()
163 {
164 // left pane
165 wxSizer *boxleft = new wxBoxSizer(wxVERTICAL);
166
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);
172
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);
179
180 boxleft->Add(filebox, 0, wxALL|wxGROW, 5);
181
182 boxleft->Add(CreateSizerWithTextAndButton
183 (
184 PickerPage_SetDir,
185 "&Initial directory",
186 wxID_ANY,
187 &m_textInitialDir
188 ), wxSizerFlags().Expand().Border());
189
190 boxleft->AddSpacer(10);
191
192 boxleft->Add(new wxButton(this, PickerPage_Reset, wxT("&Reset")),
193 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
194
195 Reset(); // set checkboxes state
196
197 // create pickers
198 m_filePicker = NULL;
199 CreatePicker();
200
201 // right pane
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
206
207 // global pane
208 wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
209 sz->Add(boxleft, 0, wxGROW|wxALL, 5);
210 sz->Add(m_sizer, 1, wxGROW|wxALL, 5);
211
212 SetSizer(sz);
213 }
214
215 void FilePickerWidgetsPage::CreatePicker()
216 {
217 delete m_filePicker;
218
219 // pass an empty string as initial file
220 m_filePicker = new wxFilePickerCtrl(this, PickerPage_File,
221 wxEmptyString,
222 wxT("Hello!"), wxT("*"),
223 wxDefaultPosition, wxDefaultSize,
224 GetPickerStyle());
225 }
226
227 long FilePickerWidgetsPage::GetPickerStyle()
228 {
229 long style = 0;
230
231 if ( m_chkFileTextCtrl->GetValue() )
232 style |= wxFLP_USE_TEXTCTRL;
233
234 if ( m_chkFileOverwritePrompt->GetValue() )
235 style |= wxFLP_OVERWRITE_PROMPT;
236
237 if ( m_chkFileMustExist->GetValue() )
238 style |= wxFLP_FILE_MUST_EXIST;
239
240 if ( m_chkFileChangeDir->GetValue() )
241 style |= wxFLP_CHANGE_DIR;
242
243 if ( m_chkSmall->GetValue() )
244 style |= wxFLP_SMALL;
245
246 if (m_radioFilePickerMode->GetSelection() == FilePickerMode_Open)
247 style |= wxFLP_OPEN;
248 else
249 style |= wxFLP_SAVE;
250
251 return style;
252 }
253
254 void FilePickerWidgetsPage::RecreatePicker()
255 {
256 m_sizer->Remove(1);
257 CreatePicker();
258 m_sizer->Insert(1, m_filePicker, 0, wxEXPAND|wxALL, 5);
259
260 m_sizer->Layout();
261 }
262
263 void FilePickerWidgetsPage::Reset()
264 {
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);
272
273 UpdateFilePickerMode();
274 }
275
276 void FilePickerWidgetsPage::UpdateFilePickerMode()
277 {
278 switch (m_radioFilePickerMode->GetSelection())
279 {
280 case FilePickerMode_Open:
281 m_chkFileOverwritePrompt->SetValue(false);
282 m_chkFileOverwritePrompt->Disable();
283 m_chkFileMustExist->Enable();
284 break;
285 case FilePickerMode_Save:
286 m_chkFileMustExist->SetValue(false);
287 m_chkFileMustExist->Disable();
288 m_chkFileOverwritePrompt->Enable();
289 break;
290 }
291 }
292
293
294 // ----------------------------------------------------------------------------
295 // event handlers
296 // ----------------------------------------------------------------------------
297
298 void FilePickerWidgetsPage::OnButtonSetDir(wxCommandEvent& WXUNUSED(event))
299 {
300 m_filePicker->SetInitialDirectory(m_textInitialDir->GetValue());
301 }
302
303 void FilePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
304 {
305 Reset();
306
307 RecreatePicker();
308 }
309
310 void FilePickerWidgetsPage::OnFileChange(wxFileDirPickerEvent& event)
311 {
312 wxLogMessage(wxT("The file changed to '%s' ! The current working directory is '%s'"),
313 event.GetPath().c_str(), wxGetCwd().c_str());
314 }
315
316 void FilePickerWidgetsPage::OnCheckBox(wxCommandEvent &event)
317 {
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)
323 RecreatePicker();
324
325 if (event.GetEventObject() == m_radioFilePickerMode)
326 {
327 UpdateFilePickerMode();
328 RecreatePicker();
329 }
330 }
331
332 #endif // wxUSE_FILEPICKERCTRL