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