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