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