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