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 // ----------------------------------------------------------------------------
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
)
45 // ============================================================================
47 // ============================================================================
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 bool wxChoice::Create(wxWindow
*parent
,
57 int n
, const wxString choices
[],
59 const wxValidator
& validator
,
62 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
65 long msStyle
= WS_CHILD
| CBS_DROPDOWNLIST
| WS_TABSTOP
| WS_VISIBLE
| WS_HSCROLL
| WS_VSCROLL
/* | WS_CLIPSIBLINGS */;
66 if ( style
& wxCB_SORT
)
69 if ( style
& wxCLIP_SIBLINGS
)
70 msStyle
|= WS_CLIPSIBLINGS
;
73 // Experience shows that wxChoice vs. wxComboBox distinction confuses
74 // quite a few people - try to help them
75 wxASSERT_MSG( !(style
& wxCB_DROPDOWN
) &&
76 !(style
& wxCB_READONLY
) &&
77 !(style
& wxCB_SIMPLE
),
78 _T("this style flag is ignored by wxChoice, you ")
79 _T("probably want to use a wxComboBox") );
81 if ( !MSWCreateControl(wxT("COMBOBOX"), msStyle
) )
84 // A choice/combobox normally has a white background (or other, depending
85 // on global settings) rather than inheriting the parent's background colour.
86 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
88 for ( int i
= 0; i
< n
; i
++ )
93 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
103 // ----------------------------------------------------------------------------
104 // adding/deleting items to/from the list
105 // ----------------------------------------------------------------------------
107 int wxChoice::DoAppend(const wxString
& item
)
109 int n
= (int)SendMessage(GetHwnd(), CB_ADDSTRING
, 0, (LONG
)item
.c_str());
112 wxLogLastError(wxT("SendMessage(CB_ADDSTRING)"));
118 int wxChoice::DoInsert(const wxString
& item
, int pos
)
120 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
121 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
123 int n
= (int)SendMessage(GetHwnd(), CB_INSERTSTRING
, pos
, (LONG
)item
.c_str());
126 wxLogLastError(wxT("SendMessage(CB_INSERTSTRING)"));
132 void wxChoice::Delete(int n
)
134 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
136 if ( HasClientObjectData() )
138 delete GetClientObject(n
);
141 SendMessage(GetHwnd(), CB_DELETESTRING
, n
, 0);
144 void wxChoice::Clear()
148 SendMessage(GetHwnd(), CB_RESETCONTENT
, 0, 0);
151 void wxChoice::Free()
153 if ( HasClientObjectData() )
155 size_t count
= GetCount();
156 for ( size_t n
= 0; n
< count
; n
++ )
158 delete GetClientObject(n
);
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 int wxChoice::GetSelection() const
169 return (int)SendMessage(GetHwnd(), CB_GETCURSEL
, 0, 0);
172 void wxChoice::SetSelection(int n
)
174 SendMessage(GetHwnd(), CB_SETCURSEL
, n
, 0);
177 // ----------------------------------------------------------------------------
178 // string list functions
179 // ----------------------------------------------------------------------------
181 int wxChoice::GetCount() const
183 return (int)SendMessage(GetHwnd(), CB_GETCOUNT
, 0, 0);
186 int wxChoice::FindString(const wxString
& s
) const
188 #if defined(__WATCOMC__) && defined(__WIN386__)
189 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
190 // wxChoice::Do it the long way instead.
191 int count
= GetCount();
192 for ( int i
= 0; i
< count
; i
++ )
194 // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too
195 if ( GetString(i
).IsSameAs(s
, FALSE
) )
201 int pos
= (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT
,
202 (WPARAM
)-1, (LPARAM
)s
.c_str());
204 return pos
== LB_ERR
? wxNOT_FOUND
: pos
;
205 #endif // Watcom/!Watcom
208 void wxChoice::SetString(int n
, const wxString
& s
)
210 wxCHECK_RET( n
>= 0 && n
< GetCount(),
211 wxT("invalid item index in wxChoice::SetString") );
213 // we have to delete and add back the string as there is no way to change a
216 // we need to preserve the client data
218 if ( m_clientDataItemsType
!= wxClientData_None
)
220 data
= DoGetItemClientData(n
);
222 else // no client data
227 ::SendMessage(GetHwnd(), CB_DELETESTRING
, n
, 0);
228 ::SendMessage(GetHwnd(), CB_INSERTSTRING
, n
, (LPARAM
)s
.c_str() );
232 DoSetItemClientData(n
, data
);
234 //else: it's already NULL by default
237 wxString
wxChoice::GetString(int n
) const
239 int len
= (int)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN
, n
, 0);
242 if ( len
!= CB_ERR
&& len
> 0 )
249 (LPARAM
)(wxChar
*)wxStringBuffer(str
, len
)
252 wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)"));
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
265 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
267 if ( ::SendMessage(GetHwnd(), CB_SETITEMDATA
,
268 n
, (LPARAM
)clientData
) == CB_ERR
)
270 wxLogLastError(wxT("CB_SETITEMDATA"));
274 void* wxChoice::DoGetItemClientData( int n
) const
276 LPARAM rc
= SendMessage(GetHwnd(), CB_GETITEMDATA
, n
, 0);
279 wxLogLastError(wxT("CB_GETITEMDATA"));
281 // unfortunately, there is no way to return an error code to the user
288 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
290 DoSetItemClientData(n
, clientData
);
293 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
295 return (wxClientData
*)DoGetItemClientData(n
);
298 // ----------------------------------------------------------------------------
299 // wxMSW specific helpers
300 // ----------------------------------------------------------------------------
302 void wxChoice::DoMoveWindow(int x
, int y
, int width
, int height
)
304 // here is why this is necessary: if the width is negative, the combobox
305 // window proc makes the window of the size width*height instead of
306 // interpreting height in the usual manner (meaning the height of the drop
307 // down list - usually the height specified in the call to MoveWindow()
308 // will not change the height of combo box per se)
310 // this behaviour is not documented anywhere, but this is just how it is
311 // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without
312 // the check, constraints/sizers using combos may break the height
313 // constraint will have not at all the same value as expected
317 wxControl::DoMoveWindow(x
, y
, width
, height
);
320 void wxChoice::DoSetSize(int x
, int y
,
321 int width
, int WXUNUSED(height
),
324 // Ignore height parameter because height doesn't mean 'initially
325 // displayed' height, it refers to the drop-down menu as well. The
326 // wxWindows interpretation is different; also, getting the size returns
327 // the _displayed_ size (NOT the drop down menu size) so
328 // setting-getting-setting size would not work.
330 wxControl::DoSetSize(x
, y
, width
, -1, sizeFlags
);
333 wxSize
wxChoice::DoGetBestSize() const
335 // find the widest string
338 int nItems
= GetCount();
339 for ( int i
= 0; i
< nItems
; i
++ )
341 wxString
str(GetString(i
));
342 GetTextExtent(str
, &wLine
, NULL
);
343 if ( wLine
> wChoice
)
347 // give it some reasonable default value if there are no strings in the
352 // the combobox should be larger than the widest string
354 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
358 // Choice drop-down list depends on number of items (limited to 10)
359 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
360 int hChoice
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*nStrings
;
362 return wxSize(wChoice
, hChoice
);
365 long wxChoice::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
367 if ( nMsg
== WM_LBUTTONUP
)
369 int x
= (int)LOWORD(lParam
);
370 int y
= (int)HIWORD(lParam
);
372 // Ok, this is truly weird, but if a panel with a wxChoice loses the
373 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
374 // y = 65535. Filter out this nonsense.
376 // VZ: I'd like to know how to reproduce this please...
377 if ( x
== 65535 && y
== 65535 )
381 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
384 bool wxChoice::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
386 if ( param
!= CBN_SELCHANGE
)
388 // "selection changed" is the only event we're after
392 int n
= GetSelection();
395 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
397 event
.SetEventObject(this);
398 event
.SetString(GetStringSelection());
399 if ( HasClientObjectData() )
400 event
.SetClientObject( GetClientObject(n
) );
401 else if ( HasClientUntypedData() )
402 event
.SetClientData( GetClientData(n
) );
403 ProcessCommand(event
);
409 WXHBRUSH
wxChoice::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
410 WXUINT
WXUNUSED(message
),
411 WXWPARAM
WXUNUSED(wParam
),
412 WXLPARAM
WXUNUSED(lParam
)
416 wxColour colBack
= GetBackgroundColour();
419 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
421 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
422 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
424 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
426 return (WXHBRUSH
)brush
->GetResourceHandle();
429 #endif // wxUSE_CHOICE