1/////////////////////////////////////////////////////////////////////////////
2// Program:     wxWidgets Widgets Sample
3// Name:        clrpicker.cpp
4// Purpose:     Shows wxColourPickerCtrl
5// Author:      Francesco Montorsi
6// Created:     20/6/2006
7// Id:          $Id: clrpicker.cpp 43755 2006-12-03 13:43:44Z VZ $
8// Copyright:   (c) 2006 Francesco Montorsi
9// License:     wxWindows license
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_COLOURPICKERCTRL
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#endif
35
36#include "wx/artprov.h"
37#include "wx/sizer.h"
38#include "wx/stattext.h"
39#include "wx/checkbox.h"
40#include "wx/imaglist.h"
41
42#include "wx/clrpicker.h"
43#include "widgets.h"
44
45#include "icons/clrpicker.xpm"
46
47// ----------------------------------------------------------------------------
48// constants
49// ----------------------------------------------------------------------------
50
51// control ids
52enum
53{
54    PickerPage_Reset = wxID_HIGHEST,
55    PickerPage_Colour
56};
57
58
59// ----------------------------------------------------------------------------
60// ColourPickerWidgetsPage
61// ----------------------------------------------------------------------------
62
63class ColourPickerWidgetsPage : public WidgetsPage
64{
65public:
66    ColourPickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
67    virtual ~ColourPickerWidgetsPage(){};
68
69    virtual wxControl *GetWidget() const { return m_clrPicker; }
70    virtual void RecreateWidget() { RecreatePicker(); }
71
72    // lazy creation of the content
73    virtual void CreateContent();
74
75protected:
76
77    // called only once at first construction
78    void CreatePicker();
79
80    // called to recreate an existing control
81    void RecreatePicker();
82
83    // restore the checkboxes state to the initial values
84    void Reset();
85
86    // get the initial style for the picker of the given kind
87    long GetPickerStyle();
88
89
90    void OnColourChange(wxColourPickerEvent &ev);
91    void OnCheckBox(wxCommandEvent &ev);
92    void OnButtonReset(wxCommandEvent &ev);
93
94    // the picker
95    wxColourPickerCtrl *m_clrPicker;
96
97
98    // other controls
99    // --------------
100
101    wxCheckBox *m_chkColourTextCtrl,
102               *m_chkColourShowLabel;
103    wxBoxSizer *m_sizer;
104
105private:
106    DECLARE_EVENT_TABLE()
107    DECLARE_WIDGETS_PAGE(ColourPickerWidgetsPage)
108};
109
110// ----------------------------------------------------------------------------
111// event tables
112// ----------------------------------------------------------------------------
113
114BEGIN_EVENT_TABLE(ColourPickerWidgetsPage, WidgetsPage)
115    EVT_BUTTON(PickerPage_Reset, ColourPickerWidgetsPage::OnButtonReset)
116
117    EVT_COLOURPICKER_CHANGED(PickerPage_Colour, ColourPickerWidgetsPage::OnColourChange)
118
119    EVT_CHECKBOX(wxID_ANY, ColourPickerWidgetsPage::OnCheckBox)
120END_EVENT_TABLE()
121
122// ============================================================================
123// implementation
124// ============================================================================
125
126#if defined(__WXGTK24__)
127    #define FAMILY_CTRLS NATIVE_CTRLS
128#else
129    #define FAMILY_CTRLS GENERIC_CTRLS
130#endif
131
132IMPLEMENT_WIDGETS_PAGE(ColourPickerWidgetsPage, _T("ColourPicker"),
133                       PICKER_CTRLS | FAMILY_CTRLS);
134
135ColourPickerWidgetsPage::ColourPickerWidgetsPage(WidgetsBookCtrl *book,
136                                     wxImageList *imaglist)
137                  : WidgetsPage(book, imaglist, clrpicker_xpm)
138{
139}
140
141void ColourPickerWidgetsPage::CreateContent()
142{
143    // left pane
144    wxSizer *boxleft = new wxBoxSizer(wxVERTICAL);
145
146    wxStaticBoxSizer *clrbox = new wxStaticBoxSizer(wxVERTICAL, this, _T("&ColourPicker style"));
147    m_chkColourTextCtrl = CreateCheckBoxAndAddToSizer(clrbox, _T("With textctrl"), false);
148    m_chkColourShowLabel = CreateCheckBoxAndAddToSizer(clrbox, _T("With label"), false);
149    boxleft->Add(clrbox, 0, wxALL|wxGROW, 5);
150
151    boxleft->Add(new wxButton(this, PickerPage_Reset, _T("&Reset")),
152                 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
153
154    Reset();    // set checkboxes state
155
156    // create pickers
157    m_clrPicker = NULL;
158    CreatePicker();
159
160    // right pane
161    m_sizer = new wxBoxSizer(wxVERTICAL);
162    m_sizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
163    m_sizer->Add(m_clrPicker, 0, wxALIGN_CENTER|wxALL, 5);
164    m_sizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
165
166    // global pane
167    wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
168    sz->Add(boxleft, 0, wxGROW|wxALL, 5);
169    sz->Add(m_sizer, 1, wxGROW|wxALL, 5);
170
171    SetSizer(sz);
172}
173
174void ColourPickerWidgetsPage::CreatePicker()
175{
176    delete m_clrPicker;
177
178    m_clrPicker = new wxColourPickerCtrl(this, PickerPage_Colour, *wxRED,
179                                            wxDefaultPosition, wxDefaultSize,
180                                            GetPickerStyle());
181}
182
183long ColourPickerWidgetsPage::GetPickerStyle()
184{
185    long style = 0;
186
187    if ( m_chkColourTextCtrl->GetValue() )
188        style |= wxCLRP_USE_TEXTCTRL;
189
190    if ( m_chkColourShowLabel->GetValue() )
191        style |= wxCLRP_SHOW_LABEL;
192
193    return style;
194}
195
196void ColourPickerWidgetsPage::RecreatePicker()
197{
198    m_sizer->Remove(1);
199    CreatePicker();
200    m_sizer->Insert(1, m_clrPicker, 0, wxALIGN_CENTER|wxALL, 5);
201
202    m_sizer->Layout();
203}
204
205void ColourPickerWidgetsPage::Reset()
206{
207    m_chkColourTextCtrl->SetValue((wxCLRP_DEFAULT_STYLE & wxCLRP_USE_TEXTCTRL) != 0);
208    m_chkColourShowLabel->SetValue((wxCLRP_DEFAULT_STYLE & wxCLRP_SHOW_LABEL) != 0);
209}
210
211
212// ----------------------------------------------------------------------------
213// event handlers
214// ----------------------------------------------------------------------------
215
216void ColourPickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
217{
218    Reset();
219    RecreatePicker();
220}
221
222void ColourPickerWidgetsPage::OnColourChange(wxColourPickerEvent& event)
223{
224    wxLogMessage(wxT("The colour changed to '%s' !"),
225                 event.GetColour().GetAsString(wxC2S_CSS_SYNTAX).c_str());
226}
227
228void ColourPickerWidgetsPage::OnCheckBox(wxCommandEvent &event)
229{
230    if (event.GetEventObject() == m_chkColourTextCtrl ||
231        event.GetEventObject() == m_chkColourShowLabel)
232        RecreatePicker();
233}
234
235#endif  //  wxUSE_COLOURPICKERCTRL
236