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
, wxPoint(40,0), wxSize(30,-1),
63 GetPickerStyle(style
));
65 // complete sizer creation
66 wxPickerBase::PostCreation();
68 m_picker
->Connect(wxEVT_COMMAND_COLOURPICKER_CHANGED
,
69 wxColourPickerEventHandler(wxColourPickerCtrl::OnColourChange
),
75 void wxColourPickerCtrl::SetColour(const wxColour
&col
)
77 M_PICKER
->SetColour(col
);
78 UpdateTextCtrlFromPicker();
81 bool wxColourPickerCtrl::SetColour(const wxString
&text
)
83 wxColour
col(text
); // smart wxString->wxColour conversion
86 M_PICKER
->SetColour(col
);
87 UpdateTextCtrlFromPicker();
92 void wxColourPickerCtrl::UpdatePickerFromTextCtrl()
96 if (m_bIgnoreNextTextCtrlUpdate
)
99 m_bIgnoreNextTextCtrlUpdate
= false;
103 // wxString -> wxColour conversion
104 wxColour
col(m_text
->GetValue());
106 return; // invalid user input
108 if (M_PICKER
->GetColour() != col
)
110 M_PICKER
->SetColour(col
);
113 wxColourPickerEvent
event(this, GetId(), col
);
114 GetEventHandler()->ProcessEvent(event
);
118 void wxColourPickerCtrl::UpdateTextCtrlFromPicker()
121 return; // no textctrl to update
123 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
124 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
125 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
126 m_bIgnoreNextTextCtrlUpdate
= true;
127 m_text
->SetValue(M_PICKER
->GetColour().GetAsString());
132 // ----------------------------------------------------------------------------
133 // wxColourPickerCtrl - event handlers
134 // ----------------------------------------------------------------------------
136 void wxColourPickerCtrl::OnColourChange(wxColourPickerEvent
&ev
)
138 UpdateTextCtrlFromPicker();
140 // the wxColourPickerWidget sent us a colour-change notification.
141 // forward this event to our parent
142 wxColourPickerEvent
event(this, GetId(), ev
.GetColour());
143 GetEventHandler()->ProcessEvent(event
);
146 #endif // wxUSE_COLOURPICKERCTRL