1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/combobox.h"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/settings.h"
35 // for wxEVT_COMMAND_TEXT_ENTER
36 #include "wx/textctrl.h"
41 #include "wx/clipbrd.h"
42 #include "wx/dynlib.h"
43 #include "wx/wupdlock.h"
44 #include "wx/msw/private.h"
47 #include "wx/msw/uxtheme.h"
51 #include "wx/tooltip.h"
52 #endif // wxUSE_TOOLTIPS
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
59 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
60 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
61 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
62 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
63 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
64 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
65 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
67 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
68 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
69 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
70 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
71 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
72 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
73 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
76 // ----------------------------------------------------------------------------
77 // function prototypes
78 // ----------------------------------------------------------------------------
80 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
85 // ---------------------------------------------------------------------------
87 // ---------------------------------------------------------------------------
89 // the pointer to standard radio button wnd proc
90 static WNDPROC gs_wndprocEdit
= (WNDPROC
)NULL
;
92 // ============================================================================
94 // ============================================================================
99 // Check if the given message should be forwarded from the edit control which
100 // is part of the combobox to wxComboBox itself. All messages generating the
101 // events that the code using wxComboBox could be interested in must be
103 bool ShouldForwardFromEditToCombo(UINT message
)
124 } // anonymous namespace
126 // ----------------------------------------------------------------------------
127 // wnd proc for subclassed edit control
128 // ----------------------------------------------------------------------------
130 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
135 HWND hwndCombo
= ::GetParent(hWnd
);
136 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)hwndCombo
);
138 if ( ShouldForwardFromEditToCombo(message
) )
140 wxComboBox
*combo
= wxDynamicCast(win
, wxComboBox
);
143 // we can get WM_KILLFOCUS while our parent is already half
144 // destroyed and hence doesn't look like a combobx any
145 // longer, check for it to avoid bogus assert failures
146 if ( !win
->IsBeingDeleted() )
148 wxFAIL_MSG( wxT("should have combo as parent") );
151 else if ( combo
->MSWProcessEditMsg(message
, wParam
, lParam
) )
157 else if ( message
== WM_GETDLGCODE
)
159 wxCHECK_MSG( win
, 0, wxT("should have a parent") );
161 if ( win
->GetWindowStyle() & wxTE_PROCESS_ENTER
)
163 // need to return a custom dlg code or we'll never get it
164 return DLGC_WANTMESSAGE
;
168 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
171 // ----------------------------------------------------------------------------
172 // wxComboBox callbacks
173 // ----------------------------------------------------------------------------
175 WXLRESULT
wxComboBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
177 // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to
178 // set its colour correctly (to be the same as our own one)
183 // wxStaticBox can generate this message, when modifying the control's style.
184 // This causes the content of the combobox to be selected, for some reason.
185 case WM_STYLECHANGED
:
187 // combobox selection sometimes spontaneously changes when its
188 // size changes, restore it to the old value if necessary
189 if ( !GetEditHWNDIfAvailable() )
193 GetSelection(&fromOld
, &toOld
);
195 // if an editable combobox has a not empty text not from the
196 // list, it tries to autocomplete it from the list when it is
197 // resized, but we don't want this to happen as it doesn't seem
198 // to make any sense, so we forcefully restore the old text
200 if ( !HasFlag(wxCB_READONLY
) && GetCurrentSelection() == -1 )
201 textOld
= GetValue();
203 // eliminate flickering during following hacks
204 wxWindowUpdateLocker
lock(this);
206 WXLRESULT result
= wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
208 if ( !textOld
.empty() && GetValue() != textOld
)
212 GetSelection(&fromNew
, &toNew
);
214 if ( fromOld
!= fromNew
|| toOld
!= toNew
)
216 SetSelection(fromOld
, toOld
);
223 return wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
226 bool wxComboBox::MSWProcessEditMsg(WXUINT msg
, WXWPARAM wParam
, WXLPARAM lParam
)
231 // for compatibility with wxTextCtrl, generate a special message
232 // when Enter is pressed
233 if ( wParam
== VK_RETURN
)
235 if (SendMessage(GetHwnd(), CB_GETDROPPEDSTATE
, 0, 0))
238 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
240 const int sel
= GetSelection();
242 event
.SetString(GetValue());
243 InitCommandEventWithItems(event
, sel
);
245 if ( ProcessCommand(event
) )
247 // don't let the event through to the native control
248 // because it doesn't need it and may generate an annoying
249 // beep if it gets it
253 // fall through, WM_CHAR is one of the message we should forward.
256 if ( ShouldForwardFromEditToCombo(msg
) )
258 // For all the messages forward from the edit control the
259 // result is not used.
261 return MSWHandleMessage(&result
, msg
, wParam
, lParam
);
268 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD id
)
276 // remember the last selection, just as wxChoice does
277 m_lastAcceptedSelection
= GetCurrentSelection();
278 if ( m_lastAcceptedSelection
== -1 )
280 // but unlike with wxChoice we may have no selection but still
281 // have some text and we should avoid erasing it if the drop
282 // down is cancelled (see #8474)
283 m_lastAcceptedSelection
= wxID_NONE
;
286 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_DROPDOWN
, GetId());
287 event
.SetEventObject(this);
288 ProcessCommand(event
);
293 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_CLOSEUP
, GetId());
294 event
.SetEventObject(this);
295 ProcessCommand(event
);
299 #ifndef __SMARTPHONE__
300 // we need to reset this to prevent the selection from being undone
301 // by wxChoice, see wxChoice::MSWCommand() and comments there
302 m_lastAcceptedSelection
= wxID_NONE
;
305 // set these variables so that they could be also fixed in
306 // CBN_EDITCHANGE below
307 sel
= GetSelection();
308 value
= GetStringSelection();
310 // this string is going to become the new combobox value soon but
311 // we need it to be done right now, otherwise the event handler
312 // could get a wrong value when it calls our GetValue()
313 ::SetWindowText(GetHwnd(), value
.t_str());
316 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
318 event
.SetString(value
);
319 InitCommandEventWithItems(event
, sel
);
321 ProcessCommand(event
);
324 // fall through: for compability with wxGTK, also send the text
325 // update event when the selection changes (this also seems more
326 // logical as the text does change)
329 if ( m_allowTextEvents
)
331 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
333 // if sel != -1, value was already initialized above
336 value
= wxGetWindowText(GetHwnd());
339 event
.SetString(value
);
340 InitCommandEventWithItems(event
, sel
);
342 ProcessCommand(event
);
347 return wxChoice::MSWCommand(param
, id
);
350 // skip wxChoice version as it would generate its own events for
351 // CBN_SELENDOK and also interfere with our handling of CBN_DROPDOWN
355 bool wxComboBox::MSWShouldPreProcessMessage(WXMSG
*pMsg
)
357 // prevent command accelerators from stealing editing
358 // hotkeys when we have the focus
361 WPARAM vkey
= pMsg
->wParam
;
376 return wxChoice::MSWShouldPreProcessMessage(pMsg
);
379 WXHWND
wxComboBox::GetEditHWNDIfAvailable() const
381 #if wxUSE_DYNLIB_CLASS
382 #if defined(WINVER) && WINVER >= 0x0500
383 typedef BOOL (WINAPI
*GetComboBoxInfo_t
)(HWND
, COMBOBOXINFO
*);
384 static GetComboBoxInfo_t s_pfnGetComboBoxInfo
= NULL
;
385 static bool s_triedToLoad
= false;
386 if ( !s_triedToLoad
)
388 s_triedToLoad
= true;
389 wxLoadedDLL
dllUser32("user32.dll");
390 wxDL_INIT_FUNC(s_pfn
, GetComboBoxInfo
, dllUser32
);
393 if ( s_pfnGetComboBoxInfo
)
395 WinStruct
<COMBOBOXINFO
> info
;
396 (*s_pfnGetComboBoxInfo
)(GetHwnd(), &info
);
397 return info
.hwndItem
;
399 #endif // WINVER >= 0x0500
400 #endif // wxUSE_DYNLIB_CLASS
402 if (HasFlag(wxCB_SIMPLE
))
406 return (WXHWND
) ::ChildWindowFromPoint(GetHwnd(), pt
);
409 // notice that a slightly safer alternative could be to use FindWindowEx()
410 // but it's not available under WinCE so just take the first child for now
411 // to keep one version of the code for all platforms and fix it later if
412 // problems are discovered
414 // we assume that the only child of the combobox is the edit window
415 return (WXHWND
)::GetWindow(GetHwnd(), GW_CHILD
);
418 WXHWND
wxComboBox::GetEditHWND() const
420 // this function should not be called for wxCB_READONLY controls, it is
421 // the callers responsibility to check this
422 wxASSERT_MSG( !HasFlag(wxCB_READONLY
),
423 wxT("read-only combobox doesn't have any edit control") );
425 WXHWND hWndEdit
= GetEditHWNDIfAvailable();
426 wxASSERT_MSG( hWndEdit
, wxT("combobox without edit control?") );
431 wxWindow
*wxComboBox::GetEditableWindow()
433 wxASSERT_MSG( !HasFlag(wxCB_READONLY
),
434 wxT("read-only combobox doesn't have any edit control") );
439 // ----------------------------------------------------------------------------
440 // wxComboBox creation
441 // ----------------------------------------------------------------------------
443 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
444 const wxString
& value
,
447 int n
, const wxString choices
[],
449 const wxValidator
& validator
,
450 const wxString
& name
)
452 // pretend that wxComboBox is hidden while it is positioned and resized and
453 // show it only right before leaving this method because otherwise there is
454 // some noticeable flicker while the control rearranges itself
457 if ( !CreateAndInit(parent
, id
, pos
, size
, n
, choices
, style
,
461 // we shouldn't call SetValue() for an empty string because this would
462 // (correctly) result in an assert with a read only combobox and is useless
463 // for the other ones anyhow
464 if ( !value
.empty() )
467 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
468 // and an edit control inside it and if we want to catch events from this
469 // edit control, we must subclass it as well
470 if ( !(style
& wxCB_READONLY
) )
472 gs_wndprocEdit
= wxSetWindowProc((HWND
)GetEditHWND(), wxComboEditWndProc
);
475 // and finally, show the control
481 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
482 const wxString
& value
,
485 const wxArrayString
& choices
,
487 const wxValidator
& validator
,
488 const wxString
& name
)
490 wxCArrayString
chs(choices
);
491 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
492 chs
.GetStrings(), style
, validator
, name
);
495 WXDWORD
wxComboBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
497 // we never have an external border
498 WXDWORD msStyle
= wxChoice::MSWGetStyle
500 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
503 // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're
504 // created hidden (see Create() above), it is not done for us but we still
505 // want to have this style
506 msStyle
|= WS_TABSTOP
;
508 // remove the style always added by wxChoice
509 msStyle
&= ~CBS_DROPDOWNLIST
;
511 if ( style
& wxCB_READONLY
)
512 msStyle
|= CBS_DROPDOWNLIST
;
514 else if ( style
& wxCB_SIMPLE
)
515 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
518 msStyle
|= CBS_DROPDOWN
;
520 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
521 msStyle
|= CBS_AUTOHSCROLL
;
523 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
528 // ----------------------------------------------------------------------------
529 // wxComboBox text control-like methods
530 // ----------------------------------------------------------------------------
532 wxString
wxComboBox::GetValue() const
534 return HasFlag(wxCB_READONLY
) ? GetStringSelection()
535 : wxTextEntry::GetValue();
538 void wxComboBox::SetValue(const wxString
& value
)
540 if ( HasFlag(wxCB_READONLY
) )
541 SetStringSelection(value
);
543 wxTextEntry::SetValue(value
);
546 void wxComboBox::Clear()
549 if ( !HasFlag(wxCB_READONLY
) )
550 wxTextEntry::Clear();
553 bool wxComboBox::ContainsHWND(WXHWND hWnd
) const
555 return hWnd
== GetEditHWNDIfAvailable();
558 void wxComboBox::GetSelection(long *from
, long *to
) const
560 if ( !HasFlag(wxCB_READONLY
) )
562 wxTextEntry::GetSelection(from
, to
);
564 else // text selection doesn't make sense for read only comboboxes
573 bool wxComboBox::IsEditable() const
575 return !HasFlag(wxCB_READONLY
) && wxTextEntry::IsEditable();
578 // ----------------------------------------------------------------------------
579 // standard event handling
580 // ----------------------------------------------------------------------------
582 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
587 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
592 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
597 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
602 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
607 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
612 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
617 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
619 event
.Enable( CanCut() );
622 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
624 event
.Enable( CanCopy() );
627 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
629 event
.Enable( CanPaste() );
632 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
634 event
.Enable( IsEditable() && CanUndo() );
637 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
639 event
.Enable( IsEditable() && CanRedo() );
642 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
644 event
.Enable(IsEditable() && HasSelection());
647 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
649 event
.Enable(IsEditable() && !wxTextEntry::IsEmpty());
654 void wxComboBox::DoSetToolTip(wxToolTip
*tip
)
656 wxChoice::DoSetToolTip(tip
);
658 if ( tip
&& !HasFlag(wxCB_READONLY
) )
659 tip
->AddOtherWindow(GetEditHWND());
662 #endif // wxUSE_TOOLTIPS
666 bool wxComboBox::SetHint(const wxString
& hintOrig
)
668 wxString
hint(hintOrig
);
669 if ( wxUxThemeEngine::GetIfActive() )
671 // under XP (but not Vista) there is a bug in cue banners
672 // implementation for combobox edit control: the first character is
673 // partially chopped off, so prepend a space to make it fully visible
677 return wxTextEntry::SetHint(hint
);
680 #endif // wxUSE_UXTHEME
682 #endif // wxUSE_COMBOBOX