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