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
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/clrpicker.h"
43 #include "wx/filepicker.h"
44 #include "wx/fontpicker.h"
48 #include "icons/picker.xpm"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
56 FilePickerMode_Open
= 0,
63 PickerPage_Reset
= wxID_HIGHEST
,
65 #if wxUSE_COLOURPICKERCTRL
68 #if wxUSE_FILEPICKERCTRL
71 #if wxUSE_DIRPICKERCTRL
74 #if wxUSE_FONTPICKERCTRL
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 class PickerWidgetsPage
: public WidgetsPage
87 PickerWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
88 virtual ~PickerWidgetsPage(){};
90 virtual wxControl
*GetWidget() const { return m_filePicker
; }
91 virtual void RecreateWidget() { RecreateAllPickers(); }
103 // called only once at first construction
104 void CreatePickers(PickerKind picker
);
106 // called to recreate an existing control
107 void RecreatePicker(PickerKind picker
);
109 // recreate all existing picker controls
110 void RecreateAllPickers();
112 // restore the checkboxes state to the initial values
115 // get the initial style for the picker of the given kind
116 long GetPickerStyle(PickerKind kind
);
118 // update filepicker radiobox
119 void UpdateFilePickerMode();
121 // the pickers and the relative event handlers
122 #if wxUSE_COLOURPICKERCTRL
123 wxColourPickerCtrl
*m_clrPicker
;
124 void OnColourChange(wxColourPickerEvent
&ev
);
126 #if wxUSE_FILEPICKERCTRL
127 wxFilePickerCtrl
*m_filePicker
;
128 void OnFileChange(wxFileDirPickerEvent
&ev
);
130 #if wxUSE_DIRPICKERCTRL
131 wxDirPickerCtrl
*m_dirPicker
;
132 void OnDirChange(wxFileDirPickerEvent
&ev
);
134 #if wxUSE_FONTPICKERCTRL
135 wxFontPickerCtrl
*m_fontPicker
;
136 void OnFontChange(wxFontPickerEvent
&ev
);
138 void OnCheckBox(wxCommandEvent
&ev
);
139 void OnButtonReset(wxCommandEvent
&ev
);
145 wxCheckBox
*m_chkColourTextCtrl
,
146 *m_chkColourShowLabel
;
148 wxCheckBox
*m_chkFileTextCtrl
,
149 *m_chkFileOverwritePrompt
,
152 wxRadioBox
*m_radioFilePickerMode
;
154 wxCheckBox
*m_chkDirTextCtrl
,
158 wxCheckBox
*m_chkFontTextCtrl
,
159 *m_chkFontDescAsLabel
,
160 *m_chkFontUseFontForLabel
;
162 wxFlexGridSizer
*m_sizerPicker
;
165 DECLARE_EVENT_TABLE()
166 DECLARE_WIDGETS_PAGE(PickerWidgetsPage
)
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 BEGIN_EVENT_TABLE(PickerWidgetsPage
, WidgetsPage
)
174 EVT_BUTTON(PickerPage_Reset
, PickerWidgetsPage::OnButtonReset
)
176 #if wxUSE_COLOURPICKERCTRL
177 EVT_COLOURPICKER_CHANGED(PickerPage_Colour
, PickerWidgetsPage::OnColourChange
)
179 #if wxUSE_FILEPICKERCTRL
180 EVT_FILEPICKER_CHANGED(PickerPage_File
, PickerWidgetsPage::OnFileChange
)
182 #if wxUSE_DIRPICKERCTRL
183 EVT_DIRPICKER_CHANGED(PickerPage_Dir
, PickerWidgetsPage::OnDirChange
)
185 #if wxUSE_FONTPICKERCTRL
186 EVT_FONTPICKER_CHANGED(PickerPage_Font
, PickerWidgetsPage::OnFontChange
)
189 EVT_CHECKBOX(wxID_ANY
, PickerWidgetsPage::OnCheckBox
)
190 EVT_RADIOBOX(wxID_ANY
, PickerWidgetsPage::OnCheckBox
)
193 // ============================================================================
195 // ============================================================================
197 #if defined(__WXGTK24__)
198 #define FAMILY_CTRLS NATIVE_CTRLS
200 #define FAMILY_CTRLS GENERIC_CTRLS
203 IMPLEMENT_WIDGETS_PAGE(PickerWidgetsPage
, _T("Pickers"),
204 PICKER_CTRLS
| FAMILY_CTRLS
);
206 PickerWidgetsPage::PickerWidgetsPage(WidgetsBookCtrl
*book
,
207 wxImageList
*imaglist
)
208 : WidgetsPage(book
, imaglist
, picker_xpm
)
211 wxSizer
*boxleft
= new wxBoxSizer(wxVERTICAL
);
213 #if wxUSE_COLOURPICKERCTRL
214 wxStaticBoxSizer
*clrbox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&ColourPicker style"));
215 m_chkColourTextCtrl
= CreateCheckBoxAndAddToSizer(clrbox
, _T("With textctrl"), false);
216 m_chkColourShowLabel
= CreateCheckBoxAndAddToSizer(clrbox
, _T("With label"), false);
217 boxleft
->Add(clrbox
, 0, wxALL
|wxGROW
, 5);
218 boxleft
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
219 #endif // wxUSE_COLOURPICKERCTRL
221 #if wxUSE_FILEPICKERCTRL
222 static const wxString mode
[] = { _T("open"), _T("save") };
223 m_radioFilePickerMode
= new wxRadioBox(this, wxID_ANY
, _T("wxFilePicker mode"),
224 wxDefaultPosition
, wxDefaultSize
,
225 WXSIZEOF(mode
), mode
);
226 boxleft
->Add(m_radioFilePickerMode
, 0, wxALL
|wxGROW
, 5);
228 wxStaticBoxSizer
*filebox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&FilePicker style"));
229 m_chkFileTextCtrl
= CreateCheckBoxAndAddToSizer(filebox
, _T("With textctrl"), false);
230 m_chkFileOverwritePrompt
= CreateCheckBoxAndAddToSizer(filebox
, _T("Overwrite prompt"), false);
231 m_chkFileMustExist
= CreateCheckBoxAndAddToSizer(filebox
, _T("File must exist"), false);
232 m_chkFileChangeDir
= CreateCheckBoxAndAddToSizer(filebox
, _T("Change working dir"), false);
233 boxleft
->Add(filebox
, 0, wxALL
|wxGROW
, 5);
234 boxleft
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
235 #endif // wxUSE_FILEPICKERCTRL
237 #if wxUSE_DIRPICKERCTRL
238 wxStaticBoxSizer
*dirbox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&DirPicker style"));
239 m_chkDirTextCtrl
= CreateCheckBoxAndAddToSizer(dirbox
, _T("With textctrl"), false);
240 m_chkDirMustExist
= CreateCheckBoxAndAddToSizer(dirbox
, _T("Dir must exist"), false);
241 m_chkDirChangeDir
= CreateCheckBoxAndAddToSizer(dirbox
, _T("Change working dir"), false);
242 boxleft
->Add(dirbox
, 0, wxALL
|wxGROW
, 5);
243 boxleft
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
244 #endif // wxUSE_DIRPICKERCTRL
246 #if wxUSE_FONTPICKERCTRL
247 wxStaticBoxSizer
*fontbox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&FontPicker style"));
248 m_chkFontTextCtrl
= CreateCheckBoxAndAddToSizer(fontbox
, _T("With textctrl"));
249 m_chkFontDescAsLabel
= CreateCheckBoxAndAddToSizer(fontbox
, _T("Font desc as btn label"));
250 m_chkFontUseFontForLabel
= CreateCheckBoxAndAddToSizer(fontbox
, _T("Use font for label"), false);
251 boxleft
->Add(fontbox
, 0, wxALL
|wxGROW
, 5);
252 #endif // wxUSE_FONTPICKERCTRL
254 boxleft
->Add(new wxButton(this, PickerPage_Reset
, _T("&Reset")),
255 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
257 Reset(); // set checkboxes state
266 #if wxUSE_COLOURPICKERCTRL
267 CreatePickers(Picker_Colour
);
269 #endif // wxUSE_COLOURPICKERCTRL
271 #if wxUSE_FILEPICKERCTRL
272 CreatePickers(Picker_File
);
274 #endif // wxUSE_FILEPICKERCTRL
276 #if wxUSE_DIRPICKERCTRL
277 CreatePickers(Picker_Dir
);
279 #endif // wxUSE_DIRPICKERCTRL
281 #if wxUSE_FONTPICKERCTRL
282 CreatePickers(Picker_Font
);
284 #endif // wxUSE_FONTPICKERCTRL
286 m_sizerPicker
= new wxFlexGridSizer(nrows
, 2, 0, 0); // 4 rows x 2 columns
287 #if wxUSE_COLOURPICKERCTRL
288 m_sizerPicker
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxColourPickerCtrl:")),
289 0, wxALIGN_CENTER_VERTICAL
| wxALIGN_RIGHT
| wxALL
, 5);
290 m_sizerPicker
->Add(m_clrPicker
, 1, wxGROW
| wxALL
, 5);
292 #if wxUSE_FILEPICKERCTRL
293 m_sizerPicker
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxFilePickerCtrl:")),
294 0, wxALIGN_CENTER_VERTICAL
| wxALIGN_RIGHT
| wxALL
, 5);
295 m_sizerPicker
->Add(m_filePicker
, 1, wxGROW
| wxALL
, 5);
297 #if wxUSE_DIRPICKERCTRL
298 m_sizerPicker
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxDirPickerCtrl:")),
299 0, wxALIGN_CENTER_VERTICAL
| wxALIGN_RIGHT
| wxALL
, 5);
300 m_sizerPicker
->Add(m_dirPicker
, 1, wxGROW
| wxALL
, 5);
302 #if wxUSE_FONTPICKERCTRL
303 m_sizerPicker
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxFontPickerCtrl:")),
304 0, wxALIGN_CENTER_VERTICAL
| wxALIGN_RIGHT
| wxALL
, 5);
305 m_sizerPicker
->Add(m_fontPicker
, 1, wxGROW
| wxALL
, 5);
308 m_sizerPicker
->AddGrowableCol(1, 3);
312 boxright
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("Pickers"));
313 boxright
->Add(m_sizerPicker
, 1, wxGROW
| wxALL
, 5);
316 wxSizer
*sz
= new wxBoxSizer(wxHORIZONTAL
);
317 sz
->Add(boxleft
, 0, wxGROW
|wxALL
, 5);
318 sz
->Add(boxright
, 1, wxGROW
|wxALL
, 5);
323 void PickerWidgetsPage::CreatePickers(PickerKind picker
)
327 #if wxUSE_COLOURPICKERCTRL
331 m_clrPicker
= new wxColourPickerCtrl(this, PickerPage_Colour
, *wxRED
,
332 wxDefaultPosition
, wxDefaultSize
,
333 GetPickerStyle(Picker_Colour
));
335 #endif // wxUSE_COLOURPICKERCTRL
337 #if wxUSE_FILEPICKERCTRL
341 // pass an empty string as initial file
342 m_filePicker
= new wxFilePickerCtrl(this, PickerPage_File
,
344 wxT("Hello!"), wxT("*"),
345 wxDefaultPosition
, wxDefaultSize
,
346 GetPickerStyle(Picker_File
));
348 #endif // wxUSE_FILEPICKERCTRL
350 #if wxUSE_DIRPICKERCTRL
354 m_dirPicker
= new wxDirPickerCtrl(this, PickerPage_Dir
,
355 wxGetHomeDir(), wxT("Hello!"),
356 wxDefaultPosition
, wxDefaultSize
,
357 GetPickerStyle(Picker_Dir
));
359 #endif // wxUSE_DIRPICKERCTRL
361 #if wxUSE_FONTPICKERCTRL
365 m_fontPicker
= new wxFontPickerCtrl(this, PickerPage_Font
,
367 wxDefaultPosition
, wxDefaultSize
,
368 GetPickerStyle(Picker_Font
));
370 #endif // wxUSE_FONTPICKERCTRL
373 wxFAIL_MSG( _T("unknown picker kind") );
377 long PickerWidgetsPage::GetPickerStyle(PickerKind picker
)
383 #if wxUSE_COLOURPICKERCTRL
385 if ( m_chkColourTextCtrl
->GetValue() )
386 style
|= wxCLRP_USE_TEXTCTRL
;
388 if ( m_chkColourShowLabel
->GetValue() )
389 style
|= wxCLRP_SHOW_LABEL
;
392 #endif // wxUSE_COLOURPICKERCTRL
394 #if wxUSE_FILEPICKERCTRL
396 if ( m_chkFileTextCtrl
->GetValue() )
397 style
|= wxFLP_USE_TEXTCTRL
;
399 if ( m_chkFileOverwritePrompt
->GetValue() )
400 style
|= wxFLP_OVERWRITE_PROMPT
;
402 if ( m_chkFileMustExist
->GetValue() )
403 style
|= wxFLP_FILE_MUST_EXIST
;
405 if ( m_chkFileChangeDir
->GetValue() )
406 style
|= wxFLP_CHANGE_DIR
;
408 if (m_radioFilePickerMode
->GetSelection() == FilePickerMode_Open
)
414 #endif // wxUSE_FILEPICKERCTRL
416 #if wxUSE_DIRPICKERCTRL
418 if ( m_chkDirTextCtrl
->GetValue() )
419 style
|= wxDIRP_USE_TEXTCTRL
;
421 if ( m_chkDirMustExist
->GetValue() )
422 style
|= wxDIRP_DIR_MUST_EXIST
;
424 if ( m_chkDirChangeDir
->GetValue() )
425 style
|= wxDIRP_CHANGE_DIR
;
428 #endif // wxUSE_DIRPICKERCTRL
430 #if wxUSE_FONTPICKERCTRL
432 if ( m_chkFontTextCtrl
->GetValue() )
433 style
|= wxFNTP_USE_TEXTCTRL
;
435 if ( m_chkFontUseFontForLabel
->GetValue() )
436 style
|= wxFNTP_USEFONT_FOR_LABEL
;
438 if ( m_chkFontDescAsLabel
->GetValue() )
439 style
|= wxFNTP_FONTDESC_AS_LABEL
;
442 #endif // wxUSE_FONTPICKERCTRL
445 wxFAIL_MSG( _T("unknown picker kind") );
451 void PickerWidgetsPage::RecreatePicker(PickerKind picker
)
455 #if wxUSE_COLOURPICKERCTRL
457 m_sizerPicker
->Remove(1);
458 CreatePickers(Picker_Colour
);
459 m_sizerPicker
->Insert(1, m_clrPicker
, 1, wxGROW
|wxALL
, 5);
461 #endif // wxUSE_COLOURPICKERCTRL
463 #if wxUSE_FILEPICKERCTRL
465 m_sizerPicker
->Remove(3);
466 CreatePickers(Picker_File
);
467 m_sizerPicker
->Insert(3, m_filePicker
, 1, wxGROW
|wxALL
, 5);
469 #endif // wxUSE_FILEPICKERCTRL
471 #if wxUSE_DIRPICKERCTRL
473 m_sizerPicker
->Remove(5);
474 CreatePickers(Picker_Dir
);
475 m_sizerPicker
->Insert(5, m_dirPicker
, 1, wxGROW
|wxALL
, 5);
477 #endif // wxUSE_DIRPICKERCTRL
479 #if wxUSE_FONTPICKERCTRL
481 wxASSERT(m_sizerPicker
->Remove(7));
482 CreatePickers(Picker_Font
);
483 m_sizerPicker
->Insert(7, m_fontPicker
, 1, wxGROW
|wxALL
, 5);
485 #endif // wxUSE_FONTPICKERCTRL
488 wxFAIL_MSG( _T("unknown picker kind") );
491 m_sizerPicker
->Layout();
494 void PickerWidgetsPage::RecreateAllPickers()
496 #if wxUSE_COLOURPICKERCTRL
497 RecreatePicker(Picker_Colour
);
498 #endif // wxUSE_COLOURPICKERCTRL
500 #if wxUSE_FILEPICKERCTRL
501 RecreatePicker(Picker_File
);
502 #endif // wxUSE_FILEPICKERCTRL
504 #if wxUSE_DIRPICKERCTRL
505 RecreatePicker(Picker_Dir
);
506 #endif // wxUSE_DIRPICKERCTRL
508 #if wxUSE_FONTPICKERCTRL
509 RecreatePicker(Picker_Font
);
510 #endif // wxUSE_FONTPICKERCTRL
513 void PickerWidgetsPage::Reset()
515 #if wxUSE_COLOURPICKERCTRL
516 m_chkColourTextCtrl
->SetValue((wxCLRP_DEFAULT_STYLE
& wxCLRP_USE_TEXTCTRL
) != 0);
517 m_chkColourShowLabel
->SetValue((wxCLRP_DEFAULT_STYLE
& wxCLRP_SHOW_LABEL
) != 0);
520 #if wxUSE_FILEPICKERCTRL
521 m_radioFilePickerMode
->SetSelection((wxFLP_DEFAULT_STYLE
& wxFLP_OPEN
) ?
522 FilePickerMode_Open
: FilePickerMode_Save
);
523 m_chkFileTextCtrl
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_USE_TEXTCTRL
) != 0);
524 m_chkFileOverwritePrompt
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_OVERWRITE_PROMPT
) != 0);
525 m_chkFileMustExist
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_FILE_MUST_EXIST
) != 0);
526 m_chkFileChangeDir
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_CHANGE_DIR
) != 0);
528 UpdateFilePickerMode();
531 #if wxUSE_DIRPICKERCTRL
532 m_chkDirTextCtrl
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_USE_TEXTCTRL
) != 0);
533 m_chkDirMustExist
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_DIR_MUST_EXIST
) != 0);
534 m_chkDirChangeDir
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_CHANGE_DIR
) != 0);
537 #if wxUSE_FONTPICKERCTRL
538 m_chkFontTextCtrl
->SetValue((wxFNTP_DEFAULT_STYLE
& wxFNTP_USE_TEXTCTRL
) != 0);
539 m_chkFontUseFontForLabel
->SetValue((wxFNTP_DEFAULT_STYLE
& wxFNTP_USEFONT_FOR_LABEL
) != 0);
540 m_chkFontDescAsLabel
->SetValue((wxFNTP_DEFAULT_STYLE
& wxFNTP_FONTDESC_AS_LABEL
) != 0);
544 void PickerWidgetsPage::UpdateFilePickerMode()
546 switch (m_radioFilePickerMode
->GetSelection())
548 case FilePickerMode_Open
:
549 m_chkFileOverwritePrompt
->SetValue(false);
550 m_chkFileOverwritePrompt
->Disable();
551 m_chkFileMustExist
->Enable();
553 case FilePickerMode_Save
:
554 m_chkFileMustExist
->SetValue(false);
555 m_chkFileMustExist
->Disable();
556 m_chkFileOverwritePrompt
->Enable();
562 // ----------------------------------------------------------------------------
564 // ----------------------------------------------------------------------------
566 void PickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
570 RecreateAllPickers();
573 #if wxUSE_COLOURPICKERCTRL
574 void PickerWidgetsPage::OnColourChange(wxColourPickerEvent
& event
)
576 wxLogMessage(wxT("The colour changed to '%s' !"),
577 event
.GetColour().GetAsString(wxC2S_CSS_SYNTAX
).c_str());
580 #if wxUSE_FILEPICKERCTRL
581 void PickerWidgetsPage::OnFileChange(wxFileDirPickerEvent
& event
)
583 wxLogMessage(wxT("The file changed to '%s' ! The current working directory is '%s'"),
584 event
.GetPath().c_str(), wxGetCwd().c_str());
587 #if wxUSE_DIRPICKERCTRL
588 void PickerWidgetsPage::OnDirChange(wxFileDirPickerEvent
& event
)
590 wxLogMessage(wxT("The directory changed to '%s' ! The current working directory is '%s'"),
591 event
.GetPath().c_str(), wxGetCwd().c_str());
594 #if wxUSE_FONTPICKERCTRL
595 void PickerWidgetsPage::OnFontChange(wxFontPickerEvent
& event
)
597 wxLogMessage(wxT("The font changed to '%s' with size %d !"),
598 event
.GetFont().GetFaceName().c_str(), event
.GetFont().GetPointSize());
602 void PickerWidgetsPage::OnCheckBox(wxCommandEvent
&event
)
604 if (event
.GetEventObject() == m_chkColourTextCtrl
||
605 event
.GetEventObject() == m_chkColourShowLabel
)
606 RecreatePicker(Picker_Colour
);
608 if (event
.GetEventObject() == m_chkFileTextCtrl
||
609 event
.GetEventObject() == m_chkFileOverwritePrompt
||
610 event
.GetEventObject() == m_chkFileMustExist
||
611 event
.GetEventObject() == m_chkFileChangeDir
)
612 RecreatePicker(Picker_File
);
614 if (event
.GetEventObject() == m_chkDirTextCtrl
||
615 event
.GetEventObject() == m_chkDirChangeDir
||
616 event
.GetEventObject() == m_chkDirMustExist
)
617 RecreatePicker(Picker_Dir
);
619 if (event
.GetEventObject() == m_chkFontTextCtrl
||
620 event
.GetEventObject() == m_chkFontDescAsLabel
||
621 event
.GetEventObject() == m_chkFontUseFontForLabel
)
622 RecreatePicker(Picker_Font
);
624 if (event
.GetEventObject() == m_radioFilePickerMode
)
626 UpdateFilePickerMode();
627 RecreatePicker(Picker_File
);
631 #endif // wxUSE_COLOURPICKERCTRL || wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL || wxUSE_FONTPICKERCTRL