1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to derive from wxChoiceBase
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "choice.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/choice.h"
38 #include "wx/settings.h"
41 #include "wx/msw/private.h"
43 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
52 // ============================================================================
54 // ============================================================================
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 bool wxChoice::Create(wxWindow
*parent
,
64 int n
, const wxString choices
[],
66 const wxValidator
& validator
,
69 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
72 long msStyle
= WS_CHILD
| CBS_DROPDOWNLIST
| WS_TABSTOP
| WS_VISIBLE
| WS_HSCROLL
| WS_VSCROLL
/* | WS_CLIPSIBLINGS */;
73 if ( style
& wxCB_SORT
)
76 if ( style
& wxCLIP_SIBLINGS
)
77 msStyle
|= WS_CLIPSIBLINGS
;
80 // Experience shows that wxChoice vs. wxComboBox distinction confuses
81 // quite a few people - try to help them
82 wxASSERT_MSG( !(style
& wxCB_DROPDOWN
) &&
83 !(style
& wxCB_READONLY
) &&
84 !(style
& wxCB_SIMPLE
),
85 _T("this style flag is ignored by wxChoice, you ")
86 _T("probably want to use a wxComboBox") );
88 if ( !MSWCreateControl(wxT("COMBOBOX"), msStyle
) )
91 // A choice/combobox normally has a white background (or other, depending
92 // on global settings) rather than inheriting the parent's background colour.
93 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
95 for ( int i
= 0; i
< n
; i
++ )
100 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
105 wxChoice::~wxChoice()
110 // ----------------------------------------------------------------------------
111 // adding/deleting items to/from the list
112 // ----------------------------------------------------------------------------
114 int wxChoice::DoAppend(const wxString
& item
)
116 int n
= (int)SendMessage(GetHwnd(), CB_ADDSTRING
, 0, (LONG
)item
.c_str());
119 wxLogLastError(wxT("SendMessage(CB_ADDSTRING)"));
125 int wxChoice::DoInsert(const wxString
& item
, int pos
)
127 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
128 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
130 int n
= (int)SendMessage(GetHwnd(), CB_INSERTSTRING
, pos
, (LONG
)item
.c_str());
133 wxLogLastError(wxT("SendMessage(CB_INSERTSTRING)"));
139 void wxChoice::Delete(int n
)
141 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
143 if ( HasClientObjectData() )
145 delete GetClientObject(n
);
148 SendMessage(GetHwnd(), CB_DELETESTRING
, n
, 0);
151 void wxChoice::Clear()
155 SendMessage(GetHwnd(), CB_RESETCONTENT
, 0, 0);
158 void wxChoice::Free()
160 if ( HasClientObjectData() )
162 size_t count
= GetCount();
163 for ( size_t n
= 0; n
< count
; n
++ )
165 delete GetClientObject(n
);
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 int wxChoice::GetSelection() const
176 return (int)SendMessage(GetHwnd(), CB_GETCURSEL
, 0, 0);
179 void wxChoice::SetSelection(int n
)
181 SendMessage(GetHwnd(), CB_SETCURSEL
, n
, 0);
184 // ----------------------------------------------------------------------------
185 // string list functions
186 // ----------------------------------------------------------------------------
188 int wxChoice::GetCount() const
190 return (int)SendMessage(GetHwnd(), CB_GETCOUNT
, 0, 0);
193 int wxChoice::FindString(const wxString
& s
) const
195 #if defined(__WATCOMC__) && defined(__WIN386__)
196 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
197 // wxChoice::Do it the long way instead.
198 int count
= GetCount();
199 for ( int i
= 0; i
< count
; i
++ )
201 // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too
202 if ( GetString(i
).IsSameAs(s
, FALSE
) )
208 int pos
= (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT
,
209 (WPARAM
)-1, (LPARAM
)s
.c_str());
211 return pos
== LB_ERR
? wxNOT_FOUND
: pos
;
212 #endif // Watcom/!Watcom
215 void wxChoice::SetString(int n
, const wxString
& s
)
217 wxCHECK_RET( n
>= 0 && n
< GetCount(),
218 wxT("invalid item index in wxChoice::SetString") );
220 // we have to delete and add back the string as there is no way to change a
223 // we need to preserve the client data
225 if ( m_clientDataItemsType
!= wxClientData_None
)
227 data
= DoGetItemClientData(n
);
229 else // no client data
234 ::SendMessage(GetHwnd(), CB_DELETESTRING
, n
, 0);
235 ::SendMessage(GetHwnd(), CB_INSERTSTRING
, n
, (LPARAM
)s
.c_str() );
239 DoSetItemClientData(n
, data
);
241 //else: it's already NULL by default
244 wxString
wxChoice::GetString(int n
) const
246 int len
= (int)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN
, n
, 0);
249 if ( len
!= CB_ERR
&& len
> 0 )
256 (LPARAM
)(wxChar
*)wxStringBuffer(str
, len
)
259 wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)"));
266 // ----------------------------------------------------------------------------
268 // ----------------------------------------------------------------------------
270 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
272 if ( ::SendMessage(GetHwnd(), CB_SETITEMDATA
,
273 n
, (LPARAM
)clientData
) == CB_ERR
)
275 wxLogLastError(wxT("CB_SETITEMDATA"));
279 void* wxChoice::DoGetItemClientData( int n
) const
281 LPARAM rc
= SendMessage(GetHwnd(), CB_GETITEMDATA
, n
, 0);
284 wxLogLastError(wxT("CB_GETITEMDATA"));
286 // unfortunately, there is no way to return an error code to the user
293 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
295 DoSetItemClientData(n
, clientData
);
298 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
300 return (wxClientData
*)DoGetItemClientData(n
);
303 // ----------------------------------------------------------------------------
304 // wxMSW specific helpers
305 // ----------------------------------------------------------------------------
307 void wxChoice::DoMoveWindow(int x
, int y
, int width
, int height
)
309 // here is why this is necessary: if the width is negative, the combobox
310 // window proc makes the window of the size width*height instead of
311 // interpreting height in the usual manner (meaning the height of the drop
312 // down list - usually the height specified in the call to MoveWindow()
313 // will not change the height of combo box per se)
315 // this behaviour is not documented anywhere, but this is just how it is
316 // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without
317 // the check, constraints/sizers using combos may break the height
318 // constraint will have not at all the same value as expected
322 wxControl::DoMoveWindow(x
, y
, width
, height
);
325 void wxChoice::DoSetSize(int x
, int y
,
326 int width
, int WXUNUSED(height
),
329 // Ignore height parameter because height doesn't mean 'initially
330 // displayed' height, it refers to the drop-down menu as well. The
331 // wxWindows interpretation is different; also, getting the size returns
332 // the _displayed_ size (NOT the drop down menu size) so
333 // setting-getting-setting size would not work.
335 wxControl::DoSetSize(x
, y
, width
, -1, sizeFlags
);
338 wxSize
wxChoice::DoGetBestSize() const
340 // find the widest string
343 int nItems
= GetCount();
344 for ( int i
= 0; i
< nItems
; i
++ )
346 wxString
str(GetString(i
));
347 GetTextExtent(str
, &wLine
, NULL
);
348 if ( wLine
> wChoice
)
352 // give it some reasonable default value if there are no strings in the
357 // the combobox should be larger than the widest string
359 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
363 // Choice drop-down list depends on number of items (limited to 10)
364 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
365 int hChoice
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*nStrings
;
367 return wxSize(wChoice
, hChoice
);
370 long wxChoice::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
372 if ( nMsg
== WM_LBUTTONUP
)
374 int x
= (int)LOWORD(lParam
);
375 int y
= (int)HIWORD(lParam
);
377 // Ok, this is truly weird, but if a panel with a wxChoice loses the
378 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
379 // y = 65535. Filter out this nonsense.
381 // VZ: I'd like to know how to reproduce this please...
382 if ( x
== 65535 && y
== 65535 )
386 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
389 bool wxChoice::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
391 if ( param
!= CBN_SELCHANGE
)
393 // "selection changed" is the only event we're after
397 int n
= GetSelection();
400 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
402 event
.SetEventObject(this);
403 event
.SetString(GetStringSelection());
404 if ( HasClientObjectData() )
405 event
.SetClientObject( GetClientObject(n
) );
406 else if ( HasClientUntypedData() )
407 event
.SetClientData( GetClientData(n
) );
408 ProcessCommand(event
);
414 WXHBRUSH
wxChoice::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
415 WXUINT
WXUNUSED(message
),
416 WXWPARAM
WXUNUSED(wParam
),
417 WXLPARAM
WXUNUSED(lParam
)
421 wxColour colBack
= GetBackgroundColour();
424 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
426 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
427 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
429 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
431 return (WXHBRUSH
)brush
->GetResourceHandle();
434 #endif // wxUSE_CHOICE