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/wupdlock.h"
43 #include "wx/msw/private.h"
46 #include "wx/msw/uxtheme.h"
50 #include "wx/tooltip.h"
51 #endif // wxUSE_TOOLTIPS
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 #if wxUSE_EXTENDED_RTTI
58 WX_DEFINE_FLAGS( wxComboBoxStyle
)
60 wxBEGIN_FLAGS( wxComboBoxStyle
)
61 // new style border flags, we put them first to
62 // use them for streaming out
63 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
64 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
65 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
66 wxFLAGS_MEMBER(wxBORDER_RAISED
)
67 wxFLAGS_MEMBER(wxBORDER_STATIC
)
68 wxFLAGS_MEMBER(wxBORDER_NONE
)
70 // old style border flags
71 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
72 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
73 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
74 wxFLAGS_MEMBER(wxRAISED_BORDER
)
75 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
76 wxFLAGS_MEMBER(wxBORDER
)
78 // standard window styles
79 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
80 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
81 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
82 wxFLAGS_MEMBER(wxWANTS_CHARS
)
83 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
84 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
85 wxFLAGS_MEMBER(wxVSCROLL
)
86 wxFLAGS_MEMBER(wxHSCROLL
)
88 wxFLAGS_MEMBER(wxCB_SIMPLE
)
89 wxFLAGS_MEMBER(wxCB_SORT
)
90 wxFLAGS_MEMBER(wxCB_READONLY
)
91 wxFLAGS_MEMBER(wxCB_DROPDOWN
)
93 wxEND_FLAGS( wxComboBoxStyle
)
95 IMPLEMENT_DYNAMIC_CLASS_XTI(wxComboBox
, wxChoice
,"wx/combobox.h")
97 wxBEGIN_PROPERTIES_TABLE(wxComboBox
)
98 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_COMBOBOX_SELECTED
, wxCommandEvent
)
99 wxEVENT_PROPERTY( TextEnter
, wxEVT_COMMAND_TEXT_ENTER
, wxCommandEvent
)
102 wxPROPERTY( Font
, wxFont
, SetFont
, GetFont
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
103 wxPROPERTY_COLLECTION( Choices
, wxArrayString
, wxString
, AppendString
, GetStrings
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
104 wxPROPERTY( Value
,wxString
, SetValue
, GetValue
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
105 wxPROPERTY( Selection
,int, SetSelection
, GetSelection
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
106 wxPROPERTY_FLAGS( WindowStyle
, wxComboBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
107 wxEND_PROPERTIES_TABLE()
109 wxBEGIN_HANDLERS_TABLE(wxComboBox
)
110 wxEND_HANDLERS_TABLE()
112 wxCONSTRUCTOR_5( wxComboBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Value
, wxPoint
, Position
, wxSize
, Size
)
116 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxChoice
)
120 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
121 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
122 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
123 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
124 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
125 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
126 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
127 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
129 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
130 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
131 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
132 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
133 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
134 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
135 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
138 // ----------------------------------------------------------------------------
139 // function prototypes
140 // ----------------------------------------------------------------------------
142 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
147 // ---------------------------------------------------------------------------
149 // ---------------------------------------------------------------------------
151 // the pointer to standard radio button wnd proc
152 static WNDPROC gs_wndprocEdit
= (WNDPROC
)NULL
;
154 // ============================================================================
156 // ============================================================================
158 // ----------------------------------------------------------------------------
159 // wnd proc for subclassed edit control
160 // ----------------------------------------------------------------------------
162 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
167 HWND hwndCombo
= ::GetParent(hWnd
);
168 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)hwndCombo
);
172 // forward some messages to the combobox to generate the appropriate
173 // wxEvents from them
183 wxComboBox
*combo
= wxDynamicCast(win
, wxComboBox
);
186 // we can get WM_KILLFOCUS while our parent is already half
187 // destroyed and hence doesn't look like a combobx any
188 // longer, check for it to avoid bogus assert failures
189 if ( !win
->IsBeingDeleted() )
191 wxFAIL_MSG( wxT("should have combo as parent") );
194 else if ( combo
->MSWProcessEditMsg(message
, wParam
, lParam
) )
204 wxCHECK_MSG( win
, 0, wxT("should have a parent") );
206 if ( win
->GetWindowStyle() & wxTE_PROCESS_ENTER
)
208 // need to return a custom dlg code or we'll never get it
209 return DLGC_WANTMESSAGE
;
215 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
218 // ----------------------------------------------------------------------------
219 // wxComboBox callbacks
220 // ----------------------------------------------------------------------------
222 WXLRESULT
wxComboBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
224 // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to
225 // set its colour correctly (to be the same as our own one)
230 // wxStaticBox can generate this message, when modifying the control's style.
231 // This causes the content of the combobox to be selected, for some reason.
232 case WM_STYLECHANGED
:
234 // combobox selection sometimes spontaneously changes when its
235 // size changes, restore it to the old value if necessary
236 if ( !GetEditHWNDIfAvailable() )
240 GetSelection(&fromOld
, &toOld
);
242 // if an editable combobox has a not empty text not from the
243 // list, it tries to autocomplete it from the list when it is
244 // resized, but we don't want this to happen as it doesn't seem
245 // to make any sense, so we forcefully restore the old text
247 if ( !HasFlag(wxCB_READONLY
) && GetCurrentSelection() == -1 )
248 textOld
= GetValue();
250 // eliminate flickering during following hacks
251 wxWindowUpdateLocker
lock(this);
253 WXLRESULT result
= wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
255 if ( !textOld
.empty() && GetValue() != textOld
)
259 GetSelection(&fromNew
, &toNew
);
261 if ( fromOld
!= fromNew
|| toOld
!= toNew
)
263 SetSelection(fromOld
, toOld
);
270 return wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
273 bool wxComboBox::MSWProcessEditMsg(WXUINT msg
, WXWPARAM wParam
, WXLPARAM lParam
)
278 // for compatibility with wxTextCtrl, generate a special message
279 // when Enter is pressed
280 if ( wParam
== VK_RETURN
)
282 if (SendMessage(GetHwnd(), CB_GETDROPPEDSTATE
, 0, 0))
285 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
287 const int sel
= GetSelection();
289 event
.SetString(GetValue());
290 InitCommandEventWithItems(event
, sel
);
292 if ( ProcessCommand(event
) )
294 // don't let the event through to the native control
295 // because it doesn't need it and may generate an annoying
296 // beep if it gets it
303 return HandleChar(wParam
, lParam
, true /* isASCII */);
307 return HandleKeyDown(wParam
, lParam
);
311 return HandleKeyUp(wParam
, lParam
);
314 return HandleSetFocus((WXHWND
)wParam
);
317 return HandleKillFocus((WXHWND
)wParam
);
322 return HandleClipboardEvent(msg
);
328 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD id
)
336 // remember the last selection, just as wxChoice does
337 m_lastAcceptedSelection
= GetCurrentSelection();
338 if ( m_lastAcceptedSelection
== -1 )
340 // but unlike with wxChoice we may have no selection but still
341 // have some text and we should avoid erasing it if the drop
342 // down is cancelled (see #8474)
343 m_lastAcceptedSelection
= wxID_NONE
;
346 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_DROPDOWN
, GetId());
347 event
.SetEventObject(this);
348 ProcessCommand(event
);
353 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_CLOSEUP
, GetId());
354 event
.SetEventObject(this);
355 ProcessCommand(event
);
359 #ifndef __SMARTPHONE__
360 // we need to reset this to prevent the selection from being undone
361 // by wxChoice, see wxChoice::MSWCommand() and comments there
362 m_lastAcceptedSelection
= wxID_NONE
;
365 // set these variables so that they could be also fixed in
366 // CBN_EDITCHANGE below
367 sel
= GetSelection();
368 value
= GetStringSelection();
370 // this string is going to become the new combobox value soon but
371 // we need it to be done right now, otherwise the event handler
372 // could get a wrong value when it calls our GetValue()
373 ::SetWindowText(GetHwnd(), value
.wx_str());
376 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
378 event
.SetString(value
);
379 InitCommandEventWithItems(event
, sel
);
381 ProcessCommand(event
);
384 // fall through: for compability with wxGTK, also send the text
385 // update event when the selection changes (this also seems more
386 // logical as the text does change)
389 if ( m_allowTextEvents
)
391 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
393 // if sel != -1, value was already initialized above
396 value
= wxGetWindowText(GetHwnd());
399 event
.SetString(value
);
400 InitCommandEventWithItems(event
, sel
);
402 ProcessCommand(event
);
407 return wxChoice::MSWCommand(param
, id
);
410 // skip wxChoice version as it would generate its own events for
411 // CBN_SELENDOK and also interfere with our handling of CBN_DROPDOWN
415 bool wxComboBox::MSWShouldPreProcessMessage(WXMSG
*pMsg
)
417 // prevent command accelerators from stealing editing
418 // hotkeys when we have the focus
421 WPARAM vkey
= pMsg
->wParam
;
436 return wxChoice::MSWShouldPreProcessMessage(pMsg
);
439 WXHWND
wxComboBox::GetEditHWNDIfAvailable() const
441 #if defined(WINVER) && WINVER >= 0x0500
442 typedef BOOL (WINAPI
*GetComboBoxInfo_t
)(HWND
, COMBOBOXINFO
*);
443 static GetComboBoxInfo_t s_pfnGetComboBoxInfo
= NULL
;
444 static bool s_triedToLoad
= false;
445 if ( !s_triedToLoad
)
447 s_triedToLoad
= true;
448 wxLoadedDLL
dllUser32("user32.dll");
449 wxDL_INIT_FUNC(s_pfn
, GetComboBoxInfo
, dllUser32
);
452 if ( s_pfnGetComboBoxInfo
)
454 WinStruct
<COMBOBOXINFO
> info
;
455 (*s_pfnGetComboBoxInfo
)(GetHwnd(), &info
);
456 return info
.hwndItem
;
460 if (HasFlag(wxCB_SIMPLE
))
464 return (WXHWND
) ::ChildWindowFromPoint(GetHwnd(), pt
);
467 // notice that a slightly safer alternative could be to use FindWindowEx()
468 // but it's not available under WinCE so just take the first child for now
469 // to keep one version of the code for all platforms and fix it later if
470 // problems are discovered
472 // we assume that the only child of the combobox is the edit window
473 return (WXHWND
)::GetWindow(GetHwnd(), GW_CHILD
);
476 WXHWND
wxComboBox::GetEditHWND() const
478 // this function should not be called for wxCB_READONLY controls, it is
479 // the callers responsibility to check this
480 wxASSERT_MSG( !HasFlag(wxCB_READONLY
),
481 wxT("read-only combobox doesn't have any edit control") );
483 WXHWND hWndEdit
= GetEditHWNDIfAvailable();
484 wxASSERT_MSG( hWndEdit
, wxT("combobox without edit control?") );
489 wxWindow
*wxComboBox::GetEditableWindow()
491 wxASSERT_MSG( !HasFlag(wxCB_READONLY
),
492 wxT("read-only combobox doesn't have any edit control") );
497 // ----------------------------------------------------------------------------
498 // wxComboBox creation
499 // ----------------------------------------------------------------------------
501 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
502 const wxString
& value
,
505 int n
, const wxString choices
[],
507 const wxValidator
& validator
,
508 const wxString
& name
)
510 // pretend that wxComboBox is hidden while it is positioned and resized and
511 // show it only right before leaving this method because otherwise there is
512 // some noticeable flicker while the control rearranges itself
515 if ( !CreateAndInit(parent
, id
, pos
, size
, n
, choices
, style
,
519 // we shouldn't call SetValue() for an empty string because this would
520 // (correctly) result in an assert with a read only combobox and is useless
521 // for the other ones anyhow
522 if ( !value
.empty() )
525 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
526 // and an edit control inside it and if we want to catch events from this
527 // edit control, we must subclass it as well
528 if ( !(style
& wxCB_READONLY
) )
530 gs_wndprocEdit
= wxSetWindowProc((HWND
)GetEditHWND(), wxComboEditWndProc
);
533 // and finally, show the control
539 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
540 const wxString
& value
,
543 const wxArrayString
& choices
,
545 const wxValidator
& validator
,
546 const wxString
& name
)
548 wxCArrayString
chs(choices
);
549 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
550 chs
.GetStrings(), style
, validator
, name
);
553 WXDWORD
wxComboBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
555 // we never have an external border
556 WXDWORD msStyle
= wxChoice::MSWGetStyle
558 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
561 // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're
562 // created hidden (see Create() above), it is not done for us but we still
563 // want to have this style
564 msStyle
|= WS_TABSTOP
;
566 // remove the style always added by wxChoice
567 msStyle
&= ~CBS_DROPDOWNLIST
;
569 if ( style
& wxCB_READONLY
)
570 msStyle
|= CBS_DROPDOWNLIST
;
572 else if ( style
& wxCB_SIMPLE
)
573 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
576 msStyle
|= CBS_DROPDOWN
;
578 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
579 msStyle
|= CBS_AUTOHSCROLL
;
581 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
586 // ----------------------------------------------------------------------------
587 // wxComboBox text control-like methods
588 // ----------------------------------------------------------------------------
590 wxString
wxComboBox::GetValue() const
592 return HasFlag(wxCB_READONLY
) ? GetStringSelection()
593 : wxTextEntry::GetValue();
596 void wxComboBox::SetValue(const wxString
& value
)
598 if ( HasFlag(wxCB_READONLY
) )
599 SetStringSelection(value
);
601 wxTextEntry::SetValue(value
);
604 void wxComboBox::Clear()
607 if ( !HasFlag(wxCB_READONLY
) )
608 wxTextEntry::Clear();
611 void wxComboBox::GetSelection(long *from
, long *to
) const
613 if ( !HasFlag(wxCB_READONLY
) )
615 wxTextEntry::GetSelection(from
, to
);
617 else // text selection doesn't make sense for read only comboboxes
626 bool wxComboBox::IsEditable() const
628 return !HasFlag(wxCB_READONLY
) && wxTextEntry::IsEditable();
631 // ----------------------------------------------------------------------------
632 // standard event handling
633 // ----------------------------------------------------------------------------
635 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
640 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
645 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
650 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
655 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
660 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
665 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
670 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
672 event
.Enable( CanCut() );
675 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
677 event
.Enable( CanCopy() );
680 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
682 event
.Enable( CanPaste() );
685 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
687 event
.Enable( IsEditable() && CanUndo() );
690 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
692 event
.Enable( IsEditable() && CanRedo() );
695 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
697 event
.Enable(IsEditable() && HasSelection());
700 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
702 event
.Enable(IsEditable() && !wxTextEntry::IsEmpty());
707 void wxComboBox::DoSetToolTip(wxToolTip
*tip
)
709 wxChoice::DoSetToolTip(tip
);
711 if ( tip
&& !HasFlag(wxCB_READONLY
) )
712 tip
->Add(GetEditHWND());
715 #endif // wxUSE_TOOLTIPS
719 bool wxComboBox::SetHint(const wxString
& hintOrig
)
721 wxString
hint(hintOrig
);
722 if ( wxUxThemeEngine::GetIfActive() )
724 // under XP (but not Vista) there is a bug in cue banners
725 // implementation for combobox edit control: the first character is
726 // partially chopped off, so prepend a space to make it fully visible
730 return wxTextEntry::SetHint(hint
);
733 #endif // wxUSE_UXTHEME
735 #endif // wxUSE_COMBOBOX