Applied patch [ 832096 ] Final separation for GUI and console for Open Watcom
[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 licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 wxCheckBoxBase::wxCheckBoxBase()
56 {
57 }
58
59 wxCheckBox::wxCheckBox()
60 {
61 Init();
62 }
63
64 wxCheckBox::wxCheckBox(wxWindow *parent,
65 wxWindowID id,
66 const wxString& label,
67 const wxPoint& pos,
68 const wxSize& size,
69 long style,
70 const wxValidator& validator,
71 const wxString& name)
72 {
73 Init();
74
75 Create(parent, id, label, pos, size, style, validator, name);
76 }
77
78 void wxCheckBox::Init()
79 {
80 m_isPressed = FALSE;
81 m_status = Status_Unchecked;
82 }
83
84 bool wxCheckBox::Create(wxWindow *parent,
85 wxWindowID id,
86 const wxString &label,
87 const wxPoint &pos,
88 const wxSize &size,
89 long style,
90 const wxValidator& validator,
91 const wxString &name)
92 {
93 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
94 return FALSE;
95
96 SetLabel(label);
97 SetBestSize(size);
98
99 CreateInputHandler(wxINP_HANDLER_CHECKBOX);
100
101 return TRUE;
102 }
103
104 // ----------------------------------------------------------------------------
105 // checkbox interface
106 // ----------------------------------------------------------------------------
107
108 bool wxCheckBox::GetValue() const
109 {
110 return m_status == Status_Checked;
111 }
112
113 void wxCheckBox::SetValue(bool value)
114 {
115 Status status = value ? Status_Checked : Status_Unchecked;
116 if ( status != m_status )
117 {
118 m_status = status;
119
120 if ( m_status == Status_Checked )
121 {
122 // invoke the hook
123 OnCheck();
124 }
125
126 Refresh();
127 }
128 }
129
130 void wxCheckBox::OnCheck()
131 {
132 // we do nothing here
133 }
134
135 // ----------------------------------------------------------------------------
136 // indicator bitmaps
137 // ----------------------------------------------------------------------------
138
139 wxBitmap wxCheckBox::GetBitmap(State state, Status status) const
140 {
141 wxBitmap bmp = m_bitmaps[state][status];
142 if ( !bmp.Ok() )
143 bmp = m_bitmaps[State_Normal][status];
144
145 return bmp;
146 }
147
148 void wxCheckBox::SetBitmap(const wxBitmap& bmp, State state, Status status)
149 {
150 m_bitmaps[state][status] = bmp;
151 }
152
153 // ----------------------------------------------------------------------------
154 // drawing
155 // ----------------------------------------------------------------------------
156
157 wxCheckBox::State wxCheckBox::GetState(int flags) const
158 {
159 if ( flags & wxCONTROL_DISABLED )
160 return State_Disabled;
161 else if ( flags & wxCONTROL_PRESSED )
162 return State_Pressed;
163 else if ( flags & wxCONTROL_CURRENT )
164 return State_Current;
165 else
166 return State_Normal;
167 }
168
169 void wxCheckBox::DoDraw(wxControlRenderer *renderer)
170 {
171 int flags = GetStateFlags();
172
173 wxDC& dc = renderer->GetDC();
174 dc.SetFont(GetFont());
175 dc.SetTextForeground(GetForegroundColour());
176
177 if ( m_status == Status_Checked )
178 flags |= wxCONTROL_CHECKED;
179
180 wxBitmap bitmap(GetBitmap(GetState(flags), m_status));
181
182 renderer->GetRenderer()->
183 DrawCheckButton(dc,
184 GetLabel(),
185 bitmap,
186 renderer->GetRect(),
187 flags,
188 GetWindowStyle() & wxALIGN_RIGHT ? wxALIGN_RIGHT
189 : wxALIGN_LEFT,
190 GetAccelIndex());
191 }
192
193 // ----------------------------------------------------------------------------
194 // geometry calculations
195 // ----------------------------------------------------------------------------
196
197 wxSize wxCheckBox::GetBitmapSize() const
198 {
199 wxBitmap bmp = GetBitmap(State_Normal, Status_Checked);
200 return bmp.Ok() ? wxSize(bmp.GetWidth(), bmp.GetHeight())
201 : GetRenderer()->GetCheckBitmapSize();
202 }
203
204 wxSize wxCheckBox::DoGetBestClientSize() const
205 {
206 wxClientDC dc(wxConstCast(this, wxCheckBox));
207 dc.SetFont(GetFont());
208 wxCoord width, height;
209 dc.GetMultiLineTextExtent(GetLabel(), &width, &height);
210
211 wxSize sizeBmp = GetBitmapSize();
212 if ( height < sizeBmp.y )
213 height = sizeBmp.y;
214
215 #if wxUNIV_COMPATIBLE_MSW
216 // this looks better but is different from what wxMSW does
217 height += GetCharHeight()/2;
218 #endif // wxUNIV_COMPATIBLE_MSW
219
220 width += sizeBmp.x + 2*GetCharWidth();
221
222 return wxSize(width, height);
223 }
224
225 // ----------------------------------------------------------------------------
226 // checkbox actions
227 // ----------------------------------------------------------------------------
228
229 void wxCheckBox::Press()
230 {
231 if ( !m_isPressed )
232 {
233 m_isPressed = TRUE;
234
235 Refresh();
236 }
237 }
238
239 void wxCheckBox::Release()
240 {
241 if ( m_isPressed )
242 {
243 m_isPressed = FALSE;
244
245 Refresh();
246 }
247 }
248
249 void wxCheckBox::Toggle()
250 {
251 m_isPressed = FALSE;
252
253 ChangeValue(!GetValue());
254 }
255
256 void wxCheckBox::ChangeValue(bool value)
257 {
258 SetValue(value);
259
260 SendEvent();
261 }
262
263 void wxCheckBox::SendEvent()
264 {
265 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, GetId());
266 InitCommandEvent(event);
267 event.SetInt(IsChecked());
268 Command(event);
269 }
270
271 // ----------------------------------------------------------------------------
272 // input handling
273 // ----------------------------------------------------------------------------
274
275 bool wxCheckBox::PerformAction(const wxControlAction& action,
276 long numArg,
277 const wxString& strArg)
278 {
279 if ( action == wxACTION_BUTTON_PRESS )
280 Press();
281 else if ( action == wxACTION_BUTTON_RELEASE )
282 Release();
283 if ( action == wxACTION_CHECKBOX_CHECK )
284 ChangeValue(TRUE);
285 else if ( action == wxACTION_CHECKBOX_CLEAR )
286 ChangeValue(FALSE);
287 else if ( action == wxACTION_CHECKBOX_TOGGLE )
288 Toggle();
289 else
290 return wxControl::PerformAction(action, numArg, strArg);
291
292 return TRUE;
293 }
294
295 // ----------------------------------------------------------------------------
296 // wxStdCheckboxInputHandler
297 // ----------------------------------------------------------------------------
298
299 wxStdCheckboxInputHandler::wxStdCheckboxInputHandler(wxInputHandler *inphand)
300 : wxStdButtonInputHandler(inphand)
301 {
302 }
303
304 bool wxStdCheckboxInputHandler::HandleActivation(wxInputConsumer *consumer,
305 bool WXUNUSED(activated))
306 {
307 // only the focused checkbox appearance changes when the app gains/loses
308 // activation
309 return consumer->GetInputWindow()->IsFocused();
310 }
311
312 #endif // wxUSE_CHECKBOX