]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/listbox.mm
wxGTK1 : another msiing include file in evtloop.cpp
[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 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #if wxUSE_LISTBOX
14
15 #include "wx/listbox.h"
16 #include "wx/dnd.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 wxOSX_10_6_AND_LATER(<NSTableViewDataSource>)
36 {
37 wxListWidgetCocoaImpl* impl;
38 }
39
40 - (id)tableView:(NSTableView *)aTableView
41 objectValueForTableColumn:(NSTableColumn *)aTableColumn
42 row:(NSInteger)rowIndex;
43
44 - (void)tableView:(NSTableView *)aTableView
45 setObjectValue:(id)value forTableColumn:(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 wxOSX_10_6_AND_LATER(<NSTableViewDelegate>)
56 {
57 }
58
59 @end
60
61 //
62 // table column
63 //
64
65 class wxCocoaTableColumn;
66
67 @interface wxNSTableColumn : NSTableColumn
68 {
69 wxCocoaTableColumn* column;
70 }
71
72 - (void) setColumn: (wxCocoaTableColumn*) col;
73
74 - (wxCocoaTableColumn*) column;
75
76 @end
77
78 class WXDLLIMPEXP_CORE wxCocoaTableColumn : public wxListWidgetColumn
79 {
80 public :
81 wxCocoaTableColumn( wxNSTableColumn* column, bool editable )
82 : m_column( column ), m_editable(editable)
83 {
84 }
85
86 ~wxCocoaTableColumn()
87 {
88 }
89
90 wxNSTableColumn* GetNSTableColumn() const { return m_column ; }
91
92 bool IsEditable() const { return m_editable; }
93
94 protected :
95 wxNSTableColumn* m_column;
96 bool m_editable;
97 } ;
98
99 NSString* column1 = @"1";
100
101 class wxListWidgetCocoaImpl : public wxWidgetCocoaImpl, public wxListWidgetImpl
102 {
103 public :
104 wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data );
105
106 ~wxListWidgetCocoaImpl();
107
108 virtual wxListWidgetColumn* InsertTextColumn( unsigned pos, const wxString& title, bool editable = false,
109 wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ;
110 virtual wxListWidgetColumn* InsertCheckColumn( unsigned pos , const wxString& title, bool editable = false,
111 wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ;
112
113 // add and remove
114
115 virtual void ListDelete( unsigned int n ) ;
116 virtual void ListInsert( unsigned int n ) ;
117 virtual void ListClear() ;
118
119 // selecting
120
121 virtual void ListDeselectAll();
122
123 virtual void ListSetSelection( unsigned int n, bool select, bool multi ) ;
124 virtual int ListGetSelection() const ;
125
126 virtual int ListGetSelections( wxArrayInt& aSelections ) const ;
127
128 virtual bool ListIsSelected( unsigned int n ) const ;
129
130 // display
131
132 virtual void ListScrollTo( unsigned int n ) ;
133
134 // accessing content
135
136 virtual unsigned int ListGetCount() const ;
137 virtual int DoListHitTest( const wxPoint& inpoint ) 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
149 protected :
150 wxNSTableView* m_tableView ;
151
152 wxNSTableDataSource* m_dataSource;
153 } ;
154
155 //
156 // implementations
157 //
158
159 @implementation wxNSTableColumn
160
161 - (id) init
162 {
163 self = [super init];
164 column = nil;
165 return self;
166 }
167
168 - (void) setColumn: (wxCocoaTableColumn*) col
169 {
170 column = col;
171 }
172
173 - (wxCocoaTableColumn*) column
174 {
175 return column;
176 }
177
178 @end
179
180 class wxNSTableViewCellValue : public wxListWidgetCellValue
181 {
182 public :
183 wxNSTableViewCellValue( id &v ) : value(v)
184 {
185 }
186
187 virtual ~wxNSTableViewCellValue() {}
188
189 virtual void Set( CFStringRef v )
190 {
191 value = [[(NSString*)v retain] autorelease];
192 }
193 virtual void Set( const wxString& value )
194 {
195 Set( (CFStringRef) wxCFStringRef( value ) );
196 }
197 virtual void Set( int v )
198 {
199 value = [NSNumber numberWithInt:v];
200 }
201
202 virtual int GetIntValue() const
203 {
204 if ( [value isKindOfClass:[NSNumber class]] )
205 return [ (NSNumber*) value intValue ];
206
207 return 0;
208 }
209
210 virtual wxString GetStringValue() const
211 {
212 if ( [value isKindOfClass:[NSString class]] )
213 return wxCFStringRef::AsString( (NSString*) value );
214
215 return wxEmptyString;
216 }
217
218 protected:
219 id& value;
220 } ;
221
222 @implementation wxNSTableDataSource
223
224 - (id) init
225 {
226 self = [super init];
227 impl = nil;
228 return self;
229 }
230
231 - (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation
232 {
233 impl = theImplementation;
234 }
235
236 - (wxListWidgetCocoaImpl*) implementation
237 {
238 return impl;
239 }
240
241 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
242 {
243 wxUnusedVar(aTableView);
244 if ( impl )
245 return impl->ListGetCount();
246 return 0;
247 }
248
249 - (id)tableView:(NSTableView *)aTableView
250 objectValueForTableColumn:(NSTableColumn *)aTableColumn
251 row:(NSInteger)rowIndex
252 {
253 wxUnusedVar(aTableView);
254 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
255 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
256 wxCocoaTableColumn* col = [tablecol column];
257 id value = nil;
258 wxNSTableViewCellValue cellvalue(value);
259 lb->GetValueCallback(rowIndex, col, cellvalue);
260 return value;
261 }
262
263 - (void)tableView:(NSTableView *)aTableView
264 setObjectValue:(id)value forTableColumn:(NSTableColumn *)aTableColumn
265 row:(NSInteger)rowIndex
266 {
267 wxUnusedVar(aTableView);
268 wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn;
269 wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer());
270 wxCocoaTableColumn* col = [tablecol column];
271 wxNSTableViewCellValue cellvalue(value);
272 lb->SetValueCallback(rowIndex, col, cellvalue);
273 }
274
275 @end
276
277 @implementation wxNSTableView
278
279 + (void)initialize
280 {
281 static BOOL initialized = NO;
282 if (!initialized)
283 {
284 initialized = YES;
285 wxOSXCocoaClassAddWXMethods( self );
286 }
287 }
288
289 - (void) tableViewSelectionDidChange: (NSNotification *) notification
290 {
291 wxUnusedVar(notification);
292
293 int row = [self selectedRow];
294
295 if (row == -1)
296 {
297 // no row selected
298 }
299 else
300 {
301 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
302 wxListBox *list = static_cast<wxListBox*> ( impl->GetWXPeer());
303 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
304
305 wxCommandEvent event( wxEVT_LISTBOX, list->GetId() );
306
307 if ((row < 0) || (row > (int) list->GetCount())) // OS X can select an item below the last item
308 return;
309
310 if ( !list->MacGetBlockEvents() )
311 list->HandleLineEvent( row, false );
312 }
313
314 }
315
316 - (void)setFont:(NSFont *)aFont
317 {
318 NSArray *tableColumns = [self tableColumns];
319 unsigned int columnIndex = [tableColumns count];
320 while (columnIndex--)
321 [[(NSTableColumn *)[tableColumns objectAtIndex:columnIndex] dataCell] setFont:aFont];
322
323 [self setRowHeight:[gNSLayoutManager defaultLineHeightForFont:aFont]+2];
324 }
325
326 - (void) setControlSize:(NSControlSize) size
327 {
328 NSArray *tableColumns = [self tableColumns];
329 unsigned int columnIndex = [tableColumns count];
330 while (columnIndex--)
331 [[(NSTableColumn *)[tableColumns objectAtIndex:columnIndex] dataCell] setControlSize:size];
332 }
333
334 @end
335
336 //
337 //
338 //
339
340 wxListWidgetCocoaImpl::wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data ) :
341 wxWidgetCocoaImpl( peer, view ), m_tableView(tableview), m_dataSource(data)
342 {
343 InstallEventHandler( tableview );
344 }
345
346 wxListWidgetCocoaImpl::~wxListWidgetCocoaImpl()
347 {
348 [m_dataSource release];
349 }
350
351 unsigned int wxListWidgetCocoaImpl::ListGetCount() const
352 {
353 wxListBox* lb = dynamic_cast<wxListBox*> ( GetWXPeer() );
354 return lb->GetCount();
355 }
356
357 //
358 // columns
359 //
360
361 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertTextColumn( unsigned pos, const wxString& WXUNUSED(title), bool editable,
362 wxAlignment WXUNUSED(just), int defaultWidth)
363 {
364 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
365 [col1 setEditable:editable];
366
367 unsigned formerColCount = [m_tableView numberOfColumns];
368
369 // there's apparently no way to insert at a specific position
370 [m_tableView addTableColumn:col1 ];
371 if ( pos < formerColCount )
372 [m_tableView moveColumn:formerColCount toColumn:pos];
373
374 if ( defaultWidth >= 0 )
375 {
376 [col1 setMaxWidth:defaultWidth];
377 [col1 setMinWidth:defaultWidth];
378 [col1 setWidth:defaultWidth];
379 }
380 else
381 {
382 [col1 setMaxWidth:1000];
383 [col1 setMinWidth:10];
384 // temporary hack, because I cannot get the automatic column resizing
385 // to work properly
386 [col1 setWidth:1000];
387 }
388 [col1 setResizingMask: NSTableColumnAutoresizingMask];
389
390 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
391 if ( list != NULL )
392 [[col1 dataCell] setFont:list->GetFont().OSXGetNSFont()];
393
394 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
395 [col1 setColumn:wxcol];
396
397 // owned by the tableview
398 [col1 release];
399 return wxcol;
400 }
401
402 wxListWidgetColumn* wxListWidgetCocoaImpl::InsertCheckColumn( unsigned pos , const wxString& WXUNUSED(title), bool editable,
403 wxAlignment WXUNUSED(just), int defaultWidth )
404 {
405 wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init];
406 [col1 setEditable:editable];
407
408 // set your custom cell & set it up
409 NSButtonCell* checkbox = [[NSButtonCell alloc] init];
410 [checkbox setTitle:@""];
411 [checkbox setButtonType:NSSwitchButton];
412 [col1 setDataCell:checkbox] ;
413
414 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
415 if ( list != NULL )
416 {
417 NSControlSize size = NSRegularControlSize;
418
419 switch ( list->GetWindowVariant() )
420 {
421 case wxWINDOW_VARIANT_NORMAL :
422 size = NSRegularControlSize;
423 break ;
424
425 case wxWINDOW_VARIANT_SMALL :
426 size = NSSmallControlSize;
427 break ;
428
429 case wxWINDOW_VARIANT_MINI :
430 size = NSMiniControlSize;
431 break ;
432
433 case wxWINDOW_VARIANT_LARGE :
434 size = NSRegularControlSize;
435 break ;
436
437 default:
438 break ;
439 }
440
441 [[col1 dataCell] setControlSize:size];
442 // although there is no text, it may help to get the correct vertical layout
443 [[col1 dataCell] setFont:list->GetFont().OSXGetNSFont()];
444 }
445
446 [checkbox release];
447
448 unsigned formerColCount = [m_tableView numberOfColumns];
449
450 // there's apparently no way to insert at a specific position
451 [m_tableView addTableColumn:col1 ];
452 if ( pos < formerColCount )
453 [m_tableView moveColumn:formerColCount toColumn:pos];
454
455 if ( defaultWidth >= 0 )
456 {
457 [col1 setMaxWidth:defaultWidth];
458 [col1 setMinWidth:defaultWidth];
459 [col1 setWidth:defaultWidth];
460 }
461
462 [col1 setResizingMask: NSTableColumnNoResizing];
463 wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable );
464 [col1 setColumn:wxcol];
465
466 // owned by the tableview
467 [col1 release];
468 return wxcol;
469 }
470
471
472 //
473 // inserting / removing lines
474 //
475
476 void wxListWidgetCocoaImpl::ListInsert( unsigned int WXUNUSED(n) )
477 {
478 [m_tableView reloadData];
479 }
480
481 void wxListWidgetCocoaImpl::ListDelete( unsigned int WXUNUSED(n) )
482 {
483 [m_tableView reloadData];
484 }
485
486 void wxListWidgetCocoaImpl::ListClear()
487 {
488 [m_tableView reloadData];
489 }
490
491 // selecting
492
493 void wxListWidgetCocoaImpl::ListDeselectAll()
494 {
495 [m_tableView deselectAll:nil];
496 }
497
498 void wxListWidgetCocoaImpl::ListSetSelection( unsigned int n, bool select, bool multi )
499 {
500 // TODO
501 if ( select )
502 [m_tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:n]
503 byExtendingSelection:multi];
504 else
505 [m_tableView deselectRow: n];
506
507 }
508
509 int wxListWidgetCocoaImpl::ListGetSelection() const
510 {
511 return [m_tableView selectedRow];
512 }
513
514 int wxListWidgetCocoaImpl::ListGetSelections( wxArrayInt& aSelections ) const
515 {
516 aSelections.Empty();
517
518 int count = ListGetCount();
519
520 for ( int i = 0; i < count; ++i)
521 {
522 if ([m_tableView isRowSelected:i])
523 aSelections.Add(i);
524 }
525
526 return aSelections.Count();
527 }
528
529 bool wxListWidgetCocoaImpl::ListIsSelected( unsigned int n ) const
530 {
531 return [m_tableView isRowSelected:n];
532 }
533
534 // display
535
536 void wxListWidgetCocoaImpl::ListScrollTo( unsigned int n )
537 {
538 [m_tableView scrollRowToVisible:n];
539 }
540
541
542 void wxListWidgetCocoaImpl::UpdateLine( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) )
543 {
544 // TODO optimize
545 [m_tableView reloadData];
546 }
547
548 void wxListWidgetCocoaImpl::UpdateLineToEnd( unsigned int WXUNUSED(n))
549 {
550 // TODO optimize
551 [m_tableView reloadData];
552 }
553
554 void wxListWidgetCocoaImpl::controlDoubleAction(WXWidget WXUNUSED(slf),void* WXUNUSED(_cmd), void *WXUNUSED(sender))
555 {
556 wxListBox *list = static_cast<wxListBox*> ( GetWXPeer());
557 wxCHECK_RET( list != NULL , wxT("Listbox expected"));
558
559 int sel = [m_tableView clickedRow];
560 if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?)
561 return;
562
563 list->HandleLineEvent( sel, true );
564 }
565
566 // accessing content
567
568
569 wxWidgetImplType* wxWidgetImpl::CreateListBox( wxWindowMac* wxpeer,
570 wxWindowMac* WXUNUSED(parent),
571 wxWindowID WXUNUSED(id),
572 const wxPoint& pos,
573 const wxSize& size,
574 long style,
575 long WXUNUSED(extraStyle))
576 {
577 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
578 NSScrollView* scrollview = [[NSScrollView alloc] initWithFrame:r];
579
580 // use same scroll flags logic as msw
581
582 [scrollview setHasVerticalScroller:YES];
583
584 if ( style & wxLB_HSCROLL )
585 [scrollview setHasHorizontalScroller:YES];
586
587 [scrollview setAutohidesScrollers: ((style & wxLB_ALWAYS_SB) ? NO : YES)];
588
589 // setting up the true table
590
591 wxNSTableView* tableview = [[wxNSTableView alloc] init];
592 [tableview setDelegate:tableview];
593 // only one multi-select mode available
594 if ( (style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE) )
595 [tableview setAllowsMultipleSelection:YES];
596
597 // simple listboxes have no header row
598 [tableview setHeaderView:nil];
599
600 if ( style & wxLB_HSCROLL )
601 [tableview setColumnAutoresizingStyle:NSTableViewNoColumnAutoresizing];
602 else
603 [tableview setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle];
604
605 wxNSTableDataSource* ds = [[ wxNSTableDataSource alloc] init];
606 [tableview setDataSource:ds];
607 [scrollview setDocumentView:tableview];
608 [tableview release];
609
610 wxListWidgetCocoaImpl* c = new wxListWidgetCocoaImpl( wxpeer, scrollview, tableview, ds );
611
612 // temporary hook for dnd
613 // [tableview registerForDraggedTypes:[NSArray arrayWithObjects:
614 // NSStringPboardType, NSFilenamesPboardType, (NSString*) kPasteboardTypeFileURLPromise, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
615
616 [ds setImplementation:c];
617 return c;
618 }
619
620 int wxListWidgetCocoaImpl::DoListHitTest(const wxPoint& inpoint) const
621 {
622 // translate inpoint to listpoint via scrollview
623 NSPoint p = wxToNSPoint( m_osxView, inpoint );
624 p = [m_osxView convertPoint:p toView:m_tableView];
625 // hittest using new point
626 NSInteger i = [m_tableView rowAtPoint:p];
627 return i;
628 }
629
630 #endif // wxUSE_LISTBOX