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"
32 #include "wx/textctrl.h"
36 // ============================================================================
38 // ============================================================================
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLOURPICKER_CHANGED
)
41 IMPLEMENT_DYNAMIC_CLASS(wxColourPickerCtrl
, wxPickerBase
)
42 IMPLEMENT_DYNAMIC_CLASS(wxColourPickerEvent
, wxEvent
)
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 #define M_PICKER ((wxColourPickerWidget*)m_picker)
50 bool wxColourPickerCtrl::Create( wxWindow
*parent
, wxWindowID id
,
52 const wxPoint
&pos
, const wxSize
&size
,
53 long style
, const wxValidator
& validator
,
54 const wxString
&name
)
56 if (!wxPickerBase::CreateBase(parent
, id
, col
.GetAsString(), pos
, size
,
57 style
, validator
, name
))
60 // we are not interested to the ID of our picker as we connect
61 // to its "changed" event dynamically...
62 m_picker
= new wxColourPickerWidget(this, wxID_ANY
, col
,
63 wxDefaultPosition
, wxDefaultSize
,
64 GetPickerStyle(style
));
66 // complete sizer creation
67 wxPickerBase::PostCreation();
69 m_picker
->Connect(wxEVT_COMMAND_COLOURPICKER_CHANGED
,
70 wxColourPickerEventHandler(wxColourPickerCtrl::OnColourChange
),
76 void wxColourPickerCtrl::SetColour(const wxColour
&col
)
78 M_PICKER
->SetColour(col
);
79 UpdateTextCtrlFromPicker();
82 bool wxColourPickerCtrl::SetColour(const wxString
&text
)
84 wxColour
col(text
); // smart wxString->wxColour conversion
87 M_PICKER
->SetColour(col
);
88 UpdateTextCtrlFromPicker();
93 void wxColourPickerCtrl::UpdatePickerFromTextCtrl()
97 if (m_bIgnoreNextTextCtrlUpdate
)
100 m_bIgnoreNextTextCtrlUpdate
= false;
104 // wxString -> wxColour conversion
105 wxColour
col(m_text
->GetValue());
107 return; // invalid user input
109 if (M_PICKER
->GetColour() != col
)
111 M_PICKER
->SetColour(col
);
114 wxColourPickerEvent
event(this, GetId(), col
);
115 GetEventHandler()->ProcessEvent(event
);
119 void wxColourPickerCtrl::UpdateTextCtrlFromPicker()
122 return; // no textctrl to update
124 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
125 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
126 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
127 m_bIgnoreNextTextCtrlUpdate
= true;
128 m_text
->SetValue(M_PICKER
->GetColour().GetAsString());
133 // ----------------------------------------------------------------------------
134 // wxColourPickerCtrl - event handlers
135 // ----------------------------------------------------------------------------
137 void wxColourPickerCtrl::OnColourChange(wxColourPickerEvent
&ev
)
139 UpdateTextCtrlFromPicker();
141 // the wxColourPickerWidget sent us a colour-change notification.
142 // forward this event to our parent
143 wxColourPickerEvent
event(this, GetId(), ev
.GetColour());
144 GetEventHandler()->ProcessEvent(event
);
147 #endif // wxUSE_COLOURPICKERCTRL