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"
28 #include "wx/msw/private.h"
30 #if !USE_SHARED_LIBRARY
31 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
34 bool wxChoice::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
36 if (param
== CBN_SELCHANGE
)
38 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
39 event
.SetInt(GetSelection());
40 event
.SetEventObject(this);
41 event
.SetString(copystring(GetStringSelection()));
42 ProcessCommand(event
);
43 delete[] event
.GetString();
49 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
52 int n
, const wxString choices
[],
54 const wxValidator
& validator
,
58 SetValidator(validator
);
59 if (parent
) parent
->AddChild(this);
60 SetBackgroundColour(parent
->GetBackgroundColour()) ;
61 SetForegroundColour(parent
->GetForegroundColour()) ;
64 m_windowStyle
= style
;
67 m_windowId
= (int)NewControlId();
76 long msStyle
= WS_CHILD
| CBS_DROPDOWNLIST
| WS_HSCROLL
| WS_VSCROLL
77 | WS_TABSTOP
| WS_VISIBLE
;
78 if (m_windowStyle
& wxCB_SORT
)
82 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
84 // Even with extended styles, need to combine with WS_BORDER
85 // for them to look right.
86 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
89 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, "COMBOBOX", NULL
,
91 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
92 wxGetInstance(), NULL
);
94 wxCHECK_MSG( m_hWnd
, FALSE
, "Failed to create combobox" );
101 Ctl3dSubclassCtl(wx_combo); // Does CTL3D affect the combobox? I think not.
106 // Subclass again for purposes of dialog editing mode
109 SetFont(parent
->GetFont());
112 for (i
= 0; i
< n
; i
++)
118 SetSize(x
, y
, width
, height
);
123 void wxChoice::Append(const wxString
& item
)
125 SendMessage((HWND
) GetHWND(), CB_ADDSTRING
, 0, (LONG
)(const char *)item
);
130 void wxChoice::Delete(int n
)
132 m_noStrings
= (int)SendMessage((HWND
) GetHWND(), CB_DELETESTRING
, n
, 0);
135 void wxChoice::Clear(void)
137 SendMessage((HWND
) GetHWND(), CB_RESETCONTENT
, 0, 0);
143 int wxChoice::GetSelection(void) const
145 return (int)SendMessage((HWND
) GetHWND(), CB_GETCURSEL
, 0, 0);
148 void wxChoice::SetSelection(int n
)
150 SendMessage((HWND
) GetHWND(), CB_SETCURSEL
, n
, 0);
153 int wxChoice::FindString(const wxString
& s
) const
155 #if defined(__WATCOMC__) && defined(__WIN386__)
156 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
157 // Do it the long way instead.
159 for (int i
= 0; i
< Number(); i
++)
161 int len
= (int)SendMessage((HWND
) GetHWND(), CB_GETLBTEXT
, i
, (LPARAM
)(LPSTR
)buf
);
163 if (strcmp(buf
, (const char *)s
) == 0)
168 int pos
= (int)SendMessage((HWND
) GetHWND(), CB_FINDSTRINGEXACT
, (WPARAM
)-1, (LPARAM
)(LPSTR
)(const char *)s
);
176 wxString
wxChoice::GetString(int n
) const
178 int len
= (int)SendMessage((HWND
) GetHWND(), CB_GETLBTEXT
, n
, (long)wxBuffer
);
180 return wxString(wxBuffer
);
183 void wxChoice::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
185 int currentX
, currentY
;
186 GetPosition(¤tX
, ¤tY
);
193 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
195 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
198 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
200 // If we're prepared to use the existing size, then...
201 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
206 int cx
; // button font dimensions
208 wxGetCharSize(GetHWND(), &cx
, &cy
, & GetFont());
210 int control_width
, control_height
;
212 // Ignore height parameter because height doesn't
213 // mean 'initially displayed' height, it refers to the
214 // drop-down menu as well. The wxWindows interpretation
215 // is different; also, getting the size returns the
216 // _displayed_ size (NOT the drop down menu size)
217 // so setting-getting-setting size would not work.
220 // Deal with default size (using -1 values)
223 // Find the longest string
224 if (m_noStrings
== 0)
233 for (i
= 0; i
< m_noStrings
; i
++)
235 wxString
str(GetString(i
));
236 GetTextExtent(str
, &len
, &ht
, NULL
, NULL
, & GetFont());
241 control_width
= longest
+ cx
*5;
246 // If non-default width...
251 // Choice drop-down list depends on number of items (limited to 10)
254 if (m_noStrings
== 0)
255 h1
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*10;
257 h1
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*(wxMin(10, m_noStrings
) + 1);
262 // Calculations may have made text size too small
263 if (control_height
<= 0)
264 control_height
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
266 if (control_width
<= 0)
269 MoveWindow((HWND
)GetHWND(), x1
, y1
,
270 control_width
, control_height
, TRUE
);
273 WXHBRUSH
wxChoice::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
274 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
279 long wxChoice::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
286 if (GetWindowStyleFlag() & wxPROCESS_ENTER)
287 return DLGC_WANTALLKEYS;
292 case WM_CHAR: // Always an ASCII character
294 if (wParam == VK_RETURN)
296 wxCommandEvent event(wxEVENT_TYPE_TEXT_ENTER_COMMAND);
297 event.commandString = ((wxTextCtrl *)item)->GetValue();
298 event.eventObject = item;
299 item->ProcessCommand(event);
307 int x
= (int)LOWORD(lParam
);
308 int y
= (int)HIWORD(lParam
);
310 // Ok, this is truly weird, but if a panel with a wxChoice loses the
311 // focus, then you get a *fake* WM_LBUTTONUP message
312 // with x = 65535 and y = 65535.
313 // Filter out this nonsense.
314 if (x
== 65535 && y
== 65535)
320 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
323 wxString
wxChoice::GetStringSelection (void) const
325 int sel
= GetSelection ();
327 return wxString(this->GetString (sel
));
332 bool wxChoice::SetStringSelection (const wxString
& s
)
334 int sel
= FindString (s
);
344 void wxChoice::Command(wxCommandEvent
& event
)
346 SetSelection (event
.GetInt());
347 ProcessCommand (event
);