]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/checkbox.cpp
Make sure wchar_t CRT functions work on OS X.
[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 long msStyle = WS_TABSTOP;
108
109 if ( style & wxCHK_3STATE )
110 msStyle |= BS_3STATE;
111 else
112 msStyle |= BS_CHECKBOX;
113
114 if ( style & wxALIGN_RIGHT )
115 {
116 msStyle |= BS_LEFTTEXT | BS_RIGHT;
117 }
118
119 msStyle |= wxMSWButton::GetMultilineStyle(label);
120
121 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, 0);
122}
123
124// ----------------------------------------------------------------------------
125// wxCheckBox geometry
126// ----------------------------------------------------------------------------
127
128wxSize wxCheckBox::DoGetBestSize() const
129{
130 static int s_checkSize = 0;
131
132 if ( !s_checkSize )
133 {
134 wxScreenDC dc;
135 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
136
137 s_checkSize = dc.GetCharHeight();
138 }
139
140 wxString str = wxGetWindowText(GetHWND());
141
142 int wCheckbox, hCheckbox;
143 if ( !str.empty() )
144 {
145 wxClientDC dc(const_cast<wxCheckBox *>(this));
146 dc.SetFont(GetFont());
147 dc.GetMultiLineTextExtent(GetLabelText(str), &wCheckbox, &hCheckbox);
148 wCheckbox += s_checkSize + GetCharWidth();
149
150 if ( ::GetWindowLong(GetHwnd(), GWL_STYLE) & BS_MULTILINE )
151 {
152 // We need to make the checkbox even wider in this case because
153 // otherwise it wraps lines automatically and not only on "\n"s as
154 // we need and this makes the size computed here wrong resulting in
155 // checkbox contents being truncated when it's actually displayed.
156 // Without this hack simple checkbox with "Some thing\n and more"
157 // label appears on 3 lines, not 2, under Windows 2003 using
158 // classic look and feel (although it works fine under Windows 7,
159 // with or without themes).
160 wCheckbox += s_checkSize;
161 }
162
163 if ( hCheckbox < s_checkSize )
164 hCheckbox = s_checkSize;
165 }
166 else
167 {
168 wCheckbox = s_checkSize;
169 hCheckbox = s_checkSize;
170 }
171#ifdef __WXWINCE__
172 hCheckbox += 1;
173#endif
174
175 wxSize best(wCheckbox, hCheckbox);
176 CacheBestSize(best);
177 return best;
178}
179
180// ----------------------------------------------------------------------------
181// wxCheckBox operations
182// ----------------------------------------------------------------------------
183
184void wxCheckBox::SetLabel(const wxString& label)
185{
186 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label);
187
188 wxCheckBoxBase::SetLabel(label);
189}
190
191void wxCheckBox::SetValue(bool val)
192{
193 Set3StateValue(val ? wxCHK_CHECKED : wxCHK_UNCHECKED);
194}
195
196bool wxCheckBox::GetValue() const
197{
198 return Get3StateValue() != wxCHK_UNCHECKED;
199}
200
201void wxCheckBox::Command(wxCommandEvent& event)
202{
203 int state = event.GetInt();
204 wxCHECK_RET( (state == wxCHK_UNCHECKED) || (state == wxCHK_CHECKED)
205 || (state == wxCHK_UNDETERMINED),
206 wxT("event.GetInt() returned an invalid checkbox state") );
207
208 Set3StateValue((wxCheckBoxState) state);
209 ProcessCommand(event);
210}
211
212wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED == BST_UNCHECKED
213 && wxCHK_CHECKED == BST_CHECKED
214 && wxCHK_UNDETERMINED == BST_INDETERMINATE, EnumValuesIncorrect);
215
216void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
217{
218 m_state = state;
219 if ( !IsOwnerDrawn() )
220 ::SendMessage(GetHwnd(), BM_SETCHECK, (WPARAM) state, 0);
221 else // owner drawn buttons don't react to this message
222 Refresh();
223}
224
225wxCheckBoxState wxCheckBox::DoGet3StateValue() const
226{
227 return m_state;
228}
229
230bool wxCheckBox::MSWCommand(WXUINT cmd, WXWORD WXUNUSED(id))
231{
232 if ( cmd != BN_CLICKED && cmd != BN_DBLCLK )
233 return false;
234
235 // first update the value so that user event handler gets the new checkbox
236 // value
237
238 // ownerdrawn buttons don't manage their state themselves unlike usual
239 // auto checkboxes so do it ourselves in any case
240 wxCheckBoxState state;
241 if ( Is3rdStateAllowedForUser() )
242 {
243 state = (wxCheckBoxState)((m_state + 1) % 3);
244 }
245 else // 2 state checkbox (at least from users point of view)
246 {
247 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
248 state = m_state == wxCHK_UNCHECKED ? wxCHK_CHECKED : wxCHK_UNCHECKED;
249 }
250
251 DoSet3StateValue(state);
252
253
254 // generate the event
255 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId);
256
257 event.SetInt(state);
258 event.SetEventObject(this);
259 ProcessCommand(event);
260
261 return true;
262}
263
264// ----------------------------------------------------------------------------
265// owner drawn checkboxes stuff
266// ----------------------------------------------------------------------------
267
268bool wxCheckBox::SetForegroundColour(const wxColour& colour)
269{
270 if ( !wxCheckBoxBase::SetForegroundColour(colour) )
271 return false;
272
273 // the only way to change the checkbox foreground colour under Windows XP
274 // is to owner draw it
275 if ( wxUxThemeEngine::GetIfActive() )
276 MakeOwnerDrawn(colour.IsOk());
277
278 return true;
279}
280
281bool wxCheckBox::IsOwnerDrawn() const
282{
283 return
284 (::GetWindowLong(GetHwnd(), GWL_STYLE) & BS_OWNERDRAW) == BS_OWNERDRAW;
285}
286
287void wxCheckBox::MakeOwnerDrawn(bool ownerDrawn)
288{
289 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
290
291 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
292 // them as on independent style bits
293 if ( ownerDrawn )
294 {
295 style &= ~(BS_CHECKBOX | BS_3STATE);
296 style |= BS_OWNERDRAW;
297
298 Connect(wxEVT_ENTER_WINDOW,
299 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
300 Connect(wxEVT_LEAVE_WINDOW,
301 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
302 Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
303 Connect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
304 Connect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
305 Connect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
306 }
307 else // reset to default colour
308 {
309 style &= ~BS_OWNERDRAW;
310 style |= HasFlag(wxCHK_3STATE) ? BS_3STATE : BS_CHECKBOX;
311
312 Disconnect(wxEVT_ENTER_WINDOW,
313 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
314 Disconnect(wxEVT_LEAVE_WINDOW,
315 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
316 Disconnect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
317 Disconnect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
318 Disconnect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
319 Disconnect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
320 }
321
322 ::SetWindowLong(GetHwnd(), GWL_STYLE, style);
323
324 if ( !ownerDrawn )
325 {
326 // ensure that controls state is consistent with internal state
327 DoSet3StateValue(m_state);
328 }
329}
330
331void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent& event)
332{
333 m_isHot = event.GetEventType() == wxEVT_ENTER_WINDOW;
334 if ( !m_isHot )
335 m_isPressed = false;
336
337 Refresh();
338
339 event.Skip();
340}
341
342void wxCheckBox::OnMouseLeft(wxMouseEvent& event)
343{
344 // TODO: we should capture the mouse here to be notified about left up
345 // event but this interferes with BN_CLICKED generation so if we
346 // want to do this we'd need to generate them ourselves
347 m_isPressed = event.GetEventType() == wxEVT_LEFT_DOWN;
348 Refresh();
349
350 event.Skip();
351}
352
353void wxCheckBox::OnFocus(wxFocusEvent& event)
354{
355 Refresh();
356
357 event.Skip();
358}
359
360bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
361{
362 DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)item;
363
364 if ( !IsOwnerDrawn() || dis->CtlType != ODT_BUTTON )
365 return wxCheckBoxBase::MSWOnDraw(item);
366
367 // calculate the rectangles for the check mark itself and the label
368 HDC hdc = dis->hDC;
369 RECT& rect = dis->rcItem;
370 RECT rectCheck,
371 rectLabel;
372 rectCheck.top =
373 rectLabel.top = rect.top;
374 rectCheck.bottom =
375 rectLabel.bottom = rect.bottom;
376 const int checkSize = GetBestSize().y;
377 const int MARGIN = 3;
378
379 const bool isRightAligned = HasFlag(wxALIGN_RIGHT);
380 if ( isRightAligned )
381 {
382 rectCheck.right = rect.right;
383 rectCheck.left = rectCheck.right - checkSize;
384
385 rectLabel.right = rectCheck.left - MARGIN;
386 rectLabel.left = rect.left;
387 }
388 else // normal, left-aligned checkbox
389 {
390 rectCheck.left = rect.left;
391 rectCheck.right = rectCheck.left + checkSize;
392
393 rectLabel.left = rectCheck.right + MARGIN;
394 rectLabel.right = rect.right;
395 }
396
397 // show we draw a focus rect?
398 const bool isFocused = m_isPressed || FindFocus() == this;
399
400
401 // draw the checkbox itself
402 wxDCTemp dc(hdc);
403
404 int flags = 0;
405 if ( !IsEnabled() )
406 flags |= wxCONTROL_DISABLED;
407 switch ( Get3StateValue() )
408 {
409 case wxCHK_CHECKED:
410 flags |= wxCONTROL_CHECKED;
411 break;
412
413 case wxCHK_UNDETERMINED:
414 flags |= wxCONTROL_PRESSED;
415 break;
416
417 default:
418 wxFAIL_MSG( wxT("unexpected Get3StateValue() return value") );
419 // fall through
420
421 case wxCHK_UNCHECKED:
422 // no extra styles needed
423 break;
424 }
425
426 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
427 flags |= wxCONTROL_CURRENT;
428
429 wxRendererNative::Get().
430 DrawCheckBox(this, dc, wxRectFromRECT(rectCheck), flags);
431
432 // draw the text
433 const wxString& label = GetLabel();
434
435 // first we need to measure it
436 UINT fmt = DT_NOCLIP;
437
438 // drawing underlying doesn't look well with focus rect (and the native
439 // control doesn't do it)
440 if ( isFocused )
441 fmt |= DT_HIDEPREFIX;
442 if ( isRightAligned )
443 fmt |= DT_RIGHT;
444 // TODO: also use DT_HIDEPREFIX if the system is configured so
445
446 // we need to get the label real size first if we have to draw a focus rect
447 // around it
448 if ( isFocused )
449 {
450 if ( !::DrawText(hdc, label.t_str(), label.length(), &rectLabel,
451 fmt | DT_CALCRECT) )
452 {
453 wxLogLastError(wxT("DrawText(DT_CALCRECT)"));
454 }
455 }
456
457 if ( !IsEnabled() )
458 {
459 ::SetTextColor(hdc, ::GetSysColor(COLOR_GRAYTEXT));
460 }
461
462 if ( !::DrawText(hdc, label.t_str(), label.length(), &rectLabel, fmt) )
463 {
464 wxLogLastError(wxT("DrawText()"));
465 }
466
467 // finally draw the focus
468 if ( isFocused )
469 {
470 rectLabel.left--;
471 rectLabel.right++;
472 if ( !::DrawFocusRect(hdc, &rectLabel) )
473 {
474 wxLogLastError(wxT("DrawFocusRect()"));
475 }
476 }
477
478 return true;
479}
480
481#endif // wxUSE_CHECKBOX