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 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
89 CreateInputHandler(wxINP_HANDLER_CHECKBOX
);
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 bool wxCheckBox::GetValue() const
100 return (Get3StateValue() != wxCHK_UNCHECKED
);
103 void wxCheckBox::SetValue(bool value
)
105 Set3StateValue( value
? wxCHK_CHECKED
: wxCHK_UNCHECKED
);
108 void wxCheckBox::OnCheck()
110 // we do nothing here
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 wxBitmap
wxCheckBox::GetBitmap(State state
, Status status
) const
119 wxBitmap bmp
= m_bitmaps
[state
][status
];
121 bmp
= m_bitmaps
[State_Normal
][status
];
126 void wxCheckBox::SetBitmap(const wxBitmap
& bmp
, State state
, Status status
)
128 m_bitmaps
[state
][status
] = bmp
;
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 wxCheckBox::State
wxCheckBox::GetState(int flags
) const
137 if ( flags
& wxCONTROL_DISABLED
)
138 return State_Disabled
;
139 else if ( flags
& wxCONTROL_PRESSED
)
140 return State_Pressed
;
141 else if ( flags
& wxCONTROL_CURRENT
)
142 return State_Current
;
147 void wxCheckBox::DoDraw(wxControlRenderer
*renderer
)
149 int flags
= GetStateFlags();
151 wxDC
& dc
= renderer
->GetDC();
152 dc
.SetFont(GetFont());
153 dc
.SetTextForeground(GetForegroundColour());
155 switch ( Get3StateValue() )
157 case wxCHK_CHECKED
: flags
|= wxCONTROL_CHECKED
; break;
158 case wxCHK_UNDETERMINED
: flags
|= wxCONTROL_UNDETERMINED
; break;
159 default: /* do nothing */ break;
162 wxBitmap
bitmap(GetBitmap(GetState(flags
), m_status
));
164 renderer
->GetRenderer()->
170 GetWindowStyle() & wxALIGN_RIGHT
? wxALIGN_RIGHT
175 // ----------------------------------------------------------------------------
176 // geometry calculations
177 // ----------------------------------------------------------------------------
179 wxSize
wxCheckBox::GetBitmapSize() const
181 wxBitmap bmp
= GetBitmap(State_Normal
, Status_Checked
);
182 return bmp
.Ok() ? wxSize(bmp
.GetWidth(), bmp
.GetHeight())
183 : GetRenderer()->GetCheckBitmapSize();
186 wxSize
wxCheckBox::DoGetBestClientSize() const
188 wxClientDC
dc(wxConstCast(this, wxCheckBox
));
189 dc
.SetFont(GetFont());
190 wxCoord width
, height
;
191 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
193 wxSize sizeBmp
= GetBitmapSize();
194 if ( height
< sizeBmp
.y
)
197 #if defined(wxUNIV_COMPATIBLE_MSW) && wxUNIV_COMPATIBLE_MSW
198 // FIXME: flag nowhere defined so perhaps should be removed?
200 // this looks better but is different from what wxMSW does
201 height
+= GetCharHeight()/2;
202 #endif // wxUNIV_COMPATIBLE_MSW
204 width
+= sizeBmp
.x
+ 2*GetCharWidth();
206 return wxSize(width
, height
);
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
213 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
218 case wxCHK_UNCHECKED
: status
= Status_Unchecked
; break;
219 case wxCHK_CHECKED
: status
= Status_Checked
; break;
220 default: wxFAIL_MSG(_T("Unknown checkbox state"));
221 case wxCHK_UNDETERMINED
: status
= Status_3rdState
; break;
223 if ( status
!= m_status
)
227 if ( m_status
== Status_Checked
)
237 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
241 case Status_Checked
: return wxCHK_CHECKED
;
242 case Status_Unchecked
: return wxCHK_UNCHECKED
;
243 default: /* go further */ break;
245 return wxCHK_UNDETERMINED
;
248 void wxCheckBox::Press()
258 void wxCheckBox::Release()
268 void wxCheckBox::Toggle()
272 Status status
= m_status
;
274 switch ( Get3StateValue() )
277 Set3StateValue(Is3rdStateAllowedForUser() ? wxCHK_UNDETERMINED
: wxCHK_UNCHECKED
);
280 case wxCHK_UNCHECKED
:
281 Set3StateValue(wxCHK_CHECKED
);
284 case wxCHK_UNDETERMINED
:
285 Set3StateValue(wxCHK_UNCHECKED
);
289 if( status
!= m_status
)
293 void wxCheckBox::ChangeValue(bool value
)
300 void wxCheckBox::SendEvent()
302 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, GetId());
303 InitCommandEvent(event
);
304 wxCheckBoxState state
= Get3StateValue();
306 // If the style flag to allow the user setting the undetermined state
307 // is not set, then skip the undetermined state and set it to unchecked.
308 if ( state
== wxCHK_UNDETERMINED
&& !Is3rdStateAllowedForUser() )
310 state
= wxCHK_UNCHECKED
;
311 Set3StateValue(state
);
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 bool wxCheckBox::PerformAction(const wxControlAction
& action
,
324 const wxString
& strArg
)
326 if ( action
== wxACTION_BUTTON_PRESS
)
328 else if ( action
== wxACTION_BUTTON_RELEASE
)
330 if ( action
== wxACTION_CHECKBOX_CHECK
)
332 else if ( action
== wxACTION_CHECKBOX_CLEAR
)
334 else if ( action
== wxACTION_CHECKBOX_TOGGLE
)
337 return wxControl::PerformAction(action
, numArg
, strArg
);
343 wxInputHandler
*wxCheckBox::CreateStdInputHandler(wxInputHandler
*handlerDef
)
345 static wxStdCheckboxInputHandler
s_handler(handlerDef
);
350 // ----------------------------------------------------------------------------
351 // wxStdCheckboxInputHandler
352 // ----------------------------------------------------------------------------
354 wxStdCheckboxInputHandler::wxStdCheckboxInputHandler(wxInputHandler
*def
)
355 : wxStdInputHandler(wxButton::GetStdInputHandler(def
))
359 bool wxStdCheckboxInputHandler::HandleActivation(wxInputConsumer
*consumer
,
360 bool WXUNUSED(activated
))
362 // only the focused checkbox appearance changes when the app gains/loses
364 return consumer
->GetInputWindow()->IsFocused();
367 #endif // wxUSE_CHECKBOX