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
);
87 MacPostControlCreate( pos
, size
);
91 // Needed because it is a wxControlWithItems
92 SetInitialSize( size
);
97 wxListBox::~wxListBox()
100 m_peer
->SetReference( 0 );
103 void wxListBox::FreeData()
105 GetPeer()->MacClear();
108 void wxListBox::DoSetFirstItem(int n
)
110 GetPeer()->MacScrollTo( n
);
113 void wxListBox::EnsureVisible(int n
)
115 GetPeer()->MacScrollTo( n
);
118 void wxListBox::DoDeleteOneItem(unsigned int n
)
120 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::Delete") );
122 GetPeer()->MacDelete( n
);
125 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
128 wxClientDataType type
)
130 InvalidateBestSize();
132 GetPeer()->MacInsert( pos
, items
);
133 const unsigned int count
= items
.GetCount();
136 for (unsigned int i
= 0; i
< count
; ++i
)
137 AssignNewItemClientData( pos
+ i
, clientData
, i
, type
);
140 return pos
+ count
- 1;
143 int wxListBox::FindString(const wxString
& s
, bool bCase
) const
145 for ( size_t i
= 0; i
< GetCount(); ++ i
)
147 if (s
.IsSameAs( GetString( i
), bCase
) )
154 void wxListBox::DoClear()
159 void wxListBox::DoSetSelection(int n
, bool select
)
161 wxCHECK_RET( n
== wxNOT_FOUND
|| IsValid(n
),
162 wxT("invalid index in wxListBox::SetSelection") );
164 if ( n
== wxNOT_FOUND
)
165 GetPeer()->MacDeselectAll();
167 GetPeer()->MacSetSelection( n
, select
, HasMultipleSelection() );
170 bool wxListBox::IsSelected(int n
) const
172 wxCHECK_MSG( IsValid(n
), false, wxT("invalid index in wxListBox::Selected") );
174 return GetPeer()->MacIsSelected( n
);
177 void *wxListBox::DoGetItemClientData(unsigned int n
) const
179 wxCHECK_MSG( IsValid(n
), NULL
, wxT("invalid index in wxListBox::GetClientData"));
180 return GetPeer()->MacGetClientData( n
);
183 void wxListBox::DoSetItemClientData(unsigned int n
, void *clientData
)
185 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetClientData") );
186 GetPeer()->MacSetClientData( n
, clientData
);
189 // Return number of selections and an array of selected integers
190 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
192 return GetPeer()->MacGetSelections( aSelections
);
195 // Get single selection, for single choice list items
196 int wxListBox::GetSelection() const
198 return GetPeer()->MacGetSelection();
201 // Find string for position
202 wxString
wxListBox::GetString(unsigned int n
) const
204 wxCHECK_MSG( IsValid(n
), wxEmptyString
, wxT("invalid index in wxListBox::GetString") );
205 return GetPeer()->MacGetString(n
);
208 void wxListBox::SetString(unsigned int n
, const wxString
& s
)
210 GetPeer()->MacSetString( n
, s
);
213 wxSize
wxListBox::DoGetBestSize() const
215 int lbWidth
= 100; // some defaults
220 #if wxMAC_USE_CORE_GRAPHICS
221 wxClientDC
dc(const_cast<wxListBox
*>(this));
222 dc
.SetFont(GetFont());
224 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
)MacGetTopLevelWindowRef() ) );
226 // TODO: clean this up
229 ::TextFont( m_font
.MacGetFontNum() );
230 ::TextSize( m_font
.MacGetFontSize() );
231 ::TextFace( m_font
.MacGetFontStyle() );
235 ::TextFont( kFontIDMonaco
);
240 // Find the widest line
241 for (unsigned int i
= 0; i
< GetCount(); i
++)
243 wxString
str( GetString( i
) );
244 #if wxMAC_USE_CORE_GRAPHICS
245 wxCoord width
, height
;
246 dc
.GetTextExtent( str
, &width
, &height
);
250 Point bounds
= {0, 0};
253 // NB: what if m_font.Ok() == false ???
254 ::GetThemeTextDimensions(
255 wxMacCFStringHolder( str
, m_font
.GetEncoding() ),
256 kThemeCurrentPortFont
,
263 wLine
= ::TextWidth( str
.c_str(), 0, str
.length() );
266 lbWidth
= wxMax( lbWidth
, wLine
);
269 // Add room for the scrollbar
270 lbWidth
+= wxSystemSettings::GetMetric( wxSYS_VSCROLL_X
);
272 // And just a bit more
274 #if wxMAC_USE_CORE_GRAPHICS
275 wxCoord width
, height
;
276 dc
.GetTextExtent( wxT("XX") , &width
, &height
);
279 int cx
= ::TextWidth( "XX", 0, 1 );
283 // don't make the listbox too tall (limit height to around 10 items)
284 // but don't make it too small neither
285 lbHeight
= wxMax( (cy
+ 4) * wxMin( wxMax( GetCount(), 3 ), 10 ), 70 );
288 return wxSize( lbWidth
, lbHeight
);
291 unsigned int wxListBox::GetCount() const
293 return GetPeer()->MacGetCount();
296 void wxListBox::Refresh(bool eraseBack
, const wxRect
*rect
)
298 wxControl::Refresh( eraseBack
, rect
);
301 // Some custom controls depend on this
302 /* static */ wxVisualAttributes
303 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
305 wxVisualAttributes attr
;
307 attr
.colFg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
308 attr
.colBg
= wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
);
309 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
314 int wxListBox::DoListHitTest(const wxPoint
& inpoint
) const
318 // There are few reasons why this is complicated:
319 // 1) There is no native HitTest function for Mac
320 // 2) GetDataBrowserItemPartBounds only works on visible items
321 // 3) We can't do it through GetDataBrowserTableView[Item]RowHeight
322 // because what it returns is basically inaccurate in the context
323 // of the coordinates we want here, but we use this as a guess
324 // for where the first visible item lies
326 wxPoint point
= inpoint
;
328 // interestingly enough 10.2 (and below?) have GetDataBrowserItemPartBounds
329 // giving root window coordinates but 10.3 and above give client coordinates
330 // so we only compare using root window coordinates on 10.3 and up
331 if ( UMAGetSystemVersion() < 0x1030 )
332 MacClientToRootWindow(&point
.x
, &point
.y
);
334 // get column property ID (req. for call to itempartbounds)
335 DataBrowserTableViewColumnID colId
= 0;
336 err
= GetDataBrowserTableViewColumnProperty(m_peer
->GetControlRef(), 0, &colId
);
337 wxCHECK_MSG(err
== noErr
, wxNOT_FOUND
, wxT("Unexpected error from GetDataBrowserTableViewColumnProperty"));
339 // OK, first we need to find the first visible item we have -
340 // this will be the "low" for our binary search. There is no real
341 // easy way around this, as we will need to do a SLOW linear search
342 // until we find a visible item, but we can do a cheap calculation
343 // via the row height to speed things up a bit
344 UInt32 scrollx
, scrolly
;
345 err
= GetDataBrowserScrollPosition(m_peer
->GetControlRef(), &scrollx
, &scrolly
);
346 wxCHECK_MSG(err
== noErr
, wxNOT_FOUND
, wxT("Unexpected error from GetDataBrowserScrollPosition"));
349 err
= GetDataBrowserTableViewRowHeight(m_peer
->GetControlRef(), &height
);
350 wxCHECK_MSG(err
== noErr
, wxNOT_FOUND
, wxT("Unexpected error from GetDataBrowserTableViewRowHeight"));
352 // these indices are 0-based, as usual, so we need to add 1 to them when
353 // passing them to data browser functions which use 1-based indices
354 int low
= scrolly
/ height
,
355 high
= GetCount() - 1;
357 // search for the first visible item (note that the scroll guess above
358 // is the low bounds of where the item might lie so we only use that as a
359 // starting point - we should reach it within 1 or 2 iterations of the loop)
360 while ( low
<= high
)
363 err
= GetDataBrowserItemPartBounds(
364 m_peer
->GetControlRef(), low
+ 1, colId
,
365 kDataBrowserPropertyEnclosingPart
,
366 &bounds
); // note +1 to translate to Mac ID
370 // errDataBrowserItemNotFound is expected as it simply means that the
371 // item is not currently visible -- but other errors are not
372 wxCHECK_MSG( err
== errDataBrowserItemNotFound
, wxNOT_FOUND
,
373 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
378 // NOW do a binary search for where the item lies, searching low again if
379 // we hit an item that isn't visible
380 while ( low
<= high
)
382 int mid
= (low
+ high
) / 2;
385 err
= GetDataBrowserItemPartBounds(
386 m_peer
->GetControlRef(), mid
+ 1, colId
,
387 kDataBrowserPropertyEnclosingPart
,
388 &bounds
); //note +1 to trans to mac id
389 wxCHECK_MSG( err
== noErr
|| err
== errDataBrowserItemNotFound
,
391 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
393 if ( err
== errDataBrowserItemNotFound
)
395 // item not visible, attempt to find a visible one
399 else // visible item, do actual hitttest
401 // if point is within the bounds, return this item (since we assume
402 // all x coords of items are equal we only test the x coord in
404 if ((point
.x
>= bounds
.left
&& point
.x
<= bounds
.right
) &&
405 (point
.y
>= bounds
.top
&& point
.y
<= bounds
.bottom
) )
411 if ( point
.y
< bounds
.top
)
412 // index(bounds) greater then key(point)
415 // index(bounds) less then key(point)
423 // ============================================================================
424 // data browser based implementation
425 // ============================================================================
427 wxMacListBoxItem::wxMacListBoxItem()
432 wxMacListBoxItem::~wxMacListBoxItem()
436 void wxMacListBoxItem::Notification(wxMacDataItemBrowserControl
*owner
,
437 DataBrowserItemNotification message
,
438 DataBrowserItemDataRef itemData
) const
440 wxMacDataBrowserListControl
*lb
= wxDynamicCast(owner
,wxMacDataBrowserListControl
);
442 // we want to depend on as little as possible to make sure tear-down of controls is safe
444 if ( message
== kDataBrowserItemRemoved
)
446 if ( lb
!= NULL
&& lb
->GetClientDataType() == wxClientData_Object
)
448 delete (wxClientData
*) (m_data
);
455 wxListBox
*list
= wxDynamicCast( owner
->GetPeer() , wxListBox
);
456 wxCHECK_RET( list
!= NULL
, wxT("Listbox expected"));
458 bool trigger
= false;
459 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_SELECTED
, list
->GetId() );
462 case kDataBrowserItemDeselected
:
463 if ( list
->HasMultipleSelection() )
464 trigger
= !lb
->IsSelectionSuppressed();
467 case kDataBrowserItemSelected
:
468 trigger
= !lb
->IsSelectionSuppressed();
471 case kDataBrowserItemDoubleClicked
:
472 event
.SetEventType( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
);
482 event
.SetEventObject( list
);
483 if ( list
->HasClientObjectData() )
484 event
.SetClientObject( (wxClientData
*) m_data
);
485 else if ( list
->HasClientUntypedData() )
486 event
.SetClientData( m_data
);
487 event
.SetString( m_label
);
488 event
.SetInt( owner
->GetLineFromItem( this ) );
489 event
.SetExtraLong( list
->HasMultipleSelection() ? message
== kDataBrowserItemSelected
: true );
491 // direct notification is not always having the listbox GetSelection()
492 // having in synch with event, so use wxPostEvent instead
493 // list->GetEventHandler()->ProcessEvent(event);
495 wxPostEvent( list
->GetEventHandler(), event
);
499 IMPLEMENT_DYNAMIC_CLASS( wxMacDataBrowserListControl
, wxMacDataItemBrowserControl
)
501 wxMacDataBrowserListControl::wxMacDataBrowserListControl( wxWindow
*peer
, const wxPoint
& pos
, const wxSize
& size
, long style
)
502 : wxMacDataItemBrowserControl( peer
, pos
, size
, style
)
504 OSStatus err
= noErr
;
505 m_clientDataItemsType
= wxClientData_None
;
506 if ( style
& wxLB_SORT
)
507 m_sortOrder
= SortOrder_Text_Ascending
;
509 DataBrowserSelectionFlags options
= kDataBrowserDragSelect
;
510 if ( style
& wxLB_MULTIPLE
)
512 options
|= kDataBrowserAlwaysExtendSelection
| kDataBrowserCmdTogglesSelection
;
514 else if ( style
& wxLB_EXTENDED
)
516 options
|= kDataBrowserCmdTogglesSelection
;
520 options
|= kDataBrowserSelectOnlyOne
;
522 err
= SetSelectionFlags( options
);
525 DataBrowserListViewColumnDesc columnDesc
;
526 columnDesc
.headerBtnDesc
.titleOffset
= 0;
527 columnDesc
.headerBtnDesc
.version
= kDataBrowserListViewLatestHeaderDesc
;
529 columnDesc
.headerBtnDesc
.btnFontStyle
.flags
=
530 kControlUseFontMask
| kControlUseJustMask
;
532 columnDesc
.headerBtnDesc
.btnContentInfo
.contentType
= kControlNoContent
;
533 columnDesc
.headerBtnDesc
.btnFontStyle
.just
= teFlushDefault
;
534 columnDesc
.headerBtnDesc
.btnFontStyle
.font
= kControlFontViewSystemFont
;
535 columnDesc
.headerBtnDesc
.btnFontStyle
.style
= normal
;
536 columnDesc
.headerBtnDesc
.titleString
= NULL
;
538 columnDesc
.headerBtnDesc
.minimumWidth
= 0;
539 columnDesc
.headerBtnDesc
.maximumWidth
= 10000;
541 columnDesc
.propertyDesc
.propertyID
= kTextColumnId
;
542 columnDesc
.propertyDesc
.propertyType
= kDataBrowserTextType
;
543 columnDesc
.propertyDesc
.propertyFlags
= kDataBrowserTableViewSelectionColumn
;
544 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
545 columnDesc
.propertyDesc
.propertyFlags
|= kDataBrowserListViewTypeSelectColumn
;
548 verify_noerr( AddColumn( &columnDesc
, kDataBrowserListViewAppendColumn
) );
550 columnDesc
.headerBtnDesc
.minimumWidth
= 0;
551 columnDesc
.headerBtnDesc
.maximumWidth
= 0;
552 columnDesc
.propertyDesc
.propertyID
= kNumericOrderColumnId
;
553 columnDesc
.propertyDesc
.propertyType
= kDataBrowserPropertyRelevanceRankPart
;
554 columnDesc
.propertyDesc
.propertyFlags
= kDataBrowserTableViewSelectionColumn
;
555 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
556 columnDesc
.propertyDesc
.propertyFlags
|= kDataBrowserListViewTypeSelectColumn
;
559 verify_noerr( AddColumn( &columnDesc
, kDataBrowserListViewAppendColumn
) );
561 SetDataBrowserSortProperty( m_controlRef
, kTextColumnId
);
562 if ( m_sortOrder
== SortOrder_Text_Ascending
)
564 SetDataBrowserSortProperty( m_controlRef
, kTextColumnId
);
565 SetDataBrowserSortOrder( m_controlRef
, kDataBrowserOrderIncreasing
);
569 SetDataBrowserSortProperty( m_controlRef
, kNumericOrderColumnId
);
570 SetDataBrowserSortOrder( m_controlRef
, kDataBrowserOrderIncreasing
);
573 verify_noerr( AutoSizeColumns() );
574 verify_noerr( SetHiliteStyle(kDataBrowserTableViewFillHilite
) );
575 verify_noerr( SetHeaderButtonHeight( 0 ) );
576 err
= SetHasScrollBars( (style
& wxHSCROLL
) != 0 , true );
578 // shouldn't be necessary anymore under 10.2
579 m_peer
->SetData( kControlNoPart
, kControlDataBrowserIncludesFrameAndFocusTag
, (Boolean
)false );
580 m_peer
->SetNeedsFocusRect( true );
584 wxMacDataBrowserListControl::~wxMacDataBrowserListControl()
588 wxWindow
* wxMacDataBrowserListControl::GetPeer() const
590 return wxDynamicCast( wxMacControl::GetPeer() , wxWindow
);
593 wxMacDataItem
* wxMacDataBrowserListControl::CreateItem()
595 return new wxMacListBoxItem();
600 // in case we need that one day
602 // ============================================================================
603 // HIView owner-draw-based implementation
604 // ============================================================================
606 static pascal void ListBoxDrawProc(
607 ControlRef browser
, DataBrowserItemID item
, DataBrowserPropertyID property
,
608 DataBrowserItemState itemState
, const Rect
*itemRect
, SInt16 depth
, Boolean isColorDevice
)
610 CFStringRef cfString
;
611 ThemeDrawingState themeState
;
614 GetThemeDrawingState( &themeState
);
615 cfString
= CFStringCreateWithFormat( NULL
, NULL
, CFSTR("Row %d"), item
);
617 // In this sample we handle the "selected" state; all others fall through to our "active" state
618 if ( itemState
== kDataBrowserItemIsSelected
)
620 ThemeBrush colorBrushID
;
622 // TODO: switch over to wxSystemSettingsNative::GetColour() when kThemeBrushSecondaryHighlightColor
623 // is incorporated Panther DB starts using kThemeBrushSecondaryHighlightColor
624 // for inactive browser highlighting
625 Gestalt( gestaltSystemVersion
, &systemVersion
);
626 if ( (systemVersion
>= 0x00001030) && !IsControlActive( browser
) )
627 colorBrushID
= kThemeBrushSecondaryHighlightColor
;
629 colorBrushID
= kThemeBrushPrimaryHighlightColor
;
631 // First paint the hilite rect, then the text on top
632 SetThemePen( colorBrushID
, 32, true );
633 PaintRect( itemRect
);
634 SetThemeDrawingState( themeState
, false );
637 DrawThemeTextBox( cfString
, kThemeApplicationFont
, kThemeStateActive
, true, itemRect
, teFlushDefault
, NULL
);
638 SetThemeDrawingState( themeState
, true );
640 if ( cfString
!= NULL
)
641 CFRelease( cfString
);
647 #endif // wxUSE_LISTBOX