]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checkbox.cpp
adding CoreText
[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
82// ----------------------------------------------------------------------------
2f259810 83// wxCheckBox creation
4438caf4
VZ
84// ----------------------------------------------------------------------------
85
2f259810 86void wxCheckBox::Init()
2bda0e17 87{
2f259810
VZ
88 m_state = wxCHK_UNCHECKED;
89 m_isPressed =
90 m_isHot = false;
2bda0e17
KB
91}
92
11b6a93b
VZ
93bool wxCheckBox::Create(wxWindow *parent,
94 wxWindowID id,
95 const wxString& label,
96 const wxPoint& pos,
97 const wxSize& size, long style,
98 const wxValidator& validator,
99 const wxString& name)
2bda0e17 100{
2f259810
VZ
101 Init();
102
f254e242 103 WXValidateStyle(&style);
787a85c2 104 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
02b7b6b0 105 return false;
4438caf4 106
687823a1
VZ
107 WXDWORD exstyle;
108 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
109
110 msStyle |= wxMSWButton::GetMultilineStyle(label);
111
112 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, exstyle);
113}
114
115WXDWORD wxCheckBox::MSWGetStyle(long style, WXDWORD *exstyle) const
116{
117 // buttons never have an external border, they draw their own one
118 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
8941fa88
VZ
119
120 if ( style & wxCHK_3STATE )
2f259810 121 msStyle |= BS_3STATE;
8941fa88 122 else
2f259810 123 msStyle |= BS_CHECKBOX;
8941fa88 124
4438caf4 125 if ( style & wxALIGN_RIGHT )
8941fa88 126 {
93212fee 127 msStyle |= BS_LEFTTEXT | BS_RIGHT;
8941fa88 128 }
4438caf4 129
687823a1 130 return msStyle;
2bda0e17
KB
131}
132
2f259810
VZ
133// ----------------------------------------------------------------------------
134// wxCheckBox geometry
135// ----------------------------------------------------------------------------
2bda0e17 136
e380ca3c 137wxSize wxCheckBox::DoGetBestClientSize() const
2bda0e17 138{
f6bcfd97 139 static int s_checkSize = 0;
81d66cf3 140
f6bcfd97
BP
141 if ( !s_checkSize )
142 {
143 wxScreenDC dc;
a756f210 144 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
2bda0e17 145
fb1a41cd 146 s_checkSize = dc.GetCharHeight();
f6bcfd97
BP
147 }
148
149 wxString str = wxGetWindowText(GetHWND());
150
151 int wCheckbox, hCheckbox;
ab1ce969 152 if ( !str.empty() )
4438caf4 153 {
5c33522f 154 wxClientDC dc(const_cast<wxCheckBox *>(this));
533171c2
VZ
155 dc.SetFont(GetFont());
156 dc.GetMultiLineTextExtent(GetLabelText(str), &wCheckbox, &hCheckbox);
f6bcfd97 157 wCheckbox += s_checkSize + GetCharWidth();
27fda0b6 158
35f92de3
VZ
159 if ( ::GetWindowLong(GetHwnd(), GWL_STYLE) & BS_MULTILINE )
160 {
161 // We need to make the checkbox even wider in this case because
162 // otherwise it wraps lines automatically and not only on "\n"s as
163 // we need and this makes the size computed here wrong resulting in
164 // checkbox contents being truncated when it's actually displayed.
165 // Without this hack simple checkbox with "Some thing\n and more"
166 // label appears on 3 lines, not 2, under Windows 2003 using
167 // classic look and feel (although it works fine under Windows 7,
168 // with or without themes).
169 wCheckbox += s_checkSize;
170 }
171
f6bcfd97
BP
172 if ( hCheckbox < s_checkSize )
173 hCheckbox = s_checkSize;
4438caf4
VZ
174 }
175 else
2bda0e17 176 {
f6bcfd97
BP
177 wCheckbox = s_checkSize;
178 hCheckbox = s_checkSize;
2bda0e17 179 }
5a6371b9
JS
180#ifdef __WXWINCE__
181 hCheckbox += 1;
182#endif
2bda0e17 183
31582e4e
RD
184 wxSize best(wCheckbox, hCheckbox);
185 CacheBestSize(best);
186 return best;
2bda0e17
KB
187}
188
2f259810
VZ
189// ----------------------------------------------------------------------------
190// wxCheckBox operations
191// ----------------------------------------------------------------------------
192
533171c2
VZ
193void wxCheckBox::SetLabel(const wxString& label)
194{
195 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label);
196
197 wxCheckBoxBase::SetLabel(label);
198}
199
debe6624 200void wxCheckBox::SetValue(bool val)
2bda0e17 201{
c3732409 202 Set3StateValue(val ? wxCHK_CHECKED : wxCHK_UNCHECKED);
2bda0e17
KB
203}
204
bfc6fde4 205bool wxCheckBox::GetValue() const
2bda0e17 206{
c3732409 207 return Get3StateValue() != wxCHK_UNCHECKED;
2bda0e17
KB
208}
209
2b5f62a0 210void wxCheckBox::Command(wxCommandEvent& event)
2bda0e17 211{
8941fa88
VZ
212 int state = event.GetInt();
213 wxCHECK_RET( (state == wxCHK_UNCHECKED) || (state == wxCHK_CHECKED)
214 || (state == wxCHK_UNDETERMINED),
215 wxT("event.GetInt() returned an invalid checkbox state") );
216
217 Set3StateValue((wxCheckBoxState) state);
2b5f62a0 218 ProcessCommand(event);
2bda0e17 219}
1e6feb95 220
8941fa88
VZ
221wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED == BST_UNCHECKED
222 && wxCHK_CHECKED == BST_CHECKED
223 && wxCHK_UNDETERMINED == BST_INDETERMINATE, EnumValuesIncorrect);
224
225void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
226{
2f259810
VZ
227 m_state = state;
228 if ( !IsOwnerDrawn() )
229 ::SendMessage(GetHwnd(), BM_SETCHECK, (WPARAM) state, 0);
230 else // owner drawn buttons don't react to this message
231 Refresh();
8941fa88
VZ
232}
233
234wxCheckBoxState wxCheckBox::DoGet3StateValue() const
235{
2f259810
VZ
236 return m_state;
237}
238
239bool wxCheckBox::MSWCommand(WXUINT cmd, WXWORD WXUNUSED(id))
240{
241 if ( cmd != BN_CLICKED && cmd != BN_DBLCLK )
242 return false;
243
244 // first update the value so that user event handler gets the new checkbox
245 // value
246
247 // ownerdrawn buttons don't manage their state themselves unlike usual
248 // auto checkboxes so do it ourselves in any case
249 wxCheckBoxState state;
250 if ( Is3rdStateAllowedForUser() )
251 {
252 state = (wxCheckBoxState)((m_state + 1) % 3);
253 }
254 else // 2 state checkbox (at least from users point of view)
255 {
256 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
257 state = m_state == wxCHK_UNCHECKED ? wxCHK_CHECKED : wxCHK_UNCHECKED;
258 }
259
260 DoSet3StateValue(state);
261
262
263 // generate the event
264 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId);
265
266 event.SetInt(state);
267 event.SetEventObject(this);
268 ProcessCommand(event);
269
270 return true;
271}
272
273// ----------------------------------------------------------------------------
274// owner drawn checkboxes stuff
275// ----------------------------------------------------------------------------
276
277bool wxCheckBox::SetForegroundColour(const wxColour& colour)
278{
279 if ( !wxCheckBoxBase::SetForegroundColour(colour) )
280 return false;
281
282 // the only way to change the checkbox foreground colour under Windows XP
283 // is to owner draw it
284 if ( wxUxThemeEngine::GetIfActive() )
9b9a7c33 285 MSWMakeOwnerDrawn(colour.IsOk());
2f259810
VZ
286
287 return true;
288}
289
290bool wxCheckBox::IsOwnerDrawn() const
291{
292 return
293 (::GetWindowLong(GetHwnd(), GWL_STYLE) & BS_OWNERDRAW) == BS_OWNERDRAW;
294}
295
9b9a7c33 296void wxCheckBox::MSWMakeOwnerDrawn(bool ownerDrawn)
2f259810
VZ
297{
298 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
299
300 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
301 // them as on independent style bits
302 if ( ownerDrawn )
303 {
304 style &= ~(BS_CHECKBOX | BS_3STATE);
305 style |= BS_OWNERDRAW;
306
307 Connect(wxEVT_ENTER_WINDOW,
308 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
309 Connect(wxEVT_LEAVE_WINDOW,
310 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
311 Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
312 Connect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
313 Connect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
314 Connect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
315 }
316 else // reset to default colour
317 {
318 style &= ~BS_OWNERDRAW;
319 style |= HasFlag(wxCHK_3STATE) ? BS_3STATE : BS_CHECKBOX;
320
321 Disconnect(wxEVT_ENTER_WINDOW,
322 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
323 Disconnect(wxEVT_LEAVE_WINDOW,
324 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
325 Disconnect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
326 Disconnect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
327 Disconnect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
328 Disconnect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
329 }
330
331 ::SetWindowLong(GetHwnd(), GWL_STYLE, style);
b3433ee7
VZ
332
333 if ( !ownerDrawn )
334 {
335 // ensure that controls state is consistent with internal state
336 DoSet3StateValue(m_state);
337 }
2f259810
VZ
338}
339
340void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent& event)
341{
342 m_isHot = event.GetEventType() == wxEVT_ENTER_WINDOW;
343 if ( !m_isHot )
344 m_isPressed = false;
345
346 Refresh();
347
348 event.Skip();
349}
350
351void wxCheckBox::OnMouseLeft(wxMouseEvent& event)
352{
353 // TODO: we should capture the mouse here to be notified about left up
354 // event but this interferes with BN_CLICKED generation so if we
355 // want to do this we'd need to generate them ourselves
356 m_isPressed = event.GetEventType() == wxEVT_LEFT_DOWN;
357 Refresh();
358
359 event.Skip();
360}
361
362void wxCheckBox::OnFocus(wxFocusEvent& event)
363{
364 Refresh();
365
366 event.Skip();
367}
368
369bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
370{
371 DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)item;
372
373 if ( !IsOwnerDrawn() || dis->CtlType != ODT_BUTTON )
374 return wxCheckBoxBase::MSWOnDraw(item);
375
376 // calculate the rectangles for the check mark itself and the label
377 HDC hdc = dis->hDC;
378 RECT& rect = dis->rcItem;
379 RECT rectCheck,
380 rectLabel;
f2e763d7
VZ
381 rectLabel.top = rect.top + (rect.bottom - rect.top - GetBestSize().y) / 2;
382 rectLabel.bottom = rectLabel.top + GetBestSize().y;
2f259810 383 const int MARGIN = 3;
f2e763d7
VZ
384 const int CXMENUCHECK = ::GetSystemMetrics(SM_CXMENUCHECK);
385 // the space between the checkbox and the label is included in the
386 // check-mark bitmap
387 const int checkSize = wxMin(CXMENUCHECK - MARGIN, GetSize().y);
388 rectCheck.top = rect.top + (rect.bottom - rect.top - checkSize) / 2;
389 rectCheck.bottom = rectCheck.top + checkSize;
2f259810
VZ
390
391 const bool isRightAligned = HasFlag(wxALIGN_RIGHT);
392 if ( isRightAligned )
393 {
f2e763d7 394 rectLabel.right = rect.right - CXMENUCHECK;
2f259810 395 rectLabel.left = rect.left;
f2e763d7
VZ
396
397 rectCheck.left = rectLabel.right + ( CXMENUCHECK + MARGIN - checkSize ) / 2;
398 rectCheck.right = rectCheck.left + checkSize;
2f259810
VZ
399 }
400 else // normal, left-aligned checkbox
401 {
f2e763d7 402 rectCheck.left = rect.left + ( CXMENUCHECK - MARGIN - checkSize ) / 2;
2f259810
VZ
403 rectCheck.right = rectCheck.left + checkSize;
404
f2e763d7 405 rectLabel.left = rect.left + CXMENUCHECK;
2f259810
VZ
406 rectLabel.right = rect.right;
407 }
408
f2e763d7 409 // shall we draw a focus rect?
2f259810
VZ
410 const bool isFocused = m_isPressed || FindFocus() == this;
411
412
d4adf63b
VZ
413 // draw the checkbox itself
414 wxDCTemp dc(hdc);
2f259810 415
d4adf63b 416 int flags = 0;
2f259810 417 if ( !IsEnabled() )
d4adf63b 418 flags |= wxCONTROL_DISABLED;
2f259810
VZ
419 switch ( Get3StateValue() )
420 {
421 case wxCHK_CHECKED:
d4adf63b 422 flags |= wxCONTROL_CHECKED;
2f259810
VZ
423 break;
424
425 case wxCHK_UNDETERMINED:
d4adf63b 426 flags |= wxCONTROL_PRESSED;
2f259810
VZ
427 break;
428
429 default:
9a83f860 430 wxFAIL_MSG( wxT("unexpected Get3StateValue() return value") );
2f259810
VZ
431 // fall through
432
433 case wxCHK_UNCHECKED:
434 // no extra styles needed
435 break;
436 }
437
438 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
d4adf63b 439 flags |= wxCONTROL_CURRENT;
2f259810 440
d4adf63b
VZ
441 wxRendererNative::Get().
442 DrawCheckBox(this, dc, wxRectFromRECT(rectCheck), flags);
2f259810
VZ
443
444 // draw the text
445 const wxString& label = GetLabel();
446
447 // first we need to measure it
448 UINT fmt = DT_NOCLIP;
449
450 // drawing underlying doesn't look well with focus rect (and the native
451 // control doesn't do it)
452 if ( isFocused )
453 fmt |= DT_HIDEPREFIX;
454 if ( isRightAligned )
455 fmt |= DT_RIGHT;
456 // TODO: also use DT_HIDEPREFIX if the system is configured so
457
458 // we need to get the label real size first if we have to draw a focus rect
459 // around it
460 if ( isFocused )
461 {
f2e763d7
VZ
462 RECT oldLabelRect = rectLabel; // needed if right aligned
463
017dc06b 464 if ( !::DrawText(hdc, label.t_str(), label.length(), &rectLabel,
2f259810
VZ
465 fmt | DT_CALCRECT) )
466 {
9a83f860 467 wxLogLastError(wxT("DrawText(DT_CALCRECT)"));
2f259810 468 }
f2e763d7
VZ
469
470 if ( isRightAligned )
471 {
472 // move the label rect to the right
473 const int labelWidth = rectLabel.right - rectLabel.left;
474 rectLabel.right = oldLabelRect.right;
475 rectLabel.left = rectLabel.right - labelWidth;
476 }
2f259810
VZ
477 }
478
479 if ( !IsEnabled() )
480 {
481 ::SetTextColor(hdc, ::GetSysColor(COLOR_GRAYTEXT));
482 }
483
017dc06b 484 if ( !::DrawText(hdc, label.t_str(), label.length(), &rectLabel, fmt) )
2f259810 485 {
9a83f860 486 wxLogLastError(wxT("DrawText()"));
2f259810
VZ
487 }
488
489 // finally draw the focus
490 if ( isFocused )
491 {
492 rectLabel.left--;
493 rectLabel.right++;
494 if ( !::DrawFocusRect(hdc, &rectLabel) )
495 {
9a83f860 496 wxLogLastError(wxT("DrawFocusRect()"));
2f259810
VZ
497 }
498 }
499
500 return true;
8941fa88
VZ
501}
502
1e6feb95 503#endif // wxUSE_CHECKBOX