1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/combobox.h"
31 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/settings.h"
34 // for wxEVT_TEXT_ENTER
35 #include "wx/textctrl.h"
40 #include "wx/clipbrd.h"
41 #include "wx/wupdlock.h"
42 #include "wx/msw/private.h"
45 #include "wx/msw/uxtheme.h"
49 #include "wx/tooltip.h"
50 #endif // wxUSE_TOOLTIPS
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
57 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
58 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
59 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
60 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
61 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
62 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
63 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
65 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
66 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
67 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
68 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
69 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
70 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
71 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
74 // ----------------------------------------------------------------------------
75 // function prototypes
76 // ----------------------------------------------------------------------------
78 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
83 // ---------------------------------------------------------------------------
85 // ---------------------------------------------------------------------------
87 // the pointer to standard radio button wnd proc
88 static WNDPROC gs_wndprocEdit
= (WNDPROC
)NULL
;
90 // ============================================================================
92 // ============================================================================
97 // Check if the given message should be forwarded from the edit control which
98 // is part of the combobox to wxComboBox itself. All messages generating the
99 // events that the code using wxComboBox could be interested in must be
101 bool ShouldForwardFromEditToCombo(UINT message
)
122 } // anonymous namespace
124 // ----------------------------------------------------------------------------
125 // wnd proc for subclassed edit control
126 // ----------------------------------------------------------------------------
128 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
133 HWND hwndCombo
= ::GetParent(hWnd
);
134 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)hwndCombo
);
136 if ( ShouldForwardFromEditToCombo(message
) )
138 wxComboBox
*combo
= wxDynamicCast(win
, wxComboBox
);
141 // we can get WM_KILLFOCUS while our parent is already half
142 // destroyed and hence doesn't look like a combobx any
143 // longer, check for it to avoid bogus assert failures
144 if ( !win
->IsBeingDeleted() )
146 wxFAIL_MSG( wxT("should have combo as parent") );
149 else if ( combo
->MSWProcessEditMsg(message
, wParam
, lParam
) )
155 else if ( message
== WM_GETDLGCODE
)
157 wxCHECK_MSG( win
, 0, wxT("should have a parent") );
159 if ( win
->GetWindowStyle() & wxTE_PROCESS_ENTER
)
161 // need to return a custom dlg code or we'll never get it
162 return DLGC_WANTMESSAGE
;
166 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
169 // ----------------------------------------------------------------------------
170 // wxComboBox callbacks
171 // ----------------------------------------------------------------------------
173 WXLRESULT
wxComboBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
175 // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to
176 // set its colour correctly (to be the same as our own one)
181 // wxStaticBox can generate this message, when modifying the control's style.
182 // This causes the content of the combobox to be selected, for some reason.
183 case WM_STYLECHANGED
:
185 // combobox selection sometimes spontaneously changes when its
186 // size changes, restore it to the old value if necessary
187 if ( !GetEditHWNDIfAvailable() )
191 GetSelection(&fromOld
, &toOld
);
193 // if an editable combobox has a not empty text not from the
194 // list, it tries to autocomplete it from the list when it is
195 // resized, but we don't want this to happen as it doesn't seem
196 // to make any sense, so we forcefully restore the old text
198 if ( !HasFlag(wxCB_READONLY
) && GetCurrentSelection() == -1 )
199 textOld
= GetValue();
201 // eliminate flickering during following hacks
202 wxWindowUpdateLocker
lock(this);
204 WXLRESULT result
= wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
206 if ( !textOld
.empty() && GetValue() != textOld
)
210 GetSelection(&fromNew
, &toNew
);
212 if ( fromOld
!= fromNew
|| toOld
!= toNew
)
214 SetSelection(fromOld
, toOld
);
221 return wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
224 bool wxComboBox::MSWProcessEditMsg(WXUINT msg
, WXWPARAM wParam
, WXLPARAM lParam
)
229 // for compatibility with wxTextCtrl, generate a special message
230 // when Enter is pressed
231 if ( wParam
== VK_RETURN
)
233 if (SendMessage(GetHwnd(), CB_GETDROPPEDSTATE
, 0, 0))
236 wxCommandEvent
event(wxEVT_TEXT_ENTER
, m_windowId
);
238 const int sel
= GetSelection();
240 event
.SetString(GetValue());
241 InitCommandEventWithItems(event
, sel
);
243 if ( ProcessCommand(event
) )
245 // don't let the event through to the native control
246 // because it doesn't need it and may generate an annoying
247 // beep if it gets it
251 // fall through, WM_CHAR is one of the message we should forward.
254 if ( ShouldForwardFromEditToCombo(msg
) )
256 // For all the messages forward from the edit control the
257 // result is not used.
259 return MSWHandleMessage(&result
, msg
, wParam
, lParam
);
266 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD id
)
274 // remember the last selection, just as wxChoice does
275 m_lastAcceptedSelection
= GetCurrentSelection();
277 wxCommandEvent
event(wxEVT_COMBOBOX_DROPDOWN
, GetId());
278 event
.SetEventObject(this);
279 ProcessCommand(event
);
284 // Do the same thing as in wxChoice but using different event type.
285 if ( m_pendingSelection
!= wxID_NONE
)
287 SendSelectionChangedEvent(wxEVT_COMBOBOX
);
288 m_pendingSelection
= wxID_NONE
;
291 wxCommandEvent
event(wxEVT_COMBOBOX_CLOSEUP
, GetId());
292 event
.SetEventObject(this);
293 ProcessCommand(event
);
298 #ifndef __SMARTPHONE__
299 // we need to reset this to prevent the selection from being undone
300 // by wxChoice, see wxChoice::MSWCommand() and comments there
301 m_lastAcceptedSelection
= wxID_NONE
;
304 // set these variables so that they could be also fixed in
305 // CBN_EDITCHANGE below
306 sel
= GetSelection();
307 value
= GetStringSelection();
309 // this string is going to become the new combobox value soon but
310 // we need it to be done right now, otherwise the event handler
311 // could get a wrong value when it calls our GetValue()
312 ::SetWindowText(GetHwnd(), value
.t_str());
314 SendSelectionChangedEvent(wxEVT_COMBOBOX
);
316 // fall through: for compatibility with wxGTK, also send the text
317 // update event when the selection changes (this also seems more
318 // logical as the text does change)
321 if ( m_allowTextEvents
)
323 wxCommandEvent
event(wxEVT_TEXT
, GetId());
325 // if sel != -1, value was already initialized above
328 value
= wxGetWindowText(GetHwnd());
331 event
.SetString(value
);
332 InitCommandEventWithItems(event
, sel
);
334 ProcessCommand(event
);
339 return wxChoice::MSWCommand(param
, id
);
342 // skip wxChoice version as it would generate its own events for
343 // CBN_SELENDOK and also interfere with our handling of CBN_DROPDOWN
347 bool wxComboBox::MSWShouldPreProcessMessage(WXMSG
*pMsg
)
349 // prevent command accelerators from stealing editing
350 // hotkeys when we have the focus
353 WPARAM vkey
= pMsg
->wParam
;
368 return wxChoice::MSWShouldPreProcessMessage(pMsg
);
371 WXHWND
wxComboBox::GetEditHWNDIfAvailable() const
373 // FIXME-VC6: Only VC6 needs this guard, see WINVER definition in
374 // include/wx/msw/wrapwin.h
375 #if defined(WINVER) && WINVER >= 0x0500
376 WinStruct
<COMBOBOXINFO
> info
;
377 if ( MSWGetComboBoxInfo(&info
) )
378 return info
.hwndItem
;
379 #endif // WINVER >= 0x0500
381 if (HasFlag(wxCB_SIMPLE
))
385 return (WXHWND
) ::ChildWindowFromPoint(GetHwnd(), pt
);
388 // notice that a slightly safer alternative could be to use FindWindowEx()
389 // but it's not available under WinCE so just take the first child for now
390 // to keep one version of the code for all platforms and fix it later if
391 // problems are discovered
393 // we assume that the only child of the combobox is the edit window
394 return (WXHWND
)::GetWindow(GetHwnd(), GW_CHILD
);
397 WXHWND
wxComboBox::GetEditHWND() const
399 // this function should not be called for wxCB_READONLY controls, it is
400 // the callers responsibility to check this
401 wxASSERT_MSG( !HasFlag(wxCB_READONLY
),
402 wxT("read-only combobox doesn't have any edit control") );
404 WXHWND hWndEdit
= GetEditHWNDIfAvailable();
405 wxASSERT_MSG( hWndEdit
, wxT("combobox without edit control?") );
410 wxWindow
*wxComboBox::GetEditableWindow()
412 wxASSERT_MSG( !HasFlag(wxCB_READONLY
),
413 wxT("read-only combobox doesn't have any edit control") );
418 // ----------------------------------------------------------------------------
419 // wxComboBox creation
420 // ----------------------------------------------------------------------------
422 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
423 const wxString
& value
,
426 int n
, const wxString choices
[],
428 const wxValidator
& validator
,
429 const wxString
& name
)
431 // pretend that wxComboBox is hidden while it is positioned and resized and
432 // show it only right before leaving this method because otherwise there is
433 // some noticeable flicker while the control rearranges itself
436 if ( !CreateAndInit(parent
, id
, pos
, size
, n
, choices
, style
,
440 // we shouldn't call SetValue() for an empty string because this would
441 // (correctly) result in an assert with a read only combobox and is useless
442 // for the other ones anyhow
443 if ( !value
.empty() )
446 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
447 // and an edit control inside it and if we want to catch events from this
448 // edit control, we must subclass it as well
449 if ( !(style
& wxCB_READONLY
) )
451 gs_wndprocEdit
= wxSetWindowProc((HWND
)GetEditHWND(), wxComboEditWndProc
);
454 // and finally, show the control
460 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
461 const wxString
& value
,
464 const wxArrayString
& choices
,
466 const wxValidator
& validator
,
467 const wxString
& name
)
469 wxCArrayString
chs(choices
);
470 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
471 chs
.GetStrings(), style
, validator
, name
);
474 WXDWORD
wxComboBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
476 // we never have an external border
477 WXDWORD msStyle
= wxChoice::MSWGetStyle
479 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
482 // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're
483 // created hidden (see Create() above), it is not done for us but we still
484 // want to have this style
485 msStyle
|= WS_TABSTOP
;
487 // remove the style always added by wxChoice
488 msStyle
&= ~CBS_DROPDOWNLIST
;
490 if ( style
& wxCB_READONLY
)
491 msStyle
|= CBS_DROPDOWNLIST
;
493 else if ( style
& wxCB_SIMPLE
)
494 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
497 msStyle
|= CBS_DROPDOWN
;
499 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
500 msStyle
|= CBS_AUTOHSCROLL
;
502 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
507 // ----------------------------------------------------------------------------
508 // wxComboBox text control-like methods
509 // ----------------------------------------------------------------------------
511 wxString
wxComboBox::GetValue() const
513 return HasFlag(wxCB_READONLY
) ? GetStringSelection()
514 : wxTextEntry::GetValue();
517 void wxComboBox::SetValue(const wxString
& value
)
519 if ( HasFlag(wxCB_READONLY
) )
520 SetStringSelection(value
);
522 wxTextEntry::SetValue(value
);
525 void wxComboBox::Clear()
528 if ( !HasFlag(wxCB_READONLY
) )
529 wxTextEntry::Clear();
532 bool wxComboBox::ContainsHWND(WXHWND hWnd
) const
534 return hWnd
== GetEditHWNDIfAvailable();
537 void wxComboBox::GetSelection(long *from
, long *to
) const
539 if ( !HasFlag(wxCB_READONLY
) )
541 wxTextEntry::GetSelection(from
, to
);
543 else // text selection doesn't make sense for read only comboboxes
552 bool wxComboBox::IsEditable() const
554 return !HasFlag(wxCB_READONLY
) && wxTextEntry::IsEditable();
557 // ----------------------------------------------------------------------------
558 // standard event handling
559 // ----------------------------------------------------------------------------
561 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
566 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
571 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
576 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
581 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
586 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
591 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
596 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
598 event
.Enable( CanCut() );
601 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
603 event
.Enable( CanCopy() );
606 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
608 event
.Enable( CanPaste() );
611 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
613 event
.Enable( IsEditable() && CanUndo() );
616 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
618 event
.Enable( IsEditable() && CanRedo() );
621 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
623 event
.Enable(IsEditable() && HasSelection());
626 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
628 event
.Enable(IsEditable() && !wxTextEntry::IsEmpty());
633 void wxComboBox::DoSetToolTip(wxToolTip
*tip
)
635 wxChoice::DoSetToolTip(tip
);
637 if ( tip
&& !HasFlag(wxCB_READONLY
) )
638 tip
->AddOtherWindow(GetEditHWND());
641 #endif // wxUSE_TOOLTIPS
645 bool wxComboBox::SetHint(const wxString
& hintOrig
)
647 wxString
hint(hintOrig
);
648 if ( wxUxThemeEngine::GetIfActive() )
650 // under XP (but not Vista) there is a bug in cue banners
651 // implementation for combobox edit control: the first character is
652 // partially chopped off, so prepend a space to make it fully visible
656 return wxTextEntry::SetHint(hint
);
659 #endif // wxUSE_UXTHEME
661 wxSize
wxComboBox::DoGetSizeFromTextSize(int xlen
, int ylen
) const
663 wxSize
tsize( wxChoice::DoGetSizeFromTextSize(xlen
, ylen
) );
665 if ( !HasFlag(wxCB_READONLY
) )
667 // Add the margins we have previously set
668 wxPoint
marg( GetMargins() );
669 marg
.x
= wxMax(0, marg
.x
);
670 marg
.y
= wxMax(0, marg
.y
);
677 #endif // wxUSE_COMBOBOX