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