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