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