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
));
64 m_picker
->Connect(wxEVT_COMMAND_COLOURPICKER_CHANGED
,
65 wxColourPickerEventHandler(wxColourPickerCtrl::OnColourChange
),
71 void wxColourPickerCtrl::SetColour(const wxColour
&col
)
73 M_PICKER
->SetColour(col
);
74 UpdateTextCtrlFromPicker();
77 bool wxColourPickerCtrl::SetColour(const wxString
&text
)
79 wxColour
col(text
); // smart wxString->wxColour conversion
82 M_PICKER
->SetColour(col
);
83 UpdateTextCtrlFromPicker();
88 void wxColourPickerCtrl::UpdatePickerFromTextCtrl()
92 if (m_bIgnoreNextTextCtrlUpdate
)
95 m_bIgnoreNextTextCtrlUpdate
= false;
99 // wxString -> wxColour conversion
100 wxColour
col(m_text
->GetValue());
102 return; // invalid user input
104 if (M_PICKER
->GetColour() != col
)
106 M_PICKER
->SetColour(col
);
109 wxColourPickerEvent
event(this, GetId(), col
);
110 GetEventHandler()->ProcessEvent(event
);
114 void wxColourPickerCtrl::UpdateTextCtrlFromPicker()
117 return; // no textctrl to update
119 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
120 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
121 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
122 m_bIgnoreNextTextCtrlUpdate
= true;
123 m_text
->SetValue(M_PICKER
->GetColour().GetAsString());
128 // ----------------------------------------------------------------------------
129 // wxColourPickerCtrl - event handlers
130 // ----------------------------------------------------------------------------
132 void wxColourPickerCtrl::OnColourChange(wxColourPickerEvent
&ev
)
134 UpdateTextCtrlFromPicker();
136 // the wxColourPickerWidget sent us a colour-change notification.
137 // forward this event to our parent
138 wxColourPickerEvent
event(this, GetId(), ev
.GetColour());
139 GetEventHandler()->ProcessEvent(event
);
142 #endif // wxUSE_COLOURPICKERCTRL