]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/listbox.mm
temporary workaround
[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$
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 virtual int DoListHitTest( const wxPoint& inpoint ) const;
139
140 int ListGetColumnType( int col )
141 {
142 return col;
143 }
144 virtual void UpdateLine( unsigned int n, wxListWidgetColumn* col = NULL ) ;
145 virtual void UpdateLineToEnd( unsigned int n);
146
147 virtual void controlDoubleAction(WXWidget slf, void* _cmd, void *sender);
148
149
150 protected :
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 self = [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
181 class wxNSTableViewCellValue : public wxListWidgetCellValue
182 {
183 public :
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::AsString( (NSString*) value );
215
216 return wxEmptyString;
217 }
218
219 protected:
220 id& value;
221 } ;
222
223 @implementation wxNSTableDataSource
224
225 - (id) init
226 {
227 self = [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 wxUnusedVar(aTableView);
245 if ( impl )
246 return impl->ListGetCount();
247 return 0;
248 }
249
250 - (id)tableView:(NSTableView *)aTableView
251 objectValueForTableColumn:(NSTableColumn *)aTableColumn
252 row:(NSInteger)rowIndex
253 {
254 wxUnusedVar(aTableView);
255 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
256 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
257 wxCocoaTableColumn* col = [tablecol column];
258 id value = nil;
259 wxNSTableViewCellValue cellvalue(value);
260 lb->GetValueCallback(rowIndex, col, cellvalue);
261 return value;
262 }
263
264 - (void)tableView:(NSTableView *)aTableView
265 setObjectValue:(id)value forTableColumn:(NSTableColumn *)aTableColumn
266 row:(NSInteger)rowIndex
267 {
268 wxUnusedVar(aTableView);
269 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
270 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
271 wxCocoaTableColumn* col = [tablecol column];
272 wxNSTableViewCellValue cellvalue(value);
273 lb->SetValueCallback(rowIndex, col, cellvalue);
274 }
275
276 @end
277
278 @implementation wxNSTableView
279
280 + (void)initialize
281 {
282 static BOOL initialized = NO;
283 if (!initialized)
284 {
285 initialized = YES;
286 wxOSXCocoaClassAddWXMethods( self );
287 }
288 }
289
290 - (void) tableViewSelectionDidChange: (NSNotification *) notification
291 {
292 wxUnusedVar(notification);
293
294 int row = [self selectedRow];
295
296 if (row == -1)
297 {
298 // no row selected
299 }
300 else
301 {
302 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
303 wxListBox *list = static_cast<wxListBox*> ( impl->GetWXPeer());
304 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
305
306 wxCommandEvent event( wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() );
307
308 if ((row < 0) || (row > (int) list->GetCount())) // OS X can select an item below the last item
309 return;
310
311 if ( !list->MacGetBlockEvents() )
312 list->HandleLineEvent( row, false );
313 }
314
315 }
316
317 - (void)setFont:(NSFont *)aFont
318 {
319 NSArray *tableColumns = [self tableColumns];
320 unsigned int columnIndex = [tableColumns count];
321 while (columnIndex--)
322 [[(NSTableColumn *)[tableColumns objectAtIndex:columnIndex] dataCell] setFont:aFont];
323
324 // todo introduce a common NSLayoutManager instance for all and use new method
325 #ifndef __LP64__
326 [self setRowHeight:[aFont defaultLineHeightForFont]+2];
327 #endif
328 }
329
330 - (void) setControlSize:(NSControlSize) size
331 {
332 NSArray *tableColumns = [self tableColumns];
333 unsigned int columnIndex = [tableColumns count];
334 while (columnIndex--)
335 [[(NSTableColumn *)[tableColumns objectAtIndex:columnIndex] dataCell] setControlSize:size];
336 }
337
338 @end
339
340 //
341 //
342 //
343
344 wxListWidgetCocoaImpl::wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data ) :
345 wxWidgetCocoaImpl( peer, view ), m_tableView(tableview), m_dataSource(data)
346 {
347 InstallEventHandler( tableview );
348 }
349
350 wxListWidgetCocoaImpl::~wxListWidgetCocoaImpl()
351 {
352 [m_dataSource release];
353 }
354
355 unsigned int wxListWidgetCocoaImpl::ListGetCount() const
356 {
357 wxListBox* lb = dynamic_cast<wxListBox*> ( GetWXPeer() );
358 return lb->GetCount();
359 }
360
361 //
362 // columns
363 //
364
365 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertTextColumn( unsigned pos, const wxString& WXUNUSED(title), bool editable,
366 wxAlignment WXUNUSED(just), int defaultWidth)
367 {
368 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
369 [col1 setEditable:editable];
370
371 unsigned formerColCount = [m_tableView numberOfColumns];
372
373 // there's apparently no way to insert at a specific position
374 [m_tableView addTableColumn:col1 ];
375 if ( pos < formerColCount )
376 [m_tableView moveColumn:formerColCount toColumn:pos];
377
378 if ( defaultWidth >= 0 )
379 {
380 [col1 setMaxWidth:defaultWidth];
381 [col1 setMinWidth:defaultWidth];
382 [col1 setWidth:defaultWidth];
383 }
384 else
385 {
386 [col1 setMaxWidth:1000];
387 [col1 setMinWidth:10];
388 // temporary hack, because I cannot get the automatic column resizing
389 // to work properly
390 [col1 setWidth:1000];
391 }
392 [col1 setResizingMask: NSTableColumnAutoresizingMask];
393
394 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
395 if ( list != NULL )
396 [[col1 dataCell] setFont:list->GetFont().OSXGetNSFont()];
397
398 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
399 [col1 setColumn:wxcol];
400
401 // owned by the tableview
402 [col1 release];
403 return wxcol;
404 }
405
406 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertCheckColumn( unsigned pos , const wxString& WXUNUSED(title), bool editable,
407 wxAlignment WXUNUSED(just), int defaultWidth )
408 {
409 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
410 [col1 setEditable:editable];
411
412 // set your custom cell & set it up
413 NSButtonCell* checkbox = [[NSButtonCell alloc] init];
414 [checkbox setTitle:@""];
415 [checkbox setButtonType:NSSwitchButton];
416 [col1 setDataCell:checkbox] ;
417
418 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
419 if ( list != NULL )
420 {
421 NSControlSize size = NSRegularControlSize;
422
423 switch ( list->GetWindowVariant() )
424 {
425 case wxWINDOW_VARIANT_NORMAL :
426 size = NSRegularControlSize;
427 break ;
428
429 case wxWINDOW_VARIANT_SMALL :
430 size = NSSmallControlSize;
431 break ;
432
433 case wxWINDOW_VARIANT_MINI :
434 size = NSMiniControlSize;
435 break ;
436
437 case wxWINDOW_VARIANT_LARGE :
438 size = NSRegularControlSize;
439 break ;
440
441 default:
442 break ;
443 }
444
445 [[col1 dataCell] setControlSize:size];
446 // although there is no text, it may help to get the correct vertical layout
447 [[col1 dataCell] setFont:list->GetFont().OSXGetNSFont()];
448 }
449
450 [checkbox release];
451
452 unsigned formerColCount = [m_tableView numberOfColumns];
453
454 // there's apparently no way to insert at a specific position
455 [m_tableView addTableColumn:col1 ];
456 if ( pos < formerColCount )
457 [m_tableView moveColumn:formerColCount toColumn:pos];
458
459 if ( defaultWidth >= 0 )
460 {
461 [col1 setMaxWidth:defaultWidth];
462 [col1 setMinWidth:defaultWidth];
463 [col1 setWidth:defaultWidth];
464 }
465
466 [col1 setResizingMask: NSTableColumnNoResizing];
467 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
468 [col1 setColumn:wxcol];
469
470 // owned by the tableview
471 [col1 release];
472 return wxcol;
473 }
474
475
476 //
477 // inserting / removing lines
478 //
479
480 void wxListWidgetCocoaImpl::ListInsert( unsigned int WXUNUSED(n) )
481 {
482 [m_tableView reloadData];
483 }
484
485 void wxListWidgetCocoaImpl::ListDelete( unsigned int WXUNUSED(n) )
486 {
487 [m_tableView reloadData];
488 }
489
490 void wxListWidgetCocoaImpl::ListClear()
491 {
492 [m_tableView reloadData];
493 }
494
495 // selecting
496
497 void wxListWidgetCocoaImpl::ListDeselectAll()
498 {
499 [m_tableView deselectAll:nil];
500 }
501
502 void wxListWidgetCocoaImpl::ListSetSelection( unsigned int n, bool select, bool multi )
503 {
504 // TODO
505 if ( select )
506 [m_tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:n]
507 byExtendingSelection:multi];
508 else
509 [m_tableView deselectRow: n];
510
511 }
512
513 int wxListWidgetCocoaImpl::ListGetSelection() const
514 {
515 return [m_tableView selectedRow];
516 }
517
518 int wxListWidgetCocoaImpl::ListGetSelections( wxArrayInt& aSelections ) const
519 {
520 aSelections.Empty();
521
522 int count = ListGetCount();
523
524 for ( int i = 0; i < count; ++i)
525 {
526 if ([m_tableView isRowSelected:i])
527 aSelections.Add(i);
528 }
529
530 return aSelections.Count();
531 }
532
533 bool wxListWidgetCocoaImpl::ListIsSelected( unsigned int n ) const
534 {
535 return [m_tableView isRowSelected:n];
536 }
537
538 // display
539
540 void wxListWidgetCocoaImpl::ListScrollTo( unsigned int n )
541 {
542 [m_tableView scrollRowToVisible:n];
543 }
544
545
546 void wxListWidgetCocoaImpl::UpdateLine( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) )
547 {
548 // TODO optimize
549 [m_tableView reloadData];
550 }
551
552 void wxListWidgetCocoaImpl::UpdateLineToEnd( unsigned int WXUNUSED(n))
553 {
554 // TODO optimize
555 [m_tableView reloadData];
556 }
557
558 void wxListWidgetCocoaImpl::controlDoubleAction(WXWidget WXUNUSED(slf),void* WXUNUSED(_cmd), void *WXUNUSED(sender))
559 {
560 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
561 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
562
563 int sel = [m_tableView clickedRow];
564 if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?)
565 return;
566
567 list->HandleLineEvent( sel, true );
568 }
569
570 // accessing content
571
572
573 wxWidgetImplType* wxWidgetImpl::CreateListBox( wxWindowMac* wxpeer,
574 wxWindowMac* WXUNUSED(parent),
575 wxWindowID WXUNUSED(id),
576 const wxPoint& pos,
577 const wxSize& size,
578 long style,
579 long WXUNUSED(extraStyle))
580 {
581 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
582 NSScrollView* scrollview = [[NSScrollView alloc] initWithFrame:r];
583
584 // use same scroll flags logic as msw
585
586 [scrollview setHasVerticalScroller:YES];
587
588 if ( style & wxLB_HSCROLL )
589 [scrollview setHasHorizontalScroller:YES];
590
591 [scrollview setAutohidesScrollers: ((style & wxLB_ALWAYS_SB) ? NO : YES)];
592
593 // setting up the true table
594
595 wxNSTableView* tableview = [[wxNSTableView alloc] init];
596 [tableview setDelegate:tableview];
597 // only one multi-select mode available
598 if ( (style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE) )
599 [tableview setAllowsMultipleSelection:YES];
600
601 // simple listboxes have no header row
602 [tableview setHeaderView:nil];
603
604 if ( style & wxLB_HSCROLL )
605 [tableview setColumnAutoresizingStyle:NSTableViewNoColumnAutoresizing];
606 else
607 [tableview setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle];
608
609 wxNSTableDataSource* ds = [[ wxNSTableDataSource alloc] init];
610 [tableview setDataSource:ds];
611 [scrollview setDocumentView:tableview];
612 [tableview release];
613
614 wxListWidgetCocoaImpl* c = new wxListWidgetCocoaImpl( wxpeer, scrollview, tableview, ds );
615
616 // temporary hook for dnd
617 // [tableview registerForDraggedTypes:[NSArray arrayWithObjects:
618 // NSStringPboardType, NSFilenamesPboardType, (NSString*) kPasteboardTypeFileURLPromise, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
619
620 [ds setImplementation:c];
621 return c;
622 }
623
624 int wxListWidgetCocoaImpl::DoListHitTest(const wxPoint& inpoint) const
625 {
626 // translate inpoint to listpoint via scrollview
627 NSPoint p = wxToNSPoint( m_osxView, inpoint );
628 p = [m_osxView convertPoint:p toView:m_tableView];
629 // hittest using new point
630 NSInteger i = [m_tableView rowAtPoint:p];
631 return i;
632 }
633
634 #endif // wxUSE_LISTBOX