1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/listbox.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
17 #include "wx/listbox.h"
18 #include "wx/button.h"
19 #include "wx/settings.h"
20 #include "wx/toplevel.h"
21 #include "wx/dynarray.h"
26 #include "wx/mac/uma.h"
28 const short kTextColumnId
= 1024 ;
30 // new DataBrowser-based version:
31 // because of the limited insert functionality of DataBrowser,
32 // we just introduce IDs corresponding to the line number
35 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
37 BEGIN_EVENT_TABLE(wxListBox
, wxControl
)
39 // EVT_SIZE( wxListBox::OnSize )
40 EVT_CHAR( wxListBox
::OnChar
)
45 DataBrowserItemDataUPP gDataBrowserItemDataUPP
= NULL
;
46 DataBrowserItemNotificationUPP gDataBrowserItemNotificationUPP
= NULL
;
47 DataBrowserDrawItemUPP gDataBrowserDrawItemUPP
= NULL
;
50 #if TARGET_API_MAC_OSX
51 static pascal void DataBrowserItemNotificationProc(ControlRef browser
, DataBrowserItemID itemID
,
52 DataBrowserItemNotification message
, DataBrowserItemDataRef itemData
)
54 static pascal void DataBrowserItemNotificationProc(ControlRef browser
, DataBrowserItemID itemID
,
55 DataBrowserItemNotification message
)
58 long ref
= GetControlReference( browser
) ;
61 wxListBox
* list
= wxDynamicCast( (wxObject
*) ref
, wxListBox
) ;
63 if (i
>= 0 && i
< (int)list
->GetCount() )
65 bool trigger
= false ;
66 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_SELECTED
, list
->GetId() );
69 case kDataBrowserItemDeselected
:
70 if ( list
->HasMultipleSelection() )
71 trigger
= !list
->MacIsSelectionSuppressed() ;
74 case kDataBrowserItemSelected
:
75 trigger
= !list
->MacIsSelectionSuppressed() ;
78 case kDataBrowserItemDoubleClicked
:
79 event
.SetEventType( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
) ;
89 event
.SetEventObject( list
);
90 if ( list
->HasClientObjectData() )
91 event
.SetClientObject( list
->GetClientObject( i
) );
92 else if ( list
->HasClientUntypedData() )
93 event
.SetClientData( list
->GetClientData( i
) );
94 event
.SetString( list
->GetString( i
) );
96 event
.SetExtraLong( list
->HasMultipleSelection() ? message
== kDataBrowserItemSelected
: true );
97 wxPostEvent( list
->GetEventHandler() , event
) ;
98 // direct notification is not always having the listbox GetSelection() having in synch with event
99 // list->GetEventHandler()->ProcessEvent(event) ;
105 static pascal OSStatus
ListBoxGetSetItemData(ControlRef browser
,
106 DataBrowserItemID itemID
, DataBrowserPropertyID property
,
107 DataBrowserItemDataRef itemData
, Boolean changeValue
)
109 OSStatus err
= errDataBrowserPropertyNotSupported
;
117 long ref
= GetControlReference( browser
) ;
120 wxListBox
* list
= wxDynamicCast( (wxObject
*) ref
, wxListBox
) ;
122 if (i
>= 0 && i
< (int)list
->GetCount() )
124 wxMacCFStringHolder
cf( list
->GetString( i
) , list
->GetFont().GetEncoding() ) ;
125 verify_noerr( ::SetDataBrowserItemDataText( itemData
, cf
) ) ;
140 static pascal void ListBoxDrawProc( ControlRef browser
, DataBrowserItemID item
, DataBrowserPropertyID property
,
141 DataBrowserItemState itemState
, const Rect
*itemRect
, SInt16 depth
, Boolean isColorDevice
)
143 CFStringRef cfString
;
144 ThemeDrawingState themeState
;
147 GetThemeDrawingState( &themeState
) ;
148 cfString
= CFStringCreateWithFormat( NULL
, NULL
, CFSTR("Row %d"), item
);
150 // In this sample we handle the "selected" state; all others fall through to our "active" state
151 if ( itemState
== kDataBrowserItemIsSelected
)
153 ThemeBrush colorBrushID
;
155 Gestalt( gestaltSystemVersion
, &systemVersion
);
157 // TODO: switch over to wxSystemSettingsNative::GetColour() when kThemeBrushSecondaryHighlightColor is incorporated
158 // Panther DB starts using kThemeBrushSecondaryHighlightColor for inactive browser hilighting
159 if ( (systemVersion
>= 0x00001030) && !IsControlActive( browser
) )
160 colorBrushID
= kThemeBrushSecondaryHighlightColor
;
162 colorBrushID
= kThemeBrushPrimaryHighlightColor
;
164 // First paint the hilite rect, then the text on top
165 SetThemePen( colorBrushID
, 32, true );
166 PaintRect( itemRect
);
167 SetThemeDrawingState( themeState
, false ) ;
170 DrawThemeTextBox( cfString
, kThemeApplicationFont
, kThemeStateActive
, true, itemRect
, teFlushDefault
, NULL
);
171 SetThemeDrawingState( themeState
, true ) ;
173 if ( cfString
!= NULL
)
174 CFRelease( cfString
);
178 wxListBox
::wxListBox()
183 m_suppressSelection
= false ;
186 bool wxListBox
::Create(wxWindow
*parent
,
190 const wxArrayString
& choices
,
192 const wxValidator
& validator
,
193 const wxString
& name
)
195 wxCArrayString
chs(choices
);
197 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
198 style
, validator
, name
);
201 bool wxListBox
::Create(wxWindow
*parent
,
206 const wxString choices
[],
208 const wxValidator
& validator
,
209 const wxString
& name
)
211 m_macIsUserPane
= false ;
213 wxASSERT_MSG( !(style
& wxLB_MULTIPLE
) || !(style
& wxLB_EXTENDED
),
214 _T("only one of listbox selection modes can be specified") );
216 if ( !wxListBoxBase
::Create(parent
, id
, pos
, size
, style
& ~(wxHSCROLL
|wxVSCROLL
), validator
, name
) )
219 m_noItems
= 0 ; // this will be increased by our append command
222 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
224 m_peer
= new wxMacControl( this ) ;
226 ::CreateDataBrowserControl(
227 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()), &bounds
,
228 kDataBrowserListView
, m_peer
->GetControlRefAddr() ) );
230 DataBrowserSelectionFlags options
= kDataBrowserDragSelect
;
231 if ( style
& wxLB_MULTIPLE
)
232 options
|= kDataBrowserAlwaysExtendSelection
| kDataBrowserCmdTogglesSelection
;
233 else if ( style
& wxLB_EXTENDED
)
234 ; // default behaviour
236 options
|= kDataBrowserSelectOnlyOne
;
238 verify_noerr( m_peer
->SetSelectionFlags( options
) );
240 if ( gDataBrowserItemDataUPP
== NULL
)
241 gDataBrowserItemDataUPP
= NewDataBrowserItemDataUPP(ListBoxGetSetItemData
) ;
242 if ( gDataBrowserItemNotificationUPP
== NULL
)
244 gDataBrowserItemNotificationUPP
=
245 #if TARGET_API_MAC_OSX
246 (DataBrowserItemNotificationUPP
) NewDataBrowserItemNotificationWithItemUPP(DataBrowserItemNotificationProc
) ;
248 NewDataBrowserItemNotificationUPP(DataBrowserItemNotificationProc
) ;
252 if ( gDataBrowserDrawItemUPP
== NULL
)
253 gDataBrowserDrawItemUPP
= NewDataBrowserDrawItemUPP(ListBoxDrawProc
) ;
255 DataBrowserCallbacks callbacks
;
256 InitializeDataBrowserCallbacks( &callbacks
, kDataBrowserLatestCallbacks
) ;
258 callbacks
.u
.v1
.itemDataCallback
= gDataBrowserItemDataUPP
;
259 callbacks
.u
.v1
.itemNotificationCallback
= gDataBrowserItemNotificationUPP
;
260 m_peer
->SetCallbacks( &callbacks
);
262 DataBrowserCustomCallbacks customCallbacks
;
263 InitializeDataBrowserCustomCallbacks( &customCallbacks
, kDataBrowserLatestCustomCallbacks
) ;
265 customCallbacks
.u
.v1
.drawItemCallback
= gDataBrowserDrawItemUPP
;
267 SetDataBrowserCustomCallbacks( m_peer
->GetControlRef() , &customCallbacks
) ;
269 DataBrowserListViewColumnDesc columnDesc
;
270 columnDesc
.headerBtnDesc
.titleOffset
= 0;
271 columnDesc
.headerBtnDesc
.version
= kDataBrowserListViewLatestHeaderDesc
;
272 columnDesc
.headerBtnDesc
.btnFontStyle
.flags
= kControlUseFontMask
| kControlUseJustMask
;
273 columnDesc
.headerBtnDesc
.btnContentInfo
.contentType
= kControlNoContent
;
274 columnDesc
.headerBtnDesc
.btnFontStyle
.just
= teFlushDefault
;
275 columnDesc
.headerBtnDesc
.minimumWidth
= 0;
276 columnDesc
.headerBtnDesc
.maximumWidth
= 10000;
278 columnDesc
.headerBtnDesc
.btnFontStyle
.font
= kControlFontViewSystemFont
;
279 columnDesc
.headerBtnDesc
.btnFontStyle
.style
= normal
;
280 columnDesc
.headerBtnDesc
.titleString
= NULL
; // CFSTR( "" );
282 columnDesc
.propertyDesc
.propertyID
= kTextColumnId
;
283 columnDesc
.propertyDesc
.propertyType
= kDataBrowserTextType
; // kDataBrowserCustomType;
284 columnDesc
.propertyDesc
.propertyFlags
= kDataBrowserTableViewSelectionColumn
;
286 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
287 columnDesc
.propertyDesc
.propertyFlags
|= kDataBrowserListViewTypeSelectColumn
;
290 verify_noerr( m_peer
->AddListViewColumn( &columnDesc
, kDataBrowserListViewAppendColumn
) ) ;
291 verify_noerr( m_peer
->AutoSizeListViewColumns() ) ;
292 verify_noerr( m_peer
->SetHasScrollBars( false, true ) ) ;
293 verify_noerr( m_peer
->SetTableViewHiliteStyle( kDataBrowserTableViewFillHilite
) ) ;
294 verify_noerr( m_peer
->SetListViewHeaderBtnHeight( 0 ) ) ;
297 // shouldn't be necessary anymore under 10.2
298 m_peer
->SetData( kControlNoPart
, kControlDataBrowserIncludesFrameAndFocusTag
, (Boolean
) false ) ;
299 m_peer
->SetNeedsFocusRect( true ) ;
302 MacPostControlCreate( pos
, size
) ;
304 for ( int i
= 0 ; i
< n
; i
++ )
306 Append( choices
[i
] ) ;
309 // Needed because it is a wxControlWithItems
315 wxListBox
::~wxListBox()
317 m_peer
->SetReference( 0 ) ;
320 // avoid access during destruction
325 void wxListBox
::FreeData()
327 if ( HasClientObjectData() )
329 for ( unsigned int n
= 0; n
< m_noItems
; n
++ )
331 delete GetClientObject( n
);
336 void wxListBox
::DoSetSize(int x
, int y
,
337 int width
, int height
,
340 wxControl
::DoSetSize( x
, y
, width
, height
, sizeFlags
) ;
343 void wxListBox
::DoSetFirstItem(int n
)
348 void wxListBox
::Delete(unsigned int n
)
350 wxCHECK_RET( IsValid(n
),
351 wxT("invalid index in wxListBox::Delete") );
353 if ( HasClientObjectData() )
354 delete GetClientObject( n
);
356 m_stringArray
.RemoveAt( n
) ;
357 m_dataArray
.RemoveAt( n
) ;
363 int wxListBox
::DoAppend(const wxString
& item
)
365 InvalidateBestSize();
367 unsigned int index
= m_noItems
;
368 m_stringArray
.Add( item
) ;
369 m_dataArray
.Add( NULL
);
371 DoSetItemClientData( index
, NULL
) ;
377 void wxListBox
::DoSetItems(const wxArrayString
& choices
, void** clientData
)
380 unsigned int n
= choices
.GetCount();
382 for ( size_t i
= 0 ; i
< n
; ++i
)
385 Append( choices
[i
] , clientData
[i
] ) ;
387 Append( choices
[i
] ) ;
391 int wxListBox
::FindString(const wxString
& s
, bool bCase
) const
393 for ( size_t i
= 0 ; i
< m_noItems
; ++ i
)
395 if (s
.IsSameAs(GetString(i
), bCase
))
402 void wxListBox
::Clear()
406 m_stringArray
.Empty() ;
407 m_dataArray
.Empty() ;
411 void wxListBox
::DoSetSelection(int n
, bool select
)
413 wxCHECK_RET( n
== wxNOT_FOUND
|| IsValid(n
) ,
414 wxT("invalid index in wxListBox::SetSelection") );
416 if ( n
== wxNOT_FOUND
)
419 MacSetSelection( n
, select
) ;
422 bool wxListBox
::IsSelected(int n
) const
424 wxCHECK_MSG( IsValid(n
), false,
425 wxT("invalid index in wxListBox::Selected") );
427 return MacIsSelected( n
) ;
430 void *wxListBox
::DoGetItemClientData(unsigned int n
) const
432 wxCHECK_MSG( IsValid(n
), NULL
, wxT("invalid index in wxListBox::GetClientData"));
434 wxASSERT_MSG( m_dataArray
.GetCount() >= (unsigned int) n
, wxT("invalid client_data array") ) ;
436 return (void *)m_dataArray
[n
];
439 wxClientData
*wxListBox
::DoGetItemClientObject(unsigned int n
) const
441 return (wxClientData
*) DoGetItemClientData( n
) ;
444 void wxListBox
::DoSetItemClientData(unsigned int n
, void *clientData
)
446 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetClientData") );
448 wxASSERT_MSG( m_dataArray
.GetCount() >= (unsigned int) n
, wxT("invalid client_data array") ) ;
450 if ( m_dataArray
.GetCount() > (unsigned int) n
)
451 m_dataArray
[n
] = (char*)clientData
;
453 m_dataArray
.Add( (char*)clientData
) ;
456 void wxListBox
::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
458 DoSetItemClientData(n
, clientData
);
461 // Return number of selections and an array of selected integers
462 int wxListBox
::GetSelections(wxArrayInt
& aSelections
) const
464 return MacGetSelections( aSelections
) ;
467 // Get single selection, for single choice list items
468 int wxListBox
::GetSelection() const
470 return MacGetSelection() ;
473 // Find string for position
474 wxString wxListBox
::GetString(unsigned int n
) const
476 wxCHECK_MSG( IsValid(n
), wxEmptyString
,
477 wxT("invalid index in wxListBox::GetString") );
479 return m_stringArray
[n
] ;
482 void wxListBox
::DoInsertItems(const wxArrayString
& items
, unsigned int pos
)
484 wxCHECK_RET( IsValidInsert(pos
),
485 wxT("invalid index in wxListBox::InsertItems") );
487 InvalidateBestSize();
489 unsigned int nItems
= items
.GetCount();
491 for ( unsigned int i
= 0 ; i
< nItems
; i
++ )
493 m_stringArray
.Insert( items
[i
] , pos
+ i
) ;
494 m_dataArray
.Insert( NULL
, pos
+ i
) ;
496 MacInsert( pos
+ i
, items
[i
] ) ;
500 void wxListBox
::SetString(unsigned int n
, const wxString
& s
)
502 m_stringArray
[n
] = s
;
506 wxSize wxListBox
::DoGetBestSize() const
508 int lbWidth
= 100; // some defaults
513 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
)MacGetTopLevelWindowRef() ) ) ;
515 // TODO: clean this up
518 ::TextFont( m_font
.MacGetFontNum() ) ;
519 ::TextSize( m_font
.MacGetFontSize() ) ;
520 ::TextFace( m_font
.MacGetFontStyle() ) ;
524 ::TextFont( kFontIDMonaco
) ;
529 // Find the widest line
530 for (unsigned int i
= 0; i
< GetCount(); i
++)
532 wxString
str(GetString(i
));
535 Point bounds
= {0, 0} ;
538 // NB: what if m_font.Ok() == false ???
539 ::GetThemeTextDimensions(
540 wxMacCFStringHolder( str
, m_font
.GetEncoding() ) ,
541 kThemeCurrentPortFont
,
548 wLine
= ::TextWidth( str
.c_str() , 0 , str
.length() ) ;
551 lbWidth
= wxMax( lbWidth
, wLine
);
554 // Add room for the scrollbar
555 lbWidth
+= wxSystemSettings
::GetMetric(wxSYS_VSCROLL_X
);
557 // And just a bit more
559 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
562 // don't make the listbox too tall (limit height to around 10 items)
563 // but don't make it too small neither
564 lbHeight
= wxMax( (cy
+ 4) * wxMin( wxMax( GetCount(), 3 ), 10 ), 70 );
567 return wxSize( lbWidth
, lbHeight
);
570 unsigned int wxListBox
::GetCount() const
575 void wxListBox
::Refresh(bool eraseBack
, const wxRect
*rect
)
577 wxControl
::Refresh( eraseBack
, rect
) ;
580 // Some custom controls depend on this
581 /* static */ wxVisualAttributes
582 wxListBox
::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
584 wxVisualAttributes attr
;
586 attr
.colFg
= wxSystemSettings
::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
587 attr
.colBg
= wxSystemSettings
::GetColour( wxSYS_COLOUR_LISTBOX
);
588 attr
.font
= wxSystemSettings
::GetFont( wxSYS_DEFAULT_GUI_FONT
);
593 // ============================================================================
594 // list box control implementation
595 // ============================================================================
597 void wxListBox
::MacDelete( int n
)
599 wxArrayInt selectionBefore
;
600 MacGetSelections( selectionBefore
) ;
602 UInt32 id
= m_noItems
+ 1 ;
604 verify_noerr( m_peer
->RemoveItems( kDataBrowserNoItem
, 1 , (UInt32
*) &id
, kDataBrowserItemNoProperty
) ) ;
605 for ( unsigned int i
= 0 ; i
< selectionBefore
.GetCount() ; ++i
)
607 int current
= selectionBefore
[i
] ;
610 // selection was deleted
611 MacSetSelection( current
, false ) ;
613 else if ( current
> n
)
615 // something behind the deleted item was selected -> move up
616 MacSetSelection( current
- 1 , true ) ;
617 MacSetSelection( current
, false ) ;
624 kDataBrowserNoItem
, 1, (UInt32
*)kDataBrowserNoItem
,
625 kDataBrowserItemNoProperty
, kDataBrowserItemNoProperty
) ) ;
628 void wxListBox
::MacInsert( int n
, const wxString
& text
)
630 wxArrayInt selectionBefore
;
631 MacGetSelections( selectionBefore
) ;
633 // this has already been increased
634 UInt32 id
= m_noItems
;
635 verify_noerr( m_peer
->AddItems( kDataBrowserNoItem
, 1 , (UInt32
*) &id
, kDataBrowserItemNoProperty
) ) ;
637 for ( int i
= selectionBefore
.GetCount()-1 ; i
>= 0 ; --i
)
639 int current
= selectionBefore
[i
] ;
642 MacSetSelection( current
+ 1 , true ) ;
643 MacSetSelection( current
, false ) ;
650 kDataBrowserNoItem
, 1, (UInt32
*)kDataBrowserNoItem
,
651 kDataBrowserItemNoProperty
, kDataBrowserItemNoProperty
) ) ;
654 void wxListBox
::MacAppend( const wxString
& text
)
656 UInt32 id
= m_noItems
; // this has already been increased
657 verify_noerr( m_peer
->AddItems( kDataBrowserNoItem
, 1 , (UInt32
*) &id
, kDataBrowserItemNoProperty
) ) ;
658 // no need to deal with selections nor refreshed, as we have appended
661 void wxListBox
::MacClear()
663 verify_noerr( m_peer
->RemoveItems( kDataBrowserNoItem
, 0 , NULL
, kDataBrowserItemNoProperty
) ) ;
666 void wxListBox
::MacDeselectAll()
668 bool former
= MacSuppressSelection( true ) ;
669 verify_noerr(m_peer
->SetSelectedItems( 0 , NULL
, kDataBrowserItemsRemove
) ) ;
670 MacSuppressSelection( former
) ;
673 void wxListBox
::MacSetSelection( int n
, bool select
)
675 bool former
= MacSuppressSelection( true ) ;
678 if ( m_peer
->IsItemSelected( id
) != select
)
681 verify_noerr( m_peer
->SetSelectedItems( 1 , & id
, HasMultipleSelection() ? kDataBrowserItemsAdd
: kDataBrowserItemsAssign
) ) ;
683 verify_noerr( m_peer
->SetSelectedItems( 1 , & id
, kDataBrowserItemsRemove
) ) ;
687 MacSuppressSelection( former
) ;
690 bool wxListBox
::MacSuppressSelection( bool suppress
)
692 bool former
= m_suppressSelection
;
693 m_suppressSelection
= suppress
;
697 bool wxListBox
::MacIsSelected( int n
) const
699 return m_peer
->IsItemSelected( n
+ 1 ) ;
702 int wxListBox
::MacGetSelection() const
704 for ( unsigned int i
= 0 ; i
< GetCount() ; ++i
)
706 if ( m_peer
->IsItemSelected( i
+ 1 ) )
713 int wxListBox
::MacGetSelections( wxArrayInt
& aSelections
) const
719 UInt32 first
, last
;
720 m_peer
->GetSelectionAnchor( &first
, &last
) ;
721 if ( first
!= kDataBrowserNoItem
)
723 for ( size_t i
= first
; i
<= last
; ++i
)
725 if ( m_peer
->IsItemSelected( i
) )
727 aSelections
.Add( i
- 1 ) ;
736 void wxListBox
::MacSet( int n
, const wxString
& text
)
738 // as we don't store the strings we only have to issue a redraw
740 verify_noerr( m_peer
->UpdateItems( kDataBrowserNoItem
, 1 , &id
, kDataBrowserItemNoProperty
, kDataBrowserItemNoProperty
) ) ;
743 void wxListBox
::MacScrollTo( int n
)
746 verify_noerr( m_peer
->RevealItem( id
, kTextColumnId
, kDataBrowserRevealWithoutSelecting
) ) ;
749 int wxListBox
::DoListHitTest(const wxPoint
& inpoint
) const
753 // There are few reasons why this is complicated:
754 // 1) There is no native hittest function for mac
755 // 2) GetDataBrowserItemPartBounds only works on visible items
756 // 3) We can't do it through GetDataBrowserTableView[Item]RowHeight
757 // because what it returns is basically inaccurate in the context
758 // of the coordinates we want here, but we use this as a guess
759 // for where the first visible item lies
761 wxPoint point
= inpoint
;
762 // interestingly enough 10.2 (and below?) have GetDataBrowserItemPartBounds
763 // giving root window coordinates but 10.3 and above give client coordinates
764 // so we only compare using root window coordinates on 10.3 and up
765 if ( UMAGetSystemVersion() < 0x1030 )
766 MacClientToRootWindow(&point
.x
, &point
.y
);
768 // get column property id (req. for call to itempartbounds)
769 DataBrowserTableViewColumnID colId
= 0;
770 err
= GetDataBrowserTableViewColumnProperty(m_peer
->GetControlRef(), 0, &colId
);
771 wxCHECK_MSG(err
== noErr
, wxNOT_FOUND
, wxT("Unexpected error from GetDataBrowserTableViewColumnProperty"));
773 // OK, first we need to find the first visible item we have -
774 // this will be the "low" for our binary search. There is no real
775 // easy way around this, as we will need to do a SLOW linear search
776 // until we find a visible item, but we can do a cheap calculation
777 // via the row height to speed things up a bit
778 UInt32 scrollx
, scrolly
;
779 err
= GetDataBrowserScrollPosition(m_peer
->GetControlRef(), &scrollx
, &scrolly
);
780 wxCHECK_MSG(err
== noErr
, wxNOT_FOUND
, wxT("Unexpected error from GetDataBrowserScrollPosition"));
783 err
= GetDataBrowserTableViewRowHeight(m_peer
->GetControlRef(), &height
);
784 wxCHECK_MSG(err
== noErr
, wxNOT_FOUND
, wxT("Unexpected error from GetDataBrowserTableViewRowHeight"));
786 // these indices are 0-based, as usual, so we need to add 1 to them when
787 // passing them to data browser functions which use 1-based indices
788 int low
= scrolly
/ height
,
789 high
= GetCount() - 1;
792 // search for the first visible item (note that the scroll guess above
793 // is the low bounds of where the item might lie so we only use that as a
794 // starting point - we should reach it within 1 or 2 iterations of the loop)
795 while ( low
<= high
)
798 err
= GetDataBrowserItemPartBounds(m_peer
->GetControlRef(), low
+ 1, colId
,
799 kDataBrowserPropertyEnclosingPart
,
800 &bounds
); //note +1 to trans to mac id
804 // errDataBrowserItemNotFound is expected as it simply means that the
805 // item is not currently visible -- but other errors are not
806 wxCHECK_MSG( err
== errDataBrowserItemNotFound
, wxNOT_FOUND
,
807 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
812 // NOW do a binary search for where the item lies, searching low again if
813 // we hit an item that isn't visible
814 while ( low
<= high
)
816 int mid
= (low
+ high
) / 2;
819 err
= GetDataBrowserItemPartBounds(m_peer
->GetControlRef(), mid
+ 1, colId
,
820 kDataBrowserPropertyEnclosingPart
,
821 &bounds
); //note +1 to trans to mac id
822 wxCHECK_MSG( err
== noErr
|| err
== errDataBrowserItemNotFound
,
824 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
826 if ( err
== errDataBrowserItemNotFound
)
828 // item not visible, attempt to find a visible one
829 high
= mid
- 1; // search lower
831 else // visible item, do actual hitttest
833 // if point is within the bounds, return this item (since we assume
834 // all x coords of items are equal we only test the x coord in
836 if( (point
.x
>= bounds
.left
&& point
.x
<= bounds
.right
) &&
837 (point
.y
>= bounds
.top
&& point
.y
<= bounds
.bottom
) )
839 return mid
; // found!
842 if ( point
.y
< bounds
.top
)
843 high
= mid
- 1; // index(bounds) greater then key(point)
845 low
= mid
+ 1; // index(bounds) less then key(point)
852 #if !TARGET_API_MAC_OSX
854 void wxListBox
::OnChar(wxKeyEvent
& event
)
856 // TODO: trigger proper events here
860 if ( event
.GetKeyCode() == WXK_RETURN
|| event
.GetKeyCode() == WXK_NUMPAD_ENTER
)
862 wxWindow
* parent
= GetParent() ;
864 while ( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
)
865 parent
= parent
->GetParent() ;
867 if ( parent
&& parent
->GetDefaultItem() )
869 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(), wxButton
);
870 if ( def
&& def
->IsEnabled() )
872 wxCommandEvent
event( wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
873 event
.SetEventObject( def
);
874 def
->Command( event
);
883 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
884 else if (event
.GetKeyCode() == WXK_ESCAPE
|| (event
.GetKeyCode() == '.' && event
.MetaDown() ) )
886 // FIXME: look in ancestors, not just parent.
887 wxWindow
* win
= GetParent()->FindWindow( wxID_CANCEL
) ;
890 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
891 new_event
.SetEventObject( win
);
892 win
->GetEventHandler()->ProcessEvent( new_event
);
895 else if ( event
.GetKeyCode() == WXK_TAB
)
897 wxNavigationKeyEvent new_event
;
898 new_event
.SetEventObject( this );
899 new_event
.SetDirection( !event
.ShiftDown() );
900 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
901 new_event
.SetWindowChange( event
.ControlDown() );
902 new_event
.SetCurrentFocus( this );
903 if ( !GetEventHandler()->ProcessEvent( new_event
) )
906 else if ( event
.GetKeyCode() == WXK_DOWN
|| event
.GetKeyCode() == WXK_UP
)
908 // perform the default key handling first
909 wxControl
::OnKeyDown( event
) ;
911 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
912 event
.SetEventObject( this );
914 wxArrayInt aSelections
;
915 int n
, count
= GetSelections(aSelections
);
919 if ( HasClientObjectData() )
920 event
.SetClientObject( GetClientObject( n
) );
921 else if ( HasClientUntypedData() )
922 event
.SetClientData( GetClientData( n
) );
923 event
.SetString(GetString(n
));
932 GetEventHandler()->ProcessEvent(event
);
936 if ( event
.GetTimestamp() > m_lastTypeIn
+ 60 )
937 m_typeIn
= wxEmptyString
;
939 m_lastTypeIn
= event
.GetTimestamp() ;
940 m_typeIn
+= (char) event
.GetKeyCode() ;
941 int line
= FindString( wxT("*") + m_typeIn
+ wxT("*") ) ;
944 if ( GetSelection() != line
)
946 SetSelection( line
) ;
948 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
949 event
.SetEventObject( this );
951 if ( HasClientObjectData() )
952 event
.SetClientObject( GetClientObject( line
) );
953 else if ( HasClientUntypedData() )
954 event
.SetClientData( GetClientData( line
) );
955 event
.SetString(GetString(line
));
958 GetEventHandler()->ProcessEvent(event
);
964 #endif // !TARGET_API_MAC_OSX