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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "univcheckbox.h"
24 #include "wx/wxprec.h"
33 #include "wx/dcclient.h"
34 #include "wx/checkbox.h"
35 #include "wx/validate.h"
37 #include "wx/button.h" // for wxACTION_BUTTON_XXX
40 #include "wx/univ/theme.h"
41 #include "wx/univ/renderer.h"
42 #include "wx/univ/inphand.h"
43 #include "wx/univ/colschem.h"
45 // ============================================================================
47 // ============================================================================
49 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 void wxCheckBox::Init()
58 m_status
= Status_Unchecked
;
61 bool wxCheckBox::Create(wxWindow
*parent
,
63 const wxString
&label
,
67 const wxValidator
& validator
,
70 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
76 CreateInputHandler(wxINP_HANDLER_CHECKBOX
);
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 bool wxCheckBox::GetValue() const
87 return (Get3StateValue() != wxCHK_UNCHECKED
);
90 void wxCheckBox::SetValue(bool value
)
92 Set3StateValue( value
? wxCHK_CHECKED
: wxCHK_UNCHECKED
);
95 void wxCheckBox::OnCheck()
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 wxBitmap
wxCheckBox::GetBitmap(State state
, Status status
) const
106 wxBitmap bmp
= m_bitmaps
[state
][status
];
108 bmp
= m_bitmaps
[State_Normal
][status
];
113 void wxCheckBox::SetBitmap(const wxBitmap
& bmp
, State state
, Status status
)
115 m_bitmaps
[state
][status
] = bmp
;
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 wxCheckBox::State
wxCheckBox::GetState(int flags
) const
124 if ( flags
& wxCONTROL_DISABLED
)
125 return State_Disabled
;
126 else if ( flags
& wxCONTROL_PRESSED
)
127 return State_Pressed
;
128 else if ( flags
& wxCONTROL_CURRENT
)
129 return State_Current
;
134 void wxCheckBox::DoDraw(wxControlRenderer
*renderer
)
136 int flags
= GetStateFlags();
138 wxDC
& dc
= renderer
->GetDC();
139 dc
.SetFont(GetFont());
140 dc
.SetTextForeground(GetForegroundColour());
142 switch ( Get3StateValue() )
144 case wxCHK_CHECKED
: flags
|= wxCONTROL_CHECKED
;
145 case wxCHK_UNDETERMINED
: flags
|= wxCONTROL_UNDETERMINED
;
148 wxBitmap
bitmap(GetBitmap(GetState(flags
), m_status
));
150 renderer
->GetRenderer()->
156 GetWindowStyle() & wxALIGN_RIGHT
? wxALIGN_RIGHT
161 // ----------------------------------------------------------------------------
162 // geometry calculations
163 // ----------------------------------------------------------------------------
165 wxSize
wxCheckBox::GetBitmapSize() const
167 wxBitmap bmp
= GetBitmap(State_Normal
, Status_Checked
);
168 return bmp
.Ok() ? wxSize(bmp
.GetWidth(), bmp
.GetHeight())
169 : GetRenderer()->GetCheckBitmapSize();
172 wxSize
wxCheckBox::DoGetBestClientSize() const
174 wxClientDC
dc(wxConstCast(this, wxCheckBox
));
175 dc
.SetFont(GetFont());
176 wxCoord width
, height
;
177 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
179 wxSize sizeBmp
= GetBitmapSize();
180 if ( height
< sizeBmp
.y
)
183 #if wxUNIV_COMPATIBLE_MSW
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
;
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