]> git.saurik.com Git - wxWidgets.git/blame - src/univ/checklst.cpp
fixing osx_carbon
[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
1e6feb95
VZ
60// ----------------------------------------------------------------------------
61// creation
62// ----------------------------------------------------------------------------
63
64void wxCheckListBox::Init()
65{
66}
67
584ad2a3
MB
68wxCheckListBox::wxCheckListBox(wxWindow *parent,
69 wxWindowID id,
70 const wxPoint &pos,
71 const wxSize &size,
72 const wxArrayString& choices,
73 long style,
74 const wxValidator& validator,
75 const wxString &name)
76{
77 Init();
78
79 Create(parent, id, pos, size, choices, style, validator, name);
80}
81
82bool wxCheckListBox::Create(wxWindow *parent,
83 wxWindowID id,
84 const wxPoint &pos,
85 const wxSize &size,
86 const wxArrayString& choices,
87 long style,
88 const wxValidator& validator,
89 const wxString &name)
90{
91 wxCArrayString chs(choices);
92
93 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
94 style, validator, name);
95}
96
1e6feb95
VZ
97bool wxCheckListBox::Create(wxWindow *parent,
98 wxWindowID id,
99 const wxPoint &pos,
100 const wxSize &size,
101 int n,
102 const wxString choices[],
103 long style,
104 const wxValidator& validator,
105 const wxString &name)
106{
107 if ( !wxListBox::Create(parent, id, pos, size,
108 n, choices, style, validator, name) )
a290fa5a 109 return false;
1e6feb95
VZ
110
111 CreateInputHandler(wxINP_HANDLER_CHECKLISTBOX);
112
a290fa5a 113 return true;
1e6feb95
VZ
114}
115
116// ----------------------------------------------------------------------------
117// wxCheckListBox functions
118// ----------------------------------------------------------------------------
119
aa61d352 120bool wxCheckListBox::IsChecked(unsigned int item) const
1e6feb95 121{
aa61d352 122 wxCHECK_MSG( IsValid(item), false,
9a83f860 123 wxT("invalid index in wxCheckListBox::IsChecked") );
1e6feb95
VZ
124
125 return m_checks[item] != 0;
126}
127
aa61d352 128void wxCheckListBox::Check(unsigned int item, bool check)
1e6feb95 129{
aa61d352 130 wxCHECK_RET( IsValid(item),
9a83f860 131 wxT("invalid index in wxCheckListBox::Check") );
1e6feb95
VZ
132
133 // intermediate var is needed to avoid compiler warning with VC++
134 bool isChecked = m_checks[item] != 0;
135 if ( check != isChecked )
136 {
137 m_checks[item] = check;
138
139 RefreshItem(item);
140 }
141}
142
143// ----------------------------------------------------------------------------
144// methods forwarded to wxListBox
145// ----------------------------------------------------------------------------
146
a236aa20 147void wxCheckListBox::DoDeleteOneItem(unsigned int n)
1e6feb95 148{
a236aa20 149 wxListBox::DoDeleteOneItem(n);
1e6feb95
VZ
150
151 m_checks.RemoveAt(n);
152}
153
a236aa20 154void wxCheckListBox::OnItemInserted(unsigned int pos)
1e6feb95 155{
a290fa5a 156 m_checks.Insert(false, pos);
1e6feb95
VZ
157}
158
159void wxCheckListBox::DoClear()
160{
161 m_checks.Empty();
162}
163
164// ----------------------------------------------------------------------------
165// drawing
166// ----------------------------------------------------------------------------
167
168wxSize wxCheckListBox::DoGetBestClientSize() const
169{
170 wxSize size = wxListBox::DoGetBestClientSize();
171 size.x += GetRenderer()->GetCheckBitmapSize().x;
172
173 return size;
174}
175
176void wxCheckListBox::DoDrawRange(wxControlRenderer *renderer,
177 int itemFirst, int itemLast)
178{
179 renderer->DrawCheckItems(this, itemFirst, itemLast);
180}
181
182// ----------------------------------------------------------------------------
183// actions
184// ----------------------------------------------------------------------------
185
186bool wxCheckListBox::PerformAction(const wxControlAction& action,
187 long numArg,
188 const wxString& strArg)
189{
190 if ( action == wxACTION_CHECKLISTBOX_TOGGLE )
191 {
192 int sel = (int)numArg;
193 if ( sel == -1 )
194 {
195 sel = GetSelection();
196 }
197
198 if ( sel != -1 )
199 {
200 Check(sel, !IsChecked(sel));
201
202 SendEvent(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, sel);
203 }
204 }
205 else
206 {
207 return wxListBox::PerformAction(action, numArg, strArg);
208 }
209
a290fa5a 210 return true;
1e6feb95
VZ
211}
212
9467bdb7
VZ
213/* static */
214wxInputHandler *wxCheckListBox::GetStdInputHandler(wxInputHandler *handlerDef)
215{
216 static wxStdCheckListboxInputHandler s_handler(handlerDef);
217
218 return &s_handler;
219}
220
1e6feb95
VZ
221// ----------------------------------------------------------------------------
222// wxStdCheckListboxInputHandler
223// ----------------------------------------------------------------------------
224
225wxStdCheckListboxInputHandler::
226wxStdCheckListboxInputHandler(wxInputHandler *inphand)
9467bdb7 227 : wxStdInputHandler(wxListBox::GetStdInputHandler(inphand))
1e6feb95
VZ
228{
229}
230
23645bfa 231bool wxStdCheckListboxInputHandler::HandleKey(wxInputConsumer *consumer,
1e6feb95
VZ
232 const wxKeyEvent& event,
233 bool pressed)
234{
235 if ( pressed && (event.GetKeyCode() == WXK_SPACE) )
23645bfa 236 consumer->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE);
1e6feb95 237
9467bdb7 238 return wxStdInputHandler::HandleKey(consumer, event, pressed);
1e6feb95
VZ
239}
240
23645bfa 241bool wxStdCheckListboxInputHandler::HandleMouse(wxInputConsumer *consumer,
1e6feb95
VZ
242 const wxMouseEvent& event)
243{
244 if ( event.LeftDown() || event.LeftDClick() )
245 {
23645bfa 246 wxCheckListBox *lbox = wxStaticCast(consumer->GetInputWindow(), wxCheckListBox);
1e6feb95
VZ
247 int x, y;
248
249 wxPoint pt = event.GetPosition();
23645bfa 250 pt -= consumer->GetInputWindow()->GetClientAreaOrigin();
1e6feb95
VZ
251 lbox->CalcUnscrolledPosition(pt.x, pt.y, &x, &y);
252
253 wxRenderer *renderer = lbox->GetRenderer();
254 x -= renderer->GetCheckItemMargin();
255
256 int item = y / lbox->GetLineHeight();
257 if ( x >= 0 &&
258 x < renderer->GetCheckBitmapSize().x &&
259 item >= 0 &&
aa61d352 260 (unsigned int)item < lbox->GetCount() )
1e6feb95
VZ
261 {
262 lbox->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE, item);
263
a290fa5a 264 return true;
1e6feb95
VZ
265 }
266 }
267
9467bdb7 268 return wxStdInputHandler::HandleMouse(consumer, event);
1e6feb95
VZ
269}
270
271#endif // wxUSE_CHECKLISTBOX