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
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
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 controlAction(WXWidget slf, void* _cmd, void *sender);
147 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( (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
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 );
294 wxListWidgetCocoaImpl::wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data ) :
295 wxWidgetCocoaImpl( peer, view ), m_tableView(tableview), m_dataSource(data)
297 InstallEventHandler( tableview );
300 wxListWidgetCocoaImpl::~wxListWidgetCocoaImpl()
302 [m_dataSource release];
305 unsigned int wxListWidgetCocoaImpl::ListGetCount() const
307 wxListBox* lb = dynamic_cast<wxListBox*> ( GetWXPeer() );
308 return lb->GetCount();
315 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertTextColumn( unsigned pos, const wxString& WXUNUSED(title), bool editable,
316 wxAlignment WXUNUSED(just), int defaultWidth)
318 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
319 [col1 setEditable:editable];
321 unsigned formerColCount = [m_tableView numberOfColumns];
323 // there's apparently no way to insert at a specific position
324 [m_tableView addTableColumn:col1 ];
325 if ( pos < formerColCount )
326 [m_tableView moveColumn:formerColCount toColumn:pos];
328 if ( defaultWidth >= 0 )
330 [col1 setMaxWidth:defaultWidth];
331 [col1 setMinWidth:defaultWidth];
332 [col1 setWidth:defaultWidth];
336 [col1 setMaxWidth:1000];
337 [col1 setMinWidth:10];
338 // temporary hack, because I cannot get the automatic column resizing
340 [col1 setWidth:1000];
342 [col1 setResizingMask: NSTableColumnAutoresizingMask];
343 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
344 [col1 setColumn:wxcol];
346 // owned by the tableview
351 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertCheckColumn( unsigned pos , const wxString& WXUNUSED(title), bool editable,
352 wxAlignment WXUNUSED(just), int defaultWidth )
354 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
355 [col1 setEditable:editable];
357 // set your custom cell & set it up
358 NSButtonCell* checkbox = [[NSButtonCell alloc] init];
359 [checkbox setTitle:@""];
360 [checkbox setButtonType:NSSwitchButton];
361 [col1 setDataCell:checkbox] ;
364 unsigned formerColCount = [m_tableView numberOfColumns];
366 // there's apparently no way to insert at a specific position
367 [m_tableView addTableColumn:col1 ];
368 if ( pos < formerColCount )
369 [m_tableView moveColumn:formerColCount toColumn:pos];
371 if ( defaultWidth >= 0 )
373 [col1 setMaxWidth:defaultWidth];
374 [col1 setMinWidth:defaultWidth];
375 [col1 setWidth:defaultWidth];
378 [col1 setResizingMask: NSTableColumnNoResizing];
379 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
380 [col1 setColumn:wxcol];
382 // owned by the tableview
389 // inserting / removing lines
392 void wxListWidgetCocoaImpl::ListInsert( unsigned int WXUNUSED(n) )
394 [m_tableView reloadData];
397 void wxListWidgetCocoaImpl::ListDelete( unsigned int WXUNUSED(n) )
399 [m_tableView reloadData];
402 void wxListWidgetCocoaImpl::ListClear()
404 [m_tableView reloadData];
409 void wxListWidgetCocoaImpl::ListDeselectAll()
411 [m_tableView deselectAll:nil];
414 void wxListWidgetCocoaImpl::ListSetSelection( unsigned int n, bool select, bool multi )
418 [m_tableView selectRow: n byExtendingSelection:multi];
420 [m_tableView deselectRow: n];
424 int wxListWidgetCocoaImpl::ListGetSelection() const
426 return [m_tableView selectedRow];
429 int wxListWidgetCocoaImpl::ListGetSelections( wxArrayInt& aSelections ) const
433 int count = ListGetCount();
435 for ( int i = 0; i < count; ++i)
437 if ([m_tableView isRowSelected:count])
441 return aSelections.Count();
444 bool wxListWidgetCocoaImpl::ListIsSelected( unsigned int n ) const
446 return [m_tableView isRowSelected:n];
451 void wxListWidgetCocoaImpl::ListScrollTo( unsigned int n )
453 [m_tableView scrollRowToVisible:n];
457 void wxListWidgetCocoaImpl::UpdateLine( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) )
460 [m_tableView reloadData];
463 void wxListWidgetCocoaImpl::UpdateLineToEnd( unsigned int WXUNUSED(n))
466 [m_tableView reloadData];
469 void wxListWidgetCocoaImpl::controlAction(WXWidget WXUNUSED(slf),void* WXUNUSED(_cmd), void *WXUNUSED(sender))
471 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
472 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
474 wxCommandEvent event( wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() );
476 int sel = [m_tableView clickedRow];
477 if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?)
480 list->HandleLineEvent( sel, false );
483 void wxListWidgetCocoaImpl::controlDoubleAction(WXWidget WXUNUSED(slf),void* WXUNUSED(_cmd), void *WXUNUSED(sender))
485 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
486 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
488 int sel = [m_tableView clickedRow];
489 if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?)
492 list->HandleLineEvent( sel, true );
498 wxWidgetImplType* wxWidgetImpl::CreateListBox( wxWindowMac* wxpeer,
499 wxWindowMac* WXUNUSED(parent),
500 wxWindowID WXUNUSED(id),
504 long WXUNUSED(extraStyle))
506 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
507 NSScrollView* scrollview = [[NSScrollView alloc] initWithFrame:r];
509 // use same scroll flags logic as msw
511 [scrollview setHasVerticalScroller:YES];
513 if ( style & wxLB_HSCROLL )
514 [scrollview setHasHorizontalScroller:YES];
516 [scrollview setAutohidesScrollers: ((style & wxLB_ALWAYS_SB) ? NO : YES)];
518 // setting up the true table
520 wxNSTableView* tableview = [[wxNSTableView alloc] init];
521 // only one multi-select mode available
522 if ( (style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE) )
523 [tableview setAllowsMultipleSelection:YES];
525 // simple listboxes have no header row
526 [tableview setHeaderView:nil];
528 if ( style & wxLB_HSCROLL )
529 [tableview setColumnAutoresizingStyle:NSTableViewNoColumnAutoresizing];
531 [tableview setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle];
533 wxNSTableDataSource* ds = [[ wxNSTableDataSource alloc] init];
534 [tableview setDataSource:ds];
535 [scrollview setDocumentView:tableview];
538 wxListWidgetCocoaImpl* c = new wxListWidgetCocoaImpl( wxpeer, scrollview, tableview, ds );
540 // temporary hook for dnd
541 [tableview registerForDraggedTypes:[NSArray arrayWithObjects:
542 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
544 [ds setImplementation:c];
548 int wxListBox::DoListHitTest(const wxPoint& WXUNUSED(inpoint)) const
553 // There are few reasons why this is complicated:
554 // 1) There is no native HitTest function for Mac
555 // 2) GetDataBrowserItemPartBounds only works on visible items
556 // 3) We can't do it through GetDataBrowserTableView[Item]RowHeight
557 // because what it returns is basically inaccurate in the context
558 // of the coordinates we want here, but we use this as a guess
559 // for where the first visible item lies
561 wxPoint point = inpoint;
563 // get column property ID (req. for call to itempartbounds)
564 DataBrowserTableViewColumnID colId = 0;
565 err = GetDataBrowserTableViewColumnProperty(m_peer->GetControlRef(), 0, &colId);
566 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserTableViewColumnProperty"));
568 // OK, first we need to find the first visible item we have -
569 // this will be the "low" for our binary search. There is no real
570 // easy way around this, as we will need to do a SLOW linear search
571 // until we find a visible item, but we can do a cheap calculation
572 // via the row height to speed things up a bit
573 UInt32 scrollx, scrolly;
574 err = GetDataBrowserScrollPosition(m_peer->GetControlRef(), &scrollx, &scrolly);
575 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserScrollPosition"));
578 err = GetDataBrowserTableViewRowHeight(m_peer->GetControlRef(), &height);
579 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserTableViewRowHeight"));
581 // these indices are 0-based, as usual, so we need to add 1 to them when
582 // passing them to data browser functions which use 1-based indices
583 int low = scrolly / height,
584 high = GetCount() - 1;
586 // search for the first visible item (note that the scroll guess above
587 // is the low bounds of where the item might lie so we only use that as a
588 // starting point - we should reach it within 1 or 2 iterations of the loop)
589 while ( low <= high )
592 err = GetDataBrowserItemPartBounds(
593 m_peer->GetControlRef(), low + 1, colId,
594 kDataBrowserPropertyEnclosingPart,
595 &bounds); // note +1 to translate to Mac ID
599 // errDataBrowserItemNotFound is expected as it simply means that the
600 // item is not currently visible -- but other errors are not
601 wxCHECK_MSG( err == errDataBrowserItemNotFound, wxNOT_FOUND,
602 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
607 // NOW do a binary search for where the item lies, searching low again if
608 // we hit an item that isn't visible
609 while ( low <= high )
611 int mid = (low + high) / 2;
614 err = GetDataBrowserItemPartBounds(
615 m_peer->GetControlRef(), mid + 1, colId,
616 kDataBrowserPropertyEnclosingPart,
617 &bounds); //note +1 to trans to mac id
618 wxCHECK_MSG( err == noErr || err == errDataBrowserItemNotFound,
620 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
622 if ( err == errDataBrowserItemNotFound )
624 // item not visible, attempt to find a visible one
628 else // visible item, do actual hitttest
630 // if point is within the bounds, return this item (since we assume
631 // all x coords of items are equal we only test the x coord in
633 if ((point.x >= bounds.left && point.x <= bounds.right) &&
634 (point.y >= bounds.top && point.y <= bounds.bottom) )
640 if ( point.y < bounds.top )
641 // index(bounds) greater then key(point)
644 // index(bounds) less then key(point)
652 #endif // wxUSE_LISTBOX