1 /////////////////////////////////////////////////////////////////////////////
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"
16 #include "wx/choice.h"
19 #include "wx/settings.h"
22 #include "wx/os2/private.h"
24 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
26 bool wxChoice::Create(
32 , const wxString asChoices
[]
34 , const wxValidator
& rValidator
35 , const wxString
& rsName
40 if (!CreateControl( pParent
49 lSstyle
= CBS_DROPDOWNLIST
|
53 if (lStyle
& wxCLIP_SIBLINGS
)
54 lSstyle
|= WS_CLIPSIBLINGS
;
56 wxASSERT_MSG( !(lStyle
& wxCB_DROPDOWN
) &&
57 !(lStyle
& wxCB_READONLY
) &&
58 !(lStyle
& wxCB_SIMPLE
),
59 wxT("this style flag is ignored by wxChoice, you "
60 "probably want to use a wxComboBox") );
62 if (!OS2CreateControl( wxT("COMBOBOX")
68 // A choice/combobox normally has a white background (or other, depending
69 // on global settings) rather than inheriting the parent's background colour.
71 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
72 for (int i
= 0; i
< n
; i
++)
76 wxFont
* pTextFont
= new wxFont( 10
89 } // end of wxChoice::Create
91 // ----------------------------------------------------------------------------
92 // adding/deleting items to/from the list
93 // ----------------------------------------------------------------------------
95 int wxChoice::DoAppend(
96 const wxString
& rsItem
100 SHORT nIndexType
= 0;
102 if (m_windowStyle
& wxLB_SORT
)
103 nIndexType
= LIT_SORTASCENDING
;
105 nIndexType
= LIT_END
;
106 nIndex
= (int)::WinSendMsg( GetHwnd()
109 ,(MPARAM
)rsItem
.c_str()
112 } // end of wxChoice::DoAppend
114 int wxChoice::DoInsert(
115 const wxString
& rsItem
,
119 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
120 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
122 if (pos
== GetCount())
123 return DoAppend(rsItem
);
126 SHORT nIndexType
= 0;
128 if (m_windowStyle
& wxLB_SORT
)
129 nIndexType
= LIT_SORTASCENDING
;
132 nIndex
= (int)::WinSendMsg( GetHwnd()
135 ,(MPARAM
)rsItem
.c_str()
138 } // end of wxChoice::DoInsert
140 void wxChoice::Delete(
144 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
145 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, (MPARAM
)0);
146 } // end of wxChoice::Delete
148 void wxChoice::Clear()
151 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
152 } // end of wxChoice::Clear
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 int wxChoice::GetSelection() const
160 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0)));
161 } // end of wxChoice::GetSelection
163 void wxChoice::SetSelection(
167 ::WinSendMsg( GetHwnd()
172 } // end of wxChoice::SetSelection
174 // ----------------------------------------------------------------------------
175 // string list functions
176 // ----------------------------------------------------------------------------
178 int wxChoice::GetCount() const
180 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT
, (MPARAM
)0, (MPARAM
)0)));
181 } // end of wxChoice::GetCount
183 int wxChoice::FindString(
184 const wxString
& rsStr
192 nItemCount
= (int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT
, (MPARAM
)0, (MPARAM
)0));
193 for (nPos
= 0; nPos
< nItemCount
; nPos
++)
195 nTextLength
= (int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)nPos
, (MPARAM
)0));
196 zStr
= new char[nTextLength
+ 1];
197 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT
, MPFROM2SHORT((SHORT
)nPos
, (SHORT
)nTextLength
), (MPARAM
)zStr
);
198 if (rsStr
== (char*)zStr
)
206 } // end of wxChoice::FindString
208 void wxChoice::SetString(
210 , const wxString
& rsStr
213 SHORT nIndexType
= 0;
216 if ( m_clientDataItemsType
!= wxClientData_None
)
218 pData
= DoGetItemClientData(n
);
220 else // no client data
225 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, 0);
227 if (m_windowStyle
& wxLB_SORT
)
228 nIndexType
= LIT_SORTASCENDING
;
230 nIndexType
= LIT_END
;
231 ::WinSendMsg( GetHwnd()
234 ,(MPARAM
)rsStr
.c_str()
239 DoSetItemClientData( n
243 } // end of wxChoice::SetString
245 wxString
wxChoice::GetString(
253 nLen
= (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)n
, (MPARAM
)0));
254 if (nLen
!= LIT_ERROR
&& nLen
> 0)
256 zBuf
= new char[nLen
+ 1];
257 ::WinSendMsg( GetHwnd()
259 ,MPFROM2SHORT((SHORT
)n
, (SHORT
)nLen
)
266 } // end of wxChoice::GetString
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 void wxChoice::DoSetItemClientData(
277 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)n
, MPFROMP(pClientData
));
278 } // end of wxChoice::DoSetItemClientData
280 void* wxChoice::DoGetItemClientData( int n
) const
284 rc
= ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, (MPARAM
)n
, (MPARAM
)0);
286 } // end of wxChoice::DoSetItemClientData
288 void wxChoice::DoSetItemClientObject(
290 , wxClientData
* pClientData
293 DoSetItemClientData( n
296 } // end of wxChoice::DoSetItemClientObject
298 wxClientData
* wxChoice::DoGetItemClientObject(
302 return (wxClientData
*)DoGetItemClientData(n
);
303 } // end of wxChoice::DoGetItemClientObject
305 // ----------------------------------------------------------------------------
306 // wxOS2 specific helpers
307 // ----------------------------------------------------------------------------
309 void wxChoice::DoSetSize(
318 // Ignore height parameter because height doesn't mean 'initially
319 // displayed' height, it refers to the drop-down menu as well. The
320 // wxWindows interpretation is different; also, getting the size returns
321 // the _displayed_ size (NOT the drop down menu size) so
322 // setting-getting-setting size would not work.
324 wxControl::DoSetSize( nX
330 } // end of wxChoice::DoSetSize
332 wxSize
wxChoice::DoGetBestSize() const
335 // Find the widest string
338 int nChoiceWidth
= 0;
339 int nItems
= GetCount();
343 for (int i
= 0; i
< nItems
; i
++)
345 wxString
sStr(GetString(i
));
351 if (nLineWidth
> nChoiceWidth
)
352 nChoiceWidth
= nLineWidth
;
356 // Give it some reasonable default value if there are no strings in the
359 if (nChoiceWidth
== 0L)
363 // The combobox should be larger than the widest string
365 wxGetCharSize( GetHWND()
370 nChoiceWidth
+= 5 * nCx
;
373 // Choice drop-down list depends on number of items (limited to 10)
375 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
376 int nChoiceHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * nStrings
;
378 return wxSize( nChoiceWidth
381 } // end of wxChoice::DoGetBestSize
383 MRESULT
wxChoice::OS2WindowProc(
389 return wxWindow::OS2WindowProc( uMsg
393 } // end of wxChoice::OS2WindowProc
395 bool wxChoice::OS2Command(
397 , WXWORD
WXUNUSED(wId
)
400 if (uParam
!= LN_SELECT
)
403 // "selection changed" is the only event we're after
407 int n
= GetSelection();
411 wxCommandEvent
vEvent( wxEVT_COMMAND_CHOICE_SELECTED
416 vEvent
.SetEventObject(this);
417 vEvent
.SetString((char*)GetStringSelection().c_str());
418 if (HasClientObjectData())
419 vEvent
.SetClientObject(GetClientObject(n
));
420 else if (HasClientUntypedData())
421 vEvent
.SetClientData(GetClientData(n
));
422 ProcessCommand(vEvent
);
425 } // end of wxChoice::OS2Command
427 void wxChoice::Free()
429 if (HasClientObjectData())
431 size_t nCount
= GetCount();
433 for (size_t n
= 0; n
< nCount
; n
++)
435 delete GetClientObject(n
);
438 } // end of wxhoice::Free