]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/checkbox.cpp
simplify code so it always returns the same object
[wxWidgets.git] / src / msw / checkbox.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/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#include "wx/checkbox.h"
30
31#ifndef WX_PRECOMP
32 #include "wx/brush.h"
33 #include "wx/dcclient.h"
34 #include "wx/dcscreen.h"
35 #include "wx/settings.h"
36#endif
37
38#include "wx/msw/dc.h" // for wxDCTemp
39#include "wx/renderer.h"
40#include "wx/msw/uxtheme.h"
41#include "wx/msw/private/button.h"
42#include "wx/msw/missing.h"
43
44// ----------------------------------------------------------------------------
45// constants
46// ----------------------------------------------------------------------------
47
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
78// ============================================================================
79// implementation
80// ============================================================================
81
82// ----------------------------------------------------------------------------
83// wxCheckBox creation
84// ----------------------------------------------------------------------------
85
86void wxCheckBox::Init()
87{
88 m_state = wxCHK_UNCHECKED;
89 m_isPressed =
90 m_isHot = false;
91}
92
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)
100{
101 Init();
102
103 WXValidateStyle(&style);
104 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
105 return false;
106
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);
119
120 if ( style & wxCHK_3STATE )
121 msStyle |= BS_3STATE;
122 else
123 msStyle |= BS_CHECKBOX;
124
125 if ( style & wxALIGN_RIGHT )
126 {
127 msStyle |= BS_LEFTTEXT | BS_RIGHT;
128 }
129
130 return msStyle;
131}
132
133// ----------------------------------------------------------------------------
134// wxCheckBox geometry
135// ----------------------------------------------------------------------------
136
137wxSize wxCheckBox::DoGetBestClientSize() const
138{
139 static int s_checkSize = 0;
140
141 if ( !s_checkSize )
142 {
143 wxScreenDC dc;
144 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
145
146 s_checkSize = dc.GetCharHeight();
147 }
148
149 wxString str = wxGetWindowText(GetHWND());
150
151 int wCheckbox, hCheckbox;
152 if ( !str.empty() )
153 {
154 wxClientDC dc(const_cast<wxCheckBox *>(this));
155 dc.SetFont(GetFont());
156 dc.GetMultiLineTextExtent(GetLabelText(str), &wCheckbox, &hCheckbox);
157 wCheckbox += s_checkSize + GetCharWidth();
158
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
172 if ( hCheckbox < s_checkSize )
173 hCheckbox = s_checkSize;
174 }
175 else
176 {
177 wCheckbox = s_checkSize;
178 hCheckbox = s_checkSize;
179 }
180#ifdef __WXWINCE__
181 hCheckbox += 1;
182#endif
183
184 wxSize best(wCheckbox, hCheckbox);
185 CacheBestSize(best);
186 return best;
187}
188
189// ----------------------------------------------------------------------------
190// wxCheckBox operations
191// ----------------------------------------------------------------------------
192
193void wxCheckBox::SetLabel(const wxString& label)
194{
195 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label);
196
197 wxCheckBoxBase::SetLabel(label);
198}
199
200void wxCheckBox::SetValue(bool val)
201{
202 Set3StateValue(val ? wxCHK_CHECKED : wxCHK_UNCHECKED);
203}
204
205bool wxCheckBox::GetValue() const
206{
207 return Get3StateValue() != wxCHK_UNCHECKED;
208}
209
210void wxCheckBox::Command(wxCommandEvent& event)
211{
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);
218 ProcessCommand(event);
219}
220
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{
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();
232}
233
234wxCheckBoxState wxCheckBox::DoGet3StateValue() const
235{
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() )
285 MSWMakeOwnerDrawn(colour.IsOk());
286
287 return true;
288}
289
290bool wxCheckBox::IsOwnerDrawn() const
291{
292 return
293 (::GetWindowLong(GetHwnd(), GWL_STYLE) & BS_OWNERDRAW) == BS_OWNERDRAW;
294}
295
296void wxCheckBox::MSWMakeOwnerDrawn(bool ownerDrawn)
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);
332
333 if ( !ownerDrawn )
334 {
335 // ensure that controls state is consistent with internal state
336 DoSet3StateValue(m_state);
337 }
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
410 // draw the checkbox itself
411 wxDCTemp dc(hdc);
412
413 int flags = 0;
414 if ( !IsEnabled() )
415 flags |= wxCONTROL_DISABLED;
416 switch ( Get3StateValue() )
417 {
418 case wxCHK_CHECKED:
419 flags |= wxCONTROL_CHECKED;
420 break;
421
422 case wxCHK_UNDETERMINED:
423 flags |= wxCONTROL_PRESSED;
424 break;
425
426 default:
427 wxFAIL_MSG( wxT("unexpected Get3StateValue() return value") );
428 // fall through
429
430 case wxCHK_UNCHECKED:
431 // no extra styles needed
432 break;
433 }
434
435 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
436 flags |= wxCONTROL_CURRENT;
437
438 wxRendererNative::Get().
439 DrawCheckBox(this, dc, wxRectFromRECT(rectCheck), flags);
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 {
459 if ( !::DrawText(hdc, label.t_str(), label.length(), &rectLabel,
460 fmt | DT_CALCRECT) )
461 {
462 wxLogLastError(wxT("DrawText(DT_CALCRECT)"));
463 }
464 }
465
466 if ( !IsEnabled() )
467 {
468 ::SetTextColor(hdc, ::GetSysColor(COLOR_GRAYTEXT));
469 }
470
471 if ( !::DrawText(hdc, label.t_str(), label.length(), &rectLabel, fmt) )
472 {
473 wxLogLastError(wxT("DrawText()"));
474 }
475
476 // finally draw the focus
477 if ( isFocused )
478 {
479 rectLabel.left--;
480 rectLabel.right++;
481 if ( !::DrawFocusRect(hdc, &rectLabel) )
482 {
483 wxLogLastError(wxT("DrawFocusRect()"));
484 }
485 }
486
487 return true;
488}
489
490#endif // wxUSE_CHECKBOX