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 // ----------------------------------------------------------------------------
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 m_status
== Status_Checked
;
90 void wxCheckBox::SetValue(bool value
)
92 Status status
= value
? Status_Checked
: Status_Unchecked
;
93 if ( status
!= m_status
)
97 if ( m_status
== Status_Checked
)
107 void wxCheckBox::OnCheck()
109 // we do nothing here
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 wxBitmap
wxCheckBox::GetBitmap(State state
, Status status
) const
118 wxBitmap bmp
= m_bitmaps
[state
][status
];
120 bmp
= m_bitmaps
[State_Normal
][status
];
125 void wxCheckBox::SetBitmap(const wxBitmap
& bmp
, State state
, Status status
)
127 m_bitmaps
[state
][status
] = bmp
;
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 wxCheckBox::State
wxCheckBox::GetState(int flags
) const
136 if ( flags
& wxCONTROL_DISABLED
)
137 return State_Disabled
;
138 else if ( flags
& wxCONTROL_PRESSED
)
139 return State_Pressed
;
140 else if ( flags
& wxCONTROL_CURRENT
)
141 return State_Current
;
146 void wxCheckBox::DoDraw(wxControlRenderer
*renderer
)
148 int flags
= GetStateFlags();
150 wxDC
& dc
= renderer
->GetDC();
151 dc
.SetFont(GetFont());
152 dc
.SetTextForeground(GetForegroundColour());
154 if ( m_status
== Status_Checked
)
155 flags
|= wxCONTROL_CHECKED
;
157 wxBitmap
bitmap(GetBitmap(GetState(flags
), m_status
));
159 renderer
->GetRenderer()->
165 GetWindowStyle() & wxALIGN_RIGHT
? wxALIGN_RIGHT
170 // ----------------------------------------------------------------------------
171 // geometry calculations
172 // ----------------------------------------------------------------------------
174 wxSize
wxCheckBox::GetBitmapSize() const
176 wxBitmap bmp
= GetBitmap(State_Normal
, Status_Checked
);
177 return bmp
.Ok() ? wxSize(bmp
.GetWidth(), bmp
.GetHeight())
178 : GetRenderer()->GetCheckBitmapSize();
181 wxSize
wxCheckBox::DoGetBestClientSize() const
183 wxClientDC
dc(wxConstCast(this, wxCheckBox
));
184 dc
.SetFont(GetFont());
185 wxCoord width
, height
;
186 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
188 wxSize sizeBmp
= GetBitmapSize();
189 if ( height
< sizeBmp
.y
)
192 #if wxUNIV_COMPATIBLE_MSW
193 // this looks better but is different from what wxMSW does
194 height
+= GetCharHeight()/2;
195 #endif // wxUNIV_COMPATIBLE_MSW
197 width
+= sizeBmp
.x
+ 2*GetCharWidth();
199 return wxSize(width
, height
);
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
206 void wxCheckBox::Press()
216 void wxCheckBox::Release()
226 void wxCheckBox::Toggle()
230 ChangeValue(!GetValue());
233 void wxCheckBox::ChangeValue(bool value
)
240 void wxCheckBox::SendEvent()
242 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, GetId());
243 InitCommandEvent(event
);
244 event
.SetInt(IsChecked());
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
252 bool wxCheckBox::PerformAction(const wxControlAction
& action
,
254 const wxString
& strArg
)
256 if ( action
== wxACTION_BUTTON_PRESS
)
258 else if ( action
== wxACTION_BUTTON_RELEASE
)
260 if ( action
== wxACTION_CHECKBOX_CHECK
)
262 else if ( action
== wxACTION_CHECKBOX_CLEAR
)
264 else if ( action
== wxACTION_CHECKBOX_TOGGLE
)
267 return wxControl::PerformAction(action
, numArg
, strArg
);
272 // ----------------------------------------------------------------------------
273 // wxStdCheckboxInputHandler
274 // ----------------------------------------------------------------------------
276 wxStdCheckboxInputHandler::wxStdCheckboxInputHandler(wxInputHandler
*inphand
)
277 : wxStdButtonInputHandler(inphand
)
281 bool wxStdCheckboxInputHandler::HandleActivation(wxInputConsumer
*consumer
,
282 bool WXUNUSED(activated
))
284 // only the focused checkbox appearance changes when the app gains/loses
286 return consumer
->GetInputWindow()->IsFocused();
289 #endif // wxUSE_CHECKBOX