]>
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 | ||
708cc394 RD |
129 | wxString text = items[i]; |
130 | if (text == wxEmptyString) | |
131 | text = " "; // menu items can't have empty labels | |
132 | m_popUpMenu->Insert( idx, i+1, text ); | |
524c47aa SC |
133 | m_datas.Insert( NULL, idx ); |
134 | AssignNewItemClientData(idx, clientData, i, type); | |
135 | } | |
136 | ||
137 | m_peer->SetMaximum( GetCount() ); | |
138 | ||
139 | return pos - 1; | |
140 | } | |
141 | ||
142 | void wxChoice::DoDeleteOneItem(unsigned int n) | |
143 | { | |
144 | wxCHECK_RET( IsValid(n) , wxT("wxChoice::Delete: invalid index") ); | |
145 | ||
146 | if ( HasClientObjectData() ) | |
147 | delete GetClientObject( n ); | |
03647350 | 148 | |
524c47aa SC |
149 | m_popUpMenu->Delete( m_popUpMenu->FindItemByPosition( n ) ); |
150 | ||
151 | m_strings.RemoveAt( n ) ; | |
152 | m_datas.RemoveAt( n ) ; | |
153 | m_peer->SetMaximum( GetCount() ) ; | |
154 | ||
155 | } | |
156 | ||
157 | void wxChoice::DoClear() | |
158 | { | |
159 | for ( unsigned int i = 0 ; i < GetCount() ; i++ ) | |
160 | { | |
161 | m_popUpMenu->Delete( m_popUpMenu->FindItemByPosition( 0 ) ); | |
162 | } | |
163 | ||
164 | m_strings.Empty() ; | |
165 | m_datas.Empty() ; | |
166 | ||
167 | m_peer->SetMaximum( 0 ) ; | |
168 | } | |
169 | ||
170 | // ---------------------------------------------------------------------------- | |
171 | // selection | |
172 | // ---------------------------------------------------------------------------- | |
173 | int wxChoice::GetSelection() const | |
174 | { | |
aaf37267 | 175 | return m_peer->GetValue(); |
524c47aa SC |
176 | } |
177 | ||
178 | void wxChoice::SetSelection( int n ) | |
179 | { | |
aaf37267 | 180 | m_peer->SetValue( n ); |
524c47aa SC |
181 | } |
182 | ||
183 | // ---------------------------------------------------------------------------- | |
184 | // string list functions | |
185 | // ---------------------------------------------------------------------------- | |
186 | ||
187 | unsigned int wxChoice::GetCount() const | |
188 | { | |
189 | return m_strings.GetCount() ; | |
190 | } | |
191 | ||
192 | int wxChoice::FindString( const wxString& s, bool bCase ) const | |
193 | { | |
194 | #if !wxUSE_STL | |
195 | // Avoid assert for non-default args passed to sorted array Index | |
196 | if ( IsSorted() ) | |
197 | bCase = true; | |
198 | #endif | |
199 | ||
200 | return m_strings.Index( s , bCase ) ; | |
201 | } | |
202 | ||
203 | void wxChoice::SetString(unsigned int n, const wxString& s) | |
204 | { | |
205 | wxCHECK_RET( IsValid(n), wxT("wxChoice::SetString(): invalid index") ); | |
206 | ||
207 | m_strings[n] = s ; | |
208 | ||
209 | m_popUpMenu->FindItemByPosition( n )->SetItemLabel( s ) ; | |
210 | } | |
211 | ||
212 | wxString wxChoice::GetString(unsigned int n) const | |
213 | { | |
214 | wxCHECK_MSG( IsValid(n), wxEmptyString, wxT("wxChoice::GetString(): invalid index") ); | |
215 | ||
216 | return m_strings[n] ; | |
217 | } | |
218 | ||
219 | // ---------------------------------------------------------------------------- | |
220 | // client data | |
221 | // ---------------------------------------------------------------------------- | |
222 | void wxChoice::DoSetItemClientData(unsigned int n, void* clientData) | |
223 | { | |
224 | wxCHECK_RET( IsValid(n), wxT("wxChoice::DoSetItemClientData: invalid index") ); | |
225 | ||
226 | m_datas[n] = (char*)clientData ; | |
227 | } | |
228 | ||
229 | void * wxChoice::DoGetItemClientData(unsigned int n) const | |
230 | { | |
231 | wxCHECK_MSG( IsValid(n), NULL, wxT("wxChoice::DoGetClientData: invalid index") ); | |
232 | ||
233 | return (void *)m_datas[n]; | |
234 | } | |
235 | ||
0faf03bf | 236 | bool wxChoice::OSXHandleClicked( double WXUNUSED(timestampsec) ) |
524c47aa SC |
237 | { |
238 | wxCommandEvent event( wxEVT_COMMAND_CHOICE_SELECTED, m_windowId ); | |
239 | ||
240 | // actually n should be made sure by the os to be a valid selection, but ... | |
241 | int n = GetSelection(); | |
242 | if ( n > -1 ) | |
243 | { | |
244 | event.SetInt( n ); | |
245 | event.SetString( GetStringSelection() ); | |
246 | event.SetEventObject( this ); | |
03647350 | 247 | |
524c47aa SC |
248 | if ( HasClientObjectData() ) |
249 | event.SetClientObject( GetClientObject( n ) ); | |
250 | else if ( HasClientUntypedData() ) | |
251 | event.SetClientData( GetClientData( n ) ); | |
252 | ||
253 | ProcessCommand( event ); | |
254 | } | |
255 | ||
256 | return true ; | |
257 | } | |
258 | ||
259 | wxSize wxChoice::DoGetBestSize() const | |
260 | { | |
261 | int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults | |
d33e45f1 SC |
262 | wxSize baseSize = wxWindow::DoGetBestSize(); |
263 | int lbHeight = baseSize.y; | |
524c47aa SC |
264 | int wLine; |
265 | ||
266 | { | |
267 | wxClientDC dc(const_cast<wxChoice*>(this)); | |
268 | ||
269 | // Find the widest line | |
270 | for(unsigned int i = 0; i < GetCount(); i++) | |
271 | { | |
272 | wxString str(GetString(i)); | |
273 | ||
274 | wxCoord width, height ; | |
275 | dc.GetTextExtent( str , &width, &height); | |
276 | wLine = width ; | |
277 | ||
278 | lbWidth = wxMax( lbWidth, wLine ) ; | |
279 | } | |
280 | ||
281 | // Add room for the popup arrow | |
282 | lbWidth += 2 * lbHeight ; | |
283 | ||
284 | wxCoord width, height ; | |
285 | dc.GetTextExtent( wxT("X"), &width, &height); | |
286 | int cx = width ; | |
524c47aa SC |
287 | |
288 | lbWidth += cx ; | |
289 | } | |
290 | ||
291 | return wxSize( lbWidth, lbHeight ); | |
292 | } | |
293 | ||
294 | #endif // wxUSE_CHOICE |