X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/be1aa07f8a8194fa6c947dd39e0ac98a59498728..5f5f809d57694ccca5c80245f98fe05170962194:/src/mac/carbon/listbox.cpp diff --git a/src/mac/carbon/listbox.cpp b/src/mac/carbon/listbox.cpp index 50e9f4445f..ba5b33e446 100644 --- a/src/mac/carbon/listbox.cpp +++ b/src/mac/carbon/listbox.cpp @@ -27,7 +27,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) BEGIN_EVENT_TABLE(wxListBox, wxControl) -#if !__WXMAC_OSX__ +#ifndef __WXMAC_OSX__ EVT_SIZE( wxListBox::OnSize ) EVT_CHAR( wxListBox::OnChar ) #endif @@ -39,6 +39,10 @@ END_EVENT_TABLE() const short kTextColumnId = 1024 ; // new databrowserbased version +// because of the limited insert +// functionality of DataBrowser, +// we just introduce id s corresponding +// to the line number // Listbox item wxListBox::wxListBox() @@ -74,44 +78,43 @@ static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrow if ( ref ) { wxListBox* list = wxDynamicCast( (wxObject*) ref , wxListBox ) ; - for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i ) - if ( list->m_idArray[i] == (long) itemID ) + int i = itemID - 1 ; + if (i >= 0 && i < list->GetCount() ) + { + bool trigger = false ; + wxCommandEvent event( + wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() ); + switch( message ) { - bool trigger = false ; - wxCommandEvent event( - wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() ); - switch( message ) - { - case kDataBrowserItemDeselected : - if ( list->HasMultipleSelection() ) - trigger = true ; - break ; - case kDataBrowserItemSelected : - trigger = true ; - break ; - case kDataBrowserItemDoubleClicked : - event.SetEventType(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED) ; + case kDataBrowserItemDeselected : + if ( list->HasMultipleSelection() ) trigger = true ; - break ; - default : - break ; - } - if ( trigger ) - { - event.SetEventObject( list ); - if ( list->HasClientObjectData() ) - event.SetClientObject( list->GetClientObject(i) ); - else if ( list->HasClientUntypedData() ) - event.SetClientData( list->GetClientData(i) ); - event.SetString( list->GetString(i) ); - event.SetInt(i) ; - 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 - // list->GetEventHandler()->ProcessEvent(event) ; - } - break ; + break ; + case kDataBrowserItemSelected : + trigger = true ; + break ; + case kDataBrowserItemDoubleClicked : + event.SetEventType(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED) ; + trigger = true ; + break ; + default : + break ; } + if ( trigger ) + { + event.SetEventObject( list ); + if ( list->HasClientObjectData() ) + event.SetClientObject( list->GetClientObject(i) ); + else if ( list->HasClientUntypedData() ) + event.SetClientData( list->GetClientData(i) ); + event.SetString( list->GetString(i) ); + event.SetInt(i) ; + 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 + // list->GetEventHandler()->ProcessEvent(event) ; + } + } } } @@ -133,14 +136,13 @@ static pascal OSStatus ListBoxGetSetItemData(ControlRef browser, if ( ref ) { wxListBox* list = wxDynamicCast( (wxObject*) ref , wxListBox ) ; - for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i ) - if ( list->m_idArray[i] == (long) itemID ) - { - wxMacCFStringHolder cf( list->GetString(i) , list->GetFont().GetEncoding() ) ; - verify_noerr( ::SetDataBrowserItemDataText( itemData , cf ) ) ; - err = noErr ; - break ; - } + int i = itemID - 1 ; + if (i >= 0 && i < list->GetCount() ) + { + wxMacCFStringHolder cf( list->GetString(i) , list->GetFont().GetEncoding() ) ; + verify_noerr( ::SetDataBrowserItemDataText( itemData , cf ) ) ; + err = noErr ; + } } } break; @@ -171,14 +173,11 @@ bool wxListBox::Create(wxWindow *parent, wxWindowID id, m_noItems = 0 ; // this will be increased by our append command m_selected = 0; - m_nextId = 1 ; - Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ; - ControlRef browser ; - verify_noerr( ::CreateDataBrowserControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, kDataBrowserListView , (ControlRef *)&m_macControl ) ); - browser = (ControlRef) m_macControl ; + m_peer = new wxMacControl() ; + verify_noerr( ::CreateDataBrowserControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, kDataBrowserListView , m_peer->GetControlRefAddr() ) ); DataBrowserSelectionFlags options = kDataBrowserDragSelect ; if ( style & wxLB_MULTIPLE ) @@ -193,7 +192,7 @@ bool wxListBox::Create(wxWindow *parent, wxWindowID id, { options += kDataBrowserSelectOnlyOne ; } - verify_noerr(SetDataBrowserSelectionFlags (browser, options ) ); + verify_noerr(m_peer->SetSelectionFlags( options ) ); DataBrowserListViewColumnDesc columnDesc ; columnDesc.headerBtnDesc.titleOffset = 0; @@ -221,11 +220,11 @@ bool wxListBox::Create(wxWindow *parent, wxWindowID id, kDataBrowserTableViewSelectionColumn ; - verify_noerr(::AddDataBrowserListViewColumn(browser, &columnDesc, kDataBrowserListViewAppendColumn) ) ; - verify_noerr(::AutoSizeDataBrowserListViewColumns( browser ) ) ; - verify_noerr(::SetDataBrowserHasScrollBars( browser , false , true ) ) ; - verify_noerr(::SetDataBrowserTableViewHiliteStyle( browser, kDataBrowserTableViewFillHilite ) ) ; - verify_noerr(::SetDataBrowserListViewHeaderBtnHeight( browser , 0 ) ) ; + verify_noerr(m_peer->AddListViewColumn( &columnDesc, kDataBrowserListViewAppendColumn) ) ; + verify_noerr(m_peer->AutoSizeListViewColumns() ) ; + verify_noerr(m_peer->SetHasScrollBars(false , true ) ) ; + verify_noerr(m_peer->SetTableViewHiliteStyle(kDataBrowserTableViewFillHilite ) ) ; + verify_noerr(m_peer->SetListViewHeaderBtnHeight( 0 ) ) ; DataBrowserCallbacks callbacks ; callbacks.version = kDataBrowserLatestCallbacks; @@ -241,7 +240,13 @@ bool wxListBox::Create(wxWindow *parent, wxWindowID id, #else NewDataBrowserItemNotificationUPP(DataBrowserItemNotificationProc) ; #endif - SetDataBrowserCallbacks(browser, &callbacks); + m_peer->SetCallbacks( &callbacks); + +#if TARGET_API_MAC_OSX + // there is a redraw bug in 10.2.X + if ( UMAGetSystemVersion() < 0x1030 ) + m_peer->SetData( kControlNoPart, kControlDataBrowserIncludesFrameAndFocusTag, (Boolean) false ) ; +#endif MacPostControlCreate(pos,size) ; @@ -257,7 +262,7 @@ bool wxListBox::Create(wxWindow *parent, wxWindowID id, wxListBox::~wxListBox() { - SetControlReference( (ControlRef) m_macControl , NULL ) ; + m_peer->SetReference( NULL ) ; FreeData() ; // avoid access during destruction if ( m_macList ) @@ -325,6 +330,8 @@ void wxListBox::Delete(int N) int wxListBox::DoAppend(const wxString& item) { + InvalidateBestSize(); + int index = m_noItems ; m_stringArray.Add( item ) ; m_dataArray.Add( NULL ); @@ -432,6 +439,21 @@ void wxListBox::Clear() void wxListBox::SetSelection(int N, bool select) { + if ( N == wxNOT_FOUND ) + { + // unselect everything + int sel = MacGetSelection() ; + if ( sel != wxNOT_FOUND ) + { + UInt32 id = sel + 1 ; + verify_noerr( + m_peer->SetSelectedItems( 1 , & id , kDataBrowserItemsRemove ) + ); + } + + return; + } + wxCHECK_RET( N >= 0 && N < m_noItems, wxT("invalid index in wxListBox::SetSelection") ); MacSetSelection( N , select ) ; @@ -512,16 +534,17 @@ void wxListBox::DoInsertItems(const wxArrayString& items, int pos) wxCHECK_RET( pos >= 0 && pos <= m_noItems, wxT("invalid index in wxListBox::InsertItems") ); + InvalidateBestSize(); + int nItems = items.GetCount(); for ( int i = 0 ; i < nItems ; i++ ) { m_stringArray.Insert( items[i] , pos + i ) ; m_dataArray.Insert( NULL , pos + i ) ; + m_noItems++ ; MacInsert( pos + i , items[i] ) ; } - - m_noItems += nItems; } void wxListBox::SetString(int N, const wxString& s) @@ -619,66 +642,76 @@ wxOwnerDrawn *wxListBox::CreateItem(size_t n) #endif //USE_OWNER_DRAWN + +// Some custom controls depend on this +/* static */ wxVisualAttributes +wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) +{ + wxVisualAttributes attr; + attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT); + attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX); + attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); + return attr; +} + // ============================================================================ // list box control implementation // ============================================================================ void wxListBox::MacDelete( int N ) { - UInt32 id = m_idArray[N] ; - verify_noerr(::RemoveDataBrowserItems((ControlRef) m_macControl , kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ; - m_idArray.RemoveAt( N ) ; + UInt32 id = m_noItems+1 ; + verify_noerr( m_peer->RemoveItems( kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ; + verify_noerr( m_peer->UpdateItems( kDataBrowserNoItem , 1 , (UInt32*) kDataBrowserNoItem , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ; } void wxListBox::MacInsert( int n , const wxString& text) { - verify_noerr(::AddDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , (UInt32*) &m_nextId , kDataBrowserItemNoProperty ) ) ; - m_idArray.Insert( m_nextId , n ) ; - ++m_nextId ; + UInt32 id = m_noItems ; // this has already been increased + verify_noerr( m_peer->AddItems( kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ; + verify_noerr( m_peer->UpdateItems( kDataBrowserNoItem , 1 , (UInt32*) kDataBrowserNoItem , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ; } void wxListBox::MacAppend( const wxString& text) { - verify_noerr(::AddDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , (UInt32*) &m_nextId , kDataBrowserItemNoProperty ) ) ; - m_idArray.Add( m_nextId ) ; - ++m_nextId ; + UInt32 id = m_noItems ; // this has already been increased + verify_noerr( m_peer->AddItems( kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ; } void wxListBox::MacClear() { - verify_noerr(::RemoveDataBrowserItems((ControlRef) m_macControl , kDataBrowserNoItem , 0 , NULL , kDataBrowserItemNoProperty ) ) ; - m_idArray.Empty() ; + verify_noerr( m_peer->RemoveItems( kDataBrowserNoItem , 0 , NULL , kDataBrowserItemNoProperty ) ) ; } void wxListBox::MacSetSelection( int n , bool select ) { - UInt32 id = m_idArray[n] ; + UInt32 id = n + 1 ; if ( !(GetWindowStyle() & (wxLB_MULTIPLE|wxLB_EXTENDED) ) ) { int n = MacGetSelection() ; if ( n >= 0 ) { - UInt32 idOld = m_idArray[n] ; - SetDataBrowserSelectedItems((ControlRef) m_macControl , 1 , & idOld , kDataBrowserItemsRemove ) ; + UInt32 idOld = n + 1 ; + m_peer->SetSelectedItems( 1 , & idOld , kDataBrowserItemsRemove ) ; } } - if ( ::IsDataBrowserItemSelected( (ControlRef) m_macControl , id ) != select ) + if ( m_peer->IsItemSelected( id ) != select ) { - verify_noerr(::SetDataBrowserSelectedItems((ControlRef) m_macControl , 1 , & id , kDataBrowserItemsToggle ) ) ; + verify_noerr(m_peer->SetSelectedItems( 1 , & id , kDataBrowserItemsToggle ) ) ; } MacScrollTo( n ) ; } bool wxListBox::MacIsSelected( int n ) const { - return ::IsDataBrowserItemSelected( (ControlRef) m_macControl , m_idArray[n] ) ; + return m_peer->IsItemSelected( n + 1 ) ; } int wxListBox::MacGetSelection() const { - for ( size_t i = 0 ; i < m_idArray.GetCount() ; ++i ) + for ( int i = 0 ; i < GetCount() ; ++i ) { - if ( ::IsDataBrowserItemSelected((ControlRef) m_macControl , m_idArray[i] ) ) + if ( m_peer->IsItemSelected( i + 1 ) ) { return i ; } @@ -691,9 +724,9 @@ int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const int no_sel = 0 ; aSelections.Empty(); - for ( size_t i = 0 ; i < m_idArray.GetCount() ; ++i ) + for ( int i = 0 ; i < GetCount() ; ++i ) { - if ( ::IsDataBrowserItemSelected((ControlRef) m_macControl , m_idArray[i] ) ) + if ( m_peer->IsItemSelected( i + 1 ) ) { aSelections.Add( i ) ; no_sel++ ; @@ -705,14 +738,14 @@ int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const void wxListBox::MacSet( int n , const wxString& text ) { // as we don't store the strings we only have to issue a redraw - UInt32 id = m_idArray[n] ; - verify_noerr( ::UpdateDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ; + UInt32 id = n + 1 ; + verify_noerr( m_peer->UpdateItems( kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ; } void wxListBox::MacScrollTo( int n ) { - UInt32 id = m_idArray[n] ; - verify_noerr( ::RevealDataBrowserItem((ControlRef) m_macControl , id , kTextColumnId , kDataBrowserRevealWithoutSelecting ) ) ; + UInt32 id = n + 1 ; + verify_noerr( m_peer->RevealItem( id , kTextColumnId , kDataBrowserRevealWithoutSelecting ) ) ; } #if !TARGET_API_MAC_OSX