]> git.saurik.com Git - wxWidgets.git/blob - src/msw/choice.cpp
compilation fix
[wxWidgets.git] / src / msw / choice.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: choice.cpp
3 // Purpose: wxChoice
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "choice.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/choice.h"
25 #include "wx/utils.h"
26 #include "wx/log.h"
27 #endif
28
29 #include "wx/msw/private.h"
30
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
33 #endif
34
35 bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
36 {
37 if (param == CBN_SELCHANGE)
38 {
39 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
40 event.SetInt(GetSelection());
41 event.SetEventObject(this);
42 event.SetString(GetStringSelection());
43 ProcessCommand(event);
44
45 return TRUE;
46 }
47 else
48 return FALSE;
49 }
50
51 bool wxChoice::Create(wxWindow *parent, wxWindowID id,
52 const wxPoint& pos,
53 const wxSize& size,
54 int n, const wxString choices[],
55 long style,
56 const wxValidator& validator,
57 const wxString& name)
58 {
59 SetName(name);
60 SetValidator(validator);
61 if (parent) parent->AddChild(this);
62 SetBackgroundColour(parent->GetBackgroundColour()) ;
63 SetForegroundColour(parent->GetForegroundColour()) ;
64 m_noStrings = 0;
65
66 m_windowStyle = style;
67
68 if ( id == -1 )
69 m_windowId = (int)NewControlId();
70 else
71 m_windowId = id;
72
73 int x = pos.x;
74 int y = pos.y;
75 int width = size.x;
76 int height = size.y;
77
78 long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_HSCROLL | WS_VSCROLL
79 | WS_TABSTOP | WS_VISIBLE;
80 if (m_windowStyle & wxCB_SORT)
81 msStyle |= CBS_SORT;
82
83 bool want3D;
84 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
85
86 // Even with extended styles, need to combine with WS_BORDER
87 // for them to look right.
88 if ( want3D || wxStyleHasBorder(m_windowStyle) )
89 msStyle |= WS_BORDER;
90
91 m_hWnd = (WXHWND)::CreateWindowEx(exStyle, _T("COMBOBOX"), NULL,
92 msStyle,
93 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
94 wxGetInstance(), NULL);
95
96 wxCHECK_MSG( m_hWnd, FALSE, _T("Failed to create combobox") );
97
98 /*
99 #if wxUSE_CTL3D
100 if (want3D)
101 {
102 m_useCtl3D = TRUE;
103 Ctl3dSubclassCtl(wx_combo); // Does CTL3D affect the combobox? I think not.
104 }
105 #endif
106 */
107
108 // Subclass again for purposes of dialog editing mode
109 SubclassWin(m_hWnd);
110
111 SetFont(parent->GetFont());
112
113 int i;
114 for (i = 0; i < n; i++)
115 {
116 Append(choices[i]);
117 }
118 SetSelection(n);
119
120 SetSize(x, y, width, height);
121
122 return TRUE;
123 }
124
125 void wxChoice::Append(const wxString& item)
126 {
127 SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LONG)(const wxChar *)item);
128
129 m_noStrings ++;
130 }
131
132 void wxChoice::Delete(int n)
133 {
134 m_noStrings = (int)SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
135 }
136
137 void wxChoice::Clear(void)
138 {
139 SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
140
141 m_noStrings = 0;
142 }
143
144
145 int wxChoice::GetSelection(void) const
146 {
147 return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
148 }
149
150 void wxChoice::SetSelection(int n)
151 {
152 SendMessage(GetHwnd(), CB_SETCURSEL, n, 0);
153 }
154
155 int wxChoice::FindString(const wxString& s) const
156 {
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.
160 char buf[512];
161 for (int i = 0; i < Number(); i++)
162 {
163 int len = (int)SendMessage(GetHwnd(), CB_GETLBTEXT, i, (LPARAM)(LPSTR)buf);
164 buf[len] = 0;
165 if (strcmp(buf, (const char *)s) == 0)
166 return i;
167 }
168 return -1;
169 #else
170 int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)(LPSTR)(const wxChar *)s);
171 if (pos == LB_ERR)
172 return -1;
173 else
174 return pos;
175 #endif
176 }
177
178 wxString wxChoice::GetString(int n) const
179 {
180 size_t len = (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
181 wxString str;
182 if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT, n,
183 (LPARAM)str.GetWriteBuf(len)) == CB_ERR )
184 {
185 wxLogLastError("SendMessage(CB_GETLBTEXT)");
186 }
187
188 str.UngetWriteBuf();
189
190 return str;
191 }
192
193 void wxChoice::DoSetSize(int x, int y,
194 int width, int height,
195 int sizeFlags)
196 {
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);
203 }
204
205 wxSize wxChoice::DoGetBestSize()
206 {
207 // find the widest string
208 int wLine;
209 int wChoice = 0;
210 for ( int i = 0; i < m_noStrings; i++ )
211 {
212 wxString str(GetString(i));
213 GetTextExtent(str, &wLine, NULL);
214 if ( wLine > wChoice )
215 wChoice = wLine;
216 }
217
218 // give it some reasonable default value if there are no strings in the
219 // list
220 if ( wChoice == 0 )
221 wChoice = 100;
222
223 // the combobox should be larger than the widest string
224 int cx, cy;
225 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
226
227 wChoice += 5*cx;
228
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;
232
233 return wxSize(wChoice, hChoice);
234 }
235
236 WXHBRUSH wxChoice::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
237 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
238 {
239 return 0;
240 }
241
242 long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
243 {
244 switch (nMsg)
245 {
246 /*
247 case WM_GETDLGCODE:
248 {
249 if (GetWindowStyleFlag() & wxPROCESS_ENTER)
250 return DLGC_WANTALLKEYS;
251 break;
252 }
253 */
254 /*
255 case WM_CHAR: // Always an ASCII character
256 {
257 if (wParam == VK_RETURN)
258 {
259 wxCommandEvent event(wxEVENT_TYPE_TEXT_ENTER_COMMAND);
260 event.commandString = ((wxTextCtrl *)item)->GetValue();
261 event.eventObject = item;
262 item->ProcessCommand(event);
263 return FALSE;
264 }
265 break;
266 }
267 */
268 case WM_LBUTTONUP:
269 {
270 int x = (int)LOWORD(lParam);
271 int y = (int)HIWORD(lParam);
272
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)
278 return 0;
279 break;
280 }
281 }
282
283 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
284 }
285
286 wxString wxChoice::GetStringSelection (void) const
287 {
288 int sel = GetSelection ();
289 if (sel > -1)
290 return wxString(this->GetString (sel));
291 else
292 return wxString(_T(""));
293 }
294
295 bool wxChoice::SetStringSelection (const wxString& s)
296 {
297 int sel = FindString (s);
298 if (sel > -1)
299 {
300 SetSelection (sel);
301 return TRUE;
302 }
303 else
304 return FALSE;
305 }
306
307 void wxChoice::Command(wxCommandEvent & event)
308 {
309 SetSelection (event.GetInt());
310 ProcessCommand (event);
311 }
312
313
314