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