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 #if wxUSE_EXTENDED_RTTI
44 IMPLEMENT_DYNAMIC_CLASS_XTI(wxChoice
, wxControl
,"wx/checkbox.h")
46 WX_BEGIN_PROPERTIES_TABLE(wxChoice
)
48 WX_PROPERTY( Font
, wxFont
, SetFont
, GetFont
, )
49 WX_PROPERTY_COLLECTION( Choices
, wxArrayString
, wxString
, AppendString
, GetStrings
)
50 WX_PROPERTY( Selection
,int, SetSelection
, GetSelection
, )
51 WX_END_PROPERTIES_TABLE()
53 WX_BEGIN_HANDLERS_TABLE(wxChoice
)
54 WX_END_HANDLERS_TABLE()
56 WX_CONSTRUCTOR_4( wxChoice
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
)
58 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
67 // ============================================================================
69 // ============================================================================
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 bool wxChoice::Create(wxWindow
*parent
,
79 int n
, const wxString choices
[],
81 const wxValidator
& validator
,
84 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
87 long msStyle
= WS_CHILD
| CBS_DROPDOWNLIST
| WS_TABSTOP
| WS_VISIBLE
| WS_HSCROLL
| WS_VSCROLL
/* | WS_CLIPSIBLINGS */;
88 if ( style
& wxCB_SORT
)
91 if ( style
& wxCLIP_SIBLINGS
)
92 msStyle
|= WS_CLIPSIBLINGS
;
95 // Experience shows that wxChoice vs. wxComboBox distinction confuses
96 // quite a few people - try to help them
97 wxASSERT_MSG( !(style
& wxCB_DROPDOWN
) &&
98 !(style
& wxCB_READONLY
) &&
99 !(style
& wxCB_SIMPLE
),
100 _T("this style flag is ignored by wxChoice, you ")
101 _T("probably want to use a wxComboBox") );
103 if ( !MSWCreateControl(wxT("COMBOBOX"), msStyle
) )
106 // A choice/combobox normally has a white background (or other, depending
107 // on global settings) rather than inheriting the parent's background colour.
108 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
110 for ( int i
= 0; i
< n
; i
++ )
115 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
120 wxChoice::~wxChoice()
125 // ----------------------------------------------------------------------------
126 // adding/deleting items to/from the list
127 // ----------------------------------------------------------------------------
129 int wxChoice::DoAppend(const wxString
& item
)
131 int n
= (int)SendMessage(GetHwnd(), CB_ADDSTRING
, 0, (LONG
)item
.c_str());
134 wxLogLastError(wxT("SendMessage(CB_ADDSTRING)"));
140 int wxChoice::DoInsert(const wxString
& item
, int pos
)
142 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
143 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
145 int n
= (int)SendMessage(GetHwnd(), CB_INSERTSTRING
, pos
, (LONG
)item
.c_str());
148 wxLogLastError(wxT("SendMessage(CB_INSERTSTRING)"));
154 void wxChoice::Delete(int n
)
156 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
158 if ( HasClientObjectData() )
160 delete GetClientObject(n
);
163 SendMessage(GetHwnd(), CB_DELETESTRING
, n
, 0);
166 void wxChoice::Clear()
170 SendMessage(GetHwnd(), CB_RESETCONTENT
, 0, 0);
173 void wxChoice::Free()
175 if ( HasClientObjectData() )
177 size_t count
= GetCount();
178 for ( size_t n
= 0; n
< count
; n
++ )
180 delete GetClientObject(n
);
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 int wxChoice::GetSelection() const
191 return (int)SendMessage(GetHwnd(), CB_GETCURSEL
, 0, 0);
194 void wxChoice::SetSelection(int n
)
196 SendMessage(GetHwnd(), CB_SETCURSEL
, n
, 0);
199 // ----------------------------------------------------------------------------
200 // string list functions
201 // ----------------------------------------------------------------------------
203 int wxChoice::GetCount() const
205 return (int)SendMessage(GetHwnd(), CB_GETCOUNT
, 0, 0);
208 int wxChoice::FindString(const wxString
& s
) const
210 #if defined(__WATCOMC__) && defined(__WIN386__)
211 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
212 // wxChoice::Do it the long way instead.
213 int count
= GetCount();
214 for ( int i
= 0; i
< count
; i
++ )
216 // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too
217 if ( GetString(i
).IsSameAs(s
, FALSE
) )
223 int pos
= (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT
,
224 (WPARAM
)-1, (LPARAM
)s
.c_str());
226 return pos
== LB_ERR
? wxNOT_FOUND
: pos
;
227 #endif // Watcom/!Watcom
230 void wxChoice::SetString(int n
, const wxString
& s
)
232 wxCHECK_RET( n
>= 0 && n
< GetCount(),
233 wxT("invalid item index in wxChoice::SetString") );
235 // we have to delete and add back the string as there is no way to change a
238 // we need to preserve the client data
240 if ( m_clientDataItemsType
!= wxClientData_None
)
242 data
= DoGetItemClientData(n
);
244 else // no client data
249 ::SendMessage(GetHwnd(), CB_DELETESTRING
, n
, 0);
250 ::SendMessage(GetHwnd(), CB_INSERTSTRING
, n
, (LPARAM
)s
.c_str() );
254 DoSetItemClientData(n
, data
);
256 //else: it's already NULL by default
259 wxString
wxChoice::GetString(int n
) const
261 int len
= (int)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN
, n
, 0);
264 if ( len
!= CB_ERR
&& len
> 0 )
271 (LPARAM
)(wxChar
*)wxStringBuffer(str
, len
)
274 wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)"));
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
287 if ( ::SendMessage(GetHwnd(), CB_SETITEMDATA
,
288 n
, (LPARAM
)clientData
) == CB_ERR
)
290 wxLogLastError(wxT("CB_SETITEMDATA"));
294 void* wxChoice::DoGetItemClientData( int n
) const
296 LPARAM rc
= SendMessage(GetHwnd(), CB_GETITEMDATA
, n
, 0);
299 wxLogLastError(wxT("CB_GETITEMDATA"));
301 // unfortunately, there is no way to return an error code to the user
308 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
310 DoSetItemClientData(n
, clientData
);
313 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
315 return (wxClientData
*)DoGetItemClientData(n
);
318 // ----------------------------------------------------------------------------
319 // wxMSW specific helpers
320 // ----------------------------------------------------------------------------
322 void wxChoice::DoMoveWindow(int x
, int y
, int width
, int height
)
324 // here is why this is necessary: if the width is negative, the combobox
325 // window proc makes the window of the size width*height instead of
326 // interpreting height in the usual manner (meaning the height of the drop
327 // down list - usually the height specified in the call to MoveWindow()
328 // will not change the height of combo box per se)
330 // this behaviour is not documented anywhere, but this is just how it is
331 // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without
332 // the check, constraints/sizers using combos may break the height
333 // constraint will have not at all the same value as expected
337 wxControl::DoMoveWindow(x
, y
, width
, height
);
340 void wxChoice::DoSetSize(int x
, int y
,
341 int width
, int WXUNUSED(height
),
344 // Ignore height parameter because height doesn't mean 'initially
345 // displayed' height, it refers to the drop-down menu as well. The
346 // wxWindows interpretation is different; also, getting the size returns
347 // the _displayed_ size (NOT the drop down menu size) so
348 // setting-getting-setting size would not work.
350 wxControl::DoSetSize(x
, y
, width
, -1, sizeFlags
);
353 wxSize
wxChoice::DoGetBestSize() const
355 // find the widest string
358 int nItems
= GetCount();
359 for ( int i
= 0; i
< nItems
; i
++ )
361 wxString
str(GetString(i
));
362 GetTextExtent(str
, &wLine
, NULL
);
363 if ( wLine
> wChoice
)
367 // give it some reasonable default value if there are no strings in the
372 // the combobox should be larger than the widest string
374 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
378 // Choice drop-down list depends on number of items (limited to 10)
379 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
380 int hChoice
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*nStrings
;
382 return wxSize(wChoice
, hChoice
);
385 long wxChoice::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
387 if ( nMsg
== WM_LBUTTONUP
)
389 int x
= (int)LOWORD(lParam
);
390 int y
= (int)HIWORD(lParam
);
392 // Ok, this is truly weird, but if a panel with a wxChoice loses the
393 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
394 // y = 65535. Filter out this nonsense.
396 // VZ: I'd like to know how to reproduce this please...
397 if ( x
== 65535 && y
== 65535 )
401 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
404 bool wxChoice::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
406 if ( param
!= CBN_SELCHANGE
)
408 // "selection changed" is the only event we're after
412 int n
= GetSelection();
415 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
417 event
.SetEventObject(this);
418 event
.SetString(GetStringSelection());
419 if ( HasClientObjectData() )
420 event
.SetClientObject( GetClientObject(n
) );
421 else if ( HasClientUntypedData() )
422 event
.SetClientData( GetClientData(n
) );
423 ProcessCommand(event
);
429 WXHBRUSH
wxChoice::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
430 WXUINT
WXUNUSED(message
),
431 WXWPARAM
WXUNUSED(wParam
),
432 WXLPARAM
WXUNUSED(lParam
)
436 wxColour colBack
= GetBackgroundColour();
439 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
441 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
442 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
444 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
446 return (WXHBRUSH
)brush
->GetResourceHandle();
449 #endif // wxUSE_CHOICE