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"
35 #include "wx/settings.h"
38 #include "wx/msw/private.h"
40 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
42 // ============================================================================
44 // ============================================================================
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 bool wxChoice::Create(wxWindow
*parent
,
54 int n
, const wxString choices
[],
56 const wxValidator
& validator
,
59 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
62 long msStyle
= WS_CHILD
| CBS_DROPDOWNLIST
| WS_TABSTOP
| WS_VISIBLE
| WS_HSCROLL
| WS_VSCROLL
;
63 if ( style
& wxCB_SORT
)
66 // the experience shows that wxChoice vs. wxComboBox distinction confuses
67 // quite a few people - try to help them
68 wxASSERT_MSG( !(style
& wxCB_DROPDOWN
) &&
69 !(style
& wxCB_READONLY
) &&
70 !(style
& wxCB_SIMPLE
),
71 wxT("this style flag is ignored by wxChoice, you "
72 "probably want to use a wxComboBox") );
74 if ( !MSWCreateControl(wxT("COMBOBOX"), msStyle
) )
77 // A choice/combobox normally has a white background (or other, depending
78 // on global settings) rather than inheriting the parent's background colour.
79 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
81 for ( int i
= 0; i
< n
; i
++ )
86 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
96 // ----------------------------------------------------------------------------
97 // adding/deleting items to/from the list
98 // ----------------------------------------------------------------------------
100 int wxChoice::DoAppend(const wxString
& item
)
102 int n
= (int)SendMessage(GetHwnd(), CB_ADDSTRING
, 0, (LONG
)item
.c_str());
105 wxLogLastError("SendMessage(CB_ADDSTRING)");
111 void wxChoice::Delete(int n
)
113 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
115 if ( HasClientObjectData() )
117 delete GetClientObject(n
);
120 SendMessage(GetHwnd(), CB_DELETESTRING
, n
, 0);
123 void wxChoice::Clear()
127 SendMessage(GetHwnd(), CB_RESETCONTENT
, 0, 0);
130 void wxChoice::Free()
132 if ( HasClientObjectData() )
134 size_t count
= GetCount();
135 for ( size_t n
= 0; n
< count
; n
++ )
137 delete GetClientObject(n
);
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 int wxChoice::GetSelection() const
148 return (int)SendMessage(GetHwnd(), CB_GETCURSEL
, 0, 0);
151 void wxChoice::SetSelection(int n
)
153 SendMessage(GetHwnd(), CB_SETCURSEL
, n
, 0);
156 // ----------------------------------------------------------------------------
157 // string list functions
158 // ----------------------------------------------------------------------------
160 int wxChoice::GetCount() const
162 return (int)SendMessage(GetHwnd(), CB_GETCOUNT
, 0, 0);
165 int wxChoice::FindString(const wxString
& s
) const
167 #if defined(__WATCOMC__) && defined(__WIN386__)
168 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
169 // wxChoice::Do it the long way instead.
170 int count
= GetCount();
171 for ( int i
= 0; i
< count
; i
++ )
173 // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too
174 if ( GetString(i
).IsSameAs(s
, FALSE
) )
180 int pos
= (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT
,
181 (WPARAM
)-1, (LPARAM
)s
.c_str());
183 return pos
== LB_ERR
? wxNOT_FOUND
: pos
;
184 #endif // Watcom/!Watcom
187 void wxChoice::SetString(int n
, const wxString
& s
)
189 wxFAIL_MSG(wxT("not implemented"));
191 #if 0 // should do this, but no Insert() so far
197 wxString
wxChoice::GetString(int n
) const
199 size_t len
= (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN
, n
, 0);
202 if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT
, n
,
203 (LPARAM
)str
.GetWriteBuf(len
)) == CB_ERR
) {
204 wxLogLastError("SendMessage(CB_GETLBTEXT)");
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
216 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
218 if ( SendMessage(GetHwnd(), CB_SETITEMDATA
, n
, (LPARAM
)clientData
) == CB_ERR
)
220 wxLogLastError(wxT("CB_SETITEMDATA"));
224 void* wxChoice::DoGetItemClientData( int n
) const
226 LPARAM rc
= SendMessage(GetHwnd(), CB_GETITEMDATA
, n
, 0);
229 wxLogLastError(wxT("CB_GETITEMDATA"));
231 // unfortunately, there is no way to return an error code to the user
238 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
240 DoSetItemClientData(n
, clientData
);
243 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
245 return (wxClientData
*)DoGetItemClientData(n
);
248 // ----------------------------------------------------------------------------
249 // wxMSW specific helpers
250 // ----------------------------------------------------------------------------
252 void wxChoice::DoSetSize(int x
, int y
,
253 int width
, int height
,
256 // Ignore height parameter because height doesn't mean 'initially
257 // displayed' height, it refers to the drop-down menu as well. The
258 // wxWindows interpretation is different; also, getting the size returns
259 // the _displayed_ size (NOT the drop down menu size) so
260 // setting-getting-setting size would not work.
261 wxControl::DoSetSize(x
, y
, width
, -1, sizeFlags
);
264 wxSize
wxChoice::DoGetBestSize() const
266 // find the widest string
269 int nItems
= GetCount();
270 for ( int i
= 0; i
< nItems
; i
++ )
272 wxString
str(GetString(i
));
273 GetTextExtent(str
, &wLine
, NULL
);
274 if ( wLine
> wChoice
)
278 // give it some reasonable default value if there are no strings in the
283 // the combobox should be larger than the widest string
285 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
289 // Choice drop-down list depends on number of items (limited to 10)
290 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
291 int hChoice
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*nStrings
;
293 return wxSize(wChoice
, hChoice
);
296 long wxChoice::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
298 if ( nMsg
== WM_LBUTTONUP
)
300 int x
= (int)LOWORD(lParam
);
301 int y
= (int)HIWORD(lParam
);
303 // Ok, this is truly weird, but if a panel with a wxChoice loses the
304 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
305 // y = 65535. Filter out this nonsense.
307 // VZ: I'd like to know how to reproduce this please...
308 if ( x
== 65535 && y
== 65535 )
312 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
315 bool wxChoice::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
317 if ( param
!= CBN_SELCHANGE
)
319 // "selection changed" is the only event we're after
323 int n
= GetSelection();
326 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
328 event
.SetEventObject(this);
329 event
.SetString(GetStringSelection());
330 if ( HasClientObjectData() )
331 event
.SetClientObject( GetClientObject(n
) );
332 else if ( HasClientUntypedData() )
333 event
.SetClientData( GetClientData(n
) );
334 ProcessCommand(event
);