]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/checklst.cpp
filedata implementation streamlined
[wxWidgets.git] / src / mac / carbon / checklst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/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 // new DataBrowser-based version
13
14
15 #include "wx/wxprec.h"
16
17 #if wxUSE_CHECKLISTBOX
18
19 #include "wx/checklst.h"
20 #include "wx/arrstr.h"
21
22 #include "wx/mac/uma.h"
23
24 #ifndef __DARWIN__
25 #include <Appearance.h>
26 #endif
27
28 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
29
30 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
31 END_EVENT_TABLE()
32
33 const short kTextColumnId = 1024;
34 const short kCheckboxColumnId = 1025;
35
36
37 void wxCheckListBox::Init()
38 {
39 }
40
41 bool wxCheckListBox::Create(
42 wxWindow *parent,
43 wxWindowID id,
44 const wxPoint &pos,
45 const wxSize &size,
46 const wxArrayString& choices,
47 long style,
48 const wxValidator& validator,
49 const wxString &name )
50 {
51 wxCArrayString chs( choices );
52
53 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(), style, validator, name );
54 }
55
56 #if TARGET_API_MAC_OSX
57 static pascal void DataBrowserItemNotificationProc(
58 ControlRef browser,
59 DataBrowserItemID itemID,
60 DataBrowserItemNotification message,
61 DataBrowserItemDataRef itemData )
62 #else
63 static pascal void DataBrowserItemNotificationProc(
64 ControlRef browser,
65 DataBrowserItemID itemID,
66 DataBrowserItemNotification message )
67 #endif
68 {
69 long ref = GetControlReference( browser );
70 if (ref != 0)
71 {
72 wxCheckListBox* list = wxDynamicCast( (wxObject*)ref, wxCheckListBox );
73 int i = itemID - 1;
74 if ((i >= 0) && (i < (int)list->GetCount()))
75 {
76 bool trigger = false;
77 wxCommandEvent event( wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() );
78 switch ( message )
79 {
80 case kDataBrowserItemDeselected:
81 if ( list->HasMultipleSelection() )
82 trigger = true;
83 break;
84
85 case kDataBrowserItemSelected:
86 trigger = true;
87 break;
88
89 case kDataBrowserItemDoubleClicked:
90 event.SetEventType( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED );
91 trigger = true;
92 break;
93
94 default:
95 break;
96 }
97
98 if ( trigger )
99 {
100 event.SetEventObject( list );
101 if ( list->HasClientObjectData() )
102 event.SetClientObject( list->GetClientObject( i ) );
103 else if ( list->HasClientUntypedData() )
104 event.SetClientData( list->GetClientData( i ) );
105 event.SetString( list->GetString( i ) );
106 event.SetInt( i );
107 event.SetExtraLong( list->HasMultipleSelection() ? message == kDataBrowserItemSelected : true );
108 wxPostEvent( list->GetEventHandler(), event );
109
110 // direct notification is not always having the listbox GetSelection() having in sync with event
111 // list->GetEventHandler()->ProcessEvent( event );
112 }
113 }
114 }
115 }
116
117 static pascal OSStatus ListBoxGetSetItemData(
118 ControlRef browser,
119 DataBrowserItemID itemID,
120 DataBrowserPropertyID property,
121 DataBrowserItemDataRef itemData,
122 Boolean changeValue )
123 {
124 OSStatus err = errDataBrowserPropertyNotSupported;
125
126 if ( !changeValue )
127 {
128 switch (property)
129 {
130 case kTextColumnId:
131 {
132 long ref = GetControlReference( browser );
133 if (ref != 0)
134 {
135 wxCheckListBox* list = wxDynamicCast( (wxObject*) ref, wxCheckListBox );
136 int i = itemID - 1;
137 if ((i >= 0) && (i < (int)list->GetCount()))
138 {
139 wxMacCFStringHolder cf( list->GetString( i ), list->GetFont().GetEncoding() );
140 verify_noerr( ::SetDataBrowserItemDataText( itemData, cf ) );
141 err = noErr;
142 }
143 }
144 }
145 break;
146
147 case kCheckboxColumnId:
148 {
149 long ref = GetControlReference( browser );
150 if (ref != 0)
151 {
152 wxCheckListBox* list = wxDynamicCast( (wxObject*)ref, wxCheckListBox );
153 int i = itemID - 1;
154 if ((i >= 0) && (i < (int)list->GetCount()))
155 {
156 verify_noerr( ::SetDataBrowserItemDataButtonValue( itemData, list->IsChecked( i ) ? kThemeButtonOn : kThemeButtonOff ) );
157 err = noErr;
158 }
159 }
160 }
161 break;
162
163 case kDataBrowserItemIsEditableProperty:
164 {
165 err = ::SetDataBrowserItemDataBooleanValue( itemData, true );
166 }
167 break;
168
169 default:
170 break;
171 }
172 }
173 else
174 {
175 switch ( property )
176 {
177 case kCheckboxColumnId:
178 {
179 long ref = GetControlReference( browser );
180 if (ref != 0)
181 {
182 wxCheckListBox* list = wxDynamicCast( (wxObject*) ref, wxCheckListBox );
183 int i = itemID - 1;
184 if ((i >= 0) && (i < (int)list->GetCount()))
185 {
186 // we have to change this behind the back, since Check() would be triggering another update round
187 bool newVal = !list->IsChecked( i );
188 verify_noerr( ::SetDataBrowserItemDataButtonValue( itemData, newVal ? kThemeButtonOn : kThemeButtonOff ) );
189 err = noErr;
190 list->m_checks[i] = newVal;
191
192 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, list->GetId());
193 event.SetInt( i );
194 event.SetEventObject( list );
195 list->GetEventHandler()->ProcessEvent( event );
196 }
197 }
198 }
199 break;
200
201 default:
202 break;
203 }
204 }
205
206 return err;
207 }
208
209 bool wxCheckListBox::Create(
210 wxWindow *parent,
211 wxWindowID id,
212 const wxPoint& pos,
213 const wxSize& size,
214 int n,
215 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 wxT("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 // this will be increased by our Append command
229 m_noItems = 0;
230 m_selected = 0;
231
232 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
233
234 m_peer = new wxMacControl( this );
235 OSStatus err = ::CreateDataBrowserControl(
236 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
237 &bounds, kDataBrowserListView, m_peer->GetControlRefAddr() );
238 verify_noerr( err );
239
240 DataBrowserSelectionFlags options = kDataBrowserDragSelect;
241 if ( style & wxLB_MULTIPLE )
242 {
243 options |= kDataBrowserAlwaysExtendSelection | kDataBrowserCmdTogglesSelection;
244 }
245 else if ( style & wxLB_EXTENDED )
246 {
247 // default behaviour
248 }
249 else
250 {
251 options |= kDataBrowserSelectOnlyOne;
252 }
253
254 err = m_peer->SetSelectionFlags( options );
255 verify_noerr( err );
256
257 DataBrowserListViewColumnDesc columnDesc;
258 columnDesc.headerBtnDesc.titleOffset = 0;
259 columnDesc.headerBtnDesc.version = kDataBrowserListViewLatestHeaderDesc;
260
261 columnDesc.headerBtnDesc.btnFontStyle.flags =
262 kControlUseFontMask | kControlUseJustMask;
263
264 columnDesc.headerBtnDesc.btnContentInfo.contentType = kControlNoContent;
265 columnDesc.headerBtnDesc.btnFontStyle.just = teFlushDefault;
266 columnDesc.headerBtnDesc.btnFontStyle.font = kControlFontViewSystemFont;
267 columnDesc.headerBtnDesc.btnFontStyle.style = normal;
268 columnDesc.headerBtnDesc.titleString = NULL; // CFSTR( "" );
269
270 // check column
271
272 columnDesc.headerBtnDesc.minimumWidth = 30;
273 columnDesc.headerBtnDesc.maximumWidth = 30;
274
275 columnDesc.propertyDesc.propertyID = kCheckboxColumnId;
276 columnDesc.propertyDesc.propertyType = kDataBrowserCheckboxType;
277 columnDesc.propertyDesc.propertyFlags =
278 kDataBrowserPropertyIsMutable
279 | kDataBrowserTableViewSelectionColumn
280 | kDataBrowserDefaultPropertyFlags;
281 err = m_peer->AddListViewColumn( &columnDesc, kDataBrowserListViewAppendColumn );
282 verify_noerr( err );
283
284 // text column
285
286 columnDesc.headerBtnDesc.minimumWidth = 0;
287 columnDesc.headerBtnDesc.maximumWidth = 10000;
288
289 columnDesc.propertyDesc.propertyID = kTextColumnId;
290 columnDesc.propertyDesc.propertyType = kDataBrowserTextType;
291 columnDesc.propertyDesc.propertyFlags = kDataBrowserTableViewSelectionColumn
292 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
293 | kDataBrowserListViewTypeSelectColumn
294 #endif
295 ;
296
297 verify_noerr( m_peer->AddListViewColumn( &columnDesc, kDataBrowserListViewAppendColumn ) );
298
299 verify_noerr( m_peer->AutoSizeListViewColumns() );
300 verify_noerr( m_peer->SetHasScrollBars( false, true ) );
301 verify_noerr( m_peer->SetTableViewHiliteStyle( kDataBrowserTableViewFillHilite ) );
302 verify_noerr( m_peer->SetListViewHeaderBtnHeight( 0 ) );
303
304 DataBrowserCallbacks callbacks;
305 callbacks.version = kDataBrowserLatestCallbacks;
306 InitDataBrowserCallbacks( &callbacks );
307 callbacks.u.v1.itemDataCallback = NewDataBrowserItemDataUPP( &ListBoxGetSetItemData );
308 callbacks.u.v1.itemNotificationCallback =
309 #if TARGET_API_MAC_OSX
310 (DataBrowserItemNotificationUPP) NewDataBrowserItemNotificationWithItemUPP( &DataBrowserItemNotificationProc );
311 #else
312 NewDataBrowserItemNotificationUPP( &DataBrowserItemNotificationProc );
313 #endif
314 m_peer->SetCallbacks( &callbacks );
315
316 #if 0
317 // shouldn't be necessary anymore under 10.2
318 m_peer->SetData( kControlNoPart, kControlDataBrowserIncludesFrameAndFocusTag, (Boolean)false );
319 m_peer->SetNeedsFocusRect( true );
320 #endif
321
322 MacPostControlCreate( pos, size );
323
324 for ( int i = 0; i < n; i++ )
325 {
326 Append( choices[i] );
327 }
328
329 // Needed because it is a wxControlWithItems
330 SetBestSize( size );
331
332 return true;
333 }
334
335 // ----------------------------------------------------------------------------
336 // wxCheckListBox functions
337 // ----------------------------------------------------------------------------
338
339 bool wxCheckListBox::IsChecked(size_t item) const
340 {
341 wxCHECK_MSG( item < m_checks.GetCount(), false,
342 wxT("invalid index in wxCheckListBox::IsChecked") );
343
344 return m_checks[item] != 0;
345 }
346
347 void wxCheckListBox::Check(size_t item, bool check)
348 {
349 wxCHECK_RET( item < m_checks.GetCount(),
350 wxT("invalid index in wxCheckListBox::Check") );
351
352 bool isChecked = m_checks[item] != 0;
353 if ( check != isChecked )
354 {
355 m_checks[item] = check;
356 UInt32 id = item + 1;
357 OSStatus err = m_peer->UpdateItems(
358 kDataBrowserNoItem, 1, &id,
359 kDataBrowserItemNoProperty, kDataBrowserItemNoProperty );
360 verify_noerr( err );
361 }
362 }
363
364 // ----------------------------------------------------------------------------
365 // methods forwarded to wxCheckListBox
366 // ----------------------------------------------------------------------------
367
368 void wxCheckListBox::Delete(int n)
369 {
370 wxCHECK_RET( (size_t)n < GetCount(), wxT("invalid index in wxCheckListBox::Delete") );
371
372 wxListBox::Delete( n );
373 m_checks.RemoveAt( n );
374 }
375
376 int wxCheckListBox::DoAppend(const wxString& item)
377 {
378 int pos = wxListBox::DoAppend( item );
379
380 // the item is initially unchecked
381 m_checks.Insert( false, pos );
382
383 return pos;
384 }
385
386 void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
387 {
388 wxListBox::DoInsertItems( items, pos );
389
390 size_t count = items.GetCount();
391 for ( size_t n = 0; n < count; n++ )
392 {
393 m_checks.Insert( false, pos + n );
394 }
395 }
396
397 void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
398 {
399 // call it first as it does DoClear()
400 wxListBox::DoSetItems( items, clientData );
401
402 size_t count = items.GetCount();
403 for ( size_t n = 0; n < count; n++ )
404 {
405 m_checks.Add( false );
406 }
407 }
408
409 void wxCheckListBox::DoClear()
410 {
411 m_checks.Empty();
412 }
413
414 #endif // wxUSE_CHECKLISTBOX