1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "choice.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #include "wx/choice.h"
29 #include "wx/msw/private.h"
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
35 bool wxChoice::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
37 if (param
== CBN_SELCHANGE
)
39 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
40 event
.SetInt(GetSelection());
41 event
.SetEventObject(this);
42 event
.SetString(GetStringSelection());
43 ProcessCommand(event
);
51 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
54 int n
, const wxString choices
[],
56 const wxValidator
& validator
,
60 SetValidator(validator
);
61 if (parent
) parent
->AddChild(this);
62 SetBackgroundColour(parent
->GetBackgroundColour()) ;
63 SetForegroundColour(parent
->GetForegroundColour()) ;
66 m_windowStyle
= style
;
69 m_windowId
= (int)NewControlId();
78 long msStyle
= WS_CHILD
| CBS_DROPDOWNLIST
| WS_HSCROLL
| WS_VSCROLL
79 | WS_TABSTOP
| WS_VISIBLE
;
80 if (m_windowStyle
& wxCB_SORT
)
84 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
86 // Even with extended styles, need to combine with WS_BORDER
87 // for them to look right.
88 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
91 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, _T("COMBOBOX"), NULL
,
93 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
94 wxGetInstance(), NULL
);
96 wxCHECK_MSG( m_hWnd
, FALSE
, _T("Failed to create combobox") );
103 Ctl3dSubclassCtl(wx_combo); // Does CTL3D affect the combobox? I think not.
108 // Subclass again for purposes of dialog editing mode
111 SetFont(parent
->GetFont());
114 for (i
= 0; i
< n
; i
++)
120 SetSize(x
, y
, width
, height
);
125 void wxChoice::Append(const wxString
& item
)
127 SendMessage(GetHwnd(), CB_ADDSTRING
, 0, (LONG
)(const wxChar
*)item
);
132 void wxChoice::Delete(int n
)
134 m_noStrings
= (int)SendMessage(GetHwnd(), CB_DELETESTRING
, n
, 0);
137 void wxChoice::Clear(void)
139 SendMessage(GetHwnd(), CB_RESETCONTENT
, 0, 0);
145 int wxChoice::GetSelection(void) const
147 return (int)SendMessage(GetHwnd(), CB_GETCURSEL
, 0, 0);
150 void wxChoice::SetSelection(int n
)
152 SendMessage(GetHwnd(), CB_SETCURSEL
, n
, 0);
155 int wxChoice::FindString(const wxString
& s
) const
157 #if defined(__WATCOMC__) && defined(__WIN386__)
158 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
159 // Do it the long way instead.
161 for (int i
= 0; i
< Number(); i
++)
163 int len
= (int)SendMessage(GetHwnd(), CB_GETLBTEXT
, i
, (LPARAM
)(LPSTR
)buf
);
165 if (strcmp(buf
, (const char *)s
) == 0)
170 int pos
= (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT
, (WPARAM
)-1, (LPARAM
)(LPSTR
)(const wxChar
*)s
);
178 wxString
wxChoice::GetString(int n
) const
180 size_t len
= (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN
, n
, 0);
182 if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT
, n
,
183 (LPARAM
)str
.GetWriteBuf(len
)) == CB_ERR
)
185 wxLogLastError("SendMessage(CB_GETLBTEXT)");
193 void wxChoice::DoSetSize(int x
, int y
,
194 int width
, int height
,
197 // Ignore height parameter because height doesn't mean 'initially
198 // displayed' height, it refers to the drop-down menu as well. The
199 // wxWindows interpretation is different; also, getting the size returns
200 // the _displayed_ size (NOT the drop down menu size) so
201 // setting-getting-setting size would not work.
202 wxControl::DoSetSize(x
, y
, width
, -1, sizeFlags
);
205 wxSize
wxChoice::DoGetBestSize()
207 // find the widest string
210 for ( int i
= 0; i
< m_noStrings
; i
++ )
212 wxString
str(GetString(i
));
213 GetTextExtent(str
, &wLine
, NULL
);
214 if ( wLine
> wChoice
)
218 // give it some reasonable default value if there are no strings in the
223 // the combobox should be larger than the widest string
225 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
229 // Choice drop-down list depends on number of items (limited to 10)
230 size_t nStrings
= m_noStrings
== 0 ? 10 : wxMin(10, m_noStrings
) + 1;
231 int hChoice
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*nStrings
;
233 return wxSize(wChoice
, hChoice
);
236 WXHBRUSH
wxChoice::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
237 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
242 long wxChoice::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
249 if (GetWindowStyleFlag() & wxPROCESS_ENTER)
250 return DLGC_WANTALLKEYS;
255 case WM_CHAR: // Always an ASCII character
257 if (wParam == VK_RETURN)
259 wxCommandEvent event(wxEVENT_TYPE_TEXT_ENTER_COMMAND);
260 event.commandString = ((wxTextCtrl *)item)->GetValue();
261 event.eventObject = item;
262 item->ProcessCommand(event);
270 int x
= (int)LOWORD(lParam
);
271 int y
= (int)HIWORD(lParam
);
273 // Ok, this is truly weird, but if a panel with a wxChoice loses the
274 // focus, then you get a *fake* WM_LBUTTONUP message
275 // with x = 65535 and y = 65535.
276 // Filter out this nonsense.
277 if (x
== 65535 && y
== 65535)
283 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
286 wxString
wxChoice::GetStringSelection (void) const
288 int sel
= GetSelection ();
290 return wxString(this->GetString (sel
));
292 return wxString(_T(""));
295 bool wxChoice::SetStringSelection (const wxString
& s
)
297 int sel
= FindString (s
);
307 void wxChoice::Command(wxCommandEvent
& event
)
309 SetSelection (event
.GetInt());
310 ProcessCommand (event
);