use wxBoxSizer in wxPickerBase instead of doing the layout manually + other picker...
[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
65 // complete sizer creation
66 wxPickerBase::PostCreation();
67
68 m_picker->Connect(wxEVT_COMMAND_COLOURPICKER_CHANGED,
69 wxColourPickerEventHandler(wxColourPickerCtrl::OnColourChange),
70 NULL, this);
71
72 return true;
73 }
74
75 void wxColourPickerCtrl::SetColour(const wxColour &col)
76 {
77 M_PICKER->SetColour(col);
78 UpdateTextCtrlFromPicker();
79 }
80
81 bool wxColourPickerCtrl::SetColour(const wxString &text)
82 {
83 wxColour col(text); // smart wxString->wxColour conversion
84 if ( !col.Ok() )
85 return false;
86 M_PICKER->SetColour(col);
87 UpdateTextCtrlFromPicker();
88
89 return true;
90 }
91
92 void wxColourPickerCtrl::UpdatePickerFromTextCtrl()
93 {
94 wxASSERT(m_text);
95
96 if (m_bIgnoreNextTextCtrlUpdate)
97 {
98 // ignore this update
99 m_bIgnoreNextTextCtrlUpdate = false;
100 return;
101 }
102
103 // wxString -> wxColour conversion
104 wxColour col(m_text->GetValue());
105 if ( !col.Ok() )
106 return; // invalid user input
107
108 if (M_PICKER->GetColour() != col)
109 {
110 M_PICKER->SetColour(col);
111
112 // fire an event
113 wxColourPickerEvent event(this, GetId(), col);
114 GetEventHandler()->ProcessEvent(event);
115 }
116 }
117
118 void wxColourPickerCtrl::UpdateTextCtrlFromPicker()
119 {
120 if (!m_text)
121 return; // no textctrl to update
122
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());
128 }
129
130
131
132 // ----------------------------------------------------------------------------
133 // wxColourPickerCtrl - event handlers
134 // ----------------------------------------------------------------------------
135
136 void wxColourPickerCtrl::OnColourChange(wxColourPickerEvent &ev)
137 {
138 UpdateTextCtrlFromPicker();
139
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);
144 }
145
146 #endif // wxUSE_COLOURPICKERCTRL