]> git.saurik.com Git - wxWidgets.git/blob - src/os2/choice.cpp
c989624c46510cbb7464c0e4537695be12a0dc83
[wxWidgets.git] / src / os2 / choice.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifndef WX_PRECOMP
16 #include "wx/choice.h"
17 #include "wx/utils.h"
18 #include "wx/log.h"
19 #include "wx/settings.h"
20 #endif
21
22 #include "wx/os2/private.h"
23
24 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
25
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
34 , const wxValidator& rValidator
35 , const wxString& rsName
36 )
37 {
38 long lSstyle;
39
40 if (!CreateControl( pParent
41 ,vId
42 ,rPos
43 ,rSize
44 ,lStyle
45 ,rValidator
46 ,rsName
47 ))
48 return FALSE;
49 lSstyle = CBS_DROPDOWNLIST |
50 WS_TABSTOP |
51 WS_VISIBLE;
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),
59 wxT("this style flag is ignored by wxChoice, you "
60 "probably want to use a wxComboBox") );
61
62 if (!OS2CreateControl( wxT("COMBOBOX")
63 ,lSstyle
64 ))
65 return FALSE;
66
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 //
71 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
72 for (int i = 0; i < n; i++)
73 {
74 Append(asChoices[i]);
75 }
76 wxFont* pTextFont = new wxFont( 10
77 ,wxMODERN
78 ,wxNORMAL
79 ,wxNORMAL
80 );
81 SetFont(*pTextFont);
82 SetSize( rPos.x
83 ,rPos.y
84 ,rSize.x
85 ,rSize.y
86 );
87 delete pTextFont;
88 return TRUE;
89 } // end of wxChoice::Create
90
91 // ----------------------------------------------------------------------------
92 // adding/deleting items to/from the list
93 // ----------------------------------------------------------------------------
94
95 int wxChoice::DoAppend(
96 const wxString& rsItem
97 )
98 {
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 int wxChoice::DoInsert(
115 const wxString& rsItem
116 int pos
117 )
118 {
119 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
120 wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
121
122 if (pos == GetCount())
123 return DoAppend(item);
124
125 int nIndex;
126 SHORT nIndexType = 0;
127
128 if (m_windowStyle & wxLB_SORT)
129 nIndexType = LIT_SORTASCENDING;
130 else
131 nIndexType = pos;
132 nIndex = (int)::WinSendMsg( GetHwnd()
133 ,LM_INSERTITEM
134 ,(MPARAM)nIndexType
135 ,(MPARAM)rsItem.c_str()
136 );
137 return nIndex;
138 } // end of wxChoice::DoInsert
139
140 void wxChoice::Delete(
141 int n
142 )
143 {
144 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
145 ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)n, (MPARAM)0);
146 } // end of wxChoice::Delete
147
148 void wxChoice::Clear()
149 {
150 Free();
151 ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0);
152 } // end of wxChoice::Clear
153
154 // ----------------------------------------------------------------------------
155 // selection
156 // ----------------------------------------------------------------------------
157
158 int wxChoice::GetSelection() const
159 {
160 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)LIT_FIRST, (MPARAM)0)));
161 } // end of wxChoice::GetSelection
162
163 void wxChoice::SetSelection(
164 int n
165 )
166 {
167 ::WinSendMsg( GetHwnd()
168 ,LM_SELECTITEM
169 ,(MPARAM)n
170 ,(MPARAM)TRUE
171 );
172 } // end of wxChoice::SetSelection
173
174 // ----------------------------------------------------------------------------
175 // string list functions
176 // ----------------------------------------------------------------------------
177
178 int wxChoice::GetCount() const
179 {
180 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT, (MPARAM)0, (MPARAM)0)));
181 } // end of wxChoice::GetCount
182
183 int wxChoice::FindString(
184 const wxString& rsStr
185 ) const
186 {
187 int nPos;
188 int nTextLength;
189 PSZ zStr;
190 int nItemCount;
191
192 nItemCount = (int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT, (MPARAM)0, (MPARAM)0));
193 for (nPos = 0; nPos < nItemCount; nPos++)
194 {
195 nTextLength = (int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)nPos, (MPARAM)0));
196 zStr = new char[nTextLength + 1];
197 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT, MPFROM2SHORT((SHORT)nPos, (SHORT)nTextLength), (MPARAM)zStr);
198 if (rsStr == (char*)zStr)
199 {
200 delete [] zStr;
201 break;
202 }
203 delete [] zStr;
204 }
205 return nPos;
206 } // end of wxChoice::FindString
207
208 void wxChoice::SetString(
209 int n
210 , const wxString& rsStr
211 )
212 {
213 SHORT nIndexType = 0;
214 void* pData;
215
216 if ( m_clientDataItemsType != wxClientData_None )
217 {
218 pData = DoGetItemClientData(n);
219 }
220 else // no client data
221 {
222 pData = NULL;
223 }
224
225 ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)n, 0);
226
227 if (m_windowStyle & wxLB_SORT)
228 nIndexType = LIT_SORTASCENDING;
229 else
230 nIndexType = LIT_END;
231 ::WinSendMsg( GetHwnd()
232 ,LM_INSERTITEM
233 ,(MPARAM)nIndexType
234 ,(MPARAM)rsStr.c_str()
235 );
236
237 if (pData)
238 {
239 DoSetItemClientData( n
240 ,pData
241 );
242 }
243 } // end of wxChoice::SetString
244
245 wxString wxChoice::GetString(
246 int n
247 ) const
248 {
249 size_t nLen = 0;
250 wxString sStr = "";
251 char* 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 char[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(
273 int n
274 , void* pClientData
275 )
276 {
277 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, (MPARAM)n, MPFROMP(pClientData));
278 } // end of wxChoice::DoSetItemClientData
279
280 void* wxChoice::DoGetItemClientData( int n ) const
281 {
282 MRESULT rc = 0L;
283
284 rc = ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE, (MPARAM)n, (MPARAM)0);
285 return((void*)rc);
286 } // end of wxChoice::DoSetItemClientData
287
288 void wxChoice::DoSetItemClientObject(
289 int n
290 , wxClientData* pClientData
291 )
292 {
293 DoSetItemClientData( n
294 ,pClientData
295 );
296 } // end of wxChoice::DoSetItemClientObject
297
298 wxClientData* wxChoice::DoGetItemClientObject(
299 int n
300 ) const
301 {
302 return (wxClientData *)DoGetItemClientData(n);
303 } // end of wxChoice::DoGetItemClientObject
304
305 // ----------------------------------------------------------------------------
306 // wxOS2 specific helpers
307 // ----------------------------------------------------------------------------
308
309 void wxChoice::DoSetSize(
310 int nX
311 , int nY
312 , int nWidth
313 , int nHeight
314 , int nSizeFlags
315 )
316 {
317 //
318 // Ignore height parameter because height doesn't mean 'initially
319 // displayed' height, it refers to the drop-down menu as well. The
320 // wxWindows interpretation is different; also, getting the size returns
321 // the _displayed_ size (NOT the drop down menu size) so
322 // setting-getting-setting size would not work.
323 //
324 wxControl::DoSetSize( nX
325 ,nY
326 ,nWidth
327 ,-1
328 ,nSizeFlags
329 );
330 } // end of wxChoice::DoSetSize
331
332 wxSize wxChoice::DoGetBestSize() const
333 {
334 //
335 // Find the widest string
336 //
337 int nLineWidth;
338 int nChoiceWidth = 0;
339 int nItems = GetCount();
340 int nCx;
341 int nCy;
342
343 for (int i = 0; i < nItems; i++)
344 {
345 wxString sStr(GetString(i));
346
347 GetTextExtent( sStr
348 ,&nLineWidth
349 ,NULL
350 );
351 if (nLineWidth > nChoiceWidth)
352 nChoiceWidth = nLineWidth;
353 }
354
355 //
356 // Give it some reasonable default value if there are no strings in the
357 // list
358 //
359 if (nChoiceWidth == 0L)
360 nChoiceWidth = 100L;
361
362 //
363 // The combobox should be larger than the widest string
364 //
365 wxGetCharSize( GetHWND()
366 ,&nCx
367 ,&nCy
368 ,(wxFont*)&GetFont()
369 );
370 nChoiceWidth += 5 * nCx;
371
372 //
373 // Choice drop-down list depends on number of items (limited to 10)
374 //
375 size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1;
376 int nChoiceHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * nStrings;
377
378 return wxSize( nChoiceWidth
379 ,nChoiceHeight
380 );
381 } // end of wxChoice::DoGetBestSize
382
383 MRESULT wxChoice::OS2WindowProc(
384 WXUINT uMsg
385 , WXWPARAM wParam
386 , WXLPARAM lParam
387 )
388 {
389 return wxWindow::OS2WindowProc( uMsg
390 ,wParam
391 ,lParam
392 );
393 } // end of wxChoice::OS2WindowProc
394
395 bool wxChoice::OS2Command(
396 WXUINT uParam
397 , WXWORD WXUNUSED(wId)
398 )
399 {
400 if (uParam != LN_SELECT)
401 {
402 //
403 // "selection changed" is the only event we're after
404 //
405 return FALSE;
406 }
407 int n = GetSelection();
408
409 if (n > -1)
410 {
411 wxCommandEvent vEvent( wxEVT_COMMAND_CHOICE_SELECTED
412 ,m_windowId
413 );
414
415 vEvent.SetInt(n);
416 vEvent.SetEventObject(this);
417 vEvent.SetString((char*)GetStringSelection().c_str());
418 if (HasClientObjectData())
419 vEvent.SetClientObject(GetClientObject(n));
420 else if (HasClientUntypedData())
421 vEvent.SetClientData(GetClientData(n));
422 ProcessCommand(vEvent);
423 }
424 return TRUE;
425 } // end of wxChoice::OS2Command
426
427 void wxChoice::Free()
428 {
429 if (HasClientObjectData())
430 {
431 size_t nCount = GetCount();
432
433 for (size_t n = 0; n < nCount; n++)
434 {
435 delete GetClientObject(n);
436 }
437 }
438 } // end of wxhoice::Free