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