1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/choice.cpp
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
20 #include "wx/choice.h"
23 #include "wx/settings.h"
26 #include "wx/os2/private.h"
28 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
30 bool wxChoice::Create(
35 , const wxArrayString
& asChoices
37 , const wxValidator
& rValidator
38 , const wxString
& rsName
41 wxCArrayString
chs(asChoices
);
43 return Create(pParent
, vId
, rPos
, rSize
, chs
.GetCount(), chs
.GetStrings(),
44 lStyle
, rValidator
, rsName
);
47 bool wxChoice::Create(
53 , const wxString asChoices
[]
55 , const wxValidator
& rValidator
56 , const wxString
& rsName
61 if (!CreateControl( pParent
70 lSstyle
= CBS_DROPDOWNLIST
|
74 if (lStyle
& wxCLIP_SIBLINGS
)
75 lSstyle
|= WS_CLIPSIBLINGS
;
77 wxASSERT_MSG( !(lStyle
& wxCB_DROPDOWN
) &&
78 !(lStyle
& wxCB_READONLY
) &&
79 !(lStyle
& wxCB_SIMPLE
),
80 wxT("this style flag is ignored by wxChoice, you "
81 "probably want to use a wxComboBox") );
83 if (!OS2CreateControl( wxT("COMBOBOX")
89 // A choice/combobox normally has a white background (or other, depending
90 // on global settings) rather than inheriting the parent's background colour.
92 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
93 for (int i
= 0; i
< n
; i
++)
97 wxFont
* pTextFont
= new wxFont( 10
110 } // end of wxChoice::Create
112 // ----------------------------------------------------------------------------
113 // adding/deleting items to/from the list
114 // ----------------------------------------------------------------------------
116 int wxChoice::DoAppend(
117 const wxString
& rsItem
123 if (m_windowStyle
& wxLB_SORT
)
124 nIndexType
= LIT_SORTASCENDING
;
126 nIndexType
= LIT_END
;
127 nIndex
= (int)::WinSendMsg( GetHwnd()
130 ,(MPARAM
)rsItem
.c_str()
133 } // end of wxChoice::DoAppend
135 int wxChoice::DoInsert(
136 const wxString
& rsItem
,
140 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
141 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
143 if (pos
== GetCount())
144 return DoAppend(rsItem
);
149 if (m_windowStyle
& wxLB_SORT
)
150 nIndexType
= LIT_SORTASCENDING
;
153 nIndex
= (int)::WinSendMsg( GetHwnd()
156 ,(MPARAM
)rsItem
.c_str()
159 } // end of wxChoice::DoInsert
161 void wxChoice::Delete(
165 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
166 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, (MPARAM
)0);
167 } // end of wxChoice::Delete
169 void wxChoice::Clear()
172 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
173 } // end of wxChoice::Clear
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 int wxChoice::GetSelection() const
181 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0)));
182 } // end of wxChoice::GetSelection
184 void wxChoice::SetSelection(
188 ::WinSendMsg( GetHwnd()
193 } // end of wxChoice::SetSelection
195 // ----------------------------------------------------------------------------
196 // string list functions
197 // ----------------------------------------------------------------------------
199 int wxChoice::GetCount() const
201 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT
, (MPARAM
)0, (MPARAM
)0)));
202 } // end of wxChoice::GetCount
204 void wxChoice::SetString( int n
, const wxString
& rsStr
)
209 if ( m_clientDataItemsType
!= wxClientData_None
)
211 pData
= DoGetItemClientData(n
);
213 else // no client data
218 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, 0);
220 if (m_windowStyle
& wxLB_SORT
)
221 nIndexType
= LIT_SORTASCENDING
;
223 nIndexType
= LIT_END
;
224 ::WinSendMsg( GetHwnd()
227 ,(MPARAM
)rsStr
.c_str()
232 DoSetItemClientData( n
236 } // end of wxChoice::SetString
238 wxString
wxChoice::GetString(int n
) const
241 wxString sStr
= wxEmptyString
;
244 nLen
= (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)n
, (MPARAM
)0));
245 if (nLen
!= LIT_ERROR
&& nLen
> 0)
247 zBuf
= new wxChar
[nLen
+ 1];
248 ::WinSendMsg( GetHwnd()
250 ,MPFROM2SHORT((SHORT
)n
, (SHORT
)nLen
)
257 } // end of wxChoice::GetString
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
263 void wxChoice::DoSetItemClientData(
268 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)n
, MPFROMP(pClientData
));
269 } // end of wxChoice::DoSetItemClientData
271 void* wxChoice::DoGetItemClientData( int n
) const
275 rc
= ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, (MPARAM
)n
, (MPARAM
)0);
277 } // end of wxChoice::DoSetItemClientData
279 void wxChoice::DoSetItemClientObject(
281 , wxClientData
* pClientData
284 DoSetItemClientData( n
287 } // end of wxChoice::DoSetItemClientObject
289 wxClientData
* wxChoice::DoGetItemClientObject(
293 return (wxClientData
*)DoGetItemClientData(n
);
294 } // end of wxChoice::DoGetItemClientObject
296 // ----------------------------------------------------------------------------
297 // wxOS2 specific helpers
298 // ----------------------------------------------------------------------------
300 void wxChoice::DoSetSize(int nX
,
303 int WXUNUSED(nHeight
),
307 // Ignore height parameter because height doesn't mean 'initially
308 // displayed' height, it refers to the drop-down menu as well. The
309 // wxWidgets interpretation is different; also, getting the size returns
310 // the _displayed_ size (NOT the drop down menu size) so
311 // setting-getting-setting size would not work.
313 wxControl::DoSetSize( nX
319 } // end of wxChoice::DoSetSize
321 wxSize
wxChoice::DoGetBestSize() const
324 // Find the widest string
327 int nChoiceWidth
= 0;
328 int nItems
= GetCount();
331 wxFont vFont
= (wxFont
)GetFont();
333 for (int i
= 0; i
< nItems
; i
++)
335 wxString
sStr(GetString(i
));
341 if (nLineWidth
> nChoiceWidth
)
342 nChoiceWidth
= nLineWidth
;
346 // Give it some reasonable default value if there are no strings in the
349 if (nChoiceWidth
== 0L)
353 // The combobox should be larger than the widest string
355 wxGetCharSize( GetHWND()
360 nChoiceWidth
+= 5 * nCx
;
363 // Choice drop-down list depends on number of items (limited to 10)
365 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
366 int nChoiceHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * nStrings
;
368 return wxSize( nChoiceWidth
371 } // end of wxChoice::DoGetBestSize
373 MRESULT
wxChoice::OS2WindowProc(
379 return wxWindow::OS2WindowProc( uMsg
383 } // end of wxChoice::OS2WindowProc
385 bool wxChoice::OS2Command(
387 , WXWORD
WXUNUSED(wId
)
390 if (uParam
!= LN_SELECT
)
393 // "selection changed" is the only event we're after
397 int n
= GetSelection();
401 wxCommandEvent
vEvent( wxEVT_COMMAND_CHOICE_SELECTED
406 vEvent
.SetEventObject(this);
407 vEvent
.SetString(GetStringSelection());
408 if (HasClientObjectData())
409 vEvent
.SetClientObject(GetClientObject(n
));
410 else if (HasClientUntypedData())
411 vEvent
.SetClientData(GetClientData(n
));
412 ProcessCommand(vEvent
);
415 } // end of wxChoice::OS2Command
417 void wxChoice::Free()
419 if (HasClientObjectData())
421 size_t nCount
= GetCount();
423 for (size_t n
= 0; n
< nCount
; n
++)
425 delete GetClientObject(n
);
428 } // end of wxChoice::Free
430 #endif // wxUSE_CHOICE