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 #if !defined(__WXWINCE__)
70 // 1 if wxTextEntry::SetMargins() can be used to set the left margin
71 #define LEFT_MARGIN_CAN_BE_SET 1
73 #define LEFT_MARGIN_CAN_BE_SET 0
76 //#undef wxUSE_POPUPWIN
77 //#define wxUSE_POPUPWIN 0
79 #elif defined(__WXGTK__)
81 #include "wx/gtk/private.h"
83 // NB: It is not recommended to use wxDialog as popup on wxGTK, because of
84 // this bug: If wxDialog is hidden, its position becomes corrupt
85 // between hide and next show, but without internal coordinates being
86 // reflected (or something like that - atleast commenting out ->Hide()
87 // seemed to eliminate the position change).
89 // NB: Let's not be afraid to use wxGTK's wxPopupTransientWindow as a
90 // 'perfect' popup, as it can succesfully host child controls even in
91 // popups that are shown in modal dialogs.
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 0 // No focus ring on wxGTK
100 #if GTK_CHECK_VERSION(2,10,0)
101 // 1 if wxTextEntry::SetMargins() can be used to set the left margin
102 #define LEFT_MARGIN_CAN_BE_SET 1
104 #define LEFT_MARGIN_CAN_BE_SET 0
107 #elif defined(__WXMAC__)
109 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
110 #define TRANSIENT_POPUPWIN_IS_PERFECT 1 // wxPopupTransientWindow works, its child can have focus, and common
111 // native controls work on it like normal.
112 #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
113 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
114 #define FOCUS_RING 3 // Reserve room for the textctrl's focus ring to display
116 #undef DEFAULT_DROPBUTTON_WIDTH
117 #define DEFAULT_DROPBUTTON_WIDTH 22
119 #define COMBO_MARGIN FOCUS_RING
121 // 1 if wxTextEntry::SetMargins() can be used to set the left margin
122 #define LEFT_MARGIN_CAN_BE_SET 0
126 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
127 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
128 // native controls work on it like normal.
129 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
130 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
133 // 1 if wxTextEntry::SetMargins() can be used to set the left margin
134 #define LEFT_MARGIN_CAN_BE_SET 0
139 // Popupwin is really only supported on wxMSW (not WINCE) and wxGTK, regardless
140 // what the wxUSE_POPUPWIN says.
141 // FIXME: Why isn't wxUSE_POPUPWIN reliable any longer? (it was in wxW2.6.2)
142 #if (!defined(__WXMSW__) && !defined(__WXGTK__) && !defined(__WXMAC__)) || defined(__WXWINCE__)
143 #undef wxUSE_POPUPWIN
144 #define wxUSE_POPUPWIN 0
149 #include "wx/popupwin.h"
151 #undef USE_TRANSIENT_POPUP
152 #define USE_TRANSIENT_POPUP 0
156 // Define different types of popup windows
160 POPUPWIN_WXPOPUPTRANSIENTWINDOW
= 1,
161 POPUPWIN_WXPOPUPWINDOW
= 2,
162 POPUPWIN_WXDIALOG
= 3
166 #if USE_TRANSIENT_POPUP
167 // wxPopupTransientWindow is implemented
169 #define wxComboPopupWindowBase wxPopupTransientWindow
170 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPTRANSIENTWINDOW
171 #define USES_WXPOPUPTRANSIENTWINDOW 1
173 #if TRANSIENT_POPUPWIN_IS_PERFECT
175 #elif POPUPWIN_IS_PERFECT
176 #define wxComboPopupWindowBase2 wxPopupWindow
177 #define SECONDARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
178 #define USES_WXPOPUPWINDOW 1
180 #define wxComboPopupWindowBase2 wxDialog
181 #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG
182 #define USES_WXDIALOG 1
186 // wxPopupWindow (but not wxPopupTransientWindow) is properly implemented
188 #define wxComboPopupWindowBase wxPopupWindow
189 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
190 #define USES_WXPOPUPWINDOW 1
192 #if !POPUPWIN_IS_PERFECT
193 #define wxComboPopupWindowBase2 wxDialog
194 #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG
195 #define USES_WXDIALOG 1
199 // wxPopupWindow is not implemented
201 #define wxComboPopupWindowBase wxDialog
202 #define PRIMARY_POPUP_TYPE POPUPWIN_WXDIALOG
203 #define USES_WXDIALOG 1
208 #ifndef USES_WXPOPUPTRANSIENTWINDOW
209 #define USES_WXPOPUPTRANSIENTWINDOW 0
212 #ifndef USES_WXPOPUPWINDOW
213 #define USES_WXPOPUPWINDOW 0
216 #ifndef USES_WXDIALOG
217 #define USES_WXDIALOG 0
221 #if USES_WXPOPUPWINDOW
222 #define INSTALL_TOPLEV_HANDLER 1
224 #define INSTALL_TOPLEV_HANDLER 0
230 // * wxComboPopupWindow for external use (ie. replace old wxUniv wxPopupComboWindow)
234 // ----------------------------------------------------------------------------
235 // wxComboFrameEventHandler takes care of hiding the popup when events happen
236 // in its top level parent.
237 // ----------------------------------------------------------------------------
239 #if INSTALL_TOPLEV_HANDLER
242 // This will no longer be necessary after wxTransientPopupWindow
243 // works well on all platforms.
246 class wxComboFrameEventHandler
: public wxEvtHandler
249 wxComboFrameEventHandler( wxComboCtrlBase
* pCb
);
250 virtual ~wxComboFrameEventHandler();
254 void OnIdle( wxIdleEvent
& event
);
255 void OnMouseEvent( wxMouseEvent
& event
);
256 void OnActivate( wxActivateEvent
& event
);
257 void OnResize( wxSizeEvent
& event
);
258 void OnMove( wxMoveEvent
& event
);
259 void OnMenuEvent( wxMenuEvent
& event
);
260 void OnClose( wxCloseEvent
& event
);
263 wxWindow
* m_focusStart
;
264 wxComboCtrlBase
* m_combo
;
267 DECLARE_EVENT_TABLE()
270 BEGIN_EVENT_TABLE(wxComboFrameEventHandler
, wxEvtHandler
)
271 EVT_IDLE(wxComboFrameEventHandler::OnIdle
)
272 EVT_LEFT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
273 EVT_RIGHT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
274 EVT_SIZE(wxComboFrameEventHandler::OnResize
)
275 EVT_MOVE(wxComboFrameEventHandler::OnMove
)
276 EVT_MENU_HIGHLIGHT(wxID_ANY
,wxComboFrameEventHandler::OnMenuEvent
)
277 EVT_MENU_OPEN(wxComboFrameEventHandler::OnMenuEvent
)
278 EVT_ACTIVATE(wxComboFrameEventHandler::OnActivate
)
279 EVT_CLOSE(wxComboFrameEventHandler::OnClose
)
282 wxComboFrameEventHandler::wxComboFrameEventHandler( wxComboCtrlBase
* combo
)
288 wxComboFrameEventHandler::~wxComboFrameEventHandler()
292 void wxComboFrameEventHandler::OnPopup()
294 m_focusStart
= ::wxWindow::FindFocus();
297 void wxComboFrameEventHandler::OnIdle( wxIdleEvent
& event
)
299 wxWindow
* winFocused
= ::wxWindow::FindFocus();
301 wxWindow
* popup
= m_combo
->GetPopupControl()->GetControl();
302 wxWindow
* winpopup
= m_combo
->GetPopupWindow();
305 winFocused
!= m_focusStart
&&
306 winFocused
!= popup
&&
307 winFocused
->GetParent() != popup
&&
308 winFocused
!= winpopup
&&
309 winFocused
->GetParent() != winpopup
&&
310 winFocused
!= m_combo
&&
311 winFocused
!= m_combo
->GetButton() // GTK (atleast) requires this
314 m_combo
->HidePopup();
320 void wxComboFrameEventHandler::OnMenuEvent( wxMenuEvent
& event
)
322 m_combo
->HidePopup();
326 void wxComboFrameEventHandler::OnMouseEvent( wxMouseEvent
& event
)
328 m_combo
->HidePopup();
332 void wxComboFrameEventHandler::OnClose( wxCloseEvent
& event
)
334 m_combo
->HidePopup();
338 void wxComboFrameEventHandler::OnActivate( wxActivateEvent
& event
)
340 m_combo
->HidePopup();
344 void wxComboFrameEventHandler::OnResize( wxSizeEvent
& event
)
346 m_combo
->HidePopup();
350 void wxComboFrameEventHandler::OnMove( wxMoveEvent
& event
)
352 m_combo
->HidePopup();
356 #endif // INSTALL_TOPLEV_HANDLER
358 // ----------------------------------------------------------------------------
359 // wxComboPopupWindow is, in essence, wxPopupWindow customized for
361 // ----------------------------------------------------------------------------
363 class wxComboPopupWindow
: public wxComboPopupWindowBase
367 wxComboPopupWindow( wxComboCtrlBase
*parent
,
369 #if USES_WXPOPUPWINDOW || USES_WXPOPUPTRANSIENTWINDOW
370 : wxComboPopupWindowBase(parent
,style
)
372 : wxComboPopupWindowBase(parent
,
383 #if USES_WXPOPUPTRANSIENTWINDOW
384 virtual bool Show( bool show
);
385 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
387 virtual void OnDismiss();
395 #if USES_WXPOPUPTRANSIENTWINDOW
396 bool wxComboPopupWindow::Show( bool show
)
398 // Guard against recursion
400 return wxComboPopupWindowBase::Show(show
);
404 wxASSERT( IsKindOf(CLASSINFO(wxPopupTransientWindow
)) );
406 wxPopupTransientWindow
* ptw
= (wxPopupTransientWindow
*) this;
408 if ( show
!= ptw
->IsShown() )
411 // We used to do wxPopupTransientWindow::Popup here,
412 // but this would hide normal Show, which we are
413 // also going to need.
424 bool wxComboPopupWindow::ProcessLeftDown(wxMouseEvent
& event
)
426 return wxPopupTransientWindow::ProcessLeftDown(event
);
429 // First thing that happens when a transient popup closes is that this method gets called.
430 void wxComboPopupWindow::OnDismiss()
432 wxComboCtrlBase
* combo
= (wxComboCtrlBase
*) GetParent();
433 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxComboCtrlBase
)),
434 wxT("parent might not be wxComboCtrl, but check IMPLEMENT_DYNAMIC_CLASS(2) macro for correctness") );
436 combo
->OnPopupDismiss();
438 #endif // USES_WXPOPUPTRANSIENTWINDOW
441 // ----------------------------------------------------------------------------
442 // wxComboPopupWindowEvtHandler does bulk of the custom event handling
443 // of a popup window. It is separate so we can have different types
445 // ----------------------------------------------------------------------------
447 class wxComboPopupWindowEvtHandler
: public wxEvtHandler
451 wxComboPopupWindowEvtHandler( wxComboCtrlBase
*parent
)
456 void OnSizeEvent( wxSizeEvent
& event
);
457 void OnKeyEvent(wxKeyEvent
& event
);
459 void OnActivate( wxActivateEvent
& event
);
463 wxComboCtrlBase
* m_combo
;
465 DECLARE_EVENT_TABLE()
469 BEGIN_EVENT_TABLE(wxComboPopupWindowEvtHandler
, wxEvtHandler
)
470 EVT_KEY_DOWN(wxComboPopupWindowEvtHandler::OnKeyEvent
)
471 EVT_KEY_UP(wxComboPopupWindowEvtHandler::OnKeyEvent
)
473 EVT_ACTIVATE(wxComboPopupWindowEvtHandler::OnActivate
)
475 EVT_SIZE(wxComboPopupWindowEvtHandler::OnSizeEvent
)
479 void wxComboPopupWindowEvtHandler::OnSizeEvent( wxSizeEvent
& WXUNUSED(event
) )
481 // Block the event so that the popup control does not get auto-resized.
484 void wxComboPopupWindowEvtHandler::OnKeyEvent( wxKeyEvent
& event
)
486 // Relay keyboard event to the main child controls
487 wxWindowList children
= m_combo
->GetPopupWindow()->GetChildren();
488 wxWindowList::iterator node
= children
.begin();
489 wxWindow
* child
= (wxWindow
*)*node
;
490 child
->GetEventHandler()->AddPendingEvent(event
);
494 void wxComboPopupWindowEvtHandler::OnActivate( wxActivateEvent
& event
)
496 if ( !event
.GetActive() )
498 // Tell combo control that we are dismissed.
499 m_combo
->HidePopup();
507 // ----------------------------------------------------------------------------
510 // ----------------------------------------------------------------------------
512 wxComboPopup::~wxComboPopup()
516 void wxComboPopup::OnPopup()
520 void wxComboPopup::OnDismiss()
524 wxComboCtrl
* wxComboPopup::GetComboCtrl() const
526 return wxStaticCast(m_combo
, wxComboCtrl
);
529 wxSize
wxComboPopup::GetAdjustedSize( int minWidth
,
531 int WXUNUSED(maxHeight
) )
533 return wxSize(minWidth
,prefHeight
);
536 void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase
* combo
,
537 wxDC
& dc
, const wxRect
& rect
)
539 if ( combo
->GetWindowStyle() & wxCB_READONLY
) // ie. no textctrl
541 combo
->PrepareBackground(dc
,rect
,0);
543 dc
.DrawText( combo
->GetValue(),
544 rect
.x
+ combo
->m_marginLeft
,
545 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
549 void wxComboPopup::PaintComboControl( wxDC
& dc
, const wxRect
& rect
)
551 DefaultPaintComboControl(m_combo
,dc
,rect
);
554 void wxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
559 void wxComboPopup::OnComboDoubleClick()
563 void wxComboPopup::SetStringValue( const wxString
& WXUNUSED(value
) )
567 bool wxComboPopup::LazyCreate()
572 void wxComboPopup::Dismiss()
574 m_combo
->HidePopup();
577 // ----------------------------------------------------------------------------
579 // ----------------------------------------------------------------------------
582 // This is pushed to the event handler queue of the child textctrl.
584 class wxComboBoxExtraInputHandler
: public wxEvtHandler
588 wxComboBoxExtraInputHandler( wxComboCtrlBase
* combo
)
593 virtual ~wxComboBoxExtraInputHandler() { }
594 void OnKey(wxKeyEvent
& event
);
595 void OnFocus(wxFocusEvent
& event
);
598 wxComboCtrlBase
* m_combo
;
601 DECLARE_EVENT_TABLE()
605 BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler
, wxEvtHandler
)
606 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey
)
607 EVT_KEY_UP(wxComboBoxExtraInputHandler::OnKey
)
608 EVT_CHAR(wxComboBoxExtraInputHandler::OnKey
)
609 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
610 EVT_KILL_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
614 void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent
& event
)
616 // Let the wxComboCtrl event handler have a go first.
617 wxComboCtrlBase
* combo
= m_combo
;
619 wxKeyEvent
redirectedEvent(event
);
620 redirectedEvent
.SetId(combo
->GetId());
621 redirectedEvent
.SetEventObject(combo
);
623 if ( !combo
->GetEventHandler()->ProcessEvent(redirectedEvent
) )
625 // Don't let TAB through to the text ctrl - looks ugly
626 if ( event
.GetKeyCode() != WXK_TAB
)
631 void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent
& event
)
633 // FIXME: This code does run when control is clicked,
634 // yet on Windows it doesn't select all the text.
635 if ( event
.GetEventType() == wxEVT_SET_FOCUS
&&
636 !(m_combo
->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT
) )
638 if ( m_combo
->GetTextCtrl() )
639 m_combo
->GetTextCtrl()->SelectAll();
641 m_combo
->SetSelection(-1,-1);
644 // Send focus indication to parent.
645 // NB: This is needed for cases where the textctrl gets focus
646 // instead of its parent. While this may trigger multiple
647 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
648 // from combo's focus event handler), they should be quite
650 wxFocusEvent
evt2(event
.GetEventType(),m_combo
->GetId());
651 evt2
.SetEventObject(m_combo
);
652 m_combo
->GetEventHandler()->ProcessEvent(evt2
);
659 // This is pushed to the event handler queue of the control in popup.
662 class wxComboPopupExtraEventHandler
: public wxEvtHandler
666 wxComboPopupExtraEventHandler( wxComboCtrlBase
* combo
)
670 m_beenInside
= false;
672 virtual ~wxComboPopupExtraEventHandler() { }
674 void OnMouseEvent( wxMouseEvent
& event
);
676 // Called from wxComboCtrlBase::OnPopupDismiss
677 void OnPopupDismiss()
679 m_beenInside
= false;
683 wxComboCtrlBase
* m_combo
;
688 DECLARE_EVENT_TABLE()
692 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler
, wxEvtHandler
)
693 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent
)
697 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent
& event
)
699 wxPoint pt
= event
.GetPosition();
700 wxSize sz
= m_combo
->GetPopupControl()->GetControl()->GetClientSize();
701 int evtType
= event
.GetEventType();
702 bool isInside
= pt
.x
>= 0 && pt
.y
>= 0 && pt
.x
< sz
.x
&& pt
.y
< sz
.y
;
703 bool relayToButton
= false;
707 if ( evtType
== wxEVT_MOTION
||
708 evtType
== wxEVT_LEFT_DOWN
||
709 evtType
== wxEVT_RIGHT_DOWN
)
711 // Block motion and click events outside the popup
712 if ( !isInside
|| !m_combo
->IsPopupShown() )
717 else if ( evtType
== wxEVT_LEFT_UP
)
719 if ( !m_combo
->IsPopupShown() )
722 relayToButton
= true;
724 else if ( !m_beenInside
)
732 relayToButton
= true;
740 // Some mouse events to popup that happen outside it, before cursor
741 // has been inside the popup, need to be ignored by it but relayed to
744 wxWindow
* eventSink
= m_combo
;
745 wxWindow
* btn
= m_combo
->GetButton();
749 eventSink
->GetEventHandler()->ProcessEvent(event
);
753 // ----------------------------------------------------------------------------
754 // wxComboCtrlTextCtrl
755 // ----------------------------------------------------------------------------
757 class wxComboCtrlTextCtrl
: public wxTextCtrl
760 wxComboCtrlTextCtrl() : wxTextCtrl() { }
761 virtual ~wxComboCtrlTextCtrl() { }
763 virtual wxWindow
*GetMainWindowOfCompositeControl()
765 wxComboCtrl
* combo
= (wxComboCtrl
*) GetParent();
767 // Returning this instead of just 'parent' lets FindFocus work
768 // correctly even when parent control is a child of a composite
769 // generic control (as is case with wxGenericDatePickerCtrl).
770 return combo
->GetMainWindowOfCompositeControl();
774 // ----------------------------------------------------------------------------
776 // ----------------------------------------------------------------------------
779 BEGIN_EVENT_TABLE(wxComboCtrlBase
, wxControl
)
780 EVT_TEXT(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
781 EVT_SIZE(wxComboCtrlBase::OnSizeEvent
)
782 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent
)
783 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent
)
784 EVT_IDLE(wxComboCtrlBase::OnIdleEvent
)
785 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
786 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent
)
787 EVT_TEXT_ENTER(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
788 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged
)
792 IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase
, wxControl
)
794 void wxComboCtrlBase::Init()
798 m_popupWinState
= Hidden
;
801 m_popupInterface
= NULL
;
803 m_popupExtraHandler
= NULL
;
804 m_textEvtHandler
= NULL
;
806 #if INSTALL_TOPLEV_HANDLER
807 m_toplevEvtHandler
= NULL
;
810 m_mainCtrlWnd
= this;
813 m_widthMinPopup
= -1;
815 m_widthCustomPaint
= 0;
816 m_widthCustomBorder
= 0;
820 m_blankButtonBg
= false;
822 m_popupWinType
= POPUPWIN_NONE
;
823 m_btnWid
= m_btnHei
= -1;
831 m_timeCanAcceptClick
= 0;
833 m_resetFocus
= false;
836 bool wxComboCtrlBase::Create(wxWindow
*parent
,
838 const wxString
& value
,
842 const wxValidator
& validator
,
843 const wxString
& name
)
845 if ( !wxControl::Create(parent
,
849 style
| wxWANTS_CHARS
,
854 m_valueString
= value
;
858 m_marginLeft
= GetNativeTextIndent();
860 m_iFlags
|= wxCC_IFLAG_CREATED
;
862 // If x and y indicate valid size, wxSizeEvent won't be
863 // emitted automatically, so we need to add artifical one.
864 if ( size
.x
> 0 && size
.y
> 0 )
866 wxSizeEvent
evt(size
,GetId());
867 GetEventHandler()->AddPendingEvent(evt
);
873 void wxComboCtrlBase::InstallInputHandlers()
877 m_textEvtHandler
= new wxComboBoxExtraInputHandler(this);
878 m_text
->PushEventHandler(m_textEvtHandler
);
883 wxComboCtrlBase::CreateTextCtrl(int style
, const wxValidator
& validator
)
885 if ( !(m_windowStyle
& wxCB_READONLY
) )
890 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
891 // not used by the wxPropertyGrid and therefore the tab is processed by
892 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
893 // navigation event is then sent to the wrong window.
894 style
|= wxTE_PROCESS_TAB
;
896 if ( HasFlag(wxTE_PROCESS_ENTER
) )
897 style
|= wxTE_PROCESS_ENTER
;
899 // Ignore EVT_TEXT generated by the constructor (but only
900 // if the event redirector already exists)
901 // NB: This must be " = 1" instead of "++";
902 if ( m_textEvtHandler
)
907 m_text
= new wxComboCtrlTextCtrl();
908 m_text
->Create(this, wxID_ANY
, m_valueString
,
909 wxDefaultPosition
, wxSize(10,-1),
914 void wxComboCtrlBase::OnThemeChange()
916 // Leave the default bg on the Mac so the area used by the focus ring will
917 // be the correct colour and themed brush. Instead we'll use
918 // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed.
920 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
924 wxComboCtrlBase::~wxComboCtrlBase()
929 #if INSTALL_TOPLEV_HANDLER
930 delete ((wxComboFrameEventHandler
*)m_toplevEvtHandler
);
931 m_toplevEvtHandler
= NULL
;
937 m_text
->RemoveEventHandler(m_textEvtHandler
);
939 delete m_textEvtHandler
;
943 // ----------------------------------------------------------------------------
945 // ----------------------------------------------------------------------------
947 // Recalculates button and textctrl areas
948 void wxComboCtrlBase::CalculateAreas( int btnWidth
)
950 wxSize sz
= GetClientSize();
951 int customBorder
= m_widthCustomBorder
;
952 int btnBorder
; // border for button only
954 // check if button should really be outside the border: we'll do it it if
955 // its platform default or bitmap+pushbutton background is used, but not if
956 // there is vertical size adjustment or horizontal spacing.
957 if ( ( (m_iFlags
& wxCC_BUTTON_OUTSIDE_BORDER
) ||
958 (m_bmpNormal
.Ok() && m_blankButtonBg
) ) &&
959 m_btnSpacingX
== 0 &&
962 m_iFlags
|= wxCC_IFLAG_BUTTON_OUTSIDE
;
965 else if ( (m_iFlags
& wxCC_BUTTON_COVERS_BORDER
) &&
966 m_btnSpacingX
== 0 && !m_bmpNormal
.Ok() )
968 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
973 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
974 btnBorder
= customBorder
;
977 // Defaul indentation
978 if ( m_marginLeft
< 0 )
979 m_marginLeft
= GetNativeTextIndent();
981 int butWidth
= btnWidth
;
984 butWidth
= m_btnWidDefault
;
986 m_btnWidDefault
= butWidth
;
991 int butHeight
= sz
.y
- btnBorder
*2;
993 // Adjust button width
998 // Adjust button width to match aspect ratio
999 // (but only if control is smaller than best size).
1000 int bestHeight
= GetBestSize().y
;
1001 int height
= GetSize().y
;
1003 if ( height
< bestHeight
)
1005 // Make very small buttons square, as it makes
1006 // them accommodate arrow image better and still
1009 butWidth
= (height
*butWidth
)/bestHeight
;
1011 butWidth
= butHeight
;
1015 // Adjust button height
1017 butHeight
= m_btnHei
;
1019 // Use size of normal bitmap if...
1022 // button width is set to default and blank button bg is not drawn
1023 if ( m_bmpNormal
.Ok() )
1025 int bmpReqWidth
= m_bmpNormal
.GetWidth();
1026 int bmpReqHeight
= m_bmpNormal
.GetHeight();
1028 // If drawing blank button background, we need to add some margin.
1029 if ( m_blankButtonBg
)
1031 bmpReqWidth
+= BMP_BUTTON_MARGIN
*2;
1032 bmpReqHeight
+= BMP_BUTTON_MARGIN
*2;
1035 if ( butWidth
< bmpReqWidth
|| ( m_btnWid
== 0 && !m_blankButtonBg
) )
1036 butWidth
= bmpReqWidth
;
1037 if ( butHeight
< bmpReqHeight
|| ( m_btnHei
== 0 && !m_blankButtonBg
) )
1038 butHeight
= bmpReqHeight
;
1040 // Need to fix height?
1041 if ( (sz
.y
-(customBorder
*2)) < butHeight
&& btnWidth
== 0 )
1043 int newY
= butHeight
+(customBorder
*2);
1044 SetClientSize(wxDefaultCoord
,newY
);
1045 if ( m_bmpNormal
.Ok() || m_btnArea
.width
!= butWidth
|| m_btnArea
.height
!= butHeight
)
1046 m_iFlags
|= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1048 m_iFlags
&= ~wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1054 int butAreaWid
= butWidth
+ (m_btnSpacingX
*2);
1056 m_btnSize
.x
= butWidth
;
1057 m_btnSize
.y
= butHeight
;
1059 m_btnArea
.x
= ( m_btnSide
==wxRIGHT
? sz
.x
- butAreaWid
- btnBorder
: btnBorder
);
1060 m_btnArea
.y
= btnBorder
+ FOCUS_RING
;
1061 m_btnArea
.width
= butAreaWid
;
1062 m_btnArea
.height
= sz
.y
- ((btnBorder
+FOCUS_RING
)*2);
1064 m_tcArea
.x
= ( m_btnSide
==wxRIGHT
? 0 : butAreaWid
) + customBorder
+ FOCUS_RING
;
1065 m_tcArea
.y
= customBorder
+ FOCUS_RING
;
1066 m_tcArea
.width
= sz
.x
- butAreaWid
- (customBorder
*2) - (FOCUS_RING
*2);
1067 m_tcArea
.height
= sz
.y
- ((customBorder
+FOCUS_RING
)*2);
1072 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
1073 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1078 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
)
1083 wxSize sz
= GetClientSize();
1085 int customBorder
= m_widthCustomBorder
;
1086 if ( (m_text
->GetWindowStyleFlag() & wxBORDER_MASK
) == wxNO_BORDER
)
1088 #if LEFT_MARGIN_CAN_BE_SET
1089 // Call SetMargins() on textctrl if LEFT_MARGIN_CAN_BE_SET == 1
1090 wxUnusedVar(textCtrlXAdjust
);
1091 m_text
->SetMargins(0);
1092 textCtrlXAdjust
= 0;
1096 #if !TEXTCTRL_TEXT_CENTERED
1097 int tcSizeY
= m_text
->GetBestSize().y
;
1098 int diff0
= sz
.y
- tcSizeY
;
1099 int y
= textCtrlYAdjust
+ (diff0
/2);
1101 wxUnusedVar(textCtrlYAdjust
);
1105 if ( y
< customBorder
)
1108 int x
= m_tcArea
.x
+ m_widthCustomPaint
+
1109 m_marginLeft
+ textCtrlXAdjust
;
1113 m_tcArea
.width
- m_tcArea
.x
- x
,
1114 /*m_tcArea.width - COMBO_MARGIN -
1115 (textCtrlXAdjust + m_widthCustomPaint +
1119 // Make sure textctrl doesn't exceed the bottom custom border
1120 wxSize tsz
= m_text
->GetSize();
1121 int diff1
= (y
+ tsz
.y
) - (sz
.y
- customBorder
);
1124 tsz
.y
= tsz
.y
- diff1
- 1;
1125 m_text
->SetSize(tsz
);
1130 // If it has border, have textctrl fill the entire text field.
1131 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
,
1133 m_tcArea
.width
- m_widthCustomPaint
,
1138 wxSize
wxComboCtrlBase::DoGetBestSize() const
1140 wxSize
sizeText(150,0);
1143 sizeText
= m_text
->GetBestSize();
1145 // TODO: Better method to calculate close-to-native control height.
1149 fhei
= (m_font
.GetPointSize()*2) + 5;
1150 else if ( wxNORMAL_FONT
->Ok() )
1151 fhei
= (wxNORMAL_FONT
->GetPointSize()*2) + 5;
1153 fhei
= sizeText
.y
+ 4;
1155 // Need to force height to accomodate bitmap?
1156 int btnSizeY
= m_btnSize
.y
;
1157 if ( m_bmpNormal
.Ok() && fhei
< btnSizeY
)
1160 // Control height doesn't depend on border
1163 int border = m_windowStyle & wxBORDER_MASK;
1164 if ( border == wxSIMPLE_BORDER )
1166 else if ( border == wxNO_BORDER )
1167 fhei += (m_widthCustomBorder*2);
1173 // Final adjustments
1179 // these are the numbers from the HIG:
1180 switch ( m_windowVariant
)
1182 case wxWINDOW_VARIANT_NORMAL
:
1186 case wxWINDOW_VARIANT_SMALL
:
1189 case wxWINDOW_VARIANT_MINI
:
1195 fhei
+= 2 * FOCUS_RING
;
1196 int width
= sizeText
.x
+ FOCUS_RING
+ COMBO_MARGIN
+ DEFAULT_DROPBUTTON_WIDTH
;
1198 wxSize
ret(width
, fhei
);
1203 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent
& event
)
1208 // defined by actual wxComboCtrls
1214 // ----------------------------------------------------------------------------
1215 // standard operations
1216 // ----------------------------------------------------------------------------
1218 bool wxComboCtrlBase::Enable(bool enable
)
1220 if ( !wxControl::Enable(enable
) )
1224 m_btn
->Enable(enable
);
1226 m_text
->Enable(enable
);
1233 bool wxComboCtrlBase::Show(bool show
)
1235 if ( !wxControl::Show(show
) )
1247 bool wxComboCtrlBase::SetFont ( const wxFont
& font
)
1249 if ( !wxControl::SetFont(font
) )
1253 m_text
->SetFont(font
);
1259 void wxComboCtrlBase::DoSetToolTip(wxToolTip
*tooltip
)
1261 wxControl::DoSetToolTip(tooltip
);
1263 // Set tool tip for button and text box
1266 const wxString
&tip
= tooltip
->GetTip();
1267 if ( m_text
) m_text
->SetToolTip(tip
);
1268 if ( m_btn
) m_btn
->SetToolTip(tip
);
1272 if ( m_text
) m_text
->SetToolTip( NULL
);
1273 if ( m_btn
) m_btn
->SetToolTip( NULL
);
1276 #endif // wxUSE_TOOLTIPS
1278 #if wxUSE_VALIDATORS
1279 void wxComboCtrlBase::SetValidator(const wxValidator
& validator
)
1281 wxTextCtrl
* textCtrl
= GetTextCtrl();
1284 textCtrl
->SetValidator( validator
);
1286 wxControl::SetValidator( validator
);
1289 wxValidator
* wxComboCtrlBase::GetValidator()
1291 wxTextCtrl
* textCtrl
= GetTextCtrl();
1293 return textCtrl
? textCtrl
->GetValidator() : wxControl::GetValidator();
1295 #endif // wxUSE_VALIDATORS
1297 // ----------------------------------------------------------------------------
1299 // ----------------------------------------------------------------------------
1301 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1302 // prepare combo box background on area in a way typical on platform
1303 void wxComboCtrlBase::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
1305 wxSize sz
= GetClientSize();
1307 bool doDrawFocusRect
; // also selected
1309 // For smaller size control (and for disabled background) use less spacing
1313 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1316 isEnabled
= IsEnabled();
1317 doDrawFocusRect
= ShouldDrawFocus() && !(m_iFlags
& wxCC_FULL_BUTTON
);
1319 // Windows-style: for smaller size control (and for disabled background) use less spacing
1320 focusSpacingX
= isEnabled
? 2 : 1;
1321 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
1325 // Drawing a list item
1326 isEnabled
= true; // they are never disabled
1327 doDrawFocusRect
= (flags
& wxCONTROL_SELECTED
) != 0;
1333 // Set the background sub-rectangle for selection, disabled etc
1334 wxRect
selRect(rect
);
1335 selRect
.y
+= focusSpacingY
;
1336 selRect
.height
-= (focusSpacingY
*2);
1340 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1341 wcp
+= m_widthCustomPaint
;
1343 selRect
.x
+= wcp
+ focusSpacingX
;
1344 selRect
.width
-= wcp
+ (focusSpacingX
*2);
1347 bool doDrawSelRect
= true;
1351 // If popup is hidden and this control is focused,
1352 // then draw the focus-indicator (selbgcolor background etc.).
1353 if ( doDrawFocusRect
)
1355 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1356 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1360 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
1361 #ifndef __WXMAC__ // see note in OnThemeChange
1362 doDrawSelRect
= false;
1363 bgCol
= GetBackgroundColour();
1365 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1371 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
1372 #ifndef __WXMAC__ // see note in OnThemeChange
1373 bgCol
= GetBackgroundColour();
1375 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1379 dc
.SetBrush( bgCol
);
1380 if ( doDrawSelRect
)
1383 dc
.DrawRectangle( selRect
);
1386 // Don't clip exactly to the selection rectangle so we can draw
1387 // to the non-selected area in front of it.
1388 wxRect
clipRect(rect
.x
,rect
.y
,
1389 (selRect
.x
+selRect
.width
)-rect
.x
,rect
.height
);
1390 dc
.SetClippingRegion(clipRect
);
1393 // Save the library size a bit for platforms that re-implement this.
1394 void wxComboCtrlBase::PrepareBackground( wxDC
&, const wxRect
&, int ) const
1399 void wxComboCtrlBase::DrawButton( wxDC
& dc
, const wxRect
& rect
, int flags
)
1401 int drawState
= m_btnState
;
1403 if ( (m_iFlags
& wxCC_BUTTON_STAYS_DOWN
) &&
1404 GetPopupWindowState() >= Animating
)
1405 drawState
|= wxCONTROL_PRESSED
;
1407 wxRect
drawRect(rect
.x
+m_btnSpacingX
,
1408 rect
.y
+((rect
.height
-m_btnSize
.y
)/2),
1412 // Make sure area is not larger than the control
1413 if ( drawRect
.y
< rect
.y
)
1414 drawRect
.y
= rect
.y
;
1415 if ( drawRect
.height
> rect
.height
)
1416 drawRect
.height
= rect
.height
;
1418 bool enabled
= IsEnabled();
1421 drawState
|= wxCONTROL_DISABLED
;
1423 if ( !m_bmpNormal
.Ok() )
1425 if ( flags
& Button_BitmapOnly
)
1428 // Need to clear button background even if m_btn is present
1429 if ( flags
& Button_PaintBackground
)
1433 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
1434 bgCol
= GetParent()->GetBackgroundColour();
1436 bgCol
= GetBackgroundColour();
1440 dc
.DrawRectangle(rect
);
1443 // Draw standard button
1444 wxRendererNative::Get().DrawComboBoxDropButton(this,
1456 pBmp
= &m_bmpDisabled
;
1457 else if ( m_btnState
& wxCONTROL_PRESSED
)
1458 pBmp
= &m_bmpPressed
;
1459 else if ( m_btnState
& wxCONTROL_CURRENT
)
1462 pBmp
= &m_bmpNormal
;
1464 if ( m_blankButtonBg
)
1466 // If using blank button background, we need to clear its background
1467 // with button face colour instead of colour for rest of the control.
1468 if ( flags
& Button_PaintBackground
)
1470 wxColour bgCol
= GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1471 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1474 dc
.DrawRectangle(rect
);
1477 if ( !(flags
& Button_BitmapOnly
) )
1479 wxRendererNative::Get().DrawPushButton(this,
1488 // Need to clear button background even if m_btn is present
1489 // (assume non-button background was cleared just before this call so brushes are good)
1490 if ( flags
& Button_PaintBackground
)
1491 dc
.DrawRectangle(rect
);
1494 // Draw bitmap centered in drawRect
1495 dc
.DrawBitmap(*pBmp
,
1496 drawRect
.x
+ (drawRect
.width
-pBmp
->GetWidth())/2,
1497 drawRect
.y
+ (drawRect
.height
-pBmp
->GetHeight())/2,
1502 void wxComboCtrlBase::RecalcAndRefresh()
1506 wxSizeEvent
evt(GetSize(),GetId());
1507 GetEventHandler()->ProcessEvent(evt
);
1512 // ----------------------------------------------------------------------------
1513 // miscellaneous event handlers
1514 // ----------------------------------------------------------------------------
1516 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent
& event
)
1518 if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1520 if ( m_ignoreEvtText
> 0 )
1527 // Change event id, object and string before relaying it forward
1528 event
.SetId(GetId());
1529 wxString s
= event
.GetString();
1530 event
.SetEventObject(this);
1535 // call if cursor is on button area or mouse is captured for the button
1536 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent
& event
,
1539 int type
= event
.GetEventType();
1541 if ( type
== wxEVT_MOTION
)
1543 if ( (flags
& wxCC_MF_ON_BUTTON
) &&
1544 IsPopupWindowState(Hidden
) )
1546 if ( !(m_btnState
& wxCONTROL_CURRENT
) )
1548 // Mouse hover begins
1549 m_btnState
|= wxCONTROL_CURRENT
;
1550 if ( HasCapture() ) // Retain pressed state.
1551 m_btnState
|= wxCONTROL_PRESSED
;
1555 else if ( (m_btnState
& wxCONTROL_CURRENT
) )
1558 m_btnState
&= ~(wxCONTROL_CURRENT
|wxCONTROL_PRESSED
);
1562 else if ( type
== wxEVT_LEFT_DOWN
|| type
== wxEVT_LEFT_DCLICK
)
1564 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1566 m_btnState
|= wxCONTROL_PRESSED
;
1569 if ( !(m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
) )
1572 // If showing popup now, do not capture mouse or there will be interference
1576 else if ( type
== wxEVT_LEFT_UP
)
1579 // Only accept event if mouse was left-press was previously accepted
1583 if ( m_btnState
& wxCONTROL_PRESSED
)
1585 // If mouse was inside, fire the click event.
1586 if ( m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
)
1588 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1592 m_btnState
&= ~(wxCONTROL_PRESSED
);
1596 else if ( type
== wxEVT_LEAVE_WINDOW
)
1598 if ( m_btnState
& (wxCONTROL_CURRENT
|wxCONTROL_PRESSED
) )
1600 m_btnState
&= ~(wxCONTROL_CURRENT
);
1603 if ( IsPopupWindowState(Hidden
) )
1605 m_btnState
&= ~(wxCONTROL_PRESSED
);
1613 // Never have 'hot' state when popup is being shown
1614 // (this is mostly needed because of the animation).
1615 if ( !IsPopupWindowState(Hidden
) )
1616 m_btnState
&= ~wxCONTROL_CURRENT
;
1621 // returns true if event was consumed or filtered
1622 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent
& event
,
1623 int WXUNUSED(flags
) )
1625 wxLongLong t
= ::wxGetLocalTimeMillis();
1626 int evtType
= event
.GetEventType();
1628 #if USES_WXPOPUPWINDOW || USES_WXDIALOG
1629 if ( m_popupWinType
!= POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1631 if ( IsPopupWindowState(Visible
) &&
1632 ( evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_RIGHT_DOWN
) )
1640 // Filter out clicks on button immediately after popup dismiss
1641 if ( evtType
== wxEVT_LEFT_DOWN
&& t
< m_timeCanAcceptClick
)
1643 event
.SetEventType(0);
1650 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent
& event
)
1652 int evtType
= event
.GetEventType();
1654 if ( (evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_LEFT_DCLICK
) &&
1655 (m_windowStyle
& wxCB_READONLY
) )
1657 if ( GetPopupWindowState() >= Animating
)
1659 #if USES_WXPOPUPWINDOW
1660 // Click here always hides the popup.
1661 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1667 if ( !(m_windowStyle
& wxCC_SPECIAL_DCLICK
) )
1669 // In read-only mode, clicking the text is the
1670 // same as clicking the button.
1673 else if ( /*evtType == wxEVT_LEFT_UP || */evtType
== wxEVT_LEFT_DCLICK
)
1675 //if ( m_popupInterface->CycleValue() )
1677 if ( m_popupInterface
)
1678 m_popupInterface
->OnComboDoubleClick();
1683 if ( IsPopupShown() )
1685 // relay (some) mouse events to the popup
1686 if ( evtType
== wxEVT_MOUSEWHEEL
)
1687 m_popup
->GetEventHandler()->AddPendingEvent(event
);
1693 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent
& event
)
1695 if ( IsPopupShown() )
1697 // pass it to the popped up control
1698 GetPopupControl()->GetControl()->GetEventHandler()->AddPendingEvent(event
);
1702 if ( GetParent()->HasFlag(wxTAB_TRAVERSAL
) &&
1703 HandleAsNavigationKey(event
) )
1706 if ( IsKeyPopupToggle(event
) )
1712 int comboStyle
= GetWindowStyle();
1713 wxComboPopup
* popupInterface
= GetPopupControl();
1715 if ( !popupInterface
)
1721 int keycode
= event
.GetKeyCode();
1723 if ( (comboStyle
& wxCB_READONLY
) ||
1724 (keycode
!= WXK_RIGHT
&& keycode
!= WXK_LEFT
) )
1726 popupInterface
->OnComboKeyEvent(event
);
1733 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent
& event
)
1735 if ( event
.GetEventType() == wxEVT_SET_FOCUS
)
1737 wxWindow
* tc
= GetTextCtrl();
1738 if ( tc
&& tc
!= DoFindFocus() )
1740 m_resetFocus
= true;
1751 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent
& WXUNUSED(event
) )
1755 m_resetFocus
= false;
1756 wxWindow
* tc
= GetTextCtrl();
1762 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
1765 // left margin may also have changed
1766 if ( !(m_iFlags
& wxCC_IFLAG_LEFT_MARGIN_SET
) )
1767 m_marginLeft
= GetNativeTextIndent();
1771 // ----------------------------------------------------------------------------
1773 // ----------------------------------------------------------------------------
1775 // Create popup window and the child control
1776 void wxComboCtrlBase::CreatePopup()
1778 wxComboPopup
* popupInterface
= m_popupInterface
;
1783 #ifdef wxComboPopupWindowBase2
1784 if ( m_iFlags
& wxCC_IFLAG_USE_ALT_POPUP
)
1787 m_winPopup
= new wxComboPopupWindowBase2( this, wxNO_BORDER
);
1789 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
, wxEmptyString
,
1790 wxPoint(-21,-21), wxSize(20, 20),
1793 m_popupWinType
= SECONDARY_POPUP_TYPE
;
1796 #endif // wxComboPopupWindowBase2
1798 m_winPopup
= new wxComboPopupWindow( this, wxNO_BORDER
);
1799 m_popupWinType
= PRIMARY_POPUP_TYPE
;
1801 m_popupWinEvtHandler
= new wxComboPopupWindowEvtHandler(this);
1802 m_winPopup
->PushEventHandler(m_popupWinEvtHandler
);
1805 popupInterface
->Create(m_winPopup
);
1806 m_popup
= popup
= popupInterface
->GetControl();
1808 m_popupExtraHandler
= new wxComboPopupExtraEventHandler(this);
1809 popup
->PushEventHandler( m_popupExtraHandler
);
1811 // This may be helpful on some platforms
1812 // (eg. it bypasses a wxGTK popupwindow bug where
1813 // window is not initially hidden when it should be)
1816 popupInterface
->m_iFlags
|= wxCP_IFLAG_CREATED
;
1819 // Destroy popup window and the child control
1820 void wxComboCtrlBase::DestroyPopup()
1825 m_popup
->RemoveEventHandler(m_popupExtraHandler
);
1827 delete m_popupExtraHandler
;
1829 delete m_popupInterface
;
1833 m_winPopup
->RemoveEventHandler(m_popupWinEvtHandler
);
1834 delete m_popupWinEvtHandler
;
1835 m_popupWinEvtHandler
= NULL
;
1836 m_winPopup
->Destroy();
1839 m_popupExtraHandler
= NULL
;
1840 m_popupInterface
= NULL
;
1845 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup
* iface
)
1847 wxCHECK_RET( iface
, wxT("no popup interface set for wxComboCtrl") );
1851 iface
->InitBase(this);
1854 m_popupInterface
= iface
;
1856 if ( !iface
->LazyCreate() )
1865 // This must be done after creation
1866 if ( m_valueString
.length() )
1868 iface
->SetStringValue(m_valueString
);
1873 // Ensures there is atleast the default popup
1874 void wxComboCtrlBase::EnsurePopupControl()
1876 if ( !m_popupInterface
)
1877 SetPopupControl(NULL
);
1880 void wxComboCtrlBase::OnButtonClick()
1882 // Derived classes can override this method for totally custom
1884 if ( !IsPopupWindowState(Visible
) )
1890 void wxComboCtrlBase::ShowPopup()
1892 EnsurePopupControl();
1893 wxCHECK_RET( !IsPopupWindowState(Visible
), wxT("popup window already shown") );
1895 if ( IsPopupWindowState(Animating
) )
1900 // Space above and below
1906 wxSize ctrlSz
= GetSize();
1908 screenHeight
= wxSystemSettings::GetMetric( wxSYS_SCREEN_Y
);
1909 scrPos
= GetParent()->ClientToScreen(GetPosition());
1911 spaceAbove
= scrPos
.y
;
1912 spaceBelow
= screenHeight
- spaceAbove
- ctrlSz
.y
;
1914 maxHeightPopup
= spaceBelow
;
1915 if ( spaceAbove
> spaceBelow
)
1916 maxHeightPopup
= spaceAbove
;
1919 int widthPopup
= ctrlSz
.x
+ m_extLeft
+ m_extRight
;
1921 if ( widthPopup
< m_widthMinPopup
)
1922 widthPopup
= m_widthMinPopup
;
1924 wxWindow
* winPopup
= m_winPopup
;
1927 // Need to disable tab traversal of parent
1929 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1930 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1931 // that if transient popup is open, then tab traversal is to be ignored.
1932 // However, I think this code would still be needed for cases where
1933 // transient popup doesn't work yet (wxWinCE?).
1934 wxWindow
* parent
= GetParent();
1935 int parentFlags
= parent
->GetWindowStyle();
1936 if ( parentFlags
& wxTAB_TRAVERSAL
)
1938 parent
->SetWindowStyle( parentFlags
& ~(wxTAB_TRAVERSAL
) );
1939 m_iFlags
|= wxCC_IFLAG_PARENT_TAB_TRAVERSAL
;
1945 winPopup
= m_winPopup
;
1955 wxASSERT( !m_popup
|| m_popup
== popup
); // Consistency check.
1957 wxSize adjustedSize
= m_popupInterface
->GetAdjustedSize(widthPopup
,
1958 m_heightPopup
<=0?DEFAULT_POPUP_HEIGHT
:m_heightPopup
,
1961 popup
->SetSize(adjustedSize
);
1963 m_popupInterface
->OnPopup();
1966 // Reposition and resize popup window
1969 wxSize szp
= popup
->GetSize();
1972 int popupY
= scrPos
.y
+ ctrlSz
.y
;
1974 // Default anchor is wxLEFT
1975 int anchorSide
= m_anchorSide
;
1977 anchorSide
= wxLEFT
;
1979 int rightX
= scrPos
.x
+ ctrlSz
.x
+ m_extRight
- szp
.x
;
1980 int leftX
= scrPos
.x
- m_extLeft
;
1982 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
1985 int screenWidth
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
);
1987 // If there is not enough horizontal space, anchor on the other side.
1988 // If there is no space even then, place the popup at x 0.
1989 if ( anchorSide
== wxRIGHT
)
1993 if ( (leftX
+szp
.x
) < screenWidth
)
1994 anchorSide
= wxLEFT
;
2001 if ( (leftX
+szp
.x
) >= screenWidth
)
2004 anchorSide
= wxRIGHT
;
2010 // Select x coordinate according to the anchor side
2011 if ( anchorSide
== wxRIGHT
)
2013 else if ( anchorSide
== wxLEFT
)
2018 int showFlags
= CanDeferShow
;
2020 if ( spaceBelow
< szp
.y
)
2022 popupY
= scrPos
.y
- szp
.y
;
2023 showFlags
|= ShowAbove
;
2026 #if INSTALL_TOPLEV_HANDLER
2027 // Put top level window event handler into place
2028 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
2030 if ( !m_toplevEvtHandler
)
2031 m_toplevEvtHandler
= new wxComboFrameEventHandler(this);
2033 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2035 ((wxComboFrameEventHandler
*)m_toplevEvtHandler
)->OnPopup();
2036 toplev
->PushEventHandler( m_toplevEvtHandler
);
2040 // Set string selection (must be this way instead of SetStringSelection)
2043 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2044 m_text
->SelectAll();
2046 m_popupInterface
->SetStringValue( m_text
->GetValue() );
2050 // This is neede since focus/selection indication may change when popup is shown
2054 // This must be after SetStringValue
2055 m_popupWinState
= Animating
;
2057 wxRect
popupWinRect( popupX
, popupY
, szp
.x
, szp
.y
);
2060 if ( (m_iFlags
& wxCC_IFLAG_DISABLE_POPUP_ANIM
) ||
2061 AnimateShow( popupWinRect
, showFlags
) )
2063 DoShowPopup( popupWinRect
, showFlags
);
2067 bool wxComboCtrlBase::AnimateShow( const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
) )
2072 void wxComboCtrlBase::DoShowPopup( const wxRect
& rect
, int WXUNUSED(flags
) )
2074 wxWindow
* winPopup
= m_winPopup
;
2076 if ( IsPopupWindowState(Animating
) )
2078 // Make sure the popup window is shown in the right position.
2079 // Should not matter even if animation already did this.
2081 // Some platforms (GTK) may like SetSize and Move to be separate
2082 // (though the bug was probably fixed).
2083 winPopup
->SetSize( rect
);
2085 #if USES_WXPOPUPTRANSIENTWINDOW
2086 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2087 ((wxPopupTransientWindow
*)winPopup
)->Popup(m_popup
);
2092 m_popupWinState
= Visible
;
2094 else if ( IsPopupWindowState(Hidden
) )
2096 // Animation was aborted
2098 wxASSERT( !winPopup
->IsShown() );
2100 m_popupWinState
= Hidden
;
2106 void wxComboCtrlBase::OnPopupDismiss()
2108 // Just in case, avoid double dismiss
2109 if ( IsPopupWindowState(Hidden
) )
2112 // This must be set before focus - otherwise there will be recursive
2113 // OnPopupDismisses.
2114 m_popupWinState
= Hidden
;
2117 m_winPopup
->Disable();
2119 // Inform popup control itself
2120 m_popupInterface
->OnDismiss();
2122 if ( m_popupExtraHandler
)
2123 ((wxComboPopupExtraEventHandler
*)m_popupExtraHandler
)->OnPopupDismiss();
2125 #if INSTALL_TOPLEV_HANDLER
2126 // Remove top level window event handler
2127 if ( m_toplevEvtHandler
)
2129 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2131 toplev
->RemoveEventHandler( m_toplevEvtHandler
);
2135 m_timeCanAcceptClick
= ::wxGetLocalTimeMillis();
2137 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2138 m_timeCanAcceptClick
+= 150;
2140 // If cursor not on dropdown button, then clear its state
2141 // (technically not required by all ports, but do it for all just in case)
2142 if ( !m_btnArea
.Contains(ScreenToClient(::wxGetMousePosition())) )
2145 // Return parent's tab traversal flag.
2146 // See ShowPopup for notes.
2147 if ( m_iFlags
& wxCC_IFLAG_PARENT_TAB_TRAVERSAL
)
2149 wxWindow
* parent
= GetParent();
2150 parent
->SetWindowStyle( parent
->GetWindowStyle() | wxTAB_TRAVERSAL
);
2151 m_iFlags
&= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL
);
2154 // refresh control (necessary even if m_text)
2160 void wxComboCtrlBase::HidePopup()
2162 // Should be able to call this without popup interface
2163 if ( IsPopupWindowState(Hidden
) )
2166 // transfer value and show it in textctrl, if any
2167 if ( !IsPopupWindowState(Animating
) )
2168 SetValue( m_popupInterface
->GetStringValue() );
2175 // ----------------------------------------------------------------------------
2176 // customization methods
2177 // ----------------------------------------------------------------------------
2179 void wxComboCtrlBase::SetButtonPosition( int width
, int height
,
2180 int side
, int spacingX
)
2185 m_btnSpacingX
= spacingX
;
2190 wxSize
wxComboCtrlBase::GetButtonSize()
2192 if ( m_btnSize
.x
> 0 )
2195 wxSize
retSize(m_btnWid
,m_btnHei
);
2197 // Need to call CalculateAreas now if button size is
2198 // is not explicitly specified.
2199 if ( retSize
.x
<= 0 || retSize
.y
<= 0)
2203 retSize
= m_btnSize
;
2209 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap
& bmpNormal
,
2211 const wxBitmap
& bmpPressed
,
2212 const wxBitmap
& bmpHover
,
2213 const wxBitmap
& bmpDisabled
)
2215 m_bmpNormal
= bmpNormal
;
2216 m_blankButtonBg
= blankButtonBg
;
2218 if ( bmpPressed
.Ok() )
2219 m_bmpPressed
= bmpPressed
;
2221 m_bmpPressed
= bmpNormal
;
2223 if ( bmpHover
.Ok() )
2224 m_bmpHover
= bmpHover
;
2226 m_bmpHover
= bmpNormal
;
2228 if ( bmpDisabled
.Ok() )
2229 m_bmpDisabled
= bmpDisabled
;
2231 m_bmpDisabled
= bmpNormal
;
2236 void wxComboCtrlBase::SetCustomPaintWidth( int width
)
2240 // move textctrl accordingly
2241 wxRect r
= m_text
->GetRect();
2242 int inc
= width
- m_widthCustomPaint
;
2245 m_text
->SetSize( r
);
2248 m_widthCustomPaint
= width
;
2253 bool wxComboCtrlBase::DoSetMargins(const wxPoint
& margins
)
2255 // For general sanity's sake, we ignore top margin. Instead
2256 // we will always try to center the text vertically.
2259 if ( margins
.x
!= -1 )
2261 m_marginLeft
= margins
.x
;
2262 m_iFlags
|= wxCC_IFLAG_LEFT_MARGIN_SET
;
2266 m_marginLeft
= GetNativeTextIndent();
2267 m_iFlags
&= ~(wxCC_IFLAG_LEFT_MARGIN_SET
);
2270 if ( margins
.y
!= -1 )
2280 wxPoint
wxComboCtrlBase::DoGetMargins() const
2282 return wxPoint(m_marginLeft
, -1);
2285 #if WXWIN_COMPATIBILITY_2_6
2286 void wxComboCtrlBase::SetTextIndent( int indent
)
2290 m_marginLeft
= GetNativeTextIndent();
2291 m_iFlags
&= ~(wxCC_IFLAG_LEFT_MARGIN_SET
);
2295 m_marginLeft
= indent
;
2296 m_iFlags
|= wxCC_IFLAG_LEFT_MARGIN_SET
;
2302 wxCoord
wxComboCtrlBase::GetTextIndent() const;
2304 return m_marginLeft
;
2308 wxCoord
wxComboCtrlBase::GetNativeTextIndent() const
2310 return DEFAULT_TEXT_INDENT
;
2313 // ----------------------------------------------------------------------------
2314 // methods forwarded to wxTextCtrl
2315 // ----------------------------------------------------------------------------
2317 wxString
wxComboCtrlBase::GetValue() const
2320 return m_text
->GetValue();
2321 return m_valueString
;
2324 void wxComboCtrlBase::SetValueWithEvent(const wxString
& value
, bool withEvent
)
2331 m_text
->SetValue(value
);
2333 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2334 m_text
->SelectAll();
2337 // Since wxComboPopup may want to paint the combo as well, we need
2338 // to set the string value here (as well as sometimes in ShowPopup).
2339 if ( m_valueString
!= value
)
2341 m_valueString
= value
;
2343 EnsurePopupControl();
2345 if (m_popupInterface
)
2346 m_popupInterface
->SetStringValue(value
);
2352 void wxComboCtrlBase::SetValue(const wxString
& value
)
2354 SetValueWithEvent(value
, false);
2357 // In this SetValue variant wxComboPopup::SetStringValue is not called
2358 void wxComboCtrlBase::SetText(const wxString
& value
)
2360 // Unlike in SetValue(), this must be called here or
2361 // the behaviour will no be consistent in readonlys.
2362 EnsurePopupControl();
2364 m_valueString
= value
;
2369 m_text
->SetValue( value
);
2375 void wxComboCtrlBase::Copy()
2381 void wxComboCtrlBase::Cut()
2387 void wxComboCtrlBase::Paste()
2393 void wxComboCtrlBase::SetInsertionPoint(long pos
)
2396 m_text
->SetInsertionPoint(pos
);
2399 void wxComboCtrlBase::SetInsertionPointEnd()
2402 m_text
->SetInsertionPointEnd();
2405 long wxComboCtrlBase::GetInsertionPoint() const
2408 return m_text
->GetInsertionPoint();
2413 long wxComboCtrlBase::GetLastPosition() const
2416 return m_text
->GetLastPosition();
2421 void wxComboCtrlBase::Replace(long from
, long to
, const wxString
& value
)
2424 m_text
->Replace(from
, to
, value
);
2427 void wxComboCtrlBase::Remove(long from
, long to
)
2430 m_text
->Remove(from
, to
);
2433 void wxComboCtrlBase::SetSelection(long from
, long to
)
2436 m_text
->SetSelection(from
, to
);
2439 void wxComboCtrlBase::Undo()
2445 #endif // wxUSE_COMBOCTRL