]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/choice.cpp
don't reset the selection after event was vetoed if there is no old selection
[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 #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
229 void 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
239 wxString 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 // ----------------------------------------------------------------------------
249 void 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
256 void * 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
263 void wxChoice::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
264 {
265 DoSetItemClientData(n, clientData);
266 }
267
268 wxClientData* wxChoice::DoGetItemClientObject(unsigned int n) const
269 {
270 return (wxClientData*)DoGetItemClientData( n ) ;
271 }
272
273 wxInt32 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
296 wxSize 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 long metric ;
304
305 GetThemeMetric( kThemeMetricPopupButtonHeight , &metric );
306 lbHeight = metric ;
307 #endif
308
309 {
310 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
311 if ( m_font.Ok() )
312 {
313 ::TextFont( m_font.MacGetFontNum() ) ;
314 ::TextSize( m_font.MacGetFontSize() ) ;
315 ::TextFace( m_font.MacGetFontStyle() ) ;
316 }
317 else
318 {
319 ::TextFont( kFontIDMonaco ) ;
320 ::TextSize( 9 ) ;
321 ::TextFace( 0 ) ;
322 }
323
324 // Find the widest line
325 for(unsigned int i = 0; i < GetCount(); i++)
326 {
327 wxString str(GetString(i));
328
329 #if wxUSE_UNICODE
330 Point bounds = { 0, 0 } ;
331 SInt16 baseline ;
332
333 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
334 kThemeCurrentPortFont,
335 kThemeStateActive,
336 false,
337 &bounds,
338 &baseline );
339
340 wLine = bounds.h ;
341 #else
342 wLine = ::TextWidth( str.c_str() , 0 , str.length() ) ;
343 #endif
344
345 lbWidth = wxMax( lbWidth, wLine ) ;
346 }
347
348 // Add room for the popup arrow
349 lbWidth += 2 * lbHeight ;
350
351 // And just a bit more
352 int cx = ::TextWidth( "X" , 0 , 1 ) ;
353 lbWidth += cx ;
354 }
355
356 return wxSize( lbWidth, lbHeight );
357 }
358
359 #endif // wxUSE_CHOICE