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 // TODO CHECK 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
);
125 UpdateOldSelections();
128 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
131 wxClientDataType type
)
133 InvalidateBestSize();
135 GetPeer()->MacInsert( pos
, items
);
136 const unsigned int count
= items
.GetCount();
139 for (unsigned int i
= 0; i
< count
; ++i
)
140 AssignNewItemClientData( pos
+ i
, clientData
, i
, type
);
143 return pos
+ count
- 1;
145 UpdateOldSelections();
148 int wxListBox::FindString(const wxString
& s
, bool bCase
) const
150 for ( size_t i
= 0; i
< GetCount(); ++ i
)
152 if (s
.IsSameAs( GetString( i
), bCase
) )
159 void wxListBox::DoClear()
163 UpdateOldSelections();
166 void wxListBox::DoSetSelection(int n
, bool select
)
168 wxCHECK_RET( n
== wxNOT_FOUND
|| IsValid(n
),
169 wxT("invalid index in wxListBox::SetSelection") );
171 if ( n
== wxNOT_FOUND
)
172 GetPeer()->MacDeselectAll();
174 GetPeer()->MacSetSelection( n
, select
, HasMultipleSelection() );
176 UpdateOldSelections();
179 bool wxListBox::IsSelected(int n
) const
181 wxCHECK_MSG( IsValid(n
), false, wxT("invalid index in wxListBox::Selected") );
183 return GetPeer()->MacIsSelected( n
);
186 void *wxListBox::DoGetItemClientData(unsigned int n
) const
188 wxCHECK_MSG( IsValid(n
), NULL
, wxT("invalid index in wxListBox::GetClientData"));
189 return GetPeer()->MacGetClientData( n
);
192 void wxListBox::DoSetItemClientData(unsigned int n
, void *clientData
)
194 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetClientData") );
195 GetPeer()->MacSetClientData( n
, clientData
);
198 // Return number of selections and an array of selected integers
199 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
201 return GetPeer()->MacGetSelections( aSelections
);
204 // Get single selection, for single choice list items
205 int wxListBox::GetSelection() const
207 return GetPeer()->MacGetSelection();
210 // Find string for position
211 wxString
wxListBox::GetString(unsigned int n
) const
213 wxCHECK_MSG( IsValid(n
), wxEmptyString
, wxT("invalid index in wxListBox::GetString") );
214 return GetPeer()->MacGetString(n
);
217 void wxListBox::SetString(unsigned int n
, const wxString
& s
)
219 GetPeer()->MacSetString( n
, s
);
222 wxSize
wxListBox::DoGetBestSize() const
224 int lbWidth
= 100; // some defaults
229 wxClientDC
dc(const_cast<wxListBox
*>(this));
230 dc
.SetFont(GetFont());
232 // Find the widest line
233 for (unsigned int i
= 0; i
< GetCount(); i
++)
235 wxString
str( GetString( i
) );
237 wxCoord width
, height
;
238 dc
.GetTextExtent( str
, &width
, &height
);
240 lbWidth
= wxMax( lbWidth
, wLine
);
243 // Add room for the scrollbar
244 lbWidth
+= wxSystemSettings::GetMetric( wxSYS_VSCROLL_X
);
246 // And just a bit more
249 wxCoord width
, height
;
250 dc
.GetTextExtent( wxT("XX") , &width
, &height
);
254 // don't make the listbox too tall (limit height to around 10 items)
255 // but don't make it too small neither
256 lbHeight
= wxMax( (cy
+ 4) * wxMin( wxMax( GetCount(), 3 ), 10 ), 70 );
259 return wxSize( lbWidth
, lbHeight
);
262 unsigned int wxListBox::GetCount() const
264 return GetPeer()->MacGetCount();
267 void wxListBox::Refresh(bool eraseBack
, const wxRect
*rect
)
269 wxControl::Refresh( eraseBack
, rect
);
272 // Some custom controls depend on this
273 /* static */ wxVisualAttributes
274 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
276 wxVisualAttributes attr
;
278 attr
.colFg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
279 attr
.colBg
= wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
);
280 attr
.font
.MacCreateFromThemeFont(kThemeViewsFont
);
285 int wxListBox::DoListHitTest(const wxPoint
& inpoint
) const
289 // There are few reasons why this is complicated:
290 // 1) There is no native HitTest function for Mac
291 // 2) GetDataBrowserItemPartBounds only works on visible items
292 // 3) We can't do it through GetDataBrowserTableView[Item]RowHeight
293 // because what it returns is basically inaccurate in the context
294 // of the coordinates we want here, but we use this as a guess
295 // for where the first visible item lies
297 wxPoint point
= inpoint
;
299 // get column property ID (req. for call to itempartbounds)
300 DataBrowserTableViewColumnID colId
= 0;
301 err
= GetDataBrowserTableViewColumnProperty(m_peer
->GetControlRef(), 0, &colId
);
302 wxCHECK_MSG(err
== noErr
, wxNOT_FOUND
, wxT("Unexpected error from GetDataBrowserTableViewColumnProperty"));
304 // OK, first we need to find the first visible item we have -
305 // this will be the "low" for our binary search. There is no real
306 // easy way around this, as we will need to do a SLOW linear search
307 // until we find a visible item, but we can do a cheap calculation
308 // via the row height to speed things up a bit
309 UInt32 scrollx
, scrolly
;
310 err
= GetDataBrowserScrollPosition(m_peer
->GetControlRef(), &scrollx
, &scrolly
);
311 wxCHECK_MSG(err
== noErr
, wxNOT_FOUND
, wxT("Unexpected error from GetDataBrowserScrollPosition"));
314 err
= GetDataBrowserTableViewRowHeight(m_peer
->GetControlRef(), &height
);
315 wxCHECK_MSG(err
== noErr
, wxNOT_FOUND
, wxT("Unexpected error from GetDataBrowserTableViewRowHeight"));
317 // these indices are 0-based, as usual, so we need to add 1 to them when
318 // passing them to data browser functions which use 1-based indices
319 int low
= scrolly
/ height
,
320 high
= GetCount() - 1;
322 // search for the first visible item (note that the scroll guess above
323 // is the low bounds of where the item might lie so we only use that as a
324 // starting point - we should reach it within 1 or 2 iterations of the loop)
325 while ( low
<= high
)
328 err
= GetDataBrowserItemPartBounds(
329 m_peer
->GetControlRef(), low
+ 1, colId
,
330 kDataBrowserPropertyEnclosingPart
,
331 &bounds
); // note +1 to translate to Mac ID
335 // errDataBrowserItemNotFound is expected as it simply means that the
336 // item is not currently visible -- but other errors are not
337 wxCHECK_MSG( err
== errDataBrowserItemNotFound
, wxNOT_FOUND
,
338 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
343 // NOW do a binary search for where the item lies, searching low again if
344 // we hit an item that isn't visible
345 while ( low
<= high
)
347 int mid
= (low
+ high
) / 2;
350 err
= GetDataBrowserItemPartBounds(
351 m_peer
->GetControlRef(), mid
+ 1, colId
,
352 kDataBrowserPropertyEnclosingPart
,
353 &bounds
); //note +1 to trans to mac id
354 wxCHECK_MSG( err
== noErr
|| err
== errDataBrowserItemNotFound
,
356 wxT("Unexpected error from GetDataBrowserItemPartBounds") );
358 if ( err
== errDataBrowserItemNotFound
)
360 // item not visible, attempt to find a visible one
364 else // visible item, do actual hitttest
366 // if point is within the bounds, return this item (since we assume
367 // all x coords of items are equal we only test the x coord in
369 if ((point
.x
>= bounds
.left
&& point
.x
<= bounds
.right
) &&
370 (point
.y
>= bounds
.top
&& point
.y
<= bounds
.bottom
) )
376 if ( point
.y
< bounds
.top
)
377 // index(bounds) greater then key(point)
380 // index(bounds) less then key(point)
388 // ============================================================================
389 // data browser based implementation
390 // ============================================================================
392 wxMacListBoxItem::wxMacListBoxItem()
397 wxMacListBoxItem::~wxMacListBoxItem()
401 void wxMacListBoxItem::Notification(wxMacDataItemBrowserControl
*owner
,
402 DataBrowserItemNotification message
,
403 DataBrowserItemDataRef
WXUNUSED(itemData
) ) const
405 wxMacDataBrowserListControl
*lb
= wxDynamicCast(owner
,wxMacDataBrowserListControl
);
407 // we want to depend on as little as possible to make sure tear-down of controls is safe
409 if ( message
== kDataBrowserItemRemoved
)
411 if ( lb
!= NULL
&& lb
->GetClientDataType() == wxClientData_Object
)
413 delete (wxClientData
*) (m_data
);
420 wxListBox
*list
= wxDynamicCast( owner
->GetPeer() , wxListBox
);
421 wxCHECK_RET( list
!= NULL
, wxT("Listbox expected"));
425 if (list
->HasMultipleSelection() && (message
== kDataBrowserSelectionSetChanged
))
427 list
->CalcAndSendEvent();
431 if (list
->HasMultipleSelection() && ((message
== kDataBrowserItemSelected
) || (message
== kDataBrowserItemDeselected
)))
433 list
->CalcAndSendEvent();
438 bool trigger
= false;
439 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_SELECTED
, list
->GetId() );
442 case kDataBrowserItemSelected
:
443 trigger
= !lb
->IsSelectionSuppressed();
446 case kDataBrowserItemDoubleClicked
:
447 event
.SetEventType( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
);
457 event
.SetEventObject( list
);
458 if ( list
->HasClientObjectData() )
459 event
.SetClientObject( (wxClientData
*) m_data
);
460 else if ( list
->HasClientUntypedData() )
461 event
.SetClientData( m_data
);
462 event
.SetString( m_label
);
463 event
.SetInt( owner
->GetLineFromItem( this ) );
464 event
.SetExtraLong( list
->HasMultipleSelection() ? message
== kDataBrowserItemSelected
: true );
466 // direct notification is not always having the listbox GetSelection()
467 // having in synch with event, so use wxPostEvent instead
468 // list->HandleWindowEvent(event);
470 wxPostEvent( list
->GetEventHandler(), event
);
474 IMPLEMENT_DYNAMIC_CLASS( wxMacDataBrowserListControl
, wxMacDataItemBrowserControl
)
476 wxMacDataBrowserListControl::wxMacDataBrowserListControl( wxWindow
*peer
, const wxPoint
& pos
, const wxSize
& size
, long style
)
477 : wxMacDataItemBrowserControl( peer
, pos
, size
, style
)
479 OSStatus err
= noErr
;
480 m_clientDataItemsType
= wxClientData_None
;
481 if ( style
& wxLB_SORT
)
482 m_sortOrder
= SortOrder_Text_Ascending
;
484 DataBrowserSelectionFlags options
= kDataBrowserDragSelect
;
485 if ( style
& wxLB_MULTIPLE
)
487 options
|= kDataBrowserAlwaysExtendSelection
| kDataBrowserCmdTogglesSelection
;
489 else if ( style
& wxLB_EXTENDED
)
491 options
|= kDataBrowserCmdTogglesSelection
;
495 options
|= kDataBrowserSelectOnlyOne
;
497 err
= SetSelectionFlags( options
);
500 DataBrowserListViewColumnDesc columnDesc
;
501 columnDesc
.headerBtnDesc
.titleOffset
= 0;
502 columnDesc
.headerBtnDesc
.version
= kDataBrowserListViewLatestHeaderDesc
;
504 columnDesc
.headerBtnDesc
.btnFontStyle
.flags
=
505 kControlUseFontMask
| kControlUseJustMask
;
507 columnDesc
.headerBtnDesc
.btnContentInfo
.contentType
= kControlNoContent
;
508 columnDesc
.headerBtnDesc
.btnFontStyle
.just
= teFlushDefault
;
509 columnDesc
.headerBtnDesc
.btnFontStyle
.font
= kControlFontViewSystemFont
;
510 columnDesc
.headerBtnDesc
.btnFontStyle
.style
= normal
;
511 columnDesc
.headerBtnDesc
.titleString
= NULL
;
513 columnDesc
.headerBtnDesc
.minimumWidth
= 0;
514 columnDesc
.headerBtnDesc
.maximumWidth
= 10000;
516 columnDesc
.propertyDesc
.propertyID
= kTextColumnId
;
517 columnDesc
.propertyDesc
.propertyType
= kDataBrowserTextType
;
518 columnDesc
.propertyDesc
.propertyFlags
= kDataBrowserTableViewSelectionColumn
;
519 columnDesc
.propertyDesc
.propertyFlags
|= kDataBrowserListViewTypeSelectColumn
;
521 verify_noerr( AddColumn( &columnDesc
, kDataBrowserListViewAppendColumn
) );
523 columnDesc
.headerBtnDesc
.minimumWidth
= 0;
524 columnDesc
.headerBtnDesc
.maximumWidth
= 0;
525 columnDesc
.propertyDesc
.propertyID
= kNumericOrderColumnId
;
526 columnDesc
.propertyDesc
.propertyType
= kDataBrowserPropertyRelevanceRankPart
;
527 columnDesc
.propertyDesc
.propertyFlags
= kDataBrowserTableViewSelectionColumn
;
528 columnDesc
.propertyDesc
.propertyFlags
|= kDataBrowserListViewTypeSelectColumn
;
530 verify_noerr( AddColumn( &columnDesc
, kDataBrowserListViewAppendColumn
) );
532 SetDataBrowserSortProperty( m_controlRef
, kTextColumnId
);
533 if ( m_sortOrder
== SortOrder_Text_Ascending
)
535 SetDataBrowserSortProperty( m_controlRef
, kTextColumnId
);
536 SetDataBrowserSortOrder( m_controlRef
, kDataBrowserOrderIncreasing
);
540 SetDataBrowserSortProperty( m_controlRef
, kNumericOrderColumnId
);
541 SetDataBrowserSortOrder( m_controlRef
, kDataBrowserOrderIncreasing
);
544 verify_noerr( AutoSizeColumns() );
545 verify_noerr( SetHiliteStyle(kDataBrowserTableViewFillHilite
) );
546 verify_noerr( SetHeaderButtonHeight( 0 ) );
547 err
= SetHasScrollBars( (style
& wxHSCROLL
) != 0 , true );
549 // shouldn't be necessary anymore under 10.2
550 m_peer
->SetData( kControlNoPart
, kControlDataBrowserIncludesFrameAndFocusTag
, (Boolean
)false );
551 m_peer
->SetNeedsFocusRect( true );
555 wxMacDataBrowserListControl::~wxMacDataBrowserListControl()
559 wxWindow
* wxMacDataBrowserListControl::GetPeer() const
561 return wxDynamicCast( wxMacControl::GetPeer() , wxWindow
);
564 wxMacDataItem
* wxMacDataBrowserListControl::CreateItem()
566 return new wxMacListBoxItem();
571 // in case we need that one day
573 // ============================================================================
574 // HIView owner-draw-based implementation
575 // ============================================================================
577 static pascal void ListBoxDrawProc(
578 ControlRef browser
, DataBrowserItemID item
, DataBrowserPropertyID property
,
579 DataBrowserItemState itemState
, const Rect
*itemRect
, SInt16 depth
, Boolean isColorDevice
)
581 CFStringRef cfString
;
582 ThemeDrawingState themeState
;
585 GetThemeDrawingState( &themeState
);
586 cfString
= CFStringCreateWithFormat( NULL
, NULL
, CFSTR("Row %d"), item
);
588 // In this sample we handle the "selected" state; all others fall through to our "active" state
589 if ( itemState
== kDataBrowserItemIsSelected
)
591 ThemeBrush colorBrushID
;
593 // TODO: switch over to wxSystemSettingsNative::GetColour() when kThemeBrushSecondaryHighlightColor
594 // is incorporated Panther DB starts using kThemeBrushSecondaryHighlightColor
595 // for inactive browser highlighting
596 if ( !IsControlActive( browser
) )
597 colorBrushID
= kThemeBrushSecondaryHighlightColor
;
599 colorBrushID
= kThemeBrushPrimaryHighlightColor
;
601 // First paint the hilite rect, then the text on top
602 SetThemePen( colorBrushID
, 32, true );
603 PaintRect( itemRect
);
604 SetThemeDrawingState( themeState
, false );
607 DrawThemeTextBox( cfString
, kThemeApplicationFont
, kThemeStateActive
, true, itemRect
, teFlushDefault
, NULL
);
608 SetThemeDrawingState( themeState
, true );
610 if ( cfString
!= NULL
)
611 CFRelease( cfString
);
617 #endif // wxUSE_LISTBOX