]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/choice.cpp
minor reformat
[wxWidgets.git] / src / mac / carbon / choice.cpp
CommitLineData
2ac013b1 1/////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/mac/carbon/choice.cpp
e9576ca5 3// Purpose: wxChoice
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5 10/////////////////////////////////////////////////////////////////////////////
e40298d5 11
a8e9860d 12#include "wx/wxprec.h"
312ebad4
WS
13
14#if wxUSE_CHOICE
15
e9576ca5 16#include "wx/choice.h"
03e11df5 17#include "wx/menu.h"
519cb848 18#include "wx/mac/uma.h"
e40298d5 19
d34cca53
DS
20extern MenuHandle NewUniqueMenu() ;
21
e9576ca5 22IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
e40298d5 23
e40298d5 24
5b781a67
SC
25wxChoice::~wxChoice()
26{
d76240bf
MB
27 if ( HasClientObjectData() )
28 {
29 size_t i, max = GetCount();
30
31 for ( i = 0; i < max; ++i )
d34cca53 32 delete GetClientObject( i );
d76240bf
MB
33 }
34
d34cca53 35 // DeleteMenu( m_macPopUpMenuId ) ;
e40298d5 36 // DisposeMenu( m_macPopUpMenuHandle ) ;
5b781a67 37}
e40298d5 38
d34cca53
DS
39bool wxChoice::Create(wxWindow *parent,
40 wxWindowID id,
41 const wxPoint& pos,
42 const wxSize& size,
43 const wxArrayString& choices,
44 long style,
45 const wxValidator& validator,
46 const wxString& name )
584ad2a3 47{
d34cca53 48 wxCArrayString chs( choices );
584ad2a3 49
d34cca53
DS
50 return Create(
51 parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
52 style, validator, name );
584ad2a3
MB
53}
54
d34cca53
DS
55bool wxChoice::Create(wxWindow *parent,
56 wxWindowID id,
57 const wxPoint& pos,
58 const wxSize& size,
59 int n,
60 const wxString choices[],
61 long style,
62 const wxValidator& validator,
63 const wxString& name )
e9576ca5 64{
d34cca53 65 m_macIsUserPane = false;
312ebad4 66
d34cca53 67 if ( !wxChoiceBase::Create( parent, id, pos, size, style, validator, name ) )
b45ed7a2
VZ
68 return false;
69
d34cca53 70 Rect bounds = wxMacGetBoundsForControl( this , pos , size );
4c37f124 71
d34cca53
DS
72 m_peer = new wxMacControl( this ) ;
73 OSStatus err = CreatePopupButtonControl(
74 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , CFSTR("") ,
75 -12345 , false /* no variable width */ , 0 , 0 , 0 , m_peer->GetControlRefAddr() );
76 verify_noerr( err );
312ebad4 77
d34cca53 78 m_macPopUpMenuHandle = NewUniqueMenu() ;
21fd5529 79 m_peer->SetData<MenuHandle>( kControlNoPart , kControlPopupButtonMenuHandleTag , (MenuHandle) m_macPopUpMenuHandle ) ;
d34cca53
DS
80 m_peer->SetValueAndRange( n > 0 ? 1 : 0 , 0 , 0 );
81 MacPostControlCreate( pos, size );
34585919 82
7b6986c3 83#if !wxUSE_STL
34585919 84 if ( style & wxCB_SORT )
d34cca53
DS
85 // autosort
86 m_strings = wxArrayString( 1 );
7b6986c3 87#endif
11e62fe6 88
b668a735
SC
89 for ( int i = 0; i < n; i++ )
90 {
d34cca53 91 Append( choices[i] );
b668a735 92 }
d34cca53
DS
93
94 // Set the first item as being selected
95 if (n > 0)
96 SetSelection( 0 );
97
98 // Needed because it is a wxControlWithItems
99 SetBestSize( size );
100
312ebad4 101 return true;
e9576ca5 102}
e40298d5 103
b668a735
SC
104// ----------------------------------------------------------------------------
105// adding/deleting items to/from the list
106// ----------------------------------------------------------------------------
d34cca53 107int wxChoice::DoAppend( const wxString& item )
e9576ca5 108{
aacd1442 109#if wxUSE_STL
9c707f80
MB
110 wxArrayString::iterator insertPoint;
111 size_t index;
11e62fe6 112
9c707f80
MB
113 if (GetWindowStyle() & wxCB_SORT)
114 {
115 insertPoint = std::lower_bound( m_strings.begin(), m_strings.end(), item );
116 index = insertPoint - m_strings.begin();
117 }
118 else
119 {
120 insertPoint = m_strings.end();
121 index = m_strings.size();
122 }
123
124 m_strings.insert( insertPoint, item );
aacd1442 125#else
d34cca53 126 size_t index = m_strings.Add( item );
aacd1442 127#endif
d34cca53
DS
128
129 m_datas.Insert( NULL , index );
130 UMAInsertMenuItem( MAC_WXHMENU( m_macPopUpMenuHandle ), item, m_font.GetEncoding(), index );
131 DoSetItemClientData( index, NULL );
132 m_peer->SetMaximum( GetCount() );
133
134 return index;
2597135a 135}
e40298d5 136
d34cca53 137int wxChoice::DoInsert( const wxString& item, int pos )
243dbf1a 138{
d34cca53
DS
139 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1, wxT("wxChoice::DoInsert: can't insert into sorted list") );
140 wxCHECK_MSG( (pos >= 0) && (pos <= GetCount()), -1, wxT("wxChoice::DoInsert: invalid index") );
243dbf1a
VZ
141
142 if (pos == GetCount())
d34cca53 143 return DoAppend( item );
243dbf1a 144
d34cca53
DS
145 UMAInsertMenuItem( MAC_WXHMENU( m_macPopUpMenuHandle ), item, m_font.GetEncoding(), pos );
146 m_strings.Insert( item, pos );
147 m_datas.Insert( NULL, pos );
148 DoSetItemClientData( pos, NULL );
149 m_peer->SetMaximum( GetCount() );
150
151 return pos;
243dbf1a
VZ
152}
153
d34cca53 154void wxChoice::Delete( int n )
2597135a 155{
d34cca53
DS
156 wxCHECK_RET( n < GetCount(), wxT("wxChoice::Delete: invalid index") );
157
b668a735 158 if ( HasClientObjectData() )
d34cca53
DS
159 delete GetClientObject( n );
160
161 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1 ) ;
5fe38474 162 m_strings.RemoveAt( n ) ;
3ef585df 163 m_datas.RemoveAt( n ) ;
21fd5529 164 m_peer->SetMaximum( GetCount() ) ;
e9576ca5 165}
e40298d5 166
e9576ca5
SC
167void wxChoice::Clear()
168{
4b651a46 169 FreeData();
b668a735 170 for ( int i = 0 ; i < GetCount() ; i++ )
519cb848 171 {
e40298d5 172 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , 1 ) ;
4b651a46 173 }
d34cca53 174
b668a735
SC
175 m_strings.Empty() ;
176 m_datas.Empty() ;
21fd5529 177 m_peer->SetMaximum( 0 ) ;
e9576ca5 178}
e40298d5 179
4b651a46 180void wxChoice::FreeData()
b668a735
SC
181{
182 if ( HasClientObjectData() )
183 {
184 size_t count = GetCount();
185 for ( size_t n = 0; n < count; n++ )
186 {
d34cca53 187 delete GetClientObject( n );
b668a735
SC
188 }
189 }
190}
e40298d5 191
b668a735
SC
192// ----------------------------------------------------------------------------
193// selection
194// ----------------------------------------------------------------------------
e9576ca5
SC
195int wxChoice::GetSelection() const
196{
d34cca53 197 return m_peer->GetValue() - 1 ;
e9576ca5 198}
e40298d5 199
d34cca53 200void wxChoice::SetSelection( int n )
519cb848 201{
21fd5529 202 m_peer->SetValue( n + 1 ) ;
519cb848 203}
e40298d5 204
b668a735
SC
205// ----------------------------------------------------------------------------
206// string list functions
207// ----------------------------------------------------------------------------
e40298d5 208
b668a735 209int wxChoice::GetCount() const
e9576ca5 210{
b668a735 211 return m_strings.GetCount() ;
e9576ca5 212}
e40298d5 213
d34cca53 214int wxChoice::FindString( const wxString& s, bool bCase ) const
e9576ca5 215{
11e62fe6 216 return m_strings.Index( s , bCase ) ;
b668a735 217}
e40298d5 218
d34cca53 219void wxChoice::SetString( int n, const wxString& s )
b668a735 220{
d34cca53
DS
221 wxCHECK_RET( n >= 0 && (size_t)n < m_strings.GetCount(),
222 wxT("wxChoice::SetString(): invalid index") );
223
34585919 224 m_strings[n] = s ;
d34cca53 225
34585919
SC
226 // apple menu pos is 1-based
227 UMASetMenuItemText( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1 , s , wxFont::GetDefaultEncoding() ) ;
e9576ca5
SC
228}
229
d34cca53 230wxString wxChoice::GetString( int n ) const
e9576ca5 231{
11e62fe6 232 wxCHECK_MSG( n >= 0 && (size_t)n < m_strings.GetCount(), wxEmptyString,
d34cca53 233 wxT("wxChoice::GetString(): invalid index") );
17a6a662 234
e40298d5 235 return m_strings[n] ;
e9576ca5 236}
17a6a662 237
b668a735
SC
238// ----------------------------------------------------------------------------
239// client data
240// ----------------------------------------------------------------------------
b668a735
SC
241void wxChoice::DoSetItemClientData( int n, void* clientData )
242{
d81cc883 243 wxCHECK_RET( n >= 0 && (size_t)n < m_datas.GetCount(),
d34cca53 244 wxT("wxChoice::DoSetItemClientData: invalid index") );
c69279ef 245
d34cca53 246 m_datas[n] = (char*)clientData ;
b668a735 247}
e40298d5 248
d34cca53 249void * wxChoice::DoGetItemClientData( int n ) const
b668a735 250{
3ba18664 251 wxCHECK_MSG( n >= 0 && (size_t)n < m_datas.GetCount(), NULL,
d34cca53
DS
252 wxT("wxChoice::DoGetClientData: invalid index") );
253
d81cc883 254 return (void *)m_datas[n];
b668a735 255}
e40298d5 256
b668a735
SC
257void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
258{
d34cca53 259 DoSetItemClientData( n, clientData ) ;
b668a735 260}
e40298d5 261
b668a735 262wxClientData* wxChoice::DoGetItemClientObject( int n ) const
e9576ca5 263{
d34cca53 264 return (wxClientData*)DoGetItemClientData( n ) ;
e9576ca5 265}
e40298d5 266
d34cca53 267wxInt32 wxChoice::MacControlHit( WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
b668a735 268{
d34cca53
DS
269 wxCommandEvent event( wxEVT_COMMAND_CHOICE_SELECTED, m_windowId );
270
f2d990ad 271 // actually n should be made sure by the os to be a valid selection, but ...
d34cca53 272 int n = GetSelection();
f2d990ad
SC
273 if ( n > -1 )
274 {
275 event.SetInt( n );
d34cca53
DS
276 event.SetString( GetStringSelection() );
277 event.SetEventObject( this );
278
f2d990ad 279 if ( HasClientObjectData() )
d34cca53 280 event.SetClientObject( GetClientObject( n ) );
f2d990ad 281 else if ( HasClientUntypedData() )
d34cca53
DS
282 event.SetClientData( GetClientData( n ) );
283
284 ProcessCommand( event );
f2d990ad 285 }
d34cca53 286
4c37f124 287 return noErr ;
9453cf2b 288}
e40298d5 289
f2d990ad 290wxSize wxChoice::DoGetBestSize() const
b668a735 291{
637988a4 292 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
f2d990ad
SC
293 int lbHeight = 20;
294 int wLine;
d34cca53 295
f2d990ad
SC
296#if TARGET_CARBON
297 long metric ;
d34cca53
DS
298
299 GetThemeMetric( kThemeMetricPopupButtonHeight , &metric );
c69279ef 300 lbHeight = metric ;
f2d990ad 301#endif
d34cca53 302
e40298d5 303 {
facd6764 304 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
fcb35beb 305 if ( m_font.Ok() )
e40298d5 306 {
facd6764
SC
307 ::TextFont( m_font.MacGetFontNum() ) ;
308 ::TextSize( m_font.MacGetFontSize() ) ;
309 ::TextFace( m_font.MacGetFontStyle() ) ;
e40298d5
JS
310 }
311 else
312 {
313 ::TextFont( kFontIDMonaco ) ;
d34cca53 314 ::TextSize( 9 ) ;
e40298d5
JS
315 ::TextFace( 0 ) ;
316 }
d34cca53 317
e40298d5 318 // Find the widest line
d34cca53
DS
319 for(int i = 0; i < GetCount(); i++)
320 {
321 wxString str( GetString( i ) );
322
323#if wxUSE_UNICODE
324 Point bounds = { 0, 0 } ;
2c1a3312 325 SInt16 baseline ;
d34cca53 326
a9412f8f 327 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
2c1a3312
SC
328 kThemeCurrentPortFont,
329 kThemeStateActive,
330 false,
331 &bounds,
332 &baseline );
d34cca53 333
2c1a3312 334 wLine = bounds.h ;
d34cca53 335#else
939fba6c 336 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
d34cca53
DS
337#endif
338
339 lbWidth = wxMax( lbWidth, wLine ) ;
e40298d5 340 }
d34cca53 341
e40298d5
JS
342 // Add room for the popup arrow
343 lbWidth += 2 * lbHeight ;
d34cca53 344
e40298d5 345 // And just a bit more
e40298d5
JS
346 int cx = ::TextWidth( "X" , 0 , 1 ) ;
347 lbWidth += cx ;
e40298d5 348 }
d34cca53
DS
349
350 return wxSize( lbWidth, lbHeight );
d76240bf 351}
312ebad4
WS
352
353#endif // wxUSE_CHOICE