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