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