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 bool wxChoice::Create(
32 , const wxArrayString
& asChoices
34 , const wxValidator
& rValidator
35 , const wxString
& rsName
38 wxCArrayString
chs(asChoices
);
40 return Create(pParent
, vId
, rPos
, rSize
, chs
.GetCount(), chs
.GetStrings(),
41 lStyle
, rValidator
, rsName
);
44 bool wxChoice::Create(
50 , const wxString asChoices
[]
52 , const wxValidator
& rValidator
53 , const wxString
& rsName
58 if (!CreateControl( pParent
67 lSstyle
= CBS_DROPDOWNLIST
|
71 // clipping siblings does not yet work
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 // Set height to use with sizers i.e. without the dropdown listbox
104 wxFont vFont
= GetFont();
106 wxGetCharSize( GetHWND(), NULL
, &nEditHeight
, &vFont
);
107 nEditHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nEditHeight
);
108 SetInitialSize(wxSize(-1,nEditHeight
+4)); // +2x2 for the border
111 } // end of wxChoice::Create
113 wxChoice::~wxChoice()
118 // ----------------------------------------------------------------------------
119 // adding/deleting items to/from the list
120 // ----------------------------------------------------------------------------
122 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
125 , wxClientDataType type
128 int nIndex
= wxNOT_FOUND
;
130 bool incrementPos
= false;
132 nIndexType
= LIT_SORTASCENDING
;
133 else if (pos
== GetCount())
134 nIndexType
= LIT_END
;
141 const unsigned int count
= items
.GetCount();
142 for( unsigned int i
= 0; i
< count
; ++i
)
144 nIndex
= (int)::WinSendMsg( GetHwnd()
147 ,(MPARAM
)items
[i
].wx_str()
151 nIndex
= wxNOT_FOUND
;
154 AssignNewItemClientData(nIndex
, clientData
, i
, type
);
160 } // end of wxChoice::DoInsertAppendItemsWithData
162 void wxChoice::DoDeleteOneItem(unsigned int n
)
164 wxCHECK_RET( IsValid(n
), wxT("invalid item index in wxChoice::Delete") );
166 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, (MPARAM
)0);
167 } // end of wxChoice::Delete
169 void wxChoice::DoClear()
171 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
172 } // end of wxChoice::Clear
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
178 int wxChoice::GetSelection() const
180 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0)));
181 } // end of wxChoice::GetSelection
183 void wxChoice::SetSelection(
187 ::WinSendMsg( GetHwnd()
192 } // end of wxChoice::SetSelection
194 // ----------------------------------------------------------------------------
195 // string list functions
196 // ----------------------------------------------------------------------------
198 unsigned int wxChoice::GetCount() const
200 return((unsigned int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT
, (MPARAM
)0, (MPARAM
)0)));
201 } // end of wxChoice::GetCount
203 void wxChoice::SetString(unsigned int n
, const wxString
& rsStr
)
208 if ( HasClientData() )
210 pData
= DoGetItemClientData(n
);
212 else // no client data
217 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, 0);
219 if (m_windowStyle
& wxCB_SORT
)
220 nIndexType
= LIT_SORTASCENDING
;
222 nIndexType
= LIT_END
;
223 ::WinSendMsg( GetHwnd()
226 ,(MPARAM
)rsStr
.wx_str()
231 DoSetItemClientData(n
, pData
);
233 } // end of wxChoice::SetString
235 wxString
wxChoice::GetString(unsigned int n
) const
238 wxString sStr
= wxEmptyString
;
241 nLen
= (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)n
, (MPARAM
)0));
242 if (nLen
!= LIT_ERROR
&& nLen
> 0)
244 zBuf
= new wxChar
[++nLen
];
245 ::WinSendMsg( GetHwnd()
247 ,MPFROM2SHORT((SHORT
)n
, (SHORT
)nLen
)
254 } // end of wxChoice::GetString
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 void wxChoice::DoSetItemClientData(unsigned int n
, void* pClientData
)
262 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)n
, MPFROMP(pClientData
));
263 } // end of wxChoice::DoSetItemClientData
265 void* wxChoice::DoGetItemClientData(unsigned int n
) const
267 MRESULT rc
= ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, (MPARAM
)n
, (MPARAM
)0);
269 } // end of wxChoice::DoGetItemClientData
271 // ----------------------------------------------------------------------------
272 // wxOS2 specific helpers
273 // ----------------------------------------------------------------------------
275 void wxChoice::DoSetSize(int nX
,
278 int WXUNUSED(nHeight
),
282 // Ignore height parameter because height doesn't mean 'initially
283 // displayed' height, it refers to the drop-down menu as well. The
284 // wxWidgets interpretation is different; also, getting the size returns
285 // the _displayed_ size (NOT the drop down menu size) so
286 // setting-getting-setting size would not work.
288 wxControl::DoSetSize( nX
294 } // end of wxChoice::DoSetSize
296 wxSize
wxChoice::DoGetBestSize() const
299 // Find the widest string
302 int nChoiceWidth
= 0;
305 wxFont vFont
= (wxFont
)GetFont();
307 const unsigned int nItems
= GetCount();
309 for (unsigned int i
= 0; i
< nItems
; i
++)
311 wxString
sStr(GetString(i
));
312 GetTextExtent( sStr
, &nLineWidth
, NULL
);
313 if (nLineWidth
> nChoiceWidth
)
314 nChoiceWidth
= nLineWidth
;
318 // Give it some reasonable default value if there are no strings in the
321 if (nChoiceWidth
== 0L)
325 // The combobox should be larger than the widest string
327 wxGetCharSize( GetHWND(), &nCx
, &nCy
, &vFont
);
328 nChoiceWidth
+= 5 * nCx
;
331 // Choice drop-down list depends on number of items (limited to 10)
333 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
334 int nChoiceHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * nStrings
;
336 return wxSize(nChoiceWidth
, nChoiceHeight
);
337 } // end of wxChoice::DoGetBestSize
339 MRESULT
wxChoice::OS2WindowProc(
345 return wxWindow::OS2WindowProc( uMsg
349 } // end of wxChoice::OS2WindowProc
351 bool wxChoice::OS2Command(
353 , WXWORD
WXUNUSED(wId
)
356 if (uParam
!= LN_SELECT
)
359 // "selection changed" is the only event we're after
363 int n
= GetSelection();
367 wxCommandEvent
vEvent( wxEVT_COMMAND_CHOICE_SELECTED
372 vEvent
.SetEventObject(this);
373 vEvent
.SetString(GetStringSelection());
374 if (HasClientObjectData())
375 vEvent
.SetClientObject(GetClientObject(n
));
376 else if (HasClientUntypedData())
377 vEvent
.SetClientData(GetClientData(n
));
378 ProcessCommand(vEvent
);
381 } // end of wxChoice::OS2Command
383 #endif // wxUSE_CHOICE