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 #if wxUSE_EXTENDED_RTTI
59 WX_DEFINE_FLAGS( wxComboBoxStyle
)
61 wxBEGIN_FLAGS( wxComboBoxStyle
)
62 // new style border flags, we put them first to
63 // use them for streaming out
64 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
65 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
66 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
67 wxFLAGS_MEMBER(wxBORDER_RAISED
)
68 wxFLAGS_MEMBER(wxBORDER_STATIC
)
69 wxFLAGS_MEMBER(wxBORDER_NONE
)
71 // old style border flags
72 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
73 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
74 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
75 wxFLAGS_MEMBER(wxRAISED_BORDER
)
76 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
77 wxFLAGS_MEMBER(wxBORDER
)
79 // standard window styles
80 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
81 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
82 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
83 wxFLAGS_MEMBER(wxWANTS_CHARS
)
84 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
85 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
86 wxFLAGS_MEMBER(wxVSCROLL
)
87 wxFLAGS_MEMBER(wxHSCROLL
)
89 wxFLAGS_MEMBER(wxCB_SIMPLE
)
90 wxFLAGS_MEMBER(wxCB_SORT
)
91 wxFLAGS_MEMBER(wxCB_READONLY
)
92 wxFLAGS_MEMBER(wxCB_DROPDOWN
)
94 wxEND_FLAGS( wxComboBoxStyle
)
96 IMPLEMENT_DYNAMIC_CLASS_XTI(wxComboBox
, wxChoice
,"wx/combobox.h")
98 wxBEGIN_PROPERTIES_TABLE(wxComboBox
)
99 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_COMBOBOX_SELECTED
, wxCommandEvent
)
100 wxEVENT_PROPERTY( TextEnter
, wxEVT_COMMAND_TEXT_ENTER
, wxCommandEvent
)
103 wxPROPERTY( Font
, wxFont
, SetFont
, GetFont
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
104 wxPROPERTY_COLLECTION( Choices
, wxArrayString
, wxString
, AppendString
, GetStrings
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
105 wxPROPERTY( Value
,wxString
, SetValue
, GetValue
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
106 wxPROPERTY( Selection
,int, SetSelection
, GetSelection
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
107 wxPROPERTY_FLAGS( WindowStyle
, wxComboBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
108 wxEND_PROPERTIES_TABLE()
110 wxBEGIN_HANDLERS_TABLE(wxComboBox
)
111 wxEND_HANDLERS_TABLE()
113 wxCONSTRUCTOR_5( wxComboBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Value
, wxPoint
, Position
, wxSize
, Size
)
117 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxChoice
)
121 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
122 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
123 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
124 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
125 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
126 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
127 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
128 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
130 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
131 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
132 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
133 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
134 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
135 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
136 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
139 // ----------------------------------------------------------------------------
140 // function prototypes
141 // ----------------------------------------------------------------------------
143 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
148 // ---------------------------------------------------------------------------
150 // ---------------------------------------------------------------------------
152 // the pointer to standard radio button wnd proc
153 static WNDPROC gs_wndprocEdit
= (WNDPROC
)NULL
;
155 // ============================================================================
157 // ============================================================================
159 // ----------------------------------------------------------------------------
160 // wnd proc for subclassed edit control
161 // ----------------------------------------------------------------------------
163 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
168 HWND hwndCombo
= ::GetParent(hWnd
);
169 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)hwndCombo
);
173 // forward some messages to the combobox to generate the appropriate
174 // wxEvents from them
184 wxComboBox
*combo
= wxDynamicCast(win
, wxComboBox
);
187 // we can get WM_KILLFOCUS while our parent is already half
188 // destroyed and hence doesn't look like a combobx any
189 // longer, check for it to avoid bogus assert failures
190 if ( !win
->IsBeingDeleted() )
192 wxFAIL_MSG( wxT("should have combo as parent") );
195 else if ( combo
->MSWProcessEditMsg(message
, wParam
, lParam
) )
205 wxCHECK_MSG( win
, 0, wxT("should have a parent") );
207 if ( win
->GetWindowStyle() & wxTE_PROCESS_ENTER
)
209 // need to return a custom dlg code or we'll never get it
210 return DLGC_WANTMESSAGE
;
216 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
219 // ----------------------------------------------------------------------------
220 // wxComboBox callbacks
221 // ----------------------------------------------------------------------------
223 WXLRESULT
wxComboBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
225 // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to
226 // set its colour correctly (to be the same as our own one)
231 // wxStaticBox can generate this message, when modifying the control's style.
232 // This causes the content of the combobox to be selected, for some reason.
233 case WM_STYLECHANGED
:
235 // combobox selection sometimes spontaneously changes when its
236 // size changes, restore it to the old value if necessary
237 if ( !GetEditHWNDIfAvailable() )
241 GetSelection(&fromOld
, &toOld
);
243 // if an editable combobox has a not empty text not from the
244 // list, it tries to autocomplete it from the list when it is
245 // resized, but we don't want this to happen as it doesn't seem
246 // to make any sense, so we forcefully restore the old text
248 if ( !HasFlag(wxCB_READONLY
) && GetCurrentSelection() == -1 )
249 textOld
= GetValue();
251 // eliminate flickering during following hacks
252 wxWindowUpdateLocker
lock(this);
254 WXLRESULT result
= wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
256 if ( !textOld
.empty() && GetValue() != textOld
)
260 GetSelection(&fromNew
, &toNew
);
262 if ( fromOld
!= fromNew
|| toOld
!= toNew
)
264 SetSelection(fromOld
, toOld
);
271 return wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
274 bool wxComboBox::MSWProcessEditMsg(WXUINT msg
, WXWPARAM wParam
, WXLPARAM lParam
)
279 // for compatibility with wxTextCtrl, generate a special message
280 // when Enter is pressed
281 if ( wParam
== VK_RETURN
)
283 if (SendMessage(GetHwnd(), CB_GETDROPPEDSTATE
, 0, 0))
286 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
288 const int sel
= GetSelection();
290 event
.SetString(GetValue());
291 InitCommandEventWithItems(event
, sel
);
293 if ( ProcessCommand(event
) )
295 // don't let the event through to the native control
296 // because it doesn't need it and may generate an annoying
297 // beep if it gets it
304 return HandleChar(wParam
, lParam
);
308 return HandleKeyDown(wParam
, lParam
);
312 return HandleKeyUp(wParam
, lParam
);
315 return HandleSetFocus((WXHWND
)wParam
);
318 return HandleKillFocus((WXHWND
)wParam
);
323 return HandleClipboardEvent(msg
);
329 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD id
)
337 // remember the last selection, just as wxChoice does
338 m_lastAcceptedSelection
= GetCurrentSelection();
339 if ( m_lastAcceptedSelection
== -1 )
341 // but unlike with wxChoice we may have no selection but still
342 // have some text and we should avoid erasing it if the drop
343 // down is cancelled (see #8474)
344 m_lastAcceptedSelection
= wxID_NONE
;
347 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_DROPDOWN
, GetId());
348 event
.SetEventObject(this);
349 ProcessCommand(event
);
354 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_CLOSEUP
, GetId());
355 event
.SetEventObject(this);
356 ProcessCommand(event
);
360 #ifndef __SMARTPHONE__
361 // we need to reset this to prevent the selection from being undone
362 // by wxChoice, see wxChoice::MSWCommand() and comments there
363 m_lastAcceptedSelection
= wxID_NONE
;
366 // set these variables so that they could be also fixed in
367 // CBN_EDITCHANGE below
368 sel
= GetSelection();
369 value
= GetStringSelection();
371 // this string is going to become the new combobox value soon but
372 // we need it to be done right now, otherwise the event handler
373 // could get a wrong value when it calls our GetValue()
374 ::SetWindowText(GetHwnd(), value
.wx_str());
377 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
379 event
.SetString(value
);
380 InitCommandEventWithItems(event
, sel
);
382 ProcessCommand(event
);
385 // fall through: for compability with wxGTK, also send the text
386 // update event when the selection changes (this also seems more
387 // logical as the text does change)
390 if ( m_allowTextEvents
)
392 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
394 // if sel != -1, value was already initialized above
397 value
= wxGetWindowText(GetHwnd());
400 event
.SetString(value
);
401 InitCommandEventWithItems(event
, sel
);
403 ProcessCommand(event
);
408 return wxChoice::MSWCommand(param
, id
);
411 // skip wxChoice version as it would generate its own events for
412 // CBN_SELENDOK and also interfere with our handling of CBN_DROPDOWN
416 bool wxComboBox::MSWShouldPreProcessMessage(WXMSG
*pMsg
)
418 // prevent command accelerators from stealing editing
419 // hotkeys when we have the focus
422 WPARAM vkey
= pMsg
->wParam
;
437 return wxChoice::MSWShouldPreProcessMessage(pMsg
);
440 WXHWND
wxComboBox::GetEditHWNDIfAvailable() const
442 #if wxUSE_DYNLIB_CLASS
443 #if defined(WINVER) && WINVER >= 0x0500
444 typedef BOOL (WINAPI
*GetComboBoxInfo_t
)(HWND
, COMBOBOXINFO
*);
445 static GetComboBoxInfo_t s_pfnGetComboBoxInfo
= NULL
;
446 static bool s_triedToLoad
= false;
447 if ( !s_triedToLoad
)
449 s_triedToLoad
= true;
450 wxLoadedDLL
dllUser32("user32.dll");
451 wxDL_INIT_FUNC(s_pfn
, GetComboBoxInfo
, dllUser32
);
454 if ( s_pfnGetComboBoxInfo
)
456 WinStruct
<COMBOBOXINFO
> info
;
457 (*s_pfnGetComboBoxInfo
)(GetHwnd(), &info
);
458 return info
.hwndItem
;
460 #endif // WINVER >= 0x0500
461 #endif // wxUSE_DYNLIB_CLASS
463 if (HasFlag(wxCB_SIMPLE
))
467 return (WXHWND
) ::ChildWindowFromPoint(GetHwnd(), pt
);
470 // notice that a slightly safer alternative could be to use FindWindowEx()
471 // but it's not available under WinCE so just take the first child for now
472 // to keep one version of the code for all platforms and fix it later if
473 // problems are discovered
475 // we assume that the only child of the combobox is the edit window
476 return (WXHWND
)::GetWindow(GetHwnd(), GW_CHILD
);
479 WXHWND
wxComboBox::GetEditHWND() const
481 // this function should not be called for wxCB_READONLY controls, it is
482 // the callers responsibility to check this
483 wxASSERT_MSG( !HasFlag(wxCB_READONLY
),
484 wxT("read-only combobox doesn't have any edit control") );
486 WXHWND hWndEdit
= GetEditHWNDIfAvailable();
487 wxASSERT_MSG( hWndEdit
, wxT("combobox without edit control?") );
492 wxWindow
*wxComboBox::GetEditableWindow()
494 wxASSERT_MSG( !HasFlag(wxCB_READONLY
),
495 wxT("read-only combobox doesn't have any edit control") );
500 // ----------------------------------------------------------------------------
501 // wxComboBox creation
502 // ----------------------------------------------------------------------------
504 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
505 const wxString
& value
,
508 int n
, const wxString choices
[],
510 const wxValidator
& validator
,
511 const wxString
& name
)
513 // pretend that wxComboBox is hidden while it is positioned and resized and
514 // show it only right before leaving this method because otherwise there is
515 // some noticeable flicker while the control rearranges itself
518 if ( !CreateAndInit(parent
, id
, pos
, size
, n
, choices
, style
,
522 // we shouldn't call SetValue() for an empty string because this would
523 // (correctly) result in an assert with a read only combobox and is useless
524 // for the other ones anyhow
525 if ( !value
.empty() )
528 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
529 // and an edit control inside it and if we want to catch events from this
530 // edit control, we must subclass it as well
531 if ( !(style
& wxCB_READONLY
) )
533 gs_wndprocEdit
= wxSetWindowProc((HWND
)GetEditHWND(), wxComboEditWndProc
);
536 // and finally, show the control
542 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
543 const wxString
& value
,
546 const wxArrayString
& choices
,
548 const wxValidator
& validator
,
549 const wxString
& name
)
551 wxCArrayString
chs(choices
);
552 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
553 chs
.GetStrings(), style
, validator
, name
);
556 WXDWORD
wxComboBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
558 // we never have an external border
559 WXDWORD msStyle
= wxChoice::MSWGetStyle
561 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
564 // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're
565 // created hidden (see Create() above), it is not done for us but we still
566 // want to have this style
567 msStyle
|= WS_TABSTOP
;
569 // remove the style always added by wxChoice
570 msStyle
&= ~CBS_DROPDOWNLIST
;
572 if ( style
& wxCB_READONLY
)
573 msStyle
|= CBS_DROPDOWNLIST
;
575 else if ( style
& wxCB_SIMPLE
)
576 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
579 msStyle
|= CBS_DROPDOWN
;
581 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
582 msStyle
|= CBS_AUTOHSCROLL
;
584 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
589 // ----------------------------------------------------------------------------
590 // wxComboBox text control-like methods
591 // ----------------------------------------------------------------------------
593 wxString
wxComboBox::GetValue() const
595 return HasFlag(wxCB_READONLY
) ? GetStringSelection()
596 : wxTextEntry::GetValue();
599 void wxComboBox::SetValue(const wxString
& value
)
601 if ( HasFlag(wxCB_READONLY
) )
602 SetStringSelection(value
);
604 wxTextEntry::SetValue(value
);
607 void wxComboBox::Clear()
610 if ( !HasFlag(wxCB_READONLY
) )
611 wxTextEntry::Clear();
614 void wxComboBox::GetSelection(long *from
, long *to
) const
616 if ( !HasFlag(wxCB_READONLY
) )
618 wxTextEntry::GetSelection(from
, to
);
620 else // text selection doesn't make sense for read only comboboxes
629 bool wxComboBox::IsEditable() const
631 return !HasFlag(wxCB_READONLY
) && wxTextEntry::IsEditable();
634 // ----------------------------------------------------------------------------
635 // standard event handling
636 // ----------------------------------------------------------------------------
638 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
643 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
648 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
653 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
658 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
663 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
668 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
673 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
675 event
.Enable( CanCut() );
678 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
680 event
.Enable( CanCopy() );
683 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
685 event
.Enable( CanPaste() );
688 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
690 event
.Enable( IsEditable() && CanUndo() );
693 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
695 event
.Enable( IsEditable() && CanRedo() );
698 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
700 event
.Enable(IsEditable() && HasSelection());
703 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
705 event
.Enable(IsEditable() && !wxTextEntry::IsEmpty());
708 void wxComboBox::MSWDoPopupOrDismiss(bool show
)
710 wxASSERT_MSG( !HasFlag(wxCB_SIMPLE
),
711 wxT("can't popup/dismiss the list for simple combo box") );
713 // we *must* set focus to the combobox before showing or hiding the drop
714 // down as without this we get WM_LBUTTONDOWN messages with invalid HWND
715 // when hiding it (whether programmatically or manually) resulting in a
716 // crash when we pass them to IsDialogMessage()
718 // this can be seen in the combo page of the widgets sample under Windows 7
721 ::SendMessage(GetHwnd(), CB_SHOWDROPDOWN
, show
, 0);
726 void wxComboBox::DoSetToolTip(wxToolTip
*tip
)
728 wxChoice::DoSetToolTip(tip
);
730 if ( tip
&& !HasFlag(wxCB_READONLY
) )
731 tip
->Add(GetEditHWND());
734 #endif // wxUSE_TOOLTIPS
738 bool wxComboBox::SetHint(const wxString
& hintOrig
)
740 wxString
hint(hintOrig
);
741 if ( wxUxThemeEngine::GetIfActive() )
743 // under XP (but not Vista) there is a bug in cue banners
744 // implementation for combobox edit control: the first character is
745 // partially chopped off, so prepend a space to make it fully visible
749 return wxTextEntry::SetHint(hint
);
752 #endif // wxUSE_UXTHEME
754 #endif // wxUSE_COMBOBOX