]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/checklst.cpp
tentative fix for BestSize problems (non-native toolbar)
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "checklst.h"
18 #endif
19
20 #include "wx/wxprec.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 #ifndef __DARWIN__
29 #include <Appearance.h>
30 #endif
31
32 // ============================================================================
33 // implementation of wxCheckListBox
34 // ============================================================================
35
36 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
37
38 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
39 END_EVENT_TABLE()
40
41 const short kTextColumnId = 1024 ;
42 const short kCheckboxColumnId = 1025 ;
43
44 // new databrowser based version
45
46 // Listbox item
47 void wxCheckListBox::Init()
48 {
49 }
50
51 bool 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
66 #if TARGET_API_MAC_OSX
67 static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID,
68 DataBrowserItemNotification message, DataBrowserItemDataRef itemData)
69 #else
70 static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID,
71 DataBrowserItemNotification message)
72 #endif
73 {
74 long ref = GetControlReference( browser ) ;
75 if ( ref )
76 {
77 wxCheckListBox* list = wxDynamicCast( (wxObject*) ref , wxCheckListBox ) ;
78 int i = itemID - 1 ;
79 if (i >= 0 && i < list->GetCount() )
80 {
81 bool trigger = false ;
82 wxCommandEvent event(
83 wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() );
84 switch( message )
85 {
86 case kDataBrowserItemDeselected :
87 if ( list->HasMultipleSelection() )
88 trigger = true ;
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 ;
99 }
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
113 // list->GetEventHandler()->ProcessEvent(event) ;
114 }
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 int i = itemID - 1 ;
138 if (i >= 0 && i < list->GetCount() )
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 ) ;
153 int i = itemID - 1 ;
154 if (i >= 0 && i < list->GetCount() )
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 ) ;
182 int i = itemID - 1 ;
183 if (i >= 0 && i < list->GetCount() )
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 }
197
198 }
199 break ;
200
201 default :
202 break ;
203 }
204 }
205
206 return err;
207 }
208 bool 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)
215 {
216 m_macIsUserPane = false ;
217
218 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
219 _T("only one of listbox selection modes can be specified") );
220
221 if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) )
222 return false;
223
224 m_noItems = 0 ; // this will be increased by our append command
225 m_selected = 0;
226
227 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
228
229 m_peer = new wxMacControl(this) ;
230 verify_noerr( ::CreateDataBrowserControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, kDataBrowserListView , m_peer->GetControlRefAddr() ) );
231
232
233 DataBrowserSelectionFlags options = kDataBrowserDragSelect ;
234 if ( style & wxLB_MULTIPLE )
235 {
236 options += kDataBrowserAlwaysExtendSelection + kDataBrowserCmdTogglesSelection ;
237 }
238 else if ( style & wxLB_EXTENDED )
239 {
240 // default behaviour
241 }
242 else
243 {
244 options += kDataBrowserSelectOnlyOne ;
245 }
246 verify_noerr(m_peer->SetSelectionFlags( options ) );
247
248 DataBrowserListViewColumnDesc columnDesc ;
249 columnDesc.headerBtnDesc.titleOffset = 0;
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( "" );
260
261 // check column
262
263 columnDesc.headerBtnDesc.minimumWidth = 30 ;
264 columnDesc.headerBtnDesc.maximumWidth = 30;
265
266 columnDesc.propertyDesc.propertyID = kCheckboxColumnId;
267 columnDesc.propertyDesc.propertyType = kDataBrowserCheckboxType;
268 columnDesc.propertyDesc.propertyFlags = kDataBrowserPropertyIsMutable | kDataBrowserTableViewSelectionColumn |
269 kDataBrowserDefaultPropertyFlags;
270 verify_noerr( m_peer->AddListViewColumn( &columnDesc, kDataBrowserListViewAppendColumn) ) ;
271
272 // text column
273
274 columnDesc.headerBtnDesc.minimumWidth = 0;
275 columnDesc.headerBtnDesc.maximumWidth = 10000;
276
277 columnDesc.propertyDesc.propertyID = kTextColumnId;
278 columnDesc.propertyDesc.propertyType = kDataBrowserTextType;
279 columnDesc.propertyDesc.propertyFlags = kDataBrowserTableViewSelectionColumn
280 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
281 | kDataBrowserListViewTypeSelectColumn
282 #endif
283 ;
284
285
286 verify_noerr( m_peer->AddListViewColumn( &columnDesc, kDataBrowserListViewAppendColumn) ) ;
287
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 ) ) ;
292
293 DataBrowserCallbacks callbacks ;
294 callbacks.version = kDataBrowserLatestCallbacks;
295 InitDataBrowserCallbacks(&callbacks);
296 callbacks.u.v1.itemDataCallback = NewDataBrowserItemDataUPP(ListBoxGetSetItemData);
297 callbacks.u.v1.itemNotificationCallback =
298 #if TARGET_API_MAC_OSX
299 (DataBrowserItemNotificationUPP) NewDataBrowserItemNotificationWithItemUPP(DataBrowserItemNotificationProc) ;
300 #else
301 NewDataBrowserItemNotificationUPP(DataBrowserItemNotificationProc) ;
302 #endif
303 m_peer->SetCallbacks( &callbacks);
304
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
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 bool isChecked = m_checks[item] != 0;
341 if ( check != isChecked )
342 {
343 m_checks[item] = check;
344 UInt32 id = item + 1 ;
345 verify_noerr( m_peer->UpdateItems(kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
346 }
347 }
348
349 // ----------------------------------------------------------------------------
350 // methods forwarded to wxCheckListBox
351 // ----------------------------------------------------------------------------
352
353 void wxCheckListBox::Delete(int n)
354 {
355 wxCHECK_RET( n < GetCount(), _T("invalid index in wxCheckListBox::Delete") );
356
357 wxListBox::Delete(n);
358
359 m_checks.RemoveAt(n);
360 }
361
362 int wxCheckListBox::DoAppend(const wxString& item)
363 {
364 int pos = wxListBox::DoAppend(item);
365
366 // the item is initially unchecked
367 m_checks.Insert(false, pos);
368
369 return pos;
370 }
371
372 void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
373 {
374 wxListBox::DoInsertItems(items, pos);
375
376 size_t count = items.GetCount();
377 for ( size_t n = 0; n < count; n++ )
378 {
379 m_checks.Insert(false, pos + n);
380 }
381 }
382
383 void 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 {
391 m_checks.Add(false);
392 }
393 }
394
395 void wxCheckListBox::DoClear()
396 {
397 m_checks.Empty();
398 }
399
400 #endif // wxUSE_CHECKLISTBOX