]> git.saurik.com Git - wxWidgets.git/blob - src/msw/wince/checklst.cpp
no real changes, just minor refactoring and cleanup
[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 #include "wx/checklst.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #endif
34
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
108 ListView_SetItemCount( GetHwnd(), n );
109
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 SetInitialSize(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
155 void wxCheckListBox::DoDeleteOneItem(unsigned int n)
156 {
157 wxCHECK_RET( IsValid( n ), _T("invalid index in wxCheckListBox::Delete") );
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
169 bool wxCheckListBox::IsChecked(unsigned int uiIndex) const
170 {
171 wxCHECK_MSG( IsValid( uiIndex ), false,
172 _T("invalid index in wxCheckListBox::IsChecked") );
173
174 return (ListView_GetCheckState(((HWND)GetHWND()), uiIndex) != 0);
175 }
176
177 void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck)
178 {
179 wxCHECK_RET( IsValid( uiIndex ),
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::DoClear()
189 {
190 unsigned int n = GetCount();
191
192 while ( n > 0 )
193 {
194 n--;
195 DoDeleteOneItem(n);
196 }
197
198 wxASSERT_MSG( IsEmpty(), _T("logic error in DoClear()") );
199 }
200
201 unsigned int wxCheckListBox::GetCount() const
202 {
203 return (unsigned int)ListView_GetItemCount( (HWND)GetHWND() );
204 }
205
206 int wxCheckListBox::GetSelection() const
207 {
208 int i;
209 for (i = 0; (unsigned int)i < GetCount(); i++)
210 {
211 int selState = ListView_GetItemState(GetHwnd(), i, LVIS_SELECTED);
212 if (selState == LVIS_SELECTED)
213 return i;
214 }
215
216 return wxNOT_FOUND;
217 }
218
219 int wxCheckListBox::GetSelections(wxArrayInt& aSelections) const
220 {
221 int i;
222 for (i = 0; (unsigned int)i < GetCount(); i++)
223 {
224 int selState = ListView_GetItemState(GetHwnd(), i, LVIS_SELECTED);
225 if (selState == LVIS_SELECTED)
226 aSelections.Add(i);
227 }
228
229 return aSelections.GetCount();
230 }
231
232 wxString wxCheckListBox::GetString(unsigned int n) const
233 {
234 const int bufSize = 513;
235 wxChar buf[bufSize];
236 ListView_GetItemText( (HWND)GetHWND(), n, 0, buf, bufSize - 1 );
237 buf[bufSize-1] = _T('\0');
238 wxString str(buf);
239 return str;
240 }
241
242 bool wxCheckListBox::IsSelected(int n) const
243 {
244 int selState = ListView_GetItemState(GetHwnd(), n, LVIS_SELECTED);
245 return (selState == LVIS_SELECTED);
246 }
247
248 void wxCheckListBox::SetString(unsigned int n, const wxString& s)
249 {
250 wxCHECK_RET( IsValid( n ),
251 _T("invalid index in wxCheckListBox::SetString") );
252 wxChar *buf = new wxChar[s.length()+1];
253 wxStrcpy(buf, s.c_str());
254 ListView_SetItemText( (HWND)GetHWND(), n, 0, buf );
255 delete [] buf;
256 }
257
258 void* wxCheckListBox::DoGetItemClientData(unsigned int n) const
259 {
260 return m_itemsClientData.Item(n);
261 }
262
263 int wxCheckListBox::DoInsertItems(const wxArrayStringsAdapter & items,
264 unsigned int pos,
265 void **clientData, wxClientDataType type)
266 {
267 const unsigned int count = items.GetCount();
268
269 ListView_SetItemCount( GetHwnd(), GetCount() + count );
270
271 int n = wxNOT_FOUND;
272
273 for( unsigned int i = 0; i < count; i++ )
274 {
275 LVITEM newItem;
276 wxZeroMemory(newItem);
277 newItem.iItem = pos + i;
278 n = ListView_InsertItem( (HWND)GetHWND(), & newItem );
279 wxCHECK_MSG( n != -1, -1, _T("Item not added") );
280 SetString( n, items[i] );
281 m_itemsClientData.Insert(NULL, n);
282
283 AssignNewItemClientData(n, clientData, i, type);
284 }
285
286 return n;
287 }
288
289 void wxCheckListBox::DoSetFirstItem(int n)
290 {
291 int pos = ListView_GetTopIndex( (HWND)GetHWND() );
292 if(pos == n) return;
293 POINT ppt;
294 BOOL ret = ListView_GetItemPosition( (HWND)GetHWND(), n, &ppt );
295 wxCHECK_RET( ret == TRUE, _T("Broken DoSetFirstItem") );
296 ListView_Scroll( (HWND)GetHWND(), 0, 0 );
297 ListView_Scroll( (HWND)GetHWND(), 0, ppt.y );
298 }
299
300 void wxCheckListBox::DoSetItemClientData(unsigned int n, void* clientData)
301 {
302 m_itemsClientData.Item(n) = clientData;
303 }
304
305 void wxCheckListBox::DoSetSelection(int n, bool select)
306 {
307 ListView_SetItemState(GetHwnd(), n, select ? LVIS_SELECTED : 0, LVIS_SELECTED);
308 }
309
310 bool wxCheckListBox::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
311 {
312 // prepare the event
313 // -----------------
314
315 wxCommandEvent event(wxEVT_NULL, m_windowId);
316 event.SetEventObject(this);
317
318 wxEventType eventType = wxEVT_NULL;
319
320 NMHDR *nmhdr = (NMHDR *)lParam;
321
322 if ( nmhdr->hwndFrom == GetHwnd() )
323 {
324 // almost all messages use NM_LISTVIEW
325 NM_LISTVIEW *nmLV = (NM_LISTVIEW *)nmhdr;
326
327 const int iItem = nmLV->iItem;
328
329 bool processed = true;
330 switch ( nmhdr->code )
331 {
332 case LVN_ITEMCHANGED:
333 // we translate this catch all message into more interesting
334 // (and more easy to process) wxWidgets events
335
336 // first of all, we deal with the state change events only and
337 // only for valid items (item == -1 for the virtual list
338 // control)
339 if ( nmLV->uChanged & LVIF_STATE && iItem != -1 )
340 {
341 // temp vars for readability
342 const UINT stOld = nmLV->uOldState;
343 const UINT stNew = nmLV->uNewState;
344
345 // Check image changed
346 if ((stOld & LVIS_STATEIMAGEMASK) != (stNew & LVIS_STATEIMAGEMASK))
347 {
348 event.SetEventType(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED);
349 event.SetInt(IsChecked(iItem));
350 (void) GetEventHandler()->ProcessEvent(event);
351 }
352
353 if ( (stNew & LVIS_SELECTED) != (stOld & LVIS_SELECTED) )
354 {
355 eventType = wxEVT_COMMAND_LISTBOX_SELECTED;
356
357 event.SetExtraLong( (stNew & LVIS_SELECTED) != 0 ); // is a selection
358 event.SetInt(iItem);
359 }
360 }
361
362 if ( eventType == wxEVT_NULL )
363 {
364 // not an interesting event for us
365 return false;
366 }
367
368 break;
369
370 default:
371 processed = false;
372 }
373
374 if ( !processed )
375 return wxControl::MSWOnNotify(idCtrl, lParam, result);
376 }
377 else
378 {
379 // where did this one come from?
380 return false;
381 }
382
383 // process the event
384 // -----------------
385
386 event.SetEventType(eventType);
387
388 bool processed = GetEventHandler()->ProcessEvent(event);
389 if ( processed )
390 *result = 0;
391
392 return processed;
393 }
394
395
396 #endif