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