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 wxCheckListBox
46 // ============================================================================
48 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 void wxCheckListBox::Init()
58 wxCheckListBox::wxCheckListBox(wxWindow *parent,
62 const wxArrayString& choices,
64 const wxValidator& validator,
69 Create(parent, id, pos, size, choices, style, validator, name);
72 bool wxCheckListBox::Create(wxWindow *parent,
76 const wxArrayString& choices,
78 const wxValidator& validator,
81 wxCArrayString chs(choices);
83 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
84 style, validator, name);
87 bool wxCheckListBox::Create(wxWindow *parent,
92 const wxString choices[],
94 const wxValidator& validator,
97 if ( !wxListBox::Create(parent, id, pos, size,
98 n, choices, style, validator, name) )
101 CreateInputHandler(wxINP_HANDLER_CHECKLISTBOX);
106 // ----------------------------------------------------------------------------
107 // wxCheckListBox functions
108 // ----------------------------------------------------------------------------
110 bool wxCheckListBox::IsChecked(size_t item) const
112 wxCHECK_MSG( item < m_checks.GetCount(), false,
113 _T("invalid index in wxCheckListBox::IsChecked") );
115 return m_checks[item] != 0;
118 void wxCheckListBox::Check(size_t item, bool check)
120 wxCHECK_RET( item < m_checks.GetCount(),
121 _T("invalid index in wxCheckListBox::Check") );
123 // intermediate var is needed to avoid compiler warning with VC++
124 bool isChecked = m_checks[item] != 0;
125 if ( check != isChecked )
127 m_checks[item] = check;
133 // ----------------------------------------------------------------------------
134 // methods forwarded to wxListBox
135 // ----------------------------------------------------------------------------
137 void wxCheckListBox::Delete(int n)
139 wxCHECK_RET( n < GetCount(), _T("invalid index in wxListBox::Delete") );
141 wxListBox::Delete(n);
143 m_checks.RemoveAt(n);
146 int wxCheckListBox::DoAppend(const wxString& item)
148 int pos = wxListBox::DoAppend(item);
150 // the item is initially unchecked
151 m_checks.Insert(false, pos);
156 void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
158 wxListBox::DoInsertItems(items, pos);
160 size_t count = items.GetCount();
161 for ( size_t n = 0; n < count; n++ )
163 m_checks.Insert(false, pos + n);
167 void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
169 // call it first as it does DoClear()
170 wxListBox::DoSetItems(items, clientData);
172 size_t count = items.GetCount();
173 for ( size_t n = 0; n < count; n++ )
179 void wxCheckListBox::DoClear()
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 wxSize wxCheckListBox::DoGetBestClientSize() const
190 wxSize size = wxListBox::DoGetBestClientSize();
191 size.x += GetRenderer()->GetCheckBitmapSize().x;
196 void wxCheckListBox::DoDrawRange(wxControlRenderer *renderer,
197 int itemFirst, int itemLast)
199 renderer->DrawCheckItems(this, itemFirst, itemLast);
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
206 bool wxCheckListBox::PerformAction(const wxControlAction& action,
208 const wxString& strArg)
210 if ( action == wxACTION_CHECKLISTBOX_TOGGLE )
212 int sel = (int)numArg;
215 sel = GetSelection();
220 Check(sel, !IsChecked(sel));
222 SendEvent(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, sel);
227 return wxListBox::PerformAction(action, numArg, strArg);
233 // ----------------------------------------------------------------------------
234 // wxStdCheckListboxInputHandler
235 // ----------------------------------------------------------------------------
237 wxStdCheckListboxInputHandler::
238 wxStdCheckListboxInputHandler(wxInputHandler *inphand)
239 : wxStdListboxInputHandler(inphand)
243 bool wxStdCheckListboxInputHandler::HandleKey(wxInputConsumer *consumer,
244 const wxKeyEvent& event,
247 if ( pressed && (event.GetKeyCode() == WXK_SPACE) )
248 consumer->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE);
250 return wxStdListboxInputHandler::HandleKey(consumer, event, pressed);
253 bool wxStdCheckListboxInputHandler::HandleMouse(wxInputConsumer *consumer,
254 const wxMouseEvent& event)
256 if ( event.LeftDown() || event.LeftDClick() )
258 wxCheckListBox *lbox = wxStaticCast(consumer->GetInputWindow(), wxCheckListBox);
261 wxPoint pt = event.GetPosition();
262 pt -= consumer->GetInputWindow()->GetClientAreaOrigin();
263 lbox->CalcUnscrolledPosition(pt.x, pt.y, &x, &y);
265 wxRenderer *renderer = lbox->GetRenderer();
266 x -= renderer->GetCheckItemMargin();
268 int item = y / lbox->GetLineHeight();
270 x < renderer->GetCheckBitmapSize().x &&
272 item < lbox->GetCount() )
274 lbox->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE, item);
280 return wxStdListboxInputHandler::HandleMouse(consumer, event);
283 #endif // wxUSE_CHECKLISTBOX