]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/choice.cpp
adjust subbitmap mask code to new representation
[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 size_t 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 size_t 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 size_t 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, int pos )
138 {
139 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1, wxT("wxChoice::DoInsert: can't insert into sorted list") );
140 wxCHECK_MSG( (pos >= 0) && (pos <= GetCount()), -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( int n )
155 {
156 wxCHECK_RET( n < GetCount(), 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 ( 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 size_t count = GetCount();
185 for ( size_t 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 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( int n, const wxString& s )
220 {
221 wxCHECK_RET( n >= 0 && (size_t)n < m_strings.GetCount(),
222 wxT("wxChoice::SetString(): invalid index") );
223
224 m_strings[n] = s ;
225
226 // apple menu pos is 1-based
227 UMASetMenuItemText( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1 , s , wxFont::GetDefaultEncoding() ) ;
228 }
229
230 wxString wxChoice::GetString( int n ) const
231 {
232 wxCHECK_MSG( n >= 0 && (size_t)n < m_strings.GetCount(), wxEmptyString,
233 wxT("wxChoice::GetString(): invalid index") );
234
235 return m_strings[n] ;
236 }
237
238 // ----------------------------------------------------------------------------
239 // client data
240 // ----------------------------------------------------------------------------
241 void wxChoice::DoSetItemClientData( int n, void* clientData )
242 {
243 wxCHECK_RET( n >= 0 && (size_t)n < m_datas.GetCount(),
244 wxT("wxChoice::DoSetItemClientData: invalid index") );
245
246 m_datas[n] = (char*)clientData ;
247 }
248
249 void * wxChoice::DoGetItemClientData( int n ) const
250 {
251 wxCHECK_MSG( n >= 0 && (size_t)n < m_datas.GetCount(), NULL,
252 wxT("wxChoice::DoGetClientData: invalid index") );
253
254 return (void *)m_datas[n];
255 }
256
257 void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
258 {
259 DoSetItemClientData( n, clientData ) ;
260 }
261
262 wxClientData* wxChoice::DoGetItemClientObject( 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(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