]> git.saurik.com Git - wxWidgets.git/blob - src/univ/checklst.cpp
Use unsigned char for XBM bitmaps data.
[wxWidgets.git] / src / univ / checklst.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_CHECKLISTBOX
27
28 #include "wx/checklst.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/log.h"
32 #include "wx/dcclient.h"
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
40 // ----------------------------------------------------------------------------
41 // wxStdCheckListBoxInputHandler
42 // ----------------------------------------------------------------------------
43
44 class WXDLLEXPORT wxStdCheckListboxInputHandler : public wxStdInputHandler
45 {
46 public:
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
56 // ============================================================================
57 // implementation of wxCheckListBox
58 // ============================================================================
59
60 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
61
62 // ----------------------------------------------------------------------------
63 // creation
64 // ----------------------------------------------------------------------------
65
66 void wxCheckListBox::Init()
67 {
68 }
69
70 wxCheckListBox::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
84 bool 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
99 bool 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) )
111 return false;
112
113 CreateInputHandler(wxINP_HANDLER_CHECKLISTBOX);
114
115 return true;
116 }
117
118 // ----------------------------------------------------------------------------
119 // wxCheckListBox functions
120 // ----------------------------------------------------------------------------
121
122 bool wxCheckListBox::IsChecked(unsigned int item) const
123 {
124 wxCHECK_MSG( IsValid(item), false,
125 wxT("invalid index in wxCheckListBox::IsChecked") );
126
127 return m_checks[item] != 0;
128 }
129
130 void wxCheckListBox::Check(unsigned int item, bool check)
131 {
132 wxCHECK_RET( IsValid(item),
133 wxT("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
149 void wxCheckListBox::DoDeleteOneItem(unsigned int n)
150 {
151 wxListBox::DoDeleteOneItem(n);
152
153 m_checks.RemoveAt(n);
154 }
155
156 void wxCheckListBox::OnItemInserted(unsigned int pos)
157 {
158 m_checks.Insert(false, pos);
159 }
160
161 void wxCheckListBox::DoClear()
162 {
163 m_checks.Empty();
164 }
165
166 // ----------------------------------------------------------------------------
167 // drawing
168 // ----------------------------------------------------------------------------
169
170 wxSize wxCheckListBox::DoGetBestClientSize() const
171 {
172 wxSize size = wxListBox::DoGetBestClientSize();
173 size.x += GetRenderer()->GetCheckBitmapSize().x;
174
175 return size;
176 }
177
178 void wxCheckListBox::DoDrawRange(wxControlRenderer *renderer,
179 int itemFirst, int itemLast)
180 {
181 renderer->DrawCheckItems(this, itemFirst, itemLast);
182 }
183
184 // ----------------------------------------------------------------------------
185 // actions
186 // ----------------------------------------------------------------------------
187
188 bool 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
212 return true;
213 }
214
215 /* static */
216 wxInputHandler *wxCheckListBox::GetStdInputHandler(wxInputHandler *handlerDef)
217 {
218 static wxStdCheckListboxInputHandler s_handler(handlerDef);
219
220 return &s_handler;
221 }
222
223 // ----------------------------------------------------------------------------
224 // wxStdCheckListboxInputHandler
225 // ----------------------------------------------------------------------------
226
227 wxStdCheckListboxInputHandler::
228 wxStdCheckListboxInputHandler(wxInputHandler *inphand)
229 : wxStdInputHandler(wxListBox::GetStdInputHandler(inphand))
230 {
231 }
232
233 bool wxStdCheckListboxInputHandler::HandleKey(wxInputConsumer *consumer,
234 const wxKeyEvent& event,
235 bool pressed)
236 {
237 if ( pressed && (event.GetKeyCode() == WXK_SPACE) )
238 consumer->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE);
239
240 return wxStdInputHandler::HandleKey(consumer, event, pressed);
241 }
242
243 bool wxStdCheckListboxInputHandler::HandleMouse(wxInputConsumer *consumer,
244 const wxMouseEvent& event)
245 {
246 if ( event.LeftDown() || event.LeftDClick() )
247 {
248 wxCheckListBox *lbox = wxStaticCast(consumer->GetInputWindow(), wxCheckListBox);
249 int x, y;
250
251 wxPoint pt = event.GetPosition();
252 pt -= consumer->GetInputWindow()->GetClientAreaOrigin();
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 &&
262 (unsigned int)item < lbox->GetCount() )
263 {
264 lbox->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE, item);
265
266 return true;
267 }
268 }
269
270 return wxStdInputHandler::HandleMouse(consumer, event);
271 }
272
273 #endif // wxUSE_CHECKLISTBOX