1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/clrpickercmn.cpp
3 // Purpose: wxColourPickerCtrl class implementation
4 // Author: Francesco Montorsi (readapted code written by Vadim Zeitlin)
8 // Copyright: (c) Vadim Zeitlin, Francesco Montorsi
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_COLOURPICKERCTRL
29 #include "wx/clrpicker.h"
31 #include "wx/textctrl.h"
34 // ============================================================================
36 // ============================================================================
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLOURPICKER_CHANGED
)
39 IMPLEMENT_DYNAMIC_CLASS(wxColourPickerCtrl
, wxPickerBase
)
40 IMPLEMENT_DYNAMIC_CLASS(wxColourPickerEvent
, wxEvent
)
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 #define M_PICKER ((wxColourPickerWidget*)m_picker)
48 bool wxColourPickerCtrl::Create( wxWindow
*parent
, wxWindowID id
,
50 const wxPoint
&pos
, const wxSize
&size
,
51 long style
, const wxValidator
& validator
,
52 const wxString
&name
)
54 if (!wxPickerBase::CreateBase(parent
, id
, col
.GetAsString(), pos
, size
,
55 style
, validator
, name
))
58 // we are not interested to the ID of our picker as we connect
59 // to its "changed" event dynamically...
60 m_picker
= new wxColourPickerWidget(this, wxID_ANY
, col
, wxPoint(40,0), wxSize(30,-1),
61 GetPickerStyle(style
));
62 m_picker
->Connect(wxEVT_COMMAND_COLOURPICKER_CHANGED
,
63 wxColourPickerEventHandler(wxColourPickerCtrl::OnColourChange
),
69 void wxColourPickerCtrl::SetColour(const wxColour
&col
)
71 M_PICKER
->SetColour(col
);
72 UpdateTextCtrlFromPicker();
75 bool wxColourPickerCtrl::SetColour(const wxString
&text
)
77 wxColour
col(text
); // smart wxString->wxColour conversion
80 M_PICKER
->SetColour(col
);
81 UpdateTextCtrlFromPicker();
86 void wxColourPickerCtrl::UpdatePickerFromTextCtrl()
90 if (m_bIgnoreNextTextCtrlUpdate
)
93 m_bIgnoreNextTextCtrlUpdate
= false;
97 // wxString -> wxColour conversion
98 wxColour
col(m_text
->GetValue());
100 return; // invalid user input
102 if (M_PICKER
->GetColour() != col
)
104 M_PICKER
->SetColour(col
);
107 wxColourPickerEvent
event(this, GetId(), col
);
108 GetEventHandler()->ProcessEvent(event
);
112 void wxColourPickerCtrl::UpdateTextCtrlFromPicker()
115 return; // no textctrl to update
117 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
118 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
119 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
120 m_bIgnoreNextTextCtrlUpdate
= true;
121 m_text
->SetValue(M_PICKER
->GetColour().GetAsString());
126 // ----------------------------------------------------------------------------
127 // wxColourPickerCtrl - event handlers
128 // ----------------------------------------------------------------------------
130 void wxColourPickerCtrl::OnColourChange(wxColourPickerEvent
&ev
)
132 UpdateTextCtrlFromPicker();
134 // the wxColourPickerWidget sent us a colour-change notification.
135 // forward this event to our parent
136 wxColourPickerEvent
event(this, GetId(), ev
.GetColour());
137 GetEventHandler()->ProcessEvent(event
);
140 #endif // wxUSE_COLOURPICKERCTRL