]>
Commit | Line | Data |
---|---|---|
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 | // 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__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #if wxUSE_CHECKLISTBOX | |
27 | ||
28 | #include "wx/checklst.h" | |
29 | ||
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" | |
32 | #endif | |
33 | ||
34 | // ============================================================================ | |
35 | // implementation | |
36 | // ============================================================================ | |
37 | ||
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 | ||
105 | ListView_SetItemCount( GetHwnd(), n ); | |
106 | ||
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 | |
116 | SetInitialSize(size); | |
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 | ||
152 | void wxCheckListBox::DoDeleteOneItem(unsigned int n) | |
153 | { | |
154 | wxCHECK_RET( IsValid( n ), wxT("invalid index in wxCheckListBox::Delete") ); | |
155 | ||
156 | if ( !ListView_DeleteItem(GetHwnd(), n) ) | |
157 | { | |
158 | wxLogLastError(wxT("ListView_DeleteItem")); | |
159 | } | |
160 | m_itemsClientData.RemoveAt(n); | |
161 | } | |
162 | ||
163 | // check items | |
164 | // ----------- | |
165 | ||
166 | bool wxCheckListBox::IsChecked(unsigned int uiIndex) const | |
167 | { | |
168 | wxCHECK_MSG( IsValid( uiIndex ), false, | |
169 | wxT("invalid index in wxCheckListBox::IsChecked") ); | |
170 | ||
171 | return (ListView_GetCheckState(((HWND)GetHWND()), uiIndex) != 0); | |
172 | } | |
173 | ||
174 | void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck) | |
175 | { | |
176 | wxCHECK_RET( IsValid( uiIndex ), | |
177 | wxT("invalid index in wxCheckListBox::Check") ); | |
178 | ||
179 | ListView_SetCheckState(((HWND)GetHWND()), uiIndex, bCheck) | |
180 | } | |
181 | ||
182 | // interface derived from wxListBox and lower classes | |
183 | // -------------------------------------------------- | |
184 | ||
185 | void wxCheckListBox::DoClear() | |
186 | { | |
187 | unsigned int n = GetCount(); | |
188 | ||
189 | while ( n > 0 ) | |
190 | { | |
191 | n--; | |
192 | DoDeleteOneItem(n); | |
193 | } | |
194 | ||
195 | wxASSERT_MSG( IsEmpty(), wxT("logic error in DoClear()") ); | |
196 | } | |
197 | ||
198 | unsigned int wxCheckListBox::GetCount() const | |
199 | { | |
200 | return (unsigned int)ListView_GetItemCount( (HWND)GetHWND() ); | |
201 | } | |
202 | ||
203 | int wxCheckListBox::GetSelection() const | |
204 | { | |
205 | int i; | |
206 | for (i = 0; (unsigned int)i < GetCount(); i++) | |
207 | { | |
208 | int selState = ListView_GetItemState(GetHwnd(), i, LVIS_SELECTED); | |
209 | if (selState == LVIS_SELECTED) | |
210 | return i; | |
211 | } | |
212 | ||
213 | return wxNOT_FOUND; | |
214 | } | |
215 | ||
216 | int wxCheckListBox::GetSelections(wxArrayInt& aSelections) const | |
217 | { | |
218 | int i; | |
219 | for (i = 0; (unsigned int)i < GetCount(); i++) | |
220 | { | |
221 | int selState = ListView_GetItemState(GetHwnd(), i, LVIS_SELECTED); | |
222 | if (selState == LVIS_SELECTED) | |
223 | aSelections.Add(i); | |
224 | } | |
225 | ||
226 | return aSelections.GetCount(); | |
227 | } | |
228 | ||
229 | wxString wxCheckListBox::GetString(unsigned int n) const | |
230 | { | |
231 | const int bufSize = 513; | |
232 | wxChar buf[bufSize]; | |
233 | ListView_GetItemText( (HWND)GetHWND(), n, 0, buf, bufSize - 1 ); | |
234 | buf[bufSize-1] = wxT('\0'); | |
235 | wxString str(buf); | |
236 | return str; | |
237 | } | |
238 | ||
239 | bool wxCheckListBox::IsSelected(int n) const | |
240 | { | |
241 | int selState = ListView_GetItemState(GetHwnd(), n, LVIS_SELECTED); | |
242 | return (selState == LVIS_SELECTED); | |
243 | } | |
244 | ||
245 | void wxCheckListBox::SetString(unsigned int n, const wxString& s) | |
246 | { | |
247 | wxCHECK_RET( IsValid( n ), | |
248 | wxT("invalid index in wxCheckListBox::SetString") ); | |
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 | ||
255 | void* wxCheckListBox::DoGetItemClientData(unsigned int n) const | |
256 | { | |
257 | return m_itemsClientData.Item(n); | |
258 | } | |
259 | ||
260 | int wxCheckListBox::DoInsertItems(const wxArrayStringsAdapter & items, | |
261 | unsigned int pos, | |
262 | void **clientData, wxClientDataType type) | |
263 | { | |
264 | const unsigned int count = items.GetCount(); | |
265 | ||
266 | ListView_SetItemCount( GetHwnd(), GetCount() + count ); | |
267 | ||
268 | int n = wxNOT_FOUND; | |
269 | ||
270 | for( unsigned int i = 0; i < count; i++ ) | |
271 | { | |
272 | LVITEM newItem; | |
273 | wxZeroMemory(newItem); | |
274 | newItem.iItem = pos + i; | |
275 | n = ListView_InsertItem( (HWND)GetHWND(), & newItem ); | |
276 | wxCHECK_MSG( n != -1, -1, wxT("Item not added") ); | |
277 | SetString( n, items[i] ); | |
278 | m_itemsClientData.Insert(NULL, n); | |
279 | ||
280 | AssignNewItemClientData(n, clientData, i, type); | |
281 | } | |
282 | ||
283 | return n; | |
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 ); | |
292 | wxCHECK_RET( ret == TRUE, wxT("Broken DoSetFirstItem") ); | |
293 | ListView_Scroll( (HWND)GetHWND(), 0, 0 ); | |
294 | ListView_Scroll( (HWND)GetHWND(), 0, ppt.y ); | |
295 | } | |
296 | ||
297 | void wxCheckListBox::DoSetItemClientData(unsigned int n, void* clientData) | |
298 | { | |
299 | m_itemsClientData.Item(n) = clientData; | |
300 | } | |
301 | ||
302 | void wxCheckListBox::DoSetSelection(int n, bool select) | |
303 | { | |
304 | ListView_SetItemState(GetHwnd(), n, select ? LVIS_SELECTED : 0, LVIS_SELECTED); | |
305 | } | |
306 | ||
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 | { | |
345 | event.SetEventType(wxEVT_CHECKLISTBOX); | |
346 | event.SetInt(IsChecked(iItem)); | |
347 | (void) GetEventHandler()->ProcessEvent(event); | |
348 | } | |
349 | ||
350 | if ( (stNew & LVIS_SELECTED) != (stOld & LVIS_SELECTED) ) | |
351 | { | |
352 | eventType = wxEVT_LISTBOX; | |
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 | ||
393 | #endif |