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