1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 // we need to reset this to prevent the selection from being undone
327 // by wxChoice, see wxChoice::MSWCommand() and comments there
328 m_lastAcceptedSelection
= wxID_NONE
;
330 // set these variables so that they could be also fixed in
331 // CBN_EDITCHANGE below
332 sel
= GetSelection();
335 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
336 event
.SetEventObject(this);
338 event
.SetString(value
);
339 ProcessCommand(event
);
342 // fall through: for compability with wxGTK, also send the text
343 // update event when the selection changes (this also seems more
344 // logical as the text does change)
348 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
349 event
.SetEventObject(this);
351 // if sel != -1, value was already initialized above
354 value
= wxGetWindowText(GetHwnd());
357 event
.SetString(value
);
358 ProcessCommand(event
);
363 return wxChoice::MSWCommand(param
, id
);
366 // skip wxChoice version as it would generate its own events for
368 return wxControl::MSWCommand(param
, id
);
371 WXHWND
wxComboBox::GetEditHWND() const
373 // this function should not be called for wxCB_READONLY controls, it is
374 // the callers responsability to check this
375 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY
),
376 _T("read-only combobox doesn't have any edit control") );
380 HWND hwndEdit
= ::ChildWindowFromPoint(GetHwnd(), pt
);
381 if ( !hwndEdit
|| hwndEdit
== GetHwnd() )
383 wxFAIL_MSG(_T("not read only combobox without edit control?"));
386 return (WXHWND
)hwndEdit
;
389 // ----------------------------------------------------------------------------
390 // wxComboBox creation
391 // ----------------------------------------------------------------------------
393 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
394 const wxString
& value
,
397 int n
, const wxString choices
[],
399 const wxValidator
& validator
,
400 const wxString
& name
)
402 // pretend that wxComboBox is hidden while it is positioned and resized and
403 // show it only right before leaving this method because otherwise there is
404 // some noticeable flicker while the control rearranges itself
407 if ( !CreateAndInit(parent
, id
, pos
, size
, n
, choices
, style
,
411 // we shouldn't call SetValue() for an empty string because this would
412 // (correctly) result in an assert with a read only combobox and is useless
413 // for the other ones anyhow
414 if ( !value
.empty() )
417 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
418 // and an edit control inside it and if we want to catch events from this
419 // edit control, we must subclass it as well
420 if ( !(style
& wxCB_READONLY
) )
422 gs_wndprocEdit
= wxSetWindowProc((HWND
)GetEditHWND(),
426 // and finally, show the control
432 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
433 const wxString
& value
,
436 const wxArrayString
& choices
,
438 const wxValidator
& validator
,
439 const wxString
& name
)
441 wxCArrayString
chs(choices
);
442 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
443 chs
.GetStrings(), style
, validator
, name
);
446 WXDWORD
wxComboBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
448 // we never have an external border
449 WXDWORD msStyle
= wxChoice::MSWGetStyle
451 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
454 // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're
455 // created hidden (see Create() above), it is not done for us but we still
456 // want to have this style
457 msStyle
|= WS_TABSTOP
;
459 // remove the style always added by wxChoice
460 msStyle
&= ~CBS_DROPDOWNLIST
;
462 if ( style
& wxCB_READONLY
)
463 msStyle
|= CBS_DROPDOWNLIST
;
465 else if ( style
& wxCB_SIMPLE
)
466 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
469 msStyle
|= CBS_DROPDOWN
;
471 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
472 msStyle
|= CBS_AUTOHSCROLL
;
474 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
479 // ----------------------------------------------------------------------------
480 // wxComboBox text control-like methods
481 // ----------------------------------------------------------------------------
483 wxString
wxComboBox::GetValue() const
485 return wxGetWindowText(m_hWnd
);
488 void wxComboBox::SetValue(const wxString
& value
)
490 if ( HasFlag(wxCB_READONLY
) )
491 SetStringSelection(value
);
493 SetWindowText(GetHwnd(), value
.c_str());
496 // Clipboard operations
497 void wxComboBox::Copy()
499 SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
502 void wxComboBox::Cut()
504 SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
507 void wxComboBox::Paste()
509 SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
512 void wxComboBox::Undo()
516 HWND hEditWnd
= (HWND
) GetEditHWND();
518 ::SendMessage(hEditWnd
, EM_UNDO
, 0, 0);
522 void wxComboBox::Redo()
526 // Same as Undo, since Undo undoes the undo, i.e. a redo.
527 HWND hEditWnd
= (HWND
) GetEditHWND();
529 ::SendMessage(hEditWnd
, EM_UNDO
, 0, 0);
533 void wxComboBox::SelectAll()
535 SetSelection(0, GetLastPosition());
538 bool wxComboBox::CanUndo() const
543 HWND hEditWnd
= (HWND
) GetEditHWND();
545 return ::SendMessage(hEditWnd
, EM_CANUNDO
, 0, 0) != 0;
550 bool wxComboBox::CanRedo() const
555 HWND hEditWnd
= (HWND
) GetEditHWND();
557 return ::SendMessage(hEditWnd
, EM_CANUNDO
, 0, 0) != 0;
562 bool wxComboBox::HasSelection() const
565 GetSelection(&from
, &to
);
569 bool wxComboBox::CanCopy() const
571 // Can copy if there's a selection
572 return HasSelection();
575 bool wxComboBox::CanCut() const
577 return IsEditable() && CanCopy();
580 bool wxComboBox::CanPaste() const
585 // Standard edit control: check for straight text on clipboard
586 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
589 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
592 return isTextAvailable
;
595 bool wxComboBox::IsEditable() const
597 return !HasFlag(wxCB_READONLY
);
600 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
602 // Can't implement in MSW?
603 // HWND hWnd = GetHwnd();
604 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
607 void wxComboBox::SetInsertionPoint(long pos
)
609 if ( GetWindowStyle() & wxCB_READONLY
)
613 HWND hWnd
= GetHwnd();
614 ::SendMessage(hWnd
, CB_SETEDITSEL
, 0, MAKELPARAM(pos
, pos
));
615 HWND hEditWnd
= (HWND
) GetEditHWND();
618 // Scroll insertion point into view
619 SendMessage(hEditWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
620 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
621 SendMessage(hEditWnd
, EM_REPLACESEL
, 0, (LPARAM
) wxEmptyString
);
626 void wxComboBox::SetInsertionPointEnd()
628 // setting insertion point doesn't make sense for read only comboboxes
629 if ( !(GetWindowStyle() & wxCB_READONLY
) )
631 wxTextPos pos
= GetLastPosition();
632 SetInsertionPoint(pos
);
636 long wxComboBox::GetInsertionPoint() const
638 // CB_GETEDITSEL returns the index of the first character of the selection in
639 // its low-order word
640 DWORD pos
= (DWORD
)::SendMessage(GetHwnd(), CB_GETEDITSEL
, 0, 0L);
644 wxTextPos
wxComboBox::GetLastPosition() const
646 HWND hEditWnd
= (HWND
) GetEditHWND();
648 // Get number of characters in the last (only) line. We'll add this to the character
649 // index for the last line, 1st position.
650 wxTextPos lineLength
= (wxTextPos
)SendMessage(hEditWnd
, EM_LINELENGTH
, (WPARAM
) 0, (LPARAM
)0L);
655 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
660 // Now replace with 'value', by pasting.
661 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
663 // Paste into edit control
664 SendMessage(GetHwnd(), WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
672 void wxComboBox::Remove(long from
, long to
)
674 // Set selection and remove it
675 SetSelection(from
, to
);
676 SendMessage(GetHwnd(), WM_CUT
, (WPARAM
)0, (LPARAM
)0);
679 void wxComboBox::SetSelection(long from
, long to
)
681 // if from and to are both -1, it means (in wxWidgets) that all text should
682 // be selected, translate this into Windows convention
683 if ( (from
== -1) && (to
== -1) )
688 if ( SendMessage(GetHwnd(), CB_SETEDITSEL
,
689 0, (LPARAM
)MAKELONG(from
, to
)) == CB_ERR
)
691 wxLogDebug(_T("CB_SETEDITSEL failed"));
695 void wxComboBox::GetSelection(long* from
, long* to
) const
697 DWORD dwStart
, dwEnd
;
698 if ( ::SendMessage(GetHwnd(), CB_GETEDITSEL
,
699 (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
) == CB_ERR
)
711 // ----------------------------------------------------------------------------
712 // standard event handling
713 // ----------------------------------------------------------------------------
715 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
720 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
725 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
730 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
735 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
740 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
743 GetSelection(& from
, & to
);
744 if (from
!= -1 && to
!= -1)
748 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
750 SetSelection(-1, -1);
753 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
755 event
.Enable( CanCut() );
758 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
760 event
.Enable( CanCopy() );
763 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
765 event
.Enable( CanPaste() );
768 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
770 event
.Enable( CanUndo() );
773 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
775 event
.Enable( CanRedo() );
778 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
780 event
.Enable(HasSelection() && IsEditable());
783 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
785 event
.Enable(GetLastPosition() > 0);
788 #endif // wxUSE_COMBOBOX