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