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"
17 #include "wx/choice.h"
22 #include "wx/settings.h"
25 #include "wx/os2/private.h"
27 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
29 bool wxChoice::Create(
34 , const wxArrayString
& asChoices
36 , const wxValidator
& rValidator
37 , const wxString
& rsName
40 wxCArrayString
chs(asChoices
);
42 return Create(pParent
, vId
, rPos
, rSize
, chs
.GetCount(), chs
.GetStrings(),
43 lStyle
, rValidator
, rsName
);
46 bool wxChoice::Create(
52 , const wxString asChoices
[]
54 , const wxValidator
& rValidator
55 , const wxString
& rsName
60 if (!CreateControl( pParent
69 lSstyle
= CBS_DROPDOWNLIST
|
73 if (lStyle
& wxCLIP_SIBLINGS
)
74 lSstyle
|= WS_CLIPSIBLINGS
;
76 wxASSERT_MSG( !(lStyle
& wxCB_DROPDOWN
) &&
77 !(lStyle
& wxCB_READONLY
) &&
78 !(lStyle
& wxCB_SIMPLE
),
79 wxT("this style flag is ignored by wxChoice, you "
80 "probably want to use a wxComboBox") );
82 if (!OS2CreateControl( wxT("COMBOBOX")
88 // A choice/combobox normally has a white background (or other, depending
89 // on global settings) rather than inheriting the parent's background colour.
91 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
93 // initialize the controls contents
94 for (int i
= 0; i
< n
; i
++)
104 } // end of wxChoice::Create
106 wxChoice::~wxChoice()
111 // ----------------------------------------------------------------------------
112 // adding/deleting items to/from the list
113 // ----------------------------------------------------------------------------
115 int wxChoice::DoAppend(
116 const wxString
& rsItem
122 if (m_windowStyle
& wxLB_SORT
)
123 nIndexType
= LIT_SORTASCENDING
;
125 nIndexType
= LIT_END
;
126 nIndex
= (int)::WinSendMsg( GetHwnd()
129 ,(MPARAM
)rsItem
.c_str()
132 } // end of wxChoice::DoAppend
134 int wxChoice::DoInsert( const wxString
& rsItem
, unsigned int pos
)
136 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
137 wxCHECK_MSG(IsValidInsert(pos
), -1, wxT("invalid index"));
139 if (pos
== GetCount())
140 return DoAppend(rsItem
);
145 if (m_windowStyle
& wxLB_SORT
)
146 nIndexType
= LIT_SORTASCENDING
;
149 nIndex
= (int)::WinSendMsg( GetHwnd()
152 ,(MPARAM
)rsItem
.c_str()
155 } // end of wxChoice::DoInsert
157 void wxChoice::Delete(unsigned int n
)
159 wxCHECK_RET( IsValid(n
), wxT("invalid item index in wxChoice::Delete") );
161 if ( HasClientObjectData() )
163 delete GetClientObject(n
);
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 // if m_lastAcceptedSelection is set, it means that the dropdown is
182 // currently shown and that we want to use the last "permanent" selection
183 // instead of whatever is under the mouse pointer currently
185 // otherwise, get the selection from the control
186 return m_lastAcceptedSelection
== wxID_NONE
? GetCurrentSelection()
187 : m_lastAcceptedSelection
;
190 int wxChoice::GetCurrentSelection() const
192 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0)));
193 } // end of wxChoice::GetSelection
195 void wxChoice::SetSelection(
199 ::WinSendMsg( GetHwnd()
204 } // end of wxChoice::SetSelection
206 // ----------------------------------------------------------------------------
207 // string list functions
208 // ----------------------------------------------------------------------------
210 unsigned int wxChoice::GetCount() const
212 return((unsigned int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT
, (MPARAM
)0, (MPARAM
)0)));
213 } // end of wxChoice::GetCount
215 void wxChoice::SetString(unsigned int n
, const wxString
& rsStr
)
220 if ( m_clientDataItemsType
!= wxClientData_None
)
222 pData
= DoGetItemClientData(n
);
224 else // no client data
229 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, 0);
231 if (m_windowStyle
& wxLB_SORT
)
232 nIndexType
= LIT_SORTASCENDING
;
234 nIndexType
= LIT_END
;
235 ::WinSendMsg( GetHwnd()
238 ,(MPARAM
)rsStr
.c_str()
243 DoSetItemClientData(n
, pData
);
245 } // end of wxChoice::SetString
247 wxString
wxChoice::GetString(unsigned int n
) const
250 wxString sStr
= wxEmptyString
;
253 nLen
= (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)n
, (MPARAM
)0));
254 if (nLen
!= LIT_ERROR
&& nLen
> 0)
256 zBuf
= new wxChar
[nLen
+ 1];
257 ::WinSendMsg( GetHwnd()
259 ,MPFROM2SHORT((SHORT
)n
, (SHORT
)nLen
)
266 } // end of wxChoice::GetString
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 void wxChoice::DoSetItemClientData(unsigned int n
, void* pClientData
)
274 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)n
, MPFROMP(pClientData
));
275 } // end of wxChoice::DoSetItemClientData
277 void* wxChoice::DoGetItemClientData(unsigned int n
) const
279 MRESULT rc
= ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, (MPARAM
)n
, (MPARAM
)0);
281 } // end of wxChoice::DoGetItemClientData
283 void wxChoice::DoSetItemClientObject(unsigned int n
, wxClientData
* pClientData
)
285 DoSetItemClientData(n
, pClientData
);
286 } // end of wxChoice::DoSetItemClientObject
288 wxClientData
* wxChoice::DoGetItemClientObject(unsigned int n
) const
290 return (wxClientData
*)DoGetItemClientData(n
);
291 } // end of wxChoice::DoGetItemClientObject
293 // ----------------------------------------------------------------------------
294 // wxOS2 specific helpers
295 // ----------------------------------------------------------------------------
297 void wxChoice::DoSetSize(int nX
,
300 int WXUNUSED(nHeight
),
304 // Ignore height parameter because height doesn't mean 'initially
305 // displayed' height, it refers to the drop-down menu as well. The
306 // wxWidgets interpretation is different; also, getting the size returns
307 // the _displayed_ size (NOT the drop down menu size) so
308 // setting-getting-setting size would not work.
310 wxControl::DoSetSize( nX
316 } // end of wxChoice::DoSetSize
318 wxSize
wxChoice::DoGetBestSize() const
321 // Find the widest string
324 int nChoiceWidth
= 0;
327 wxFont vFont
= (wxFont
)GetFont();
329 const unsigned int nItems
= GetCount();
331 for (unsigned int i
= 0; i
< nItems
; i
++)
333 wxString
sStr(GetString(i
));
334 GetTextExtent( sStr
, &nLineWidth
, NULL
);
335 if (nLineWidth
> nChoiceWidth
)
336 nChoiceWidth
= nLineWidth
;
340 // Give it some reasonable default value if there are no strings in the
343 if (nChoiceWidth
== 0L)
347 // The combobox should be larger than the widest string
349 wxGetCharSize( GetHWND(), &nCx
, &nCy
, &vFont
);
350 nChoiceWidth
+= 5 * nCx
;
353 // Choice drop-down list depends on number of items (limited to 10)
355 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
356 int nChoiceHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * nStrings
;
358 return wxSize(nChoiceWidth
, nChoiceHeight
);
359 } // end of wxChoice::DoGetBestSize
361 MRESULT
wxChoice::OS2WindowProc(
367 return wxWindow::OS2WindowProc( uMsg
371 } // end of wxChoice::OS2WindowProc
373 bool wxChoice::OS2Command(
375 , WXWORD
WXUNUSED(wId
)
378 if (uParam
!= LN_SELECT
)
381 // "selection changed" is the only event we're after
385 int n
= GetSelection();
389 wxCommandEvent
vEvent( wxEVT_COMMAND_CHOICE_SELECTED
394 vEvent
.SetEventObject(this);
395 vEvent
.SetString(GetStringSelection());
396 if (HasClientObjectData())
397 vEvent
.SetClientObject(GetClientObject(n
));
398 else if (HasClientUntypedData())
399 vEvent
.SetClientData(GetClientData(n
));
400 ProcessCommand(vEvent
);
403 } // end of wxChoice::OS2Command
405 void wxChoice::Free()
407 if (HasClientObjectData())
409 const unsigned int nCount
= GetCount();
411 for (unsigned int n
= 0; n
< nCount
; n
++)
413 delete GetClientObject(n
);
416 } // end of wxChoice::Free
418 #endif // wxUSE_CHOICE