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