]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: choice.cpp | |
3 | // Purpose: wxChoice | |
4 | // Author: David Webster | |
5 | // Modified by: | |
6 | // Created: 10/13/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) David Webster | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #include "wx/defs.h" | |
16 | ||
17 | #if wxUSE_CHOICE | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/choice.h" | |
21 | #include "wx/utils.h" | |
22 | #include "wx/log.h" | |
23 | #include "wx/settings.h" | |
24 | #endif | |
25 | ||
26 | #include "wx/os2/private.h" | |
27 | ||
28 | IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl) | |
29 | ||
30 | bool wxChoice::Create( | |
31 | wxWindow* pParent | |
32 | , wxWindowID vId | |
33 | , const wxPoint& rPos | |
34 | , const wxSize& rSize | |
35 | , const wxArrayString& asChoices | |
36 | , long lStyle | |
37 | , const wxValidator& rValidator | |
38 | , const wxString& rsName | |
39 | ) | |
40 | { | |
41 | wxCArrayString chs(asChoices); | |
42 | ||
43 | return Create(pParent, vId, rPos, rSize, chs.GetCount(), chs.GetStrings(), | |
44 | lStyle, rValidator, rsName); | |
45 | } | |
46 | ||
47 | bool wxChoice::Create( | |
48 | wxWindow* pParent | |
49 | , wxWindowID vId | |
50 | , const wxPoint& rPos | |
51 | , const wxSize& rSize | |
52 | , int n | |
53 | , const wxString asChoices[] | |
54 | , long lStyle | |
55 | , const wxValidator& rValidator | |
56 | , const wxString& rsName | |
57 | ) | |
58 | { | |
59 | long lSstyle; | |
60 | ||
61 | if (!CreateControl( pParent | |
62 | ,vId | |
63 | ,rPos | |
64 | ,rSize | |
65 | ,lStyle | |
66 | ,rValidator | |
67 | ,rsName | |
68 | )) | |
69 | return false; | |
70 | lSstyle = CBS_DROPDOWNLIST | | |
71 | WS_TABSTOP | | |
72 | WS_VISIBLE; | |
73 | ||
74 | if (lStyle & wxCLIP_SIBLINGS ) | |
75 | lSstyle |= WS_CLIPSIBLINGS; | |
76 | ||
77 | wxASSERT_MSG( !(lStyle & wxCB_DROPDOWN) && | |
78 | !(lStyle & wxCB_READONLY) && | |
79 | !(lStyle & wxCB_SIMPLE), | |
80 | wxT("this style flag is ignored by wxChoice, you " | |
81 | "probably want to use a wxComboBox") ); | |
82 | ||
83 | if (!OS2CreateControl( wxT("COMBOBOX") | |
84 | ,lSstyle | |
85 | )) | |
86 | return false; | |
87 | ||
88 | // | |
89 | // A choice/combobox normally has a white background (or other, depending | |
90 | // on global settings) rather than inheriting the parent's background colour. | |
91 | // | |
92 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
93 | for (int i = 0; i < n; i++) | |
94 | { | |
95 | Append(asChoices[i]); | |
96 | } | |
97 | wxFont* pTextFont = new wxFont( 10 | |
98 | ,wxMODERN | |
99 | ,wxNORMAL | |
100 | ,wxNORMAL | |
101 | ); | |
102 | SetFont(*pTextFont); | |
103 | SetSize( rPos.x | |
104 | ,rPos.y | |
105 | ,rSize.x | |
106 | ,rSize.y | |
107 | ); | |
108 | delete pTextFont; | |
109 | return true; | |
110 | } // end of wxChoice::Create | |
111 | ||
112 | // ---------------------------------------------------------------------------- | |
113 | // adding/deleting items to/from the list | |
114 | // ---------------------------------------------------------------------------- | |
115 | ||
116 | int wxChoice::DoAppend( | |
117 | const wxString& rsItem | |
118 | ) | |
119 | { | |
120 | int nIndex; | |
121 | LONG nIndexType = 0; | |
122 | ||
123 | if (m_windowStyle & wxLB_SORT) | |
124 | nIndexType = LIT_SORTASCENDING; | |
125 | else | |
126 | nIndexType = LIT_END; | |
127 | nIndex = (int)::WinSendMsg( GetHwnd() | |
128 | ,LM_INSERTITEM | |
129 | ,(MPARAM)nIndexType | |
130 | ,(MPARAM)rsItem.c_str() | |
131 | ); | |
132 | return nIndex; | |
133 | } // end of wxChoice::DoAppend | |
134 | ||
135 | int wxChoice::DoInsert( | |
136 | const wxString& rsItem, | |
137 | int pos | |
138 | ) | |
139 | { | |
140 | wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list")); | |
141 | wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); | |
142 | ||
143 | if (pos == GetCount()) | |
144 | return DoAppend(rsItem); | |
145 | ||
146 | int nIndex; | |
147 | LONG nIndexType = 0; | |
148 | ||
149 | if (m_windowStyle & wxLB_SORT) | |
150 | nIndexType = LIT_SORTASCENDING; | |
151 | else | |
152 | nIndexType = pos; | |
153 | nIndex = (int)::WinSendMsg( GetHwnd() | |
154 | ,LM_INSERTITEM | |
155 | ,(MPARAM)nIndexType | |
156 | ,(MPARAM)rsItem.c_str() | |
157 | ); | |
158 | return nIndex; | |
159 | } // end of wxChoice::DoInsert | |
160 | ||
161 | void wxChoice::Delete( | |
162 | int n | |
163 | ) | |
164 | { | |
165 | wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") ); | |
166 | ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)n, (MPARAM)0); | |
167 | } // end of wxChoice::Delete | |
168 | ||
169 | void wxChoice::Clear() | |
170 | { | |
171 | Free(); | |
172 | ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0); | |
173 | } // end of wxChoice::Clear | |
174 | ||
175 | // ---------------------------------------------------------------------------- | |
176 | // selection | |
177 | // ---------------------------------------------------------------------------- | |
178 | ||
179 | int wxChoice::GetSelection() const | |
180 | { | |
181 | return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)LIT_FIRST, (MPARAM)0))); | |
182 | } // end of wxChoice::GetSelection | |
183 | ||
184 | void wxChoice::SetSelection( | |
185 | int n | |
186 | ) | |
187 | { | |
188 | ::WinSendMsg( GetHwnd() | |
189 | ,LM_SELECTITEM | |
190 | ,(MPARAM)n | |
191 | ,(MPARAM)TRUE | |
192 | ); | |
193 | } // end of wxChoice::SetSelection | |
194 | ||
195 | // ---------------------------------------------------------------------------- | |
196 | // string list functions | |
197 | // ---------------------------------------------------------------------------- | |
198 | ||
199 | int wxChoice::GetCount() const | |
200 | { | |
201 | return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT, (MPARAM)0, (MPARAM)0))); | |
202 | } // end of wxChoice::GetCount | |
203 | ||
204 | int wxChoice::FindString( | |
205 | const wxString& rsStr | |
206 | ) const | |
207 | { | |
208 | int nPos; | |
209 | int nTextLength; | |
210 | PSZ zStr; | |
211 | int nItemCount; | |
212 | ||
213 | nItemCount = (int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT, (MPARAM)0, (MPARAM)0)); | |
214 | for (nPos = 0; nPos < nItemCount; nPos++) | |
215 | { | |
216 | nTextLength = (int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)nPos, (MPARAM)0)); | |
217 | zStr = new char[nTextLength + 1]; | |
218 | ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT, MPFROM2SHORT((SHORT)nPos, (SHORT)nTextLength), (MPARAM)zStr); | |
219 | if (rsStr == (wxChar*)zStr) | |
220 | { | |
221 | delete [] zStr; | |
222 | break; | |
223 | } | |
224 | delete [] zStr; | |
225 | } | |
226 | return nPos; | |
227 | } // end of wxChoice::FindString | |
228 | ||
229 | void wxChoice::SetString( | |
230 | int n | |
231 | , const wxString& rsStr | |
232 | ) | |
233 | { | |
234 | LONG nIndexType = 0; | |
235 | void* pData; | |
236 | ||
237 | if ( m_clientDataItemsType != wxClientData_None ) | |
238 | { | |
239 | pData = DoGetItemClientData(n); | |
240 | } | |
241 | else // no client data | |
242 | { | |
243 | pData = NULL; | |
244 | } | |
245 | ||
246 | ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)n, 0); | |
247 | ||
248 | if (m_windowStyle & wxLB_SORT) | |
249 | nIndexType = LIT_SORTASCENDING; | |
250 | else | |
251 | nIndexType = LIT_END; | |
252 | ::WinSendMsg( GetHwnd() | |
253 | ,LM_INSERTITEM | |
254 | ,(MPARAM)nIndexType | |
255 | ,(MPARAM)rsStr.c_str() | |
256 | ); | |
257 | ||
258 | if (pData) | |
259 | { | |
260 | DoSetItemClientData( n | |
261 | ,pData | |
262 | ); | |
263 | } | |
264 | } // end of wxChoice::SetString | |
265 | ||
266 | wxString wxChoice::GetString( | |
267 | int n | |
268 | ) const | |
269 | { | |
270 | int nLen = 0; | |
271 | wxString sStr = wxEmptyString; | |
272 | wxChar* zBuf; | |
273 | ||
274 | nLen = (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)n, (MPARAM)0)); | |
275 | if (nLen != LIT_ERROR && nLen > 0) | |
276 | { | |
277 | zBuf = new wxChar[nLen + 1]; | |
278 | ::WinSendMsg( GetHwnd() | |
279 | ,LM_QUERYITEMTEXT | |
280 | ,MPFROM2SHORT((SHORT)n, (SHORT)nLen) | |
281 | ,(MPARAM)zBuf | |
282 | ); | |
283 | sStr = zBuf; | |
284 | delete [] zBuf; | |
285 | } | |
286 | return sStr; | |
287 | } // end of wxChoice::GetString | |
288 | ||
289 | // ---------------------------------------------------------------------------- | |
290 | // client data | |
291 | // ---------------------------------------------------------------------------- | |
292 | ||
293 | void wxChoice::DoSetItemClientData( | |
294 | int n | |
295 | , void* pClientData | |
296 | ) | |
297 | { | |
298 | ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, (MPARAM)n, MPFROMP(pClientData)); | |
299 | } // end of wxChoice::DoSetItemClientData | |
300 | ||
301 | void* wxChoice::DoGetItemClientData( int n ) const | |
302 | { | |
303 | MRESULT rc = 0L; | |
304 | ||
305 | rc = ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE, (MPARAM)n, (MPARAM)0); | |
306 | return((void*)rc); | |
307 | } // end of wxChoice::DoSetItemClientData | |
308 | ||
309 | void wxChoice::DoSetItemClientObject( | |
310 | int n | |
311 | , wxClientData* pClientData | |
312 | ) | |
313 | { | |
314 | DoSetItemClientData( n | |
315 | ,pClientData | |
316 | ); | |
317 | } // end of wxChoice::DoSetItemClientObject | |
318 | ||
319 | wxClientData* wxChoice::DoGetItemClientObject( | |
320 | int n | |
321 | ) const | |
322 | { | |
323 | return (wxClientData *)DoGetItemClientData(n); | |
324 | } // end of wxChoice::DoGetItemClientObject | |
325 | ||
326 | // ---------------------------------------------------------------------------- | |
327 | // wxOS2 specific helpers | |
328 | // ---------------------------------------------------------------------------- | |
329 | ||
330 | void wxChoice::DoSetSize( | |
331 | int nX | |
332 | , int nY | |
333 | , int nWidth | |
334 | , int nHeight | |
335 | , int nSizeFlags | |
336 | ) | |
337 | { | |
338 | // | |
339 | // Ignore height parameter because height doesn't mean 'initially | |
340 | // displayed' height, it refers to the drop-down menu as well. The | |
341 | // wxWidgets interpretation is different; also, getting the size returns | |
342 | // the _displayed_ size (NOT the drop down menu size) so | |
343 | // setting-getting-setting size would not work. | |
344 | // | |
345 | wxControl::DoSetSize( nX | |
346 | ,nY | |
347 | ,nWidth | |
348 | ,-1 | |
349 | ,nSizeFlags | |
350 | ); | |
351 | } // end of wxChoice::DoSetSize | |
352 | ||
353 | wxSize wxChoice::DoGetBestSize() const | |
354 | { | |
355 | // | |
356 | // Find the widest string | |
357 | // | |
358 | int nLineWidth; | |
359 | int nChoiceWidth = 0; | |
360 | int nItems = GetCount(); | |
361 | int nCx; | |
362 | int nCy; | |
363 | wxFont vFont = (wxFont)GetFont(); | |
364 | ||
365 | for (int i = 0; i < nItems; i++) | |
366 | { | |
367 | wxString sStr(GetString(i)); | |
368 | ||
369 | GetTextExtent( sStr | |
370 | ,&nLineWidth | |
371 | ,NULL | |
372 | ); | |
373 | if (nLineWidth > nChoiceWidth) | |
374 | nChoiceWidth = nLineWidth; | |
375 | } | |
376 | ||
377 | // | |
378 | // Give it some reasonable default value if there are no strings in the | |
379 | // list | |
380 | // | |
381 | if (nChoiceWidth == 0L) | |
382 | nChoiceWidth = 100L; | |
383 | ||
384 | // | |
385 | // The combobox should be larger than the widest string | |
386 | // | |
387 | wxGetCharSize( GetHWND() | |
388 | ,&nCx | |
389 | ,&nCy | |
390 | ,&vFont | |
391 | ); | |
392 | nChoiceWidth += 5 * nCx; | |
393 | ||
394 | // | |
395 | // Choice drop-down list depends on number of items (limited to 10) | |
396 | // | |
397 | size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1; | |
398 | int nChoiceHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * nStrings; | |
399 | ||
400 | return wxSize( nChoiceWidth | |
401 | ,nChoiceHeight | |
402 | ); | |
403 | } // end of wxChoice::DoGetBestSize | |
404 | ||
405 | MRESULT wxChoice::OS2WindowProc( | |
406 | WXUINT uMsg | |
407 | , WXWPARAM wParam | |
408 | , WXLPARAM lParam | |
409 | ) | |
410 | { | |
411 | return wxWindow::OS2WindowProc( uMsg | |
412 | ,wParam | |
413 | ,lParam | |
414 | ); | |
415 | } // end of wxChoice::OS2WindowProc | |
416 | ||
417 | bool wxChoice::OS2Command( | |
418 | WXUINT uParam | |
419 | , WXWORD WXUNUSED(wId) | |
420 | ) | |
421 | { | |
422 | if (uParam != LN_SELECT) | |
423 | { | |
424 | // | |
425 | // "selection changed" is the only event we're after | |
426 | // | |
427 | return false; | |
428 | } | |
429 | int n = GetSelection(); | |
430 | ||
431 | if (n > -1) | |
432 | { | |
433 | wxCommandEvent vEvent( wxEVT_COMMAND_CHOICE_SELECTED | |
434 | ,m_windowId | |
435 | ); | |
436 | ||
437 | vEvent.SetInt(n); | |
438 | vEvent.SetEventObject(this); | |
439 | vEvent.SetString(GetStringSelection()); | |
440 | if (HasClientObjectData()) | |
441 | vEvent.SetClientObject(GetClientObject(n)); | |
442 | else if (HasClientUntypedData()) | |
443 | vEvent.SetClientData(GetClientData(n)); | |
444 | ProcessCommand(vEvent); | |
445 | } | |
446 | return true; | |
447 | } // end of wxChoice::OS2Command | |
448 | ||
449 | void wxChoice::Free() | |
450 | { | |
451 | if (HasClientObjectData()) | |
452 | { | |
453 | size_t nCount = GetCount(); | |
454 | ||
455 | for (size_t n = 0; n < nCount; n++) | |
456 | { | |
457 | delete GetClientObject(n); | |
458 | } | |
459 | } | |
460 | } // end of wxChoice::Free | |
461 | ||
462 | #endif // wxUSE_CHOICE |