]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/choice.cpp
Unused variable removal.
[wxWidgets.git] / src / mac / carbon / choice.cpp
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
24 extern MenuHandle NewUniqueMenu() ;
25
26 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
27
28
29 wxChoice::~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
43 bool 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
59 bool 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 // ----------------------------------------------------------------------------
111 int 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
141 int 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
158 void 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
171 void 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
184 void 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 // ----------------------------------------------------------------------------
199 int wxChoice::GetSelection() const
200 {
201 return m_peer->GetValue() - 1 ;
202 }
203
204 void wxChoice::SetSelection( int n )
205 {
206 m_peer->SetValue( n + 1 ) ;
207 }
208
209 // ----------------------------------------------------------------------------
210 // string list functions
211 // ----------------------------------------------------------------------------
212
213 unsigned int wxChoice::GetCount() const
214 {
215 return m_strings.GetCount() ;
216 }
217
218 int wxChoice::FindString( const wxString& s, bool bCase ) const
219 {
220 return m_strings.Index( s , bCase ) ;
221 }
222
223 void wxChoice::SetString(unsigned int n, const wxString& s)
224 {
225 wxCHECK_RET( IsValid(n), wxT("wxChoice::SetString(): invalid index") );
226
227 m_strings[n] = s ;
228
229 // apple menu pos is 1-based
230 UMASetMenuItemText( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1 , s , wxFont::GetDefaultEncoding() ) ;
231 }
232
233 wxString wxChoice::GetString(unsigned int n) const
234 {
235 wxCHECK_MSG( IsValid(n), wxEmptyString, wxT("wxChoice::GetString(): invalid index") );
236
237 return m_strings[n] ;
238 }
239
240 // ----------------------------------------------------------------------------
241 // client data
242 // ----------------------------------------------------------------------------
243 void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
244 {
245 wxCHECK_RET( IsValid(n), wxT("wxChoice::DoSetItemClientData: invalid index") );
246
247 m_datas[n] = (char*)clientData ;
248 }
249
250 void * wxChoice::DoGetItemClientData(unsigned int n) const
251 {
252 wxCHECK_MSG( IsValid(n), NULL, wxT("wxChoice::DoGetClientData: invalid index") );
253
254 return (void *)m_datas[n];
255 }
256
257 void wxChoice::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
258 {
259 DoSetItemClientData(n, clientData);
260 }
261
262 wxClientData* wxChoice::DoGetItemClientObject(unsigned int n) const
263 {
264 return (wxClientData*)DoGetItemClientData( n ) ;
265 }
266
267 wxInt32 wxChoice::MacControlHit( WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
268 {
269 wxCommandEvent event( wxEVT_COMMAND_CHOICE_SELECTED, m_windowId );
270
271 // actually n should be made sure by the os to be a valid selection, but ...
272 int n = GetSelection();
273 if ( n > -1 )
274 {
275 event.SetInt( n );
276 event.SetString( GetStringSelection() );
277 event.SetEventObject( this );
278
279 if ( HasClientObjectData() )
280 event.SetClientObject( GetClientObject( n ) );
281 else if ( HasClientUntypedData() )
282 event.SetClientData( GetClientData( n ) );
283
284 ProcessCommand( event );
285 }
286
287 return noErr ;
288 }
289
290 wxSize wxChoice::DoGetBestSize() const
291 {
292 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
293 int lbHeight = 20;
294 int wLine;
295
296 #if TARGET_CARBON
297 long metric ;
298
299 GetThemeMetric( kThemeMetricPopupButtonHeight , &metric );
300 lbHeight = metric ;
301 #endif
302
303 {
304 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
305 if ( m_font.Ok() )
306 {
307 ::TextFont( m_font.MacGetFontNum() ) ;
308 ::TextSize( m_font.MacGetFontSize() ) ;
309 ::TextFace( m_font.MacGetFontStyle() ) ;
310 }
311 else
312 {
313 ::TextFont( kFontIDMonaco ) ;
314 ::TextSize( 9 ) ;
315 ::TextFace( 0 ) ;
316 }
317
318 // Find the widest line
319 for(unsigned int i = 0; i < GetCount(); i++)
320 {
321 wxString str(GetString(i));
322
323 #if wxUSE_UNICODE
324 Point bounds = { 0, 0 } ;
325 SInt16 baseline ;
326
327 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
328 kThemeCurrentPortFont,
329 kThemeStateActive,
330 false,
331 &bounds,
332 &baseline );
333
334 wLine = bounds.h ;
335 #else
336 wLine = ::TextWidth( str.c_str() , 0 , str.length() ) ;
337 #endif
338
339 lbWidth = wxMax( lbWidth, wLine ) ;
340 }
341
342 // Add room for the popup arrow
343 lbWidth += 2 * lbHeight ;
344
345 // And just a bit more
346 int cx = ::TextWidth( "X" , 0 , 1 ) ;
347 lbWidth += cx ;
348 }
349
350 return wxSize( lbWidth, lbHeight );
351 }
352
353 #endif // wxUSE_CHOICE