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 void wxChoice::Delete(
118 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
119 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, (MPARAM
)0);
120 } // end of wxChoice::Delete
122 void wxChoice::Clear()
125 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
126 } // end of wxChoice::Clear
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
132 int wxChoice::GetSelection() const
134 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0)));
135 } // end of wxChoice::GetSelection
137 void wxChoice::SetSelection(
141 ::WinSendMsg( GetHwnd()
146 } // end of wxChoice::SetSelection
148 // ----------------------------------------------------------------------------
149 // string list functions
150 // ----------------------------------------------------------------------------
152 int wxChoice::GetCount() const
154 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT
, (MPARAM
)0, (MPARAM
)0)));
155 } // end of wxChoice::GetCount
157 int wxChoice::FindString(
158 const wxString
& rsStr
166 nItemCount
= (int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT
, (MPARAM
)0, (MPARAM
)0));
167 for (nPos
= 0; nPos
< nItemCount
; nPos
++)
169 nTextLength
= (int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)nPos
, (MPARAM
)0));
170 zStr
= new char[nTextLength
+ 1];
171 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT
, MPFROM2SHORT((SHORT
)nPos
, (SHORT
)nTextLength
), (MPARAM
)zStr
);
172 if (rsStr
== (char*)zStr
)
180 } // end of wxChoice::FindString
182 void wxChoice::SetString(
184 , const wxString
& rsStr
187 SHORT nIndexType
= 0;
189 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, 0);
191 if (m_windowStyle
& wxLB_SORT
)
192 nIndexType
= LIT_SORTASCENDING
;
194 nIndexType
= LIT_END
;
195 ::WinSendMsg( GetHwnd()
198 ,(MPARAM
)rsStr
.c_str()
200 } // end of wxChoice::SetString
202 wxString
wxChoice::GetString(
210 nLen
= (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)n
, (MPARAM
)0));
211 if (nLen
!= LIT_ERROR
&& nLen
> 0)
213 zBuf
= new char[nLen
+ 1];
214 ::WinSendMsg( GetHwnd()
216 ,MPFROM2SHORT((SHORT
)n
, (SHORT
)nLen
)
223 } // end of wxChoice::GetString
225 // ----------------------------------------------------------------------------
227 // ----------------------------------------------------------------------------
229 void wxChoice::DoSetItemClientData(
234 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)n
, MPFROMP(pClientData
));
235 } // end of wxChoice::DoSetItemClientData
237 void* wxChoice::DoGetItemClientData( int n
) const
241 rc
= ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, (MPARAM
)n
, (MPARAM
)0);
243 } // end of wxChoice::DoSetItemClientData
245 void wxChoice::DoSetItemClientObject(
247 , wxClientData
* pClientData
250 DoSetItemClientData( n
253 } // end of wxChoice::DoSetItemClientObject
255 wxClientData
* wxChoice::DoGetItemClientObject(
259 return (wxClientData
*)DoGetItemClientData(n
);
260 } // end of wxChoice::DoGetItemClientObject
262 // ----------------------------------------------------------------------------
263 // wxOS2 specific helpers
264 // ----------------------------------------------------------------------------
266 void wxChoice::DoSetSize(
275 // Ignore height parameter because height doesn't mean 'initially
276 // displayed' height, it refers to the drop-down menu as well. The
277 // wxWindows interpretation is different; also, getting the size returns
278 // the _displayed_ size (NOT the drop down menu size) so
279 // setting-getting-setting size would not work.
281 wxControl::DoSetSize( nX
287 } // end of wxChoice::DoSetSize
289 wxSize
wxChoice::DoGetBestSize() const
292 // Find the widest string
295 int nChoiceWidth
= 0;
296 int nItems
= GetCount();
300 for (int i
= 0; i
< nItems
; i
++)
302 wxString
sStr(GetString(i
));
308 if (nLineWidth
> nChoiceWidth
)
309 nChoiceWidth
= nLineWidth
;
313 // Give it some reasonable default value if there are no strings in the
316 if (nChoiceWidth
== 0L)
320 // The combobox should be larger than the widest string
322 wxGetCharSize( GetHWND()
327 nChoiceWidth
+= 5 * nCx
;
330 // Choice drop-down list depends on number of items (limited to 10)
332 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
333 int nChoiceHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * nStrings
;
335 return wxSize( nChoiceWidth
338 } // end of wxChoice::DoGetBestSize
340 MRESULT
wxChoice::OS2WindowProc(
346 return wxWindow::OS2WindowProc( uMsg
350 } // end of wxChoice::OS2WindowProc
352 bool wxChoice::OS2Command(
354 , WXWORD
WXUNUSED(wId
)
357 if (uParam
!= LN_SELECT
)
360 // "selection changed" is the only event we're after
364 int n
= GetSelection();
368 wxCommandEvent
vEvent( wxEVT_COMMAND_CHOICE_SELECTED
373 vEvent
.SetEventObject(this);
374 vEvent
.SetString((char*)GetStringSelection().c_str());
375 if (HasClientObjectData())
376 vEvent
.SetClientObject(GetClientObject(n
));
377 else if (HasClientUntypedData())
378 vEvent
.SetClientData(GetClientData(n
));
379 ProcessCommand(vEvent
);
382 } // end of wxChoice::OS2Command
384 void wxChoice::Free()
386 if (HasClientObjectData())
388 size_t nCount
= GetCount();
390 for (size_t n
= 0; n
< nCount
; n
++)
392 delete GetClientObject(n
);
395 } // end of wxhoice::Free