1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
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_COLOURPICKERCTRL || wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL || wxUSE_FONTPICKERCTRL
29 // for all others, include the necessary headers
35 #include "wx/artprov.h"
37 #include "wx/stattext.h"
38 #include "wx/checkbox.h"
39 #include "wx/imaglist.h"
41 #include "wx/clrpicker.h"
42 #include "wx/filepicker.h"
43 #include "wx/fontpicker.h"
47 #include "icons/picker.xpm"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
56 PickerPage_Reset
= wxID_HIGHEST
,
58 #if wxUSE_COLOURPICKERCTRL
61 #if wxUSE_FILEPICKERCTRL
64 #if wxUSE_DIRPICKERCTRL
67 #if wxUSE_FONTPICKERCTRL
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 class PickerWidgetsPage
: public WidgetsPage
80 PickerWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
81 virtual ~PickerWidgetsPage(){};
83 virtual wxControl
*GetWidget() const { /*return m_fontPicker;*/ return NULL
; }
84 virtual void RecreateWidget() { RecreateAllPickers(); }
96 // called only once at first construction
97 void CreatePickers(PickerKind picker
);
99 // called to recreate an existing control
100 void RecreatePicker(PickerKind picker
);
102 // recreate all existing picker controls
103 void RecreateAllPickers();
105 // restore the checkboxes state to the initial values
108 // get the initial style for the picker of the given kind
109 long GetPickerStyle(PickerKind kind
);
112 // the pickers and the relative event handlers
113 #if wxUSE_COLOURPICKERCTRL
114 wxColourPickerCtrl
*m_clrPicker
;
115 void OnColourChange(wxColourPickerEvent
&ev
);
117 #if wxUSE_FILEPICKERCTRL
118 wxFilePickerCtrl
*m_filePicker
;
119 void OnFileChange(wxFileDirPickerEvent
&ev
);
121 #if wxUSE_DIRPICKERCTRL
122 wxDirPickerCtrl
*m_dirPicker
;
123 void OnDirChange(wxFileDirPickerEvent
&ev
);
125 #if wxUSE_FONTPICKERCTRL
126 wxFontPickerCtrl
*m_fontPicker
;
127 void OnFontChange(wxFontPickerEvent
&ev
);
129 void OnCheckBox(wxCommandEvent
&ev
);
130 void OnButtonReset(wxCommandEvent
&ev
);
136 wxCheckBox
*m_chkColourTextCtrl
,
137 *m_chkColourShowLabel
;
139 wxCheckBox
*m_chkFileTextCtrl
,
140 *m_chkFileOverwritePrompt
,
144 wxCheckBox
*m_chkDirTextCtrl
,
148 wxCheckBox
*m_chkFontTextCtrl
,
149 *m_chkFontDescAsLabel
,
150 *m_chkFontUseFontForLabel
;
152 wxFlexGridSizer
*m_sizerPicker
;
155 DECLARE_EVENT_TABLE()
156 DECLARE_WIDGETS_PAGE(PickerWidgetsPage
)
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 BEGIN_EVENT_TABLE(PickerWidgetsPage
, WidgetsPage
)
164 EVT_BUTTON(PickerPage_Reset
, PickerWidgetsPage::OnButtonReset
)
166 #if wxUSE_COLOURPICKERCTRL
167 EVT_COLOURPICKER_CHANGED(PickerPage_Colour
, PickerWidgetsPage::OnColourChange
)
169 #if wxUSE_FILEPICKERCTRL
170 EVT_FILEPICKER_CHANGED(PickerPage_File
, PickerWidgetsPage::OnFileChange
)
172 #if wxUSE_DIRPICKERCTRL
173 EVT_DIRPICKER_CHANGED(PickerPage_Dir
, PickerWidgetsPage::OnDirChange
)
175 #if wxUSE_FONTPICKERCTRL
176 EVT_FONTPICKER_CHANGED(PickerPage_Font
, PickerWidgetsPage::OnFontChange
)
179 EVT_CHECKBOX(wxID_ANY
, PickerWidgetsPage::OnCheckBox
)
182 // ============================================================================
184 // ============================================================================
186 IMPLEMENT_WIDGETS_PAGE(PickerWidgetsPage
, _T("Pickers"), PICKER_CTRLS
);
188 PickerWidgetsPage::PickerWidgetsPage(WidgetsBookCtrl
*book
,
189 wxImageList
*imaglist
)
190 : WidgetsPage(book
, imaglist
, picker_xpm
)
192 imaglist
->Add(wxBitmap(picker_xpm
));
195 wxSizer
*boxleft
= new wxBoxSizer(wxVERTICAL
);
197 #if wxUSE_COLOURPICKERCTRL
198 wxStaticBoxSizer
*clrbox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&ColourPicker style"));
199 m_chkColourTextCtrl
= CreateCheckBoxAndAddToSizer(clrbox
, _T("With textctrl"), false);
200 m_chkColourShowLabel
= CreateCheckBoxAndAddToSizer(clrbox
, _T("With label"), false);
201 boxleft
->Add(clrbox
, 0, wxALL
|wxGROW
, 5);
202 boxleft
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
203 #endif // wxUSE_COLOURPICKERCTRL
205 #if wxUSE_FILEPICKERCTRL
206 wxStaticBoxSizer
*filebox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&FilePicker style"));
207 m_chkFileTextCtrl
= CreateCheckBoxAndAddToSizer(filebox
, _T("With textctrl"), false);
208 m_chkFileOverwritePrompt
= CreateCheckBoxAndAddToSizer(filebox
, _T("Overwrite prompt"), false);
209 m_chkFileMustExist
= CreateCheckBoxAndAddToSizer(filebox
, _T("File must exist"), false);
210 m_chkFileChangeDir
= CreateCheckBoxAndAddToSizer(filebox
, _T("Change working dir"), false);
211 boxleft
->Add(filebox
, 0, wxALL
|wxGROW
, 5);
212 boxleft
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
213 #endif // wxUSE_FILEPICKERCTRL
215 #if wxUSE_DIRPICKERCTRL
216 wxStaticBoxSizer
*dirbox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&DirPicker style"));
217 m_chkDirTextCtrl
= CreateCheckBoxAndAddToSizer(dirbox
, _T("With textctrl"), false);
218 m_chkDirMustExist
= CreateCheckBoxAndAddToSizer(dirbox
, _T("Dir must exist"), false);
219 m_chkDirChangeDir
= CreateCheckBoxAndAddToSizer(dirbox
, _T("Change working dir"), false);
220 boxleft
->Add(dirbox
, 0, wxALL
|wxGROW
, 5);
221 boxleft
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
222 #endif // wxUSE_DIRPICKERCTRL
224 #if wxUSE_FONTPICKERCTRL
225 wxStaticBoxSizer
*fontbox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&FontPicker style"));
226 m_chkFontTextCtrl
= CreateCheckBoxAndAddToSizer(fontbox
, _T("With textctrl"));
227 m_chkFontDescAsLabel
= CreateCheckBoxAndAddToSizer(fontbox
, _T("Font desc as btn label"));
228 m_chkFontUseFontForLabel
= CreateCheckBoxAndAddToSizer(fontbox
, _T("Use font for label"), false);
229 boxleft
->Add(fontbox
, 0, wxALL
|wxGROW
, 5);
230 #endif // wxUSE_FONTPICKERCTRL
232 boxleft
->Add(new wxButton(this, PickerPage_Reset
, _T("&Reset")),
233 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
235 Reset(); // set checkboxes state
244 #if wxUSE_COLOURPICKERCTRL
245 CreatePickers(Picker_Colour
);
247 #endif // wxUSE_COLOURPICKERCTRL
249 #if wxUSE_FILEPICKERCTRL
250 CreatePickers(Picker_File
);
252 #endif // wxUSE_FILEPICKERCTRL
254 #if wxUSE_DIRPICKERCTRL
255 CreatePickers(Picker_Dir
);
257 #endif // wxUSE_DIRPICKERCTRL
259 #if wxUSE_FONTPICKERCTRL
260 CreatePickers(Picker_Font
);
262 #endif // wxUSE_FONTPICKERCTRL
264 m_sizerPicker
= new wxFlexGridSizer(nrows
, 2, 0, 0); // 4 rows x 2 columns
265 #if wxUSE_COLOURPICKERCTRL
266 m_sizerPicker
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxColourPickerCtrl:")),
267 0, wxALIGN_CENTER
| wxALL
, 5);
268 m_sizerPicker
->Add(m_clrPicker
, 1, wxGROW
| wxALL
, 5);
270 #if wxUSE_FILEPICKERCTRL
271 m_sizerPicker
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxFilePickerCtrl:")),
272 0, wxALIGN_CENTER
| wxALL
, 5);
273 m_sizerPicker
->Add(m_filePicker
, 1, wxGROW
| wxALL
, 5);
275 #if wxUSE_DIRPICKERCTRL
276 m_sizerPicker
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxDirPickerCtrl:")),
277 0, wxALIGN_CENTER
| wxALL
, 5);
278 m_sizerPicker
->Add(m_dirPicker
, 1, wxGROW
| wxALL
, 5);
280 #if wxUSE_FONTPICKERCTRL
281 m_sizerPicker
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxFontPickerCtrl:")),
282 0, wxALIGN_CENTER
| wxALL
, 5);
283 m_sizerPicker
->Add(m_fontPicker
, 1, wxGROW
| wxALL
, 5);
286 m_sizerPicker
->AddGrowableCol(1, 3);
290 boxright
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("Pickers"));
291 boxright
->Add(m_sizerPicker
, 1, wxGROW
| wxALL
, 5);
294 wxSizer
*sz
= new wxBoxSizer(wxHORIZONTAL
);
295 sz
->Add(boxleft
, 0, wxGROW
|wxALL
, 5);
296 sz
->Add(boxright
, 1, wxGROW
|wxALL
, 5);
301 void PickerWidgetsPage::CreatePickers(PickerKind picker
)
305 #if wxUSE_COLOURPICKERCTRL
309 m_clrPicker
= new wxColourPickerCtrl(this, PickerPage_Colour
, *wxRED
,
310 wxDefaultPosition
, wxDefaultSize
,
311 GetPickerStyle(Picker_Colour
));
313 #endif // wxUSE_COLOURPICKERCTRL
315 #if wxUSE_FILEPICKERCTRL
319 // pass an empty string as initial file
320 m_filePicker
= new wxFilePickerCtrl(this, PickerPage_File
,
322 wxT("Hello!"), wxT("*"),
323 wxDefaultPosition
, wxDefaultSize
,
324 GetPickerStyle(Picker_File
));
326 #endif // wxUSE_FILEPICKERCTRL
328 #if wxUSE_DIRPICKERCTRL
332 m_dirPicker
= new wxDirPickerCtrl(this, PickerPage_Dir
,
333 wxGetHomeDir(), wxT("Hello!"),
334 wxDefaultPosition
, wxDefaultSize
,
335 GetPickerStyle(Picker_Dir
));
337 #endif // wxUSE_DIRPICKERCTRL
339 #if wxUSE_FONTPICKERCTRL
343 m_fontPicker
= new wxFontPickerCtrl(this, PickerPage_Font
,
345 wxDefaultPosition
, wxDefaultSize
,
346 GetPickerStyle(Picker_Font
));
348 #endif // wxUSE_FONTPICKERCTRL
351 wxFAIL_MSG( _T("unknown picker kind") );
355 long PickerWidgetsPage::GetPickerStyle(PickerKind picker
)
361 #if wxUSE_COLOURPICKERCTRL
363 if ( m_chkColourTextCtrl
->GetValue() )
364 style
|= wxCLRP_USE_TEXTCTRL
;
366 if ( m_chkColourShowLabel
->GetValue() )
367 style
|= wxCLRP_SHOW_LABEL
;
370 #endif // wxUSE_COLOURPICKERCTRL
372 #if wxUSE_FILEPICKERCTRL
374 if ( m_chkFileTextCtrl
->GetValue() )
375 style
|= wxFLP_USE_TEXTCTRL
;
377 if ( m_chkFileOverwritePrompt
->GetValue() )
378 style
|= wxFLP_OVERWRITE_PROMPT
;
380 if ( m_chkFileMustExist
->GetValue() )
381 style
|= wxFLP_FILE_MUST_EXIST
;
383 if ( m_chkFileChangeDir
->GetValue() )
384 style
|= wxFLP_CHANGE_DIR
;
387 #endif // wxUSE_FILEPICKERCTRL
389 #if wxUSE_DIRPICKERCTRL
391 if ( m_chkDirTextCtrl
->GetValue() )
392 style
|= wxDIRP_USE_TEXTCTRL
;
394 if ( m_chkDirMustExist
->GetValue() )
395 style
|= wxDIRP_DIR_MUST_EXIST
;
397 if ( m_chkDirChangeDir
->GetValue() )
398 style
|= wxDIRP_CHANGE_DIR
;
401 #endif // wxUSE_DIRPICKERCTRL
403 #if wxUSE_FONTPICKERCTRL
405 if ( m_chkFontTextCtrl
->GetValue() )
406 style
|= wxFNTP_USE_TEXTCTRL
;
408 if ( m_chkFontUseFontForLabel
->GetValue() )
409 style
|= wxFNTP_USEFONT_FOR_LABEL
;
411 if ( m_chkFontDescAsLabel
->GetValue() )
412 style
|= wxFNTP_FONTDESC_AS_LABEL
;
415 #endif // wxUSE_FONTPICKERCTRL
418 wxFAIL_MSG( _T("unknown picker kind") );
424 void PickerWidgetsPage::RecreatePicker(PickerKind picker
)
428 #if wxUSE_COLOURPICKERCTRL
430 m_sizerPicker
->Remove(1);
431 CreatePickers(Picker_Colour
);
432 m_sizerPicker
->Insert(1, m_clrPicker
, 1, wxGROW
|wxALL
, 5);
434 #endif // wxUSE_COLOURPICKERCTRL
436 #if wxUSE_FILEPICKERCTRL
438 m_sizerPicker
->Remove(3);
439 CreatePickers(Picker_File
);
440 m_sizerPicker
->Insert(3, m_filePicker
, 1, wxGROW
|wxALL
, 5);
442 #endif // wxUSE_FILEPICKERCTRL
444 #if wxUSE_DIRPICKERCTRL
446 m_sizerPicker
->Remove(5);
447 CreatePickers(Picker_Dir
);
448 m_sizerPicker
->Insert(5, m_dirPicker
, 1, wxGROW
|wxALL
, 5);
450 #endif // wxUSE_DIRPICKERCTRL
452 #if wxUSE_FONTPICKERCTRL
454 wxASSERT(m_sizerPicker
->Remove(7));
455 CreatePickers(Picker_Font
);
456 m_sizerPicker
->Insert(7, m_fontPicker
, 1, wxGROW
|wxALL
, 5);
458 #endif // wxUSE_FONTPICKERCTRL
461 wxFAIL_MSG( _T("unknown picker kind") );
464 m_sizerPicker
->Layout();
467 void PickerWidgetsPage::RecreateAllPickers()
469 #if wxUSE_COLOURPICKERCTRL
470 RecreatePicker(Picker_Colour
);
471 #endif // wxUSE_COLOURPICKERCTRL
473 #if wxUSE_FILEPICKERCTRL
474 RecreatePicker(Picker_File
);
475 #endif // wxUSE_FILEPICKERCTRL
477 #if wxUSE_DIRPICKERCTRL
478 RecreatePicker(Picker_Dir
);
479 #endif // wxUSE_DIRPICKERCTRL
481 #if wxUSE_FONTPICKERCTRL
482 RecreatePicker(Picker_Font
);
483 #endif // wxUSE_FONTPICKERCTRL
486 void PickerWidgetsPage::Reset()
488 #if wxUSE_COLOURPICKERCTRL
489 m_chkColourTextCtrl
->SetValue((wxCLRP_DEFAULT_STYLE
& wxCLRP_USE_TEXTCTRL
) != 0);
490 m_chkColourShowLabel
->SetValue((wxCLRP_DEFAULT_STYLE
& wxCLRP_SHOW_LABEL
) != 0);
493 #if wxUSE_FILEPICKERCTRL
494 m_chkFileTextCtrl
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_USE_TEXTCTRL
) != 0);
495 m_chkFileOverwritePrompt
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_OVERWRITE_PROMPT
) != 0);
496 m_chkFileMustExist
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_FILE_MUST_EXIST
) != 0);
497 m_chkFileChangeDir
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_CHANGE_DIR
) != 0);
500 #if wxUSE_DIRPICKERCTRL
501 m_chkDirTextCtrl
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_USE_TEXTCTRL
) != 0);
502 m_chkDirMustExist
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_DIR_MUST_EXIST
) != 0);
503 m_chkDirChangeDir
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_CHANGE_DIR
) != 0);
506 #if wxUSE_FONTPICKERCTRL
507 m_chkFontTextCtrl
->SetValue((wxFNTP_DEFAULT_STYLE
& wxFNTP_USE_TEXTCTRL
) != 0);
508 m_chkFontUseFontForLabel
->SetValue((wxFNTP_DEFAULT_STYLE
& wxFNTP_USEFONT_FOR_LABEL
) != 0);
509 m_chkFontDescAsLabel
->SetValue((wxFNTP_DEFAULT_STYLE
& wxFNTP_FONTDESC_AS_LABEL
) != 0);
514 // ----------------------------------------------------------------------------
516 // ----------------------------------------------------------------------------
518 void PickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
522 RecreateAllPickers();
525 #if wxUSE_COLOURPICKERCTRL
526 void PickerWidgetsPage::OnColourChange(wxColourPickerEvent
& event
)
528 wxLogMessage(wxT("The colour changed to '%s' !"),
529 event
.GetColour().GetAsString(wxC2S_CSS_SYNTAX
).c_str());
532 #if wxUSE_FILEPICKERCTRL
533 void PickerWidgetsPage::OnFileChange(wxFileDirPickerEvent
& event
)
535 wxLogMessage(wxT("The file changed to '%s' ! The current working directory is '%s'"),
536 event
.GetPath().c_str(), wxGetCwd().c_str());
539 #if wxUSE_DIRPICKERCTRL
540 void PickerWidgetsPage::OnDirChange(wxFileDirPickerEvent
& event
)
542 wxLogMessage(wxT("The directory changed to '%s' ! The current working directory is '%s'"),
543 event
.GetPath().c_str(), wxGetCwd().c_str());
546 #if wxUSE_FONTPICKERCTRL
547 void PickerWidgetsPage::OnFontChange(wxFontPickerEvent
& event
)
549 wxLogMessage(wxT("The font changed to '%s' with size %d !"),
550 event
.GetFont().GetFaceName().c_str(), event
.GetFont().GetPointSize());
554 void PickerWidgetsPage::OnCheckBox(wxCommandEvent
&event
)
556 if (event
.GetEventObject() == m_chkColourTextCtrl
||
557 event
.GetEventObject() == m_chkColourShowLabel
)
558 RecreatePicker(Picker_Colour
);
560 if (event
.GetEventObject() == m_chkFileTextCtrl
||
561 event
.GetEventObject() == m_chkFileOverwritePrompt
||
562 event
.GetEventObject() == m_chkFileMustExist
||
563 event
.GetEventObject() == m_chkFileChangeDir
)
564 RecreatePicker(Picker_File
);
566 if (event
.GetEventObject() == m_chkDirTextCtrl
||
567 event
.GetEventObject() == m_chkDirChangeDir
||
568 event
.GetEventObject() == m_chkDirMustExist
)
569 RecreatePicker(Picker_Dir
);
571 if (event
.GetEventObject() == m_chkFontTextCtrl
||
572 event
.GetEventObject() == m_chkFontDescAsLabel
||
573 event
.GetEventObject() == m_chkFontUseFontForLabel
)
574 RecreatePicker(Picker_Font
);
577 #endif // wxUSE_COLOURPICKERCTRL || wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL || wxUSE_FONTPICKERCTRL