]>
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 | ||
114 | void wxChoice::Delete( | |
115 | int n | |
116 | ) | |
0e320a79 | 117 | { |
37f214d5 | 118 | wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") ); |
0cf6acbf DW |
119 | ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)n, (MPARAM)0); |
120 | } // end of wxChoice::Delete | |
0e320a79 DW |
121 | |
122 | void wxChoice::Clear() | |
123 | { | |
0cf6acbf DW |
124 | Free(); |
125 | ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0); | |
126 | } // end of wxChoice::Clear | |
0e320a79 | 127 | |
37f214d5 DW |
128 | // ---------------------------------------------------------------------------- |
129 | // selection | |
130 | // ---------------------------------------------------------------------------- | |
131 | ||
0e320a79 DW |
132 | int wxChoice::GetSelection() const |
133 | { | |
0cf6acbf DW |
134 | return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)LIT_FIRST, (MPARAM)0))); |
135 | } // end of wxChoice::GetSelection | |
0e320a79 | 136 | |
0cf6acbf DW |
137 | void wxChoice::SetSelection( |
138 | int n | |
139 | ) | |
0e320a79 | 140 | { |
0cf6acbf DW |
141 | ::WinSendMsg( GetHwnd() |
142 | ,LM_SELECTITEM | |
143 | ,(MPARAM)n | |
144 | ,(MPARAM)TRUE | |
145 | ); | |
146 | } // end of wxChoice::SetSelection | |
37f214d5 DW |
147 | |
148 | // ---------------------------------------------------------------------------- | |
149 | // string list functions | |
150 | // ---------------------------------------------------------------------------- | |
151 | ||
152 | int wxChoice::GetCount() const | |
153 | { | |
0cf6acbf DW |
154 | return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT, (MPARAM)0, (MPARAM)0))); |
155 | } // end of wxChoice::GetCount | |
0e320a79 | 156 | |
0cf6acbf DW |
157 | int wxChoice::FindString( |
158 | const wxString& rsStr | |
159 | ) const | |
0e320a79 | 160 | { |
0cf6acbf DW |
161 | int nPos; |
162 | int nTextLength; | |
163 | PSZ zStr; | |
164 | int nItemCount; | |
37f214d5 | 165 | |
0cf6acbf DW |
166 | nItemCount = (int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT, (MPARAM)0, (MPARAM)0)); |
167 | for (nPos = 0; nPos < nItemCount; nPos++) | |
168 | { | |
169 | nTextLength = (int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)nPos, (MPARAM)0)); | |
170 | zStr = new char[nTextLength + 1]; | |
171 | ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT, MPFROM2SHORT((SHORT)nPos, (SHORT)nTextLength), (MPARAM)zStr); | |
172 | if (rsStr == (char*)zStr) | |
173 | { | |
174 | delete [] zStr; | |
175 | break; | |
176 | } | |
177 | delete [] zStr; | |
178 | } | |
179 | return nPos; | |
180 | } // end of wxChoice::FindString | |
0e320a79 | 181 | |
0cf6acbf DW |
182 | void wxChoice::SetString( |
183 | int n | |
184 | , const wxString& rsStr | |
185 | ) | |
dcd307ee | 186 | { |
58238121 | 187 | SHORT nIndexType = 0; |
57ff8a87 DW |
188 | void* pData; |
189 | ||
190 | if ( m_clientDataItemsType != wxClientData_None ) | |
191 | { | |
192 | pData = DoGetItemClientData(n); | |
193 | } | |
194 | else // no client data | |
195 | { | |
196 | pData = NULL; | |
197 | } | |
dcd307ee | 198 | |
e90bc5bf | 199 | ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)n, 0); |
58238121 | 200 | |
e90bc5bf | 201 | if (m_windowStyle & wxLB_SORT) |
58238121 DW |
202 | nIndexType = LIT_SORTASCENDING; |
203 | else | |
204 | nIndexType = LIT_END; | |
e90bc5bf DW |
205 | ::WinSendMsg( GetHwnd() |
206 | ,LM_INSERTITEM | |
207 | ,(MPARAM)nIndexType | |
208 | ,(MPARAM)rsStr.c_str() | |
209 | ); | |
57ff8a87 DW |
210 | |
211 | if (pData) | |
212 | { | |
213 | DoSetItemClientData( n | |
214 | ,pData | |
215 | ); | |
216 | } | |
0cf6acbf | 217 | } // end of wxChoice::SetString |
dcd307ee | 218 | |
0cf6acbf DW |
219 | wxString wxChoice::GetString( |
220 | int n | |
221 | ) const | |
0e320a79 | 222 | { |
0cf6acbf DW |
223 | size_t nLen = 0; |
224 | wxString sStr = ""; | |
225 | char* zBuf; | |
226 | ||
227 | nLen = (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)n, (MPARAM)0)); | |
cfcebdb1 | 228 | if (nLen != LIT_ERROR && nLen > 0) |
0cf6acbf DW |
229 | { |
230 | zBuf = new char[nLen + 1]; | |
231 | ::WinSendMsg( GetHwnd() | |
232 | ,LM_QUERYITEMTEXT | |
233 | ,MPFROM2SHORT((SHORT)n, (SHORT)nLen) | |
234 | ,(MPARAM)zBuf | |
235 | ); | |
236 | sStr = zBuf; | |
237 | delete [] zBuf; | |
37f214d5 | 238 | } |
0cf6acbf DW |
239 | return sStr; |
240 | } // end of wxChoice::GetString | |
0e320a79 | 241 | |
37f214d5 DW |
242 | // ---------------------------------------------------------------------------- |
243 | // client data | |
244 | // ---------------------------------------------------------------------------- | |
245 | ||
0cf6acbf DW |
246 | void wxChoice::DoSetItemClientData( |
247 | int n | |
248 | , void* pClientData | |
249 | ) | |
0e320a79 | 250 | { |
0cf6acbf DW |
251 | ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, (MPARAM)n, MPFROMP(pClientData)); |
252 | } // end of wxChoice::DoSetItemClientData | |
0e320a79 | 253 | |
dcd307ee | 254 | void* wxChoice::DoGetItemClientData( int n ) const |
0e320a79 | 255 | { |
0cf6acbf | 256 | MRESULT rc = 0L; |
37f214d5 | 257 | |
0cf6acbf DW |
258 | rc = ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE, (MPARAM)n, (MPARAM)0); |
259 | return((void*)rc); | |
260 | } // end of wxChoice::DoSetItemClientData | |
0e320a79 | 261 | |
0cf6acbf DW |
262 | void wxChoice::DoSetItemClientObject( |
263 | int n | |
264 | , wxClientData* pClientData | |
265 | ) | |
0e320a79 | 266 | { |
0cf6acbf DW |
267 | DoSetItemClientData( n |
268 | ,pClientData | |
269 | ); | |
270 | } // end of wxChoice::DoSetItemClientObject | |
271 | ||
272 | wxClientData* wxChoice::DoGetItemClientObject( | |
273 | int n | |
274 | ) const | |
0e320a79 | 275 | { |
e6ebb514 | 276 | return (wxClientData *)DoGetItemClientData(n); |
0cf6acbf | 277 | } // end of wxChoice::DoGetItemClientObject |
37f214d5 DW |
278 | |
279 | // ---------------------------------------------------------------------------- | |
280 | // wxOS2 specific helpers | |
281 | // ---------------------------------------------------------------------------- | |
282 | ||
0cf6acbf DW |
283 | void wxChoice::DoSetSize( |
284 | int nX | |
285 | , int nY | |
286 | , int nWidth | |
287 | , int nHeight | |
288 | , int nSizeFlags | |
289 | ) | |
37f214d5 | 290 | { |
0cf6acbf | 291 | // |
37f214d5 DW |
292 | // Ignore height parameter because height doesn't mean 'initially |
293 | // displayed' height, it refers to the drop-down menu as well. The | |
294 | // wxWindows interpretation is different; also, getting the size returns | |
295 | // the _displayed_ size (NOT the drop down menu size) so | |
296 | // setting-getting-setting size would not work. | |
0cf6acbf DW |
297 | // |
298 | wxControl::DoSetSize( nX | |
299 | ,nY | |
300 | ,nWidth | |
301 | ,-1 | |
302 | ,nSizeFlags | |
303 | ); | |
304 | } // end of wxChoice::DoSetSize | |
37f214d5 | 305 | |
e78c4d50 | 306 | wxSize wxChoice::DoGetBestSize() const |
37f214d5 | 307 | { |
0cf6acbf DW |
308 | // |
309 | // Find the widest string | |
310 | // | |
311 | int nLineWidth; | |
312 | int nChoiceWidth = 0; | |
313 | int nItems = GetCount(); | |
314 | int nCx; | |
315 | int nCy; | |
316 | ||
317 | for (int i = 0; i < nItems; i++) | |
37f214d5 | 318 | { |
0cf6acbf DW |
319 | wxString sStr(GetString(i)); |
320 | ||
321 | GetTextExtent( sStr | |
322 | ,&nLineWidth | |
323 | ,NULL | |
324 | ); | |
325 | if (nLineWidth > nChoiceWidth) | |
326 | nChoiceWidth = nLineWidth; | |
37f214d5 DW |
327 | } |
328 | ||
0cf6acbf DW |
329 | // |
330 | // Give it some reasonable default value if there are no strings in the | |
37f214d5 | 331 | // list |
0cf6acbf DW |
332 | // |
333 | if (nChoiceWidth == 0L) | |
334 | nChoiceWidth = 100L; | |
335 | ||
336 | // | |
337 | // The combobox should be larger than the widest string | |
338 | // | |
339 | wxGetCharSize( GetHWND() | |
340 | ,&nCx | |
341 | ,&nCy | |
342 | ,(wxFont*)&GetFont() | |
343 | ); | |
344 | nChoiceWidth += 5 * nCx; | |
345 | ||
346 | // | |
37f214d5 | 347 | // Choice drop-down list depends on number of items (limited to 10) |
0cf6acbf DW |
348 | // |
349 | size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1; | |
350 | int nChoiceHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * nStrings; | |
351 | ||
352 | return wxSize( nChoiceWidth | |
353 | ,nChoiceHeight | |
354 | ); | |
355 | } // end of wxChoice::DoGetBestSize | |
356 | ||
357 | MRESULT wxChoice::OS2WindowProc( | |
358 | WXUINT uMsg | |
359 | , WXWPARAM wParam | |
360 | , WXLPARAM lParam | |
361 | ) | |
37f214d5 | 362 | { |
0cf6acbf DW |
363 | return wxWindow::OS2WindowProc( uMsg |
364 | ,wParam | |
365 | ,lParam | |
366 | ); | |
367 | } // end of wxChoice::OS2WindowProc | |
368 | ||
369 | bool wxChoice::OS2Command( | |
370 | WXUINT uParam | |
371 | , WXWORD WXUNUSED(wId) | |
372 | ) | |
37f214d5 | 373 | { |
0cf6acbf | 374 | if (uParam != LN_SELECT) |
37f214d5 | 375 | { |
0cf6acbf | 376 | // |
37f214d5 | 377 | // "selection changed" is the only event we're after |
0cf6acbf | 378 | // |
37f214d5 DW |
379 | return FALSE; |
380 | } | |
0cf6acbf | 381 | int n = GetSelection(); |
37f214d5 | 382 | |
0cf6acbf DW |
383 | if (n > -1) |
384 | { | |
385 | wxCommandEvent vEvent( wxEVT_COMMAND_CHOICE_SELECTED | |
386 | ,m_windowId | |
387 | ); | |
388 | ||
389 | vEvent.SetInt(n); | |
390 | vEvent.SetEventObject(this); | |
391 | vEvent.SetString((char*)GetStringSelection().c_str()); | |
392 | if (HasClientObjectData()) | |
393 | vEvent.SetClientObject(GetClientObject(n)); | |
394 | else if (HasClientUntypedData()) | |
395 | vEvent.SetClientData(GetClientData(n)); | |
396 | ProcessCommand(vEvent); | |
397 | } | |
37f214d5 | 398 | return TRUE; |
0cf6acbf | 399 | } // end of wxChoice::OS2Command |
0e320a79 | 400 | |
0cf6acbf DW |
401 | void wxChoice::Free() |
402 | { | |
403 | if (HasClientObjectData()) | |
404 | { | |
405 | size_t nCount = GetCount(); | |
406 | ||
407 | for (size_t n = 0; n < nCount; n++) | |
408 | { | |
409 | delete GetClientObject(n); | |
410 | } | |
411 | } | |
412 | } // end of wxhoice::Free |