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