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 #if defined(__WXGTK24__)
187 #define FAMILY_CTRLS NATIVE_CTRLS
189 #define FAMILY_CTRLS GENERIC_CTRLS
192 IMPLEMENT_WIDGETS_PAGE(PickerWidgetsPage
, _T("Pickers"),
193 PICKER_CTRLS
| FAMILY_CTRLS
);
195 PickerWidgetsPage::PickerWidgetsPage(WidgetsBookCtrl
*book
,
196 wxImageList
*imaglist
)
197 : WidgetsPage(book
, imaglist
, picker_xpm
)
200 wxSizer
*boxleft
= new wxBoxSizer(wxVERTICAL
);
202 #if wxUSE_COLOURPICKERCTRL
203 wxStaticBoxSizer
*clrbox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&ColourPicker style"));
204 m_chkColourTextCtrl
= CreateCheckBoxAndAddToSizer(clrbox
, _T("With textctrl"), false);
205 m_chkColourShowLabel
= CreateCheckBoxAndAddToSizer(clrbox
, _T("With label"), false);
206 boxleft
->Add(clrbox
, 0, wxALL
|wxGROW
, 5);
207 boxleft
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
208 #endif // wxUSE_COLOURPICKERCTRL
210 #if wxUSE_FILEPICKERCTRL
211 wxStaticBoxSizer
*filebox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&FilePicker style"));
212 m_chkFileTextCtrl
= CreateCheckBoxAndAddToSizer(filebox
, _T("With textctrl"), false);
213 m_chkFileOverwritePrompt
= CreateCheckBoxAndAddToSizer(filebox
, _T("Overwrite prompt"), false);
214 m_chkFileMustExist
= CreateCheckBoxAndAddToSizer(filebox
, _T("File must exist"), false);
215 m_chkFileChangeDir
= CreateCheckBoxAndAddToSizer(filebox
, _T("Change working dir"), false);
216 boxleft
->Add(filebox
, 0, wxALL
|wxGROW
, 5);
217 boxleft
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
218 #endif // wxUSE_FILEPICKERCTRL
220 #if wxUSE_DIRPICKERCTRL
221 wxStaticBoxSizer
*dirbox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&DirPicker style"));
222 m_chkDirTextCtrl
= CreateCheckBoxAndAddToSizer(dirbox
, _T("With textctrl"), false);
223 m_chkDirMustExist
= CreateCheckBoxAndAddToSizer(dirbox
, _T("Dir must exist"), false);
224 m_chkDirChangeDir
= CreateCheckBoxAndAddToSizer(dirbox
, _T("Change working dir"), false);
225 boxleft
->Add(dirbox
, 0, wxALL
|wxGROW
, 5);
226 boxleft
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
227 #endif // wxUSE_DIRPICKERCTRL
229 #if wxUSE_FONTPICKERCTRL
230 wxStaticBoxSizer
*fontbox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&FontPicker style"));
231 m_chkFontTextCtrl
= CreateCheckBoxAndAddToSizer(fontbox
, _T("With textctrl"));
232 m_chkFontDescAsLabel
= CreateCheckBoxAndAddToSizer(fontbox
, _T("Font desc as btn label"));
233 m_chkFontUseFontForLabel
= CreateCheckBoxAndAddToSizer(fontbox
, _T("Use font for label"), false);
234 boxleft
->Add(fontbox
, 0, wxALL
|wxGROW
, 5);
235 #endif // wxUSE_FONTPICKERCTRL
237 boxleft
->Add(new wxButton(this, PickerPage_Reset
, _T("&Reset")),
238 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
240 Reset(); // set checkboxes state
249 #if wxUSE_COLOURPICKERCTRL
250 CreatePickers(Picker_Colour
);
252 #endif // wxUSE_COLOURPICKERCTRL
254 #if wxUSE_FILEPICKERCTRL
255 CreatePickers(Picker_File
);
257 #endif // wxUSE_FILEPICKERCTRL
259 #if wxUSE_DIRPICKERCTRL
260 CreatePickers(Picker_Dir
);
262 #endif // wxUSE_DIRPICKERCTRL
264 #if wxUSE_FONTPICKERCTRL
265 CreatePickers(Picker_Font
);
267 #endif // wxUSE_FONTPICKERCTRL
269 m_sizerPicker
= new wxFlexGridSizer(nrows
, 2, 0, 0); // 4 rows x 2 columns
270 #if wxUSE_COLOURPICKERCTRL
271 m_sizerPicker
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxColourPickerCtrl:")),
272 0, wxALIGN_CENTER_VERTICAL
| wxALIGN_RIGHT
| wxALL
, 5);
273 m_sizerPicker
->Add(m_clrPicker
, 1, wxGROW
| wxALL
, 5);
275 #if wxUSE_FILEPICKERCTRL
276 m_sizerPicker
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxFilePickerCtrl:")),
277 0, wxALIGN_CENTER_VERTICAL
| wxALIGN_RIGHT
| wxALL
, 5);
278 m_sizerPicker
->Add(m_filePicker
, 1, wxGROW
| wxALL
, 5);
280 #if wxUSE_DIRPICKERCTRL
281 m_sizerPicker
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxDirPickerCtrl:")),
282 0, wxALIGN_CENTER_VERTICAL
| wxALIGN_RIGHT
| wxALL
, 5);
283 m_sizerPicker
->Add(m_dirPicker
, 1, wxGROW
| wxALL
, 5);
285 #if wxUSE_FONTPICKERCTRL
286 m_sizerPicker
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxFontPickerCtrl:")),
287 0, wxALIGN_CENTER_VERTICAL
| wxALIGN_RIGHT
| wxALL
, 5);
288 m_sizerPicker
->Add(m_fontPicker
, 1, wxGROW
| wxALL
, 5);
291 m_sizerPicker
->AddGrowableCol(1, 3);
295 boxright
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("Pickers"));
296 boxright
->Add(m_sizerPicker
, 1, wxGROW
| wxALL
, 5);
299 wxSizer
*sz
= new wxBoxSizer(wxHORIZONTAL
);
300 sz
->Add(boxleft
, 0, wxGROW
|wxALL
, 5);
301 sz
->Add(boxright
, 1, wxGROW
|wxALL
, 5);
306 void PickerWidgetsPage::CreatePickers(PickerKind picker
)
310 #if wxUSE_COLOURPICKERCTRL
314 m_clrPicker
= new wxColourPickerCtrl(this, PickerPage_Colour
, *wxRED
,
315 wxDefaultPosition
, wxDefaultSize
,
316 GetPickerStyle(Picker_Colour
));
318 #endif // wxUSE_COLOURPICKERCTRL
320 #if wxUSE_FILEPICKERCTRL
324 // pass an empty string as initial file
325 m_filePicker
= new wxFilePickerCtrl(this, PickerPage_File
,
327 wxT("Hello!"), wxT("*"),
328 wxDefaultPosition
, wxDefaultSize
,
329 GetPickerStyle(Picker_File
));
331 #endif // wxUSE_FILEPICKERCTRL
333 #if wxUSE_DIRPICKERCTRL
337 m_dirPicker
= new wxDirPickerCtrl(this, PickerPage_Dir
,
338 wxGetHomeDir(), wxT("Hello!"),
339 wxDefaultPosition
, wxDefaultSize
,
340 GetPickerStyle(Picker_Dir
));
342 #endif // wxUSE_DIRPICKERCTRL
344 #if wxUSE_FONTPICKERCTRL
348 m_fontPicker
= new wxFontPickerCtrl(this, PickerPage_Font
,
350 wxDefaultPosition
, wxDefaultSize
,
351 GetPickerStyle(Picker_Font
));
353 #endif // wxUSE_FONTPICKERCTRL
356 wxFAIL_MSG( _T("unknown picker kind") );
360 long PickerWidgetsPage::GetPickerStyle(PickerKind picker
)
366 #if wxUSE_COLOURPICKERCTRL
368 if ( m_chkColourTextCtrl
->GetValue() )
369 style
|= wxCLRP_USE_TEXTCTRL
;
371 if ( m_chkColourShowLabel
->GetValue() )
372 style
|= wxCLRP_SHOW_LABEL
;
375 #endif // wxUSE_COLOURPICKERCTRL
377 #if wxUSE_FILEPICKERCTRL
379 if ( m_chkFileTextCtrl
->GetValue() )
380 style
|= wxFLP_USE_TEXTCTRL
;
382 if ( m_chkFileOverwritePrompt
->GetValue() )
383 style
|= wxFLP_OVERWRITE_PROMPT
;
385 if ( m_chkFileMustExist
->GetValue() )
386 style
|= wxFLP_FILE_MUST_EXIST
;
388 if ( m_chkFileChangeDir
->GetValue() )
389 style
|= wxFLP_CHANGE_DIR
;
392 #endif // wxUSE_FILEPICKERCTRL
394 #if wxUSE_DIRPICKERCTRL
396 if ( m_chkDirTextCtrl
->GetValue() )
397 style
|= wxDIRP_USE_TEXTCTRL
;
399 if ( m_chkDirMustExist
->GetValue() )
400 style
|= wxDIRP_DIR_MUST_EXIST
;
402 if ( m_chkDirChangeDir
->GetValue() )
403 style
|= wxDIRP_CHANGE_DIR
;
406 #endif // wxUSE_DIRPICKERCTRL
408 #if wxUSE_FONTPICKERCTRL
410 if ( m_chkFontTextCtrl
->GetValue() )
411 style
|= wxFNTP_USE_TEXTCTRL
;
413 if ( m_chkFontUseFontForLabel
->GetValue() )
414 style
|= wxFNTP_USEFONT_FOR_LABEL
;
416 if ( m_chkFontDescAsLabel
->GetValue() )
417 style
|= wxFNTP_FONTDESC_AS_LABEL
;
420 #endif // wxUSE_FONTPICKERCTRL
423 wxFAIL_MSG( _T("unknown picker kind") );
429 void PickerWidgetsPage::RecreatePicker(PickerKind picker
)
433 #if wxUSE_COLOURPICKERCTRL
435 m_sizerPicker
->Remove(1);
436 CreatePickers(Picker_Colour
);
437 m_sizerPicker
->Insert(1, m_clrPicker
, 1, wxGROW
|wxALL
, 5);
439 #endif // wxUSE_COLOURPICKERCTRL
441 #if wxUSE_FILEPICKERCTRL
443 m_sizerPicker
->Remove(3);
444 CreatePickers(Picker_File
);
445 m_sizerPicker
->Insert(3, m_filePicker
, 1, wxGROW
|wxALL
, 5);
447 #endif // wxUSE_FILEPICKERCTRL
449 #if wxUSE_DIRPICKERCTRL
451 m_sizerPicker
->Remove(5);
452 CreatePickers(Picker_Dir
);
453 m_sizerPicker
->Insert(5, m_dirPicker
, 1, wxGROW
|wxALL
, 5);
455 #endif // wxUSE_DIRPICKERCTRL
457 #if wxUSE_FONTPICKERCTRL
459 wxASSERT(m_sizerPicker
->Remove(7));
460 CreatePickers(Picker_Font
);
461 m_sizerPicker
->Insert(7, m_fontPicker
, 1, wxGROW
|wxALL
, 5);
463 #endif // wxUSE_FONTPICKERCTRL
466 wxFAIL_MSG( _T("unknown picker kind") );
469 m_sizerPicker
->Layout();
472 void PickerWidgetsPage::RecreateAllPickers()
474 #if wxUSE_COLOURPICKERCTRL
475 RecreatePicker(Picker_Colour
);
476 #endif // wxUSE_COLOURPICKERCTRL
478 #if wxUSE_FILEPICKERCTRL
479 RecreatePicker(Picker_File
);
480 #endif // wxUSE_FILEPICKERCTRL
482 #if wxUSE_DIRPICKERCTRL
483 RecreatePicker(Picker_Dir
);
484 #endif // wxUSE_DIRPICKERCTRL
486 #if wxUSE_FONTPICKERCTRL
487 RecreatePicker(Picker_Font
);
488 #endif // wxUSE_FONTPICKERCTRL
491 void PickerWidgetsPage::Reset()
493 #if wxUSE_COLOURPICKERCTRL
494 m_chkColourTextCtrl
->SetValue((wxCLRP_DEFAULT_STYLE
& wxCLRP_USE_TEXTCTRL
) != 0);
495 m_chkColourShowLabel
->SetValue((wxCLRP_DEFAULT_STYLE
& wxCLRP_SHOW_LABEL
) != 0);
498 #if wxUSE_FILEPICKERCTRL
499 m_chkFileTextCtrl
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_USE_TEXTCTRL
) != 0);
500 m_chkFileOverwritePrompt
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_OVERWRITE_PROMPT
) != 0);
501 m_chkFileMustExist
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_FILE_MUST_EXIST
) != 0);
502 m_chkFileChangeDir
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_CHANGE_DIR
) != 0);
505 #if wxUSE_DIRPICKERCTRL
506 m_chkDirTextCtrl
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_USE_TEXTCTRL
) != 0);
507 m_chkDirMustExist
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_DIR_MUST_EXIST
) != 0);
508 m_chkDirChangeDir
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_CHANGE_DIR
) != 0);
511 #if wxUSE_FONTPICKERCTRL
512 m_chkFontTextCtrl
->SetValue((wxFNTP_DEFAULT_STYLE
& wxFNTP_USE_TEXTCTRL
) != 0);
513 m_chkFontUseFontForLabel
->SetValue((wxFNTP_DEFAULT_STYLE
& wxFNTP_USEFONT_FOR_LABEL
) != 0);
514 m_chkFontDescAsLabel
->SetValue((wxFNTP_DEFAULT_STYLE
& wxFNTP_FONTDESC_AS_LABEL
) != 0);
519 // ----------------------------------------------------------------------------
521 // ----------------------------------------------------------------------------
523 void PickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
527 RecreateAllPickers();
530 #if wxUSE_COLOURPICKERCTRL
531 void PickerWidgetsPage::OnColourChange(wxColourPickerEvent
& event
)
533 wxLogMessage(wxT("The colour changed to '%s' !"),
534 event
.GetColour().GetAsString(wxC2S_CSS_SYNTAX
).c_str());
537 #if wxUSE_FILEPICKERCTRL
538 void PickerWidgetsPage::OnFileChange(wxFileDirPickerEvent
& event
)
540 wxLogMessage(wxT("The file changed to '%s' ! The current working directory is '%s'"),
541 event
.GetPath().c_str(), wxGetCwd().c_str());
544 #if wxUSE_DIRPICKERCTRL
545 void PickerWidgetsPage::OnDirChange(wxFileDirPickerEvent
& event
)
547 wxLogMessage(wxT("The directory changed to '%s' ! The current working directory is '%s'"),
548 event
.GetPath().c_str(), wxGetCwd().c_str());
551 #if wxUSE_FONTPICKERCTRL
552 void PickerWidgetsPage::OnFontChange(wxFontPickerEvent
& event
)
554 wxLogMessage(wxT("The font changed to '%s' with size %d !"),
555 event
.GetFont().GetFaceName().c_str(), event
.GetFont().GetPointSize());
559 void PickerWidgetsPage::OnCheckBox(wxCommandEvent
&event
)
561 if (event
.GetEventObject() == m_chkColourTextCtrl
||
562 event
.GetEventObject() == m_chkColourShowLabel
)
563 RecreatePicker(Picker_Colour
);
565 if (event
.GetEventObject() == m_chkFileTextCtrl
||
566 event
.GetEventObject() == m_chkFileOverwritePrompt
||
567 event
.GetEventObject() == m_chkFileMustExist
||
568 event
.GetEventObject() == m_chkFileChangeDir
)
569 RecreatePicker(Picker_File
);
571 if (event
.GetEventObject() == m_chkDirTextCtrl
||
572 event
.GetEventObject() == m_chkDirChangeDir
||
573 event
.GetEventObject() == m_chkDirMustExist
)
574 RecreatePicker(Picker_Dir
);
576 if (event
.GetEventObject() == m_chkFontTextCtrl
||
577 event
.GetEventObject() == m_chkFontDescAsLabel
||
578 event
.GetEventObject() == m_chkFontUseFontForLabel
)
579 RecreatePicker(Picker_Font
);
582 #endif // wxUSE_COLOURPICKERCTRL || wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL || wxUSE_FONTPICKERCTRL