]> git.saurik.com Git - wxWidgets.git/blob - src/univ/checkbox.cpp
Updated to new PyCrust
[wxWidgets.git] / src / univ / checkbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: univ/checkbox.cpp
3 // Purpose: wxCheckBox implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 25.08.00
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "univcheckbox.h"
22 #endif
23
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 #if wxUSE_CHECKBOX
31
32 #ifndef WX_PRECOMP
33 #include "wx/dcclient.h"
34 #include "wx/checkbox.h"
35 #include "wx/validate.h"
36
37 #include "wx/button.h" // for wxACTION_BUTTON_XXX
38 #endif
39
40 #include "wx/univ/theme.h"
41 #include "wx/univ/renderer.h"
42 #include "wx/univ/inphand.h"
43 #include "wx/univ/colschem.h"
44
45 // ============================================================================
46 // implementation
47 // ============================================================================
48
49 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
50
51 // ----------------------------------------------------------------------------
52 // wxCheckBox
53 // ----------------------------------------------------------------------------
54
55 void wxCheckBox::Init()
56 {
57 m_isPressed = FALSE;
58 m_status = Status_Unchecked;
59 }
60
61 bool wxCheckBox::Create(wxWindow *parent,
62 wxWindowID id,
63 const wxString &label,
64 const wxPoint &pos,
65 const wxSize &size,
66 long style,
67 const wxValidator& validator,
68 const wxString &name)
69 {
70 if ( !wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name) )
71 return FALSE;
72
73 SetLabel(label);
74 SetBestSize(size);
75
76 CreateInputHandler(wxINP_HANDLER_CHECKBOX);
77
78 return TRUE;
79 }
80
81 // ----------------------------------------------------------------------------
82 // checkbox interface
83 // ----------------------------------------------------------------------------
84
85 bool wxCheckBox::GetValue() const
86 {
87 return m_status == Status_Checked;
88 }
89
90 void wxCheckBox::SetValue(bool value)
91 {
92 Status status = value ? Status_Checked : Status_Unchecked;
93 if ( status != m_status )
94 {
95 m_status = status;
96
97 if ( m_status == Status_Checked )
98 {
99 // invoke the hook
100 OnCheck();
101 }
102
103 Refresh();
104 }
105 }
106
107 void wxCheckBox::OnCheck()
108 {
109 // we do nothing here
110 }
111
112 // ----------------------------------------------------------------------------
113 // indicator bitmaps
114 // ----------------------------------------------------------------------------
115
116 wxBitmap wxCheckBox::GetBitmap(State state, Status status) const
117 {
118 wxBitmap bmp = m_bitmaps[state][status];
119 if ( !bmp.Ok() )
120 bmp = m_bitmaps[State_Normal][status];
121
122 return bmp;
123 }
124
125 void wxCheckBox::SetBitmap(const wxBitmap& bmp, State state, Status status)
126 {
127 m_bitmaps[state][status] = bmp;
128 }
129
130 // ----------------------------------------------------------------------------
131 // drawing
132 // ----------------------------------------------------------------------------
133
134 wxCheckBox::State wxCheckBox::GetState(int flags) const
135 {
136 if ( flags & wxCONTROL_DISABLED )
137 return State_Disabled;
138 else if ( flags & wxCONTROL_PRESSED )
139 return State_Pressed;
140 else if ( flags & wxCONTROL_CURRENT )
141 return State_Current;
142 else
143 return State_Normal;
144 }
145
146 void wxCheckBox::DoDraw(wxControlRenderer *renderer)
147 {
148 int flags = GetStateFlags();
149
150 wxDC& dc = renderer->GetDC();
151 dc.SetFont(GetFont());
152 dc.SetTextForeground(GetForegroundColour());
153
154 if ( m_status == Status_Checked )
155 flags |= wxCONTROL_CHECKED;
156
157 renderer->GetRenderer()->
158 DrawCheckButton(dc,
159 GetLabel(),
160 GetBitmap(GetState(flags), m_status),
161 renderer->GetRect(),
162 flags,
163 GetWindowStyle() & wxALIGN_RIGHT ? wxALIGN_RIGHT
164 : wxALIGN_LEFT,
165 GetAccelIndex());
166 }
167
168 // ----------------------------------------------------------------------------
169 // geometry calculations
170 // ----------------------------------------------------------------------------
171
172 wxSize wxCheckBox::GetBitmapSize() const
173 {
174 wxBitmap bmp = GetBitmap(State_Normal, Status_Checked);
175 return bmp.Ok() ? wxSize(bmp.GetWidth(), bmp.GetHeight())
176 : GetRenderer()->GetCheckBitmapSize();
177 }
178
179 wxSize wxCheckBox::DoGetBestClientSize() const
180 {
181 wxClientDC dc(wxConstCast(this, wxCheckBox));
182 dc.SetFont(GetFont());
183 wxCoord width, height;
184 dc.GetMultiLineTextExtent(GetLabel(), &width, &height);
185
186 wxSize sizeBmp = GetBitmapSize();
187 if ( height < sizeBmp.y )
188 height = sizeBmp.y;
189
190 #if wxUNIV_COMPATIBLE_MSW
191 // this looks better but is different from what wxMSW does
192 height += GetCharHeight()/2;
193 #endif // wxUNIV_COMPATIBLE_MSW
194
195 width += sizeBmp.x + 2*GetCharWidth();
196
197 return wxSize(width, height);
198 }
199
200 // ----------------------------------------------------------------------------
201 // checkbox actions
202 // ----------------------------------------------------------------------------
203
204 void wxCheckBox::Press()
205 {
206 if ( !m_isPressed )
207 {
208 m_isPressed = TRUE;
209
210 Refresh();
211 }
212 }
213
214 void wxCheckBox::Release()
215 {
216 if ( m_isPressed )
217 {
218 m_isPressed = FALSE;
219
220 Refresh();
221 }
222 }
223
224 void wxCheckBox::Toggle()
225 {
226 m_isPressed = FALSE;
227
228 ChangeValue(!GetValue());
229 }
230
231 void wxCheckBox::ChangeValue(bool value)
232 {
233 SetValue(value);
234
235 SendEvent();
236 }
237
238 void wxCheckBox::SendEvent()
239 {
240 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, GetId());
241 InitCommandEvent(event);
242 event.SetInt(IsChecked());
243 Command(event);
244 }
245
246 // ----------------------------------------------------------------------------
247 // input handling
248 // ----------------------------------------------------------------------------
249
250 bool wxCheckBox::PerformAction(const wxControlAction& action,
251 long numArg,
252 const wxString& strArg)
253 {
254 if ( action == wxACTION_BUTTON_PRESS )
255 Press();
256 else if ( action == wxACTION_BUTTON_RELEASE )
257 Release();
258 if ( action == wxACTION_CHECKBOX_CHECK )
259 ChangeValue(TRUE);
260 else if ( action == wxACTION_CHECKBOX_CLEAR )
261 ChangeValue(FALSE);
262 else if ( action == wxACTION_CHECKBOX_TOGGLE )
263 Toggle();
264 else
265 return wxControl::PerformAction(action, numArg, strArg);
266
267 return TRUE;
268 }
269
270 // ----------------------------------------------------------------------------
271 // wxStdCheckboxInputHandler
272 // ----------------------------------------------------------------------------
273
274 wxStdCheckboxInputHandler::wxStdCheckboxInputHandler(wxInputHandler *inphand)
275 : wxStdButtonInputHandler(inphand)
276 {
277 }
278
279 bool wxStdCheckboxInputHandler::HandleActivation(wxInputConsumer *consumer,
280 bool activated)
281 {
282 // only the focused checkbox appearance changes when the app gains/loses
283 // activation
284 return consumer->GetInputWindow()->IsFocused();
285 }
286
287 #endif // wxUSE_CHECKBOX