]> git.saurik.com Git - wxWidgets.git/blame - src/msw/combobox.cpp
Committing in .
[wxWidgets.git] / src / msw / combobox.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
f6bcfd97 2// Name: msw/combobox.cpp
2bda0e17
KB
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
f6bcfd97
BP
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20#ifdef __GNUG__
21#pragma implementation "combobox.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
fddfd9e1 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
af79c52d
VZ
31#if wxUSE_COMBOBOX
32
2bda0e17 33#ifndef WX_PRECOMP
fddfd9e1
VZ
34 #include "wx/settings.h"
35 #include "wx/log.h"
2bda0e17
KB
36#endif
37
2bda0e17 38#include "wx/combobox.h"
f6bcfd97 39#include "wx/brush.h"
2bda0e17
KB
40#include "wx/clipbrd.h"
41#include "wx/msw/private.h"
42
f6bcfd97 43#if wxUSE_TOOLTIPS
ae090fdb 44 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
f6bcfd97
BP
45 #include <commctrl.h>
46 #endif
47 #include "wx/tooltip.h"
48#endif // wxUSE_TOOLTIPS
49
50// ----------------------------------------------------------------------------
51// wxWin macros
52// ----------------------------------------------------------------------------
53
2bda0e17 54IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
2bda0e17 55
f6bcfd97
BP
56// ----------------------------------------------------------------------------
57// function prototypes
58// ----------------------------------------------------------------------------
59
60LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
61 UINT message,
62 WPARAM wParam,
63 LPARAM lParam);
64
65// ---------------------------------------------------------------------------
66// global vars
67// ---------------------------------------------------------------------------
68
69// the pointer to standard radio button wnd proc
70static WXFARPROC gs_wndprocEdit = (WXFARPROC)NULL;
71
72// ============================================================================
73// implementation
74// ============================================================================
75
76// ----------------------------------------------------------------------------
77// wnd proc for subclassed edit control
78// ----------------------------------------------------------------------------
79
80LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
81 UINT message,
82 WPARAM wParam,
83 LPARAM lParam)
84{
85 HWND hwndCombo = ::GetParent(hWnd);
86 wxWindow *win = wxFindWinFromHandle((WXHWND)hwndCombo);
87
88 switch ( message )
89 {
53c3a78b
VZ
90 // forward some messages to the combobox to generate the appropriate
91 // wxEvents from them
f6bcfd97
BP
92 case WM_KEYUP:
93 case WM_KEYDOWN:
94 case WM_CHAR:
53c3a78b
VZ
95 case WM_SETFOCUS:
96 case WM_KILLFOCUS:
f6bcfd97
BP
97 {
98 wxComboBox *combo = wxDynamicCast(win, wxComboBox);
99 wxCHECK_MSG( combo, 0, _T("should have combo as parent") );
100
101 if ( combo->MSWProcessEditMsg(message, wParam, lParam) )
102 return 0;
103 }
104 break;
105
106#if 0
107 case WM_GETDLGCODE:
108 {
109 wxCHECK_MSG( win, 0, _T("should have a parent") );
110
111 if ( win->GetWindowStyle() & wxPROCESS_ENTER )
112 {
113 // need to return a custom dlg code or we'll never get it
114 return DLGC_WANTMESSAGE;
115 }
116 }
117 break;
118#endif // 0
119
120 // deal with tooltips here
ae090fdb 121#if wxUSE_TOOLTIPS && defined(TTN_NEEDTEXT)
f6bcfd97
BP
122 case WM_NOTIFY:
123 {
124 wxCHECK_MSG( win, 0, _T("should have a parent") );
125
126 NMHDR* hdr = (NMHDR *)lParam;
127 if ( (int)hdr->code == TTN_NEEDTEXT )
128 {
129 wxToolTip *tooltip = win->GetToolTip();
130 if ( tooltip )
131 {
132 TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
133 ttt->lpszText = (wxChar *)tooltip->GetTip().c_str();
134 }
135
136 // processed
137 return 0;
138 }
139 }
140 break;
141#endif // wxUSE_TOOLTIPS
142 }
143
144 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit, hWnd, message, wParam, lParam);
145}
146
33ac7e6f 147WXHBRUSH wxComboBox::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
788722ac
JS
148#if wxUSE_CTL3D
149 WXUINT message,
150 WXWPARAM wParam,
151 WXLPARAM lParam
152#else
33ac7e6f
KB
153 WXUINT WXUNUSED(message),
154 WXWPARAM WXUNUSED(wParam),
788722ac
JS
155 WXLPARAM WXUNUSED(lParam)
156#endif
157 )
f6bcfd97
BP
158{
159#if wxUSE_CTL3D
160 if ( m_useCtl3D )
161 {
162 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
163 return (WXHBRUSH) hbrush;
164 }
165#endif // wxUSE_CTL3D
166
167 HDC hdc = (HDC)pDC;
168 if (GetParent()->GetTransparentBackground())
169 SetBkMode(hdc, TRANSPARENT);
170 else
171 SetBkMode(hdc, OPAQUE);
172
173 wxColour colBack = GetBackgroundColour();
174
175 if (!IsEnabled())
a756f210 176 colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
f6bcfd97
BP
177
178 ::SetBkColor(hdc, wxColourToRGB(colBack));
179 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
180
181 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
182
183 return (WXHBRUSH)brush->GetResourceHandle();
184}
185
186// ----------------------------------------------------------------------------
187// wxComboBox
188// ----------------------------------------------------------------------------
189
190bool wxComboBox::MSWProcessEditMsg(WXUINT msg, WXWPARAM wParam, WXLPARAM lParam)
191{
192 switch ( msg )
193 {
194 case WM_CHAR:
195 return HandleChar(wParam, lParam, TRUE /* isASCII */);
889f0b7c 196
f6bcfd97
BP
197 case WM_KEYDOWN:
198 return HandleKeyDown(wParam, lParam);
199
200 case WM_KEYUP:
201 return HandleKeyUp(wParam, lParam);
53c3a78b
VZ
202
203 case WM_SETFOCUS:
204 return HandleSetFocus((WXHWND)wParam);
205
206 case WM_KILLFOCUS:
207 return HandleKillFocus((WXHWND)wParam);
f6bcfd97
BP
208 }
209
210 return FALSE;
211}
212
debe6624 213bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 214{
fddfd9e1
VZ
215 wxString value;
216 int sel = -1;
cd0b1709 217 switch ( param )
8c1c5302 218 {
cd0b1709 219 case CBN_SELCHANGE:
fddfd9e1
VZ
220 sel = GetSelection();
221 if ( sel > -1 )
cd0b1709 222 {
fddfd9e1
VZ
223 value = GetString(sel);
224
cd0b1709 225 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId());
fddfd9e1 226 event.SetInt(sel);
cd0b1709 227 event.SetEventObject(this);
fddfd9e1 228 event.SetString(value);
cd0b1709
VZ
229 ProcessCommand(event);
230 }
fddfd9e1
VZ
231 else
232 {
233 break;
234 }
235
236 // fall through: for compability with wxGTK, also send the text
237 // update event when the selection changes (this also seems more
238 // logical as the text does change)
29006414 239
cd0b1709
VZ
240 case CBN_EDITCHANGE:
241 {
7ba166dd 242 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
3adc70bf 243
fddfd9e1
VZ
244 // if sel != -1, value was initialized above (and we can't use
245 // GetValue() here as it would return the old selection and we
246 // want the new one)
7ba166dd 247 if ( sel == -1 )
3adc70bf 248 {
7ba166dd 249 value = GetValue();
3adc70bf
VZ
250 }
251 else // we're synthesizing text updated event from sel change
252 {
253 // we need to do this because the user code expects
254 // wxComboBox::GetValue() to return the new value from
255 // "text updated" handler but it hadn't been updated yet
256 SetValue(value);
257 }
258
7ba166dd 259 event.SetString(value);
cd0b1709
VZ
260 event.SetEventObject(this);
261 ProcessCommand(event);
262 }
263 break;
264 }
29006414 265
cd0b1709
VZ
266 // there is no return value for the CBN_ notifications, so always return
267 // FALSE from here to pass the message to DefWindowProc()
268 return FALSE;
2bda0e17
KB
269}
270
f6bcfd97
BP
271WXHWND wxComboBox::GetEditHWND() const
272{
273 // this function should not be called for wxCB_READONLY controls, it is
274 // the callers responsability to check this
275 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY),
276 _T("read-only combobox doesn't have any edit control") );
277
278 POINT pt;
279 pt.x = pt.y = 4;
280 HWND hwndEdit = ::ChildWindowFromPoint(GetHwnd(), pt);
281 if ( !hwndEdit || hwndEdit == GetHwnd() )
282 {
283 wxFAIL_MSG(_T("not read only combobox without edit control?"));
284 }
285
286 return (WXHWND)hwndEdit;
287}
288
debe6624 289bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
c085e333
VZ
290 const wxString& value,
291 const wxPoint& pos,
292 const wxSize& size,
293 int n, const wxString choices[],
294 long style,
295 const wxValidator& validator,
296 const wxString& name)
2bda0e17 297{
f6bcfd97
BP
298 // first create wxWin object
299 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
300 return FALSE;
301
302 // get the right style
303 long msStyle = WS_TABSTOP | WS_VSCROLL | WS_HSCROLL |
304 CBS_AUTOHSCROLL | CBS_NOINTEGRALHEIGHT /* | WS_CLIPSIBLINGS */;
305 if ( style & wxCB_READONLY )
306 msStyle |= CBS_DROPDOWNLIST;
307 else if ( style & wxCB_SIMPLE )
308 msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
309 else
310 msStyle |= CBS_DROPDOWN;
311
312 if ( style & wxCB_SORT )
313 msStyle |= CBS_SORT;
314
b0766406
JS
315 if ( style & wxCLIP_SIBLINGS )
316 msStyle |= WS_CLIPSIBLINGS;
317
318
f6bcfd97
BP
319 // and now create the MSW control
320 if ( !MSWCreateControl(_T("COMBOBOX"), msStyle) )
321 return FALSE;
322
f6bcfd97
BP
323 // A choice/combobox normally has a white background (or other, depending
324 // on global settings) rather than inheriting the parent's background colour.
a756f210 325 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
f6bcfd97
BP
326
327 for ( int i = 0; i < n; i++ )
328 {
329 Append(choices[i]);
330 }
c085e333 331
f6bcfd97
BP
332 if ( !value.IsEmpty() )
333 {
334 SetValue(value);
335 }
2bda0e17 336
db34b147
VZ
337 // do this after appending the values to the combobox so that autosizing
338 // works correctly
339 SetSize(pos.x, pos.y, size.x, size.y);
340
f6bcfd97
BP
341 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
342 // and an edit control inside it and if we want to catch events from this
343 // edit control, we must subclass it as well
344 if ( !(style & wxCB_READONLY) )
345 {
346 gs_wndprocEdit = (WXFARPROC)::SetWindowLong
347 (
348 (HWND)GetEditHWND(),
349 GWL_WNDPROC,
350 (LPARAM)wxComboEditWndProc
351 );
352 }
2bda0e17 353
f6bcfd97 354 return TRUE;
2bda0e17
KB
355}
356
f6bcfd97
BP
357// TODO: update and clear all this horrible mess (VZ)
358
2bda0e17
KB
359void wxComboBox::SetValue(const wxString& value)
360{
361 // If newlines are denoted by just 10, must stick 13 in front.
362 int singletons = 0;
363 int len = value.Length();
364 int i;
365 for (i = 0; i < len; i ++)
366 {
367 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
368 singletons ++;
369 }
370 if (singletons > 0)
371 {
837e5743 372 wxChar *tmp = new wxChar[len + singletons + 1];
2bda0e17
KB
373 int j = 0;
374 for (i = 0; i < len; i ++)
375 {
376 if ((i > 0) && (value[i] == 10) && (value[i-1] != 13))
377 {
378 tmp[j] = 13;
379 j ++;
380 }
381 tmp[j] = value[i];
382 j ++;
383 }
384 tmp[j] = 0;
4438caf4 385 SetWindowText(GetHwnd(), tmp);
2bda0e17
KB
386 delete[] tmp;
387 }
388 else
4438caf4 389 SetWindowText(GetHwnd(), value);
2bda0e17
KB
390}
391
392// Clipboard operations
1c4a764c 393void wxComboBox::Copy()
2bda0e17 394{
4438caf4 395 HWND hWnd = GetHwnd();
2bda0e17
KB
396 SendMessage(hWnd, WM_COPY, 0, 0L);
397}
398
1c4a764c 399void wxComboBox::Cut()
2bda0e17 400{
4438caf4 401 HWND hWnd = GetHwnd();
2bda0e17
KB
402 SendMessage(hWnd, WM_CUT, 0, 0L);
403}
404
1c4a764c 405void wxComboBox::Paste()
2bda0e17 406{
4438caf4 407 HWND hWnd = GetHwnd();
2bda0e17
KB
408 SendMessage(hWnd, WM_PASTE, 0, 0L);
409}
410
33ac7e6f 411void wxComboBox::SetEditable(bool WXUNUSED(editable))
2bda0e17
KB
412{
413 // Can't implement in MSW?
4438caf4 414// HWND hWnd = GetHwnd();
2bda0e17
KB
415// SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
416}
417
debe6624 418void wxComboBox::SetInsertionPoint(long pos)
2bda0e17 419{
6d14cac7
VZ
420 if ( GetWindowStyle() & wxCB_READONLY )
421 return;
422
2bda0e17 423#ifdef __WIN32__
6d14cac7
VZ
424 HWND hWnd = GetHwnd();
425 ::SendMessage(hWnd, CB_SETEDITSEL, 0, MAKELPARAM(pos, pos));
426 HWND hEditWnd = (HWND) GetEditHWND() ;
427 if ( hEditWnd )
428 {
429 // Scroll insertion point into view
430 SendMessage(hEditWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
431 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
432 SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM)_T(""));
433 }
434#endif // __WIN32__
2bda0e17
KB
435}
436
1c4a764c 437void wxComboBox::SetInsertionPointEnd()
2bda0e17 438{
6d14cac7
VZ
439 // setting insertion point doesn't make sense for read only comboboxes
440 if ( !(GetWindowStyle() & wxCB_READONLY) )
441 {
442 long pos = GetLastPosition();
443 SetInsertionPoint(pos);
444 }
2bda0e17
KB
445}
446
1c4a764c 447long wxComboBox::GetInsertionPoint() const
2bda0e17 448{
f7703477
JS
449#ifdef __WIN32__
450 DWORD Pos=(DWORD)SendMessage(GetHwnd(), CB_GETEDITSEL, 0, 0L);
451 return Pos&0xFFFF;
452#else
453 return 0;
454#endif
2bda0e17
KB
455}
456
1c4a764c 457long wxComboBox::GetLastPosition() const
2bda0e17 458{
f7703477 459 HWND hEditWnd = (HWND) GetEditHWND();
4438caf4 460
f7703477 461 // Get number of characters in the last (only) line. We'll add this to the character
2bda0e17 462 // index for the last line, 1st position.
f7703477 463 int lineLength = (int)SendMessage(hEditWnd, EM_LINELENGTH, (WPARAM) 0, (LPARAM)0L);
2bda0e17 464
f7703477 465 return (long)(lineLength);
2bda0e17
KB
466}
467
debe6624 468void wxComboBox::Replace(long from, long to, const wxString& value)
2bda0e17 469{
47d67540 470#if wxUSE_CLIPBOARD
fddfd9e1 471 Remove(from, to);
2bda0e17
KB
472
473 // Now replace with 'value', by pasting.
837e5743 474 wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0);
2bda0e17
KB
475
476 // Paste into edit control
fddfd9e1 477 SendMessage(GetHwnd(), WM_PASTE, (WPARAM)0, (LPARAM)0L);
2bda0e17
KB
478#endif
479}
480
debe6624 481void wxComboBox::Remove(long from, long to)
2bda0e17 482{
fddfd9e1
VZ
483 // Set selection and remove it
484 SetSelection(from, to);
485 SendMessage(GetHwnd(), WM_CUT, (WPARAM)0, (LPARAM)0);
2bda0e17
KB
486}
487
debe6624 488void wxComboBox::SetSelection(long from, long to)
2bda0e17 489{
4438caf4 490 HWND hWnd = GetHwnd();
2bda0e17
KB
491 long fromChar = from;
492 long toChar = to;
493 // if from and to are both -1, it means
494 // (in wxWindows) that all text should be selected.
495 // This translates into Windows convention
496 if ((from == -1) && (to == -1))
497 {
498 fromChar = 0;
499 toChar = -1;
500 }
4438caf4 501
33ac7e6f 502 if (
2bda0e17 503#ifdef __WIN32__
fddfd9e1
VZ
504 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar))
505#else // Win16
506 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)fromChar, (LPARAM)toChar)
2bda0e17 507#endif
fddfd9e1
VZ
508 == CB_ERR )
509 {
510 wxLogDebug(_T("CB_SETEDITSEL failed"));
511 }
2bda0e17
KB
512}
513
514#endif
47d67540 515 // wxUSE_COMBOBOX
2bda0e17 516