]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/checklst.cpp
colors are only set if non-black, otherwise Enable(false) does not gray them out
[wxWidgets.git] / src / mac / carbon / checklst.cpp
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 ) );
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(SetDataBrowserSelectionFlags (*m_peer, 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(::AddDataBrowserListViewColumn(*m_peer, &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(::AddDataBrowserListViewColumn(*m_peer, &columnDesc, kDataBrowserListViewAppendColumn) ) ;
293
294 verify_noerr(::AutoSizeDataBrowserListViewColumns( *m_peer ) ) ;
295 verify_noerr(::SetDataBrowserHasScrollBars( *m_peer , false , true ) ) ;
296 verify_noerr(::SetDataBrowserTableViewHiliteStyle( *m_peer, kDataBrowserTableViewFillHilite ) ) ;
297 verify_noerr(::SetDataBrowserListViewHeaderBtnHeight( *m_peer , 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 SetDataBrowserCallbacks(*m_peer, &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 // intermediate var is needed to avoid compiler warning with VC++
341 bool isChecked = m_checks[item] != 0;
342 if ( check != isChecked )
343 {
344 m_checks[item] = check;
345 UInt32 id = m_idArray[item] ;
346 verify_noerr( ::UpdateDataBrowserItems( *m_peer , kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
347 }
348 }
349
350 // ----------------------------------------------------------------------------
351 // methods forwarded to wxCheckListBox
352 // ----------------------------------------------------------------------------
353
354 void wxCheckListBox::Delete(int n)
355 {
356 wxCHECK_RET( n < GetCount(), _T("invalid index in wxCheckListBox::Delete") );
357
358 wxListBox::Delete(n);
359
360 m_checks.RemoveAt(n);
361 }
362
363 int wxCheckListBox::DoAppend(const wxString& item)
364 {
365 int pos = wxListBox::DoAppend(item);
366
367 // the item is initially unchecked
368 m_checks.Insert(FALSE, pos);
369
370 return pos;
371 }
372
373 void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
374 {
375 wxListBox::DoInsertItems(items, pos);
376
377 size_t count = items.GetCount();
378 for ( size_t n = 0; n < count; n++ )
379 {
380 m_checks.Insert(FALSE, pos + n);
381 }
382 }
383
384 void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
385 {
386 // call it first as it does DoClear()
387 wxListBox::DoSetItems(items, clientData);
388
389 size_t count = items.GetCount();
390 for ( size_t n = 0; n < count; n++ )
391 {
392 m_checks.Add(FALSE);
393 }
394 }
395
396 void wxCheckListBox::DoClear()
397 {
398 m_checks.Empty();
399 }
400
401 #endif // wxUSE_CHECKLISTBOX