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 // ----------------------------------------------------------------------------
43 // wxStdCheckboxInputHandler: handles the mouse events for the check and radio
44 // boxes (handling the keyboard input is simple, but its handling differs a
45 // lot between GTK and MSW, so a new class should be derived for this)
46 // ----------------------------------------------------------------------------
48 class WXDLLEXPORT wxStdCheckboxInputHandler
: public wxStdInputHandler
51 wxStdCheckboxInputHandler(wxInputHandler
*inphand
);
53 // we have to override this one as wxStdButtonInputHandler version works
54 // only with the buttons
55 virtual bool HandleActivation(wxInputConsumer
*consumer
, bool activated
);
58 // ============================================================================
60 // ============================================================================
62 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 void wxCheckBox::Init()
71 m_status
= Status_Unchecked
;
74 bool wxCheckBox::Create(wxWindow
*parent
,
76 const wxString
&label
,
80 const wxValidator
& validator
,
83 WXValidateStyle( &style
);
84 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
90 CreateInputHandler(wxINP_HANDLER_CHECKBOX
);
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 bool wxCheckBox::GetValue() const
101 return (Get3StateValue() != wxCHK_UNCHECKED
);
104 void wxCheckBox::SetValue(bool value
)
106 Set3StateValue( value
? wxCHK_CHECKED
: wxCHK_UNCHECKED
);
109 void wxCheckBox::OnCheck()
111 // we do nothing here
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 wxBitmap
wxCheckBox::GetBitmap(State state
, Status status
) const
120 wxBitmap bmp
= m_bitmaps
[state
][status
];
122 bmp
= m_bitmaps
[State_Normal
][status
];
127 void wxCheckBox::SetBitmap(const wxBitmap
& bmp
, State state
, Status status
)
129 m_bitmaps
[state
][status
] = bmp
;
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 wxCheckBox::State
wxCheckBox::GetState(int flags
) const
138 if ( flags
& wxCONTROL_DISABLED
)
139 return State_Disabled
;
140 else if ( flags
& wxCONTROL_PRESSED
)
141 return State_Pressed
;
142 else if ( flags
& wxCONTROL_CURRENT
)
143 return State_Current
;
148 void wxCheckBox::DoDraw(wxControlRenderer
*renderer
)
150 int flags
= GetStateFlags();
152 wxDC
& dc
= renderer
->GetDC();
153 dc
.SetFont(GetFont());
154 dc
.SetTextForeground(GetForegroundColour());
156 switch ( Get3StateValue() )
158 case wxCHK_CHECKED
: flags
|= wxCONTROL_CHECKED
; break;
159 case wxCHK_UNDETERMINED
: flags
|= wxCONTROL_UNDETERMINED
; break;
160 default: /* do nothing */ break;
163 wxBitmap
bitmap(GetBitmap(GetState(flags
), m_status
));
165 renderer
->GetRenderer()->
171 GetWindowStyle() & wxALIGN_RIGHT
? wxALIGN_RIGHT
176 // ----------------------------------------------------------------------------
177 // geometry calculations
178 // ----------------------------------------------------------------------------
180 wxSize
wxCheckBox::GetBitmapSize() const
182 wxBitmap bmp
= GetBitmap(State_Normal
, Status_Checked
);
183 return bmp
.Ok() ? wxSize(bmp
.GetWidth(), bmp
.GetHeight())
184 : GetRenderer()->GetCheckBitmapSize();
187 wxSize
wxCheckBox::DoGetBestClientSize() const
189 wxClientDC
dc(wxConstCast(this, wxCheckBox
));
190 dc
.SetFont(GetFont());
191 wxCoord width
, height
;
192 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
194 wxSize sizeBmp
= GetBitmapSize();
195 if ( height
< sizeBmp
.y
)
198 #if defined(wxUNIV_COMPATIBLE_MSW) && wxUNIV_COMPATIBLE_MSW
199 // FIXME: flag nowhere defined so perhaps should be removed?
201 // this looks better but is different from what wxMSW does
202 height
+= GetCharHeight()/2;
203 #endif // wxUNIV_COMPATIBLE_MSW
205 width
+= sizeBmp
.x
+ 2*GetCharWidth();
207 return wxSize(width
, height
);
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
214 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
219 case wxCHK_UNCHECKED
: status
= Status_Unchecked
; break;
220 case wxCHK_CHECKED
: status
= Status_Checked
; break;
221 default: wxFAIL_MSG(wxT("Unknown checkbox state"));
222 case wxCHK_UNDETERMINED
: status
= Status_3rdState
; break;
224 if ( status
!= m_status
)
228 if ( m_status
== Status_Checked
)
238 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
242 case Status_Checked
: return wxCHK_CHECKED
;
243 case Status_Unchecked
: return wxCHK_UNCHECKED
;
244 default: /* go further */ break;
246 return wxCHK_UNDETERMINED
;
249 void wxCheckBox::Press()
259 void wxCheckBox::Release()
269 void wxCheckBox::Toggle()
273 Status status
= m_status
;
275 switch ( Get3StateValue() )
278 Set3StateValue(Is3rdStateAllowedForUser() ? wxCHK_UNDETERMINED
: wxCHK_UNCHECKED
);
281 case wxCHK_UNCHECKED
:
282 Set3StateValue(wxCHK_CHECKED
);
285 case wxCHK_UNDETERMINED
:
286 Set3StateValue(wxCHK_UNCHECKED
);
290 if( status
!= m_status
)
294 void wxCheckBox::ChangeValue(bool value
)
301 void wxCheckBox::SendEvent()
303 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, GetId());
304 InitCommandEvent(event
);
305 wxCheckBoxState state
= Get3StateValue();
307 // If the style flag to allow the user setting the undetermined state
308 // is not set, then skip the undetermined state and set it to unchecked.
309 if ( state
== wxCHK_UNDETERMINED
&& !Is3rdStateAllowedForUser() )
311 state
= wxCHK_UNCHECKED
;
312 Set3StateValue(state
);
319 // ----------------------------------------------------------------------------
321 // ----------------------------------------------------------------------------
323 bool wxCheckBox::PerformAction(const wxControlAction
& action
,
325 const wxString
& strArg
)
327 if ( action
== wxACTION_BUTTON_PRESS
)
329 else if ( action
== wxACTION_BUTTON_RELEASE
)
331 if ( action
== wxACTION_CHECKBOX_CHECK
)
333 else if ( action
== wxACTION_CHECKBOX_CLEAR
)
335 else if ( action
== wxACTION_CHECKBOX_TOGGLE
)
338 return wxControl::PerformAction(action
, numArg
, strArg
);
344 wxInputHandler
*wxCheckBox::CreateStdInputHandler(wxInputHandler
*handlerDef
)
346 static wxStdCheckboxInputHandler
s_handler(handlerDef
);
351 // ----------------------------------------------------------------------------
352 // wxStdCheckboxInputHandler
353 // ----------------------------------------------------------------------------
355 wxStdCheckboxInputHandler::wxStdCheckboxInputHandler(wxInputHandler
*def
)
356 : wxStdInputHandler(wxButton::GetStdInputHandler(def
))
360 bool wxStdCheckboxInputHandler::HandleActivation(wxInputConsumer
*consumer
,
361 bool WXUNUSED(activated
))
363 // only the focused checkbox appearance changes when the app gains/loses
365 return consumer
->GetInputWindow()->IsFocused();
368 #endif // wxUSE_CHECKBOX