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