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;
62 - (void)clickedAction: (id) sender;
63 - (void)doubleClickedAction: (id) sender;
71 class wxCocoaTableColumn;
73 @interface wxNSTableColumn : NSTableColumn
75 wxCocoaTableColumn* column;
78 - (void) setColumn: (wxCocoaTableColumn*) col;
80 - (wxCocoaTableColumn*) column;
84 class WXDLLIMPEXP_CORE wxCocoaTableColumn : public wxListWidgetColumn
87 wxCocoaTableColumn( wxNSTableColumn* column, bool editable )
88 : m_column( column ), m_editable(editable)
96 wxNSTableColumn* GetNSTableColumn() const { return m_column ; }
98 bool IsEditable() const { return m_editable; }
101 wxNSTableColumn* m_column;
105 NSString* column1 = @"1";
107 class wxListWidgetCocoaImpl : public wxWidgetCocoaImpl, public wxListWidgetImpl
110 wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data );
112 ~wxListWidgetCocoaImpl();
114 virtual wxListWidgetColumn* InsertTextColumn( unsigned pos, const wxString& title, bool editable = false,
115 wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ;
116 virtual wxListWidgetColumn* InsertCheckColumn( unsigned pos , const wxString& title, bool editable = false,
117 wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ;
121 virtual void ListDelete( unsigned int n ) ;
122 virtual void ListInsert( unsigned int n ) ;
123 virtual void ListClear() ;
127 virtual void ListDeselectAll();
129 virtual void ListSetSelection( unsigned int n, bool select, bool multi ) ;
130 virtual int ListGetSelection() const ;
132 virtual int ListGetSelections( wxArrayInt& aSelections ) const ;
134 virtual bool ListIsSelected( unsigned int n ) const ;
138 virtual void ListScrollTo( unsigned int n ) ;
142 virtual unsigned int ListGetCount() const ;
144 int ListGetColumnType( int col )
148 virtual void UpdateLine( unsigned int n, wxListWidgetColumn* col = NULL ) ;
149 virtual void UpdateLineToEnd( unsigned int n);
151 wxNSTableView* m_tableView ;
153 wxNSTableDataSource* m_dataSource;
160 @implementation wxNSTableColumn
169 - (void) setColumn: (wxCocoaTableColumn*) col
174 - (wxCocoaTableColumn*) column
181 class wxNSTableViewCellValue : public wxListWidgetCellValue
184 wxNSTableViewCellValue( id &v ) : value(v)
188 virtual ~wxNSTableViewCellValue() {}
190 virtual void Set( CFStringRef v )
192 value = [[(NSString*)v retain] autorelease];
194 virtual void Set( const wxString& value )
196 Set( (CFStringRef) wxCFStringRef( value ) );
198 virtual void Set( int v )
200 value = [NSNumber numberWithInt:v];
203 virtual int GetIntValue() const
205 if ( [value isKindOfClass:[NSNumber class]] )
206 return [ (NSNumber*) value intValue ];
211 virtual wxString GetStringValue() const
213 if ( [value isKindOfClass:[NSString class]] )
214 return wxCFStringRef( (CFStringRef) [value retain] ).AsString();
216 return wxEmptyString;
223 @implementation wxNSTableDataSource
232 - (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation
234 impl = theImplementation;
237 - (wxListWidgetCocoaImpl*) implementation
242 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
245 return impl->ListGetCount();
249 - (id)tableView:(NSTableView *)aTableView
250 objectValueForTableColumn:(NSTableColumn *)aTableColumn
251 row:(NSInteger)rowIndex
253 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
254 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
255 wxCocoaTableColumn* col = [tablecol column];
257 wxNSTableViewCellValue cellvalue(value);
258 lb->GetValueCallback(rowIndex, col, cellvalue);
262 - (void)tableView:(NSTableView *)aTableView
263 setObjectValue:(id)value forTableColumn:(NSTableColumn *)aTableColumn
264 row:(NSInteger)rowIndex
266 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
267 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
268 wxCocoaTableColumn* col = [tablecol column];
269 wxNSTableViewCellValue cellvalue(value);
270 lb->SetValueCallback(rowIndex, col, cellvalue);
275 @implementation wxNSTableView
277 - (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation
279 impl = theImplementation;
282 - (wxListWidgetCocoaImpl*) implementation
291 [self setTarget: self];
292 [self setAction: @selector(clickedAction:)];
293 [self setDoubleAction: @selector(doubleClickedAction:)];
297 - (void) clickedAction: (id) sender
301 wxListBox *list = static_cast<wxListBox*> ( impl->GetWXPeer());
302 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
304 wxCommandEvent event( wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() );
306 int sel = [self clickedRow];
307 if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?)
310 list->HandleLineEvent( sel, false );
314 - (void) doubleClickedAction: (id) sender
318 wxListBox *list = static_cast<wxListBox*> ( impl->GetWXPeer());
319 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
321 int sel = [self clickedRow];
322 if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?)
325 list->HandleLineEvent( sel, true );
335 wxListWidgetCocoaImpl::wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data ) :
336 wxWidgetCocoaImpl( peer, view ), m_tableView(tableview), m_dataSource(data)
340 wxListWidgetCocoaImpl::~wxListWidgetCocoaImpl()
342 [m_dataSource release];
345 unsigned int wxListWidgetCocoaImpl::ListGetCount() const
347 wxListBox* lb = dynamic_cast<wxListBox*> ( GetWXPeer() );
348 return lb->GetCount();
355 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertTextColumn( unsigned pos, const wxString& title, bool editable,
356 wxAlignment just, int defaultWidth)
358 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
359 [col1 setEditable:editable];
361 unsigned formerColCount = [m_tableView numberOfColumns];
363 // there's apparently no way to insert at a specific position
364 [m_tableView addTableColumn:col1 ];
365 if ( pos < formerColCount )
366 [m_tableView moveColumn:formerColCount toColumn:pos];
368 if ( defaultWidth >= 0 )
370 [col1 setMaxWidth:defaultWidth];
371 [col1 setMinWidth:defaultWidth];
374 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
375 [col1 setColumn:wxcol];
377 // owned by the tableview
382 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertCheckColumn( unsigned pos , const wxString& title, bool editable,
383 wxAlignment just, int defaultWidth )
385 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
386 [col1 setEditable:editable];
388 // set your custom cell & set it up
389 NSButtonCell* checkbox = [[NSButtonCell alloc] init];
390 [checkbox setTitle:@""];
391 [checkbox setButtonType:NSSwitchButton];
392 [col1 setDataCell:checkbox] ;
395 unsigned formerColCount = [m_tableView numberOfColumns];
397 // there's apparently no way to insert at a specific position
398 [m_tableView addTableColumn:col1 ];
399 if ( pos < formerColCount )
400 [m_tableView moveColumn:formerColCount toColumn:pos];
402 if ( defaultWidth >= 0 )
404 [col1 setMaxWidth:defaultWidth];
405 [col1 setMinWidth:defaultWidth];
408 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
409 [col1 setColumn:wxcol];
411 // owned by the tableview
418 // inserting / removing lines
421 void wxListWidgetCocoaImpl::ListInsert( unsigned int n )
425 wxListBoxCocoaLine* line = new wxListBoxCocoaLine();
426 line->SetLabel(items[i]);
427 if ( m_items.size() <= n+i )
428 m_items.push_back( line );
430 m_items.insert(m_items.begin()+n, line);
432 NSMutableDictionary* line = [[NSMutableDictionary alloc] init];
433 [line setObject:wxCFStringRef(items[i]).AsNSString() forKey:column1];
434 NSMutableArray* array = [m_dataSource items];
435 if ( [array count] <= n+i )
436 [array addObject:line];
438 [array insertObject:line atIndex:n];
442 [m_tableView reloadData];
445 void wxListWidgetCocoaImpl::ListDelete( unsigned int n )
447 [m_tableView reloadData];
450 void wxListWidgetCocoaImpl::ListClear()
452 [m_tableView reloadData];
457 void wxListWidgetCocoaImpl::ListDeselectAll()
459 [m_tableView deselectAll:nil];
462 void wxListWidgetCocoaImpl::ListSetSelection( unsigned int n, bool select, bool multi )
466 [m_tableView selectRow: n byExtendingSelection:multi];
468 [m_tableView deselectRow: n];
472 int wxListWidgetCocoaImpl::ListGetSelection() const
474 return [m_tableView selectedRow];
477 int wxListWidgetCocoaImpl::ListGetSelections( wxArrayInt& aSelections ) const
481 int count = ListGetCount();
483 for ( int i = 0; i < count; ++i)
485 if ([m_tableView isRowSelected:count])
489 return aSelections.Count();
492 bool wxListWidgetCocoaImpl::ListIsSelected( unsigned int n ) const
494 return [m_tableView isRowSelected:n];
499 void wxListWidgetCocoaImpl::ListScrollTo( unsigned int n )
501 [m_tableView scrollRowToVisible:n];
505 void wxListWidgetCocoaImpl::UpdateLine( unsigned int n, wxListWidgetColumn* col )
508 [m_tableView reloadData];
511 void wxListWidgetCocoaImpl::UpdateLineToEnd( unsigned int n)
514 [m_tableView reloadData];
521 wxWidgetImplType* wxWidgetImpl::CreateListBox( wxWindowMac* wxpeer,
529 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
530 NSScrollView* scrollview = [[NSScrollView alloc] initWithFrame:r];
532 // use same scroll flags logic as msw
534 [scrollview setHasVerticalScroller:YES];
536 if ( style & wxLB_HSCROLL )
537 [scrollview setHasHorizontalScroller:YES];
539 [scrollview setAutohidesScrollers: ((style & wxLB_ALWAYS_SB) ? NO : YES)];
541 // setting up the true table
543 wxNSTableView* tableview = [[wxNSTableView alloc] init];
544 [scrollview setDocumentView:tableview];
547 // only one multi-select mode available
548 if ( (style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE) )
549 [tableview setAllowsMultipleSelection:YES];
551 // simple listboxes have no header row
552 [tableview setHeaderView:nil];
554 [tableview setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle];
555 wxNSTableDataSource* ds = [[ wxNSTableDataSource alloc] init];
556 [tableview setDataSource:ds];
557 wxListWidgetCocoaImpl* c = new wxListWidgetCocoaImpl( wxpeer, scrollview, tableview, ds );
558 [tableview setImplementation:c];
559 [ds setImplementation:c];
563 int wxListBox::DoListHitTest(const wxPoint& inpoint) const
568 // There are few reasons why this is complicated:
569 // 1) There is no native HitTest function for Mac
570 // 2) GetDataBrowserItemPartBounds only works on visible items
571 // 3) We can't do it through GetDataBrowserTableView[Item]RowHeight
572 // because what it returns is basically inaccurate in the context
573 // of the coordinates we want here, but we use this as a guess
574 // for where the first visible item lies
576 wxPoint point = inpoint;
578 // get column property ID (req. for call to itempartbounds)
579 DataBrowserTableViewColumnID colId = 0;
580 err = GetDataBrowserTableViewColumnProperty(m_peer->GetControlRef(), 0, &colId);
581 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserTableViewColumnProperty"));
583 // OK, first we need to find the first visible item we have -
584 // this will be the "low" for our binary search. There is no real
585 // easy way around this, as we will need to do a SLOW linear search
586 // until we find a visible item, but we can do a cheap calculation
587 // via the row height to speed things up a bit
588 UInt32 scrollx, scrolly;
589 err = GetDataBrowserScrollPosition(m_peer->GetControlRef(), &scrollx, &scrolly);
590 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserScrollPosition"));
593 err = GetDataBrowserTableViewRowHeight(m_peer->GetControlRef(), &height);
594 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserTableViewRowHeight"));
596 // these indices are 0-based, as usual, so we need to add 1 to them when
597 // passing them to data browser functions which use 1-based indices
598 int low = scrolly / height,
599 high = GetCount() - 1;
601 // search for the first visible item (note that the scroll guess above
602 // is the low bounds of where the item might lie so we only use that as a
603 // starting point - we should reach it within 1 or 2 iterations of the loop)
604 while ( low <= high )
607 err = GetDataBrowserItemPartBounds(
608 m_peer->GetControlRef(), low + 1, colId,
609 kDataBrowserPropertyEnclosingPart,
610 &bounds); // note +1 to translate to Mac ID
614 // errDataBrowserItemNotFound is expected as it simply means that the
615 // item is not currently visible -- but other errors are not
616 wxCHECK_MSG( err == errDataBrowserItemNotFound, wxNOT_FOUND,
617 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
622 // NOW do a binary search for where the item lies, searching low again if
623 // we hit an item that isn't visible
624 while ( low <= high )
626 int mid = (low + high) / 2;
629 err = GetDataBrowserItemPartBounds(
630 m_peer->GetControlRef(), mid + 1, colId,
631 kDataBrowserPropertyEnclosingPart,
632 &bounds); //note +1 to trans to mac id
633 wxCHECK_MSG( err == noErr || err == errDataBrowserItemNotFound,
635 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
637 if ( err == errDataBrowserItemNotFound )
639 // item not visible, attempt to find a visible one
643 else // visible item, do actual hitttest
645 // if point is within the bounds, return this item (since we assume
646 // all x coords of items are equal we only test the x coord in
648 if ((point.x >= bounds.left && point.x <= bounds.right) &&
649 (point.y >= bounds.top && point.y <= bounds.bottom) )
655 if ( point.y < bounds.top )
656 // index(bounds) greater then key(point)
659 // index(bounds) less then key(point)
667 #endif // wxUSE_LISTBOX