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
; break;
145 case wxCHK_UNDETERMINED
: flags
|= wxCONTROL_UNDETERMINED
; break;
146 default: /* do nothing */ break;
149 wxBitmap
bitmap(GetBitmap(GetState(flags
), m_status
));
151 renderer
->GetRenderer()->
157 GetWindowStyle() & wxALIGN_RIGHT
? wxALIGN_RIGHT
162 // ----------------------------------------------------------------------------
163 // geometry calculations
164 // ----------------------------------------------------------------------------
166 wxSize
wxCheckBox::GetBitmapSize() const
168 wxBitmap bmp
= GetBitmap(State_Normal
, Status_Checked
);
169 return bmp
.Ok() ? wxSize(bmp
.GetWidth(), bmp
.GetHeight())
170 : GetRenderer()->GetCheckBitmapSize();
173 wxSize
wxCheckBox::DoGetBestClientSize() const
175 wxClientDC
dc(wxConstCast(this, wxCheckBox
));
176 dc
.SetFont(GetFont());
177 wxCoord width
, height
;
178 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
180 wxSize sizeBmp
= GetBitmapSize();
181 if ( height
< sizeBmp
.y
)
184 #if wxUNIV_COMPATIBLE_MSW
185 // this looks better but is different from what wxMSW does
186 height
+= GetCharHeight()/2;
187 #endif // wxUNIV_COMPATIBLE_MSW
189 width
+= sizeBmp
.x
+ 2*GetCharWidth();
191 return wxSize(width
, height
);
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
203 case wxCHK_UNCHECKED
: status
= Status_Unchecked
; break;
204 case wxCHK_CHECKED
: status
= Status_Checked
; break;
205 default: wxFAIL_MSG(_T("Unknown checkbox state"));
206 case wxCHK_UNDETERMINED
: status
= Status_3rdState
; break;
208 if ( status
!= m_status
)
212 if ( m_status
== Status_Checked
)
222 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
226 case Status_Checked
: return wxCHK_CHECKED
;
227 case Status_Unchecked
: return wxCHK_UNCHECKED
;
228 default: /* go further */ break;
230 return wxCHK_UNDETERMINED
;
233 void wxCheckBox::Press()
243 void wxCheckBox::Release()
253 void wxCheckBox::Toggle()
257 Status status
= m_status
;
259 switch ( Get3StateValue() )
262 Set3StateValue(Is3rdStateAllowedForUser() ? wxCHK_UNDETERMINED
: wxCHK_UNCHECKED
);
265 case wxCHK_UNCHECKED
:
266 Set3StateValue(wxCHK_CHECKED
);
269 case wxCHK_UNDETERMINED
:
270 Set3StateValue(wxCHK_UNCHECKED
);
274 if( status
!= m_status
)
278 void wxCheckBox::ChangeValue(bool value
)
285 void wxCheckBox::SendEvent()
287 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, GetId());
288 InitCommandEvent(event
);
289 wxCheckBoxState state
= Get3StateValue();
291 // If the style flag to allow the user setting the undetermined state
292 // is not set, then skip the undetermined state and set it to unchecked.
293 if ( state
== wxCHK_UNDETERMINED
&& !Is3rdStateAllowedForUser() )
295 state
= wxCHK_UNCHECKED
;
296 Set3StateValue(state
);
303 // ----------------------------------------------------------------------------
305 // ----------------------------------------------------------------------------
307 bool wxCheckBox::PerformAction(const wxControlAction
& action
,
309 const wxString
& strArg
)
311 if ( action
== wxACTION_BUTTON_PRESS
)
313 else if ( action
== wxACTION_BUTTON_RELEASE
)
315 if ( action
== wxACTION_CHECKBOX_CHECK
)
317 else if ( action
== wxACTION_CHECKBOX_CLEAR
)
319 else if ( action
== wxACTION_CHECKBOX_TOGGLE
)
322 return wxControl::PerformAction(action
, numArg
, strArg
);
327 // ----------------------------------------------------------------------------
328 // wxStdCheckboxInputHandler
329 // ----------------------------------------------------------------------------
331 wxStdCheckboxInputHandler::wxStdCheckboxInputHandler(wxInputHandler
*inphand
)
332 : wxStdButtonInputHandler(inphand
)
336 bool wxStdCheckboxInputHandler::HandleActivation(wxInputConsumer
*consumer
,
337 bool WXUNUSED(activated
))
339 // only the focused checkbox appearance changes when the app gains/loses
341 return consumer
->GetInputWindow()->IsFocused();
344 #endif // wxUSE_CHECKBOX