]> git.saurik.com Git - wxWidgets.git/blob - src/msw/choice.cpp
Made wxGTK dcps.cpp generic.
[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 #endif
27
28 #include "wx/msw/private.h"
29
30 #if !USE_SHARED_LIBRARY
31 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
32 #endif
33
34 bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
35 {
36 if (param == CBN_SELCHANGE)
37 {
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();
44 return TRUE;
45 }
46 else return FALSE;
47 }
48
49 bool wxChoice::Create(wxWindow *parent, wxWindowID id,
50 const wxPoint& pos,
51 const wxSize& size,
52 int n, const wxString choices[],
53 long style,
54 const wxValidator& validator,
55 const wxString& name)
56 {
57 SetName(name);
58 SetValidator(validator);
59 if (parent) parent->AddChild(this);
60 SetBackgroundColour(parent->GetBackgroundColour()) ;
61 SetForegroundColour(parent->GetForegroundColour()) ;
62 m_noStrings = n;
63
64 m_windowStyle = style;
65
66 if ( id == -1 )
67 m_windowId = (int)NewControlId();
68 else
69 m_windowId = id;
70
71 int x = pos.x;
72 int y = pos.y;
73 int width = size.x;
74 int height = size.y;
75
76 long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_HSCROLL | WS_VSCROLL
77 | WS_TABSTOP | WS_VISIBLE;
78 if (m_windowStyle & wxCB_SORT)
79 msStyle |= CBS_SORT;
80
81 bool want3D;
82 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
83
84 // Even with extended styles, need to combine with WS_BORDER
85 // for them to look right.
86 if ( want3D || wxStyleHasBorder(m_windowStyle) )
87 msStyle |= WS_BORDER;
88
89 m_hWnd = (WXHWND)::CreateWindowEx(exStyle, "COMBOBOX", NULL,
90 msStyle,
91 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
92 wxGetInstance(), NULL);
93
94 wxCHECK_MSG( m_hWnd, FALSE, "Failed to create combobox" );
95
96 /*
97 #if CTL3D
98 if (want3D)
99 {
100 m_useCtl3D = TRUE;
101 Ctl3dSubclassCtl(wx_combo); // Does CTL3D affect the combobox? I think not.
102 }
103 #endif
104 */
105
106 // Subclass again for purposes of dialog editing mode
107 SubclassWin(m_hWnd);
108
109 SetFont(parent->GetFont());
110
111 int i;
112 for (i = 0; i < n; i++)
113 {
114 Append(choices[i]);
115 }
116 SetSelection(n);
117
118 SetSize(x, y, width, height);
119
120 return TRUE;
121 }
122
123 void wxChoice::Append(const wxString& item)
124 {
125 SendMessage((HWND) GetHWND(), CB_ADDSTRING, 0, (LONG)(const char *)item);
126
127 m_noStrings ++;
128 }
129
130 void wxChoice::Delete(int n)
131 {
132 m_noStrings = (int)SendMessage((HWND) GetHWND(), CB_DELETESTRING, n, 0);
133 }
134
135 void wxChoice::Clear(void)
136 {
137 SendMessage((HWND) GetHWND(), CB_RESETCONTENT, 0, 0);
138
139 m_noStrings = 0;
140 }
141
142
143 int wxChoice::GetSelection(void) const
144 {
145 return (int)SendMessage((HWND) GetHWND(), CB_GETCURSEL, 0, 0);
146 }
147
148 void wxChoice::SetSelection(int n)
149 {
150 SendMessage((HWND) GetHWND(), CB_SETCURSEL, n, 0);
151 }
152
153 int wxChoice::FindString(const wxString& s) const
154 {
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.
158 char buf[512];
159 for (int i = 0; i < Number(); i++)
160 {
161 int len = (int)SendMessage((HWND) GetHWND(), CB_GETLBTEXT, i, (LPARAM)(LPSTR)buf);
162 buf[len] = 0;
163 if (strcmp(buf, (const char *)s) == 0)
164 return i;
165 }
166 return -1;
167 #else
168 int pos = (int)SendMessage((HWND) GetHWND(), CB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)(LPSTR)(const char *)s);
169 if (pos == LB_ERR)
170 return -1;
171 else
172 return pos;
173 #endif
174 }
175
176 wxString wxChoice::GetString(int n) const
177 {
178 int len = (int)SendMessage((HWND) GetHWND(), CB_GETLBTEXT, n, (long)wxBuffer);
179 wxBuffer[len] = 0;
180 return wxString(wxBuffer);
181 }
182
183 void wxChoice::SetSize(int x, int y, int width, int height, int sizeFlags)
184 {
185 int currentX, currentY;
186 GetPosition(&currentX, &currentY);
187
188 int x1 = x;
189 int y1 = y;
190 int w1 = width;
191 int h1 = height;
192
193 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
194 x1 = currentX;
195 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
196 y1 = currentY;
197
198 AdjustForParentClientOrigin(x1, y1, sizeFlags);
199
200 // If we're prepared to use the existing size, then...
201 if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO))
202 {
203 GetSize(&w1, &h1);
204 }
205
206 int cx; // button font dimensions
207 int cy;
208 wxGetCharSize(GetHWND(), &cx, &cy, & this->GetFont());
209
210 int control_width, control_height;
211
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.
218 h1 = -1;
219
220 // Deal with default size (using -1 values)
221 if (width <= 0)
222 {
223 // Find the longest string
224 if (m_noStrings == 0)
225 {
226 control_width = 100;
227 }
228 else
229 {
230 int len, ht;
231 int longest = 0;
232 int i;
233 for (i = 0; i < m_noStrings; i++)
234 {
235 wxString str(GetString(i));
236 GetTextExtent(str, &len, &ht, NULL, NULL, & this->GetFont());
237 if ( len > longest)
238 longest = len;
239 }
240
241 control_width = longest + cx*5;
242 }
243 }
244 else
245 {
246 // If non-default width...
247 control_width = w1;
248 }
249
250
251 // Choice drop-down list depends on number of items (limited to 10)
252 if (h1 <= 0)
253 {
254 if (m_noStrings == 0)
255 h1 = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*10;
256 else
257 h1 = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*(wxMin(10, m_noStrings) + 1);
258 }
259
260 control_height = h1;
261
262 // Calculations may have made text size too small
263 if (control_height <= 0)
264 control_height = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
265
266 if (control_width <= 0)
267 control_width = 100;
268
269 MoveWindow((HWND)GetHWND(), x1, y1,
270 control_width, control_height, TRUE);
271 }
272
273 WXHBRUSH wxChoice::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
274 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
275 {
276 return 0;
277 }
278
279 long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
280 {
281 switch (nMsg)
282 {
283 /*
284 case WM_GETDLGCODE:
285 {
286 if (GetWindowStyleFlag() & wxPROCESS_ENTER)
287 return DLGC_WANTALLKEYS;
288 break;
289 }
290 */
291 /*
292 case WM_CHAR: // Always an ASCII character
293 {
294 if (wParam == VK_RETURN)
295 {
296 wxCommandEvent event(wxEVENT_TYPE_TEXT_ENTER_COMMAND);
297 event.commandString = ((wxTextCtrl *)item)->GetValue();
298 event.eventObject = item;
299 item->ProcessCommand(event);
300 return FALSE;
301 }
302 break;
303 }
304 */
305 case WM_LBUTTONUP:
306 {
307 int x = (int)LOWORD(lParam);
308 int y = (int)HIWORD(lParam);
309
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)
315 return Default();
316 break;
317 }
318 }
319
320 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
321 }
322
323 wxString wxChoice::GetStringSelection (void) const
324 {
325 int sel = GetSelection ();
326 if (sel > -1)
327 return wxString(this->GetString (sel));
328 else
329 return wxString("");
330 }
331
332 bool wxChoice::SetStringSelection (const wxString& s)
333 {
334 int sel = FindString (s);
335 if (sel > -1)
336 {
337 SetSelection (sel);
338 return TRUE;
339 }
340 else
341 return FALSE;
342 }
343
344 void wxChoice::Command(wxCommandEvent & event)
345 {
346 SetSelection (event.GetInt());
347 ProcessCommand (event);
348 }
349
350
351