X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/83eabba429ab076322fbdddd1d011c73af814fe2..1c62bcbeeb1f0f7fafc72544acc7d9caf1d0df1d:/src/mac/carbon/listbox.cpp diff --git a/src/mac/carbon/listbox.cpp b/src/mac/carbon/listbox.cpp index 48eecddac0..6697768649 100644 --- a/src/mac/carbon/listbox.cpp +++ b/src/mac/carbon/listbox.cpp @@ -16,25 +16,20 @@ #include "wx/listbox.h" #ifndef WX_PRECOMP - #include "wx/dynarray.h" #include "wx/log.h" - #include "wx/app.h" + #include "wx/intl.h" #include "wx/utils.h" - #include "wx/button.h" + #include "wx/settings.h" + #include "wx/arrstr.h" + #include "wx/dcclient.h" #endif -#include "wx/settings.h" -#include "wx/toplevel.h" -#include "wx/intl.h" - IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) BEGIN_EVENT_TABLE(wxListBox, wxControl) END_EVENT_TABLE() #include "wx/mac/uma.h" -#include "wx/dynarray.h" -#include "wx/arrstr.h" // ============================================================================ // list box control implementation @@ -63,7 +58,8 @@ bool wxListBox::Create( wxMacListControl* wxListBox::GetPeer() const { - return dynamic_cast(m_peer); + wxMacDataBrowserListControl *lb = wxDynamicCast(m_peer,wxMacDataBrowserListControl); + return lb ? wx_static_cast(wxMacListControl*,lb) : 0 ; } bool wxListBox::Create( @@ -94,7 +90,7 @@ bool wxListBox::Create( InsertItems( n, choices, 0 ); // Needed because it is a wxControlWithItems - SetBestSize( size ); + SetInitialSize( size ); return true; } @@ -176,7 +172,7 @@ void wxListBox::DoSetSelection(int n, bool select) if ( n == wxNOT_FOUND ) GetPeer()->MacDeselectAll(); else - GetPeer()->MacSetSelection( n, select ); + GetPeer()->MacSetSelection( n, select, HasMultipleSelection() ); } bool wxListBox::IsSelected(int n) const @@ -248,6 +244,9 @@ wxSize wxListBox::DoGetBestSize() const int wLine; { +#if wxMAC_USE_CORE_GRAPHICS + wxClientDC dc(const_cast(this)); +#else wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef)MacGetTopLevelWindowRef() ) ); // TODO: clean this up @@ -263,12 +262,16 @@ wxSize wxListBox::DoGetBestSize() const ::TextSize( 9 ); ::TextFace( 0 ); } - +#endif // Find the widest line for (unsigned int i = 0; i < GetCount(); i++) { wxString str( GetString( i ) ); - +#if wxMAC_USE_CORE_GRAPHICS + wxCoord width, height ; + dc.GetTextExtent( str , &width, &height); + wLine = width ; +#else #if wxUSE_UNICODE Point bounds = {0, 0}; SInt16 baseline; @@ -287,6 +290,7 @@ wxSize wxListBox::DoGetBestSize() const #endif lbWidth = wxMax( lbWidth, wLine ); +#endif } // Add room for the scrollbar @@ -294,7 +298,13 @@ wxSize wxListBox::DoGetBestSize() const // And just a bit more int cy = 12; +#if wxMAC_USE_CORE_GRAPHICS + wxCoord width, height ; + dc.GetTextExtent( wxT("X") , &width, &height); + int cx = width ; +#else int cx = ::TextWidth( "X", 0, 1 ); +#endif lbWidth += cx; // don't make the listbox too tall (limit height to around 10 items) @@ -441,118 +451,20 @@ int wxListBox::DoListHitTest(const wxPoint& inpoint) const // data browser based implementation // ============================================================================ -const short kTextColumnId = 1024; -const short kNumericOrderColumnId = 1025; - wxMacListBoxItem::wxMacListBoxItem() + :wxMacDataItem() { - m_data = NULL; - m_order = 0; } wxMacListBoxItem::~wxMacListBoxItem() { } -void wxMacListBoxItem::SetOrder( SInt32 order ) -{ - m_order = order; -} - -SInt32 wxMacListBoxItem::GetOrder() const -{ - return m_order; -} - -void wxMacListBoxItem::SetData( void* data) -{ - m_data = data; -} - -void* wxMacListBoxItem::GetData() const -{ - return m_data; -} - -void wxMacListBoxItem::SetLabel( const wxString& str) -{ - m_label = str; - m_cfLabel.Assign( str , wxLocale::GetSystemEncoding()); -} - -const wxString& wxMacListBoxItem::GetLabel() const -{ - return m_label; -} - -bool wxMacListBoxItem::IsLessThan(wxMacDataItemBrowserControl *owner , - const wxMacDataItem* rhs, - DataBrowserPropertyID sortProperty) const -{ - const wxMacListBoxItem* otherItem = dynamic_cast(rhs); - bool retval = false; - switch (sortProperty) - { - case kTextColumnId: - retval = m_label.CmpNoCase( otherItem->m_label) < 0; - break; - - case kNumericOrderColumnId: - retval = m_order < otherItem->m_order; - break; - - default: - break; - }; - - return retval; -} - -OSStatus wxMacListBoxItem::GetSetData( wxMacDataItemBrowserControl *owner , - DataBrowserPropertyID property, - DataBrowserItemDataRef itemData, - bool changeValue ) -{ - OSStatus err = errDataBrowserPropertyNotSupported; - wxListBox *list = wxDynamicCast( owner->GetPeer() , wxListBox ); - wxCHECK_MSG( list != NULL , errDataBrowserPropertyNotSupported , wxT("Listbox expected")); - - if ( !changeValue ) - { - switch (property) - { - case kTextColumnId: - err = ::SetDataBrowserItemDataText( itemData, m_cfLabel ); - err = noErr; - break; - - case kNumericOrderColumnId: - err = ::SetDataBrowserItemDataValue( itemData, m_order ); - err = noErr; - break; - - default: - break; - } - } - else - { - switch (property) - { - // no editable props here - default: - break; - } - } - - return err; -} - void wxMacListBoxItem::Notification(wxMacDataItemBrowserControl *owner , DataBrowserItemNotification message, DataBrowserItemDataRef itemData ) const { - wxMacDataBrowserListControl *lb = dynamic_cast(owner); + wxMacDataBrowserListControl *lb = wxDynamicCast(owner,wxMacDataBrowserListControl); // we want to depend on as little as possible to make sure tear-down of controls is safe @@ -602,19 +514,24 @@ void wxMacListBoxItem::Notification(wxMacDataItemBrowserControl *owner , event.SetString( m_label ); event.SetInt( owner->GetLineFromItem( this ) ); event.SetExtraLong( list->HasMultipleSelection() ? message == kDataBrowserItemSelected : true ); - wxPostEvent( list->GetEventHandler(), event ); - // direct notification is not always having the listbox GetSelection() having in synch with event + // direct notification is not always having the listbox GetSelection() + // having in synch with event, so use wxPostEvent instead // list->GetEventHandler()->ProcessEvent(event); + + wxPostEvent( list->GetEventHandler(), event ); } } -wxMacDataBrowserListControl::wxMacDataBrowserListControl( wxListBox *peer, const wxPoint& pos, const wxSize& size, long style) +IMPLEMENT_DYNAMIC_CLASS( wxMacDataBrowserListControl , wxMacDataItemBrowserControl ) + +wxMacDataBrowserListControl::wxMacDataBrowserListControl( wxWindow *peer, const wxPoint& pos, const wxSize& size, long style) : wxMacDataItemBrowserControl( peer, pos, size, style ) { OSStatus err = noErr; m_clientDataItemsType = wxClientData_None; - m_stringSorted = style & wxLB_SORT; + if ( style & wxLB_SORT ) + m_sortOrder = SortOrder_Text_Ascending; DataBrowserSelectionFlags options = kDataBrowserDragSelect; if ( style & wxLB_MULTIPLE ) @@ -623,7 +540,7 @@ wxMacDataBrowserListControl::wxMacDataBrowserListControl( wxListBox *peer, const } else if ( style & wxLB_EXTENDED ) { - // default behaviour + options |= kDataBrowserCmdTogglesSelection; } else { @@ -669,7 +586,7 @@ wxMacDataBrowserListControl::wxMacDataBrowserListControl( wxListBox *peer, const verify_noerr( AddColumn( &columnDesc, kDataBrowserListViewAppendColumn ) ); SetDataBrowserSortProperty( m_controlRef , kTextColumnId); - if ( m_stringSorted ) + if ( m_sortOrder == SortOrder_Text_Ascending ) { SetDataBrowserSortProperty( m_controlRef , kTextColumnId); SetDataBrowserSortOrder( m_controlRef , kDataBrowserOrderIncreasing); @@ -695,217 +612,14 @@ wxMacDataBrowserListControl::~wxMacDataBrowserListControl() { } - -wxMacListBoxItem* wxMacDataBrowserListControl::CreateItem() -{ - return new wxMacListBoxItem(); -} - -wxListBox * wxMacDataBrowserListControl::GetPeer() const -{ - return wxDynamicCast( wxMacControl::GetPeer() , wxListBox ); -} - -wxClientDataType wxMacDataBrowserListControl::GetClientDataType() const -{ - return m_clientDataItemsType; -} -void wxMacDataBrowserListControl::SetClientDataType(wxClientDataType clientDataItemsType) -{ - m_clientDataItemsType = clientDataItemsType; -} - -unsigned int wxMacDataBrowserListControl::MacGetCount() const +wxWindow * wxMacDataBrowserListControl::GetPeer() const { - return GetItemCount(wxMacDataBrowserRootContainer,false,kDataBrowserItemAnyState); + return wxDynamicCast( wxMacControl::GetPeer() , wxWindow ); } -void wxMacDataBrowserListControl::MacDelete( unsigned int n ) +wxMacDataItem* wxMacDataBrowserListControl::CreateItem() { - wxMacListBoxItem* item = (wxMacListBoxItem*)GetItemFromLine( n ); - RemoveItem( wxMacDataBrowserRootContainer, item ); -} - -void wxMacDataBrowserListControl::MacInsert( unsigned int n, const wxString& text) -{ - wxMacListBoxItem* newItem = CreateItem(); - newItem->SetLabel( text ); - - if ( !m_stringSorted ) - { - // increase the order of the lines to be shifted - unsigned int lines = MacGetCount(); - for ( unsigned int i = n; i < lines; ++i) - { - wxMacListBoxItem* iter = (wxMacListBoxItem*) GetItemFromLine(i); - iter->SetOrder( iter->GetOrder() + 1 ); - } - - SInt32 frontLineOrder = 0; - if ( n > 0 ) - { - wxMacListBoxItem* iter = (wxMacListBoxItem*) GetItemFromLine(n-1); - frontLineOrder = iter->GetOrder(); - } - newItem->SetOrder( frontLineOrder + 1 ); - } - - AddItem( wxMacDataBrowserRootContainer, newItem ); -} - -void wxMacDataBrowserListControl::MacInsert( unsigned int n, const wxArrayString& items) -{ - size_t itemsCount = items.GetCount(); - if ( itemsCount == 0 ) - return; - - SInt32 frontLineOrder = 0; - - if ( !m_stringSorted ) - { - // increase the order of the lines to be shifted - unsigned int lines = MacGetCount(); - for ( unsigned int i = n; i < lines; ++i) - { - wxMacListBoxItem* iter = (wxMacListBoxItem*) GetItemFromLine(i); - iter->SetOrder( iter->GetOrder() + itemsCount ); - } - if ( n > 0 ) - { - wxMacListBoxItem* iter = (wxMacListBoxItem*) GetItemFromLine(n-1); - frontLineOrder = iter->GetOrder(); - } - } - - wxArrayMacDataItemPtr ids; - ids.SetCount( itemsCount ); - - for ( unsigned int i = 0; i < itemsCount; ++i ) - { - wxMacListBoxItem* item = CreateItem(); - item->SetLabel( items[i]); - if ( !m_stringSorted ) - item->SetOrder( frontLineOrder + 1 + i ); - - ids[i] = item; - } - - AddItems( wxMacDataBrowserRootContainer, ids ); -} - -int wxMacDataBrowserListControl::MacAppend( const wxString& text) -{ - wxMacListBoxItem* item = CreateItem(); - item->SetLabel( text ); - if ( !m_stringSorted ) - { - unsigned int lines = MacGetCount(); - if ( lines == 0 ) - item->SetOrder( 1 ); - else - { - wxMacListBoxItem* frontItem = (wxMacListBoxItem*) GetItemFromLine(lines-1); - item->SetOrder( frontItem->GetOrder() + 1 ); - } - } - AddItem( wxMacDataBrowserRootContainer, item ); - - return GetLineFromItem(item); -} - -void wxMacDataBrowserListControl::MacClear() -{ - wxMacDataItemBrowserSelectionSuppressor suppressor(this); - RemoveAllItems(wxMacDataBrowserRootContainer); -} - -void wxMacDataBrowserListControl::MacDeselectAll() -{ - wxMacDataItemBrowserSelectionSuppressor suppressor(this); - SetSelectedAllItems( kDataBrowserItemsRemove ); -} - -void wxMacDataBrowserListControl::MacSetSelection( unsigned int n, bool select ) -{ - wxMacListBoxItem* item = (wxMacListBoxItem*) GetItemFromLine(n); - wxMacDataItemBrowserSelectionSuppressor suppressor(this); - - if ( IsItemSelected( item ) != select ) - { - if ( select ) - SetSelectedItem( item, GetPeer()->HasMultipleSelection() ? kDataBrowserItemsAdd : kDataBrowserItemsAssign ); - else - SetSelectedItem( item, kDataBrowserItemsRemove ); - } - - MacScrollTo( n ); -} - -bool wxMacDataBrowserListControl::MacIsSelected( unsigned int n ) const -{ - wxMacListBoxItem* item = (wxMacListBoxItem*) GetItemFromLine(n); - return IsItemSelected( item ); -} - -int wxMacDataBrowserListControl::MacGetSelection() const -{ - wxMacDataItemPtr first, last; - GetSelectionAnchor( &first, &last ); - - if ( first != NULL ) - { - return GetLineFromItem( first ); - } - - return -1; -} - -int wxMacDataBrowserListControl::MacGetSelections( wxArrayInt& aSelections ) const -{ - aSelections.Empty(); - wxArrayMacDataItemPtr selectedItems; - GetItems( wxMacDataBrowserRootContainer, false , kDataBrowserItemIsSelected, selectedItems); - - int count = selectedItems.GetCount(); - - for ( int i = 0; i < count; ++i) - { - aSelections.Add(GetLineFromItem(selectedItems[i])); - } - - return count; -} - -void wxMacDataBrowserListControl::MacSetString( unsigned int n, const wxString& text ) -{ - // as we don't store the strings we only have to issue a redraw - wxMacListBoxItem* item = (wxMacListBoxItem*) GetItemFromLine( n); - item->SetLabel( text ); - UpdateItem( wxMacDataBrowserRootContainer, item , kTextColumnId ); -} - -wxString wxMacDataBrowserListControl::MacGetString( unsigned int n ) const -{ - wxMacListBoxItem * item = (wxMacListBoxItem*) GetItemFromLine( n ); - return item->GetLabel(); -} - -void wxMacDataBrowserListControl::MacSetClientData( unsigned int n, void * data) -{ - wxMacListBoxItem* item = (wxMacListBoxItem*) GetItemFromLine( n); - item->SetData( data ); - // not displayed, therefore no Update infos to DataBrowser -} - -void * wxMacDataBrowserListControl::MacGetClientData( unsigned int n) const -{ - wxMacListBoxItem * item = (wxMacListBoxItem*) GetItemFromLine( n ); - return item->GetData(); -} - -void wxMacDataBrowserListControl::MacScrollTo( unsigned int n ) -{ - RevealItem( GetItemFromLine( n) , kDataBrowserRevealWithoutSelecting ); + return new wxMacListBoxItem(); } #if 0 @@ -957,4 +671,4 @@ static pascal void ListBoxDrawProc( #endif -#endif +#endif // wxUSE_LISTBOX