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"
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
++)
103 } // end of wxChoice::Create
105 // ----------------------------------------------------------------------------
106 // adding/deleting items to/from the list
107 // ----------------------------------------------------------------------------
109 int wxChoice::DoAppend(
110 const wxString
& rsItem
116 if (m_windowStyle
& wxLB_SORT
)
117 nIndexType
= LIT_SORTASCENDING
;
119 nIndexType
= LIT_END
;
120 nIndex
= (int)::WinSendMsg( GetHwnd()
123 ,(MPARAM
)rsItem
.c_str()
126 } // end of wxChoice::DoAppend
128 int wxChoice::DoInsert(
129 const wxString
& rsItem
,
133 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
134 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
136 if (pos
== GetCount())
137 return DoAppend(rsItem
);
142 if (m_windowStyle
& wxLB_SORT
)
143 nIndexType
= LIT_SORTASCENDING
;
146 nIndex
= (int)::WinSendMsg( GetHwnd()
149 ,(MPARAM
)rsItem
.c_str()
152 } // end of wxChoice::DoInsert
154 void wxChoice::Delete(
158 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
159 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, (MPARAM
)0);
160 } // end of wxChoice::Delete
162 void wxChoice::Clear()
165 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
166 } // end of wxChoice::Clear
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 int wxChoice::GetSelection() const
174 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0)));
175 } // end of wxChoice::GetSelection
177 void wxChoice::SetSelection(
181 ::WinSendMsg( GetHwnd()
186 } // end of wxChoice::SetSelection
188 // ----------------------------------------------------------------------------
189 // string list functions
190 // ----------------------------------------------------------------------------
192 int wxChoice::GetCount() const
194 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT
, (MPARAM
)0, (MPARAM
)0)));
195 } // end of wxChoice::GetCount
197 void wxChoice::SetString( int n
, const wxString
& rsStr
)
202 if ( m_clientDataItemsType
!= wxClientData_None
)
204 pData
= DoGetItemClientData(n
);
206 else // no client data
211 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, 0);
213 if (m_windowStyle
& wxLB_SORT
)
214 nIndexType
= LIT_SORTASCENDING
;
216 nIndexType
= LIT_END
;
217 ::WinSendMsg( GetHwnd()
220 ,(MPARAM
)rsStr
.c_str()
225 DoSetItemClientData( n
229 } // end of wxChoice::SetString
231 wxString
wxChoice::GetString(int n
) const
234 wxString sStr
= wxEmptyString
;
237 nLen
= (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)n
, (MPARAM
)0));
238 if (nLen
!= LIT_ERROR
&& nLen
> 0)
240 zBuf
= new wxChar
[nLen
+ 1];
241 ::WinSendMsg( GetHwnd()
243 ,MPFROM2SHORT((SHORT
)n
, (SHORT
)nLen
)
250 } // end of wxChoice::GetString
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
256 void wxChoice::DoSetItemClientData(
261 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)n
, MPFROMP(pClientData
));
262 } // end of wxChoice::DoSetItemClientData
264 void* wxChoice::DoGetItemClientData( int n
) const
268 rc
= ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, (MPARAM
)n
, (MPARAM
)0);
270 } // end of wxChoice::DoSetItemClientData
272 void wxChoice::DoSetItemClientObject(
274 , wxClientData
* pClientData
277 DoSetItemClientData( n
280 } // end of wxChoice::DoSetItemClientObject
282 wxClientData
* wxChoice::DoGetItemClientObject(
286 return (wxClientData
*)DoGetItemClientData(n
);
287 } // end of wxChoice::DoGetItemClientObject
289 // ----------------------------------------------------------------------------
290 // wxOS2 specific helpers
291 // ----------------------------------------------------------------------------
293 void wxChoice::DoSetSize(int nX
,
296 int WXUNUSED(nHeight
),
300 // Ignore height parameter because height doesn't mean 'initially
301 // displayed' height, it refers to the drop-down menu as well. The
302 // wxWidgets interpretation is different; also, getting the size returns
303 // the _displayed_ size (NOT the drop down menu size) so
304 // setting-getting-setting size would not work.
306 wxControl::DoSetSize( nX
312 } // end of wxChoice::DoSetSize
314 wxSize
wxChoice::DoGetBestSize() const
317 // Find the widest string
320 int nChoiceWidth
= 0;
321 int nItems
= GetCount();
324 wxFont vFont
= (wxFont
)GetFont();
326 for (int i
= 0; i
< nItems
; i
++)
328 wxString
sStr(GetString(i
));
334 if (nLineWidth
> nChoiceWidth
)
335 nChoiceWidth
= nLineWidth
;
339 // Give it some reasonable default value if there are no strings in the
342 if (nChoiceWidth
== 0L)
346 // The combobox should be larger than the widest string
348 wxGetCharSize( GetHWND()
353 nChoiceWidth
+= 5 * nCx
;
356 // Choice drop-down list depends on number of items (limited to 10)
358 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
359 int nChoiceHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * nStrings
;
361 return wxSize( nChoiceWidth
364 } // end of wxChoice::DoGetBestSize
366 MRESULT
wxChoice::OS2WindowProc(
372 return wxWindow::OS2WindowProc( uMsg
376 } // end of wxChoice::OS2WindowProc
378 bool wxChoice::OS2Command(
380 , WXWORD
WXUNUSED(wId
)
383 if (uParam
!= LN_SELECT
)
386 // "selection changed" is the only event we're after
390 int n
= GetSelection();
394 wxCommandEvent
vEvent( wxEVT_COMMAND_CHOICE_SELECTED
399 vEvent
.SetEventObject(this);
400 vEvent
.SetString(GetStringSelection());
401 if (HasClientObjectData())
402 vEvent
.SetClientObject(GetClientObject(n
));
403 else if (HasClientUntypedData())
404 vEvent
.SetClientData(GetClientData(n
));
405 ProcessCommand(vEvent
);
408 } // end of wxChoice::OS2Command
410 void wxChoice::Free()
412 if (HasClientObjectData())
414 size_t nCount
= GetCount();
416 for (size_t n
= 0; n
< nCount
; n
++)
418 delete GetClientObject(n
);
421 } // end of wxChoice::Free
423 #endif // wxUSE_CHOICE