]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/checklst.cpp
use current mouse position as default position in wxWindow::PopupMenu (works better...
[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
e9576ca5 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
16#ifdef __GNUG__
17#pragma implementation "checklst.h"
18#endif
19
b698c8e9 20#include "wx/defs.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"
7f0c3a63 28#include <Appearance.h>
dbfc5b97 29
e9576ca5 30// ============================================================================
dbfc5b97 31// implementation of wxCheckListBox
e9576ca5
SC
32// ============================================================================
33
dbfc5b97
SC
34IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
35
d058c153
SC
36BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
37END_EVENT_TABLE()
dbfc5b97 38
d058c153
SC
39const short kTextColumnId = 1024 ;
40const short kCheckboxColumnId = 1025 ;
dbfc5b97 41
d058c153 42// new databrowser based version
dbfc5b97 43
d058c153 44// Listbox item
dbfc5b97
SC
45void wxCheckListBox::Init()
46{
47}
48
584ad2a3
MB
49bool 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
d058c153
SC
64#if TARGET_API_MAC_OSX
65static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID,
66 DataBrowserItemNotification message, DataBrowserItemDataRef itemData)
67#else
68static 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 );
be1aa07f
SC
109 wxPostEvent( list->GetEventHandler() , event ) ;
110 // direct notification is not always having the listbox GetSelection() having in synch with event
111 // list->GetEventHandler()->ProcessEvent(event) ;
d058c153
SC
112 }
113
114 break ;
115 }
116 }
117}
118
119
120static 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 for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i )
138 if ( list->m_idArray[i] == (long) itemID )
139 {
140 wxMacCFStringHolder cf( list->GetString(i) , list->GetFont().GetEncoding() ) ;
141 verify_noerr( ::SetDataBrowserItemDataText( itemData , cf ) ) ;
142 err = noErr ;
143 break ;
144 }
145 }
146 }
147 break;
148 case kCheckboxColumnId :
149 {
150 long ref = GetControlReference( browser ) ;
151 if ( ref )
152 {
153 wxCheckListBox* list = wxDynamicCast( (wxObject*) ref , wxCheckListBox ) ;
154 for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i )
155 if ( list->m_idArray[i] == (long) itemID )
156 {
157 verify_noerr( ::SetDataBrowserItemDataButtonValue( itemData , list->IsChecked( i ) ? kThemeButtonOn : kThemeButtonOff ) ) ;
158 err = noErr ;
159 break ;
160 }
161 }
162 }
163 break ;
164 case kDataBrowserItemIsEditableProperty:
165 {
166 err = ::SetDataBrowserItemDataBooleanValue(itemData, true);
167 }
168 break;
169
170 default:
171 break;
172 }
173 }
174 else
175 {
176 switch( property )
177 {
178 case kCheckboxColumnId :
179 {
180 long ref = GetControlReference( browser ) ;
181 if ( ref )
182 {
183 wxCheckListBox* list = wxDynamicCast( (wxObject*) ref , wxCheckListBox ) ;
184 for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i )
185 if ( list->m_idArray[i] == (long) itemID )
186 {
187 // we have to change this behind the back, since Check() would be triggering another update round
188 bool newVal = !list->IsChecked( i ) ;
189 verify_noerr( ::SetDataBrowserItemDataButtonValue( itemData , newVal ? kThemeButtonOn : kThemeButtonOff ) ) ;
190 err = noErr ;
191 list->m_checks[ i ] = newVal ;
192
193 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, list->GetId());
194 event.SetInt(i);
195 event.SetEventObject(list);
196 list->GetEventHandler()->ProcessEvent(event);
197
198 break ;
199 }
200 }
201
202 }
203 break ;
204
205 default :
206 break ;
207 }
208 }
209
210 return err;
211}
212bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
213 const wxPoint& pos,
214 const wxSize& size,
215 int n, const wxString choices[],
216 long style,
217 const wxValidator& validator,
218 const wxString& name)
dbfc5b97 219{
facd6764 220 m_macIsUserPane = FALSE ;
d058c153
SC
221
222 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
223 _T("only one of listbox selection modes can be specified") );
facd6764 224
d058c153 225 if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) )
b45ed7a2
VZ
226 return false;
227
dbfc5b97
SC
228 m_noItems = 0 ; // this will be increased by our append command
229 m_selected = 0;
d058c153 230 m_nextId = 1 ;
dbfc5b97 231
facd6764
SC
232
233 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
d058c153 234 ControlRef browser ;
dbfc5b97 235
d058c153
SC
236 verify_noerr( ::CreateDataBrowserControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, kDataBrowserListView , (ControlRef *)&m_macControl ) );
237 browser = (ControlRef) m_macControl ;
dbfc5b97 238
d058c153 239 DataBrowserSelectionFlags options = kDataBrowserDragSelect ;
dbfc5b97
SC
240 if ( style & wxLB_MULTIPLE )
241 {
d058c153 242 options += kDataBrowserAlwaysExtendSelection + kDataBrowserCmdTogglesSelection ;
dbfc5b97
SC
243 }
244 else if ( style & wxLB_EXTENDED )
245 {
d058c153 246 // default behaviour
dbfc5b97
SC
247 }
248 else
249 {
d058c153 250 options += kDataBrowserSelectOnlyOne ;
dbfc5b97 251 }
d058c153
SC
252 verify_noerr(SetDataBrowserSelectionFlags (browser, options ) );
253
254 DataBrowserListViewColumnDesc columnDesc ;
255 columnDesc.headerBtnDesc.titleOffset = 0;
256 columnDesc.headerBtnDesc.version = kDataBrowserListViewLatestHeaderDesc;
257
258 columnDesc.headerBtnDesc.btnFontStyle.flags =
259 kControlUseFontMask | kControlUseJustMask;
260
261 columnDesc.headerBtnDesc.btnContentInfo.contentType = kControlNoContent;
262 columnDesc.headerBtnDesc.btnFontStyle.just = teFlushDefault;
263 columnDesc.headerBtnDesc.btnFontStyle.font = kControlFontViewSystemFont;
264 columnDesc.headerBtnDesc.btnFontStyle.style = normal;
265 columnDesc.headerBtnDesc.titleString = NULL ; // CFSTR( "" );
266
267 // check column
268
269 columnDesc.headerBtnDesc.minimumWidth = 30 ;
270 columnDesc.headerBtnDesc.maximumWidth = 30;
271
272 columnDesc.propertyDesc.propertyID = kCheckboxColumnId;
273 columnDesc.propertyDesc.propertyType = kDataBrowserCheckboxType;
274 columnDesc.propertyDesc.propertyFlags = kDataBrowserPropertyIsMutable | kDataBrowserTableViewSelectionColumn |
275 kDataBrowserDefaultPropertyFlags;
276 verify_noerr(::AddDataBrowserListViewColumn(browser, &columnDesc, kDataBrowserListViewAppendColumn) ) ;
277
278 // text column
279
280 columnDesc.headerBtnDesc.minimumWidth = 0;
281 columnDesc.headerBtnDesc.maximumWidth = 10000;
282
283 columnDesc.propertyDesc.propertyID = kTextColumnId;
284 columnDesc.propertyDesc.propertyType = kDataBrowserTextType;
285 columnDesc.propertyDesc.propertyFlags = kDataBrowserTableViewSelectionColumn
286#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
287 | kDataBrowserListViewTypeSelectColumn
288#endif
289 ;
290
291
292 verify_noerr(::AddDataBrowserListViewColumn(browser, &columnDesc, kDataBrowserListViewAppendColumn) ) ;
293
294 verify_noerr(::AutoSizeDataBrowserListViewColumns( browser ) ) ;
295 verify_noerr(::SetDataBrowserHasScrollBars( browser , false , true ) ) ;
296 verify_noerr(::SetDataBrowserTableViewHiliteStyle( browser, kDataBrowserTableViewFillHilite ) ) ;
297 verify_noerr(::SetDataBrowserListViewHeaderBtnHeight( browser , 0 ) ) ;
298
299 DataBrowserCallbacks callbacks ;
300 callbacks.version = kDataBrowserLatestCallbacks;
301 InitDataBrowserCallbacks(&callbacks);
302 callbacks.u.v1.itemDataCallback = NewDataBrowserItemDataUPP(ListBoxGetSetItemData);
303 callbacks.u.v1.itemNotificationCallback =
304#if TARGET_API_MAC_OSX
305 (DataBrowserItemNotificationUPP) NewDataBrowserItemNotificationWithItemUPP(DataBrowserItemNotificationProc) ;
306#else
307 NewDataBrowserItemNotificationUPP(DataBrowserItemNotificationProc) ;
308#endif
309 SetDataBrowserCallbacks(browser, &callbacks);
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
SC
318 SetBestSize(size); // Needed because it is a wxControlWithItems
319
dbfc5b97
SC
320 return TRUE;
321}
e9576ca5
SC
322
323// ----------------------------------------------------------------------------
dbfc5b97 324// wxCheckListBox functions
e9576ca5
SC
325// ----------------------------------------------------------------------------
326
dbfc5b97
SC
327bool 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
335void wxCheckListBox::Check(size_t item, bool check)
336{
337 wxCHECK_RET( item < m_checks.GetCount(),
338 _T("invalid index in wxCheckListBox::Check") );
339
340 // intermediate var is needed to avoid compiler warning with VC++
341 bool isChecked = m_checks[item] != 0;
342 if ( check != isChecked )
343 {
344 m_checks[item] = check;
d058c153
SC
345 UInt32 id = m_idArray[item] ;
346 verify_noerr( ::UpdateDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
dbfc5b97
SC
347 }
348}
349
350// ----------------------------------------------------------------------------
d058c153 351// methods forwarded to wxCheckListBox
dbfc5b97
SC
352// ----------------------------------------------------------------------------
353
354void wxCheckListBox::Delete(int n)
355{
d058c153 356 wxCHECK_RET( n < GetCount(), _T("invalid index in wxCheckListBox::Delete") );
e9576ca5 357
dbfc5b97 358 wxListBox::Delete(n);
e9576ca5 359
dbfc5b97
SC
360 m_checks.RemoveAt(n);
361}
362
363int wxCheckListBox::DoAppend(const wxString& item)
e9576ca5 364{
dbfc5b97
SC
365 int pos = wxListBox::DoAppend(item);
366
367 // the item is initially unchecked
368 m_checks.Insert(FALSE, pos);
369
370 return pos;
e9576ca5
SC
371}
372
dbfc5b97 373void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
e9576ca5 374{
dbfc5b97
SC
375 wxListBox::DoInsertItems(items, pos);
376
377 size_t count = items.GetCount();
378 for ( size_t n = 0; n < count; n++ )
379 {
380 m_checks.Insert(FALSE, pos + n);
381 }
e9576ca5
SC
382}
383
dbfc5b97
SC
384void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
385{
386 // call it first as it does DoClear()
387 wxListBox::DoSetItems(items, clientData);
388
389 size_t count = items.GetCount();
390 for ( size_t n = 0; n < count; n++ )
391 {
392 m_checks.Add(FALSE);
393 }
394}
e9576ca5 395
dbfc5b97 396void wxCheckListBox::DoClear()
e9576ca5 397{
dbfc5b97 398 m_checks.Empty();
e9576ca5
SC
399}
400
dbfc5b97 401#endif // wxUSE_CHECKLISTBOX