]> git.saurik.com Git - wxWidgets.git/blame - src/msw/combobox.cpp
Make it LPNM_UPDOWN, not LPNMUPDOWN, in Borland C++
[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()) ;
2bda0e17
KB
76
77 m_windowStyle = style;
78
79 if ( id == -1 )
c085e333 80 m_windowId = (int)NewControlId();
2bda0e17 81 else
c085e333 82 m_windowId = id;
2bda0e17
KB
83
84 int x = pos.x;
85 int y = pos.y;
86 int width = size.x;
87 int height = size.y;
88
8d99be5f 89 long msStyle = WS_CHILD | WS_TABSTOP | WS_VISIBLE | CBS_NOINTEGRALHEIGHT;
c085e333 90
2bda0e17
KB
91 if (m_windowStyle & wxCB_READONLY)
92 msStyle |= CBS_DROPDOWNLIST;
c085e333
VZ
93 else if (m_windowStyle & wxCB_SIMPLE)
94 msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
2bda0e17
KB
95 else
96 msStyle |= CBS_DROPDOWN;
97
98 if (m_windowStyle & wxCB_SORT)
99 msStyle |= CBS_SORT;
100
101 bool want3D;
102 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
103
104 // Even with extended styles, need to combine with WS_BORDER
105 // for them to look right.
c085e333 106 if ( want3D || wxStyleHasBorder(m_windowStyle) )
2bda0e17
KB
107 msStyle |= WS_BORDER;
108
837e5743 109 m_hWnd = (WXHWND)::CreateWindowEx(exStyle, _T("COMBOBOX"), NULL,
2bda0e17
KB
110 msStyle,
111 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
112 wxGetInstance(), NULL);
c085e333 113
837e5743 114 wxCHECK_MSG( m_hWnd, FALSE, _T("Failed to create combobox") );
c085e333 115
2bda0e17 116/*
1f112209 117#if wxUSE_CTL3D
2bda0e17
KB
118 if (want3D)
119 {
120 Ctl3dSubclassCtl(wx_combo);
121 m_useCtl3D = TRUE;
122 }
123#endif
124*/
125
2bda0e17 126 // Subclass again for purposes of dialog editing mode
c085e333 127 SubclassWin(m_hWnd);
2bda0e17 128
c0ed460c 129 SetFont(parent->GetFont());
2bda0e17
KB
130 int i;
131 for (i = 0; i < n; i++)
c085e333
VZ
132 {
133 Append(choices[i]);
134 }
135
136 SetSelection(i);
2bda0e17
KB
137
138 SetSize(x, y, width, height);
c085e333
VZ
139 if ( !value.IsEmpty() )
140 {
141 SetValue(value);
142 }
2bda0e17
KB
143
144 return TRUE;
145}
146
2bda0e17
KB
147void wxComboBox::SetValue(const wxString& value)
148{
149 // If newlines are denoted by just 10, must stick 13 in front.
150 int singletons = 0;
151 int len = value.Length();
152 int i;
153 for (i = 0; i < len; i ++)
154 {
155 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
156 singletons ++;
157 }
158 if (singletons > 0)
159 {
837e5743 160 wxChar *tmp = new wxChar[len + singletons + 1];
2bda0e17
KB
161 int j = 0;
162 for (i = 0; i < len; i ++)
163 {
164 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
165 {
166 tmp[j] = 13;
167 j ++;
168 }
169 tmp[j] = value[i];
170 j ++;
171 }
172 tmp[j] = 0;
4438caf4 173 SetWindowText(GetHwnd(), tmp);
2bda0e17
KB
174 delete[] tmp;
175 }
176 else
4438caf4 177 SetWindowText(GetHwnd(), value);
2bda0e17
KB
178}
179
180// Clipboard operations
1c4a764c 181void wxComboBox::Copy()
2bda0e17 182{
4438caf4 183 HWND hWnd = GetHwnd();
2bda0e17
KB
184 SendMessage(hWnd, WM_COPY, 0, 0L);
185}
186
1c4a764c 187void wxComboBox::Cut()
2bda0e17 188{
4438caf4 189 HWND hWnd = GetHwnd();
2bda0e17
KB
190 SendMessage(hWnd, WM_CUT, 0, 0L);
191}
192
1c4a764c 193void wxComboBox::Paste()
2bda0e17 194{
4438caf4 195 HWND hWnd = GetHwnd();
2bda0e17
KB
196 SendMessage(hWnd, WM_PASTE, 0, 0L);
197}
198
debe6624 199void wxComboBox::SetEditable(bool editable)
2bda0e17
KB
200{
201 // Can't implement in MSW?
4438caf4 202// HWND hWnd = GetHwnd();
2bda0e17
KB
203// SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
204}
205
debe6624 206void wxComboBox::SetInsertionPoint(long pos)
2bda0e17
KB
207{
208/*
4438caf4 209 HWND hWnd = GetHwnd();
2bda0e17
KB
210#ifdef __WIN32__
211 SendMessage(hWnd, EM_SETSEL, pos, pos);
212 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
213#else
214 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
215#endif
216 char *nothing = "";
217 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
218*/
219}
220
1c4a764c 221void wxComboBox::SetInsertionPointEnd()
2bda0e17
KB
222{
223/*
224 long pos = GetLastPosition();
225 SetInsertionPoint(pos);
226*/
227}
228
1c4a764c 229long wxComboBox::GetInsertionPoint() const
2bda0e17
KB
230{
231/*
4438caf4 232 DWORD Pos=(DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
2bda0e17
KB
233 return Pos&0xFFFF;
234*/
235 return 0;
236}
237
1c4a764c 238long wxComboBox::GetLastPosition() const
2bda0e17
KB
239{
240/*
4438caf4 241 HWND hWnd = GetHwnd();
2bda0e17
KB
242
243 // Will always return a number > 0 (according to docs)
244 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
245
246 // This gets the char index for the _beginning_ of the last line
247 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
4438caf4 248
2bda0e17
KB
249 // Get number of characters in the last line. We'll add this to the character
250 // index for the last line, 1st position.
251 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
252
253 return (long)(charIndex + lineLength);
254*/
255 return 0;
256}
257
debe6624 258void wxComboBox::Replace(long from, long to, const wxString& value)
2bda0e17 259{
47d67540 260#if wxUSE_CLIPBOARD
4438caf4 261 HWND hWnd = GetHwnd();
2bda0e17
KB
262 long fromChar = from;
263 long toChar = to;
4438caf4 264
2bda0e17
KB
265 // Set selection and remove it
266#ifdef __WIN32__
267 SendMessage(hWnd, CB_SETEDITSEL, fromChar, toChar);
268#else
269 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
270#endif
271 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
272
273 // Now replace with 'value', by pasting.
837e5743 274 wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0);
2bda0e17
KB
275
276 // Paste into edit control
277 SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
278#endif
279}
280
debe6624 281void wxComboBox::Remove(long from, long to)
2bda0e17 282{
4438caf4 283 HWND hWnd = GetHwnd();
2bda0e17
KB
284 long fromChar = from;
285 long toChar = to;
4438caf4 286
2bda0e17
KB
287 // Cut all selected text
288#ifdef __WIN32__
289 SendMessage(hWnd, CB_SETEDITSEL, fromChar, toChar);
290#else
291 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
292#endif
293 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
294}
295
debe6624 296void wxComboBox::SetSelection(long from, long to)
2bda0e17 297{
4438caf4 298 HWND hWnd = GetHwnd();
2bda0e17
KB
299 long fromChar = from;
300 long toChar = to;
301 // if from and to are both -1, it means
302 // (in wxWindows) that all text should be selected.
303 // This translates into Windows convention
304 if ((from == -1) && (to == -1))
305 {
306 fromChar = 0;
307 toChar = -1;
308 }
4438caf4 309
2bda0e17
KB
310#ifdef __WIN32__
311 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)fromChar, (LPARAM)toChar);
312// SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
313#else
314 // WPARAM is 0: selection is scrolled into view
315 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
316#endif
317}
318
4438caf4
VZ
319void wxComboBox::DoSetSize(int x, int y,
320 int width, int height,
321 int sizeFlags)
322{
323 wxControl::DoSetSize(x, y, width, height, sizeFlags);
4438caf4
VZ
324}
325
2bda0e17 326#endif
47d67540 327 // wxUSE_COMBOBOX
2bda0e17 328