]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/listbox.mm
s/wxSplitPath/wxFileName::SplitPath
[wxWidgets.git] / src / osx / cocoa / listbox.mm
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
33 class 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
69 class 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
82 class WXDLLIMPEXP_CORE wxCocoaTableColumn : public wxListWidgetColumn
83 {
84 public :
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
98 protected :
99 wxNSTableColumn* m_column;
100 bool m_editable;
101 } ;
102
103 NSString* column1 = @"1";
104
105 class wxListWidgetCocoaImpl : public wxWidgetCocoaImpl, public wxListWidgetImpl
106 {
107 public :
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);
148 protected :
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
179 class wxNSTableViewCellValue : public wxListWidgetCellValue
180 {
181 public :
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
217 protected:
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
292 wxListWidgetCocoaImpl::wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data ) :
293 wxWidgetCocoaImpl( peer, view ), m_tableView(tableview), m_dataSource(data)
294 {
295 }
296
297 wxListWidgetCocoaImpl::~wxListWidgetCocoaImpl()
298 {
299 [m_dataSource release];
300 }
301
302 unsigned int wxListWidgetCocoaImpl::ListGetCount() const
303 {
304 wxListBox* lb = dynamic_cast<wxListBox*> ( GetWXPeer() );
305 return lb->GetCount();
306 }
307
308 //
309 // columns
310 //
311
312 wxListWidgetColumn* 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
339 wxListWidgetColumn* 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
378 void 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
402 void wxListWidgetCocoaImpl::ListDelete( unsigned int n )
403 {
404 [m_tableView reloadData];
405 }
406
407 void wxListWidgetCocoaImpl::ListClear()
408 {
409 [m_tableView reloadData];
410 }
411
412 // selecting
413
414 void wxListWidgetCocoaImpl::ListDeselectAll()
415 {
416 [m_tableView deselectAll:nil];
417 }
418
419 void 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
429 int wxListWidgetCocoaImpl::ListGetSelection() const
430 {
431 return 0;
432 }
433
434 int wxListWidgetCocoaImpl::ListGetSelections( wxArrayInt& aSelections ) const
435 {
436 return 0;
437 }
438
439 bool wxListWidgetCocoaImpl::ListIsSelected( unsigned int n ) const
440 {
441 return false;
442 }
443
444 // display
445
446 void wxListWidgetCocoaImpl::ListScrollTo( unsigned int n )
447 {
448 [m_tableView scrollRowToVisible:n];
449 }
450
451
452 void wxListWidgetCocoaImpl::UpdateLine( unsigned int n, wxListWidgetColumn* col )
453 {
454 // TODO optimize
455 [m_tableView reloadData];
456 }
457
458 void wxListWidgetCocoaImpl::UpdateLineToEnd( unsigned int n)
459 {
460 // TODO optimize
461 [m_tableView reloadData];
462 }
463
464
465 // accessing content
466
467
468 wxWidgetImplType* 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 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
477 NSScrollView* scrollview = [[NSScrollView alloc] initWithFrame:r];
478
479 // use same scroll flags logic as msw
480
481 [scrollview setHasVerticalScroller:YES];
482
483 if ( style & wxLB_HSCROLL )
484 [scrollview setHasHorizontalScroller:YES];
485
486 [scrollview setAutohidesScrollers: ((style & wxLB_ALWAYS_SB) ? NO : YES)];
487
488 // setting up the true table
489
490 wxNSTableView* tableview = [[wxNSTableView alloc] init];
491 [scrollview setDocumentView:tableview];
492 [tableview release];
493
494 // only one multi-select mode available
495 if ( (style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE) )
496 [tableview setAllowsMultipleSelection:YES];
497
498 // simple listboxes have no header row
499 [tableview setHeaderView:nil];
500
501 [tableview setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle];
502 wxNSTableDataSource* ds = [[ wxNSTableDataSource alloc] init];
503 [tableview setDataSource:ds];
504 wxListWidgetCocoaImpl* c = new wxListWidgetCocoaImpl( wxpeer, scrollview, tableview, ds );
505 [tableview setImplementation:c];
506 [ds setImplementation:c];
507 return c;
508 }
509
510 int wxListBox::DoListHitTest(const wxPoint& inpoint) const
511 {
512 #if wxOSX_USE_CARBON
513 OSStatus err;
514
515 // There are few reasons why this is complicated:
516 // 1) There is no native HitTest function for Mac
517 // 2) GetDataBrowserItemPartBounds only works on visible items
518 // 3) We can't do it through GetDataBrowserTableView[Item]RowHeight
519 // because what it returns is basically inaccurate in the context
520 // of the coordinates we want here, but we use this as a guess
521 // for where the first visible item lies
522
523 wxPoint point = inpoint;
524
525 // get column property ID (req. for call to itempartbounds)
526 DataBrowserTableViewColumnID colId = 0;
527 err = GetDataBrowserTableViewColumnProperty(m_peer->GetControlRef(), 0, &colId);
528 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserTableViewColumnProperty"));
529
530 // OK, first we need to find the first visible item we have -
531 // this will be the "low" for our binary search. There is no real
532 // easy way around this, as we will need to do a SLOW linear search
533 // until we find a visible item, but we can do a cheap calculation
534 // via the row height to speed things up a bit
535 UInt32 scrollx, scrolly;
536 err = GetDataBrowserScrollPosition(m_peer->GetControlRef(), &scrollx, &scrolly);
537 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserScrollPosition"));
538
539 UInt16 height;
540 err = GetDataBrowserTableViewRowHeight(m_peer->GetControlRef(), &height);
541 wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserTableViewRowHeight"));
542
543 // these indices are 0-based, as usual, so we need to add 1 to them when
544 // passing them to data browser functions which use 1-based indices
545 int low = scrolly / height,
546 high = GetCount() - 1;
547
548 // search for the first visible item (note that the scroll guess above
549 // is the low bounds of where the item might lie so we only use that as a
550 // starting point - we should reach it within 1 or 2 iterations of the loop)
551 while ( low <= high )
552 {
553 Rect bounds;
554 err = GetDataBrowserItemPartBounds(
555 m_peer->GetControlRef(), low + 1, colId,
556 kDataBrowserPropertyEnclosingPart,
557 &bounds); // note +1 to translate to Mac ID
558 if ( err == noErr )
559 break;
560
561 // errDataBrowserItemNotFound is expected as it simply means that the
562 // item is not currently visible -- but other errors are not
563 wxCHECK_MSG( err == errDataBrowserItemNotFound, wxNOT_FOUND,
564 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
565
566 low++;
567 }
568
569 // NOW do a binary search for where the item lies, searching low again if
570 // we hit an item that isn't visible
571 while ( low <= high )
572 {
573 int mid = (low + high) / 2;
574
575 Rect bounds;
576 err = GetDataBrowserItemPartBounds(
577 m_peer->GetControlRef(), mid + 1, colId,
578 kDataBrowserPropertyEnclosingPart,
579 &bounds); //note +1 to trans to mac id
580 wxCHECK_MSG( err == noErr || err == errDataBrowserItemNotFound,
581 wxNOT_FOUND,
582 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
583
584 if ( err == errDataBrowserItemNotFound )
585 {
586 // item not visible, attempt to find a visible one
587 // search lower
588 high = mid - 1;
589 }
590 else // visible item, do actual hitttest
591 {
592 // if point is within the bounds, return this item (since we assume
593 // all x coords of items are equal we only test the x coord in
594 // equality)
595 if ((point.x >= bounds.left && point.x <= bounds.right) &&
596 (point.y >= bounds.top && point.y <= bounds.bottom) )
597 {
598 // found!
599 return mid;
600 }
601
602 if ( point.y < bounds.top )
603 // index(bounds) greater then key(point)
604 high = mid - 1;
605 else
606 // index(bounds) less then key(point)
607 low = mid + 1;
608 }
609 }
610 #endif
611 return wxNOT_FOUND;
612 }
613
614 #endif // wxUSE_LISTBOX