1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/choice_osx.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/choice.h"
20 #include "wx/dcclient.h"
23 #include "wx/osx/private.h"
27 if ( HasClientObjectData() )
29 unsigned int i
, max
= GetCount();
31 for ( i
= 0; i
< max
; ++i
)
32 delete GetClientObject( i
);
37 bool wxChoice::Create(wxWindow
*parent
,
41 const wxArrayString
& choices
,
43 const wxValidator
& validator
,
44 const wxString
& name
)
46 if ( !Create( parent
, id
, pos
, size
, 0, NULL
, style
, validator
, name
) )
51 if ( !choices
.empty() )
54 SetInitialSize( size
);
59 bool wxChoice::Create(wxWindow
*parent
,
64 const wxString choices
[],
66 const wxValidator
& validator
,
67 const wxString
& name
)
71 if ( !wxChoiceBase::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
74 m_popUpMenu
= new wxMenu();
75 m_popUpMenu
->SetNoEventsMode(true);
77 SetPeer(wxWidgetImpl::CreateChoice( this, parent
, id
, m_popUpMenu
, pos
, size
, style
, GetExtraStyle() ));
79 MacPostControlCreate( pos
, size
);
81 #if !wxUSE_STD_CONTAINERS
82 if ( style
& wxCB_SORT
)
84 m_strings
= wxArrayString( 1 );
89 // Set the first item as being selected
93 // Needed because it is a wxControlWithItems
94 SetInitialSize( size
);
99 // ----------------------------------------------------------------------------
100 // adding/deleting items to/from the list
101 // ----------------------------------------------------------------------------
103 void wxChoice::DoAfterItemCountChange()
105 InvalidateBestSize();
107 GetPeer()->SetMaximum( GetCount() );
110 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
112 void **clientData
, wxClientDataType type
)
114 const unsigned int numItems
= items
.GetCount();
115 for( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
119 #if wxUSE_STD_CONTAINERS
122 wxArrayString::iterator
123 insertPoint
= std::lower_bound( m_strings
.begin(), m_strings
.end(), items
[i
] );
124 idx
= insertPoint
- m_strings
.begin();
125 m_strings
.insert( insertPoint
, items
[i
] );
128 #endif // wxUSE_STD_CONTAINERS
131 m_strings
.Insert( items
[i
], idx
);
134 wxString text
= items
[i
];
135 if (text
== wxEmptyString
)
136 text
= " "; // menu items can't have empty labels
137 m_popUpMenu
->Insert( idx
, i
+1, text
);
138 m_datas
.Insert( NULL
, idx
);
139 AssignNewItemClientData(idx
, clientData
, i
, type
);
142 DoAfterItemCountChange();
147 void wxChoice::DoDeleteOneItem(unsigned int n
)
149 wxCHECK_RET( IsValid(n
) , wxT("wxChoice::Delete: invalid index") );
151 if ( HasClientObjectData() )
152 delete GetClientObject( n
);
154 m_popUpMenu
->Delete( m_popUpMenu
->FindItemByPosition( n
) );
156 m_strings
.RemoveAt( n
) ;
157 m_datas
.RemoveAt( n
) ;
159 DoAfterItemCountChange();
162 void wxChoice::DoClear()
164 for ( unsigned int i
= 0 ; i
< GetCount() ; i
++ )
166 m_popUpMenu
->Delete( m_popUpMenu
->FindItemByPosition( 0 ) );
172 DoAfterItemCountChange();
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
178 int wxChoice::GetSelection() const
180 return GetPeer()->GetValue();
183 void wxChoice::SetSelection( int n
)
185 GetPeer()->SetValue( n
);
188 // ----------------------------------------------------------------------------
189 // string list functions
190 // ----------------------------------------------------------------------------
192 unsigned int wxChoice::GetCount() const
194 return m_strings
.GetCount() ;
197 int wxChoice::FindString( const wxString
& s
, bool bCase
) const
199 #if !wxUSE_STD_CONTAINERS
200 // Avoid assert for non-default args passed to sorted array Index
205 return m_strings
.Index( s
, bCase
) ;
208 void wxChoice::SetString(unsigned int n
, const wxString
& s
)
210 wxCHECK_RET( IsValid(n
), wxT("wxChoice::SetString(): invalid index") );
214 m_popUpMenu
->FindItemByPosition( n
)->SetItemLabel( s
) ;
217 wxString
wxChoice::GetString(unsigned int n
) const
219 wxCHECK_MSG( IsValid(n
), wxEmptyString
, wxT("wxChoice::GetString(): invalid index") );
221 return m_strings
[n
] ;
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
227 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
229 m_datas
[n
] = (char*)clientData
;
232 void * wxChoice::DoGetItemClientData(unsigned int n
) const
234 return (void *)m_datas
[n
];
237 bool wxChoice::OSXHandleClicked( double WXUNUSED(timestampsec
) )
239 SendSelectionChangedEvent(wxEVT_COMMAND_CHOICE_SELECTED
);
244 wxSize
wxChoice::DoGetBestSize() const
246 // We use the base window size for the height (which is wrong as it doesn't
247 // take the font into account -- TODO) and add some margins to the width
248 // computed by the base class method to account for the arrow.
249 const int lbHeight
= wxWindow::DoGetBestSize().y
;
251 return wxSize(wxChoiceBase::DoGetBestSize().x
+ 2*lbHeight
+ GetCharWidth(),
255 #endif // wxUSE_CHOICE