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 #include "wx/gtk/private.h"
76 // NB: It is not recommended to use wxDialog as popup on wxGTK, because of
77 // this bug: If wxDialog is hidden, its position becomes corrupt
78 // between hide and next show, but without internal coordinates being
79 // reflected (or something like that - atleast commenting out ->Hide()
80 // seemed to eliminate the position change).
82 // NB: Let's not be afraid to use wxGTK's wxPopupTransientWindow as a
83 // 'perfect' popup, as it can succesfully host child controls even in
84 // popups that are shown in modal dialogs.
86 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
87 #define TRANSIENT_POPUPWIN_IS_PERFECT 1 // wxPopupTransientWindow works, its child can have focus, and common
88 // native controls work on it like normal.
89 #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
90 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
91 #define FOCUS_RING 0 // No focus ring on wxGTK
93 #elif defined(__WXMAC__)
95 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
96 #define TRANSIENT_POPUPWIN_IS_PERFECT 1 // wxPopupTransientWindow works, its child can have focus, and common
97 // native controls work on it like normal.
98 #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
99 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
100 #define FOCUS_RING 3 // Reserve room for the textctrl's focus ring to display
102 #undef DEFAULT_DROPBUTTON_WIDTH
103 #define DEFAULT_DROPBUTTON_WIDTH 22
105 #define COMBO_MARGIN FOCUS_RING
109 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
110 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
111 // native controls work on it like normal.
112 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
113 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
119 // Popupwin is really only supported on wxMSW (not WINCE) and wxGTK, regardless
120 // what the wxUSE_POPUPWIN says.
121 // FIXME: Why isn't wxUSE_POPUPWIN reliable any longer? (it was in wxW2.6.2)
122 #if (!defined(__WXMSW__) && !defined(__WXGTK__) && !defined(__WXMAC__)) || defined(__WXWINCE__)
123 #undef wxUSE_POPUPWIN
124 #define wxUSE_POPUPWIN 0
129 #include "wx/popupwin.h"
131 #undef USE_TRANSIENT_POPUP
132 #define USE_TRANSIENT_POPUP 0
136 // Define different types of popup windows
140 POPUPWIN_WXPOPUPTRANSIENTWINDOW
= 1,
141 POPUPWIN_WXPOPUPWINDOW
= 2,
142 POPUPWIN_WXDIALOG
= 3
146 #if USE_TRANSIENT_POPUP
147 // wxPopupTransientWindow is implemented
149 #define wxComboPopupWindowBase wxPopupTransientWindow
150 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPTRANSIENTWINDOW
151 #define USES_WXPOPUPTRANSIENTWINDOW 1
153 #if TRANSIENT_POPUPWIN_IS_PERFECT
155 #elif POPUPWIN_IS_PERFECT
156 #define wxComboPopupWindowBase2 wxPopupWindow
157 #define SECONDARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
158 #define USES_WXPOPUPWINDOW 1
160 #define wxComboPopupWindowBase2 wxDialog
161 #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG
162 #define USES_WXDIALOG 1
166 // wxPopupWindow (but not wxPopupTransientWindow) is properly implemented
168 #define wxComboPopupWindowBase wxPopupWindow
169 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
170 #define USES_WXPOPUPWINDOW 1
172 #if !POPUPWIN_IS_PERFECT
173 #define wxComboPopupWindowBase2 wxDialog
174 #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG
175 #define USES_WXDIALOG 1
179 // wxPopupWindow is not implemented
181 #define wxComboPopupWindowBase wxDialog
182 #define PRIMARY_POPUP_TYPE POPUPWIN_WXDIALOG
183 #define USES_WXDIALOG 1
188 #ifndef USES_WXPOPUPTRANSIENTWINDOW
189 #define USES_WXPOPUPTRANSIENTWINDOW 0
192 #ifndef USES_WXPOPUPWINDOW
193 #define USES_WXPOPUPWINDOW 0
196 #ifndef USES_WXDIALOG
197 #define USES_WXDIALOG 0
201 #if USES_WXPOPUPWINDOW
202 #define INSTALL_TOPLEV_HANDLER 1
204 #define INSTALL_TOPLEV_HANDLER 0
210 // * wxComboPopupWindow for external use (ie. replace old wxUniv wxPopupComboWindow)
214 // ----------------------------------------------------------------------------
215 // wxComboFrameEventHandler takes care of hiding the popup when events happen
216 // in its top level parent.
217 // ----------------------------------------------------------------------------
219 #if INSTALL_TOPLEV_HANDLER
222 // This will no longer be necessary after wxTransientPopupWindow
223 // works well on all platforms.
226 class wxComboFrameEventHandler
: public wxEvtHandler
229 wxComboFrameEventHandler( wxComboCtrlBase
* pCb
);
230 virtual ~wxComboFrameEventHandler();
234 void OnIdle( wxIdleEvent
& event
);
235 void OnMouseEvent( wxMouseEvent
& event
);
236 void OnActivate( wxActivateEvent
& event
);
237 void OnResize( wxSizeEvent
& event
);
238 void OnMove( wxMoveEvent
& event
);
239 void OnMenuEvent( wxMenuEvent
& event
);
240 void OnClose( wxCloseEvent
& event
);
243 wxWindow
* m_focusStart
;
244 wxComboCtrlBase
* m_combo
;
247 DECLARE_EVENT_TABLE()
250 BEGIN_EVENT_TABLE(wxComboFrameEventHandler
, wxEvtHandler
)
251 EVT_IDLE(wxComboFrameEventHandler::OnIdle
)
252 EVT_LEFT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
253 EVT_RIGHT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
254 EVT_SIZE(wxComboFrameEventHandler::OnResize
)
255 EVT_MOVE(wxComboFrameEventHandler::OnMove
)
256 EVT_MENU_HIGHLIGHT(wxID_ANY
,wxComboFrameEventHandler::OnMenuEvent
)
257 EVT_MENU_OPEN(wxComboFrameEventHandler::OnMenuEvent
)
258 EVT_ACTIVATE(wxComboFrameEventHandler::OnActivate
)
259 EVT_CLOSE(wxComboFrameEventHandler::OnClose
)
262 wxComboFrameEventHandler::wxComboFrameEventHandler( wxComboCtrlBase
* combo
)
268 wxComboFrameEventHandler::~wxComboFrameEventHandler()
272 void wxComboFrameEventHandler::OnPopup()
274 m_focusStart
= ::wxWindow::FindFocus();
277 void wxComboFrameEventHandler::OnIdle( wxIdleEvent
& event
)
279 wxWindow
* winFocused
= ::wxWindow::FindFocus();
281 wxWindow
* popup
= m_combo
->GetPopupControl()->GetControl();
282 wxWindow
* winpopup
= m_combo
->GetPopupWindow();
285 winFocused
!= m_focusStart
&&
286 winFocused
!= popup
&&
287 winFocused
->GetParent() != popup
&&
288 winFocused
!= winpopup
&&
289 winFocused
->GetParent() != winpopup
&&
290 winFocused
!= m_combo
&&
291 winFocused
!= m_combo
->GetButton() // GTK (atleast) requires this
294 m_combo
->HidePopup();
300 void wxComboFrameEventHandler::OnMenuEvent( wxMenuEvent
& event
)
302 m_combo
->HidePopup();
306 void wxComboFrameEventHandler::OnMouseEvent( wxMouseEvent
& event
)
308 m_combo
->HidePopup();
312 void wxComboFrameEventHandler::OnClose( wxCloseEvent
& event
)
314 m_combo
->HidePopup();
318 void wxComboFrameEventHandler::OnActivate( wxActivateEvent
& event
)
320 m_combo
->HidePopup();
324 void wxComboFrameEventHandler::OnResize( wxSizeEvent
& event
)
326 m_combo
->HidePopup();
330 void wxComboFrameEventHandler::OnMove( wxMoveEvent
& event
)
332 m_combo
->HidePopup();
336 #endif // INSTALL_TOPLEV_HANDLER
338 // ----------------------------------------------------------------------------
339 // wxComboPopupWindow is, in essence, wxPopupWindow customized for
341 // ----------------------------------------------------------------------------
343 class wxComboPopupWindow
: public wxComboPopupWindowBase
347 wxComboPopupWindow( wxComboCtrlBase
*parent
,
349 #if USES_WXPOPUPWINDOW || USES_WXPOPUPTRANSIENTWINDOW
350 : wxComboPopupWindowBase(parent
,style
)
352 : wxComboPopupWindowBase(parent
,
363 #if USES_WXPOPUPTRANSIENTWINDOW
364 virtual bool Show( bool show
);
365 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
367 virtual void OnDismiss();
375 #if USES_WXPOPUPTRANSIENTWINDOW
376 bool wxComboPopupWindow::Show( bool show
)
378 // Guard against recursion
380 return wxComboPopupWindowBase::Show(show
);
384 wxASSERT( IsKindOf(CLASSINFO(wxPopupTransientWindow
)) );
386 wxPopupTransientWindow
* ptw
= (wxPopupTransientWindow
*) this;
388 if ( show
!= ptw
->IsShown() )
391 // We used to do wxPopupTransientWindow::Popup here,
392 // but this would hide normal Show, which we are
393 // also going to need.
404 bool wxComboPopupWindow::ProcessLeftDown(wxMouseEvent
& event
)
406 return wxPopupTransientWindow::ProcessLeftDown(event
);
409 // First thing that happens when a transient popup closes is that this method gets called.
410 void wxComboPopupWindow::OnDismiss()
412 wxComboCtrlBase
* combo
= (wxComboCtrlBase
*) GetParent();
413 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxComboCtrlBase
)),
414 wxT("parent might not be wxComboCtrl, but check IMPLEMENT_DYNAMIC_CLASS(2) macro for correctness") );
416 combo
->OnPopupDismiss();
418 #endif // USES_WXPOPUPTRANSIENTWINDOW
421 // ----------------------------------------------------------------------------
422 // wxComboPopupWindowEvtHandler does bulk of the custom event handling
423 // of a popup window. It is separate so we can have different types
425 // ----------------------------------------------------------------------------
427 class wxComboPopupWindowEvtHandler
: public wxEvtHandler
431 wxComboPopupWindowEvtHandler( wxComboCtrlBase
*parent
)
436 void OnSizeEvent( wxSizeEvent
& event
);
437 void OnKeyEvent(wxKeyEvent
& event
);
439 void OnActivate( wxActivateEvent
& event
);
443 wxComboCtrlBase
* m_combo
;
445 DECLARE_EVENT_TABLE()
449 BEGIN_EVENT_TABLE(wxComboPopupWindowEvtHandler
, wxEvtHandler
)
450 EVT_KEY_DOWN(wxComboPopupWindowEvtHandler::OnKeyEvent
)
451 EVT_KEY_UP(wxComboPopupWindowEvtHandler::OnKeyEvent
)
453 EVT_ACTIVATE(wxComboPopupWindowEvtHandler::OnActivate
)
455 EVT_SIZE(wxComboPopupWindowEvtHandler::OnSizeEvent
)
459 void wxComboPopupWindowEvtHandler::OnSizeEvent( wxSizeEvent
& WXUNUSED(event
) )
461 // Block the event so that the popup control does not get auto-resized.
464 void wxComboPopupWindowEvtHandler::OnKeyEvent( wxKeyEvent
& event
)
466 // Relay keyboard event to the main child controls
467 wxWindowList children
= m_combo
->GetPopupWindow()->GetChildren();
468 wxWindowList::iterator node
= children
.begin();
469 wxWindow
* child
= (wxWindow
*)*node
;
470 child
->GetEventHandler()->AddPendingEvent(event
);
474 void wxComboPopupWindowEvtHandler::OnActivate( wxActivateEvent
& event
)
476 if ( !event
.GetActive() )
478 // Tell combo control that we are dismissed.
479 m_combo
->HidePopup();
487 // ----------------------------------------------------------------------------
490 // ----------------------------------------------------------------------------
492 wxComboPopup::~wxComboPopup()
496 void wxComboPopup::OnPopup()
500 void wxComboPopup::OnDismiss()
504 wxComboCtrl
* wxComboPopup::GetComboCtrl() const
506 return wxStaticCast(m_combo
, wxComboCtrl
);
509 wxSize
wxComboPopup::GetAdjustedSize( int minWidth
,
511 int WXUNUSED(maxHeight
) )
513 return wxSize(minWidth
,prefHeight
);
516 void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase
* combo
,
517 wxDC
& dc
, const wxRect
& rect
)
519 if ( combo
->GetWindowStyle() & wxCB_READONLY
) // ie. no textctrl
521 combo
->PrepareBackground(dc
,rect
,0);
523 dc
.DrawText( combo
->GetValue(),
524 rect
.x
+ combo
->m_marginLeft
,
525 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
529 void wxComboPopup::PaintComboControl( wxDC
& dc
, const wxRect
& rect
)
531 DefaultPaintComboControl(m_combo
,dc
,rect
);
534 void wxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
539 void wxComboPopup::OnComboDoubleClick()
543 void wxComboPopup::SetStringValue( const wxString
& WXUNUSED(value
) )
547 bool wxComboPopup::LazyCreate()
552 void wxComboPopup::Dismiss()
554 m_combo
->HidePopup();
557 // ----------------------------------------------------------------------------
559 // ----------------------------------------------------------------------------
562 // This is pushed to the event handler queue of the child textctrl.
564 class wxComboBoxExtraInputHandler
: public wxEvtHandler
568 wxComboBoxExtraInputHandler( wxComboCtrlBase
* combo
)
573 virtual ~wxComboBoxExtraInputHandler() { }
574 void OnKey(wxKeyEvent
& event
);
575 void OnFocus(wxFocusEvent
& event
);
578 wxComboCtrlBase
* m_combo
;
581 DECLARE_EVENT_TABLE()
585 BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler
, wxEvtHandler
)
586 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey
)
587 EVT_KEY_UP(wxComboBoxExtraInputHandler::OnKey
)
588 EVT_CHAR(wxComboBoxExtraInputHandler::OnKey
)
589 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
590 EVT_KILL_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
594 void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent
& event
)
596 // Let the wxComboCtrl event handler have a go first.
597 wxComboCtrlBase
* combo
= m_combo
;
599 wxKeyEvent
redirectedEvent(event
);
600 redirectedEvent
.SetId(combo
->GetId());
601 redirectedEvent
.SetEventObject(combo
);
603 if ( !combo
->GetEventHandler()->ProcessEvent(redirectedEvent
) )
605 // Don't let TAB through to the text ctrl - looks ugly
606 if ( event
.GetKeyCode() != WXK_TAB
)
611 void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent
& event
)
613 // FIXME: This code does run when control is clicked,
614 // yet on Windows it doesn't select all the text.
615 if ( event
.GetEventType() == wxEVT_SET_FOCUS
&&
616 !(m_combo
->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT
) )
618 if ( m_combo
->GetTextCtrl() )
619 m_combo
->GetTextCtrl()->SelectAll();
621 m_combo
->SetSelection(-1,-1);
624 // Send focus indication to parent.
625 // NB: This is needed for cases where the textctrl gets focus
626 // instead of its parent. While this may trigger multiple
627 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
628 // from combo's focus event handler), they should be quite
630 wxFocusEvent
evt2(event
.GetEventType(),m_combo
->GetId());
631 evt2
.SetEventObject(m_combo
);
632 m_combo
->GetEventHandler()->ProcessEvent(evt2
);
639 // This is pushed to the event handler queue of the control in popup.
642 class wxComboPopupExtraEventHandler
: public wxEvtHandler
646 wxComboPopupExtraEventHandler( wxComboCtrlBase
* combo
)
650 m_beenInside
= false;
652 virtual ~wxComboPopupExtraEventHandler() { }
654 void OnMouseEvent( wxMouseEvent
& event
);
656 // Called from wxComboCtrlBase::OnPopupDismiss
657 void OnPopupDismiss()
659 m_beenInside
= false;
663 wxComboCtrlBase
* m_combo
;
668 DECLARE_EVENT_TABLE()
672 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler
, wxEvtHandler
)
673 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent
)
677 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent
& event
)
679 wxPoint pt
= event
.GetPosition();
680 wxSize sz
= m_combo
->GetPopupControl()->GetControl()->GetClientSize();
681 int evtType
= event
.GetEventType();
682 bool isInside
= pt
.x
>= 0 && pt
.y
>= 0 && pt
.x
< sz
.x
&& pt
.y
< sz
.y
;
683 bool relayToButton
= false;
687 if ( evtType
== wxEVT_MOTION
||
688 evtType
== wxEVT_LEFT_DOWN
||
689 evtType
== wxEVT_RIGHT_DOWN
)
691 // Block motion and click events outside the popup
692 if ( !isInside
|| !m_combo
->IsPopupShown() )
697 else if ( evtType
== wxEVT_LEFT_UP
)
699 if ( !m_combo
->IsPopupShown() )
702 relayToButton
= true;
704 else if ( !m_beenInside
)
712 relayToButton
= true;
720 // Some mouse events to popup that happen outside it, before cursor
721 // has been inside the popup, need to be ignored by it but relayed to
724 wxWindow
* eventSink
= m_combo
;
725 wxWindow
* btn
= m_combo
->GetButton();
729 eventSink
->GetEventHandler()->ProcessEvent(event
);
733 // ----------------------------------------------------------------------------
734 // wxComboCtrlTextCtrl
735 // ----------------------------------------------------------------------------
737 class wxComboCtrlTextCtrl
: public wxTextCtrl
740 wxComboCtrlTextCtrl() : wxTextCtrl() { }
741 virtual ~wxComboCtrlTextCtrl() { }
743 virtual wxWindow
*GetMainWindowOfCompositeControl()
745 wxComboCtrl
* combo
= (wxComboCtrl
*) GetParent();
747 // Returning this instead of just 'parent' lets FindFocus work
748 // correctly even when parent control is a child of a composite
749 // generic control (as is case with wxGenericDatePickerCtrl).
750 return combo
->GetMainWindowOfCompositeControl();
754 // ----------------------------------------------------------------------------
756 // ----------------------------------------------------------------------------
759 BEGIN_EVENT_TABLE(wxComboCtrlBase
, wxControl
)
760 EVT_TEXT(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
761 EVT_SIZE(wxComboCtrlBase::OnSizeEvent
)
762 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent
)
763 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent
)
764 EVT_IDLE(wxComboCtrlBase::OnIdleEvent
)
765 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
766 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent
)
767 EVT_TEXT_ENTER(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
768 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged
)
772 IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase
, wxControl
)
774 void wxComboCtrlBase::Init()
778 m_popupWinState
= Hidden
;
781 m_popupInterface
= NULL
;
783 m_popupExtraHandler
= NULL
;
784 m_textEvtHandler
= NULL
;
786 #if INSTALL_TOPLEV_HANDLER
787 m_toplevEvtHandler
= NULL
;
790 m_mainCtrlWnd
= this;
793 m_widthMinPopup
= -1;
795 m_widthCustomPaint
= 0;
796 m_widthCustomBorder
= 0;
800 m_blankButtonBg
= false;
802 m_popupWinType
= POPUPWIN_NONE
;
803 m_btnWid
= m_btnHei
= -1;
811 m_timeCanAcceptClick
= 0;
813 m_resetFocus
= false;
816 bool wxComboCtrlBase::Create(wxWindow
*parent
,
818 const wxString
& value
,
822 const wxValidator
& validator
,
823 const wxString
& name
)
825 if ( !wxControl::Create(parent
,
829 style
| wxWANTS_CHARS
,
834 m_valueString
= value
;
838 m_marginLeft
= GetNativeTextIndent();
840 m_iFlags
|= wxCC_IFLAG_CREATED
;
842 // If x and y indicate valid size, wxSizeEvent won't be
843 // emitted automatically, so we need to add artifical one.
844 if ( size
.x
> 0 && size
.y
> 0 )
846 wxSizeEvent
evt(size
,GetId());
847 GetEventHandler()->AddPendingEvent(evt
);
853 void wxComboCtrlBase::InstallInputHandlers()
857 m_textEvtHandler
= new wxComboBoxExtraInputHandler(this);
858 m_text
->PushEventHandler(m_textEvtHandler
);
863 wxComboCtrlBase::CreateTextCtrl(int style
, const wxValidator
& validator
)
865 if ( !(m_windowStyle
& wxCB_READONLY
) )
870 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
871 // not used by the wxPropertyGrid and therefore the tab is processed by
872 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
873 // navigation event is then sent to the wrong window.
874 style
|= wxTE_PROCESS_TAB
;
876 if ( HasFlag(wxTE_PROCESS_ENTER
) )
877 style
|= wxTE_PROCESS_ENTER
;
879 // Ignore EVT_TEXT generated by the constructor (but only
880 // if the event redirector already exists)
881 // NB: This must be " = 1" instead of "++";
882 if ( m_textEvtHandler
)
887 m_text
= new wxComboCtrlTextCtrl();
888 m_text
->Create(this, wxID_ANY
, m_valueString
,
889 wxDefaultPosition
, wxSize(10,-1),
894 void wxComboCtrlBase::OnThemeChange()
896 // Leave the default bg on the Mac so the area used by the focus ring will
897 // be the correct colour and themed brush. Instead we'll use
898 // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed.
900 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
904 wxComboCtrlBase::~wxComboCtrlBase()
909 #if INSTALL_TOPLEV_HANDLER
910 delete ((wxComboFrameEventHandler
*)m_toplevEvtHandler
);
911 m_toplevEvtHandler
= NULL
;
917 m_text
->RemoveEventHandler(m_textEvtHandler
);
919 delete m_textEvtHandler
;
923 // ----------------------------------------------------------------------------
925 // ----------------------------------------------------------------------------
927 // Recalculates button and textctrl areas
928 void wxComboCtrlBase::CalculateAreas( int btnWidth
)
930 wxSize sz
= GetClientSize();
931 int customBorder
= m_widthCustomBorder
;
932 int btnBorder
; // border for button only
934 // check if button should really be outside the border: we'll do it it if
935 // its platform default or bitmap+pushbutton background is used, but not if
936 // there is vertical size adjustment or horizontal spacing.
937 if ( ( (m_iFlags
& wxCC_BUTTON_OUTSIDE_BORDER
) ||
938 (m_bmpNormal
.Ok() && m_blankButtonBg
) ) &&
939 m_btnSpacingX
== 0 &&
942 m_iFlags
|= wxCC_IFLAG_BUTTON_OUTSIDE
;
945 else if ( (m_iFlags
& wxCC_BUTTON_COVERS_BORDER
) &&
946 m_btnSpacingX
== 0 && !m_bmpNormal
.Ok() )
948 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
953 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
954 btnBorder
= customBorder
;
957 // Defaul indentation
958 if ( m_marginLeft
< 0 )
959 m_marginLeft
= GetNativeTextIndent();
961 int butWidth
= btnWidth
;
964 butWidth
= m_btnWidDefault
;
966 m_btnWidDefault
= butWidth
;
971 int butHeight
= sz
.y
- btnBorder
*2;
973 // Adjust button width
978 // Adjust button width to match aspect ratio
979 // (but only if control is smaller than best size).
980 int bestHeight
= GetBestSize().y
;
981 int height
= GetSize().y
;
983 if ( height
< bestHeight
)
985 // Make very small buttons square, as it makes
986 // them accommodate arrow image better and still
989 butWidth
= (height
*butWidth
)/bestHeight
;
991 butWidth
= butHeight
;
995 // Adjust button height
997 butHeight
= m_btnHei
;
999 // Use size of normal bitmap if...
1002 // button width is set to default and blank button bg is not drawn
1003 if ( m_bmpNormal
.Ok() )
1005 int bmpReqWidth
= m_bmpNormal
.GetWidth();
1006 int bmpReqHeight
= m_bmpNormal
.GetHeight();
1008 // If drawing blank button background, we need to add some margin.
1009 if ( m_blankButtonBg
)
1011 bmpReqWidth
+= BMP_BUTTON_MARGIN
*2;
1012 bmpReqHeight
+= BMP_BUTTON_MARGIN
*2;
1015 if ( butWidth
< bmpReqWidth
|| ( m_btnWid
== 0 && !m_blankButtonBg
) )
1016 butWidth
= bmpReqWidth
;
1017 if ( butHeight
< bmpReqHeight
|| ( m_btnHei
== 0 && !m_blankButtonBg
) )
1018 butHeight
= bmpReqHeight
;
1020 // Need to fix height?
1021 if ( (sz
.y
-(customBorder
*2)) < butHeight
&& btnWidth
== 0 )
1023 int newY
= butHeight
+(customBorder
*2);
1024 SetClientSize(wxDefaultCoord
,newY
);
1025 if ( m_bmpNormal
.Ok() || m_btnArea
.width
!= butWidth
|| m_btnArea
.height
!= butHeight
)
1026 m_iFlags
|= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1028 m_iFlags
&= ~wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1034 int butAreaWid
= butWidth
+ (m_btnSpacingX
*2);
1036 m_btnSize
.x
= butWidth
;
1037 m_btnSize
.y
= butHeight
;
1039 m_btnArea
.x
= ( m_btnSide
==wxRIGHT
? sz
.x
- butAreaWid
- btnBorder
: btnBorder
);
1040 m_btnArea
.y
= btnBorder
+ FOCUS_RING
;
1041 m_btnArea
.width
= butAreaWid
;
1042 m_btnArea
.height
= sz
.y
- ((btnBorder
+FOCUS_RING
)*2);
1044 m_tcArea
.x
= ( m_btnSide
==wxRIGHT
? 0 : butAreaWid
) + customBorder
+ FOCUS_RING
;
1045 m_tcArea
.y
= customBorder
+ FOCUS_RING
;
1046 m_tcArea
.width
= sz
.x
- butAreaWid
- (customBorder
*2) - (FOCUS_RING
*2);
1047 m_tcArea
.height
= sz
.y
- ((customBorder
+FOCUS_RING
)*2);
1052 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
1053 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1058 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
)
1063 wxSize sz
= GetClientSize();
1065 int customBorder
= m_widthCustomBorder
;
1066 if ( (m_text
->GetWindowStyleFlag() & wxBORDER_MASK
) == wxNO_BORDER
)
1070 if ( !m_widthCustomPaint
)
1072 // No special custom paint area - we can use 0 left margin
1074 if ( m_text
->SetMargins(0) )
1075 textCtrlXAdjust
= 0;
1076 x
= m_tcArea
.x
+ m_marginLeft
+ textCtrlXAdjust
;
1080 // There is special custom paint area - it is better to
1081 // use some margin with the wxTextCtrl.
1082 m_text
->SetMargins(m_marginLeft
);
1083 x
= m_tcArea
.x
+ m_widthCustomPaint
+
1084 m_marginLeft
+ textCtrlXAdjust
;
1087 // Centre textctrl vertically, if needed
1088 #if !TEXTCTRL_TEXT_CENTERED
1089 int tcSizeY
= m_text
->GetBestSize().y
;
1090 int diff0
= sz
.y
- tcSizeY
;
1091 int y
= textCtrlYAdjust
+ (diff0
/2);
1093 wxUnusedVar(textCtrlYAdjust
);
1097 if ( y
< customBorder
)
1102 m_tcArea
.width
- m_tcArea
.x
- x
,
1105 // Make sure textctrl doesn't exceed the bottom custom border
1106 wxSize tsz
= m_text
->GetSize();
1107 int diff1
= (y
+ tsz
.y
) - (sz
.y
- customBorder
);
1110 tsz
.y
= tsz
.y
- diff1
- 1;
1111 m_text
->SetSize(tsz
);
1116 // If it has border, have textctrl fill the entire text field.
1117 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
,
1119 m_tcArea
.width
- m_widthCustomPaint
,
1124 wxSize
wxComboCtrlBase::DoGetBestSize() const
1126 wxSize
sizeText(150,0);
1129 sizeText
= m_text
->GetBestSize();
1131 // TODO: Better method to calculate close-to-native control height.
1135 fhei
= (m_font
.GetPointSize()*2) + 5;
1136 else if ( wxNORMAL_FONT
->Ok() )
1137 fhei
= (wxNORMAL_FONT
->GetPointSize()*2) + 5;
1139 fhei
= sizeText
.y
+ 4;
1141 // Need to force height to accomodate bitmap?
1142 int btnSizeY
= m_btnSize
.y
;
1143 if ( m_bmpNormal
.Ok() && fhei
< btnSizeY
)
1146 // Control height doesn't depend on border
1149 int border = m_windowStyle & wxBORDER_MASK;
1150 if ( border == wxSIMPLE_BORDER )
1152 else if ( border == wxNO_BORDER )
1153 fhei += (m_widthCustomBorder*2);
1159 // Final adjustments
1165 // these are the numbers from the HIG:
1166 switch ( m_windowVariant
)
1168 case wxWINDOW_VARIANT_NORMAL
:
1172 case wxWINDOW_VARIANT_SMALL
:
1175 case wxWINDOW_VARIANT_MINI
:
1181 fhei
+= 2 * FOCUS_RING
;
1182 int width
= sizeText
.x
+ FOCUS_RING
+ COMBO_MARGIN
+ DEFAULT_DROPBUTTON_WIDTH
;
1184 wxSize
ret(width
, fhei
);
1189 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent
& event
)
1194 // defined by actual wxComboCtrls
1200 // ----------------------------------------------------------------------------
1201 // standard operations
1202 // ----------------------------------------------------------------------------
1204 bool wxComboCtrlBase::Enable(bool enable
)
1206 if ( !wxControl::Enable(enable
) )
1210 m_btn
->Enable(enable
);
1212 m_text
->Enable(enable
);
1219 bool wxComboCtrlBase::Show(bool show
)
1221 if ( !wxControl::Show(show
) )
1233 bool wxComboCtrlBase::SetFont ( const wxFont
& font
)
1235 if ( !wxControl::SetFont(font
) )
1240 // Without hiding the wxTextCtrl there would be some
1241 // visible 'flicker' (at least on Windows XP).
1243 m_text
->SetFont(font
);
1252 void wxComboCtrlBase::DoSetToolTip(wxToolTip
*tooltip
)
1254 wxControl::DoSetToolTip(tooltip
);
1256 // Set tool tip for button and text box
1259 const wxString
&tip
= tooltip
->GetTip();
1260 if ( m_text
) m_text
->SetToolTip(tip
);
1261 if ( m_btn
) m_btn
->SetToolTip(tip
);
1265 if ( m_text
) m_text
->SetToolTip( NULL
);
1266 if ( m_btn
) m_btn
->SetToolTip( NULL
);
1269 #endif // wxUSE_TOOLTIPS
1271 #if wxUSE_VALIDATORS
1272 void wxComboCtrlBase::SetValidator(const wxValidator
& validator
)
1274 wxTextCtrl
* textCtrl
= GetTextCtrl();
1277 textCtrl
->SetValidator( validator
);
1279 wxControl::SetValidator( validator
);
1282 wxValidator
* wxComboCtrlBase::GetValidator()
1284 wxTextCtrl
* textCtrl
= GetTextCtrl();
1286 return textCtrl
? textCtrl
->GetValidator() : wxControl::GetValidator();
1288 #endif // wxUSE_VALIDATORS
1290 // ----------------------------------------------------------------------------
1292 // ----------------------------------------------------------------------------
1294 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1295 // prepare combo box background on area in a way typical on platform
1296 void wxComboCtrlBase::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
1298 wxSize sz
= GetClientSize();
1300 bool doDrawFocusRect
; // also selected
1302 // For smaller size control (and for disabled background) use less spacing
1306 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1309 isEnabled
= IsEnabled();
1310 doDrawFocusRect
= ShouldDrawFocus() && !(m_iFlags
& wxCC_FULL_BUTTON
);
1312 // Windows-style: for smaller size control (and for disabled background) use less spacing
1313 focusSpacingX
= isEnabled
? 2 : 1;
1314 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
1318 // Drawing a list item
1319 isEnabled
= true; // they are never disabled
1320 doDrawFocusRect
= (flags
& wxCONTROL_SELECTED
) != 0;
1326 // Set the background sub-rectangle for selection, disabled etc
1327 wxRect
selRect(rect
);
1328 selRect
.y
+= focusSpacingY
;
1329 selRect
.height
-= (focusSpacingY
*2);
1333 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1334 wcp
+= m_widthCustomPaint
;
1336 selRect
.x
+= wcp
+ focusSpacingX
;
1337 selRect
.width
-= wcp
+ (focusSpacingX
*2);
1340 bool doDrawSelRect
= true;
1344 // If popup is hidden and this control is focused,
1345 // then draw the focus-indicator (selbgcolor background etc.).
1346 if ( doDrawFocusRect
)
1348 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1349 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1353 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
1354 #ifndef __WXMAC__ // see note in OnThemeChange
1355 doDrawSelRect
= false;
1356 bgCol
= GetBackgroundColour();
1358 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1364 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
1365 #ifndef __WXMAC__ // see note in OnThemeChange
1366 bgCol
= GetBackgroundColour();
1368 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1372 dc
.SetBrush( bgCol
);
1373 if ( doDrawSelRect
)
1376 dc
.DrawRectangle( selRect
);
1379 // Don't clip exactly to the selection rectangle so we can draw
1380 // to the non-selected area in front of it.
1381 wxRect
clipRect(rect
.x
,rect
.y
,
1382 (selRect
.x
+selRect
.width
)-rect
.x
,rect
.height
);
1383 dc
.SetClippingRegion(clipRect
);
1386 // Save the library size a bit for platforms that re-implement this.
1387 void wxComboCtrlBase::PrepareBackground( wxDC
&, const wxRect
&, int ) const
1392 void wxComboCtrlBase::DrawButton( wxDC
& dc
, const wxRect
& rect
, int flags
)
1394 int drawState
= m_btnState
;
1396 if ( (m_iFlags
& wxCC_BUTTON_STAYS_DOWN
) &&
1397 GetPopupWindowState() >= Animating
)
1398 drawState
|= wxCONTROL_PRESSED
;
1400 wxRect
drawRect(rect
.x
+m_btnSpacingX
,
1401 rect
.y
+((rect
.height
-m_btnSize
.y
)/2),
1405 // Make sure area is not larger than the control
1406 if ( drawRect
.y
< rect
.y
)
1407 drawRect
.y
= rect
.y
;
1408 if ( drawRect
.height
> rect
.height
)
1409 drawRect
.height
= rect
.height
;
1411 bool enabled
= IsEnabled();
1414 drawState
|= wxCONTROL_DISABLED
;
1416 if ( !m_bmpNormal
.Ok() )
1418 if ( flags
& Button_BitmapOnly
)
1421 // Need to clear button background even if m_btn is present
1422 if ( flags
& Button_PaintBackground
)
1426 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
1427 bgCol
= GetParent()->GetBackgroundColour();
1429 bgCol
= GetBackgroundColour();
1433 dc
.DrawRectangle(rect
);
1436 // Draw standard button
1437 wxRendererNative::Get().DrawComboBoxDropButton(this,
1449 pBmp
= &m_bmpDisabled
;
1450 else if ( m_btnState
& wxCONTROL_PRESSED
)
1451 pBmp
= &m_bmpPressed
;
1452 else if ( m_btnState
& wxCONTROL_CURRENT
)
1455 pBmp
= &m_bmpNormal
;
1457 if ( m_blankButtonBg
)
1459 // If using blank button background, we need to clear its background
1460 // with button face colour instead of colour for rest of the control.
1461 if ( flags
& Button_PaintBackground
)
1463 wxColour bgCol
= GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1464 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1467 dc
.DrawRectangle(rect
);
1470 if ( !(flags
& Button_BitmapOnly
) )
1472 wxRendererNative::Get().DrawPushButton(this,
1481 // Need to clear button background even if m_btn is present
1482 // (assume non-button background was cleared just before this call so brushes are good)
1483 if ( flags
& Button_PaintBackground
)
1484 dc
.DrawRectangle(rect
);
1487 // Draw bitmap centered in drawRect
1488 dc
.DrawBitmap(*pBmp
,
1489 drawRect
.x
+ (drawRect
.width
-pBmp
->GetWidth())/2,
1490 drawRect
.y
+ (drawRect
.height
-pBmp
->GetHeight())/2,
1495 void wxComboCtrlBase::RecalcAndRefresh()
1499 wxSizeEvent
evt(GetSize(),GetId());
1500 GetEventHandler()->ProcessEvent(evt
);
1505 // ----------------------------------------------------------------------------
1506 // miscellaneous event handlers
1507 // ----------------------------------------------------------------------------
1509 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent
& event
)
1511 if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1513 if ( m_ignoreEvtText
> 0 )
1520 // Change event id, object and string before relaying it forward
1521 event
.SetId(GetId());
1522 wxString s
= event
.GetString();
1523 event
.SetEventObject(this);
1528 // call if cursor is on button area or mouse is captured for the button
1529 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent
& event
,
1532 int type
= event
.GetEventType();
1534 if ( type
== wxEVT_MOTION
)
1536 if ( (flags
& wxCC_MF_ON_BUTTON
) &&
1537 IsPopupWindowState(Hidden
) )
1539 if ( !(m_btnState
& wxCONTROL_CURRENT
) )
1541 // Mouse hover begins
1542 m_btnState
|= wxCONTROL_CURRENT
;
1543 if ( HasCapture() ) // Retain pressed state.
1544 m_btnState
|= wxCONTROL_PRESSED
;
1548 else if ( (m_btnState
& wxCONTROL_CURRENT
) )
1551 m_btnState
&= ~(wxCONTROL_CURRENT
|wxCONTROL_PRESSED
);
1555 else if ( type
== wxEVT_LEFT_DOWN
|| type
== wxEVT_LEFT_DCLICK
)
1557 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1559 m_btnState
|= wxCONTROL_PRESSED
;
1562 if ( !(m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
) )
1565 // If showing popup now, do not capture mouse or there will be interference
1569 else if ( type
== wxEVT_LEFT_UP
)
1572 // Only accept event if mouse was left-press was previously accepted
1576 if ( m_btnState
& wxCONTROL_PRESSED
)
1578 // If mouse was inside, fire the click event.
1579 if ( m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
)
1581 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1585 m_btnState
&= ~(wxCONTROL_PRESSED
);
1589 else if ( type
== wxEVT_LEAVE_WINDOW
)
1591 if ( m_btnState
& (wxCONTROL_CURRENT
|wxCONTROL_PRESSED
) )
1593 m_btnState
&= ~(wxCONTROL_CURRENT
);
1596 if ( IsPopupWindowState(Hidden
) )
1598 m_btnState
&= ~(wxCONTROL_PRESSED
);
1606 // Never have 'hot' state when popup is being shown
1607 // (this is mostly needed because of the animation).
1608 if ( !IsPopupWindowState(Hidden
) )
1609 m_btnState
&= ~wxCONTROL_CURRENT
;
1614 // returns true if event was consumed or filtered
1615 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent
& event
,
1616 int WXUNUSED(flags
) )
1618 wxLongLong t
= ::wxGetLocalTimeMillis();
1619 int evtType
= event
.GetEventType();
1621 #if USES_WXPOPUPWINDOW || USES_WXDIALOG
1622 if ( m_popupWinType
!= POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1624 if ( IsPopupWindowState(Visible
) &&
1625 ( evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_RIGHT_DOWN
) )
1633 // Filter out clicks on button immediately after popup dismiss
1634 if ( evtType
== wxEVT_LEFT_DOWN
&& t
< m_timeCanAcceptClick
)
1636 event
.SetEventType(0);
1643 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent
& event
)
1645 int evtType
= event
.GetEventType();
1647 if ( (evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_LEFT_DCLICK
) &&
1648 (m_windowStyle
& wxCB_READONLY
) )
1650 if ( GetPopupWindowState() >= Animating
)
1652 #if USES_WXPOPUPWINDOW
1653 // Click here always hides the popup.
1654 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1660 if ( !(m_windowStyle
& wxCC_SPECIAL_DCLICK
) )
1662 // In read-only mode, clicking the text is the
1663 // same as clicking the button.
1666 else if ( /*evtType == wxEVT_LEFT_UP || */evtType
== wxEVT_LEFT_DCLICK
)
1668 //if ( m_popupInterface->CycleValue() )
1670 if ( m_popupInterface
)
1671 m_popupInterface
->OnComboDoubleClick();
1676 if ( IsPopupShown() )
1678 // relay (some) mouse events to the popup
1679 if ( evtType
== wxEVT_MOUSEWHEEL
)
1680 m_popup
->GetEventHandler()->AddPendingEvent(event
);
1686 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent
& event
)
1688 if ( IsPopupShown() )
1690 // pass it to the popped up control
1691 GetPopupControl()->GetControl()->GetEventHandler()->AddPendingEvent(event
);
1695 if ( GetParent()->HasFlag(wxTAB_TRAVERSAL
) &&
1696 HandleAsNavigationKey(event
) )
1699 if ( IsKeyPopupToggle(event
) )
1705 int comboStyle
= GetWindowStyle();
1706 wxComboPopup
* popupInterface
= GetPopupControl();
1708 if ( !popupInterface
)
1714 int keycode
= event
.GetKeyCode();
1716 if ( (comboStyle
& wxCB_READONLY
) ||
1717 (keycode
!= WXK_RIGHT
&& keycode
!= WXK_LEFT
) )
1719 popupInterface
->OnComboKeyEvent(event
);
1726 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent
& event
)
1728 if ( event
.GetEventType() == wxEVT_SET_FOCUS
)
1730 wxWindow
* tc
= GetTextCtrl();
1731 if ( tc
&& tc
!= DoFindFocus() )
1740 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent
& WXUNUSED(event
) )
1744 m_resetFocus
= false;
1745 wxWindow
* tc
= GetTextCtrl();
1751 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
1754 // left margin may also have changed
1755 if ( !(m_iFlags
& wxCC_IFLAG_LEFT_MARGIN_SET
) )
1756 m_marginLeft
= GetNativeTextIndent();
1760 // ----------------------------------------------------------------------------
1762 // ----------------------------------------------------------------------------
1764 // Create popup window and the child control
1765 void wxComboCtrlBase::CreatePopup()
1767 wxComboPopup
* popupInterface
= m_popupInterface
;
1772 #ifdef wxComboPopupWindowBase2
1773 if ( m_iFlags
& wxCC_IFLAG_USE_ALT_POPUP
)
1776 m_winPopup
= new wxComboPopupWindowBase2( this, wxNO_BORDER
);
1778 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
, wxEmptyString
,
1779 wxPoint(-21,-21), wxSize(20, 20),
1782 m_popupWinType
= SECONDARY_POPUP_TYPE
;
1785 #endif // wxComboPopupWindowBase2
1787 m_winPopup
= new wxComboPopupWindow( this, wxNO_BORDER
);
1788 m_popupWinType
= PRIMARY_POPUP_TYPE
;
1790 m_popupWinEvtHandler
= new wxComboPopupWindowEvtHandler(this);
1791 m_winPopup
->PushEventHandler(m_popupWinEvtHandler
);
1794 popupInterface
->Create(m_winPopup
);
1795 m_popup
= popup
= popupInterface
->GetControl();
1797 m_popupExtraHandler
= new wxComboPopupExtraEventHandler(this);
1798 popup
->PushEventHandler( m_popupExtraHandler
);
1800 // This may be helpful on some platforms
1801 // (eg. it bypasses a wxGTK popupwindow bug where
1802 // window is not initially hidden when it should be)
1805 popupInterface
->m_iFlags
|= wxCP_IFLAG_CREATED
;
1808 // Destroy popup window and the child control
1809 void wxComboCtrlBase::DestroyPopup()
1814 m_popup
->RemoveEventHandler(m_popupExtraHandler
);
1816 delete m_popupExtraHandler
;
1818 delete m_popupInterface
;
1822 m_winPopup
->RemoveEventHandler(m_popupWinEvtHandler
);
1823 delete m_popupWinEvtHandler
;
1824 m_popupWinEvtHandler
= NULL
;
1825 m_winPopup
->Destroy();
1828 m_popupExtraHandler
= NULL
;
1829 m_popupInterface
= NULL
;
1834 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup
* iface
)
1836 wxCHECK_RET( iface
, wxT("no popup interface set for wxComboCtrl") );
1840 iface
->InitBase(this);
1843 m_popupInterface
= iface
;
1845 if ( !iface
->LazyCreate() )
1854 // This must be done after creation
1855 if ( m_valueString
.length() )
1857 iface
->SetStringValue(m_valueString
);
1862 // Ensures there is atleast the default popup
1863 void wxComboCtrlBase::EnsurePopupControl()
1865 if ( !m_popupInterface
)
1866 SetPopupControl(NULL
);
1869 void wxComboCtrlBase::OnButtonClick()
1871 // Derived classes can override this method for totally custom
1873 if ( !IsPopupWindowState(Visible
) )
1879 void wxComboCtrlBase::ShowPopup()
1881 EnsurePopupControl();
1882 wxCHECK_RET( !IsPopupWindowState(Visible
), wxT("popup window already shown") );
1884 if ( IsPopupWindowState(Animating
) )
1889 // Space above and below
1895 wxSize ctrlSz
= GetSize();
1897 screenHeight
= wxSystemSettings::GetMetric( wxSYS_SCREEN_Y
);
1898 scrPos
= GetParent()->ClientToScreen(GetPosition());
1900 spaceAbove
= scrPos
.y
;
1901 spaceBelow
= screenHeight
- spaceAbove
- ctrlSz
.y
;
1903 maxHeightPopup
= spaceBelow
;
1904 if ( spaceAbove
> spaceBelow
)
1905 maxHeightPopup
= spaceAbove
;
1908 int widthPopup
= ctrlSz
.x
+ m_extLeft
+ m_extRight
;
1910 if ( widthPopup
< m_widthMinPopup
)
1911 widthPopup
= m_widthMinPopup
;
1913 wxWindow
* winPopup
= m_winPopup
;
1916 // Need to disable tab traversal of parent
1918 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1919 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1920 // that if transient popup is open, then tab traversal is to be ignored.
1921 // However, I think this code would still be needed for cases where
1922 // transient popup doesn't work yet (wxWinCE?).
1923 wxWindow
* parent
= GetParent();
1924 int parentFlags
= parent
->GetWindowStyle();
1925 if ( parentFlags
& wxTAB_TRAVERSAL
)
1927 parent
->SetWindowStyle( parentFlags
& ~(wxTAB_TRAVERSAL
) );
1928 m_iFlags
|= wxCC_IFLAG_PARENT_TAB_TRAVERSAL
;
1934 winPopup
= m_winPopup
;
1944 wxASSERT( !m_popup
|| m_popup
== popup
); // Consistency check.
1946 wxSize adjustedSize
= m_popupInterface
->GetAdjustedSize(widthPopup
,
1947 m_heightPopup
<=0?DEFAULT_POPUP_HEIGHT
:m_heightPopup
,
1950 popup
->SetSize(adjustedSize
);
1952 m_popupInterface
->OnPopup();
1955 // Reposition and resize popup window
1958 wxSize szp
= popup
->GetSize();
1961 int popupY
= scrPos
.y
+ ctrlSz
.y
;
1963 // Default anchor is wxLEFT
1964 int anchorSide
= m_anchorSide
;
1966 anchorSide
= wxLEFT
;
1968 int rightX
= scrPos
.x
+ ctrlSz
.x
+ m_extRight
- szp
.x
;
1969 int leftX
= scrPos
.x
- m_extLeft
;
1971 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
1974 int screenWidth
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
);
1976 // If there is not enough horizontal space, anchor on the other side.
1977 // If there is no space even then, place the popup at x 0.
1978 if ( anchorSide
== wxRIGHT
)
1982 if ( (leftX
+szp
.x
) < screenWidth
)
1983 anchorSide
= wxLEFT
;
1990 if ( (leftX
+szp
.x
) >= screenWidth
)
1993 anchorSide
= wxRIGHT
;
1999 // Select x coordinate according to the anchor side
2000 if ( anchorSide
== wxRIGHT
)
2002 else if ( anchorSide
== wxLEFT
)
2007 int showFlags
= CanDeferShow
;
2009 if ( spaceBelow
< szp
.y
)
2011 popupY
= scrPos
.y
- szp
.y
;
2012 showFlags
|= ShowAbove
;
2015 #if INSTALL_TOPLEV_HANDLER
2016 // Put top level window event handler into place
2017 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
2019 if ( !m_toplevEvtHandler
)
2020 m_toplevEvtHandler
= new wxComboFrameEventHandler(this);
2022 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2024 ((wxComboFrameEventHandler
*)m_toplevEvtHandler
)->OnPopup();
2025 toplev
->PushEventHandler( m_toplevEvtHandler
);
2029 // Set string selection (must be this way instead of SetStringSelection)
2032 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2033 m_text
->SelectAll();
2035 m_popupInterface
->SetStringValue( m_text
->GetValue() );
2039 // This is neede since focus/selection indication may change when popup is shown
2043 // This must be after SetStringValue
2044 m_popupWinState
= Animating
;
2046 wxRect
popupWinRect( popupX
, popupY
, szp
.x
, szp
.y
);
2049 if ( (m_iFlags
& wxCC_IFLAG_DISABLE_POPUP_ANIM
) ||
2050 AnimateShow( popupWinRect
, showFlags
) )
2052 DoShowPopup( popupWinRect
, showFlags
);
2056 bool wxComboCtrlBase::AnimateShow( const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
) )
2061 void wxComboCtrlBase::DoShowPopup( const wxRect
& rect
, int WXUNUSED(flags
) )
2063 wxWindow
* winPopup
= m_winPopup
;
2065 if ( IsPopupWindowState(Animating
) )
2067 // Make sure the popup window is shown in the right position.
2068 // Should not matter even if animation already did this.
2070 // Some platforms (GTK) may like SetSize and Move to be separate
2071 // (though the bug was probably fixed).
2072 winPopup
->SetSize( rect
);
2074 #if USES_WXPOPUPTRANSIENTWINDOW
2075 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2076 ((wxPopupTransientWindow
*)winPopup
)->Popup(m_popup
);
2081 m_popupWinState
= Visible
;
2083 else if ( IsPopupWindowState(Hidden
) )
2085 // Animation was aborted
2087 wxASSERT( !winPopup
->IsShown() );
2089 m_popupWinState
= Hidden
;
2095 void wxComboCtrlBase::OnPopupDismiss()
2097 // Just in case, avoid double dismiss
2098 if ( IsPopupWindowState(Hidden
) )
2101 // This must be set before focus - otherwise there will be recursive
2102 // OnPopupDismisses.
2103 m_popupWinState
= Hidden
;
2106 m_winPopup
->Disable();
2108 // Inform popup control itself
2109 m_popupInterface
->OnDismiss();
2111 if ( m_popupExtraHandler
)
2112 ((wxComboPopupExtraEventHandler
*)m_popupExtraHandler
)->OnPopupDismiss();
2114 #if INSTALL_TOPLEV_HANDLER
2115 // Remove top level window event handler
2116 if ( m_toplevEvtHandler
)
2118 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2120 toplev
->RemoveEventHandler( m_toplevEvtHandler
);
2124 m_timeCanAcceptClick
= ::wxGetLocalTimeMillis();
2126 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2127 m_timeCanAcceptClick
+= 150;
2129 // If cursor not on dropdown button, then clear its state
2130 // (technically not required by all ports, but do it for all just in case)
2131 if ( !m_btnArea
.Contains(ScreenToClient(::wxGetMousePosition())) )
2134 // Return parent's tab traversal flag.
2135 // See ShowPopup for notes.
2136 if ( m_iFlags
& wxCC_IFLAG_PARENT_TAB_TRAVERSAL
)
2138 wxWindow
* parent
= GetParent();
2139 parent
->SetWindowStyle( parent
->GetWindowStyle() | wxTAB_TRAVERSAL
);
2140 m_iFlags
&= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL
);
2143 // refresh control (necessary even if m_text)
2149 void wxComboCtrlBase::HidePopup()
2151 // Should be able to call this without popup interface
2152 if ( IsPopupWindowState(Hidden
) )
2155 // transfer value and show it in textctrl, if any
2156 if ( !IsPopupWindowState(Animating
) )
2157 SetValue( m_popupInterface
->GetStringValue() );
2164 // ----------------------------------------------------------------------------
2165 // customization methods
2166 // ----------------------------------------------------------------------------
2168 void wxComboCtrlBase::SetButtonPosition( int width
, int height
,
2169 int side
, int spacingX
)
2174 m_btnSpacingX
= spacingX
;
2179 wxSize
wxComboCtrlBase::GetButtonSize()
2181 if ( m_btnSize
.x
> 0 )
2184 wxSize
retSize(m_btnWid
,m_btnHei
);
2186 // Need to call CalculateAreas now if button size is
2187 // is not explicitly specified.
2188 if ( retSize
.x
<= 0 || retSize
.y
<= 0)
2192 retSize
= m_btnSize
;
2198 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap
& bmpNormal
,
2200 const wxBitmap
& bmpPressed
,
2201 const wxBitmap
& bmpHover
,
2202 const wxBitmap
& bmpDisabled
)
2204 m_bmpNormal
= bmpNormal
;
2205 m_blankButtonBg
= blankButtonBg
;
2207 if ( bmpPressed
.Ok() )
2208 m_bmpPressed
= bmpPressed
;
2210 m_bmpPressed
= bmpNormal
;
2212 if ( bmpHover
.Ok() )
2213 m_bmpHover
= bmpHover
;
2215 m_bmpHover
= bmpNormal
;
2217 if ( bmpDisabled
.Ok() )
2218 m_bmpDisabled
= bmpDisabled
;
2220 m_bmpDisabled
= bmpNormal
;
2225 void wxComboCtrlBase::SetCustomPaintWidth( int width
)
2229 // move textctrl accordingly
2230 wxRect r
= m_text
->GetRect();
2231 int inc
= width
- m_widthCustomPaint
;
2234 m_text
->SetSize( r
);
2237 m_widthCustomPaint
= width
;
2242 bool wxComboCtrlBase::DoSetMargins(const wxPoint
& margins
)
2244 // For general sanity's sake, we ignore top margin. Instead
2245 // we will always try to center the text vertically.
2248 if ( margins
.x
!= -1 )
2250 m_marginLeft
= margins
.x
;
2251 m_iFlags
|= wxCC_IFLAG_LEFT_MARGIN_SET
;
2255 m_marginLeft
= GetNativeTextIndent();
2256 m_iFlags
&= ~(wxCC_IFLAG_LEFT_MARGIN_SET
);
2259 if ( margins
.y
!= -1 )
2269 wxPoint
wxComboCtrlBase::DoGetMargins() const
2271 return wxPoint(m_marginLeft
, -1);
2274 #if WXWIN_COMPATIBILITY_2_6
2275 void wxComboCtrlBase::SetTextIndent( int indent
)
2279 m_marginLeft
= GetNativeTextIndent();
2280 m_iFlags
&= ~(wxCC_IFLAG_LEFT_MARGIN_SET
);
2284 m_marginLeft
= indent
;
2285 m_iFlags
|= wxCC_IFLAG_LEFT_MARGIN_SET
;
2291 wxCoord
wxComboCtrlBase::GetTextIndent() const
2293 return m_marginLeft
;
2297 wxCoord
wxComboCtrlBase::GetNativeTextIndent() const
2299 return DEFAULT_TEXT_INDENT
;
2302 // ----------------------------------------------------------------------------
2303 // methods forwarded to wxTextCtrl
2304 // ----------------------------------------------------------------------------
2306 wxString
wxComboCtrlBase::GetValue() const
2309 return m_text
->GetValue();
2310 return m_valueString
;
2313 void wxComboCtrlBase::SetValueWithEvent(const wxString
& value
, bool withEvent
)
2320 m_text
->SetValue(value
);
2322 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2323 m_text
->SelectAll();
2326 // Since wxComboPopup may want to paint the combo as well, we need
2327 // to set the string value here (as well as sometimes in ShowPopup).
2328 if ( m_valueString
!= value
)
2330 m_valueString
= value
;
2332 EnsurePopupControl();
2334 if (m_popupInterface
)
2335 m_popupInterface
->SetStringValue(value
);
2341 void wxComboCtrlBase::SetValue(const wxString
& value
)
2343 SetValueWithEvent(value
, false);
2346 // In this SetValue variant wxComboPopup::SetStringValue is not called
2347 void wxComboCtrlBase::SetText(const wxString
& value
)
2349 // Unlike in SetValue(), this must be called here or
2350 // the behaviour will no be consistent in readonlys.
2351 EnsurePopupControl();
2353 m_valueString
= value
;
2358 m_text
->SetValue( value
);
2364 void wxComboCtrlBase::Copy()
2370 void wxComboCtrlBase::Cut()
2376 void wxComboCtrlBase::Paste()
2382 void wxComboCtrlBase::SetInsertionPoint(long pos
)
2385 m_text
->SetInsertionPoint(pos
);
2388 void wxComboCtrlBase::SetInsertionPointEnd()
2391 m_text
->SetInsertionPointEnd();
2394 long wxComboCtrlBase::GetInsertionPoint() const
2397 return m_text
->GetInsertionPoint();
2402 long wxComboCtrlBase::GetLastPosition() const
2405 return m_text
->GetLastPosition();
2410 void wxComboCtrlBase::Replace(long from
, long to
, const wxString
& value
)
2413 m_text
->Replace(from
, to
, value
);
2416 void wxComboCtrlBase::Remove(long from
, long to
)
2419 m_text
->Remove(from
, to
);
2422 void wxComboCtrlBase::SetSelection(long from
, long to
)
2425 m_text
->SetSelection(from
, to
);
2428 void wxComboCtrlBase::Undo()
2434 #endif // wxUSE_COMBOCTRL