1 /////////////////////////////////////////////////////////////////////////////
2 // Name: univ/checkbox.cpp
3 // Purpose: wxCheckBox implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
29 #include "wx/dcclient.h"
30 #include "wx/checkbox.h"
31 #include "wx/validate.h"
33 #include "wx/button.h" // for wxACTION_BUTTON_XXX
36 #include "wx/univ/theme.h"
37 #include "wx/univ/renderer.h"
38 #include "wx/univ/inphand.h"
39 #include "wx/univ/colschem.h"
41 // ============================================================================
43 // ============================================================================
45 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 void wxCheckBox::Init()
54 m_status
= Status_Unchecked
;
57 bool wxCheckBox::Create(wxWindow
*parent
,
59 const wxString
&label
,
63 const wxValidator
& validator
,
66 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
72 CreateInputHandler(wxINP_HANDLER_CHECKBOX
);
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 bool wxCheckBox::GetValue() const
83 return (Get3StateValue() != wxCHK_UNCHECKED
);
86 void wxCheckBox::SetValue(bool value
)
88 Set3StateValue( value
? wxCHK_CHECKED
: wxCHK_UNCHECKED
);
91 void wxCheckBox::OnCheck()
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 wxBitmap
wxCheckBox::GetBitmap(State state
, Status status
) const
102 wxBitmap bmp
= m_bitmaps
[state
][status
];
104 bmp
= m_bitmaps
[State_Normal
][status
];
109 void wxCheckBox::SetBitmap(const wxBitmap
& bmp
, State state
, Status status
)
111 m_bitmaps
[state
][status
] = bmp
;
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 wxCheckBox::State
wxCheckBox::GetState(int flags
) const
120 if ( flags
& wxCONTROL_DISABLED
)
121 return State_Disabled
;
122 else if ( flags
& wxCONTROL_PRESSED
)
123 return State_Pressed
;
124 else if ( flags
& wxCONTROL_CURRENT
)
125 return State_Current
;
130 void wxCheckBox::DoDraw(wxControlRenderer
*renderer
)
132 int flags
= GetStateFlags();
134 wxDC
& dc
= renderer
->GetDC();
135 dc
.SetFont(GetFont());
136 dc
.SetTextForeground(GetForegroundColour());
138 switch ( Get3StateValue() )
140 case wxCHK_CHECKED
: flags
|= wxCONTROL_CHECKED
; break;
141 case wxCHK_UNDETERMINED
: flags
|= wxCONTROL_UNDETERMINED
; break;
142 default: /* do nothing */ break;
145 wxBitmap
bitmap(GetBitmap(GetState(flags
), m_status
));
147 renderer
->GetRenderer()->
153 GetWindowStyle() & wxALIGN_RIGHT
? wxALIGN_RIGHT
158 // ----------------------------------------------------------------------------
159 // geometry calculations
160 // ----------------------------------------------------------------------------
162 wxSize
wxCheckBox::GetBitmapSize() const
164 wxBitmap bmp
= GetBitmap(State_Normal
, Status_Checked
);
165 return bmp
.Ok() ? wxSize(bmp
.GetWidth(), bmp
.GetHeight())
166 : GetRenderer()->GetCheckBitmapSize();
169 wxSize
wxCheckBox::DoGetBestClientSize() const
171 wxClientDC
dc(wxConstCast(this, wxCheckBox
));
172 dc
.SetFont(GetFont());
173 wxCoord width
, height
;
174 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
176 wxSize sizeBmp
= GetBitmapSize();
177 if ( height
< sizeBmp
.y
)
180 #if wxUNIV_COMPATIBLE_MSW
181 // this looks better but is different from what wxMSW does
182 height
+= GetCharHeight()/2;
183 #endif // wxUNIV_COMPATIBLE_MSW
185 width
+= sizeBmp
.x
+ 2*GetCharWidth();
187 return wxSize(width
, height
);
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
199 case wxCHK_UNCHECKED
: status
= Status_Unchecked
; break;
200 case wxCHK_CHECKED
: status
= Status_Checked
; break;
201 default: wxFAIL_MSG(_T("Unknown checkbox state"));
202 case wxCHK_UNDETERMINED
: status
= Status_3rdState
; break;
204 if ( status
!= m_status
)
208 if ( m_status
== Status_Checked
)
218 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
222 case Status_Checked
: return wxCHK_CHECKED
;
223 case Status_Unchecked
: return wxCHK_UNCHECKED
;
224 default: /* go further */ break;
226 return wxCHK_UNDETERMINED
;
229 void wxCheckBox::Press()
239 void wxCheckBox::Release()
249 void wxCheckBox::Toggle()
253 Status status
= m_status
;
255 switch ( Get3StateValue() )
258 Set3StateValue(Is3rdStateAllowedForUser() ? wxCHK_UNDETERMINED
: wxCHK_UNCHECKED
);
261 case wxCHK_UNCHECKED
:
262 Set3StateValue(wxCHK_CHECKED
);
265 case wxCHK_UNDETERMINED
:
266 Set3StateValue(wxCHK_UNCHECKED
);
270 if( status
!= m_status
)
274 void wxCheckBox::ChangeValue(bool value
)
281 void wxCheckBox::SendEvent()
283 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, GetId());
284 InitCommandEvent(event
);
285 wxCheckBoxState state
= Get3StateValue();
287 // If the style flag to allow the user setting the undetermined state
288 // is not set, then skip the undetermined state and set it to unchecked.
289 if ( state
== wxCHK_UNDETERMINED
&& !Is3rdStateAllowedForUser() )
291 state
= wxCHK_UNCHECKED
;
292 Set3StateValue(state
);
299 // ----------------------------------------------------------------------------
301 // ----------------------------------------------------------------------------
303 bool wxCheckBox::PerformAction(const wxControlAction
& action
,
305 const wxString
& strArg
)
307 if ( action
== wxACTION_BUTTON_PRESS
)
309 else if ( action
== wxACTION_BUTTON_RELEASE
)
311 if ( action
== wxACTION_CHECKBOX_CHECK
)
313 else if ( action
== wxACTION_CHECKBOX_CLEAR
)
315 else if ( action
== wxACTION_CHECKBOX_TOGGLE
)
318 return wxControl::PerformAction(action
, numArg
, strArg
);
323 // ----------------------------------------------------------------------------
324 // wxStdCheckboxInputHandler
325 // ----------------------------------------------------------------------------
327 wxStdCheckboxInputHandler::wxStdCheckboxInputHandler(wxInputHandler
*inphand
)
328 : wxStdButtonInputHandler(inphand
)
332 bool wxStdCheckboxInputHandler::HandleActivation(wxInputConsumer
*consumer
,
333 bool WXUNUSED(activated
))
335 // only the focused checkbox appearance changes when the app gains/loses
337 return consumer
->GetInputWindow()->IsFocused();
340 #endif // wxUSE_CHECKBOX