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/tooltip.h"
47 #endif // wxUSE_TOOLTIPS
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 #if wxUSE_EXTENDED_RTTI
54 WX_DEFINE_FLAGS( wxComboBoxStyle
)
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
)
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
)
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
)
84 wxFLAGS_MEMBER(wxCB_SIMPLE
)
85 wxFLAGS_MEMBER(wxCB_SORT
)
86 wxFLAGS_MEMBER(wxCB_READONLY
)
87 wxFLAGS_MEMBER(wxCB_DROPDOWN
)
89 wxEND_FLAGS( wxComboBoxStyle
)
91 IMPLEMENT_DYNAMIC_CLASS_XTI(wxComboBox
, wxChoice
,"wx/combobox.h")
93 wxBEGIN_PROPERTIES_TABLE(wxComboBox
)
94 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_COMBOBOX_SELECTED
, wxCommandEvent
)
95 wxEVENT_PROPERTY( TextEnter
, wxEVT_COMMAND_TEXT_ENTER
, wxCommandEvent
)
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()
105 wxBEGIN_HANDLERS_TABLE(wxComboBox
)
106 wxEND_HANDLERS_TABLE()
108 wxCONSTRUCTOR_5( wxComboBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Value
, wxPoint
, Position
, wxSize
, Size
)
112 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxChoice
)
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
)
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
)
134 // ----------------------------------------------------------------------------
135 // function prototypes
136 // ----------------------------------------------------------------------------
138 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
143 // ---------------------------------------------------------------------------
145 // ---------------------------------------------------------------------------
147 // the pointer to standard radio button wnd proc
148 static WNDPROC gs_wndprocEdit
= (WNDPROC
)NULL
;
150 // ============================================================================
152 // ============================================================================
154 // ----------------------------------------------------------------------------
155 // wnd proc for subclassed edit control
156 // ----------------------------------------------------------------------------
158 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
163 HWND hwndCombo
= ::GetParent(hWnd
);
164 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)hwndCombo
);
168 // forward some messages to the combobox to generate the appropriate
169 // wxEvents from them
179 wxComboBox
*combo
= wxDynamicCast(win
, wxComboBox
);
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() )
187 wxFAIL_MSG( _T("should have combo as parent") );
190 else if ( combo
->MSWProcessEditMsg(message
, wParam
, lParam
) )
200 wxCHECK_MSG( win
, 0, _T("should have a parent") );
202 if ( win
->GetWindowStyle() & wxTE_PROCESS_ENTER
)
204 // need to return a custom dlg code or we'll never get it
205 return DLGC_WANTMESSAGE
;
211 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
214 // ----------------------------------------------------------------------------
215 // wxComboBox callbacks
216 // ----------------------------------------------------------------------------
218 WXLRESULT
wxComboBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
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)
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
:
230 // combobox selection sometimes spontaneously changes when its
231 // size changes, restore it to the old value if necessary
232 if ( !GetEditHWNDIfAvailable() )
236 GetSelection(&fromOld
, &toOld
);
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
243 if ( !HasFlag(wxCB_READONLY
) && GetCurrentSelection() == -1 )
244 textOld
= GetValue();
246 // eliminate flickering during following hacks
247 wxWindowUpdateLocker
lock(this);
249 WXLRESULT result
= wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
251 if ( !textOld
.empty() && GetValue() != textOld
)
255 GetSelection(&fromNew
, &toNew
);
257 if ( fromOld
!= fromNew
|| toOld
!= toNew
)
259 SetSelection(fromOld
, toOld
);
266 return wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
269 bool wxComboBox::MSWProcessEditMsg(WXUINT msg
, WXWPARAM wParam
, WXLPARAM lParam
)
274 // for compatibility with wxTextCtrl, generate a special message
275 // when Enter is pressed
276 if ( wParam
== VK_RETURN
)
278 if (SendMessage(GetHwnd(), CB_GETDROPPEDSTATE
, 0, 0))
281 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
283 const int sel
= GetSelection();
285 event
.SetString(GetValue());
286 InitCommandEventWithItems(event
, sel
);
288 if ( ProcessCommand(event
) )
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
299 return HandleChar(wParam
, lParam
, true /* isASCII */);
303 return HandleKeyDown(wParam
, lParam
);
307 return HandleKeyUp(wParam
, lParam
);
310 return HandleSetFocus((WXHWND
)wParam
);
313 return HandleKillFocus((WXHWND
)wParam
);
318 return HandleClipboardEvent(msg
);
324 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD id
)
332 // remember the last selection, just as wxChoice does
333 m_lastAcceptedSelection
= GetCurrentSelection();
334 if ( m_lastAcceptedSelection
== -1 )
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
;
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
;
350 // set these variables so that they could be also fixed in
351 // CBN_EDITCHANGE below
352 sel
= GetSelection();
353 value
= GetStringSelection();
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());
361 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
363 event
.SetString(value
);
364 InitCommandEventWithItems(event
, sel
);
366 ProcessCommand(event
);
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)
374 if ( m_allowTextEvents
)
376 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
378 // if sel != -1, value was already initialized above
381 value
= wxGetWindowText(GetHwnd());
384 event
.SetString(value
);
385 InitCommandEventWithItems(event
, sel
);
387 ProcessCommand(event
);
392 return wxChoice::MSWCommand(param
, id
);
395 // skip wxChoice version as it would generate its own events for
396 // CBN_SELENDOK and also interfere with our handling of CBN_DROPDOWN
400 bool wxComboBox::MSWShouldPreProcessMessage(WXMSG
*pMsg
)
402 // prevent command accelerators from stealing editing
403 // hotkeys when we have the focus
406 WPARAM vkey
= pMsg
->wParam
;
421 return wxChoice::MSWShouldPreProcessMessage(pMsg
);
424 WXHWND
wxComboBox::GetEditHWNDIfAvailable() const
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
431 // we assume that the only child of the combobox is the edit window
432 return (WXHWND
)::GetWindow(GetHwnd(), GW_CHILD
);
435 WXHWND
wxComboBox::GetEditHWND() const
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") );
442 WXHWND hWndEdit
= GetEditHWNDIfAvailable();
443 wxASSERT_MSG( hWndEdit
, _T("combobox without edit control?") );
448 // ----------------------------------------------------------------------------
449 // wxComboBox creation
450 // ----------------------------------------------------------------------------
452 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
453 const wxString
& value
,
456 int n
, const wxString choices
[],
458 const wxValidator
& validator
,
459 const wxString
& name
)
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
466 if ( !CreateAndInit(parent
, id
, pos
, size
, n
, choices
, style
,
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() )
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
) )
481 gs_wndprocEdit
= wxSetWindowProc((HWND
)GetEditHWND(), wxComboEditWndProc
);
484 // and finally, show the control
490 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
491 const wxString
& value
,
494 const wxArrayString
& choices
,
496 const wxValidator
& validator
,
497 const wxString
& name
)
499 wxCArrayString
chs(choices
);
500 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
501 chs
.GetStrings(), style
, validator
, name
);
504 WXDWORD
wxComboBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
506 // we never have an external border
507 WXDWORD msStyle
= wxChoice::MSWGetStyle
509 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
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
;
517 // remove the style always added by wxChoice
518 msStyle
&= ~CBS_DROPDOWNLIST
;
520 if ( style
& wxCB_READONLY
)
521 msStyle
|= CBS_DROPDOWNLIST
;
523 else if ( style
& wxCB_SIMPLE
)
524 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
527 msStyle
|= CBS_DROPDOWN
;
529 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
530 msStyle
|= CBS_AUTOHSCROLL
;
532 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
537 // ----------------------------------------------------------------------------
538 // wxComboBox text control-like methods
539 // ----------------------------------------------------------------------------
541 wxString
wxComboBox::GetValue() const
543 return HasFlag(wxCB_READONLY
) ? GetStringSelection()
544 : wxTextEntry::GetValue();
547 void wxComboBox::SetValue(const wxString
& value
)
549 if ( HasFlag(wxCB_READONLY
) )
550 SetStringSelection(value
);
552 wxTextEntry::SetValue(value
);
555 void wxComboBox::Clear()
558 if ( !HasFlag(wxCB_READONLY
) )
559 wxTextEntry::Clear();
562 void wxComboBox::GetSelection(long *from
, long *to
) const
564 if ( !HasFlag(wxCB_READONLY
) )
566 wxTextEntry::GetSelection(from
, to
);
568 else // text selection doesn't make sense for read only comboboxes
577 bool wxComboBox::IsEditable() const
579 return !HasFlag(wxCB_READONLY
) && wxTextEntry::IsEditable();
582 // ----------------------------------------------------------------------------
583 // standard event handling
584 // ----------------------------------------------------------------------------
586 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
591 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
596 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
601 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
606 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
611 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
616 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
621 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
623 event
.Enable( CanCut() );
626 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
628 event
.Enable( CanCopy() );
631 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
633 event
.Enable( CanPaste() );
636 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
638 event
.Enable( IsEditable() && CanUndo() );
641 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
643 event
.Enable( IsEditable() && CanRedo() );
646 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
648 event
.Enable(IsEditable() && HasSelection());
651 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
653 event
.Enable(IsEditable() && !wxTextEntry::IsEmpty());
658 void wxComboBox::DoSetToolTip(wxToolTip
*tip
)
660 wxChoice::DoSetToolTip(tip
);
662 if ( tip
&& !HasFlag(wxCB_READONLY
) )
663 tip
->Add(GetEditHWND());
666 #endif // wxUSE_TOOLTIPS
668 #endif // wxUSE_COMBOBOX