]>
Commit | Line | Data |
---|---|---|
dbeddfb9 SC |
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" | |
4dd9fdf8 | 17 | #include "wx/dnd.h" |
dbeddfb9 SC |
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 | ||
030495ec | 36 | @interface wxNSTableDataSource : NSObject wxOSX_10_6_AND_LATER(<NSTableViewDataSource>) |
dbeddfb9 SC |
37 | { |
38 | wxListWidgetCocoaImpl* impl; | |
39 | } | |
40 | ||
03647350 VZ |
41 | - (id)tableView:(NSTableView *)aTableView |
42 | objectValueForTableColumn:(NSTableColumn *)aTableColumn | |
dbeddfb9 SC |
43 | row:(NSInteger)rowIndex; |
44 | ||
03647350 VZ |
45 | - (void)tableView:(NSTableView *)aTableView |
46 | setObjectValue:(id)value forTableColumn:(NSTableColumn *)aTableColumn | |
dbeddfb9 | 47 | row:(NSInteger)rowIndex; |
03647350 | 48 | |
dbeddfb9 SC |
49 | - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView; |
50 | ||
51 | - (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation; | |
52 | - (wxListWidgetCocoaImpl*) implementation; | |
53 | ||
54 | @end | |
55 | ||
030495ec | 56 | @interface wxNSTableView : NSTableView wxOSX_10_6_AND_LATER(<NSTableViewDelegate>) |
dbeddfb9 | 57 | { |
dbeddfb9 SC |
58 | } |
59 | ||
dbeddfb9 SC |
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 : | |
03647350 | 82 | wxCocoaTableColumn( wxNSTableColumn* column, bool editable ) |
dbeddfb9 SC |
83 | : m_column( column ), m_editable(editable) |
84 | { | |
85 | } | |
03647350 | 86 | |
dbeddfb9 SC |
87 | ~wxCocoaTableColumn() |
88 | { | |
89 | } | |
03647350 | 90 | |
dbeddfb9 | 91 | wxNSTableColumn* GetNSTableColumn() const { return m_column ; } |
03647350 | 92 | |
dbeddfb9 | 93 | bool IsEditable() const { return m_editable; } |
03647350 | 94 | |
dbeddfb9 SC |
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 ); | |
03647350 | 106 | |
dbeddfb9 | 107 | ~wxListWidgetCocoaImpl(); |
03647350 VZ |
108 | |
109 | virtual wxListWidgetColumn* InsertTextColumn( unsigned pos, const wxString& title, bool editable = false, | |
dbeddfb9 | 110 | wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ; |
03647350 | 111 | virtual wxListWidgetColumn* InsertCheckColumn( unsigned pos , const wxString& title, bool editable = false, |
dbeddfb9 | 112 | wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ; |
03647350 | 113 | |
dbeddfb9 | 114 | // add and remove |
03647350 VZ |
115 | |
116 | virtual void ListDelete( unsigned int n ) ; | |
dbeddfb9 SC |
117 | virtual void ListInsert( unsigned int n ) ; |
118 | virtual void ListClear() ; | |
119 | ||
120 | // selecting | |
121 | ||
122 | virtual void ListDeselectAll(); | |
03647350 | 123 | |
dbeddfb9 SC |
124 | virtual void ListSetSelection( unsigned int n, bool select, bool multi ) ; |
125 | virtual int ListGetSelection() const ; | |
03647350 | 126 | |
dbeddfb9 | 127 | virtual int ListGetSelections( wxArrayInt& aSelections ) const ; |
03647350 | 128 | |
dbeddfb9 | 129 | virtual bool ListIsSelected( unsigned int n ) const ; |
03647350 | 130 | |
dbeddfb9 SC |
131 | // display |
132 | ||
133 | virtual void ListScrollTo( unsigned int n ) ; | |
134 | ||
135 | // accessing content | |
136 | ||
137 | virtual unsigned int ListGetCount() const ; | |
03647350 | 138 | |
dbeddfb9 SC |
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); | |
4dd9fdf8 | 145 | |
01000cbc | 146 | virtual void controlDoubleAction(WXWidget slf, void* _cmd, void *sender); |
f4c24d3d | 147 | |
dbeddfb9 SC |
148 | protected : |
149 | wxNSTableView* m_tableView ; | |
03647350 | 150 | |
dbeddfb9 SC |
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 | } | |
03647350 | 185 | |
dbeddfb9 SC |
186 | virtual ~wxNSTableViewCellValue() {} |
187 | ||
188 | virtual void Set( CFStringRef v ) | |
189 | { | |
190 | value = [[(NSString*)v retain] autorelease]; | |
191 | } | |
03647350 | 192 | virtual void Set( const wxString& value ) |
dbeddfb9 SC |
193 | { |
194 | Set( (CFStringRef) wxCFStringRef( value ) ); | |
195 | } | |
03647350 | 196 | virtual void Set( int v ) |
dbeddfb9 SC |
197 | { |
198 | value = [NSNumber numberWithInt:v]; | |
199 | } | |
03647350 VZ |
200 | |
201 | virtual int GetIntValue() const | |
dbeddfb9 SC |
202 | { |
203 | if ( [value isKindOfClass:[NSNumber class]] ) | |
204 | return [ (NSNumber*) value intValue ]; | |
03647350 | 205 | |
dbeddfb9 SC |
206 | return 0; |
207 | } | |
03647350 VZ |
208 | |
209 | virtual wxString GetStringValue() const | |
dbeddfb9 SC |
210 | { |
211 | if ( [value isKindOfClass:[NSString class]] ) | |
f66ecdc4 | 212 | return wxCFStringRef::AsString( (NSString*) value ); |
03647350 | 213 | |
dbeddfb9 SC |
214 | return wxEmptyString; |
215 | } | |
03647350 | 216 | |
dbeddfb9 SC |
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 | { | |
d8207702 | 242 | wxUnusedVar(aTableView); |
dbeddfb9 SC |
243 | if ( impl ) |
244 | return impl->ListGetCount(); | |
245 | return 0; | |
246 | } | |
247 | ||
03647350 VZ |
248 | - (id)tableView:(NSTableView *)aTableView |
249 | objectValueForTableColumn:(NSTableColumn *)aTableColumn | |
dbeddfb9 SC |
250 | row:(NSInteger)rowIndex |
251 | { | |
d8207702 | 252 | wxUnusedVar(aTableView); |
dbeddfb9 SC |
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 | ||
03647350 VZ |
262 | - (void)tableView:(NSTableView *)aTableView |
263 | setObjectValue:(id)value forTableColumn:(NSTableColumn *)aTableColumn | |
dbeddfb9 SC |
264 | row:(NSInteger)rowIndex |
265 | { | |
d8207702 | 266 | wxUnusedVar(aTableView); |
dbeddfb9 SC |
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 | ||
4dd9fdf8 | 278 | + (void)initialize |
21267321 | 279 | { |
4dd9fdf8 | 280 | static BOOL initialized = NO; |
03647350 | 281 | if (!initialized) |
21267321 | 282 | { |
4dd9fdf8 SC |
283 | initialized = YES; |
284 | wxOSXCocoaClassAddWXMethods( self ); | |
21267321 SC |
285 | } |
286 | } | |
dbeddfb9 | 287 | |
81eaa4da SC |
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 | ||
dbeddfb9 SC |
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 | { | |
4dd9fdf8 | 324 | InstallEventHandler( tableview ); |
dbeddfb9 SC |
325 | } |
326 | ||
327 | wxListWidgetCocoaImpl::~wxListWidgetCocoaImpl() | |
328 | { | |
329 | [m_dataSource release]; | |
330 | } | |
331 | ||
03647350 | 332 | unsigned int wxListWidgetCocoaImpl::ListGetCount() const |
dbeddfb9 SC |
333 | { |
334 | wxListBox* lb = dynamic_cast<wxListBox*> ( GetWXPeer() ); | |
335 | return lb->GetCount(); | |
336 | } | |
337 | ||
338 | // | |
339 | // columns | |
340 | // | |
341 | ||
03647350 VZ |
342 | wxListWidgetColumn* wxListWidgetCocoaImpl::InsertTextColumn( unsigned pos, const wxString& WXUNUSED(title), bool editable, |
343 | wxAlignment WXUNUSED(just), int defaultWidth) | |
dbeddfb9 SC |
344 | { |
345 | wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init]; | |
346 | [col1 setEditable:editable]; | |
03647350 | 347 | |
dbeddfb9 | 348 | unsigned formerColCount = [m_tableView numberOfColumns]; |
03647350 | 349 | |
dbeddfb9 SC |
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]; | |
03647350 | 354 | |
dbeddfb9 SC |
355 | if ( defaultWidth >= 0 ) |
356 | { | |
357 | [col1 setMaxWidth:defaultWidth]; | |
358 | [col1 setMinWidth:defaultWidth]; | |
e32090ba | 359 | [col1 setWidth:defaultWidth]; |
dbeddfb9 | 360 | } |
e32090ba SC |
361 | else |
362 | { | |
03647350 | 363 | [col1 setMaxWidth:1000]; |
e32090ba SC |
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]; | |
dbeddfb9 SC |
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 | ||
03647350 VZ |
378 | wxListWidgetColumn* wxListWidgetCocoaImpl::InsertCheckColumn( unsigned pos , const wxString& WXUNUSED(title), bool editable, |
379 | wxAlignment WXUNUSED(just), int defaultWidth ) | |
dbeddfb9 SC |
380 | { |
381 | wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init]; | |
382 | [col1 setEditable:editable]; | |
03647350 | 383 | |
dbeddfb9 SC |
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]; | |
03647350 | 390 | |
dbeddfb9 | 391 | unsigned formerColCount = [m_tableView numberOfColumns]; |
03647350 | 392 | |
dbeddfb9 SC |
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]; | |
03647350 | 397 | |
dbeddfb9 SC |
398 | if ( defaultWidth >= 0 ) |
399 | { | |
400 | [col1 setMaxWidth:defaultWidth]; | |
401 | [col1 setMinWidth:defaultWidth]; | |
e32090ba | 402 | [col1 setWidth:defaultWidth]; |
dbeddfb9 | 403 | } |
03647350 | 404 | |
e32090ba | 405 | [col1 setResizingMask: NSTableColumnNoResizing]; |
dbeddfb9 SC |
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 | ||
03647350 | 419 | void wxListWidgetCocoaImpl::ListInsert( unsigned int WXUNUSED(n) ) |
dbeddfb9 | 420 | { |
dbeddfb9 SC |
421 | [m_tableView reloadData]; |
422 | } | |
423 | ||
03647350 | 424 | void wxListWidgetCocoaImpl::ListDelete( unsigned int WXUNUSED(n) ) |
dbeddfb9 SC |
425 | { |
426 | [m_tableView reloadData]; | |
427 | } | |
428 | ||
03647350 | 429 | void wxListWidgetCocoaImpl::ListClear() |
dbeddfb9 SC |
430 | { |
431 | [m_tableView reloadData]; | |
432 | } | |
433 | ||
434 | // selecting | |
435 | ||
436 | void wxListWidgetCocoaImpl::ListDeselectAll() | |
437 | { | |
438 | [m_tableView deselectAll:nil]; | |
439 | } | |
440 | ||
03647350 | 441 | void wxListWidgetCocoaImpl::ListSetSelection( unsigned int n, bool select, bool multi ) |
dbeddfb9 SC |
442 | { |
443 | // TODO | |
444 | if ( select ) | |
445 | [m_tableView selectRow: n byExtendingSelection:multi]; | |
446 | else | |
447 | [m_tableView deselectRow: n]; | |
448 | ||
449 | } | |
450 | ||
03647350 | 451 | int wxListWidgetCocoaImpl::ListGetSelection() const |
dbeddfb9 | 452 | { |
21267321 | 453 | return [m_tableView selectedRow]; |
dbeddfb9 SC |
454 | } |
455 | ||
03647350 | 456 | int wxListWidgetCocoaImpl::ListGetSelections( wxArrayInt& aSelections ) const |
dbeddfb9 | 457 | { |
21267321 SC |
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(); | |
dbeddfb9 SC |
469 | } |
470 | ||
03647350 | 471 | bool wxListWidgetCocoaImpl::ListIsSelected( unsigned int n ) const |
dbeddfb9 | 472 | { |
21267321 | 473 | return [m_tableView isRowSelected:n]; |
dbeddfb9 SC |
474 | } |
475 | ||
476 | // display | |
477 | ||
03647350 | 478 | void wxListWidgetCocoaImpl::ListScrollTo( unsigned int n ) |
dbeddfb9 SC |
479 | { |
480 | [m_tableView scrollRowToVisible:n]; | |
481 | } | |
482 | ||
03647350 VZ |
483 | |
484 | void wxListWidgetCocoaImpl::UpdateLine( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) ) | |
dbeddfb9 SC |
485 | { |
486 | // TODO optimize | |
487 | [m_tableView reloadData]; | |
488 | } | |
489 | ||
03647350 | 490 | void wxListWidgetCocoaImpl::UpdateLineToEnd( unsigned int WXUNUSED(n)) |
dbeddfb9 SC |
491 | { |
492 | // TODO optimize | |
493 | [m_tableView reloadData]; | |
494 | } | |
495 | ||
d8207702 | 496 | void wxListWidgetCocoaImpl::controlDoubleAction(WXWidget WXUNUSED(slf),void* WXUNUSED(_cmd), void *WXUNUSED(sender)) |
4dd9fdf8 SC |
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 | } | |
dbeddfb9 SC |
507 | |
508 | // accessing content | |
509 | ||
510 | ||
03647350 VZ |
511 | wxWidgetImplType* wxWidgetImpl::CreateListBox( wxWindowMac* wxpeer, |
512 | wxWindowMac* WXUNUSED(parent), | |
513 | wxWindowID WXUNUSED(id), | |
514 | const wxPoint& pos, | |
dbeddfb9 | 515 | const wxSize& size, |
03647350 | 516 | long style, |
d8207702 | 517 | long WXUNUSED(extraStyle)) |
dbeddfb9 | 518 | { |
dbeddfb9 SC |
519 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
520 | NSScrollView* scrollview = [[NSScrollView alloc] initWithFrame:r]; | |
03647350 | 521 | |
dbeddfb9 | 522 | // use same scroll flags logic as msw |
03647350 | 523 | |
dbeddfb9 | 524 | [scrollview setHasVerticalScroller:YES]; |
03647350 | 525 | |
dbeddfb9 SC |
526 | if ( style & wxLB_HSCROLL ) |
527 | [scrollview setHasHorizontalScroller:YES]; | |
03647350 | 528 | |
dbeddfb9 | 529 | [scrollview setAutohidesScrollers: ((style & wxLB_ALWAYS_SB) ? NO : YES)]; |
e32090ba | 530 | |
dbeddfb9 | 531 | // setting up the true table |
03647350 | 532 | |
dbeddfb9 | 533 | wxNSTableView* tableview = [[wxNSTableView alloc] init]; |
81eaa4da | 534 | [tableview setDelegate:tableview]; |
dbeddfb9 SC |
535 | // only one multi-select mode available |
536 | if ( (style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE) ) | |
537 | [tableview setAllowsMultipleSelection:YES]; | |
03647350 | 538 | |
dbeddfb9 SC |
539 | // simple listboxes have no header row |
540 | [tableview setHeaderView:nil]; | |
03647350 | 541 | |
e32090ba SC |
542 | if ( style & wxLB_HSCROLL ) |
543 | [tableview setColumnAutoresizingStyle:NSTableViewNoColumnAutoresizing]; | |
544 | else | |
545 | [tableview setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle]; | |
03647350 | 546 | |
dbeddfb9 SC |
547 | wxNSTableDataSource* ds = [[ wxNSTableDataSource alloc] init]; |
548 | [tableview setDataSource:ds]; | |
e32090ba SC |
549 | [scrollview setDocumentView:tableview]; |
550 | [tableview release]; | |
551 | ||
dbeddfb9 | 552 | wxListWidgetCocoaImpl* c = new wxListWidgetCocoaImpl( wxpeer, scrollview, tableview, ds ); |
4dd9fdf8 SC |
553 | |
554 | // temporary hook for dnd | |
555 | [tableview registerForDraggedTypes:[NSArray arrayWithObjects: | |
556 | NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]]; | |
557 | ||
dbeddfb9 SC |
558 | [ds setImplementation:c]; |
559 | return c; | |
560 | } | |
561 | ||
d8207702 | 562 | int wxListBox::DoListHitTest(const wxPoint& WXUNUSED(inpoint)) const |
dbeddfb9 SC |
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 |