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