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
, wxControlWithItems
)
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 SetInitialSize(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::DoInsertItems(const wxArrayStringsAdapter
& items
127 , wxClientDataType type
130 int nIndex
= wxNOT_FOUND
;
132 bool incrementPos
= false;
134 nIndexType
= LIT_SORTASCENDING
;
135 else if (pos
== GetCount())
136 nIndexType
= LIT_END
;
143 const unsigned int count
= items
.GetCount();
144 for( unsigned int i
= 0; i
< count
; ++i
)
146 nIndex
= (int)::WinSendMsg( GetHwnd()
149 ,(MPARAM
)items
[i
].wx_str()
153 nIndex
= wxNOT_FOUND
;
156 AssignNewItemClientData(nIndex
, clientData
, i
, type
);
162 } // end of wxChoice::DoInsertAppendItemsWithData
164 void wxChoice::DoDeleteOneItem(unsigned int n
)
166 wxCHECK_RET( IsValid(n
), wxT("invalid item index in wxChoice::Delete") );
168 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, (MPARAM
)0);
169 } // end of wxChoice::Delete
171 void wxChoice::DoClear()
173 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
174 } // end of wxChoice::Clear
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 int wxChoice::GetSelection() const
182 return((int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0)));
183 } // end of wxChoice::GetSelection
185 void wxChoice::SetSelection(
189 ::WinSendMsg( GetHwnd()
194 } // end of wxChoice::SetSelection
196 // ----------------------------------------------------------------------------
197 // string list functions
198 // ----------------------------------------------------------------------------
200 unsigned int wxChoice::GetCount() const
202 return((unsigned int)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMCOUNT
, (MPARAM
)0, (MPARAM
)0)));
203 } // end of wxChoice::GetCount
205 void wxChoice::SetString(unsigned int n
, const wxString
& rsStr
)
210 if ( HasClientData() )
212 pData
= DoGetItemClientData(n
);
214 else // no client data
219 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, 0);
221 if (m_windowStyle
& wxCB_SORT
)
222 nIndexType
= LIT_SORTASCENDING
;
224 nIndexType
= LIT_END
;
225 ::WinSendMsg( GetHwnd()
228 ,(MPARAM
)rsStr
.wx_str()
233 DoSetItemClientData(n
, pData
);
235 } // end of wxChoice::SetString
237 wxString
wxChoice::GetString(unsigned int n
) const
240 wxString sStr
= wxEmptyString
;
243 nLen
= (size_t)LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)n
, (MPARAM
)0));
244 if (nLen
!= LIT_ERROR
&& nLen
> 0)
246 zBuf
= new wxChar
[++nLen
];
247 ::WinSendMsg( GetHwnd()
249 ,MPFROM2SHORT((SHORT
)n
, (SHORT
)nLen
)
256 } // end of wxChoice::GetString
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 void wxChoice::DoSetItemClientData(unsigned int n
, void* pClientData
)
264 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)n
, MPFROMP(pClientData
));
265 } // end of wxChoice::DoSetItemClientData
267 void* wxChoice::DoGetItemClientData(unsigned int n
) const
269 MRESULT rc
= ::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, (MPARAM
)n
, (MPARAM
)0);
271 } // end of wxChoice::DoGetItemClientData
273 // ----------------------------------------------------------------------------
274 // wxOS2 specific helpers
275 // ----------------------------------------------------------------------------
277 void wxChoice::DoSetSize(int nX
,
280 int WXUNUSED(nHeight
),
284 // Ignore height parameter because height doesn't mean 'initially
285 // displayed' height, it refers to the drop-down menu as well. The
286 // wxWidgets interpretation is different; also, getting the size returns
287 // the _displayed_ size (NOT the drop down menu size) so
288 // setting-getting-setting size would not work.
290 wxControl::DoSetSize( nX
296 } // end of wxChoice::DoSetSize
298 wxSize
wxChoice::DoGetBestSize() const
301 // Find the widest string
304 int nChoiceWidth
= 0;
307 wxFont vFont
= (wxFont
)GetFont();
309 const unsigned int nItems
= GetCount();
311 for (unsigned int i
= 0; i
< nItems
; i
++)
313 wxString
sStr(GetString(i
));
314 GetTextExtent( sStr
, &nLineWidth
, NULL
);
315 if (nLineWidth
> nChoiceWidth
)
316 nChoiceWidth
= nLineWidth
;
320 // Give it some reasonable default value if there are no strings in the
323 if (nChoiceWidth
== 0L)
327 // The combobox should be larger than the widest string
329 wxGetCharSize( GetHWND(), &nCx
, &nCy
, &vFont
);
330 nChoiceWidth
+= 5 * nCx
;
333 // Choice drop-down list depends on number of items (limited to 10)
335 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
336 int nChoiceHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * nStrings
;
338 return wxSize(nChoiceWidth
, nChoiceHeight
);
339 } // end of wxChoice::DoGetBestSize
341 MRESULT
wxChoice::OS2WindowProc(
347 return wxWindow::OS2WindowProc( uMsg
351 } // end of wxChoice::OS2WindowProc
353 bool wxChoice::OS2Command(
355 , WXWORD
WXUNUSED(wId
)
358 if (uParam
!= LN_SELECT
)
361 // "selection changed" is the only event we're after
365 int n
= GetSelection();
369 wxCommandEvent
vEvent( wxEVT_COMMAND_CHOICE_SELECTED
374 vEvent
.SetEventObject(this);
375 vEvent
.SetString(GetStringSelection());
376 if (HasClientObjectData())
377 vEvent
.SetClientObject(GetClientObject(n
));
378 else if (HasClientUntypedData())
379 vEvent
.SetClientData(GetClientData(n
));
380 ProcessCommand(vEvent
);
383 } // end of wxChoice::OS2Command
385 #endif // wxUSE_CHOICE