1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 // ----------------------------------------------------------------------------
21 #pragma implementation "univchecklst.h"
24 #include "wx/wxprec.h"
30 #if wxUSE_CHECKLISTBOX
35 #include "wx/dcclient.h"
36 #include "wx/checklst.h"
37 #include "wx/validate.h"
40 #include "wx/univ/renderer.h"
41 #include "wx/univ/inphand.h"
42 #include "wx/univ/theme.h"
44 // ============================================================================
45 // implementation of wxCheckListBox
46 // ============================================================================
48 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 void wxCheckListBox::Init()
58 bool wxCheckListBox::Create(wxWindow
*parent
,
63 const wxString choices
[],
65 const wxValidator
& validator
,
68 if ( !wxListBox::Create(parent
, id
, pos
, size
,
69 n
, choices
, style
, validator
, name
) )
72 CreateInputHandler(wxINP_HANDLER_CHECKLISTBOX
);
77 // ----------------------------------------------------------------------------
78 // wxCheckListBox functions
79 // ----------------------------------------------------------------------------
81 bool wxCheckListBox::IsChecked(size_t item
) const
83 wxCHECK_MSG( item
< m_checks
.GetCount(), FALSE
,
84 _T("invalid index in wxCheckListBox::IsChecked") );
86 return m_checks
[item
] != 0;
89 void wxCheckListBox::Check(size_t item
, bool check
)
91 wxCHECK_RET( item
< m_checks
.GetCount(),
92 _T("invalid index in wxCheckListBox::Check") );
94 // intermediate var is needed to avoid compiler warning with VC++
95 bool isChecked
= m_checks
[item
] != 0;
96 if ( check
!= isChecked
)
98 m_checks
[item
] = check
;
104 // ----------------------------------------------------------------------------
105 // methods forwarded to wxListBox
106 // ----------------------------------------------------------------------------
108 void wxCheckListBox::Delete(int n
)
110 wxCHECK_RET( n
< GetCount(), _T("invalid index in wxListBox::Delete") );
112 wxListBox::Delete(n
);
114 m_checks
.RemoveAt(n
);
117 int wxCheckListBox::DoAppend(const wxString
& item
)
119 int pos
= wxListBox::DoAppend(item
);
121 // the item is initially unchecked
122 m_checks
.Insert(FALSE
, pos
);
127 void wxCheckListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
129 wxListBox::DoInsertItems(items
, pos
);
131 size_t count
= items
.GetCount();
132 for ( size_t n
= 0; n
< count
; n
++ )
134 m_checks
.Insert(FALSE
, pos
+ n
);
138 void wxCheckListBox::DoSetItems(const wxArrayString
& items
, void **clientData
)
140 // call it first as it does DoClear()
141 wxListBox::DoSetItems(items
, clientData
);
143 size_t count
= items
.GetCount();
144 for ( size_t n
= 0; n
< count
; n
++ )
150 void wxCheckListBox::DoClear()
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 wxSize
wxCheckListBox::DoGetBestClientSize() const
161 wxSize size
= wxListBox::DoGetBestClientSize();
162 size
.x
+= GetRenderer()->GetCheckBitmapSize().x
;
167 void wxCheckListBox::DoDrawRange(wxControlRenderer
*renderer
,
168 int itemFirst
, int itemLast
)
170 renderer
->DrawCheckItems(this, itemFirst
, itemLast
);
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 bool wxCheckListBox::PerformAction(const wxControlAction
& action
,
179 const wxString
& strArg
)
181 if ( action
== wxACTION_CHECKLISTBOX_TOGGLE
)
183 int sel
= (int)numArg
;
186 sel
= GetSelection();
191 Check(sel
, !IsChecked(sel
));
193 SendEvent(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, sel
);
198 return wxListBox::PerformAction(action
, numArg
, strArg
);
204 // ----------------------------------------------------------------------------
205 // wxStdCheckListboxInputHandler
206 // ----------------------------------------------------------------------------
208 wxStdCheckListboxInputHandler::
209 wxStdCheckListboxInputHandler(wxInputHandler
*inphand
)
210 : wxStdListboxInputHandler(inphand
)
214 bool wxStdCheckListboxInputHandler::HandleKey(wxControl
*control
,
215 const wxKeyEvent
& event
,
218 if ( pressed
&& (event
.GetKeyCode() == WXK_SPACE
) )
219 control
->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE
);
221 return wxStdListboxInputHandler::HandleKey(control
, event
, pressed
);
224 bool wxStdCheckListboxInputHandler::HandleMouse(wxControl
*control
,
225 const wxMouseEvent
& event
)
227 if ( event
.LeftDown() || event
.LeftDClick() )
229 wxCheckListBox
*lbox
= wxStaticCast(control
, wxCheckListBox
);
232 wxPoint pt
= event
.GetPosition();
233 pt
-= control
->GetClientAreaOrigin();
234 lbox
->CalcUnscrolledPosition(pt
.x
, pt
.y
, &x
, &y
);
236 wxRenderer
*renderer
= lbox
->GetRenderer();
237 x
-= renderer
->GetCheckItemMargin();
239 int item
= y
/ lbox
->GetLineHeight();
241 x
< renderer
->GetCheckBitmapSize().x
&&
243 item
< lbox
->GetCount() )
245 lbox
->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE
, item
);
251 return wxStdListboxInputHandler::HandleMouse(control
, event
);
254 #endif // wxUSE_CHECKLISTBOX