1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/combocmn.cpp
3 // Purpose: wxComboCtrlBase
4 // Author: Jaakko Salli
6 // Created: Apr-30-2006
8 // Copyright: (c) 2005 Jaakko Salli
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
28 #include "wx/combobox.h"
33 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/dialog.h"
37 #include "wx/textctrl.h"
40 #include "wx/tooltip.h"
47 // ----------------------------------------------------------------------------
49 #define DEFAULT_DROPBUTTON_WIDTH 19
51 #define BMP_BUTTON_MARGIN 4
53 #define DEFAULT_POPUP_HEIGHT 400
55 #define DEFAULT_TEXT_INDENT 3
57 #define COMBO_MARGIN 2 // spacing right of wxTextCtrl
60 #if defined(__WXMSW__)
62 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
63 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
64 // native controls work on it like normal.
65 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
66 #define TEXTCTRL_TEXT_CENTERED 0 // 1 if text in textctrl is vertically centered
67 #define FOCUS_RING 0 // No focus ring on wxMSW
69 //#undef wxUSE_POPUPWIN
70 //#define wxUSE_POPUPWIN 0
72 #elif defined(__WXGTK__)
74 // NB: It is not recommended to use wxDialog as popup on wxGTK, because of
75 // this bug: If wxDialog is hidden, its position becomes corrupt
76 // between hide and next show, but without internal coordinates being
77 // reflected (or something like that - atleast commenting out ->Hide()
78 // seemed to eliminate the position change).
80 // NB: Let's not be afraid to use wxGTK's wxPopupTransientWindow as a
81 // 'perfect' popup, as it can succesfully host child controls even in
82 // popups that are shown in modal dialogs.
84 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
85 #define TRANSIENT_POPUPWIN_IS_PERFECT 1 // wxPopupTransientWindow works, its child can have focus, and common
86 // native controls work on it like normal.
87 #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
88 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
89 #define FOCUS_RING 0 // No focus ring on wxGTK
91 #elif defined(__WXMAC__)
93 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
94 #define TRANSIENT_POPUPWIN_IS_PERFECT 1 // wxPopupTransientWindow works, its child can have focus, and common
95 // native controls work on it like normal.
96 #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
97 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
98 #define FOCUS_RING 3 // Reserve room for the textctrl's focus ring to display
100 #undef DEFAULT_DROPBUTTON_WIDTH
101 #define DEFAULT_DROPBUTTON_WIDTH 22
103 #define COMBO_MARGIN FOCUS_RING
107 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
108 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
109 // native controls work on it like normal.
110 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
111 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
117 // Popupwin is really only supported on wxMSW (not WINCE) and wxGTK, regardless
118 // what the wxUSE_POPUPWIN says.
119 // FIXME: Why isn't wxUSE_POPUPWIN reliable any longer? (it was in wxW2.6.2)
120 #if (!defined(__WXMSW__) && !defined(__WXGTK__) && !defined(__WXMAC__)) || defined(__WXWINCE__)
121 #undef wxUSE_POPUPWIN
122 #define wxUSE_POPUPWIN 0
127 #include "wx/popupwin.h"
129 #undef USE_TRANSIENT_POPUP
130 #define USE_TRANSIENT_POPUP 0
134 // Define different types of popup windows
138 POPUPWIN_WXPOPUPTRANSIENTWINDOW
= 1,
139 POPUPWIN_WXPOPUPWINDOW
= 2,
140 POPUPWIN_WXDIALOG
= 3
144 #if USE_TRANSIENT_POPUP
145 // wxPopupTransientWindow is implemented
147 #define wxComboPopupWindowBase wxPopupTransientWindow
148 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPTRANSIENTWINDOW
149 #define USES_WXPOPUPTRANSIENTWINDOW 1
151 #if TRANSIENT_POPUPWIN_IS_PERFECT
153 #elif POPUPWIN_IS_PERFECT
154 #define wxComboPopupWindowBase2 wxPopupWindow
155 #define SECONDARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
156 #define USES_WXPOPUPWINDOW 1
158 #define wxComboPopupWindowBase2 wxDialog
159 #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG
160 #define USES_WXDIALOG 1
164 // wxPopupWindow (but not wxPopupTransientWindow) is properly implemented
166 #define wxComboPopupWindowBase wxPopupWindow
167 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
168 #define USES_WXPOPUPWINDOW 1
170 #if !POPUPWIN_IS_PERFECT
171 #define wxComboPopupWindowBase2 wxDialog
172 #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG
173 #define USES_WXDIALOG 1
177 // wxPopupWindow is not implemented
179 #define wxComboPopupWindowBase wxDialog
180 #define PRIMARY_POPUP_TYPE POPUPWIN_WXDIALOG
181 #define USES_WXDIALOG 1
186 #ifndef USES_WXPOPUPTRANSIENTWINDOW
187 #define USES_WXPOPUPTRANSIENTWINDOW 0
190 #ifndef USES_WXPOPUPWINDOW
191 #define USES_WXPOPUPWINDOW 0
194 #ifndef USES_WXDIALOG
195 #define USES_WXDIALOG 0
199 #if USES_WXPOPUPWINDOW
200 #define INSTALL_TOPLEV_HANDLER 1
202 #define INSTALL_TOPLEV_HANDLER 0
208 // * wxComboPopupWindow for external use (ie. replace old wxUniv wxPopupComboWindow)
212 // ----------------------------------------------------------------------------
213 // wxComboFrameEventHandler takes care of hiding the popup when events happen
214 // in its top level parent.
215 // ----------------------------------------------------------------------------
217 #if INSTALL_TOPLEV_HANDLER
220 // This will no longer be necessary after wxTransientPopupWindow
221 // works well on all platforms.
224 class wxComboFrameEventHandler
: public wxEvtHandler
227 wxComboFrameEventHandler( wxComboCtrlBase
* pCb
);
228 virtual ~wxComboFrameEventHandler();
232 void OnIdle( wxIdleEvent
& event
);
233 void OnMouseEvent( wxMouseEvent
& event
);
234 void OnActivate( wxActivateEvent
& event
);
235 void OnResize( wxSizeEvent
& event
);
236 void OnMove( wxMoveEvent
& event
);
237 void OnMenuEvent( wxMenuEvent
& event
);
238 void OnClose( wxCloseEvent
& event
);
241 wxWindow
* m_focusStart
;
242 wxComboCtrlBase
* m_combo
;
245 DECLARE_EVENT_TABLE()
248 BEGIN_EVENT_TABLE(wxComboFrameEventHandler
, wxEvtHandler
)
249 EVT_IDLE(wxComboFrameEventHandler::OnIdle
)
250 EVT_LEFT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
251 EVT_RIGHT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
252 EVT_SIZE(wxComboFrameEventHandler::OnResize
)
253 EVT_MOVE(wxComboFrameEventHandler::OnMove
)
254 EVT_MENU_HIGHLIGHT(wxID_ANY
,wxComboFrameEventHandler::OnMenuEvent
)
255 EVT_MENU_OPEN(wxComboFrameEventHandler::OnMenuEvent
)
256 EVT_ACTIVATE(wxComboFrameEventHandler::OnActivate
)
257 EVT_CLOSE(wxComboFrameEventHandler::OnClose
)
260 wxComboFrameEventHandler::wxComboFrameEventHandler( wxComboCtrlBase
* combo
)
266 wxComboFrameEventHandler::~wxComboFrameEventHandler()
270 void wxComboFrameEventHandler::OnPopup()
272 m_focusStart
= ::wxWindow::FindFocus();
275 void wxComboFrameEventHandler::OnIdle( wxIdleEvent
& event
)
277 wxWindow
* winFocused
= ::wxWindow::FindFocus();
279 wxWindow
* popup
= m_combo
->GetPopupControl()->GetControl();
280 wxWindow
* winpopup
= m_combo
->GetPopupWindow();
283 winFocused
!= m_focusStart
&&
284 winFocused
!= popup
&&
285 winFocused
->GetParent() != popup
&&
286 winFocused
!= winpopup
&&
287 winFocused
->GetParent() != winpopup
&&
288 winFocused
!= m_combo
&&
289 winFocused
!= m_combo
->GetButton() // GTK (atleast) requires this
292 m_combo
->HidePopup();
298 void wxComboFrameEventHandler::OnMenuEvent( wxMenuEvent
& event
)
300 m_combo
->HidePopup();
304 void wxComboFrameEventHandler::OnMouseEvent( wxMouseEvent
& event
)
306 m_combo
->HidePopup();
310 void wxComboFrameEventHandler::OnClose( wxCloseEvent
& event
)
312 m_combo
->HidePopup();
316 void wxComboFrameEventHandler::OnActivate( wxActivateEvent
& event
)
318 m_combo
->HidePopup();
322 void wxComboFrameEventHandler::OnResize( wxSizeEvent
& event
)
324 m_combo
->HidePopup();
328 void wxComboFrameEventHandler::OnMove( wxMoveEvent
& event
)
330 m_combo
->HidePopup();
334 #endif // INSTALL_TOPLEV_HANDLER
336 // ----------------------------------------------------------------------------
337 // wxComboPopupWindow is, in essence, wxPopupWindow customized for
339 // ----------------------------------------------------------------------------
341 class wxComboPopupWindow
: public wxComboPopupWindowBase
345 wxComboPopupWindow( wxComboCtrlBase
*parent
,
347 #if USES_WXPOPUPWINDOW || USES_WXPOPUPTRANSIENTWINDOW
348 : wxComboPopupWindowBase(parent
,style
)
350 : wxComboPopupWindowBase(parent
,
361 #if USES_WXPOPUPTRANSIENTWINDOW
362 virtual bool Show( bool show
);
363 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
365 virtual void OnDismiss();
373 #if USES_WXPOPUPTRANSIENTWINDOW
374 bool wxComboPopupWindow::Show( bool show
)
376 // Guard against recursion
378 return wxComboPopupWindowBase::Show(show
);
382 wxASSERT( IsKindOf(CLASSINFO(wxPopupTransientWindow
)) );
384 wxPopupTransientWindow
* ptw
= (wxPopupTransientWindow
*) this;
386 if ( show
!= ptw
->IsShown() )
389 // We used to do wxPopupTransientWindow::Popup here,
390 // but this would hide normal Show, which we are
391 // also going to need.
402 bool wxComboPopupWindow::ProcessLeftDown(wxMouseEvent
& event
)
404 return wxPopupTransientWindow::ProcessLeftDown(event
);
407 // First thing that happens when a transient popup closes is that this method gets called.
408 void wxComboPopupWindow::OnDismiss()
410 wxComboCtrlBase
* combo
= (wxComboCtrlBase
*) GetParent();
411 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxComboCtrlBase
)),
412 wxT("parent might not be wxComboCtrl, but check IMPLEMENT_DYNAMIC_CLASS(2) macro for correctness") );
414 combo
->OnPopupDismiss();
416 #endif // USES_WXPOPUPTRANSIENTWINDOW
419 // ----------------------------------------------------------------------------
420 // wxComboPopupWindowEvtHandler does bulk of the custom event handling
421 // of a popup window. It is separate so we can have different types
423 // ----------------------------------------------------------------------------
425 class wxComboPopupWindowEvtHandler
: public wxEvtHandler
429 wxComboPopupWindowEvtHandler( wxComboCtrlBase
*parent
)
434 void OnSizeEvent( wxSizeEvent
& event
);
435 void OnKeyEvent(wxKeyEvent
& event
);
437 void OnActivate( wxActivateEvent
& event
);
441 wxComboCtrlBase
* m_combo
;
443 DECLARE_EVENT_TABLE()
447 BEGIN_EVENT_TABLE(wxComboPopupWindowEvtHandler
, wxEvtHandler
)
448 EVT_KEY_DOWN(wxComboPopupWindowEvtHandler::OnKeyEvent
)
449 EVT_KEY_UP(wxComboPopupWindowEvtHandler::OnKeyEvent
)
451 EVT_ACTIVATE(wxComboPopupWindowEvtHandler::OnActivate
)
453 EVT_SIZE(wxComboPopupWindowEvtHandler::OnSizeEvent
)
457 void wxComboPopupWindowEvtHandler::OnSizeEvent( wxSizeEvent
& WXUNUSED(event
) )
459 // Block the event so that the popup control does not get auto-resized.
462 void wxComboPopupWindowEvtHandler::OnKeyEvent( wxKeyEvent
& event
)
464 // Relay keyboard event to the main child controls
465 wxWindowList children
= m_combo
->GetPopupWindow()->GetChildren();
466 wxWindowList::iterator node
= children
.begin();
467 wxWindow
* child
= (wxWindow
*)*node
;
468 child
->GetEventHandler()->AddPendingEvent(event
);
472 void wxComboPopupWindowEvtHandler::OnActivate( wxActivateEvent
& event
)
474 if ( !event
.GetActive() )
476 // Tell combo control that we are dismissed.
477 m_combo
->HidePopup();
485 // ----------------------------------------------------------------------------
488 // ----------------------------------------------------------------------------
490 wxComboPopup::~wxComboPopup()
494 void wxComboPopup::OnPopup()
498 void wxComboPopup::OnDismiss()
502 wxComboCtrl
* wxComboPopup::GetComboCtrl() const
504 return wxStaticCast(m_combo
, wxComboCtrl
);
507 wxSize
wxComboPopup::GetAdjustedSize( int minWidth
,
509 int WXUNUSED(maxHeight
) )
511 return wxSize(minWidth
,prefHeight
);
514 void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase
* combo
,
515 wxDC
& dc
, const wxRect
& rect
)
517 if ( combo
->GetWindowStyle() & wxCB_READONLY
) // ie. no textctrl
519 combo
->PrepareBackground(dc
,rect
,0);
521 dc
.DrawText( combo
->GetValue(),
522 rect
.x
+ combo
->GetTextIndent(),
523 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
527 void wxComboPopup::PaintComboControl( wxDC
& dc
, const wxRect
& rect
)
529 DefaultPaintComboControl(m_combo
,dc
,rect
);
532 void wxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
537 void wxComboPopup::OnComboDoubleClick()
541 void wxComboPopup::SetStringValue( const wxString
& WXUNUSED(value
) )
545 bool wxComboPopup::LazyCreate()
550 void wxComboPopup::Dismiss()
552 m_combo
->HidePopup();
555 // ----------------------------------------------------------------------------
557 // ----------------------------------------------------------------------------
560 // This is pushed to the event handler queue of the child textctrl.
562 class wxComboBoxExtraInputHandler
: public wxEvtHandler
566 wxComboBoxExtraInputHandler( wxComboCtrlBase
* combo
)
571 virtual ~wxComboBoxExtraInputHandler() { }
572 void OnKey(wxKeyEvent
& event
);
573 void OnFocus(wxFocusEvent
& event
);
576 wxComboCtrlBase
* m_combo
;
579 DECLARE_EVENT_TABLE()
583 BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler
, wxEvtHandler
)
584 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey
)
585 EVT_KEY_UP(wxComboBoxExtraInputHandler::OnKey
)
586 EVT_CHAR(wxComboBoxExtraInputHandler::OnKey
)
587 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
588 EVT_KILL_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
592 void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent
& event
)
594 // Let the wxComboCtrl event handler have a go first.
595 wxComboCtrlBase
* combo
= m_combo
;
597 wxKeyEvent
redirectedEvent(event
);
598 redirectedEvent
.SetId(combo
->GetId());
599 redirectedEvent
.SetEventObject(combo
);
601 if ( !combo
->GetEventHandler()->ProcessEvent(redirectedEvent
) )
603 // Don't let TAB through to the text ctrl - looks ugly
604 if ( event
.GetKeyCode() != WXK_TAB
)
609 void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent
& event
)
611 // FIXME: This code does run when control is clicked,
612 // yet on Windows it doesn't select all the text.
613 if ( event
.GetEventType() == wxEVT_SET_FOCUS
&&
614 !(m_combo
->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT
) )
616 if ( m_combo
->GetTextCtrl() )
617 m_combo
->GetTextCtrl()->SelectAll();
619 m_combo
->SetSelection(-1,-1);
622 // Send focus indication to parent.
623 // NB: This is needed for cases where the textctrl gets focus
624 // instead of its parent. While this may trigger multiple
625 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
626 // from combo's focus event handler), they should be quite
628 wxFocusEvent
evt2(event
.GetEventType(),m_combo
->GetId());
629 evt2
.SetEventObject(m_combo
);
630 m_combo
->GetEventHandler()->ProcessEvent(evt2
);
637 // This is pushed to the event handler queue of the control in popup.
640 class wxComboPopupExtraEventHandler
: public wxEvtHandler
644 wxComboPopupExtraEventHandler( wxComboCtrlBase
* combo
)
648 m_beenInside
= false;
650 virtual ~wxComboPopupExtraEventHandler() { }
652 void OnMouseEvent( wxMouseEvent
& event
);
654 // Called from wxComboCtrlBase::OnPopupDismiss
655 void OnPopupDismiss()
657 m_beenInside
= false;
661 wxComboCtrlBase
* m_combo
;
666 DECLARE_EVENT_TABLE()
670 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler
, wxEvtHandler
)
671 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent
)
675 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent
& event
)
677 wxPoint pt
= event
.GetPosition();
678 wxSize sz
= m_combo
->GetPopupControl()->GetControl()->GetClientSize();
679 int evtType
= event
.GetEventType();
680 bool isInside
= pt
.x
>= 0 && pt
.y
>= 0 && pt
.x
< sz
.x
&& pt
.y
< sz
.y
;
681 bool relayToButton
= false;
685 if ( evtType
== wxEVT_MOTION
||
686 evtType
== wxEVT_LEFT_DOWN
||
687 evtType
== wxEVT_RIGHT_DOWN
)
689 // Block motion and click events outside the popup
690 if ( !isInside
|| !m_combo
->IsPopupShown() )
695 else if ( evtType
== wxEVT_LEFT_UP
)
697 if ( !m_combo
->IsPopupShown() )
700 relayToButton
= true;
702 else if ( !m_beenInside
)
710 relayToButton
= true;
718 // Some mouse events to popup that happen outside it, before cursor
719 // has been inside the popup, need to be ignored by it but relayed to
722 wxWindow
* eventSink
= m_combo
;
723 wxWindow
* btn
= m_combo
->GetButton();
727 eventSink
->GetEventHandler()->ProcessEvent(event
);
731 // ----------------------------------------------------------------------------
732 // wxComboCtrlTextCtrl
733 // ----------------------------------------------------------------------------
735 class wxComboCtrlTextCtrl
: public wxTextCtrl
738 wxComboCtrlTextCtrl() : wxTextCtrl() { }
739 virtual ~wxComboCtrlTextCtrl() { }
741 virtual wxWindow
*GetMainWindowOfCompositeControl()
743 wxComboCtrl
* combo
= (wxComboCtrl
*) GetParent();
745 // Returning this instead of just 'parent' lets FindFocus work
746 // correctly even when parent control is a child of a composite
747 // generic control (as is case with wxGenericDatePickerCtrl).
748 return combo
->GetMainWindowOfCompositeControl();
752 // ----------------------------------------------------------------------------
754 // ----------------------------------------------------------------------------
757 BEGIN_EVENT_TABLE(wxComboCtrlBase
, wxControl
)
758 EVT_TEXT(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
759 EVT_SIZE(wxComboCtrlBase::OnSizeEvent
)
760 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent
)
761 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent
)
762 EVT_IDLE(wxComboCtrlBase::OnIdleEvent
)
763 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
764 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent
)
765 EVT_TEXT_ENTER(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
766 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged
)
770 IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase
, wxControl
)
772 void wxComboCtrlBase::Init()
776 m_popupWinState
= Hidden
;
779 m_popupInterface
= NULL
;
781 m_popupExtraHandler
= NULL
;
782 m_textEvtHandler
= NULL
;
784 #if INSTALL_TOPLEV_HANDLER
785 m_toplevEvtHandler
= NULL
;
788 m_mainCtrlWnd
= this;
791 m_widthMinPopup
= -1;
793 m_widthCustomPaint
= 0;
794 m_widthCustomBorder
= 0;
798 m_blankButtonBg
= false;
800 m_popupWinType
= POPUPWIN_NONE
;
801 m_btnWid
= m_btnHei
= -1;
809 m_timeCanAcceptClick
= 0;
811 m_resetFocus
= false;
814 bool wxComboCtrlBase::Create(wxWindow
*parent
,
816 const wxString
& value
,
820 const wxValidator
& validator
,
821 const wxString
& name
)
823 if ( !wxControl::Create(parent
,
827 style
| wxWANTS_CHARS
,
832 m_valueString
= value
;
836 m_absIndent
= GetNativeTextIndent();
838 m_iFlags
|= wxCC_IFLAG_CREATED
;
840 // If x and y indicate valid size, wxSizeEvent won't be
841 // emitted automatically, so we need to add artifical one.
842 if ( size
.x
> 0 && size
.y
> 0 )
844 wxSizeEvent
evt(size
,GetId());
845 GetEventHandler()->AddPendingEvent(evt
);
851 void wxComboCtrlBase::InstallInputHandlers()
855 m_textEvtHandler
= new wxComboBoxExtraInputHandler(this);
856 m_text
->PushEventHandler(m_textEvtHandler
);
861 wxComboCtrlBase::CreateTextCtrl(int style
, const wxValidator
& validator
)
863 if ( !(m_windowStyle
& wxCB_READONLY
) )
868 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
869 // not used by the wxPropertyGrid and therefore the tab is processed by
870 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
871 // navigation event is then sent to the wrong window.
872 style
|= wxTE_PROCESS_TAB
;
874 if ( HasFlag(wxTE_PROCESS_ENTER
) )
875 style
|= wxTE_PROCESS_ENTER
;
877 // Ignore EVT_TEXT generated by the constructor (but only
878 // if the event redirector already exists)
879 // NB: This must be " = 1" instead of "++";
880 if ( m_textEvtHandler
)
885 m_text
= new wxComboCtrlTextCtrl();
886 m_text
->Create(this, wxID_ANY
, m_valueString
,
887 wxDefaultPosition
, wxSize(10,-1),
892 void wxComboCtrlBase::OnThemeChange()
894 // Leave the default bg on the Mac so the area used by the focus ring will
895 // be the correct colour and themed brush. Instead we'll use
896 // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed.
898 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
902 wxComboCtrlBase::~wxComboCtrlBase()
907 #if INSTALL_TOPLEV_HANDLER
908 delete ((wxComboFrameEventHandler
*)m_toplevEvtHandler
);
909 m_toplevEvtHandler
= NULL
;
915 m_text
->RemoveEventHandler(m_textEvtHandler
);
917 delete m_textEvtHandler
;
921 // ----------------------------------------------------------------------------
923 // ----------------------------------------------------------------------------
925 // Recalculates button and textctrl areas
926 void wxComboCtrlBase::CalculateAreas( int btnWidth
)
928 wxSize sz
= GetClientSize();
929 int customBorder
= m_widthCustomBorder
;
930 int btnBorder
; // border for button only
932 // check if button should really be outside the border: we'll do it it if
933 // its platform default or bitmap+pushbutton background is used, but not if
934 // there is vertical size adjustment or horizontal spacing.
935 if ( ( (m_iFlags
& wxCC_BUTTON_OUTSIDE_BORDER
) ||
936 (m_bmpNormal
.Ok() && m_blankButtonBg
) ) &&
937 m_btnSpacingX
== 0 &&
940 m_iFlags
|= wxCC_IFLAG_BUTTON_OUTSIDE
;
943 else if ( (m_iFlags
& wxCC_BUTTON_COVERS_BORDER
) &&
944 m_btnSpacingX
== 0 && !m_bmpNormal
.Ok() )
946 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
951 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
952 btnBorder
= customBorder
;
955 // Defaul indentation
956 if ( m_absIndent
< 0 )
957 m_absIndent
= GetNativeTextIndent();
959 int butWidth
= btnWidth
;
962 butWidth
= m_btnWidDefault
;
964 m_btnWidDefault
= butWidth
;
969 int butHeight
= sz
.y
- btnBorder
*2;
971 // Adjust button width
976 // Adjust button width to match aspect ratio
977 // (but only if control is smaller than best size).
978 int bestHeight
= GetBestSize().y
;
979 int height
= GetSize().y
;
981 if ( height
< bestHeight
)
983 // Make very small buttons square, as it makes
984 // them accommodate arrow image better and still
987 butWidth
= (height
*butWidth
)/bestHeight
;
989 butWidth
= butHeight
;
993 // Adjust button height
995 butHeight
= m_btnHei
;
997 // Use size of normal bitmap if...
1000 // button width is set to default and blank button bg is not drawn
1001 if ( m_bmpNormal
.Ok() )
1003 int bmpReqWidth
= m_bmpNormal
.GetWidth();
1004 int bmpReqHeight
= m_bmpNormal
.GetHeight();
1006 // If drawing blank button background, we need to add some margin.
1007 if ( m_blankButtonBg
)
1009 bmpReqWidth
+= BMP_BUTTON_MARGIN
*2;
1010 bmpReqHeight
+= BMP_BUTTON_MARGIN
*2;
1013 if ( butWidth
< bmpReqWidth
|| ( m_btnWid
== 0 && !m_blankButtonBg
) )
1014 butWidth
= bmpReqWidth
;
1015 if ( butHeight
< bmpReqHeight
|| ( m_btnHei
== 0 && !m_blankButtonBg
) )
1016 butHeight
= bmpReqHeight
;
1018 // Need to fix height?
1019 if ( (sz
.y
-(customBorder
*2)) < butHeight
&& btnWidth
== 0 )
1021 int newY
= butHeight
+(customBorder
*2);
1022 SetClientSize(wxDefaultCoord
,newY
);
1023 if ( m_bmpNormal
.Ok() || m_btnArea
.width
!= butWidth
|| m_btnArea
.height
!= butHeight
)
1024 m_iFlags
|= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1026 m_iFlags
&= ~wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1032 int butAreaWid
= butWidth
+ (m_btnSpacingX
*2);
1034 m_btnSize
.x
= butWidth
;
1035 m_btnSize
.y
= butHeight
;
1037 m_btnArea
.x
= ( m_btnSide
==wxRIGHT
? sz
.x
- butAreaWid
- btnBorder
: btnBorder
);
1038 m_btnArea
.y
= btnBorder
+ FOCUS_RING
;
1039 m_btnArea
.width
= butAreaWid
;
1040 m_btnArea
.height
= sz
.y
- ((btnBorder
+FOCUS_RING
)*2);
1042 m_tcArea
.x
= ( m_btnSide
==wxRIGHT
? 0 : butAreaWid
) + customBorder
+ FOCUS_RING
;
1043 m_tcArea
.y
= customBorder
+ FOCUS_RING
;
1044 m_tcArea
.width
= sz
.x
- butAreaWid
- (customBorder
*2) - (FOCUS_RING
*2);
1045 m_tcArea
.height
= sz
.y
- ((customBorder
+FOCUS_RING
)*2);
1050 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
1051 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1056 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
)
1061 #if !TEXTCTRL_TEXT_CENTERED
1063 wxSize sz
= GetClientSize();
1065 int customBorder
= m_widthCustomBorder
;
1066 if ( (m_text
->GetWindowStyleFlag() & wxBORDER_MASK
) == wxNO_BORDER
)
1069 int tcSizeY
= m_text
->GetBestSize().y
;
1070 int diff
= sz
.y
- tcSizeY
;
1071 int y
= textCtrlYAdjust
+ (diff
/2);
1073 if ( y
< customBorder
)
1076 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
+ m_absIndent
+ textCtrlXAdjust
,
1078 m_tcArea
.width
- COMBO_MARGIN
-
1079 (textCtrlXAdjust
+ m_widthCustomPaint
+ m_absIndent
),
1082 // Make sure textctrl doesn't exceed the bottom custom border
1083 wxSize tsz
= m_text
->GetSize();
1084 diff
= (y
+ tsz
.y
) - (sz
.y
- customBorder
);
1087 tsz
.y
= tsz
.y
- diff
- 1;
1088 m_text
->SetSize(tsz
);
1092 #else // TEXTCTRL_TEXT_CENTERED
1093 wxUnusedVar(textCtrlXAdjust
);
1094 wxUnusedVar(textCtrlYAdjust
);
1095 #endif // !TEXTCTRL_TEXT_CENTERED/TEXTCTRL_TEXT_CENTERED
1097 // If it has border, have textctrl will the entire text field.
1098 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
,
1100 m_tcArea
.width
- m_widthCustomPaint
,
1105 wxSize
wxComboCtrlBase::DoGetBestSize() const
1107 wxSize
sizeText(150,0);
1110 sizeText
= m_text
->GetBestSize();
1112 // TODO: Better method to calculate close-to-native control height.
1116 fhei
= (m_font
.GetPointSize()*2) + 5;
1117 else if ( wxNORMAL_FONT
->Ok() )
1118 fhei
= (wxNORMAL_FONT
->GetPointSize()*2) + 5;
1120 fhei
= sizeText
.y
+ 4;
1122 // Need to force height to accomodate bitmap?
1123 int btnSizeY
= m_btnSize
.y
;
1124 if ( m_bmpNormal
.Ok() && fhei
< btnSizeY
)
1127 // Control height doesn't depend on border
1130 int border = m_windowStyle & wxBORDER_MASK;
1131 if ( border == wxSIMPLE_BORDER )
1133 else if ( border == wxNO_BORDER )
1134 fhei += (m_widthCustomBorder*2);
1140 // Final adjustments
1146 // these are the numbers from the HIG:
1147 switch ( m_windowVariant
)
1149 case wxWINDOW_VARIANT_NORMAL
:
1153 case wxWINDOW_VARIANT_SMALL
:
1156 case wxWINDOW_VARIANT_MINI
:
1162 fhei
+= 2 * FOCUS_RING
;
1163 int width
= sizeText
.x
+ FOCUS_RING
+ COMBO_MARGIN
+ DEFAULT_DROPBUTTON_WIDTH
;
1165 wxSize
ret(width
, fhei
);
1170 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent
& event
)
1175 // defined by actual wxComboCtrls
1181 // ----------------------------------------------------------------------------
1182 // standard operations
1183 // ----------------------------------------------------------------------------
1185 bool wxComboCtrlBase::Enable(bool enable
)
1187 if ( !wxControl::Enable(enable
) )
1191 m_btn
->Enable(enable
);
1193 m_text
->Enable(enable
);
1200 bool wxComboCtrlBase::Show(bool show
)
1202 if ( !wxControl::Show(show
) )
1214 bool wxComboCtrlBase::SetFont ( const wxFont
& font
)
1216 if ( !wxControl::SetFont(font
) )
1220 m_text
->SetFont(font
);
1226 void wxComboCtrlBase::DoSetToolTip(wxToolTip
*tooltip
)
1228 wxControl::DoSetToolTip(tooltip
);
1230 // Set tool tip for button and text box
1233 const wxString
&tip
= tooltip
->GetTip();
1234 if ( m_text
) m_text
->SetToolTip(tip
);
1235 if ( m_btn
) m_btn
->SetToolTip(tip
);
1239 if ( m_text
) m_text
->SetToolTip( NULL
);
1240 if ( m_btn
) m_btn
->SetToolTip( NULL
);
1243 #endif // wxUSE_TOOLTIPS
1245 #if wxUSE_VALIDATORS
1246 void wxComboCtrlBase::SetValidator(const wxValidator
& validator
)
1248 wxTextCtrl
* textCtrl
= GetTextCtrl();
1251 textCtrl
->SetValidator( validator
);
1253 wxControl::SetValidator( validator
);
1256 wxValidator
* wxComboCtrlBase::GetValidator()
1258 wxTextCtrl
* textCtrl
= GetTextCtrl();
1260 return textCtrl
? textCtrl
->GetValidator() : wxControl::GetValidator();
1262 #endif // wxUSE_VALIDATORS
1264 // ----------------------------------------------------------------------------
1266 // ----------------------------------------------------------------------------
1268 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1269 // prepare combo box background on area in a way typical on platform
1270 void wxComboCtrlBase::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
1272 wxSize sz
= GetClientSize();
1274 bool doDrawFocusRect
; // also selected
1276 // For smaller size control (and for disabled background) use less spacing
1280 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1283 isEnabled
= IsEnabled();
1284 doDrawFocusRect
= ShouldDrawFocus() && !(m_iFlags
& wxCC_FULL_BUTTON
);
1286 // Windows-style: for smaller size control (and for disabled background) use less spacing
1287 focusSpacingX
= isEnabled
? 2 : 1;
1288 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
1292 // Drawing a list item
1293 isEnabled
= true; // they are never disabled
1294 doDrawFocusRect
= (flags
& wxCONTROL_SELECTED
) != 0;
1300 // Set the background sub-rectangle for selection, disabled etc
1301 wxRect
selRect(rect
);
1302 selRect
.y
+= focusSpacingY
;
1303 selRect
.height
-= (focusSpacingY
*2);
1307 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1308 wcp
+= m_widthCustomPaint
;
1310 selRect
.x
+= wcp
+ focusSpacingX
;
1311 selRect
.width
-= wcp
+ (focusSpacingX
*2);
1314 bool doDrawSelRect
= true;
1318 // If popup is hidden and this control is focused,
1319 // then draw the focus-indicator (selbgcolor background etc.).
1320 if ( doDrawFocusRect
)
1322 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1323 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1327 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
1328 #ifndef __WXMAC__ // see note in OnThemeChange
1329 doDrawSelRect
= false;
1330 bgCol
= GetBackgroundColour();
1332 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1338 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
1339 #ifndef __WXMAC__ // see note in OnThemeChange
1340 bgCol
= GetBackgroundColour();
1342 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1346 dc
.SetBrush( bgCol
);
1347 if ( doDrawSelRect
)
1350 dc
.DrawRectangle( selRect
);
1353 // Don't clip exactly to the selection rectangle so we can draw
1354 // to the non-selected area in front of it.
1355 wxRect
clipRect(rect
.x
,rect
.y
,
1356 (selRect
.x
+selRect
.width
)-rect
.x
,rect
.height
);
1357 dc
.SetClippingRegion(clipRect
);
1360 // Save the library size a bit for platforms that re-implement this.
1361 void wxComboCtrlBase::PrepareBackground( wxDC
&, const wxRect
&, int ) const
1366 void wxComboCtrlBase::DrawButton( wxDC
& dc
, const wxRect
& rect
, int flags
)
1368 int drawState
= m_btnState
;
1370 if ( (m_iFlags
& wxCC_BUTTON_STAYS_DOWN
) &&
1371 GetPopupWindowState() >= Animating
)
1372 drawState
|= wxCONTROL_PRESSED
;
1374 wxRect
drawRect(rect
.x
+m_btnSpacingX
,
1375 rect
.y
+((rect
.height
-m_btnSize
.y
)/2),
1379 // Make sure area is not larger than the control
1380 if ( drawRect
.y
< rect
.y
)
1381 drawRect
.y
= rect
.y
;
1382 if ( drawRect
.height
> rect
.height
)
1383 drawRect
.height
= rect
.height
;
1385 bool enabled
= IsEnabled();
1388 drawState
|= wxCONTROL_DISABLED
;
1390 if ( !m_bmpNormal
.Ok() )
1392 if ( flags
& Button_BitmapOnly
)
1395 // Need to clear button background even if m_btn is present
1396 if ( flags
& Button_PaintBackground
)
1400 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
1401 bgCol
= GetParent()->GetBackgroundColour();
1403 bgCol
= GetBackgroundColour();
1407 dc
.DrawRectangle(rect
);
1410 // Draw standard button
1411 wxRendererNative::Get().DrawComboBoxDropButton(this,
1423 pBmp
= &m_bmpDisabled
;
1424 else if ( m_btnState
& wxCONTROL_PRESSED
)
1425 pBmp
= &m_bmpPressed
;
1426 else if ( m_btnState
& wxCONTROL_CURRENT
)
1429 pBmp
= &m_bmpNormal
;
1431 if ( m_blankButtonBg
)
1433 // If using blank button background, we need to clear its background
1434 // with button face colour instead of colour for rest of the control.
1435 if ( flags
& Button_PaintBackground
)
1437 wxColour bgCol
= GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1438 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1441 dc
.DrawRectangle(rect
);
1444 if ( !(flags
& Button_BitmapOnly
) )
1446 wxRendererNative::Get().DrawPushButton(this,
1455 // Need to clear button background even if m_btn is present
1456 // (assume non-button background was cleared just before this call so brushes are good)
1457 if ( flags
& Button_PaintBackground
)
1458 dc
.DrawRectangle(rect
);
1461 // Draw bitmap centered in drawRect
1462 dc
.DrawBitmap(*pBmp
,
1463 drawRect
.x
+ (drawRect
.width
-pBmp
->GetWidth())/2,
1464 drawRect
.y
+ (drawRect
.height
-pBmp
->GetHeight())/2,
1469 void wxComboCtrlBase::RecalcAndRefresh()
1473 wxSizeEvent
evt(GetSize(),GetId());
1474 GetEventHandler()->ProcessEvent(evt
);
1479 // ----------------------------------------------------------------------------
1480 // miscellaneous event handlers
1481 // ----------------------------------------------------------------------------
1483 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent
& event
)
1485 if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1487 if ( m_ignoreEvtText
> 0 )
1494 // Change event id, object and string before relaying it forward
1495 event
.SetId(GetId());
1496 wxString s
= event
.GetString();
1497 event
.SetEventObject(this);
1502 // call if cursor is on button area or mouse is captured for the button
1503 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent
& event
,
1506 int type
= event
.GetEventType();
1508 if ( type
== wxEVT_MOTION
)
1510 if ( (flags
& wxCC_MF_ON_BUTTON
) &&
1511 IsPopupWindowState(Hidden
) )
1513 if ( !(m_btnState
& wxCONTROL_CURRENT
) )
1515 // Mouse hover begins
1516 m_btnState
|= wxCONTROL_CURRENT
;
1517 if ( HasCapture() ) // Retain pressed state.
1518 m_btnState
|= wxCONTROL_PRESSED
;
1522 else if ( (m_btnState
& wxCONTROL_CURRENT
) )
1525 m_btnState
&= ~(wxCONTROL_CURRENT
|wxCONTROL_PRESSED
);
1529 else if ( type
== wxEVT_LEFT_DOWN
|| type
== wxEVT_LEFT_DCLICK
)
1531 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1533 m_btnState
|= wxCONTROL_PRESSED
;
1536 if ( !(m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
) )
1539 // If showing popup now, do not capture mouse or there will be interference
1543 else if ( type
== wxEVT_LEFT_UP
)
1546 // Only accept event if mouse was left-press was previously accepted
1550 if ( m_btnState
& wxCONTROL_PRESSED
)
1552 // If mouse was inside, fire the click event.
1553 if ( m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
)
1555 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1559 m_btnState
&= ~(wxCONTROL_PRESSED
);
1563 else if ( type
== wxEVT_LEAVE_WINDOW
)
1565 if ( m_btnState
& (wxCONTROL_CURRENT
|wxCONTROL_PRESSED
) )
1567 m_btnState
&= ~(wxCONTROL_CURRENT
);
1570 if ( IsPopupWindowState(Hidden
) )
1572 m_btnState
&= ~(wxCONTROL_PRESSED
);
1580 // Never have 'hot' state when popup is being shown
1581 // (this is mostly needed because of the animation).
1582 if ( !IsPopupWindowState(Hidden
) )
1583 m_btnState
&= ~wxCONTROL_CURRENT
;
1588 // returns true if event was consumed or filtered
1589 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent
& event
,
1590 int WXUNUSED(flags
) )
1592 wxLongLong t
= ::wxGetLocalTimeMillis();
1593 int evtType
= event
.GetEventType();
1595 #if USES_WXPOPUPWINDOW || USES_WXDIALOG
1596 if ( m_popupWinType
!= POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1598 if ( IsPopupWindowState(Visible
) &&
1599 ( evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_RIGHT_DOWN
) )
1607 // Filter out clicks on button immediately after popup dismiss
1608 if ( evtType
== wxEVT_LEFT_DOWN
&& t
< m_timeCanAcceptClick
)
1610 event
.SetEventType(0);
1617 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent
& event
)
1619 int evtType
= event
.GetEventType();
1621 if ( (evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_LEFT_DCLICK
) &&
1622 (m_windowStyle
& wxCB_READONLY
) )
1624 if ( GetPopupWindowState() >= Animating
)
1626 #if USES_WXPOPUPWINDOW
1627 // Click here always hides the popup.
1628 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1634 if ( !(m_windowStyle
& wxCC_SPECIAL_DCLICK
) )
1636 // In read-only mode, clicking the text is the
1637 // same as clicking the button.
1640 else if ( /*evtType == wxEVT_LEFT_UP || */evtType
== wxEVT_LEFT_DCLICK
)
1642 //if ( m_popupInterface->CycleValue() )
1644 if ( m_popupInterface
)
1645 m_popupInterface
->OnComboDoubleClick();
1650 if ( IsPopupShown() )
1652 // relay (some) mouse events to the popup
1653 if ( evtType
== wxEVT_MOUSEWHEEL
)
1654 m_popup
->GetEventHandler()->AddPendingEvent(event
);
1660 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent
& event
)
1662 if ( IsPopupShown() )
1664 // pass it to the popped up control
1665 GetPopupControl()->GetControl()->GetEventHandler()->AddPendingEvent(event
);
1669 if ( GetParent()->HasFlag(wxTAB_TRAVERSAL
) &&
1670 HandleAsNavigationKey(event
) )
1673 if ( IsKeyPopupToggle(event
) )
1679 int comboStyle
= GetWindowStyle();
1680 wxComboPopup
* popupInterface
= GetPopupControl();
1682 if ( !popupInterface
)
1688 int keycode
= event
.GetKeyCode();
1690 if ( (comboStyle
& wxCB_READONLY
) ||
1691 (keycode
!= WXK_RIGHT
&& keycode
!= WXK_LEFT
) )
1693 popupInterface
->OnComboKeyEvent(event
);
1700 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent
& event
)
1702 if ( event
.GetEventType() == wxEVT_SET_FOCUS
)
1704 wxWindow
* tc
= GetTextCtrl();
1705 if ( tc
&& tc
!= DoFindFocus() )
1707 m_resetFocus
= true;
1718 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent
& WXUNUSED(event
) )
1722 m_resetFocus
= false;
1723 wxWindow
* tc
= GetTextCtrl();
1729 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
1732 // indentation may also have changed
1733 if ( !(m_iFlags
& wxCC_IFLAG_INDENT_SET
) )
1734 m_absIndent
= GetNativeTextIndent();
1738 // ----------------------------------------------------------------------------
1740 // ----------------------------------------------------------------------------
1742 // Create popup window and the child control
1743 void wxComboCtrlBase::CreatePopup()
1745 wxComboPopup
* popupInterface
= m_popupInterface
;
1750 #ifdef wxComboPopupWindowBase2
1751 if ( m_iFlags
& wxCC_IFLAG_USE_ALT_POPUP
)
1754 m_winPopup
= new wxComboPopupWindowBase2( this, wxNO_BORDER
);
1756 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
, wxEmptyString
,
1757 wxPoint(-21,-21), wxSize(20, 20),
1760 m_popupWinType
= SECONDARY_POPUP_TYPE
;
1763 #endif // wxComboPopupWindowBase2
1765 m_winPopup
= new wxComboPopupWindow( this, wxNO_BORDER
);
1766 m_popupWinType
= PRIMARY_POPUP_TYPE
;
1768 m_popupWinEvtHandler
= new wxComboPopupWindowEvtHandler(this);
1769 m_winPopup
->PushEventHandler(m_popupWinEvtHandler
);
1772 popupInterface
->Create(m_winPopup
);
1773 m_popup
= popup
= popupInterface
->GetControl();
1775 m_popupExtraHandler
= new wxComboPopupExtraEventHandler(this);
1776 popup
->PushEventHandler( m_popupExtraHandler
);
1778 // This may be helpful on some platforms
1779 // (eg. it bypasses a wxGTK popupwindow bug where
1780 // window is not initially hidden when it should be)
1783 popupInterface
->m_iFlags
|= wxCP_IFLAG_CREATED
;
1786 // Destroy popup window and the child control
1787 void wxComboCtrlBase::DestroyPopup()
1792 m_popup
->RemoveEventHandler(m_popupExtraHandler
);
1794 delete m_popupExtraHandler
;
1796 delete m_popupInterface
;
1800 m_winPopup
->RemoveEventHandler(m_popupWinEvtHandler
);
1801 delete m_popupWinEvtHandler
;
1802 m_popupWinEvtHandler
= NULL
;
1803 m_winPopup
->Destroy();
1806 m_popupExtraHandler
= NULL
;
1807 m_popupInterface
= NULL
;
1812 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup
* iface
)
1814 wxCHECK_RET( iface
, wxT("no popup interface set for wxComboCtrl") );
1818 iface
->InitBase(this);
1821 m_popupInterface
= iface
;
1823 if ( !iface
->LazyCreate() )
1832 // This must be done after creation
1833 if ( m_valueString
.length() )
1835 iface
->SetStringValue(m_valueString
);
1840 // Ensures there is atleast the default popup
1841 void wxComboCtrlBase::EnsurePopupControl()
1843 if ( !m_popupInterface
)
1844 SetPopupControl(NULL
);
1847 void wxComboCtrlBase::OnButtonClick()
1849 // Derived classes can override this method for totally custom
1851 if ( !IsPopupWindowState(Visible
) )
1857 void wxComboCtrlBase::ShowPopup()
1859 EnsurePopupControl();
1860 wxCHECK_RET( !IsPopupWindowState(Visible
), wxT("popup window already shown") );
1862 if ( IsPopupWindowState(Animating
) )
1867 // Space above and below
1873 wxSize ctrlSz
= GetSize();
1875 screenHeight
= wxSystemSettings::GetMetric( wxSYS_SCREEN_Y
);
1876 scrPos
= GetParent()->ClientToScreen(GetPosition());
1878 spaceAbove
= scrPos
.y
;
1879 spaceBelow
= screenHeight
- spaceAbove
- ctrlSz
.y
;
1881 maxHeightPopup
= spaceBelow
;
1882 if ( spaceAbove
> spaceBelow
)
1883 maxHeightPopup
= spaceAbove
;
1886 int widthPopup
= ctrlSz
.x
+ m_extLeft
+ m_extRight
;
1888 if ( widthPopup
< m_widthMinPopup
)
1889 widthPopup
= m_widthMinPopup
;
1891 wxWindow
* winPopup
= m_winPopup
;
1894 // Need to disable tab traversal of parent
1896 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1897 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1898 // that if transient popup is open, then tab traversal is to be ignored.
1899 // However, I think this code would still be needed for cases where
1900 // transient popup doesn't work yet (wxWinCE?).
1901 wxWindow
* parent
= GetParent();
1902 int parentFlags
= parent
->GetWindowStyle();
1903 if ( parentFlags
& wxTAB_TRAVERSAL
)
1905 parent
->SetWindowStyle( parentFlags
& ~(wxTAB_TRAVERSAL
) );
1906 m_iFlags
|= wxCC_IFLAG_PARENT_TAB_TRAVERSAL
;
1912 winPopup
= m_winPopup
;
1922 wxASSERT( !m_popup
|| m_popup
== popup
); // Consistency check.
1924 wxSize adjustedSize
= m_popupInterface
->GetAdjustedSize(widthPopup
,
1925 m_heightPopup
<=0?DEFAULT_POPUP_HEIGHT
:m_heightPopup
,
1928 popup
->SetSize(adjustedSize
);
1930 m_popupInterface
->OnPopup();
1933 // Reposition and resize popup window
1936 wxSize szp
= popup
->GetSize();
1939 int popupY
= scrPos
.y
+ ctrlSz
.y
;
1941 // Default anchor is wxLEFT
1942 int anchorSide
= m_anchorSide
;
1944 anchorSide
= wxLEFT
;
1946 int rightX
= scrPos
.x
+ ctrlSz
.x
+ m_extRight
- szp
.x
;
1947 int leftX
= scrPos
.x
- m_extLeft
;
1949 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
1952 int screenWidth
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
);
1954 // If there is not enough horizontal space, anchor on the other side.
1955 // If there is no space even then, place the popup at x 0.
1956 if ( anchorSide
== wxRIGHT
)
1960 if ( (leftX
+szp
.x
) < screenWidth
)
1961 anchorSide
= wxLEFT
;
1968 if ( (leftX
+szp
.x
) >= screenWidth
)
1971 anchorSide
= wxRIGHT
;
1977 // Select x coordinate according to the anchor side
1978 if ( anchorSide
== wxRIGHT
)
1980 else if ( anchorSide
== wxLEFT
)
1985 int showFlags
= CanDeferShow
;
1987 if ( spaceBelow
< szp
.y
)
1989 popupY
= scrPos
.y
- szp
.y
;
1990 showFlags
|= ShowAbove
;
1993 #if INSTALL_TOPLEV_HANDLER
1994 // Put top level window event handler into place
1995 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1997 if ( !m_toplevEvtHandler
)
1998 m_toplevEvtHandler
= new wxComboFrameEventHandler(this);
2000 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2002 ((wxComboFrameEventHandler
*)m_toplevEvtHandler
)->OnPopup();
2003 toplev
->PushEventHandler( m_toplevEvtHandler
);
2007 // Set string selection (must be this way instead of SetStringSelection)
2010 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2011 m_text
->SelectAll();
2013 m_popupInterface
->SetStringValue( m_text
->GetValue() );
2017 // This is neede since focus/selection indication may change when popup is shown
2021 // This must be after SetStringValue
2022 m_popupWinState
= Animating
;
2024 wxRect
popupWinRect( popupX
, popupY
, szp
.x
, szp
.y
);
2027 if ( (m_iFlags
& wxCC_IFLAG_DISABLE_POPUP_ANIM
) ||
2028 AnimateShow( popupWinRect
, showFlags
) )
2030 DoShowPopup( popupWinRect
, showFlags
);
2034 bool wxComboCtrlBase::AnimateShow( const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
) )
2039 void wxComboCtrlBase::DoShowPopup( const wxRect
& rect
, int WXUNUSED(flags
) )
2041 wxWindow
* winPopup
= m_winPopup
;
2043 if ( IsPopupWindowState(Animating
) )
2045 // Make sure the popup window is shown in the right position.
2046 // Should not matter even if animation already did this.
2048 // Some platforms (GTK) may like SetSize and Move to be separate
2049 // (though the bug was probably fixed).
2050 winPopup
->SetSize( rect
);
2052 #if USES_WXPOPUPTRANSIENTWINDOW
2053 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2054 ((wxPopupTransientWindow
*)winPopup
)->Popup(m_popup
);
2059 m_popupWinState
= Visible
;
2061 else if ( IsPopupWindowState(Hidden
) )
2063 // Animation was aborted
2065 wxASSERT( !winPopup
->IsShown() );
2067 m_popupWinState
= Hidden
;
2073 void wxComboCtrlBase::OnPopupDismiss()
2075 // Just in case, avoid double dismiss
2076 if ( IsPopupWindowState(Hidden
) )
2079 // This must be set before focus - otherwise there will be recursive
2080 // OnPopupDismisses.
2081 m_popupWinState
= Hidden
;
2084 m_winPopup
->Disable();
2086 // Inform popup control itself
2087 m_popupInterface
->OnDismiss();
2089 if ( m_popupExtraHandler
)
2090 ((wxComboPopupExtraEventHandler
*)m_popupExtraHandler
)->OnPopupDismiss();
2092 #if INSTALL_TOPLEV_HANDLER
2093 // Remove top level window event handler
2094 if ( m_toplevEvtHandler
)
2096 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2098 toplev
->RemoveEventHandler( m_toplevEvtHandler
);
2102 m_timeCanAcceptClick
= ::wxGetLocalTimeMillis();
2104 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2105 m_timeCanAcceptClick
+= 150;
2107 // If cursor not on dropdown button, then clear its state
2108 // (technically not required by all ports, but do it for all just in case)
2109 if ( !m_btnArea
.Contains(ScreenToClient(::wxGetMousePosition())) )
2112 // Return parent's tab traversal flag.
2113 // See ShowPopup for notes.
2114 if ( m_iFlags
& wxCC_IFLAG_PARENT_TAB_TRAVERSAL
)
2116 wxWindow
* parent
= GetParent();
2117 parent
->SetWindowStyle( parent
->GetWindowStyle() | wxTAB_TRAVERSAL
);
2118 m_iFlags
&= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL
);
2121 // refresh control (necessary even if m_text)
2127 void wxComboCtrlBase::HidePopup()
2129 // Should be able to call this without popup interface
2130 if ( IsPopupWindowState(Hidden
) )
2133 // transfer value and show it in textctrl, if any
2134 if ( !IsPopupWindowState(Animating
) )
2135 SetValue( m_popupInterface
->GetStringValue() );
2142 // ----------------------------------------------------------------------------
2143 // customization methods
2144 // ----------------------------------------------------------------------------
2146 void wxComboCtrlBase::SetButtonPosition( int width
, int height
,
2147 int side
, int spacingX
)
2152 m_btnSpacingX
= spacingX
;
2157 wxSize
wxComboCtrlBase::GetButtonSize()
2159 if ( m_btnSize
.x
> 0 )
2162 wxSize
retSize(m_btnWid
,m_btnHei
);
2164 // Need to call CalculateAreas now if button size is
2165 // is not explicitly specified.
2166 if ( retSize
.x
<= 0 || retSize
.y
<= 0)
2170 retSize
= m_btnSize
;
2176 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap
& bmpNormal
,
2178 const wxBitmap
& bmpPressed
,
2179 const wxBitmap
& bmpHover
,
2180 const wxBitmap
& bmpDisabled
)
2182 m_bmpNormal
= bmpNormal
;
2183 m_blankButtonBg
= blankButtonBg
;
2185 if ( bmpPressed
.Ok() )
2186 m_bmpPressed
= bmpPressed
;
2188 m_bmpPressed
= bmpNormal
;
2190 if ( bmpHover
.Ok() )
2191 m_bmpHover
= bmpHover
;
2193 m_bmpHover
= bmpNormal
;
2195 if ( bmpDisabled
.Ok() )
2196 m_bmpDisabled
= bmpDisabled
;
2198 m_bmpDisabled
= bmpNormal
;
2203 void wxComboCtrlBase::SetCustomPaintWidth( int width
)
2207 // move textctrl accordingly
2208 wxRect r
= m_text
->GetRect();
2209 int inc
= width
- m_widthCustomPaint
;
2212 m_text
->SetSize( r
);
2215 m_widthCustomPaint
= width
;
2220 void wxComboCtrlBase::SetTextIndent( int indent
)
2224 m_absIndent
= GetNativeTextIndent();
2225 m_iFlags
&= ~(wxCC_IFLAG_INDENT_SET
);
2229 m_absIndent
= indent
;
2230 m_iFlags
|= wxCC_IFLAG_INDENT_SET
;
2236 wxCoord
wxComboCtrlBase::GetNativeTextIndent() const
2238 return DEFAULT_TEXT_INDENT
;
2241 // ----------------------------------------------------------------------------
2242 // methods forwarded to wxTextCtrl
2243 // ----------------------------------------------------------------------------
2245 wxString
wxComboCtrlBase::GetValue() const
2248 return m_text
->GetValue();
2249 return m_valueString
;
2252 void wxComboCtrlBase::SetValueWithEvent(const wxString
& value
, bool withEvent
)
2259 m_text
->SetValue(value
);
2261 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2262 m_text
->SelectAll();
2265 // Since wxComboPopup may want to paint the combo as well, we need
2266 // to set the string value here (as well as sometimes in ShowPopup).
2267 if ( m_valueString
!= value
)
2269 m_valueString
= value
;
2271 EnsurePopupControl();
2273 if (m_popupInterface
)
2274 m_popupInterface
->SetStringValue(value
);
2280 void wxComboCtrlBase::SetValue(const wxString
& value
)
2282 SetValueWithEvent(value
, false);
2285 // In this SetValue variant wxComboPopup::SetStringValue is not called
2286 void wxComboCtrlBase::SetText(const wxString
& value
)
2288 // Unlike in SetValue(), this must be called here or
2289 // the behaviour will no be consistent in readonlys.
2290 EnsurePopupControl();
2292 m_valueString
= value
;
2297 m_text
->SetValue( value
);
2303 void wxComboCtrlBase::Copy()
2309 void wxComboCtrlBase::Cut()
2315 void wxComboCtrlBase::Paste()
2321 void wxComboCtrlBase::SetInsertionPoint(long pos
)
2324 m_text
->SetInsertionPoint(pos
);
2327 void wxComboCtrlBase::SetInsertionPointEnd()
2330 m_text
->SetInsertionPointEnd();
2333 long wxComboCtrlBase::GetInsertionPoint() const
2336 return m_text
->GetInsertionPoint();
2341 long wxComboCtrlBase::GetLastPosition() const
2344 return m_text
->GetLastPosition();
2349 void wxComboCtrlBase::Replace(long from
, long to
, const wxString
& value
)
2352 m_text
->Replace(from
, to
, value
);
2355 void wxComboCtrlBase::Remove(long from
, long to
)
2358 m_text
->Remove(from
, to
);
2361 void wxComboCtrlBase::SetSelection(long from
, long to
)
2364 m_text
->SetSelection(from
, to
);
2367 void wxComboCtrlBase::Undo()
2373 #endif // wxUSE_COMBOCTRL