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