Native wxCheckListBox implementation for wxWinCE.
[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 // 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 SetBestSize(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::Delete(int n)
156 {
157 wxCHECK_RET( n >= 0 && n < GetCount(),
158 _T("invalid index in wxCheckListBox::Delete") );
159
160 if ( !ListView_DeleteItem(GetHwnd(), n) )
161 {
162 wxLogLastError(_T("ListView_DeleteItem"));
163 }
164 m_itemsClientData.RemoveAt(n);
165 }
166
167 // check items
168 // -----------
169
170 bool wxCheckListBox::IsChecked(size_t uiIndex) const
171 {
172 wxCHECK_MSG( uiIndex < (size_t)GetCount(), false,
173 _T("invalid index in wxCheckListBox::IsChecked") );
174
175 return (ListView_GetCheckState(((HWND)GetHWND()), uiIndex) != 0);
176 }
177
178 void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
179 {
180 wxCHECK_RET( uiIndex < (size_t)GetCount(),
181 _T("invalid index in wxCheckListBox::Check") );
182
183 ListView_SetCheckState(((HWND)GetHWND()), uiIndex, bCheck)
184 }
185
186 // interface derived from wxListBox and lower classes
187 // --------------------------------------------------
188
189 void wxCheckListBox::Clear()
190 {
191 int n = GetCount();
192
193 while ( n > 0 )
194 {
195 n--;
196 Delete(n);
197 }
198
199 m_itemsClientData.Clear();
200
201 wxCHECK_RET( n == GetCount(),
202 _T("broken wxCheckListBox::Clear()") );
203 }
204
205 int wxCheckListBox::GetCount() const
206 {
207 return ListView_GetItemCount( (HWND)GetHWND() );
208 }
209
210 int wxCheckListBox::GetSelection() const
211 {
212 return wxNOT_FOUND;
213 }
214
215 int wxCheckListBox::GetSelections(wxArrayInt& aSelections) const
216 {
217 int n = GetCount();
218 while ( n > 0 )
219 {
220 n--;
221 if(IsChecked(n)) aSelections.Insert(n,0);
222 }
223
224 return aSelections.GetCount();
225 }
226
227 wxString wxCheckListBox::GetString(int n) const
228 {
229 const int bufSize = 513;
230 wxChar buf[bufSize];
231 ListView_GetItemText( (HWND)GetHWND(), n, 0, buf, bufSize - 1 );
232 buf[bufSize-1] = _T('\0');
233 wxString str(buf);
234 return str;
235 }
236
237 bool wxCheckListBox::IsSelected(int n) const
238 {
239 return IsChecked(n);
240 }
241
242 void wxCheckListBox::SetString(int n, const wxString& s)
243 {
244 wxCHECK_RET( n < GetCount(),
245 _T("invalid index in wxCheckListBox::SetString") );
246 wxChar *buf = new wxChar[s.length()+1];
247 wxStrcpy(buf, s.c_str());
248 ListView_SetItemText( (HWND)GetHWND(), n, 0, buf );
249 delete [] buf;
250 }
251
252 int wxCheckListBox::DoAppend(const wxString& item)
253 {
254 int n = GetCount();
255 LVITEM newItem;
256 wxZeroMemory(newItem);
257 newItem.iItem = n;
258 int ret = ListView_InsertItem( (HWND)GetHWND(), & newItem );
259 wxCHECK_MSG( n == ret , -1, _T("Item not added") );
260 SetString( ret , item );
261 m_itemsClientData.Insert(NULL, ret);
262 return ret;
263 }
264
265 void* wxCheckListBox::DoGetItemClientData(int n) const
266 {
267 return m_itemsClientData.Item(n);
268 }
269
270 wxClientData* wxCheckListBox::DoGetItemClientObject(int n) const
271 {
272 return (wxClientData *)DoGetItemClientData(n);
273 }
274
275 void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
276 {
277 for( size_t i = 0; i < items.GetCount(); i++ )
278 {
279 Insert(items[i],pos+i);
280 }
281 }
282
283 void wxCheckListBox::DoSetFirstItem(int n)
284 {
285 int pos = ListView_GetTopIndex( (HWND)GetHWND() );
286 if(pos == n) return;
287 POINT ppt;
288 BOOL ret = ListView_GetItemPosition( (HWND)GetHWND(), n, &ppt );
289 wxCHECK_RET( ret == TRUE, _T("Broken DoSetFirstItem") );
290 ListView_Scroll( (HWND)GetHWND(), 0, 0 );
291 ListView_Scroll( (HWND)GetHWND(), 0, ppt.y );
292 }
293
294 void wxCheckListBox::DoSetItemClientData(int n, void* clientData)
295 {
296 m_itemsClientData.Item(n) = clientData;
297 }
298
299 void wxCheckListBox::DoSetItemClientObject(int n, wxClientData* clientData)
300 {
301 DoSetItemClientData(n, clientData);
302 }
303
304 void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
305 {
306 for( size_t i = 0; i < items.GetCount(); i++ )
307 {
308 int pos = Append(items[i]);
309 if( pos >= 0 && clientData )
310 DoSetItemClientData(pos, clientData[i]);
311 }
312 }
313
314 void wxCheckListBox::DoSetSelection(int n, bool select)
315 {
316 Check(n,select);
317 }
318
319 #endif