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