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