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