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