1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to derive from wxChoiceBase
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "choice.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/choice.h"
37 #include "wx/msw/private.h"
39 #if !USE_SHARED_LIBRARY
40 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
43 // ============================================================================
45 // ============================================================================
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 bool wxChoice::Create(wxWindow
*parent
,
55 int n
, const wxString choices
[],
57 const wxValidator
& validator
,
60 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
63 long msStyle
= WS_CHILD
| CBS_DROPDOWNLIST
| WS_TABSTOP
| WS_VISIBLE
| WS_HSCROLL
| WS_VSCROLL
;
64 if ( style
& wxCB_SORT
)
67 // the experience shows that wxChoice vs. wxComboBox distinction confuses
68 // quite a few people - try to help them
69 wxASSERT_MSG( !(style
& wxCB_DROPDOWN
) &&
70 !(style
& wxCB_READONLY
) &&
71 !(style
& wxCB_SIMPLE
),
72 wxT("this style flag is ignored by wxChoice, you "
73 "probably want to use a wxComboBox") );
75 if ( !MSWCreateControl(wxT("COMBOBOX"), msStyle
) )
78 for ( int i
= 0; i
< n
; i
++ )
83 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
88 // ----------------------------------------------------------------------------
89 // adding/deleting items to/from the list
90 // ----------------------------------------------------------------------------
92 int wxChoice::DoAppend(const wxString
& item
)
94 int n
= (int)SendMessage(GetHwnd(), CB_ADDSTRING
, 0, (LONG
)item
.c_str());
97 wxLogLastError("SendMessage(CB_ADDSTRING)");
103 void wxChoice::Delete(int n
)
105 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
107 SendMessage(GetHwnd(), CB_DELETESTRING
, n
, 0);
110 void wxChoice::Clear()
112 SendMessage(GetHwnd(), CB_RESETCONTENT
, 0, 0);
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 int wxChoice::GetSelection() const
121 return (int)SendMessage(GetHwnd(), CB_GETCURSEL
, 0, 0);
124 void wxChoice::SetSelection(int n
)
126 SendMessage(GetHwnd(), CB_SETCURSEL
, n
, 0);
129 // ----------------------------------------------------------------------------
130 // string list functions
131 // ----------------------------------------------------------------------------
133 int wxChoice::GetCount() const
135 return (int)SendMessage(GetHwnd(), CB_GETCOUNT
, 0, 0);
138 int wxChoice::FindString(const wxString
& s
) const
140 #if defined(__WATCOMC__) && defined(__WIN386__)
141 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
142 // wxChoice::Do it the long way instead.
143 int count
= GetCount();
144 for ( int i
= 0; i
< count
; i
++ )
146 // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too
147 if ( GetString(i
).IsSameAs(s
, FALSE
) )
153 int pos
= (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT
,
154 (WPARAM
)-1, (LPARAM
)s
.c_str());
156 return pos
== LB_ERR
? wxNOT_FOUND
: pos
;
157 #endif // Watcom/!Watcom
160 wxString
wxChoice::GetString(int n
) const
162 size_t len
= (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN
, n
, 0);
165 if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT
, n
,
166 (LPARAM
)str
.GetWriteBuf(len
)) == CB_ERR
) {
167 wxLogLastError("SendMessage(CB_GETLBTEXT)");
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 void wxChoice::DoSetClientData( int n
, void* clientData
)
181 if ( SendMessage(GetHwnd(), CB_SETITEMDATA
, n
, (LPARAM
)clientData
) == CB_ERR
)
183 wxLogLastError(wxT("CB_SETITEMDATA"));
187 void* wxChoice::DoGetClientData( int n
) const
189 LPARAM rc
= SendMessage(GetHwnd(), CB_GETITEMDATA
, n
, 0);
192 wxLogLastError(wxT("CB_GETITEMDATA"));
194 // unfortunately, there is no way to return an error code to the user
201 void wxChoice::DoSetClientObject( int n
, wxClientData
* clientData
)
203 DoSetClientData(n
, clientData
);
206 wxClientData
* wxChoice::DoGetClientObject( int n
) const
208 return (wxClientData
*)DoGetClientData(n
);
211 // ----------------------------------------------------------------------------
212 // wxMSW specific helpers
213 // ----------------------------------------------------------------------------
215 void wxChoice::DoSetSize(int x
, int y
,
216 int width
, int height
,
219 // Ignore height parameter because height doesn't mean 'initially
220 // displayed' height, it refers to the drop-down menu as well. The
221 // wxWindows interpretation is different; also, getting the size returns
222 // the _displayed_ size (NOT the drop down menu size) so
223 // setting-getting-setting size would not work.
224 wxControl::DoSetSize(x
, y
, width
, -1, sizeFlags
);
227 wxSize
wxChoice::DoGetBestSize()
229 // find the widest string
232 int nItems
= GetCount();
233 for ( int i
= 0; i
< nItems
; i
++ )
235 wxString
str(GetString(i
));
236 GetTextExtent(str
, &wLine
, NULL
);
237 if ( wLine
> wChoice
)
241 // give it some reasonable default value if there are no strings in the
246 // the combobox should be larger than the widest string
248 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
252 // Choice drop-down list depends on number of items (limited to 10)
253 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
254 int hChoice
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*nStrings
;
256 return wxSize(wChoice
, hChoice
);
259 long wxChoice::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
261 if ( nMsg
== WM_LBUTTONUP
)
263 int x
= (int)LOWORD(lParam
);
264 int y
= (int)HIWORD(lParam
);
266 // Ok, this is truly weird, but if a panel with a wxChoice loses the
267 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
268 // y = 65535. Filter out this nonsense.
270 // VZ: I'd like to know how to reproduce this please...
271 if ( x
== 65535 && y
== 65535 )
275 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
278 bool wxChoice::MSWCommand(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 event
.SetString(GetStringSelection());
290 ProcessCommand(event
);