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