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