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