]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/choice.cpp
simplify OnActivate menubar install code
[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 #include "wx/menu.h"
18 #include "wx/mac/uma.h"
19
20 extern MenuHandle NewUniqueMenu() ;
21
22 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
23
24
25 wxChoice::~wxChoice()
26 {
27 if ( HasClientObjectData() )
28 {
29 unsigned int i, max = GetCount();
30
31 for ( i = 0; i < max; ++i )
32 delete GetClientObject( i );
33 }
34
35 // DeleteMenu( m_macPopUpMenuId ) ;
36 // DisposeMenu( m_macPopUpMenuHandle ) ;
37 }
38
39 bool 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 )
47 {
48 wxCArrayString chs( choices );
49
50 return Create(
51 parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
52 style, validator, name );
53 }
54
55 bool 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 )
64 {
65 m_macIsUserPane = false;
66
67 if ( !wxChoiceBase::Create( parent, id, pos, size, style, validator, name ) )
68 return false;
69
70 Rect bounds = wxMacGetBoundsForControl( this , pos , size );
71
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 );
77
78 m_macPopUpMenuHandle = NewUniqueMenu() ;
79 m_peer->SetData<MenuHandle>( kControlNoPart , kControlPopupButtonMenuHandleTag , (MenuHandle) m_macPopUpMenuHandle ) ;
80 m_peer->SetValueAndRange( n > 0 ? 1 : 0 , 0 , 0 );
81 MacPostControlCreate( pos, size );
82
83 #if !wxUSE_STL
84 if ( style & wxCB_SORT )
85 // autosort
86 m_strings = wxArrayString( 1 );
87 #endif
88
89 for ( int i = 0; i < n; i++ )
90 {
91 Append( choices[i] );
92 }
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
101 return true;
102 }
103
104 // ----------------------------------------------------------------------------
105 // adding/deleting items to/from the list
106 // ----------------------------------------------------------------------------
107 int wxChoice::DoAppend( const wxString& item )
108 {
109 #if wxUSE_STL
110 wxArrayString::iterator insertPoint;
111 unsigned int index;
112
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 );
125 #else
126 unsigned int index = m_strings.Add( item );
127 #endif
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;
135 }
136
137 int wxChoice::DoInsert( const wxString& item, unsigned int pos )
138 {
139 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1, wxT("wxChoice::DoInsert: can't insert into sorted list") );
140 wxCHECK_MSG( IsValidInsert(pos), -1, wxT("wxChoice::DoInsert: invalid index") );
141
142 if (pos == GetCount())
143 return DoAppend( item );
144
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;
152 }
153
154 void wxChoice::Delete(unsigned int n)
155 {
156 wxCHECK_RET( IsValid(n) , wxT("wxChoice::Delete: invalid index") );
157
158 if ( HasClientObjectData() )
159 delete GetClientObject( n );
160
161 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1 ) ;
162 m_strings.RemoveAt( n ) ;
163 m_datas.RemoveAt( n ) ;
164 m_peer->SetMaximum( GetCount() ) ;
165 }
166
167 void wxChoice::Clear()
168 {
169 FreeData();
170 for ( unsigned int i = 0 ; i < GetCount() ; i++ )
171 {
172 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , 1 ) ;
173 }
174
175 m_strings.Empty() ;
176 m_datas.Empty() ;
177 m_peer->SetMaximum( 0 ) ;
178 }
179
180 void wxChoice::FreeData()
181 {
182 if ( HasClientObjectData() )
183 {
184 unsigned int count = GetCount();
185 for ( unsigned int n = 0; n < count; n++ )
186 {
187 delete GetClientObject( n );
188 }
189 }
190 }
191
192 // ----------------------------------------------------------------------------
193 // selection
194 // ----------------------------------------------------------------------------
195 int wxChoice::GetSelection() const
196 {
197 return m_peer->GetValue() - 1 ;
198 }
199
200 void wxChoice::SetSelection( int n )
201 {
202 m_peer->SetValue( n + 1 ) ;
203 }
204
205 // ----------------------------------------------------------------------------
206 // string list functions
207 // ----------------------------------------------------------------------------
208
209 unsigned int wxChoice::GetCount() const
210 {
211 return m_strings.GetCount() ;
212 }
213
214 int wxChoice::FindString( const wxString& s, bool bCase ) const
215 {
216 return m_strings.Index( s , bCase ) ;
217 }
218
219 void wxChoice::SetString(unsigned int n, const wxString& s)
220 {
221 wxCHECK_RET( IsValid(n), wxT("wxChoice::SetString(): invalid index") );
222
223 m_strings[n] = s ;
224
225 // apple menu pos is 1-based
226 UMASetMenuItemText( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1 , s , wxFont::GetDefaultEncoding() ) ;
227 }
228
229 wxString wxChoice::GetString(unsigned int n) const
230 {
231 wxCHECK_MSG( IsValid(n), wxEmptyString, wxT("wxChoice::GetString(): invalid index") );
232
233 return m_strings[n] ;
234 }
235
236 // ----------------------------------------------------------------------------
237 // client data
238 // ----------------------------------------------------------------------------
239 void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
240 {
241 wxCHECK_RET( IsValid(n), wxT("wxChoice::DoSetItemClientData: invalid index") );
242
243 m_datas[n] = (char*)clientData ;
244 }
245
246 void * wxChoice::DoGetItemClientData(unsigned int n) const
247 {
248 wxCHECK_MSG( IsValid(n), NULL, wxT("wxChoice::DoGetClientData: invalid index") );
249
250 return (void *)m_datas[n];
251 }
252
253 void wxChoice::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
254 {
255 DoSetItemClientData(n, clientData);
256 }
257
258 wxClientData* wxChoice::DoGetItemClientObject(unsigned int n) const
259 {
260 return (wxClientData*)DoGetItemClientData( n ) ;
261 }
262
263 wxInt32 wxChoice::MacControlHit( WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
264 {
265 wxCommandEvent event( wxEVT_COMMAND_CHOICE_SELECTED, m_windowId );
266
267 // actually n should be made sure by the os to be a valid selection, but ...
268 int n = GetSelection();
269 if ( n > -1 )
270 {
271 event.SetInt( n );
272 event.SetString( GetStringSelection() );
273 event.SetEventObject( this );
274
275 if ( HasClientObjectData() )
276 event.SetClientObject( GetClientObject( n ) );
277 else if ( HasClientUntypedData() )
278 event.SetClientData( GetClientData( n ) );
279
280 ProcessCommand( event );
281 }
282
283 return noErr ;
284 }
285
286 wxSize wxChoice::DoGetBestSize() const
287 {
288 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
289 int lbHeight = 20;
290 int wLine;
291
292 #if TARGET_CARBON
293 long metric ;
294
295 GetThemeMetric( kThemeMetricPopupButtonHeight , &metric );
296 lbHeight = metric ;
297 #endif
298
299 {
300 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
301 if ( m_font.Ok() )
302 {
303 ::TextFont( m_font.MacGetFontNum() ) ;
304 ::TextSize( m_font.MacGetFontSize() ) ;
305 ::TextFace( m_font.MacGetFontStyle() ) ;
306 }
307 else
308 {
309 ::TextFont( kFontIDMonaco ) ;
310 ::TextSize( 9 ) ;
311 ::TextFace( 0 ) ;
312 }
313
314 // Find the widest line
315 for(unsigned int i = 0; i < GetCount(); i++)
316 {
317 wxString str(GetString(i));
318
319 #if wxUSE_UNICODE
320 Point bounds = { 0, 0 } ;
321 SInt16 baseline ;
322
323 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
324 kThemeCurrentPortFont,
325 kThemeStateActive,
326 false,
327 &bounds,
328 &baseline );
329
330 wLine = bounds.h ;
331 #else
332 wLine = ::TextWidth( str.c_str() , 0 , str.length() ) ;
333 #endif
334
335 lbWidth = wxMax( lbWidth, wLine ) ;
336 }
337
338 // Add room for the popup arrow
339 lbWidth += 2 * lbHeight ;
340
341 // And just a bit more
342 int cx = ::TextWidth( "X" , 0 , 1 ) ;
343 lbWidth += cx ;
344 }
345
346 return wxSize( lbWidth, lbHeight );
347 }
348
349 #endif // wxUSE_CHOICE