]> git.saurik.com Git - wxWidgets.git/blame - src/msw/choice.cpp
1. panels pass focus to the next item in parent panel
[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"
4e938f5b 26#include "wx/log.h"
2bda0e17
KB
27#endif
28
29#include "wx/msw/private.h"
30
31#if !USE_SHARED_LIBRARY
32IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
33#endif
34
debe6624 35bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17
KB
36{
37 if (param == CBN_SELCHANGE)
38 {
5de76427 39 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
2bda0e17
KB
40 event.SetInt(GetSelection());
41 event.SetEventObject(this);
29006414 42 event.SetString(GetStringSelection());
2bda0e17 43 ProcessCommand(event);
29006414 44
2bda0e17
KB
45 return TRUE;
46 }
29006414
VZ
47 else
48 return FALSE;
2bda0e17
KB
49}
50
debe6624 51bool wxChoice::Create(wxWindow *parent, wxWindowID id,
2bda0e17
KB
52 const wxPoint& pos,
53 const wxSize& size,
c085e333
VZ
54 int n, const wxString choices[],
55 long style,
2bda0e17
KB
56 const wxValidator& validator,
57 const wxString& name)
58{
59 SetName(name);
60 SetValidator(validator);
61 if (parent) parent->AddChild(this);
fd71308f
JS
62 SetBackgroundColour(parent->GetBackgroundColour()) ;
63 SetForegroundColour(parent->GetForegroundColour()) ;
33b64e6f 64 m_noStrings = 0;
2bda0e17
KB
65
66 m_windowStyle = style;
67
68 if ( id == -1 )
c085e333 69 m_windowId = (int)NewControlId();
2bda0e17 70 else
c085e333 71 m_windowId = id;
2bda0e17
KB
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.
c085e333 88 if ( want3D || wxStyleHasBorder(m_windowStyle) )
2bda0e17
KB
89 msStyle |= WS_BORDER;
90
837e5743 91 m_hWnd = (WXHWND)::CreateWindowEx(exStyle, _T("COMBOBOX"), NULL,
2bda0e17
KB
92 msStyle,
93 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
94 wxGetInstance(), NULL);
c085e333 95
837e5743 96 wxCHECK_MSG( m_hWnd, FALSE, _T("Failed to create combobox") );
c085e333 97
2bda0e17 98/*
1f112209 99#if wxUSE_CTL3D
2bda0e17
KB
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
2bda0e17 108 // Subclass again for purposes of dialog editing mode
c085e333 109 SubclassWin(m_hWnd);
2bda0e17 110
c0ed460c 111 SetFont(parent->GetFont());
2bda0e17
KB
112
113 int i;
114 for (i = 0; i < n; i++)
c085e333
VZ
115 {
116 Append(choices[i]);
117 }
118 SetSelection(n);
2bda0e17
KB
119
120 SetSize(x, y, width, height);
121
122 return TRUE;
123}
124
125void wxChoice::Append(const wxString& item)
126{
4438caf4 127 SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LONG)(const wxChar *)item);
2bda0e17 128
bbcdf8bc 129 m_noStrings ++;
2bda0e17
KB
130}
131
debe6624 132void wxChoice::Delete(int n)
2bda0e17 133{
4438caf4 134 m_noStrings = (int)SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
2bda0e17
KB
135}
136
137void wxChoice::Clear(void)
138{
4438caf4 139 SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
2bda0e17 140
bbcdf8bc 141 m_noStrings = 0;
2bda0e17
KB
142}
143
144
145int wxChoice::GetSelection(void) const
146{
4438caf4 147 return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
2bda0e17
KB
148}
149
debe6624 150void wxChoice::SetSelection(int n)
2bda0e17 151{
4438caf4 152 SendMessage(GetHwnd(), CB_SETCURSEL, n, 0);
2bda0e17
KB
153}
154
155int 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 {
4438caf4 163 int len = (int)SendMessage(GetHwnd(), CB_GETLBTEXT, i, (LPARAM)(LPSTR)buf);
2bda0e17
KB
164 buf[len] = 0;
165 if (strcmp(buf, (const char *)s) == 0)
166 return i;
167 }
168 return -1;
169#else
4438caf4 170 int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)(LPSTR)(const wxChar *)s);
2bda0e17
KB
171 if (pos == LB_ERR)
172 return -1;
173 else
174 return pos;
175#endif
176}
177
debe6624 178wxString wxChoice::GetString(int n) const
2bda0e17 179{
4438caf4
VZ
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 }
2bda0e17 187
4438caf4 188 str.UngetWriteBuf();
2bda0e17 189
4438caf4
VZ
190 return str;
191}
2bda0e17 192
4438caf4
VZ
193void 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}
2bda0e17 204
4438caf4
VZ
205wxSize wxChoice::DoGetBestSize()
206{
207 // find the widest string
208 int wLine;
209 int wChoice = 0;
210 for ( int i = 0; i < m_noStrings; i++ )
2bda0e17 211 {
2bda0e17 212 wxString str(GetString(i));
4438caf4
VZ
213 GetTextExtent(str, &wLine, NULL);
214 if ( wLine > wChoice )
215 wChoice = wLine;
2bda0e17 216 }
fd3f686c 217
4438caf4
VZ
218 // give it some reasonable default value if there are no strings in the
219 // list
220 if ( wChoice == 0 )
221 wChoice = 100;
2bda0e17 222
4438caf4
VZ
223 // the combobox should be larger than the widest string
224 int cx, cy;
225 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
2bda0e17 226
4438caf4 227 wChoice += 5*cx;
2bda0e17 228
4438caf4
VZ
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;
2bda0e17 232
4438caf4 233 return wxSize(wChoice, hChoice);
2bda0e17
KB
234}
235
debe6624 236WXHBRUSH wxChoice::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
c085e333 237 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
2bda0e17 238{
c085e333 239 return 0;
2bda0e17
KB
240}
241
242long 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)
42e69d6b 278 return 0;
2bda0e17
KB
279 break;
280 }
281 }
282
283 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
284}
285
286wxString wxChoice::GetStringSelection (void) const
287{
288 int sel = GetSelection ();
289 if (sel > -1)
290 return wxString(this->GetString (sel));
291 else
837e5743 292 return wxString(_T(""));
2bda0e17
KB
293}
294
295bool 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
307void wxChoice::Command(wxCommandEvent & event)
308{
309 SetSelection (event.GetInt());
310 ProcessCommand (event);
311}
312
313
314