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/msw/private.h"
45 #include "wx/tooltip.h"
46 #endif // wxUSE_TOOLTIPS
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 #if wxUSE_EXTENDED_RTTI
53 WX_DEFINE_FLAGS( wxComboBoxStyle
)
55 wxBEGIN_FLAGS( wxComboBoxStyle
)
56 // new style border flags, we put them first to
57 // use them for streaming out
58 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
59 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
60 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
61 wxFLAGS_MEMBER(wxBORDER_RAISED
)
62 wxFLAGS_MEMBER(wxBORDER_STATIC
)
63 wxFLAGS_MEMBER(wxBORDER_NONE
)
65 // old style border flags
66 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
67 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
68 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
69 wxFLAGS_MEMBER(wxRAISED_BORDER
)
70 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
71 wxFLAGS_MEMBER(wxBORDER
)
73 // standard window styles
74 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
75 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
76 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
77 wxFLAGS_MEMBER(wxWANTS_CHARS
)
78 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
79 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
80 wxFLAGS_MEMBER(wxVSCROLL
)
81 wxFLAGS_MEMBER(wxHSCROLL
)
83 wxFLAGS_MEMBER(wxCB_SIMPLE
)
84 wxFLAGS_MEMBER(wxCB_SORT
)
85 wxFLAGS_MEMBER(wxCB_READONLY
)
86 wxFLAGS_MEMBER(wxCB_DROPDOWN
)
88 wxEND_FLAGS( wxComboBoxStyle
)
90 IMPLEMENT_DYNAMIC_CLASS_XTI(wxComboBox
, wxChoice
,"wx/combobox.h")
92 wxBEGIN_PROPERTIES_TABLE(wxComboBox
)
93 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_COMBOBOX_SELECTED
, wxCommandEvent
)
94 wxEVENT_PROPERTY( TextEnter
, wxEVT_COMMAND_TEXT_ENTER
, wxCommandEvent
)
97 wxPROPERTY( Font
, wxFont
, SetFont
, GetFont
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
98 wxPROPERTY_COLLECTION( Choices
, wxArrayString
, wxString
, AppendString
, GetStrings
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
99 wxPROPERTY( Value
,wxString
, SetValue
, GetValue
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
100 wxPROPERTY( Selection
,int, SetSelection
, GetSelection
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
101 wxPROPERTY_FLAGS( WindowStyle
, wxComboBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
102 wxEND_PROPERTIES_TABLE()
104 wxBEGIN_HANDLERS_TABLE(wxComboBox
)
105 wxEND_HANDLERS_TABLE()
107 wxCONSTRUCTOR_5( wxComboBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Value
, wxPoint
, Position
, wxSize
, Size
)
111 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxChoice
)
115 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
116 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
117 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
118 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
119 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
120 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
121 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
122 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
124 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
125 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
126 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
127 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
128 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
129 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
130 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
133 // ----------------------------------------------------------------------------
134 // function prototypes
135 // ----------------------------------------------------------------------------
137 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
142 // ---------------------------------------------------------------------------
144 // ---------------------------------------------------------------------------
146 // the pointer to standard radio button wnd proc
147 static WNDPROC gs_wndprocEdit
= (WNDPROC
)NULL
;
149 // ============================================================================
151 // ============================================================================
153 // ----------------------------------------------------------------------------
154 // wnd proc for subclassed edit control
155 // ----------------------------------------------------------------------------
157 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
162 HWND hwndCombo
= ::GetParent(hWnd
);
163 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)hwndCombo
);
167 // forward some messages to the combobox to generate the appropriate
168 // wxEvents from them
178 wxComboBox
*combo
= wxDynamicCast(win
, wxComboBox
);
181 // we can get WM_KILLFOCUS while our parent is already half
182 // destroyed and hence doesn't look like a combobx any
183 // longer, check for it to avoid bogus assert failures
184 if ( !win
->IsBeingDeleted() )
186 wxFAIL_MSG( _T("should have combo as parent") );
189 else if ( combo
->MSWProcessEditMsg(message
, wParam
, lParam
) )
199 wxCHECK_MSG( win
, 0, _T("should have a parent") );
201 if ( win
->GetWindowStyle() & wxTE_PROCESS_ENTER
)
203 // need to return a custom dlg code or we'll never get it
204 return DLGC_WANTMESSAGE
;
210 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
213 // ----------------------------------------------------------------------------
214 // wxComboBox callbacks
215 // ----------------------------------------------------------------------------
217 WXLRESULT
wxComboBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
219 // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to
220 // set its colour correctly (to be the same as our own one)
225 // wxStaticBox can generate this message, when modifying the control's style.
226 // This causes the content of the combobox to be selected, for some reason.
227 case WM_STYLECHANGED
:
229 // combobox selection sometimes spontaneously changes when its
230 // size changes, restore it to the old value if necessary
231 if ( !GetEditHWNDIfAvailable() )
235 GetSelection(&fromOld
, &toOld
);
236 WXLRESULT result
= wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
239 GetSelection(&fromNew
, &toNew
);
241 if ( fromOld
!= fromNew
|| toOld
!= toNew
)
243 SetSelection(fromOld
, toOld
);
250 return wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
253 bool wxComboBox::MSWProcessEditMsg(WXUINT msg
, WXWPARAM wParam
, WXLPARAM lParam
)
258 // for compatibility with wxTextCtrl, generate a special message
259 // when Enter is pressed
260 if ( wParam
== VK_RETURN
)
262 if (SendMessage(GetHwnd(), CB_GETDROPPEDSTATE
, 0, 0))
265 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
267 const int sel
= GetSelection();
269 event
.SetString(GetValue());
270 InitCommandEventWithItems(event
, sel
);
272 if ( ProcessCommand(event
) )
274 // don't let the event through to the native control
275 // because it doesn't need it and may generate an annoying
276 // beep if it gets it
283 return HandleChar(wParam
, lParam
, true /* isASCII */);
287 return HandleKeyDown(wParam
, lParam
);
291 return HandleKeyUp(wParam
, lParam
);
294 return HandleSetFocus((WXHWND
)wParam
);
297 return HandleKillFocus((WXHWND
)wParam
);
302 return HandleClipboardEvent(msg
);
308 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD id
)
316 // remember the last selection, just as wxChoice does
317 m_lastAcceptedSelection
= GetCurrentSelection();
318 if ( m_lastAcceptedSelection
== -1 )
320 // but unlike with wxChoice we may have no selection but still
321 // have some text and we should avoid erasing it if the drop
322 // down is cancelled (see #8474)
323 m_lastAcceptedSelection
= wxID_NONE
;
328 #ifndef __SMARTPHONE__
329 // we need to reset this to prevent the selection from being undone
330 // by wxChoice, see wxChoice::MSWCommand() and comments there
331 m_lastAcceptedSelection
= wxID_NONE
;
334 // set these variables so that they could be also fixed in
335 // CBN_EDITCHANGE below
336 sel
= GetSelection();
337 value
= GetStringSelection();
339 // this string is going to become the new combobox value soon but
340 // we need it to be done right now, otherwise the event handler
341 // could get a wrong value when it calls our GetValue()
342 ::SetWindowText(GetHwnd(), value
.wx_str());
345 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
347 event
.SetString(value
);
348 InitCommandEventWithItems(event
, sel
);
350 ProcessCommand(event
);
353 // fall through: for compability with wxGTK, also send the text
354 // update event when the selection changes (this also seems more
355 // logical as the text does change)
358 if ( m_allowTextEvents
)
360 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
362 // if sel != -1, value was already initialized above
365 value
= wxGetWindowText(GetHwnd());
368 event
.SetString(value
);
369 InitCommandEventWithItems(event
, sel
);
371 ProcessCommand(event
);
376 return wxChoice::MSWCommand(param
, id
);
379 // skip wxChoice version as it would generate its own events for
380 // CBN_SELENDOK and also interfere with our handling of CBN_DROPDOWN
384 bool wxComboBox::MSWShouldPreProcessMessage(WXMSG
*pMsg
)
386 // prevent command accelerators from stealing editing
387 // hotkeys when we have the focus
390 WPARAM vkey
= pMsg
->wParam
;
405 return wxChoice::MSWShouldPreProcessMessage(pMsg
);
408 WXHWND
wxComboBox::GetEditHWNDIfAvailable() const
410 // notice that a slightly safer alternative could be to use FindWindowEx()
411 // but it's not available under WinCE so just take the first child for now
412 // to keep one version of the code for all platforms and fix it later if
413 // problems are discovered
415 // we assume that the only child of the combobox is the edit window
416 return (WXHWND
)::GetWindow(GetHwnd(), GW_CHILD
);
419 WXHWND
wxComboBox::GetEditHWND() const
421 // this function should not be called for wxCB_READONLY controls, it is
422 // the callers responsibility to check this
423 wxASSERT_MSG( !HasFlag(wxCB_READONLY
),
424 _T("read-only combobox doesn't have any edit control") );
426 WXHWND hWndEdit
= GetEditHWNDIfAvailable();
427 wxASSERT_MSG( hWndEdit
, _T("combobox without edit control?") );
432 // ----------------------------------------------------------------------------
433 // wxComboBox creation
434 // ----------------------------------------------------------------------------
436 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
437 const wxString
& value
,
440 int n
, const wxString choices
[],
442 const wxValidator
& validator
,
443 const wxString
& name
)
445 // pretend that wxComboBox is hidden while it is positioned and resized and
446 // show it only right before leaving this method because otherwise there is
447 // some noticeable flicker while the control rearranges itself
450 if ( !CreateAndInit(parent
, id
, pos
, size
, n
, choices
, style
,
454 // we shouldn't call SetValue() for an empty string because this would
455 // (correctly) result in an assert with a read only combobox and is useless
456 // for the other ones anyhow
457 if ( !value
.empty() )
460 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
461 // and an edit control inside it and if we want to catch events from this
462 // edit control, we must subclass it as well
463 if ( !(style
& wxCB_READONLY
) )
465 gs_wndprocEdit
= wxSetWindowProc((HWND
)GetEditHWND(), wxComboEditWndProc
);
468 // and finally, show the control
474 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
475 const wxString
& value
,
478 const wxArrayString
& choices
,
480 const wxValidator
& validator
,
481 const wxString
& name
)
483 wxCArrayString
chs(choices
);
484 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
485 chs
.GetStrings(), style
, validator
, name
);
488 WXDWORD
wxComboBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
490 // we never have an external border
491 WXDWORD msStyle
= wxChoice::MSWGetStyle
493 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
496 // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're
497 // created hidden (see Create() above), it is not done for us but we still
498 // want to have this style
499 msStyle
|= WS_TABSTOP
;
501 // remove the style always added by wxChoice
502 msStyle
&= ~CBS_DROPDOWNLIST
;
504 if ( style
& wxCB_READONLY
)
505 msStyle
|= CBS_DROPDOWNLIST
;
507 else if ( style
& wxCB_SIMPLE
)
508 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
511 msStyle
|= CBS_DROPDOWN
;
513 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
514 msStyle
|= CBS_AUTOHSCROLL
;
516 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
521 // ----------------------------------------------------------------------------
522 // wxComboBox text control-like methods
523 // ----------------------------------------------------------------------------
525 wxString
wxComboBox::GetValue() const
527 return HasFlag(wxCB_READONLY
) ? GetStringSelection()
528 : wxTextEntry::GetValue();
531 void wxComboBox::SetValue(const wxString
& value
)
533 if ( HasFlag(wxCB_READONLY
) )
534 SetStringSelection(value
);
536 wxTextEntry::SetValue(value
);
539 void wxComboBox::Clear()
542 if ( !HasFlag(wxCB_READONLY
) )
543 wxTextEntry::Clear();
546 void wxComboBox::GetSelection(long *from
, long *to
) const
548 if ( !HasFlag(wxCB_READONLY
) )
550 wxTextEntry::GetSelection(from
, to
);
552 else // text selection doesn't make sense for read only comboboxes
561 bool wxComboBox::IsEditable() const
563 return !HasFlag(wxCB_READONLY
) && wxTextEntry::IsEditable();
566 // ----------------------------------------------------------------------------
567 // standard event handling
568 // ----------------------------------------------------------------------------
570 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
575 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
580 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
585 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
590 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
595 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
600 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
605 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
607 event
.Enable( CanCut() );
610 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
612 event
.Enable( CanCopy() );
615 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
617 event
.Enable( CanPaste() );
620 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
622 event
.Enable( IsEditable() && CanUndo() );
625 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
627 event
.Enable( IsEditable() && CanRedo() );
630 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
632 event
.Enable(IsEditable() && HasSelection());
635 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
637 event
.Enable(IsEditable() && !wxTextEntry::IsEmpty());
642 void wxComboBox::DoSetToolTip(wxToolTip
*tip
)
644 wxChoice::DoSetToolTip(tip
);
646 if ( tip
&& !HasFlag(wxCB_READONLY
) )
647 tip
->Add(GetEditHWND());
650 #endif // wxUSE_TOOLTIPS
652 #endif // wxUSE_COMBOBOX