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