1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/checklst.cpp
3 // Purpose: wxCheckListBox 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 #include "wx/wxprec.h"
26 #if wxUSE_CHECKLISTBOX
31 #include "wx/dcclient.h"
32 #include "wx/checklst.h"
33 #include "wx/validate.h"
36 #include "wx/univ/renderer.h"
37 #include "wx/univ/inphand.h"
38 #include "wx/univ/theme.h"
40 // ============================================================================
41 // implementation of wxCheckListBox
42 // ============================================================================
44 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 void wxCheckListBox::Init()
54 wxCheckListBox::wxCheckListBox(wxWindow
*parent
,
58 const wxArrayString
& choices
,
60 const wxValidator
& validator
,
65 Create(parent
, id
, pos
, size
, choices
, style
, validator
, name
);
68 bool wxCheckListBox::Create(wxWindow
*parent
,
72 const wxArrayString
& choices
,
74 const wxValidator
& validator
,
77 wxCArrayString
chs(choices
);
79 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
80 style
, validator
, name
);
83 bool wxCheckListBox::Create(wxWindow
*parent
,
88 const wxString choices
[],
90 const wxValidator
& validator
,
93 if ( !wxListBox::Create(parent
, id
, pos
, size
,
94 n
, choices
, style
, validator
, name
) )
97 CreateInputHandler(wxINP_HANDLER_CHECKLISTBOX
);
102 // ----------------------------------------------------------------------------
103 // wxCheckListBox functions
104 // ----------------------------------------------------------------------------
106 bool wxCheckListBox::IsChecked(size_t item
) const
108 wxCHECK_MSG( item
< m_checks
.GetCount(), false,
109 _T("invalid index in wxCheckListBox::IsChecked") );
111 return m_checks
[item
] != 0;
114 void wxCheckListBox::Check(size_t item
, bool check
)
116 wxCHECK_RET( item
< m_checks
.GetCount(),
117 _T("invalid index in wxCheckListBox::Check") );
119 // intermediate var is needed to avoid compiler warning with VC++
120 bool isChecked
= m_checks
[item
] != 0;
121 if ( check
!= isChecked
)
123 m_checks
[item
] = check
;
129 // ----------------------------------------------------------------------------
130 // methods forwarded to wxListBox
131 // ----------------------------------------------------------------------------
133 void wxCheckListBox::Delete(int n
)
135 wxCHECK_RET( IsValid(n
), _T("invalid index in wxListBox::Delete") );
137 wxListBox::Delete(n
);
139 m_checks
.RemoveAt(n
);
142 int wxCheckListBox::DoAppend(const wxString
& item
)
144 int pos
= wxListBox::DoAppend(item
);
146 // the item is initially unchecked
147 m_checks
.Insert(false, pos
);
152 void wxCheckListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
154 wxListBox::DoInsertItems(items
, pos
);
156 size_t count
= items
.GetCount();
157 for ( size_t n
= 0; n
< count
; n
++ )
159 m_checks
.Insert(false, pos
+ n
);
163 void wxCheckListBox::DoSetItems(const wxArrayString
& items
, void **clientData
)
165 // call it first as it does DoClear()
166 wxListBox::DoSetItems(items
, clientData
);
168 size_t count
= items
.GetCount();
169 for ( size_t n
= 0; n
< count
; n
++ )
175 void wxCheckListBox::DoClear()
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 wxSize
wxCheckListBox::DoGetBestClientSize() const
186 wxSize size
= wxListBox::DoGetBestClientSize();
187 size
.x
+= GetRenderer()->GetCheckBitmapSize().x
;
192 void wxCheckListBox::DoDrawRange(wxControlRenderer
*renderer
,
193 int itemFirst
, int itemLast
)
195 renderer
->DrawCheckItems(this, itemFirst
, itemLast
);
198 // ----------------------------------------------------------------------------
200 // ----------------------------------------------------------------------------
202 bool wxCheckListBox::PerformAction(const wxControlAction
& action
,
204 const wxString
& strArg
)
206 if ( action
== wxACTION_CHECKLISTBOX_TOGGLE
)
208 int sel
= (int)numArg
;
211 sel
= GetSelection();
216 Check(sel
, !IsChecked(sel
));
218 SendEvent(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, sel
);
223 return wxListBox::PerformAction(action
, numArg
, strArg
);
229 // ----------------------------------------------------------------------------
230 // wxStdCheckListboxInputHandler
231 // ----------------------------------------------------------------------------
233 wxStdCheckListboxInputHandler::
234 wxStdCheckListboxInputHandler(wxInputHandler
*inphand
)
235 : wxStdListboxInputHandler(inphand
)
239 bool wxStdCheckListboxInputHandler::HandleKey(wxInputConsumer
*consumer
,
240 const wxKeyEvent
& event
,
243 if ( pressed
&& (event
.GetKeyCode() == WXK_SPACE
) )
244 consumer
->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE
);
246 return wxStdListboxInputHandler::HandleKey(consumer
, event
, pressed
);
249 bool wxStdCheckListboxInputHandler::HandleMouse(wxInputConsumer
*consumer
,
250 const wxMouseEvent
& event
)
252 if ( event
.LeftDown() || event
.LeftDClick() )
254 wxCheckListBox
*lbox
= wxStaticCast(consumer
->GetInputWindow(), wxCheckListBox
);
257 wxPoint pt
= event
.GetPosition();
258 pt
-= consumer
->GetInputWindow()->GetClientAreaOrigin();
259 lbox
->CalcUnscrolledPosition(pt
.x
, pt
.y
, &x
, &y
);
261 wxRenderer
*renderer
= lbox
->GetRenderer();
262 x
-= renderer
->GetCheckItemMargin();
264 int item
= y
/ lbox
->GetLineHeight();
266 x
< renderer
->GetCheckBitmapSize().x
&&
268 (size_t)item
< lbox
->GetCount() )
270 lbox
->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE
, item
);
276 return wxStdListboxInputHandler::HandleMouse(consumer
, event
);
279 #endif // wxUSE_CHECKLISTBOX