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"
36 #include "wx/settings.h"
39 #include "wx/msw/private.h"
41 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
43 // ============================================================================
45 // ============================================================================
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 bool wxChoice::Create(wxWindow
*parent
,
55 int n
, const wxString choices
[],
57 const wxValidator
& validator
,
60 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
63 long msStyle
= WS_CHILD
| CBS_DROPDOWNLIST
| WS_TABSTOP
| WS_VISIBLE
| WS_HSCROLL
| WS_VSCROLL
/* | WS_CLIPSIBLINGS */;
64 if ( style
& wxCB_SORT
)
67 // Experience shows that wxChoice vs. wxComboBox distinction confuses
68 // quite a few people - try to help them
69 wxASSERT_MSG( !(style
& wxCB_DROPDOWN
) &&
70 !(style
& wxCB_READONLY
) &&
71 !(style
& wxCB_SIMPLE
),
72 _T("this style flag is ignored by wxChoice, you ")
73 _T("probably want to use a wxComboBox") );
75 if ( !MSWCreateControl(wxT("COMBOBOX"), msStyle
) )
78 // A choice/combobox normally has a white background (or other, depending
79 // on global settings) rather than inheriting the parent's background colour.
80 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
82 for ( int i
= 0; i
< n
; i
++ )
87 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
97 // ----------------------------------------------------------------------------
98 // adding/deleting items to/from the list
99 // ----------------------------------------------------------------------------
101 int wxChoice::DoAppend(const wxString
& item
)
103 int n
= (int)SendMessage(GetHwnd(), CB_ADDSTRING
, 0, (LONG
)item
.c_str());
106 wxLogLastError(wxT("SendMessage(CB_ADDSTRING)"));
112 void wxChoice::Delete(int n
)
114 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
116 if ( HasClientObjectData() )
118 delete GetClientObject(n
);
121 SendMessage(GetHwnd(), CB_DELETESTRING
, n
, 0);
124 void wxChoice::Clear()
128 SendMessage(GetHwnd(), CB_RESETCONTENT
, 0, 0);
131 void wxChoice::Free()
133 if ( HasClientObjectData() )
135 size_t count
= GetCount();
136 for ( size_t n
= 0; n
< count
; n
++ )
138 delete GetClientObject(n
);
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 int wxChoice::GetSelection() const
149 return (int)SendMessage(GetHwnd(), CB_GETCURSEL
, 0, 0);
152 void wxChoice::SetSelection(int n
)
154 SendMessage(GetHwnd(), CB_SETCURSEL
, n
, 0);
157 // ----------------------------------------------------------------------------
158 // string list functions
159 // ----------------------------------------------------------------------------
161 int wxChoice::GetCount() const
163 return (int)SendMessage(GetHwnd(), CB_GETCOUNT
, 0, 0);
166 int wxChoice::FindString(const wxString
& s
) const
168 #if defined(__WATCOMC__) && defined(__WIN386__)
169 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
170 // wxChoice::Do it the long way instead.
171 int count
= GetCount();
172 for ( int i
= 0; i
< count
; i
++ )
174 // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too
175 if ( GetString(i
).IsSameAs(s
, FALSE
) )
181 int pos
= (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT
,
182 (WPARAM
)-1, (LPARAM
)s
.c_str());
184 return pos
== LB_ERR
? wxNOT_FOUND
: pos
;
185 #endif // Watcom/!Watcom
188 void wxChoice::SetString(int n
, const wxString
& s
)
190 wxFAIL_MSG(wxT("not implemented"));
192 #if 0 // should do this, but no Insert() so far
198 wxString
wxChoice::GetString(int n
) const
200 size_t len
= (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN
, n
, 0);
203 if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT
, n
,
204 (LPARAM
)str
.GetWriteBuf(len
)) == CB_ERR
) {
205 wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)"));
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
219 if ( SendMessage(GetHwnd(), CB_SETITEMDATA
, n
, (LPARAM
)clientData
) == CB_ERR
)
221 wxLogLastError(wxT("CB_SETITEMDATA"));
225 void* wxChoice::DoGetItemClientData( int n
) const
227 LPARAM rc
= SendMessage(GetHwnd(), CB_GETITEMDATA
, n
, 0);
230 wxLogLastError(wxT("CB_GETITEMDATA"));
232 // unfortunately, there is no way to return an error code to the user
239 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
241 DoSetItemClientData(n
, clientData
);
244 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
246 return (wxClientData
*)DoGetItemClientData(n
);
249 // ----------------------------------------------------------------------------
250 // wxMSW specific helpers
251 // ----------------------------------------------------------------------------
253 void wxChoice::DoSetSize(int x
, int y
,
254 int width
, int height
,
257 // Ignore height parameter because height doesn't mean 'initially
258 // displayed' height, it refers to the drop-down menu as well. The
259 // wxWindows interpretation is different; also, getting the size returns
260 // the _displayed_ size (NOT the drop down menu size) so
261 // setting-getting-setting size would not work.
262 wxControl::DoSetSize(x
, y
, width
, -1, sizeFlags
);
265 wxSize
wxChoice::DoGetBestSize() const
267 // find the widest string
270 int nItems
= GetCount();
271 for ( int i
= 0; i
< nItems
; i
++ )
273 wxString
str(GetString(i
));
274 GetTextExtent(str
, &wLine
, NULL
);
275 if ( wLine
> wChoice
)
279 // give it some reasonable default value if there are no strings in the
284 // the combobox should be larger than the widest string
286 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
290 // Choice drop-down list depends on number of items (limited to 10)
291 size_t nStrings
= nItems
== 0 ? 10 : wxMin(10, nItems
) + 1;
292 int hChoice
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*nStrings
;
294 return wxSize(wChoice
, hChoice
);
297 long wxChoice::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
299 if ( nMsg
== WM_LBUTTONUP
)
301 int x
= (int)LOWORD(lParam
);
302 int y
= (int)HIWORD(lParam
);
304 // Ok, this is truly weird, but if a panel with a wxChoice loses the
305 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
306 // y = 65535. Filter out this nonsense.
308 // VZ: I'd like to know how to reproduce this please...
309 if ( x
== 65535 && y
== 65535 )
313 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
316 bool wxChoice::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
318 if ( param
!= CBN_SELCHANGE
)
320 // "selection changed" is the only event we're after
324 int n
= GetSelection();
327 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
329 event
.SetEventObject(this);
330 event
.SetString(GetStringSelection());
331 if ( HasClientObjectData() )
332 event
.SetClientObject( GetClientObject(n
) );
333 else if ( HasClientUntypedData() )
334 event
.SetClientData( GetClientData(n
) );
335 ProcessCommand(event
);
341 WXHBRUSH
wxChoice::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
349 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
350 return (WXHBRUSH
) hbrush
;
352 #endif // wxUSE_CTL3D
355 if (GetParent()->GetTransparentBackground())
356 SetBkMode(hdc
, TRANSPARENT
);
358 SetBkMode(hdc
, OPAQUE
);
360 wxColour colBack
= GetBackgroundColour();
363 colBack
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
365 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
366 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
368 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
370 return (WXHBRUSH
)brush
->GetResourceHandle();