1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/clrpickercmn.cpp
3 // Purpose: wxColourPickerCtrl class implementation
4 // Author: Francesco Montorsi (readapted code written by Vadim Zeitlin)
7 // Copyright: (c) Vadim Zeitlin, Francesco Montorsi
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_COLOURPICKERCTRL
28 #include "wx/clrpicker.h"
31 #include "wx/textctrl.h"
34 const char wxColourPickerCtrlNameStr
[] = "colourpicker";
35 const char wxColourPickerWidgetNameStr
[] = "colourpickerwidget";
37 // ============================================================================
39 // ============================================================================
41 wxDEFINE_EVENT(wxEVT_COLOURPICKER_CHANGED
, wxColourPickerEvent
);
42 IMPLEMENT_DYNAMIC_CLASS(wxColourPickerCtrl
, wxPickerBase
)
43 IMPLEMENT_DYNAMIC_CLASS(wxColourPickerEvent
, wxEvent
)
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 #define M_PICKER ((wxColourPickerWidget*)m_picker)
51 bool wxColourPickerCtrl::Create( wxWindow
*parent
, wxWindowID id
,
53 const wxPoint
&pos
, const wxSize
&size
,
54 long style
, const wxValidator
& validator
,
55 const wxString
&name
)
57 if (!wxPickerBase::CreateBase(parent
, id
, col
.GetAsString(), pos
, size
,
58 style
, validator
, name
))
61 // we are not interested to the ID of our picker as we connect
62 // to its "changed" event dynamically...
63 m_picker
= new wxColourPickerWidget(this, wxID_ANY
, col
,
64 wxDefaultPosition
, wxDefaultSize
,
65 GetPickerStyle(style
));
67 // complete sizer creation
68 wxPickerBase::PostCreation();
70 m_picker
->Connect(wxEVT_COLOURPICKER_CHANGED
,
71 wxColourPickerEventHandler(wxColourPickerCtrl::OnColourChange
),
77 void wxColourPickerCtrl::SetColour(const wxColour
&col
)
79 M_PICKER
->SetColour(col
);
80 UpdateTextCtrlFromPicker();
83 bool wxColourPickerCtrl::SetColour(const wxString
&text
)
85 wxColour
col(text
); // smart wxString->wxColour conversion
88 M_PICKER
->SetColour(col
);
89 UpdateTextCtrlFromPicker();
94 void wxColourPickerCtrl::UpdatePickerFromTextCtrl()
98 // wxString -> wxColour conversion
99 wxColour
col(m_text
->GetValue());
101 return; // invalid user input
103 if (M_PICKER
->GetColour() != col
)
105 M_PICKER
->SetColour(col
);
108 wxColourPickerEvent
event(this, GetId(), col
);
109 GetEventHandler()->ProcessEvent(event
);
113 void wxColourPickerCtrl::UpdateTextCtrlFromPicker()
116 return; // no textctrl to update
118 // Take care to use ChangeValue() here and not SetValue() to avoid
119 // infinite recursion.
120 m_text
->ChangeValue(M_PICKER
->GetColour().GetAsString());
125 // ----------------------------------------------------------------------------
126 // wxColourPickerCtrl - event handlers
127 // ----------------------------------------------------------------------------
129 void wxColourPickerCtrl::OnColourChange(wxColourPickerEvent
&ev
)
131 UpdateTextCtrlFromPicker();
133 // the wxColourPickerWidget sent us a colour-change notification.
134 // forward this event to our parent
135 wxColourPickerEvent
event(this, GetId(), ev
.GetColour());
136 GetEventHandler()->ProcessEvent(event
);
139 #endif // wxUSE_COLOURPICKERCTRL