]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checkbox.cpp
Set missing Language: headers in PO files.
[wxWidgets.git] / src / msw / checkbox.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
ab1ce969 2// Name: src/msw/checkbox.cpp
2bda0e17
KB
3// Purpose: wxCheckBox
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
6c9a19aa 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
2bda0e17
KB
9/////////////////////////////////////////////////////////////////////////////
10
4438caf4
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
2bda0e17
KB
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
4438caf4 23 #pragma hdrstop
2bda0e17
KB
24#endif
25
1e6feb95
VZ
26#if wxUSE_CHECKBOX
27
ab1ce969
WS
28#include "wx/checkbox.h"
29
2bda0e17 30#ifndef WX_PRECOMP
4438caf4 31 #include "wx/brush.h"
6c2cd2a2 32 #include "wx/dcclient.h"
f6bcfd97
BP
33 #include "wx/dcscreen.h"
34 #include "wx/settings.h"
2bda0e17
KB
35#endif
36
025f7d77 37#include "wx/msw/dc.h" // for wxDCTemp
d4adf63b 38#include "wx/renderer.h"
533171c2
VZ
39#include "wx/msw/uxtheme.h"
40#include "wx/msw/private/button.h"
2f0312f0 41#include "wx/msw/missing.h"
2bda0e17 42
2f259810
VZ
43// ----------------------------------------------------------------------------
44// constants
45// ----------------------------------------------------------------------------
46
2f259810
VZ
47#ifndef BP_CHECKBOX
48 #define BP_CHECKBOX 3
49#endif
50
51// these values are defined in tmschema.h (except the first one)
52enum
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
70enum
71{
72 CBS_HOT_OFFSET = 1,
73 CBS_PRESSED_OFFSET = 2,
74 CBS_DISABLED_OFFSET = 3
75};
76
4438caf4
VZ
77// ============================================================================
78// implementation
79// ============================================================================
80
81// ----------------------------------------------------------------------------
2f259810 82// wxCheckBox creation
4438caf4
VZ
83// ----------------------------------------------------------------------------
84
2f259810 85void wxCheckBox::Init()
2bda0e17 86{
2f259810
VZ
87 m_state = wxCHK_UNCHECKED;
88 m_isPressed =
89 m_isHot = false;
2bda0e17
KB
90}
91
11b6a93b
VZ
92bool 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)
2bda0e17 99{
2f259810
VZ
100 Init();
101
f254e242 102 WXValidateStyle(&style);
787a85c2 103 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
02b7b6b0 104 return false;
4438caf4 105
687823a1
VZ
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
114WXDWORD 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);
8941fa88
VZ
118
119 if ( style & wxCHK_3STATE )
2f259810 120 msStyle |= BS_3STATE;
8941fa88 121 else
2f259810 122 msStyle |= BS_CHECKBOX;
8941fa88 123
4438caf4 124 if ( style & wxALIGN_RIGHT )
8941fa88 125 {
93212fee 126 msStyle |= BS_LEFTTEXT | BS_RIGHT;
8941fa88 127 }
4438caf4 128
687823a1 129 return msStyle;
2bda0e17
KB
130}
131
2f259810
VZ
132// ----------------------------------------------------------------------------
133// wxCheckBox geometry
134// ----------------------------------------------------------------------------
2bda0e17 135
e380ca3c 136wxSize wxCheckBox::DoGetBestClientSize() const
2bda0e17 137{
f6bcfd97 138 static int s_checkSize = 0;
81d66cf3 139
f6bcfd97
BP
140 if ( !s_checkSize )
141 {
142 wxScreenDC dc;
a756f210 143 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
2bda0e17 144
fb1a41cd 145 s_checkSize = dc.GetCharHeight();
f6bcfd97
BP
146 }
147
148 wxString str = wxGetWindowText(GetHWND());
149
150 int wCheckbox, hCheckbox;
ab1ce969 151 if ( !str.empty() )
4438caf4 152 {
5c33522f 153 wxClientDC dc(const_cast<wxCheckBox *>(this));
533171c2
VZ
154 dc.SetFont(GetFont());
155 dc.GetMultiLineTextExtent(GetLabelText(str), &wCheckbox, &hCheckbox);
f6bcfd97 156 wCheckbox += s_checkSize + GetCharWidth();
27fda0b6 157
35f92de3
VZ
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
f6bcfd97
BP
171 if ( hCheckbox < s_checkSize )
172 hCheckbox = s_checkSize;
4438caf4
VZ
173 }
174 else
2bda0e17 175 {
f6bcfd97
BP
176 wCheckbox = s_checkSize;
177 hCheckbox = s_checkSize;
2bda0e17 178 }
5a6371b9
JS
179#ifdef __WXWINCE__
180 hCheckbox += 1;
181#endif
2bda0e17 182
31582e4e
RD
183 wxSize best(wCheckbox, hCheckbox);
184 CacheBestSize(best);
185 return best;
2bda0e17
KB
186}
187
2f259810
VZ
188// ----------------------------------------------------------------------------
189// wxCheckBox operations
190// ----------------------------------------------------------------------------
191
533171c2
VZ
192void wxCheckBox::SetLabel(const wxString& label)
193{
194 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label);
195
196 wxCheckBoxBase::SetLabel(label);
197}
198
debe6624 199void wxCheckBox::SetValue(bool val)
2bda0e17 200{
c3732409 201 Set3StateValue(val ? wxCHK_CHECKED : wxCHK_UNCHECKED);
2bda0e17
KB
202}
203
bfc6fde4 204bool wxCheckBox::GetValue() const
2bda0e17 205{
c3732409 206 return Get3StateValue() != wxCHK_UNCHECKED;
2bda0e17
KB
207}
208
2b5f62a0 209void wxCheckBox::Command(wxCommandEvent& event)
2bda0e17 210{
8941fa88
VZ
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);
2b5f62a0 217 ProcessCommand(event);
2bda0e17 218}
1e6feb95 219
8941fa88
VZ
220wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED == BST_UNCHECKED
221 && wxCHK_CHECKED == BST_CHECKED
222 && wxCHK_UNDETERMINED == BST_INDETERMINATE, EnumValuesIncorrect);
223
224void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
225{
2f259810
VZ
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();
8941fa88
VZ
231}
232
233wxCheckBoxState wxCheckBox::DoGet3StateValue() const
234{
2f259810
VZ
235 return m_state;
236}
237
238bool 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
ce7fe42e 263 wxCommandEvent event(wxEVT_CHECKBOX, m_windowId);
2f259810
VZ
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
276bool 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() )
9b9a7c33 284 MSWMakeOwnerDrawn(colour.IsOk());
2f259810
VZ
285
286 return true;
287}
288
289bool wxCheckBox::IsOwnerDrawn() const
290{
291 return
292 (::GetWindowLong(GetHwnd(), GWL_STYLE) & BS_OWNERDRAW) == BS_OWNERDRAW;
293}
294
9b9a7c33 295void wxCheckBox::MSWMakeOwnerDrawn(bool ownerDrawn)
2f259810
VZ
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);
b3433ee7
VZ
331
332 if ( !ownerDrawn )
333 {
334 // ensure that controls state is consistent with internal state
335 DoSet3StateValue(m_state);
336 }
2f259810
VZ
337}
338
339void 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
350void 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
361void wxCheckBox::OnFocus(wxFocusEvent& event)
362{
363 Refresh();
364
365 event.Skip();
366}
367
368bool 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;
f2e763d7
VZ
380 rectLabel.top = rect.top + (rect.bottom - rect.top - GetBestSize().y) / 2;
381 rectLabel.bottom = rectLabel.top + GetBestSize().y;
2f259810 382 const int MARGIN = 3;
f2e763d7
VZ
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;
2f259810
VZ
389
390 const bool isRightAligned = HasFlag(wxALIGN_RIGHT);
391 if ( isRightAligned )
392 {
f2e763d7 393 rectLabel.right = rect.right - CXMENUCHECK;
2f259810 394 rectLabel.left = rect.left;
f2e763d7
VZ
395
396 rectCheck.left = rectLabel.right + ( CXMENUCHECK + MARGIN - checkSize ) / 2;
397 rectCheck.right = rectCheck.left + checkSize;
2f259810
VZ
398 }
399 else // normal, left-aligned checkbox
400 {
f2e763d7 401 rectCheck.left = rect.left + ( CXMENUCHECK - MARGIN - checkSize ) / 2;
2f259810
VZ
402 rectCheck.right = rectCheck.left + checkSize;
403
f2e763d7 404 rectLabel.left = rect.left + CXMENUCHECK;
2f259810
VZ
405 rectLabel.right = rect.right;
406 }
407
f2e763d7 408 // shall we draw a focus rect?
2f259810
VZ
409 const bool isFocused = m_isPressed || FindFocus() == this;
410
411
d4adf63b
VZ
412 // draw the checkbox itself
413 wxDCTemp dc(hdc);
2f259810 414
d4adf63b 415 int flags = 0;
2f259810 416 if ( !IsEnabled() )
d4adf63b 417 flags |= wxCONTROL_DISABLED;
2f259810
VZ
418 switch ( Get3StateValue() )
419 {
420 case wxCHK_CHECKED:
d4adf63b 421 flags |= wxCONTROL_CHECKED;
2f259810
VZ
422 break;
423
424 case wxCHK_UNDETERMINED:
d4adf63b 425 flags |= wxCONTROL_PRESSED;
2f259810
VZ
426 break;
427
428 default:
9a83f860 429 wxFAIL_MSG( wxT("unexpected Get3StateValue() return value") );
2f259810
VZ
430 // fall through
431
432 case wxCHK_UNCHECKED:
433 // no extra styles needed
434 break;
435 }
436
437 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
d4adf63b 438 flags |= wxCONTROL_CURRENT;
2f259810 439
d4adf63b
VZ
440 wxRendererNative::Get().
441 DrawCheckBox(this, dc, wxRectFromRECT(rectCheck), flags);
2f259810
VZ
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 {
f2e763d7
VZ
461 RECT oldLabelRect = rectLabel; // needed if right aligned
462
017dc06b 463 if ( !::DrawText(hdc, label.t_str(), label.length(), &rectLabel,
2f259810
VZ
464 fmt | DT_CALCRECT) )
465 {
9a83f860 466 wxLogLastError(wxT("DrawText(DT_CALCRECT)"));
2f259810 467 }
f2e763d7
VZ
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 }
2f259810
VZ
476 }
477
478 if ( !IsEnabled() )
479 {
480 ::SetTextColor(hdc, ::GetSysColor(COLOR_GRAYTEXT));
481 }
482
017dc06b 483 if ( !::DrawText(hdc, label.t_str(), label.length(), &rectLabel, fmt) )
2f259810 484 {
9a83f860 485 wxLogLastError(wxT("DrawText()"));
2f259810
VZ
486 }
487
488 // finally draw the focus
489 if ( isFocused )
490 {
491 rectLabel.left--;
492 rectLabel.right++;
493 if ( !::DrawFocusRect(hdc, &rectLabel) )
494 {
9a83f860 495 wxLogLastError(wxT("DrawFocusRect()"));
2f259810
VZ
496 }
497 }
498
499 return true;
8941fa88
VZ
500}
501
1e6feb95 502#endif // wxUSE_CHECKBOX