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