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