X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/77ffb5937e89927b621128789401db8921fe580f..99d21c0e1f8d87570702c52dccd3ad698f0a0931:/src/mac/carbon/combobxc.cpp?ds=inline diff --git a/src/mac/carbon/combobxc.cpp b/src/mac/carbon/combobxc.cpp index ce47b87064..670cbf7b6d 100644 --- a/src/mac/carbon/combobxc.cpp +++ b/src/mac/carbon/combobxc.cpp @@ -1,33 +1,31 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: combobox.cpp -// Purpose: wxComboBox class +// Name: src/mac/carbon/combobxc.cpp +// Purpose: wxComboBox class using HIView ComboBox // Author: Stefan Csomor // Modified by: // Created: 1998-01-01 // RCS-ID: $Id$ // Copyright: (c) Stefan Csomor -// Licence: wxWidgets licence +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ -#pragma implementation "combobox.h" -#endif +#include "wx/wxprec.h" #include "wx/combobox.h" -#include "wx/button.h" -#include "wx/menu.h" + +#ifndef WX_PRECOMP + #include "wx/button.h" + #include "wx/menu.h" +#endif + #include "wx/mac/uma.h" #if TARGET_API_MAC_OSX #ifndef __HIVIEW__ - #include + #include #endif #endif -#if !USE_SHARED_LIBRARY IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) -#endif - -// composite combobox implementation by Dan "Bud" Keith bud@otsys.com #if TARGET_API_MAC_OSX #define USE_HICOMBOBOX 1 //use hi combobox define @@ -35,14 +33,56 @@ IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) #define USE_HICOMBOBOX 0 #endif -static int nextPopUpMenuId = 1000 ; -MenuHandle NewUniqueMenu() +static int nextPopUpMenuId = 1000; +MenuHandle NewUniqueMenu() { - MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" ) ; - nextPopUpMenuId++ ; - return handle ; + MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" ); + nextPopUpMenuId++; + return handle; } +#if USE_HICOMBOBOX +static const EventTypeSpec eventList[] = +{ + { kEventClassTextField , kEventTextAccepted } , +}; + +static pascal OSStatus wxMacComboBoxEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) +{ + OSStatus result = eventNotHandledErr; + wxComboBox* cb = (wxComboBox*) data; + + wxMacCarbonEvent cEvent( event ); + + switch( cEvent.GetClass() ) + { + case kEventClassTextField : + switch( cEvent.GetKind() ) + { + case kEventTextAccepted : + { + wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, cb->GetId() ); + event.SetInt( cb->GetSelection() ); + event.SetString( cb->GetStringSelection() ); + event.SetEventObject( cb ); + cb->GetEventHandler()->ProcessEvent( event ); + } + break; + default : + break; + } + break; + default : + break; + } + + + return result; +} + +DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacComboBoxEventHandler ) + +#endif // ---------------------------------------------------------------------------- // constants @@ -107,27 +147,23 @@ protected: // This will invoke the dialog default action, such // as the clicking the default button. - wxWindow *parent = GetParent(); - while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) { - parent = parent->GetParent() ; - } - if ( parent && parent->GetDefaultItem() ) + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + if ( tlw && tlw->GetDefaultItem() ) { - wxButton *def = wxDynamicCast(parent->GetDefaultItem(), - wxButton); + wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton); if ( def && def->IsEnabled() ) { wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); event.SetEventObject(def); def->Command(event); - return ; - } + return; + } } return; } } - + event.Skip(); } private: @@ -163,10 +199,10 @@ protected: } virtual wxSize DoGetBestSize() const { - wxSize sz = wxChoice::DoGetBestSize() ; - sz.x = POPUPWIDTH ; - return sz ; - } + wxSize sz = wxChoice::DoGetBestSize(); + sz.x = POPUPWIDTH; + return sz; + } private: wxComboBox *m_cb; @@ -175,14 +211,11 @@ private: }; BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice) - EVT_CHOICE(-1, wxComboBoxChoice::OnChoice) + EVT_CHOICE(wxID_ANY, wxComboBoxChoice::OnChoice) END_EVENT_TABLE() wxComboBox::~wxComboBox() { - // delete client objects - FreeData(); - // delete the controls now, don't leave them alive even though they would // still be eventually deleted by our parent - but it will be too late, the // user code expects them to be gone now @@ -204,14 +237,14 @@ wxComboBox::~wxComboBox() wxSize wxComboBox::DoGetBestSize() const { #if USE_HICOMBOBOX - return wxControl::DoGetBestSize(); + return wxControl::DoGetBestSize(); #else wxSize size = m_choice->GetBestSize(); - + if ( m_text != NULL ) { wxSize sizeText = m_text->GetBestSize(); - + size.x = POPUPWIDTH + sizeText.x + MARGIN; } @@ -221,25 +254,25 @@ wxSize wxComboBox::DoGetBestSize() const void wxComboBox::DoMoveWindow(int x, int y, int width, int height) { #if USE_HICOMBOBOX - wxControl::DoMoveWindow(x, y, width, height); + wxControl::DoMoveWindow(x, y, width, height); #else height = POPUPHEIGHT; - + wxControl::DoMoveWindow(x, y, width, height); if ( m_text == NULL ) { // we might not be fully constructed yet, therefore watch out... if ( m_choice ) - m_choice->SetSize(0, 0 , width, -1); + m_choice->SetSize(0, 0 , width, wxDefaultCoord); } else { wxCoord wText = width - POPUPWIDTH - MARGIN; m_text->SetSize(0, 0, wText, height); - m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, -1); + m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, wxDefaultCoord); } -#endif +#endif } @@ -251,23 +284,23 @@ void wxComboBox::DoMoveWindow(int x, int y, int width, int height) { bool wxComboBox::Enable(bool enable) { if ( !wxControl::Enable(enable) ) - return FALSE; + return false; - return TRUE; + return true; } bool wxComboBox::Show(bool show) { if ( !wxControl::Show(show) ) - return FALSE; + return false; - return TRUE; + return true; } void wxComboBox::SetFocus() { #if USE_HICOMBOBOX - wxControl::SetFocus(); + wxControl::SetFocus(); #else if ( m_text != NULL) { m_text->SetFocus(); @@ -316,51 +349,52 @@ bool wxComboBox::Create(wxWindow *parent, wxWindowID id, m_text = NULL; m_choice = NULL; #if USE_HICOMBOBOX - m_macIsUserPane = FALSE ; + m_macIsUserPane = false; #endif if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style , wxDefaultValidator, name) ) { - return FALSE; + return false; } #if USE_HICOMBOBOX - Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ; + Rect bounds = wxMacGetBoundsForControl( this , pos , size ); HIRect hiRect; - + hiRect.origin.x = 20; //bounds.left; hiRect.origin.y = 25; //bounds.top; hiRect.size.width = 120;// bounds.right - bounds.left; - hiRect.size.height = 24; - + hiRect.size.height = 24; + //For some reason, this code causes the combo box not to be displayed at all. //hiRect.origin.x = bounds.left; //hiRect.origin.y = bounds.top; //hiRect.size.width = bounds.right - bounds.left; //hiRect.size.height = bounds.bottom - bounds.top; //printf("left = %d, right = %d, top = %d, bottom = %d\n", bounds.left, bounds.right, bounds.top, bounds.bottom); - //printf("x = %d, y = %d, width = %d, height = %d\n", hibounds.origin.x, hibounds.origin.y, hibounds.size.width, hibounds.size.height); - verify_noerr( HIComboBoxCreate( &hiRect, CFSTR(""), NULL, NULL, kHIComboBoxStandardAttributes, (HIViewRef*) &m_macControl) ); + //printf("x = %d, y = %d, width = %d, height = %d\n", hibounds.origin.x, hibounds.origin.y, hibounds.size.width, hibounds.size.height); + m_peer = new wxMacControl(this); + verify_noerr( HIComboBoxCreate( &hiRect, CFSTR(""), NULL, NULL, kHIComboBoxStandardAttributes, m_peer->GetControlRefAddr() ) ); + - SetControl32BitMinimum( (ControlRef) m_macControl , 0 ) ; - SetControl32BitMaximum( (ControlRef) m_macControl , 100) ; + m_peer->SetMinimum( 0 ); + m_peer->SetMaximum( 100); if ( n > 0 ) - SetControl32BitValue( (ControlRef) m_macControl , 1 ) ; - - MacPostControlCreate(pos,size) ; - - for ( int i = 0 ; i < n ; i++ ) - { - DoAppend( choices[ i ] ); - } - - HIViewSetVisible( (HIViewRef) m_macControl, true ); + m_peer->SetValue( 1 ); + + MacPostControlCreate(pos,size); + + Append( choices[ i ] ); + + HIViewSetVisible( m_peer->GetControlRef(), true ); SetSelection(0); + EventHandlerRef comboEventHandler; + InstallControlEventHandler( m_peer->GetControlRef(), GetwxMacComboBoxEventHandlerUPP(), + GetEventTypeCount(eventList), eventList, this, + (EventHandlerRef *)&comboEventHandler); #else m_choice = new wxComboBoxChoice(this, style ); + m_choice->SetMinSize( wxSize( POPUPWIDTH , POPUPHEIGHT ) ); - m_choice = new wxComboBoxChoice(this, style ); - m_choice->SetSizeHints( wxSize( POPUPWIDTH , POPUPHEIGHT ) ) ; - wxSize csize = size; if ( style & wxCB_READONLY ) { @@ -369,32 +403,29 @@ bool wxComboBox::Create(wxWindow *parent, wxWindowID id, else { m_text = new wxComboBoxText(this); - if ( size.y == -1 ) { - csize.y = m_text->GetSize().y ; + if ( size.y == wxDefaultCoord ) { + csize.y = m_text->GetSize().y; } } - + DoSetSize(pos.x, pos.y, csize.x, csize.y); - - for ( int i = 0 ; i < n ; i++ ) - { - m_choice->DoAppend( choices[ i ] ); - } - SetBestSize(csize); // Needed because it is a wxControlWithItems + + m_choice->Append( n, choices ); + SetInitialSize(csize); // Needed because it is a wxControlWithItems #endif - return TRUE; + return true; } wxString wxComboBox::GetValue() const { #if USE_HICOMBOBOX CFStringRef myString; - HIComboBoxCopyTextItemAtIndex( (HIViewRef) m_macControl, (CFIndex)GetSelection(), &myString ); + HIComboBoxCopyTextItemAtIndex( m_peer->GetControlRef(), (CFIndex)GetSelection(), &myString ); return wxMacCFStringHolder( myString, m_font.GetEncoding() ).AsString(); #else wxString result; - + if ( m_text == NULL ) { result = m_choice->GetString( m_choice->GetSelection() ); @@ -403,7 +434,7 @@ wxString wxComboBox::GetValue() const { result = m_text->GetValue(); } - + return result; #endif } @@ -411,14 +442,14 @@ wxString wxComboBox::GetValue() const void wxComboBox::SetValue(const wxString& value) { #if USE_HICOMBOBOX - + #else int s = FindString (value); if (s == wxNOT_FOUND && !HasFlag(wxCB_READONLY) ) { - m_choice->Append(value) ; + m_choice->Append(value); } - SetStringSelection( value ) ; + SetStringSelection( value ); #endif } @@ -461,7 +492,7 @@ void wxComboBox::SetEditable(bool editable) int currentX, currentY; GetPosition( ¤tX, ¤tY ); - + int currentW, currentH; GetSize( ¤tW, ¤tH ); @@ -484,7 +515,7 @@ long wxComboBox::GetInsertionPoint() const return 0; } -long wxComboBox::GetLastPosition() const +wxTextPos wxComboBox::GetLastPosition() const { // TODO return 0; @@ -505,211 +536,258 @@ void wxComboBox::SetSelection(long from, long to) // TODO } -int wxComboBox::DoAppend(const wxString& item) +int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items, + unsigned int pos, + void **clientData, wxClientDataType type) { #if USE_HICOMBOBOX - CFIndex outIndex; - HIComboBoxAppendTextItem( (HIViewRef) m_macControl, wxMacCFStringHolder( item, m_font.GetEncoding() ), &outIndex ); - //SetControl32BitMaximum( (HIViewRef) m_macControl, GetCount() ); - return (int) outIndex; + const unsigned int count = items.GetCount(); + for ( unsigned int i = 0; i < count; ++i, ++pos ) + { + HIComboBoxInsertTextItemAtIndex(m_peer->GetControlRef(), + (CFIndex)pos, + wxMacCFStringHolder(items[i], + m_font.GetEncoding())); + AssignNewItemClientData(pos, clientData, i, type); + } + + //SetControl32BitMaximum( m_peer->GetControlRef(), GetCount() ); + + return pos - 1; #else - return m_choice->DoAppend( item ) ; + return m_choice->DoInsertItems( items, pos, clientData, type ); #endif } -int wxComboBox::DoInsert(const wxString& item, int pos) +void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData) { #if USE_HICOMBOBOX - HIComboBoxInsertTextItemAtIndex( (HIViewRef) m_macControl, (CFIndex)pos, wxMacCFStringHolder(item, m_font.GetEncoding()) ); - - //SetControl32BitMaximum( (HIViewRef) m_macControl, GetCount() ); - - return pos; + return; //TODO #else - return m_choice->DoInsert( item , pos ) ; + return m_choice->DoSetItemClientData( n , clientData ); #endif } -void wxComboBox::DoSetItemClientData(int n, void* clientData) +void* wxComboBox::DoGetItemClientData(unsigned int n) const { #if USE_HICOMBOBOX - return; //TODO + return NULL; //TODO +#else + return m_choice->DoGetItemClientData( n ); +#endif +} + +unsigned int wxComboBox::GetCount() const { +#if USE_HICOMBOBOX + return (unsigned int) HIComboBoxGetItemCount( m_peer->GetControlRef() ); #else - return m_choice->DoSetItemClientData( n , clientData ) ; + return m_choice->GetCount(); #endif } -void* wxComboBox::DoGetItemClientData(int n) const +void wxComboBox::DoDeleteOneItem(unsigned int n) { #if USE_HICOMBOBOX - return NULL; //TODO + HIComboBoxRemoveItemAtIndex( m_peer->GetControlRef(), (CFIndex)n ); #else - return m_choice->DoGetItemClientData( n ) ; + m_choice->Delete( n ); #endif } -void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData) +void wxComboBox::DoClear() { #if USE_HICOMBOBOX - return; //TODO + for ( CFIndex i = GetCount() - 1; i >= 0; ++ i ) + verify_noerr( HIComboBoxRemoveItemAtIndex( m_peer->GetControlRef(), i ) ); + m_peer->SetData(kHIComboBoxEditTextPart,kControlEditTextCFStringTag,CFSTR("")); #else - return m_choice->DoSetItemClientObject( n , clientData ) ; + m_choice->Clear(); #endif } -wxClientData* wxComboBox::DoGetItemClientObject(int n) const +int wxComboBox::GetSelection() const { #if USE_HICOMBOBOX - return NULL; + return FindString( GetStringSelection() ); #else - return m_choice->DoGetItemClientObject( n ) ; + return m_choice->GetSelection(); #endif } -void wxComboBox::FreeData() +void wxComboBox::SetSelection(int n) { - if ( HasClientObjectData() ) +#if USE_HICOMBOBOX + SetControl32BitValue( m_peer->GetControlRef() , n + 1 ); +#else + m_choice->SetSelection( n ); + + if ( m_text != NULL ) { - size_t count = GetCount(); - for ( size_t n = 0; n < count; n++ ) - { - SetClientObject( n, NULL ); - } + m_text->SetValue(GetString(n)); } +#endif } -int wxComboBox::GetCount() const { +int wxComboBox::FindString(const wxString& s, bool bCase) const +{ #if USE_HICOMBOBOX - return (int) HIComboBoxGetItemCount( (HIViewRef) m_macControl ); + for( unsigned int i = 0 ; i < GetCount() ; i++ ) + { + if (GetString(i).IsSameAs(s, bCase) ) + return i ; + } + return wxNOT_FOUND; #else - return m_choice->GetCount() ; + return m_choice->FindString( s, bCase ); #endif } -void wxComboBox::Delete(int n) +wxString wxComboBox::GetString(unsigned int n) const { #if USE_HICOMBOBOX - HIComboBoxRemoveItemAtIndex( (HIViewRef) m_macControl, (CFIndex)n ); + CFStringRef itemText; + HIComboBoxCopyTextItemAtIndex( m_peer->GetControlRef(), (CFIndex)n, &itemText ); + return wxMacCFStringHolder(itemText).AsString(); #else - // force client object deletion - if( HasClientObjectData() ) - SetClientObject( n, NULL ); - m_choice->Delete( n ); + return m_choice->GetString( n ); #endif } -void wxComboBox::Clear() +wxString wxComboBox::GetStringSelection() const { - FreeData(); #if USE_HICOMBOBOX - for ( CFIndex i = GetCount() - 1 ; i >= 0 ; ++ i ) - verify_noerr( HIComboBoxRemoveItemAtIndex( (HIViewRef) m_macControl, i ) ); - wxMacCFStringHolder cf(wxEmptyString,m_font.GetEncoding()) ; - CFStringRef cfr = cf ; - SetControlData((ControlRef) m_macControl,kHIComboBoxEditTextPart,kControlEditTextCFStringTag, - sizeof(CFStringRef),(Ptr) &cfr); + return wxMacCFStringHolder(m_peer->GetData(kHIComboBoxEditTextPart,kControlEditTextCFStringTag)).AsString(); #else - m_choice->Clear(); + int sel = GetSelection (); + if (sel != wxNOT_FOUND) + return wxString(this->GetString((unsigned int)sel)); + else + return wxEmptyString; #endif } -int wxComboBox::GetSelection() const +void wxComboBox::SetString(unsigned int n, const wxString& s) { #if USE_HICOMBOBOX - return FindString( GetStringSelection() ) ; + verify_noerr ( HIComboBoxInsertTextItemAtIndex( m_peer->GetControlRef(), (CFIndex) n, + wxMacCFStringHolder(s, m_font.GetEncoding()) ) ); + verify_noerr ( HIComboBoxRemoveItemAtIndex( m_peer->GetControlRef(), (CFIndex) n + 1 ) ); #else - return m_choice->GetSelection(); + m_choice->SetString( n , s ); #endif } -void wxComboBox::SetSelection(int n) +bool wxComboBox::IsEditable() const { #if USE_HICOMBOBOX - SetControl32BitValue( (ControlRef) m_macControl , n + 1 ) ; + // TODO + return !HasFlag(wxCB_READONLY); #else - m_choice->SetSelection( n ); - - if ( m_text != NULL ) - { - m_text->SetValue( GetString( n ) ); - } + return m_text != NULL && !HasFlag(wxCB_READONLY); #endif } -int wxComboBox::FindString(const wxString& s) const +void wxComboBox::Undo() { #if USE_HICOMBOBOX - for( int i = 0 ; i < GetCount() ; i++ ) - { - if ( GetString( i ).IsSameAs(s, FALSE) ) - return i ; - } - return wxNOT_FOUND ; + // TODO #else - return m_choice->FindString( s ); + if (m_text != NULL) + m_text->Undo(); #endif } -wxString wxComboBox::GetString(int n) const +void wxComboBox::Redo() { #if USE_HICOMBOBOX - CFStringRef itemText; - HIComboBoxCopyTextItemAtIndex( (HIViewRef) m_macControl, (CFIndex)n, &itemText ); - return wxMacCFStringHolder(itemText).AsString(); + // TODO #else - return m_choice->GetString( n ); + if (m_text != NULL) + m_text->Redo(); #endif } -wxString wxComboBox::GetStringSelection() const +void wxComboBox::SelectAll() { #if USE_HICOMBOBOX - CFStringRef cfr ; - verify_noerr(GetControlData((ControlRef) m_macControl,kHIComboBoxEditTextPart,kControlEditTextCFStringTag, - sizeof(CFStringRef),(Ptr) &cfr,NULL)); - // takes of release responsibility - wxMacCFStringHolder cf( cfr ) ; - return cf.AsString() ; + // TODO #else - int sel = GetSelection (); - if (sel > -1) - return wxString(this->GetString (sel)); + if (m_text != NULL) + m_text->SelectAll(); +#endif +} + +bool wxComboBox::CanCopy() const +{ +#if USE_HICOMBOBOX + // TODO + return false; +#else + if (m_text != NULL) + return m_text->CanCopy(); else - return wxEmptyString; + return false; #endif } -bool wxComboBox::SetStringSelection(const wxString& sel) +bool wxComboBox::CanCut() const { - int s = FindString (sel); - if (s > -1) - { - SetSelection (s); - return TRUE; - } +#if USE_HICOMBOBOX + // TODO + return false; +#else + if (m_text != NULL) + return m_text->CanCut(); else - return FALSE; + return false; +#endif } -void wxComboBox::SetString(int n, const wxString& s) +bool wxComboBox::CanPaste() const { #if USE_HICOMBOBOX - verify_noerr ( HIComboBoxInsertTextItemAtIndex( (HIViewRef) m_macControl, (CFIndex) n, - wxMacCFStringHolder(s, m_font.GetEncoding()) ) ); - verify_noerr ( HIComboBoxRemoveItemAtIndex( (HIViewRef) m_macControl, (CFIndex) n + 1 ) ); + // TODO + return false; +#else + if (m_text != NULL) + return m_text->CanPaste(); + else + return false; +#endif +} + +bool wxComboBox::CanUndo() const +{ +#if USE_HICOMBOBOX + // TODO + return false; #else - m_choice->SetString( n , s ) ; + if (m_text != NULL) + return m_text->CanUndo(); + else + return false; #endif } +bool wxComboBox::CanRedo() const +{ +#if USE_HICOMBOBOX + // TODO + return false; +#else + if (m_text != NULL) + return m_text->CanRedo(); + else + return false; +#endif +} -wxInt32 wxComboBox::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) ) +wxInt32 wxComboBox::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) ) { wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId ); event.SetInt(GetSelection()); event.SetEventObject(this); event.SetString(GetStringSelection()); ProcessCommand(event); - return noErr ; + return noErr; } -