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