]> git.saurik.com Git - wxWidgets.git/blame - src/os2/choice.cpp
fixed event generation for wxChoice: it now sends one and exactly one wxEVT_COMMAND_C...
[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 }
0cf6acbf
DW
97 SetSize( rPos.x
98 ,rPos.y
99 ,rSize.x
100 ,rSize.y
101 );
312ebad4 102 return true;
0cf6acbf 103} // end of wxChoice::Create
0e320a79 104
37f214d5
DW
105// ----------------------------------------------------------------------------
106// adding/deleting items to/from the list
107// ----------------------------------------------------------------------------
108
0cf6acbf
DW
109int wxChoice::DoAppend(
110 const wxString& rsItem
111)
0e320a79 112{
0cf6acbf 113 int nIndex;
9923c37d 114 LONG nIndexType = 0;
0cf6acbf
DW
115
116 if (m_windowStyle & wxLB_SORT)
117 nIndexType = LIT_SORTASCENDING;
118 else
119 nIndexType = LIT_END;
120 nIndex = (int)::WinSendMsg( GetHwnd()
121 ,LM_INSERTITEM
122 ,(MPARAM)nIndexType
123 ,(MPARAM)rsItem.c_str()
124 );
125 return nIndex;
126} // end of wxChoice::DoAppend
127
243dbf1a 128int wxChoice::DoInsert(
33c6ae1d 129 const wxString& rsItem,
243dbf1a
VZ
130 int pos
131)
132{
133 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
134 wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
135
136 if (pos == GetCount())
33c6ae1d 137 return DoAppend(rsItem);
243dbf1a
VZ
138
139 int nIndex;
9923c37d 140 LONG nIndexType = 0;
243dbf1a
VZ
141
142 if (m_windowStyle & wxLB_SORT)
143 nIndexType = LIT_SORTASCENDING;
144 else
145 nIndexType = pos;
146 nIndex = (int)::WinSendMsg( GetHwnd()
147 ,LM_INSERTITEM
148 ,(MPARAM)nIndexType
149 ,(MPARAM)rsItem.c_str()
150 );
151 return nIndex;
152} // end of wxChoice::DoInsert
153
0cf6acbf
DW
154void wxChoice::Delete(
155 int n
156)
0e320a79 157{
37f214d5 158 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
0cf6acbf
DW
159 ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)n, (MPARAM)0);
160} // end of wxChoice::Delete
0e320a79
DW
161
162void wxChoice::Clear()
163{
0cf6acbf
DW
164 Free();
165 ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0);
166} // end of wxChoice::Clear
0e320a79 167
37f214d5
DW
168// ----------------------------------------------------------------------------
169// selection
170// ----------------------------------------------------------------------------
171
0e320a79
DW
172int wxChoice::GetSelection() const
173{
0cf6acbf
DW
174 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)LIT_FIRST, (MPARAM)0)));
175} // end of wxChoice::GetSelection
0e320a79 176
0cf6acbf
DW
177void wxChoice::SetSelection(
178 int n
179)
0e320a79 180{
0cf6acbf
DW
181 ::WinSendMsg( GetHwnd()
182 ,LM_SELECTITEM
183 ,(MPARAM)n
184 ,(MPARAM)TRUE
185 );
186} // end of wxChoice::SetSelection
37f214d5
DW
187
188// ----------------------------------------------------------------------------
189// string list functions
190// ----------------------------------------------------------------------------
191
192int wxChoice::GetCount() const
193{
0cf6acbf
DW
194 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT, (MPARAM)0, (MPARAM)0)));
195} // end of wxChoice::GetCount
0e320a79 196
11e62fe6 197void wxChoice::SetString( int n, const wxString& rsStr )
0e320a79 198{
11e62fe6
WS
199 LONG nIndexType = 0;
200 void* pData;
57ff8a87
DW
201
202 if ( m_clientDataItemsType != wxClientData_None )
203 {
204 pData = DoGetItemClientData(n);
205 }
206 else // no client data
207 {
208 pData = NULL;
209 }
dcd307ee 210
e90bc5bf 211 ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)n, 0);
58238121 212
e90bc5bf 213 if (m_windowStyle & wxLB_SORT)
58238121
DW
214 nIndexType = LIT_SORTASCENDING;
215 else
216 nIndexType = LIT_END;
e90bc5bf
DW
217 ::WinSendMsg( GetHwnd()
218 ,LM_INSERTITEM
219 ,(MPARAM)nIndexType
220 ,(MPARAM)rsStr.c_str()
221 );
57ff8a87
DW
222
223 if (pData)
224 {
225 DoSetItemClientData( n
226 ,pData
227 );
228 }
0cf6acbf 229} // end of wxChoice::SetString
dcd307ee 230
11e62fe6 231wxString wxChoice::GetString(int n) const
0e320a79 232{
11e62fe6
WS
233 int nLen = 0;
234 wxString sStr = wxEmptyString;
235 wxChar* zBuf;
0cf6acbf
DW
236
237 nLen = (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)n, (MPARAM)0));
cfcebdb1 238 if (nLen != LIT_ERROR && nLen > 0)
0cf6acbf 239 {
0fba44b4 240 zBuf = new wxChar[nLen + 1];
0cf6acbf
DW
241 ::WinSendMsg( GetHwnd()
242 ,LM_QUERYITEMTEXT
243 ,MPFROM2SHORT((SHORT)n, (SHORT)nLen)
244 ,(MPARAM)zBuf
245 );
246 sStr = zBuf;
247 delete [] zBuf;
37f214d5 248 }
0cf6acbf
DW
249 return sStr;
250} // end of wxChoice::GetString
0e320a79 251
37f214d5
DW
252// ----------------------------------------------------------------------------
253// client data
254// ----------------------------------------------------------------------------
255
0cf6acbf
DW
256void wxChoice::DoSetItemClientData(
257 int n
258, void* pClientData
259)
0e320a79 260{
0cf6acbf
DW
261 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, (MPARAM)n, MPFROMP(pClientData));
262} // end of wxChoice::DoSetItemClientData
0e320a79 263
dcd307ee 264void* wxChoice::DoGetItemClientData( int n ) const
0e320a79 265{
0cf6acbf 266 MRESULT rc = 0L;
37f214d5 267
0cf6acbf
DW
268 rc = ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE, (MPARAM)n, (MPARAM)0);
269 return((void*)rc);
270} // end of wxChoice::DoSetItemClientData
0e320a79 271
0cf6acbf
DW
272void wxChoice::DoSetItemClientObject(
273 int n
274, wxClientData* pClientData
275)
0e320a79 276{
0cf6acbf
DW
277 DoSetItemClientData( n
278 ,pClientData
279 );
280} // end of wxChoice::DoSetItemClientObject
281
282wxClientData* wxChoice::DoGetItemClientObject(
283 int n
284) const
0e320a79 285{
e6ebb514 286 return (wxClientData *)DoGetItemClientData(n);
0cf6acbf 287} // end of wxChoice::DoGetItemClientObject
37f214d5
DW
288
289// ----------------------------------------------------------------------------
290// wxOS2 specific helpers
291// ----------------------------------------------------------------------------
292
6670f564
WS
293void wxChoice::DoSetSize(int nX,
294 int nY,
295 int nWidth,
296 int WXUNUSED(nHeight),
297 int nSizeFlags)
37f214d5 298{
0cf6acbf 299 //
37f214d5
DW
300 // Ignore height parameter because height doesn't mean 'initially
301 // displayed' height, it refers to the drop-down menu as well. The
77ffb593 302 // wxWidgets interpretation is different; also, getting the size returns
37f214d5
DW
303 // the _displayed_ size (NOT the drop down menu size) so
304 // setting-getting-setting size would not work.
0cf6acbf
DW
305 //
306 wxControl::DoSetSize( nX
307 ,nY
308 ,nWidth
6670f564 309 ,wxDefaultCoord
0cf6acbf
DW
310 ,nSizeFlags
311 );
312} // end of wxChoice::DoSetSize
37f214d5 313
e78c4d50 314wxSize wxChoice::DoGetBestSize() const
37f214d5 315{
0cf6acbf
DW
316 //
317 // Find the widest string
318 //
319 int nLineWidth;
320 int nChoiceWidth = 0;
321 int nItems = GetCount();
322 int nCx;
323 int nCy;
0d598bae 324 wxFont vFont = (wxFont)GetFont();
0cf6acbf
DW
325
326 for (int i = 0; i < nItems; i++)
37f214d5 327 {
0cf6acbf
DW
328 wxString sStr(GetString(i));
329
330 GetTextExtent( sStr
331 ,&nLineWidth
332 ,NULL
333 );
334 if (nLineWidth > nChoiceWidth)
335 nChoiceWidth = nLineWidth;
37f214d5
DW
336 }
337
0cf6acbf
DW
338 //
339 // Give it some reasonable default value if there are no strings in the
37f214d5 340 // list
0cf6acbf
DW
341 //
342 if (nChoiceWidth == 0L)
343 nChoiceWidth = 100L;
344
345 //
346 // The combobox should be larger than the widest string
347 //
348 wxGetCharSize( GetHWND()
349 ,&nCx
350 ,&nCy
0d598bae 351 ,&vFont
0cf6acbf
DW
352 );
353 nChoiceWidth += 5 * nCx;
354
355 //
37f214d5 356 // Choice drop-down list depends on number of items (limited to 10)
0cf6acbf
DW
357 //
358 size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1;
359 int nChoiceHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * nStrings;
360
361 return wxSize( nChoiceWidth
362 ,nChoiceHeight
363 );
364} // end of wxChoice::DoGetBestSize
365
366MRESULT wxChoice::OS2WindowProc(
367 WXUINT uMsg
368, WXWPARAM wParam
369, WXLPARAM lParam
370)
37f214d5 371{
0cf6acbf
DW
372 return wxWindow::OS2WindowProc( uMsg
373 ,wParam
374 ,lParam
375 );
376} // end of wxChoice::OS2WindowProc
377
378bool wxChoice::OS2Command(
379 WXUINT uParam
380, WXWORD WXUNUSED(wId)
381)
37f214d5 382{
0cf6acbf 383 if (uParam != LN_SELECT)
37f214d5 384 {
0cf6acbf 385 //
37f214d5 386 // "selection changed" is the only event we're after
0cf6acbf 387 //
312ebad4 388 return false;
37f214d5 389 }
0cf6acbf 390 int n = GetSelection();
37f214d5 391
0cf6acbf
DW
392 if (n > -1)
393 {
394 wxCommandEvent vEvent( wxEVT_COMMAND_CHOICE_SELECTED
395 ,m_windowId
396 );
397
398 vEvent.SetInt(n);
399 vEvent.SetEventObject(this);
0fba44b4 400 vEvent.SetString(GetStringSelection());
0cf6acbf
DW
401 if (HasClientObjectData())
402 vEvent.SetClientObject(GetClientObject(n));
403 else if (HasClientUntypedData())
404 vEvent.SetClientData(GetClientData(n));
405 ProcessCommand(vEvent);
406 }
312ebad4 407 return true;
0cf6acbf 408} // end of wxChoice::OS2Command
0e320a79 409
0cf6acbf
DW
410void wxChoice::Free()
411{
412 if (HasClientObjectData())
413 {
414 size_t nCount = GetCount();
415
416 for (size_t n = 0; n < nCount; n++)
417 {
418 delete GetClientObject(n);
419 }
420 }
312ebad4
WS
421} // end of wxChoice::Free
422
423#endif // wxUSE_CHOICE