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 license
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
, wxDefaultValidator
, 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 renderer
->GetRenderer()->
160 GetBitmap(GetState(flags
), m_status
),
163 GetWindowStyle() & wxALIGN_RIGHT
? wxALIGN_RIGHT
168 // ----------------------------------------------------------------------------
169 // geometry calculations
170 // ----------------------------------------------------------------------------
172 wxSize
wxCheckBox::GetBitmapSize() const
174 wxBitmap bmp
= GetBitmap(State_Normal
, Status_Checked
);
175 return bmp
.Ok() ? wxSize(bmp
.GetWidth(), bmp
.GetHeight())
176 : GetRenderer()->GetCheckBitmapSize();
179 wxSize
wxCheckBox::DoGetBestClientSize() const
181 wxClientDC
dc(wxConstCast(this, wxCheckBox
));
182 dc
.SetFont(GetFont());
183 wxCoord width
, height
;
184 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
186 wxSize sizeBmp
= GetBitmapSize();
187 if ( height
< sizeBmp
.y
)
190 #if wxUNIV_COMPATIBLE_MSW
191 // this looks better but is different from what wxMSW does
192 height
+= GetCharHeight()/2;
193 #endif // wxUNIV_COMPATIBLE_MSW
195 width
+= sizeBmp
.x
+ 2*GetCharWidth();
197 return wxSize(width
, height
);
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 void wxCheckBox::Press()
214 void wxCheckBox::Release()
224 void wxCheckBox::Toggle()
228 ChangeValue(!GetValue());
231 void wxCheckBox::ChangeValue(bool value
)
238 void wxCheckBox::SendEvent()
240 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, GetId());
241 InitCommandEvent(event
);
242 event
.SetInt(IsChecked());
246 // ----------------------------------------------------------------------------
248 // ----------------------------------------------------------------------------
250 bool wxCheckBox::PerformAction(const wxControlAction
& action
,
252 const wxString
& strArg
)
254 if ( action
== wxACTION_BUTTON_PRESS
)
256 else if ( action
== wxACTION_BUTTON_RELEASE
)
258 if ( action
== wxACTION_CHECKBOX_CHECK
)
260 else if ( action
== wxACTION_CHECKBOX_CLEAR
)
262 else if ( action
== wxACTION_CHECKBOX_TOGGLE
)
265 return wxControl::PerformAction(action
, numArg
, strArg
);
270 // ----------------------------------------------------------------------------
271 // wxStdCheckboxInputHandler
272 // ----------------------------------------------------------------------------
274 wxStdCheckboxInputHandler::wxStdCheckboxInputHandler(wxInputHandler
*inphand
)
275 : wxStdButtonInputHandler(inphand
)
279 bool wxStdCheckboxInputHandler::HandleActivation(wxInputConsumer
*consumer
,
282 // only the focused checkbox appearance changes when the app gains/loses
284 return consumer
->GetInputWindow()->IsFocused();
287 #endif // wxUSE_CHECKBOX