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
) )
73 m_hasDialogBackground
= TRUE
;
78 CreateInputHandler(wxINP_HANDLER_CHECKBOX
);
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 bool wxCheckBox::GetValue() const
89 return m_status
== Status_Checked
;
92 void wxCheckBox::SetValue(bool value
)
94 Status status
= value
? Status_Checked
: Status_Unchecked
;
95 if ( status
!= m_status
)
99 if ( m_status
== Status_Checked
)
109 void wxCheckBox::OnCheck()
111 // we do nothing here
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 wxBitmap
wxCheckBox::GetBitmap(State state
, Status status
) const
120 wxBitmap bmp
= m_bitmaps
[state
][status
];
122 bmp
= m_bitmaps
[State_Normal
][status
];
127 void wxCheckBox::SetBitmap(const wxBitmap
& bmp
, State state
, Status status
)
129 m_bitmaps
[state
][status
] = bmp
;
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 wxCheckBox::State
wxCheckBox::GetState(int flags
) const
138 if ( flags
& wxCONTROL_DISABLED
)
139 return State_Disabled
;
140 else if ( flags
& wxCONTROL_PRESSED
)
141 return State_Pressed
;
142 else if ( flags
& wxCONTROL_CURRENT
)
143 return State_Current
;
148 void wxCheckBox::DoDraw(wxControlRenderer
*renderer
)
150 int flags
= GetStateFlags();
152 wxDC
& dc
= renderer
->GetDC();
153 dc
.SetFont(GetFont());
154 dc
.SetTextForeground(GetForegroundColour());
156 if ( m_status
== Status_Checked
)
157 flags
|= wxCONTROL_CHECKED
;
159 wxBitmap
bitmap(GetBitmap(GetState(flags
), m_status
));
161 renderer
->GetRenderer()->
167 GetWindowStyle() & wxALIGN_RIGHT
? wxALIGN_RIGHT
172 // ----------------------------------------------------------------------------
173 // geometry calculations
174 // ----------------------------------------------------------------------------
176 wxSize
wxCheckBox::GetBitmapSize() const
178 wxBitmap bmp
= GetBitmap(State_Normal
, Status_Checked
);
179 return bmp
.Ok() ? wxSize(bmp
.GetWidth(), bmp
.GetHeight())
180 : GetRenderer()->GetCheckBitmapSize();
183 wxSize
wxCheckBox::DoGetBestClientSize() const
185 wxClientDC
dc(wxConstCast(this, wxCheckBox
));
186 dc
.SetFont(GetFont());
187 wxCoord width
, height
;
188 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
190 wxSize sizeBmp
= GetBitmapSize();
191 if ( height
< sizeBmp
.y
)
194 #if wxUNIV_COMPATIBLE_MSW
195 // this looks better but is different from what wxMSW does
196 height
+= GetCharHeight()/2;
197 #endif // wxUNIV_COMPATIBLE_MSW
199 width
+= sizeBmp
.x
+ 2*GetCharWidth();
201 return wxSize(width
, height
);
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 void wxCheckBox::Press()
218 void wxCheckBox::Release()
228 void wxCheckBox::Toggle()
232 ChangeValue(!GetValue());
235 void wxCheckBox::ChangeValue(bool value
)
242 void wxCheckBox::SendEvent()
244 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, GetId());
245 InitCommandEvent(event
);
246 event
.SetInt(IsChecked());
250 // ----------------------------------------------------------------------------
252 // ----------------------------------------------------------------------------
254 bool wxCheckBox::PerformAction(const wxControlAction
& action
,
256 const wxString
& strArg
)
258 if ( action
== wxACTION_BUTTON_PRESS
)
260 else if ( action
== wxACTION_BUTTON_RELEASE
)
262 if ( action
== wxACTION_CHECKBOX_CHECK
)
264 else if ( action
== wxACTION_CHECKBOX_CLEAR
)
266 else if ( action
== wxACTION_CHECKBOX_TOGGLE
)
269 return wxControl::PerformAction(action
, numArg
, strArg
);
274 // ----------------------------------------------------------------------------
275 // wxStdCheckboxInputHandler
276 // ----------------------------------------------------------------------------
278 wxStdCheckboxInputHandler::wxStdCheckboxInputHandler(wxInputHandler
*inphand
)
279 : wxStdButtonInputHandler(inphand
)
283 bool wxStdCheckboxInputHandler::HandleActivation(wxInputConsumer
*consumer
,
286 // only the focused checkbox appearance changes when the app gains/loses
288 return consumer
->GetInputWindow()->IsFocused();
291 #endif // wxUSE_CHECKBOX