]> git.saurik.com Git - wxWidgets.git/blame - src/msw/combobox.cpp
1. panels pass focus to the next item in parent panel
[wxWidgets.git] / src / msw / combobox.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: combobox.cpp
3// Purpose: wxComboBox class
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
c085e333 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "combobox.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/setup.h"
25#endif
26
47d67540 27#if wxUSE_COMBOBOX
2bda0e17
KB
28
29#include "wx/combobox.h"
30#include "wx/clipbrd.h"
31#include "wx/msw/private.h"
32
33#if !USE_SHARED_LIBRARY
34IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
35#endif
36
debe6624 37bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17
KB
38{
39 if (param == CBN_SELCHANGE)
40 {
5de76427 41 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId);
2bda0e17
KB
42 event.SetInt(GetSelection());
43 event.SetEventObject(this);
29006414 44 event.SetString(GetStringSelection());
2bda0e17 45 ProcessCommand(event);
29006414 46
2bda0e17
KB
47 return TRUE;
48 }
5de76427
JS
49 else if (param == CBN_EDITCHANGE)
50 {
51 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
29006414 52 event.SetString(GetValue());
088a95f5 53 event.SetEventObject(this);
5de76427 54 ProcessCommand(event);
29006414 55
5de76427
JS
56 return TRUE;
57 }
29006414
VZ
58 else
59 return FALSE;
2bda0e17
KB
60}
61
debe6624 62bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
c085e333
VZ
63 const wxString& value,
64 const wxPoint& pos,
65 const wxSize& size,
66 int n, const wxString choices[],
67 long style,
68 const wxValidator& validator,
69 const wxString& name)
2bda0e17
KB
70{
71 SetName(name);
72 SetValidator(validator);
73 if (parent) parent->AddChild(this);
fd71308f
JS
74 SetBackgroundColour(parent->GetBackgroundColour()) ;
75 SetForegroundColour(parent->GetForegroundColour()) ;
33b64e6f 76 m_noStrings = 0;
2bda0e17
KB
77
78 m_windowStyle = style;
79
80 if ( id == -1 )
c085e333 81 m_windowId = (int)NewControlId();
2bda0e17 82 else
c085e333 83 m_windowId = id;
2bda0e17
KB
84
85 int x = pos.x;
86 int y = pos.y;
87 int width = size.x;
88 int height = size.y;
89
c085e333
VZ
90 long msStyle = WS_CHILD | WS_HSCROLL | WS_VSCROLL |
91 WS_TABSTOP | WS_VISIBLE | CBS_NOINTEGRALHEIGHT;
92
2bda0e17
KB
93 if (m_windowStyle & wxCB_READONLY)
94 msStyle |= CBS_DROPDOWNLIST;
c085e333
VZ
95 else if (m_windowStyle & wxCB_SIMPLE)
96 msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
2bda0e17
KB
97 else
98 msStyle |= CBS_DROPDOWN;
99
100 if (m_windowStyle & wxCB_SORT)
101 msStyle |= CBS_SORT;
102
103 bool want3D;
104 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
105
106 // Even with extended styles, need to combine with WS_BORDER
107 // for them to look right.
c085e333 108 if ( want3D || wxStyleHasBorder(m_windowStyle) )
2bda0e17
KB
109 msStyle |= WS_BORDER;
110
837e5743 111 m_hWnd = (WXHWND)::CreateWindowEx(exStyle, _T("COMBOBOX"), NULL,
2bda0e17
KB
112 msStyle,
113 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
114 wxGetInstance(), NULL);
c085e333 115
837e5743 116 wxCHECK_MSG( m_hWnd, FALSE, _T("Failed to create combobox") );
c085e333 117
2bda0e17 118/*
1f112209 119#if wxUSE_CTL3D
2bda0e17
KB
120 if (want3D)
121 {
122 Ctl3dSubclassCtl(wx_combo);
123 m_useCtl3D = TRUE;
124 }
125#endif
126*/
127
2bda0e17 128 // Subclass again for purposes of dialog editing mode
c085e333 129 SubclassWin(m_hWnd);
2bda0e17 130
c0ed460c 131 SetFont(parent->GetFont());
2bda0e17
KB
132 int i;
133 for (i = 0; i < n; i++)
c085e333
VZ
134 {
135 Append(choices[i]);
136 }
137
138 SetSelection(i);
2bda0e17
KB
139
140 SetSize(x, y, width, height);
c085e333
VZ
141 if ( !value.IsEmpty() )
142 {
143 SetValue(value);
144 }
2bda0e17
KB
145
146 return TRUE;
147}
148
2bda0e17
KB
149void wxComboBox::SetValue(const wxString& value)
150{
151 // If newlines are denoted by just 10, must stick 13 in front.
152 int singletons = 0;
153 int len = value.Length();
154 int i;
155 for (i = 0; i < len; i ++)
156 {
157 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
158 singletons ++;
159 }
160 if (singletons > 0)
161 {
837e5743 162 wxChar *tmp = new wxChar[len + singletons + 1];
2bda0e17
KB
163 int j = 0;
164 for (i = 0; i < len; i ++)
165 {
166 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
167 {
168 tmp[j] = 13;
169 j ++;
170 }
171 tmp[j] = value[i];
172 j ++;
173 }
174 tmp[j] = 0;
4438caf4 175 SetWindowText(GetHwnd(), tmp);
2bda0e17
KB
176 delete[] tmp;
177 }
178 else
4438caf4 179 SetWindowText(GetHwnd(), value);
2bda0e17
KB
180}
181
182// Clipboard operations
1c4a764c 183void wxComboBox::Copy()
2bda0e17 184{
4438caf4 185 HWND hWnd = GetHwnd();
2bda0e17
KB
186 SendMessage(hWnd, WM_COPY, 0, 0L);
187}
188
1c4a764c 189void wxComboBox::Cut()
2bda0e17 190{
4438caf4 191 HWND hWnd = GetHwnd();
2bda0e17
KB
192 SendMessage(hWnd, WM_CUT, 0, 0L);
193}
194
1c4a764c 195void wxComboBox::Paste()
2bda0e17 196{
4438caf4 197 HWND hWnd = GetHwnd();
2bda0e17
KB
198 SendMessage(hWnd, WM_PASTE, 0, 0L);
199}
200
debe6624 201void wxComboBox::SetEditable(bool editable)
2bda0e17
KB
202{
203 // Can't implement in MSW?
4438caf4 204// HWND hWnd = GetHwnd();
2bda0e17
KB
205// SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
206}
207
debe6624 208void wxComboBox::SetInsertionPoint(long pos)
2bda0e17
KB
209{
210/*
4438caf4 211 HWND hWnd = GetHwnd();
2bda0e17
KB
212#ifdef __WIN32__
213 SendMessage(hWnd, EM_SETSEL, pos, pos);
214 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
215#else
216 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
217#endif
218 char *nothing = "";
219 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
220*/
221}
222
1c4a764c 223void wxComboBox::SetInsertionPointEnd()
2bda0e17
KB
224{
225/*
226 long pos = GetLastPosition();
227 SetInsertionPoint(pos);
228*/
229}
230
1c4a764c 231long wxComboBox::GetInsertionPoint() const
2bda0e17
KB
232{
233/*
4438caf4 234 DWORD Pos=(DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
2bda0e17
KB
235 return Pos&0xFFFF;
236*/
237 return 0;
238}
239
1c4a764c 240long wxComboBox::GetLastPosition() const
2bda0e17
KB
241{
242/*
4438caf4 243 HWND hWnd = GetHwnd();
2bda0e17
KB
244
245 // Will always return a number > 0 (according to docs)
246 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
247
248 // This gets the char index for the _beginning_ of the last line
249 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
4438caf4 250
2bda0e17
KB
251 // Get number of characters in the last line. We'll add this to the character
252 // index for the last line, 1st position.
253 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
254
255 return (long)(charIndex + lineLength);
256*/
257 return 0;
258}
259
debe6624 260void wxComboBox::Replace(long from, long to, const wxString& value)
2bda0e17 261{
47d67540 262#if wxUSE_CLIPBOARD
4438caf4 263 HWND hWnd = GetHwnd();
2bda0e17
KB
264 long fromChar = from;
265 long toChar = to;
4438caf4 266
2bda0e17
KB
267 // Set selection and remove it
268#ifdef __WIN32__
269 SendMessage(hWnd, CB_SETEDITSEL, fromChar, toChar);
270#else
271 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
272#endif
273 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
274
275 // Now replace with 'value', by pasting.
837e5743 276 wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0);
2bda0e17
KB
277
278 // Paste into edit control
279 SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
280#endif
281}
282
debe6624 283void wxComboBox::Remove(long from, long to)
2bda0e17 284{
4438caf4 285 HWND hWnd = GetHwnd();
2bda0e17
KB
286 long fromChar = from;
287 long toChar = to;
4438caf4 288
2bda0e17
KB
289 // Cut all selected text
290#ifdef __WIN32__
291 SendMessage(hWnd, CB_SETEDITSEL, fromChar, toChar);
292#else
293 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
294#endif
295 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
296}
297
debe6624 298void wxComboBox::SetSelection(long from, long to)
2bda0e17 299{
4438caf4 300 HWND hWnd = GetHwnd();
2bda0e17
KB
301 long fromChar = from;
302 long toChar = to;
303 // if from and to are both -1, it means
304 // (in wxWindows) that all text should be selected.
305 // This translates into Windows convention
306 if ((from == -1) && (to == -1))
307 {
308 fromChar = 0;
309 toChar = -1;
310 }
4438caf4 311
2bda0e17
KB
312#ifdef __WIN32__
313 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)fromChar, (LPARAM)toChar);
314// SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
315#else
316 // WPARAM is 0: selection is scrolled into view
317 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
318#endif
319}
320
4438caf4
VZ
321void wxComboBox::DoSetSize(int x, int y,
322 int width, int height,
323 int sizeFlags)
324{
325 wxControl::DoSetSize(x, y, width, height, sizeFlags);
4438caf4
VZ
326}
327
2bda0e17 328#endif
47d67540 329 // wxUSE_COMBOBOX
2bda0e17 330