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