1 /////////////////////////////////////////////////////////////////////////////
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"
16 #include "wx/choice.h"
21 #include "wx/os2/private.h"
23 #if !USE_SHARED_LIBRARY
24 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
27 bool wxChoice::Create(wxWindow
*parent
,
31 int n
, const wxString choices
[],
34 const wxValidator
& validator
,
38 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
42 long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_TABSTOP | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
43 if ( style & wxCB_SORT )
46 // the experience shows that wxChoice vs. wxComboBox distinction confuses
47 // quite a few people - try to help them
48 wxASSERT_MSG( !(style & wxCB_DROPDOWN) &&
49 !(style & wxCB_READONLY) &&
50 !(style & wxCB_SIMPLE),
51 wxT("this style flag is ignored by wxChoice, you "
52 "probably want to use a wxComboBox") );
54 if ( !OS2CreateControl(wxT("COMBOBOX"), msStyle) )
57 for ( int i = 0; i < n; i++ )
62 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
67 // ----------------------------------------------------------------------------
68 // adding/deleting items to/from the list
69 // ----------------------------------------------------------------------------
71 int wxChoice::DoAppend(const wxString
& item
)
75 int n = (int)SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LONG)item.c_str());
78 wxLogLastError("SendMessage(CB_ADDSTRING)");
84 void wxChoice::Delete(int n
)
86 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
88 // TODO: SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
91 void wxChoice::Clear()
93 // TODO: SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 int wxChoice::GetSelection() const
102 // TODO: return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
106 void wxChoice::SetSelection(int n
)
108 // TODO: SendMessage(GetHwnd(), CB_SETCURSEL, n, 0);
111 // ----------------------------------------------------------------------------
112 // string list functions
113 // ----------------------------------------------------------------------------
115 int wxChoice::GetCount() const
117 // TODO: return (int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0);
121 int wxChoice::FindString(const wxString
& s
) const
125 int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT,
126 (WPARAM)-1, (LPARAM)s.c_str());
128 return pos == LB_ERR ? wxNOT_FOUND : pos;
133 void wxChoice::SetString(int n
, const wxString
& s
)
135 wxFAIL_MSG(wxT("not implemented"));
137 #if 0 // should do this, but no Insert() so far
143 wxString
wxChoice::GetString(int n
) const
145 size_t len
= 0; // TODO: (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
150 if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT, n,
151 (LPARAM)str.GetWriteBuf(len)) == CB_ERR ) {
152 wxLogLastError("SendMessage(CB_GETLBTEXT)");
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
164 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
168 if ( SendMessage(GetHwnd(), CB_SETITEMDATA, n, (LPARAM)clientData) == CB_ERR )
170 wxLogLastError(wxT("CB_SETITEMDATA"));
175 void* wxChoice::DoGetItemClientData( int n
) const
179 LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0);
182 wxLogLastError(wxT("CB_GETITEMDATA"));
184 // unfortunately, there is no way to return an error code to the user
193 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
195 DoSetItemClientData(n
, clientData
);
198 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
200 return (wxClientData
*)DoGetItemClientData(n
);
203 // ----------------------------------------------------------------------------
204 // wxOS2 specific helpers
205 // ----------------------------------------------------------------------------
207 void wxChoice::DoSetSize(int x
, int y
,
208 int width
, int height
,
211 // Ignore height parameter because height doesn't mean 'initially
212 // displayed' height, it refers to the drop-down menu as well. The
213 // wxWindows interpretation is different; also, getting the size returns
214 // the _displayed_ size (NOT the drop down menu size) so
215 // setting-getting-setting size would not work.
216 wxControl::DoSetSize(x
, y
, width
, -1, sizeFlags
);
219 wxSize
wxChoice::DoGetBestSize() const
221 // find the widest string
224 int nItems
= GetCount();
225 for ( int i
= 0; i
< nItems
; i
++ )
227 wxString
str(GetString(i
));
228 GetTextExtent(str
, &wLine
, NULL
);
229 if ( wLine
> wChoice
)
233 // give it some reasonable default value if there are no strings in the
238 // the combobox should be larger than the widest string
240 wxGetCharSize(GetHWND(), &cx
, &cy
, (wxFont
*)&GetFont());
244 // Choice drop-down list depends on number of items (limited to 10)
245 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
246 int hChoice
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*nStrings
;
248 return wxSize(wChoice
, hChoice
);
251 MRESULT
wxChoice::OS2WindowProc(HWND hwnd
, WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
255 if ( nMsg == WM_LBUTTONUP )
257 int x = (int)LOWORD(lParam);
258 int y = (int)HIWORD(lParam);
260 // Ok, this is truly weird, but if a panel with a wxChoice loses the
261 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
262 // y = 65535. Filter out this nonsense.
264 // VZ: I'd like to know how to reproduce this please...
265 if ( x == 65535 && y == 65535 )
269 return wxWindow::OS2WindowProc(hwnd
, nMsg
, wParam
, lParam
);
272 bool wxChoice::OS2Command(WXUINT param
, WXWORD
WXUNUSED(id
))
276 if ( param != CBN_SELCHANGE)
278 // "selection changed" is the only event we're after
282 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
283 event
.SetInt(GetSelection());
284 event
.SetEventObject(this);
285 // TODO: event.SetString(GetStringSelection());
286 ProcessCommand(event
);