]>
Commit | Line | Data |
---|---|---|
31618973 WS |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/msw/wince/checklst.cpp | |
3 | // Purpose: implementation of wxCheckListBox class | |
4 | // Author: Wlodzimierz ABX Skiba | |
5 | // Modified by: | |
6 | // Created: 30.10.2005 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Wlodzimierz Skiba | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
e6e83933 | 24 | #pragma hdrstop |
31618973 WS |
25 | #endif |
26 | ||
27 | #if wxUSE_CHECKLISTBOX | |
28 | ||
e6e83933 WS |
29 | #include "wx/checklst.h" |
30 | ||
31618973 | 31 | #ifndef WX_PRECOMP |
57bd4c60 | 32 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" |
31618973 WS |
33 | #endif |
34 | ||
31618973 WS |
35 | // ============================================================================ |
36 | // implementation | |
37 | // ============================================================================ | |
38 | ||
39 | IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxControl) | |
40 | ||
41 | // ---------------------------------------------------------------------------- | |
42 | // implementation of wxCheckListBox class | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | // define event table | |
46 | // ------------------ | |
47 | BEGIN_EVENT_TABLE(wxCheckListBox, wxControl) | |
48 | EVT_SIZE(wxCheckListBox::OnSize) | |
49 | END_EVENT_TABLE() | |
50 | ||
51 | // control creation | |
52 | // ---------------- | |
53 | ||
54 | // def ctor: use Create() to really create the control | |
55 | wxCheckListBox::wxCheckListBox() | |
56 | { | |
57 | } | |
58 | ||
59 | // ctor which creates the associated control | |
60 | wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id, | |
61 | const wxPoint& pos, const wxSize& size, | |
62 | int nStrings, const wxString choices[], | |
63 | long style, const wxValidator& val, | |
64 | const wxString& name) | |
65 | { | |
66 | Create(parent, id, pos, size, nStrings, choices, style, val, name); | |
67 | } | |
68 | ||
69 | wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id, | |
70 | const wxPoint& pos, const wxSize& size, | |
71 | const wxArrayString& choices, | |
72 | long style, const wxValidator& val, | |
73 | const wxString& name) | |
74 | { | |
75 | Create(parent, id, pos, size, choices, style, val, name); | |
76 | } | |
77 | ||
78 | wxCheckListBox::~wxCheckListBox() | |
79 | { | |
80 | m_itemsClientData.Clear(); | |
81 | } | |
82 | ||
83 | bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id, | |
84 | const wxPoint& pos, const wxSize& size, | |
85 | int n, const wxString choices[], | |
86 | long style, | |
87 | const wxValidator& validator, const wxString& name) | |
88 | { | |
89 | // initialize base class fields | |
90 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) | |
91 | return false; | |
92 | ||
93 | // create the native control | |
94 | if ( !MSWCreateControl(WC_LISTVIEW, wxEmptyString, pos, size) ) | |
95 | { | |
96 | // control creation failed | |
97 | return false; | |
98 | } | |
99 | ||
100 | ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE, 0, | |
101 | LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT ); | |
102 | ||
103 | // insert single column with checkboxes and labels | |
104 | LV_COLUMN col; | |
105 | wxZeroMemory(col); | |
106 | ListView_InsertColumn(GetHwnd(), 0, &col ); | |
107 | ||
792f83f0 WS |
108 | ListView_SetItemCount( GetHwnd(), n ); |
109 | ||
31618973 WS |
110 | // initialize the contents |
111 | for ( int i = 0; i < n; i++ ) | |
112 | { | |
113 | Append(choices[i]); | |
114 | } | |
115 | ||
116 | m_itemsClientData.SetCount(n); | |
117 | ||
118 | // now we can compute our best size correctly, so do it if necessary | |
119 | SetBestSize(size); | |
120 | ||
121 | return true; | |
122 | } | |
123 | ||
124 | bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id, | |
125 | const wxPoint& pos, const wxSize& size, | |
126 | const wxArrayString& choices, | |
127 | long style, | |
128 | const wxValidator& validator, const wxString& name) | |
129 | { | |
130 | wxCArrayString chs(choices); | |
131 | return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
132 | style, validator, name); | |
133 | } | |
134 | ||
135 | WXDWORD wxCheckListBox::MSWGetStyle(long style, WXDWORD *exstyle) const | |
136 | { | |
137 | WXDWORD wstyle = wxControl::MSWGetStyle(style, exstyle); | |
138 | ||
139 | wstyle |= LVS_REPORT | LVS_NOCOLUMNHEADER | LVS_NOSORTHEADER; | |
140 | ||
141 | return wstyle; | |
142 | } | |
143 | ||
144 | void wxCheckListBox::OnSize(wxSizeEvent& event) | |
145 | { | |
146 | // set width of the column we use to the width of list client area | |
147 | event.Skip(); | |
148 | int w = GetClientSize().x; | |
149 | ListView_SetColumnWidth( GetHwnd(), 0, w ); | |
150 | } | |
151 | ||
152 | // misc overloaded methods | |
153 | // ----------------------- | |
154 | ||
aa61d352 | 155 | void wxCheckListBox::Delete(unsigned int n) |
31618973 | 156 | { |
8228b893 | 157 | wxCHECK_RET( IsValid( n ), _T("invalid index in wxCheckListBox::Delete") ); |
31618973 WS |
158 | |
159 | if ( !ListView_DeleteItem(GetHwnd(), n) ) | |
160 | { | |
161 | wxLogLastError(_T("ListView_DeleteItem")); | |
162 | } | |
163 | m_itemsClientData.RemoveAt(n); | |
164 | } | |
165 | ||
166 | // check items | |
167 | // ----------- | |
168 | ||
aa61d352 | 169 | bool wxCheckListBox::IsChecked(unsigned int uiIndex) const |
31618973 | 170 | { |
8228b893 | 171 | wxCHECK_MSG( IsValid( uiIndex ), false, |
31618973 WS |
172 | _T("invalid index in wxCheckListBox::IsChecked") ); |
173 | ||
174 | return (ListView_GetCheckState(((HWND)GetHWND()), uiIndex) != 0); | |
175 | } | |
176 | ||
aa61d352 | 177 | void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck) |
31618973 | 178 | { |
8228b893 | 179 | wxCHECK_RET( IsValid( uiIndex ), |
31618973 WS |
180 | _T("invalid index in wxCheckListBox::Check") ); |
181 | ||
182 | ListView_SetCheckState(((HWND)GetHWND()), uiIndex, bCheck) | |
183 | } | |
184 | ||
185 | // interface derived from wxListBox and lower classes | |
186 | // -------------------------------------------------- | |
187 | ||
188 | void wxCheckListBox::Clear() | |
189 | { | |
aa61d352 | 190 | unsigned int n = GetCount(); |
31618973 WS |
191 | |
192 | while ( n > 0 ) | |
193 | { | |
194 | n--; | |
195 | Delete(n); | |
196 | } | |
197 | ||
198 | m_itemsClientData.Clear(); | |
199 | ||
200 | wxCHECK_RET( n == GetCount(), | |
201 | _T("broken wxCheckListBox::Clear()") ); | |
202 | } | |
203 | ||
aa61d352 | 204 | unsigned int wxCheckListBox::GetCount() const |
31618973 | 205 | { |
aa61d352 | 206 | return (unsigned int)ListView_GetItemCount( (HWND)GetHWND() ); |
31618973 WS |
207 | } |
208 | ||
209 | int wxCheckListBox::GetSelection() const | |
210 | { | |
f7273ce0 | 211 | int i; |
aa61d352 | 212 | for (i = 0; (unsigned int)i < GetCount(); i++) |
f7273ce0 JS |
213 | { |
214 | int selState = ListView_GetItemState(GetHwnd(), i, LVIS_SELECTED); | |
215 | if (selState == LVIS_SELECTED) | |
216 | return i; | |
217 | } | |
218 | ||
31618973 WS |
219 | return wxNOT_FOUND; |
220 | } | |
221 | ||
222 | int wxCheckListBox::GetSelections(wxArrayInt& aSelections) const | |
223 | { | |
f7273ce0 | 224 | int i; |
aa61d352 | 225 | for (i = 0; (unsigned int)i < GetCount(); i++) |
31618973 | 226 | { |
f7273ce0 JS |
227 | int selState = ListView_GetItemState(GetHwnd(), i, LVIS_SELECTED); |
228 | if (selState == LVIS_SELECTED) | |
229 | aSelections.Add(i); | |
31618973 WS |
230 | } |
231 | ||
232 | return aSelections.GetCount(); | |
233 | } | |
234 | ||
aa61d352 | 235 | wxString wxCheckListBox::GetString(unsigned int n) const |
31618973 WS |
236 | { |
237 | const int bufSize = 513; | |
238 | wxChar buf[bufSize]; | |
239 | ListView_GetItemText( (HWND)GetHWND(), n, 0, buf, bufSize - 1 ); | |
240 | buf[bufSize-1] = _T('\0'); | |
241 | wxString str(buf); | |
242 | return str; | |
243 | } | |
244 | ||
245 | bool wxCheckListBox::IsSelected(int n) const | |
246 | { | |
f7273ce0 JS |
247 | int selState = ListView_GetItemState(GetHwnd(), n, LVIS_SELECTED); |
248 | return (selState == LVIS_SELECTED); | |
31618973 WS |
249 | } |
250 | ||
aa61d352 | 251 | void wxCheckListBox::SetString(unsigned int n, const wxString& s) |
31618973 | 252 | { |
8228b893 | 253 | wxCHECK_RET( IsValid( n ), |
31618973 WS |
254 | _T("invalid index in wxCheckListBox::SetString") ); |
255 | wxChar *buf = new wxChar[s.length()+1]; | |
256 | wxStrcpy(buf, s.c_str()); | |
257 | ListView_SetItemText( (HWND)GetHWND(), n, 0, buf ); | |
258 | delete [] buf; | |
259 | } | |
260 | ||
261 | int wxCheckListBox::DoAppend(const wxString& item) | |
262 | { | |
8228b893 | 263 | int n = (int)GetCount(); |
31618973 WS |
264 | LVITEM newItem; |
265 | wxZeroMemory(newItem); | |
266 | newItem.iItem = n; | |
267 | int ret = ListView_InsertItem( (HWND)GetHWND(), & newItem ); | |
268 | wxCHECK_MSG( n == ret , -1, _T("Item not added") ); | |
269 | SetString( ret , item ); | |
270 | m_itemsClientData.Insert(NULL, ret); | |
271 | return ret; | |
272 | } | |
273 | ||
aa61d352 | 274 | void* wxCheckListBox::DoGetItemClientData(unsigned int n) const |
31618973 WS |
275 | { |
276 | return m_itemsClientData.Item(n); | |
277 | } | |
278 | ||
aa61d352 | 279 | wxClientData* wxCheckListBox::DoGetItemClientObject(unsigned int n) const |
31618973 WS |
280 | { |
281 | return (wxClientData *)DoGetItemClientData(n); | |
282 | } | |
283 | ||
aa61d352 | 284 | void wxCheckListBox::DoInsertItems(const wxArrayString& items, unsigned int pos) |
31618973 | 285 | { |
8228b893 | 286 | wxCHECK_RET( IsValidInsert( pos ), |
c5491b21 WS |
287 | wxT("invalid index in wxListBox::InsertItems") ); |
288 | ||
aa61d352 | 289 | for( unsigned int i = 0; i < items.GetCount(); i++ ) |
31618973 | 290 | { |
c5491b21 WS |
291 | LVITEM newItem; |
292 | wxZeroMemory(newItem); | |
293 | newItem.iItem = i+pos; | |
294 | int ret = ListView_InsertItem( (HWND)GetHWND(), & newItem ); | |
295 | wxASSERT_MSG( int(i+pos) == ret , _T("Item not added") ); | |
296 | SetString( ret , items[i] ); | |
297 | m_itemsClientData.Insert(NULL, ret); | |
31618973 WS |
298 | } |
299 | } | |
300 | ||
301 | void wxCheckListBox::DoSetFirstItem(int n) | |
302 | { | |
303 | int pos = ListView_GetTopIndex( (HWND)GetHWND() ); | |
304 | if(pos == n) return; | |
305 | POINT ppt; | |
306 | BOOL ret = ListView_GetItemPosition( (HWND)GetHWND(), n, &ppt ); | |
307 | wxCHECK_RET( ret == TRUE, _T("Broken DoSetFirstItem") ); | |
308 | ListView_Scroll( (HWND)GetHWND(), 0, 0 ); | |
309 | ListView_Scroll( (HWND)GetHWND(), 0, ppt.y ); | |
310 | } | |
311 | ||
aa61d352 | 312 | void wxCheckListBox::DoSetItemClientData(unsigned int n, void* clientData) |
31618973 WS |
313 | { |
314 | m_itemsClientData.Item(n) = clientData; | |
315 | } | |
316 | ||
aa61d352 | 317 | void wxCheckListBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData) |
31618973 WS |
318 | { |
319 | DoSetItemClientData(n, clientData); | |
320 | } | |
321 | ||
322 | void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData) | |
323 | { | |
792f83f0 WS |
324 | ListView_SetItemCount( GetHwnd(), GetCount() + items.GetCount() ); |
325 | ||
aa61d352 | 326 | for( unsigned int i = 0; i < items.GetCount(); i++ ) |
31618973 WS |
327 | { |
328 | int pos = Append(items[i]); | |
329 | if( pos >= 0 && clientData ) | |
330 | DoSetItemClientData(pos, clientData[i]); | |
331 | } | |
332 | } | |
333 | ||
334 | void wxCheckListBox::DoSetSelection(int n, bool select) | |
335 | { | |
f7273ce0 | 336 | ListView_SetItemState(GetHwnd(), n, select ? LVIS_SELECTED : 0, LVIS_SELECTED); |
31618973 WS |
337 | } |
338 | ||
f7273ce0 JS |
339 | bool wxCheckListBox::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) |
340 | { | |
341 | // prepare the event | |
342 | // ----------------- | |
343 | ||
344 | wxCommandEvent event(wxEVT_NULL, m_windowId); | |
345 | event.SetEventObject(this); | |
346 | ||
347 | wxEventType eventType = wxEVT_NULL; | |
348 | ||
349 | NMHDR *nmhdr = (NMHDR *)lParam; | |
350 | ||
351 | if ( nmhdr->hwndFrom == GetHwnd() ) | |
352 | { | |
353 | // almost all messages use NM_LISTVIEW | |
354 | NM_LISTVIEW *nmLV = (NM_LISTVIEW *)nmhdr; | |
355 | ||
356 | const int iItem = nmLV->iItem; | |
357 | ||
358 | bool processed = true; | |
359 | switch ( nmhdr->code ) | |
360 | { | |
361 | case LVN_ITEMCHANGED: | |
362 | // we translate this catch all message into more interesting | |
363 | // (and more easy to process) wxWidgets events | |
364 | ||
365 | // first of all, we deal with the state change events only and | |
366 | // only for valid items (item == -1 for the virtual list | |
367 | // control) | |
368 | if ( nmLV->uChanged & LVIF_STATE && iItem != -1 ) | |
369 | { | |
370 | // temp vars for readability | |
371 | const UINT stOld = nmLV->uOldState; | |
372 | const UINT stNew = nmLV->uNewState; | |
373 | ||
374 | // Check image changed | |
375 | if ((stOld & LVIS_STATEIMAGEMASK) != (stNew & LVIS_STATEIMAGEMASK)) | |
376 | { | |
377 | event.SetEventType(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED); | |
378 | event.SetInt(IsChecked(iItem)); | |
379 | (void) GetEventHandler()->ProcessEvent(event); | |
380 | } | |
381 | ||
382 | if ( (stNew & LVIS_SELECTED) != (stOld & LVIS_SELECTED) ) | |
383 | { | |
384 | eventType = wxEVT_COMMAND_LISTBOX_SELECTED; | |
385 | ||
386 | event.SetExtraLong( (stNew & LVIS_SELECTED) != 0 ); // is a selection | |
387 | event.SetInt(iItem); | |
388 | } | |
389 | } | |
390 | ||
391 | if ( eventType == wxEVT_NULL ) | |
392 | { | |
393 | // not an interesting event for us | |
394 | return false; | |
395 | } | |
396 | ||
397 | break; | |
398 | ||
399 | default: | |
400 | processed = false; | |
401 | } | |
402 | ||
403 | if ( !processed ) | |
404 | return wxControl::MSWOnNotify(idCtrl, lParam, result); | |
405 | } | |
406 | else | |
407 | { | |
408 | // where did this one come from? | |
409 | return false; | |
410 | } | |
411 | ||
412 | // process the event | |
413 | // ----------------- | |
414 | ||
415 | event.SetEventType(eventType); | |
416 | ||
417 | bool processed = GetEventHandler()->ProcessEvent(event); | |
418 | if ( processed ) | |
419 | *result = 0; | |
420 | ||
421 | return processed; | |
422 | } | |
423 | ||
424 | ||
31618973 | 425 | #endif |