1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/listbox.mm
4 // Author: Stefan Csomor
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 ;
138 virtual int DoListHitTest( const wxPoint& inpoint ) const;
140 int ListGetColumnType( int col )
144 virtual void UpdateLine( unsigned int n, wxListWidgetColumn* col = NULL ) ;
145 virtual void UpdateLineToEnd( unsigned int n);
147 virtual void controlDoubleAction(WXWidget slf, void* _cmd, void *sender);
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::AsString( (NSString*) value );
216 return wxEmptyString;
223 @implementation wxNSTableDataSource
232 - (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation
234 impl = theImplementation;
237 - (wxListWidgetCocoaImpl*) implementation
242 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
244 wxUnusedVar(aTableView);
246 return impl->ListGetCount();
250 - (id)tableView:(NSTableView *)aTableView
251 objectValueForTableColumn:(NSTableColumn *)aTableColumn
252 row:(NSInteger)rowIndex
254 wxUnusedVar(aTableView);
255 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
256 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
257 wxCocoaTableColumn* col = [tablecol column];
259 wxNSTableViewCellValue cellvalue(value);
260 lb->GetValueCallback(rowIndex, col, cellvalue);
264 - (void)tableView:(NSTableView *)aTableView
265 setObjectValue:(id)value forTableColumn:(NSTableColumn *)aTableColumn
266 row:(NSInteger)rowIndex
268 wxUnusedVar(aTableView);
269 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
270 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
271 wxCocoaTableColumn* col = [tablecol column];
272 wxNSTableViewCellValue cellvalue(value);
273 lb->SetValueCallback(rowIndex, col, cellvalue);
278 @implementation wxNSTableView
282 static BOOL initialized = NO;
286 wxOSXCocoaClassAddWXMethods( self );
290 - (void) tableViewSelectionDidChange: (NSNotification *) notification
292 wxUnusedVar(notification);
294 int row = [self selectedRow];
302 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
303 wxListBox *list = static_cast<wxListBox*> ( impl->GetWXPeer());
304 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
306 wxCommandEvent event( wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() );
308 if ((row < 0) || (row > (int) list->GetCount())) // OS X can select an item below the last item
311 if ( !list->MacGetBlockEvents() )
312 list->HandleLineEvent( row, false );
323 wxListWidgetCocoaImpl::wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data ) :
324 wxWidgetCocoaImpl( peer, view ), m_tableView(tableview), m_dataSource(data)
326 InstallEventHandler( tableview );
329 wxListWidgetCocoaImpl::~wxListWidgetCocoaImpl()
331 [m_dataSource release];
334 unsigned int wxListWidgetCocoaImpl::ListGetCount() const
336 wxListBox* lb = dynamic_cast<wxListBox*> ( GetWXPeer() );
337 return lb->GetCount();
344 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertTextColumn( unsigned pos, const wxString& WXUNUSED(title), bool editable,
345 wxAlignment WXUNUSED(just), int defaultWidth)
347 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
348 [col1 setEditable:editable];
350 unsigned formerColCount = [m_tableView numberOfColumns];
352 // there's apparently no way to insert at a specific position
353 [m_tableView addTableColumn:col1 ];
354 if ( pos < formerColCount )
355 [m_tableView moveColumn:formerColCount toColumn:pos];
357 if ( defaultWidth >= 0 )
359 [col1 setMaxWidth:defaultWidth];
360 [col1 setMinWidth:defaultWidth];
361 [col1 setWidth:defaultWidth];
365 [col1 setMaxWidth:1000];
366 [col1 setMinWidth:10];
367 // temporary hack, because I cannot get the automatic column resizing
369 [col1 setWidth:1000];
371 [col1 setResizingMask: NSTableColumnAutoresizingMask];
372 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
373 [col1 setColumn:wxcol];
375 // owned by the tableview
380 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertCheckColumn( unsigned pos , const wxString& WXUNUSED(title), bool editable,
381 wxAlignment WXUNUSED(just), int defaultWidth )
383 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
384 [col1 setEditable:editable];
386 // set your custom cell & set it up
387 NSButtonCell* checkbox = [[NSButtonCell alloc] init];
388 [checkbox setTitle:@""];
389 [checkbox setButtonType:NSSwitchButton];
390 [col1 setDataCell:checkbox] ;
393 unsigned formerColCount = [m_tableView numberOfColumns];
395 // there's apparently no way to insert at a specific position
396 [m_tableView addTableColumn:col1 ];
397 if ( pos < formerColCount )
398 [m_tableView moveColumn:formerColCount toColumn:pos];
400 if ( defaultWidth >= 0 )
402 [col1 setMaxWidth:defaultWidth];
403 [col1 setMinWidth:defaultWidth];
404 [col1 setWidth:defaultWidth];
407 [col1 setResizingMask: NSTableColumnNoResizing];
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 WXUNUSED(n) )
423 [m_tableView reloadData];
426 void wxListWidgetCocoaImpl::ListDelete( unsigned int WXUNUSED(n) )
428 [m_tableView reloadData];
431 void wxListWidgetCocoaImpl::ListClear()
433 [m_tableView reloadData];
438 void wxListWidgetCocoaImpl::ListDeselectAll()
440 [m_tableView deselectAll:nil];
443 void wxListWidgetCocoaImpl::ListSetSelection( unsigned int n, bool select, bool multi )
447 [m_tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:n]
448 byExtendingSelection:multi];
450 [m_tableView deselectRow: n];
454 int wxListWidgetCocoaImpl::ListGetSelection() const
456 return [m_tableView selectedRow];
459 int wxListWidgetCocoaImpl::ListGetSelections( wxArrayInt& aSelections ) const
463 int count = ListGetCount();
465 for ( int i = 0; i < count; ++i)
467 if ([m_tableView isRowSelected:i])
471 return aSelections.Count();
474 bool wxListWidgetCocoaImpl::ListIsSelected( unsigned int n ) const
476 return [m_tableView isRowSelected:n];
481 void wxListWidgetCocoaImpl::ListScrollTo( unsigned int n )
483 [m_tableView scrollRowToVisible:n];
487 void wxListWidgetCocoaImpl::UpdateLine( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) )
490 [m_tableView reloadData];
493 void wxListWidgetCocoaImpl::UpdateLineToEnd( unsigned int WXUNUSED(n))
496 [m_tableView reloadData];
499 void wxListWidgetCocoaImpl::controlDoubleAction(WXWidget WXUNUSED(slf),void* WXUNUSED(_cmd), void *WXUNUSED(sender))
501 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
502 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
504 int sel = [m_tableView clickedRow];
505 if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?)
508 list->HandleLineEvent( sel, true );
514 wxWidgetImplType* wxWidgetImpl::CreateListBox( wxWindowMac* wxpeer,
515 wxWindowMac* WXUNUSED(parent),
516 wxWindowID WXUNUSED(id),
520 long WXUNUSED(extraStyle))
522 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
523 NSScrollView* scrollview = [[NSScrollView alloc] initWithFrame:r];
525 // use same scroll flags logic as msw
527 [scrollview setHasVerticalScroller:YES];
529 if ( style & wxLB_HSCROLL )
530 [scrollview setHasHorizontalScroller:YES];
532 [scrollview setAutohidesScrollers: ((style & wxLB_ALWAYS_SB) ? NO : YES)];
534 // setting up the true table
536 wxNSTableView* tableview = [[wxNSTableView alloc] init];
537 [tableview setDelegate:tableview];
538 // only one multi-select mode available
539 if ( (style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE) )
540 [tableview setAllowsMultipleSelection:YES];
542 // simple listboxes have no header row
543 [tableview setHeaderView:nil];
545 if ( style & wxLB_HSCROLL )
546 [tableview setColumnAutoresizingStyle:NSTableViewNoColumnAutoresizing];
548 [tableview setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle];
550 wxNSTableDataSource* ds = [[ wxNSTableDataSource alloc] init];
551 [tableview setDataSource:ds];
552 [scrollview setDocumentView:tableview];
555 wxListWidgetCocoaImpl* c = new wxListWidgetCocoaImpl( wxpeer, scrollview, tableview, ds );
557 // temporary hook for dnd
558 [tableview registerForDraggedTypes:[NSArray arrayWithObjects:
559 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
561 [ds setImplementation:c];
565 int wxListWidgetCocoaImpl::DoListHitTest(const wxPoint& inpoint) const
567 // translate inpoint to listpoint via scrollview
568 NSPoint p = wxToNSPoint( m_osxView, inpoint );
569 p = [m_osxView convertPoint:p toView:m_tableView];
570 // hittest using new point
571 NSInteger i = [m_tableView rowAtPoint:p];
575 #endif // wxUSE_LISTBOX