1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/listbox.mm
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: listbox.cpp 54820 2008-07-29 20:04:11Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/listbox.h"
22 #include "wx/settings.h"
23 #include "wx/arrstr.h"
24 #include "wx/dcclient.h"
27 #include "wx/osx/private.h"
33 class wxListWidgetCocoaImpl;
35 @interface wxNSTableDataSource : NSObject
37 wxListWidgetCocoaImpl* impl;
40 - (id)tableView:(NSTableView *)aTableView
41 objectValueForTableColumn:(NSTableColumn *)aTableColumn
42 row:(NSInteger)rowIndex;
44 - (id)tableView:(NSTableView *)aTableView
45 setObjectValue:(NSTableColumn *)aTableColumn
46 row:(NSInteger)rowIndex;
48 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView;
50 - (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation;
51 - (wxListWidgetCocoaImpl*) implementation;
55 @interface wxNSTableView : NSTableView
57 wxListWidgetCocoaImpl* impl;
60 - (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation;
61 - (wxListWidgetCocoaImpl*) implementation;
69 class wxCocoaTableColumn;
71 @interface wxNSTableColumn : NSTableColumn
73 wxCocoaTableColumn* column;
76 - (void) setColumn: (wxCocoaTableColumn*) col;
78 - (wxCocoaTableColumn*) column;
82 class WXDLLIMPEXP_CORE wxCocoaTableColumn : public wxListWidgetColumn
85 wxCocoaTableColumn( wxNSTableColumn* column, bool editable )
86 : m_column( column ), m_editable(editable)
94 wxNSTableColumn* GetNSTableColumn() const { return m_column ; }
96 bool IsEditable() const { return m_editable; }
99 wxNSTableColumn* m_column;
103 NSString* column1 = @"1";
105 class wxListWidgetCocoaImpl : public wxWidgetCocoaImpl, public wxListWidgetImpl
108 wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data );
110 ~wxListWidgetCocoaImpl();
112 virtual wxListWidgetColumn* InsertTextColumn( unsigned pos, const wxString& title, bool editable = false,
113 wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ;
114 virtual wxListWidgetColumn* InsertCheckColumn( unsigned pos , const wxString& title, bool editable = false,
115 wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ;
119 virtual void ListDelete( unsigned int n ) ;
120 virtual void ListInsert( unsigned int n ) ;
121 virtual void ListClear() ;
125 virtual void ListDeselectAll();
127 virtual void ListSetSelection( unsigned int n, bool select, bool multi ) ;
128 virtual int ListGetSelection() const ;
130 virtual int ListGetSelections( wxArrayInt& aSelections ) const ;
132 virtual bool ListIsSelected( unsigned int n ) const ;
136 virtual void ListScrollTo( unsigned int n ) ;
140 virtual unsigned int ListGetCount() const ;
142 int ListGetColumnType( int col )
146 virtual void UpdateLine( unsigned int n, wxListWidgetColumn* col = NULL ) ;
147 virtual void UpdateLineToEnd( unsigned int n);
149 wxNSTableView* m_tableView ;
151 wxNSTableDataSource* m_dataSource;
158 @implementation wxNSTableColumn
167 - (void) setColumn: (wxCocoaTableColumn*) col
172 - (wxCocoaTableColumn*) column
179 class wxNSTableViewCellValue : public wxListWidgetCellValue
182 wxNSTableViewCellValue( id &v ) : value(v)
186 virtual ~wxNSTableViewCellValue() {}
188 virtual void Set( CFStringRef v )
190 value = [[(NSString*)v retain] autorelease];
192 virtual void Set( const wxString& value )
194 Set( (CFStringRef) wxCFStringRef( value ) );
196 virtual void Set( int v )
198 value = [NSNumber numberWithInt:v];
201 virtual int GetIntValue() const
203 if ( [value isKindOfClass:[NSNumber class]] )
204 return [ (NSNumber*) value intValue ];
209 virtual wxString GetStringValue() const
211 if ( [value isKindOfClass:[NSString class]] )
212 return wxCFStringRef( (CFStringRef) [value retain] ).AsString();
214 return wxEmptyString;
221 @implementation wxNSTableDataSource
230 - (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation
232 impl = theImplementation;
235 - (wxListWidgetCocoaImpl*) implementation
240 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
243 return impl->ListGetCount();
247 - (id)tableView:(NSTableView *)aTableView
248 objectValueForTableColumn:(NSTableColumn *)aTableColumn
249 row:(NSInteger)rowIndex
251 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
252 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
253 wxCocoaTableColumn* col = [tablecol column];
255 wxNSTableViewCellValue cellvalue(value);
256 lb->GetValueCallback(rowIndex, col, cellvalue);
260 - (void)tableView:(NSTableView *)aTableView
261 setObjectValue:(id)value forTableColumn:(NSTableColumn *)aTableColumn
262 row:(NSInteger)rowIndex
264 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
265 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
266 wxCocoaTableColumn* col = [tablecol column];
267 wxNSTableViewCellValue cellvalue(value);
268 lb->SetValueCallback(rowIndex, col, cellvalue);
273 @implementation wxNSTableView
275 - (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation
277 impl = theImplementation;
280 - (wxListWidgetCocoaImpl*) implementation
292 wxListWidgetCocoaImpl::wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data ) :
293 wxWidgetCocoaImpl( peer, view ), m_tableView(tableview), m_dataSource(data)
297 wxListWidgetCocoaImpl::~wxListWidgetCocoaImpl()
299 [m_dataSource release];
302 unsigned int wxListWidgetCocoaImpl::ListGetCount() const
304 wxListBox* lb = dynamic_cast<wxListBox*> ( GetWXPeer() );
305 return lb->GetCount();
312 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertTextColumn( unsigned pos, const wxString& title, bool editable,
313 wxAlignment just, int defaultWidth)
315 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
316 [col1 setEditable:editable];
318 unsigned formerColCount = [m_tableView numberOfColumns];
320 // there's apparently no way to insert at a specific position
321 [m_tableView addTableColumn:col1 ];
322 if ( pos < formerColCount )
323 [m_tableView moveColumn:formerColCount toColumn:pos];
325 if ( defaultWidth >= 0 )
327 [col1 setMaxWidth:defaultWidth];
328 [col1 setMinWidth:defaultWidth];
331 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
332 [col1 setColumn:wxcol];
334 // owned by the tableview
339 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertCheckColumn( unsigned pos , const wxString& title, bool editable,
340 wxAlignment just, int defaultWidth )
342 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
343 [col1 setEditable:editable];
345 // set your custom cell & set it up
346 NSButtonCell* checkbox = [[NSButtonCell alloc] init];
347 [checkbox setTitle:@""];
348 [checkbox setButtonType:NSSwitchButton];
349 [col1 setDataCell:checkbox] ;
352 unsigned formerColCount = [m_tableView numberOfColumns];
354 // there's apparently no way to insert at a specific position
355 [m_tableView addTableColumn:col1 ];
356 if ( pos < formerColCount )
357 [m_tableView moveColumn:formerColCount toColumn:pos];
359 if ( defaultWidth >= 0 )
361 [col1 setMaxWidth:defaultWidth];
362 [col1 setMinWidth:defaultWidth];
365 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
366 [col1 setColumn:wxcol];
368 // owned by the tableview
375 // inserting / removing lines
378 void wxListWidgetCocoaImpl::ListInsert( unsigned int n )
382 wxListBoxCocoaLine* line = new wxListBoxCocoaLine();
383 line->SetLabel(items[i]);
384 if ( m_items.size() <= n+i )
385 m_items.push_back( line );
387 m_items.insert(m_items.begin()+n, line);
389 NSMutableDictionary* line = [[NSMutableDictionary alloc] init];
390 [line setObject:wxCFStringRef(items[i]).AsNSString() forKey:column1];
391 NSMutableArray* array = [m_dataSource items];
392 if ( [array count] <= n+i )
393 [array addObject:line];
395 [array insertObject:line atIndex:n];
399 [m_tableView reloadData];
402 void wxListWidgetCocoaImpl::ListDelete( unsigned int n )
404 [m_tableView reloadData];
407 void wxListWidgetCocoaImpl::ListClear()
409 [m_tableView reloadData];
414 void wxListWidgetCocoaImpl::ListDeselectAll()
416 [m_tableView deselectAll:nil];
419 void wxListWidgetCocoaImpl::ListSetSelection( unsigned int n, bool select, bool multi )
423 [m_tableView selectRow: n byExtendingSelection:multi];
425 [m_tableView deselectRow: n];
429 int wxListWidgetCocoaImpl::ListGetSelection() const
434 int wxListWidgetCocoaImpl::ListGetSelections( wxArrayInt& aSelections ) const
439 bool wxListWidgetCocoaImpl::ListIsSelected( unsigned int n ) const
446 void wxListWidgetCocoaImpl::ListScrollTo( unsigned int n )
448 [m_tableView scrollRowToVisible:n];
452 void wxListWidgetCocoaImpl::UpdateLine( unsigned int n, wxListWidgetColumn* col )
455 [m_tableView reloadData];
458 void wxListWidgetCocoaImpl::UpdateLineToEnd( unsigned int n)
461 [m_tableView reloadData];
468 wxWidgetImplType* wxWidgetImpl::CreateListBox( wxWindowMac* wxpeer,
476 NSView* superv = (wxpeer->GetParent()->GetHandle() );
478 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
479 NSScrollView* scrollview = [[NSScrollView alloc] initWithFrame:r];
481 // use same scroll flags logic as msw
483 [scrollview setHasVerticalScroller:YES];
485 if ( style & wxLB_HSCROLL )
486 [scrollview setHasHorizontalScroller:YES];
488 [scrollview setAutohidesScrollers: ((style & wxLB_ALWAYS_SB) ? NO : YES)];
490 // setting up the true table
492 wxNSTableView* tableview = [[wxNSTableView alloc] init];
493 [scrollview setDocumentView:tableview];
496 // only one multi-select mode available
497 if ( (style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE) )
498 [tableview setAllowsMultipleSelection:YES];
500 // simple listboxes have no header row
501 [tableview setHeaderView:nil];
503 [tableview setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle];
504 wxNSTableDataSource* ds = [[ wxNSTableDataSource alloc] init];
505 [tableview setDataSource:ds];
506 [superv addSubview:scrollview];
507 wxListWidgetCocoaImpl* c = new wxListWidgetCocoaImpl( wxpeer, scrollview, tableview, ds );
508 [tableview setImplementation:c];
509 [ds setImplementation:c];
513 int wxListBox::DoListHitTest(const wxPoint& inpoint) const
518 // There are few reasons why this is complicated:
519 // 1) There is no native HitTest function for Mac
520 // 2) GetDataBrowserItemPartBounds only works on visible items
521 // 3) We can't do it through GetDataBrowserTableView[Item]RowHeight
522 // because what it returns is basically inaccurate in the context
523 // of the coordinates we want here, but we use this as a guess
524 // for where the first visible item lies
526 wxPoint point = inpoint;
528 // get column property ID (req. for call to itempartbounds)
529 DataBrowserTableViewColumnID colId = 0;
530 err = GetDataBrowserTableViewColumnProperty(m_peer->GetControlRef(), 0, &colId);
531 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserTableViewColumnProperty"));
533 // OK, first we need to find the first visible item we have -
534 // this will be the "low" for our binary search. There is no real
535 // easy way around this, as we will need to do a SLOW linear search
536 // until we find a visible item, but we can do a cheap calculation
537 // via the row height to speed things up a bit
538 UInt32 scrollx, scrolly;
539 err = GetDataBrowserScrollPosition(m_peer->GetControlRef(), &scrollx, &scrolly);
540 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserScrollPosition"));
543 err = GetDataBrowserTableViewRowHeight(m_peer->GetControlRef(), &height);
544 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserTableViewRowHeight"));
546 // these indices are 0-based, as usual, so we need to add 1 to them when
547 // passing them to data browser functions which use 1-based indices
548 int low = scrolly / height,
549 high = GetCount() - 1;
551 // search for the first visible item (note that the scroll guess above
552 // is the low bounds of where the item might lie so we only use that as a
553 // starting point - we should reach it within 1 or 2 iterations of the loop)
554 while ( low <= high )
557 err = GetDataBrowserItemPartBounds(
558 m_peer->GetControlRef(), low + 1, colId,
559 kDataBrowserPropertyEnclosingPart,
560 &bounds); // note +1 to translate to Mac ID
564 // errDataBrowserItemNotFound is expected as it simply means that the
565 // item is not currently visible -- but other errors are not
566 wxCHECK_MSG( err == errDataBrowserItemNotFound, wxNOT_FOUND,
567 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
572 // NOW do a binary search for where the item lies, searching low again if
573 // we hit an item that isn't visible
574 while ( low <= high )
576 int mid = (low + high) / 2;
579 err = GetDataBrowserItemPartBounds(
580 m_peer->GetControlRef(), mid + 1, colId,
581 kDataBrowserPropertyEnclosingPart,
582 &bounds); //note +1 to trans to mac id
583 wxCHECK_MSG( err == noErr || err == errDataBrowserItemNotFound,
585 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
587 if ( err == errDataBrowserItemNotFound )
589 // item not visible, attempt to find a visible one
593 else // visible item, do actual hitttest
595 // if point is within the bounds, return this item (since we assume
596 // all x coords of items are equal we only test the x coord in
598 if ((point.x >= bounds.left && point.x <= bounds.right) &&
599 (point.y >= bounds.top && point.y <= bounds.bottom) )
605 if ( point.y < bounds.top )
606 // index(bounds) greater then key(point)
609 // index(bounds) less then key(point)
617 #endif // wxUSE_LISTBOX