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"
18 #include "wx/choice.h"
21 #include "wx/settings.h"
24 #include "wx/os2/private.h"
26 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
28 bool wxChoice::Create(
33 , const wxArrayString
& asChoices
35 , const wxValidator
& rValidator
36 , const wxString
& rsName
39 wxCArrayString
chs(asChoices
);
41 return Create(pParent
, vId
, rPos
, rSize
, chs
.GetCount(), chs
.GetStrings(),
42 lStyle
, rValidator
, rsName
);
45 bool wxChoice::Create(
51 , const wxString asChoices
[]
53 , const wxValidator
& rValidator
54 , const wxString
& rsName
59 if (!CreateControl( pParent
68 lSstyle
= CBS_DROPDOWNLIST
|
72 if (lStyle
& wxCLIP_SIBLINGS
)
73 lSstyle
|= WS_CLIPSIBLINGS
;
75 wxASSERT_MSG( !(lStyle
& wxCB_DROPDOWN
) &&
76 !(lStyle
& wxCB_READONLY
) &&
77 !(lStyle
& wxCB_SIMPLE
),
78 wxT("this style flag is ignored by wxChoice, you "
79 "probably want to use a wxComboBox") );
81 if (!OS2CreateControl( wxT("COMBOBOX")
87 // A choice/combobox normally has a white background (or other, depending
88 // on global settings) rather than inheriting the parent's background colour.
90 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
92 // initialize the controls contents
93 for (int i
= 0; i
< n
; i
++)
103 } // end of wxChoice::Create
105 wxChoice::~wxChoice()
110 // ----------------------------------------------------------------------------
111 // adding/deleting items to/from the list
112 // ----------------------------------------------------------------------------
114 int wxChoice::DoAppend(
115 const wxString
& rsItem
121 if (m_windowStyle
& wxLB_SORT
)
122 nIndexType
= LIT_SORTASCENDING
;
124 nIndexType
= LIT_END
;
125 nIndex
= (int)::WinSendMsg( GetHwnd()
128 ,(MPARAM
)rsItem
.c_str()
131 } // end of wxChoice::DoAppend
133 int wxChoice::DoInsert( const wxString
& rsItem
, unsigned int pos
)
135 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
136 wxCHECK_MSG(IsValidInsert(pos
), -1, wxT("invalid index"));
138 if (pos
== GetCount())
139 return DoAppend(rsItem
);
144 if (m_windowStyle
& wxLB_SORT
)
145 nIndexType
= LIT_SORTASCENDING
;
148 nIndex
= (int)::WinSendMsg( GetHwnd()
151 ,(MPARAM
)rsItem
.c_str()
154 } // end of wxChoice::DoInsert
156 void wxChoice::Delete(unsigned int n
)
158 wxCHECK_RET( IsValid(n
), wxT("invalid item index in wxChoice::Delete") );
160 if ( HasClientObjectData() )
162 delete GetClientObject(n
);
165 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, (MPARAM
)0);
166 } // end of wxChoice::Delete
168 void wxChoice::Clear()
171 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
172 } // end of wxChoice::Clear
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
178 int wxChoice::GetSelection() const
180 // if m_lastAcceptedSelection is set, it means that the dropdown is
181 // currently shown and that we want to use the last "permanent" selection
182 // instead of whatever is under the mouse pointer currently
184 // otherwise, get the selection from the control
185 return m_lastAcceptedSelection
== wxID_NONE
? GetCurrentSelection()
186 : m_lastAcceptedSelection
;
189 int wxChoice::GetCurrentSelection() const
191 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0)));
192 } // end of wxChoice::GetSelection
194 void wxChoice::SetSelection(
198 ::WinSendMsg( GetHwnd()
203 } // end of wxChoice::SetSelection
205 // ----------------------------------------------------------------------------
206 // string list functions
207 // ----------------------------------------------------------------------------
209 unsigned int wxChoice::GetCount() const
211 return((unsigned int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT
, (MPARAM
)0, (MPARAM
)0)));
212 } // end of wxChoice::GetCount
214 void wxChoice::SetString(unsigned int n
, const wxString
& rsStr
)
219 if ( m_clientDataItemsType
!= wxClientData_None
)
221 pData
= DoGetItemClientData(n
);
223 else // no client data
228 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, 0);
230 if (m_windowStyle
& wxLB_SORT
)
231 nIndexType
= LIT_SORTASCENDING
;
233 nIndexType
= LIT_END
;
234 ::WinSendMsg( GetHwnd()
237 ,(MPARAM
)rsStr
.c_str()
242 DoSetItemClientData(n
, pData
);
244 } // end of wxChoice::SetString
246 wxString
wxChoice::GetString(unsigned int n
) const
249 wxString sStr
= wxEmptyString
;
252 nLen
= (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)n
, (MPARAM
)0));
253 if (nLen
!= LIT_ERROR
&& nLen
> 0)
255 zBuf
= new wxChar
[nLen
+ 1];
256 ::WinSendMsg( GetHwnd()
258 ,MPFROM2SHORT((SHORT
)n
, (SHORT
)nLen
)
265 } // end of wxChoice::GetString
267 // ----------------------------------------------------------------------------
269 // ----------------------------------------------------------------------------
271 void wxChoice::DoSetItemClientData(unsigned int n
, void* pClientData
)
273 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)n
, MPFROMP(pClientData
));
274 } // end of wxChoice::DoSetItemClientData
276 void* wxChoice::DoGetItemClientData(unsigned int n
) const
278 MRESULT rc
= ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, (MPARAM
)n
, (MPARAM
)0);
280 } // end of wxChoice::DoGetItemClientData
282 void wxChoice::DoSetItemClientObject(unsigned int n
, wxClientData
* pClientData
)
284 DoSetItemClientData(n
, pClientData
);
285 } // end of wxChoice::DoSetItemClientObject
287 wxClientData
* wxChoice::DoGetItemClientObject(unsigned int n
) const
289 return (wxClientData
*)DoGetItemClientData(n
);
290 } // end of wxChoice::DoGetItemClientObject
292 // ----------------------------------------------------------------------------
293 // wxOS2 specific helpers
294 // ----------------------------------------------------------------------------
296 void wxChoice::DoSetSize(int nX
,
299 int WXUNUSED(nHeight
),
303 // Ignore height parameter because height doesn't mean 'initially
304 // displayed' height, it refers to the drop-down menu as well. The
305 // wxWidgets interpretation is different; also, getting the size returns
306 // the _displayed_ size (NOT the drop down menu size) so
307 // setting-getting-setting size would not work.
309 wxControl::DoSetSize( nX
315 } // end of wxChoice::DoSetSize
317 wxSize
wxChoice::DoGetBestSize() const
320 // Find the widest string
323 int nChoiceWidth
= 0;
326 wxFont vFont
= (wxFont
)GetFont();
328 const unsigned int nItems
= GetCount();
330 for (unsigned int i
= 0; i
< nItems
; i
++)
332 wxString
sStr(GetString(i
));
333 GetTextExtent( sStr
, &nLineWidth
, NULL
);
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(), &nCx
, &nCy
, &vFont
);
349 nChoiceWidth
+= 5 * nCx
;
352 // Choice drop-down list depends on number of items (limited to 10)
354 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
355 int nChoiceHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * nStrings
;
357 return wxSize(nChoiceWidth
, nChoiceHeight
);
358 } // end of wxChoice::DoGetBestSize
360 MRESULT
wxChoice::OS2WindowProc(
366 return wxWindow::OS2WindowProc( uMsg
370 } // end of wxChoice::OS2WindowProc
372 bool wxChoice::OS2Command(
374 , WXWORD
WXUNUSED(wId
)
377 if (uParam
!= LN_SELECT
)
380 // "selection changed" is the only event we're after
384 int n
= GetSelection();
388 wxCommandEvent
vEvent( wxEVT_COMMAND_CHOICE_SELECTED
393 vEvent
.SetEventObject(this);
394 vEvent
.SetString(GetStringSelection());
395 if (HasClientObjectData())
396 vEvent
.SetClientObject(GetClientObject(n
));
397 else if (HasClientUntypedData())
398 vEvent
.SetClientData(GetClientData(n
));
399 ProcessCommand(vEvent
);
402 } // end of wxChoice::OS2Command
404 void wxChoice::Free()
406 if (HasClientObjectData())
408 const unsigned int nCount
= GetCount();
410 for (unsigned int n
= 0; n
< nCount
; n
++)
412 delete GetClientObject(n
);
415 } // end of wxChoice::Free
417 #endif // wxUSE_CHOICE