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(const WXUINT param
, const WXWORD
WXUNUSED(id
))
35 if (param
== CBN_SELCHANGE
)
37 wxCommandEvent
event(wxEVENT_TYPE_CHOICE_COMMAND
, 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
, const wxWindowID id
,
51 const int n
, const wxString choices
[],
53 const wxValidator
& validator
,
57 SetValidator(validator
);
58 if (parent
) parent
->AddChild(this);
59 SetBackgroundColour(parent
->GetDefaultBackgroundColour()) ;
60 SetForegroundColour(parent
->GetDefaultForegroundColour()) ;
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
|| (m_windowStyle
& wxSIMPLE_BORDER
) || (m_windowStyle
& wxRAISED_BORDER
) ||
86 (m_windowStyle
& wxSUNKEN_BORDER
) || (m_windowStyle
& wxDOUBLE_BORDER
))
89 HWND wx_combo
= CreateWindowEx(exStyle
, "COMBOBOX", NULL
,
91 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
92 wxGetInstance(), NULL
);
98 Ctl3dSubclassCtl(wx_combo); // Does CTL3D affect the combobox? I think not.
103 m_hWnd
= (WXHWND
) wx_combo
;
105 // Subclass again for purposes of dialog editing mode
106 SubclassWin((WXHWND
) wx_combo
);
108 SetFont(* parent
->GetFont());
111 for (i
= 0; i
< n
; i
++)
112 SendMessage(wx_combo
, CB_INSERTSTRING
, i
, (LONG
)(const char *)choices
[i
]);
113 SendMessage(wx_combo
, CB_SETCURSEL
, i
, 0);
115 SetSize(x
, y
, width
, height
);
120 void wxChoice::Append(const wxString
& item
)
122 SendMessage((HWND
) GetHWND(), CB_ADDSTRING
, 0, (LONG
)(const char *)item
);
127 void wxChoice::Delete(const int n
)
129 no_strings
= (int)SendMessage((HWND
) GetHWND(), CB_DELETESTRING
, n
, 0);
132 void wxChoice::Clear(void)
134 SendMessage((HWND
) GetHWND(), CB_RESETCONTENT
, 0, 0);
140 int wxChoice::GetSelection(void) const
142 return (int)SendMessage((HWND
) GetHWND(), CB_GETCURSEL
, 0, 0);
145 void wxChoice::SetSelection(const int n
)
147 SendMessage((HWND
) GetHWND(), CB_SETCURSEL
, n
, 0);
150 int wxChoice::FindString(const wxString
& s
) const
152 #if defined(__WATCOMC__) && defined(__WIN386__)
153 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
154 // Do it the long way instead.
156 for (int i
= 0; i
< Number(); i
++)
158 int len
= (int)SendMessage((HWND
) GetHWND(), CB_GETLBTEXT
, i
, (LPARAM
)(LPSTR
)buf
);
160 if (strcmp(buf
, (const char *)s
) == 0)
165 int pos
= (int)SendMessage((HWND
) GetHWND(), CB_FINDSTRINGEXACT
, (WPARAM
)-1, (LPARAM
)(LPSTR
)(const char *)s
);
173 wxString
wxChoice::GetString(const int n
) const
175 int len
= (int)SendMessage((HWND
) GetHWND(), CB_GETLBTEXT
, n
, (long)wxBuffer
);
177 return wxString(wxBuffer
);
180 void wxChoice::SetSize(const int x
, const int y
, const int width
, const int height
, const int sizeFlags
)
182 int currentX
, currentY
;
183 GetPosition(¤tX
, ¤tY
);
190 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
192 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
195 // If we're prepared to use the existing size, then...
196 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
201 int cx
; // button font dimensions
203 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
205 float control_width
, control_height
;
207 // Ignore height parameter because height doesn't
208 // mean 'initially displayed' height, it refers to the
209 // drop-down menu as well. The wxWindows interpretation
210 // is different; also, getting the size returns the
211 // _displayed_ size (NOT the drop down menu size)
212 // so setting-getting-setting size would not work.
215 // Deal with default size (using -1 values)
218 // Find the longest string
220 control_width
= (float)100.0;
224 float longest
= (float)0.0;
226 for (i
= 0; i
< no_strings
; i
++)
228 wxString
str(GetString(i
));
229 GetTextExtent(str
, &len
, &ht
, NULL
, NULL
,GetFont());
230 if ( len
> longest
) longest
= len
;
233 control_width
= (float)(int)(longest
+ cx
*5);
237 // Choice drop-down list depends on number of items (limited to 10)
241 h1
= (int)(EDIT_CONTROL_FACTOR
*cy
*10.0);
242 else h1
= (int)(EDIT_CONTROL_FACTOR
*cy
*(wxMin(10, no_strings
) + 1));
245 // If non-default width...
247 control_width
= (float)width
;
249 control_height
= (float)h1
;
251 // Calculations may have made text size too small
252 if (control_height
<= 0)
253 control_height
= (float)(int)(cy
*EDIT_CONTROL_FACTOR
) ;
255 if (control_width
<= 0)
256 control_width
= (float)100.0;
258 MoveWindow((HWND
) GetHWND(), x1
, y1
,
259 (int)control_width
, (int)control_height
, TRUE
);
262 #if WXWIN_COMPATIBILITY
263 GetEventHandler()->OldOnSize(width, height);
265 wxSizeEvent event(wxSize(width, height), m_windowId);
266 event.eventObject = this;
267 GetEventHandler()->ProcessEvent(event);
272 WXHBRUSH
wxChoice::OnCtlColor(const WXHDC pDC
, const WXHWND pWnd
, const WXUINT nCtlColor
,
273 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
278 long wxChoice::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
285 if (GetWindowStyleFlag() & wxPROCESS_ENTER)
286 return DLGC_WANTALLKEYS;
291 case WM_CHAR: // Always an ASCII character
293 if (wParam == VK_RETURN)
295 wxCommandEvent event(wxEVENT_TYPE_TEXT_ENTER_COMMAND);
296 event.commandString = ((wxTextCtrl *)item)->GetValue();
297 event.eventObject = item;
298 item->ProcessCommand(event);
306 int x
= (int)LOWORD(lParam
);
307 int y
= (int)HIWORD(lParam
);
309 // Ok, this is truly weird, but if a panel with a wxChoice loses the
310 // focus, then you get a *fake* WM_LBUTTONUP message
311 // with x = 65535 and y = 65535.
312 // Filter out this nonsense.
313 if (x
== 65535 && y
== 65535)
319 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
322 wxString
wxChoice::GetStringSelection (void) const
324 int sel
= GetSelection ();
326 return wxString(this->GetString (sel
));
331 bool wxChoice::SetStringSelection (const wxString
& s
)
333 int sel
= FindString (s
);
343 void wxChoice::Command(wxCommandEvent
& event
)
345 SetSelection (event
.GetInt());
346 ProcessCommand (event
);