]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/listbox.mm
The alignment controls are now left-aligned if the floating controls are not shown.
[wxWidgets.git] / src / osx / cocoa / listbox.mm
CommitLineData
dbeddfb9
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/cocoa/listbox.mm
3// Purpose: wxListBox
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
dbeddfb9
SC
7// Copyright: (c) Stefan Csomor
8// Licence: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
11#include "wx/wxprec.h"
12
13#if wxUSE_LISTBOX
14
15#include "wx/listbox.h"
4dd9fdf8 16#include "wx/dnd.h"
dbeddfb9
SC
17
18#ifndef WX_PRECOMP
19 #include "wx/log.h"
20 #include "wx/intl.h"
21 #include "wx/utils.h"
22 #include "wx/settings.h"
23 #include "wx/arrstr.h"
24 #include "wx/dcclient.h"
25#endif
26
27#include "wx/osx/private.h"
28
29#include <vector>
30
31// forward decls
32
33class wxListWidgetCocoaImpl;
34
030495ec 35@interface wxNSTableDataSource : NSObject wxOSX_10_6_AND_LATER(<NSTableViewDataSource>)
dbeddfb9
SC
36{
37 wxListWidgetCocoaImpl* impl;
38}
39
03647350
VZ
40- (id)tableView:(NSTableView *)aTableView
41 objectValueForTableColumn:(NSTableColumn *)aTableColumn
dbeddfb9
SC
42 row:(NSInteger)rowIndex;
43
03647350
VZ
44- (void)tableView:(NSTableView *)aTableView
45 setObjectValue:(id)value forTableColumn:(NSTableColumn *)aTableColumn
dbeddfb9 46 row:(NSInteger)rowIndex;
03647350 47
dbeddfb9
SC
48- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView;
49
50- (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation;
51- (wxListWidgetCocoaImpl*) implementation;
52
53@end
54
030495ec 55@interface wxNSTableView : NSTableView wxOSX_10_6_AND_LATER(<NSTableViewDelegate>)
dbeddfb9 56{
dbeddfb9
SC
57}
58
dbeddfb9
SC
59@end
60
61//
62// table column
63//
64
65class wxCocoaTableColumn;
66
67@interface wxNSTableColumn : NSTableColumn
68{
69 wxCocoaTableColumn* column;
70}
71
72- (void) setColumn: (wxCocoaTableColumn*) col;
73
74- (wxCocoaTableColumn*) column;
75
76@end
77
78class WXDLLIMPEXP_CORE wxCocoaTableColumn : public wxListWidgetColumn
79{
80public :
03647350 81 wxCocoaTableColumn( wxNSTableColumn* column, bool editable )
dbeddfb9
SC
82 : m_column( column ), m_editable(editable)
83 {
84 }
03647350 85
dbeddfb9
SC
86 ~wxCocoaTableColumn()
87 {
88 }
03647350 89
dbeddfb9 90 wxNSTableColumn* GetNSTableColumn() const { return m_column ; }
03647350 91
dbeddfb9 92 bool IsEditable() const { return m_editable; }
03647350 93
dbeddfb9
SC
94protected :
95 wxNSTableColumn* m_column;
96 bool m_editable;
97} ;
98
99NSString* column1 = @"1";
100
101class wxListWidgetCocoaImpl : public wxWidgetCocoaImpl, public wxListWidgetImpl
102{
103public :
104 wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data );
03647350 105
dbeddfb9 106 ~wxListWidgetCocoaImpl();
03647350
VZ
107
108 virtual wxListWidgetColumn* InsertTextColumn( unsigned pos, const wxString& title, bool editable = false,
dbeddfb9 109 wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ;
03647350 110 virtual wxListWidgetColumn* InsertCheckColumn( unsigned pos , const wxString& title, bool editable = false,
dbeddfb9 111 wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ;
03647350 112
dbeddfb9 113 // add and remove
03647350
VZ
114
115 virtual void ListDelete( unsigned int n ) ;
dbeddfb9
SC
116 virtual void ListInsert( unsigned int n ) ;
117 virtual void ListClear() ;
118
119 // selecting
120
121 virtual void ListDeselectAll();
03647350 122
dbeddfb9
SC
123 virtual void ListSetSelection( unsigned int n, bool select, bool multi ) ;
124 virtual int ListGetSelection() const ;
03647350 125
dbeddfb9 126 virtual int ListGetSelections( wxArrayInt& aSelections ) const ;
03647350 127
dbeddfb9 128 virtual bool ListIsSelected( unsigned int n ) const ;
03647350 129
dbeddfb9
SC
130 // display
131
132 virtual void ListScrollTo( unsigned int n ) ;
133
134 // accessing content
135
136 virtual unsigned int ListGetCount() const ;
20196e15 137 virtual int DoListHitTest( const wxPoint& inpoint ) const;
03647350 138
dbeddfb9
SC
139 int ListGetColumnType( int col )
140 {
141 return col;
142 }
143 virtual void UpdateLine( unsigned int n, wxListWidgetColumn* col = NULL ) ;
144 virtual void UpdateLineToEnd( unsigned int n);
4dd9fdf8 145
01000cbc 146 virtual void controlDoubleAction(WXWidget slf, void* _cmd, void *sender);
f4c24d3d 147
20196e15 148
dbeddfb9
SC
149protected :
150 wxNSTableView* m_tableView ;
03647350 151
dbeddfb9
SC
152 wxNSTableDataSource* m_dataSource;
153} ;
154
155//
156// implementations
157//
158
159@implementation wxNSTableColumn
160
161- (id) init
162{
a985b2c8 163 self = [super init];
dbeddfb9
SC
164 column = nil;
165 return self;
166}
167
168- (void) setColumn: (wxCocoaTableColumn*) col
169{
170 column = col;
171}
172
173- (wxCocoaTableColumn*) column
174{
175 return column;
176}
177
178@end
179
180class wxNSTableViewCellValue : public wxListWidgetCellValue
181{
182public :
183 wxNSTableViewCellValue( id &v ) : value(v)
184 {
185 }
03647350 186
dbeddfb9
SC
187 virtual ~wxNSTableViewCellValue() {}
188
189 virtual void Set( CFStringRef v )
190 {
191 value = [[(NSString*)v retain] autorelease];
192 }
03647350 193 virtual void Set( const wxString& value )
dbeddfb9
SC
194 {
195 Set( (CFStringRef) wxCFStringRef( value ) );
196 }
03647350 197 virtual void Set( int v )
dbeddfb9
SC
198 {
199 value = [NSNumber numberWithInt:v];
200 }
03647350
VZ
201
202 virtual int GetIntValue() const
dbeddfb9
SC
203 {
204 if ( [value isKindOfClass:[NSNumber class]] )
205 return [ (NSNumber*) value intValue ];
03647350 206
dbeddfb9
SC
207 return 0;
208 }
03647350
VZ
209
210 virtual wxString GetStringValue() const
dbeddfb9
SC
211 {
212 if ( [value isKindOfClass:[NSString class]] )
f66ecdc4 213 return wxCFStringRef::AsString( (NSString*) value );
03647350 214
dbeddfb9
SC
215 return wxEmptyString;
216 }
03647350 217
dbeddfb9
SC
218protected:
219 id& value;
220} ;
221
222@implementation wxNSTableDataSource
223
224- (id) init
225{
a985b2c8 226 self = [super init];
dbeddfb9
SC
227 impl = nil;
228 return self;
229}
230
231- (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation
232{
233 impl = theImplementation;
234}
235
236- (wxListWidgetCocoaImpl*) implementation
237{
238 return impl;
239}
240
241- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
242{
d8207702 243 wxUnusedVar(aTableView);
dbeddfb9
SC
244 if ( impl )
245 return impl->ListGetCount();
246 return 0;
247}
248
03647350
VZ
249- (id)tableView:(NSTableView *)aTableView
250 objectValueForTableColumn:(NSTableColumn *)aTableColumn
dbeddfb9
SC
251 row:(NSInteger)rowIndex
252{
d8207702 253 wxUnusedVar(aTableView);
dbeddfb9
SC
254 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
255 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
256 wxCocoaTableColumn* col = [tablecol column];
257 id value = nil;
258 wxNSTableViewCellValue cellvalue(value);
259 lb->GetValueCallback(rowIndex, col, cellvalue);
260 return value;
261}
262
03647350
VZ
263- (void)tableView:(NSTableView *)aTableView
264 setObjectValue:(id)value forTableColumn:(NSTableColumn *)aTableColumn
dbeddfb9
SC
265 row:(NSInteger)rowIndex
266{
d8207702 267 wxUnusedVar(aTableView);
dbeddfb9
SC
268 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
269 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
270 wxCocoaTableColumn* col = [tablecol column];
271 wxNSTableViewCellValue cellvalue(value);
272 lb->SetValueCallback(rowIndex, col, cellvalue);
273}
274
275@end
276
277@implementation wxNSTableView
278
4dd9fdf8 279+ (void)initialize
21267321 280{
4dd9fdf8 281 static BOOL initialized = NO;
03647350 282 if (!initialized)
21267321 283 {
4dd9fdf8
SC
284 initialized = YES;
285 wxOSXCocoaClassAddWXMethods( self );
21267321
SC
286 }
287}
dbeddfb9 288
81eaa4da
SC
289- (void) tableViewSelectionDidChange: (NSNotification *) notification
290{
291 wxUnusedVar(notification);
292
293 int row = [self selectedRow];
294
295 if (row == -1)
296 {
297 // no row selected
298 }
299 else
300 {
301 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
302 wxListBox *list = static_cast<wxListBox*> ( impl->GetWXPeer());
303 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
304
ce7fe42e 305 wxCommandEvent event( wxEVT_LISTBOX, list->GetId() );
81eaa4da
SC
306
307 if ((row < 0) || (row > (int) list->GetCount())) // OS X can select an item below the last item
308 return;
309
310 if ( !list->MacGetBlockEvents() )
311 list->HandleLineEvent( row, false );
312 }
313
314}
315
18d5c52d
SC
316- (void)setFont:(NSFont *)aFont
317{
318 NSArray *tableColumns = [self tableColumns];
319 unsigned int columnIndex = [tableColumns count];
320 while (columnIndex--)
321 [[(NSTableColumn *)[tableColumns objectAtIndex:columnIndex] dataCell] setFont:aFont];
322
85569194 323 [self setRowHeight:[gNSLayoutManager defaultLineHeightForFont:aFont]+2];
18d5c52d
SC
324}
325
326- (void) setControlSize:(NSControlSize) size
327{
328 NSArray *tableColumns = [self tableColumns];
329 unsigned int columnIndex = [tableColumns count];
330 while (columnIndex--)
331 [[(NSTableColumn *)[tableColumns objectAtIndex:columnIndex] dataCell] setControlSize:size];
332}
333
dbeddfb9
SC
334@end
335
336//
337//
338//
339
340wxListWidgetCocoaImpl::wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data ) :
341 wxWidgetCocoaImpl( peer, view ), m_tableView(tableview), m_dataSource(data)
342{
4dd9fdf8 343 InstallEventHandler( tableview );
dbeddfb9
SC
344}
345
346wxListWidgetCocoaImpl::~wxListWidgetCocoaImpl()
347{
348 [m_dataSource release];
349}
350
03647350 351unsigned int wxListWidgetCocoaImpl::ListGetCount() const
dbeddfb9
SC
352{
353 wxListBox* lb = dynamic_cast<wxListBox*> ( GetWXPeer() );
354 return lb->GetCount();
355}
356
357//
358// columns
359//
360
03647350
VZ
361wxListWidgetColumn* wxListWidgetCocoaImpl::InsertTextColumn( unsigned pos, const wxString& WXUNUSED(title), bool editable,
362 wxAlignment WXUNUSED(just), int defaultWidth)
dbeddfb9
SC
363{
364 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
365 [col1 setEditable:editable];
03647350 366
dbeddfb9 367 unsigned formerColCount = [m_tableView numberOfColumns];
03647350 368
dbeddfb9
SC
369 // there's apparently no way to insert at a specific position
370 [m_tableView addTableColumn:col1 ];
371 if ( pos < formerColCount )
372 [m_tableView moveColumn:formerColCount toColumn:pos];
03647350 373
dbeddfb9
SC
374 if ( defaultWidth >= 0 )
375 {
376 [col1 setMaxWidth:defaultWidth];
377 [col1 setMinWidth:defaultWidth];
e32090ba 378 [col1 setWidth:defaultWidth];
dbeddfb9 379 }
e32090ba
SC
380 else
381 {
03647350 382 [col1 setMaxWidth:1000];
e32090ba
SC
383 [col1 setMinWidth:10];
384 // temporary hack, because I cannot get the automatic column resizing
385 // to work properly
386 [col1 setWidth:1000];
387 }
388 [col1 setResizingMask: NSTableColumnAutoresizingMask];
18d5c52d
SC
389
390 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
391 if ( list != NULL )
392 [[col1 dataCell] setFont:list->GetFont().OSXGetNSFont()];
393
dbeddfb9
SC
394 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
395 [col1 setColumn:wxcol];
396
397 // owned by the tableview
398 [col1 release];
399 return wxcol;
400}
401
03647350
VZ
402wxListWidgetColumn* wxListWidgetCocoaImpl::InsertCheckColumn( unsigned pos , const wxString& WXUNUSED(title), bool editable,
403 wxAlignment WXUNUSED(just), int defaultWidth )
dbeddfb9
SC
404{
405 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
406 [col1 setEditable:editable];
03647350 407
dbeddfb9
SC
408 // set your custom cell & set it up
409 NSButtonCell* checkbox = [[NSButtonCell alloc] init];
410 [checkbox setTitle:@""];
411 [checkbox setButtonType:NSSwitchButton];
412 [col1 setDataCell:checkbox] ;
18d5c52d
SC
413
414 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
415 if ( list != NULL )
416 {
417 NSControlSize size = NSRegularControlSize;
418
419 switch ( list->GetWindowVariant() )
420 {
421 case wxWINDOW_VARIANT_NORMAL :
422 size = NSRegularControlSize;
423 break ;
424
425 case wxWINDOW_VARIANT_SMALL :
426 size = NSSmallControlSize;
427 break ;
428
429 case wxWINDOW_VARIANT_MINI :
430 size = NSMiniControlSize;
431 break ;
432
433 case wxWINDOW_VARIANT_LARGE :
434 size = NSRegularControlSize;
435 break ;
436
437 default:
438 break ;
439 }
440
441 [[col1 dataCell] setControlSize:size];
442 // although there is no text, it may help to get the correct vertical layout
443 [[col1 dataCell] setFont:list->GetFont().OSXGetNSFont()];
444 }
445
dbeddfb9 446 [checkbox release];
03647350 447
dbeddfb9 448 unsigned formerColCount = [m_tableView numberOfColumns];
03647350 449
dbeddfb9
SC
450 // there's apparently no way to insert at a specific position
451 [m_tableView addTableColumn:col1 ];
452 if ( pos < formerColCount )
453 [m_tableView moveColumn:formerColCount toColumn:pos];
03647350 454
dbeddfb9
SC
455 if ( defaultWidth >= 0 )
456 {
457 [col1 setMaxWidth:defaultWidth];
458 [col1 setMinWidth:defaultWidth];
e32090ba 459 [col1 setWidth:defaultWidth];
dbeddfb9 460 }
03647350 461
e32090ba 462 [col1 setResizingMask: NSTableColumnNoResizing];
dbeddfb9
SC
463 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
464 [col1 setColumn:wxcol];
465
466 // owned by the tableview
467 [col1 release];
468 return wxcol;
469}
470
471
472//
473// inserting / removing lines
474//
475
03647350 476void wxListWidgetCocoaImpl::ListInsert( unsigned int WXUNUSED(n) )
dbeddfb9 477{
dbeddfb9
SC
478 [m_tableView reloadData];
479}
480
03647350 481void wxListWidgetCocoaImpl::ListDelete( unsigned int WXUNUSED(n) )
dbeddfb9
SC
482{
483 [m_tableView reloadData];
484}
485
03647350 486void wxListWidgetCocoaImpl::ListClear()
dbeddfb9
SC
487{
488 [m_tableView reloadData];
489}
490
491// selecting
492
493void wxListWidgetCocoaImpl::ListDeselectAll()
494{
495 [m_tableView deselectAll:nil];
496}
497
03647350 498void wxListWidgetCocoaImpl::ListSetSelection( unsigned int n, bool select, bool multi )
dbeddfb9
SC
499{
500 // TODO
501 if ( select )
64d79e16
VZ
502 [m_tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:n]
503 byExtendingSelection:multi];
dbeddfb9
SC
504 else
505 [m_tableView deselectRow: n];
506
507}
508
03647350 509int wxListWidgetCocoaImpl::ListGetSelection() const
dbeddfb9 510{
21267321 511 return [m_tableView selectedRow];
dbeddfb9
SC
512}
513
03647350 514int wxListWidgetCocoaImpl::ListGetSelections( wxArrayInt& aSelections ) const
dbeddfb9 515{
21267321
SC
516 aSelections.Empty();
517
518 int count = ListGetCount();
519
520 for ( int i = 0; i < count; ++i)
521 {
7914542b 522 if ([m_tableView isRowSelected:i])
21267321
SC
523 aSelections.Add(i);
524 }
525
526 return aSelections.Count();
dbeddfb9
SC
527}
528
03647350 529bool wxListWidgetCocoaImpl::ListIsSelected( unsigned int n ) const
dbeddfb9 530{
21267321 531 return [m_tableView isRowSelected:n];
dbeddfb9
SC
532}
533
534// display
535
03647350 536void wxListWidgetCocoaImpl::ListScrollTo( unsigned int n )
dbeddfb9
SC
537{
538 [m_tableView scrollRowToVisible:n];
539}
540
03647350
VZ
541
542void wxListWidgetCocoaImpl::UpdateLine( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) )
dbeddfb9
SC
543{
544 // TODO optimize
545 [m_tableView reloadData];
546}
547
03647350 548void wxListWidgetCocoaImpl::UpdateLineToEnd( unsigned int WXUNUSED(n))
dbeddfb9
SC
549{
550 // TODO optimize
551 [m_tableView reloadData];
552}
553
d8207702 554void wxListWidgetCocoaImpl::controlDoubleAction(WXWidget WXUNUSED(slf),void* WXUNUSED(_cmd), void *WXUNUSED(sender))
4dd9fdf8
SC
555{
556 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
557 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
558
559 int sel = [m_tableView clickedRow];
560 if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?)
561 return;
562
563 list->HandleLineEvent( sel, true );
564}
dbeddfb9
SC
565
566// accessing content
567
568
03647350
VZ
569wxWidgetImplType* wxWidgetImpl::CreateListBox( wxWindowMac* wxpeer,
570 wxWindowMac* WXUNUSED(parent),
571 wxWindowID WXUNUSED(id),
572 const wxPoint& pos,
dbeddfb9 573 const wxSize& size,
03647350 574 long style,
d8207702 575 long WXUNUSED(extraStyle))
dbeddfb9 576{
dbeddfb9
SC
577 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
578 NSScrollView* scrollview = [[NSScrollView alloc] initWithFrame:r];
03647350 579
dbeddfb9 580 // use same scroll flags logic as msw
03647350 581
dbeddfb9 582 [scrollview setHasVerticalScroller:YES];
03647350 583
dbeddfb9
SC
584 if ( style & wxLB_HSCROLL )
585 [scrollview setHasHorizontalScroller:YES];
03647350 586
dbeddfb9 587 [scrollview setAutohidesScrollers: ((style & wxLB_ALWAYS_SB) ? NO : YES)];
e32090ba 588
dbeddfb9 589 // setting up the true table
03647350 590
dbeddfb9 591 wxNSTableView* tableview = [[wxNSTableView alloc] init];
81eaa4da 592 [tableview setDelegate:tableview];
dbeddfb9
SC
593 // only one multi-select mode available
594 if ( (style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE) )
595 [tableview setAllowsMultipleSelection:YES];
03647350 596
dbeddfb9
SC
597 // simple listboxes have no header row
598 [tableview setHeaderView:nil];
03647350 599
e32090ba
SC
600 if ( style & wxLB_HSCROLL )
601 [tableview setColumnAutoresizingStyle:NSTableViewNoColumnAutoresizing];
602 else
603 [tableview setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle];
03647350 604
dbeddfb9
SC
605 wxNSTableDataSource* ds = [[ wxNSTableDataSource alloc] init];
606 [tableview setDataSource:ds];
e32090ba
SC
607 [scrollview setDocumentView:tableview];
608 [tableview release];
609
dbeddfb9 610 wxListWidgetCocoaImpl* c = new wxListWidgetCocoaImpl( wxpeer, scrollview, tableview, ds );
4dd9fdf8
SC
611
612 // temporary hook for dnd
d757ef93
SC
613 // [tableview registerForDraggedTypes:[NSArray arrayWithObjects:
614 // NSStringPboardType, NSFilenamesPboardType, (NSString*) kPasteboardTypeFileURLPromise, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
4dd9fdf8 615
dbeddfb9
SC
616 [ds setImplementation:c];
617 return c;
618}
619
20196e15
SC
620int wxListWidgetCocoaImpl::DoListHitTest(const wxPoint& inpoint) const
621{
622 // translate inpoint to listpoint via scrollview
623 NSPoint p = wxToNSPoint( m_osxView, inpoint );
624 p = [m_osxView convertPoint:p toView:m_tableView];
625 // hittest using new point
626 NSInteger i = [m_tableView rowAtPoint:p];
627 return i;
dbeddfb9
SC
628}
629
630#endif // wxUSE_LISTBOX