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