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 // ============================================================================
96 // ----------------------------------------------------------------------------
97 // wnd proc for subclassed edit control
98 // ----------------------------------------------------------------------------
100 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
105 HWND hwndCombo
= ::GetParent(hWnd
);
106 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)hwndCombo
);
110 // forward some messages to the combobox to generate the appropriate
111 // wxEvents from them
121 wxComboBox
*combo
= wxDynamicCast(win
, wxComboBox
);
124 // we can get WM_KILLFOCUS while our parent is already half
125 // destroyed and hence doesn't look like a combobx any
126 // longer, check for it to avoid bogus assert failures
127 if ( !win
->IsBeingDeleted() )
129 wxFAIL_MSG( wxT("should have combo as parent") );
132 else if ( combo
->MSWProcessEditMsg(message
, wParam
, lParam
) )
142 wxCHECK_MSG( win
, 0, wxT("should have a parent") );
144 if ( win
->GetWindowStyle() & wxTE_PROCESS_ENTER
)
146 // need to return a custom dlg code or we'll never get it
147 return DLGC_WANTMESSAGE
;
153 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
156 // ----------------------------------------------------------------------------
157 // wxComboBox callbacks
158 // ----------------------------------------------------------------------------
160 WXLRESULT
wxComboBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
162 // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to
163 // set its colour correctly (to be the same as our own one)
168 // wxStaticBox can generate this message, when modifying the control's style.
169 // This causes the content of the combobox to be selected, for some reason.
170 case WM_STYLECHANGED
:
172 // combobox selection sometimes spontaneously changes when its
173 // size changes, restore it to the old value if necessary
174 if ( !GetEditHWNDIfAvailable() )
178 GetSelection(&fromOld
, &toOld
);
180 // if an editable combobox has a not empty text not from the
181 // list, it tries to autocomplete it from the list when it is
182 // resized, but we don't want this to happen as it doesn't seem
183 // to make any sense, so we forcefully restore the old text
185 if ( !HasFlag(wxCB_READONLY
) && GetCurrentSelection() == -1 )
186 textOld
= GetValue();
188 // eliminate flickering during following hacks
189 wxWindowUpdateLocker
lock(this);
191 WXLRESULT result
= wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
193 if ( !textOld
.empty() && GetValue() != textOld
)
197 GetSelection(&fromNew
, &toNew
);
199 if ( fromOld
!= fromNew
|| toOld
!= toNew
)
201 SetSelection(fromOld
, toOld
);
208 return wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
211 bool wxComboBox::MSWProcessEditMsg(WXUINT msg
, WXWPARAM wParam
, WXLPARAM lParam
)
216 // for compatibility with wxTextCtrl, generate a special message
217 // when Enter is pressed
218 if ( wParam
== VK_RETURN
)
220 if (SendMessage(GetHwnd(), CB_GETDROPPEDSTATE
, 0, 0))
223 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
225 const int sel
= GetSelection();
227 event
.SetString(GetValue());
228 InitCommandEventWithItems(event
, sel
);
230 if ( ProcessCommand(event
) )
232 // don't let the event through to the native control
233 // because it doesn't need it and may generate an annoying
234 // beep if it gets it
241 return HandleChar(wParam
, lParam
);
245 return HandleKeyDown(wParam
, lParam
);
249 return HandleKeyUp(wParam
, lParam
);
252 return HandleSetFocus((WXHWND
)wParam
);
255 return HandleKillFocus((WXHWND
)wParam
);
260 return HandleClipboardEvent(msg
);
266 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD id
)
274 // remember the last selection, just as wxChoice does
275 m_lastAcceptedSelection
= GetCurrentSelection();
276 if ( m_lastAcceptedSelection
== -1 )
278 // but unlike with wxChoice we may have no selection but still
279 // have some text and we should avoid erasing it if the drop
280 // down is cancelled (see #8474)
281 m_lastAcceptedSelection
= wxID_NONE
;
284 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_DROPDOWN
, GetId());
285 event
.SetEventObject(this);
286 ProcessCommand(event
);
291 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_CLOSEUP
, GetId());
292 event
.SetEventObject(this);
293 ProcessCommand(event
);
297 #ifndef __SMARTPHONE__
298 // we need to reset this to prevent the selection from being undone
299 // by wxChoice, see wxChoice::MSWCommand() and comments there
300 m_lastAcceptedSelection
= wxID_NONE
;
303 // set these variables so that they could be also fixed in
304 // CBN_EDITCHANGE below
305 sel
= GetSelection();
306 value
= GetStringSelection();
308 // this string is going to become the new combobox value soon but
309 // we need it to be done right now, otherwise the event handler
310 // could get a wrong value when it calls our GetValue()
311 ::SetWindowText(GetHwnd(), value
.wx_str());
314 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
316 event
.SetString(value
);
317 InitCommandEventWithItems(event
, sel
);
319 ProcessCommand(event
);
322 // fall through: for compability with wxGTK, also send the text
323 // update event when the selection changes (this also seems more
324 // logical as the text does change)
327 if ( m_allowTextEvents
)
329 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
331 // if sel != -1, value was already initialized above
334 value
= wxGetWindowText(GetHwnd());
337 event
.SetString(value
);
338 InitCommandEventWithItems(event
, sel
);
340 ProcessCommand(event
);
345 return wxChoice::MSWCommand(param
, id
);
348 // skip wxChoice version as it would generate its own events for
349 // CBN_SELENDOK and also interfere with our handling of CBN_DROPDOWN
353 bool wxComboBox::MSWShouldPreProcessMessage(WXMSG
*pMsg
)
355 // prevent command accelerators from stealing editing
356 // hotkeys when we have the focus
359 WPARAM vkey
= pMsg
->wParam
;
374 return wxChoice::MSWShouldPreProcessMessage(pMsg
);
377 WXHWND
wxComboBox::GetEditHWNDIfAvailable() const
379 #if wxUSE_DYNLIB_CLASS
380 #if defined(WINVER) && WINVER >= 0x0500
381 typedef BOOL (WINAPI
*GetComboBoxInfo_t
)(HWND
, COMBOBOXINFO
*);
382 static GetComboBoxInfo_t s_pfnGetComboBoxInfo
= NULL
;
383 static bool s_triedToLoad
= false;
384 if ( !s_triedToLoad
)
386 s_triedToLoad
= true;
387 wxLoadedDLL
dllUser32("user32.dll");
388 wxDL_INIT_FUNC(s_pfn
, GetComboBoxInfo
, dllUser32
);
391 if ( s_pfnGetComboBoxInfo
)
393 WinStruct
<COMBOBOXINFO
> info
;
394 (*s_pfnGetComboBoxInfo
)(GetHwnd(), &info
);
395 return info
.hwndItem
;
397 #endif // WINVER >= 0x0500
398 #endif // wxUSE_DYNLIB_CLASS
400 if (HasFlag(wxCB_SIMPLE
))
404 return (WXHWND
) ::ChildWindowFromPoint(GetHwnd(), pt
);
407 // notice that a slightly safer alternative could be to use FindWindowEx()
408 // but it's not available under WinCE so just take the first child for now
409 // to keep one version of the code for all platforms and fix it later if
410 // problems are discovered
412 // we assume that the only child of the combobox is the edit window
413 return (WXHWND
)::GetWindow(GetHwnd(), GW_CHILD
);
416 WXHWND
wxComboBox::GetEditHWND() const
418 // this function should not be called for wxCB_READONLY controls, it is
419 // the callers responsibility to check this
420 wxASSERT_MSG( !HasFlag(wxCB_READONLY
),
421 wxT("read-only combobox doesn't have any edit control") );
423 WXHWND hWndEdit
= GetEditHWNDIfAvailable();
424 wxASSERT_MSG( hWndEdit
, wxT("combobox without edit control?") );
429 wxWindow
*wxComboBox::GetEditableWindow()
431 wxASSERT_MSG( !HasFlag(wxCB_READONLY
),
432 wxT("read-only combobox doesn't have any edit control") );
437 // ----------------------------------------------------------------------------
438 // wxComboBox creation
439 // ----------------------------------------------------------------------------
441 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
442 const wxString
& value
,
445 int n
, const wxString choices
[],
447 const wxValidator
& validator
,
448 const wxString
& name
)
450 // pretend that wxComboBox is hidden while it is positioned and resized and
451 // show it only right before leaving this method because otherwise there is
452 // some noticeable flicker while the control rearranges itself
455 if ( !CreateAndInit(parent
, id
, pos
, size
, n
, choices
, style
,
459 // we shouldn't call SetValue() for an empty string because this would
460 // (correctly) result in an assert with a read only combobox and is useless
461 // for the other ones anyhow
462 if ( !value
.empty() )
465 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
466 // and an edit control inside it and if we want to catch events from this
467 // edit control, we must subclass it as well
468 if ( !(style
& wxCB_READONLY
) )
470 gs_wndprocEdit
= wxSetWindowProc((HWND
)GetEditHWND(), wxComboEditWndProc
);
473 // and finally, show the control
479 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
480 const wxString
& value
,
483 const wxArrayString
& choices
,
485 const wxValidator
& validator
,
486 const wxString
& name
)
488 wxCArrayString
chs(choices
);
489 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
490 chs
.GetStrings(), style
, validator
, name
);
493 WXDWORD
wxComboBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
495 // we never have an external border
496 WXDWORD msStyle
= wxChoice::MSWGetStyle
498 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
501 // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're
502 // created hidden (see Create() above), it is not done for us but we still
503 // want to have this style
504 msStyle
|= WS_TABSTOP
;
506 // remove the style always added by wxChoice
507 msStyle
&= ~CBS_DROPDOWNLIST
;
509 if ( style
& wxCB_READONLY
)
510 msStyle
|= CBS_DROPDOWNLIST
;
512 else if ( style
& wxCB_SIMPLE
)
513 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
516 msStyle
|= CBS_DROPDOWN
;
518 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
519 msStyle
|= CBS_AUTOHSCROLL
;
521 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
526 // ----------------------------------------------------------------------------
527 // wxComboBox text control-like methods
528 // ----------------------------------------------------------------------------
530 wxString
wxComboBox::GetValue() const
532 return HasFlag(wxCB_READONLY
) ? GetStringSelection()
533 : wxTextEntry::GetValue();
536 void wxComboBox::SetValue(const wxString
& value
)
538 if ( HasFlag(wxCB_READONLY
) )
539 SetStringSelection(value
);
541 wxTextEntry::SetValue(value
);
544 void wxComboBox::Clear()
547 if ( !HasFlag(wxCB_READONLY
) )
548 wxTextEntry::Clear();
551 void wxComboBox::GetSelection(long *from
, long *to
) const
553 if ( !HasFlag(wxCB_READONLY
) )
555 wxTextEntry::GetSelection(from
, to
);
557 else // text selection doesn't make sense for read only comboboxes
566 bool wxComboBox::IsEditable() const
568 return !HasFlag(wxCB_READONLY
) && wxTextEntry::IsEditable();
571 // ----------------------------------------------------------------------------
572 // standard event handling
573 // ----------------------------------------------------------------------------
575 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
580 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
585 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
590 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
595 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
600 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
605 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
610 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
612 event
.Enable( CanCut() );
615 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
617 event
.Enable( CanCopy() );
620 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
622 event
.Enable( CanPaste() );
625 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
627 event
.Enable( IsEditable() && CanUndo() );
630 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
632 event
.Enable( IsEditable() && CanRedo() );
635 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
637 event
.Enable(IsEditable() && HasSelection());
640 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
642 event
.Enable(IsEditable() && !wxTextEntry::IsEmpty());
647 void wxComboBox::DoSetToolTip(wxToolTip
*tip
)
649 wxChoice::DoSetToolTip(tip
);
651 if ( tip
&& !HasFlag(wxCB_READONLY
) )
652 tip
->AddOtherWindow(GetEditHWND());
655 #endif // wxUSE_TOOLTIPS
659 bool wxComboBox::SetHint(const wxString
& hintOrig
)
661 wxString
hint(hintOrig
);
662 if ( wxUxThemeEngine::GetIfActive() )
664 // under XP (but not Vista) there is a bug in cue banners
665 // implementation for combobox edit control: the first character is
666 // partially chopped off, so prepend a space to make it fully visible
670 return wxTextEntry::SetHint(hint
);
673 #endif // wxUSE_UXTHEME
675 #endif // wxUSE_COMBOBOX