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"
28 #include "wx/checkbox.h"
31 #include "wx/dcclient.h"
32 #include "wx/validate.h"
34 #include "wx/button.h" // for wxACTION_BUTTON_XXX
37 #include "wx/univ/theme.h"
38 #include "wx/univ/renderer.h"
39 #include "wx/univ/inphand.h"
40 #include "wx/univ/colschem.h"
42 // ============================================================================
44 // ============================================================================
46 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 void wxCheckBox::Init()
55 m_status
= Status_Unchecked
;
58 bool wxCheckBox::Create(wxWindow
*parent
,
60 const wxString
&label
,
64 const wxValidator
& validator
,
67 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
73 CreateInputHandler(wxINP_HANDLER_CHECKBOX
);
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 bool wxCheckBox::GetValue() const
84 return (Get3StateValue() != wxCHK_UNCHECKED
);
87 void wxCheckBox::SetValue(bool value
)
89 Set3StateValue( value
? wxCHK_CHECKED
: wxCHK_UNCHECKED
);
92 void wxCheckBox::OnCheck()
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 wxBitmap
wxCheckBox::GetBitmap(State state
, Status status
) const
103 wxBitmap bmp
= m_bitmaps
[state
][status
];
105 bmp
= m_bitmaps
[State_Normal
][status
];
110 void wxCheckBox::SetBitmap(const wxBitmap
& bmp
, State state
, Status status
)
112 m_bitmaps
[state
][status
] = bmp
;
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 wxCheckBox::State
wxCheckBox::GetState(int flags
) const
121 if ( flags
& wxCONTROL_DISABLED
)
122 return State_Disabled
;
123 else if ( flags
& wxCONTROL_PRESSED
)
124 return State_Pressed
;
125 else if ( flags
& wxCONTROL_CURRENT
)
126 return State_Current
;
131 void wxCheckBox::DoDraw(wxControlRenderer
*renderer
)
133 int flags
= GetStateFlags();
135 wxDC
& dc
= renderer
->GetDC();
136 dc
.SetFont(GetFont());
137 dc
.SetTextForeground(GetForegroundColour());
139 switch ( Get3StateValue() )
141 case wxCHK_CHECKED
: flags
|= wxCONTROL_CHECKED
; break;
142 case wxCHK_UNDETERMINED
: flags
|= wxCONTROL_UNDETERMINED
; break;
143 default: /* do nothing */ break;
146 wxBitmap
bitmap(GetBitmap(GetState(flags
), m_status
));
148 renderer
->GetRenderer()->
154 GetWindowStyle() & wxALIGN_RIGHT
? wxALIGN_RIGHT
159 // ----------------------------------------------------------------------------
160 // geometry calculations
161 // ----------------------------------------------------------------------------
163 wxSize
wxCheckBox::GetBitmapSize() const
165 wxBitmap bmp
= GetBitmap(State_Normal
, Status_Checked
);
166 return bmp
.Ok() ? wxSize(bmp
.GetWidth(), bmp
.GetHeight())
167 : GetRenderer()->GetCheckBitmapSize();
170 wxSize
wxCheckBox::DoGetBestClientSize() const
172 wxClientDC
dc(wxConstCast(this, wxCheckBox
));
173 dc
.SetFont(GetFont());
174 wxCoord width
, height
;
175 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
177 wxSize sizeBmp
= GetBitmapSize();
178 if ( height
< sizeBmp
.y
)
181 #if defined(wxUNIV_COMPATIBLE_MSW) && wxUNIV_COMPATIBLE_MSW
182 // FIXME: flag nowhere defined so perhaps should be removed?
184 // this looks better but is different from what wxMSW does
185 height
+= GetCharHeight()/2;
186 #endif // wxUNIV_COMPATIBLE_MSW
188 width
+= sizeBmp
.x
+ 2*GetCharWidth();
190 return wxSize(width
, height
);
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
197 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
202 case wxCHK_UNCHECKED
: status
= Status_Unchecked
; break;
203 case wxCHK_CHECKED
: status
= Status_Checked
; break;
204 default: wxFAIL_MSG(_T("Unknown checkbox state"));
205 case wxCHK_UNDETERMINED
: status
= Status_3rdState
; break;
207 if ( status
!= m_status
)
211 if ( m_status
== Status_Checked
)
221 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
225 case Status_Checked
: return wxCHK_CHECKED
;
226 case Status_Unchecked
: return wxCHK_UNCHECKED
;
227 default: /* go further */ break;
229 return wxCHK_UNDETERMINED
;
232 void wxCheckBox::Press()
242 void wxCheckBox::Release()
252 void wxCheckBox::Toggle()
256 Status status
= m_status
;
258 switch ( Get3StateValue() )
261 Set3StateValue(Is3rdStateAllowedForUser() ? wxCHK_UNDETERMINED
: wxCHK_UNCHECKED
);
264 case wxCHK_UNCHECKED
:
265 Set3StateValue(wxCHK_CHECKED
);
268 case wxCHK_UNDETERMINED
:
269 Set3StateValue(wxCHK_UNCHECKED
);
273 if( status
!= m_status
)
277 void wxCheckBox::ChangeValue(bool value
)
284 void wxCheckBox::SendEvent()
286 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, GetId());
287 InitCommandEvent(event
);
288 wxCheckBoxState state
= Get3StateValue();
290 // If the style flag to allow the user setting the undetermined state
291 // is not set, then skip the undetermined state and set it to unchecked.
292 if ( state
== wxCHK_UNDETERMINED
&& !Is3rdStateAllowedForUser() )
294 state
= wxCHK_UNCHECKED
;
295 Set3StateValue(state
);
302 // ----------------------------------------------------------------------------
304 // ----------------------------------------------------------------------------
306 bool wxCheckBox::PerformAction(const wxControlAction
& action
,
308 const wxString
& strArg
)
310 if ( action
== wxACTION_BUTTON_PRESS
)
312 else if ( action
== wxACTION_BUTTON_RELEASE
)
314 if ( action
== wxACTION_CHECKBOX_CHECK
)
316 else if ( action
== wxACTION_CHECKBOX_CLEAR
)
318 else if ( action
== wxACTION_CHECKBOX_TOGGLE
)
321 return wxControl::PerformAction(action
, numArg
, strArg
);
326 // ----------------------------------------------------------------------------
327 // wxStdCheckboxInputHandler
328 // ----------------------------------------------------------------------------
330 wxStdCheckboxInputHandler::wxStdCheckboxInputHandler(wxInputHandler
*inphand
)
331 : wxStdButtonInputHandler(inphand
)
335 bool wxStdCheckboxInputHandler::HandleActivation(wxInputConsumer
*consumer
,
336 bool WXUNUSED(activated
))
338 // only the focused checkbox appearance changes when the app gains/loses
340 return consumer
->GetInputWindow()->IsFocused();
343 #endif // wxUSE_CHECKBOX