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"
33 // ============================================================================
35 // ============================================================================
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLOURPICKER_CHANGED
)
38 IMPLEMENT_DYNAMIC_CLASS(wxColourPickerCtrl
, wxPickerBase
)
39 IMPLEMENT_DYNAMIC_CLASS(wxColourPickerEvent
, wxEvent
)
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 #define M_PICKER ((wxColourPickerWidget*)m_picker)
47 bool wxColourPickerCtrl::Create( wxWindow
*parent
, wxWindowID id
,
49 const wxPoint
&pos
, const wxSize
&size
,
50 long style
, const wxValidator
& validator
,
51 const wxString
&name
)
53 if (!wxPickerBase::CreateBase(parent
, id
, col
.GetAsString(), pos
, size
,
54 style
, validator
, name
))
57 // we are not interested to the ID of our picker as we connect
58 // to its "changed" event dynamically...
59 m_picker
= new wxColourPickerWidget(this, wxID_ANY
, col
, wxPoint(40,0), wxSize(30,-1),
60 GetPickerStyle(style
));
61 m_picker
->Connect(wxEVT_COMMAND_COLOURPICKER_CHANGED
,
62 wxColourPickerEventHandler(wxColourPickerCtrl::OnColourChange
),
68 void wxColourPickerCtrl::SetColour(const wxColour
&col
)
70 M_PICKER
->SetColour(col
);
71 UpdateTextCtrlFromPicker();
74 bool wxColourPickerCtrl::SetColour(const wxString
&text
)
76 wxColour
col(text
); // smart wxString->wxColour conversion
79 M_PICKER
->SetColour(col
);
80 UpdateTextCtrlFromPicker();
85 void wxColourPickerCtrl::UpdatePickerFromTextCtrl()
89 if (m_bIgnoreNextTextCtrlUpdate
)
92 m_bIgnoreNextTextCtrlUpdate
= false;
96 // wxString -> wxColour conversion
97 wxColour
col(m_text
->GetValue());
99 return; // invalid user input
101 if (M_PICKER
->GetColour() != col
)
103 M_PICKER
->SetColour(col
);
106 wxColourPickerEvent
event(this, GetId(), col
);
107 GetEventHandler()->ProcessEvent(event
);
111 void wxColourPickerCtrl::UpdateTextCtrlFromPicker()
114 return; // no textctrl to update
116 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
117 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
118 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
119 m_bIgnoreNextTextCtrlUpdate
= true;
120 m_text
->SetValue(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