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