Lots of fixes for scrolling
[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 wxFAIL_MSG(wxT("not implemented"));
192
193 #if 0 // should do this, but no Insert() so far
194 Delete(n);
195 Insert(n + 1, s);
196 #endif
197 } // end of wxChoice::SetString
198
199 wxString wxChoice::GetString(
200 int n
201 ) const
202 {
203 size_t nLen = 0;
204 wxString sStr = "";
205 char* zBuf;
206
207 nLen = (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)n, (MPARAM)0));
208 if (nLen)
209 {
210 zBuf = new char[nLen + 1];
211 ::WinSendMsg( GetHwnd()
212 ,LM_QUERYITEMTEXT
213 ,MPFROM2SHORT((SHORT)n, (SHORT)nLen)
214 ,(MPARAM)zBuf
215 );
216 sStr = zBuf;
217 delete [] zBuf;
218 }
219 return sStr;
220 } // end of wxChoice::GetString
221
222 // ----------------------------------------------------------------------------
223 // client data
224 // ----------------------------------------------------------------------------
225
226 void wxChoice::DoSetItemClientData(
227 int n
228 , void* pClientData
229 )
230 {
231 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, (MPARAM)n, MPFROMP(pClientData));
232 } // end of wxChoice::DoSetItemClientData
233
234 void* wxChoice::DoGetItemClientData( int n ) const
235 {
236 MRESULT rc = 0L;
237
238 rc = ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE, (MPARAM)n, (MPARAM)0);
239 return((void*)rc);
240 } // end of wxChoice::DoSetItemClientData
241
242 void wxChoice::DoSetItemClientObject(
243 int n
244 , wxClientData* pClientData
245 )
246 {
247 DoSetItemClientData( n
248 ,pClientData
249 );
250 } // end of wxChoice::DoSetItemClientObject
251
252 wxClientData* wxChoice::DoGetItemClientObject(
253 int n
254 ) const
255 {
256 return (wxClientData *)DoGetItemClientData(n);
257 } // end of wxChoice::DoGetItemClientObject
258
259 // ----------------------------------------------------------------------------
260 // wxOS2 specific helpers
261 // ----------------------------------------------------------------------------
262
263 void wxChoice::DoSetSize(
264 int nX
265 , int nY
266 , int nWidth
267 , int nHeight
268 , int nSizeFlags
269 )
270 {
271 //
272 // Ignore height parameter because height doesn't mean 'initially
273 // displayed' height, it refers to the drop-down menu as well. The
274 // wxWindows interpretation is different; also, getting the size returns
275 // the _displayed_ size (NOT the drop down menu size) so
276 // setting-getting-setting size would not work.
277 //
278 wxControl::DoSetSize( nX
279 ,nY
280 ,nWidth
281 ,-1
282 ,nSizeFlags
283 );
284 } // end of wxChoice::DoSetSize
285
286 wxSize wxChoice::DoGetBestSize() const
287 {
288 //
289 // Find the widest string
290 //
291 int nLineWidth;
292 int nChoiceWidth = 0;
293 int nItems = GetCount();
294 int nCx;
295 int nCy;
296
297 for (int i = 0; i < nItems; i++)
298 {
299 wxString sStr(GetString(i));
300
301 GetTextExtent( sStr
302 ,&nLineWidth
303 ,NULL
304 );
305 if (nLineWidth > nChoiceWidth)
306 nChoiceWidth = nLineWidth;
307 }
308
309 //
310 // Give it some reasonable default value if there are no strings in the
311 // list
312 //
313 if (nChoiceWidth == 0L)
314 nChoiceWidth = 100L;
315
316 //
317 // The combobox should be larger than the widest string
318 //
319 wxGetCharSize( GetHWND()
320 ,&nCx
321 ,&nCy
322 ,(wxFont*)&GetFont()
323 );
324 nChoiceWidth += 5 * nCx;
325
326 //
327 // Choice drop-down list depends on number of items (limited to 10)
328 //
329 size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1;
330 int nChoiceHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * nStrings;
331
332 return wxSize( nChoiceWidth
333 ,nChoiceHeight
334 );
335 } // end of wxChoice::DoGetBestSize
336
337 MRESULT wxChoice::OS2WindowProc(
338 WXUINT uMsg
339 , WXWPARAM wParam
340 , WXLPARAM lParam
341 )
342 {
343 return wxWindow::OS2WindowProc( uMsg
344 ,wParam
345 ,lParam
346 );
347 } // end of wxChoice::OS2WindowProc
348
349 bool wxChoice::OS2Command(
350 WXUINT uParam
351 , WXWORD WXUNUSED(wId)
352 )
353 {
354 if (uParam != LN_SELECT)
355 {
356 //
357 // "selection changed" is the only event we're after
358 //
359 return FALSE;
360 }
361 int n = GetSelection();
362
363 if (n > -1)
364 {
365 wxCommandEvent vEvent( wxEVT_COMMAND_CHOICE_SELECTED
366 ,m_windowId
367 );
368
369 vEvent.SetInt(n);
370 vEvent.SetEventObject(this);
371 vEvent.SetString((char*)GetStringSelection().c_str());
372 if (HasClientObjectData())
373 vEvent.SetClientObject(GetClientObject(n));
374 else if (HasClientUntypedData())
375 vEvent.SetClientData(GetClientData(n));
376 ProcessCommand(vEvent);
377 }
378 return TRUE;
379 } // end of wxChoice::OS2Command
380
381 void wxChoice::Free()
382 {
383 if (HasClientObjectData())
384 {
385 size_t nCount = GetCount();
386
387 for (size_t n = 0; n < nCount; n++)
388 {
389 delete GetClientObject(n);
390 }
391 }
392 } // end of wxhoice::Free