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 wxCheckBoxBase::wxCheckBoxBase()
59 wxCheckBox::wxCheckBox()
64 wxCheckBox::wxCheckBox(wxWindow
*parent
,
66 const wxString
& label
,
70 const wxValidator
& validator
,
75 Create(parent
, id
, label
, pos
, size
, style
, validator
, name
);
78 void wxCheckBox::Init()
81 m_status
= Status_Unchecked
;
84 bool wxCheckBox::Create(wxWindow
*parent
,
86 const wxString
&label
,
90 const wxValidator
& validator
,
93 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
99 CreateInputHandler(wxINP_HANDLER_CHECKBOX
);
104 // ----------------------------------------------------------------------------
105 // checkbox interface
106 // ----------------------------------------------------------------------------
108 bool wxCheckBox::GetValue() const
110 return m_status
== Status_Checked
;
113 void wxCheckBox::SetValue(bool value
)
115 Status status
= value
? Status_Checked
: Status_Unchecked
;
116 if ( status
!= m_status
)
120 if ( m_status
== Status_Checked
)
130 void wxCheckBox::OnCheck()
132 // we do nothing here
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 wxBitmap
wxCheckBox::GetBitmap(State state
, Status status
) const
141 wxBitmap bmp
= m_bitmaps
[state
][status
];
143 bmp
= m_bitmaps
[State_Normal
][status
];
148 void wxCheckBox::SetBitmap(const wxBitmap
& bmp
, State state
, Status status
)
150 m_bitmaps
[state
][status
] = bmp
;
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 wxCheckBox::State
wxCheckBox::GetState(int flags
) const
159 if ( flags
& wxCONTROL_DISABLED
)
160 return State_Disabled
;
161 else if ( flags
& wxCONTROL_PRESSED
)
162 return State_Pressed
;
163 else if ( flags
& wxCONTROL_CURRENT
)
164 return State_Current
;
169 void wxCheckBox::DoDraw(wxControlRenderer
*renderer
)
171 int flags
= GetStateFlags();
173 wxDC
& dc
= renderer
->GetDC();
174 dc
.SetFont(GetFont());
175 dc
.SetTextForeground(GetForegroundColour());
177 if ( m_status
== Status_Checked
)
178 flags
|= wxCONTROL_CHECKED
;
180 wxBitmap
bitmap(GetBitmap(GetState(flags
), m_status
));
182 renderer
->GetRenderer()->
188 GetWindowStyle() & wxALIGN_RIGHT
? wxALIGN_RIGHT
193 // ----------------------------------------------------------------------------
194 // geometry calculations
195 // ----------------------------------------------------------------------------
197 wxSize
wxCheckBox::GetBitmapSize() const
199 wxBitmap bmp
= GetBitmap(State_Normal
, Status_Checked
);
200 return bmp
.Ok() ? wxSize(bmp
.GetWidth(), bmp
.GetHeight())
201 : GetRenderer()->GetCheckBitmapSize();
204 wxSize
wxCheckBox::DoGetBestClientSize() const
206 wxClientDC
dc(wxConstCast(this, wxCheckBox
));
207 dc
.SetFont(GetFont());
208 wxCoord width
, height
;
209 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
211 wxSize sizeBmp
= GetBitmapSize();
212 if ( height
< sizeBmp
.y
)
215 #if wxUNIV_COMPATIBLE_MSW
216 // this looks better but is different from what wxMSW does
217 height
+= GetCharHeight()/2;
218 #endif // wxUNIV_COMPATIBLE_MSW
220 width
+= sizeBmp
.x
+ 2*GetCharWidth();
222 return wxSize(width
, height
);
225 // ----------------------------------------------------------------------------
227 // ----------------------------------------------------------------------------
229 void wxCheckBox::Press()
239 void wxCheckBox::Release()
249 void wxCheckBox::Toggle()
253 ChangeValue(!GetValue());
256 void wxCheckBox::ChangeValue(bool value
)
263 void wxCheckBox::SendEvent()
265 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, GetId());
266 InitCommandEvent(event
);
267 event
.SetInt(IsChecked());
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 bool wxCheckBox::PerformAction(const wxControlAction
& action
,
277 const wxString
& strArg
)
279 if ( action
== wxACTION_BUTTON_PRESS
)
281 else if ( action
== wxACTION_BUTTON_RELEASE
)
283 if ( action
== wxACTION_CHECKBOX_CHECK
)
285 else if ( action
== wxACTION_CHECKBOX_CLEAR
)
287 else if ( action
== wxACTION_CHECKBOX_TOGGLE
)
290 return wxControl::PerformAction(action
, numArg
, strArg
);
295 // ----------------------------------------------------------------------------
296 // wxStdCheckboxInputHandler
297 // ----------------------------------------------------------------------------
299 wxStdCheckboxInputHandler::wxStdCheckboxInputHandler(wxInputHandler
*inphand
)
300 : wxStdButtonInputHandler(inphand
)
304 bool wxStdCheckboxInputHandler::HandleActivation(wxInputConsumer
*consumer
,
305 bool WXUNUSED(activated
))
307 // only the focused checkbox appearance changes when the app gains/loses
309 return consumer
->GetInputWindow()->IsFocused();
312 #endif // wxUSE_CHECKBOX