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"
30 #include "wx/settings.h"
32 // for wxEVT_COMMAND_TEXT_ENTER
33 #include "wx/textctrl.h"
37 #include "wx/combobox.h"
39 #include "wx/clipbrd.h"
40 #include "wx/msw/private.h"
43 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
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
, wxControl
,"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
, wxControl
)
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() & wxPROCESS_ENTER
)
204 // need to return a custom dlg code or we'll never get it
205 return DLGC_WANTMESSAGE
;
210 // deal with tooltips here
211 #if wxUSE_TOOLTIPS && defined(TTN_NEEDTEXT)
214 wxCHECK_MSG( win
, 0, _T("should have a parent") );
216 NMHDR
* hdr
= (NMHDR
*)lParam
;
217 if ( hdr
->code
== TTN_NEEDTEXT
)
219 wxToolTip
*tooltip
= win
->GetToolTip();
222 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
223 ttt
->lpszText
= (wxChar
*)tooltip
->GetTip().c_str();
231 #endif // wxUSE_TOOLTIPS
234 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
237 // ----------------------------------------------------------------------------
238 // wxComboBox callbacks
239 // ----------------------------------------------------------------------------
241 WXLRESULT
wxComboBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
243 // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to
244 // set its colour correctly (to be the same as our own one)
249 // wxStaticBox can generate this message, when modifying the control's style.
250 // This causes the content of the combobox to be selected, for some reason.
251 case WM_STYLECHANGED
:
253 // combobox selection sometimes spontaneously changes when its
254 // size changes, restore it to the old value if necessary
256 GetSelection(&fromOld
, &toOld
);
257 WXLRESULT result
= wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
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 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
284 InitCommandEvent(event
);
285 event
.SetString(GetValue());
286 event
.SetInt(GetSelection());
287 if ( ProcessCommand(event
) )
289 // don't let the event through to the native control
290 // because it doesn't need it and may generate an annoying
291 // beep if it gets it
298 return HandleChar(wParam
, lParam
, true /* isASCII */);
302 return HandleKeyDown(wParam
, lParam
);
306 return HandleKeyUp(wParam
, lParam
);
309 return HandleSetFocus((WXHWND
)wParam
);
312 return HandleKillFocus((WXHWND
)wParam
);
318 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD id
)
326 #ifndef __SMARTPHONE__
327 // we need to reset this to prevent the selection from being undone
328 // by wxChoice, see wxChoice::MSWCommand() and comments there
329 m_lastAcceptedSelection
= wxID_NONE
;
332 // set these variables so that they could be also fixed in
333 // CBN_EDITCHANGE below
334 sel
= GetSelection();
335 value
= GetStringSelection();
337 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
338 event
.SetEventObject(this);
340 event
.SetString(value
);
341 ProcessCommand(event
);
344 // fall through: for compability with wxGTK, also send the text
345 // update event when the selection changes (this also seems more
346 // logical as the text does change)
350 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
351 event
.SetEventObject(this);
353 // if sel != -1, value was already initialized above
356 value
= wxGetWindowText(GetHwnd());
359 event
.SetString(value
);
360 ProcessCommand(event
);
365 return wxChoice::MSWCommand(param
, id
);
368 // skip wxChoice version as it would generate its own events for
370 return wxControl::MSWCommand(param
, id
);
373 WXHWND
wxComboBox::GetEditHWND() const
375 // this function should not be called for wxCB_READONLY controls, it is
376 // the callers responsability to check this
377 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY
),
378 _T("read-only combobox doesn't have any edit control") );
382 HWND hwndEdit
= ::ChildWindowFromPoint(GetHwnd(), pt
);
383 if ( !hwndEdit
|| hwndEdit
== GetHwnd() )
385 wxFAIL_MSG(_T("not read only combobox without edit control?"));
388 return (WXHWND
)hwndEdit
;
391 // ----------------------------------------------------------------------------
392 // wxComboBox creation
393 // ----------------------------------------------------------------------------
395 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
396 const wxString
& value
,
399 int n
, const wxString choices
[],
401 const wxValidator
& validator
,
402 const wxString
& name
)
404 // pretend that wxComboBox is hidden while it is positioned and resized and
405 // show it only right before leaving this method because otherwise there is
406 // some noticeable flicker while the control rearranges itself
409 if ( !CreateAndInit(parent
, id
, pos
, size
, n
, choices
, style
,
413 // we shouldn't call SetValue() for an empty string because this would
414 // (correctly) result in an assert with a read only combobox and is useless
415 // for the other ones anyhow
416 if ( !value
.empty() )
419 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
420 // and an edit control inside it and if we want to catch events from this
421 // edit control, we must subclass it as well
422 if ( !(style
& wxCB_READONLY
) )
424 gs_wndprocEdit
= wxSetWindowProc((HWND
)GetEditHWND(),
428 // and finally, show the control
434 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
435 const wxString
& value
,
438 const wxArrayString
& choices
,
440 const wxValidator
& validator
,
441 const wxString
& name
)
443 wxCArrayString
chs(choices
);
444 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
445 chs
.GetStrings(), style
, validator
, name
);
448 WXDWORD
wxComboBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
450 // we never have an external border
451 WXDWORD msStyle
= wxChoice::MSWGetStyle
453 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
456 // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're
457 // created hidden (see Create() above), it is not done for us but we still
458 // want to have this style
459 msStyle
|= WS_TABSTOP
;
461 // remove the style always added by wxChoice
462 msStyle
&= ~CBS_DROPDOWNLIST
;
464 if ( style
& wxCB_READONLY
)
465 msStyle
|= CBS_DROPDOWNLIST
;
467 else if ( style
& wxCB_SIMPLE
)
468 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
471 msStyle
|= CBS_DROPDOWN
;
473 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
474 msStyle
|= CBS_AUTOHSCROLL
;
476 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
481 // ----------------------------------------------------------------------------
482 // wxComboBox text control-like methods
483 // ----------------------------------------------------------------------------
485 wxString
wxComboBox::GetValue() const
487 return wxGetWindowText(m_hWnd
);
490 void wxComboBox::SetValue(const wxString
& value
)
492 if ( HasFlag(wxCB_READONLY
) )
493 SetStringSelection(value
);
495 SetWindowText(GetHwnd(), value
.c_str());
498 // Clipboard operations
499 void wxComboBox::Copy()
501 SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
504 void wxComboBox::Cut()
506 SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
509 void wxComboBox::Paste()
511 SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
514 void wxComboBox::Undo()
518 HWND hEditWnd
= (HWND
) GetEditHWND();
520 ::SendMessage(hEditWnd
, EM_UNDO
, 0, 0);
524 void wxComboBox::Redo()
528 // Same as Undo, since Undo undoes the undo, i.e. a redo.
529 HWND hEditWnd
= (HWND
) GetEditHWND();
531 ::SendMessage(hEditWnd
, EM_UNDO
, 0, 0);
535 void wxComboBox::SelectAll()
537 SetSelection(0, GetLastPosition());
540 bool wxComboBox::CanUndo() const
545 HWND hEditWnd
= (HWND
) GetEditHWND();
547 return ::SendMessage(hEditWnd
, EM_CANUNDO
, 0, 0) != 0;
552 bool wxComboBox::CanRedo() const
557 HWND hEditWnd
= (HWND
) GetEditHWND();
559 return ::SendMessage(hEditWnd
, EM_CANUNDO
, 0, 0) != 0;
564 bool wxComboBox::HasSelection() const
567 GetSelection(&from
, &to
);
571 bool wxComboBox::CanCopy() const
573 // Can copy if there's a selection
574 return HasSelection();
577 bool wxComboBox::CanCut() const
579 return IsEditable() && CanCopy();
582 bool wxComboBox::CanPaste() const
587 // Standard edit control: check for straight text on clipboard
588 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
591 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
594 return isTextAvailable
;
597 bool wxComboBox::IsEditable() const
599 return !HasFlag(wxCB_READONLY
);
602 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
604 // Can't implement in MSW?
605 // HWND hWnd = GetHwnd();
606 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
609 void wxComboBox::SetInsertionPoint(long pos
)
611 if ( GetWindowStyle() & wxCB_READONLY
)
615 HWND hWnd
= GetHwnd();
616 ::SendMessage(hWnd
, CB_SETEDITSEL
, 0, MAKELPARAM(pos
, pos
));
617 HWND hEditWnd
= (HWND
) GetEditHWND();
620 // Scroll insertion point into view
621 SendMessage(hEditWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
622 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
623 SendMessage(hEditWnd
, EM_REPLACESEL
, 0, (LPARAM
) wxEmptyString
);
628 void wxComboBox::SetInsertionPointEnd()
630 // setting insertion point doesn't make sense for read only comboboxes
631 if ( !(GetWindowStyle() & wxCB_READONLY
) )
633 wxTextPos pos
= GetLastPosition();
634 SetInsertionPoint(pos
);
638 long wxComboBox::GetInsertionPoint() const
640 // CB_GETEDITSEL returns the index of the first character of the selection in
641 // its low-order word
642 DWORD pos
= (DWORD
)::SendMessage(GetHwnd(), CB_GETEDITSEL
, 0, 0L);
646 wxTextPos
wxComboBox::GetLastPosition() const
648 HWND hEditWnd
= (HWND
) GetEditHWND();
650 // Get number of characters in the last (only) line. We'll add this to the character
651 // index for the last line, 1st position.
652 wxTextPos lineLength
= (wxTextPos
)SendMessage(hEditWnd
, EM_LINELENGTH
, (WPARAM
) 0, (LPARAM
)0L);
657 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
662 // Now replace with 'value', by pasting.
663 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
665 // Paste into edit control
666 SendMessage(GetHwnd(), WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
674 void wxComboBox::Remove(long from
, long to
)
676 // Set selection and remove it
677 SetSelection(from
, to
);
678 SendMessage(GetHwnd(), WM_CUT
, (WPARAM
)0, (LPARAM
)0);
681 void wxComboBox::SetSelection(long from
, long to
)
683 // if from and to are both -1, it means (in wxWidgets) that all text should
684 // be selected, translate this into Windows convention
685 if ( (from
== -1) && (to
== -1) )
690 if ( SendMessage(GetHwnd(), CB_SETEDITSEL
,
691 0, (LPARAM
)MAKELONG(from
, to
)) == CB_ERR
)
693 wxLogDebug(_T("CB_SETEDITSEL failed"));
697 void wxComboBox::GetSelection(long* from
, long* to
) const
699 DWORD dwStart
, dwEnd
;
700 if ( ::SendMessage(GetHwnd(), CB_GETEDITSEL
,
701 (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
) == CB_ERR
)
713 // ----------------------------------------------------------------------------
714 // standard event handling
715 // ----------------------------------------------------------------------------
717 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
722 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
727 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
732 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
737 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
742 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
745 GetSelection(& from
, & to
);
746 if (from
!= -1 && to
!= -1)
750 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
752 SetSelection(-1, -1);
755 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
757 event
.Enable( CanCut() );
760 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
762 event
.Enable( CanCopy() );
765 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
767 event
.Enable( CanPaste() );
770 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
772 event
.Enable( CanUndo() );
775 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
777 event
.Enable( CanRedo() );
780 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
782 event
.Enable(HasSelection() && IsEditable());
785 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
787 event
.Enable(GetLastPosition() > 0);
790 #endif // wxUSE_COMBOBOX