]>
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 | |
81eaa4da SC |
37 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 |
38 | <NSTableViewDataSource> | |
39 | #endif | |
dbeddfb9 SC |
40 | { |
41 | wxListWidgetCocoaImpl* impl; | |
42 | } | |
43 | ||
03647350 VZ |
44 | - (id)tableView:(NSTableView *)aTableView |
45 | objectValueForTableColumn:(NSTableColumn *)aTableColumn | |
dbeddfb9 SC |
46 | row:(NSInteger)rowIndex; |
47 | ||
03647350 VZ |
48 | - (void)tableView:(NSTableView *)aTableView |
49 | setObjectValue:(id)value forTableColumn:(NSTableColumn *)aTableColumn | |
dbeddfb9 | 50 | row:(NSInteger)rowIndex; |
03647350 | 51 | |
dbeddfb9 SC |
52 | - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView; |
53 | ||
54 | - (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation; | |
55 | - (wxListWidgetCocoaImpl*) implementation; | |
56 | ||
57 | @end | |
58 | ||
59 | @interface wxNSTableView : NSTableView | |
81eaa4da SC |
60 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 |
61 | <NSTableViewDelegate> | |
62 | #endif | |
dbeddfb9 | 63 | { |
dbeddfb9 SC |
64 | } |
65 | ||
dbeddfb9 SC |
66 | @end |
67 | ||
68 | // | |
69 | // table column | |
70 | // | |
71 | ||
72 | class wxCocoaTableColumn; | |
73 | ||
74 | @interface wxNSTableColumn : NSTableColumn | |
75 | { | |
76 | wxCocoaTableColumn* column; | |
77 | } | |
78 | ||
79 | - (void) setColumn: (wxCocoaTableColumn*) col; | |
80 | ||
81 | - (wxCocoaTableColumn*) column; | |
82 | ||
83 | @end | |
84 | ||
85 | class WXDLLIMPEXP_CORE wxCocoaTableColumn : public wxListWidgetColumn | |
86 | { | |
87 | public : | |
03647350 | 88 | wxCocoaTableColumn( wxNSTableColumn* column, bool editable ) |
dbeddfb9 SC |
89 | : m_column( column ), m_editable(editable) |
90 | { | |
91 | } | |
03647350 | 92 | |
dbeddfb9 SC |
93 | ~wxCocoaTableColumn() |
94 | { | |
95 | } | |
03647350 | 96 | |
dbeddfb9 | 97 | wxNSTableColumn* GetNSTableColumn() const { return m_column ; } |
03647350 | 98 | |
dbeddfb9 | 99 | bool IsEditable() const { return m_editable; } |
03647350 | 100 | |
dbeddfb9 SC |
101 | protected : |
102 | wxNSTableColumn* m_column; | |
103 | bool m_editable; | |
104 | } ; | |
105 | ||
106 | NSString* column1 = @"1"; | |
107 | ||
108 | class wxListWidgetCocoaImpl : public wxWidgetCocoaImpl, public wxListWidgetImpl | |
109 | { | |
110 | public : | |
111 | wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data ); | |
03647350 | 112 | |
dbeddfb9 | 113 | ~wxListWidgetCocoaImpl(); |
03647350 VZ |
114 | |
115 | virtual wxListWidgetColumn* InsertTextColumn( unsigned pos, const wxString& title, bool editable = false, | |
dbeddfb9 | 116 | wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ; |
03647350 | 117 | virtual wxListWidgetColumn* InsertCheckColumn( unsigned pos , const wxString& title, bool editable = false, |
dbeddfb9 | 118 | wxAlignment just = wxALIGN_LEFT , int defaultWidth = -1) ; |
03647350 | 119 | |
dbeddfb9 | 120 | // add and remove |
03647350 VZ |
121 | |
122 | virtual void ListDelete( unsigned int n ) ; | |
dbeddfb9 SC |
123 | virtual void ListInsert( unsigned int n ) ; |
124 | virtual void ListClear() ; | |
125 | ||
126 | // selecting | |
127 | ||
128 | virtual void ListDeselectAll(); | |
03647350 | 129 | |
dbeddfb9 SC |
130 | virtual void ListSetSelection( unsigned int n, bool select, bool multi ) ; |
131 | virtual int ListGetSelection() const ; | |
03647350 | 132 | |
dbeddfb9 | 133 | virtual int ListGetSelections( wxArrayInt& aSelections ) const ; |
03647350 | 134 | |
dbeddfb9 | 135 | virtual bool ListIsSelected( unsigned int n ) const ; |
03647350 | 136 | |
dbeddfb9 SC |
137 | // display |
138 | ||
139 | virtual void ListScrollTo( unsigned int n ) ; | |
140 | ||
141 | // accessing content | |
142 | ||
143 | virtual unsigned int ListGetCount() const ; | |
03647350 | 144 | |
dbeddfb9 SC |
145 | int ListGetColumnType( int col ) |
146 | { | |
147 | return col; | |
148 | } | |
149 | virtual void UpdateLine( unsigned int n, wxListWidgetColumn* col = NULL ) ; | |
150 | virtual void UpdateLineToEnd( unsigned int n); | |
4dd9fdf8 | 151 | |
01000cbc | 152 | virtual void controlDoubleAction(WXWidget slf, void* _cmd, void *sender); |
f4c24d3d | 153 | |
dbeddfb9 SC |
154 | protected : |
155 | wxNSTableView* m_tableView ; | |
03647350 | 156 | |
dbeddfb9 SC |
157 | wxNSTableDataSource* m_dataSource; |
158 | } ; | |
159 | ||
160 | // | |
161 | // implementations | |
162 | // | |
163 | ||
164 | @implementation wxNSTableColumn | |
165 | ||
166 | - (id) init | |
167 | { | |
168 | [super init]; | |
169 | column = nil; | |
170 | return self; | |
171 | } | |
172 | ||
173 | - (void) setColumn: (wxCocoaTableColumn*) col | |
174 | { | |
175 | column = col; | |
176 | } | |
177 | ||
178 | - (wxCocoaTableColumn*) column | |
179 | { | |
180 | return column; | |
181 | } | |
182 | ||
183 | @end | |
184 | ||
185 | class wxNSTableViewCellValue : public wxListWidgetCellValue | |
186 | { | |
187 | public : | |
188 | wxNSTableViewCellValue( id &v ) : value(v) | |
189 | { | |
190 | } | |
03647350 | 191 | |
dbeddfb9 SC |
192 | virtual ~wxNSTableViewCellValue() {} |
193 | ||
194 | virtual void Set( CFStringRef v ) | |
195 | { | |
196 | value = [[(NSString*)v retain] autorelease]; | |
197 | } | |
03647350 | 198 | virtual void Set( const wxString& value ) |
dbeddfb9 SC |
199 | { |
200 | Set( (CFStringRef) wxCFStringRef( value ) ); | |
201 | } | |
03647350 | 202 | virtual void Set( int v ) |
dbeddfb9 SC |
203 | { |
204 | value = [NSNumber numberWithInt:v]; | |
205 | } | |
03647350 VZ |
206 | |
207 | virtual int GetIntValue() const | |
dbeddfb9 SC |
208 | { |
209 | if ( [value isKindOfClass:[NSNumber class]] ) | |
210 | return [ (NSNumber*) value intValue ]; | |
03647350 | 211 | |
dbeddfb9 SC |
212 | return 0; |
213 | } | |
03647350 VZ |
214 | |
215 | virtual wxString GetStringValue() const | |
dbeddfb9 SC |
216 | { |
217 | if ( [value isKindOfClass:[NSString class]] ) | |
f66ecdc4 | 218 | return wxCFStringRef::AsString( (NSString*) value ); |
03647350 | 219 | |
dbeddfb9 SC |
220 | return wxEmptyString; |
221 | } | |
03647350 | 222 | |
dbeddfb9 SC |
223 | protected: |
224 | id& value; | |
225 | } ; | |
226 | ||
227 | @implementation wxNSTableDataSource | |
228 | ||
229 | - (id) init | |
230 | { | |
231 | [super init]; | |
232 | impl = nil; | |
233 | return self; | |
234 | } | |
235 | ||
236 | - (void)setImplementation: (wxListWidgetCocoaImpl *) theImplementation | |
237 | { | |
238 | impl = theImplementation; | |
239 | } | |
240 | ||
241 | - (wxListWidgetCocoaImpl*) implementation | |
242 | { | |
243 | return impl; | |
244 | } | |
245 | ||
246 | - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView | |
247 | { | |
d8207702 | 248 | wxUnusedVar(aTableView); |
dbeddfb9 SC |
249 | if ( impl ) |
250 | return impl->ListGetCount(); | |
251 | return 0; | |
252 | } | |
253 | ||
03647350 VZ |
254 | - (id)tableView:(NSTableView *)aTableView |
255 | objectValueForTableColumn:(NSTableColumn *)aTableColumn | |
dbeddfb9 SC |
256 | row:(NSInteger)rowIndex |
257 | { | |
d8207702 | 258 | wxUnusedVar(aTableView); |
dbeddfb9 SC |
259 | wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn; |
260 | wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer()); | |
261 | wxCocoaTableColumn* col = [tablecol column]; | |
262 | id value = nil; | |
263 | wxNSTableViewCellValue cellvalue(value); | |
264 | lb->GetValueCallback(rowIndex, col, cellvalue); | |
265 | return value; | |
266 | } | |
267 | ||
03647350 VZ |
268 | - (void)tableView:(NSTableView *)aTableView |
269 | setObjectValue:(id)value forTableColumn:(NSTableColumn *)aTableColumn | |
dbeddfb9 SC |
270 | row:(NSInteger)rowIndex |
271 | { | |
d8207702 | 272 | wxUnusedVar(aTableView); |
dbeddfb9 SC |
273 | wxNSTableColumn* tablecol = (wxNSTableColumn *)aTableColumn; |
274 | wxListBox* lb = dynamic_cast<wxListBox*>(impl->GetWXPeer()); | |
275 | wxCocoaTableColumn* col = [tablecol column]; | |
276 | wxNSTableViewCellValue cellvalue(value); | |
277 | lb->SetValueCallback(rowIndex, col, cellvalue); | |
278 | } | |
279 | ||
280 | @end | |
281 | ||
282 | @implementation wxNSTableView | |
283 | ||
4dd9fdf8 | 284 | + (void)initialize |
21267321 | 285 | { |
4dd9fdf8 | 286 | static BOOL initialized = NO; |
03647350 | 287 | if (!initialized) |
21267321 | 288 | { |
4dd9fdf8 SC |
289 | initialized = YES; |
290 | wxOSXCocoaClassAddWXMethods( self ); | |
21267321 SC |
291 | } |
292 | } | |
dbeddfb9 | 293 | |
81eaa4da SC |
294 | - (void) tableViewSelectionDidChange: (NSNotification *) notification |
295 | { | |
296 | wxUnusedVar(notification); | |
297 | ||
298 | int row = [self selectedRow]; | |
299 | ||
300 | if (row == -1) | |
301 | { | |
302 | // no row selected | |
303 | } | |
304 | else | |
305 | { | |
306 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
307 | wxListBox *list = static_cast<wxListBox*> ( impl->GetWXPeer()); | |
308 | wxCHECK_RET( list != NULL , wxT("Listbox expected")); | |
309 | ||
310 | wxCommandEvent event( wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() ); | |
311 | ||
312 | if ((row < 0) || (row > (int) list->GetCount())) // OS X can select an item below the last item | |
313 | return; | |
314 | ||
315 | if ( !list->MacGetBlockEvents() ) | |
316 | list->HandleLineEvent( row, false ); | |
317 | } | |
318 | ||
319 | } | |
320 | ||
dbeddfb9 SC |
321 | @end |
322 | ||
323 | // | |
324 | // | |
325 | // | |
326 | ||
327 | wxListWidgetCocoaImpl::wxListWidgetCocoaImpl( wxWindowMac* peer, NSScrollView* view, wxNSTableView* tableview, wxNSTableDataSource* data ) : | |
328 | wxWidgetCocoaImpl( peer, view ), m_tableView(tableview), m_dataSource(data) | |
329 | { | |
4dd9fdf8 | 330 | InstallEventHandler( tableview ); |
dbeddfb9 SC |
331 | } |
332 | ||
333 | wxListWidgetCocoaImpl::~wxListWidgetCocoaImpl() | |
334 | { | |
335 | [m_dataSource release]; | |
336 | } | |
337 | ||
03647350 | 338 | unsigned int wxListWidgetCocoaImpl::ListGetCount() const |
dbeddfb9 SC |
339 | { |
340 | wxListBox* lb = dynamic_cast<wxListBox*> ( GetWXPeer() ); | |
341 | return lb->GetCount(); | |
342 | } | |
343 | ||
344 | // | |
345 | // columns | |
346 | // | |
347 | ||
03647350 VZ |
348 | wxListWidgetColumn* wxListWidgetCocoaImpl::InsertTextColumn( unsigned pos, const wxString& WXUNUSED(title), bool editable, |
349 | wxAlignment WXUNUSED(just), int defaultWidth) | |
dbeddfb9 SC |
350 | { |
351 | wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init]; | |
352 | [col1 setEditable:editable]; | |
03647350 | 353 | |
dbeddfb9 | 354 | unsigned formerColCount = [m_tableView numberOfColumns]; |
03647350 | 355 | |
dbeddfb9 SC |
356 | // there's apparently no way to insert at a specific position |
357 | [m_tableView addTableColumn:col1 ]; | |
358 | if ( pos < formerColCount ) | |
359 | [m_tableView moveColumn:formerColCount toColumn:pos]; | |
03647350 | 360 | |
dbeddfb9 SC |
361 | if ( defaultWidth >= 0 ) |
362 | { | |
363 | [col1 setMaxWidth:defaultWidth]; | |
364 | [col1 setMinWidth:defaultWidth]; | |
e32090ba | 365 | [col1 setWidth:defaultWidth]; |
dbeddfb9 | 366 | } |
e32090ba SC |
367 | else |
368 | { | |
03647350 | 369 | [col1 setMaxWidth:1000]; |
e32090ba SC |
370 | [col1 setMinWidth:10]; |
371 | // temporary hack, because I cannot get the automatic column resizing | |
372 | // to work properly | |
373 | [col1 setWidth:1000]; | |
374 | } | |
375 | [col1 setResizingMask: NSTableColumnAutoresizingMask]; | |
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 | ||
03647350 VZ |
384 | wxListWidgetColumn* wxListWidgetCocoaImpl::InsertCheckColumn( unsigned pos , const wxString& WXUNUSED(title), bool editable, |
385 | wxAlignment WXUNUSED(just), int defaultWidth ) | |
dbeddfb9 SC |
386 | { |
387 | wxNSTableColumn* col1 = [[wxNSTableColumn alloc] init]; | |
388 | [col1 setEditable:editable]; | |
03647350 | 389 | |
dbeddfb9 SC |
390 | // set your custom cell & set it up |
391 | NSButtonCell* checkbox = [[NSButtonCell alloc] init]; | |
392 | [checkbox setTitle:@""]; | |
393 | [checkbox setButtonType:NSSwitchButton]; | |
394 | [col1 setDataCell:checkbox] ; | |
395 | [checkbox release]; | |
03647350 | 396 | |
dbeddfb9 | 397 | unsigned formerColCount = [m_tableView numberOfColumns]; |
03647350 | 398 | |
dbeddfb9 SC |
399 | // there's apparently no way to insert at a specific position |
400 | [m_tableView addTableColumn:col1 ]; | |
401 | if ( pos < formerColCount ) | |
402 | [m_tableView moveColumn:formerColCount toColumn:pos]; | |
03647350 | 403 | |
dbeddfb9 SC |
404 | if ( defaultWidth >= 0 ) |
405 | { | |
406 | [col1 setMaxWidth:defaultWidth]; | |
407 | [col1 setMinWidth:defaultWidth]; | |
e32090ba | 408 | [col1 setWidth:defaultWidth]; |
dbeddfb9 | 409 | } |
03647350 | 410 | |
e32090ba | 411 | [col1 setResizingMask: NSTableColumnNoResizing]; |
dbeddfb9 SC |
412 | wxCocoaTableColumn* wxcol = new wxCocoaTableColumn( col1, editable ); |
413 | [col1 setColumn:wxcol]; | |
414 | ||
415 | // owned by the tableview | |
416 | [col1 release]; | |
417 | return wxcol; | |
418 | } | |
419 | ||
420 | ||
421 | // | |
422 | // inserting / removing lines | |
423 | // | |
424 | ||
03647350 | 425 | void wxListWidgetCocoaImpl::ListInsert( unsigned int WXUNUSED(n) ) |
dbeddfb9 | 426 | { |
dbeddfb9 SC |
427 | [m_tableView reloadData]; |
428 | } | |
429 | ||
03647350 | 430 | void wxListWidgetCocoaImpl::ListDelete( unsigned int WXUNUSED(n) ) |
dbeddfb9 SC |
431 | { |
432 | [m_tableView reloadData]; | |
433 | } | |
434 | ||
03647350 | 435 | void wxListWidgetCocoaImpl::ListClear() |
dbeddfb9 SC |
436 | { |
437 | [m_tableView reloadData]; | |
438 | } | |
439 | ||
440 | // selecting | |
441 | ||
442 | void wxListWidgetCocoaImpl::ListDeselectAll() | |
443 | { | |
444 | [m_tableView deselectAll:nil]; | |
445 | } | |
446 | ||
03647350 | 447 | void wxListWidgetCocoaImpl::ListSetSelection( unsigned int n, bool select, bool multi ) |
dbeddfb9 SC |
448 | { |
449 | // TODO | |
450 | if ( select ) | |
451 | [m_tableView selectRow: n byExtendingSelection:multi]; | |
452 | else | |
453 | [m_tableView deselectRow: n]; | |
454 | ||
455 | } | |
456 | ||
03647350 | 457 | int wxListWidgetCocoaImpl::ListGetSelection() const |
dbeddfb9 | 458 | { |
21267321 | 459 | return [m_tableView selectedRow]; |
dbeddfb9 SC |
460 | } |
461 | ||
03647350 | 462 | int wxListWidgetCocoaImpl::ListGetSelections( wxArrayInt& aSelections ) const |
dbeddfb9 | 463 | { |
21267321 SC |
464 | aSelections.Empty(); |
465 | ||
466 | int count = ListGetCount(); | |
467 | ||
468 | for ( int i = 0; i < count; ++i) | |
469 | { | |
470 | if ([m_tableView isRowSelected:count]) | |
471 | aSelections.Add(i); | |
472 | } | |
473 | ||
474 | return aSelections.Count(); | |
dbeddfb9 SC |
475 | } |
476 | ||
03647350 | 477 | bool wxListWidgetCocoaImpl::ListIsSelected( unsigned int n ) const |
dbeddfb9 | 478 | { |
21267321 | 479 | return [m_tableView isRowSelected:n]; |
dbeddfb9 SC |
480 | } |
481 | ||
482 | // display | |
483 | ||
03647350 | 484 | void wxListWidgetCocoaImpl::ListScrollTo( unsigned int n ) |
dbeddfb9 SC |
485 | { |
486 | [m_tableView scrollRowToVisible:n]; | |
487 | } | |
488 | ||
03647350 VZ |
489 | |
490 | void wxListWidgetCocoaImpl::UpdateLine( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) ) | |
dbeddfb9 SC |
491 | { |
492 | // TODO optimize | |
493 | [m_tableView reloadData]; | |
494 | } | |
495 | ||
03647350 | 496 | void wxListWidgetCocoaImpl::UpdateLineToEnd( unsigned int WXUNUSED(n)) |
dbeddfb9 SC |
497 | { |
498 | // TODO optimize | |
499 | [m_tableView reloadData]; | |
500 | } | |
501 | ||
d8207702 | 502 | void wxListWidgetCocoaImpl::controlDoubleAction(WXWidget WXUNUSED(slf),void* WXUNUSED(_cmd), void *WXUNUSED(sender)) |
4dd9fdf8 SC |
503 | { |
504 | wxListBox *list = static_cast<wxListBox*> ( GetWXPeer()); | |
505 | wxCHECK_RET( list != NULL , wxT("Listbox expected")); | |
506 | ||
507 | int sel = [m_tableView clickedRow]; | |
508 | if ((sel < 0) || (sel > (int) list->GetCount())) // OS X can select an item below the last item (why?) | |
509 | return; | |
510 | ||
511 | list->HandleLineEvent( sel, true ); | |
512 | } | |
dbeddfb9 SC |
513 | |
514 | // accessing content | |
515 | ||
516 | ||
03647350 VZ |
517 | wxWidgetImplType* wxWidgetImpl::CreateListBox( wxWindowMac* wxpeer, |
518 | wxWindowMac* WXUNUSED(parent), | |
519 | wxWindowID WXUNUSED(id), | |
520 | const wxPoint& pos, | |
dbeddfb9 | 521 | const wxSize& size, |
03647350 | 522 | long style, |
d8207702 | 523 | long WXUNUSED(extraStyle)) |
dbeddfb9 | 524 | { |
dbeddfb9 SC |
525 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
526 | NSScrollView* scrollview = [[NSScrollView alloc] initWithFrame:r]; | |
03647350 | 527 | |
dbeddfb9 | 528 | // use same scroll flags logic as msw |
03647350 | 529 | |
dbeddfb9 | 530 | [scrollview setHasVerticalScroller:YES]; |
03647350 | 531 | |
dbeddfb9 SC |
532 | if ( style & wxLB_HSCROLL ) |
533 | [scrollview setHasHorizontalScroller:YES]; | |
03647350 | 534 | |
dbeddfb9 | 535 | [scrollview setAutohidesScrollers: ((style & wxLB_ALWAYS_SB) ? NO : YES)]; |
e32090ba | 536 | |
dbeddfb9 | 537 | // setting up the true table |
03647350 | 538 | |
dbeddfb9 | 539 | wxNSTableView* tableview = [[wxNSTableView alloc] init]; |
81eaa4da | 540 | [tableview setDelegate:tableview]; |
dbeddfb9 SC |
541 | // only one multi-select mode available |
542 | if ( (style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE) ) | |
543 | [tableview setAllowsMultipleSelection:YES]; | |
03647350 | 544 | |
dbeddfb9 SC |
545 | // simple listboxes have no header row |
546 | [tableview setHeaderView:nil]; | |
03647350 | 547 | |
e32090ba SC |
548 | if ( style & wxLB_HSCROLL ) |
549 | [tableview setColumnAutoresizingStyle:NSTableViewNoColumnAutoresizing]; | |
550 | else | |
551 | [tableview setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle]; | |
03647350 | 552 | |
dbeddfb9 SC |
553 | wxNSTableDataSource* ds = [[ wxNSTableDataSource alloc] init]; |
554 | [tableview setDataSource:ds]; | |
e32090ba SC |
555 | [scrollview setDocumentView:tableview]; |
556 | [tableview release]; | |
557 | ||
dbeddfb9 | 558 | wxListWidgetCocoaImpl* c = new wxListWidgetCocoaImpl( wxpeer, scrollview, tableview, ds ); |
4dd9fdf8 SC |
559 | |
560 | // temporary hook for dnd | |
561 | [tableview registerForDraggedTypes:[NSArray arrayWithObjects: | |
562 | NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]]; | |
563 | ||
dbeddfb9 SC |
564 | [ds setImplementation:c]; |
565 | return c; | |
566 | } | |
567 | ||
d8207702 | 568 | int wxListBox::DoListHitTest(const wxPoint& WXUNUSED(inpoint)) const |
dbeddfb9 SC |
569 | { |
570 | #if wxOSX_USE_CARBON | |
571 | OSStatus err; | |
572 | ||
573 | // There are few reasons why this is complicated: | |
574 | // 1) There is no native HitTest function for Mac | |
575 | // 2) GetDataBrowserItemPartBounds only works on visible items | |
576 | // 3) We can't do it through GetDataBrowserTableView[Item]RowHeight | |
577 | // because what it returns is basically inaccurate in the context | |
578 | // of the coordinates we want here, but we use this as a guess | |
579 | // for where the first visible item lies | |
580 | ||
581 | wxPoint point = inpoint; | |
582 | ||
583 | // get column property ID (req. for call to itempartbounds) | |
584 | DataBrowserTableViewColumnID colId = 0; | |
585 | err = GetDataBrowserTableViewColumnProperty(m_peer->GetControlRef(), 0, &colId); | |
586 | wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserTableViewColumnProperty")); | |
587 | ||
588 | // OK, first we need to find the first visible item we have - | |
589 | // this will be the "low" for our binary search. There is no real | |
590 | // easy way around this, as we will need to do a SLOW linear search | |
591 | // until we find a visible item, but we can do a cheap calculation | |
592 | // via the row height to speed things up a bit | |
593 | UInt32 scrollx, scrolly; | |
594 | err = GetDataBrowserScrollPosition(m_peer->GetControlRef(), &scrollx, &scrolly); | |
595 | wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserScrollPosition")); | |
596 | ||
597 | UInt16 height; | |
598 | err = GetDataBrowserTableViewRowHeight(m_peer->GetControlRef(), &height); | |
599 | wxCHECK_MSG(err == noErr, wxNOT_FOUND, wxT("Unexpected error from GetDataBrowserTableViewRowHeight")); | |
600 | ||
601 | // these indices are 0-based, as usual, so we need to add 1 to them when | |
602 | // passing them to data browser functions which use 1-based indices | |
603 | int low = scrolly / height, | |
604 | high = GetCount() - 1; | |
605 | ||
606 | // search for the first visible item (note that the scroll guess above | |
607 | // is the low bounds of where the item might lie so we only use that as a | |
608 | // starting point - we should reach it within 1 or 2 iterations of the loop) | |
609 | while ( low <= high ) | |
610 | { | |
611 | Rect bounds; | |
612 | err = GetDataBrowserItemPartBounds( | |
613 | m_peer->GetControlRef(), low + 1, colId, | |
614 | kDataBrowserPropertyEnclosingPart, | |
615 | &bounds); // note +1 to translate to Mac ID | |
616 | if ( err == noErr ) | |
617 | break; | |
618 | ||
619 | // errDataBrowserItemNotFound is expected as it simply means that the | |
620 | // item is not currently visible -- but other errors are not | |
621 | wxCHECK_MSG( err == errDataBrowserItemNotFound, wxNOT_FOUND, | |
622 | wxT("Unexpected error from GetDataBrowserItemPartBounds") ); | |
623 | ||
624 | low++; | |
625 | } | |
626 | ||
627 | // NOW do a binary search for where the item lies, searching low again if | |
628 | // we hit an item that isn't visible | |
629 | while ( low <= high ) | |
630 | { | |
631 | int mid = (low + high) / 2; | |
632 | ||
633 | Rect bounds; | |
634 | err = GetDataBrowserItemPartBounds( | |
635 | m_peer->GetControlRef(), mid + 1, colId, | |
636 | kDataBrowserPropertyEnclosingPart, | |
637 | &bounds); //note +1 to trans to mac id | |
638 | wxCHECK_MSG( err == noErr || err == errDataBrowserItemNotFound, | |
639 | wxNOT_FOUND, | |
640 | wxT("Unexpected error from GetDataBrowserItemPartBounds") ); | |
641 | ||
642 | if ( err == errDataBrowserItemNotFound ) | |
643 | { | |
644 | // item not visible, attempt to find a visible one | |
645 | // search lower | |
646 | high = mid - 1; | |
647 | } | |
648 | else // visible item, do actual hitttest | |
649 | { | |
650 | // if point is within the bounds, return this item (since we assume | |
651 | // all x coords of items are equal we only test the x coord in | |
652 | // equality) | |
653 | if ((point.x >= bounds.left && point.x <= bounds.right) && | |
654 | (point.y >= bounds.top && point.y <= bounds.bottom) ) | |
655 | { | |
656 | // found! | |
657 | return mid; | |
658 | } | |
659 | ||
660 | if ( point.y < bounds.top ) | |
661 | // index(bounds) greater then key(point) | |
662 | high = mid - 1; | |
663 | else | |
664 | // index(bounds) less then key(point) | |
665 | low = mid + 1; | |
666 | } | |
667 | } | |
668 | #endif | |
669 | return wxNOT_FOUND; | |
670 | } | |
671 | ||
672 | #endif // wxUSE_LISTBOX |