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"
23 #include "wx/settings.h"
24 #include "wx/arrstr.h"
25 #include "wx/dcclient.h"
28 #include "wx/osx/private.h"
34 class wxListWidgetCocoaImpl;
36 @interface wxNSTableDataSource : NSObject wxOSX_10_6_AND_LATER(<NSTableViewDataSource>)
38 wxListWidgetCocoaImpl* impl;
41 - (id)tableView:(NSTableView *)aTableView
42 objectValueForTableColumn:(NSTableColumn *)aTableColumn
43 row:(NSInteger)rowIndex;
45 - (void)tableView:(NSTableView *)aTableView
46 setObjectValue:(id)value forTableColumn:(NSTableColumn *)aTableColumn
47 row:(NSInteger)rowIndex;
49 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView;
51 - (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation;
52 - (wxListWidgetCocoaImpl*) implementation;
56 @interface wxNSTableView : NSTableView wxOSX_10_6_AND_LATER(<NSTableViewDelegate>)
66 class wxCocoaTableColumn;
68 @interface wxNSTableColumn : NSTableColumn
70 wxCocoaTableColumn* column;
73 - (void) setColumn: (wxCocoaTableColumn*) col;
75 - (wxCocoaTableColumn*) column;
79 class WXDLLIMPEXP_CORE wxCocoaTableColumn : public wxListWidgetColumn
82 wxCocoaTableColumn( wxNSTableColumn* column, bool editable )
83 : m_column( column ), m_editable(editable)
91 wxNSTableColumn* GetNSTableColumn() const { return m_column ; }
93 bool IsEditable() const { return m_editable; }
96 wxNSTableColumn* m_column;
100 NSString* column1 = @"1";
102 class wxListWidgetCocoaImpl : public wxWidgetCocoaImpl, public wxListWidgetImpl
105 wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data );
107 ~wxListWidgetCocoaImpl();
109 virtual wxListWidgetColumn* InsertTextColumn( unsigned pos, const wxString& title, bool editable = false,
110 wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ;
111 virtual wxListWidgetColumn* InsertCheckColumn( unsigned pos , const wxString& title, bool editable = false,
112 wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ;
116 virtual void ListDelete( unsigned int n ) ;
117 virtual void ListInsert( unsigned int n ) ;
118 virtual void ListClear() ;
122 virtual void ListDeselectAll();
124 virtual void ListSetSelection( unsigned int n, bool select, bool multi ) ;
125 virtual int ListGetSelection() const ;
127 virtual int ListGetSelections( wxArrayInt& aSelections ) const ;
129 virtual bool ListIsSelected( unsigned int n ) const ;
133 virtual void ListScrollTo( unsigned int n ) ;
137 virtual unsigned int ListGetCount() const ;
139 int ListGetColumnType( int col )
143 virtual void UpdateLine( unsigned int n, wxListWidgetColumn* col = NULL ) ;
144 virtual void UpdateLineToEnd( unsigned int n);
146 virtual void controlDoubleAction(WXWidget slf, void* _cmd, void *sender);
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::AsString( (NSString*) value );
214 return wxEmptyString;
221 @implementation wxNSTableDataSource
230 - (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation
232 impl = theImplementation;
235 - (wxListWidgetCocoaImpl*) implementation
240 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
242 wxUnusedVar(aTableView);
244 return impl->ListGetCount();
248 - (id)tableView:(NSTableView *)aTableView
249 objectValueForTableColumn:(NSTableColumn *)aTableColumn
250 row:(NSInteger)rowIndex
252 wxUnusedVar(aTableView);
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 wxUnusedVar(aTableView);
267 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
268 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
269 wxCocoaTableColumn* col = [tablecol column];
270 wxNSTableViewCellValue cellvalue(value);
271 lb->SetValueCallback(rowIndex, col, cellvalue);
276 @implementation wxNSTableView
280 static BOOL initialized = NO;
284 wxOSXCocoaClassAddWXMethods( self );
288 - (void) tableViewSelectionDidChange: (NSNotification *) notification
290 wxUnusedVar(notification);
292 int row = [self selectedRow];
300 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
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 if ((row < 0) || (row > (int) list->GetCount())) // OS X can select an item below the last item
309 if ( !list->MacGetBlockEvents() )
310 list->HandleLineEvent( row, false );
321 wxListWidgetCocoaImpl::wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data ) :
322 wxWidgetCocoaImpl( peer, view ), m_tableView(tableview), m_dataSource(data)
324 InstallEventHandler( tableview );
327 wxListWidgetCocoaImpl::~wxListWidgetCocoaImpl()
329 [m_dataSource release];
332 unsigned int wxListWidgetCocoaImpl::ListGetCount() const
334 wxListBox* lb = dynamic_cast<wxListBox*> ( GetWXPeer() );
335 return lb->GetCount();
342 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertTextColumn( unsigned pos, const wxString& WXUNUSED(title), bool editable,
343 wxAlignment WXUNUSED(just), int defaultWidth)
345 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
346 [col1 setEditable:editable];
348 unsigned formerColCount = [m_tableView numberOfColumns];
350 // there's apparently no way to insert at a specific position
351 [m_tableView addTableColumn:col1 ];
352 if ( pos < formerColCount )
353 [m_tableView moveColumn:formerColCount toColumn:pos];
355 if ( defaultWidth >= 0 )
357 [col1 setMaxWidth:defaultWidth];
358 [col1 setMinWidth:defaultWidth];
359 [col1 setWidth:defaultWidth];
363 [col1 setMaxWidth:1000];
364 [col1 setMinWidth:10];
365 // temporary hack, because I cannot get the automatic column resizing
367 [col1 setWidth:1000];
369 [col1 setResizingMask: NSTableColumnAutoresizingMask];
370 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
371 [col1 setColumn:wxcol];
373 // owned by the tableview
378 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertCheckColumn( unsigned pos , const wxString& WXUNUSED(title), bool editable,
379 wxAlignment WXUNUSED(just), int defaultWidth )
381 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
382 [col1 setEditable:editable];
384 // set your custom cell & set it up
385 NSButtonCell* checkbox = [[NSButtonCell alloc] init];
386 [checkbox setTitle:@""];
387 [checkbox setButtonType:NSSwitchButton];
388 [col1 setDataCell:checkbox] ;
391 unsigned formerColCount = [m_tableView numberOfColumns];
393 // there's apparently no way to insert at a specific position
394 [m_tableView addTableColumn:col1 ];
395 if ( pos < formerColCount )
396 [m_tableView moveColumn:formerColCount toColumn:pos];
398 if ( defaultWidth >= 0 )
400 [col1 setMaxWidth:defaultWidth];
401 [col1 setMinWidth:defaultWidth];
402 [col1 setWidth:defaultWidth];
405 [col1 setResizingMask: NSTableColumnNoResizing];
406 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
407 [col1 setColumn:wxcol];
409 // owned by the tableview
416 // inserting / removing lines
419 void wxListWidgetCocoaImpl::ListInsert( unsigned int WXUNUSED(n) )
421 [m_tableView reloadData];
424 void wxListWidgetCocoaImpl::ListDelete( unsigned int WXUNUSED(n) )
426 [m_tableView reloadData];
429 void wxListWidgetCocoaImpl::ListClear()
431 [m_tableView reloadData];
436 void wxListWidgetCocoaImpl::ListDeselectAll()
438 [m_tableView deselectAll:nil];
441 void wxListWidgetCocoaImpl::ListSetSelection( unsigned int n, bool select, bool multi )
445 [m_tableView selectRow: n byExtendingSelection:multi];
447 [m_tableView deselectRow: n];
451 int wxListWidgetCocoaImpl::ListGetSelection() const
453 return [m_tableView selectedRow];
456 int wxListWidgetCocoaImpl::ListGetSelections( wxArrayInt& aSelections ) const
460 int count = ListGetCount();
462 for ( int i = 0; i < count; ++i)
464 if ([m_tableView isRowSelected:i])
468 return aSelections.Count();
471 bool wxListWidgetCocoaImpl::ListIsSelected( unsigned int n ) const
473 return [m_tableView isRowSelected:n];
478 void wxListWidgetCocoaImpl::ListScrollTo( unsigned int n )
480 [m_tableView scrollRowToVisible:n];
484 void wxListWidgetCocoaImpl::UpdateLine( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) )
487 [m_tableView reloadData];
490 void wxListWidgetCocoaImpl::UpdateLineToEnd( unsigned int WXUNUSED(n))
493 [m_tableView reloadData];
496 void wxListWidgetCocoaImpl::controlDoubleAction(WXWidget WXUNUSED(slf),void* WXUNUSED(_cmd), void *WXUNUSED(sender))
498 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
499 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
501 int sel = [m_tableView clickedRow];
502 if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?)
505 list->HandleLineEvent( sel, true );
511 wxWidgetImplType* wxWidgetImpl::CreateListBox( wxWindowMac* wxpeer,
512 wxWindowMac* WXUNUSED(parent),
513 wxWindowID WXUNUSED(id),
517 long WXUNUSED(extraStyle))
519 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
520 NSScrollView* scrollview = [[NSScrollView alloc] initWithFrame:r];
522 // use same scroll flags logic as msw
524 [scrollview setHasVerticalScroller:YES];
526 if ( style & wxLB_HSCROLL )
527 [scrollview setHasHorizontalScroller:YES];
529 [scrollview setAutohidesScrollers: ((style & wxLB_ALWAYS_SB) ? NO : YES)];
531 // setting up the true table
533 wxNSTableView* tableview = [[wxNSTableView alloc] init];
534 [tableview setDelegate:tableview];
535 // only one multi-select mode available
536 if ( (style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE) )
537 [tableview setAllowsMultipleSelection:YES];
539 // simple listboxes have no header row
540 [tableview setHeaderView:nil];
542 if ( style & wxLB_HSCROLL )
543 [tableview setColumnAutoresizingStyle:NSTableViewNoColumnAutoresizing];
545 [tableview setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle];
547 wxNSTableDataSource* ds = [[ wxNSTableDataSource alloc] init];
548 [tableview setDataSource:ds];
549 [scrollview setDocumentView:tableview];
552 wxListWidgetCocoaImpl* c = new wxListWidgetCocoaImpl( wxpeer, scrollview, tableview, ds );
554 // temporary hook for dnd
555 [tableview registerForDraggedTypes:[NSArray arrayWithObjects:
556 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
558 [ds setImplementation:c];
562 int wxListBox::DoListHitTest(const wxPoint& WXUNUSED(inpoint)) const
567 // There are few reasons why this is complicated:
568 // 1) There is no native HitTest function for Mac
569 // 2) GetDataBrowserItemPartBounds only works on visible items
570 // 3) We can't do it through GetDataBrowserTableView[Item]RowHeight
571 // because what it returns is basically inaccurate in the context
572 // of the coordinates we want here, but we use this as a guess
573 // for where the first visible item lies
575 wxPoint point = inpoint;
577 // get column property ID (req. for call to itempartbounds)
578 DataBrowserTableViewColumnID colId = 0;
579 err = GetDataBrowserTableViewColumnProperty(m_peer->GetControlRef(), 0, &colId);
580 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserTableViewColumnProperty"));
582 // OK, first we need to find the first visible item we have -
583 // this will be the "low" for our binary search. There is no real
584 // easy way around this, as we will need to do a SLOW linear search
585 // until we find a visible item, but we can do a cheap calculation
586 // via the row height to speed things up a bit
587 UInt32 scrollx, scrolly;
588 err = GetDataBrowserScrollPosition(m_peer->GetControlRef(), &scrollx, &scrolly);
589 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserScrollPosition"));
592 err = GetDataBrowserTableViewRowHeight(m_peer->GetControlRef(), &height);
593 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserTableViewRowHeight"));
595 // these indices are 0-based, as usual, so we need to add 1 to them when
596 // passing them to data browser functions which use 1-based indices
597 int low = scrolly / height,
598 high = GetCount() - 1;
600 // search for the first visible item (note that the scroll guess above
601 // is the low bounds of where the item might lie so we only use that as a
602 // starting point - we should reach it within 1 or 2 iterations of the loop)
603 while ( low <= high )
606 err = GetDataBrowserItemPartBounds(
607 m_peer->GetControlRef(), low + 1, colId,
608 kDataBrowserPropertyEnclosingPart,
609 &bounds); // note +1 to translate to Mac ID
613 // errDataBrowserItemNotFound is expected as it simply means that the
614 // item is not currently visible -- but other errors are not
615 wxCHECK_MSG( err == errDataBrowserItemNotFound, wxNOT_FOUND,
616 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
621 // NOW do a binary search for where the item lies, searching low again if
622 // we hit an item that isn't visible
623 while ( low <= high )
625 int mid = (low + high) / 2;
628 err = GetDataBrowserItemPartBounds(
629 m_peer->GetControlRef(), mid + 1, colId,
630 kDataBrowserPropertyEnclosingPart,
631 &bounds); //note +1 to trans to mac id
632 wxCHECK_MSG( err == noErr || err == errDataBrowserItemNotFound,
634 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
636 if ( err == errDataBrowserItemNotFound )
638 // item not visible, attempt to find a visible one
642 else // visible item, do actual hitttest
644 // if point is within the bounds, return this item (since we assume
645 // all x coords of items are equal we only test the x coord in
647 if ((point.x >= bounds.left && point.x <= bounds.right) &&
648 (point.y >= bounds.top && point.y <= bounds.bottom) )
654 if ( point.y < bounds.top )
655 // index(bounds) greater then key(point)
658 // index(bounds) less then key(point)
666 #endif // wxUSE_LISTBOX