]> git.saurik.com Git - wxWidgets.git/blame - src/os2/choice.cpp
[ 1495131 ] wxODComboBox const methods to work prior to SetPopupControl.
[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
0e320a79 27IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
0e320a79 28
584ad2a3
MB
29bool 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
0cf6acbf
DW
46bool 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
0cf6acbf 54, const wxValidator& rValidator
0cf6acbf
DW
55, const wxString& rsName
56)
0e320a79 57{
0cf6acbf
DW
58 long lSstyle;
59
b9b1d6c8
DW
60 if (!CreateControl( pParent
61 ,vId
62 ,rPos
63 ,rSize
64 ,lStyle
b9b1d6c8 65 ,rValidator
b9b1d6c8
DW
66 ,rsName
67 ))
312ebad4 68 return false;
0cf6acbf 69 lSstyle = CBS_DROPDOWNLIST |
5d44b24e
DW
70 WS_TABSTOP |
71 WS_VISIBLE;
0cf6acbf
DW
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),
37f214d5
DW
79 wxT("this style flag is ignored by wxChoice, you "
80 "probably want to use a wxComboBox") );
0e320a79 81
0cf6acbf
DW
82 if (!OS2CreateControl( wxT("COMBOBOX")
83 ,lSstyle
84 ))
312ebad4 85 return false;
0e320a79 86
0cf6acbf
DW
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 //
a756f210 91 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
0934d91c
SN
92
93 // initialize the controls contents
0cf6acbf 94 for (int i = 0; i < n; i++)
37f214d5 95 {
0cf6acbf 96 Append(asChoices[i]);
37f214d5 97 }
0cf6acbf
DW
98 SetSize( rPos.x
99 ,rPos.y
100 ,rSize.x
101 ,rSize.y
102 );
312ebad4 103 return true;
0cf6acbf 104} // end of wxChoice::Create
0e320a79 105
0934d91c
SN
106wxChoice::~wxChoice()
107{
108 Free();
109}
110
37f214d5
DW
111// ----------------------------------------------------------------------------
112// adding/deleting items to/from the list
113// ----------------------------------------------------------------------------
114
0cf6acbf
DW
115int wxChoice::DoAppend(
116 const wxString& rsItem
117)
0e320a79 118{
0cf6acbf 119 int nIndex;
9923c37d 120 LONG nIndexType = 0;
0cf6acbf
DW
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
aa61d352 134int wxChoice::DoInsert( const wxString& rsItem, unsigned int pos )
243dbf1a
VZ
135{
136 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
8228b893 137 wxCHECK_MSG(IsValidInsert(pos), -1, wxT("invalid index"));
243dbf1a 138
aa61d352 139 if (pos == GetCount())
33c6ae1d 140 return DoAppend(rsItem);
243dbf1a 141
8228b893
WS
142 int nIndex;
143 LONG nIndexType = 0;
243dbf1a
VZ
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
aa61d352 157void wxChoice::Delete(unsigned int n)
0e320a79 158{
8228b893 159 wxCHECK_RET( IsValid(n), wxT("invalid item index in wxChoice::Delete") );
0934d91c
SN
160
161 if ( HasClientObjectData() )
162 {
163 delete GetClientObject(n);
164 }
165
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 179int wxChoice::GetSelection() const
0934d91c
SN
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
190int wxChoice::GetCurrentSelection() const
0e320a79 191{
0cf6acbf
DW
192 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)LIT_FIRST, (MPARAM)0)));
193} // end of wxChoice::GetSelection
0e320a79 194
0cf6acbf
DW
195void wxChoice::SetSelection(
196 int n
197)
0e320a79 198{
0cf6acbf
DW
199 ::WinSendMsg( GetHwnd()
200 ,LM_SELECTITEM
201 ,(MPARAM)n
202 ,(MPARAM)TRUE
203 );
204} // end of wxChoice::SetSelection
37f214d5
DW
205
206// ----------------------------------------------------------------------------
207// string list functions
208// ----------------------------------------------------------------------------
209
aa61d352 210unsigned int wxChoice::GetCount() const
37f214d5 211{
aa61d352 212 return((unsigned int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT, (MPARAM)0, (MPARAM)0)));
0cf6acbf 213} // end of wxChoice::GetCount
0e320a79 214
aa61d352 215void wxChoice::SetString(unsigned int n, const wxString& rsStr)
0e320a79 216{
11e62fe6
WS
217 LONG nIndexType = 0;
218 void* pData;
57ff8a87
DW
219
220 if ( m_clientDataItemsType != wxClientData_None )
221 {
222 pData = DoGetItemClientData(n);
223 }
224 else // no client data
225 {
226 pData = NULL;
227 }
dcd307ee 228
e90bc5bf 229 ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)n, 0);
58238121 230
e90bc5bf 231 if (m_windowStyle & wxLB_SORT)
58238121
DW
232 nIndexType = LIT_SORTASCENDING;
233 else
234 nIndexType = LIT_END;
e90bc5bf
DW
235 ::WinSendMsg( GetHwnd()
236 ,LM_INSERTITEM
237 ,(MPARAM)nIndexType
238 ,(MPARAM)rsStr.c_str()
239 );
57ff8a87
DW
240
241 if (pData)
242 {
aa61d352 243 DoSetItemClientData(n, pData);
57ff8a87 244 }
0cf6acbf 245} // end of wxChoice::SetString
dcd307ee 246
aa61d352 247wxString wxChoice::GetString(unsigned int n) const
0e320a79 248{
11e62fe6
WS
249 int nLen = 0;
250 wxString sStr = wxEmptyString;
251 wxChar* zBuf;
0cf6acbf
DW
252
253 nLen = (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)n, (MPARAM)0));
cfcebdb1 254 if (nLen != LIT_ERROR && nLen > 0)
0cf6acbf 255 {
0fba44b4 256 zBuf = new wxChar[nLen + 1];
0cf6acbf
DW
257 ::WinSendMsg( GetHwnd()
258 ,LM_QUERYITEMTEXT
259 ,MPFROM2SHORT((SHORT)n, (SHORT)nLen)
260 ,(MPARAM)zBuf
261 );
262 sStr = zBuf;
263 delete [] zBuf;
37f214d5 264 }
0cf6acbf
DW
265 return sStr;
266} // end of wxChoice::GetString
0e320a79 267
37f214d5
DW
268// ----------------------------------------------------------------------------
269// client data
270// ----------------------------------------------------------------------------
271
aa61d352 272void wxChoice::DoSetItemClientData(unsigned int n, void* pClientData)
0e320a79 273{
0cf6acbf
DW
274 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, (MPARAM)n, MPFROMP(pClientData));
275} // end of wxChoice::DoSetItemClientData
0e320a79 276
aa61d352 277void* wxChoice::DoGetItemClientData(unsigned int n) const
0e320a79 278{
aa61d352 279 MRESULT rc = ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE, (MPARAM)n, (MPARAM)0);
0cf6acbf 280 return((void*)rc);
aa61d352 281} // end of wxChoice::DoGetItemClientData
0e320a79 282
aa61d352 283void wxChoice::DoSetItemClientObject(unsigned int n, wxClientData* pClientData)
0e320a79 284{
aa61d352 285 DoSetItemClientData(n, pClientData);
0cf6acbf
DW
286} // end of wxChoice::DoSetItemClientObject
287
aa61d352 288wxClientData* wxChoice::DoGetItemClientObject(unsigned int n) const
0e320a79 289{
e6ebb514 290 return (wxClientData *)DoGetItemClientData(n);
0cf6acbf 291} // end of wxChoice::DoGetItemClientObject
37f214d5
DW
292
293// ----------------------------------------------------------------------------
294// wxOS2 specific helpers
295// ----------------------------------------------------------------------------
296
6670f564
WS
297void wxChoice::DoSetSize(int nX,
298 int nY,
299 int nWidth,
300 int WXUNUSED(nHeight),
301 int nSizeFlags)
37f214d5 302{
0cf6acbf 303 //
37f214d5
DW
304 // Ignore height parameter because height doesn't mean 'initially
305 // displayed' height, it refers to the drop-down menu as well. The
77ffb593 306 // wxWidgets interpretation is different; also, getting the size returns
37f214d5
DW
307 // the _displayed_ size (NOT the drop down menu size) so
308 // setting-getting-setting size would not work.
0cf6acbf
DW
309 //
310 wxControl::DoSetSize( nX
311 ,nY
312 ,nWidth
6670f564 313 ,wxDefaultCoord
0cf6acbf
DW
314 ,nSizeFlags
315 );
316} // end of wxChoice::DoSetSize
37f214d5 317
e78c4d50 318wxSize wxChoice::DoGetBestSize() const
37f214d5 319{
0cf6acbf
DW
320 //
321 // Find the widest string
322 //
8228b893
WS
323 int nLineWidth;
324 int nChoiceWidth = 0;
325 int nCx;
326 int nCy;
327 wxFont vFont = (wxFont)GetFont();
328
aa61d352 329 const unsigned int nItems = GetCount();
0cf6acbf 330
aa61d352 331 for (unsigned int i = 0; i < nItems; i++)
8228b893
WS
332 {
333 wxString sStr(GetString(i));
334 GetTextExtent( sStr, &nLineWidth, NULL );
0cf6acbf
DW
335 if (nLineWidth > nChoiceWidth)
336 nChoiceWidth = nLineWidth;
37f214d5
DW
337 }
338
0cf6acbf
DW
339 //
340 // Give it some reasonable default value if there are no strings in the
37f214d5 341 // list
0cf6acbf
DW
342 //
343 if (nChoiceWidth == 0L)
344 nChoiceWidth = 100L;
345
346 //
347 // The combobox should be larger than the widest string
348 //
8228b893 349 wxGetCharSize( GetHWND(), &nCx, &nCy, &vFont );
0cf6acbf
DW
350 nChoiceWidth += 5 * nCx;
351
352 //
37f214d5 353 // Choice drop-down list depends on number of items (limited to 10)
0cf6acbf 354 //
aa61d352
VZ
355 size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1;
356 int nChoiceHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * nStrings;
0cf6acbf 357
aa61d352 358 return wxSize(nChoiceWidth, nChoiceHeight);
0cf6acbf
DW
359} // end of wxChoice::DoGetBestSize
360
361MRESULT wxChoice::OS2WindowProc(
362 WXUINT uMsg
363, WXWPARAM wParam
364, WXLPARAM lParam
365)
37f214d5 366{
0cf6acbf
DW
367 return wxWindow::OS2WindowProc( uMsg
368 ,wParam
369 ,lParam
370 );
371} // end of wxChoice::OS2WindowProc
372
373bool wxChoice::OS2Command(
374 WXUINT uParam
375, WXWORD WXUNUSED(wId)
376)
37f214d5 377{
0cf6acbf 378 if (uParam != LN_SELECT)
37f214d5 379 {
0cf6acbf 380 //
37f214d5 381 // "selection changed" is the only event we're after
0cf6acbf 382 //
312ebad4 383 return false;
37f214d5 384 }
0cf6acbf 385 int n = GetSelection();
37f214d5 386
0cf6acbf
DW
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);
0fba44b4 395 vEvent.SetString(GetStringSelection());
0cf6acbf
DW
396 if (HasClientObjectData())
397 vEvent.SetClientObject(GetClientObject(n));
398 else if (HasClientUntypedData())
399 vEvent.SetClientData(GetClientData(n));
400 ProcessCommand(vEvent);
401 }
312ebad4 402 return true;
0cf6acbf 403} // end of wxChoice::OS2Command
0e320a79 404
0cf6acbf
DW
405void wxChoice::Free()
406{
407 if (HasClientObjectData())
408 {
aa61d352 409 const unsigned int nCount = GetCount();
0cf6acbf 410
aa61d352 411 for (unsigned int n = 0; n < nCount; n++)
0cf6acbf
DW
412 {
413 delete GetClientObject(n);
414 }
415 }
312ebad4
WS
416} // end of wxChoice::Free
417
418#endif // wxUSE_CHOICE