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 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 void wxCheckListBox::Init()
68 wxCheckListBox::wxCheckListBox(wxWindow *parent,
72 const wxArrayString& choices,
74 const wxValidator& validator,
79 Create(parent, id, pos, size, choices, style, validator, name);
82 bool wxCheckListBox::Create(wxWindow *parent,
86 const wxArrayString& choices,
88 const wxValidator& validator,
91 wxCArrayString chs(choices);
93 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
94 style, validator, name);
97 bool wxCheckListBox::Create(wxWindow *parent,
102 const wxString choices[],
104 const wxValidator& validator,
105 const wxString &name)
107 if ( !wxListBox::Create(parent, id, pos, size,
108 n, choices, style, validator, name) )
111 CreateInputHandler(wxINP_HANDLER_CHECKLISTBOX);
116 // ----------------------------------------------------------------------------
117 // wxCheckListBox functions
118 // ----------------------------------------------------------------------------
120 bool wxCheckListBox::IsChecked(unsigned int item) const
122 wxCHECK_MSG( IsValid(item), false,
123 wxT("invalid index in wxCheckListBox::IsChecked") );
125 return m_checks[item] != 0;
128 void wxCheckListBox::Check(unsigned int item, bool check)
130 wxCHECK_RET( IsValid(item),
131 wxT("invalid index in wxCheckListBox::Check") );
133 // intermediate var is needed to avoid compiler warning with VC++
134 bool isChecked = m_checks[item] != 0;
135 if ( check != isChecked )
137 m_checks[item] = check;
143 // ----------------------------------------------------------------------------
144 // methods forwarded to wxListBox
145 // ----------------------------------------------------------------------------
147 void wxCheckListBox::DoDeleteOneItem(unsigned int n)
149 wxListBox::DoDeleteOneItem(n);
151 m_checks.RemoveAt(n);
154 void wxCheckListBox::OnItemInserted(unsigned int pos)
156 m_checks.Insert(false, pos);
159 void wxCheckListBox::DoClear()
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
168 wxSize wxCheckListBox::DoGetBestClientSize() const
170 wxSize size = wxListBox::DoGetBestClientSize();
171 size.x += GetRenderer()->GetCheckBitmapSize().x;
176 void wxCheckListBox::DoDrawRange(wxControlRenderer *renderer,
177 int itemFirst, int itemLast)
179 renderer->DrawCheckItems(this, itemFirst, itemLast);
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 bool wxCheckListBox::PerformAction(const wxControlAction& action,
188 const wxString& strArg)
190 if ( action == wxACTION_CHECKLISTBOX_TOGGLE )
192 int sel = (int)numArg;
195 sel = GetSelection();
200 Check(sel, !IsChecked(sel));
202 SendEvent(wxEVT_CHECKLISTBOX, sel);
207 return wxListBox::PerformAction(action, numArg, strArg);
214 wxInputHandler *wxCheckListBox::GetStdInputHandler(wxInputHandler *handlerDef)
216 static wxStdCheckListboxInputHandler s_handler(handlerDef);
221 // ----------------------------------------------------------------------------
222 // wxStdCheckListboxInputHandler
223 // ----------------------------------------------------------------------------
225 wxStdCheckListboxInputHandler::
226 wxStdCheckListboxInputHandler(wxInputHandler *inphand)
227 : wxStdInputHandler(wxListBox::GetStdInputHandler(inphand))
231 bool wxStdCheckListboxInputHandler::HandleKey(wxInputConsumer *consumer,
232 const wxKeyEvent& event,
235 if ( pressed && (event.GetKeyCode() == WXK_SPACE) )
236 consumer->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE);
238 return wxStdInputHandler::HandleKey(consumer, event, pressed);
241 bool wxStdCheckListboxInputHandler::HandleMouse(wxInputConsumer *consumer,
242 const wxMouseEvent& event)
244 if ( event.LeftDown() || event.LeftDClick() )
246 wxCheckListBox *lbox = wxStaticCast(consumer->GetInputWindow(), wxCheckListBox);
249 wxPoint pt = event.GetPosition();
250 pt -= consumer->GetInputWindow()->GetClientAreaOrigin();
251 lbox->CalcUnscrolledPosition(pt.x, pt.y, &x, &y);
253 wxRenderer *renderer = lbox->GetRenderer();
254 x -= renderer->GetCheckItemMargin();
256 int item = y / lbox->GetLineHeight();
258 x < renderer->GetCheckBitmapSize().x &&
260 (unsigned int)item < lbox->GetCount() )
262 lbox->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE, item);
268 return wxStdInputHandler::HandleMouse(consumer, event);
271 #endif // wxUSE_CHECKLISTBOX