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