]> git.saurik.com Git - wxWidgets.git/blob - src/os2/choice.cpp
Restore stock item labels mistakenly removed by r68641.
[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 bool wxChoice::Create(
28 wxWindow* pParent
29 , wxWindowID vId
30 , const wxPoint& rPos
31 , const wxSize& rSize
32 , const wxArrayString& asChoices
33 , long lStyle
34 , const wxValidator& rValidator
35 , const wxString& rsName
36 )
37 {
38 wxCArrayString chs(asChoices);
39
40 return Create(pParent, vId, rPos, rSize, chs.GetCount(), chs.GetStrings(),
41 lStyle, rValidator, rsName);
42 }
43
44 bool wxChoice::Create(
45 wxWindow* pParent
46 , wxWindowID vId
47 , const wxPoint& rPos
48 , const wxSize& rSize
49 , int n
50 , const wxString asChoices[]
51 , long lStyle
52 , const wxValidator& rValidator
53 , const wxString& rsName
54 )
55 {
56 long lSstyle;
57
58 if (!CreateControl( pParent
59 ,vId
60 ,rPos
61 ,rSize
62 ,lStyle
63 ,rValidator
64 ,rsName
65 ))
66 return false;
67 lSstyle = CBS_DROPDOWNLIST |
68 WS_TABSTOP |
69 WS_VISIBLE;
70
71 // clipping siblings does not yet work
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
92 // initialize the controls contents
93 for (int i = 0; i < n; i++)
94 {
95 Append(asChoices[i]);
96 }
97 SetSize( rPos.x
98 ,rPos.y
99 ,rSize.x
100 ,rSize.y
101 );
102
103 // Set height to use with sizers i.e. without the dropdown listbox
104 wxFont vFont = GetFont();
105 int nEditHeight;
106 wxGetCharSize( GetHWND(), NULL, &nEditHeight, &vFont );
107 nEditHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nEditHeight);
108 SetInitialSize(wxSize(-1,nEditHeight+4)); // +2x2 for the border
109
110 return true;
111 } // end of wxChoice::Create
112
113 wxChoice::~wxChoice()
114 {
115 Clear();
116 }
117
118 // ----------------------------------------------------------------------------
119 // adding/deleting items to/from the list
120 // ----------------------------------------------------------------------------
121
122 int wxChoice::DoInsertItems(const wxArrayStringsAdapter& items
123 , unsigned int pos
124 , void **clientData
125 , wxClientDataType type
126 )
127 {
128 int nIndex = wxNOT_FOUND;
129 LONG nIndexType = 0;
130 bool incrementPos = false;
131 if ( IsSorted() )
132 nIndexType = LIT_SORTASCENDING;
133 else if (pos == GetCount())
134 nIndexType = LIT_END;
135 else
136 {
137 nIndexType = pos;
138 incrementPos = true;
139 }
140
141 const unsigned int count = items.GetCount();
142 for( unsigned int i = 0; i < count; ++i )
143 {
144 nIndex = (int)::WinSendMsg( GetHwnd()
145 ,LM_INSERTITEM
146 ,(MPARAM)nIndexType
147 ,(MPARAM)items[i].wx_str()
148 );
149 if (nIndex < 0)
150 {
151 nIndex = wxNOT_FOUND;
152 break;
153 }
154 AssignNewItemClientData(nIndex, clientData, i, type);
155
156 if (incrementPos)
157 ++nIndexType;
158 }
159 return nIndex;
160 } // end of wxChoice::DoInsertAppendItemsWithData
161
162 void wxChoice::DoDeleteOneItem(unsigned int n)
163 {
164 wxCHECK_RET( IsValid(n), wxT("invalid item index in wxChoice::Delete") );
165
166 ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)n, (MPARAM)0);
167 } // end of wxChoice::Delete
168
169 void wxChoice::DoClear()
170 {
171 ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0);
172 } // end of wxChoice::Clear
173
174 // ----------------------------------------------------------------------------
175 // selection
176 // ----------------------------------------------------------------------------
177
178 int wxChoice::GetSelection() const
179 {
180 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)LIT_FIRST, (MPARAM)0)));
181 } // end of wxChoice::GetSelection
182
183 void wxChoice::SetSelection(
184 int n
185 )
186 {
187 ::WinSendMsg( GetHwnd()
188 ,LM_SELECTITEM
189 ,(MPARAM)n
190 ,(MPARAM)TRUE
191 );
192 } // end of wxChoice::SetSelection
193
194 // ----------------------------------------------------------------------------
195 // string list functions
196 // ----------------------------------------------------------------------------
197
198 unsigned int wxChoice::GetCount() const
199 {
200 return((unsigned int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT, (MPARAM)0, (MPARAM)0)));
201 } // end of wxChoice::GetCount
202
203 void wxChoice::SetString(unsigned int n, const wxString& rsStr)
204 {
205 LONG nIndexType = 0;
206 void* pData;
207
208 if ( HasClientData() )
209 {
210 pData = DoGetItemClientData(n);
211 }
212 else // no client data
213 {
214 pData = NULL;
215 }
216
217 ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)n, 0);
218
219 if (m_windowStyle & wxCB_SORT)
220 nIndexType = LIT_SORTASCENDING;
221 else
222 nIndexType = LIT_END;
223 ::WinSendMsg( GetHwnd()
224 ,LM_INSERTITEM
225 ,(MPARAM)nIndexType
226 ,(MPARAM)rsStr.wx_str()
227 );
228
229 if (pData)
230 {
231 DoSetItemClientData(n, pData);
232 }
233 } // end of wxChoice::SetString
234
235 wxString wxChoice::GetString(unsigned int n) const
236 {
237 int nLen = 0;
238 wxString sStr = wxEmptyString;
239 wxChar* zBuf;
240
241 nLen = (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)n, (MPARAM)0));
242 if (nLen != LIT_ERROR && nLen > 0)
243 {
244 zBuf = new wxChar[++nLen];
245 ::WinSendMsg( GetHwnd()
246 ,LM_QUERYITEMTEXT
247 ,MPFROM2SHORT((SHORT)n, (SHORT)nLen)
248 ,(MPARAM)zBuf
249 );
250 sStr = zBuf;
251 delete [] zBuf;
252 }
253 return sStr;
254 } // end of wxChoice::GetString
255
256 // ----------------------------------------------------------------------------
257 // client data
258 // ----------------------------------------------------------------------------
259
260 void wxChoice::DoSetItemClientData(unsigned int n, void* pClientData)
261 {
262 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, (MPARAM)n, MPFROMP(pClientData));
263 } // end of wxChoice::DoSetItemClientData
264
265 void* wxChoice::DoGetItemClientData(unsigned int n) const
266 {
267 MRESULT rc = ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE, (MPARAM)n, (MPARAM)0);
268 return((void*)rc);
269 } // end of wxChoice::DoGetItemClientData
270
271 // ----------------------------------------------------------------------------
272 // wxOS2 specific helpers
273 // ----------------------------------------------------------------------------
274
275 void wxChoice::DoSetSize(int nX,
276 int nY,
277 int nWidth,
278 int WXUNUSED(nHeight),
279 int nSizeFlags)
280 {
281 //
282 // Ignore height parameter because height doesn't mean 'initially
283 // displayed' height, it refers to the drop-down menu as well. The
284 // wxWidgets interpretation is different; also, getting the size returns
285 // the _displayed_ size (NOT the drop down menu size) so
286 // setting-getting-setting size would not work.
287 //
288 wxControl::DoSetSize( nX
289 ,nY
290 ,nWidth
291 ,wxDefaultCoord
292 ,nSizeFlags
293 );
294 } // end of wxChoice::DoSetSize
295
296 wxSize wxChoice::DoGetBestSize() const
297 {
298 //
299 // Find the widest string
300 //
301 int nLineWidth;
302 int nChoiceWidth = 0;
303 int nCx;
304 int nCy;
305 wxFont vFont = (wxFont)GetFont();
306
307 const unsigned int nItems = GetCount();
308
309 for (unsigned int i = 0; i < nItems; i++)
310 {
311 wxString sStr(GetString(i));
312 GetTextExtent( sStr, &nLineWidth, NULL );
313 if (nLineWidth > nChoiceWidth)
314 nChoiceWidth = nLineWidth;
315 }
316
317 //
318 // Give it some reasonable default value if there are no strings in the
319 // list
320 //
321 if (nChoiceWidth == 0L)
322 nChoiceWidth = 100L;
323
324 //
325 // The combobox should be larger than the widest string
326 //
327 wxGetCharSize( GetHWND(), &nCx, &nCy, &vFont );
328 nChoiceWidth += 5 * nCx;
329
330 //
331 // Choice drop-down list depends on number of items (limited to 10)
332 //
333 size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1;
334 int nChoiceHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * nStrings;
335
336 return wxSize(nChoiceWidth, nChoiceHeight);
337 } // end of wxChoice::DoGetBestSize
338
339 MRESULT wxChoice::OS2WindowProc(
340 WXUINT uMsg
341 , WXWPARAM wParam
342 , WXLPARAM lParam
343 )
344 {
345 return wxWindow::OS2WindowProc( uMsg
346 ,wParam
347 ,lParam
348 );
349 } // end of wxChoice::OS2WindowProc
350
351 bool wxChoice::OS2Command(
352 WXUINT uParam
353 , WXWORD WXUNUSED(wId)
354 )
355 {
356 if (uParam != LN_SELECT)
357 {
358 //
359 // "selection changed" is the only event we're after
360 //
361 return false;
362 }
363 int n = GetSelection();
364
365 if (n > -1)
366 {
367 wxCommandEvent vEvent( wxEVT_COMMAND_CHOICE_SELECTED
368 ,m_windowId
369 );
370
371 vEvent.SetInt(n);
372 vEvent.SetEventObject(this);
373 vEvent.SetString(GetStringSelection());
374 if (HasClientObjectData())
375 vEvent.SetClientObject(GetClientObject(n));
376 else if (HasClientUntypedData())
377 vEvent.SetClientData(GetClientData(n));
378 ProcessCommand(vEvent);
379 }
380 return true;
381 } // end of wxChoice::OS2Command
382
383 #endif // wxUSE_CHOICE