]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: checklst.cpp | |
3 | // Purpose: implementation of wxCheckListBox class | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // headers & declarations | |
14 | // ============================================================================ | |
15 | ||
16 | #ifdef __GNUG__ | |
17 | #pragma implementation "checklst.h" | |
18 | #endif | |
19 | ||
20 | #include "wx/defs.h" | |
21 | ||
22 | #if wxUSE_CHECKLISTBOX | |
23 | ||
24 | #include "wx/checklst.h" | |
25 | #include "wx/arrstr.h" | |
26 | ||
27 | #include "wx/mac/uma.h" | |
28 | #include <Appearance.h> | |
29 | ||
30 | // ============================================================================ | |
31 | // implementation of wxCheckListBox | |
32 | // ============================================================================ | |
33 | ||
34 | IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox) | |
35 | ||
36 | BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox) | |
37 | END_EVENT_TABLE() | |
38 | ||
39 | const short kTextColumnId = 1024 ; | |
40 | const short kCheckboxColumnId = 1025 ; | |
41 | ||
42 | // new databrowser based version | |
43 | ||
44 | // Listbox item | |
45 | void wxCheckListBox::Init() | |
46 | { | |
47 | } | |
48 | ||
49 | bool wxCheckListBox::Create(wxWindow *parent, | |
50 | wxWindowID id, | |
51 | const wxPoint &pos, | |
52 | const wxSize &size, | |
53 | const wxArrayString& choices, | |
54 | long style, | |
55 | const wxValidator& validator, | |
56 | const wxString &name) | |
57 | { | |
58 | wxCArrayString chs(choices); | |
59 | ||
60 | return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
61 | style, validator, name); | |
62 | } | |
63 | ||
64 | #if TARGET_API_MAC_OSX | |
65 | static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID, | |
66 | DataBrowserItemNotification message, DataBrowserItemDataRef itemData) | |
67 | #else | |
68 | static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID, | |
69 | DataBrowserItemNotification message) | |
70 | #endif | |
71 | { | |
72 | long ref = GetControlReference( browser ) ; | |
73 | if ( ref ) | |
74 | { | |
75 | wxCheckListBox* list = wxDynamicCast( (wxObject*) ref , wxCheckListBox ) ; | |
76 | for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i ) | |
77 | if ( list->m_idArray[i] == (long) itemID ) | |
78 | { | |
79 | bool trigger = false ; | |
80 | wxCommandEvent event( | |
81 | wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() ); | |
82 | switch( message ) | |
83 | { | |
84 | case kDataBrowserItemDeselected : | |
85 | if ( list->HasMultipleSelection() ) | |
86 | trigger = true ; | |
87 | break ; | |
88 | case kDataBrowserItemSelected : | |
89 | trigger = true ; | |
90 | break ; | |
91 | case kDataBrowserItemDoubleClicked : | |
92 | event.SetEventType(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED) ; | |
93 | trigger = true ; | |
94 | break ; | |
95 | default : | |
96 | break ; | |
97 | } | |
98 | ||
99 | if ( trigger ) | |
100 | { | |
101 | event.SetEventObject( list ); | |
102 | if ( list->HasClientObjectData() ) | |
103 | event.SetClientObject( list->GetClientObject(i) ); | |
104 | else if ( list->HasClientUntypedData() ) | |
105 | event.SetClientData( list->GetClientData(i) ); | |
106 | event.SetString( list->GetString(i) ); | |
107 | event.SetInt(i) ; | |
108 | event.SetExtraLong( list->HasMultipleSelection() ? message == kDataBrowserItemSelected : TRUE ); | |
109 | wxPostEvent( list->GetEventHandler() , event ) ; | |
110 | // direct notification is not always having the listbox GetSelection() having in synch with event | |
111 | // list->GetEventHandler()->ProcessEvent(event) ; | |
112 | } | |
113 | ||
114 | break ; | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
119 | ||
120 | static pascal OSStatus ListBoxGetSetItemData(ControlRef browser, | |
121 | DataBrowserItemID itemID, DataBrowserPropertyID property, | |
122 | DataBrowserItemDataRef itemData, Boolean changeValue) | |
123 | { | |
124 | OSStatus err = errDataBrowserPropertyNotSupported; | |
125 | ||
126 | if ( ! changeValue ) | |
127 | { | |
128 | switch (property) | |
129 | { | |
130 | ||
131 | case kTextColumnId: | |
132 | { | |
133 | long ref = GetControlReference( browser ) ; | |
134 | if ( ref ) | |
135 | { | |
136 | wxCheckListBox* list = wxDynamicCast( (wxObject*) ref , wxCheckListBox ) ; | |
137 | for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i ) | |
138 | if ( list->m_idArray[i] == (long) itemID ) | |
139 | { | |
140 | wxMacCFStringHolder cf( list->GetString(i) , list->GetFont().GetEncoding() ) ; | |
141 | verify_noerr( ::SetDataBrowserItemDataText( itemData , cf ) ) ; | |
142 | err = noErr ; | |
143 | break ; | |
144 | } | |
145 | } | |
146 | } | |
147 | break; | |
148 | case kCheckboxColumnId : | |
149 | { | |
150 | long ref = GetControlReference( browser ) ; | |
151 | if ( ref ) | |
152 | { | |
153 | wxCheckListBox* list = wxDynamicCast( (wxObject*) ref , wxCheckListBox ) ; | |
154 | for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i ) | |
155 | if ( list->m_idArray[i] == (long) itemID ) | |
156 | { | |
157 | verify_noerr( ::SetDataBrowserItemDataButtonValue( itemData , list->IsChecked( i ) ? kThemeButtonOn : kThemeButtonOff ) ) ; | |
158 | err = noErr ; | |
159 | break ; | |
160 | } | |
161 | } | |
162 | } | |
163 | break ; | |
164 | case kDataBrowserItemIsEditableProperty: | |
165 | { | |
166 | err = ::SetDataBrowserItemDataBooleanValue(itemData, true); | |
167 | } | |
168 | break; | |
169 | ||
170 | default: | |
171 | break; | |
172 | } | |
173 | } | |
174 | else | |
175 | { | |
176 | switch( property ) | |
177 | { | |
178 | case kCheckboxColumnId : | |
179 | { | |
180 | long ref = GetControlReference( browser ) ; | |
181 | if ( ref ) | |
182 | { | |
183 | wxCheckListBox* list = wxDynamicCast( (wxObject*) ref , wxCheckListBox ) ; | |
184 | for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i ) | |
185 | if ( list->m_idArray[i] == (long) itemID ) | |
186 | { | |
187 | // we have to change this behind the back, since Check() would be triggering another update round | |
188 | bool newVal = !list->IsChecked( i ) ; | |
189 | verify_noerr( ::SetDataBrowserItemDataButtonValue( itemData , newVal ? kThemeButtonOn : kThemeButtonOff ) ) ; | |
190 | err = noErr ; | |
191 | list->m_checks[ i ] = newVal ; | |
192 | ||
193 | wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, list->GetId()); | |
194 | event.SetInt(i); | |
195 | event.SetEventObject(list); | |
196 | list->GetEventHandler()->ProcessEvent(event); | |
197 | ||
198 | break ; | |
199 | } | |
200 | } | |
201 | ||
202 | } | |
203 | break ; | |
204 | ||
205 | default : | |
206 | break ; | |
207 | } | |
208 | } | |
209 | ||
210 | return err; | |
211 | } | |
212 | bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id, | |
213 | const wxPoint& pos, | |
214 | const wxSize& size, | |
215 | int n, const wxString choices[], | |
216 | long style, | |
217 | const wxValidator& validator, | |
218 | const wxString& name) | |
219 | { | |
220 | m_macIsUserPane = FALSE ; | |
221 | ||
222 | wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED), | |
223 | _T("only one of listbox selection modes can be specified") ); | |
224 | ||
225 | if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) ) | |
226 | return false; | |
227 | ||
228 | m_noItems = 0 ; // this will be increased by our append command | |
229 | m_selected = 0; | |
230 | m_nextId = 1 ; | |
231 | ||
232 | ||
233 | Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ; | |
234 | ||
235 | m_peer = new wxMacControl() ; | |
236 | verify_noerr( ::CreateDataBrowserControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, kDataBrowserListView , m_peer->GetControlRefAddr() ) ); | |
237 | ||
238 | ||
239 | DataBrowserSelectionFlags options = kDataBrowserDragSelect ; | |
240 | if ( style & wxLB_MULTIPLE ) | |
241 | { | |
242 | options += kDataBrowserAlwaysExtendSelection + kDataBrowserCmdTogglesSelection ; | |
243 | } | |
244 | else if ( style & wxLB_EXTENDED ) | |
245 | { | |
246 | // default behaviour | |
247 | } | |
248 | else | |
249 | { | |
250 | options += kDataBrowserSelectOnlyOne ; | |
251 | } | |
252 | verify_noerr(m_peer->SetSelectionFlags( options ) ); | |
253 | ||
254 | DataBrowserListViewColumnDesc columnDesc ; | |
255 | columnDesc.headerBtnDesc.titleOffset = 0; | |
256 | columnDesc.headerBtnDesc.version = kDataBrowserListViewLatestHeaderDesc; | |
257 | ||
258 | columnDesc.headerBtnDesc.btnFontStyle.flags = | |
259 | kControlUseFontMask | kControlUseJustMask; | |
260 | ||
261 | columnDesc.headerBtnDesc.btnContentInfo.contentType = kControlNoContent; | |
262 | columnDesc.headerBtnDesc.btnFontStyle.just = teFlushDefault; | |
263 | columnDesc.headerBtnDesc.btnFontStyle.font = kControlFontViewSystemFont; | |
264 | columnDesc.headerBtnDesc.btnFontStyle.style = normal; | |
265 | columnDesc.headerBtnDesc.titleString = NULL ; // CFSTR( "" ); | |
266 | ||
267 | // check column | |
268 | ||
269 | columnDesc.headerBtnDesc.minimumWidth = 30 ; | |
270 | columnDesc.headerBtnDesc.maximumWidth = 30; | |
271 | ||
272 | columnDesc.propertyDesc.propertyID = kCheckboxColumnId; | |
273 | columnDesc.propertyDesc.propertyType = kDataBrowserCheckboxType; | |
274 | columnDesc.propertyDesc.propertyFlags = kDataBrowserPropertyIsMutable | kDataBrowserTableViewSelectionColumn | | |
275 | kDataBrowserDefaultPropertyFlags; | |
276 | verify_noerr( m_peer->AddListViewColumn( &columnDesc, kDataBrowserListViewAppendColumn) ) ; | |
277 | ||
278 | // text column | |
279 | ||
280 | columnDesc.headerBtnDesc.minimumWidth = 0; | |
281 | columnDesc.headerBtnDesc.maximumWidth = 10000; | |
282 | ||
283 | columnDesc.propertyDesc.propertyID = kTextColumnId; | |
284 | columnDesc.propertyDesc.propertyType = kDataBrowserTextType; | |
285 | columnDesc.propertyDesc.propertyFlags = kDataBrowserTableViewSelectionColumn | |
286 | #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 | |
287 | | kDataBrowserListViewTypeSelectColumn | |
288 | #endif | |
289 | ; | |
290 | ||
291 | ||
292 | verify_noerr( m_peer->AddListViewColumn( &columnDesc, kDataBrowserListViewAppendColumn) ) ; | |
293 | ||
294 | verify_noerr( m_peer->AutoSizeListViewColumns() ) ; | |
295 | verify_noerr( m_peer->SetHasScrollBars( false , true ) ) ; | |
296 | verify_noerr( m_peer->SetTableViewHiliteStyle( kDataBrowserTableViewFillHilite ) ) ; | |
297 | verify_noerr( m_peer->SetListViewHeaderBtnHeight(0 ) ) ; | |
298 | ||
299 | DataBrowserCallbacks callbacks ; | |
300 | callbacks.version = kDataBrowserLatestCallbacks; | |
301 | InitDataBrowserCallbacks(&callbacks); | |
302 | callbacks.u.v1.itemDataCallback = NewDataBrowserItemDataUPP(ListBoxGetSetItemData); | |
303 | callbacks.u.v1.itemNotificationCallback = | |
304 | #if TARGET_API_MAC_OSX | |
305 | (DataBrowserItemNotificationUPP) NewDataBrowserItemNotificationWithItemUPP(DataBrowserItemNotificationProc) ; | |
306 | #else | |
307 | NewDataBrowserItemNotificationUPP(DataBrowserItemNotificationProc) ; | |
308 | #endif | |
309 | m_peer->SetCallbacks( &callbacks); | |
310 | ||
311 | MacPostControlCreate(pos,size) ; | |
312 | ||
313 | for ( int i = 0 ; i < n ; i++ ) | |
314 | { | |
315 | Append( choices[i] ) ; | |
316 | } | |
317 | ||
318 | SetBestSize(size); // Needed because it is a wxControlWithItems | |
319 | ||
320 | return TRUE; | |
321 | } | |
322 | ||
323 | // ---------------------------------------------------------------------------- | |
324 | // wxCheckListBox functions | |
325 | // ---------------------------------------------------------------------------- | |
326 | ||
327 | bool wxCheckListBox::IsChecked(size_t item) const | |
328 | { | |
329 | wxCHECK_MSG( item < m_checks.GetCount(), FALSE, | |
330 | _T("invalid index in wxCheckListBox::IsChecked") ); | |
331 | ||
332 | return m_checks[item] != 0; | |
333 | } | |
334 | ||
335 | void wxCheckListBox::Check(size_t item, bool check) | |
336 | { | |
337 | wxCHECK_RET( item < m_checks.GetCount(), | |
338 | _T("invalid index in wxCheckListBox::Check") ); | |
339 | ||
340 | bool isChecked = m_checks[item] != 0; | |
341 | if ( check != isChecked ) | |
342 | { | |
343 | m_checks[item] = check; | |
344 | UInt32 id = m_idArray[item] ; | |
345 | verify_noerr( m_peer->UpdateItems(kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ; | |
346 | } | |
347 | } | |
348 | ||
349 | // ---------------------------------------------------------------------------- | |
350 | // methods forwarded to wxCheckListBox | |
351 | // ---------------------------------------------------------------------------- | |
352 | ||
353 | void wxCheckListBox::Delete(int n) | |
354 | { | |
355 | wxCHECK_RET( n < GetCount(), _T("invalid index in wxCheckListBox::Delete") ); | |
356 | ||
357 | wxListBox::Delete(n); | |
358 | ||
359 | m_checks.RemoveAt(n); | |
360 | } | |
361 | ||
362 | int wxCheckListBox::DoAppend(const wxString& item) | |
363 | { | |
364 | int pos = wxListBox::DoAppend(item); | |
365 | ||
366 | // the item is initially unchecked | |
367 | m_checks.Insert(FALSE, pos); | |
368 | ||
369 | return pos; | |
370 | } | |
371 | ||
372 | void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos) | |
373 | { | |
374 | wxListBox::DoInsertItems(items, pos); | |
375 | ||
376 | size_t count = items.GetCount(); | |
377 | for ( size_t n = 0; n < count; n++ ) | |
378 | { | |
379 | m_checks.Insert(FALSE, pos + n); | |
380 | } | |
381 | } | |
382 | ||
383 | void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData) | |
384 | { | |
385 | // call it first as it does DoClear() | |
386 | wxListBox::DoSetItems(items, clientData); | |
387 | ||
388 | size_t count = items.GetCount(); | |
389 | for ( size_t n = 0; n < count; n++ ) | |
390 | { | |
391 | m_checks.Add(FALSE); | |
392 | } | |
393 | } | |
394 | ||
395 | void wxCheckListBox::DoClear() | |
396 | { | |
397 | m_checks.Empty(); | |
398 | } | |
399 | ||
400 | #endif // wxUSE_CHECKLISTBOX |