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