]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/choice.cpp
[ 1473731 ] 'wxColourBase and wxString <-> wxColour implementation' with minor modifi...
[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
8228b893 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 {
aa61d352 29 unsigned int i, max = GetCount();
d76240bf
MB
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 110 wxArrayString::iterator insertPoint;
aa61d352 111 unsigned int 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
aa61d352 126 unsigned int 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
aa61d352 137int wxChoice::DoInsert( const wxString& item, unsigned int pos )
243dbf1a 138{
d34cca53 139 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1, wxT("wxChoice::DoInsert: can't insert into sorted list") );
8228b893 140 wxCHECK_MSG( IsValidInsert(pos), -1, wxT("wxChoice::DoInsert: invalid index") );
243dbf1a 141
aa61d352 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
aa61d352 154void wxChoice::Delete(unsigned int n)
2597135a 155{
8228b893 156 wxCHECK_RET( IsValid(n) , wxT("wxChoice::Delete: invalid index") );
d34cca53 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();
aa61d352 170 for ( unsigned 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 {
aa61d352
VZ
184 unsigned int count = GetCount();
185 for ( unsigned int n = 0; n < count; n++ )
b668a735 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
aa61d352 209unsigned int 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
aa61d352 219void wxChoice::SetString(unsigned int n, const wxString& s)
b668a735 220{
8228b893 221 wxCHECK_RET( IsValid(n), wxT("wxChoice::SetString(): invalid index") );
d34cca53 222
34585919 223 m_strings[n] = s ;
d34cca53 224
34585919
SC
225 // apple menu pos is 1-based
226 UMASetMenuItemText( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1 , s , wxFont::GetDefaultEncoding() ) ;
e9576ca5
SC
227}
228
aa61d352 229wxString wxChoice::GetString(unsigned int n) const
e9576ca5 230{
8228b893 231 wxCHECK_MSG( IsValid(n), wxEmptyString, wxT("wxChoice::GetString(): invalid index") );
17a6a662 232
e40298d5 233 return m_strings[n] ;
e9576ca5 234}
17a6a662 235
b668a735
SC
236// ----------------------------------------------------------------------------
237// client data
238// ----------------------------------------------------------------------------
aa61d352 239void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
b668a735 240{
8228b893 241 wxCHECK_RET( IsValid(n), wxT("wxChoice::DoSetItemClientData: invalid index") );
c69279ef 242
d34cca53 243 m_datas[n] = (char*)clientData ;
b668a735 244}
e40298d5 245
aa61d352 246void * wxChoice::DoGetItemClientData(unsigned int n) const
b668a735 247{
8228b893 248 wxCHECK_MSG( IsValid(n), NULL, wxT("wxChoice::DoGetClientData: invalid index") );
d34cca53 249
d81cc883 250 return (void *)m_datas[n];
b668a735 251}
e40298d5 252
aa61d352 253void wxChoice::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
b668a735 254{
aa61d352 255 DoSetItemClientData(n, clientData);
b668a735 256}
e40298d5 257
aa61d352 258wxClientData* wxChoice::DoGetItemClientObject(unsigned int n) const
e9576ca5 259{
d34cca53 260 return (wxClientData*)DoGetItemClientData( n ) ;
e9576ca5 261}
e40298d5 262
d34cca53 263wxInt32 wxChoice::MacControlHit( WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
b668a735 264{
d34cca53
DS
265 wxCommandEvent event( wxEVT_COMMAND_CHOICE_SELECTED, m_windowId );
266
f2d990ad 267 // actually n should be made sure by the os to be a valid selection, but ...
d34cca53 268 int n = GetSelection();
f2d990ad
SC
269 if ( n > -1 )
270 {
271 event.SetInt( n );
d34cca53
DS
272 event.SetString( GetStringSelection() );
273 event.SetEventObject( this );
274
f2d990ad 275 if ( HasClientObjectData() )
d34cca53 276 event.SetClientObject( GetClientObject( n ) );
f2d990ad 277 else if ( HasClientUntypedData() )
d34cca53
DS
278 event.SetClientData( GetClientData( n ) );
279
280 ProcessCommand( event );
f2d990ad 281 }
d34cca53 282
4c37f124 283 return noErr ;
9453cf2b 284}
e40298d5 285
f2d990ad 286wxSize wxChoice::DoGetBestSize() const
b668a735 287{
637988a4 288 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
f2d990ad
SC
289 int lbHeight = 20;
290 int wLine;
d34cca53 291
f2d990ad
SC
292#if TARGET_CARBON
293 long metric ;
d34cca53
DS
294
295 GetThemeMetric( kThemeMetricPopupButtonHeight , &metric );
c69279ef 296 lbHeight = metric ;
f2d990ad 297#endif
d34cca53 298
e40298d5 299 {
facd6764 300 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
fcb35beb 301 if ( m_font.Ok() )
e40298d5 302 {
facd6764
SC
303 ::TextFont( m_font.MacGetFontNum() ) ;
304 ::TextSize( m_font.MacGetFontSize() ) ;
305 ::TextFace( m_font.MacGetFontStyle() ) ;
e40298d5
JS
306 }
307 else
308 {
309 ::TextFont( kFontIDMonaco ) ;
d34cca53 310 ::TextSize( 9 ) ;
e40298d5
JS
311 ::TextFace( 0 ) ;
312 }
d34cca53 313
e40298d5 314 // Find the widest line
aa61d352 315 for(unsigned int i = 0; i < GetCount(); i++)
d34cca53 316 {
aa61d352 317 wxString str(GetString(i));
d34cca53
DS
318
319#if wxUSE_UNICODE
320 Point bounds = { 0, 0 } ;
2c1a3312 321 SInt16 baseline ;
d34cca53 322
a9412f8f 323 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
2c1a3312
SC
324 kThemeCurrentPortFont,
325 kThemeStateActive,
326 false,
327 &bounds,
328 &baseline );
d34cca53 329
2c1a3312 330 wLine = bounds.h ;
d34cca53 331#else
8228b893 332 wLine = ::TextWidth( str.c_str() , 0 , str.length() ) ;
d34cca53
DS
333#endif
334
335 lbWidth = wxMax( lbWidth, wLine ) ;
e40298d5 336 }
d34cca53 337
e40298d5
JS
338 // Add room for the popup arrow
339 lbWidth += 2 * lbHeight ;
d34cca53 340
e40298d5 341 // And just a bit more
e40298d5
JS
342 int cx = ::TextWidth( "X" , 0 , 1 ) ;
343 lbWidth += cx ;
e40298d5 344 }
d34cca53
DS
345
346 return wxSize( lbWidth, lbHeight );
d76240bf 347}
312ebad4
WS
348
349#endif // wxUSE_CHOICE