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 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 wxCheckListBoxBase
46 // ============================================================================
48 wxCheckListBoxBase::wxCheckListBoxBase()
52 // ============================================================================
53 // implementation of wxCheckListBox
54 // ============================================================================
56 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 wxCheckListBox::wxCheckListBox()
67 wxCheckListBox::wxCheckListBox(wxWindow
*parent
,
72 const wxString
*choices
,
74 const wxValidator
& validator
,
79 Create(parent
, id
, pos
, size
, nStrings
, choices
, style
, validator
, name
);
82 void wxCheckListBox::Init()
86 bool wxCheckListBox::Create(wxWindow
*parent
,
91 const wxString choices
[],
93 const wxValidator
& validator
,
96 if ( !wxListBox::Create(parent
, id
, pos
, size
,
97 n
, choices
, style
, validator
, name
) )
100 CreateInputHandler(wxINP_HANDLER_CHECKLISTBOX
);
105 // ----------------------------------------------------------------------------
106 // wxCheckListBox functions
107 // ----------------------------------------------------------------------------
109 bool wxCheckListBox::IsChecked(size_t item
) const
111 wxCHECK_MSG( item
< m_checks
.GetCount(), FALSE
,
112 _T("invalid index in wxCheckListBox::IsChecked") );
114 return m_checks
[item
] != 0;
117 void wxCheckListBox::Check(size_t item
, bool check
)
119 wxCHECK_RET( item
< m_checks
.GetCount(),
120 _T("invalid index in wxCheckListBox::Check") );
122 // intermediate var is needed to avoid compiler warning with VC++
123 bool isChecked
= m_checks
[item
] != 0;
124 if ( check
!= isChecked
)
126 m_checks
[item
] = check
;
132 // ----------------------------------------------------------------------------
133 // methods forwarded to wxListBox
134 // ----------------------------------------------------------------------------
136 void wxCheckListBox::Delete(int n
)
138 wxCHECK_RET( n
< GetCount(), _T("invalid index in wxListBox::Delete") );
140 wxListBox::Delete(n
);
142 m_checks
.RemoveAt(n
);
145 int wxCheckListBox::DoAppend(const wxString
& item
)
147 int pos
= wxListBox::DoAppend(item
);
149 // the item is initially unchecked
150 m_checks
.Insert(FALSE
, pos
);
155 void wxCheckListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
157 wxListBox::DoInsertItems(items
, pos
);
159 size_t count
= items
.GetCount();
160 for ( size_t n
= 0; n
< count
; n
++ )
162 m_checks
.Insert(FALSE
, pos
+ n
);
166 void wxCheckListBox::DoSetItems(const wxArrayString
& items
, void **clientData
)
168 // call it first as it does DoClear()
169 wxListBox::DoSetItems(items
, clientData
);
171 size_t count
= items
.GetCount();
172 for ( size_t n
= 0; n
< count
; n
++ )
178 void wxCheckListBox::DoClear()
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 wxSize
wxCheckListBox::DoGetBestClientSize() const
189 wxSize size
= wxListBox::DoGetBestClientSize();
190 size
.x
+= GetRenderer()->GetCheckBitmapSize().x
;
195 void wxCheckListBox::DoDrawRange(wxControlRenderer
*renderer
,
196 int itemFirst
, int itemLast
)
198 renderer
->DrawCheckItems(this, itemFirst
, itemLast
);
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
205 bool wxCheckListBox::PerformAction(const wxControlAction
& action
,
207 const wxString
& strArg
)
209 if ( action
== wxACTION_CHECKLISTBOX_TOGGLE
)
211 int sel
= (int)numArg
;
214 sel
= GetSelection();
219 Check(sel
, !IsChecked(sel
));
221 SendEvent(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, sel
);
226 return wxListBox::PerformAction(action
, numArg
, strArg
);
232 // ----------------------------------------------------------------------------
233 // wxStdCheckListboxInputHandler
234 // ----------------------------------------------------------------------------
236 wxStdCheckListboxInputHandler::
237 wxStdCheckListboxInputHandler(wxInputHandler
*inphand
)
238 : wxStdListboxInputHandler(inphand
)
242 bool wxStdCheckListboxInputHandler::HandleKey(wxInputConsumer
*consumer
,
243 const wxKeyEvent
& event
,
246 if ( pressed
&& (event
.GetKeyCode() == WXK_SPACE
) )
247 consumer
->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE
);
249 return wxStdListboxInputHandler::HandleKey(consumer
, event
, pressed
);
252 bool wxStdCheckListboxInputHandler::HandleMouse(wxInputConsumer
*consumer
,
253 const wxMouseEvent
& event
)
255 if ( event
.LeftDown() || event
.LeftDClick() )
257 wxCheckListBox
*lbox
= wxStaticCast(consumer
->GetInputWindow(), wxCheckListBox
);
260 wxPoint pt
= event
.GetPosition();
261 pt
-= consumer
->GetInputWindow()->GetClientAreaOrigin();
262 lbox
->CalcUnscrolledPosition(pt
.x
, pt
.y
, &x
, &y
);
264 wxRenderer
*renderer
= lbox
->GetRenderer();
265 x
-= renderer
->GetCheckItemMargin();
267 int item
= y
/ lbox
->GetLineHeight();
269 x
< renderer
->GetCheckBitmapSize().x
&&
271 item
< lbox
->GetCount() )
273 lbox
->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE
, item
);
279 return wxStdListboxInputHandler::HandleMouse(consumer
, event
);
282 #endif // wxUSE_CHECKLISTBOX