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