]> git.saurik.com Git - wxWidgets.git/blame - src/msw/combobox.cpp
wxFileDialog cleanup, extracted common code to fldlgcmn.cpp (patch 754187)
[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
BP
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())
a756f210 173 colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
f6bcfd97
BP
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:
2b5f62a0
VZ
192 // for compatibility with wxTextCtrl, generate a special message
193 // when Enter is pressed
194 if ( wParam == VK_RETURN )
195 {
196 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
197 InitCommandEvent(event);
198 event.SetString(GetValue());
199 event.SetInt(GetSelection());
200 ProcessCommand(event);
201 }
202
f6bcfd97 203 return HandleChar(wParam, lParam, TRUE /* isASCII */);
889f0b7c 204
f6bcfd97
BP
205 case WM_KEYDOWN:
206 return HandleKeyDown(wParam, lParam);
207
208 case WM_KEYUP:
209 return HandleKeyUp(wParam, lParam);
53c3a78b
VZ
210
211 case WM_SETFOCUS:
212 return HandleSetFocus((WXHWND)wParam);
213
214 case WM_KILLFOCUS:
215 return HandleKillFocus((WXHWND)wParam);
f6bcfd97
BP
216 }
217
218 return FALSE;
219}
220
debe6624 221bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 222{
fddfd9e1
VZ
223 wxString value;
224 int sel = -1;
cd0b1709 225 switch ( param )
8c1c5302 226 {
cd0b1709 227 case CBN_SELCHANGE:
fddfd9e1
VZ
228 sel = GetSelection();
229 if ( sel > -1 )
cd0b1709 230 {
fddfd9e1
VZ
231 value = GetString(sel);
232
cd0b1709 233 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId());
fddfd9e1 234 event.SetInt(sel);
cd0b1709 235 event.SetEventObject(this);
fddfd9e1 236 event.SetString(value);
cd0b1709
VZ
237 ProcessCommand(event);
238 }
fddfd9e1
VZ
239 else
240 {
241 break;
242 }
243
244 // fall through: for compability with wxGTK, also send the text
245 // update event when the selection changes (this also seems more
246 // logical as the text does change)
29006414 247
cd0b1709
VZ
248 case CBN_EDITCHANGE:
249 {
7ba166dd 250 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
3adc70bf 251
fddfd9e1
VZ
252 // if sel != -1, value was initialized above (and we can't use
253 // GetValue() here as it would return the old selection and we
254 // want the new one)
7ba166dd 255 if ( sel == -1 )
3adc70bf 256 {
7ba166dd 257 value = GetValue();
3adc70bf
VZ
258 }
259 else // we're synthesizing text updated event from sel change
260 {
261 // we need to do this because the user code expects
262 // wxComboBox::GetValue() to return the new value from
263 // "text updated" handler but it hadn't been updated yet
264 SetValue(value);
265 }
266
7ba166dd 267 event.SetString(value);
cd0b1709 268 event.SetEventObject(this);
0ef2ebba 269 ProcessCommand(event);
cd0b1709
VZ
270 }
271 break;
272 }
29006414 273
cd0b1709
VZ
274 // there is no return value for the CBN_ notifications, so always return
275 // FALSE from here to pass the message to DefWindowProc()
276 return FALSE;
2bda0e17
KB
277}
278
f6bcfd97
BP
279WXHWND wxComboBox::GetEditHWND() const
280{
281 // this function should not be called for wxCB_READONLY controls, it is
282 // the callers responsability to check this
283 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY),
284 _T("read-only combobox doesn't have any edit control") );
285
286 POINT pt;
287 pt.x = pt.y = 4;
288 HWND hwndEdit = ::ChildWindowFromPoint(GetHwnd(), pt);
289 if ( !hwndEdit || hwndEdit == GetHwnd() )
290 {
291 wxFAIL_MSG(_T("not read only combobox without edit control?"));
292 }
293
294 return (WXHWND)hwndEdit;
295}
296
debe6624 297bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
c085e333
VZ
298 const wxString& value,
299 const wxPoint& pos,
300 const wxSize& size,
301 int n, const wxString choices[],
302 long style,
303 const wxValidator& validator,
304 const wxString& name)
2bda0e17 305{
bdf5c30d
VZ
306 // pretend that wxComboBox is hidden while it is positioned and resized and
307 // show it only right before leaving this method because otherwise there is
308 // some noticeable flicker while the control rearranges itself
309 m_isShown = FALSE;
310
f6bcfd97
BP
311 // first create wxWin object
312 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
313 return FALSE;
314
315 // get the right style
316 long msStyle = WS_TABSTOP | WS_VSCROLL | WS_HSCROLL |
317 CBS_AUTOHSCROLL | CBS_NOINTEGRALHEIGHT /* | WS_CLIPSIBLINGS */;
318 if ( style & wxCB_READONLY )
319 msStyle |= CBS_DROPDOWNLIST;
320 else if ( style & wxCB_SIMPLE )
321 msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
322 else
323 msStyle |= CBS_DROPDOWN;
324
325 if ( style & wxCB_SORT )
326 msStyle |= CBS_SORT;
327
b0766406
JS
328 if ( style & wxCLIP_SIBLINGS )
329 msStyle |= WS_CLIPSIBLINGS;
330
331
f6bcfd97 332 // and now create the MSW control
2eb4c3aa 333 if ( !MSWCreateControl(_T("COMBOBOX"), msStyle) )
f6bcfd97
BP
334 return FALSE;
335
f6bcfd97
BP
336 // A choice/combobox normally has a white background (or other, depending
337 // on global settings) rather than inheriting the parent's background colour.
a756f210 338 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
f6bcfd97
BP
339
340 for ( int i = 0; i < n; i++ )
341 {
342 Append(choices[i]);
343 }
c085e333 344
f6bcfd97
BP
345 if ( !value.IsEmpty() )
346 {
347 SetValue(value);
348 }
2bda0e17 349
db34b147
VZ
350 // do this after appending the values to the combobox so that autosizing
351 // works correctly
352 SetSize(pos.x, pos.y, size.x, size.y);
353
f6bcfd97
BP
354 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
355 // and an edit control inside it and if we want to catch events from this
356 // edit control, we must subclass it as well
357 if ( !(style & wxCB_READONLY) )
358 {
359 gs_wndprocEdit = (WXFARPROC)::SetWindowLong
360 (
361 (HWND)GetEditHWND(),
362 GWL_WNDPROC,
363 (LPARAM)wxComboEditWndProc
364 );
365 }
2bda0e17 366
bdf5c30d
VZ
367 // and finally, show the control
368 Show(TRUE);
369
f6bcfd97 370 return TRUE;
2bda0e17
KB
371}
372
2bda0e17
KB
373void wxComboBox::SetValue(const wxString& value)
374{
2b5f62a0
VZ
375 if ( HasFlag(wxCB_READONLY) )
376 SetStringSelection(value);
b38f3ff3 377 else
2b5f62a0 378 SetWindowText(GetHwnd(), value.c_str());
2bda0e17
KB
379}
380
381// Clipboard operations
1c4a764c 382void wxComboBox::Copy()
2bda0e17 383{
2b5f62a0 384 SendMessage(GetHwnd(), WM_COPY, 0, 0L);
2bda0e17
KB
385}
386
1c4a764c 387void wxComboBox::Cut()
2bda0e17 388{
2b5f62a0 389 SendMessage(GetHwnd(), WM_CUT, 0, 0L);
2bda0e17
KB
390}
391
1c4a764c 392void wxComboBox::Paste()
2bda0e17 393{
2b5f62a0 394 SendMessage(GetHwnd(), WM_PASTE, 0, 0L);
2bda0e17
KB
395}
396
33ac7e6f 397void wxComboBox::SetEditable(bool WXUNUSED(editable))
2bda0e17
KB
398{
399 // Can't implement in MSW?
4438caf4 400// HWND hWnd = GetHwnd();
2bda0e17
KB
401// SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
402}
403
debe6624 404void wxComboBox::SetInsertionPoint(long pos)
2bda0e17 405{
6d14cac7
VZ
406 if ( GetWindowStyle() & wxCB_READONLY )
407 return;
408
2bda0e17 409#ifdef __WIN32__
6d14cac7
VZ
410 HWND hWnd = GetHwnd();
411 ::SendMessage(hWnd, CB_SETEDITSEL, 0, MAKELPARAM(pos, pos));
412 HWND hEditWnd = (HWND) GetEditHWND() ;
413 if ( hEditWnd )
414 {
415 // Scroll insertion point into view
416 SendMessage(hEditWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
417 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
418 SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM)_T(""));
419 }
420#endif // __WIN32__
2bda0e17
KB
421}
422
1c4a764c 423void wxComboBox::SetInsertionPointEnd()
2bda0e17 424{
6d14cac7
VZ
425 // setting insertion point doesn't make sense for read only comboboxes
426 if ( !(GetWindowStyle() & wxCB_READONLY) )
427 {
428 long pos = GetLastPosition();
429 SetInsertionPoint(pos);
430 }
2bda0e17
KB
431}
432
1c4a764c 433long wxComboBox::GetInsertionPoint() const
2bda0e17 434{
f7703477
JS
435#ifdef __WIN32__
436 DWORD Pos=(DWORD)SendMessage(GetHwnd(), CB_GETEDITSEL, 0, 0L);
437 return Pos&0xFFFF;
438#else
439 return 0;
440#endif
2bda0e17
KB
441}
442
1c4a764c 443long wxComboBox::GetLastPosition() const
2bda0e17 444{
f7703477 445 HWND hEditWnd = (HWND) GetEditHWND();
4438caf4 446
f7703477 447 // Get number of characters in the last (only) line. We'll add this to the character
2bda0e17 448 // index for the last line, 1st position.
f7703477 449 int lineLength = (int)SendMessage(hEditWnd, EM_LINELENGTH, (WPARAM) 0, (LPARAM)0L);
2bda0e17 450
f7703477 451 return (long)(lineLength);
2bda0e17
KB
452}
453
debe6624 454void wxComboBox::Replace(long from, long to, const wxString& value)
2bda0e17 455{
47d67540 456#if wxUSE_CLIPBOARD
fddfd9e1 457 Remove(from, to);
2bda0e17
KB
458
459 // Now replace with 'value', by pasting.
837e5743 460 wxSetClipboardData(wxDF_TEXT, (wxObject *)(const wxChar *)value, 0, 0);
2bda0e17
KB
461
462 // Paste into edit control
fddfd9e1 463 SendMessage(GetHwnd(), WM_PASTE, (WPARAM)0, (LPARAM)0L);
2bda0e17
KB
464#endif
465}
466
debe6624 467void wxComboBox::Remove(long from, long to)
2bda0e17 468{
fddfd9e1
VZ
469 // Set selection and remove it
470 SetSelection(from, to);
471 SendMessage(GetHwnd(), WM_CUT, (WPARAM)0, (LPARAM)0);
2bda0e17
KB
472}
473
debe6624 474void wxComboBox::SetSelection(long from, long to)
2bda0e17 475{
4438caf4 476 HWND hWnd = GetHwnd();
2bda0e17
KB
477 long fromChar = from;
478 long toChar = to;
479 // if from and to are both -1, it means
480 // (in wxWindows) that all text should be selected.
481 // This translates into Windows convention
482 if ((from == -1) && (to == -1))
483 {
484 fromChar = 0;
485 toChar = -1;
486 }
4438caf4 487
33ac7e6f 488 if (
2bda0e17 489#ifdef __WIN32__
fddfd9e1
VZ
490 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar))
491#else // Win16
492 SendMessage(hWnd, CB_SETEDITSEL, (WPARAM)fromChar, (LPARAM)toChar)
2bda0e17 493#endif
fddfd9e1
VZ
494 == CB_ERR )
495 {
496 wxLogDebug(_T("CB_SETEDITSEL failed"));
497 }
2bda0e17
KB
498}
499
500#endif
47d67540 501 // wxUSE_COMBOBOX
2bda0e17 502