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