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_LISTBOX, 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 );
317 - (void)setFont:(NSFont *)aFont
319 NSArray *tableColumns = [self tableColumns];
320 unsigned int columnIndex = [tableColumns count];
321 while (columnIndex--)
322 [[(NSTableColumn *)[tableColumns objectAtIndex:columnIndex] dataCell] setFont:aFont];
324 [self setRowHeight:[gNSLayoutManager defaultLineHeightForFont:aFont]+2];
327 - (void) setControlSize:(NSControlSize) size
329 NSArray *tableColumns = [self tableColumns];
330 unsigned int columnIndex = [tableColumns count];
331 while (columnIndex--)
332 [[(NSTableColumn *)[tableColumns objectAtIndex:columnIndex] dataCell] setControlSize:size];
341 wxListWidgetCocoaImpl::wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data ) :
342 wxWidgetCocoaImpl( peer, view ), m_tableView(tableview), m_dataSource(data)
344 InstallEventHandler( tableview );
347 wxListWidgetCocoaImpl::~wxListWidgetCocoaImpl()
349 [m_dataSource release];
352 unsigned int wxListWidgetCocoaImpl::ListGetCount() const
354 wxListBox* lb = dynamic_cast<wxListBox*> ( GetWXPeer() );
355 return lb->GetCount();
362 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertTextColumn( unsigned pos, const wxString& WXUNUSED(title), bool editable,
363 wxAlignment WXUNUSED(just), int defaultWidth)
365 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
366 [col1 setEditable:editable];
368 unsigned formerColCount = [m_tableView numberOfColumns];
370 // there's apparently no way to insert at a specific position
371 [m_tableView addTableColumn:col1 ];
372 if ( pos < formerColCount )
373 [m_tableView moveColumn:formerColCount toColumn:pos];
375 if ( defaultWidth >= 0 )
377 [col1 setMaxWidth:defaultWidth];
378 [col1 setMinWidth:defaultWidth];
379 [col1 setWidth:defaultWidth];
383 [col1 setMaxWidth:1000];
384 [col1 setMinWidth:10];
385 // temporary hack, because I cannot get the automatic column resizing
387 [col1 setWidth:1000];
389 [col1 setResizingMask: NSTableColumnAutoresizingMask];
391 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
393 [[col1 dataCell] setFont:list->GetFont().OSXGetNSFont()];
395 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
396 [col1 setColumn:wxcol];
398 // owned by the tableview
403 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertCheckColumn( unsigned pos , const wxString& WXUNUSED(title), bool editable,
404 wxAlignment WXUNUSED(just), int defaultWidth )
406 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
407 [col1 setEditable:editable];
409 // set your custom cell & set it up
410 NSButtonCell* checkbox = [[NSButtonCell alloc] init];
411 [checkbox setTitle:@""];
412 [checkbox setButtonType:NSSwitchButton];
413 [col1 setDataCell:checkbox] ;
415 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
418 NSControlSize size = NSRegularControlSize;
420 switch ( list->GetWindowVariant() )
422 case wxWINDOW_VARIANT_NORMAL :
423 size = NSRegularControlSize;
426 case wxWINDOW_VARIANT_SMALL :
427 size = NSSmallControlSize;
430 case wxWINDOW_VARIANT_MINI :
431 size = NSMiniControlSize;
434 case wxWINDOW_VARIANT_LARGE :
435 size = NSRegularControlSize;
442 [[col1 dataCell] setControlSize:size];
443 // although there is no text, it may help to get the correct vertical layout
444 [[col1 dataCell] setFont:list->GetFont().OSXGetNSFont()];
449 unsigned formerColCount = [m_tableView numberOfColumns];
451 // there's apparently no way to insert at a specific position
452 [m_tableView addTableColumn:col1 ];
453 if ( pos < formerColCount )
454 [m_tableView moveColumn:formerColCount toColumn:pos];
456 if ( defaultWidth >= 0 )
458 [col1 setMaxWidth:defaultWidth];
459 [col1 setMinWidth:defaultWidth];
460 [col1 setWidth:defaultWidth];
463 [col1 setResizingMask: NSTableColumnNoResizing];
464 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
465 [col1 setColumn:wxcol];
467 // owned by the tableview
474 // inserting / removing lines
477 void wxListWidgetCocoaImpl::ListInsert( unsigned int WXUNUSED(n) )
479 [m_tableView reloadData];
482 void wxListWidgetCocoaImpl::ListDelete( unsigned int WXUNUSED(n) )
484 [m_tableView reloadData];
487 void wxListWidgetCocoaImpl::ListClear()
489 [m_tableView reloadData];
494 void wxListWidgetCocoaImpl::ListDeselectAll()
496 [m_tableView deselectAll:nil];
499 void wxListWidgetCocoaImpl::ListSetSelection( unsigned int n, bool select, bool multi )
503 [m_tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:n]
504 byExtendingSelection:multi];
506 [m_tableView deselectRow: n];
510 int wxListWidgetCocoaImpl::ListGetSelection() const
512 return [m_tableView selectedRow];
515 int wxListWidgetCocoaImpl::ListGetSelections( wxArrayInt& aSelections ) const
519 int count = ListGetCount();
521 for ( int i = 0; i < count; ++i)
523 if ([m_tableView isRowSelected:i])
527 return aSelections.Count();
530 bool wxListWidgetCocoaImpl::ListIsSelected( unsigned int n ) const
532 return [m_tableView isRowSelected:n];
537 void wxListWidgetCocoaImpl::ListScrollTo( unsigned int n )
539 [m_tableView scrollRowToVisible:n];
543 void wxListWidgetCocoaImpl::UpdateLine( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) )
546 [m_tableView reloadData];
549 void wxListWidgetCocoaImpl::UpdateLineToEnd( unsigned int WXUNUSED(n))
552 [m_tableView reloadData];
555 void wxListWidgetCocoaImpl::controlDoubleAction(WXWidget WXUNUSED(slf),void* WXUNUSED(_cmd), void *WXUNUSED(sender))
557 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
558 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
560 int sel = [m_tableView clickedRow];
561 if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?)
564 list->HandleLineEvent( sel, true );
570 wxWidgetImplType* wxWidgetImpl::CreateListBox( wxWindowMac* wxpeer,
571 wxWindowMac* WXUNUSED(parent),
572 wxWindowID WXUNUSED(id),
576 long WXUNUSED(extraStyle))
578 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
579 NSScrollView* scrollview = [[NSScrollView alloc] initWithFrame:r];
581 // use same scroll flags logic as msw
583 [scrollview setHasVerticalScroller:YES];
585 if ( style & wxLB_HSCROLL )
586 [scrollview setHasHorizontalScroller:YES];
588 [scrollview setAutohidesScrollers: ((style & wxLB_ALWAYS_SB) ? NO : YES)];
590 // setting up the true table
592 wxNSTableView* tableview = [[wxNSTableView alloc] init];
593 [tableview setDelegate:tableview];
594 // only one multi-select mode available
595 if ( (style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE) )
596 [tableview setAllowsMultipleSelection:YES];
598 // simple listboxes have no header row
599 [tableview setHeaderView:nil];
601 if ( style & wxLB_HSCROLL )
602 [tableview setColumnAutoresizingStyle:NSTableViewNoColumnAutoresizing];
604 [tableview setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle];
606 wxNSTableDataSource* ds = [[ wxNSTableDataSource alloc] init];
607 [tableview setDataSource:ds];
608 [scrollview setDocumentView:tableview];
611 wxListWidgetCocoaImpl* c = new wxListWidgetCocoaImpl( wxpeer, scrollview, tableview, ds );
613 // temporary hook for dnd
614 // [tableview registerForDraggedTypes:[NSArray arrayWithObjects:
615 // NSStringPboardType, NSFilenamesPboardType, (NSString*) kPasteboardTypeFileURLPromise, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
617 [ds setImplementation:c];
621 int wxListWidgetCocoaImpl::DoListHitTest(const wxPoint& inpoint) const
623 // translate inpoint to listpoint via scrollview
624 NSPoint p = wxToNSPoint( m_osxView, inpoint );
625 p = [m_osxView convertPoint:p toView:m_tableView];
626 // hittest using new point
627 NSInteger i = [m_tableView rowAtPoint:p];
631 #endif // wxUSE_LISTBOX