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