]> git.saurik.com Git - wxWidgets.git/blame - src/univ/checklst.cpp
Fix indentation from tabs to spaces in a few lines I accidentally wrote before fixing...
[wxWidgets.git] / src / univ / checklst.cpp
CommitLineData
1e6feb95 1/////////////////////////////////////////////////////////////////////////////
e95c145c 2// Name: src/univ/checklst.cpp
1e6feb95
VZ
3// Purpose: wxCheckListBox implementation
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 12.09.00
7// RCS-ID: $Id$
442b35b5 8// Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
65571936 9// Licence: wxWindows licence
1e6feb95
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
1e6feb95
VZ
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#if wxUSE_CHECKLISTBOX
27
e6e83933
WS
28#include "wx/checklst.h"
29
1e6feb95
VZ
30#ifndef WX_PRECOMP
31 #include "wx/log.h"
1e6feb95 32 #include "wx/dcclient.h"
1e6feb95
VZ
33 #include "wx/validate.h"
34#endif
35
36#include "wx/univ/renderer.h"
37#include "wx/univ/inphand.h"
38#include "wx/univ/theme.h"
39
9467bdb7
VZ
40// ----------------------------------------------------------------------------
41// wxStdCheckListBoxInputHandler
42// ----------------------------------------------------------------------------
43
44class WXDLLEXPORT wxStdCheckListboxInputHandler : public wxStdInputHandler
45{
46public:
47 wxStdCheckListboxInputHandler(wxInputHandler *inphand);
48
49 virtual bool HandleKey(wxInputConsumer *consumer,
50 const wxKeyEvent& event,
51 bool pressed);
52 virtual bool HandleMouse(wxInputConsumer *consumer,
53 const wxMouseEvent& event);
54};
55
1e6feb95
VZ
56// ============================================================================
57// implementation of wxCheckListBox
58// ============================================================================
59
60IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
61
62// ----------------------------------------------------------------------------
63// creation
64// ----------------------------------------------------------------------------
65
66void wxCheckListBox::Init()
67{
68}
69
584ad2a3
MB
70wxCheckListBox::wxCheckListBox(wxWindow *parent,
71 wxWindowID id,
72 const wxPoint &pos,
73 const wxSize &size,
74 const wxArrayString& choices,
75 long style,
76 const wxValidator& validator,
77 const wxString &name)
78{
79 Init();
80
81 Create(parent, id, pos, size, choices, style, validator, name);
82}
83
84bool wxCheckListBox::Create(wxWindow *parent,
85 wxWindowID id,
86 const wxPoint &pos,
87 const wxSize &size,
88 const wxArrayString& choices,
89 long style,
90 const wxValidator& validator,
91 const wxString &name)
92{
93 wxCArrayString chs(choices);
94
95 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
96 style, validator, name);
97}
98
1e6feb95
VZ
99bool wxCheckListBox::Create(wxWindow *parent,
100 wxWindowID id,
101 const wxPoint &pos,
102 const wxSize &size,
103 int n,
104 const wxString choices[],
105 long style,
106 const wxValidator& validator,
107 const wxString &name)
108{
109 if ( !wxListBox::Create(parent, id, pos, size,
110 n, choices, style, validator, name) )
a290fa5a 111 return false;
1e6feb95
VZ
112
113 CreateInputHandler(wxINP_HANDLER_CHECKLISTBOX);
114
a290fa5a 115 return true;
1e6feb95
VZ
116}
117
118// ----------------------------------------------------------------------------
119// wxCheckListBox functions
120// ----------------------------------------------------------------------------
121
aa61d352 122bool wxCheckListBox::IsChecked(unsigned int item) const
1e6feb95 123{
aa61d352 124 wxCHECK_MSG( IsValid(item), false,
1e6feb95
VZ
125 _T("invalid index in wxCheckListBox::IsChecked") );
126
127 return m_checks[item] != 0;
128}
129
aa61d352 130void wxCheckListBox::Check(unsigned int item, bool check)
1e6feb95 131{
aa61d352 132 wxCHECK_RET( IsValid(item),
1e6feb95
VZ
133 _T("invalid index in wxCheckListBox::Check") );
134
135 // intermediate var is needed to avoid compiler warning with VC++
136 bool isChecked = m_checks[item] != 0;
137 if ( check != isChecked )
138 {
139 m_checks[item] = check;
140
141 RefreshItem(item);
142 }
143}
144
145// ----------------------------------------------------------------------------
146// methods forwarded to wxListBox
147// ----------------------------------------------------------------------------
148
aa61d352 149void wxCheckListBox::Delete(unsigned int n)
1e6feb95 150{
e95c145c 151 wxCHECK_RET( IsValid(n), _T("invalid index in wxListBox::Delete") );
1e6feb95
VZ
152
153 wxListBox::Delete(n);
154
155 m_checks.RemoveAt(n);
156}
157
158int wxCheckListBox::DoAppend(const wxString& item)
159{
160 int pos = wxListBox::DoAppend(item);
161
162 // the item is initially unchecked
a290fa5a 163 m_checks.Insert(false, pos);
1e6feb95
VZ
164
165 return pos;
166}
167
aa61d352 168void wxCheckListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
1e6feb95
VZ
169{
170 wxListBox::DoInsertItems(items, pos);
171
aa61d352
VZ
172 unsigned int count = items.GetCount();
173 for ( unsigned int n = 0; n < count; n++ )
1e6feb95 174 {
a290fa5a 175 m_checks.Insert(false, pos + n);
1e6feb95
VZ
176 }
177}
178
179void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
180{
181 // call it first as it does DoClear()
182 wxListBox::DoSetItems(items, clientData);
183
aa61d352
VZ
184 unsigned int count = items.GetCount();
185 for ( unsigned int n = 0; n < count; n++ )
1e6feb95 186 {
a290fa5a 187 m_checks.Add(false);
1e6feb95
VZ
188 }
189}
190
191void wxCheckListBox::DoClear()
192{
193 m_checks.Empty();
194}
195
196// ----------------------------------------------------------------------------
197// drawing
198// ----------------------------------------------------------------------------
199
200wxSize wxCheckListBox::DoGetBestClientSize() const
201{
202 wxSize size = wxListBox::DoGetBestClientSize();
203 size.x += GetRenderer()->GetCheckBitmapSize().x;
204
205 return size;
206}
207
208void wxCheckListBox::DoDrawRange(wxControlRenderer *renderer,
209 int itemFirst, int itemLast)
210{
211 renderer->DrawCheckItems(this, itemFirst, itemLast);
212}
213
214// ----------------------------------------------------------------------------
215// actions
216// ----------------------------------------------------------------------------
217
218bool wxCheckListBox::PerformAction(const wxControlAction& action,
219 long numArg,
220 const wxString& strArg)
221{
222 if ( action == wxACTION_CHECKLISTBOX_TOGGLE )
223 {
224 int sel = (int)numArg;
225 if ( sel == -1 )
226 {
227 sel = GetSelection();
228 }
229
230 if ( sel != -1 )
231 {
232 Check(sel, !IsChecked(sel));
233
234 SendEvent(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, sel);
235 }
236 }
237 else
238 {
239 return wxListBox::PerformAction(action, numArg, strArg);
240 }
241
a290fa5a 242 return true;
1e6feb95
VZ
243}
244
9467bdb7
VZ
245/* static */
246wxInputHandler *wxCheckListBox::GetStdInputHandler(wxInputHandler *handlerDef)
247{
248 static wxStdCheckListboxInputHandler s_handler(handlerDef);
249
250 return &s_handler;
251}
252
1e6feb95
VZ
253// ----------------------------------------------------------------------------
254// wxStdCheckListboxInputHandler
255// ----------------------------------------------------------------------------
256
257wxStdCheckListboxInputHandler::
258wxStdCheckListboxInputHandler(wxInputHandler *inphand)
9467bdb7 259 : wxStdInputHandler(wxListBox::GetStdInputHandler(inphand))
1e6feb95
VZ
260{
261}
262
23645bfa 263bool wxStdCheckListboxInputHandler::HandleKey(wxInputConsumer *consumer,
1e6feb95
VZ
264 const wxKeyEvent& event,
265 bool pressed)
266{
267 if ( pressed && (event.GetKeyCode() == WXK_SPACE) )
23645bfa 268 consumer->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE);
1e6feb95 269
9467bdb7 270 return wxStdInputHandler::HandleKey(consumer, event, pressed);
1e6feb95
VZ
271}
272
23645bfa 273bool wxStdCheckListboxInputHandler::HandleMouse(wxInputConsumer *consumer,
1e6feb95
VZ
274 const wxMouseEvent& event)
275{
276 if ( event.LeftDown() || event.LeftDClick() )
277 {
23645bfa 278 wxCheckListBox *lbox = wxStaticCast(consumer->GetInputWindow(), wxCheckListBox);
1e6feb95
VZ
279 int x, y;
280
281 wxPoint pt = event.GetPosition();
23645bfa 282 pt -= consumer->GetInputWindow()->GetClientAreaOrigin();
1e6feb95
VZ
283 lbox->CalcUnscrolledPosition(pt.x, pt.y, &x, &y);
284
285 wxRenderer *renderer = lbox->GetRenderer();
286 x -= renderer->GetCheckItemMargin();
287
288 int item = y / lbox->GetLineHeight();
289 if ( x >= 0 &&
290 x < renderer->GetCheckBitmapSize().x &&
291 item >= 0 &&
aa61d352 292 (unsigned int)item < lbox->GetCount() )
1e6feb95
VZ
293 {
294 lbox->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE, item);
295
a290fa5a 296 return true;
1e6feb95
VZ
297 }
298 }
299
9467bdb7 300 return wxStdInputHandler::HandleMouse(consumer, event);
1e6feb95
VZ
301}
302
303#endif // wxUSE_CHECKLISTBOX