don't change the combobox text when it's being resized (see #10126)
[wxWidgets.git] / src / msw / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/combobox.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_COMBOBOX
28
29 #include "wx/combobox.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/settings.h"
34 #include "wx/log.h"
35 // for wxEVT_COMMAND_TEXT_ENTER
36 #include "wx/textctrl.h"
37 #include "wx/app.h"
38 #include "wx/brush.h"
39 #endif
40
41 #include "wx/clipbrd.h"
42 #include "wx/wupdlock.h"
43 #include "wx/msw/private.h"
44
45 #if wxUSE_TOOLTIPS
46 #include "wx/tooltip.h"
47 #endif // wxUSE_TOOLTIPS
48
49 // ----------------------------------------------------------------------------
50 // wxWin macros
51 // ----------------------------------------------------------------------------
52
53 #if wxUSE_EXTENDED_RTTI
54 WX_DEFINE_FLAGS( wxComboBoxStyle )
55
56 wxBEGIN_FLAGS( wxComboBoxStyle )
57 // new style border flags, we put them first to
58 // use them for streaming out
59 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
60 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
61 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
62 wxFLAGS_MEMBER(wxBORDER_RAISED)
63 wxFLAGS_MEMBER(wxBORDER_STATIC)
64 wxFLAGS_MEMBER(wxBORDER_NONE)
65
66 // old style border flags
67 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
68 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
69 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
70 wxFLAGS_MEMBER(wxRAISED_BORDER)
71 wxFLAGS_MEMBER(wxSTATIC_BORDER)
72 wxFLAGS_MEMBER(wxBORDER)
73
74 // standard window styles
75 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
76 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
77 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
78 wxFLAGS_MEMBER(wxWANTS_CHARS)
79 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
80 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
81 wxFLAGS_MEMBER(wxVSCROLL)
82 wxFLAGS_MEMBER(wxHSCROLL)
83
84 wxFLAGS_MEMBER(wxCB_SIMPLE)
85 wxFLAGS_MEMBER(wxCB_SORT)
86 wxFLAGS_MEMBER(wxCB_READONLY)
87 wxFLAGS_MEMBER(wxCB_DROPDOWN)
88
89 wxEND_FLAGS( wxComboBoxStyle )
90
91 IMPLEMENT_DYNAMIC_CLASS_XTI(wxComboBox, wxChoice,"wx/combobox.h")
92
93 wxBEGIN_PROPERTIES_TABLE(wxComboBox)
94 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_COMBOBOX_SELECTED , wxCommandEvent )
95 wxEVENT_PROPERTY( TextEnter , wxEVT_COMMAND_TEXT_ENTER , wxCommandEvent )
96
97 // TODO DELEGATES
98 wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
99 wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
100 wxPROPERTY( Value ,wxString, SetValue, GetValue, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
101 wxPROPERTY( Selection ,int, SetSelection, GetSelection, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
102 wxPROPERTY_FLAGS( WindowStyle , wxComboBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
103 wxEND_PROPERTIES_TABLE()
104
105 wxBEGIN_HANDLERS_TABLE(wxComboBox)
106 wxEND_HANDLERS_TABLE()
107
108 wxCONSTRUCTOR_5( wxComboBox , wxWindow* , Parent , wxWindowID , Id , wxString , Value , wxPoint , Position , wxSize , Size )
109
110 #else
111
112 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxChoice)
113
114 #endif
115
116 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
117 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
118 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
119 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
120 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
121 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
122 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
123 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
124
125 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
126 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
127 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
128 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
129 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
130 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
131 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
132 END_EVENT_TABLE()
133
134 // ----------------------------------------------------------------------------
135 // function prototypes
136 // ----------------------------------------------------------------------------
137
138 LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
139 UINT message,
140 WPARAM wParam,
141 LPARAM lParam);
142
143 // ---------------------------------------------------------------------------
144 // global vars
145 // ---------------------------------------------------------------------------
146
147 // the pointer to standard radio button wnd proc
148 static WNDPROC gs_wndprocEdit = (WNDPROC)NULL;
149
150 // ============================================================================
151 // implementation
152 // ============================================================================
153
154 // ----------------------------------------------------------------------------
155 // wnd proc for subclassed edit control
156 // ----------------------------------------------------------------------------
157
158 LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
159 UINT message,
160 WPARAM wParam,
161 LPARAM lParam)
162 {
163 HWND hwndCombo = ::GetParent(hWnd);
164 wxWindow *win = wxFindWinFromHandle((WXHWND)hwndCombo);
165
166 switch ( message )
167 {
168 // forward some messages to the combobox to generate the appropriate
169 // wxEvents from them
170 case WM_KEYUP:
171 case WM_KEYDOWN:
172 case WM_CHAR:
173 case WM_SYSCHAR:
174 case WM_SYSKEYDOWN:
175 case WM_SYSKEYUP:
176 case WM_SETFOCUS:
177 case WM_KILLFOCUS:
178 {
179 wxComboBox *combo = wxDynamicCast(win, wxComboBox);
180 if ( !combo )
181 {
182 // we can get WM_KILLFOCUS while our parent is already half
183 // destroyed and hence doesn't look like a combobx any
184 // longer, check for it to avoid bogus assert failures
185 if ( !win->IsBeingDeleted() )
186 {
187 wxFAIL_MSG( _T("should have combo as parent") );
188 }
189 }
190 else if ( combo->MSWProcessEditMsg(message, wParam, lParam) )
191 {
192 // handled by parent
193 return 0;
194 }
195 }
196 break;
197
198 case WM_GETDLGCODE:
199 {
200 wxCHECK_MSG( win, 0, _T("should have a parent") );
201
202 if ( win->GetWindowStyle() & wxTE_PROCESS_ENTER )
203 {
204 // need to return a custom dlg code or we'll never get it
205 return DLGC_WANTMESSAGE;
206 }
207 }
208 break;
209 }
210
211 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit, hWnd, message, wParam, lParam);
212 }
213
214 // ----------------------------------------------------------------------------
215 // wxComboBox callbacks
216 // ----------------------------------------------------------------------------
217
218 WXLRESULT wxComboBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
219 {
220 // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to
221 // set its colour correctly (to be the same as our own one)
222
223 switch ( nMsg )
224 {
225 case WM_SIZE:
226 // wxStaticBox can generate this message, when modifying the control's style.
227 // This causes the content of the combobox to be selected, for some reason.
228 case WM_STYLECHANGED:
229 {
230 // combobox selection sometimes spontaneously changes when its
231 // size changes, restore it to the old value if necessary
232 if ( !GetEditHWNDIfAvailable() )
233 break;
234
235 long fromOld, toOld;
236 GetSelection(&fromOld, &toOld);
237
238 // if an editable combobox has a not empty text not from the
239 // list, it tries to autocomplete it from the list when it is
240 // resized, but we don't want this to happen as it doesn't seem
241 // to make any sense, so we forcefully restore the old text
242 wxString textOld;
243 if ( !HasFlag(wxCB_READONLY) && GetCurrentSelection() == -1 )
244 textOld = GetValue();
245
246 // eliminate flickering during following hacks
247 wxWindowUpdateLocker lock(this);
248
249 WXLRESULT result = wxChoice::MSWWindowProc(nMsg, wParam, lParam);
250
251 if ( !textOld.empty() && GetValue() != textOld )
252 SetLabel(textOld);
253
254 long fromNew, toNew;
255 GetSelection(&fromNew, &toNew);
256
257 if ( fromOld != fromNew || toOld != toNew )
258 {
259 SetSelection(fromOld, toOld);
260 }
261
262 return result;
263 }
264 }
265
266 return wxChoice::MSWWindowProc(nMsg, wParam, lParam);
267 }
268
269 bool wxComboBox::MSWProcessEditMsg(WXUINT msg, WXWPARAM wParam, WXLPARAM lParam)
270 {
271 switch ( msg )
272 {
273 case WM_CHAR:
274 // for compatibility with wxTextCtrl, generate a special message
275 // when Enter is pressed
276 if ( wParam == VK_RETURN )
277 {
278 if (SendMessage(GetHwnd(), CB_GETDROPPEDSTATE, 0, 0))
279 return false;
280
281 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
282
283 const int sel = GetSelection();
284 event.SetInt(sel);
285 event.SetString(GetValue());
286 InitCommandEventWithItems(event, sel);
287
288 if ( ProcessCommand(event) )
289 {
290 // don't let the event through to the native control
291 // because it doesn't need it and may generate an annoying
292 // beep if it gets it
293 return true;
294 }
295 }
296 // fall through
297
298 case WM_SYSCHAR:
299 return HandleChar(wParam, lParam, true /* isASCII */);
300
301 case WM_SYSKEYDOWN:
302 case WM_KEYDOWN:
303 return HandleKeyDown(wParam, lParam);
304
305 case WM_SYSKEYUP:
306 case WM_KEYUP:
307 return HandleKeyUp(wParam, lParam);
308
309 case WM_SETFOCUS:
310 return HandleSetFocus((WXHWND)wParam);
311
312 case WM_KILLFOCUS:
313 return HandleKillFocus((WXHWND)wParam);
314
315 case WM_CUT:
316 case WM_COPY:
317 case WM_PASTE:
318 return HandleClipboardEvent(msg);
319 }
320
321 return false;
322 }
323
324 bool wxComboBox::MSWCommand(WXUINT param, WXWORD id)
325 {
326 int sel = -1;
327 wxString value;
328
329 switch ( param )
330 {
331 case CBN_DROPDOWN:
332 // remember the last selection, just as wxChoice does
333 m_lastAcceptedSelection = GetCurrentSelection();
334 if ( m_lastAcceptedSelection == -1 )
335 {
336 // but unlike with wxChoice we may have no selection but still
337 // have some text and we should avoid erasing it if the drop
338 // down is cancelled (see #8474)
339 m_lastAcceptedSelection = wxID_NONE;
340 }
341 break;
342
343 case CBN_SELENDOK:
344 #ifndef __SMARTPHONE__
345 // we need to reset this to prevent the selection from being undone
346 // by wxChoice, see wxChoice::MSWCommand() and comments there
347 m_lastAcceptedSelection = wxID_NONE;
348 #endif
349
350 // set these variables so that they could be also fixed in
351 // CBN_EDITCHANGE below
352 sel = GetSelection();
353 value = GetStringSelection();
354
355 // this string is going to become the new combobox value soon but
356 // we need it to be done right now, otherwise the event handler
357 // could get a wrong value when it calls our GetValue()
358 ::SetWindowText(GetHwnd(), value.wx_str());
359
360 {
361 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId());
362 event.SetInt(sel);
363 event.SetString(value);
364 InitCommandEventWithItems(event, sel);
365
366 ProcessCommand(event);
367 }
368
369 // fall through: for compability with wxGTK, also send the text
370 // update event when the selection changes (this also seems more
371 // logical as the text does change)
372
373 case CBN_EDITCHANGE:
374 if ( m_allowTextEvents )
375 {
376 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
377
378 // if sel != -1, value was already initialized above
379 if ( sel == -1 )
380 {
381 value = wxGetWindowText(GetHwnd());
382 }
383
384 event.SetString(value);
385 InitCommandEventWithItems(event, sel);
386
387 ProcessCommand(event);
388 }
389 break;
390
391 default:
392 return wxChoice::MSWCommand(param, id);
393 }
394
395 // skip wxChoice version as it would generate its own events for
396 // CBN_SELENDOK and also interfere with our handling of CBN_DROPDOWN
397 return true;
398 }
399
400 bool wxComboBox::MSWShouldPreProcessMessage(WXMSG *pMsg)
401 {
402 // prevent command accelerators from stealing editing
403 // hotkeys when we have the focus
404 if (wxIsCtrlDown())
405 {
406 WPARAM vkey = pMsg->wParam;
407
408 switch (vkey)
409 {
410 case 'C':
411 case 'V':
412 case 'X':
413 case VK_INSERT:
414 case VK_DELETE:
415 case VK_HOME:
416 case VK_END:
417 return false;
418 }
419 }
420
421 return wxChoice::MSWShouldPreProcessMessage(pMsg);
422 }
423
424 WXHWND wxComboBox::GetEditHWNDIfAvailable() const
425 {
426 // notice that a slightly safer alternative could be to use FindWindowEx()
427 // but it's not available under WinCE so just take the first child for now
428 // to keep one version of the code for all platforms and fix it later if
429 // problems are discovered
430
431 // we assume that the only child of the combobox is the edit window
432 return (WXHWND)::GetWindow(GetHwnd(), GW_CHILD);
433 }
434
435 WXHWND wxComboBox::GetEditHWND() const
436 {
437 // this function should not be called for wxCB_READONLY controls, it is
438 // the callers responsibility to check this
439 wxASSERT_MSG( !HasFlag(wxCB_READONLY),
440 _T("read-only combobox doesn't have any edit control") );
441
442 WXHWND hWndEdit = GetEditHWNDIfAvailable();
443 wxASSERT_MSG( hWndEdit, _T("combobox without edit control?") );
444
445 return hWndEdit;
446 }
447
448 // ----------------------------------------------------------------------------
449 // wxComboBox creation
450 // ----------------------------------------------------------------------------
451
452 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
453 const wxString& value,
454 const wxPoint& pos,
455 const wxSize& size,
456 int n, const wxString choices[],
457 long style,
458 const wxValidator& validator,
459 const wxString& name)
460 {
461 // pretend that wxComboBox is hidden while it is positioned and resized and
462 // show it only right before leaving this method because otherwise there is
463 // some noticeable flicker while the control rearranges itself
464 m_isShown = false;
465
466 if ( !CreateAndInit(parent, id, pos, size, n, choices, style,
467 validator, name) )
468 return false;
469
470 // we shouldn't call SetValue() for an empty string because this would
471 // (correctly) result in an assert with a read only combobox and is useless
472 // for the other ones anyhow
473 if ( !value.empty() )
474 SetValue(value);
475
476 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
477 // and an edit control inside it and if we want to catch events from this
478 // edit control, we must subclass it as well
479 if ( !(style & wxCB_READONLY) )
480 {
481 gs_wndprocEdit = wxSetWindowProc((HWND)GetEditHWND(), wxComboEditWndProc);
482 }
483
484 // and finally, show the control
485 Show(true);
486
487 return true;
488 }
489
490 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
491 const wxString& value,
492 const wxPoint& pos,
493 const wxSize& size,
494 const wxArrayString& choices,
495 long style,
496 const wxValidator& validator,
497 const wxString& name)
498 {
499 wxCArrayString chs(choices);
500 return Create(parent, id, value, pos, size, chs.GetCount(),
501 chs.GetStrings(), style, validator, name);
502 }
503
504 WXDWORD wxComboBox::MSWGetStyle(long style, WXDWORD *exstyle) const
505 {
506 // we never have an external border
507 WXDWORD msStyle = wxChoice::MSWGetStyle
508 (
509 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
510 );
511
512 // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're
513 // created hidden (see Create() above), it is not done for us but we still
514 // want to have this style
515 msStyle |= WS_TABSTOP;
516
517 // remove the style always added by wxChoice
518 msStyle &= ~CBS_DROPDOWNLIST;
519
520 if ( style & wxCB_READONLY )
521 msStyle |= CBS_DROPDOWNLIST;
522 #ifndef __WXWINCE__
523 else if ( style & wxCB_SIMPLE )
524 msStyle |= CBS_SIMPLE; // A list (shown always) and edit control
525 #endif
526 else
527 msStyle |= CBS_DROPDOWN;
528
529 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
530 msStyle |= CBS_AUTOHSCROLL;
531
532 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
533
534 return msStyle;
535 }
536
537 // ----------------------------------------------------------------------------
538 // wxComboBox text control-like methods
539 // ----------------------------------------------------------------------------
540
541 wxString wxComboBox::GetValue() const
542 {
543 return HasFlag(wxCB_READONLY) ? GetStringSelection()
544 : wxTextEntry::GetValue();
545 }
546
547 void wxComboBox::SetValue(const wxString& value)
548 {
549 if ( HasFlag(wxCB_READONLY) )
550 SetStringSelection(value);
551 else
552 wxTextEntry::SetValue(value);
553 }
554
555 void wxComboBox::Clear()
556 {
557 wxChoice::Clear();
558 if ( !HasFlag(wxCB_READONLY) )
559 wxTextEntry::Clear();
560 }
561
562 void wxComboBox::GetSelection(long *from, long *to) const
563 {
564 if ( !HasFlag(wxCB_READONLY) )
565 {
566 wxTextEntry::GetSelection(from, to);
567 }
568 else // text selection doesn't make sense for read only comboboxes
569 {
570 if ( from )
571 *from = -1;
572 if ( to )
573 *to = -1;
574 }
575 }
576
577 bool wxComboBox::IsEditable() const
578 {
579 return !HasFlag(wxCB_READONLY) && wxTextEntry::IsEditable();
580 }
581
582 // ----------------------------------------------------------------------------
583 // standard event handling
584 // ----------------------------------------------------------------------------
585
586 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
587 {
588 Cut();
589 }
590
591 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
592 {
593 Copy();
594 }
595
596 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
597 {
598 Paste();
599 }
600
601 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
602 {
603 Undo();
604 }
605
606 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
607 {
608 Redo();
609 }
610
611 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
612 {
613 RemoveSelection();
614 }
615
616 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
617 {
618 SelectAll();
619 }
620
621 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
622 {
623 event.Enable( CanCut() );
624 }
625
626 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
627 {
628 event.Enable( CanCopy() );
629 }
630
631 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
632 {
633 event.Enable( CanPaste() );
634 }
635
636 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
637 {
638 event.Enable( IsEditable() && CanUndo() );
639 }
640
641 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
642 {
643 event.Enable( IsEditable() && CanRedo() );
644 }
645
646 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
647 {
648 event.Enable(IsEditable() && HasSelection());
649 }
650
651 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
652 {
653 event.Enable(IsEditable() && !wxTextEntry::IsEmpty());
654 }
655
656 #if wxUSE_TOOLTIPS
657
658 void wxComboBox::DoSetToolTip(wxToolTip *tip)
659 {
660 wxChoice::DoSetToolTip(tip);
661
662 if ( tip && !HasFlag(wxCB_READONLY) )
663 tip->Add(GetEditHWND());
664 }
665
666 #endif // wxUSE_TOOLTIPS
667
668 #endif // wxUSE_COMBOBOX