]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/checklst.cpp
Fix docs for Bind
[wxWidgets.git] / src / mac / carbon / checklst.cpp
CommitLineData
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
e9576ca5
SC
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// headers & declarations
14// ============================================================================
15
16#ifdef __GNUG__
17#pragma implementation "checklst.h"
18#endif
19
b698c8e9 20#include "wx/defs.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
34IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
35
d058c153
SC
36BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
37END_EVENT_TABLE()
dbfc5b97 38
d058c153
SC
39const short kTextColumnId = 1024 ;
40const short kCheckboxColumnId = 1025 ;
dbfc5b97 41
d058c153 42// new databrowser based version
dbfc5b97 43
d058c153 44// Listbox item
dbfc5b97
SC
45void wxCheckListBox::Init()
46{
47}
48
584ad2a3
MB
49bool 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
65static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID,
66 DataBrowserItemNotification message, DataBrowserItemDataRef itemData)
67#else
68static 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 list->GetEventHandler()->ProcessEvent(event) ;
110 }
111
112 break ;
113 }
114 }
115}
116
117
118static 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 ) ;
135 for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i )
136 if ( list->m_idArray[i] == (long) itemID )
137 {
138 wxMacCFStringHolder cf( list->GetString(i) , list->GetFont().GetEncoding() ) ;
139 verify_noerr( ::SetDataBrowserItemDataText( itemData , cf ) ) ;
140 err = noErr ;
141 break ;
142 }
143 }
144 }
145 break;
146 case kCheckboxColumnId :
147 {
148 long ref = GetControlReference( browser ) ;
149 if ( ref )
150 {
151 wxCheckListBox* list = wxDynamicCast( (wxObject*) ref , wxCheckListBox ) ;
152 for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i )
153 if ( list->m_idArray[i] == (long) itemID )
154 {
155 verify_noerr( ::SetDataBrowserItemDataButtonValue( itemData , list->IsChecked( i ) ? kThemeButtonOn : kThemeButtonOff ) ) ;
156 err = noErr ;
157 break ;
158 }
159 }
160 }
161 break ;
162 case kDataBrowserItemIsEditableProperty:
163 {
164 err = ::SetDataBrowserItemDataBooleanValue(itemData, true);
165 }
166 break;
167
168 default:
169 break;
170 }
171 }
172 else
173 {
174 switch( property )
175 {
176 case kCheckboxColumnId :
177 {
178 long ref = GetControlReference( browser ) ;
179 if ( ref )
180 {
181 wxCheckListBox* list = wxDynamicCast( (wxObject*) ref , wxCheckListBox ) ;
182 for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i )
183 if ( list->m_idArray[i] == (long) itemID )
184 {
185 // we have to change this behind the back, since Check() would be triggering another update round
186 bool newVal = !list->IsChecked( i ) ;
187 verify_noerr( ::SetDataBrowserItemDataButtonValue( itemData , newVal ? kThemeButtonOn : kThemeButtonOff ) ) ;
188 err = noErr ;
189 list->m_checks[ i ] = newVal ;
190
191 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, list->GetId());
192 event.SetInt(i);
193 event.SetEventObject(list);
194 list->GetEventHandler()->ProcessEvent(event);
195
196 break ;
197 }
198 }
199
200 }
201 break ;
202
203 default :
204 break ;
205 }
206 }
207
208 return err;
209}
210bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
211 const wxPoint& pos,
212 const wxSize& size,
213 int n, const wxString choices[],
214 long style,
215 const wxValidator& validator,
216 const wxString& name)
dbfc5b97 217{
facd6764 218 m_macIsUserPane = FALSE ;
d058c153
SC
219
220 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
221 _T("only one of listbox selection modes can be specified") );
facd6764 222
d058c153 223 if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) )
b45ed7a2
VZ
224 return false;
225
dbfc5b97
SC
226 m_noItems = 0 ; // this will be increased by our append command
227 m_selected = 0;
d058c153 228 m_nextId = 1 ;
dbfc5b97 229
facd6764
SC
230
231 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
d058c153 232 ControlRef browser ;
dbfc5b97 233
d058c153
SC
234 verify_noerr( ::CreateDataBrowserControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, kDataBrowserListView , (ControlRef *)&m_macControl ) );
235 browser = (ControlRef) m_macControl ;
dbfc5b97 236
d058c153 237 DataBrowserSelectionFlags options = kDataBrowserDragSelect ;
dbfc5b97
SC
238 if ( style & wxLB_MULTIPLE )
239 {
d058c153 240 options += kDataBrowserAlwaysExtendSelection + kDataBrowserCmdTogglesSelection ;
dbfc5b97
SC
241 }
242 else if ( style & wxLB_EXTENDED )
243 {
d058c153 244 // default behaviour
dbfc5b97
SC
245 }
246 else
247 {
d058c153 248 options += kDataBrowserSelectOnlyOne ;
dbfc5b97 249 }
d058c153
SC
250 verify_noerr(SetDataBrowserSelectionFlags (browser, options ) );
251
252 DataBrowserListViewColumnDesc columnDesc ;
253 columnDesc.headerBtnDesc.titleOffset = 0;
254 columnDesc.headerBtnDesc.version = kDataBrowserListViewLatestHeaderDesc;
255
256 columnDesc.headerBtnDesc.btnFontStyle.flags =
257 kControlUseFontMask | kControlUseJustMask;
258
259 columnDesc.headerBtnDesc.btnContentInfo.contentType = kControlNoContent;
260 columnDesc.headerBtnDesc.btnFontStyle.just = teFlushDefault;
261 columnDesc.headerBtnDesc.btnFontStyle.font = kControlFontViewSystemFont;
262 columnDesc.headerBtnDesc.btnFontStyle.style = normal;
263 columnDesc.headerBtnDesc.titleString = NULL ; // CFSTR( "" );
264
265 // check column
266
267 columnDesc.headerBtnDesc.minimumWidth = 30 ;
268 columnDesc.headerBtnDesc.maximumWidth = 30;
269
270 columnDesc.propertyDesc.propertyID = kCheckboxColumnId;
271 columnDesc.propertyDesc.propertyType = kDataBrowserCheckboxType;
272 columnDesc.propertyDesc.propertyFlags = kDataBrowserPropertyIsMutable | kDataBrowserTableViewSelectionColumn |
273 kDataBrowserDefaultPropertyFlags;
274 verify_noerr(::AddDataBrowserListViewColumn(browser, &columnDesc, kDataBrowserListViewAppendColumn) ) ;
275
276 // text column
277
278 columnDesc.headerBtnDesc.minimumWidth = 0;
279 columnDesc.headerBtnDesc.maximumWidth = 10000;
280
281 columnDesc.propertyDesc.propertyID = kTextColumnId;
282 columnDesc.propertyDesc.propertyType = kDataBrowserTextType;
283 columnDesc.propertyDesc.propertyFlags = kDataBrowserTableViewSelectionColumn
284#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
285 | kDataBrowserListViewTypeSelectColumn
286#endif
287 ;
288
289
290 verify_noerr(::AddDataBrowserListViewColumn(browser, &columnDesc, kDataBrowserListViewAppendColumn) ) ;
291
292 verify_noerr(::AutoSizeDataBrowserListViewColumns( browser ) ) ;
293 verify_noerr(::SetDataBrowserHasScrollBars( browser , false , true ) ) ;
294 verify_noerr(::SetDataBrowserTableViewHiliteStyle( browser, kDataBrowserTableViewFillHilite ) ) ;
295 verify_noerr(::SetDataBrowserListViewHeaderBtnHeight( browser , 0 ) ) ;
296
297 DataBrowserCallbacks callbacks ;
298 callbacks.version = kDataBrowserLatestCallbacks;
299 InitDataBrowserCallbacks(&callbacks);
300 callbacks.u.v1.itemDataCallback = NewDataBrowserItemDataUPP(ListBoxGetSetItemData);
301 callbacks.u.v1.itemNotificationCallback =
302#if TARGET_API_MAC_OSX
303 (DataBrowserItemNotificationUPP) NewDataBrowserItemNotificationWithItemUPP(DataBrowserItemNotificationProc) ;
304#else
305 NewDataBrowserItemNotificationUPP(DataBrowserItemNotificationProc) ;
306#endif
307 SetDataBrowserCallbacks(browser, &callbacks);
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
325bool 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
333void wxCheckListBox::Check(size_t item, bool check)
334{
335 wxCHECK_RET( item < m_checks.GetCount(),
336 _T("invalid index in wxCheckListBox::Check") );
337
338 // intermediate var is needed to avoid compiler warning with VC++
339 bool isChecked = m_checks[item] != 0;
340 if ( check != isChecked )
341 {
342 m_checks[item] = check;
d058c153
SC
343 UInt32 id = m_idArray[item] ;
344 verify_noerr( ::UpdateDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
dbfc5b97
SC
345 }
346}
347
348// ----------------------------------------------------------------------------
d058c153 349// methods forwarded to wxCheckListBox
dbfc5b97
SC
350// ----------------------------------------------------------------------------
351
352void wxCheckListBox::Delete(int n)
353{
d058c153 354 wxCHECK_RET( n < GetCount(), _T("invalid index in wxCheckListBox::Delete") );
e9576ca5 355
dbfc5b97 356 wxListBox::Delete(n);
e9576ca5 357
dbfc5b97
SC
358 m_checks.RemoveAt(n);
359}
360
361int wxCheckListBox::DoAppend(const wxString& item)
e9576ca5 362{
dbfc5b97
SC
363 int pos = wxListBox::DoAppend(item);
364
365 // the item is initially unchecked
366 m_checks.Insert(FALSE, pos);
367
368 return pos;
e9576ca5
SC
369}
370
dbfc5b97 371void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
e9576ca5 372{
dbfc5b97
SC
373 wxListBox::DoInsertItems(items, pos);
374
375 size_t count = items.GetCount();
376 for ( size_t n = 0; n < count; n++ )
377 {
378 m_checks.Insert(FALSE, pos + n);
379 }
e9576ca5
SC
380}
381
dbfc5b97
SC
382void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
383{
384 // call it first as it does DoClear()
385 wxListBox::DoSetItems(items, clientData);
386
387 size_t count = items.GetCount();
388 for ( size_t n = 0; n < count; n++ )
389 {
390 m_checks.Add(FALSE);
391 }
392}
e9576ca5 393
dbfc5b97 394void wxCheckListBox::DoClear()
e9576ca5 395{
dbfc5b97 396 m_checks.Empty();
e9576ca5
SC
397}
398
dbfc5b97 399#endif // wxUSE_CHECKLISTBOX