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