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/settings.h"
34 // for wxEVT_COMMAND_TEXT_ENTER
35 #include "wx/textctrl.h"
40 #include "wx/clipbrd.h"
41 #include "wx/msw/private.h"
44 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
47 #include "wx/tooltip.h"
48 #endif // wxUSE_TOOLTIPS
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 #if wxUSE_EXTENDED_RTTI
55 WX_DEFINE_FLAGS( wxComboBoxStyle
)
57 wxBEGIN_FLAGS( wxComboBoxStyle
)
58 // new style border flags, we put them first to
59 // use them for streaming out
60 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
61 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
62 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
63 wxFLAGS_MEMBER(wxBORDER_RAISED
)
64 wxFLAGS_MEMBER(wxBORDER_STATIC
)
65 wxFLAGS_MEMBER(wxBORDER_NONE
)
67 // old style border flags
68 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
69 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
70 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
71 wxFLAGS_MEMBER(wxRAISED_BORDER
)
72 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
73 wxFLAGS_MEMBER(wxBORDER
)
75 // standard window styles
76 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
77 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
78 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
79 wxFLAGS_MEMBER(wxWANTS_CHARS
)
80 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
81 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
82 wxFLAGS_MEMBER(wxVSCROLL
)
83 wxFLAGS_MEMBER(wxHSCROLL
)
85 wxFLAGS_MEMBER(wxCB_SIMPLE
)
86 wxFLAGS_MEMBER(wxCB_SORT
)
87 wxFLAGS_MEMBER(wxCB_READONLY
)
88 wxFLAGS_MEMBER(wxCB_DROPDOWN
)
90 wxEND_FLAGS( wxComboBoxStyle
)
92 IMPLEMENT_DYNAMIC_CLASS_XTI(wxComboBox
, wxControl
,"wx/combobox.h")
94 wxBEGIN_PROPERTIES_TABLE(wxComboBox
)
95 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_COMBOBOX_SELECTED
, wxCommandEvent
)
96 wxEVENT_PROPERTY( TextEnter
, wxEVT_COMMAND_TEXT_ENTER
, wxCommandEvent
)
99 wxPROPERTY( Font
, wxFont
, SetFont
, GetFont
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
100 wxPROPERTY_COLLECTION( Choices
, wxArrayString
, wxString
, AppendString
, GetStrings
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
101 wxPROPERTY( Value
,wxString
, SetValue
, GetValue
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
102 wxPROPERTY( Selection
,int, SetSelection
, GetSelection
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
103 wxPROPERTY_FLAGS( WindowStyle
, wxComboBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
104 wxEND_PROPERTIES_TABLE()
106 wxBEGIN_HANDLERS_TABLE(wxComboBox
)
107 wxEND_HANDLERS_TABLE()
109 wxCONSTRUCTOR_5( wxComboBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Value
, wxPoint
, Position
, wxSize
, Size
)
113 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
117 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
118 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
119 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
120 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
121 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
122 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
123 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
124 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
126 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
127 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
128 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
129 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
130 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
131 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
132 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
135 // ----------------------------------------------------------------------------
136 // function prototypes
137 // ----------------------------------------------------------------------------
139 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
144 // ---------------------------------------------------------------------------
146 // ---------------------------------------------------------------------------
148 // the pointer to standard radio button wnd proc
149 static WNDPROC gs_wndprocEdit
= (WNDPROC
)NULL
;
151 // ============================================================================
153 // ============================================================================
155 // ----------------------------------------------------------------------------
156 // wnd proc for subclassed edit control
157 // ----------------------------------------------------------------------------
159 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
164 HWND hwndCombo
= ::GetParent(hWnd
);
165 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)hwndCombo
);
169 // forward some messages to the combobox to generate the appropriate
170 // wxEvents from them
180 wxComboBox
*combo
= wxDynamicCast(win
, wxComboBox
);
183 // we can get WM_KILLFOCUS while our parent is already half
184 // destroyed and hence doesn't look like a combobx any
185 // longer, check for it to avoid bogus assert failures
186 if ( !win
->IsBeingDeleted() )
188 wxFAIL_MSG( _T("should have combo as parent") );
191 else if ( combo
->MSWProcessEditMsg(message
, wParam
, lParam
) )
201 wxCHECK_MSG( win
, 0, _T("should have a parent") );
203 if ( win
->GetWindowStyle() & wxTE_PROCESS_ENTER
)
205 // need to return a custom dlg code or we'll never get it
206 return DLGC_WANTMESSAGE
;
212 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
215 // ----------------------------------------------------------------------------
216 // wxComboBox callbacks
217 // ----------------------------------------------------------------------------
219 WXLRESULT
wxComboBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
221 // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to
222 // set its colour correctly (to be the same as our own one)
227 // wxStaticBox can generate this message, when modifying the control's style.
228 // This causes the content of the combobox to be selected, for some reason.
229 case WM_STYLECHANGED
:
231 // combobox selection sometimes spontaneously changes when its
232 // size changes, restore it to the old value if necessary
234 GetSelection(&fromOld
, &toOld
);
235 WXLRESULT result
= wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
238 GetSelection(&fromNew
, &toNew
);
240 if ( fromOld
!= fromNew
|| toOld
!= toNew
)
242 SetSelection(fromOld
, toOld
);
249 return wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
252 bool wxComboBox::MSWProcessEditMsg(WXUINT msg
, WXWPARAM wParam
, WXLPARAM lParam
)
257 // for compatibility with wxTextCtrl, generate a special message
258 // when Enter is pressed
259 if ( wParam
== VK_RETURN
)
261 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
263 const int sel
= GetSelection();
265 event
.SetString(GetValue());
266 InitCommandEventWithItems(event
, sel
);
268 if ( ProcessCommand(event
) )
270 // don't let the event through to the native control
271 // because it doesn't need it and may generate an annoying
272 // beep if it gets it
279 return HandleChar(wParam
, lParam
, true /* isASCII */);
283 return HandleKeyDown(wParam
, lParam
);
287 return HandleKeyUp(wParam
, lParam
);
290 return HandleSetFocus((WXHWND
)wParam
);
293 return HandleKillFocus((WXHWND
)wParam
);
299 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD id
)
307 #ifndef __SMARTPHONE__
308 // we need to reset this to prevent the selection from being undone
309 // by wxChoice, see wxChoice::MSWCommand() and comments there
310 m_lastAcceptedSelection
= wxID_NONE
;
313 // set these variables so that they could be also fixed in
314 // CBN_EDITCHANGE below
315 sel
= GetSelection();
316 value
= GetStringSelection();
318 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
320 event
.SetString(value
);
321 InitCommandEventWithItems(event
, sel
);
323 ProcessCommand(event
);
326 // fall through: for compability with wxGTK, also send the text
327 // update event when the selection changes (this also seems more
328 // logical as the text does change)
332 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
334 // if sel != -1, value was already initialized above
337 value
= wxGetWindowText(GetHwnd());
340 event
.SetString(value
);
341 InitCommandEventWithItems(event
, sel
);
343 ProcessCommand(event
);
348 return wxChoice::MSWCommand(param
, id
);
351 // skip wxChoice version as it would generate its own events for
353 return wxControl::MSWCommand(param
, id
);
356 WXHWND
wxComboBox::GetEditHWND() const
358 // this function should not be called for wxCB_READONLY controls, it is
359 // the callers responsability to check this
360 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY
),
361 _T("read-only combobox doesn't have any edit control") );
365 HWND hwndEdit
= ::ChildWindowFromPoint(GetHwnd(), pt
);
366 if ( !hwndEdit
|| hwndEdit
== GetHwnd() )
368 wxFAIL_MSG(_T("not read only combobox without edit control?"));
371 return (WXHWND
)hwndEdit
;
374 // ----------------------------------------------------------------------------
375 // wxComboBox creation
376 // ----------------------------------------------------------------------------
378 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
379 const wxString
& value
,
382 int n
, const wxString choices
[],
384 const wxValidator
& validator
,
385 const wxString
& name
)
387 // pretend that wxComboBox is hidden while it is positioned and resized and
388 // show it only right before leaving this method because otherwise there is
389 // some noticeable flicker while the control rearranges itself
392 if ( !CreateAndInit(parent
, id
, pos
, size
, n
, choices
, style
,
396 // we shouldn't call SetValue() for an empty string because this would
397 // (correctly) result in an assert with a read only combobox and is useless
398 // for the other ones anyhow
399 if ( !value
.empty() )
402 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
403 // and an edit control inside it and if we want to catch events from this
404 // edit control, we must subclass it as well
405 if ( !(style
& wxCB_READONLY
) )
407 gs_wndprocEdit
= wxSetWindowProc((HWND
)GetEditHWND(),
411 // and finally, show the control
417 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
418 const wxString
& value
,
421 const wxArrayString
& choices
,
423 const wxValidator
& validator
,
424 const wxString
& name
)
426 wxCArrayString
chs(choices
);
427 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
428 chs
.GetStrings(), style
, validator
, name
);
431 WXDWORD
wxComboBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
433 // we never have an external border
434 WXDWORD msStyle
= wxChoice::MSWGetStyle
436 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
439 // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're
440 // created hidden (see Create() above), it is not done for us but we still
441 // want to have this style
442 msStyle
|= WS_TABSTOP
;
444 // remove the style always added by wxChoice
445 msStyle
&= ~CBS_DROPDOWNLIST
;
447 if ( style
& wxCB_READONLY
)
448 msStyle
|= CBS_DROPDOWNLIST
;
450 else if ( style
& wxCB_SIMPLE
)
451 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
454 msStyle
|= CBS_DROPDOWN
;
456 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
457 msStyle
|= CBS_AUTOHSCROLL
;
459 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
464 // ----------------------------------------------------------------------------
465 // wxComboBox text control-like methods
466 // ----------------------------------------------------------------------------
468 wxString
wxComboBox::GetValue() const
470 return wxGetWindowText(m_hWnd
);
473 void wxComboBox::SetValue(const wxString
& value
)
475 if ( HasFlag(wxCB_READONLY
) )
476 SetStringSelection(value
);
478 SetWindowText(GetHwnd(), value
.c_str());
481 // Clipboard operations
482 void wxComboBox::Copy()
484 SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
487 void wxComboBox::Cut()
489 SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
492 void wxComboBox::Paste()
494 SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
497 void wxComboBox::Undo()
501 HWND hEditWnd
= (HWND
) GetEditHWND();
503 ::SendMessage(hEditWnd
, EM_UNDO
, 0, 0);
507 void wxComboBox::Redo()
511 // Same as Undo, since Undo undoes the undo, i.e. a redo.
512 HWND hEditWnd
= (HWND
) GetEditHWND();
514 ::SendMessage(hEditWnd
, EM_UNDO
, 0, 0);
518 void wxComboBox::SelectAll()
520 SetSelection(0, GetLastPosition());
523 bool wxComboBox::CanUndo() const
528 HWND hEditWnd
= (HWND
) GetEditHWND();
530 return ::SendMessage(hEditWnd
, EM_CANUNDO
, 0, 0) != 0;
535 bool wxComboBox::CanRedo() const
540 HWND hEditWnd
= (HWND
) GetEditHWND();
542 return ::SendMessage(hEditWnd
, EM_CANUNDO
, 0, 0) != 0;
547 bool wxComboBox::HasSelection() const
550 GetSelection(&from
, &to
);
554 bool wxComboBox::CanCopy() const
556 // Can copy if there's a selection
557 return HasSelection();
560 bool wxComboBox::CanCut() const
562 return IsEditable() && CanCopy();
565 bool wxComboBox::CanPaste() const
570 // Standard edit control: check for straight text on clipboard
571 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
574 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
577 return isTextAvailable
;
580 bool wxComboBox::IsEditable() const
582 return !HasFlag(wxCB_READONLY
);
585 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
587 // Can't implement in MSW?
588 // HWND hWnd = GetHwnd();
589 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
592 void wxComboBox::SetInsertionPoint(long pos
)
594 if ( GetWindowStyle() & wxCB_READONLY
)
598 HWND hWnd
= GetHwnd();
599 ::SendMessage(hWnd
, CB_SETEDITSEL
, 0, MAKELPARAM(pos
, pos
));
600 HWND hEditWnd
= (HWND
) GetEditHWND();
603 // Scroll insertion point into view
604 SendMessage(hEditWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
605 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
606 SendMessage(hEditWnd
, EM_REPLACESEL
, 0, (LPARAM
) wxEmptyString
);
611 void wxComboBox::SetInsertionPointEnd()
613 // setting insertion point doesn't make sense for read only comboboxes
614 if ( !(GetWindowStyle() & wxCB_READONLY
) )
616 wxTextPos pos
= GetLastPosition();
617 SetInsertionPoint(pos
);
621 long wxComboBox::GetInsertionPoint() const
623 // CB_GETEDITSEL returns the index of the first character of the selection in
624 // its low-order word
625 DWORD pos
= (DWORD
)::SendMessage(GetHwnd(), CB_GETEDITSEL
, 0, 0L);
629 wxTextPos
wxComboBox::GetLastPosition() const
631 HWND hEditWnd
= (HWND
) GetEditHWND();
633 // Get number of characters in the last (only) line. We'll add this to the character
634 // index for the last line, 1st position.
635 wxTextPos lineLength
= (wxTextPos
)SendMessage(hEditWnd
, EM_LINELENGTH
, (WPARAM
) 0, (LPARAM
)0L);
640 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
645 // Now replace with 'value', by pasting.
646 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
648 // Paste into edit control
649 SendMessage(GetHwnd(), WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
657 void wxComboBox::Remove(long from
, long to
)
659 // Set selection and remove it
660 SetSelection(from
, to
);
661 SendMessage(GetHwnd(), WM_CUT
, (WPARAM
)0, (LPARAM
)0);
664 void wxComboBox::SetSelection(long from
, long to
)
666 // if from and to are both -1, it means (in wxWidgets) that all text should
667 // be selected, translate this into Windows convention
668 if ( (from
== -1) && (to
== -1) )
673 if ( SendMessage(GetHwnd(), CB_SETEDITSEL
,
674 0, (LPARAM
)MAKELONG(from
, to
)) == CB_ERR
)
676 wxLogDebug(_T("CB_SETEDITSEL failed"));
680 void wxComboBox::GetSelection(long* from
, long* to
) const
682 DWORD dwStart
, dwEnd
;
683 if ( ::SendMessage(GetHwnd(), CB_GETEDITSEL
,
684 (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
) == CB_ERR
)
696 // ----------------------------------------------------------------------------
697 // standard event handling
698 // ----------------------------------------------------------------------------
700 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
705 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
710 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
715 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
720 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
725 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
728 GetSelection(& from
, & to
);
729 if (from
!= -1 && to
!= -1)
733 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
735 SetSelection(-1, -1);
738 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
740 event
.Enable( CanCut() );
743 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
745 event
.Enable( CanCopy() );
748 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
750 event
.Enable( CanPaste() );
753 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
755 event
.Enable( CanUndo() );
758 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
760 event
.Enable( CanRedo() );
763 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
765 event
.Enable(HasSelection() && IsEditable());
768 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
770 event
.Enable(GetLastPosition() > 0);
773 #endif // wxUSE_COMBOBOX