]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checkbox.cpp
non-pch build fix
[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;
381 rectCheck.top =
382 rectLabel.top = rect.top;
383 rectCheck.bottom =
384 rectLabel.bottom = rect.bottom;
385 const int checkSize = GetBestSize().y;
386 const int MARGIN = 3;
387
388 const bool isRightAligned = HasFlag(wxALIGN_RIGHT);
389 if ( isRightAligned )
390 {
391 rectCheck.right = rect.right;
392 rectCheck.left = rectCheck.right - checkSize;
393
394 rectLabel.right = rectCheck.left - MARGIN;
395 rectLabel.left = rect.left;
396 }
397 else // normal, left-aligned checkbox
398 {
399 rectCheck.left = rect.left;
400 rectCheck.right = rectCheck.left + checkSize;
401
402 rectLabel.left = rectCheck.right + MARGIN;
403 rectLabel.right = rect.right;
404 }
405
406 // show we draw a focus rect?
407 const bool isFocused = m_isPressed || FindFocus() == this;
408
409
d4adf63b
VZ
410 // draw the checkbox itself
411 wxDCTemp dc(hdc);
2f259810 412
d4adf63b 413 int flags = 0;
2f259810 414 if ( !IsEnabled() )
d4adf63b 415 flags |= wxCONTROL_DISABLED;
2f259810
VZ
416 switch ( Get3StateValue() )
417 {
418 case wxCHK_CHECKED:
d4adf63b 419 flags |= wxCONTROL_CHECKED;
2f259810
VZ
420 break;
421
422 case wxCHK_UNDETERMINED:
d4adf63b 423 flags |= wxCONTROL_PRESSED;
2f259810
VZ
424 break;
425
426 default:
9a83f860 427 wxFAIL_MSG( wxT("unexpected Get3StateValue() return value") );
2f259810
VZ
428 // fall through
429
430 case wxCHK_UNCHECKED:
431 // no extra styles needed
432 break;
433 }
434
435 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
d4adf63b 436 flags |= wxCONTROL_CURRENT;
2f259810 437
d4adf63b
VZ
438 wxRendererNative::Get().
439 DrawCheckBox(this, dc, wxRectFromRECT(rectCheck), flags);
2f259810
VZ
440
441 // draw the text
442 const wxString& label = GetLabel();
443
444 // first we need to measure it
445 UINT fmt = DT_NOCLIP;
446
447 // drawing underlying doesn't look well with focus rect (and the native
448 // control doesn't do it)
449 if ( isFocused )
450 fmt |= DT_HIDEPREFIX;
451 if ( isRightAligned )
452 fmt |= DT_RIGHT;
453 // TODO: also use DT_HIDEPREFIX if the system is configured so
454
455 // we need to get the label real size first if we have to draw a focus rect
456 // around it
457 if ( isFocused )
458 {
017dc06b 459 if ( !::DrawText(hdc, label.t_str(), label.length(), &rectLabel,
2f259810
VZ
460 fmt | DT_CALCRECT) )
461 {
9a83f860 462 wxLogLastError(wxT("DrawText(DT_CALCRECT)"));
2f259810
VZ
463 }
464 }
465
466 if ( !IsEnabled() )
467 {
468 ::SetTextColor(hdc, ::GetSysColor(COLOR_GRAYTEXT));
469 }
470
017dc06b 471 if ( !::DrawText(hdc, label.t_str(), label.length(), &rectLabel, fmt) )
2f259810 472 {
9a83f860 473 wxLogLastError(wxT("DrawText()"));
2f259810
VZ
474 }
475
476 // finally draw the focus
477 if ( isFocused )
478 {
479 rectLabel.left--;
480 rectLabel.right++;
481 if ( !::DrawFocusRect(hdc, &rectLabel) )
482 {
9a83f860 483 wxLogLastError(wxT("DrawFocusRect()"));
2f259810
VZ
484 }
485 }
486
487 return true;
8941fa88
VZ
488}
489
1e6feb95 490#endif // wxUSE_CHECKBOX