Include wx/checklst.h according to precompiled headers of wx/wx.h (with other minor...
[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 #endif
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(unsigned int n)
158 {
159 wxCHECK_RET( IsValid( n ), _T("invalid index in wxCheckListBox::Delete") );
160
161 if ( !ListView_DeleteItem(GetHwnd(), n) )
162 {
163 wxLogLastError(_T("ListView_DeleteItem"));
164 }
165 m_itemsClientData.RemoveAt(n);
166 }
167
168 // check items
169 // -----------
170
171 bool wxCheckListBox::IsChecked(unsigned int uiIndex) const
172 {
173 wxCHECK_MSG( IsValid( uiIndex ), false,
174 _T("invalid index in wxCheckListBox::IsChecked") );
175
176 return (ListView_GetCheckState(((HWND)GetHWND()), uiIndex) != 0);
177 }
178
179 void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck)
180 {
181 wxCHECK_RET( IsValid( uiIndex ),
182 _T("invalid index in wxCheckListBox::Check") );
183
184 ListView_SetCheckState(((HWND)GetHWND()), uiIndex, bCheck)
185 }
186
187 // interface derived from wxListBox and lower classes
188 // --------------------------------------------------
189
190 void wxCheckListBox::Clear()
191 {
192 unsigned int n = GetCount();
193
194 while ( n > 0 )
195 {
196 n--;
197 Delete(n);
198 }
199
200 m_itemsClientData.Clear();
201
202 wxCHECK_RET( n == GetCount(),
203 _T("broken wxCheckListBox::Clear()") );
204 }
205
206 unsigned int wxCheckListBox::GetCount() const
207 {
208 return (unsigned int)ListView_GetItemCount( (HWND)GetHWND() );
209 }
210
211 int wxCheckListBox::GetSelection() const
212 {
213 int i;
214 for (i = 0; (unsigned int)i < GetCount(); i++)
215 {
216 int selState = ListView_GetItemState(GetHwnd(), i, LVIS_SELECTED);
217 if (selState == LVIS_SELECTED)
218 return i;
219 }
220
221 return wxNOT_FOUND;
222 }
223
224 int wxCheckListBox::GetSelections(wxArrayInt& aSelections) const
225 {
226 int i;
227 for (i = 0; (unsigned int)i < GetCount(); i++)
228 {
229 int selState = ListView_GetItemState(GetHwnd(), i, LVIS_SELECTED);
230 if (selState == LVIS_SELECTED)
231 aSelections.Add(i);
232 }
233
234 return aSelections.GetCount();
235 }
236
237 wxString wxCheckListBox::GetString(unsigned int n) const
238 {
239 const int bufSize = 513;
240 wxChar buf[bufSize];
241 ListView_GetItemText( (HWND)GetHWND(), n, 0, buf, bufSize - 1 );
242 buf[bufSize-1] = _T('\0');
243 wxString str(buf);
244 return str;
245 }
246
247 bool wxCheckListBox::IsSelected(int n) const
248 {
249 int selState = ListView_GetItemState(GetHwnd(), n, LVIS_SELECTED);
250 return (selState == LVIS_SELECTED);
251 }
252
253 void wxCheckListBox::SetString(unsigned int n, const wxString& s)
254 {
255 wxCHECK_RET( IsValid( n ),
256 _T("invalid index in wxCheckListBox::SetString") );
257 wxChar *buf = new wxChar[s.length()+1];
258 wxStrcpy(buf, s.c_str());
259 ListView_SetItemText( (HWND)GetHWND(), n, 0, buf );
260 delete [] buf;
261 }
262
263 int wxCheckListBox::DoAppend(const wxString& item)
264 {
265 int n = (int)GetCount();
266 LVITEM newItem;
267 wxZeroMemory(newItem);
268 newItem.iItem = n;
269 int ret = ListView_InsertItem( (HWND)GetHWND(), & newItem );
270 wxCHECK_MSG( n == ret , -1, _T("Item not added") );
271 SetString( ret , item );
272 m_itemsClientData.Insert(NULL, ret);
273 return ret;
274 }
275
276 void* wxCheckListBox::DoGetItemClientData(unsigned int n) const
277 {
278 return m_itemsClientData.Item(n);
279 }
280
281 wxClientData* wxCheckListBox::DoGetItemClientObject(unsigned int n) const
282 {
283 return (wxClientData *)DoGetItemClientData(n);
284 }
285
286 void wxCheckListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
287 {
288 wxCHECK_RET( IsValidInsert( pos ),
289 wxT("invalid index in wxListBox::InsertItems") );
290
291 for( unsigned int i = 0; i < items.GetCount(); i++ )
292 {
293 LVITEM newItem;
294 wxZeroMemory(newItem);
295 newItem.iItem = i+pos;
296 int ret = ListView_InsertItem( (HWND)GetHWND(), & newItem );
297 wxASSERT_MSG( int(i+pos) == ret , _T("Item not added") );
298 SetString( ret , items[i] );
299 m_itemsClientData.Insert(NULL, ret);
300 }
301 }
302
303 void wxCheckListBox::DoSetFirstItem(int n)
304 {
305 int pos = ListView_GetTopIndex( (HWND)GetHWND() );
306 if(pos == n) return;
307 POINT ppt;
308 BOOL ret = ListView_GetItemPosition( (HWND)GetHWND(), n, &ppt );
309 wxCHECK_RET( ret == TRUE, _T("Broken DoSetFirstItem") );
310 ListView_Scroll( (HWND)GetHWND(), 0, 0 );
311 ListView_Scroll( (HWND)GetHWND(), 0, ppt.y );
312 }
313
314 void wxCheckListBox::DoSetItemClientData(unsigned int n, void* clientData)
315 {
316 m_itemsClientData.Item(n) = clientData;
317 }
318
319 void wxCheckListBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
320 {
321 DoSetItemClientData(n, clientData);
322 }
323
324 void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
325 {
326 ListView_SetItemCount( GetHwnd(), GetCount() + items.GetCount() );
327
328 for( unsigned int i = 0; i < items.GetCount(); i++ )
329 {
330 int pos = Append(items[i]);
331 if( pos >= 0 && clientData )
332 DoSetItemClientData(pos, clientData[i]);
333 }
334 }
335
336 void wxCheckListBox::DoSetSelection(int n, bool select)
337 {
338 ListView_SetItemState(GetHwnd(), n, select ? LVIS_SELECTED : 0, LVIS_SELECTED);
339 }
340
341 bool wxCheckListBox::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
342 {
343 // prepare the event
344 // -----------------
345
346 wxCommandEvent event(wxEVT_NULL, m_windowId);
347 event.SetEventObject(this);
348
349 wxEventType eventType = wxEVT_NULL;
350
351 NMHDR *nmhdr = (NMHDR *)lParam;
352
353 if ( nmhdr->hwndFrom == GetHwnd() )
354 {
355 // almost all messages use NM_LISTVIEW
356 NM_LISTVIEW *nmLV = (NM_LISTVIEW *)nmhdr;
357
358 const int iItem = nmLV->iItem;
359
360 bool processed = true;
361 switch ( nmhdr->code )
362 {
363 case LVN_ITEMCHANGED:
364 // we translate this catch all message into more interesting
365 // (and more easy to process) wxWidgets events
366
367 // first of all, we deal with the state change events only and
368 // only for valid items (item == -1 for the virtual list
369 // control)
370 if ( nmLV->uChanged & LVIF_STATE && iItem != -1 )
371 {
372 // temp vars for readability
373 const UINT stOld = nmLV->uOldState;
374 const UINT stNew = nmLV->uNewState;
375
376 // Check image changed
377 if ((stOld & LVIS_STATEIMAGEMASK) != (stNew & LVIS_STATEIMAGEMASK))
378 {
379 event.SetEventType(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED);
380 event.SetInt(IsChecked(iItem));
381 (void) GetEventHandler()->ProcessEvent(event);
382 }
383
384 if ( (stNew & LVIS_SELECTED) != (stOld & LVIS_SELECTED) )
385 {
386 eventType = wxEVT_COMMAND_LISTBOX_SELECTED;
387
388 event.SetExtraLong( (stNew & LVIS_SELECTED) != 0 ); // is a selection
389 event.SetInt(iItem);
390 }
391 }
392
393 if ( eventType == wxEVT_NULL )
394 {
395 // not an interesting event for us
396 return false;
397 }
398
399 break;
400
401 default:
402 processed = false;
403 }
404
405 if ( !processed )
406 return wxControl::MSWOnNotify(idCtrl, lParam, result);
407 }
408 else
409 {
410 // where did this one come from?
411 return false;
412 }
413
414 // process the event
415 // -----------------
416
417 event.SetEventType(eventType);
418
419 bool processed = GetEventHandler()->ProcessEvent(event);
420 if ( processed )
421 *result = 0;
422
423 return processed;
424 }
425
426
427 #endif