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"
27 #include "wx/msw/private.h"
29 #if !USE_SHARED_LIBRARY
30 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
33 bool wxChoice::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
35 if (param
== CBN_SELCHANGE
)
37 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
38 event
.SetInt(GetSelection());
39 event
.SetEventObject(this);
40 event
.SetString(copystring(GetStringSelection()));
41 ProcessCommand(event
);
42 delete[] event
.GetString();
48 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
51 int n
, const wxString choices
[],
53 const wxValidator
& validator
,
57 SetValidator(validator
);
58 if (parent
) parent
->AddChild(this);
59 SetBackgroundColour(parent
->GetBackgroundColour()) ;
60 SetForegroundColour(parent
->GetForegroundColour()) ;
63 m_windowStyle
= style
;
66 m_windowId
= (int)NewControlId();
75 long msStyle
= WS_CHILD
| CBS_DROPDOWNLIST
| WS_HSCROLL
| WS_VSCROLL
76 | WS_TABSTOP
| WS_VISIBLE
;
77 if (m_windowStyle
& wxCB_SORT
)
81 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
83 // Even with extended styles, need to combine with WS_BORDER
84 // for them to look right.
85 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
88 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, "COMBOBOX", NULL
,
90 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
91 wxGetInstance(), NULL
);
93 wxCHECK_MSG( m_hWnd
, FALSE
, "Failed to create combobox" );
100 Ctl3dSubclassCtl(wx_combo); // Does CTL3D affect the combobox? I think not.
105 // Subclass again for purposes of dialog editing mode
108 SetFont(* parent
->GetFont());
111 for (i
= 0; i
< n
; i
++)
117 SetSize(x
, y
, width
, height
);
122 void wxChoice::Append(const wxString
& item
)
124 SendMessage((HWND
) GetHWND(), CB_ADDSTRING
, 0, (LONG
)(const char *)item
);
129 void wxChoice::Delete(int n
)
131 m_noStrings
= (int)SendMessage((HWND
) GetHWND(), CB_DELETESTRING
, n
, 0);
134 void wxChoice::Clear(void)
136 SendMessage((HWND
) GetHWND(), CB_RESETCONTENT
, 0, 0);
142 int wxChoice::GetSelection(void) const
144 return (int)SendMessage((HWND
) GetHWND(), CB_GETCURSEL
, 0, 0);
147 void wxChoice::SetSelection(int n
)
149 SendMessage((HWND
) GetHWND(), CB_SETCURSEL
, n
, 0);
152 int wxChoice::FindString(const wxString
& s
) const
154 #if defined(__WATCOMC__) && defined(__WIN386__)
155 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
156 // Do it the long way instead.
158 for (int i
= 0; i
< Number(); i
++)
160 int len
= (int)SendMessage((HWND
) GetHWND(), CB_GETLBTEXT
, i
, (LPARAM
)(LPSTR
)buf
);
162 if (strcmp(buf
, (const char *)s
) == 0)
167 int pos
= (int)SendMessage((HWND
) GetHWND(), CB_FINDSTRINGEXACT
, (WPARAM
)-1, (LPARAM
)(LPSTR
)(const char *)s
);
175 wxString
wxChoice::GetString(int n
) const
177 int len
= (int)SendMessage((HWND
) GetHWND(), CB_GETLBTEXT
, n
, (long)wxBuffer
);
179 return wxString(wxBuffer
);
182 void wxChoice::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
184 int currentX
, currentY
;
185 GetPosition(¤tX
, ¤tY
);
192 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
194 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
197 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
199 // If we're prepared to use the existing size, then...
200 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
205 int cx
; // button font dimensions
207 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
209 int control_width
, control_height
;
211 // Ignore height parameter because height doesn't
212 // mean 'initially displayed' height, it refers to the
213 // drop-down menu as well. The wxWindows interpretation
214 // is different; also, getting the size returns the
215 // _displayed_ size (NOT the drop down menu size)
216 // so setting-getting-setting size would not work.
219 // Deal with default size (using -1 values)
222 // Find the longest string
223 if (m_noStrings
== 0)
232 for (i
= 0; i
< m_noStrings
; i
++)
234 wxString
str(GetString(i
));
235 GetTextExtent(str
, &len
, &ht
, NULL
, NULL
,GetFont());
240 control_width
= longest
+ cx
*5;
244 // Choice drop-down list depends on number of items (limited to 10)
247 if (m_noStrings
== 0)
248 h1
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*10;
250 h1
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
)*(wxMin(10, m_noStrings
) + 1);
253 // If non-default width...
259 // Calculations may have made text size too small
260 if (control_height
<= 0)
261 control_height
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
263 if (control_width
<= 0)
266 MoveWindow((HWND
)GetHWND(), x1
, y1
,
267 control_width
, control_height
, TRUE
);
270 WXHBRUSH
wxChoice::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
271 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
276 long wxChoice::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
283 if (GetWindowStyleFlag() & wxPROCESS_ENTER)
284 return DLGC_WANTALLKEYS;
289 case WM_CHAR: // Always an ASCII character
291 if (wParam == VK_RETURN)
293 wxCommandEvent event(wxEVENT_TYPE_TEXT_ENTER_COMMAND);
294 event.commandString = ((wxTextCtrl *)item)->GetValue();
295 event.eventObject = item;
296 item->ProcessCommand(event);
304 int x
= (int)LOWORD(lParam
);
305 int y
= (int)HIWORD(lParam
);
307 // Ok, this is truly weird, but if a panel with a wxChoice loses the
308 // focus, then you get a *fake* WM_LBUTTONUP message
309 // with x = 65535 and y = 65535.
310 // Filter out this nonsense.
311 if (x
== 65535 && y
== 65535)
317 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
320 wxString
wxChoice::GetStringSelection (void) const
322 int sel
= GetSelection ();
324 return wxString(this->GetString (sel
));
329 bool wxChoice::SetStringSelection (const wxString
& s
)
331 int sel
= FindString (s
);
341 void wxChoice::Command(wxCommandEvent
& event
)
343 SetSelection (event
.GetInt());
344 ProcessCommand (event
);