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