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