]> git.saurik.com Git - wxWidgets.git/blame - src/univ/checklst.cpp
Improve validation of wxCheckBox styles.
[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,
9a83f860 125 wxT("invalid index in wxCheckListBox::IsChecked") );
1e6feb95
VZ
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),
9a83f860 133 wxT("invalid index in wxCheckListBox::Check") );
1e6feb95
VZ
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
a236aa20 149void wxCheckListBox::DoDeleteOneItem(unsigned int n)
1e6feb95 150{
a236aa20 151 wxListBox::DoDeleteOneItem(n);
1e6feb95
VZ
152
153 m_checks.RemoveAt(n);
154}
155
a236aa20 156void wxCheckListBox::OnItemInserted(unsigned int pos)
1e6feb95 157{
a290fa5a 158 m_checks.Insert(false, pos);
1e6feb95
VZ
159}
160
161void wxCheckListBox::DoClear()
162{
163 m_checks.Empty();
164}
165
166// ----------------------------------------------------------------------------
167// drawing
168// ----------------------------------------------------------------------------
169
170wxSize wxCheckListBox::DoGetBestClientSize() const
171{
172 wxSize size = wxListBox::DoGetBestClientSize();
173 size.x += GetRenderer()->GetCheckBitmapSize().x;
174
175 return size;
176}
177
178void wxCheckListBox::DoDrawRange(wxControlRenderer *renderer,
179 int itemFirst, int itemLast)
180{
181 renderer->DrawCheckItems(this, itemFirst, itemLast);
182}
183
184// ----------------------------------------------------------------------------
185// actions
186// ----------------------------------------------------------------------------
187
188bool wxCheckListBox::PerformAction(const wxControlAction& action,
189 long numArg,
190 const wxString& strArg)
191{
192 if ( action == wxACTION_CHECKLISTBOX_TOGGLE )
193 {
194 int sel = (int)numArg;
195 if ( sel == -1 )
196 {
197 sel = GetSelection();
198 }
199
200 if ( sel != -1 )
201 {
202 Check(sel, !IsChecked(sel));
203
204 SendEvent(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, sel);
205 }
206 }
207 else
208 {
209 return wxListBox::PerformAction(action, numArg, strArg);
210 }
211
a290fa5a 212 return true;
1e6feb95
VZ
213}
214
9467bdb7
VZ
215/* static */
216wxInputHandler *wxCheckListBox::GetStdInputHandler(wxInputHandler *handlerDef)
217{
218 static wxStdCheckListboxInputHandler s_handler(handlerDef);
219
220 return &s_handler;
221}
222
1e6feb95
VZ
223// ----------------------------------------------------------------------------
224// wxStdCheckListboxInputHandler
225// ----------------------------------------------------------------------------
226
227wxStdCheckListboxInputHandler::
228wxStdCheckListboxInputHandler(wxInputHandler *inphand)
9467bdb7 229 : wxStdInputHandler(wxListBox::GetStdInputHandler(inphand))
1e6feb95
VZ
230{
231}
232
23645bfa 233bool wxStdCheckListboxInputHandler::HandleKey(wxInputConsumer *consumer,
1e6feb95
VZ
234 const wxKeyEvent& event,
235 bool pressed)
236{
237 if ( pressed && (event.GetKeyCode() == WXK_SPACE) )
23645bfa 238 consumer->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE);
1e6feb95 239
9467bdb7 240 return wxStdInputHandler::HandleKey(consumer, event, pressed);
1e6feb95
VZ
241}
242
23645bfa 243bool wxStdCheckListboxInputHandler::HandleMouse(wxInputConsumer *consumer,
1e6feb95
VZ
244 const wxMouseEvent& event)
245{
246 if ( event.LeftDown() || event.LeftDClick() )
247 {
23645bfa 248 wxCheckListBox *lbox = wxStaticCast(consumer->GetInputWindow(), wxCheckListBox);
1e6feb95
VZ
249 int x, y;
250
251 wxPoint pt = event.GetPosition();
23645bfa 252 pt -= consumer->GetInputWindow()->GetClientAreaOrigin();
1e6feb95
VZ
253 lbox->CalcUnscrolledPosition(pt.x, pt.y, &x, &y);
254
255 wxRenderer *renderer = lbox->GetRenderer();
256 x -= renderer->GetCheckItemMargin();
257
258 int item = y / lbox->GetLineHeight();
259 if ( x >= 0 &&
260 x < renderer->GetCheckBitmapSize().x &&
261 item >= 0 &&
aa61d352 262 (unsigned int)item < lbox->GetCount() )
1e6feb95
VZ
263 {
264 lbox->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE, item);
265
a290fa5a 266 return true;
1e6feb95
VZ
267 }
268 }
269
9467bdb7 270 return wxStdInputHandler::HandleMouse(consumer, event);
1e6feb95
VZ
271}
272
273#endif // wxUSE_CHECKLISTBOX