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