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