]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checkbox.cpp
Eliminated WX_PG_DECLARE/IMPLEMENT_DERIVED_PROPERTY_CLASS
[wxWidgets.git] / src / msw / checkbox.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
ab1ce969 2// Name: src/msw/checkbox.cpp
2bda0e17
KB
3// Purpose: wxCheckBox
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
4438caf4
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
4438caf4 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
1e6feb95
VZ
27#if wxUSE_CHECKBOX
28
ab1ce969
WS
29#include "wx/checkbox.h"
30
2bda0e17 31#ifndef WX_PRECOMP
4438caf4 32 #include "wx/brush.h"
6c2cd2a2 33 #include "wx/dcclient.h"
f6bcfd97
BP
34 #include "wx/dcscreen.h"
35 #include "wx/settings.h"
2bda0e17
KB
36#endif
37
025f7d77 38#include "wx/msw/dc.h" // for wxDCTemp
d4adf63b 39#include "wx/renderer.h"
533171c2
VZ
40#include "wx/msw/uxtheme.h"
41#include "wx/msw/private/button.h"
2bda0e17 42
2f259810
VZ
43// ----------------------------------------------------------------------------
44// constants
45// ----------------------------------------------------------------------------
46
2f259810
VZ
47#ifndef BP_CHECKBOX
48 #define BP_CHECKBOX 3
49#endif
50
51// these values are defined in tmschema.h (except the first one)
52enum
53{
54 CBS_INVALID,
55 CBS_UNCHECKEDNORMAL,
56 CBS_UNCHECKEDHOT,
57 CBS_UNCHECKEDPRESSED,
58 CBS_UNCHECKEDDISABLED,
59 CBS_CHECKEDNORMAL,
60 CBS_CHECKEDHOT,
61 CBS_CHECKEDPRESSED,
62 CBS_CHECKEDDISABLED,
63 CBS_MIXEDNORMAL,
64 CBS_MIXEDHOT,
65 CBS_MIXEDPRESSED,
66 CBS_MIXEDDISABLED
67};
68
69// these are our own
70enum
71{
72 CBS_HOT_OFFSET = 1,
73 CBS_PRESSED_OFFSET = 2,
74 CBS_DISABLED_OFFSET = 3
75};
76
4438caf4
VZ
77// ============================================================================
78// implementation
79// ============================================================================
80
51741307 81#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
82WX_DEFINE_FLAGS( wxCheckBoxStyle )
83
3ff066a4 84wxBEGIN_FLAGS( wxCheckBoxStyle )
bc9fb572
JS
85 // new style border flags, we put them first to
86 // use them for streaming out
3ff066a4
SC
87 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
88 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
89 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
90 wxFLAGS_MEMBER(wxBORDER_RAISED)
91 wxFLAGS_MEMBER(wxBORDER_STATIC)
92 wxFLAGS_MEMBER(wxBORDER_NONE)
02b7b6b0 93
bc9fb572 94 // old style border flags
3ff066a4
SC
95 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
96 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
97 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
98 wxFLAGS_MEMBER(wxRAISED_BORDER)
99 wxFLAGS_MEMBER(wxSTATIC_BORDER)
8941fa88 100 wxFLAGS_MEMBER(wxNO_BORDER)
bc9fb572
JS
101
102 // standard window styles
3ff066a4
SC
103 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
104 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
105 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
106 wxFLAGS_MEMBER(wxWANTS_CHARS)
8941fa88 107 wxFLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE)
3ff066a4
SC
108 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
109 wxFLAGS_MEMBER(wxVSCROLL)
110 wxFLAGS_MEMBER(wxHSCROLL)
bc9fb572 111
3ff066a4 112wxEND_FLAGS( wxCheckBoxStyle )
bc9fb572 113
51741307
SC
114IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckBox, wxControl,"wx/checkbox.h")
115
3ff066a4 116wxBEGIN_PROPERTIES_TABLE(wxCheckBox)
93212fee 117 wxEVENT_PROPERTY( Click , wxEVT_COMMAND_CHECKBOX_CLICKED , wxCommandEvent )
51741307 118
93212fee
VZ
119 wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
120 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
121 wxPROPERTY( Value ,bool, SetValue, GetValue, EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
af498247 122 wxPROPERTY_FLAGS( WindowStyle , wxCheckBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 123wxEND_PROPERTIES_TABLE()
2b5f62a0 124
3ff066a4
SC
125wxBEGIN_HANDLERS_TABLE(wxCheckBox)
126wxEND_HANDLERS_TABLE()
51741307 127
02b7b6b0 128wxCONSTRUCTOR_6( wxCheckBox , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle )
51741307
SC
129#else
130IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
131#endif
066f1b7a 132
066f1b7a 133
4438caf4 134// ----------------------------------------------------------------------------
2f259810 135// wxCheckBox creation
4438caf4
VZ
136// ----------------------------------------------------------------------------
137
2f259810 138void wxCheckBox::Init()
2bda0e17 139{
2f259810
VZ
140 m_state = wxCHK_UNCHECKED;
141 m_isPressed =
142 m_isHot = false;
2bda0e17
KB
143}
144
11b6a93b
VZ
145bool wxCheckBox::Create(wxWindow *parent,
146 wxWindowID id,
147 const wxString& label,
148 const wxPoint& pos,
149 const wxSize& size, long style,
150 const wxValidator& validator,
151 const wxString& name)
2bda0e17 152{
2f259810
VZ
153 Init();
154
787a85c2 155 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
02b7b6b0 156 return false;
4438caf4 157
8941fa88
VZ
158 long msStyle = WS_TABSTOP;
159
160 if ( style & wxCHK_3STATE )
161 {
2f259810 162 msStyle |= BS_3STATE;
8941fa88
VZ
163 }
164 else
165 {
166 wxASSERT_MSG( !Is3rdStateAllowedForUser(),
167 wxT("Using wxCH_ALLOW_3RD_STATE_FOR_USER")
168 wxT(" style flag for a 2-state checkbox is useless") );
2f259810 169 msStyle |= BS_CHECKBOX;
8941fa88
VZ
170 }
171
4438caf4 172 if ( style & wxALIGN_RIGHT )
8941fa88 173 {
93212fee 174 msStyle |= BS_LEFTTEXT | BS_RIGHT;
8941fa88 175 }
4438caf4 176
533171c2
VZ
177 msStyle |= wxMSWButton::GetMultilineStyle(label);
178
8912d7eb 179 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, 0);
2bda0e17
KB
180}
181
2f259810
VZ
182// ----------------------------------------------------------------------------
183// wxCheckBox geometry
184// ----------------------------------------------------------------------------
2bda0e17 185
f68586e5 186wxSize wxCheckBox::DoGetBestSize() const
2bda0e17 187{
f6bcfd97 188 static int s_checkSize = 0;
81d66cf3 189
f6bcfd97
BP
190 if ( !s_checkSize )
191 {
192 wxScreenDC dc;
a756f210 193 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
2bda0e17 194
fb1a41cd 195 s_checkSize = dc.GetCharHeight();
f6bcfd97
BP
196 }
197
198 wxString str = wxGetWindowText(GetHWND());
199
200 int wCheckbox, hCheckbox;
ab1ce969 201 if ( !str.empty() )
4438caf4 202 {
533171c2
VZ
203 wxClientDC dc(wx_const_cast(wxCheckBox *, this));
204 dc.SetFont(GetFont());
205 dc.GetMultiLineTextExtent(GetLabelText(str), &wCheckbox, &hCheckbox);
f6bcfd97 206 wCheckbox += s_checkSize + GetCharWidth();
27fda0b6 207
f6bcfd97
BP
208 if ( hCheckbox < s_checkSize )
209 hCheckbox = s_checkSize;
4438caf4
VZ
210 }
211 else
2bda0e17 212 {
f6bcfd97
BP
213 wCheckbox = s_checkSize;
214 hCheckbox = s_checkSize;
2bda0e17 215 }
5a6371b9
JS
216#ifdef __WXWINCE__
217 hCheckbox += 1;
218#endif
2bda0e17 219
31582e4e
RD
220 wxSize best(wCheckbox, hCheckbox);
221 CacheBestSize(best);
222 return best;
2bda0e17
KB
223}
224
2f259810
VZ
225// ----------------------------------------------------------------------------
226// wxCheckBox operations
227// ----------------------------------------------------------------------------
228
533171c2
VZ
229void wxCheckBox::SetLabel(const wxString& label)
230{
231 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label);
232
233 wxCheckBoxBase::SetLabel(label);
234}
235
debe6624 236void wxCheckBox::SetValue(bool val)
2bda0e17 237{
c3732409 238 Set3StateValue(val ? wxCHK_CHECKED : wxCHK_UNCHECKED);
2bda0e17
KB
239}
240
bfc6fde4 241bool wxCheckBox::GetValue() const
2bda0e17 242{
c3732409 243 return Get3StateValue() != wxCHK_UNCHECKED;
2bda0e17
KB
244}
245
2b5f62a0 246void wxCheckBox::Command(wxCommandEvent& event)
2bda0e17 247{
8941fa88
VZ
248 int state = event.GetInt();
249 wxCHECK_RET( (state == wxCHK_UNCHECKED) || (state == wxCHK_CHECKED)
250 || (state == wxCHK_UNDETERMINED),
251 wxT("event.GetInt() returned an invalid checkbox state") );
252
253 Set3StateValue((wxCheckBoxState) state);
2b5f62a0 254 ProcessCommand(event);
2bda0e17 255}
1e6feb95 256
8941fa88
VZ
257wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED == BST_UNCHECKED
258 && wxCHK_CHECKED == BST_CHECKED
259 && wxCHK_UNDETERMINED == BST_INDETERMINATE, EnumValuesIncorrect);
260
261void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
262{
2f259810
VZ
263 m_state = state;
264 if ( !IsOwnerDrawn() )
265 ::SendMessage(GetHwnd(), BM_SETCHECK, (WPARAM) state, 0);
266 else // owner drawn buttons don't react to this message
267 Refresh();
8941fa88
VZ
268}
269
270wxCheckBoxState wxCheckBox::DoGet3StateValue() const
271{
2f259810
VZ
272 return m_state;
273}
274
275bool wxCheckBox::MSWCommand(WXUINT cmd, WXWORD WXUNUSED(id))
276{
277 if ( cmd != BN_CLICKED && cmd != BN_DBLCLK )
278 return false;
279
280 // first update the value so that user event handler gets the new checkbox
281 // value
282
283 // ownerdrawn buttons don't manage their state themselves unlike usual
284 // auto checkboxes so do it ourselves in any case
285 wxCheckBoxState state;
286 if ( Is3rdStateAllowedForUser() )
287 {
288 state = (wxCheckBoxState)((m_state + 1) % 3);
289 }
290 else // 2 state checkbox (at least from users point of view)
291 {
292 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
293 state = m_state == wxCHK_UNCHECKED ? wxCHK_CHECKED : wxCHK_UNCHECKED;
294 }
295
296 DoSet3StateValue(state);
297
298
299 // generate the event
300 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId);
301
302 event.SetInt(state);
303 event.SetEventObject(this);
304 ProcessCommand(event);
305
306 return true;
307}
308
309// ----------------------------------------------------------------------------
310// owner drawn checkboxes stuff
311// ----------------------------------------------------------------------------
312
313bool wxCheckBox::SetForegroundColour(const wxColour& colour)
314{
315 if ( !wxCheckBoxBase::SetForegroundColour(colour) )
316 return false;
317
318 // the only way to change the checkbox foreground colour under Windows XP
319 // is to owner draw it
320 if ( wxUxThemeEngine::GetIfActive() )
321 MakeOwnerDrawn(colour.Ok());
322
323 return true;
324}
325
326bool wxCheckBox::IsOwnerDrawn() const
327{
328 return
329 (::GetWindowLong(GetHwnd(), GWL_STYLE) & BS_OWNERDRAW) == BS_OWNERDRAW;
330}
331
332void wxCheckBox::MakeOwnerDrawn(bool ownerDrawn)
333{
334 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
335
336 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
337 // them as on independent style bits
338 if ( ownerDrawn )
339 {
340 style &= ~(BS_CHECKBOX | BS_3STATE);
341 style |= BS_OWNERDRAW;
342
343 Connect(wxEVT_ENTER_WINDOW,
344 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
345 Connect(wxEVT_LEAVE_WINDOW,
346 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
347 Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
348 Connect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
349 Connect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
350 Connect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
351 }
352 else // reset to default colour
353 {
354 style &= ~BS_OWNERDRAW;
355 style |= HasFlag(wxCHK_3STATE) ? BS_3STATE : BS_CHECKBOX;
356
357 Disconnect(wxEVT_ENTER_WINDOW,
358 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
359 Disconnect(wxEVT_LEAVE_WINDOW,
360 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
361 Disconnect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
362 Disconnect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
363 Disconnect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
364 Disconnect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
365 }
366
367 ::SetWindowLong(GetHwnd(), GWL_STYLE, style);
b3433ee7
VZ
368
369 if ( !ownerDrawn )
370 {
371 // ensure that controls state is consistent with internal state
372 DoSet3StateValue(m_state);
373 }
2f259810
VZ
374}
375
376void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent& event)
377{
378 m_isHot = event.GetEventType() == wxEVT_ENTER_WINDOW;
379 if ( !m_isHot )
380 m_isPressed = false;
381
382 Refresh();
383
384 event.Skip();
385}
386
387void wxCheckBox::OnMouseLeft(wxMouseEvent& event)
388{
389 // TODO: we should capture the mouse here to be notified about left up
390 // event but this interferes with BN_CLICKED generation so if we
391 // want to do this we'd need to generate them ourselves
392 m_isPressed = event.GetEventType() == wxEVT_LEFT_DOWN;
393 Refresh();
394
395 event.Skip();
396}
397
398void wxCheckBox::OnFocus(wxFocusEvent& event)
399{
400 Refresh();
401
402 event.Skip();
403}
404
405bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
406{
407 DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)item;
408
409 if ( !IsOwnerDrawn() || dis->CtlType != ODT_BUTTON )
410 return wxCheckBoxBase::MSWOnDraw(item);
411
412 // calculate the rectangles for the check mark itself and the label
413 HDC hdc = dis->hDC;
414 RECT& rect = dis->rcItem;
415 RECT rectCheck,
416 rectLabel;
417 rectCheck.top =
418 rectLabel.top = rect.top;
419 rectCheck.bottom =
420 rectLabel.bottom = rect.bottom;
421 const int checkSize = GetBestSize().y;
422 const int MARGIN = 3;
423
424 const bool isRightAligned = HasFlag(wxALIGN_RIGHT);
425 if ( isRightAligned )
426 {
427 rectCheck.right = rect.right;
428 rectCheck.left = rectCheck.right - checkSize;
429
430 rectLabel.right = rectCheck.left - MARGIN;
431 rectLabel.left = rect.left;
432 }
433 else // normal, left-aligned checkbox
434 {
435 rectCheck.left = rect.left;
436 rectCheck.right = rectCheck.left + checkSize;
437
438 rectLabel.left = rectCheck.right + MARGIN;
439 rectLabel.right = rect.right;
440 }
441
442 // show we draw a focus rect?
443 const bool isFocused = m_isPressed || FindFocus() == this;
444
445
d4adf63b
VZ
446 // draw the checkbox itself
447 wxDCTemp dc(hdc);
2f259810 448
d4adf63b 449 int flags = 0;
2f259810 450 if ( !IsEnabled() )
d4adf63b 451 flags |= wxCONTROL_DISABLED;
2f259810
VZ
452 switch ( Get3StateValue() )
453 {
454 case wxCHK_CHECKED:
d4adf63b 455 flags |= wxCONTROL_CHECKED;
2f259810
VZ
456 break;
457
458 case wxCHK_UNDETERMINED:
d4adf63b 459 flags |= wxCONTROL_PRESSED;
2f259810
VZ
460 break;
461
462 default:
463 wxFAIL_MSG( _T("unexpected Get3StateValue() return value") );
464 // fall through
465
466 case wxCHK_UNCHECKED:
467 // no extra styles needed
468 break;
469 }
470
471 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
d4adf63b 472 flags |= wxCONTROL_CURRENT;
2f259810 473
d4adf63b
VZ
474 wxRendererNative::Get().
475 DrawCheckBox(this, dc, wxRectFromRECT(rectCheck), flags);
2f259810
VZ
476
477 // draw the text
478 const wxString& label = GetLabel();
479
480 // first we need to measure it
481 UINT fmt = DT_NOCLIP;
482
483 // drawing underlying doesn't look well with focus rect (and the native
484 // control doesn't do it)
485 if ( isFocused )
486 fmt |= DT_HIDEPREFIX;
487 if ( isRightAligned )
488 fmt |= DT_RIGHT;
489 // TODO: also use DT_HIDEPREFIX if the system is configured so
490
491 // we need to get the label real size first if we have to draw a focus rect
492 // around it
493 if ( isFocused )
494 {
e0a050e3 495 if ( !::DrawText(hdc, label.wx_str(), label.length(), &rectLabel,
2f259810
VZ
496 fmt | DT_CALCRECT) )
497 {
498 wxLogLastError(_T("DrawText(DT_CALCRECT)"));
499 }
500 }
501
502 if ( !IsEnabled() )
503 {
504 ::SetTextColor(hdc, ::GetSysColor(COLOR_GRAYTEXT));
505 }
506
e0a050e3 507 if ( !::DrawText(hdc, label.wx_str(), label.length(), &rectLabel, fmt) )
2f259810
VZ
508 {
509 wxLogLastError(_T("DrawText()"));
510 }
511
512 // finally draw the focus
513 if ( isFocused )
514 {
515 rectLabel.left--;
516 rectLabel.right++;
517 if ( !::DrawFocusRect(hdc, &rectLabel) )
518 {
519 wxLogLastError(_T("DrawFocusRect()"));
520 }
521 }
522
523 return true;
8941fa88
VZ
524}
525
1e6feb95 526#endif // wxUSE_CHECKBOX