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