]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/checklst.cpp
factoring common code out
[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 #ifdef __GNUG__
17 #pragma implementation "checklst.h"
18 #endif
19
20 #include "wx/defs.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 #include <Appearance.h>
29
30 // ============================================================================
31 // implementation of wxCheckListBox
32 // ============================================================================
33
34 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
35
36 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
37 END_EVENT_TABLE()
38
39 const short kTextColumnId = 1024 ;
40 const short kCheckboxColumnId = 1025 ;
41
42 // new databrowser based version
43
44 // Listbox item
45 void wxCheckListBox::Init()
46 {
47 }
48
49 bool 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
64 #if TARGET_API_MAC_OSX
65 static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID,
66 DataBrowserItemNotification message, DataBrowserItemDataRef itemData)
67 #else
68 static 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
118 static 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 }
210 bool 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)
217 {
218 m_macIsUserPane = FALSE ;
219
220 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
221 _T("only one of listbox selection modes can be specified") );
222
223 if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) )
224 return false;
225
226 m_noItems = 0 ; // this will be increased by our append command
227 m_selected = 0;
228 m_nextId = 1 ;
229
230
231 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
232 ControlRef browser ;
233
234 verify_noerr( ::CreateDataBrowserControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, kDataBrowserListView , (ControlRef *)&m_macControl ) );
235 browser = (ControlRef) m_macControl ;
236
237 DataBrowserSelectionFlags options = kDataBrowserDragSelect ;
238 if ( style & wxLB_MULTIPLE )
239 {
240 options += kDataBrowserAlwaysExtendSelection + kDataBrowserCmdTogglesSelection ;
241 }
242 else if ( style & wxLB_EXTENDED )
243 {
244 // default behaviour
245 }
246 else
247 {
248 options += kDataBrowserSelectOnlyOne ;
249 }
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
309 MacPostControlCreate(pos,size) ;
310
311 for ( int i = 0 ; i < n ; i++ )
312 {
313 Append( choices[i] ) ;
314 }
315
316 SetBestSize(size); // Needed because it is a wxControlWithItems
317
318 return TRUE;
319 }
320
321 // ----------------------------------------------------------------------------
322 // wxCheckListBox functions
323 // ----------------------------------------------------------------------------
324
325 bool 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
333 void 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;
343 UInt32 id = m_idArray[item] ;
344 verify_noerr( ::UpdateDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
345 }
346 }
347
348 // ----------------------------------------------------------------------------
349 // methods forwarded to wxCheckListBox
350 // ----------------------------------------------------------------------------
351
352 void wxCheckListBox::Delete(int n)
353 {
354 wxCHECK_RET( n < GetCount(), _T("invalid index in wxCheckListBox::Delete") );
355
356 wxListBox::Delete(n);
357
358 m_checks.RemoveAt(n);
359 }
360
361 int wxCheckListBox::DoAppend(const wxString& item)
362 {
363 int pos = wxListBox::DoAppend(item);
364
365 // the item is initially unchecked
366 m_checks.Insert(FALSE, pos);
367
368 return pos;
369 }
370
371 void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
372 {
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 }
380 }
381
382 void 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 }
393
394 void wxCheckListBox::DoClear()
395 {
396 m_checks.Empty();
397 }
398
399 #endif // wxUSE_CHECKLISTBOX