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