]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checkbox.cpp
Send generic wxTreeCtrl wxEVT_COMMAND_TREE_KEY_DOWN events from OnKeyDown rather...
[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
787a85c2 156 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
02b7b6b0 157 return false;
4438caf4 158
8941fa88
VZ
159 long msStyle = WS_TABSTOP;
160
161 if ( style & wxCHK_3STATE )
162 {
2f259810 163 msStyle |= BS_3STATE;
8941fa88
VZ
164 }
165 else
166 {
167 wxASSERT_MSG( !Is3rdStateAllowedForUser(),
168 wxT("Using wxCH_ALLOW_3RD_STATE_FOR_USER")
169 wxT(" style flag for a 2-state checkbox is useless") );
2f259810 170 msStyle |= BS_CHECKBOX;
8941fa88
VZ
171 }
172
4438caf4 173 if ( style & wxALIGN_RIGHT )
8941fa88 174 {
93212fee 175 msStyle |= BS_LEFTTEXT | BS_RIGHT;
8941fa88 176 }
4438caf4 177
533171c2
VZ
178 msStyle |= wxMSWButton::GetMultilineStyle(label);
179
8912d7eb 180 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, 0);
2bda0e17
KB
181}
182
2f259810
VZ
183// ----------------------------------------------------------------------------
184// wxCheckBox geometry
185// ----------------------------------------------------------------------------
2bda0e17 186
f68586e5 187wxSize wxCheckBox::DoGetBestSize() const
2bda0e17 188{
f6bcfd97 189 static int s_checkSize = 0;
81d66cf3 190
f6bcfd97
BP
191 if ( !s_checkSize )
192 {
193 wxScreenDC dc;
a756f210 194 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
2bda0e17 195
fb1a41cd 196 s_checkSize = dc.GetCharHeight();
f6bcfd97
BP
197 }
198
199 wxString str = wxGetWindowText(GetHWND());
200
201 int wCheckbox, hCheckbox;
ab1ce969 202 if ( !str.empty() )
4438caf4 203 {
5c33522f 204 wxClientDC dc(const_cast<wxCheckBox *>(this));
533171c2
VZ
205 dc.SetFont(GetFont());
206 dc.GetMultiLineTextExtent(GetLabelText(str), &wCheckbox, &hCheckbox);
f6bcfd97 207 wCheckbox += s_checkSize + GetCharWidth();
27fda0b6 208
f6bcfd97
BP
209 if ( hCheckbox < s_checkSize )
210 hCheckbox = s_checkSize;
4438caf4
VZ
211 }
212 else
2bda0e17 213 {
f6bcfd97
BP
214 wCheckbox = s_checkSize;
215 hCheckbox = s_checkSize;
2bda0e17 216 }
5a6371b9
JS
217#ifdef __WXWINCE__
218 hCheckbox += 1;
219#endif
2bda0e17 220
31582e4e
RD
221 wxSize best(wCheckbox, hCheckbox);
222 CacheBestSize(best);
223 return best;
2bda0e17
KB
224}
225
2f259810
VZ
226// ----------------------------------------------------------------------------
227// wxCheckBox operations
228// ----------------------------------------------------------------------------
229
533171c2
VZ
230void wxCheckBox::SetLabel(const wxString& label)
231{
232 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label);
233
234 wxCheckBoxBase::SetLabel(label);
235}
236
debe6624 237void wxCheckBox::SetValue(bool val)
2bda0e17 238{
c3732409 239 Set3StateValue(val ? wxCHK_CHECKED : wxCHK_UNCHECKED);
2bda0e17
KB
240}
241
bfc6fde4 242bool wxCheckBox::GetValue() const
2bda0e17 243{
c3732409 244 return Get3StateValue() != wxCHK_UNCHECKED;
2bda0e17
KB
245}
246
2b5f62a0 247void wxCheckBox::Command(wxCommandEvent& event)
2bda0e17 248{
8941fa88
VZ
249 int state = event.GetInt();
250 wxCHECK_RET( (state == wxCHK_UNCHECKED) || (state == wxCHK_CHECKED)
251 || (state == wxCHK_UNDETERMINED),
252 wxT("event.GetInt() returned an invalid checkbox state") );
253
254 Set3StateValue((wxCheckBoxState) state);
2b5f62a0 255 ProcessCommand(event);
2bda0e17 256}
1e6feb95 257
8941fa88
VZ
258wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED == BST_UNCHECKED
259 && wxCHK_CHECKED == BST_CHECKED
260 && wxCHK_UNDETERMINED == BST_INDETERMINATE, EnumValuesIncorrect);
261
262void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
263{
2f259810
VZ
264 m_state = state;
265 if ( !IsOwnerDrawn() )
266 ::SendMessage(GetHwnd(), BM_SETCHECK, (WPARAM) state, 0);
267 else // owner drawn buttons don't react to this message
268 Refresh();
8941fa88
VZ
269}
270
271wxCheckBoxState wxCheckBox::DoGet3StateValue() const
272{
2f259810
VZ
273 return m_state;
274}
275
276bool wxCheckBox::MSWCommand(WXUINT cmd, WXWORD WXUNUSED(id))
277{
278 if ( cmd != BN_CLICKED && cmd != BN_DBLCLK )
279 return false;
280
281 // first update the value so that user event handler gets the new checkbox
282 // value
283
284 // ownerdrawn buttons don't manage their state themselves unlike usual
285 // auto checkboxes so do it ourselves in any case
286 wxCheckBoxState state;
287 if ( Is3rdStateAllowedForUser() )
288 {
289 state = (wxCheckBoxState)((m_state + 1) % 3);
290 }
291 else // 2 state checkbox (at least from users point of view)
292 {
293 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
294 state = m_state == wxCHK_UNCHECKED ? wxCHK_CHECKED : wxCHK_UNCHECKED;
295 }
296
297 DoSet3StateValue(state);
298
299
300 // generate the event
301 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId);
302
303 event.SetInt(state);
304 event.SetEventObject(this);
305 ProcessCommand(event);
306
307 return true;
308}
309
310// ----------------------------------------------------------------------------
311// owner drawn checkboxes stuff
312// ----------------------------------------------------------------------------
313
314bool wxCheckBox::SetForegroundColour(const wxColour& colour)
315{
316 if ( !wxCheckBoxBase::SetForegroundColour(colour) )
317 return false;
318
319 // the only way to change the checkbox foreground colour under Windows XP
320 // is to owner draw it
321 if ( wxUxThemeEngine::GetIfActive() )
322 MakeOwnerDrawn(colour.Ok());
323
324 return true;
325}
326
327bool wxCheckBox::IsOwnerDrawn() const
328{
329 return
330 (::GetWindowLong(GetHwnd(), GWL_STYLE) & BS_OWNERDRAW) == BS_OWNERDRAW;
331}
332
333void wxCheckBox::MakeOwnerDrawn(bool ownerDrawn)
334{
335 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
336
337 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
338 // them as on independent style bits
339 if ( ownerDrawn )
340 {
341 style &= ~(BS_CHECKBOX | BS_3STATE);
342 style |= BS_OWNERDRAW;
343
344 Connect(wxEVT_ENTER_WINDOW,
345 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
346 Connect(wxEVT_LEAVE_WINDOW,
347 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
348 Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
349 Connect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
350 Connect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
351 Connect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
352 }
353 else // reset to default colour
354 {
355 style &= ~BS_OWNERDRAW;
356 style |= HasFlag(wxCHK_3STATE) ? BS_3STATE : BS_CHECKBOX;
357
358 Disconnect(wxEVT_ENTER_WINDOW,
359 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
360 Disconnect(wxEVT_LEAVE_WINDOW,
361 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
362 Disconnect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
363 Disconnect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
364 Disconnect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
365 Disconnect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
366 }
367
368 ::SetWindowLong(GetHwnd(), GWL_STYLE, style);
b3433ee7
VZ
369
370 if ( !ownerDrawn )
371 {
372 // ensure that controls state is consistent with internal state
373 DoSet3StateValue(m_state);
374 }
2f259810
VZ
375}
376
377void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent& event)
378{
379 m_isHot = event.GetEventType() == wxEVT_ENTER_WINDOW;
380 if ( !m_isHot )
381 m_isPressed = false;
382
383 Refresh();
384
385 event.Skip();
386}
387
388void wxCheckBox::OnMouseLeft(wxMouseEvent& event)
389{
390 // TODO: we should capture the mouse here to be notified about left up
391 // event but this interferes with BN_CLICKED generation so if we
392 // want to do this we'd need to generate them ourselves
393 m_isPressed = event.GetEventType() == wxEVT_LEFT_DOWN;
394 Refresh();
395
396 event.Skip();
397}
398
399void wxCheckBox::OnFocus(wxFocusEvent& event)
400{
401 Refresh();
402
403 event.Skip();
404}
405
406bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
407{
408 DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)item;
409
410 if ( !IsOwnerDrawn() || dis->CtlType != ODT_BUTTON )
411 return wxCheckBoxBase::MSWOnDraw(item);
412
413 // calculate the rectangles for the check mark itself and the label
414 HDC hdc = dis->hDC;
415 RECT& rect = dis->rcItem;
416 RECT rectCheck,
417 rectLabel;
418 rectCheck.top =
419 rectLabel.top = rect.top;
420 rectCheck.bottom =
421 rectLabel.bottom = rect.bottom;
422 const int checkSize = GetBestSize().y;
423 const int MARGIN = 3;
424
425 const bool isRightAligned = HasFlag(wxALIGN_RIGHT);
426 if ( isRightAligned )
427 {
428 rectCheck.right = rect.right;
429 rectCheck.left = rectCheck.right - checkSize;
430
431 rectLabel.right = rectCheck.left - MARGIN;
432 rectLabel.left = rect.left;
433 }
434 else // normal, left-aligned checkbox
435 {
436 rectCheck.left = rect.left;
437 rectCheck.right = rectCheck.left + checkSize;
438
439 rectLabel.left = rectCheck.right + MARGIN;
440 rectLabel.right = rect.right;
441 }
442
443 // show we draw a focus rect?
444 const bool isFocused = m_isPressed || FindFocus() == this;
445
446
d4adf63b
VZ
447 // draw the checkbox itself
448 wxDCTemp dc(hdc);
2f259810 449
d4adf63b 450 int flags = 0;
2f259810 451 if ( !IsEnabled() )
d4adf63b 452 flags |= wxCONTROL_DISABLED;
2f259810
VZ
453 switch ( Get3StateValue() )
454 {
455 case wxCHK_CHECKED:
d4adf63b 456 flags |= wxCONTROL_CHECKED;
2f259810
VZ
457 break;
458
459 case wxCHK_UNDETERMINED:
d4adf63b 460 flags |= wxCONTROL_PRESSED;
2f259810
VZ
461 break;
462
463 default:
9a83f860 464 wxFAIL_MSG( wxT("unexpected Get3StateValue() return value") );
2f259810
VZ
465 // fall through
466
467 case wxCHK_UNCHECKED:
468 // no extra styles needed
469 break;
470 }
471
472 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
d4adf63b 473 flags |= wxCONTROL_CURRENT;
2f259810 474
d4adf63b
VZ
475 wxRendererNative::Get().
476 DrawCheckBox(this, dc, wxRectFromRECT(rectCheck), flags);
2f259810
VZ
477
478 // draw the text
479 const wxString& label = GetLabel();
480
481 // first we need to measure it
482 UINT fmt = DT_NOCLIP;
483
484 // drawing underlying doesn't look well with focus rect (and the native
485 // control doesn't do it)
486 if ( isFocused )
487 fmt |= DT_HIDEPREFIX;
488 if ( isRightAligned )
489 fmt |= DT_RIGHT;
490 // TODO: also use DT_HIDEPREFIX if the system is configured so
491
492 // we need to get the label real size first if we have to draw a focus rect
493 // around it
494 if ( isFocused )
495 {
e0a050e3 496 if ( !::DrawText(hdc, label.wx_str(), label.length(), &rectLabel,
2f259810
VZ
497 fmt | DT_CALCRECT) )
498 {
9a83f860 499 wxLogLastError(wxT("DrawText(DT_CALCRECT)"));
2f259810
VZ
500 }
501 }
502
503 if ( !IsEnabled() )
504 {
505 ::SetTextColor(hdc, ::GetSysColor(COLOR_GRAYTEXT));
506 }
507
e0a050e3 508 if ( !::DrawText(hdc, label.wx_str(), label.length(), &rectLabel, fmt) )
2f259810 509 {
9a83f860 510 wxLogLastError(wxT("DrawText()"));
2f259810
VZ
511 }
512
513 // finally draw the focus
514 if ( isFocused )
515 {
516 rectLabel.left--;
517 rectLabel.right++;
518 if ( !::DrawFocusRect(hdc, &rectLabel) )
519 {
9a83f860 520 wxLogLastError(wxT("DrawFocusRect()"));
2f259810
VZ
521 }
522 }
523
524 return true;
8941fa88
VZ
525}
526
1e6feb95 527#endif // wxUSE_CHECKBOX