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