Fixed wxPropertyGridManager::CreatePropertyGrid(), corrected documentation about...
[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
7// RCS-ID: $Id: listbox.cpp 54820 2008-07-29 20:04:11Z 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"
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
35@interface wxNSTableDataSource : NSObject
36{
37 wxListWidgetCocoaImpl* impl;
38}
39
40- (id)tableView:(NSTableView *)aTableView
41 objectValueForTableColumn:(NSTableColumn *)aTableColumn
42 row:(NSInteger)rowIndex;
43
ffad7b0d
SC
44- (void)tableView:(NSTableView *)aTableView
45 setObjectValue:(id)value forTableColumn:(NSTableColumn *)aTableColumn
dbeddfb9
SC
46 row:(NSInteger)rowIndex;
47
48- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView;
49
50- (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation;
51- (wxListWidgetCocoaImpl*) implementation;
52
53@end
54
55@interface wxNSTableView : NSTableView
56{
57 wxListWidgetCocoaImpl* impl;
58}
59
60- (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation;
61- (wxListWidgetCocoaImpl*) implementation;
21267321
SC
62- (void)clickedAction: (id) sender;
63- (void)doubleClickedAction: (id) sender;
dbeddfb9
SC
64
65@end
66
67//
68// table column
69//
70
71class wxCocoaTableColumn;
72
73@interface wxNSTableColumn : NSTableColumn
74{
75 wxCocoaTableColumn* column;
76}
77
78- (void) setColumn: (wxCocoaTableColumn*) col;
79
80- (wxCocoaTableColumn*) column;
81
82@end
83
84class WXDLLIMPEXP_CORE wxCocoaTableColumn : public wxListWidgetColumn
85{
86public :
87 wxCocoaTableColumn( wxNSTableColumn* column, bool editable )
88 : m_column( column ), m_editable(editable)
89 {
90 }
91
92 ~wxCocoaTableColumn()
93 {
94 }
95
96 wxNSTableColumn* GetNSTableColumn() const { return m_column ; }
97
98 bool IsEditable() const { return m_editable; }
99
100protected :
101 wxNSTableColumn* m_column;
102 bool m_editable;
103} ;
104
105NSString* column1 = @"1";
106
107class wxListWidgetCocoaImpl : public wxWidgetCocoaImpl, public wxListWidgetImpl
108{
109public :
110 wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data );
111
112 ~wxListWidgetCocoaImpl();
113
114 virtual wxListWidgetColumn* InsertTextColumn( unsigned pos, const wxString& title, bool editable = false,
115 wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ;
116 virtual wxListWidgetColumn* InsertCheckColumn( unsigned pos , const wxString& title, bool editable = false,
117 wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ;
118
119 // add and remove
120
121 virtual void ListDelete( unsigned int n ) ;
122 virtual void ListInsert( unsigned int n ) ;
123 virtual void ListClear() ;
124
125 // selecting
126
127 virtual void ListDeselectAll();
128
129 virtual void ListSetSelection( unsigned int n, bool select, bool multi ) ;
130 virtual int ListGetSelection() const ;
131
132 virtual int ListGetSelections( wxArrayInt& aSelections ) const ;
133
134 virtual bool ListIsSelected( unsigned int n ) const ;
135
136 // display
137
138 virtual void ListScrollTo( unsigned int n ) ;
139
140 // accessing content
141
142 virtual unsigned int ListGetCount() const ;
143
144 int ListGetColumnType( int col )
145 {
146 return col;
147 }
148 virtual void UpdateLine( unsigned int n, wxListWidgetColumn* col = NULL ) ;
149 virtual void UpdateLineToEnd( unsigned int n);
150protected :
151 wxNSTableView* m_tableView ;
152
153 wxNSTableDataSource* m_dataSource;
154} ;
155
156//
157// implementations
158//
159
160@implementation wxNSTableColumn
161
162- (id) init
163{
164 [super init];
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 }
187
188 virtual ~wxNSTableViewCellValue() {}
189
190 virtual void Set( CFStringRef v )
191 {
192 value = [[(NSString*)v retain] autorelease];
193 }
194 virtual void Set( const wxString& value )
195 {
196 Set( (CFStringRef) wxCFStringRef( value ) );
197 }
198 virtual void Set( int v )
199 {
200 value = [NSNumber numberWithInt:v];
201 }
202
203 virtual int GetIntValue() const
204 {
205 if ( [value isKindOfClass:[NSNumber class]] )
206 return [ (NSNumber*) value intValue ];
207
208 return 0;
209 }
210
211 virtual wxString GetStringValue() const
212 {
213 if ( [value isKindOfClass:[NSString class]] )
214 return wxCFStringRef( (CFStringRef) [value retain] ).AsString();
215
216 return wxEmptyString;
217 }
218
219protected:
220 id& value;
221} ;
222
223@implementation wxNSTableDataSource
224
225- (id) init
226{
227 [super init];
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{
244 if ( impl )
245 return impl->ListGetCount();
246 return 0;
247}
248
249- (id)tableView:(NSTableView *)aTableView
250 objectValueForTableColumn:(NSTableColumn *)aTableColumn
251 row:(NSInteger)rowIndex
252{
253 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
254 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
255 wxCocoaTableColumn* col = [tablecol column];
256 id value = nil;
257 wxNSTableViewCellValue cellvalue(value);
258 lb->GetValueCallback(rowIndex, col, cellvalue);
259 return value;
260}
261
262- (void)tableView:(NSTableView *)aTableView
263 setObjectValue:(id)value forTableColumn:(NSTableColumn *)aTableColumn
264 row:(NSInteger)rowIndex
265{
266 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
267 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
268 wxCocoaTableColumn* col = [tablecol column];
269 wxNSTableViewCellValue cellvalue(value);
270 lb->SetValueCallback(rowIndex, col, cellvalue);
271}
272
273@end
274
275@implementation wxNSTableView
276
277- (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation
278{
279 impl = theImplementation;
280}
281
282- (wxListWidgetCocoaImpl*) implementation
283{
284 return impl;
285}
286
21267321
SC
287- (id) init
288{
289 [super init];
290 impl = NULL;
291 [self setTarget: self];
292 [self setAction: @selector(clickedAction:)];
293 [self setDoubleAction: @selector(doubleClickedAction:)];
294 return self;
295}
296
297- (void) clickedAction: (id) sender
298{
299 if ( impl )
300 {
301 wxListBox *list = static_cast<wxListBox*> ( impl->GetWXPeer());
302 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
303
304 wxCommandEvent event( wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() );
305
306 int sel = [self clickedRow];
307 if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?)
308 return;
309
310 list->HandleLineEvent( sel, false );
311 }
312}
313
314- (void) doubleClickedAction: (id) sender
315{
316 if ( impl )
317 {
318 wxListBox *list = static_cast<wxListBox*> ( impl->GetWXPeer());
319 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
320
321 int sel = [self clickedRow];
322 if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?)
323 return;
324
325 list->HandleLineEvent( sel, true );
326 }
327}
dbeddfb9
SC
328
329@end
330
331//
332//
333//
334
335wxListWidgetCocoaImpl::wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data ) :
336 wxWidgetCocoaImpl( peer, view ), m_tableView(tableview), m_dataSource(data)
337{
338}
339
340wxListWidgetCocoaImpl::~wxListWidgetCocoaImpl()
341{
342 [m_dataSource release];
343}
344
345unsigned int wxListWidgetCocoaImpl::ListGetCount() const
346{
347 wxListBox* lb = dynamic_cast<wxListBox*> ( GetWXPeer() );
348 return lb->GetCount();
349}
350
351//
352// columns
353//
354
355wxListWidgetColumn* wxListWidgetCocoaImpl::InsertTextColumn( unsigned pos, const wxString& title, bool editable,
356 wxAlignment just, int defaultWidth)
357{
358 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
359 [col1 setEditable:editable];
360
361 unsigned formerColCount = [m_tableView numberOfColumns];
362
363 // there's apparently no way to insert at a specific position
364 [m_tableView addTableColumn:col1 ];
365 if ( pos < formerColCount )
366 [m_tableView moveColumn:formerColCount toColumn:pos];
367
368 if ( defaultWidth >= 0 )
369 {
370 [col1 setMaxWidth:defaultWidth];
371 [col1 setMinWidth:defaultWidth];
372 }
373
374 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
375 [col1 setColumn:wxcol];
376
377 // owned by the tableview
378 [col1 release];
379 return wxcol;
380}
381
382wxListWidgetColumn* wxListWidgetCocoaImpl::InsertCheckColumn( unsigned pos , const wxString& title, bool editable,
383 wxAlignment just, int defaultWidth )
384{
385 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
386 [col1 setEditable:editable];
387
388 // set your custom cell & set it up
389 NSButtonCell* checkbox = [[NSButtonCell alloc] init];
390 [checkbox setTitle:@""];
391 [checkbox setButtonType:NSSwitchButton];
392 [col1 setDataCell:checkbox] ;
393 [checkbox release];
394
395 unsigned formerColCount = [m_tableView numberOfColumns];
396
397 // there's apparently no way to insert at a specific position
398 [m_tableView addTableColumn:col1 ];
399 if ( pos < formerColCount )
400 [m_tableView moveColumn:formerColCount toColumn:pos];
401
402 if ( defaultWidth >= 0 )
403 {
404 [col1 setMaxWidth:defaultWidth];
405 [col1 setMinWidth:defaultWidth];
406 }
407
408 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
409 [col1 setColumn:wxcol];
410
411 // owned by the tableview
412 [col1 release];
413 return wxcol;
414}
415
416
417//
418// inserting / removing lines
419//
420
421void wxListWidgetCocoaImpl::ListInsert( unsigned int n )
422{
423#if 0
424 {
425 wxListBoxCocoaLine* line = new wxListBoxCocoaLine();
426 line->SetLabel(items[i]);
427 if ( m_items.size() <= n+i )
428 m_items.push_back( line );
429 else
430 m_items.insert(m_items.begin()+n, line);
431/*
432 NSMutableDictionary* line = [[NSMutableDictionary alloc] init];
433 [line setObject:wxCFStringRef(items[i]).AsNSString() forKey:column1];
434 NSMutableArray* array = [m_dataSource items];
435 if ( [array count] <= n+i )
436 [array addObject:line];
437 else
438 [array insertObject:line atIndex:n];
439*/
440 }
441#endif
442 [m_tableView reloadData];
443}
444
445void wxListWidgetCocoaImpl::ListDelete( unsigned int n )
446{
447 [m_tableView reloadData];
448}
449
450void wxListWidgetCocoaImpl::ListClear()
451{
452 [m_tableView reloadData];
453}
454
455// selecting
456
457void wxListWidgetCocoaImpl::ListDeselectAll()
458{
459 [m_tableView deselectAll:nil];
460}
461
462void wxListWidgetCocoaImpl::ListSetSelection( unsigned int n, bool select, bool multi )
463{
464 // TODO
465 if ( select )
466 [m_tableView selectRow: n byExtendingSelection:multi];
467 else
468 [m_tableView deselectRow: n];
469
470}
471
472int wxListWidgetCocoaImpl::ListGetSelection() const
473{
21267321 474 return [m_tableView selectedRow];
dbeddfb9
SC
475}
476
477int wxListWidgetCocoaImpl::ListGetSelections( wxArrayInt& aSelections ) const
478{
21267321
SC
479 aSelections.Empty();
480
481 int count = ListGetCount();
482
483 for ( int i = 0; i < count; ++i)
484 {
485 if ([m_tableView isRowSelected:count])
486 aSelections.Add(i);
487 }
488
489 return aSelections.Count();
dbeddfb9
SC
490}
491
492bool wxListWidgetCocoaImpl::ListIsSelected( unsigned int n ) const
493{
21267321 494 return [m_tableView isRowSelected:n];
dbeddfb9
SC
495}
496
497// display
498
499void wxListWidgetCocoaImpl::ListScrollTo( unsigned int n )
500{
501 [m_tableView scrollRowToVisible:n];
502}
503
504
505void wxListWidgetCocoaImpl::UpdateLine( unsigned int n, wxListWidgetColumn* col )
506{
507 // TODO optimize
508 [m_tableView reloadData];
509}
510
511void wxListWidgetCocoaImpl::UpdateLineToEnd( unsigned int n)
512{
513 // TODO optimize
514 [m_tableView reloadData];
515}
516
517
518// accessing content
519
520
521wxWidgetImplType* wxWidgetImpl::CreateListBox( wxWindowMac* wxpeer,
522 wxWindowMac* parent,
523 wxWindowID id,
524 const wxPoint& pos,
525 const wxSize& size,
526 long style,
527 long extraStyle)
528{
dbeddfb9
SC
529 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
530 NSScrollView* scrollview = [[NSScrollView alloc] initWithFrame:r];
531
532 // use same scroll flags logic as msw
533
534 [scrollview setHasVerticalScroller:YES];
535
536 if ( style & wxLB_HSCROLL )
537 [scrollview setHasHorizontalScroller:YES];
538
539 [scrollview setAutohidesScrollers: ((style & wxLB_ALWAYS_SB) ? NO : YES)];
540
541 // setting up the true table
542
543 wxNSTableView* tableview = [[wxNSTableView alloc] init];
544 [scrollview setDocumentView:tableview];
545 [tableview release];
546
547 // only one multi-select mode available
548 if ( (style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE) )
549 [tableview setAllowsMultipleSelection:YES];
550
551 // simple listboxes have no header row
552 [tableview setHeaderView:nil];
553
554 [tableview setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle];
555 wxNSTableDataSource* ds = [[ wxNSTableDataSource alloc] init];
556 [tableview setDataSource:ds];
dbeddfb9
SC
557 wxListWidgetCocoaImpl* c = new wxListWidgetCocoaImpl( wxpeer, scrollview, tableview, ds );
558 [tableview setImplementation:c];
559 [ds setImplementation:c];
560 return c;
561}
562
563int wxListBox::DoListHitTest(const wxPoint& inpoint) const
564{
565#if wxOSX_USE_CARBON
566 OSStatus err;
567
568 // There are few reasons why this is complicated:
569 // 1) There is no native HitTest function for Mac
570 // 2) GetDataBrowserItemPartBounds only works on visible items
571 // 3) We can't do it through GetDataBrowserTableView[Item]RowHeight
572 // because what it returns is basically inaccurate in the context
573 // of the coordinates we want here, but we use this as a guess
574 // for where the first visible item lies
575
576 wxPoint point = inpoint;
577
578 // get column property ID (req. for call to itempartbounds)
579 DataBrowserTableViewColumnID colId = 0;
580 err = GetDataBrowserTableViewColumnProperty(m_peer->GetControlRef(), 0, &colId);
581 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserTableViewColumnProperty"));
582
583 // OK, first we need to find the first visible item we have -
584 // this will be the "low" for our binary search. There is no real
585 // easy way around this, as we will need to do a SLOW linear search
586 // until we find a visible item, but we can do a cheap calculation
587 // via the row height to speed things up a bit
588 UInt32 scrollx, scrolly;
589 err = GetDataBrowserScrollPosition(m_peer->GetControlRef(), &scrollx, &scrolly);
590 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserScrollPosition"));
591
592 UInt16 height;
593 err = GetDataBrowserTableViewRowHeight(m_peer->GetControlRef(), &height);
594 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserTableViewRowHeight"));
595
596 // these indices are 0-based, as usual, so we need to add 1 to them when
597 // passing them to data browser functions which use 1-based indices
598 int low = scrolly / height,
599 high = GetCount() - 1;
600
601 // search for the first visible item (note that the scroll guess above
602 // is the low bounds of where the item might lie so we only use that as a
603 // starting point - we should reach it within 1 or 2 iterations of the loop)
604 while ( low <= high )
605 {
606 Rect bounds;
607 err = GetDataBrowserItemPartBounds(
608 m_peer->GetControlRef(), low + 1, colId,
609 kDataBrowserPropertyEnclosingPart,
610 &bounds); // note +1 to translate to Mac ID
611 if ( err == noErr )
612 break;
613
614 // errDataBrowserItemNotFound is expected as it simply means that the
615 // item is not currently visible -- but other errors are not
616 wxCHECK_MSG( err == errDataBrowserItemNotFound, wxNOT_FOUND,
617 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
618
619 low++;
620 }
621
622 // NOW do a binary search for where the item lies, searching low again if
623 // we hit an item that isn't visible
624 while ( low <= high )
625 {
626 int mid = (low + high) / 2;
627
628 Rect bounds;
629 err = GetDataBrowserItemPartBounds(
630 m_peer->GetControlRef(), mid + 1, colId,
631 kDataBrowserPropertyEnclosingPart,
632 &bounds); //note +1 to trans to mac id
633 wxCHECK_MSG( err == noErr || err == errDataBrowserItemNotFound,
634 wxNOT_FOUND,
635 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
636
637 if ( err == errDataBrowserItemNotFound )
638 {
639 // item not visible, attempt to find a visible one
640 // search lower
641 high = mid - 1;
642 }
643 else // visible item, do actual hitttest
644 {
645 // if point is within the bounds, return this item (since we assume
646 // all x coords of items are equal we only test the x coord in
647 // equality)
648 if ((point.x >= bounds.left && point.x <= bounds.right) &&
649 (point.y >= bounds.top && point.y <= bounds.bottom) )
650 {
651 // found!
652 return mid;
653 }
654
655 if ( point.y < bounds.top )
656 // index(bounds) greater then key(point)
657 high = mid - 1;
658 else
659 // index(bounds) less then key(point)
660 low = mid + 1;
661 }
662 }
663#endif
664 return wxNOT_FOUND;
665}
666
667#endif // wxUSE_LISTBOX