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