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