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(); }
93 // lazy creation of the content
94 virtual void CreateContent();
106 // called only once at first construction
107 void CreatePickers(PickerKind picker
);
109 // called to recreate an existing control
110 void RecreatePicker(PickerKind picker
);
112 // recreate all existing picker controls
113 void RecreateAllPickers();
115 // restore the checkboxes state to the initial values
118 // get the initial style for the picker of the given kind
119 long GetPickerStyle(PickerKind kind
);
121 // update filepicker radiobox
122 void UpdateFilePickerMode();
124 // the pickers and the relative event handlers
125 #if wxUSE_COLOURPICKERCTRL
126 wxColourPickerCtrl
*m_clrPicker
;
127 void OnColourChange(wxColourPickerEvent
&ev
);
129 #if wxUSE_FILEPICKERCTRL
130 wxFilePickerCtrl
*m_filePicker
;
131 void OnFileChange(wxFileDirPickerEvent
&ev
);
133 #if wxUSE_DIRPICKERCTRL
134 wxDirPickerCtrl
*m_dirPicker
;
135 void OnDirChange(wxFileDirPickerEvent
&ev
);
137 #if wxUSE_FONTPICKERCTRL
138 wxFontPickerCtrl
*m_fontPicker
;
139 void OnFontChange(wxFontPickerEvent
&ev
);
141 void OnCheckBox(wxCommandEvent
&ev
);
142 void OnButtonReset(wxCommandEvent
&ev
);
148 wxCheckBox
*m_chkColourTextCtrl
,
149 *m_chkColourShowLabel
;
151 wxCheckBox
*m_chkFileTextCtrl
,
152 *m_chkFileOverwritePrompt
,
155 wxRadioBox
*m_radioFilePickerMode
;
157 wxCheckBox
*m_chkDirTextCtrl
,
161 wxCheckBox
*m_chkFontTextCtrl
,
162 *m_chkFontDescAsLabel
,
163 *m_chkFontUseFontForLabel
;
165 wxFlexGridSizer
*m_sizerPicker
;
168 DECLARE_EVENT_TABLE()
169 DECLARE_WIDGETS_PAGE(PickerWidgetsPage
)
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 BEGIN_EVENT_TABLE(PickerWidgetsPage
, WidgetsPage
)
177 EVT_BUTTON(PickerPage_Reset
, PickerWidgetsPage::OnButtonReset
)
179 #if wxUSE_COLOURPICKERCTRL
180 EVT_COLOURPICKER_CHANGED(PickerPage_Colour
, PickerWidgetsPage::OnColourChange
)
182 #if wxUSE_FILEPICKERCTRL
183 EVT_FILEPICKER_CHANGED(PickerPage_File
, PickerWidgetsPage::OnFileChange
)
185 #if wxUSE_DIRPICKERCTRL
186 EVT_DIRPICKER_CHANGED(PickerPage_Dir
, PickerWidgetsPage::OnDirChange
)
188 #if wxUSE_FONTPICKERCTRL
189 EVT_FONTPICKER_CHANGED(PickerPage_Font
, PickerWidgetsPage::OnFontChange
)
192 EVT_CHECKBOX(wxID_ANY
, PickerWidgetsPage::OnCheckBox
)
193 EVT_RADIOBOX(wxID_ANY
, PickerWidgetsPage::OnCheckBox
)
196 // ============================================================================
198 // ============================================================================
200 #if defined(__WXGTK24__)
201 #define FAMILY_CTRLS NATIVE_CTRLS
203 #define FAMILY_CTRLS GENERIC_CTRLS
206 IMPLEMENT_WIDGETS_PAGE(PickerWidgetsPage
, _T("Pickers"),
207 PICKER_CTRLS
| FAMILY_CTRLS
);
209 PickerWidgetsPage::PickerWidgetsPage(WidgetsBookCtrl
*book
,
210 wxImageList
*imaglist
)
211 : WidgetsPage(book
, imaglist
, picker_xpm
)
215 void PickerWidgetsPage::CreateContent()
218 wxSizer
*boxleft
= new wxBoxSizer(wxVERTICAL
);
220 #if wxUSE_COLOURPICKERCTRL
221 wxStaticBoxSizer
*clrbox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&ColourPicker style"));
222 m_chkColourTextCtrl
= CreateCheckBoxAndAddToSizer(clrbox
, _T("With textctrl"), false);
223 m_chkColourShowLabel
= CreateCheckBoxAndAddToSizer(clrbox
, _T("With label"), false);
224 boxleft
->Add(clrbox
, 0, wxALL
|wxGROW
, 5);
225 boxleft
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
226 #endif // wxUSE_COLOURPICKERCTRL
228 #if wxUSE_FILEPICKERCTRL
229 static const wxString mode
[] = { _T("open"), _T("save") };
230 m_radioFilePickerMode
= new wxRadioBox(this, wxID_ANY
, _T("wxFilePicker mode"),
231 wxDefaultPosition
, wxDefaultSize
,
232 WXSIZEOF(mode
), mode
);
233 boxleft
->Add(m_radioFilePickerMode
, 0, wxALL
|wxGROW
, 5);
235 wxStaticBoxSizer
*filebox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&FilePicker style"));
236 m_chkFileTextCtrl
= CreateCheckBoxAndAddToSizer(filebox
, _T("With textctrl"), false);
237 m_chkFileOverwritePrompt
= CreateCheckBoxAndAddToSizer(filebox
, _T("Overwrite prompt"), false);
238 m_chkFileMustExist
= CreateCheckBoxAndAddToSizer(filebox
, _T("File must exist"), false);
239 m_chkFileChangeDir
= CreateCheckBoxAndAddToSizer(filebox
, _T("Change working dir"), false);
240 boxleft
->Add(filebox
, 0, wxALL
|wxGROW
, 5);
241 boxleft
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
242 #endif // wxUSE_FILEPICKERCTRL
244 #if wxUSE_DIRPICKERCTRL
245 wxStaticBoxSizer
*dirbox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&DirPicker style"));
246 m_chkDirTextCtrl
= CreateCheckBoxAndAddToSizer(dirbox
, _T("With textctrl"), false);
247 m_chkDirMustExist
= CreateCheckBoxAndAddToSizer(dirbox
, _T("Dir must exist"), false);
248 m_chkDirChangeDir
= CreateCheckBoxAndAddToSizer(dirbox
, _T("Change working dir"), false);
249 boxleft
->Add(dirbox
, 0, wxALL
|wxGROW
, 5);
250 boxleft
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
251 #endif // wxUSE_DIRPICKERCTRL
253 #if wxUSE_FONTPICKERCTRL
254 wxStaticBoxSizer
*fontbox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&FontPicker style"));
255 m_chkFontTextCtrl
= CreateCheckBoxAndAddToSizer(fontbox
, _T("With textctrl"));
256 m_chkFontDescAsLabel
= CreateCheckBoxAndAddToSizer(fontbox
, _T("Font desc as btn label"));
257 m_chkFontUseFontForLabel
= CreateCheckBoxAndAddToSizer(fontbox
, _T("Use font for label"), false);
258 boxleft
->Add(fontbox
, 0, wxALL
|wxGROW
, 5);
259 #endif // wxUSE_FONTPICKERCTRL
261 boxleft
->Add(new wxButton(this, PickerPage_Reset
, _T("&Reset")),
262 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
264 Reset(); // set checkboxes state
273 #if wxUSE_COLOURPICKERCTRL
274 CreatePickers(Picker_Colour
);
276 #endif // wxUSE_COLOURPICKERCTRL
278 #if wxUSE_FILEPICKERCTRL
279 CreatePickers(Picker_File
);
281 #endif // wxUSE_FILEPICKERCTRL
283 #if wxUSE_DIRPICKERCTRL
284 CreatePickers(Picker_Dir
);
286 #endif // wxUSE_DIRPICKERCTRL
288 #if wxUSE_FONTPICKERCTRL
289 CreatePickers(Picker_Font
);
291 #endif // wxUSE_FONTPICKERCTRL
293 m_sizerPicker
= new wxFlexGridSizer(nrows
, 2, 0, 0); // 4 rows x 2 columns
294 #if wxUSE_COLOURPICKERCTRL
295 m_sizerPicker
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxColourPickerCtrl:")),
296 0, wxALIGN_CENTER_VERTICAL
| wxALIGN_RIGHT
| wxALL
, 5);
297 m_sizerPicker
->Add(m_clrPicker
, 1, wxGROW
| wxALL
, 5);
299 #if wxUSE_FILEPICKERCTRL
300 m_sizerPicker
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxFilePickerCtrl:")),
301 0, wxALIGN_CENTER_VERTICAL
| wxALIGN_RIGHT
| wxALL
, 5);
302 m_sizerPicker
->Add(m_filePicker
, 1, wxGROW
| wxALL
, 5);
304 #if wxUSE_DIRPICKERCTRL
305 m_sizerPicker
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxDirPickerCtrl:")),
306 0, wxALIGN_CENTER_VERTICAL
| wxALIGN_RIGHT
| wxALL
, 5);
307 m_sizerPicker
->Add(m_dirPicker
, 1, wxGROW
| wxALL
, 5);
309 #if wxUSE_FONTPICKERCTRL
310 m_sizerPicker
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxFontPickerCtrl:")),
311 0, wxALIGN_CENTER_VERTICAL
| wxALIGN_RIGHT
| wxALL
, 5);
312 m_sizerPicker
->Add(m_fontPicker
, 1, wxGROW
| wxALL
, 5);
315 m_sizerPicker
->AddGrowableCol(1, 3);
319 boxright
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("Pickers"));
320 boxright
->Add(m_sizerPicker
, 1, wxGROW
| wxALL
, 5);
323 wxSizer
*sz
= new wxBoxSizer(wxHORIZONTAL
);
324 sz
->Add(boxleft
, 0, wxGROW
|wxALL
, 5);
325 sz
->Add(boxright
, 1, wxGROW
|wxALL
, 5);
330 void PickerWidgetsPage::CreatePickers(PickerKind picker
)
334 #if wxUSE_COLOURPICKERCTRL
338 m_clrPicker
= new wxColourPickerCtrl(this, PickerPage_Colour
, *wxRED
,
339 wxDefaultPosition
, wxDefaultSize
,
340 GetPickerStyle(Picker_Colour
));
342 #endif // wxUSE_COLOURPICKERCTRL
344 #if wxUSE_FILEPICKERCTRL
348 // pass an empty string as initial file
349 m_filePicker
= new wxFilePickerCtrl(this, PickerPage_File
,
351 wxT("Hello!"), wxT("*"),
352 wxDefaultPosition
, wxDefaultSize
,
353 GetPickerStyle(Picker_File
));
355 #endif // wxUSE_FILEPICKERCTRL
357 #if wxUSE_DIRPICKERCTRL
361 m_dirPicker
= new wxDirPickerCtrl(this, PickerPage_Dir
,
362 wxGetHomeDir(), wxT("Hello!"),
363 wxDefaultPosition
, wxDefaultSize
,
364 GetPickerStyle(Picker_Dir
));
366 #endif // wxUSE_DIRPICKERCTRL
368 #if wxUSE_FONTPICKERCTRL
372 m_fontPicker
= new wxFontPickerCtrl(this, PickerPage_Font
,
374 wxDefaultPosition
, wxDefaultSize
,
375 GetPickerStyle(Picker_Font
));
377 #endif // wxUSE_FONTPICKERCTRL
380 wxFAIL_MSG( _T("unknown picker kind") );
384 long PickerWidgetsPage::GetPickerStyle(PickerKind picker
)
390 #if wxUSE_COLOURPICKERCTRL
392 if ( m_chkColourTextCtrl
->GetValue() )
393 style
|= wxCLRP_USE_TEXTCTRL
;
395 if ( m_chkColourShowLabel
->GetValue() )
396 style
|= wxCLRP_SHOW_LABEL
;
399 #endif // wxUSE_COLOURPICKERCTRL
401 #if wxUSE_FILEPICKERCTRL
403 if ( m_chkFileTextCtrl
->GetValue() )
404 style
|= wxFLP_USE_TEXTCTRL
;
406 if ( m_chkFileOverwritePrompt
->GetValue() )
407 style
|= wxFLP_OVERWRITE_PROMPT
;
409 if ( m_chkFileMustExist
->GetValue() )
410 style
|= wxFLP_FILE_MUST_EXIST
;
412 if ( m_chkFileChangeDir
->GetValue() )
413 style
|= wxFLP_CHANGE_DIR
;
415 if (m_radioFilePickerMode
->GetSelection() == FilePickerMode_Open
)
421 #endif // wxUSE_FILEPICKERCTRL
423 #if wxUSE_DIRPICKERCTRL
425 if ( m_chkDirTextCtrl
->GetValue() )
426 style
|= wxDIRP_USE_TEXTCTRL
;
428 if ( m_chkDirMustExist
->GetValue() )
429 style
|= wxDIRP_DIR_MUST_EXIST
;
431 if ( m_chkDirChangeDir
->GetValue() )
432 style
|= wxDIRP_CHANGE_DIR
;
435 #endif // wxUSE_DIRPICKERCTRL
437 #if wxUSE_FONTPICKERCTRL
439 if ( m_chkFontTextCtrl
->GetValue() )
440 style
|= wxFNTP_USE_TEXTCTRL
;
442 if ( m_chkFontUseFontForLabel
->GetValue() )
443 style
|= wxFNTP_USEFONT_FOR_LABEL
;
445 if ( m_chkFontDescAsLabel
->GetValue() )
446 style
|= wxFNTP_FONTDESC_AS_LABEL
;
449 #endif // wxUSE_FONTPICKERCTRL
452 wxFAIL_MSG( _T("unknown picker kind") );
458 void PickerWidgetsPage::RecreatePicker(PickerKind picker
)
462 #if wxUSE_COLOURPICKERCTRL
464 m_sizerPicker
->Remove(1);
465 CreatePickers(Picker_Colour
);
466 m_sizerPicker
->Insert(1, m_clrPicker
, 1, wxGROW
|wxALL
, 5);
468 #endif // wxUSE_COLOURPICKERCTRL
470 #if wxUSE_FILEPICKERCTRL
472 m_sizerPicker
->Remove(3);
473 CreatePickers(Picker_File
);
474 m_sizerPicker
->Insert(3, m_filePicker
, 1, wxGROW
|wxALL
, 5);
476 #endif // wxUSE_FILEPICKERCTRL
478 #if wxUSE_DIRPICKERCTRL
480 m_sizerPicker
->Remove(5);
481 CreatePickers(Picker_Dir
);
482 m_sizerPicker
->Insert(5, m_dirPicker
, 1, wxGROW
|wxALL
, 5);
484 #endif // wxUSE_DIRPICKERCTRL
486 #if wxUSE_FONTPICKERCTRL
488 wxASSERT(m_sizerPicker
->Remove(7));
489 CreatePickers(Picker_Font
);
490 m_sizerPicker
->Insert(7, m_fontPicker
, 1, wxGROW
|wxALL
, 5);
492 #endif // wxUSE_FONTPICKERCTRL
495 wxFAIL_MSG( _T("unknown picker kind") );
498 m_sizerPicker
->Layout();
501 void PickerWidgetsPage::RecreateAllPickers()
503 #if wxUSE_COLOURPICKERCTRL
504 RecreatePicker(Picker_Colour
);
505 #endif // wxUSE_COLOURPICKERCTRL
507 #if wxUSE_FILEPICKERCTRL
508 RecreatePicker(Picker_File
);
509 #endif // wxUSE_FILEPICKERCTRL
511 #if wxUSE_DIRPICKERCTRL
512 RecreatePicker(Picker_Dir
);
513 #endif // wxUSE_DIRPICKERCTRL
515 #if wxUSE_FONTPICKERCTRL
516 RecreatePicker(Picker_Font
);
517 #endif // wxUSE_FONTPICKERCTRL
520 void PickerWidgetsPage::Reset()
522 #if wxUSE_COLOURPICKERCTRL
523 m_chkColourTextCtrl
->SetValue((wxCLRP_DEFAULT_STYLE
& wxCLRP_USE_TEXTCTRL
) != 0);
524 m_chkColourShowLabel
->SetValue((wxCLRP_DEFAULT_STYLE
& wxCLRP_SHOW_LABEL
) != 0);
527 #if wxUSE_FILEPICKERCTRL
528 m_radioFilePickerMode
->SetSelection((wxFLP_DEFAULT_STYLE
& wxFLP_OPEN
) ?
529 FilePickerMode_Open
: FilePickerMode_Save
);
530 m_chkFileTextCtrl
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_USE_TEXTCTRL
) != 0);
531 m_chkFileOverwritePrompt
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_OVERWRITE_PROMPT
) != 0);
532 m_chkFileMustExist
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_FILE_MUST_EXIST
) != 0);
533 m_chkFileChangeDir
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_CHANGE_DIR
) != 0);
535 UpdateFilePickerMode();
538 #if wxUSE_DIRPICKERCTRL
539 m_chkDirTextCtrl
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_USE_TEXTCTRL
) != 0);
540 m_chkDirMustExist
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_DIR_MUST_EXIST
) != 0);
541 m_chkDirChangeDir
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_CHANGE_DIR
) != 0);
544 #if wxUSE_FONTPICKERCTRL
545 m_chkFontTextCtrl
->SetValue((wxFNTP_DEFAULT_STYLE
& wxFNTP_USE_TEXTCTRL
) != 0);
546 m_chkFontUseFontForLabel
->SetValue((wxFNTP_DEFAULT_STYLE
& wxFNTP_USEFONT_FOR_LABEL
) != 0);
547 m_chkFontDescAsLabel
->SetValue((wxFNTP_DEFAULT_STYLE
& wxFNTP_FONTDESC_AS_LABEL
) != 0);
551 void PickerWidgetsPage::UpdateFilePickerMode()
553 switch (m_radioFilePickerMode
->GetSelection())
555 case FilePickerMode_Open
:
556 m_chkFileOverwritePrompt
->SetValue(false);
557 m_chkFileOverwritePrompt
->Disable();
558 m_chkFileMustExist
->Enable();
560 case FilePickerMode_Save
:
561 m_chkFileMustExist
->SetValue(false);
562 m_chkFileMustExist
->Disable();
563 m_chkFileOverwritePrompt
->Enable();
569 // ----------------------------------------------------------------------------
571 // ----------------------------------------------------------------------------
573 void PickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
577 RecreateAllPickers();
580 #if wxUSE_COLOURPICKERCTRL
581 void PickerWidgetsPage::OnColourChange(wxColourPickerEvent
& event
)
583 wxLogMessage(wxT("The colour changed to '%s' !"),
584 event
.GetColour().GetAsString(wxC2S_CSS_SYNTAX
).c_str());
587 #if wxUSE_FILEPICKERCTRL
588 void PickerWidgetsPage::OnFileChange(wxFileDirPickerEvent
& event
)
590 wxLogMessage(wxT("The file changed to '%s' ! The current working directory is '%s'"),
591 event
.GetPath().c_str(), wxGetCwd().c_str());
594 #if wxUSE_DIRPICKERCTRL
595 void PickerWidgetsPage::OnDirChange(wxFileDirPickerEvent
& event
)
597 wxLogMessage(wxT("The directory changed to '%s' ! The current working directory is '%s'"),
598 event
.GetPath().c_str(), wxGetCwd().c_str());
601 #if wxUSE_FONTPICKERCTRL
602 void PickerWidgetsPage::OnFontChange(wxFontPickerEvent
& event
)
604 wxLogMessage(wxT("The font changed to '%s' with size %d !"),
605 event
.GetFont().GetFaceName().c_str(), event
.GetFont().GetPointSize());
609 void PickerWidgetsPage::OnCheckBox(wxCommandEvent
&event
)
611 if (event
.GetEventObject() == m_chkColourTextCtrl
||
612 event
.GetEventObject() == m_chkColourShowLabel
)
613 RecreatePicker(Picker_Colour
);
615 if (event
.GetEventObject() == m_chkFileTextCtrl
||
616 event
.GetEventObject() == m_chkFileOverwritePrompt
||
617 event
.GetEventObject() == m_chkFileMustExist
||
618 event
.GetEventObject() == m_chkFileChangeDir
)
619 RecreatePicker(Picker_File
);
621 if (event
.GetEventObject() == m_chkDirTextCtrl
||
622 event
.GetEventObject() == m_chkDirChangeDir
||
623 event
.GetEventObject() == m_chkDirMustExist
)
624 RecreatePicker(Picker_Dir
);
626 if (event
.GetEventObject() == m_chkFontTextCtrl
||
627 event
.GetEventObject() == m_chkFontDescAsLabel
||
628 event
.GetEventObject() == m_chkFontUseFontForLabel
)
629 RecreatePicker(Picker_Font
);
631 if (event
.GetEventObject() == m_radioFilePickerMode
)
633 UpdateFilePickerMode();
634 RecreatePicker(Picker_File
);
638 #endif // wxUSE_COLOURPICKERCTRL || wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL || wxUSE_FONTPICKERCTRL