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