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