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