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