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