diff --git a/include/wx/osx/choice.h b/include/wx/osx/choice.h index e261782ef6..4ef0b6b3aa 100644 --- a/include/wx/osx/choice.h +++ b/include/wx/osx/choice.h @@ -91,7 +91,6 @@ protected: wxArrayString m_strings; wxChoiceDataArray m_datas ; - wxMenu* m_popUpMenu ; private: // This should be called when the number of items in the control changes. diff --git a/include/wx/osx/core/private.h b/include/wx/osx/core/private.h index 8b02bc0f41..af0657ce8f 100644 --- a/include/wx/osx/core/private.h +++ b/include/wx/osx/core/private.h @@ -726,7 +726,7 @@ private: wxDECLARE_NO_COPY_CLASS(wxTextWidgetImpl); }; -// common interface for all implementations +// common interface for all combobox implementations class WXDLLIMPEXP_CORE wxComboWidgetImpl { @@ -753,6 +753,41 @@ public : virtual int FindString(const wxString& WXUNUSED(text)) const { return -1; } }; +// +// common interface for choice +// + +class WXDLLIMPEXP_CORE wxChoiceWidgetImpl + +{ +public : + wxChoiceWidgetImpl() {} + + virtual ~wxChoiceWidgetImpl() {} + + virtual int GetSelectedItem() const { return -1; } + + virtual void SetSelectedItem(int WXUNUSED(item)) {} + + virtual size_t GetNumberOfItems() const = 0; + + virtual void InsertItem(size_t pos, int itemid, const wxString& text) = 0; + + virtual void RemoveItem(size_t pos) = 0; + + virtual void Clear() + { + size_t count = GetNumberOfItems(); + for ( size_t i = 0 ; i < count ; i++ ) + { + RemoveItem( 0 ); + } + } + + virtual void SetItem(int pos, const wxString& item) = 0; +}; + + // // common interface for buttons // diff --git a/src/osx/choice_osx.cpp b/src/osx/choice_osx.cpp index 72449ab64d..fac0ccb173 100644 --- a/src/osx/choice_osx.cpp +++ b/src/osx/choice_osx.cpp @@ -30,7 +30,6 @@ wxChoice::~wxChoice() for ( i = 0; i < max; ++i ) delete GetClientObject( i ); } - delete m_popUpMenu; } bool wxChoice::Create(wxWindow *parent, @@ -70,10 +69,7 @@ bool wxChoice::Create(wxWindow *parent, if ( !wxChoiceBase::Create( parent, id, pos, size, style, validator, name ) ) return false; - m_popUpMenu = new wxMenu(); - m_popUpMenu->SetNoEventsMode(true); - - SetPeer(wxWidgetImpl::CreateChoice( this, parent, id, m_popUpMenu, pos, size, style, GetExtraStyle() )); + SetPeer(wxWidgetImpl::CreateChoice( this, parent, id, NULL, pos, size, style, GetExtraStyle() )); MacPostControlCreate( pos, size ); @@ -133,7 +129,7 @@ int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items, wxString text = items[i]; if (text.empty()) text = " "; // menu items can't have empty labels - m_popUpMenu->Insert( idx, i+1, text ); + dynamic_cast(GetPeer())->InsertItem( idx, i+1, text ); m_datas.Insert( NULL, idx ); AssignNewItemClientData(idx, clientData, i, type); } @@ -150,8 +146,7 @@ void wxChoice::DoDeleteOneItem(unsigned int n) if ( HasClientObjectData() ) delete GetClientObject( n ); - m_popUpMenu->Delete( m_popUpMenu->FindItemByPosition( n ) ); - + dynamic_cast(GetPeer())->RemoveItem(n); m_strings.RemoveAt( n ) ; m_datas.RemoveAt( n ) ; @@ -160,10 +155,7 @@ void wxChoice::DoDeleteOneItem(unsigned int n) void wxChoice::DoClear() { - for ( unsigned int i = 0 ; i < GetCount() ; i++ ) - { - m_popUpMenu->Delete( m_popUpMenu->FindItemByPosition( 0 ) ); - } + dynamic_cast(GetPeer())->Clear(); m_strings.Empty() ; m_datas.Empty() ; @@ -210,7 +202,7 @@ void wxChoice::SetString(unsigned int n, const wxString& s) m_strings[n] = s ; - m_popUpMenu->FindItemByPosition( n )->SetItemLabel( s ) ; + dynamic_cast(GetPeer())->SetItem(n,s); } wxString wxChoice::GetString(unsigned int n) const diff --git a/src/osx/cocoa/choice.mm b/src/osx/cocoa/choice.mm index 26dccc9d4b..bcad4b29b3 100644 --- a/src/osx/cocoa/choice.mm +++ b/src/osx/cocoa/choice.mm @@ -55,12 +55,22 @@ - (NSControlSize)controlSize; @end -class wxChoiceCocoaImpl : public wxWidgetCocoaImpl +class wxChoiceCocoaImpl : public wxWidgetCocoaImpl, public wxChoiceWidgetImpl { public: wxChoiceCocoaImpl(wxWindowMac *wxpeer, wxNSPopUpButton *v) : wxWidgetCocoaImpl(wxpeer, v) { + m_popUpMenu = new wxMenu(); + m_popUpMenu->SetNoEventsMode(true); + [v setMenu: m_popUpMenu->GetHMenu()]; + [v setAutoenablesItems:NO]; + + } + + ~wxChoiceCocoaImpl() + { + delete m_popUpMenu; } void GetLayoutInset(int &left , int &top , int &right, int &bottom) const wxOVERRIDE @@ -96,6 +106,29 @@ public: break; } } + + void InsertItem( size_t pos, int itemid, const wxString& text) wxOVERRIDE + { + m_popUpMenu->Insert( pos, itemid, text ); + } + + size_t GetNumberOfItems() const wxOVERRIDE + { + return m_popUpMenu->GetMenuItemCount(); + } + + void RemoveItem( size_t pos ) wxOVERRIDE + { + m_popUpMenu->Delete( m_popUpMenu->FindItemByPosition( pos ) ); + } + + void SetItem(int pos, const wxString& s) wxOVERRIDE + { + m_popUpMenu->FindItemByPosition( pos )->SetItemLabel( s ) ; + } + +private: + wxMenu* m_popUpMenu; }; wxWidgetImplType* wxWidgetImpl::CreateChoice( wxWindowMac* wxpeer, @@ -109,8 +142,6 @@ wxWidgetImplType* wxWidgetImpl::CreateChoice( wxWindowMac* wxpeer, { NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; wxNSPopUpButton* v = [[wxNSPopUpButton alloc] initWithFrame:r pullsDown:NO]; - [v setMenu: menu->GetHMenu()]; - [v setAutoenablesItems:NO]; wxWidgetCocoaImpl* c = new wxChoiceCocoaImpl( wxpeer, v ); return c; } diff --git a/src/osx/iphone/choice.mm b/src/osx/iphone/choice.mm new file mode 100644 index 0000000000..931fd14509 --- /dev/null +++ b/src/osx/iphone/choice.mm @@ -0,0 +1,142 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: src/osx/cocoa/choice.mm +// Purpose: wxChoice +// Author: Stefan Csomor +// Modified by: +// Created: 1998-01-01 +// Copyright: (c) Stefan Csomor +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +#include "wx/wxprec.h" + +#if wxUSE_CHOICE + +#include "wx/choice.h" + +#ifndef WX_PRECOMP + #include "wx/menu.h" + #include "wx/dcclient.h" +#endif + +#include "wx/osx/private.h" + +@interface wxUIPickerView : UIPickerView +{ +} + +@property (readonly) NSMutableArray *rows; + +@end + +@implementation wxUIPickerView + ++ (void)initialize +{ + static BOOL initialized = NO; + if (!initialized) + { + initialized = YES; + wxOSXIPhoneClassAddWXMethods( self ); + } +} + +- (id)initWithFrame:(CGRect)rectBox +{ + if ( self = [super initWithFrame:rectBox] ) + { + _rows = [[NSMutableArray alloc] init]; + } + return self; +} + +- (void)dealloc +{ + [_rows dealloc]; + [super dealloc]; +} + +- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView +{ + return 1; +} + +- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component +{ + if ( component == 0 ) + return [_rows count]; + + return 0; +} + +- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component +{ + return [_rows objectAtIndex:row]; +} + +- (int) intValue +{ + return (int) [self selectedRowInComponent:0]; +} + +- (void) setIntValue: (int) v +{ + [self selectRow:v inComponent:0 animated:NO]; +} + + + +@end + +class wxChoiceIPhoneImpl : public wxWidgetIPhoneImpl, public wxChoiceWidgetImpl +{ +public: + wxChoiceIPhoneImpl(wxWindowMac *wxpeer, wxUIPickerView *v) + : wxWidgetIPhoneImpl(wxpeer, v) + { + [v setDelegate:v]; + [v setDataSource:v]; + } + + void InsertItem( size_t pos, int itemid, const wxString& text) wxOVERRIDE + { + wxCFStringRef cftext(text); + [((wxUIPickerView*)m_osxView).rows insertObject:cftext.AsNSString() atIndex:pos]; + } + + size_t GetNumberOfItems() const wxOVERRIDE + { + return [((wxUIPickerView*)m_osxView).rows count]; + } + + void RemoveItem( size_t pos ) wxOVERRIDE + { + [((wxUIPickerView*)m_osxView).rows removeObjectAtIndex:pos]; + } + + void SetItem(int pos, const wxString& text) wxOVERRIDE + { + wxCFStringRef cftext(text); + [((wxUIPickerView*)m_osxView).rows replaceObjectAtIndex:pos withObject:cftext.AsNSString()]; + } + +private: +}; + +wxWidgetImplType* wxWidgetImpl::CreateChoice( wxWindowMac* wxpeer, + wxWindowMac* WXUNUSED(parent), + wxWindowID WXUNUSED(id), + wxMenu* menu, + const wxPoint& pos, + const wxSize& size, + long WXUNUSED(style), + long WXUNUSED(extraStyle)) +{ + CGRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; + wxUIPickerView* v = [[wxUIPickerView alloc] initWithFrame:r]; + + wxChoiceIPhoneImpl* c = new wxChoiceIPhoneImpl( wxpeer, v ); + return c; +} + +#endif // wxUSE_CHOICE