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 # if defined(__VISAGECPP__)
35 const wxValidator
* validator
,
37 const wxValidator
& validator
,
42 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
46 long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_TABSTOP | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
47 if ( style & wxCB_SORT )
50 // the experience shows that wxChoice vs. wxComboBox distinction confuses
51 // quite a few people - try to help them
52 wxASSERT_MSG( !(style & wxCB_DROPDOWN) &&
53 !(style & wxCB_READONLY) &&
54 !(style & wxCB_SIMPLE),
55 wxT("this style flag is ignored by wxChoice, you "
56 "probably want to use a wxComboBox") );
58 if ( !OS2CreateControl(wxT("COMBOBOX"), msStyle) )
61 for ( int i = 0; i < n; i++ )
66 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
71 // ----------------------------------------------------------------------------
72 // adding/deleting items to/from the list
73 // ----------------------------------------------------------------------------
75 int wxChoice::DoAppend(const wxString
& item
)
79 int n = (int)SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LONG)item.c_str());
82 wxLogLastError("SendMessage(CB_ADDSTRING)");
88 void wxChoice::Delete(int n
)
90 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
92 // TODO: SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
95 void wxChoice::Clear()
97 // TODO: SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 int wxChoice::GetSelection() const
106 // TODO: return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
110 void wxChoice::SetSelection(int n
)
112 // TODO: SendMessage(GetHwnd(), CB_SETCURSEL, n, 0);
115 // ----------------------------------------------------------------------------
116 // string list functions
117 // ----------------------------------------------------------------------------
119 int wxChoice::GetCount() const
121 // TODO: return (int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0);
125 int wxChoice::FindString(const wxString
& s
) const
129 int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT,
130 (WPARAM)-1, (LPARAM)s.c_str());
132 return pos == LB_ERR ? wxNOT_FOUND : pos;
137 void wxChoice::SetString(int n
, const wxString
& s
)
139 wxFAIL_MSG(wxT("not implemented"));
141 #if 0 // should do this, but no Insert() so far
147 wxString
wxChoice::GetString(int n
) const
149 size_t len
= 0; // TODO: (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
154 if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT, n,
155 (LPARAM)str.GetWriteBuf(len)) == CB_ERR ) {
156 wxLogLastError("SendMessage(CB_GETLBTEXT)");
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
168 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
172 if ( SendMessage(GetHwnd(), CB_SETITEMDATA, n, (LPARAM)clientData) == CB_ERR )
174 wxLogLastError(wxT("CB_SETITEMDATA"));
179 void* wxChoice::DoGetItemClientData( int n
) const
183 LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0);
186 wxLogLastError(wxT("CB_GETITEMDATA"));
188 // unfortunately, there is no way to return an error code to the user
197 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
199 DoSetItemClientData(n
, clientData
);
202 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
204 return (wxClientData
*)DoGetItemClientData(n
);
207 // ----------------------------------------------------------------------------
208 // wxOS2 specific helpers
209 // ----------------------------------------------------------------------------
211 void wxChoice::DoSetSize(int x
, int y
,
212 int width
, int height
,
215 // Ignore height parameter because height doesn't mean 'initially
216 // displayed' height, it refers to the drop-down menu as well. The
217 // wxWindows interpretation is different; also, getting the size returns
218 // the _displayed_ size (NOT the drop down menu size) so
219 // setting-getting-setting size would not work.
220 wxControl::DoSetSize(x
, y
, width
, -1, sizeFlags
);
223 wxSize
wxChoice::DoGetBestSize()
225 // find the widest string
228 int nItems
= GetCount();
229 for ( int i
= 0; i
< nItems
; i
++ )
231 wxString
str(GetString(i
));
232 GetTextExtent(str
, &wLine
, NULL
);
233 if ( wLine
> wChoice
)
237 // give it some reasonable default value if there are no strings in the
242 // the combobox should be larger than the widest string
244 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
248 // Choice drop-down list depends on number of items (limited to 10)
249 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
250 int hChoice
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*nStrings
;
252 return wxSize(wChoice
, hChoice
);
255 MRESULT
wxChoice::OS2WindowProc(HWND hwnd
, WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
259 if ( nMsg == WM_LBUTTONUP )
261 int x = (int)LOWORD(lParam);
262 int y = (int)HIWORD(lParam);
264 // Ok, this is truly weird, but if a panel with a wxChoice loses the
265 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
266 // y = 65535. Filter out this nonsense.
268 // VZ: I'd like to know how to reproduce this please...
269 if ( x == 65535 && y == 65535 )
273 return wxWindow::OS2WindowProc(hwnd
, nMsg
, wParam
, lParam
);
276 bool wxChoice::OS2Command(WXUINT param
, WXWORD
WXUNUSED(id
))
280 if ( param != CBN_SELCHANGE)
282 // "selection changed" is the only event we're after
286 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
287 event
.SetInt(GetSelection());
288 event
.SetEventObject(this);
289 // TODO: event.SetString(GetStringSelection());
290 ProcessCommand(event
);