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