]>
Commit | Line | Data |
---|---|---|
524c47aa SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/osx/carbon/choice.cpp | |
3 | // Purpose: wxChoice | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
b5b208a1 | 7 | // RCS-ID: $Id$ |
524c47aa SC |
8 | // Copyright: (c) Stefan Csomor |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #if wxUSE_CHOICE | |
15 | ||
16 | #include "wx/choice.h" | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/menu.h" | |
20 | #include "wx/dcclient.h" | |
21 | #endif | |
22 | ||
23 | #include "wx/osx/private.h" | |
24 | ||
25 | IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControlWithItems) | |
26 | ||
27 | wxChoice::~wxChoice() | |
28 | { | |
29 | if ( HasClientObjectData() ) | |
30 | { | |
31 | unsigned int i, max = GetCount(); | |
32 | ||
33 | for ( i = 0; i < max; ++i ) | |
34 | delete GetClientObject( i ); | |
35 | } | |
36 | delete m_popUpMenu; | |
37 | } | |
38 | ||
39 | bool wxChoice::Create(wxWindow *parent, | |
40 | wxWindowID id, | |
41 | const wxPoint& pos, | |
42 | const wxSize& size, | |
43 | const wxArrayString& choices, | |
44 | long style, | |
45 | const wxValidator& validator, | |
46 | const wxString& name ) | |
47 | { | |
48 | if ( !Create( parent, id, pos, size, 0, NULL, style, validator, name ) ) | |
49 | return false; | |
50 | ||
51 | Append( choices ); | |
52 | ||
53 | if ( !choices.empty() ) | |
54 | SetSelection( 0 ); | |
55 | ||
56 | SetInitialSize( size ); | |
57 | ||
58 | return true; | |
59 | } | |
60 | ||
61 | bool wxChoice::Create(wxWindow *parent, | |
62 | wxWindowID id, | |
63 | const wxPoint& pos, | |
64 | const wxSize& size, | |
65 | int n, | |
66 | const wxString choices[], | |
67 | long style, | |
68 | const wxValidator& validator, | |
69 | const wxString& name ) | |
70 | { | |
71 | m_macIsUserPane = false; | |
72 | ||
73 | if ( !wxChoiceBase::Create( parent, id, pos, size, style, validator, name ) ) | |
74 | return false; | |
03647350 | 75 | |
524c47aa SC |
76 | m_popUpMenu = new wxMenu(); |
77 | m_popUpMenu->SetNoEventsMode(true); | |
03647350 | 78 | |
524c47aa SC |
79 | m_peer = wxWidgetImpl::CreateChoice( this, parent, id, m_popUpMenu, pos, size, style, GetExtraStyle() ); |
80 | ||
81 | MacPostControlCreate( pos, size ); | |
82 | ||
83 | #if !wxUSE_STL | |
84 | if ( style & wxCB_SORT ) | |
85 | // autosort | |
86 | m_strings = wxArrayString( 1 ); | |
87 | #endif | |
88 | ||
89 | Append(n, choices); | |
90 | ||
91 | // Set the first item as being selected | |
92 | if (n > 0) | |
93 | SetSelection( 0 ); | |
94 | ||
95 | // Needed because it is a wxControlWithItems | |
96 | SetInitialSize( size ); | |
97 | ||
98 | return true; | |
99 | } | |
100 | ||
101 | // ---------------------------------------------------------------------------- | |
102 | // adding/deleting items to/from the list | |
103 | // ---------------------------------------------------------------------------- | |
104 | ||
105 | int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items, | |
106 | unsigned int pos, | |
107 | void **clientData, wxClientDataType type) | |
108 | { | |
109 | const unsigned int numItems = items.GetCount(); | |
110 | for( unsigned int i = 0; i < numItems; ++i, ++pos ) | |
111 | { | |
112 | unsigned int idx; | |
113 | ||
114 | #if wxUSE_STL | |
115 | if ( IsSorted() ) | |
116 | { | |
117 | wxArrayString::iterator | |
118 | insertPoint = std::lower_bound( m_strings.begin(), m_strings.end(), items[i] ); | |
119 | idx = insertPoint - m_strings.begin(); | |
120 | m_strings.insert( insertPoint, items[i] ); | |
121 | } | |
122 | else | |
123 | #endif // wxUSE_STL | |
124 | { | |
125 | idx = pos; | |
126 | m_strings.Insert( items[i], idx ); | |
127 | } | |
128 | ||
129 | m_popUpMenu->Insert( idx, i+1, items[i] ); | |
130 | m_datas.Insert( NULL, idx ); | |
131 | AssignNewItemClientData(idx, clientData, i, type); | |
132 | } | |
133 | ||
134 | m_peer->SetMaximum( GetCount() ); | |
135 | ||
136 | return pos - 1; | |
137 | } | |
138 | ||
139 | void wxChoice::DoDeleteOneItem(unsigned int n) | |
140 | { | |
141 | wxCHECK_RET( IsValid(n) , wxT("wxChoice::Delete: invalid index") ); | |
142 | ||
143 | if ( HasClientObjectData() ) | |
144 | delete GetClientObject( n ); | |
03647350 | 145 | |
524c47aa SC |
146 | m_popUpMenu->Delete( m_popUpMenu->FindItemByPosition( n ) ); |
147 | ||
148 | m_strings.RemoveAt( n ) ; | |
149 | m_datas.RemoveAt( n ) ; | |
150 | m_peer->SetMaximum( GetCount() ) ; | |
151 | ||
152 | } | |
153 | ||
154 | void wxChoice::DoClear() | |
155 | { | |
156 | for ( unsigned int i = 0 ; i < GetCount() ; i++ ) | |
157 | { | |
158 | m_popUpMenu->Delete( m_popUpMenu->FindItemByPosition( 0 ) ); | |
159 | } | |
160 | ||
161 | m_strings.Empty() ; | |
162 | m_datas.Empty() ; | |
163 | ||
164 | m_peer->SetMaximum( 0 ) ; | |
165 | } | |
166 | ||
167 | // ---------------------------------------------------------------------------- | |
168 | // selection | |
169 | // ---------------------------------------------------------------------------- | |
170 | int wxChoice::GetSelection() const | |
171 | { | |
aaf37267 | 172 | return m_peer->GetValue(); |
524c47aa SC |
173 | } |
174 | ||
175 | void wxChoice::SetSelection( int n ) | |
176 | { | |
aaf37267 | 177 | m_peer->SetValue( n ); |
524c47aa SC |
178 | } |
179 | ||
180 | // ---------------------------------------------------------------------------- | |
181 | // string list functions | |
182 | // ---------------------------------------------------------------------------- | |
183 | ||
184 | unsigned int wxChoice::GetCount() const | |
185 | { | |
186 | return m_strings.GetCount() ; | |
187 | } | |
188 | ||
189 | int wxChoice::FindString( const wxString& s, bool bCase ) const | |
190 | { | |
191 | #if !wxUSE_STL | |
192 | // Avoid assert for non-default args passed to sorted array Index | |
193 | if ( IsSorted() ) | |
194 | bCase = true; | |
195 | #endif | |
196 | ||
197 | return m_strings.Index( s , bCase ) ; | |
198 | } | |
199 | ||
200 | void wxChoice::SetString(unsigned int n, const wxString& s) | |
201 | { | |
202 | wxCHECK_RET( IsValid(n), wxT("wxChoice::SetString(): invalid index") ); | |
203 | ||
204 | m_strings[n] = s ; | |
205 | ||
206 | m_popUpMenu->FindItemByPosition( n )->SetItemLabel( s ) ; | |
207 | } | |
208 | ||
209 | wxString wxChoice::GetString(unsigned int n) const | |
210 | { | |
211 | wxCHECK_MSG( IsValid(n), wxEmptyString, wxT("wxChoice::GetString(): invalid index") ); | |
212 | ||
213 | return m_strings[n] ; | |
214 | } | |
215 | ||
216 | // ---------------------------------------------------------------------------- | |
217 | // client data | |
218 | // ---------------------------------------------------------------------------- | |
219 | void wxChoice::DoSetItemClientData(unsigned int n, void* clientData) | |
220 | { | |
221 | wxCHECK_RET( IsValid(n), wxT("wxChoice::DoSetItemClientData: invalid index") ); | |
222 | ||
223 | m_datas[n] = (char*)clientData ; | |
224 | } | |
225 | ||
226 | void * wxChoice::DoGetItemClientData(unsigned int n) const | |
227 | { | |
228 | wxCHECK_MSG( IsValid(n), NULL, wxT("wxChoice::DoGetClientData: invalid index") ); | |
229 | ||
230 | return (void *)m_datas[n]; | |
231 | } | |
232 | ||
0faf03bf | 233 | bool wxChoice::OSXHandleClicked( double WXUNUSED(timestampsec) ) |
524c47aa SC |
234 | { |
235 | wxCommandEvent event( wxEVT_COMMAND_CHOICE_SELECTED, m_windowId ); | |
236 | ||
237 | // actually n should be made sure by the os to be a valid selection, but ... | |
238 | int n = GetSelection(); | |
239 | if ( n > -1 ) | |
240 | { | |
241 | event.SetInt( n ); | |
242 | event.SetString( GetStringSelection() ); | |
243 | event.SetEventObject( this ); | |
03647350 | 244 | |
524c47aa SC |
245 | if ( HasClientObjectData() ) |
246 | event.SetClientObject( GetClientObject( n ) ); | |
247 | else if ( HasClientUntypedData() ) | |
248 | event.SetClientData( GetClientData( n ) ); | |
249 | ||
250 | ProcessCommand( event ); | |
251 | } | |
252 | ||
253 | return true ; | |
254 | } | |
255 | ||
256 | wxSize wxChoice::DoGetBestSize() const | |
257 | { | |
258 | int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults | |
d33e45f1 SC |
259 | wxSize baseSize = wxWindow::DoGetBestSize(); |
260 | int lbHeight = baseSize.y; | |
524c47aa SC |
261 | int wLine; |
262 | ||
263 | { | |
264 | wxClientDC dc(const_cast<wxChoice*>(this)); | |
265 | ||
266 | // Find the widest line | |
267 | for(unsigned int i = 0; i < GetCount(); i++) | |
268 | { | |
269 | wxString str(GetString(i)); | |
270 | ||
271 | wxCoord width, height ; | |
272 | dc.GetTextExtent( str , &width, &height); | |
273 | wLine = width ; | |
274 | ||
275 | lbWidth = wxMax( lbWidth, wLine ) ; | |
276 | } | |
277 | ||
278 | // Add room for the popup arrow | |
279 | lbWidth += 2 * lbHeight ; | |
280 | ||
281 | wxCoord width, height ; | |
282 | dc.GetTextExtent( wxT("X"), &width, &height); | |
283 | int cx = width ; | |
524c47aa SC |
284 | |
285 | lbWidth += cx ; | |
286 | } | |
287 | ||
288 | return wxSize( lbWidth, lbHeight ); | |
289 | } | |
290 | ||
291 | #endif // wxUSE_CHOICE |