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