]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checkbox.cpp
Add wxGrid::Render() for drawing the grid to any wxDC.
[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
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
4438caf4
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
4438caf4 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
1e6feb95
VZ
27#if wxUSE_CHECKBOX
28
ab1ce969
WS
29#include "wx/checkbox.h"
30
2bda0e17 31#ifndef WX_PRECOMP
4438caf4 32 #include "wx/brush.h"
6c2cd2a2 33 #include "wx/dcclient.h"
f6bcfd97
BP
34 #include "wx/dcscreen.h"
35 #include "wx/settings.h"
2bda0e17
KB
36#endif
37
025f7d77 38#include "wx/msw/dc.h" // for wxDCTemp
d4adf63b 39#include "wx/renderer.h"
533171c2
VZ
40#include "wx/msw/uxtheme.h"
41#include "wx/msw/private/button.h"
2f0312f0 42#include "wx/msw/missing.h"
2bda0e17 43
2f259810
VZ
44// ----------------------------------------------------------------------------
45// constants
46// ----------------------------------------------------------------------------
47
2f259810
VZ
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
4438caf4
VZ
78// ============================================================================
79// implementation
80// ============================================================================
81
82// ----------------------------------------------------------------------------
2f259810 83// wxCheckBox creation
4438caf4
VZ
84// ----------------------------------------------------------------------------
85
2f259810 86void wxCheckBox::Init()
2bda0e17 87{
2f259810
VZ
88 m_state = wxCHK_UNCHECKED;
89 m_isPressed =
90 m_isHot = false;
2bda0e17
KB
91}
92
11b6a93b
VZ
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)
2bda0e17 100{
2f259810
VZ
101 Init();
102
f254e242 103 WXValidateStyle(&style);
787a85c2 104 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
02b7b6b0 105 return false;
4438caf4 106
8941fa88
VZ
107 long msStyle = WS_TABSTOP;
108
109 if ( style & wxCHK_3STATE )
2f259810 110 msStyle |= BS_3STATE;
8941fa88 111 else
2f259810 112 msStyle |= BS_CHECKBOX;
8941fa88 113
4438caf4 114 if ( style & wxALIGN_RIGHT )
8941fa88 115 {
93212fee 116 msStyle |= BS_LEFTTEXT | BS_RIGHT;
8941fa88 117 }
4438caf4 118
533171c2
VZ
119 msStyle |= wxMSWButton::GetMultilineStyle(label);
120
8912d7eb 121 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, 0);
2bda0e17
KB
122}
123
2f259810
VZ
124// ----------------------------------------------------------------------------
125// wxCheckBox geometry
126// ----------------------------------------------------------------------------
2bda0e17 127
f68586e5 128wxSize wxCheckBox::DoGetBestSize() const
2bda0e17 129{
f6bcfd97 130 static int s_checkSize = 0;
81d66cf3 131
f6bcfd97
BP
132 if ( !s_checkSize )
133 {
134 wxScreenDC dc;
a756f210 135 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
2bda0e17 136
fb1a41cd 137 s_checkSize = dc.GetCharHeight();
f6bcfd97
BP
138 }
139
140 wxString str = wxGetWindowText(GetHWND());
141
142 int wCheckbox, hCheckbox;
ab1ce969 143 if ( !str.empty() )
4438caf4 144 {
5c33522f 145 wxClientDC dc(const_cast<wxCheckBox *>(this));
533171c2
VZ
146 dc.SetFont(GetFont());
147 dc.GetMultiLineTextExtent(GetLabelText(str), &wCheckbox, &hCheckbox);
f6bcfd97 148 wCheckbox += s_checkSize + GetCharWidth();
27fda0b6 149
35f92de3
VZ
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
f6bcfd97
BP
163 if ( hCheckbox < s_checkSize )
164 hCheckbox = s_checkSize;
4438caf4
VZ
165 }
166 else
2bda0e17 167 {
f6bcfd97
BP
168 wCheckbox = s_checkSize;
169 hCheckbox = s_checkSize;
2bda0e17 170 }
5a6371b9
JS
171#ifdef __WXWINCE__
172 hCheckbox += 1;
173#endif
2bda0e17 174
31582e4e
RD
175 wxSize best(wCheckbox, hCheckbox);
176 CacheBestSize(best);
177 return best;
2bda0e17
KB
178}
179
2f259810
VZ
180// ----------------------------------------------------------------------------
181// wxCheckBox operations
182// ----------------------------------------------------------------------------
183
533171c2
VZ
184void wxCheckBox::SetLabel(const wxString& label)
185{
186 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label);
187
188 wxCheckBoxBase::SetLabel(label);
189}
190
debe6624 191void wxCheckBox::SetValue(bool val)
2bda0e17 192{
c3732409 193 Set3StateValue(val ? wxCHK_CHECKED : wxCHK_UNCHECKED);
2bda0e17
KB
194}
195
bfc6fde4 196bool wxCheckBox::GetValue() const
2bda0e17 197{
c3732409 198 return Get3StateValue() != wxCHK_UNCHECKED;
2bda0e17
KB
199}
200
2b5f62a0 201void wxCheckBox::Command(wxCommandEvent& event)
2bda0e17 202{
8941fa88
VZ
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);
2b5f62a0 209 ProcessCommand(event);
2bda0e17 210}
1e6feb95 211
8941fa88
VZ
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{
2f259810
VZ
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();
8941fa88
VZ
223}
224
225wxCheckBoxState wxCheckBox::DoGet3StateValue() const
226{
2f259810
VZ
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() )
a1b806b9 276 MakeOwnerDrawn(colour.IsOk());
2f259810
VZ
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);
b3433ee7
VZ
323
324 if ( !ownerDrawn )
325 {
326 // ensure that controls state is consistent with internal state
327 DoSet3StateValue(m_state);
328 }
2f259810
VZ
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
d4adf63b
VZ
401 // draw the checkbox itself
402 wxDCTemp dc(hdc);
2f259810 403
d4adf63b 404 int flags = 0;
2f259810 405 if ( !IsEnabled() )
d4adf63b 406 flags |= wxCONTROL_DISABLED;
2f259810
VZ
407 switch ( Get3StateValue() )
408 {
409 case wxCHK_CHECKED:
d4adf63b 410 flags |= wxCONTROL_CHECKED;
2f259810
VZ
411 break;
412
413 case wxCHK_UNDETERMINED:
d4adf63b 414 flags |= wxCONTROL_PRESSED;
2f259810
VZ
415 break;
416
417 default:
9a83f860 418 wxFAIL_MSG( wxT("unexpected Get3StateValue() return value") );
2f259810
VZ
419 // fall through
420
421 case wxCHK_UNCHECKED:
422 // no extra styles needed
423 break;
424 }
425
426 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
d4adf63b 427 flags |= wxCONTROL_CURRENT;
2f259810 428
d4adf63b
VZ
429 wxRendererNative::Get().
430 DrawCheckBox(this, dc, wxRectFromRECT(rectCheck), flags);
2f259810
VZ
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 {
e0a050e3 450 if ( !::DrawText(hdc, label.wx_str(), label.length(), &rectLabel,
2f259810
VZ
451 fmt | DT_CALCRECT) )
452 {
9a83f860 453 wxLogLastError(wxT("DrawText(DT_CALCRECT)"));
2f259810
VZ
454 }
455 }
456
457 if ( !IsEnabled() )
458 {
459 ::SetTextColor(hdc, ::GetSysColor(COLOR_GRAYTEXT));
460 }
461
e0a050e3 462 if ( !::DrawText(hdc, label.wx_str(), label.length(), &rectLabel, fmt) )
2f259810 463 {
9a83f860 464 wxLogLastError(wxT("DrawText()"));
2f259810
VZ
465 }
466
467 // finally draw the focus
468 if ( isFocused )
469 {
470 rectLabel.left--;
471 rectLabel.right++;
472 if ( !::DrawFocusRect(hdc, &rectLabel) )
473 {
9a83f860 474 wxLogLastError(wxT("DrawFocusRect()"));
2f259810
VZ
475 }
476 }
477
478 return true;
8941fa88
VZ
479}
480
1e6feb95 481#endif // wxUSE_CHECKBOX