]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checkbox.cpp
Fix to preserve alpha channel in Rotate90
[wxWidgets.git] / src / msw / checkbox.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
2b5f62a0 2// Name: 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
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
4438caf4 21 #pragma implementation "checkbox.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
4438caf4 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_CHECKBOX
32
2bda0e17 33#ifndef WX_PRECOMP
4438caf4
VZ
34 #include "wx/checkbox.h"
35 #include "wx/brush.h"
f6bcfd97
BP
36 #include "wx/dcscreen.h"
37 #include "wx/settings.h"
2bda0e17
KB
38#endif
39
2f259810 40#include "wx/msw/uxtheme.h"
2bda0e17
KB
41#include "wx/msw/private.h"
42
2f259810
VZ
43// ----------------------------------------------------------------------------
44// constants
45// ----------------------------------------------------------------------------
46
8941fa88
VZ
47#ifndef BST_UNCHECKED
48 #define BST_UNCHECKED 0x0000
49#endif
50
2b5f62a0
VZ
51#ifndef BST_CHECKED
52 #define BST_CHECKED 0x0001
53#endif
2bda0e17 54
8941fa88
VZ
55#ifndef BST_INDETERMINATE
56 #define BST_INDETERMINATE 0x0002
57#endif
58
2f259810
VZ
59#ifndef DFCS_HOT
60 #define DFCS_HOT 0x1000
61#endif
62
63#ifndef DT_HIDEPREFIX
64 #define DT_HIDEPREFIX 0x00100000
65#endif
66
67#ifndef BP_CHECKBOX
68 #define BP_CHECKBOX 3
69#endif
70
71// these values are defined in tmschema.h (except the first one)
72enum
73{
74 CBS_INVALID,
75 CBS_UNCHECKEDNORMAL,
76 CBS_UNCHECKEDHOT,
77 CBS_UNCHECKEDPRESSED,
78 CBS_UNCHECKEDDISABLED,
79 CBS_CHECKEDNORMAL,
80 CBS_CHECKEDHOT,
81 CBS_CHECKEDPRESSED,
82 CBS_CHECKEDDISABLED,
83 CBS_MIXEDNORMAL,
84 CBS_MIXEDHOT,
85 CBS_MIXEDPRESSED,
86 CBS_MIXEDDISABLED
87};
88
89// these are our own
90enum
91{
92 CBS_HOT_OFFSET = 1,
93 CBS_PRESSED_OFFSET = 2,
94 CBS_DISABLED_OFFSET = 3
95};
96
4438caf4
VZ
97// ============================================================================
98// implementation
99// ============================================================================
100
51741307 101#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
102WX_DEFINE_FLAGS( wxCheckBoxStyle )
103
3ff066a4 104wxBEGIN_FLAGS( wxCheckBoxStyle )
bc9fb572
JS
105 // new style border flags, we put them first to
106 // use them for streaming out
3ff066a4
SC
107 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
108 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
109 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
110 wxFLAGS_MEMBER(wxBORDER_RAISED)
111 wxFLAGS_MEMBER(wxBORDER_STATIC)
112 wxFLAGS_MEMBER(wxBORDER_NONE)
02b7b6b0 113
bc9fb572 114 // old style border flags
3ff066a4
SC
115 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
116 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
117 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
118 wxFLAGS_MEMBER(wxRAISED_BORDER)
119 wxFLAGS_MEMBER(wxSTATIC_BORDER)
8941fa88 120 wxFLAGS_MEMBER(wxNO_BORDER)
bc9fb572
JS
121
122 // standard window styles
3ff066a4
SC
123 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
124 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
125 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
126 wxFLAGS_MEMBER(wxWANTS_CHARS)
8941fa88 127 wxFLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE)
3ff066a4
SC
128 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
129 wxFLAGS_MEMBER(wxVSCROLL)
130 wxFLAGS_MEMBER(wxHSCROLL)
bc9fb572 131
3ff066a4 132wxEND_FLAGS( wxCheckBoxStyle )
bc9fb572 133
51741307
SC
134IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckBox, wxControl,"wx/checkbox.h")
135
3ff066a4 136wxBEGIN_PROPERTIES_TABLE(wxCheckBox)
93212fee 137 wxEVENT_PROPERTY( Click , wxEVT_COMMAND_CHECKBOX_CLICKED , wxCommandEvent )
51741307 138
93212fee
VZ
139 wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
140 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
141 wxPROPERTY( Value ,bool, SetValue, GetValue, EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
af498247 142 wxPROPERTY_FLAGS( WindowStyle , wxCheckBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 143wxEND_PROPERTIES_TABLE()
2b5f62a0 144
3ff066a4
SC
145wxBEGIN_HANDLERS_TABLE(wxCheckBox)
146wxEND_HANDLERS_TABLE()
51741307 147
02b7b6b0 148wxCONSTRUCTOR_6( wxCheckBox , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle )
51741307
SC
149#else
150IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
151#endif
066f1b7a 152
066f1b7a 153
4438caf4 154// ----------------------------------------------------------------------------
2f259810 155// wxCheckBox creation
4438caf4
VZ
156// ----------------------------------------------------------------------------
157
2f259810 158void wxCheckBox::Init()
2bda0e17 159{
2f259810
VZ
160 m_state = wxCHK_UNCHECKED;
161 m_isPressed =
162 m_isHot = false;
2bda0e17
KB
163}
164
11b6a93b
VZ
165bool wxCheckBox::Create(wxWindow *parent,
166 wxWindowID id,
167 const wxString& label,
168 const wxPoint& pos,
169 const wxSize& size, long style,
170 const wxValidator& validator,
171 const wxString& name)
2bda0e17 172{
2f259810
VZ
173 Init();
174
787a85c2 175 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
02b7b6b0 176 return false;
4438caf4 177
8941fa88
VZ
178 long msStyle = WS_TABSTOP;
179
180 if ( style & wxCHK_3STATE )
181 {
2f259810 182 msStyle |= BS_3STATE;
8941fa88
VZ
183 }
184 else
185 {
186 wxASSERT_MSG( !Is3rdStateAllowedForUser(),
187 wxT("Using wxCH_ALLOW_3RD_STATE_FOR_USER")
188 wxT(" style flag for a 2-state checkbox is useless") );
2f259810 189 msStyle |= BS_CHECKBOX;
8941fa88
VZ
190 }
191
4438caf4 192 if ( style & wxALIGN_RIGHT )
8941fa88 193 {
93212fee 194 msStyle |= BS_LEFTTEXT | BS_RIGHT;
8941fa88 195 }
4438caf4 196
8912d7eb 197 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, 0);
2bda0e17
KB
198}
199
2f259810
VZ
200// ----------------------------------------------------------------------------
201// wxCheckBox geometry
202// ----------------------------------------------------------------------------
2bda0e17 203
f68586e5 204wxSize wxCheckBox::DoGetBestSize() const
2bda0e17 205{
f6bcfd97 206 static int s_checkSize = 0;
81d66cf3 207
f6bcfd97
BP
208 if ( !s_checkSize )
209 {
210 wxScreenDC dc;
a756f210 211 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
2bda0e17 212
fb1a41cd 213 s_checkSize = dc.GetCharHeight();
f6bcfd97
BP
214 }
215
216 wxString str = wxGetWindowText(GetHWND());
217
218 int wCheckbox, hCheckbox;
4438caf4
VZ
219 if ( !str.IsEmpty() )
220 {
221 GetTextExtent(str, &wCheckbox, &hCheckbox);
f6bcfd97 222 wCheckbox += s_checkSize + GetCharWidth();
27fda0b6 223
f6bcfd97
BP
224 if ( hCheckbox < s_checkSize )
225 hCheckbox = s_checkSize;
4438caf4
VZ
226 }
227 else
2bda0e17 228 {
f6bcfd97
BP
229 wCheckbox = s_checkSize;
230 hCheckbox = s_checkSize;
2bda0e17 231 }
5a6371b9
JS
232#ifdef __WXWINCE__
233 hCheckbox += 1;
234#endif
2bda0e17 235
31582e4e
RD
236 wxSize best(wCheckbox, hCheckbox);
237 CacheBestSize(best);
238 return best;
2bda0e17
KB
239}
240
2f259810
VZ
241// ----------------------------------------------------------------------------
242// wxCheckBox operations
243// ----------------------------------------------------------------------------
244
debe6624 245void wxCheckBox::SetValue(bool val)
2bda0e17 246{
c3732409 247 Set3StateValue(val ? wxCHK_CHECKED : wxCHK_UNCHECKED);
2bda0e17
KB
248}
249
bfc6fde4 250bool wxCheckBox::GetValue() const
2bda0e17 251{
c3732409 252 return Get3StateValue() != wxCHK_UNCHECKED;
2bda0e17
KB
253}
254
2b5f62a0 255void wxCheckBox::Command(wxCommandEvent& event)
2bda0e17 256{
8941fa88
VZ
257 int state = event.GetInt();
258 wxCHECK_RET( (state == wxCHK_UNCHECKED) || (state == wxCHK_CHECKED)
259 || (state == wxCHK_UNDETERMINED),
260 wxT("event.GetInt() returned an invalid checkbox state") );
261
262 Set3StateValue((wxCheckBoxState) state);
2b5f62a0 263 ProcessCommand(event);
2bda0e17 264}
1e6feb95 265
8941fa88
VZ
266wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED == BST_UNCHECKED
267 && wxCHK_CHECKED == BST_CHECKED
268 && wxCHK_UNDETERMINED == BST_INDETERMINATE, EnumValuesIncorrect);
269
270void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
271{
2f259810
VZ
272 m_state = state;
273 if ( !IsOwnerDrawn() )
274 ::SendMessage(GetHwnd(), BM_SETCHECK, (WPARAM) state, 0);
275 else // owner drawn buttons don't react to this message
276 Refresh();
8941fa88
VZ
277}
278
279wxCheckBoxState wxCheckBox::DoGet3StateValue() const
280{
2f259810
VZ
281 return m_state;
282}
283
284bool wxCheckBox::MSWCommand(WXUINT cmd, WXWORD WXUNUSED(id))
285{
286 if ( cmd != BN_CLICKED && cmd != BN_DBLCLK )
287 return false;
288
289 // first update the value so that user event handler gets the new checkbox
290 // value
291
292 // ownerdrawn buttons don't manage their state themselves unlike usual
293 // auto checkboxes so do it ourselves in any case
294 wxCheckBoxState state;
295 if ( Is3rdStateAllowedForUser() )
296 {
297 state = (wxCheckBoxState)((m_state + 1) % 3);
298 }
299 else // 2 state checkbox (at least from users point of view)
300 {
301 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
302 state = m_state == wxCHK_UNCHECKED ? wxCHK_CHECKED : wxCHK_UNCHECKED;
303 }
304
305 DoSet3StateValue(state);
306
307
308 // generate the event
309 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId);
310
311 event.SetInt(state);
312 event.SetEventObject(this);
313 ProcessCommand(event);
314
315 return true;
316}
317
318// ----------------------------------------------------------------------------
319// owner drawn checkboxes stuff
320// ----------------------------------------------------------------------------
321
322bool wxCheckBox::SetForegroundColour(const wxColour& colour)
323{
324 if ( !wxCheckBoxBase::SetForegroundColour(colour) )
325 return false;
326
327 // the only way to change the checkbox foreground colour under Windows XP
328 // is to owner draw it
329 if ( wxUxThemeEngine::GetIfActive() )
330 MakeOwnerDrawn(colour.Ok());
331
332 return true;
333}
334
335bool wxCheckBox::IsOwnerDrawn() const
336{
337 return
338 (::GetWindowLong(GetHwnd(), GWL_STYLE) & BS_OWNERDRAW) == BS_OWNERDRAW;
339}
340
341void wxCheckBox::MakeOwnerDrawn(bool ownerDrawn)
342{
343 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
344
345 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
346 // them as on independent style bits
347 if ( ownerDrawn )
348 {
349 style &= ~(BS_CHECKBOX | BS_3STATE);
350 style |= BS_OWNERDRAW;
351
352 Connect(wxEVT_ENTER_WINDOW,
353 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
354 Connect(wxEVT_LEAVE_WINDOW,
355 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
356 Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
357 Connect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
358 Connect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
359 Connect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
360 }
361 else // reset to default colour
362 {
363 style &= ~BS_OWNERDRAW;
364 style |= HasFlag(wxCHK_3STATE) ? BS_3STATE : BS_CHECKBOX;
365
366 Disconnect(wxEVT_ENTER_WINDOW,
367 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
368 Disconnect(wxEVT_LEAVE_WINDOW,
369 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
370 Disconnect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
371 Disconnect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
372 Disconnect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
373 Disconnect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
374 }
375
376 ::SetWindowLong(GetHwnd(), GWL_STYLE, style);
b3433ee7
VZ
377
378 if ( !ownerDrawn )
379 {
380 // ensure that controls state is consistent with internal state
381 DoSet3StateValue(m_state);
382 }
2f259810
VZ
383}
384
385void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent& event)
386{
387 m_isHot = event.GetEventType() == wxEVT_ENTER_WINDOW;
388 if ( !m_isHot )
389 m_isPressed = false;
390
391 Refresh();
392
393 event.Skip();
394}
395
396void wxCheckBox::OnMouseLeft(wxMouseEvent& event)
397{
398 // TODO: we should capture the mouse here to be notified about left up
399 // event but this interferes with BN_CLICKED generation so if we
400 // want to do this we'd need to generate them ourselves
401 m_isPressed = event.GetEventType() == wxEVT_LEFT_DOWN;
402 Refresh();
403
404 event.Skip();
405}
406
407void wxCheckBox::OnFocus(wxFocusEvent& event)
408{
409 Refresh();
410
411 event.Skip();
412}
413
414bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
415{
416 DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)item;
417
418 if ( !IsOwnerDrawn() || dis->CtlType != ODT_BUTTON )
419 return wxCheckBoxBase::MSWOnDraw(item);
420
421 // calculate the rectangles for the check mark itself and the label
422 HDC hdc = dis->hDC;
423 RECT& rect = dis->rcItem;
424 RECT rectCheck,
425 rectLabel;
426 rectCheck.top =
427 rectLabel.top = rect.top;
428 rectCheck.bottom =
429 rectLabel.bottom = rect.bottom;
430 const int checkSize = GetBestSize().y;
431 const int MARGIN = 3;
432
433 const bool isRightAligned = HasFlag(wxALIGN_RIGHT);
434 if ( isRightAligned )
435 {
436 rectCheck.right = rect.right;
437 rectCheck.left = rectCheck.right - checkSize;
438
439 rectLabel.right = rectCheck.left - MARGIN;
440 rectLabel.left = rect.left;
441 }
442 else // normal, left-aligned checkbox
443 {
444 rectCheck.left = rect.left;
445 rectCheck.right = rectCheck.left + checkSize;
446
447 rectLabel.left = rectCheck.right + MARGIN;
448 rectLabel.right = rect.right;
449 }
450
451 // show we draw a focus rect?
452 const bool isFocused = m_isPressed || FindFocus() == this;
453
454
455 // draw the checkbox itself: note that this should really, really be in
456 // wxRendererNative but unfortunately we can't add a new virtual function
457 // to it without breaking backwards compatibility
458
459 // classic Win32 version -- this can be useful when we move this into
460#if 0
461 UINT state = DFCS_BUTTONCHECK;
462 if ( !IsEnabled() )
463 state |= DFCS_INACTIVE;
464 switch ( Get3StateValue() )
465 {
466 case wxCHK_CHECKED:
467 state |= DFCS_CHECKED;
468 break;
469
470 case wxCHK_UNDETERMINED:
471 state |= DFCS_PUSHED;
472 break;
473
474 default:
475 wxFAIL_MSG( _T("unexpected Get3StateValue() return value") );
476 // fall through
477
478 case wxCHK_UNCHECKED:
479 // no extra styles needed
480 break;
481 }
482
483 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
484 state |= DFCS_HOT;
485
486 if ( !::DrawFrameControl(hdc, &rectCheck, DFC_BUTTON, state) )
487 {
488 wxLogLastError(_T("DrawFrameControl(DFC_BUTTON)"));
489 }
490#else // XP version
491 wxUxThemeEngine *themeEngine = wxUxThemeEngine::GetIfActive();
492 if ( !themeEngine )
493 return false;
494
495 wxUxThemeHandle theme(this, L"BUTTON");
496 if ( !theme )
497 return false;
498
499 int state;
500 switch ( Get3StateValue() )
501 {
502 case wxCHK_CHECKED:
503 state = CBS_CHECKEDNORMAL;
504 break;
505
506 case wxCHK_UNDETERMINED:
507 state = CBS_MIXEDNORMAL;
508 break;
509
510 default:
511 wxFAIL_MSG( _T("unexpected Get3StateValue() return value") );
512 // fall through
513
514 case wxCHK_UNCHECKED:
515 state = CBS_UNCHECKEDNORMAL;
516 break;
517 }
518
519 if ( !IsEnabled() )
520 state += CBS_DISABLED_OFFSET;
521 else if ( m_isPressed )
522 state += CBS_PRESSED_OFFSET;
523 else if ( m_isHot )
524 state += CBS_HOT_OFFSET;
525
526 HRESULT hr = themeEngine->DrawThemeBackground
527 (
528 theme,
529 hdc,
530 BP_CHECKBOX,
531 state,
532 &rectCheck,
533 NULL
534 );
535 if ( FAILED(hr) )
536 {
537 wxLogApiError(_T("DrawThemeBackground(BP_CHECKBOX)"), hr);
538 }
539#endif // 0/1
540
541 // draw the text
542 const wxString& label = GetLabel();
543
544 // first we need to measure it
545 UINT fmt = DT_NOCLIP;
546
547 // drawing underlying doesn't look well with focus rect (and the native
548 // control doesn't do it)
549 if ( isFocused )
550 fmt |= DT_HIDEPREFIX;
551 if ( isRightAligned )
552 fmt |= DT_RIGHT;
553 // TODO: also use DT_HIDEPREFIX if the system is configured so
554
555 // we need to get the label real size first if we have to draw a focus rect
556 // around it
557 if ( isFocused )
558 {
559 if ( !::DrawText(hdc, label, label.length(), &rectLabel,
560 fmt | DT_CALCRECT) )
561 {
562 wxLogLastError(_T("DrawText(DT_CALCRECT)"));
563 }
564 }
565
566 if ( !IsEnabled() )
567 {
568 ::SetTextColor(hdc, ::GetSysColor(COLOR_GRAYTEXT));
569 }
570
571 if ( !::DrawText(hdc, label, label.length(), &rectLabel, fmt) )
572 {
573 wxLogLastError(_T("DrawText()"));
574 }
575
576 // finally draw the focus
577 if ( isFocused )
578 {
579 rectLabel.left--;
580 rectLabel.right++;
581 if ( !::DrawFocusRect(hdc, &rectLabel) )
582 {
583 wxLogLastError(_T("DrawFocusRect()"));
584 }
585 }
586
587 return true;
8941fa88
VZ
588}
589
1e6feb95 590#endif // wxUSE_CHECKBOX