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