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"
16 #include "wx/listbox.h"
22 #include "wx/settings.h"
23 #include "wx/arrstr.h"
24 #include "wx/dcclient.h"
27 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControlWithItems
)
29 BEGIN_EVENT_TABLE(wxListBox
, wxControl
)
32 #include "wx/mac/uma.h"
34 // ============================================================================
35 // list box control implementation
36 // ============================================================================
38 wxListBox::wxListBox()
42 bool wxListBox::Create(
47 const wxArrayString
& choices
,
49 const wxValidator
& validator
,
50 const wxString
& name
)
52 wxCArrayString
chs(choices
);
55 parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
56 style
, validator
, name
);
59 wxMacListControl
* wxListBox::GetPeer() const
61 wxMacDataBrowserListControl
*lb
= wxDynamicCast(m_peer
,wxMacDataBrowserListControl
);
62 return lb
? wx_static_cast(wxMacListControl
*,lb
) : 0 ;
65 bool wxListBox::Create(
71 const wxString choices
[],
73 const wxValidator
& validator
,
74 const wxString
& name
)
76 m_macIsUserPane
= false;
78 wxASSERT_MSG( !(style
& wxLB_MULTIPLE
) || !(style
& wxLB_EXTENDED
),
79 wxT("only a single listbox selection mode can be specified") );
81 if ( !wxListBoxBase::Create( parent
, id
, pos
, size
, style
& ~(wxHSCROLL
| wxVSCROLL
), validator
, name
) )
84 wxMacDataBrowserListControl
* control
= new wxMacDataBrowserListControl( this, pos
, size
, style
);
85 control
->SetClientDataType( m_clientDataItemsType
);
88 MacPostControlCreate( pos
, size
);
92 // Needed because it is a wxControlWithItems
93 SetInitialSize( size
);
98 wxListBox::~wxListBox()
101 m_peer
->SetReference( 0 );
104 void wxListBox::FreeData()
106 GetPeer()->MacClear();
109 void wxListBox::DoSetFirstItem(int n
)
111 GetPeer()->MacScrollTo( n
);
114 void wxListBox::EnsureVisible(int n
)
116 GetPeer()->MacScrollTo( n
);
119 void wxListBox::DoDeleteOneItem(unsigned int n
)
121 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::Delete") );
123 GetPeer()->MacDelete( n
);
126 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
129 wxClientDataType type
)
131 InvalidateBestSize();
133 GetPeer()->MacInsert( pos
, items
);
134 const unsigned int count
= items
.GetCount();
137 for (unsigned int i
= 0; i
< count
; ++i
)
138 AssignNewItemClientData( pos
+ i
, clientData
, i
, type
);
141 return pos
+ count
- 1;
144 int wxListBox::FindString(const wxString
& s
, bool bCase
) const
146 for ( size_t i
= 0; i
< GetCount(); ++ i
)
148 if (s
.IsSameAs( GetString( i
), bCase
) )
155 void wxListBox::DoClear()
160 void wxListBox::DoSetSelection(int n
, bool select
)
162 wxCHECK_RET( n
== wxNOT_FOUND
|| IsValid(n
),
163 wxT("invalid index in wxListBox::SetSelection") );
165 if ( n
== wxNOT_FOUND
)
166 GetPeer()->MacDeselectAll();
168 GetPeer()->MacSetSelection( n
, select
, HasMultipleSelection() );
171 bool wxListBox::IsSelected(int n
) const
173 wxCHECK_MSG( IsValid(n
), false, wxT("invalid index in wxListBox::Selected") );
175 return GetPeer()->MacIsSelected( n
);
178 void *wxListBox::DoGetItemClientData(unsigned int n
) const
180 wxCHECK_MSG( IsValid(n
), NULL
, wxT("invalid index in wxListBox::GetClientData"));
181 return GetPeer()->MacGetClientData( n
);
184 void wxListBox::DoSetItemClientData(unsigned int n
, void *clientData
)
186 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetClientData") );
187 GetPeer()->MacSetClientData( n
, clientData
);
190 // Return number of selections and an array of selected integers
191 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
193 return GetPeer()->MacGetSelections( aSelections
);
196 // Get single selection, for single choice list items
197 int wxListBox::GetSelection() const
199 return GetPeer()->MacGetSelection();
202 // Find string for position
203 wxString
wxListBox::GetString(unsigned int n
) const
205 wxCHECK_MSG( IsValid(n
), wxEmptyString
, wxT("invalid index in wxListBox::GetString") );
206 return GetPeer()->MacGetString(n
);
209 void wxListBox::SetString(unsigned int n
, const wxString
& s
)
211 GetPeer()->MacSetString( n
, s
);
214 wxSize
wxListBox::DoGetBestSize() const
216 int lbWidth
= 100; // some defaults
221 #if wxMAC_USE_CORE_GRAPHICS
222 wxClientDC
dc(const_cast<wxListBox
*>(this));
223 dc
.SetFont(GetFont());
225 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
)MacGetTopLevelWindowRef() ) );
227 // TODO: clean this up
230 ::TextFont( m_font
.MacGetFontNum() );
231 ::TextSize( m_font
.MacGetFontSize() );
232 ::TextFace( m_font
.MacGetFontStyle() );
236 ::TextFont( kFontIDMonaco
);
241 // Find the widest line
242 for (unsigned int i
= 0; i
< GetCount(); i
++)
244 wxString
str( GetString( i
) );
245 #if wxMAC_USE_CORE_GRAPHICS
246 wxCoord width
, height
;
247 dc
.GetTextExtent( str
, &width
, &height
);
251 Point bounds
= {0, 0};
254 // NB: what if m_font.Ok() == false ???
255 ::GetThemeTextDimensions(
256 wxMacCFStringHolder( str
, m_font
.GetEncoding() ),
257 kThemeCurrentPortFont
,
264 wLine
= ::TextWidth( str
.c_str(), 0, str
.length() );
267 lbWidth
= wxMax( lbWidth
, wLine
);
270 // Add room for the scrollbar
271 lbWidth
+= wxSystemSettings::GetMetric( wxSYS_VSCROLL_X
);
273 // And just a bit more
275 #if wxMAC_USE_CORE_GRAPHICS
276 wxCoord width
, height
;
277 dc
.GetTextExtent( wxT("XX") , &width
, &height
);
280 int cx
= ::TextWidth( "XX", 0, 1 );
284 // don't make the listbox too tall (limit height to around 10 items)
285 // but don't make it too small neither
286 lbHeight
= wxMax( (cy
+ 4) * wxMin( wxMax( GetCount(), 3 ), 10 ), 70 );
289 return wxSize( lbWidth
, lbHeight
);
292 unsigned int wxListBox::GetCount() const
294 return GetPeer()->MacGetCount();
297 void wxListBox::Refresh(bool eraseBack
, const wxRect
*rect
)
299 wxControl::Refresh( eraseBack
, rect
);
302 // Some custom controls depend on this
303 /* static */ wxVisualAttributes
304 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
306 wxVisualAttributes attr
;
308 attr
.colFg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
309 attr
.colBg
= wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
);
310 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
315 int wxListBox::DoListHitTest(const wxPoint
& inpoint
) const
319 // There are few reasons why this is complicated:
320 // 1) There is no native HitTest function for Mac
321 // 2) GetDataBrowserItemPartBounds only works on visible items
322 // 3) We can't do it through GetDataBrowserTableView[Item]RowHeight
323 // because what it returns is basically inaccurate in the context
324 // of the coordinates we want here, but we use this as a guess
325 // for where the first visible item lies
327 wxPoint point
= inpoint
;
329 // interestingly enough 10.2 (and below?) have GetDataBrowserItemPartBounds
330 // giving root window coordinates but 10.3 and above give client coordinates
331 // so we only compare using root window coordinates on 10.3 and up
332 if ( UMAGetSystemVersion() < 0x1030 )
333 MacClientToRootWindow(&point
.x
, &point
.y
);
335 // get column property ID (req. for call to itempartbounds)
336 DataBrowserTableViewColumnID colId
= 0;
337 err
= GetDataBrowserTableViewColumnProperty(m_peer
->GetControlRef(), 0, &colId
);
338 wxCHECK_MSG(err
== noErr
, wxNOT_FOUND
, wxT("Unexpected error from GetDataBrowserTableViewColumnProperty"));
340 // OK, first we need to find the first visible item we have -
341 // this will be the "low" for our binary search. There is no real
342 // easy way around this, as we will need to do a SLOW linear search
343 // until we find a visible item, but we can do a cheap calculation
344 // via the row height to speed things up a bit
345 UInt32 scrollx
, scrolly
;
346 err
= GetDataBrowserScrollPosition(m_peer
->GetControlRef(), &scrollx
, &scrolly
);
347 wxCHECK_MSG(err
== noErr
, wxNOT_FOUND
, wxT("Unexpected error from GetDataBrowserScrollPosition"));
350 err
= GetDataBrowserTableViewRowHeight(m_peer
->GetControlRef(), &height
);
351 wxCHECK_MSG(err
== noErr
, wxNOT_FOUND
, wxT("Unexpected error from GetDataBrowserTableViewRowHeight"));
353 // these indices are 0-based, as usual, so we need to add 1 to them when
354 // passing them to data browser functions which use 1-based indices
355 int low
= scrolly
/ height
,
356 high
= GetCount() - 1;
358 // search for the first visible item (note that the scroll guess above
359 // is the low bounds of where the item might lie so we only use that as a
360 // starting point - we should reach it within 1 or 2 iterations of the loop)
361 while ( low
<= high
)
364 err
= GetDataBrowserItemPartBounds(
365 m_peer
->GetControlRef(), low
+ 1, colId
,
366 kDataBrowserPropertyEnclosingPart
,
367 &bounds
); // note +1 to translate to Mac ID
371 // errDataBrowserItemNotFound is expected as it simply means that the
372 // item is not currently visible -- but other errors are not
373 wxCHECK_MSG( err
== errDataBrowserItemNotFound
, wxNOT_FOUND
,
374 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
379 // NOW do a binary search for where the item lies, searching low again if
380 // we hit an item that isn't visible
381 while ( low
<= high
)
383 int mid
= (low
+ high
) / 2;
386 err
= GetDataBrowserItemPartBounds(
387 m_peer
->GetControlRef(), mid
+ 1, colId
,
388 kDataBrowserPropertyEnclosingPart
,
389 &bounds
); //note +1 to trans to mac id
390 wxCHECK_MSG( err
== noErr
|| err
== errDataBrowserItemNotFound
,
392 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
394 if ( err
== errDataBrowserItemNotFound
)
396 // item not visible, attempt to find a visible one
400 else // visible item, do actual hitttest
402 // if point is within the bounds, return this item (since we assume
403 // all x coords of items are equal we only test the x coord in
405 if ((point
.x
>= bounds
.left
&& point
.x
<= bounds
.right
) &&
406 (point
.y
>= bounds
.top
&& point
.y
<= bounds
.bottom
) )
412 if ( point
.y
< bounds
.top
)
413 // index(bounds) greater then key(point)
416 // index(bounds) less then key(point)
424 // ============================================================================
425 // data browser based implementation
426 // ============================================================================
428 wxMacListBoxItem::wxMacListBoxItem()
433 wxMacListBoxItem::~wxMacListBoxItem()
437 void wxMacListBoxItem::Notification(wxMacDataItemBrowserControl
*owner
,
438 DataBrowserItemNotification message
,
439 DataBrowserItemDataRef itemData
) const
441 wxMacDataBrowserListControl
*lb
= wxDynamicCast(owner
,wxMacDataBrowserListControl
);
443 // we want to depend on as little as possible to make sure tear-down of controls is safe
445 if ( message
== kDataBrowserItemRemoved
)
447 if ( lb
!= NULL
&& lb
->GetClientDataType() == wxClientData_Object
)
449 delete (wxClientData
*) (m_data
);
456 wxListBox
*list
= wxDynamicCast( owner
->GetPeer() , wxListBox
);
457 wxCHECK_RET( list
!= NULL
, wxT("Listbox expected"));
459 bool trigger
= false;
460 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_SELECTED
, list
->GetId() );
463 case kDataBrowserItemDeselected
:
464 if ( list
->HasMultipleSelection() )
465 trigger
= !lb
->IsSelectionSuppressed();
468 case kDataBrowserItemSelected
:
469 trigger
= !lb
->IsSelectionSuppressed();
472 case kDataBrowserItemDoubleClicked
:
473 event
.SetEventType( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
);
483 event
.SetEventObject( list
);
484 if ( list
->HasClientObjectData() )
485 event
.SetClientObject( (wxClientData
*) m_data
);
486 else if ( list
->HasClientUntypedData() )
487 event
.SetClientData( m_data
);
488 event
.SetString( m_label
);
489 event
.SetInt( owner
->GetLineFromItem( this ) );
490 event
.SetExtraLong( list
->HasMultipleSelection() ? message
== kDataBrowserItemSelected
: true );
492 // direct notification is not always having the listbox GetSelection()
493 // having in synch with event, so use wxPostEvent instead
494 // list->GetEventHandler()->ProcessEvent(event);
496 wxPostEvent( list
->GetEventHandler(), event
);
500 IMPLEMENT_DYNAMIC_CLASS( wxMacDataBrowserListControl
, wxMacDataItemBrowserControl
)
502 wxMacDataBrowserListControl::wxMacDataBrowserListControl( wxWindow
*peer
, const wxPoint
& pos
, const wxSize
& size
, long style
)
503 : wxMacDataItemBrowserControl( peer
, pos
, size
, style
)
505 OSStatus err
= noErr
;
506 m_clientDataItemsType
= wxClientData_None
;
507 if ( style
& wxLB_SORT
)
508 m_sortOrder
= SortOrder_Text_Ascending
;
510 DataBrowserSelectionFlags options
= kDataBrowserDragSelect
;
511 if ( style
& wxLB_MULTIPLE
)
513 options
|= kDataBrowserAlwaysExtendSelection
| kDataBrowserCmdTogglesSelection
;
515 else if ( style
& wxLB_EXTENDED
)
517 options
|= kDataBrowserCmdTogglesSelection
;
521 options
|= kDataBrowserSelectOnlyOne
;
523 err
= SetSelectionFlags( options
);
526 DataBrowserListViewColumnDesc columnDesc
;
527 columnDesc
.headerBtnDesc
.titleOffset
= 0;
528 columnDesc
.headerBtnDesc
.version
= kDataBrowserListViewLatestHeaderDesc
;
530 columnDesc
.headerBtnDesc
.btnFontStyle
.flags
=
531 kControlUseFontMask
| kControlUseJustMask
;
533 columnDesc
.headerBtnDesc
.btnContentInfo
.contentType
= kControlNoContent
;
534 columnDesc
.headerBtnDesc
.btnFontStyle
.just
= teFlushDefault
;
535 columnDesc
.headerBtnDesc
.btnFontStyle
.font
= kControlFontViewSystemFont
;
536 columnDesc
.headerBtnDesc
.btnFontStyle
.style
= normal
;
537 columnDesc
.headerBtnDesc
.titleString
= NULL
;
539 columnDesc
.headerBtnDesc
.minimumWidth
= 0;
540 columnDesc
.headerBtnDesc
.maximumWidth
= 10000;
542 columnDesc
.propertyDesc
.propertyID
= kTextColumnId
;
543 columnDesc
.propertyDesc
.propertyType
= kDataBrowserTextType
;
544 columnDesc
.propertyDesc
.propertyFlags
= kDataBrowserTableViewSelectionColumn
;
545 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
546 columnDesc
.propertyDesc
.propertyFlags
|= kDataBrowserListViewTypeSelectColumn
;
549 verify_noerr( AddColumn( &columnDesc
, kDataBrowserListViewAppendColumn
) );
551 columnDesc
.headerBtnDesc
.minimumWidth
= 0;
552 columnDesc
.headerBtnDesc
.maximumWidth
= 0;
553 columnDesc
.propertyDesc
.propertyID
= kNumericOrderColumnId
;
554 columnDesc
.propertyDesc
.propertyType
= kDataBrowserPropertyRelevanceRankPart
;
555 columnDesc
.propertyDesc
.propertyFlags
= kDataBrowserTableViewSelectionColumn
;
556 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
557 columnDesc
.propertyDesc
.propertyFlags
|= kDataBrowserListViewTypeSelectColumn
;
560 verify_noerr( AddColumn( &columnDesc
, kDataBrowserListViewAppendColumn
) );
562 SetDataBrowserSortProperty( m_controlRef
, kTextColumnId
);
563 if ( m_sortOrder
== SortOrder_Text_Ascending
)
565 SetDataBrowserSortProperty( m_controlRef
, kTextColumnId
);
566 SetDataBrowserSortOrder( m_controlRef
, kDataBrowserOrderIncreasing
);
570 SetDataBrowserSortProperty( m_controlRef
, kNumericOrderColumnId
);
571 SetDataBrowserSortOrder( m_controlRef
, kDataBrowserOrderIncreasing
);
574 verify_noerr( AutoSizeColumns() );
575 verify_noerr( SetHiliteStyle(kDataBrowserTableViewFillHilite
) );
576 verify_noerr( SetHeaderButtonHeight( 0 ) );
577 err
= SetHasScrollBars( (style
& wxHSCROLL
) != 0 , true );
579 // shouldn't be necessary anymore under 10.2
580 m_peer
->SetData( kControlNoPart
, kControlDataBrowserIncludesFrameAndFocusTag
, (Boolean
)false );
581 m_peer
->SetNeedsFocusRect( true );
585 wxMacDataBrowserListControl::~wxMacDataBrowserListControl()
589 wxWindow
* wxMacDataBrowserListControl::GetPeer() const
591 return wxDynamicCast( wxMacControl::GetPeer() , wxWindow
);
594 wxMacDataItem
* wxMacDataBrowserListControl::CreateItem()
596 return new wxMacListBoxItem();
601 // in case we need that one day
603 // ============================================================================
604 // HIView owner-draw-based implementation
605 // ============================================================================
607 static pascal void ListBoxDrawProc(
608 ControlRef browser
, DataBrowserItemID item
, DataBrowserPropertyID property
,
609 DataBrowserItemState itemState
, const Rect
*itemRect
, SInt16 depth
, Boolean isColorDevice
)
611 CFStringRef cfString
;
612 ThemeDrawingState themeState
;
615 GetThemeDrawingState( &themeState
);
616 cfString
= CFStringCreateWithFormat( NULL
, NULL
, CFSTR("Row %d"), item
);
618 // In this sample we handle the "selected" state; all others fall through to our "active" state
619 if ( itemState
== kDataBrowserItemIsSelected
)
621 ThemeBrush colorBrushID
;
623 // TODO: switch over to wxSystemSettingsNative::GetColour() when kThemeBrushSecondaryHighlightColor
624 // is incorporated Panther DB starts using kThemeBrushSecondaryHighlightColor
625 // for inactive browser highlighting
626 Gestalt( gestaltSystemVersion
, &systemVersion
);
627 if ( (systemVersion
>= 0x00001030) && !IsControlActive( browser
) )
628 colorBrushID
= kThemeBrushSecondaryHighlightColor
;
630 colorBrushID
= kThemeBrushPrimaryHighlightColor
;
632 // First paint the hilite rect, then the text on top
633 SetThemePen( colorBrushID
, 32, true );
634 PaintRect( itemRect
);
635 SetThemeDrawingState( themeState
, false );
638 DrawThemeTextBox( cfString
, kThemeApplicationFont
, kThemeStateActive
, true, itemRect
, teFlushDefault
, NULL
);
639 SetThemeDrawingState( themeState
, true );
641 if ( cfString
!= NULL
)
642 CFRelease( cfString
);
648 #endif // wxUSE_LISTBOX