]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/choice.cpp
Don't copy the NULL char into the clipboard. Most Mac apps will
[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 #if !wxUSE_STL
84 if ( style & wxCB_SORT )
85 {
86 m_strings = wxArrayString(1) ; // autosort
87 }
88 #endif
89
90 for ( int i = 0; i < n; i++ )
91 {
92 Append(choices[i]);
93 }
94 SetBestSize(size); // Needed because it is a wxControlWithItems
95 return true;
96 }
97
98 // ----------------------------------------------------------------------------
99 // adding/deleting items to/from the list
100 // ----------------------------------------------------------------------------
101 int wxChoice::DoAppend(const wxString& item)
102 {
103 #if wxUSE_STL
104 wxArrayString::iterator insertPoint;
105 size_t index;
106
107 if (GetWindowStyle() & wxCB_SORT)
108 {
109 insertPoint = std::lower_bound( m_strings.begin(), m_strings.end(), item );
110 index = insertPoint - m_strings.begin();
111 }
112 else
113 {
114 insertPoint = m_strings.end();
115 index = m_strings.size();
116 }
117
118 m_strings.insert( insertPoint, item );
119 #else
120 size_t index = m_strings.Add( item ) ;
121 #endif
122 m_datas.Insert( NULL , index ) ;
123 UMAInsertMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item, m_font.GetEncoding() , index );
124 DoSetItemClientData( index , NULL ) ;
125 m_peer->SetMaximum( GetCount() ) ;
126 return index ;
127 }
128
129 int wxChoice::DoInsert(const wxString& item, int pos)
130 {
131 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
132 wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
133
134 if (pos == GetCount())
135 return DoAppend(item);
136
137 UMAInsertMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item, m_font.GetEncoding() , pos );
138 m_strings.Insert( item, pos ) ;
139 m_datas.Insert( NULL, pos ) ;
140 DoSetItemClientData( pos , NULL ) ;
141 m_peer->SetMaximum( GetCount() ) ;
142 return pos ;
143 }
144
145 void wxChoice::Delete(int n)
146 {
147 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
148 if ( HasClientObjectData() )
149 {
150 delete GetClientObject(n);
151 }
152 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1) ;
153 m_strings.RemoveAt( n ) ;
154 m_datas.RemoveAt( n ) ;
155 m_peer->SetMaximum( GetCount() ) ;
156 }
157
158 void wxChoice::Clear()
159 {
160 FreeData();
161 for ( int i = 0 ; i < GetCount() ; i++ )
162 {
163 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , 1 ) ;
164 }
165 m_strings.Empty() ;
166 m_datas.Empty() ;
167 m_peer->SetMaximum( 0 ) ;
168 }
169
170 void wxChoice::FreeData()
171 {
172 if ( HasClientObjectData() )
173 {
174 size_t count = GetCount();
175 for ( size_t n = 0; n < count; n++ )
176 {
177 delete GetClientObject(n);
178 }
179 }
180 }
181
182 // ----------------------------------------------------------------------------
183 // selection
184 // ----------------------------------------------------------------------------
185 int wxChoice::GetSelection() const
186 {
187 return m_peer->GetValue() -1 ;
188 }
189
190 void wxChoice::SetSelection(int n)
191 {
192 m_peer->SetValue( n + 1 ) ;
193 }
194
195 // ----------------------------------------------------------------------------
196 // string list functions
197 // ----------------------------------------------------------------------------
198
199 int wxChoice::GetCount() const
200 {
201 return m_strings.GetCount() ;
202 }
203
204 int wxChoice::FindString(const wxString& s) const
205 {
206 return m_strings.Index( s , true , false) ;
207 }
208
209 void wxChoice::SetString(int n, const wxString& s)
210 {
211 m_strings[n] = s ;
212 // apple menu pos is 1-based
213 UMASetMenuItemText( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1 , s , wxFont::GetDefaultEncoding() ) ;
214 }
215
216 wxString wxChoice::GetString(int n) const
217 {
218 wxCHECK_MSG( n >= 0 && (size_t)n < m_strings.GetCount(), _T(""),
219 _T("wxChoice::GetString(): invalid index") );
220
221 return m_strings[n] ;
222 }
223
224 // ----------------------------------------------------------------------------
225 // client data
226 // ----------------------------------------------------------------------------
227 void wxChoice::DoSetItemClientData( int n, void* clientData )
228 {
229 wxCHECK_RET( n >= 0 && (size_t)n < m_datas.GetCount(),
230 wxT("invalid index in wxChoice::SetClientData") );
231
232 m_datas[n] = (char*) clientData ;
233 }
234
235 void *wxChoice::DoGetItemClientData(int n) const
236 {
237 wxCHECK_MSG( n >= 0 && (size_t)n < m_datas.GetCount(), NULL,
238 wxT("invalid index in wxChoice::GetClientData") );
239 return (void *)m_datas[n];
240 }
241
242 void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
243 {
244 DoSetItemClientData(n, clientData);
245 }
246
247 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
248 {
249 return (wxClientData *)DoGetItemClientData(n);
250 }
251
252 wxInt32 wxChoice::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
253 {
254 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId );
255 int n = GetSelection();
256 // actually n should be made sure by the os to be a valid selection, but ...
257 if ( n > -1 )
258 {
259 event.SetInt( n );
260 event.SetString(GetStringSelection());
261 event.SetEventObject(this);
262 if ( HasClientObjectData() )
263 event.SetClientObject( GetClientObject(n) );
264 else if ( HasClientUntypedData() )
265 event.SetClientData( GetClientData(n) );
266 ProcessCommand(event);
267 }
268 return noErr ;
269 }
270
271 wxSize wxChoice::DoGetBestSize() const
272 {
273 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
274 int lbHeight = 20;
275 int wLine;
276 #if TARGET_CARBON
277 long metric ;
278 GetThemeMetric(kThemeMetricPopupButtonHeight , &metric );
279 lbHeight = metric ;
280 #endif
281 {
282 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
283 if ( m_font.Ok() )
284 {
285 ::TextFont( m_font.MacGetFontNum() ) ;
286 ::TextSize( m_font.MacGetFontSize() ) ;
287 ::TextFace( m_font.MacGetFontStyle() ) ;
288 }
289 else
290 {
291 ::TextFont( kFontIDMonaco ) ;
292 ::TextSize( 9 );
293 ::TextFace( 0 ) ;
294 }
295 // Find the widest line
296 for(int i = 0; i < GetCount(); i++) {
297 wxString str(GetString(i));
298 #if wxUSE_UNICODE
299 Point bounds={0,0} ;
300 SInt16 baseline ;
301 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
302 kThemeCurrentPortFont,
303 kThemeStateActive,
304 false,
305 &bounds,
306 &baseline );
307 wLine = bounds.h ;
308 #else
309 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
310 #endif
311 lbWidth = wxMax(lbWidth, wLine);
312 }
313 // Add room for the popup arrow
314 lbWidth += 2 * lbHeight ;
315 // And just a bit more
316 int cx = ::TextWidth( "X" , 0 , 1 ) ;
317 lbWidth += cx ;
318
319 }
320 return wxSize(lbWidth, lbHeight);
321 }
322
323 #endif // wxUSE_CHOICE