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
37 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
38 <NSTableViewDataSource>
41 wxListWidgetCocoaImpl* impl;
44 - (id)tableView:(NSTableView *)aTableView
45 objectValueForTableColumn:(NSTableColumn *)aTableColumn
46 row:(NSInteger)rowIndex;
48 - (void)tableView:(NSTableView *)aTableView
49 setObjectValue:(id)value forTableColumn:(NSTableColumn *)aTableColumn
50 row:(NSInteger)rowIndex;
52 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView;
54 - (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation;
55 - (wxListWidgetCocoaImpl*) implementation;
59 @interface wxNSTableView : NSTableView
60 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
72 class wxCocoaTableColumn;
74 @interface wxNSTableColumn : NSTableColumn
76 wxCocoaTableColumn* column;
79 - (void) setColumn: (wxCocoaTableColumn*) col;
81 - (wxCocoaTableColumn*) column;
85 class WXDLLIMPEXP_CORE wxCocoaTableColumn : public wxListWidgetColumn
88 wxCocoaTableColumn( wxNSTableColumn* column, bool editable )
89 : m_column( column ), m_editable(editable)
97 wxNSTableColumn* GetNSTableColumn() const { return m_column ; }
99 bool IsEditable() const { return m_editable; }
102 wxNSTableColumn* m_column;
106 NSString* column1 = @"1";
108 class wxListWidgetCocoaImpl : public wxWidgetCocoaImpl, public wxListWidgetImpl
111 wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data );
113 ~wxListWidgetCocoaImpl();
115 virtual wxListWidgetColumn* InsertTextColumn( unsigned pos, const wxString& title, bool editable = false,
116 wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ;
117 virtual wxListWidgetColumn* InsertCheckColumn( unsigned pos , const wxString& title, bool editable = false,
118 wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ;
122 virtual void ListDelete( unsigned int n ) ;
123 virtual void ListInsert( unsigned int n ) ;
124 virtual void ListClear() ;
128 virtual void ListDeselectAll();
130 virtual void ListSetSelection( unsigned int n, bool select, bool multi ) ;
131 virtual int ListGetSelection() const ;
133 virtual int ListGetSelections( wxArrayInt& aSelections ) const ;
135 virtual bool ListIsSelected( unsigned int n ) const ;
139 virtual void ListScrollTo( unsigned int n ) ;
143 virtual unsigned int ListGetCount() const ;
145 int ListGetColumnType( int col )
149 virtual void UpdateLine( unsigned int n, wxListWidgetColumn* col = NULL ) ;
150 virtual void UpdateLineToEnd( unsigned int n);
152 virtual void controlDoubleAction(WXWidget slf, void* _cmd, void *sender);
155 wxNSTableView* m_tableView ;
157 wxNSTableDataSource* m_dataSource;
164 @implementation wxNSTableColumn
173 - (void) setColumn: (wxCocoaTableColumn*) col
178 - (wxCocoaTableColumn*) column
185 class wxNSTableViewCellValue : public wxListWidgetCellValue
188 wxNSTableViewCellValue( id &v ) : value(v)
192 virtual ~wxNSTableViewCellValue() {}
194 virtual void Set( CFStringRef v )
196 value = [[(NSString*)v retain] autorelease];
198 virtual void Set( const wxString& value )
200 Set( (CFStringRef) wxCFStringRef( value ) );
202 virtual void Set( int v )
204 value = [NSNumber numberWithInt:v];
207 virtual int GetIntValue() const
209 if ( [value isKindOfClass:[NSNumber class]] )
210 return [ (NSNumber*) value intValue ];
215 virtual wxString GetStringValue() const
217 if ( [value isKindOfClass:[NSString class]] )
218 return wxCFStringRef::AsString( (NSString*) value );
220 return wxEmptyString;
227 @implementation wxNSTableDataSource
236 - (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation
238 impl = theImplementation;
241 - (wxListWidgetCocoaImpl*) implementation
246 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
248 wxUnusedVar(aTableView);
250 return impl->ListGetCount();
254 - (id)tableView:(NSTableView *)aTableView
255 objectValueForTableColumn:(NSTableColumn *)aTableColumn
256 row:(NSInteger)rowIndex
258 wxUnusedVar(aTableView);
259 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
260 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
261 wxCocoaTableColumn* col = [tablecol column];
263 wxNSTableViewCellValue cellvalue(value);
264 lb->GetValueCallback(rowIndex, col, cellvalue);
268 - (void)tableView:(NSTableView *)aTableView
269 setObjectValue:(id)value forTableColumn:(NSTableColumn *)aTableColumn
270 row:(NSInteger)rowIndex
272 wxUnusedVar(aTableView);
273 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
274 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
275 wxCocoaTableColumn* col = [tablecol column];
276 wxNSTableViewCellValue cellvalue(value);
277 lb->SetValueCallback(rowIndex, col, cellvalue);
282 @implementation wxNSTableView
286 static BOOL initialized = NO;
290 wxOSXCocoaClassAddWXMethods( self );
294 - (void) tableViewSelectionDidChange: (NSNotification *) notification
296 wxUnusedVar(notification);
298 int row = [self selectedRow];
306 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
307 wxListBox *list = static_cast<wxListBox*> ( impl->GetWXPeer());
308 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
310 wxCommandEvent event( wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() );
312 if ((row < 0) || (row > (int) list->GetCount())) // OS X can select an item below the last item
315 if ( !list->MacGetBlockEvents() )
316 list->HandleLineEvent( row, false );
327 wxListWidgetCocoaImpl::wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data ) :
328 wxWidgetCocoaImpl( peer, view ), m_tableView(tableview), m_dataSource(data)
330 InstallEventHandler( tableview );
333 wxListWidgetCocoaImpl::~wxListWidgetCocoaImpl()
335 [m_dataSource release];
338 unsigned int wxListWidgetCocoaImpl::ListGetCount() const
340 wxListBox* lb = dynamic_cast<wxListBox*> ( GetWXPeer() );
341 return lb->GetCount();
348 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertTextColumn( unsigned pos, const wxString& WXUNUSED(title), bool editable,
349 wxAlignment WXUNUSED(just), int defaultWidth)
351 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
352 [col1 setEditable:editable];
354 unsigned formerColCount = [m_tableView numberOfColumns];
356 // there's apparently no way to insert at a specific position
357 [m_tableView addTableColumn:col1 ];
358 if ( pos < formerColCount )
359 [m_tableView moveColumn:formerColCount toColumn:pos];
361 if ( defaultWidth >= 0 )
363 [col1 setMaxWidth:defaultWidth];
364 [col1 setMinWidth:defaultWidth];
365 [col1 setWidth:defaultWidth];
369 [col1 setMaxWidth:1000];
370 [col1 setMinWidth:10];
371 // temporary hack, because I cannot get the automatic column resizing
373 [col1 setWidth:1000];
375 [col1 setResizingMask: NSTableColumnAutoresizingMask];
376 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
377 [col1 setColumn:wxcol];
379 // owned by the tableview
384 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertCheckColumn( unsigned pos , const wxString& WXUNUSED(title), bool editable,
385 wxAlignment WXUNUSED(just), int defaultWidth )
387 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
388 [col1 setEditable:editable];
390 // set your custom cell & set it up
391 NSButtonCell* checkbox = [[NSButtonCell alloc] init];
392 [checkbox setTitle:@""];
393 [checkbox setButtonType:NSSwitchButton];
394 [col1 setDataCell:checkbox] ;
397 unsigned formerColCount = [m_tableView numberOfColumns];
399 // there's apparently no way to insert at a specific position
400 [m_tableView addTableColumn:col1 ];
401 if ( pos < formerColCount )
402 [m_tableView moveColumn:formerColCount toColumn:pos];
404 if ( defaultWidth >= 0 )
406 [col1 setMaxWidth:defaultWidth];
407 [col1 setMinWidth:defaultWidth];
408 [col1 setWidth:defaultWidth];
411 [col1 setResizingMask: NSTableColumnNoResizing];
412 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
413 [col1 setColumn:wxcol];
415 // owned by the tableview
422 // inserting / removing lines
425 void wxListWidgetCocoaImpl::ListInsert( unsigned int WXUNUSED(n) )
427 [m_tableView reloadData];
430 void wxListWidgetCocoaImpl::ListDelete( unsigned int WXUNUSED(n) )
432 [m_tableView reloadData];
435 void wxListWidgetCocoaImpl::ListClear()
437 [m_tableView reloadData];
442 void wxListWidgetCocoaImpl::ListDeselectAll()
444 [m_tableView deselectAll:nil];
447 void wxListWidgetCocoaImpl::ListSetSelection( unsigned int n, bool select, bool multi )
451 [m_tableView selectRow: n byExtendingSelection:multi];
453 [m_tableView deselectRow: n];
457 int wxListWidgetCocoaImpl::ListGetSelection() const
459 return [m_tableView selectedRow];
462 int wxListWidgetCocoaImpl::ListGetSelections( wxArrayInt& aSelections ) const
466 int count = ListGetCount();
468 for ( int i = 0; i < count; ++i)
470 if ([m_tableView isRowSelected:count])
474 return aSelections.Count();
477 bool wxListWidgetCocoaImpl::ListIsSelected( unsigned int n ) const
479 return [m_tableView isRowSelected:n];
484 void wxListWidgetCocoaImpl::ListScrollTo( unsigned int n )
486 [m_tableView scrollRowToVisible:n];
490 void wxListWidgetCocoaImpl::UpdateLine( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) )
493 [m_tableView reloadData];
496 void wxListWidgetCocoaImpl::UpdateLineToEnd( unsigned int WXUNUSED(n))
499 [m_tableView reloadData];
502 void wxListWidgetCocoaImpl::controlDoubleAction(WXWidget WXUNUSED(slf),void* WXUNUSED(_cmd), void *WXUNUSED(sender))
504 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
505 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
507 int sel = [m_tableView clickedRow];
508 if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?)
511 list->HandleLineEvent( sel, true );
517 wxWidgetImplType* wxWidgetImpl::CreateListBox( wxWindowMac* wxpeer,
518 wxWindowMac* WXUNUSED(parent),
519 wxWindowID WXUNUSED(id),
523 long WXUNUSED(extraStyle))
525 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
526 NSScrollView* scrollview = [[NSScrollView alloc] initWithFrame:r];
528 // use same scroll flags logic as msw
530 [scrollview setHasVerticalScroller:YES];
532 if ( style & wxLB_HSCROLL )
533 [scrollview setHasHorizontalScroller:YES];
535 [scrollview setAutohidesScrollers: ((style & wxLB_ALWAYS_SB) ? NO : YES)];
537 // setting up the true table
539 wxNSTableView* tableview = [[wxNSTableView alloc] init];
540 [tableview setDelegate:tableview];
541 // only one multi-select mode available
542 if ( (style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE) )
543 [tableview setAllowsMultipleSelection:YES];
545 // simple listboxes have no header row
546 [tableview setHeaderView:nil];
548 if ( style & wxLB_HSCROLL )
549 [tableview setColumnAutoresizingStyle:NSTableViewNoColumnAutoresizing];
551 [tableview setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle];
553 wxNSTableDataSource* ds = [[ wxNSTableDataSource alloc] init];
554 [tableview setDataSource:ds];
555 [scrollview setDocumentView:tableview];
558 wxListWidgetCocoaImpl* c = new wxListWidgetCocoaImpl( wxpeer, scrollview, tableview, ds );
560 // temporary hook for dnd
561 [tableview registerForDraggedTypes:[NSArray arrayWithObjects:
562 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
564 [ds setImplementation:c];
568 int wxListBox::DoListHitTest(const wxPoint& WXUNUSED(inpoint)) const
573 // There are few reasons why this is complicated:
574 // 1) There is no native HitTest function for Mac
575 // 2) GetDataBrowserItemPartBounds only works on visible items
576 // 3) We can't do it through GetDataBrowserTableView[Item]RowHeight
577 // because what it returns is basically inaccurate in the context
578 // of the coordinates we want here, but we use this as a guess
579 // for where the first visible item lies
581 wxPoint point = inpoint;
583 // get column property ID (req. for call to itempartbounds)
584 DataBrowserTableViewColumnID colId = 0;
585 err = GetDataBrowserTableViewColumnProperty(m_peer->GetControlRef(), 0, &colId);
586 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserTableViewColumnProperty"));
588 // OK, first we need to find the first visible item we have -
589 // this will be the "low" for our binary search. There is no real
590 // easy way around this, as we will need to do a SLOW linear search
591 // until we find a visible item, but we can do a cheap calculation
592 // via the row height to speed things up a bit
593 UInt32 scrollx, scrolly;
594 err = GetDataBrowserScrollPosition(m_peer->GetControlRef(), &scrollx, &scrolly);
595 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserScrollPosition"));
598 err = GetDataBrowserTableViewRowHeight(m_peer->GetControlRef(), &height);
599 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserTableViewRowHeight"));
601 // these indices are 0-based, as usual, so we need to add 1 to them when
602 // passing them to data browser functions which use 1-based indices
603 int low = scrolly / height,
604 high = GetCount() - 1;
606 // search for the first visible item (note that the scroll guess above
607 // is the low bounds of where the item might lie so we only use that as a
608 // starting point - we should reach it within 1 or 2 iterations of the loop)
609 while ( low <= high )
612 err = GetDataBrowserItemPartBounds(
613 m_peer->GetControlRef(), low + 1, colId,
614 kDataBrowserPropertyEnclosingPart,
615 &bounds); // note +1 to translate to Mac ID
619 // errDataBrowserItemNotFound is expected as it simply means that the
620 // item is not currently visible -- but other errors are not
621 wxCHECK_MSG( err == errDataBrowserItemNotFound, wxNOT_FOUND,
622 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
627 // NOW do a binary search for where the item lies, searching low again if
628 // we hit an item that isn't visible
629 while ( low <= high )
631 int mid = (low + high) / 2;
634 err = GetDataBrowserItemPartBounds(
635 m_peer->GetControlRef(), mid + 1, colId,
636 kDataBrowserPropertyEnclosingPart,
637 &bounds); //note +1 to trans to mac id
638 wxCHECK_MSG( err == noErr || err == errDataBrowserItemNotFound,
640 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
642 if ( err == errDataBrowserItemNotFound )
644 // item not visible, attempt to find a visible one
648 else // visible item, do actual hitttest
650 // if point is within the bounds, return this item (since we assume
651 // all x coords of items are equal we only test the x coord in
653 if ((point.x >= bounds.left && point.x <= bounds.right) &&
654 (point.y >= bounds.top && point.y <= bounds.bottom) )
660 if ( point.y < bounds.top )
661 // index(bounds) greater then key(point)
664 // index(bounds) less then key(point)
672 #endif // wxUSE_LISTBOX