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