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