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"
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 int wxChoice::FindString(
205 const wxString
& rsStr
213 nItemCount
= (int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT
, (MPARAM
)0, (MPARAM
)0));
214 for (nPos
= 0; nPos
< nItemCount
; nPos
++)
216 nTextLength
= (int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)nPos
, (MPARAM
)0));
217 zStr
= new char[nTextLength
+ 1];
218 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT
, MPFROM2SHORT((SHORT
)nPos
, (SHORT
)nTextLength
), (MPARAM
)zStr
);
219 if (rsStr
== (wxChar
*)zStr
)
227 } // end of wxChoice::FindString
229 void wxChoice::SetString(
231 , const wxString
& rsStr
237 if ( m_clientDataItemsType
!= wxClientData_None
)
239 pData
= DoGetItemClientData(n
);
241 else // no client data
246 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, 0);
248 if (m_windowStyle
& wxLB_SORT
)
249 nIndexType
= LIT_SORTASCENDING
;
251 nIndexType
= LIT_END
;
252 ::WinSendMsg( GetHwnd()
255 ,(MPARAM
)rsStr
.c_str()
260 DoSetItemClientData( n
264 } // end of wxChoice::SetString
266 wxString
wxChoice::GetString(
271 wxString sStr
= wxEmptyString
;
274 nLen
= (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)n
, (MPARAM
)0));
275 if (nLen
!= LIT_ERROR
&& nLen
> 0)
277 zBuf
= new wxChar
[nLen
+ 1];
278 ::WinSendMsg( GetHwnd()
280 ,MPFROM2SHORT((SHORT
)n
, (SHORT
)nLen
)
287 } // end of wxChoice::GetString
289 // ----------------------------------------------------------------------------
291 // ----------------------------------------------------------------------------
293 void wxChoice::DoSetItemClientData(
298 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)n
, MPFROMP(pClientData
));
299 } // end of wxChoice::DoSetItemClientData
301 void* wxChoice::DoGetItemClientData( int n
) const
305 rc
= ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, (MPARAM
)n
, (MPARAM
)0);
307 } // end of wxChoice::DoSetItemClientData
309 void wxChoice::DoSetItemClientObject(
311 , wxClientData
* pClientData
314 DoSetItemClientData( n
317 } // end of wxChoice::DoSetItemClientObject
319 wxClientData
* wxChoice::DoGetItemClientObject(
323 return (wxClientData
*)DoGetItemClientData(n
);
324 } // end of wxChoice::DoGetItemClientObject
326 // ----------------------------------------------------------------------------
327 // wxOS2 specific helpers
328 // ----------------------------------------------------------------------------
330 void wxChoice::DoSetSize(
339 // Ignore height parameter because height doesn't mean 'initially
340 // displayed' height, it refers to the drop-down menu as well. The
341 // wxWidgets interpretation is different; also, getting the size returns
342 // the _displayed_ size (NOT the drop down menu size) so
343 // setting-getting-setting size would not work.
345 wxControl::DoSetSize( nX
351 } // end of wxChoice::DoSetSize
353 wxSize
wxChoice::DoGetBestSize() const
356 // Find the widest string
359 int nChoiceWidth
= 0;
360 int nItems
= GetCount();
363 wxFont vFont
= (wxFont
)GetFont();
365 for (int i
= 0; i
< nItems
; i
++)
367 wxString
sStr(GetString(i
));
373 if (nLineWidth
> nChoiceWidth
)
374 nChoiceWidth
= nLineWidth
;
378 // Give it some reasonable default value if there are no strings in the
381 if (nChoiceWidth
== 0L)
385 // The combobox should be larger than the widest string
387 wxGetCharSize( GetHWND()
392 nChoiceWidth
+= 5 * nCx
;
395 // Choice drop-down list depends on number of items (limited to 10)
397 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
398 int nChoiceHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * nStrings
;
400 return wxSize( nChoiceWidth
403 } // end of wxChoice::DoGetBestSize
405 MRESULT
wxChoice::OS2WindowProc(
411 return wxWindow::OS2WindowProc( uMsg
415 } // end of wxChoice::OS2WindowProc
417 bool wxChoice::OS2Command(
419 , WXWORD
WXUNUSED(wId
)
422 if (uParam
!= LN_SELECT
)
425 // "selection changed" is the only event we're after
429 int n
= GetSelection();
433 wxCommandEvent
vEvent( wxEVT_COMMAND_CHOICE_SELECTED
438 vEvent
.SetEventObject(this);
439 vEvent
.SetString(GetStringSelection());
440 if (HasClientObjectData())
441 vEvent
.SetClientObject(GetClientObject(n
));
442 else if (HasClientUntypedData())
443 vEvent
.SetClientData(GetClientData(n
));
444 ProcessCommand(vEvent
);
447 } // end of wxChoice::OS2Command
449 void wxChoice::Free()
451 if (HasClientObjectData())
453 size_t nCount
= GetCount();
455 for (size_t n
= 0; n
< nCount
; n
++)
457 delete GetClientObject(n
);
460 } // end of wxChoice::Free
462 #endif // wxUSE_CHOICE