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