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
, wxControl
,"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
, wxControl
)
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
;
212 if( win
->HandleClipboardEvent( message
) )
217 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
220 // ----------------------------------------------------------------------------
221 // wxComboBox callbacks
222 // ----------------------------------------------------------------------------
224 WXLRESULT
wxComboBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
226 // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to
227 // set its colour correctly (to be the same as our own one)
232 // wxStaticBox can generate this message, when modifying the control's style.
233 // This causes the content of the combobox to be selected, for some reason.
234 case WM_STYLECHANGED
:
236 // combobox selection sometimes spontaneously changes when its
237 // size changes, restore it to the old value if necessary
239 GetSelection(&fromOld
, &toOld
);
240 WXLRESULT result
= wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
243 GetSelection(&fromNew
, &toNew
);
245 if ( fromOld
!= fromNew
|| toOld
!= toNew
)
247 SetSelection(fromOld
, toOld
);
254 return wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
257 bool wxComboBox::MSWProcessEditMsg(WXUINT msg
, WXWPARAM wParam
, WXLPARAM lParam
)
262 // for compatibility with wxTextCtrl, generate a special message
263 // when Enter is pressed
264 if ( wParam
== VK_RETURN
)
266 if (SendMessage(GetHwnd(), CB_GETDROPPEDSTATE
, 0, 0))
269 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
271 const int sel
= GetSelection();
273 event
.SetString(GetValue());
274 InitCommandEventWithItems(event
, sel
);
276 if ( ProcessCommand(event
) )
278 // don't let the event through to the native control
279 // because it doesn't need it and may generate an annoying
280 // beep if it gets it
287 return HandleChar(wParam
, lParam
, true /* isASCII */);
291 return HandleKeyDown(wParam
, lParam
);
295 return HandleKeyUp(wParam
, lParam
);
298 return HandleSetFocus((WXHWND
)wParam
);
301 return HandleKillFocus((WXHWND
)wParam
);
307 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD id
)
315 #ifndef __SMARTPHONE__
316 // we need to reset this to prevent the selection from being undone
317 // by wxChoice, see wxChoice::MSWCommand() and comments there
318 m_lastAcceptedSelection
= wxID_NONE
;
321 // set these variables so that they could be also fixed in
322 // CBN_EDITCHANGE below
323 sel
= GetSelection();
324 value
= GetStringSelection();
326 // this string is going to become the new combobox value soon but
327 // we need it to be done right now, otherwise the event handler
328 // could get a wrong value when it calls our GetValue()
329 ::SetWindowText(GetHwnd(), value
);
332 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
334 event
.SetString(value
);
335 InitCommandEventWithItems(event
, sel
);
337 ProcessCommand(event
);
340 // fall through: for compability with wxGTK, also send the text
341 // update event when the selection changes (this also seems more
342 // logical as the text does change)
346 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
348 // if sel != -1, value was already initialized above
351 value
= wxGetWindowText(GetHwnd());
354 event
.SetString(value
);
355 InitCommandEventWithItems(event
, sel
);
357 ProcessCommand(event
);
362 return wxChoice::MSWCommand(param
, id
);
365 // skip wxChoice version as it would generate its own events for
367 return wxControl::MSWCommand(param
, id
);
370 WXHWND
wxComboBox::GetEditHWND() const
372 // this function should not be called for wxCB_READONLY controls, it is
373 // the callers responsability to check this
374 wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY
),
375 _T("read-only combobox doesn't have any edit control") );
379 HWND hwndEdit
= ::ChildWindowFromPoint(GetHwnd(), pt
);
380 if ( !hwndEdit
|| hwndEdit
== GetHwnd() )
382 wxFAIL_MSG(_T("not read only combobox without edit control?"));
385 return (WXHWND
)hwndEdit
;
388 // ----------------------------------------------------------------------------
389 // wxComboBox creation
390 // ----------------------------------------------------------------------------
392 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
393 const wxString
& value
,
396 int n
, const wxString choices
[],
398 const wxValidator
& validator
,
399 const wxString
& name
)
401 // pretend that wxComboBox is hidden while it is positioned and resized and
402 // show it only right before leaving this method because otherwise there is
403 // some noticeable flicker while the control rearranges itself
406 if ( !CreateAndInit(parent
, id
, pos
, size
, n
, choices
, style
,
410 // we shouldn't call SetValue() for an empty string because this would
411 // (correctly) result in an assert with a read only combobox and is useless
412 // for the other ones anyhow
413 if ( !value
.empty() )
416 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
417 // and an edit control inside it and if we want to catch events from this
418 // edit control, we must subclass it as well
419 if ( !(style
& wxCB_READONLY
) )
421 gs_wndprocEdit
= wxSetWindowProc((HWND
)GetEditHWND(), wxComboEditWndProc
);
424 // and finally, show the control
430 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
431 const wxString
& value
,
434 const wxArrayString
& choices
,
436 const wxValidator
& validator
,
437 const wxString
& name
)
439 wxCArrayString
chs(choices
);
440 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
441 chs
.GetStrings(), style
, validator
, name
);
444 WXDWORD
wxComboBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
446 // we never have an external border
447 WXDWORD msStyle
= wxChoice::MSWGetStyle
449 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
452 // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're
453 // created hidden (see Create() above), it is not done for us but we still
454 // want to have this style
455 msStyle
|= WS_TABSTOP
;
457 // remove the style always added by wxChoice
458 msStyle
&= ~CBS_DROPDOWNLIST
;
460 if ( style
& wxCB_READONLY
)
461 msStyle
|= CBS_DROPDOWNLIST
;
463 else if ( style
& wxCB_SIMPLE
)
464 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
467 msStyle
|= CBS_DROPDOWN
;
469 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
470 msStyle
|= CBS_AUTOHSCROLL
;
472 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
477 // ----------------------------------------------------------------------------
478 // wxComboBox text control-like methods
479 // ----------------------------------------------------------------------------
481 wxString
wxComboBox::GetValue() const
483 return wxGetWindowText(m_hWnd
);
486 void wxComboBox::SetValue(const wxString
& value
)
488 if ( HasFlag(wxCB_READONLY
) )
489 SetStringSelection(value
);
491 SetWindowText(GetHwnd(), value
.c_str());
494 // Clipboard operations
495 void wxComboBox::Copy()
497 SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
500 void wxComboBox::Cut()
502 SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
505 void wxComboBox::Paste()
507 SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
510 void wxComboBox::Undo()
514 HWND hEditWnd
= (HWND
) GetEditHWND();
516 ::SendMessage(hEditWnd
, EM_UNDO
, 0, 0);
520 void wxComboBox::Redo()
524 // Same as Undo, since Undo undoes the undo, i.e. a redo.
525 HWND hEditWnd
= (HWND
) GetEditHWND();
527 ::SendMessage(hEditWnd
, EM_UNDO
, 0, 0);
531 void wxComboBox::SelectAll()
533 SetSelection(0, GetLastPosition());
536 bool wxComboBox::CanUndo() const
541 HWND hEditWnd
= (HWND
) GetEditHWND();
543 return ::SendMessage(hEditWnd
, EM_CANUNDO
, 0, 0) != 0;
548 bool wxComboBox::CanRedo() const
553 HWND hEditWnd
= (HWND
) GetEditHWND();
555 return ::SendMessage(hEditWnd
, EM_CANUNDO
, 0, 0) != 0;
560 bool wxComboBox::HasSelection() const
563 GetSelection(&from
, &to
);
567 bool wxComboBox::CanCopy() const
569 // Can copy if there's a selection
570 return HasSelection();
573 bool wxComboBox::CanCut() const
575 return IsEditable() && CanCopy();
578 bool wxComboBox::CanPaste() const
583 // Standard edit control: check for straight text on clipboard
584 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
587 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
590 return isTextAvailable
;
593 bool wxComboBox::IsEditable() const
595 return !HasFlag(wxCB_READONLY
);
598 void wxComboBox::SetEditable(bool editable
)
600 HWND hWnd
= (HWND
)GetEditHWND();
601 if ( !::SendMessage(hWnd
, EM_SETREADONLY
, !editable
, 0) )
603 wxLogLastError(_T("SendMessage(EM_SETREADONLY)"));
607 void wxComboBox::SetInsertionPoint(long pos
)
609 if ( GetWindowStyle() & wxCB_READONLY
)
612 HWND hWnd
= GetHwnd();
613 ::SendMessage(hWnd
, CB_SETEDITSEL
, 0, MAKELPARAM(pos
, pos
));
614 HWND hEditWnd
= (HWND
) GetEditHWND();
617 // Scroll insertion point into view
618 SendMessage(hEditWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
619 // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint)
620 SendMessage(hEditWnd
, EM_REPLACESEL
, 0, (LPARAM
) wxEmptyString
);
624 void wxComboBox::SetInsertionPointEnd()
626 // setting insertion point doesn't make sense for read only comboboxes
627 if ( !(GetWindowStyle() & wxCB_READONLY
) )
629 wxTextPos pos
= GetLastPosition();
630 SetInsertionPoint(pos
);
634 long wxComboBox::GetInsertionPoint() const
636 // CB_GETEDITSEL returns the index of the first character of the selection in
637 // its low-order word
638 DWORD pos
= (DWORD
)::SendMessage(GetHwnd(), CB_GETEDITSEL
, 0, 0L);
642 wxTextPos
wxComboBox::GetLastPosition() const
644 HWND hEditWnd
= (HWND
) GetEditHWND();
646 // Get number of characters in the last (only) line. We'll add this to the character
647 // index for the last line, 1st position.
648 wxTextPos lineLength
= (wxTextPos
)SendMessage(hEditWnd
, EM_LINELENGTH
, (WPARAM
) 0, (LPARAM
)0L);
653 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
658 // Now replace with 'value', by pasting.
659 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
661 // Paste into edit control
662 SendMessage(GetHwnd(), WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
670 void wxComboBox::Remove(long from
, long to
)
672 // Set selection and remove it
673 SetSelection(from
, to
);
674 SendMessage(GetHwnd(), WM_CUT
, (WPARAM
)0, (LPARAM
)0);
677 void wxComboBox::SetSelection(long from
, long to
)
679 // if from and to are both -1, it means (in wxWidgets) that all text should
680 // be selected, translate this into Windows convention
681 if ( (from
== -1) && (to
== -1) )
686 if ( SendMessage(GetHwnd(), CB_SETEDITSEL
,
687 0, (LPARAM
)MAKELONG(from
, to
)) == CB_ERR
)
689 wxLogDebug(_T("CB_SETEDITSEL failed"));
693 void wxComboBox::GetSelection(long* from
, long* to
) const
695 DWORD dwStart
, dwEnd
;
696 if ( ::SendMessage(GetHwnd(), CB_GETEDITSEL
,
697 (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
) == CB_ERR
)
709 // ----------------------------------------------------------------------------
710 // standard event handling
711 // ----------------------------------------------------------------------------
713 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
718 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
723 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
728 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
733 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
738 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
741 GetSelection(& from
, & to
);
742 if (from
!= -1 && to
!= -1)
746 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
748 SetSelection(-1, -1);
751 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
753 event
.Enable( CanCut() );
756 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
758 event
.Enable( CanCopy() );
761 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
763 event
.Enable( CanPaste() );
766 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
768 event
.Enable( CanUndo() );
771 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
773 event
.Enable( CanRedo() );
776 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
778 event
.Enable(HasSelection() && IsEditable());
781 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
783 event
.Enable(GetLastPosition() > 0);
786 #endif // wxUSE_COMBOBOX