correct access for virtuals, unneeded includes
[wxWidgets.git] / src / common / clrpickercmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/clrpickercmn.cpp
3 // Purpose: wxColourPickerCtrl class implementation
4 // Author: Francesco Montorsi (readapted code written by Vadim Zeitlin)
5 // Modified by:
6 // Created: 15/04/2006
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin, Francesco Montorsi
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_COLOURPICKERCTRL
28
29 #include "wx/clrpicker.h"
30
31 #include "wx/textctrl.h"
32
33
34 // ============================================================================
35 // implementation
36 // ============================================================================
37
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLOURPICKER_CHANGED)
39 IMPLEMENT_DYNAMIC_CLASS(wxColourPickerCtrl, wxPickerBase)
40 IMPLEMENT_DYNAMIC_CLASS(wxColourPickerEvent, wxEvent)
41
42 // ----------------------------------------------------------------------------
43 // wxColourPickerCtrl
44 // ----------------------------------------------------------------------------
45
46 #define M_PICKER ((wxColourPickerWidget*)m_picker)
47
48 bool wxColourPickerCtrl::Create( wxWindow *parent, wxWindowID id,
49 const wxColour &col,
50 const wxPoint &pos, const wxSize &size,
51 long style, const wxValidator& validator,
52 const wxString &name )
53 {
54 if (!wxPickerBase::CreateBase(parent, id, col.GetAsString(), pos, size,
55 style, validator, name))
56 return false;
57
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),
64 NULL, this);
65
66 return true;
67 }
68
69 void wxColourPickerCtrl::SetColour(const wxColour &col)
70 {
71 M_PICKER->SetColour(col);
72 UpdateTextCtrlFromPicker();
73 }
74
75 bool wxColourPickerCtrl::SetColour(const wxString &text)
76 {
77 wxColour col(text); // smart wxString->wxColour conversion
78 if ( !col.Ok() )
79 return false;
80 M_PICKER->SetColour(col);
81 UpdateTextCtrlFromPicker();
82
83 return true;
84 }
85
86 void wxColourPickerCtrl::UpdatePickerFromTextCtrl()
87 {
88 wxASSERT(m_text);
89
90 if (m_bIgnoreNextTextCtrlUpdate)
91 {
92 // ignore this update
93 m_bIgnoreNextTextCtrlUpdate = false;
94 return;
95 }
96
97 // wxString -> wxColour conversion
98 wxColour col(m_text->GetValue());
99 if ( !col.Ok() )
100 return; // invalid user input
101
102 if (M_PICKER->GetColour() != col)
103 {
104 M_PICKER->SetColour(col);
105
106 // fire an event
107 wxColourPickerEvent event(this, GetId(), col);
108 GetEventHandler()->ProcessEvent(event);
109 }
110 }
111
112 void wxColourPickerCtrl::UpdateTextCtrlFromPicker()
113 {
114 if (!m_text)
115 return; // no textctrl to update
116
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());
122 }
123
124
125
126 // ----------------------------------------------------------------------------
127 // wxColourPickerCtrl - event handlers
128 // ----------------------------------------------------------------------------
129
130 void wxColourPickerCtrl::OnColourChange(wxColourPickerEvent &ev)
131 {
132 UpdateTextCtrlFromPicker();
133
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);
138 }
139
140 #endif // wxUSE_COLOURPICKERCTRL