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/dynlib.h"
43 #include "wx/wupdlock.h"
44 #include "wx/msw/private.h"
47 #include "wx/msw/uxtheme.h"
51 #include "wx/tooltip.h"
52 #endif // wxUSE_TOOLTIPS
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
59 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
60 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
61 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
62 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
63 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
64 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
65 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
67 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
68 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
69 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
70 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
71 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
72 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
73 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
76 // ----------------------------------------------------------------------------
77 // function prototypes
78 // ----------------------------------------------------------------------------
80 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
85 // ---------------------------------------------------------------------------
87 // ---------------------------------------------------------------------------
89 // the pointer to standard radio button wnd proc
90 static WNDPROC gs_wndprocEdit
= (WNDPROC
)NULL
;
92 // ============================================================================
94 // ============================================================================
96 // ----------------------------------------------------------------------------
97 // wnd proc for subclassed edit control
98 // ----------------------------------------------------------------------------
100 LRESULT APIENTRY _EXPORT
wxComboEditWndProc(HWND hWnd
,
105 HWND hwndCombo
= ::GetParent(hWnd
);
106 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)hwndCombo
);
110 // forward some messages to the combobox to generate the appropriate
111 // wxEvents from them
121 wxComboBox
*combo
= wxDynamicCast(win
, wxComboBox
);
124 // we can get WM_KILLFOCUS while our parent is already half
125 // destroyed and hence doesn't look like a combobx any
126 // longer, check for it to avoid bogus assert failures
127 if ( !win
->IsBeingDeleted() )
129 wxFAIL_MSG( wxT("should have combo as parent") );
132 else if ( combo
->MSWProcessEditMsg(message
, wParam
, lParam
) )
142 wxCHECK_MSG( win
, 0, wxT("should have a parent") );
144 if ( win
->GetWindowStyle() & wxTE_PROCESS_ENTER
)
146 // need to return a custom dlg code or we'll never get it
147 return DLGC_WANTMESSAGE
;
153 return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
);
156 // ----------------------------------------------------------------------------
157 // wxComboBox callbacks
158 // ----------------------------------------------------------------------------
160 WXLRESULT
wxComboBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
162 // TODO: handle WM_CTLCOLOR messages from our EDIT control to be able to
163 // set its colour correctly (to be the same as our own one)
168 // wxStaticBox can generate this message, when modifying the control's style.
169 // This causes the content of the combobox to be selected, for some reason.
170 case WM_STYLECHANGED
:
172 // combobox selection sometimes spontaneously changes when its
173 // size changes, restore it to the old value if necessary
174 if ( !GetEditHWNDIfAvailable() )
178 GetSelection(&fromOld
, &toOld
);
180 // if an editable combobox has a not empty text not from the
181 // list, it tries to autocomplete it from the list when it is
182 // resized, but we don't want this to happen as it doesn't seem
183 // to make any sense, so we forcefully restore the old text
185 if ( !HasFlag(wxCB_READONLY
) && GetCurrentSelection() == -1 )
186 textOld
= GetValue();
188 // eliminate flickering during following hacks
189 wxWindowUpdateLocker
lock(this);
191 WXLRESULT result
= wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
193 if ( !textOld
.empty() && GetValue() != textOld
)
197 GetSelection(&fromNew
, &toNew
);
199 if ( fromOld
!= fromNew
|| toOld
!= toNew
)
201 SetSelection(fromOld
, toOld
);
208 return wxChoice::MSWWindowProc(nMsg
, wParam
, lParam
);
211 bool wxComboBox::MSWProcessEditMsg(WXUINT msg
, WXWPARAM wParam
, WXLPARAM lParam
)
216 // for compatibility with wxTextCtrl, generate a special message
217 // when Enter is pressed
218 if ( wParam
== VK_RETURN
)
220 if (SendMessage(GetHwnd(), CB_GETDROPPEDSTATE
, 0, 0))
223 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
225 const int sel
= GetSelection();
227 event
.SetString(GetValue());
228 InitCommandEventWithItems(event
, sel
);
230 if ( ProcessCommand(event
) )
232 // don't let the event through to the native control
233 // because it doesn't need it and may generate an annoying
234 // beep if it gets it
250 // For the messages above the result is not used.
252 return MSWHandleMessage(&result
, msg
, wParam
, lParam
);
258 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD id
)
266 // remember the last selection, just as wxChoice does
267 m_lastAcceptedSelection
= GetCurrentSelection();
268 if ( m_lastAcceptedSelection
== -1 )
270 // but unlike with wxChoice we may have no selection but still
271 // have some text and we should avoid erasing it if the drop
272 // down is cancelled (see #8474)
273 m_lastAcceptedSelection
= wxID_NONE
;
276 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_DROPDOWN
, GetId());
277 event
.SetEventObject(this);
278 ProcessCommand(event
);
283 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_CLOSEUP
, GetId());
284 event
.SetEventObject(this);
285 ProcessCommand(event
);
289 #ifndef __SMARTPHONE__
290 // we need to reset this to prevent the selection from being undone
291 // by wxChoice, see wxChoice::MSWCommand() and comments there
292 m_lastAcceptedSelection
= wxID_NONE
;
295 // set these variables so that they could be also fixed in
296 // CBN_EDITCHANGE below
297 sel
= GetSelection();
298 value
= GetStringSelection();
300 // this string is going to become the new combobox value soon but
301 // we need it to be done right now, otherwise the event handler
302 // could get a wrong value when it calls our GetValue()
303 ::SetWindowText(GetHwnd(), value
.t_str());
306 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
308 event
.SetString(value
);
309 InitCommandEventWithItems(event
, sel
);
311 ProcessCommand(event
);
314 // fall through: for compability with wxGTK, also send the text
315 // update event when the selection changes (this also seems more
316 // logical as the text does change)
319 if ( m_allowTextEvents
)
321 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
323 // if sel != -1, value was already initialized above
326 value
= wxGetWindowText(GetHwnd());
329 event
.SetString(value
);
330 InitCommandEventWithItems(event
, sel
);
332 ProcessCommand(event
);
337 return wxChoice::MSWCommand(param
, id
);
340 // skip wxChoice version as it would generate its own events for
341 // CBN_SELENDOK and also interfere with our handling of CBN_DROPDOWN
345 bool wxComboBox::MSWShouldPreProcessMessage(WXMSG
*pMsg
)
347 // prevent command accelerators from stealing editing
348 // hotkeys when we have the focus
351 WPARAM vkey
= pMsg
->wParam
;
366 return wxChoice::MSWShouldPreProcessMessage(pMsg
);
369 WXHWND
wxComboBox::GetEditHWNDIfAvailable() const
371 #if wxUSE_DYNLIB_CLASS
372 #if defined(WINVER) && WINVER >= 0x0500
373 typedef BOOL (WINAPI
*GetComboBoxInfo_t
)(HWND
, COMBOBOXINFO
*);
374 static GetComboBoxInfo_t s_pfnGetComboBoxInfo
= NULL
;
375 static bool s_triedToLoad
= false;
376 if ( !s_triedToLoad
)
378 s_triedToLoad
= true;
379 wxLoadedDLL
dllUser32("user32.dll");
380 wxDL_INIT_FUNC(s_pfn
, GetComboBoxInfo
, dllUser32
);
383 if ( s_pfnGetComboBoxInfo
)
385 WinStruct
<COMBOBOXINFO
> info
;
386 (*s_pfnGetComboBoxInfo
)(GetHwnd(), &info
);
387 return info
.hwndItem
;
389 #endif // WINVER >= 0x0500
390 #endif // wxUSE_DYNLIB_CLASS
392 if (HasFlag(wxCB_SIMPLE
))
396 return (WXHWND
) ::ChildWindowFromPoint(GetHwnd(), pt
);
399 // notice that a slightly safer alternative could be to use FindWindowEx()
400 // but it's not available under WinCE so just take the first child for now
401 // to keep one version of the code for all platforms and fix it later if
402 // problems are discovered
404 // we assume that the only child of the combobox is the edit window
405 return (WXHWND
)::GetWindow(GetHwnd(), GW_CHILD
);
408 WXHWND
wxComboBox::GetEditHWND() const
410 // this function should not be called for wxCB_READONLY controls, it is
411 // the callers responsibility to check this
412 wxASSERT_MSG( !HasFlag(wxCB_READONLY
),
413 wxT("read-only combobox doesn't have any edit control") );
415 WXHWND hWndEdit
= GetEditHWNDIfAvailable();
416 wxASSERT_MSG( hWndEdit
, wxT("combobox without edit control?") );
421 wxWindow
*wxComboBox::GetEditableWindow()
423 wxASSERT_MSG( !HasFlag(wxCB_READONLY
),
424 wxT("read-only combobox doesn't have any edit control") );
429 // ----------------------------------------------------------------------------
430 // wxComboBox creation
431 // ----------------------------------------------------------------------------
433 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
434 const wxString
& value
,
437 int n
, const wxString choices
[],
439 const wxValidator
& validator
,
440 const wxString
& name
)
442 // pretend that wxComboBox is hidden while it is positioned and resized and
443 // show it only right before leaving this method because otherwise there is
444 // some noticeable flicker while the control rearranges itself
447 if ( !CreateAndInit(parent
, id
, pos
, size
, n
, choices
, style
,
451 // we shouldn't call SetValue() for an empty string because this would
452 // (correctly) result in an assert with a read only combobox and is useless
453 // for the other ones anyhow
454 if ( !value
.empty() )
457 // a (not read only) combobox is, in fact, 2 controls: the combobox itself
458 // and an edit control inside it and if we want to catch events from this
459 // edit control, we must subclass it as well
460 if ( !(style
& wxCB_READONLY
) )
462 gs_wndprocEdit
= wxSetWindowProc((HWND
)GetEditHWND(), wxComboEditWndProc
);
465 // and finally, show the control
471 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
472 const wxString
& value
,
475 const wxArrayString
& choices
,
477 const wxValidator
& validator
,
478 const wxString
& name
)
480 wxCArrayString
chs(choices
);
481 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
482 chs
.GetStrings(), style
, validator
, name
);
485 WXDWORD
wxComboBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
487 // we never have an external border
488 WXDWORD msStyle
= wxChoice::MSWGetStyle
490 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
493 // usually WS_TABSTOP is added by wxControl::MSWGetStyle() but as we're
494 // created hidden (see Create() above), it is not done for us but we still
495 // want to have this style
496 msStyle
|= WS_TABSTOP
;
498 // remove the style always added by wxChoice
499 msStyle
&= ~CBS_DROPDOWNLIST
;
501 if ( style
& wxCB_READONLY
)
502 msStyle
|= CBS_DROPDOWNLIST
;
504 else if ( style
& wxCB_SIMPLE
)
505 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
508 msStyle
|= CBS_DROPDOWN
;
510 // there is no reason to not always use CBS_AUTOHSCROLL, so do use it
511 msStyle
|= CBS_AUTOHSCROLL
;
513 // NB: we used to also add CBS_NOINTEGRALHEIGHT here but why?
518 // ----------------------------------------------------------------------------
519 // wxComboBox text control-like methods
520 // ----------------------------------------------------------------------------
522 wxString
wxComboBox::GetValue() const
524 return HasFlag(wxCB_READONLY
) ? GetStringSelection()
525 : wxTextEntry::GetValue();
528 void wxComboBox::SetValue(const wxString
& value
)
530 if ( HasFlag(wxCB_READONLY
) )
531 SetStringSelection(value
);
533 wxTextEntry::SetValue(value
);
536 void wxComboBox::Clear()
539 if ( !HasFlag(wxCB_READONLY
) )
540 wxTextEntry::Clear();
543 void wxComboBox::GetSelection(long *from
, long *to
) const
545 if ( !HasFlag(wxCB_READONLY
) )
547 wxTextEntry::GetSelection(from
, to
);
549 else // text selection doesn't make sense for read only comboboxes
558 bool wxComboBox::IsEditable() const
560 return !HasFlag(wxCB_READONLY
) && wxTextEntry::IsEditable();
563 // ----------------------------------------------------------------------------
564 // standard event handling
565 // ----------------------------------------------------------------------------
567 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
572 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
577 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
582 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
587 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
592 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
597 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
602 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
604 event
.Enable( CanCut() );
607 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
609 event
.Enable( CanCopy() );
612 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
614 event
.Enable( CanPaste() );
617 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
619 event
.Enable( IsEditable() && CanUndo() );
622 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
624 event
.Enable( IsEditable() && CanRedo() );
627 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
629 event
.Enable(IsEditable() && HasSelection());
632 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
634 event
.Enable(IsEditable() && !wxTextEntry::IsEmpty());
639 void wxComboBox::DoSetToolTip(wxToolTip
*tip
)
641 wxChoice::DoSetToolTip(tip
);
643 if ( tip
&& !HasFlag(wxCB_READONLY
) )
644 tip
->AddOtherWindow(GetEditHWND());
647 #endif // wxUSE_TOOLTIPS
651 bool wxComboBox::SetHint(const wxString
& hintOrig
)
653 wxString
hint(hintOrig
);
654 if ( wxUxThemeEngine::GetIfActive() )
656 // under XP (but not Vista) there is a bug in cue banners
657 // implementation for combobox edit control: the first character is
658 // partially chopped off, so prepend a space to make it fully visible
662 return wxTextEntry::SetHint(hint
);
665 #endif // wxUSE_UXTHEME
667 #endif // wxUSE_COMBOBOX