]> git.saurik.com Git - wxWidgets.git/blame - src/msw/combobox.cpp
xti additions / changes, trying to reduce dependencies
[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$
6c9a19aa 8// Copyright: (c) Julian Smart
c085e333 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
f6bcfd97
BP
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
2bda0e17
KB
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"
2b5f62a0
VZ
36 // for wxEVT_COMMAND_TEXT_ENTER
37 #include "wx/textctrl.h"
2bda0e17
KB
38#endif
39
2bda0e17 40#include "wx/combobox.h"
f6bcfd97 41#include "wx/brush.h"
2bda0e17
KB
42#include "wx/clipbrd.h"
43#include "wx/msw/private.h"
44
f6bcfd97 45#if wxUSE_TOOLTIPS
ae090fdb 46 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
f6bcfd97
BP
47 #include <commctrl.h>
48 #endif
49 #include "wx/tooltip.h"
50#endif // wxUSE_TOOLTIPS
51
52// ----------------------------------------------------------------------------
53// wxWin macros
54// ----------------------------------------------------------------------------
55
6a89f9ee
SC
56#if wxUSE_EXTENDED_RTTI
57IMPLEMENT_DYNAMIC_CLASS_XTI(wxComboBox, wxControl,"wx/combobox.h")
58
59WX_BEGIN_PROPERTIES_TABLE(wxComboBox)
60 // TODO DELEGATES
f0a126fe 61 WX_PROPERTY( Font , wxFont , SetFont , GetFont , )
6a89f9ee
SC
62 WX_PROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings )
63 WX_PROPERTY( Value ,wxString, SetValue, GetValue, )
c44a2705 64 WX_PROPERTY( Selection ,int, SetSelection, GetSelection, )
6a89f9ee
SC
65WX_END_PROPERTIES_TABLE()
66
67WX_BEGIN_HANDLERS_TABLE(wxComboBox)
68WX_END_HANDLERS_TABLE()
2bda0e17 69
6a89f9ee
SC
70WX_CONSTRUCTOR_5( wxComboBox , wxWindow* , Parent , wxWindowID , Id , wxString , Value , wxPoint , Position , wxSize , Size )
71#else
72IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
73#endif
066f1b7a 74
f6bcfd97
BP
75// ----------------------------------------------------------------------------
76// function prototypes
77// ----------------------------------------------------------------------------
78
79LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
80 UINT message,
81 WPARAM wParam,
82 LPARAM lParam);
83
84// ---------------------------------------------------------------------------
85// global vars
86// ---------------------------------------------------------------------------
87
88// the pointer to standard radio button wnd proc
89static WXFARPROC gs_wndprocEdit = (WXFARPROC)NULL;
90
91// ============================================================================
92// implementation
93// ============================================================================
94
95// ----------------------------------------------------------------------------
96// wnd proc for subclassed edit control
97// ----------------------------------------------------------------------------
98
99LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
100 UINT message,
101 WPARAM wParam,
102 LPARAM lParam)
103{
104 HWND hwndCombo = ::GetParent(hWnd);
105 wxWindow *win = wxFindWinFromHandle((WXHWND)hwndCombo);
106
107 switch ( message )
108 {
53c3a78b
VZ
109 // forward some messages to the combobox to generate the appropriate
110 // wxEvents from them
f6bcfd97
BP
111 case WM_KEYUP:
112 case WM_KEYDOWN:
113 case WM_CHAR:
53c3a78b
VZ
114 case WM_SETFOCUS:
115 case WM_KILLFOCUS:
f6bcfd97
BP
116 {
117 wxComboBox *combo = wxDynamicCast(win, wxComboBox);
64b39766
VZ
118 if ( !combo )
119 {
120 // we can get WM_KILLFOCUS while our parent is already half
121 // destroyed and hence doesn't look like a combobx any
122 // longer, check for it to avoid bogus assert failures
123 if ( !win->IsBeingDeleted() )
124 {
125 wxFAIL_MSG( _T("should have combo as parent") );
126 }
127 }
128 else if ( combo->MSWProcessEditMsg(message, wParam, lParam) )
129 {
130 // handled by parent
f6bcfd97 131 return 0;
64b39766 132 }
f6bcfd97
BP
133 }
134 break;
135
f6bcfd97
BP
136 case WM_GETDLGCODE:
137 {
138 wxCHECK_MSG( win, 0, _T("should have a parent") );
139
140 if ( win->GetWindowStyle() & wxPROCESS_ENTER )
141 {
142 // need to return a custom dlg code or we'll never get it
143 return DLGC_WANTMESSAGE;
144 }
145 }
146 break;
f6bcfd97
BP
147
148 // deal with tooltips here
ae090fdb 149#if wxUSE_TOOLTIPS && defined(TTN_NEEDTEXT)
f6bcfd97
BP
150 case WM_NOTIFY:
151 {
152 wxCHECK_MSG( win, 0, _T("should have a parent") );
153
154 NMHDR* hdr = (NMHDR *)lParam;
2b5f62a0 155 if ( hdr->code == TTN_NEEDTEXT )
f6bcfd97
BP
156 {
157 wxToolTip *tooltip = win->GetToolTip();
158 if ( tooltip )
159 {
160 TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
161 ttt->lpszText = (wxChar *)tooltip->GetTip().c_str();
162 }
163
164 // processed
165 return 0;
166 }
167 }
168 break;
169#endif // wxUSE_TOOLTIPS
170 }
171
172 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit, hWnd, message, wParam, lParam);
173}
174
33ac7e6f
KB
175WXHBRUSH wxComboBox::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
176 WXUINT WXUNUSED(message),
177 WXWPARAM WXUNUSED(wParam),
788722ac 178 WXLPARAM WXUNUSED(lParam)
788722ac 179 )
f6bcfd97 180{
f6bcfd97 181 HDC hdc = (HDC)pDC;
f6bcfd97
BP
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:
2b5f62a0
VZ
204 // for compatibility with wxTextCtrl, generate a special message
205 // when Enter is pressed
206 if ( wParam == VK_RETURN )
207 {
208 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
209 InitCommandEvent(event);
210 event.SetString(GetValue());
211 event.SetInt(GetSelection());
212 ProcessCommand(event);
213 }
214
f6bcfd97 215 return HandleChar(wParam, lParam, TRUE /* isASCII */);
889f0b7c 216
f6bcfd97
BP
217 case WM_KEYDOWN:
218 return HandleKeyDown(wParam, lParam);
219
220 case WM_KEYUP:
221 return HandleKeyUp(wParam, lParam);
53c3a78b
VZ
222
223 case WM_SETFOCUS:
224 return HandleSetFocus((WXHWND)wParam);
225
226 case WM_KILLFOCUS:
227 return HandleKillFocus((WXHWND)wParam);
f6bcfd97
BP
228 }
229
230 return FALSE;
231}
232
debe6624 233bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 234{
fddfd9e1
VZ
235 wxString value;
236 int sel = -1;
cd0b1709 237 switch ( param )
8c1c5302 238 {
cd0b1709 239 case CBN_SELCHANGE:
fddfd9e1
VZ
240 sel = GetSelection();
241 if ( sel > -1 )
cd0b1709 242 {
fddfd9e1
VZ
243 value = GetString(sel);
244
cd0b1709 245 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId());
fddfd9e1 246 event.SetInt(sel);
cd0b1709 247 event.SetEventObject(this);
fddfd9e1 248 event.SetString(value);
cd0b1709
VZ
249 ProcessCommand(event);
250 }
fddfd9e1
VZ
251 else
252 {
253 break;
254 }
255
256 // fall through: for compability with wxGTK, also send the text
257 // update event when the selection changes (this also seems more
258 // logical as the text does change)
29006414 259
cd0b1709
VZ
260 case CBN_EDITCHANGE:
261 {
7ba166dd 262 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
3adc70bf 263
fddfd9e1
VZ
264 // if sel != -1, value was initialized above (and we can't use
265 // GetValue() here as it would return the old selection and we
266 // want the new one)
7ba166dd 267 if ( sel == -1 )
3adc70bf 268 {
7ba166dd 269 value = GetValue();
3adc70bf
VZ
270 }
271 else // we're synthesizing text updated event from sel change
272 {
273 // we need to do this because the user code expects
274 // wxComboBox::GetValue() to return the new value from
275 // "text updated" handler but it hadn't been updated yet
276 SetValue(value);
277 }
278
7ba166dd 279 event.SetString(value);
cd0b1709 280 event.SetEventObject(this);
0ef2ebba 281 ProcessCommand(event);
cd0b1709
VZ
282 }
283 break;
284 }
29006414 285
cd0b1709
VZ
286 // there is no return value for the CBN_ notifications, so always return
287 // FALSE from here to pass the message to DefWindowProc()
288 return FALSE;
2bda0e17
KB
289}
290
f6bcfd97
BP
291WXHWND wxComboBox::GetEditHWND() const
292{
293 // this function should not be called for wxCB_READONLY controls, it is
294 // the callers responsability to check this
295 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY),
296 _T("read-only combobox doesn't have any edit control") );
297
298 POINT pt;
299 pt.x = pt.y = 4;
300 HWND hwndEdit = ::ChildWindowFromPoint(GetHwnd(), pt);
301 if ( !hwndEdit || hwndEdit == GetHwnd() )
302 {
303 wxFAIL_MSG(_T("not read only combobox without edit control?"));
304 }
305
306 return (WXHWND)hwndEdit;
307}
308
debe6624 309bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
c085e333
VZ
310 const wxString& value,
311 const wxPoint& pos,
312 const wxSize& size,
313 int n, const wxString choices[],
314 long style,
315 const wxValidator& validator,
316 const wxString& name)
2bda0e17 317{
bdf5c30d
VZ
318 // pretend that wxComboBox is hidden while it is positioned and resized and
319 // show it only right before leaving this method because otherwise there is
320 // some noticeable flicker while the control rearranges itself
321 m_isShown = FALSE;
322
f6bcfd97
BP
323 // first create wxWin object
324 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
325 return FALSE;
326
327 // get the right style
328 long msStyle = WS_TABSTOP | WS_VSCROLL | WS_HSCROLL |
329 CBS_AUTOHSCROLL | CBS_NOINTEGRALHEIGHT /* | WS_CLIPSIBLINGS */;
330 if ( style & wxCB_READONLY )
331 msStyle |= CBS_DROPDOWNLIST;
4676948b 332#ifndef __WXWINCE__
f6bcfd97
BP
333 else if ( style & wxCB_SIMPLE )
334 msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
4676948b 335#endif
f6bcfd97
BP
336 else
337 msStyle |= CBS_DROPDOWN;
338
339 if ( style & wxCB_SORT )
340 msStyle |= CBS_SORT;
341
b0766406
JS
342 if ( style & wxCLIP_SIBLINGS )
343 msStyle |= WS_CLIPSIBLINGS;
344
345
f6bcfd97 346 // and now create the MSW control
2eb4c3aa 347 if ( !MSWCreateControl(_T("COMBOBOX"), msStyle) )
f6bcfd97
BP
348 return FALSE;
349
f6bcfd97
BP
350 // A choice/combobox normally has a white background (or other, depending
351 // on global settings) rather than inheriting the parent's background colour.
a756f210 352 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
f6bcfd97
BP
353
354 for ( int i = 0; i < n; i++ )
355 {
356 Append(choices[i]);
357 }
c085e333 358
f6bcfd97
BP
359 if ( !value.IsEmpty() )
360 {
361 SetValue(value);
362 }
2bda0e17 363
db34b147
VZ
364 // do this after appending the values to the combobox so that autosizing
365 // works correctly
366 SetSize(pos.x, pos.y, size.x, size.y);
367
f6bcfd97
BP
368 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
369 // and an edit control inside it and if we want to catch events from this
370 // edit control, we must subclass it as well
371 if ( !(style & wxCB_READONLY) )
372 {
373 gs_wndprocEdit = (WXFARPROC)::SetWindowLong
374 (
375 (HWND)GetEditHWND(),
376 GWL_WNDPROC,
377 (LPARAM)wxComboEditWndProc
378 );
379 }
2bda0e17 380
bdf5c30d
VZ
381 // and finally, show the control
382 Show(TRUE);
383
f6bcfd97 384 return TRUE;
2bda0e17
KB
385}
386
2bda0e17
KB
387void wxComboBox::SetValue(const wxString& value)
388{
2b5f62a0
VZ
389 if ( HasFlag(wxCB_READONLY) )
390 SetStringSelection(value);
b38f3ff3 391 else
2b5f62a0 392 SetWindowText(GetHwnd(), value.c_str());
2bda0e17
KB
393}
394
395// Clipboard operations
1c4a764c 396void wxComboBox::Copy()
2bda0e17 397{
2b5f62a0 398 SendMessage(GetHwnd(), WM_COPY, 0, 0L);
2bda0e17
KB
399}
400
1c4a764c 401void wxComboBox::Cut()
2bda0e17 402{
2b5f62a0 403 SendMessage(GetHwnd(), WM_CUT, 0, 0L);
2bda0e17
KB
404}
405
1c4a764c 406void wxComboBox::Paste()
2bda0e17 407{
2b5f62a0 408 SendMessage(GetHwnd(), WM_PASTE, 0, 0L);
2bda0e17
KB
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)
fda7962d 432 SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM) wxEmptyString);
6d14cac7
VZ
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