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_TEXT_ENTER
36 #include "wx/textctrl.h"
41 #include "wx/clipbrd.h"
42 #include "wx/wupdlock.h"
43 #include "wx/msw/private.h"
46 #include "wx/msw/uxtheme.h"
50 #include "wx/tooltip.h"
51 #endif // wxUSE_TOOLTIPS
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
58 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
59 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
60 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
61 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
62 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
63 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
64 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
66 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
67 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
68 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
69 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
70 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
71 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
72 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
75 // ----------------------------------------------------------------------------
76 // function prototypes
77 // ----------------------------------------------------------------------------
79 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
84 // ---------------------------------------------------------------------------
86 // ---------------------------------------------------------------------------
88 // the pointer to standard radio button wnd proc
89 static WNDPROC gs_wndprocEdit
= (WNDPROC
)NULL
;
91 // ============================================================================
93 // ============================================================================
98 // Check if the given message should be forwarded from the edit control which
99 // is part of the combobox to wxComboBox itself. All messages generating the
100 // events that the code using wxComboBox could be interested in must be
102 bool ShouldForwardFromEditToCombo(UINT message
)
123 } // anonymous namespace
125 // ----------------------------------------------------------------------------
126 // wnd proc for subclassed edit control
127 // ----------------------------------------------------------------------------
129 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
134 HWND hwndCombo
= ::GetParent(hWnd
);
135 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)hwndCombo
);
137 if ( ShouldForwardFromEditToCombo(message
) )
139 wxComboBox
*combo
= wxDynamicCast(win
, wxComboBox
);
142 // we can get WM_KILLFOCUS while our parent is already half
143 // destroyed and hence doesn't look like a combobx any
144 // longer, check for it to avoid bogus assert failures
145 if ( !win
->IsBeingDeleted() )
147 wxFAIL_MSG( wxT("should have combo as parent") );
150 else if ( combo
->MSWProcessEditMsg(message
, wParam
, lParam
) )
156 else if ( message
== WM_GETDLGCODE
)
158 wxCHECK_MSG( win
, 0, wxT("should have a parent") );
160 if ( win
->GetWindowStyle() & wxTE_PROCESS_ENTER
)
162 // need to return a custom dlg code or we'll never get it
163 return DLGC_WANTMESSAGE
;
167 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
170 // ----------------------------------------------------------------------------
171 // wxComboBox callbacks
172 // ----------------------------------------------------------------------------
174 WXLRESULT
wxComboBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
176 // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to
177 // set its colour correctly (to be the same as our own one)
182 // wxStaticBox can generate this message, when modifying the control's style.
183 // This causes the content of the combobox to be selected, for some reason.
184 case WM_STYLECHANGED
:
186 // combobox selection sometimes spontaneously changes when its
187 // size changes, restore it to the old value if necessary
188 if ( !GetEditHWNDIfAvailable() )
192 GetSelection(&fromOld
, &toOld
);
194 // if an editable combobox has a not empty text not from the
195 // list, it tries to autocomplete it from the list when it is
196 // resized, but we don't want this to happen as it doesn't seem
197 // to make any sense, so we forcefully restore the old text
199 if ( !HasFlag(wxCB_READONLY
) && GetCurrentSelection() == -1 )
200 textOld
= GetValue();
202 // eliminate flickering during following hacks
203 wxWindowUpdateLocker
lock(this);
205 WXLRESULT result
= wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
207 if ( !textOld
.empty() && GetValue() != textOld
)
211 GetSelection(&fromNew
, &toNew
);
213 if ( fromOld
!= fromNew
|| toOld
!= toNew
)
215 SetSelection(fromOld
, toOld
);
222 return wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
225 bool wxComboBox::MSWProcessEditMsg(WXUINT msg
, WXWPARAM wParam
, WXLPARAM lParam
)
230 // for compatibility with wxTextCtrl, generate a special message
231 // when Enter is pressed
232 if ( wParam
== VK_RETURN
)
234 if (SendMessage(GetHwnd(), CB_GETDROPPEDSTATE
, 0, 0))
237 wxCommandEvent
event(wxEVT_TEXT_ENTER
, m_windowId
);
239 const int sel
= GetSelection();
241 event
.SetString(GetValue());
242 InitCommandEventWithItems(event
, sel
);
244 if ( ProcessCommand(event
) )
246 // don't let the event through to the native control
247 // because it doesn't need it and may generate an annoying
248 // beep if it gets it
252 // fall through, WM_CHAR is one of the message we should forward.
255 if ( ShouldForwardFromEditToCombo(msg
) )
257 // For all the messages forward from the edit control the
258 // result is not used.
260 return MSWHandleMessage(&result
, msg
, wParam
, lParam
);
267 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD id
)
275 // remember the last selection, just as wxChoice does
276 m_lastAcceptedSelection
= GetCurrentSelection();
278 wxCommandEvent
event(wxEVT_COMBOBOX_DROPDOWN
, GetId());
279 event
.SetEventObject(this);
280 ProcessCommand(event
);
285 // Do the same thing as in wxChoice but using different event type.
286 if ( m_pendingSelection
!= wxID_NONE
)
288 SendSelectionChangedEvent(wxEVT_COMBOBOX
);
289 m_pendingSelection
= wxID_NONE
;
292 wxCommandEvent
event(wxEVT_COMBOBOX_CLOSEUP
, GetId());
293 event
.SetEventObject(this);
294 ProcessCommand(event
);
299 #ifndef __SMARTPHONE__
300 // we need to reset this to prevent the selection from being undone
301 // by wxChoice, see wxChoice::MSWCommand() and comments there
302 m_lastAcceptedSelection
= wxID_NONE
;
305 // set these variables so that they could be also fixed in
306 // CBN_EDITCHANGE below
307 sel
= GetSelection();
308 value
= GetStringSelection();
310 // this string is going to become the new combobox value soon but
311 // we need it to be done right now, otherwise the event handler
312 // could get a wrong value when it calls our GetValue()
313 ::SetWindowText(GetHwnd(), value
.t_str());
315 SendSelectionChangedEvent(wxEVT_COMBOBOX
);
317 // fall through: for compatibility with wxGTK, also send the text
318 // update event when the selection changes (this also seems more
319 // logical as the text does change)
322 if ( m_allowTextEvents
)
324 wxCommandEvent
event(wxEVT_TEXT
, GetId());
326 // if sel != -1, value was already initialized above
329 value
= wxGetWindowText(GetHwnd());
332 event
.SetString(value
);
333 InitCommandEventWithItems(event
, sel
);
335 ProcessCommand(event
);
340 return wxChoice::MSWCommand(param
, id
);
343 // skip wxChoice version as it would generate its own events for
344 // CBN_SELENDOK and also interfere with our handling of CBN_DROPDOWN
348 bool wxComboBox::MSWShouldPreProcessMessage(WXMSG
*pMsg
)
350 // prevent command accelerators from stealing editing
351 // hotkeys when we have the focus
354 WPARAM vkey
= pMsg
->wParam
;
369 return wxChoice::MSWShouldPreProcessMessage(pMsg
);
372 WXHWND
wxComboBox::GetEditHWNDIfAvailable() const
374 // FIXME-VC6: Only VC6 needs this guard, see WINVER definition in
375 // include/wx/msw/wrapwin.h
376 #if defined(WINVER) && WINVER >= 0x0500
377 WinStruct
<COMBOBOXINFO
> info
;
378 if ( MSWGetComboBoxInfo(&info
) )
379 return info
.hwndItem
;
380 #endif // WINVER >= 0x0500
382 if (HasFlag(wxCB_SIMPLE
))
386 return (WXHWND
) ::ChildWindowFromPoint(GetHwnd(), pt
);
389 // notice that a slightly safer alternative could be to use FindWindowEx()
390 // but it's not available under WinCE so just take the first child for now
391 // to keep one version of the code for all platforms and fix it later if
392 // problems are discovered
394 // we assume that the only child of the combobox is the edit window
395 return (WXHWND
)::GetWindow(GetHwnd(), GW_CHILD
);
398 WXHWND
wxComboBox::GetEditHWND() const
400 // this function should not be called for wxCB_READONLY controls, it is
401 // the callers responsibility to check this
402 wxASSERT_MSG( !HasFlag(wxCB_READONLY
),
403 wxT("read-only combobox doesn't have any edit control") );
405 WXHWND hWndEdit
= GetEditHWNDIfAvailable();
406 wxASSERT_MSG( hWndEdit
, wxT("combobox without edit control?") );
411 wxWindow
*wxComboBox::GetEditableWindow()
413 wxASSERT_MSG( !HasFlag(wxCB_READONLY
),
414 wxT("read-only combobox doesn't have any edit control") );
419 // ----------------------------------------------------------------------------
420 // wxComboBox creation
421 // ----------------------------------------------------------------------------
423 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
424 const wxString
& value
,
427 int n
, const wxString choices
[],
429 const wxValidator
& validator
,
430 const wxString
& name
)
432 // pretend that wxComboBox is hidden while it is positioned and resized and
433 // show it only right before leaving this method because otherwise there is
434 // some noticeable flicker while the control rearranges itself
437 if ( !CreateAndInit(parent
, id
, pos
, size
, n
, choices
, style
,
441 // we shouldn't call SetValue() for an empty string because this would
442 // (correctly) result in an assert with a read only combobox and is useless
443 // for the other ones anyhow
444 if ( !value
.empty() )
447 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
448 // and an edit control inside it and if we want to catch events from this
449 // edit control, we must subclass it as well
450 if ( !(style
& wxCB_READONLY
) )
452 gs_wndprocEdit
= wxSetWindowProc((HWND
)GetEditHWND(), wxComboEditWndProc
);
455 // and finally, show the control
461 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
462 const wxString
& value
,
465 const wxArrayString
& choices
,
467 const wxValidator
& validator
,
468 const wxString
& name
)
470 wxCArrayString
chs(choices
);
471 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
472 chs
.GetStrings(), style
, validator
, name
);
475 WXDWORD
wxComboBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
477 // we never have an external border
478 WXDWORD msStyle
= wxChoice::MSWGetStyle
480 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
483 // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're
484 // created hidden (see Create() above), it is not done for us but we still
485 // want to have this style
486 msStyle
|= WS_TABSTOP
;
488 // remove the style always added by wxChoice
489 msStyle
&= ~CBS_DROPDOWNLIST
;
491 if ( style
& wxCB_READONLY
)
492 msStyle
|= CBS_DROPDOWNLIST
;
494 else if ( style
& wxCB_SIMPLE
)
495 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
498 msStyle
|= CBS_DROPDOWN
;
500 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
501 msStyle
|= CBS_AUTOHSCROLL
;
503 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
508 // ----------------------------------------------------------------------------
509 // wxComboBox text control-like methods
510 // ----------------------------------------------------------------------------
512 wxString
wxComboBox::GetValue() const
514 return HasFlag(wxCB_READONLY
) ? GetStringSelection()
515 : wxTextEntry::GetValue();
518 void wxComboBox::SetValue(const wxString
& value
)
520 if ( HasFlag(wxCB_READONLY
) )
521 SetStringSelection(value
);
523 wxTextEntry::SetValue(value
);
526 void wxComboBox::Clear()
529 if ( !HasFlag(wxCB_READONLY
) )
530 wxTextEntry::Clear();
533 bool wxComboBox::ContainsHWND(WXHWND hWnd
) const
535 return hWnd
== GetEditHWNDIfAvailable();
538 void wxComboBox::GetSelection(long *from
, long *to
) const
540 if ( !HasFlag(wxCB_READONLY
) )
542 wxTextEntry::GetSelection(from
, to
);
544 else // text selection doesn't make sense for read only comboboxes
553 bool wxComboBox::IsEditable() const
555 return !HasFlag(wxCB_READONLY
) && wxTextEntry::IsEditable();
558 // ----------------------------------------------------------------------------
559 // standard event handling
560 // ----------------------------------------------------------------------------
562 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
567 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
572 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
577 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
582 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
587 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
592 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
597 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
599 event
.Enable( CanCut() );
602 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
604 event
.Enable( CanCopy() );
607 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
609 event
.Enable( CanPaste() );
612 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
614 event
.Enable( IsEditable() && CanUndo() );
617 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
619 event
.Enable( IsEditable() && CanRedo() );
622 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
624 event
.Enable(IsEditable() && HasSelection());
627 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
629 event
.Enable(IsEditable() && !wxTextEntry::IsEmpty());
634 void wxComboBox::DoSetToolTip(wxToolTip
*tip
)
636 wxChoice::DoSetToolTip(tip
);
638 if ( tip
&& !HasFlag(wxCB_READONLY
) )
639 tip
->AddOtherWindow(GetEditHWND());
642 #endif // wxUSE_TOOLTIPS
646 bool wxComboBox::SetHint(const wxString
& hintOrig
)
648 wxString
hint(hintOrig
);
649 if ( wxUxThemeEngine::GetIfActive() )
651 // under XP (but not Vista) there is a bug in cue banners
652 // implementation for combobox edit control: the first character is
653 // partially chopped off, so prepend a space to make it fully visible
657 return wxTextEntry::SetHint(hint
);
660 #endif // wxUSE_UXTHEME
662 wxSize
wxComboBox::DoGetSizeFromTextSize(int xlen
, int ylen
) const
664 wxSize
tsize( wxChoice::DoGetSizeFromTextSize(xlen
, ylen
) );
666 if ( !HasFlag(wxCB_READONLY
) )
668 // Add the margins we have previously set
669 wxPoint
marg( GetMargins() );
670 marg
.x
= wxMax(0, marg
.x
);
671 marg
.y
= wxMax(0, marg
.y
);
678 #endif // wxUSE_COMBOBOX