]> git.saurik.com Git - wxWidgets.git/blob - src/common/clrpickercmn.cpp
[ 1509599 ] 'Split pickers page in widgets sample' with more icons and rebaking.
[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 #ifndef WX_PRECOMP
32 #include "wx/textctrl.h"
33 #endif
34
35
36 // ============================================================================
37 // implementation
38 // ============================================================================
39
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLOURPICKER_CHANGED)
41 IMPLEMENT_DYNAMIC_CLASS(wxColourPickerCtrl, wxPickerBase)
42 IMPLEMENT_DYNAMIC_CLASS(wxColourPickerEvent, wxEvent)
43
44 // ----------------------------------------------------------------------------
45 // wxColourPickerCtrl
46 // ----------------------------------------------------------------------------
47
48 #define M_PICKER ((wxColourPickerWidget*)m_picker)
49
50 bool wxColourPickerCtrl::Create( wxWindow *parent, wxWindowID id,
51 const wxColour &col,
52 const wxPoint &pos, const wxSize &size,
53 long style, const wxValidator& validator,
54 const wxString &name )
55 {
56 if (!wxPickerBase::CreateBase(parent, id, col.GetAsString(), pos, size,
57 style, validator, name))
58 return false;
59
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),
66 NULL, this);
67
68 return true;
69 }
70
71 void wxColourPickerCtrl::SetColour(const wxColour &col)
72 {
73 M_PICKER->SetColour(col);
74 UpdateTextCtrlFromPicker();
75 }
76
77 bool wxColourPickerCtrl::SetColour(const wxString &text)
78 {
79 wxColour col(text); // smart wxString->wxColour conversion
80 if ( !col.Ok() )
81 return false;
82 M_PICKER->SetColour(col);
83 UpdateTextCtrlFromPicker();
84
85 return true;
86 }
87
88 void wxColourPickerCtrl::UpdatePickerFromTextCtrl()
89 {
90 wxASSERT(m_text);
91
92 if (m_bIgnoreNextTextCtrlUpdate)
93 {
94 // ignore this update
95 m_bIgnoreNextTextCtrlUpdate = false;
96 return;
97 }
98
99 // wxString -> wxColour conversion
100 wxColour col(m_text->GetValue());
101 if ( !col.Ok() )
102 return; // invalid user input
103
104 if (M_PICKER->GetColour() != col)
105 {
106 M_PICKER->SetColour(col);
107
108 // fire an event
109 wxColourPickerEvent event(this, GetId(), col);
110 GetEventHandler()->ProcessEvent(event);
111 }
112 }
113
114 void wxColourPickerCtrl::UpdateTextCtrlFromPicker()
115 {
116 if (!m_text)
117 return; // no textctrl to update
118
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());
124 }
125
126
127
128 // ----------------------------------------------------------------------------
129 // wxColourPickerCtrl - event handlers
130 // ----------------------------------------------------------------------------
131
132 void wxColourPickerCtrl::OnColourChange(wxColourPickerEvent &ev)
133 {
134 UpdateTextCtrlFromPicker();
135
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);
140 }
141
142 #endif // wxUSE_COLOURPICKERCTRL