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;
190 if ( m_clientDataItemsType
!= wxClientData_None
)
192 pData
= DoGetItemClientData(n
);
194 else // no client data
199 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, 0);
201 if (m_windowStyle
& wxLB_SORT
)
202 nIndexType
= LIT_SORTASCENDING
;
204 nIndexType
= LIT_END
;
205 ::WinSendMsg( GetHwnd()
208 ,(MPARAM
)rsStr
.c_str()
213 DoSetItemClientData( n
217 } // end of wxChoice::SetString
219 wxString
wxChoice::GetString(
227 nLen
= (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)n
, (MPARAM
)0));
228 if (nLen
!= LIT_ERROR
&& nLen
> 0)
230 zBuf
= new char[nLen
+ 1];
231 ::WinSendMsg( GetHwnd()
233 ,MPFROM2SHORT((SHORT
)n
, (SHORT
)nLen
)
240 } // end of wxChoice::GetString
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 void wxChoice::DoSetItemClientData(
251 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)n
, MPFROMP(pClientData
));
252 } // end of wxChoice::DoSetItemClientData
254 void* wxChoice::DoGetItemClientData( int n
) const
258 rc
= ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, (MPARAM
)n
, (MPARAM
)0);
260 } // end of wxChoice::DoSetItemClientData
262 void wxChoice::DoSetItemClientObject(
264 , wxClientData
* pClientData
267 DoSetItemClientData( n
270 } // end of wxChoice::DoSetItemClientObject
272 wxClientData
* wxChoice::DoGetItemClientObject(
276 return (wxClientData
*)DoGetItemClientData(n
);
277 } // end of wxChoice::DoGetItemClientObject
279 // ----------------------------------------------------------------------------
280 // wxOS2 specific helpers
281 // ----------------------------------------------------------------------------
283 void wxChoice::DoSetSize(
292 // Ignore height parameter because height doesn't mean 'initially
293 // displayed' height, it refers to the drop-down menu as well. The
294 // wxWindows interpretation is different; also, getting the size returns
295 // the _displayed_ size (NOT the drop down menu size) so
296 // setting-getting-setting size would not work.
298 wxControl::DoSetSize( nX
304 } // end of wxChoice::DoSetSize
306 wxSize
wxChoice::DoGetBestSize() const
309 // Find the widest string
312 int nChoiceWidth
= 0;
313 int nItems
= GetCount();
317 for (int i
= 0; i
< nItems
; i
++)
319 wxString
sStr(GetString(i
));
325 if (nLineWidth
> nChoiceWidth
)
326 nChoiceWidth
= nLineWidth
;
330 // Give it some reasonable default value if there are no strings in the
333 if (nChoiceWidth
== 0L)
337 // The combobox should be larger than the widest string
339 wxGetCharSize( GetHWND()
344 nChoiceWidth
+= 5 * nCx
;
347 // Choice drop-down list depends on number of items (limited to 10)
349 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
350 int nChoiceHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * nStrings
;
352 return wxSize( nChoiceWidth
355 } // end of wxChoice::DoGetBestSize
357 MRESULT
wxChoice::OS2WindowProc(
363 return wxWindow::OS2WindowProc( uMsg
367 } // end of wxChoice::OS2WindowProc
369 bool wxChoice::OS2Command(
371 , WXWORD
WXUNUSED(wId
)
374 if (uParam
!= LN_SELECT
)
377 // "selection changed" is the only event we're after
381 int n
= GetSelection();
385 wxCommandEvent
vEvent( wxEVT_COMMAND_CHOICE_SELECTED
390 vEvent
.SetEventObject(this);
391 vEvent
.SetString((char*)GetStringSelection().c_str());
392 if (HasClientObjectData())
393 vEvent
.SetClientObject(GetClientObject(n
));
394 else if (HasClientUntypedData())
395 vEvent
.SetClientData(GetClientData(n
));
396 ProcessCommand(vEvent
);
399 } // end of wxChoice::OS2Command
401 void wxChoice::Free()
403 if (HasClientObjectData())
405 size_t nCount
= GetCount();
407 for (size_t n
= 0; n
< nCount
; n
++)
409 delete GetClientObject(n
);
412 } // end of wxhoice::Free