1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 defined(wxUNIV_COMPATIBLE_MSW) && wxUNIV_COMPATIBLE_MSW
181 // FIXME: flag nowhere defined so perhaps should be removed?
183 // this looks better but is different from what wxMSW does
184 height
+= GetCharHeight()/2;
185 #endif // wxUNIV_COMPATIBLE_MSW
187 width
+= sizeBmp
.x
+ 2*GetCharWidth();
189 return wxSize(width
, height
);
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
196 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
201 case wxCHK_UNCHECKED
: status
= Status_Unchecked
; break;
202 case wxCHK_CHECKED
: status
= Status_Checked
; break;
203 default: wxFAIL_MSG(_T("Unknown checkbox state"));
204 case wxCHK_UNDETERMINED
: status
= Status_3rdState
; break;
206 if ( status
!= m_status
)
210 if ( m_status
== Status_Checked
)
220 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
224 case Status_Checked
: return wxCHK_CHECKED
;
225 case Status_Unchecked
: return wxCHK_UNCHECKED
;
226 default: /* go further */ break;
228 return wxCHK_UNDETERMINED
;
231 void wxCheckBox::Press()
241 void wxCheckBox::Release()
251 void wxCheckBox::Toggle()
255 Status status
= m_status
;
257 switch ( Get3StateValue() )
260 Set3StateValue(Is3rdStateAllowedForUser() ? wxCHK_UNDETERMINED
: wxCHK_UNCHECKED
);
263 case wxCHK_UNCHECKED
:
264 Set3StateValue(wxCHK_CHECKED
);
267 case wxCHK_UNDETERMINED
:
268 Set3StateValue(wxCHK_UNCHECKED
);
272 if( status
!= m_status
)
276 void wxCheckBox::ChangeValue(bool value
)
283 void wxCheckBox::SendEvent()
285 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, GetId());
286 InitCommandEvent(event
);
287 wxCheckBoxState state
= Get3StateValue();
289 // If the style flag to allow the user setting the undetermined state
290 // is not set, then skip the undetermined state and set it to unchecked.
291 if ( state
== wxCHK_UNDETERMINED
&& !Is3rdStateAllowedForUser() )
293 state
= wxCHK_UNCHECKED
;
294 Set3StateValue(state
);
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
305 bool wxCheckBox::PerformAction(const wxControlAction
& action
,
307 const wxString
& strArg
)
309 if ( action
== wxACTION_BUTTON_PRESS
)
311 else if ( action
== wxACTION_BUTTON_RELEASE
)
313 if ( action
== wxACTION_CHECKBOX_CHECK
)
315 else if ( action
== wxACTION_CHECKBOX_CLEAR
)
317 else if ( action
== wxACTION_CHECKBOX_TOGGLE
)
320 return wxControl::PerformAction(action
, numArg
, strArg
);
325 // ----------------------------------------------------------------------------
326 // wxStdCheckboxInputHandler
327 // ----------------------------------------------------------------------------
329 wxStdCheckboxInputHandler::wxStdCheckboxInputHandler(wxInputHandler
*inphand
)
330 : wxStdButtonInputHandler(inphand
)
334 bool wxStdCheckboxInputHandler::HandleActivation(wxInputConsumer
*consumer
,
335 bool WXUNUSED(activated
))
337 // only the focused checkbox appearance changes when the app gains/loses
339 return consumer
->GetInputWindow()->IsFocused();
342 #endif // wxUSE_CHECKBOX