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 wxSize
wxComboPopup::GetAdjustedSize( int minWidth
,
504 int WXUNUSED(maxHeight
) )
506 return wxSize(minWidth
,prefHeight
);
509 void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase
* combo
,
510 wxDC
& dc
, const wxRect
& rect
)
512 if ( combo
->GetWindowStyle() & wxCB_READONLY
) // ie. no textctrl
514 combo
->PrepareBackground(dc
,rect
,0);
516 dc
.DrawText( combo
->GetValue(),
517 rect
.x
+ combo
->GetTextIndent(),
518 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
522 void wxComboPopup::PaintComboControl( wxDC
& dc
, const wxRect
& rect
)
524 DefaultPaintComboControl(m_combo
,dc
,rect
);
527 void wxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
532 void wxComboPopup::OnComboDoubleClick()
536 void wxComboPopup::SetStringValue( const wxString
& WXUNUSED(value
) )
540 bool wxComboPopup::LazyCreate()
545 void wxComboPopup::Dismiss()
547 m_combo
->HidePopup();
550 // ----------------------------------------------------------------------------
552 // ----------------------------------------------------------------------------
555 // This is pushed to the event handler queue of the child textctrl.
557 class wxComboBoxExtraInputHandler
: public wxEvtHandler
561 wxComboBoxExtraInputHandler( wxComboCtrlBase
* combo
)
566 virtual ~wxComboBoxExtraInputHandler() { }
567 void OnKey(wxKeyEvent
& event
);
568 void OnFocus(wxFocusEvent
& event
);
571 wxComboCtrlBase
* m_combo
;
574 DECLARE_EVENT_TABLE()
578 BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler
, wxEvtHandler
)
579 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey
)
580 EVT_KEY_UP(wxComboBoxExtraInputHandler::OnKey
)
581 EVT_CHAR(wxComboBoxExtraInputHandler::OnKey
)
582 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
583 EVT_KILL_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
587 void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent
& event
)
589 // Let the wxComboCtrl event handler have a go first.
590 wxComboCtrlBase
* combo
= m_combo
;
592 wxKeyEvent
redirectedEvent(event
);
593 redirectedEvent
.SetId(combo
->GetId());
594 redirectedEvent
.SetEventObject(combo
);
596 if ( !combo
->GetEventHandler()->ProcessEvent(redirectedEvent
) )
598 // Don't let TAB through to the text ctrl - looks ugly
599 if ( event
.GetKeyCode() != WXK_TAB
)
604 void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent
& event
)
606 // FIXME: This code does run when control is clicked,
607 // yet on Windows it doesn't select all the text.
608 if ( event
.GetEventType() == wxEVT_SET_FOCUS
&&
609 !(m_combo
->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT
) )
611 if ( m_combo
->GetTextCtrl() )
612 m_combo
->GetTextCtrl()->SelectAll();
614 m_combo
->SetSelection(-1,-1);
617 // Send focus indication to parent.
618 // NB: This is needed for cases where the textctrl gets focus
619 // instead of its parent. While this may trigger multiple
620 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
621 // from combo's focus event handler), they should be quite
623 wxFocusEvent
evt2(event
.GetEventType(),m_combo
->GetId());
624 evt2
.SetEventObject(m_combo
);
625 m_combo
->GetEventHandler()->ProcessEvent(evt2
);
632 // This is pushed to the event handler queue of the control in popup.
635 class wxComboPopupExtraEventHandler
: public wxEvtHandler
639 wxComboPopupExtraEventHandler( wxComboCtrlBase
* combo
)
643 m_beenInside
= false;
645 virtual ~wxComboPopupExtraEventHandler() { }
647 void OnMouseEvent( wxMouseEvent
& event
);
649 // Called from wxComboCtrlBase::OnPopupDismiss
650 void OnPopupDismiss()
652 m_beenInside
= false;
656 wxComboCtrlBase
* m_combo
;
661 DECLARE_EVENT_TABLE()
665 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler
, wxEvtHandler
)
666 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent
)
670 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent
& event
)
672 wxPoint pt
= event
.GetPosition();
673 wxSize sz
= m_combo
->GetPopupControl()->GetControl()->GetClientSize();
674 int evtType
= event
.GetEventType();
675 bool isInside
= pt
.x
>= 0 && pt
.y
>= 0 && pt
.x
< sz
.x
&& pt
.y
< sz
.y
;
676 bool relayToButton
= false;
680 if ( evtType
== wxEVT_MOTION
||
681 evtType
== wxEVT_LEFT_DOWN
||
682 evtType
== wxEVT_RIGHT_DOWN
)
684 // Block motion and click events outside the popup
685 if ( !isInside
|| !m_combo
->IsPopupShown() )
690 else if ( evtType
== wxEVT_LEFT_UP
)
692 if ( !m_combo
->IsPopupShown() )
695 relayToButton
= true;
697 else if ( !m_beenInside
)
705 relayToButton
= true;
713 // Some mouse events to popup that happen outside it, before cursor
714 // has been inside the popup, need to be ignored by it but relayed to
717 wxWindow
* eventSink
= m_combo
;
718 wxWindow
* btn
= m_combo
->GetButton();
722 eventSink
->GetEventHandler()->ProcessEvent(event
);
726 // ----------------------------------------------------------------------------
727 // wxComboCtrlTextCtrl
728 // ----------------------------------------------------------------------------
730 class wxComboCtrlTextCtrl
: public wxTextCtrl
733 wxComboCtrlTextCtrl() : wxTextCtrl() { }
734 virtual ~wxComboCtrlTextCtrl() { }
736 virtual wxWindow
*GetMainWindowOfCompositeControl()
738 wxComboCtrl
* combo
= (wxComboCtrl
*) GetParent();
740 // Returning this instead of just 'parent' lets FindFocus work
741 // correctly even when parent control is a child of a composite
742 // generic control (as is case with wxGenericDatePickerCtrl).
743 return combo
->GetMainWindowOfCompositeControl();
747 // ----------------------------------------------------------------------------
749 // ----------------------------------------------------------------------------
752 BEGIN_EVENT_TABLE(wxComboCtrlBase
, wxControl
)
753 EVT_TEXT(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
754 EVT_SIZE(wxComboCtrlBase::OnSizeEvent
)
755 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent
)
756 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent
)
757 EVT_IDLE(wxComboCtrlBase::OnIdleEvent
)
758 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
759 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent
)
760 EVT_TEXT_ENTER(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
761 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged
)
765 IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase
, wxControl
)
767 void wxComboCtrlBase::Init()
771 m_popupWinState
= Hidden
;
774 m_popupInterface
= NULL
;
776 m_popupExtraHandler
= NULL
;
777 m_textEvtHandler
= NULL
;
779 #if INSTALL_TOPLEV_HANDLER
780 m_toplevEvtHandler
= NULL
;
783 m_mainCtrlWnd
= this;
786 m_widthMinPopup
= -1;
788 m_widthCustomPaint
= 0;
789 m_widthCustomBorder
= 0;
793 m_blankButtonBg
= false;
795 m_popupWinType
= POPUPWIN_NONE
;
796 m_btnWid
= m_btnHei
= -1;
804 m_timeCanAcceptClick
= 0;
806 m_resetFocus
= false;
809 bool wxComboCtrlBase::Create(wxWindow
*parent
,
811 const wxString
& value
,
815 const wxValidator
& validator
,
816 const wxString
& name
)
818 if ( !wxControl::Create(parent
,
822 style
| wxWANTS_CHARS
,
827 m_valueString
= value
;
831 m_absIndent
= GetNativeTextIndent();
833 m_iFlags
|= wxCC_IFLAG_CREATED
;
835 // If x and y indicate valid size, wxSizeEvent won't be
836 // emitted automatically, so we need to add artifical one.
837 if ( size
.x
> 0 && size
.y
> 0 )
839 wxSizeEvent
evt(size
,GetId());
840 GetEventHandler()->AddPendingEvent(evt
);
846 void wxComboCtrlBase::InstallInputHandlers()
850 m_textEvtHandler
= new wxComboBoxExtraInputHandler(this);
851 m_text
->PushEventHandler(m_textEvtHandler
);
856 wxComboCtrlBase::CreateTextCtrl(int style
, const wxValidator
& validator
)
858 if ( !(m_windowStyle
& wxCB_READONLY
) )
863 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
864 // not used by the wxPropertyGrid and therefore the tab is processed by
865 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
866 // navigation event is then sent to the wrong window.
867 style
|= wxTE_PROCESS_TAB
;
869 if ( HasFlag(wxTE_PROCESS_ENTER
) )
870 style
|= wxTE_PROCESS_ENTER
;
872 // Ignore EVT_TEXT generated by the constructor (but only
873 // if the event redirector already exists)
874 // NB: This must be " = 1" instead of "++";
875 if ( m_textEvtHandler
)
880 m_text
= new wxComboCtrlTextCtrl();
881 m_text
->Create(this, wxID_ANY
, m_valueString
,
882 wxDefaultPosition
, wxSize(10,-1),
887 void wxComboCtrlBase::OnThemeChange()
889 // Leave the default bg on the Mac so the area used by the focus ring will
890 // be the correct colour and themed brush. Instead we'll use
891 // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed.
893 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
897 wxComboCtrlBase::~wxComboCtrlBase()
902 #if INSTALL_TOPLEV_HANDLER
903 delete ((wxComboFrameEventHandler
*)m_toplevEvtHandler
);
904 m_toplevEvtHandler
= NULL
;
910 m_text
->RemoveEventHandler(m_textEvtHandler
);
912 delete m_textEvtHandler
;
916 // ----------------------------------------------------------------------------
918 // ----------------------------------------------------------------------------
920 // Recalculates button and textctrl areas
921 void wxComboCtrlBase::CalculateAreas( int btnWidth
)
923 wxSize sz
= GetClientSize();
924 int customBorder
= m_widthCustomBorder
;
925 int btnBorder
; // border for button only
927 // check if button should really be outside the border: we'll do it it if
928 // its platform default or bitmap+pushbutton background is used, but not if
929 // there is vertical size adjustment or horizontal spacing.
930 if ( ( (m_iFlags
& wxCC_BUTTON_OUTSIDE_BORDER
) ||
931 (m_bmpNormal
.Ok() && m_blankButtonBg
) ) &&
932 m_btnSpacingX
== 0 &&
935 m_iFlags
|= wxCC_IFLAG_BUTTON_OUTSIDE
;
938 else if ( (m_iFlags
& wxCC_BUTTON_COVERS_BORDER
) &&
939 m_btnSpacingX
== 0 && !m_bmpNormal
.Ok() )
941 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
946 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
947 btnBorder
= customBorder
;
950 // Defaul indentation
951 if ( m_absIndent
< 0 )
952 m_absIndent
= GetNativeTextIndent();
954 int butWidth
= btnWidth
;
957 butWidth
= m_btnWidDefault
;
959 m_btnWidDefault
= butWidth
;
964 int butHeight
= sz
.y
- btnBorder
*2;
966 // Adjust button width
971 // Adjust button width to match aspect ratio
972 // (but only if control is smaller than best size).
973 int bestHeight
= GetBestSize().y
;
974 int height
= GetSize().y
;
976 if ( height
< bestHeight
)
978 // Make very small buttons square, as it makes
979 // them accommodate arrow image better and still
982 butWidth
= (height
*butWidth
)/bestHeight
;
984 butWidth
= butHeight
;
988 // Adjust button height
990 butHeight
= m_btnHei
;
992 // Use size of normal bitmap if...
995 // button width is set to default and blank button bg is not drawn
996 if ( m_bmpNormal
.Ok() )
998 int bmpReqWidth
= m_bmpNormal
.GetWidth();
999 int bmpReqHeight
= m_bmpNormal
.GetHeight();
1001 // If drawing blank button background, we need to add some margin.
1002 if ( m_blankButtonBg
)
1004 bmpReqWidth
+= BMP_BUTTON_MARGIN
*2;
1005 bmpReqHeight
+= BMP_BUTTON_MARGIN
*2;
1008 if ( butWidth
< bmpReqWidth
|| ( m_btnWid
== 0 && !m_blankButtonBg
) )
1009 butWidth
= bmpReqWidth
;
1010 if ( butHeight
< bmpReqHeight
|| ( m_btnHei
== 0 && !m_blankButtonBg
) )
1011 butHeight
= bmpReqHeight
;
1013 // Need to fix height?
1014 if ( (sz
.y
-(customBorder
*2)) < butHeight
&& btnWidth
== 0 )
1016 int newY
= butHeight
+(customBorder
*2);
1017 SetClientSize(wxDefaultCoord
,newY
);
1018 if ( m_bmpNormal
.Ok() || m_btnArea
.width
!= butWidth
|| m_btnArea
.height
!= butHeight
)
1019 m_iFlags
|= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1021 m_iFlags
&= ~wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1027 int butAreaWid
= butWidth
+ (m_btnSpacingX
*2);
1029 m_btnSize
.x
= butWidth
;
1030 m_btnSize
.y
= butHeight
;
1032 m_btnArea
.x
= ( m_btnSide
==wxRIGHT
? sz
.x
- butAreaWid
- btnBorder
: btnBorder
);
1033 m_btnArea
.y
= btnBorder
+ FOCUS_RING
;
1034 m_btnArea
.width
= butAreaWid
;
1035 m_btnArea
.height
= sz
.y
- ((btnBorder
+FOCUS_RING
)*2);
1037 m_tcArea
.x
= ( m_btnSide
==wxRIGHT
? 0 : butAreaWid
) + customBorder
+ FOCUS_RING
;
1038 m_tcArea
.y
= customBorder
+ FOCUS_RING
;
1039 m_tcArea
.width
= sz
.x
- butAreaWid
- (customBorder
*2) - (FOCUS_RING
*2);
1040 m_tcArea
.height
= sz
.y
- ((customBorder
+FOCUS_RING
)*2);
1045 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
1046 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1051 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
)
1056 #if !TEXTCTRL_TEXT_CENTERED
1058 wxSize sz
= GetClientSize();
1060 int customBorder
= m_widthCustomBorder
;
1061 if ( (m_text
->GetWindowStyleFlag() & wxBORDER_MASK
) == wxNO_BORDER
)
1064 int tcSizeY
= m_text
->GetBestSize().y
;
1065 int diff
= sz
.y
- tcSizeY
;
1066 int y
= textCtrlYAdjust
+ (diff
/2);
1068 if ( y
< customBorder
)
1071 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
+ m_absIndent
+ textCtrlXAdjust
,
1073 m_tcArea
.width
- COMBO_MARGIN
-
1074 (textCtrlXAdjust
+ m_widthCustomPaint
+ m_absIndent
),
1077 // Make sure textctrl doesn't exceed the bottom custom border
1078 wxSize tsz
= m_text
->GetSize();
1079 diff
= (y
+ tsz
.y
) - (sz
.y
- customBorder
);
1082 tsz
.y
= tsz
.y
- diff
- 1;
1083 m_text
->SetSize(tsz
);
1087 #else // TEXTCTRL_TEXT_CENTERED
1088 wxUnusedVar(textCtrlXAdjust
);
1089 wxUnusedVar(textCtrlYAdjust
);
1090 #endif // !TEXTCTRL_TEXT_CENTERED/TEXTCTRL_TEXT_CENTERED
1092 // If it has border, have textctrl will the entire text field.
1093 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
,
1095 m_tcArea
.width
- m_widthCustomPaint
,
1100 wxSize
wxComboCtrlBase::DoGetBestSize() const
1102 wxSize
sizeText(150,0);
1105 sizeText
= m_text
->GetBestSize();
1107 // TODO: Better method to calculate close-to-native control height.
1111 fhei
= (m_font
.GetPointSize()*2) + 5;
1112 else if ( wxNORMAL_FONT
->Ok() )
1113 fhei
= (wxNORMAL_FONT
->GetPointSize()*2) + 5;
1115 fhei
= sizeText
.y
+ 4;
1117 // Need to force height to accomodate bitmap?
1118 int btnSizeY
= m_btnSize
.y
;
1119 if ( m_bmpNormal
.Ok() && fhei
< btnSizeY
)
1122 // Control height doesn't depend on border
1125 int border = m_windowStyle & wxBORDER_MASK;
1126 if ( border == wxSIMPLE_BORDER )
1128 else if ( border == wxNO_BORDER )
1129 fhei += (m_widthCustomBorder*2);
1135 // Final adjustments
1141 // these are the numbers from the HIG:
1142 switch ( m_windowVariant
)
1144 case wxWINDOW_VARIANT_NORMAL
:
1148 case wxWINDOW_VARIANT_SMALL
:
1151 case wxWINDOW_VARIANT_MINI
:
1157 fhei
+= 2 * FOCUS_RING
;
1158 int width
= sizeText
.x
+ FOCUS_RING
+ COMBO_MARGIN
+ DEFAULT_DROPBUTTON_WIDTH
;
1160 wxSize
ret(width
, fhei
);
1165 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent
& event
)
1170 // defined by actual wxComboCtrls
1176 // ----------------------------------------------------------------------------
1177 // standard operations
1178 // ----------------------------------------------------------------------------
1180 bool wxComboCtrlBase::Enable(bool enable
)
1182 if ( !wxControl::Enable(enable
) )
1186 m_btn
->Enable(enable
);
1188 m_text
->Enable(enable
);
1195 bool wxComboCtrlBase::Show(bool show
)
1197 if ( !wxControl::Show(show
) )
1209 bool wxComboCtrlBase::SetFont ( const wxFont
& font
)
1211 if ( !wxControl::SetFont(font
) )
1215 m_text
->SetFont(font
);
1221 void wxComboCtrlBase::DoSetToolTip(wxToolTip
*tooltip
)
1223 wxControl::DoSetToolTip(tooltip
);
1225 // Set tool tip for button and text box
1228 const wxString
&tip
= tooltip
->GetTip();
1229 if ( m_text
) m_text
->SetToolTip(tip
);
1230 if ( m_btn
) m_btn
->SetToolTip(tip
);
1234 if ( m_text
) m_text
->SetToolTip( NULL
);
1235 if ( m_btn
) m_btn
->SetToolTip( NULL
);
1238 #endif // wxUSE_TOOLTIPS
1240 #if wxUSE_VALIDATORS
1241 void wxComboCtrlBase::SetValidator(const wxValidator
& validator
)
1243 wxTextCtrl
* textCtrl
= GetTextCtrl();
1246 textCtrl
->SetValidator( validator
);
1248 wxControl::SetValidator( validator
);
1251 wxValidator
* wxComboCtrlBase::GetValidator()
1253 wxTextCtrl
* textCtrl
= GetTextCtrl();
1255 return textCtrl
? textCtrl
->GetValidator() : wxControl::GetValidator();
1257 #endif // wxUSE_VALIDATORS
1259 // ----------------------------------------------------------------------------
1261 // ----------------------------------------------------------------------------
1263 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1264 // prepare combo box background on area in a way typical on platform
1265 void wxComboCtrlBase::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
1267 wxSize sz
= GetClientSize();
1269 bool doDrawFocusRect
; // also selected
1271 // For smaller size control (and for disabled background) use less spacing
1275 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1278 isEnabled
= IsEnabled();
1279 doDrawFocusRect
= ShouldDrawFocus() && !(m_iFlags
& wxCC_FULL_BUTTON
);
1281 // Windows-style: for smaller size control (and for disabled background) use less spacing
1282 focusSpacingX
= isEnabled
? 2 : 1;
1283 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
1287 // Drawing a list item
1288 isEnabled
= true; // they are never disabled
1289 doDrawFocusRect
= (flags
& wxCONTROL_SELECTED
) != 0;
1295 // Set the background sub-rectangle for selection, disabled etc
1296 wxRect
selRect(rect
);
1297 selRect
.y
+= focusSpacingY
;
1298 selRect
.height
-= (focusSpacingY
*2);
1302 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1303 wcp
+= m_widthCustomPaint
;
1305 selRect
.x
+= wcp
+ focusSpacingX
;
1306 selRect
.width
-= wcp
+ (focusSpacingX
*2);
1309 bool doDrawSelRect
= true;
1313 // If popup is hidden and this control is focused,
1314 // then draw the focus-indicator (selbgcolor background etc.).
1315 if ( doDrawFocusRect
)
1317 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1318 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1322 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
1323 #ifndef __WXMAC__ // see note in OnThemeChange
1324 doDrawSelRect
= false;
1325 bgCol
= GetBackgroundColour();
1327 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1333 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
1334 #ifndef __WXMAC__ // see note in OnThemeChange
1335 bgCol
= GetBackgroundColour();
1337 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1341 dc
.SetBrush( bgCol
);
1342 if ( doDrawSelRect
)
1345 dc
.DrawRectangle( selRect
);
1348 // Don't clip exactly to the selection rectangle so we can draw
1349 // to the non-selected area in front of it.
1350 wxRect
clipRect(rect
.x
,rect
.y
,
1351 (selRect
.x
+selRect
.width
)-rect
.x
,rect
.height
);
1352 dc
.SetClippingRegion(clipRect
);
1355 // Save the library size a bit for platforms that re-implement this.
1356 void wxComboCtrlBase::PrepareBackground( wxDC
&, const wxRect
&, int ) const
1361 void wxComboCtrlBase::DrawButton( wxDC
& dc
, const wxRect
& rect
, int flags
)
1363 int drawState
= m_btnState
;
1365 if ( (m_iFlags
& wxCC_BUTTON_STAYS_DOWN
) &&
1366 GetPopupWindowState() >= Animating
)
1367 drawState
|= wxCONTROL_PRESSED
;
1369 wxRect
drawRect(rect
.x
+m_btnSpacingX
,
1370 rect
.y
+((rect
.height
-m_btnSize
.y
)/2),
1374 // Make sure area is not larger than the control
1375 if ( drawRect
.y
< rect
.y
)
1376 drawRect
.y
= rect
.y
;
1377 if ( drawRect
.height
> rect
.height
)
1378 drawRect
.height
= rect
.height
;
1380 bool enabled
= IsEnabled();
1383 drawState
|= wxCONTROL_DISABLED
;
1385 if ( !m_bmpNormal
.Ok() )
1387 if ( flags
& Button_BitmapOnly
)
1390 // Need to clear button background even if m_btn is present
1391 if ( flags
& Button_PaintBackground
)
1395 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
1396 bgCol
= GetParent()->GetBackgroundColour();
1398 bgCol
= GetBackgroundColour();
1402 dc
.DrawRectangle(rect
);
1405 // Draw standard button
1406 wxRendererNative::Get().DrawComboBoxDropButton(this,
1418 pBmp
= &m_bmpDisabled
;
1419 else if ( m_btnState
& wxCONTROL_PRESSED
)
1420 pBmp
= &m_bmpPressed
;
1421 else if ( m_btnState
& wxCONTROL_CURRENT
)
1424 pBmp
= &m_bmpNormal
;
1426 if ( m_blankButtonBg
)
1428 // If using blank button background, we need to clear its background
1429 // with button face colour instead of colour for rest of the control.
1430 if ( flags
& Button_PaintBackground
)
1432 wxColour bgCol
= GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1433 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1436 dc
.DrawRectangle(rect
);
1439 if ( !(flags
& Button_BitmapOnly
) )
1441 wxRendererNative::Get().DrawPushButton(this,
1450 // Need to clear button background even if m_btn is present
1451 // (assume non-button background was cleared just before this call so brushes are good)
1452 if ( flags
& Button_PaintBackground
)
1453 dc
.DrawRectangle(rect
);
1456 // Draw bitmap centered in drawRect
1457 dc
.DrawBitmap(*pBmp
,
1458 drawRect
.x
+ (drawRect
.width
-pBmp
->GetWidth())/2,
1459 drawRect
.y
+ (drawRect
.height
-pBmp
->GetHeight())/2,
1464 void wxComboCtrlBase::RecalcAndRefresh()
1468 wxSizeEvent
evt(GetSize(),GetId());
1469 GetEventHandler()->ProcessEvent(evt
);
1474 // ----------------------------------------------------------------------------
1475 // miscellaneous event handlers
1476 // ----------------------------------------------------------------------------
1478 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent
& event
)
1480 if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1482 if ( m_ignoreEvtText
> 0 )
1489 // Change event id, object and string before relaying it forward
1490 event
.SetId(GetId());
1491 wxString s
= event
.GetString();
1492 event
.SetEventObject(this);
1497 // call if cursor is on button area or mouse is captured for the button
1498 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent
& event
,
1501 int type
= event
.GetEventType();
1503 if ( type
== wxEVT_MOTION
)
1505 if ( (flags
& wxCC_MF_ON_BUTTON
) &&
1506 IsPopupWindowState(Hidden
) )
1508 if ( !(m_btnState
& wxCONTROL_CURRENT
) )
1510 // Mouse hover begins
1511 m_btnState
|= wxCONTROL_CURRENT
;
1512 if ( HasCapture() ) // Retain pressed state.
1513 m_btnState
|= wxCONTROL_PRESSED
;
1517 else if ( (m_btnState
& wxCONTROL_CURRENT
) )
1520 m_btnState
&= ~(wxCONTROL_CURRENT
|wxCONTROL_PRESSED
);
1524 else if ( type
== wxEVT_LEFT_DOWN
|| type
== wxEVT_LEFT_DCLICK
)
1526 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1528 m_btnState
|= wxCONTROL_PRESSED
;
1531 if ( !(m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
) )
1534 // If showing popup now, do not capture mouse or there will be interference
1538 else if ( type
== wxEVT_LEFT_UP
)
1541 // Only accept event if mouse was left-press was previously accepted
1545 if ( m_btnState
& wxCONTROL_PRESSED
)
1547 // If mouse was inside, fire the click event.
1548 if ( m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
)
1550 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1554 m_btnState
&= ~(wxCONTROL_PRESSED
);
1558 else if ( type
== wxEVT_LEAVE_WINDOW
)
1560 if ( m_btnState
& (wxCONTROL_CURRENT
|wxCONTROL_PRESSED
) )
1562 m_btnState
&= ~(wxCONTROL_CURRENT
);
1565 if ( IsPopupWindowState(Hidden
) )
1567 m_btnState
&= ~(wxCONTROL_PRESSED
);
1575 // Never have 'hot' state when popup is being shown
1576 // (this is mostly needed because of the animation).
1577 if ( !IsPopupWindowState(Hidden
) )
1578 m_btnState
&= ~wxCONTROL_CURRENT
;
1583 // returns true if event was consumed or filtered
1584 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent
& event
,
1585 int WXUNUSED(flags
) )
1587 wxLongLong t
= ::wxGetLocalTimeMillis();
1588 int evtType
= event
.GetEventType();
1590 #if USES_WXPOPUPWINDOW || USES_WXDIALOG
1591 if ( m_popupWinType
!= POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1593 if ( IsPopupWindowState(Visible
) &&
1594 ( evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_RIGHT_DOWN
) )
1602 // Filter out clicks on button immediately after popup dismiss
1603 if ( evtType
== wxEVT_LEFT_DOWN
&& t
< m_timeCanAcceptClick
)
1605 event
.SetEventType(0);
1612 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent
& event
)
1614 int evtType
= event
.GetEventType();
1616 if ( (evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_LEFT_DCLICK
) &&
1617 (m_windowStyle
& wxCB_READONLY
) )
1619 if ( GetPopupWindowState() >= Animating
)
1621 #if USES_WXPOPUPWINDOW
1622 // Click here always hides the popup.
1623 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1629 if ( !(m_windowStyle
& wxCC_SPECIAL_DCLICK
) )
1631 // In read-only mode, clicking the text is the
1632 // same as clicking the button.
1635 else if ( /*evtType == wxEVT_LEFT_UP || */evtType
== wxEVT_LEFT_DCLICK
)
1637 //if ( m_popupInterface->CycleValue() )
1639 if ( m_popupInterface
)
1640 m_popupInterface
->OnComboDoubleClick();
1645 if ( IsPopupShown() )
1647 // relay (some) mouse events to the popup
1648 if ( evtType
== wxEVT_MOUSEWHEEL
)
1649 m_popup
->GetEventHandler()->AddPendingEvent(event
);
1655 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent
& event
)
1657 if ( IsPopupShown() )
1659 // pass it to the popped up control
1660 GetPopupControl()->GetControl()->GetEventHandler()->AddPendingEvent(event
);
1664 if ( GetParent()->HasFlag(wxTAB_TRAVERSAL
) &&
1665 HandleAsNavigationKey(event
) )
1668 if ( IsKeyPopupToggle(event
) )
1674 int comboStyle
= GetWindowStyle();
1675 wxComboPopup
* popupInterface
= GetPopupControl();
1677 if ( !popupInterface
)
1683 int keycode
= event
.GetKeyCode();
1685 if ( (comboStyle
& wxCB_READONLY
) ||
1686 (keycode
!= WXK_RIGHT
&& keycode
!= WXK_LEFT
) )
1688 popupInterface
->OnComboKeyEvent(event
);
1695 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent
& event
)
1697 if ( event
.GetEventType() == wxEVT_SET_FOCUS
)
1699 wxWindow
* tc
= GetTextCtrl();
1700 if ( tc
&& tc
!= DoFindFocus() )
1702 m_resetFocus
= true;
1713 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent
& WXUNUSED(event
) )
1717 m_resetFocus
= false;
1718 wxWindow
* tc
= GetTextCtrl();
1724 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
1727 // indentation may also have changed
1728 if ( !(m_iFlags
& wxCC_IFLAG_INDENT_SET
) )
1729 m_absIndent
= GetNativeTextIndent();
1733 // ----------------------------------------------------------------------------
1735 // ----------------------------------------------------------------------------
1737 // Create popup window and the child control
1738 void wxComboCtrlBase::CreatePopup()
1740 wxComboPopup
* popupInterface
= m_popupInterface
;
1745 #ifdef wxComboPopupWindowBase2
1746 if ( m_iFlags
& wxCC_IFLAG_USE_ALT_POPUP
)
1749 m_winPopup
= new wxComboPopupWindowBase2( this, wxNO_BORDER
);
1751 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
, wxEmptyString
,
1752 wxPoint(-21,-21), wxSize(20, 20),
1755 m_popupWinType
= SECONDARY_POPUP_TYPE
;
1758 #endif // wxComboPopupWindowBase2
1760 m_winPopup
= new wxComboPopupWindow( this, wxNO_BORDER
);
1761 m_popupWinType
= PRIMARY_POPUP_TYPE
;
1763 m_popupWinEvtHandler
= new wxComboPopupWindowEvtHandler(this);
1764 m_winPopup
->PushEventHandler(m_popupWinEvtHandler
);
1767 popupInterface
->Create(m_winPopup
);
1768 m_popup
= popup
= popupInterface
->GetControl();
1770 m_popupExtraHandler
= new wxComboPopupExtraEventHandler(this);
1771 popup
->PushEventHandler( m_popupExtraHandler
);
1773 // This may be helpful on some platforms
1774 // (eg. it bypasses a wxGTK popupwindow bug where
1775 // window is not initially hidden when it should be)
1778 popupInterface
->m_iFlags
|= wxCP_IFLAG_CREATED
;
1781 // Destroy popup window and the child control
1782 void wxComboCtrlBase::DestroyPopup()
1787 m_popup
->RemoveEventHandler(m_popupExtraHandler
);
1789 delete m_popupExtraHandler
;
1791 delete m_popupInterface
;
1795 m_winPopup
->RemoveEventHandler(m_popupWinEvtHandler
);
1796 delete m_popupWinEvtHandler
;
1797 m_popupWinEvtHandler
= NULL
;
1798 m_winPopup
->Destroy();
1801 m_popupExtraHandler
= NULL
;
1802 m_popupInterface
= NULL
;
1807 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup
* iface
)
1809 wxCHECK_RET( iface
, wxT("no popup interface set for wxComboCtrl") );
1813 iface
->InitBase(this);
1816 m_popupInterface
= iface
;
1818 if ( !iface
->LazyCreate() )
1827 // This must be done after creation
1828 if ( m_valueString
.length() )
1830 iface
->SetStringValue(m_valueString
);
1835 // Ensures there is atleast the default popup
1836 void wxComboCtrlBase::EnsurePopupControl()
1838 if ( !m_popupInterface
)
1839 SetPopupControl(NULL
);
1842 void wxComboCtrlBase::OnButtonClick()
1844 // Derived classes can override this method for totally custom
1846 if ( !IsPopupWindowState(Visible
) )
1852 void wxComboCtrlBase::ShowPopup()
1854 EnsurePopupControl();
1855 wxCHECK_RET( !IsPopupWindowState(Visible
), wxT("popup window already shown") );
1857 if ( IsPopupWindowState(Animating
) )
1862 // Space above and below
1868 wxSize ctrlSz
= GetSize();
1870 screenHeight
= wxSystemSettings::GetMetric( wxSYS_SCREEN_Y
);
1871 scrPos
= GetParent()->ClientToScreen(GetPosition());
1873 spaceAbove
= scrPos
.y
;
1874 spaceBelow
= screenHeight
- spaceAbove
- ctrlSz
.y
;
1876 maxHeightPopup
= spaceBelow
;
1877 if ( spaceAbove
> spaceBelow
)
1878 maxHeightPopup
= spaceAbove
;
1881 int widthPopup
= ctrlSz
.x
+ m_extLeft
+ m_extRight
;
1883 if ( widthPopup
< m_widthMinPopup
)
1884 widthPopup
= m_widthMinPopup
;
1886 wxWindow
* winPopup
= m_winPopup
;
1889 // Need to disable tab traversal of parent
1891 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1892 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1893 // that if transient popup is open, then tab traversal is to be ignored.
1894 // However, I think this code would still be needed for cases where
1895 // transient popup doesn't work yet (wxWinCE?).
1896 wxWindow
* parent
= GetParent();
1897 int parentFlags
= parent
->GetWindowStyle();
1898 if ( parentFlags
& wxTAB_TRAVERSAL
)
1900 parent
->SetWindowStyle( parentFlags
& ~(wxTAB_TRAVERSAL
) );
1901 m_iFlags
|= wxCC_IFLAG_PARENT_TAB_TRAVERSAL
;
1907 winPopup
= m_winPopup
;
1917 wxASSERT( !m_popup
|| m_popup
== popup
); // Consistency check.
1919 wxSize adjustedSize
= m_popupInterface
->GetAdjustedSize(widthPopup
,
1920 m_heightPopup
<=0?DEFAULT_POPUP_HEIGHT
:m_heightPopup
,
1923 popup
->SetSize(adjustedSize
);
1925 m_popupInterface
->OnPopup();
1928 // Reposition and resize popup window
1931 wxSize szp
= popup
->GetSize();
1934 int popupY
= scrPos
.y
+ ctrlSz
.y
;
1936 // Default anchor is wxLEFT
1937 int anchorSide
= m_anchorSide
;
1939 anchorSide
= wxLEFT
;
1941 int rightX
= scrPos
.x
+ ctrlSz
.x
+ m_extRight
- szp
.x
;
1942 int leftX
= scrPos
.x
- m_extLeft
;
1944 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
1947 int screenWidth
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
);
1949 // If there is not enough horizontal space, anchor on the other side.
1950 // If there is no space even then, place the popup at x 0.
1951 if ( anchorSide
== wxRIGHT
)
1955 if ( (leftX
+szp
.x
) < screenWidth
)
1956 anchorSide
= wxLEFT
;
1963 if ( (leftX
+szp
.x
) >= screenWidth
)
1966 anchorSide
= wxRIGHT
;
1972 // Select x coordinate according to the anchor side
1973 if ( anchorSide
== wxRIGHT
)
1975 else if ( anchorSide
== wxLEFT
)
1980 int showFlags
= CanDeferShow
;
1982 if ( spaceBelow
< szp
.y
)
1984 popupY
= scrPos
.y
- szp
.y
;
1985 showFlags
|= ShowAbove
;
1988 #if INSTALL_TOPLEV_HANDLER
1989 // Put top level window event handler into place
1990 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1992 if ( !m_toplevEvtHandler
)
1993 m_toplevEvtHandler
= new wxComboFrameEventHandler(this);
1995 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
1997 ((wxComboFrameEventHandler
*)m_toplevEvtHandler
)->OnPopup();
1998 toplev
->PushEventHandler( m_toplevEvtHandler
);
2002 // Set string selection (must be this way instead of SetStringSelection)
2005 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2006 m_text
->SelectAll();
2008 m_popupInterface
->SetStringValue( m_text
->GetValue() );
2012 // This is neede since focus/selection indication may change when popup is shown
2016 // This must be after SetStringValue
2017 m_popupWinState
= Animating
;
2019 wxRect
popupWinRect( popupX
, popupY
, szp
.x
, szp
.y
);
2022 if ( (m_iFlags
& wxCC_IFLAG_DISABLE_POPUP_ANIM
) ||
2023 AnimateShow( popupWinRect
, showFlags
) )
2025 DoShowPopup( popupWinRect
, showFlags
);
2029 bool wxComboCtrlBase::AnimateShow( const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
) )
2034 void wxComboCtrlBase::DoShowPopup( const wxRect
& rect
, int WXUNUSED(flags
) )
2036 wxWindow
* winPopup
= m_winPopup
;
2038 if ( IsPopupWindowState(Animating
) )
2040 // Make sure the popup window is shown in the right position.
2041 // Should not matter even if animation already did this.
2043 // Some platforms (GTK) may like SetSize and Move to be separate
2044 // (though the bug was probably fixed).
2045 winPopup
->SetSize( rect
);
2047 #if USES_WXPOPUPTRANSIENTWINDOW
2048 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2049 ((wxPopupTransientWindow
*)winPopup
)->Popup(m_popup
);
2054 m_popupWinState
= Visible
;
2056 else if ( IsPopupWindowState(Hidden
) )
2058 // Animation was aborted
2060 wxASSERT( !winPopup
->IsShown() );
2062 m_popupWinState
= Hidden
;
2068 void wxComboCtrlBase::OnPopupDismiss()
2070 // Just in case, avoid double dismiss
2071 if ( IsPopupWindowState(Hidden
) )
2074 // This must be set before focus - otherwise there will be recursive
2075 // OnPopupDismisses.
2076 m_popupWinState
= Hidden
;
2079 m_winPopup
->Disable();
2081 // Inform popup control itself
2082 m_popupInterface
->OnDismiss();
2084 if ( m_popupExtraHandler
)
2085 ((wxComboPopupExtraEventHandler
*)m_popupExtraHandler
)->OnPopupDismiss();
2087 #if INSTALL_TOPLEV_HANDLER
2088 // Remove top level window event handler
2089 if ( m_toplevEvtHandler
)
2091 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2093 toplev
->RemoveEventHandler( m_toplevEvtHandler
);
2097 m_timeCanAcceptClick
= ::wxGetLocalTimeMillis();
2099 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2100 m_timeCanAcceptClick
+= 150;
2102 // If cursor not on dropdown button, then clear its state
2103 // (technically not required by all ports, but do it for all just in case)
2104 if ( !m_btnArea
.Contains(ScreenToClient(::wxGetMousePosition())) )
2107 // Return parent's tab traversal flag.
2108 // See ShowPopup for notes.
2109 if ( m_iFlags
& wxCC_IFLAG_PARENT_TAB_TRAVERSAL
)
2111 wxWindow
* parent
= GetParent();
2112 parent
->SetWindowStyle( parent
->GetWindowStyle() | wxTAB_TRAVERSAL
);
2113 m_iFlags
&= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL
);
2116 // refresh control (necessary even if m_text)
2122 void wxComboCtrlBase::HidePopup()
2124 // Should be able to call this without popup interface
2125 if ( IsPopupWindowState(Hidden
) )
2128 // transfer value and show it in textctrl, if any
2129 if ( !IsPopupWindowState(Animating
) )
2130 SetValue( m_popupInterface
->GetStringValue() );
2137 // ----------------------------------------------------------------------------
2138 // customization methods
2139 // ----------------------------------------------------------------------------
2141 void wxComboCtrlBase::SetButtonPosition( int width
, int height
,
2142 int side
, int spacingX
)
2147 m_btnSpacingX
= spacingX
;
2152 wxSize
wxComboCtrlBase::GetButtonSize()
2154 if ( m_btnSize
.x
> 0 )
2157 wxSize
retSize(m_btnWid
,m_btnHei
);
2159 // Need to call CalculateAreas now if button size is
2160 // is not explicitly specified.
2161 if ( retSize
.x
<= 0 || retSize
.y
<= 0)
2165 retSize
= m_btnSize
;
2171 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap
& bmpNormal
,
2173 const wxBitmap
& bmpPressed
,
2174 const wxBitmap
& bmpHover
,
2175 const wxBitmap
& bmpDisabled
)
2177 m_bmpNormal
= bmpNormal
;
2178 m_blankButtonBg
= blankButtonBg
;
2180 if ( bmpPressed
.Ok() )
2181 m_bmpPressed
= bmpPressed
;
2183 m_bmpPressed
= bmpNormal
;
2185 if ( bmpHover
.Ok() )
2186 m_bmpHover
= bmpHover
;
2188 m_bmpHover
= bmpNormal
;
2190 if ( bmpDisabled
.Ok() )
2191 m_bmpDisabled
= bmpDisabled
;
2193 m_bmpDisabled
= bmpNormal
;
2198 void wxComboCtrlBase::SetCustomPaintWidth( int width
)
2202 // move textctrl accordingly
2203 wxRect r
= m_text
->GetRect();
2204 int inc
= width
- m_widthCustomPaint
;
2207 m_text
->SetSize( r
);
2210 m_widthCustomPaint
= width
;
2215 void wxComboCtrlBase::SetTextIndent( int indent
)
2219 m_absIndent
= GetNativeTextIndent();
2220 m_iFlags
&= ~(wxCC_IFLAG_INDENT_SET
);
2224 m_absIndent
= indent
;
2225 m_iFlags
|= wxCC_IFLAG_INDENT_SET
;
2231 wxCoord
wxComboCtrlBase::GetNativeTextIndent() const
2233 return DEFAULT_TEXT_INDENT
;
2236 // ----------------------------------------------------------------------------
2237 // methods forwarded to wxTextCtrl
2238 // ----------------------------------------------------------------------------
2240 wxString
wxComboCtrlBase::GetValue() const
2243 return m_text
->GetValue();
2244 return m_valueString
;
2247 void wxComboCtrlBase::SetValueWithEvent(const wxString
& value
, bool withEvent
)
2254 m_text
->SetValue(value
);
2256 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2257 m_text
->SelectAll();
2260 // Since wxComboPopup may want to paint the combo as well, we need
2261 // to set the string value here (as well as sometimes in ShowPopup).
2262 if ( m_valueString
!= value
)
2264 m_valueString
= value
;
2266 EnsurePopupControl();
2268 if (m_popupInterface
)
2269 m_popupInterface
->SetStringValue(value
);
2275 void wxComboCtrlBase::SetValue(const wxString
& value
)
2277 SetValueWithEvent(value
, false);
2280 // In this SetValue variant wxComboPopup::SetStringValue is not called
2281 void wxComboCtrlBase::SetText(const wxString
& value
)
2283 // Unlike in SetValue(), this must be called here or
2284 // the behaviour will no be consistent in readonlys.
2285 EnsurePopupControl();
2287 m_valueString
= value
;
2292 m_text
->SetValue( value
);
2298 void wxComboCtrlBase::Copy()
2304 void wxComboCtrlBase::Cut()
2310 void wxComboCtrlBase::Paste()
2316 void wxComboCtrlBase::SetInsertionPoint(long pos
)
2319 m_text
->SetInsertionPoint(pos
);
2322 void wxComboCtrlBase::SetInsertionPointEnd()
2325 m_text
->SetInsertionPointEnd();
2328 long wxComboCtrlBase::GetInsertionPoint() const
2331 return m_text
->GetInsertionPoint();
2336 long wxComboCtrlBase::GetLastPosition() const
2339 return m_text
->GetLastPosition();
2344 void wxComboCtrlBase::Replace(long from
, long to
, const wxString
& value
)
2347 m_text
->Replace(from
, to
, value
);
2350 void wxComboCtrlBase::Remove(long from
, long to
)
2353 m_text
->Remove(from
, to
);
2356 void wxComboCtrlBase::SetSelection(long from
, long to
)
2359 m_text
->SetSelection(from
, to
);
2362 void wxComboCtrlBase::Undo()
2368 #endif // wxUSE_COMBOCTRL