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"
18 #include "wx/choice.h"
21 #include "wx/settings.h"
24 #include "wx/os2/private.h"
26 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
28 bool wxChoice::Create(
33 , const wxArrayString
& asChoices
35 , const wxValidator
& rValidator
36 , const wxString
& rsName
39 wxCArrayString
chs(asChoices
);
41 return Create(pParent
, vId
, rPos
, rSize
, chs
.GetCount(), chs
.GetStrings(),
42 lStyle
, rValidator
, rsName
);
45 bool wxChoice::Create(
51 , const wxString asChoices
[]
53 , const wxValidator
& rValidator
54 , const wxString
& rsName
59 if (!CreateControl( pParent
68 lSstyle
= CBS_DROPDOWNLIST
|
72 if (lStyle
& wxCLIP_SIBLINGS
)
73 lSstyle
|= WS_CLIPSIBLINGS
;
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") );
81 if (!OS2CreateControl( wxT("COMBOBOX")
87 // A choice/combobox normally has a white background (or other, depending
88 // on global settings) rather than inheriting the parent's background colour.
90 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
91 for (int i
= 0; i
< n
; i
++)
101 } // end of wxChoice::Create
103 // ----------------------------------------------------------------------------
104 // adding/deleting items to/from the list
105 // ----------------------------------------------------------------------------
107 int wxChoice::DoAppend(
108 const wxString
& rsItem
114 if (m_windowStyle
& wxLB_SORT
)
115 nIndexType
= LIT_SORTASCENDING
;
117 nIndexType
= LIT_END
;
118 nIndex
= (int)::WinSendMsg( GetHwnd()
121 ,(MPARAM
)rsItem
.c_str()
124 } // end of wxChoice::DoAppend
126 int wxChoice::DoInsert( const wxString
& rsItem
, int pos
)
128 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
129 wxCHECK_MSG(IsValidInsert(pos
), -1, wxT("invalid index"));
131 if ((size_t)pos
== GetCount())
132 return DoAppend(rsItem
);
137 if (m_windowStyle
& wxLB_SORT
)
138 nIndexType
= LIT_SORTASCENDING
;
141 nIndex
= (int)::WinSendMsg( GetHwnd()
144 ,(MPARAM
)rsItem
.c_str()
147 } // end of wxChoice::DoInsert
149 void wxChoice::Delete( int n
)
151 wxCHECK_RET( IsValid(n
), wxT("invalid item index in wxChoice::Delete") );
152 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, (MPARAM
)0);
153 } // end of wxChoice::Delete
155 void wxChoice::Clear()
158 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
159 } // end of wxChoice::Clear
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 int wxChoice::GetSelection() const
167 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0)));
168 } // end of wxChoice::GetSelection
170 void wxChoice::SetSelection(
174 ::WinSendMsg( GetHwnd()
179 } // end of wxChoice::SetSelection
181 // ----------------------------------------------------------------------------
182 // string list functions
183 // ----------------------------------------------------------------------------
185 size_t wxChoice::GetCount() const
187 return((size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT
, (MPARAM
)0, (MPARAM
)0)));
188 } // end of wxChoice::GetCount
190 void wxChoice::SetString( int n
, const wxString
& rsStr
)
195 if ( m_clientDataItemsType
!= wxClientData_None
)
197 pData
= DoGetItemClientData(n
);
199 else // no client data
204 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, 0);
206 if (m_windowStyle
& wxLB_SORT
)
207 nIndexType
= LIT_SORTASCENDING
;
209 nIndexType
= LIT_END
;
210 ::WinSendMsg( GetHwnd()
213 ,(MPARAM
)rsStr
.c_str()
218 DoSetItemClientData( n
222 } // end of wxChoice::SetString
224 wxString
wxChoice::GetString(int n
) const
227 wxString sStr
= wxEmptyString
;
230 nLen
= (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)n
, (MPARAM
)0));
231 if (nLen
!= LIT_ERROR
&& nLen
> 0)
233 zBuf
= new wxChar
[nLen
+ 1];
234 ::WinSendMsg( GetHwnd()
236 ,MPFROM2SHORT((SHORT
)n
, (SHORT
)nLen
)
243 } // end of wxChoice::GetString
245 // ----------------------------------------------------------------------------
247 // ----------------------------------------------------------------------------
249 void wxChoice::DoSetItemClientData(
254 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)n
, MPFROMP(pClientData
));
255 } // end of wxChoice::DoSetItemClientData
257 void* wxChoice::DoGetItemClientData( int n
) const
261 rc
= ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, (MPARAM
)n
, (MPARAM
)0);
263 } // end of wxChoice::DoSetItemClientData
265 void wxChoice::DoSetItemClientObject(
267 , wxClientData
* pClientData
270 DoSetItemClientData( n
273 } // end of wxChoice::DoSetItemClientObject
275 wxClientData
* wxChoice::DoGetItemClientObject(
279 return (wxClientData
*)DoGetItemClientData(n
);
280 } // end of wxChoice::DoGetItemClientObject
282 // ----------------------------------------------------------------------------
283 // wxOS2 specific helpers
284 // ----------------------------------------------------------------------------
286 void wxChoice::DoSetSize(int nX
,
289 int WXUNUSED(nHeight
),
293 // Ignore height parameter because height doesn't mean 'initially
294 // displayed' height, it refers to the drop-down menu as well. The
295 // wxWidgets interpretation is different; also, getting the size returns
296 // the _displayed_ size (NOT the drop down menu size) so
297 // setting-getting-setting size would not work.
299 wxControl::DoSetSize( nX
305 } // end of wxChoice::DoSetSize
307 wxSize
wxChoice::DoGetBestSize() const
310 // Find the widest string
313 int nChoiceWidth
= 0;
316 wxFont vFont
= (wxFont
)GetFont();
318 const size_t nItems
= GetCount();
320 for (size_t i
= 0; i
< nItems
; i
++)
322 wxString
sStr(GetString(i
));
323 GetTextExtent( sStr
, &nLineWidth
, NULL
);
324 if (nLineWidth
> nChoiceWidth
)
325 nChoiceWidth
= nLineWidth
;
329 // Give it some reasonable default value if there are no strings in the
332 if (nChoiceWidth
== 0L)
336 // The combobox should be larger than the widest string
338 wxGetCharSize( GetHWND(), &nCx
, &nCy
, &vFont
);
339 nChoiceWidth
+= 5 * nCx
;
342 // Choice drop-down list depends on number of items (limited to 10)
344 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
345 int nChoiceHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * nStrings
;
347 return wxSize( nChoiceWidth
350 } // end of wxChoice::DoGetBestSize
352 MRESULT
wxChoice::OS2WindowProc(
358 return wxWindow::OS2WindowProc( uMsg
362 } // end of wxChoice::OS2WindowProc
364 bool wxChoice::OS2Command(
366 , WXWORD
WXUNUSED(wId
)
369 if (uParam
!= LN_SELECT
)
372 // "selection changed" is the only event we're after
376 int n
= GetSelection();
380 wxCommandEvent
vEvent( wxEVT_COMMAND_CHOICE_SELECTED
385 vEvent
.SetEventObject(this);
386 vEvent
.SetString(GetStringSelection());
387 if (HasClientObjectData())
388 vEvent
.SetClientObject(GetClientObject(n
));
389 else if (HasClientUntypedData())
390 vEvent
.SetClientData(GetClientData(n
));
391 ProcessCommand(vEvent
);
394 } // end of wxChoice::OS2Command
396 void wxChoice::Free()
398 if (HasClientObjectData())
400 const size_t nCount
= GetCount();
402 for (size_t n
= 0; n
< nCount
; n
++)
404 delete GetClientObject(n
);
407 } // end of wxChoice::Free
409 #endif // wxUSE_CHOICE