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 // clipping siblings does not yet work
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
));
94 // initialize the controls contents
95 for (int i
= 0; i
< n
; i
++)
105 // Set height to use with sizers i.e. without the dropdown listbox
106 wxFont vFont
= GetFont();
108 wxGetCharSize( GetHWND(), NULL
, &nEditHeight
, &vFont
);
109 nEditHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nEditHeight
);
110 SetBestFittingSize(wxSize(-1,nEditHeight
+4)); // +2x2 for the border
113 } // end of wxChoice::Create
115 wxChoice::~wxChoice()
120 // ----------------------------------------------------------------------------
121 // adding/deleting items to/from the list
122 // ----------------------------------------------------------------------------
124 int wxChoice::DoAppend(
125 const wxString
& rsItem
130 if (m_windowStyle
& wxCB_SORT
)
131 nIndexType
= LIT_SORTASCENDING
;
133 nIndexType
= LIT_END
;
134 nIndex
= (int)::WinSendMsg( GetHwnd()
137 ,(MPARAM
)rsItem
.c_str()
140 } // end of wxChoice::DoAppend
142 int wxChoice::DoInsert( const wxString
& rsItem
, unsigned int pos
)
144 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
145 wxCHECK_MSG(IsValidInsert(pos
), -1, wxT("invalid index"));
147 if (pos
== GetCount())
148 return DoAppend(rsItem
);
153 if (m_windowStyle
& wxCB_SORT
)
154 nIndexType
= LIT_SORTASCENDING
;
157 nIndex
= (int)::WinSendMsg( GetHwnd()
160 ,(MPARAM
)rsItem
.c_str()
163 } // end of wxChoice::DoInsert
165 void wxChoice::Delete(unsigned int n
)
167 wxCHECK_RET( IsValid(n
), wxT("invalid item index in wxChoice::Delete") );
169 if ( HasClientObjectData() )
171 delete GetClientObject(n
);
174 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, (MPARAM
)0);
175 } // end of wxChoice::Delete
177 void wxChoice::Clear()
180 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
181 } // end of wxChoice::Clear
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 int wxChoice::GetSelection() const
189 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0)));
190 } // end of wxChoice::GetSelection
192 void wxChoice::SetSelection(
196 ::WinSendMsg( GetHwnd()
201 } // end of wxChoice::SetSelection
203 // ----------------------------------------------------------------------------
204 // string list functions
205 // ----------------------------------------------------------------------------
207 unsigned int wxChoice::GetCount() const
209 return((unsigned int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT
, (MPARAM
)0, (MPARAM
)0)));
210 } // end of wxChoice::GetCount
212 void wxChoice::SetString(unsigned int n
, const wxString
& rsStr
)
217 if ( m_clientDataItemsType
!= wxClientData_None
)
219 pData
= DoGetItemClientData(n
);
221 else // no client data
226 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, 0);
228 if (m_windowStyle
& wxCB_SORT
)
229 nIndexType
= LIT_SORTASCENDING
;
231 nIndexType
= LIT_END
;
232 ::WinSendMsg( GetHwnd()
235 ,(MPARAM
)rsStr
.c_str()
240 DoSetItemClientData(n
, pData
);
242 } // end of wxChoice::SetString
244 wxString
wxChoice::GetString(unsigned int n
) const
247 wxString sStr
= wxEmptyString
;
250 nLen
= (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)n
, (MPARAM
)0));
251 if (nLen
!= LIT_ERROR
&& nLen
> 0)
253 zBuf
= new wxChar
[++nLen
];
254 ::WinSendMsg( GetHwnd()
256 ,MPFROM2SHORT((SHORT
)n
, (SHORT
)nLen
)
263 } // end of wxChoice::GetString
265 // ----------------------------------------------------------------------------
267 // ----------------------------------------------------------------------------
269 void wxChoice::DoSetItemClientData(unsigned int n
, void* pClientData
)
271 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)n
, MPFROMP(pClientData
));
272 } // end of wxChoice::DoSetItemClientData
274 void* wxChoice::DoGetItemClientData(unsigned int n
) const
276 MRESULT rc
= ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, (MPARAM
)n
, (MPARAM
)0);
278 } // end of wxChoice::DoGetItemClientData
280 void wxChoice::DoSetItemClientObject(unsigned int n
, wxClientData
* pClientData
)
282 DoSetItemClientData(n
, pClientData
);
283 } // end of wxChoice::DoSetItemClientObject
285 wxClientData
* wxChoice::DoGetItemClientObject(unsigned int n
) const
287 return (wxClientData
*)DoGetItemClientData(n
);
288 } // end of wxChoice::DoGetItemClientObject
290 // ----------------------------------------------------------------------------
291 // wxOS2 specific helpers
292 // ----------------------------------------------------------------------------
294 void wxChoice::DoSetSize(int nX
,
297 int WXUNUSED(nHeight
),
301 // Ignore height parameter because height doesn't mean 'initially
302 // displayed' height, it refers to the drop-down menu as well. The
303 // wxWidgets interpretation is different; also, getting the size returns
304 // the _displayed_ size (NOT the drop down menu size) so
305 // setting-getting-setting size would not work.
307 wxControl::DoSetSize( nX
313 } // end of wxChoice::DoSetSize
315 wxSize
wxChoice::DoGetBestSize() const
318 // Find the widest string
321 int nChoiceWidth
= 0;
324 wxFont vFont
= (wxFont
)GetFont();
326 const unsigned int nItems
= GetCount();
328 for (unsigned int i
= 0; i
< nItems
; i
++)
330 wxString
sStr(GetString(i
));
331 GetTextExtent( sStr
, &nLineWidth
, NULL
);
332 if (nLineWidth
> nChoiceWidth
)
333 nChoiceWidth
= nLineWidth
;
337 // Give it some reasonable default value if there are no strings in the
340 if (nChoiceWidth
== 0L)
344 // The combobox should be larger than the widest string
346 wxGetCharSize( GetHWND(), &nCx
, &nCy
, &vFont
);
347 nChoiceWidth
+= 5 * nCx
;
350 // Choice drop-down list depends on number of items (limited to 10)
352 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
353 int nChoiceHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * nStrings
;
355 return wxSize(nChoiceWidth
, nChoiceHeight
);
356 } // end of wxChoice::DoGetBestSize
358 MRESULT
wxChoice::OS2WindowProc(
364 return wxWindow::OS2WindowProc( uMsg
368 } // end of wxChoice::OS2WindowProc
370 bool wxChoice::OS2Command(
372 , WXWORD
WXUNUSED(wId
)
375 if (uParam
!= LN_SELECT
)
378 // "selection changed" is the only event we're after
382 int n
= GetSelection();
386 wxCommandEvent
vEvent( wxEVT_COMMAND_CHOICE_SELECTED
391 vEvent
.SetEventObject(this);
392 vEvent
.SetString(GetStringSelection());
393 if (HasClientObjectData())
394 vEvent
.SetClientObject(GetClientObject(n
));
395 else if (HasClientUntypedData())
396 vEvent
.SetClientData(GetClientData(n
));
397 ProcessCommand(vEvent
);
400 } // end of wxChoice::OS2Command
402 void wxChoice::Free()
404 if (HasClientObjectData())
406 const unsigned int nCount
= GetCount();
408 for (unsigned int n
= 0; n
< nCount
; n
++)
410 delete GetClientObject(n
);
413 } // end of wxChoice::Free
415 #endif // wxUSE_CHOICE