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"
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 void wxChoice::Delete(int n
)
120 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
122 if ( HasClientObjectData() )
124 delete GetClientObject(n
);
127 SendMessage(GetHwnd(), CB_DELETESTRING
, n
, 0);
130 void wxChoice::Clear()
134 SendMessage(GetHwnd(), CB_RESETCONTENT
, 0, 0);
137 void wxChoice::Free()
139 if ( HasClientObjectData() )
141 size_t count
= GetCount();
142 for ( size_t n
= 0; n
< count
; n
++ )
144 delete GetClientObject(n
);
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 int wxChoice::GetSelection() const
155 return (int)SendMessage(GetHwnd(), CB_GETCURSEL
, 0, 0);
158 void wxChoice::SetSelection(int n
)
160 SendMessage(GetHwnd(), CB_SETCURSEL
, n
, 0);
163 // ----------------------------------------------------------------------------
164 // string list functions
165 // ----------------------------------------------------------------------------
167 int wxChoice::GetCount() const
169 return (int)SendMessage(GetHwnd(), CB_GETCOUNT
, 0, 0);
172 int wxChoice::FindString(const wxString
& s
) const
174 #if defined(__WATCOMC__) && defined(__WIN386__)
175 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
176 // wxChoice::Do it the long way instead.
177 int count
= GetCount();
178 for ( int i
= 0; i
< count
; i
++ )
180 // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too
181 if ( GetString(i
).IsSameAs(s
, FALSE
) )
187 int pos
= (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT
,
188 (WPARAM
)-1, (LPARAM
)s
.c_str());
190 return pos
== LB_ERR
? wxNOT_FOUND
: pos
;
191 #endif // Watcom/!Watcom
194 void wxChoice::SetString(int n
, const wxString
& s
)
196 wxCHECK_RET( (n
>=0)&&(n
<GetCount()), wxT("invalid item index in wxChoice::SetString") );
197 SendMessage(GetHwnd(), CB_DELETESTRING
, n
, 0);
198 SendMessage(GetHwnd(), CB_INSERTSTRING
, n
, (LONG
)s
.c_str() );
201 wxString
wxChoice::GetString(int n
) const
203 size_t len
= (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN
, n
, 0);
206 if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT
, n
,
207 (LPARAM
)str
.GetWriteBuf(len
)) == CB_ERR
) {
208 wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)"));
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
220 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
222 if ( SendMessage(GetHwnd(), CB_SETITEMDATA
, n
, (LPARAM
)clientData
) == CB_ERR
)
224 wxLogLastError(wxT("CB_SETITEMDATA"));
228 void* wxChoice::DoGetItemClientData( int n
) const
230 LPARAM rc
= SendMessage(GetHwnd(), CB_GETITEMDATA
, n
, 0);
233 wxLogLastError(wxT("CB_GETITEMDATA"));
235 // unfortunately, there is no way to return an error code to the user
242 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
244 DoSetItemClientData(n
, clientData
);
247 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
249 return (wxClientData
*)DoGetItemClientData(n
);
252 // ----------------------------------------------------------------------------
253 // wxMSW specific helpers
254 // ----------------------------------------------------------------------------
256 void wxChoice::DoMoveWindow(int x
, int y
, int width
, int height
)
258 // here is why this is necessary: if the width is negative, the combobox
259 // window proc makes the window of the size width*height instead of
260 // interpreting height in the usual manner (meaning the height of the drop
261 // down list - usually the height specified in the call to MoveWindow()
262 // will not change the height of combo box per se)
264 // this behaviour is not documented anywhere, but this is just how it is
265 // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without
266 // the check, constraints/sizers using combos may break the height
267 // constraint will have not at all the same value as expected
271 wxControl::DoMoveWindow(x
, y
, width
, height
);
274 void wxChoice::DoSetSize(int x
, int y
,
275 int width
, int WXUNUSED(height
),
278 // Ignore height parameter because height doesn't mean 'initially
279 // displayed' height, it refers to the drop-down menu as well. The
280 // wxWindows interpretation is different; also, getting the size returns
281 // the _displayed_ size (NOT the drop down menu size) so
282 // setting-getting-setting size would not work.
283 wxControl::DoSetSize(x
, y
, width
, -1, sizeFlags
);
286 wxSize
wxChoice::DoGetBestSize() const
288 // find the widest string
291 int nItems
= GetCount();
292 for ( int i
= 0; i
< nItems
; i
++ )
294 wxString
str(GetString(i
));
295 GetTextExtent(str
, &wLine
, NULL
);
296 if ( wLine
> wChoice
)
300 // give it some reasonable default value if there are no strings in the
305 // the combobox should be larger than the widest string
307 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
311 // Choice drop-down list depends on number of items (limited to 10)
312 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
313 int hChoice
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*nStrings
;
315 return wxSize(wChoice
, hChoice
);
318 long wxChoice::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
320 if ( nMsg
== WM_LBUTTONUP
)
322 int x
= (int)LOWORD(lParam
);
323 int y
= (int)HIWORD(lParam
);
325 // Ok, this is truly weird, but if a panel with a wxChoice loses the
326 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
327 // y = 65535. Filter out this nonsense.
329 // VZ: I'd like to know how to reproduce this please...
330 if ( x
== 65535 && y
== 65535 )
334 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
337 bool wxChoice::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
339 if ( param
!= CBN_SELCHANGE
)
341 // "selection changed" is the only event we're after
345 int n
= GetSelection();
348 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
350 event
.SetEventObject(this);
351 event
.SetString(GetStringSelection());
352 if ( HasClientObjectData() )
353 event
.SetClientObject( GetClientObject(n
) );
354 else if ( HasClientUntypedData() )
355 event
.SetClientData( GetClientData(n
) );
356 ProcessCommand(event
);
362 WXHBRUSH
wxChoice::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
368 WXUINT
WXUNUSED(message
),
369 WXWPARAM
WXUNUSED(wParam
),
370 WXLPARAM
WXUNUSED(lParam
)
377 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
378 return (WXHBRUSH
) hbrush
;
380 #endif // wxUSE_CTL3D
383 if (GetParent()->GetTransparentBackground())
384 SetBkMode(hdc
, TRANSPARENT
);
386 SetBkMode(hdc
, OPAQUE
);
388 wxColour colBack
= GetBackgroundColour();
391 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
393 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
394 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
396 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
398 return (WXHBRUSH
)brush
->GetResourceHandle();
401 #endif // wxUSE_CHOICE