]> git.saurik.com Git - wxWidgets.git/blame - src/msw/choice.cpp
latest CW additions
[wxWidgets.git] / src / msw / choice.cpp
CommitLineData
2bda0e17
KB
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
c085e333 9// Licence: wxWindows license
2bda0e17
KB
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"
2432b92d 25#include "wx/utils.h"
2bda0e17
KB
26#endif
27
28#include "wx/msw/private.h"
29
30#if !USE_SHARED_LIBRARY
31IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
32#endif
33
debe6624 34bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17
KB
35{
36 if (param == CBN_SELCHANGE)
37 {
5de76427 38 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
2bda0e17
KB
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
debe6624 49bool wxChoice::Create(wxWindow *parent, wxWindowID id,
2bda0e17
KB
50 const wxPoint& pos,
51 const wxSize& size,
c085e333
VZ
52 int n, const wxString choices[],
53 long style,
2bda0e17
KB
54 const wxValidator& validator,
55 const wxString& name)
56{
57 SetName(name);
58 SetValidator(validator);
59 if (parent) parent->AddChild(this);
fd71308f
JS
60 SetBackgroundColour(parent->GetBackgroundColour()) ;
61 SetForegroundColour(parent->GetForegroundColour()) ;
bbcdf8bc 62 m_noStrings = n;
2bda0e17
KB
63
64 m_windowStyle = style;
65
66 if ( id == -1 )
c085e333 67 m_windowId = (int)NewControlId();
2bda0e17 68 else
c085e333 69 m_windowId = id;
2bda0e17
KB
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.
c085e333 86 if ( want3D || wxStyleHasBorder(m_windowStyle) )
2bda0e17
KB
87 msStyle |= WS_BORDER;
88
c085e333 89 m_hWnd = (WXHWND)::CreateWindowEx(exStyle, "COMBOBOX", NULL,
2bda0e17
KB
90 msStyle,
91 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
92 wxGetInstance(), NULL);
c085e333
VZ
93
94 wxCHECK_MSG( m_hWnd, FALSE, "Failed to create combobox" );
95
2bda0e17
KB
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
2bda0e17 106 // Subclass again for purposes of dialog editing mode
c085e333 107 SubclassWin(m_hWnd);
2bda0e17 108
c0ed460c 109 SetFont(parent->GetFont());
2bda0e17
KB
110
111 int i;
112 for (i = 0; i < n; i++)
c085e333
VZ
113 {
114 Append(choices[i]);
115 }
116 SetSelection(n);
2bda0e17
KB
117
118 SetSize(x, y, width, height);
119
120 return TRUE;
121}
122
123void wxChoice::Append(const wxString& item)
124{
125 SendMessage((HWND) GetHWND(), CB_ADDSTRING, 0, (LONG)(const char *)item);
126
bbcdf8bc 127 m_noStrings ++;
2bda0e17
KB
128}
129
debe6624 130void wxChoice::Delete(int n)
2bda0e17 131{
bbcdf8bc 132 m_noStrings = (int)SendMessage((HWND) GetHWND(), CB_DELETESTRING, n, 0);
2bda0e17
KB
133}
134
135void wxChoice::Clear(void)
136{
137 SendMessage((HWND) GetHWND(), CB_RESETCONTENT, 0, 0);
138
bbcdf8bc 139 m_noStrings = 0;
2bda0e17
KB
140}
141
142
143int wxChoice::GetSelection(void) const
144{
145 return (int)SendMessage((HWND) GetHWND(), CB_GETCURSEL, 0, 0);
146}
147
debe6624 148void wxChoice::SetSelection(int n)
2bda0e17
KB
149{
150 SendMessage((HWND) GetHWND(), CB_SETCURSEL, n, 0);
151}
152
153int 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
debe6624 176wxString wxChoice::GetString(int n) const
2bda0e17
KB
177{
178 int len = (int)SendMessage((HWND) GetHWND(), CB_GETLBTEXT, n, (long)wxBuffer);
179 wxBuffer[len] = 0;
180 return wxString(wxBuffer);
181}
182
debe6624 183void wxChoice::SetSize(int x, int y, int width, int height, int sizeFlags)
2bda0e17
KB
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
81d66cf3
JS
198 AdjustForParentClientOrigin(x1, y1, sizeFlags);
199
2bda0e17
KB
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;
ce3ed50d 208 wxGetCharSize(GetHWND(), &cx, &cy, & this->GetFont());
2bda0e17 209
1c4a764c 210 int control_width, control_height;
2bda0e17
KB
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
bbcdf8bc 224 if (m_noStrings == 0)
1c4a764c
VZ
225 {
226 control_width = 100;
227 }
2bda0e17
KB
228 else
229 {
debe6624 230 int len, ht;
1c4a764c 231 int longest = 0;
2bda0e17 232 int i;
bbcdf8bc 233 for (i = 0; i < m_noStrings; i++)
2bda0e17
KB
234 {
235 wxString str(GetString(i));
ce3ed50d 236 GetTextExtent(str, &len, &ht, NULL, NULL, & this->GetFont());
1c4a764c
VZ
237 if ( len > longest)
238 longest = len;
2bda0e17
KB
239 }
240
1c4a764c 241 control_width = longest + cx*5;
2bda0e17
KB
242 }
243 }
fd3f686c
VZ
244 else
245 {
246 // If non-default width...
247 control_width = w1;
248 }
249
2bda0e17
KB
250
251 // Choice drop-down list depends on number of items (limited to 10)
252 if (h1 <= 0)
253 {
bbcdf8bc 254 if (m_noStrings == 0)
1c4a764c
VZ
255 h1 = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*10;
256 else
257 h1 = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*(wxMin(10, m_noStrings) + 1);
2bda0e17
KB
258 }
259
1c4a764c 260 control_height = h1;
2bda0e17
KB
261
262 // Calculations may have made text size too small
263 if (control_height <= 0)
1c4a764c 264 control_height = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
2bda0e17
KB
265
266 if (control_width <= 0)
1c4a764c 267 control_width = 100;
2bda0e17 268
1c4a764c
VZ
269 MoveWindow((HWND)GetHWND(), x1, y1,
270 control_width, control_height, TRUE);
2bda0e17
KB
271}
272
debe6624 273WXHBRUSH wxChoice::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
c085e333 274 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
2bda0e17 275{
c085e333 276 return 0;
2bda0e17
KB
277}
278
279long 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
323wxString 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
332bool 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
344void wxChoice::Command(wxCommandEvent & event)
345{
346 SetSelection (event.GetInt());
347 ProcessCommand (event);
348}
349
350
351