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
28 #include "wx/checklst.h"
32 #include "wx/dcclient.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 // wxStdCheckListBoxInputHandler
42 // ----------------------------------------------------------------------------
44 class WXDLLEXPORT wxStdCheckListboxInputHandler
: public wxStdInputHandler
47 wxStdCheckListboxInputHandler(wxInputHandler
*inphand
);
49 virtual bool HandleKey(wxInputConsumer
*consumer
,
50 const wxKeyEvent
& event
,
52 virtual bool HandleMouse(wxInputConsumer
*consumer
,
53 const wxMouseEvent
& event
);
56 // ============================================================================
57 // implementation of wxCheckListBox
58 // ============================================================================
60 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 void wxCheckListBox::Init()
70 wxCheckListBox::wxCheckListBox(wxWindow
*parent
,
74 const wxArrayString
& choices
,
76 const wxValidator
& validator
,
81 Create(parent
, id
, pos
, size
, choices
, style
, validator
, name
);
84 bool wxCheckListBox::Create(wxWindow
*parent
,
88 const wxArrayString
& choices
,
90 const wxValidator
& validator
,
93 wxCArrayString
chs(choices
);
95 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
96 style
, validator
, name
);
99 bool wxCheckListBox::Create(wxWindow
*parent
,
104 const wxString choices
[],
106 const wxValidator
& validator
,
107 const wxString
&name
)
109 if ( !wxListBox::Create(parent
, id
, pos
, size
,
110 n
, choices
, style
, validator
, name
) )
113 CreateInputHandler(wxINP_HANDLER_CHECKLISTBOX
);
118 // ----------------------------------------------------------------------------
119 // wxCheckListBox functions
120 // ----------------------------------------------------------------------------
122 bool wxCheckListBox::IsChecked(unsigned int item
) const
124 wxCHECK_MSG( IsValid(item
), false,
125 _T("invalid index in wxCheckListBox::IsChecked") );
127 return m_checks
[item
] != 0;
130 void wxCheckListBox::Check(unsigned int item
, bool check
)
132 wxCHECK_RET( IsValid(item
),
133 _T("invalid index in wxCheckListBox::Check") );
135 // intermediate var is needed to avoid compiler warning with VC++
136 bool isChecked
= m_checks
[item
] != 0;
137 if ( check
!= isChecked
)
139 m_checks
[item
] = check
;
145 // ----------------------------------------------------------------------------
146 // methods forwarded to wxListBox
147 // ----------------------------------------------------------------------------
149 void wxCheckListBox::Delete(unsigned int n
)
151 wxCHECK_RET( IsValid(n
), _T("invalid index in wxListBox::Delete") );
153 wxListBox::Delete(n
);
155 m_checks
.RemoveAt(n
);
158 int wxCheckListBox::DoAppend(const wxString
& item
)
160 int pos
= wxListBox::DoAppend(item
);
162 // the item is initially unchecked
163 m_checks
.Insert(false, pos
);
168 void wxCheckListBox::DoInsertItems(const wxArrayString
& items
, unsigned int pos
)
170 wxListBox::DoInsertItems(items
, pos
);
172 unsigned int count
= items
.GetCount();
173 for ( unsigned int n
= 0; n
< count
; n
++ )
175 m_checks
.Insert(false, pos
+ n
);
179 void wxCheckListBox::DoSetItems(const wxArrayString
& items
, void **clientData
)
181 // call it first as it does DoClear()
182 wxListBox::DoSetItems(items
, clientData
);
184 unsigned int count
= items
.GetCount();
185 for ( unsigned int n
= 0; n
< count
; n
++ )
191 void wxCheckListBox::DoClear()
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
200 wxSize
wxCheckListBox::DoGetBestClientSize() const
202 wxSize size
= wxListBox::DoGetBestClientSize();
203 size
.x
+= GetRenderer()->GetCheckBitmapSize().x
;
208 void wxCheckListBox::DoDrawRange(wxControlRenderer
*renderer
,
209 int itemFirst
, int itemLast
)
211 renderer
->DrawCheckItems(this, itemFirst
, itemLast
);
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 bool wxCheckListBox::PerformAction(const wxControlAction
& action
,
220 const wxString
& strArg
)
222 if ( action
== wxACTION_CHECKLISTBOX_TOGGLE
)
224 int sel
= (int)numArg
;
227 sel
= GetSelection();
232 Check(sel
, !IsChecked(sel
));
234 SendEvent(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, sel
);
239 return wxListBox::PerformAction(action
, numArg
, strArg
);
246 wxInputHandler
*wxCheckListBox::GetStdInputHandler(wxInputHandler
*handlerDef
)
248 static wxStdCheckListboxInputHandler
s_handler(handlerDef
);
253 // ----------------------------------------------------------------------------
254 // wxStdCheckListboxInputHandler
255 // ----------------------------------------------------------------------------
257 wxStdCheckListboxInputHandler::
258 wxStdCheckListboxInputHandler(wxInputHandler
*inphand
)
259 : wxStdInputHandler(wxListBox::GetStdInputHandler(inphand
))
263 bool wxStdCheckListboxInputHandler::HandleKey(wxInputConsumer
*consumer
,
264 const wxKeyEvent
& event
,
267 if ( pressed
&& (event
.GetKeyCode() == WXK_SPACE
) )
268 consumer
->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE
);
270 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
273 bool wxStdCheckListboxInputHandler::HandleMouse(wxInputConsumer
*consumer
,
274 const wxMouseEvent
& event
)
276 if ( event
.LeftDown() || event
.LeftDClick() )
278 wxCheckListBox
*lbox
= wxStaticCast(consumer
->GetInputWindow(), wxCheckListBox
);
281 wxPoint pt
= event
.GetPosition();
282 pt
-= consumer
->GetInputWindow()->GetClientAreaOrigin();
283 lbox
->CalcUnscrolledPosition(pt
.x
, pt
.y
, &x
, &y
);
285 wxRenderer
*renderer
= lbox
->GetRenderer();
286 x
-= renderer
->GetCheckItemMargin();
288 int item
= y
/ lbox
->GetLineHeight();
290 x
< renderer
->GetCheckBitmapSize().x
&&
292 (unsigned int)item
< lbox
->GetCount() )
294 lbox
->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE
, item
);
300 return wxStdInputHandler::HandleMouse(consumer
, event
);
303 #endif // wxUSE_CHECKLISTBOX