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