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