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"
36 #include "wx/textctrl.h"
39 #include "wx/tooltip.h"
46 // ----------------------------------------------------------------------------
48 #define DEFAULT_DROPBUTTON_WIDTH 19
50 #define BMP_BUTTON_MARGIN 4
52 #define DEFAULT_POPUP_HEIGHT 400
54 #define DEFAULT_TEXT_INDENT 3
56 #define COMBO_MARGIN 2 // spacing right of wxTextCtrl
59 #if defined(__WXMSW__)
61 // Let's use wxFrame as a fall-back solution until wxMSW gets wxNonOwnedWindow
63 #define wxCC_GENERIC_TLW_IS_FRAME
64 #define wxComboCtrlGenericTLW wxFrame
66 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
67 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
68 // native controls work on it like normal.
69 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
70 #define TEXTCTRL_TEXT_CENTERED 0 // 1 if text in textctrl is vertically centered
71 #define FOCUS_RING 0 // No focus ring on wxMSW
73 //#undef wxUSE_POPUPWIN
74 //#define wxUSE_POPUPWIN 0
76 #elif defined(__WXGTK__)
78 // NB: It is not recommended to use wxDialog as popup on wxGTK, because of
79 // this bug: If wxDialog is hidden, its position becomes corrupt
80 // between hide and next show, but without internal coordinates being
81 // reflected (or something like that - atleast commenting out ->Hide()
82 // seemed to eliminate the position change).
84 #include "wx/dialog.h"
85 #define wxCC_GENERIC_TLW_IS_DIALOG
86 #define wxComboCtrlGenericTLW wxDialog
88 #include "wx/gtk/private.h"
90 // NB: Let's not be afraid to use wxGTK's wxPopupTransientWindow as a
91 // 'perfect' popup, as it can succesfully host child controls even in
92 // popups that are shown in modal dialogs.
94 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
95 #define TRANSIENT_POPUPWIN_IS_PERFECT 1 // wxPopupTransientWindow works, its child can have focus, and common
96 // native controls work on it like normal.
97 #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
98 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
99 #define FOCUS_RING 0 // No focus ring on wxGTK
101 #elif defined(__WXMAC__)
103 #include "wx/nonownedwnd.h"
104 #define wxCC_GENERIC_TLW_IS_NONOWNEDWINDOW
105 #define wxComboCtrlGenericTLW wxNonOwnedWindow
107 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
108 #define TRANSIENT_POPUPWIN_IS_PERFECT 1 // wxPopupTransientWindow works, its child can have focus, and common
109 // native controls work on it like normal.
110 #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
111 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
112 #define FOCUS_RING 3 // Reserve room for the textctrl's focus ring to display
114 #undef DEFAULT_DROPBUTTON_WIDTH
115 #define DEFAULT_DROPBUTTON_WIDTH 22
117 #define COMBO_MARGIN FOCUS_RING
121 #include "wx/dialog.h"
122 #define wxCC_GENERIC_TLW_IS_DIALOG
123 #define wxComboCtrlGenericTLW wxDialog
125 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
126 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
127 // native controls work on it like normal.
128 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
129 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
135 // Popupwin is really only supported on wxMSW (not WINCE) and wxGTK, regardless
136 // what the wxUSE_POPUPWIN says.
137 // FIXME: Why isn't wxUSE_POPUPWIN reliable any longer? (it was in wxW2.6.2)
138 #if (!defined(__WXMSW__) && !defined(__WXGTK__) && !defined(__WXMAC__)) || defined(__WXWINCE__)
139 #undef wxUSE_POPUPWIN
140 #define wxUSE_POPUPWIN 0
145 #include "wx/popupwin.h"
147 #undef USE_TRANSIENT_POPUP
148 #define USE_TRANSIENT_POPUP 0
152 // Define different types of popup windows
156 POPUPWIN_WXPOPUPTRANSIENTWINDOW
= 1,
157 POPUPWIN_WXPOPUPWINDOW
= 2,
158 POPUPWIN_GENERICTLW
= 3
162 #if USE_TRANSIENT_POPUP
163 // wxPopupTransientWindow is implemented
165 #define wxComboPopupWindowBase wxPopupTransientWindow
166 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPTRANSIENTWINDOW
167 #define USES_WXPOPUPTRANSIENTWINDOW 1
169 #if TRANSIENT_POPUPWIN_IS_PERFECT
171 #elif POPUPWIN_IS_PERFECT
172 #define wxComboPopupWindowBase2 wxPopupWindow
173 #define SECONDARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
174 #define USES_WXPOPUPWINDOW 1
176 #define wxComboPopupWindowBase2 wxComboCtrlGenericTLW
177 #define SECONDARY_POPUP_TYPE POPUPWIN_GENERICTLW
178 #define USES_GENERICTLW 1
182 // wxPopupWindow (but not wxPopupTransientWindow) is properly implemented
184 #define wxComboPopupWindowBase wxPopupWindow
185 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
186 #define USES_WXPOPUPWINDOW 1
188 #if !POPUPWIN_IS_PERFECT
189 #define wxComboPopupWindowBase2 wxComboCtrlGenericTLW
190 #define SECONDARY_POPUP_TYPE POPUPWIN_GENERICTLW
191 #define USES_GENERICTLW 1
195 // wxPopupWindow is not implemented
197 #define wxComboPopupWindowBase wxComboCtrlGenericTLW
198 #define PRIMARY_POPUP_TYPE POPUPWIN_GENERICTLW
199 #define USES_GENERICTLW 1
204 #ifndef USES_WXPOPUPTRANSIENTWINDOW
205 #define USES_WXPOPUPTRANSIENTWINDOW 0
208 #ifndef USES_WXPOPUPWINDOW
209 #define USES_WXPOPUPWINDOW 0
212 #ifndef USES_GENERICTLW
213 #define USES_GENERICTLW 0
217 #if USES_WXPOPUPWINDOW
218 #define INSTALL_TOPLEV_HANDLER 1
220 #define INSTALL_TOPLEV_HANDLER 0
226 // * wxComboPopupWindow for external use (ie. replace old wxUniv wxPopupComboWindow)
230 // ----------------------------------------------------------------------------
231 // wxComboFrameEventHandler takes care of hiding the popup when events happen
232 // in its top level parent.
233 // ----------------------------------------------------------------------------
235 #if INSTALL_TOPLEV_HANDLER
238 // This will no longer be necessary after wxTransientPopupWindow
239 // works well on all platforms.
242 class wxComboFrameEventHandler
: public wxEvtHandler
245 wxComboFrameEventHandler( wxComboCtrlBase
* pCb
);
246 virtual ~wxComboFrameEventHandler();
250 void OnIdle( wxIdleEvent
& event
);
251 void OnMouseEvent( wxMouseEvent
& event
);
252 void OnActivate( wxActivateEvent
& event
);
253 void OnResize( wxSizeEvent
& event
);
254 void OnMove( wxMoveEvent
& event
);
255 void OnMenuEvent( wxMenuEvent
& event
);
256 void OnClose( wxCloseEvent
& event
);
259 wxWindow
* m_focusStart
;
260 wxComboCtrlBase
* m_combo
;
263 DECLARE_EVENT_TABLE()
266 BEGIN_EVENT_TABLE(wxComboFrameEventHandler
, wxEvtHandler
)
267 EVT_IDLE(wxComboFrameEventHandler::OnIdle
)
268 EVT_LEFT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
269 EVT_RIGHT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
270 EVT_SIZE(wxComboFrameEventHandler::OnResize
)
271 EVT_MOVE(wxComboFrameEventHandler::OnMove
)
272 EVT_MENU_HIGHLIGHT(wxID_ANY
,wxComboFrameEventHandler::OnMenuEvent
)
273 EVT_MENU_OPEN(wxComboFrameEventHandler::OnMenuEvent
)
274 EVT_ACTIVATE(wxComboFrameEventHandler::OnActivate
)
275 EVT_CLOSE(wxComboFrameEventHandler::OnClose
)
278 wxComboFrameEventHandler::wxComboFrameEventHandler( wxComboCtrlBase
* combo
)
284 wxComboFrameEventHandler::~wxComboFrameEventHandler()
288 void wxComboFrameEventHandler::OnPopup()
290 m_focusStart
= ::wxWindow::FindFocus();
293 void wxComboFrameEventHandler::OnIdle( wxIdleEvent
& event
)
295 wxWindow
* winFocused
= ::wxWindow::FindFocus();
297 wxWindow
* popup
= m_combo
->GetPopupControl()->GetControl();
298 wxWindow
* winpopup
= m_combo
->GetPopupWindow();
301 winFocused
!= m_focusStart
&&
302 winFocused
!= popup
&&
303 winFocused
->GetParent() != popup
&&
304 winFocused
!= winpopup
&&
305 winFocused
->GetParent() != winpopup
&&
306 winFocused
!= m_combo
&&
307 winFocused
!= m_combo
->GetButton() // GTK (atleast) requires this
310 m_combo
->HidePopup(true);
316 void wxComboFrameEventHandler::OnMenuEvent( wxMenuEvent
& event
)
318 m_combo
->HidePopup(true);
322 void wxComboFrameEventHandler::OnMouseEvent( wxMouseEvent
& event
)
324 m_combo
->HidePopup(true);
328 void wxComboFrameEventHandler::OnClose( wxCloseEvent
& event
)
330 m_combo
->HidePopup(true);
334 void wxComboFrameEventHandler::OnActivate( wxActivateEvent
& event
)
336 m_combo
->HidePopup(true);
340 void wxComboFrameEventHandler::OnResize( wxSizeEvent
& event
)
342 m_combo
->HidePopup(true);
346 void wxComboFrameEventHandler::OnMove( wxMoveEvent
& event
)
348 m_combo
->HidePopup(true);
352 #endif // INSTALL_TOPLEV_HANDLER
354 // ----------------------------------------------------------------------------
355 // wxComboPopupWindow is, in essence, wxPopupWindow customized for
357 // ----------------------------------------------------------------------------
359 class wxComboPopupWindow
: public wxComboPopupWindowBase
363 wxComboPopupWindow( wxComboCtrlBase
*parent
,
365 #if USES_WXPOPUPWINDOW || USES_WXPOPUPTRANSIENTWINDOW
366 : wxComboPopupWindowBase(parent
,style
)
368 : wxComboPopupWindowBase(parent
,
379 #if USES_WXPOPUPTRANSIENTWINDOW
380 virtual bool Show( bool show
);
381 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
383 virtual void OnDismiss();
391 #if USES_WXPOPUPTRANSIENTWINDOW
392 bool wxComboPopupWindow::Show( bool show
)
394 // Guard against recursion
396 return wxComboPopupWindowBase::Show(show
);
400 wxASSERT( IsKindOf(CLASSINFO(wxPopupTransientWindow
)) );
402 wxPopupTransientWindow
* ptw
= (wxPopupTransientWindow
*) this;
404 if ( show
!= ptw
->IsShown() )
407 // We used to do wxPopupTransientWindow::Popup here,
408 // but this would hide normal Show, which we are
409 // also going to need.
420 bool wxComboPopupWindow::ProcessLeftDown(wxMouseEvent
& event
)
422 return wxPopupTransientWindow::ProcessLeftDown(event
);
425 // First thing that happens when a transient popup closes is that this method gets called.
426 void wxComboPopupWindow::OnDismiss()
428 wxComboCtrlBase
* combo
= (wxComboCtrlBase
*) GetParent();
429 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxComboCtrlBase
)),
430 wxT("parent might not be wxComboCtrl, but check IMPLEMENT_DYNAMIC_CLASS(2) macro for correctness") );
432 combo
->OnPopupDismiss(true);
434 #endif // USES_WXPOPUPTRANSIENTWINDOW
437 // ----------------------------------------------------------------------------
438 // wxComboPopupWindowEvtHandler does bulk of the custom event handling
439 // of a popup window. It is separate so we can have different types
441 // ----------------------------------------------------------------------------
443 class wxComboPopupWindowEvtHandler
: public wxEvtHandler
447 wxComboPopupWindowEvtHandler( wxComboCtrlBase
*parent
)
452 void OnSizeEvent( wxSizeEvent
& event
);
453 void OnKeyEvent(wxKeyEvent
& event
);
455 void OnActivate( wxActivateEvent
& event
);
459 wxComboCtrlBase
* m_combo
;
461 DECLARE_EVENT_TABLE()
465 BEGIN_EVENT_TABLE(wxComboPopupWindowEvtHandler
, wxEvtHandler
)
466 EVT_KEY_DOWN(wxComboPopupWindowEvtHandler::OnKeyEvent
)
467 EVT_KEY_UP(wxComboPopupWindowEvtHandler::OnKeyEvent
)
468 EVT_CHAR(wxComboPopupWindowEvtHandler::OnKeyEvent
)
470 EVT_ACTIVATE(wxComboPopupWindowEvtHandler::OnActivate
)
472 EVT_SIZE(wxComboPopupWindowEvtHandler::OnSizeEvent
)
476 void wxComboPopupWindowEvtHandler::OnSizeEvent( wxSizeEvent
& WXUNUSED(event
) )
478 // Block the event so that the popup control does not get auto-resized.
481 void wxComboPopupWindowEvtHandler::OnKeyEvent( wxKeyEvent
& event
)
483 // Relay keyboard event to the main child controls
484 wxWindowList children
= m_combo
->GetPopupWindow()->GetChildren();
485 wxWindowList::iterator node
= children
.begin();
486 wxWindow
* child
= (wxWindow
*)*node
;
487 child
->GetEventHandler()->ProcessEvent(event
);
491 void wxComboPopupWindowEvtHandler::OnActivate( wxActivateEvent
& event
)
493 if ( !event
.GetActive() )
495 // Tell combo control that we are dismissed.
496 m_combo
->HidePopup(true);
504 // ----------------------------------------------------------------------------
507 // ----------------------------------------------------------------------------
509 wxComboPopup::~wxComboPopup()
513 void wxComboPopup::OnPopup()
517 void wxComboPopup::OnDismiss()
521 wxComboCtrl
* wxComboPopup::GetComboCtrl() const
523 return wxStaticCast(m_combo
, wxComboCtrl
);
526 wxSize
wxComboPopup::GetAdjustedSize( int minWidth
,
528 int WXUNUSED(maxHeight
) )
530 return wxSize(minWidth
,prefHeight
);
533 void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase
* combo
,
534 wxDC
& dc
, const wxRect
& rect
)
536 if ( combo
->GetWindowStyle() & wxCB_READONLY
) // ie. no textctrl
538 combo
->PrepareBackground(dc
,rect
,0);
540 dc
.DrawText( combo
->GetValue(),
541 rect
.x
+ combo
->m_marginLeft
,
542 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
546 void wxComboPopup::PaintComboControl( wxDC
& dc
, const wxRect
& rect
)
548 DefaultPaintComboControl(m_combo
,dc
,rect
);
551 void wxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
556 void wxComboPopup::OnComboCharEvent( wxKeyEvent
& event
)
561 void wxComboPopup::OnComboDoubleClick()
565 void wxComboPopup::SetStringValue( const wxString
& WXUNUSED(value
) )
569 bool wxComboPopup::LazyCreate()
574 void wxComboPopup::Dismiss()
576 m_combo
->HidePopup(true);
579 // ----------------------------------------------------------------------------
581 // ----------------------------------------------------------------------------
584 // This is pushed to the event handler queue of the child textctrl.
586 class wxComboBoxExtraInputHandler
: public wxEvtHandler
590 wxComboBoxExtraInputHandler( wxComboCtrlBase
* combo
)
595 virtual ~wxComboBoxExtraInputHandler() { }
596 void OnKey(wxKeyEvent
& event
);
597 void OnFocus(wxFocusEvent
& event
);
600 wxComboCtrlBase
* m_combo
;
603 DECLARE_EVENT_TABLE()
607 BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler
, wxEvtHandler
)
608 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey
)
609 EVT_KEY_UP(wxComboBoxExtraInputHandler::OnKey
)
610 EVT_CHAR(wxComboBoxExtraInputHandler::OnKey
)
611 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
612 EVT_KILL_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
616 void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent
& event
)
618 // Let the wxComboCtrl event handler have a go first.
619 wxComboCtrlBase
* combo
= m_combo
;
621 wxKeyEvent
redirectedEvent(event
);
622 redirectedEvent
.SetId(combo
->GetId());
623 redirectedEvent
.SetEventObject(combo
);
625 if ( !combo
->GetEventHandler()->ProcessEvent(redirectedEvent
) )
627 // Don't let TAB through to the text ctrl - looks ugly
628 if ( event
.GetKeyCode() != WXK_TAB
)
633 void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent
& event
)
635 // FIXME: This code does run when control is clicked,
636 // yet on Windows it doesn't select all the text.
637 if ( event
.GetEventType() == wxEVT_SET_FOCUS
&&
638 !(m_combo
->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT
) )
640 if ( m_combo
->GetTextCtrl() )
641 m_combo
->GetTextCtrl()->SelectAll();
643 m_combo
->SetSelection(-1,-1);
646 // Send focus indication to parent.
647 // NB: This is needed for cases where the textctrl gets focus
648 // instead of its parent. While this may trigger multiple
649 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
650 // from combo's focus event handler), they should be quite
652 wxFocusEvent
evt2(event
.GetEventType(),m_combo
->GetId());
653 evt2
.SetEventObject(m_combo
);
654 m_combo
->GetEventHandler()->ProcessEvent(evt2
);
661 // This is pushed to the event handler queue of the control in popup.
664 class wxComboPopupExtraEventHandler
: public wxEvtHandler
668 wxComboPopupExtraEventHandler( wxComboCtrlBase
* combo
)
672 m_beenInside
= false;
674 virtual ~wxComboPopupExtraEventHandler() { }
676 void OnMouseEvent( wxMouseEvent
& event
);
678 // Called from wxComboCtrlBase::OnPopupDismiss
679 void OnPopupDismiss()
681 m_beenInside
= false;
685 wxComboCtrlBase
* m_combo
;
690 DECLARE_EVENT_TABLE()
694 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler
, wxEvtHandler
)
695 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent
)
699 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent
& event
)
701 wxPoint pt
= event
.GetPosition();
702 wxSize sz
= m_combo
->GetPopupControl()->GetControl()->GetClientSize();
703 int evtType
= event
.GetEventType();
704 bool isInside
= pt
.x
>= 0 && pt
.y
>= 0 && pt
.x
< sz
.x
&& pt
.y
< sz
.y
;
705 bool relayToButton
= false;
709 if ( evtType
== wxEVT_MOTION
||
710 evtType
== wxEVT_LEFT_DOWN
||
711 evtType
== wxEVT_RIGHT_DOWN
)
713 // Block motion and click events outside the popup
714 if ( !isInside
|| !m_combo
->IsPopupShown() )
719 else if ( evtType
== wxEVT_LEFT_UP
)
721 if ( !m_combo
->IsPopupShown() )
724 relayToButton
= true;
726 else if ( !m_beenInside
)
734 relayToButton
= true;
742 // Some mouse events to popup that happen outside it, before cursor
743 // has been inside the popup, need to be ignored by it but relayed to
746 wxWindow
* eventSink
= m_combo
;
747 wxWindow
* btn
= m_combo
->GetButton();
751 eventSink
->GetEventHandler()->ProcessEvent(event
);
755 // ----------------------------------------------------------------------------
756 // wxComboCtrlTextCtrl
757 // ----------------------------------------------------------------------------
759 class wxComboCtrlTextCtrl
: public wxTextCtrl
762 wxComboCtrlTextCtrl() : wxTextCtrl() { }
763 virtual ~wxComboCtrlTextCtrl() { }
765 virtual wxWindow
*GetMainWindowOfCompositeControl()
767 wxComboCtrl
* combo
= (wxComboCtrl
*) GetParent();
769 // Returning this instead of just 'parent' lets FindFocus work
770 // correctly even when parent control is a child of a composite
771 // generic control (as is case with wxGenericDatePickerCtrl).
772 return combo
->GetMainWindowOfCompositeControl();
776 // ----------------------------------------------------------------------------
778 // ----------------------------------------------------------------------------
781 BEGIN_EVENT_TABLE(wxComboCtrlBase
, wxControl
)
782 EVT_TEXT(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
783 EVT_SIZE(wxComboCtrlBase::OnSizeEvent
)
784 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent
)
785 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent
)
786 EVT_IDLE(wxComboCtrlBase::OnIdleEvent
)
787 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
788 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent
)
789 EVT_CHAR(wxComboCtrlBase::OnCharEvent
)
790 EVT_TEXT_ENTER(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
791 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged
)
795 IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase
, wxControl
)
797 void wxComboCtrlBase::Init()
801 m_popupWinState
= Hidden
;
804 m_popupInterface
= NULL
;
806 m_popupExtraHandler
= NULL
;
807 m_textEvtHandler
= NULL
;
809 #if INSTALL_TOPLEV_HANDLER
810 m_toplevEvtHandler
= NULL
;
813 m_mainCtrlWnd
= this;
816 m_widthMinPopup
= -1;
818 m_widthCustomPaint
= 0;
819 m_widthCustomBorder
= 0;
823 m_blankButtonBg
= false;
825 m_popupWinType
= POPUPWIN_NONE
;
826 m_btnWid
= m_btnHei
= -1;
834 m_timeCanAcceptClick
= 0;
836 m_resetFocus
= false;
839 bool wxComboCtrlBase::Create(wxWindow
*parent
,
841 const wxString
& value
,
845 const wxValidator
& validator
,
846 const wxString
& name
)
848 if ( !wxControl::Create(parent
,
852 style
| wxWANTS_CHARS
,
857 m_valueString
= value
;
861 m_marginLeft
= GetNativeTextIndent();
863 m_iFlags
|= wxCC_IFLAG_CREATED
;
865 // If x and y indicate valid size, wxSizeEvent won't be
866 // emitted automatically, so we need to add artifical one.
867 if ( size
.x
> 0 && size
.y
> 0 )
869 wxSizeEvent
evt(size
,GetId());
870 GetEventHandler()->AddPendingEvent(evt
);
876 void wxComboCtrlBase::InstallInputHandlers()
880 m_textEvtHandler
= new wxComboBoxExtraInputHandler(this);
881 m_text
->PushEventHandler(m_textEvtHandler
);
886 wxComboCtrlBase::CreateTextCtrl(int style
, const wxValidator
& validator
)
888 if ( !(m_windowStyle
& wxCB_READONLY
) )
893 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
894 // not used by the wxPropertyGrid and therefore the tab is processed by
895 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
896 // navigation event is then sent to the wrong window.
897 style
|= wxTE_PROCESS_TAB
;
899 if ( HasFlag(wxTE_PROCESS_ENTER
) )
900 style
|= wxTE_PROCESS_ENTER
;
902 // Ignore EVT_TEXT generated by the constructor (but only
903 // if the event redirector already exists)
904 // NB: This must be " = 1" instead of "++";
905 if ( m_textEvtHandler
)
910 m_text
= new wxComboCtrlTextCtrl();
911 m_text
->Create(this, wxID_ANY
, m_valueString
,
912 wxDefaultPosition
, wxSize(10,-1),
914 m_text
->SetHint(m_hintText
);
918 void wxComboCtrlBase::OnThemeChange()
920 // Leave the default bg on the Mac so the area used by the focus ring will
921 // be the correct colour and themed brush. Instead we'll use
922 // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed.
924 #if defined(__WXMSW__) || defined(__WXGTK__)
925 wxVisualAttributes vattrs
= wxComboBox::GetClassDefaultAttributes();
927 wxVisualAttributes vattrs
;
928 vattrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
929 vattrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
932 // Only change the colours if application has not specified
936 SetOwnForegroundColour(vattrs
.colFg
);
941 SetOwnBackgroundColour(vattrs
.colBg
);
947 wxComboCtrlBase::~wxComboCtrlBase()
952 #if INSTALL_TOPLEV_HANDLER
953 delete ((wxComboFrameEventHandler
*)m_toplevEvtHandler
);
954 m_toplevEvtHandler
= NULL
;
960 m_text
->RemoveEventHandler(m_textEvtHandler
);
962 delete m_textEvtHandler
;
966 // ----------------------------------------------------------------------------
968 // ----------------------------------------------------------------------------
970 // Recalculates button and textctrl areas
971 void wxComboCtrlBase::CalculateAreas( int btnWidth
)
973 wxSize sz
= GetClientSize();
974 int customBorder
= m_widthCustomBorder
;
975 int btnBorder
; // border for button only
977 // check if button should really be outside the border: we'll do it it if
978 // its platform default or bitmap+pushbutton background is used, but not if
979 // there is vertical size adjustment or horizontal spacing.
980 if ( ( (m_iFlags
& wxCC_BUTTON_OUTSIDE_BORDER
) ||
981 (m_bmpNormal
.Ok() && m_blankButtonBg
) ) &&
982 m_btnSpacingX
== 0 &&
985 m_iFlags
|= wxCC_IFLAG_BUTTON_OUTSIDE
;
988 else if ( (m_iFlags
& wxCC_BUTTON_COVERS_BORDER
) &&
989 m_btnSpacingX
== 0 && !m_bmpNormal
.Ok() )
991 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
996 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
997 btnBorder
= customBorder
;
1000 // Defaul indentation
1001 if ( m_marginLeft
< 0 )
1002 m_marginLeft
= GetNativeTextIndent();
1004 int butWidth
= btnWidth
;
1006 if ( butWidth
<= 0 )
1007 butWidth
= m_btnWidDefault
;
1009 m_btnWidDefault
= butWidth
;
1011 if ( butWidth
<= 0 )
1014 int butHeight
= sz
.y
- btnBorder
*2;
1016 // Adjust button width
1018 butWidth
= m_btnWid
;
1021 // Adjust button width to match aspect ratio
1022 // (but only if control is smaller than best size).
1023 int bestHeight
= GetBestSize().y
;
1024 int height
= GetSize().y
;
1026 if ( height
< bestHeight
)
1028 // Make very small buttons square, as it makes
1029 // them accommodate arrow image better and still
1032 butWidth
= (height
*butWidth
)/bestHeight
;
1034 butWidth
= butHeight
;
1038 // Adjust button height
1040 butHeight
= m_btnHei
;
1042 // Use size of normal bitmap if...
1045 // button width is set to default and blank button bg is not drawn
1046 if ( m_bmpNormal
.Ok() )
1048 int bmpReqWidth
= m_bmpNormal
.GetWidth();
1049 int bmpReqHeight
= m_bmpNormal
.GetHeight();
1051 // If drawing blank button background, we need to add some margin.
1052 if ( m_blankButtonBg
)
1054 bmpReqWidth
+= BMP_BUTTON_MARGIN
*2;
1055 bmpReqHeight
+= BMP_BUTTON_MARGIN
*2;
1058 if ( butWidth
< bmpReqWidth
|| ( m_btnWid
== 0 && !m_blankButtonBg
) )
1059 butWidth
= bmpReqWidth
;
1060 if ( butHeight
< bmpReqHeight
|| ( m_btnHei
== 0 && !m_blankButtonBg
) )
1061 butHeight
= bmpReqHeight
;
1063 // Need to fix height?
1064 if ( (sz
.y
-(customBorder
*2)) < butHeight
&& btnWidth
== 0 )
1066 int newY
= butHeight
+(customBorder
*2);
1067 SetClientSize(wxDefaultCoord
,newY
);
1068 if ( m_bmpNormal
.Ok() || m_btnArea
.width
!= butWidth
|| m_btnArea
.height
!= butHeight
)
1069 m_iFlags
|= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1071 m_iFlags
&= ~wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1077 int butAreaWid
= butWidth
+ (m_btnSpacingX
*2);
1079 m_btnSize
.x
= butWidth
;
1080 m_btnSize
.y
= butHeight
;
1082 m_btnArea
.x
= ( m_btnSide
==wxRIGHT
? sz
.x
- butAreaWid
- btnBorder
: btnBorder
);
1083 m_btnArea
.y
= btnBorder
+ FOCUS_RING
;
1084 m_btnArea
.width
= butAreaWid
;
1085 m_btnArea
.height
= sz
.y
- ((btnBorder
+FOCUS_RING
)*2);
1087 m_tcArea
.x
= ( m_btnSide
==wxRIGHT
? 0 : butAreaWid
) + customBorder
+ FOCUS_RING
;
1088 m_tcArea
.y
= customBorder
+ FOCUS_RING
;
1089 m_tcArea
.width
= sz
.x
- butAreaWid
- (customBorder
*2) - (FOCUS_RING
*2);
1090 m_tcArea
.height
= sz
.y
- ((customBorder
+FOCUS_RING
)*2);
1095 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
1096 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1101 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
)
1106 wxSize sz
= GetClientSize();
1108 int customBorder
= m_widthCustomBorder
;
1109 if ( (m_text
->GetWindowStyleFlag() & wxBORDER_MASK
) == wxNO_BORDER
)
1113 if ( !m_widthCustomPaint
)
1115 // No special custom paint area - we can use 0 left margin
1117 if ( m_text
->SetMargins(0) )
1118 textCtrlXAdjust
= 0;
1119 x
= m_tcArea
.x
+ m_marginLeft
+ textCtrlXAdjust
;
1123 // There is special custom paint area - it is better to
1124 // use some margin with the wxTextCtrl.
1125 m_text
->SetMargins(m_marginLeft
);
1126 x
= m_tcArea
.x
+ m_widthCustomPaint
+
1127 m_marginLeft
+ textCtrlXAdjust
;
1130 // Centre textctrl vertically, if needed
1131 #if !TEXTCTRL_TEXT_CENTERED
1132 int tcSizeY
= m_text
->GetBestSize().y
;
1133 int diff0
= sz
.y
- tcSizeY
;
1134 int y
= textCtrlYAdjust
+ (diff0
/2);
1136 wxUnusedVar(textCtrlYAdjust
);
1140 if ( y
< customBorder
)
1145 m_tcArea
.width
- m_tcArea
.x
- x
,
1148 // Make sure textctrl doesn't exceed the bottom custom border
1149 wxSize tsz
= m_text
->GetSize();
1150 int diff1
= (y
+ tsz
.y
) - (sz
.y
- customBorder
);
1153 tsz
.y
= tsz
.y
- diff1
- 1;
1154 m_text
->SetSize(tsz
);
1159 // If it has border, have textctrl fill the entire text field.
1160 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
,
1162 m_tcArea
.width
- m_widthCustomPaint
,
1167 wxSize
wxComboCtrlBase::DoGetBestSize() const
1169 wxSize
sizeText(150,0);
1172 sizeText
= m_text
->GetBestSize();
1174 // TODO: Better method to calculate close-to-native control height.
1178 fhei
= (m_font
.GetPointSize()*2) + 5;
1179 else if ( wxNORMAL_FONT
->Ok() )
1180 fhei
= (wxNORMAL_FONT
->GetPointSize()*2) + 5;
1182 fhei
= sizeText
.y
+ 4;
1184 // Need to force height to accomodate bitmap?
1185 int btnSizeY
= m_btnSize
.y
;
1186 if ( m_bmpNormal
.Ok() && fhei
< btnSizeY
)
1189 // Control height doesn't depend on border
1192 int border = m_windowStyle & wxBORDER_MASK;
1193 if ( border == wxSIMPLE_BORDER )
1195 else if ( border == wxNO_BORDER )
1196 fhei += (m_widthCustomBorder*2);
1202 // Final adjustments
1208 // these are the numbers from the HIG:
1209 switch ( m_windowVariant
)
1211 case wxWINDOW_VARIANT_NORMAL
:
1215 case wxWINDOW_VARIANT_SMALL
:
1218 case wxWINDOW_VARIANT_MINI
:
1224 fhei
+= 2 * FOCUS_RING
;
1225 int width
= sizeText
.x
+ FOCUS_RING
+ COMBO_MARGIN
+ DEFAULT_DROPBUTTON_WIDTH
;
1227 wxSize
ret(width
, fhei
);
1232 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent
& event
)
1237 // defined by actual wxComboCtrls
1243 // ----------------------------------------------------------------------------
1244 // standard operations
1245 // ----------------------------------------------------------------------------
1247 bool wxComboCtrlBase::Enable(bool enable
)
1249 if ( !wxControl::Enable(enable
) )
1253 m_btn
->Enable(enable
);
1255 m_text
->Enable(enable
);
1262 bool wxComboCtrlBase::Show(bool show
)
1264 if ( !wxControl::Show(show
) )
1276 bool wxComboCtrlBase::SetFont ( const wxFont
& font
)
1278 if ( !wxControl::SetFont(font
) )
1283 // Without hiding the wxTextCtrl there would be some
1284 // visible 'flicker' (at least on Windows XP).
1286 m_text
->SetFont(font
);
1295 void wxComboCtrlBase::DoSetToolTip(wxToolTip
*tooltip
)
1297 wxControl::DoSetToolTip(tooltip
);
1299 // Set tool tip for button and text box
1302 const wxString
&tip
= tooltip
->GetTip();
1303 if ( m_text
) m_text
->SetToolTip(tip
);
1304 if ( m_btn
) m_btn
->SetToolTip(tip
);
1308 if ( m_text
) m_text
->SetToolTip( NULL
);
1309 if ( m_btn
) m_btn
->SetToolTip( NULL
);
1312 #endif // wxUSE_TOOLTIPS
1314 #if wxUSE_VALIDATORS
1315 void wxComboCtrlBase::SetValidator(const wxValidator
& validator
)
1317 wxTextCtrl
* textCtrl
= GetTextCtrl();
1320 textCtrl
->SetValidator( validator
);
1322 wxControl::SetValidator( validator
);
1325 wxValidator
* wxComboCtrlBase::GetValidator()
1327 wxTextCtrl
* textCtrl
= GetTextCtrl();
1329 return textCtrl
? textCtrl
->GetValidator() : wxControl::GetValidator();
1331 #endif // wxUSE_VALIDATORS
1333 bool wxComboCtrlBase::SetForegroundColour(const wxColour
& colour
)
1335 if ( wxControl::SetForegroundColour(colour
) )
1338 m_text
->SetForegroundColour(colour
);
1344 bool wxComboCtrlBase::SetBackgroundColour(const wxColour
& colour
)
1346 if ( wxControl::SetBackgroundColour(colour
) )
1349 m_text
->SetBackgroundColour(colour
);
1354 // ----------------------------------------------------------------------------
1356 // ----------------------------------------------------------------------------
1358 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1359 // prepare combo box background on area in a way typical on platform
1360 void wxComboCtrlBase::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
1362 wxSize sz
= GetClientSize();
1364 bool doDrawFocusRect
; // also selected
1366 // For smaller size control (and for disabled background) use less spacing
1370 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1373 isEnabled
= IsEnabled();
1374 doDrawFocusRect
= ShouldDrawFocus() && !(m_iFlags
& wxCC_FULL_BUTTON
);
1376 // Windows-style: for smaller size control (and for disabled background) use less spacing
1377 focusSpacingX
= isEnabled
? 2 : 1;
1378 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
1382 // Drawing a list item
1383 isEnabled
= true; // they are never disabled
1384 doDrawFocusRect
= (flags
& wxCONTROL_SELECTED
) != 0;
1390 // Set the background sub-rectangle for selection, disabled etc
1391 wxRect
selRect(rect
);
1392 selRect
.y
+= focusSpacingY
;
1393 selRect
.height
-= (focusSpacingY
*2);
1397 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1398 wcp
+= m_widthCustomPaint
;
1400 selRect
.x
+= wcp
+ focusSpacingX
;
1401 selRect
.width
-= wcp
+ (focusSpacingX
*2);
1406 bool doDrawSelRect
= true;
1408 // Determine foreground colour
1411 if ( doDrawFocusRect
)
1413 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1415 else if ( m_hasFgCol
)
1417 // Honour the custom foreground colour
1418 fgCol
= GetForegroundColour();
1422 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
1427 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
);
1430 // Determine background colour
1433 if ( doDrawFocusRect
)
1435 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1437 else if ( m_hasBgCol
)
1439 // Honour the custom background colour
1440 bgCol
= GetBackgroundColour();
1444 #ifndef __WXMAC__ // see note in OnThemeChange
1445 doDrawSelRect
= false;
1446 bgCol
= GetBackgroundColour();
1448 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1454 #ifndef __WXMAC__ // see note in OnThemeChange
1455 bgCol
= GetBackgroundColour();
1457 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1461 dc
.SetTextForeground( fgCol
);
1462 dc
.SetBrush( bgCol
);
1463 if ( doDrawSelRect
)
1466 dc
.DrawRectangle( selRect
);
1469 // Don't clip exactly to the selection rectangle so we can draw
1470 // to the non-selected area in front of it.
1471 wxRect
clipRect(rect
.x
,rect
.y
,
1472 (selRect
.x
+selRect
.width
)-rect
.x
,rect
.height
);
1473 dc
.SetClippingRegion(clipRect
);
1476 // Save the library size a bit for platforms that re-implement this.
1477 void wxComboCtrlBase::PrepareBackground( wxDC
&, const wxRect
&, int ) const
1482 void wxComboCtrlBase::DrawButton( wxDC
& dc
, const wxRect
& rect
, int flags
)
1484 int drawState
= m_btnState
;
1486 if ( (m_iFlags
& wxCC_BUTTON_STAYS_DOWN
) &&
1487 GetPopupWindowState() >= Animating
)
1488 drawState
|= wxCONTROL_PRESSED
;
1490 wxRect
drawRect(rect
.x
+m_btnSpacingX
,
1491 rect
.y
+((rect
.height
-m_btnSize
.y
)/2),
1495 // Make sure area is not larger than the control
1496 if ( drawRect
.y
< rect
.y
)
1497 drawRect
.y
= rect
.y
;
1498 if ( drawRect
.height
> rect
.height
)
1499 drawRect
.height
= rect
.height
;
1501 bool enabled
= IsEnabled();
1504 drawState
|= wxCONTROL_DISABLED
;
1506 if ( !m_bmpNormal
.Ok() )
1508 if ( flags
& Button_BitmapOnly
)
1511 // Need to clear button background even if m_btn is present
1512 if ( flags
& Button_PaintBackground
)
1516 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
1517 bgCol
= GetParent()->GetBackgroundColour();
1519 bgCol
= GetBackgroundColour();
1523 dc
.DrawRectangle(rect
);
1526 // Draw standard button
1527 wxRendererNative::Get().DrawComboBoxDropButton(this,
1539 pBmp
= &m_bmpDisabled
;
1540 else if ( m_btnState
& wxCONTROL_PRESSED
)
1541 pBmp
= &m_bmpPressed
;
1542 else if ( m_btnState
& wxCONTROL_CURRENT
)
1545 pBmp
= &m_bmpNormal
;
1547 if ( m_blankButtonBg
)
1549 // If using blank button background, we need to clear its background
1550 // with button face colour instead of colour for rest of the control.
1551 if ( flags
& Button_PaintBackground
)
1553 wxColour bgCol
= GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1554 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1557 dc
.DrawRectangle(rect
);
1560 if ( !(flags
& Button_BitmapOnly
) )
1562 wxRendererNative::Get().DrawPushButton(this,
1571 // Need to clear button background even if m_btn is present
1572 // (assume non-button background was cleared just before this call so brushes are good)
1573 if ( flags
& Button_PaintBackground
)
1574 dc
.DrawRectangle(rect
);
1577 // Draw bitmap centered in drawRect
1578 dc
.DrawBitmap(*pBmp
,
1579 drawRect
.x
+ (drawRect
.width
-pBmp
->GetWidth())/2,
1580 drawRect
.y
+ (drawRect
.height
-pBmp
->GetHeight())/2,
1585 void wxComboCtrlBase::RecalcAndRefresh()
1589 wxSizeEvent
evt(GetSize(),GetId());
1590 GetEventHandler()->ProcessEvent(evt
);
1595 // ----------------------------------------------------------------------------
1596 // miscellaneous event handlers
1597 // ----------------------------------------------------------------------------
1599 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent
& event
)
1601 if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1603 if ( m_ignoreEvtText
> 0 )
1610 // Change event id, object and string before relaying it forward
1611 event
.SetId(GetId());
1612 wxString s
= event
.GetString();
1613 event
.SetEventObject(this);
1618 // call if cursor is on button area or mouse is captured for the button
1619 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent
& event
,
1622 int type
= event
.GetEventType();
1624 if ( type
== wxEVT_MOTION
)
1626 if ( (flags
& wxCC_MF_ON_BUTTON
) &&
1627 IsPopupWindowState(Hidden
) )
1629 if ( !(m_btnState
& wxCONTROL_CURRENT
) )
1631 // Mouse hover begins
1632 m_btnState
|= wxCONTROL_CURRENT
;
1633 if ( HasCapture() ) // Retain pressed state.
1634 m_btnState
|= wxCONTROL_PRESSED
;
1638 else if ( (m_btnState
& wxCONTROL_CURRENT
) )
1641 m_btnState
&= ~(wxCONTROL_CURRENT
|wxCONTROL_PRESSED
);
1645 else if ( type
== wxEVT_LEFT_DOWN
|| type
== wxEVT_LEFT_DCLICK
)
1647 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1649 m_btnState
|= wxCONTROL_PRESSED
;
1652 if ( !(m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
) )
1655 // If showing popup now, do not capture mouse or there will be interference
1659 else if ( type
== wxEVT_LEFT_UP
)
1662 // Only accept event if mouse was left-press was previously accepted
1666 if ( m_btnState
& wxCONTROL_PRESSED
)
1668 // If mouse was inside, fire the click event.
1669 if ( m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
)
1671 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1675 m_btnState
&= ~(wxCONTROL_PRESSED
);
1679 else if ( type
== wxEVT_LEAVE_WINDOW
)
1681 if ( m_btnState
& (wxCONTROL_CURRENT
|wxCONTROL_PRESSED
) )
1683 m_btnState
&= ~(wxCONTROL_CURRENT
);
1686 if ( IsPopupWindowState(Hidden
) )
1688 m_btnState
&= ~(wxCONTROL_PRESSED
);
1696 // Never have 'hot' state when popup is being shown
1697 // (this is mostly needed because of the animation).
1698 if ( !IsPopupWindowState(Hidden
) )
1699 m_btnState
&= ~wxCONTROL_CURRENT
;
1704 // returns true if event was consumed or filtered
1705 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent
& event
,
1706 int WXUNUSED(flags
) )
1708 wxLongLong t
= ::wxGetLocalTimeMillis();
1709 int evtType
= event
.GetEventType();
1711 #if USES_WXPOPUPWINDOW || USES_GENERICTLW
1712 if ( m_popupWinType
!= POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1714 if ( IsPopupWindowState(Visible
) &&
1715 ( evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_RIGHT_DOWN
) )
1723 // Filter out clicks on button immediately after popup dismiss
1724 if ( evtType
== wxEVT_LEFT_DOWN
&& t
< m_timeCanAcceptClick
)
1726 event
.SetEventType(0);
1733 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent
& event
)
1735 int evtType
= event
.GetEventType();
1737 if ( (evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_LEFT_DCLICK
) &&
1738 (m_windowStyle
& wxCB_READONLY
) )
1740 if ( GetPopupWindowState() >= Animating
)
1742 #if USES_WXPOPUPWINDOW
1743 // Click here always hides the popup.
1744 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1750 if ( !(m_windowStyle
& wxCC_SPECIAL_DCLICK
) )
1752 // In read-only mode, clicking the text is the
1753 // same as clicking the button.
1756 else if ( /*evtType == wxEVT_LEFT_UP || */evtType
== wxEVT_LEFT_DCLICK
)
1758 //if ( m_popupInterface->CycleValue() )
1760 if ( m_popupInterface
)
1761 m_popupInterface
->OnComboDoubleClick();
1765 else if ( evtType
== wxEVT_MOUSEWHEEL
)
1767 if ( IsPopupShown() )
1769 // relay (some) mouse events to the popup
1770 m_popup
->GetEventHandler()->ProcessEvent(event
);
1772 else if ( event
.GetWheelAxis() == 0 &&
1773 event
.GetWheelRotation() != 0 &&
1774 event
.GetModifiers() == 0 )
1776 // Translate mousewheel actions into key up/down. This is
1777 // the simplest way of getting native behaviour: scrolling the
1778 // wheel moves selection up/down by one item.
1779 wxKeyEvent
kevent(wxEVT_KEY_DOWN
);
1780 kevent
.m_keyCode
= event
.GetWheelRotation() > 0
1783 GetEventHandler()->ProcessEvent(kevent
);
1796 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent
& event
)
1798 if ( IsPopupShown() )
1800 // pass it to the popped up control
1801 GetPopupControl()->GetControl()->GetEventHandler()->ProcessEvent(event
);
1805 wxWindow
* mainCtrl
= GetMainWindowOfCompositeControl();
1807 if ( mainCtrl
->GetParent()->HasFlag(wxTAB_TRAVERSAL
) )
1809 if ( mainCtrl
->HandleAsNavigationKey(event
) )
1813 if ( IsKeyPopupToggle(event
) )
1819 int comboStyle
= GetWindowStyle();
1820 wxComboPopup
* popupInterface
= GetPopupControl();
1822 if ( !popupInterface
)
1828 int keycode
= event
.GetKeyCode();
1830 if ( (comboStyle
& wxCB_READONLY
) ||
1831 (keycode
!= WXK_RIGHT
&& keycode
!= WXK_LEFT
) )
1833 popupInterface
->OnComboKeyEvent(event
);
1840 void wxComboCtrlBase::OnCharEvent(wxKeyEvent
& event
)
1842 if ( IsPopupShown() )
1844 // pass it to the popped up control
1845 GetPopupControl()->GetControl()->GetEventHandler()->ProcessEvent(event
);
1849 wxComboPopup
* popupInterface
= GetPopupControl();
1850 if ( popupInterface
)
1852 popupInterface
->OnComboCharEvent(event
);
1861 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent
& event
)
1863 if ( event
.GetEventType() == wxEVT_SET_FOCUS
)
1865 wxWindow
* tc
= GetTextCtrl();
1866 if ( tc
&& tc
!= DoFindFocus() )
1875 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent
& WXUNUSED(event
) )
1879 m_resetFocus
= false;
1880 wxWindow
* tc
= GetTextCtrl();
1886 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
1889 // left margin may also have changed
1890 if ( !(m_iFlags
& wxCC_IFLAG_LEFT_MARGIN_SET
) )
1891 m_marginLeft
= GetNativeTextIndent();
1895 // ----------------------------------------------------------------------------
1897 // ----------------------------------------------------------------------------
1899 // Create popup window and the child control
1900 void wxComboCtrlBase::CreatePopup()
1902 wxComboPopup
* popupInterface
= m_popupInterface
;
1907 #ifdef wxComboPopupWindowBase2
1908 if ( m_iFlags
& wxCC_IFLAG_USE_ALT_POPUP
)
1910 #if !USES_GENERICTLW
1911 m_winPopup
= new wxComboPopupWindowBase2( this, wxNO_BORDER
);
1913 int tlwFlags
= wxNO_BORDER
;
1914 #ifdef wxCC_GENERIC_TLW_IS_FRAME
1915 tlwFlags
|= wxFRAME_NO_TASKBAR
;
1918 #ifdef wxCC_GENERIC_TLW_IS_NONOWNEDWINDOW
1919 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
,
1920 wxPoint(-21,-21), wxSize(20, 20),
1923 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
, wxEmptyString
,
1924 wxPoint(-21,-21), wxSize(20, 20),
1928 m_popupWinType
= SECONDARY_POPUP_TYPE
;
1931 #endif // wxComboPopupWindowBase2
1933 m_winPopup
= new wxComboPopupWindow( this, wxNO_BORDER
);
1934 m_popupWinType
= PRIMARY_POPUP_TYPE
;
1936 m_popupWinEvtHandler
= new wxComboPopupWindowEvtHandler(this);
1937 m_winPopup
->PushEventHandler(m_popupWinEvtHandler
);
1940 popupInterface
->Create(m_winPopup
);
1941 m_popup
= popup
= popupInterface
->GetControl();
1943 m_popupExtraHandler
= new wxComboPopupExtraEventHandler(this);
1944 popup
->PushEventHandler( m_popupExtraHandler
);
1946 // This may be helpful on some platforms
1947 // (eg. it bypasses a wxGTK popupwindow bug where
1948 // window is not initially hidden when it should be)
1951 popupInterface
->m_iFlags
|= wxCP_IFLAG_CREATED
;
1954 // Destroy popup window and the child control
1955 void wxComboCtrlBase::DestroyPopup()
1960 m_popup
->RemoveEventHandler(m_popupExtraHandler
);
1962 delete m_popupExtraHandler
;
1964 delete m_popupInterface
;
1968 m_winPopup
->RemoveEventHandler(m_popupWinEvtHandler
);
1969 delete m_popupWinEvtHandler
;
1970 m_popupWinEvtHandler
= NULL
;
1971 m_winPopup
->Destroy();
1974 m_popupExtraHandler
= NULL
;
1975 m_popupInterface
= NULL
;
1980 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup
* iface
)
1982 wxCHECK_RET( iface
, wxT("no popup interface set for wxComboCtrl") );
1986 iface
->InitBase(this);
1989 m_popupInterface
= iface
;
1991 if ( !iface
->LazyCreate() )
2000 // This must be done after creation
2001 if ( m_valueString
.length() )
2003 iface
->SetStringValue(m_valueString
);
2008 // Ensures there is atleast the default popup
2009 void wxComboCtrlBase::EnsurePopupControl()
2011 if ( !m_popupInterface
)
2012 SetPopupControl(NULL
);
2015 void wxComboCtrlBase::OnButtonClick()
2017 // Derived classes can override this method for totally custom
2019 if ( !IsPopupWindowState(Visible
) )
2021 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_DROPDOWN
, GetId());
2022 event
.SetEventObject(this);
2023 HandleWindowEvent(event
);
2033 void wxComboCtrlBase::ShowPopup()
2035 EnsurePopupControl();
2036 wxCHECK_RET( !IsPopupWindowState(Visible
), wxT("popup window already shown") );
2038 if ( IsPopupWindowState(Animating
) )
2043 // Space above and below
2049 wxSize ctrlSz
= GetSize();
2051 screenHeight
= wxSystemSettings::GetMetric( wxSYS_SCREEN_Y
);
2052 scrPos
= GetParent()->ClientToScreen(GetPosition());
2054 spaceAbove
= scrPos
.y
;
2055 spaceBelow
= screenHeight
- spaceAbove
- ctrlSz
.y
;
2057 maxHeightPopup
= spaceBelow
;
2058 if ( spaceAbove
> spaceBelow
)
2059 maxHeightPopup
= spaceAbove
;
2062 int widthPopup
= ctrlSz
.x
+ m_extLeft
+ m_extRight
;
2064 if ( widthPopup
< m_widthMinPopup
)
2065 widthPopup
= m_widthMinPopup
;
2067 wxWindow
* winPopup
= m_winPopup
;
2070 // Need to disable tab traversal of parent
2072 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
2073 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
2074 // that if transient popup is open, then tab traversal is to be ignored.
2075 // However, I think this code would still be needed for cases where
2076 // transient popup doesn't work yet (wxWinCE?).
2077 wxWindow
* mainCtrl
= GetMainWindowOfCompositeControl();
2078 wxWindow
* parent
= mainCtrl
->GetParent();
2079 int parentFlags
= parent
->GetWindowStyle();
2080 if ( parentFlags
& wxTAB_TRAVERSAL
)
2082 parent
->SetWindowStyle( parentFlags
& ~(wxTAB_TRAVERSAL
) );
2083 m_iFlags
|= wxCC_IFLAG_PARENT_TAB_TRAVERSAL
;
2089 winPopup
= m_winPopup
;
2099 wxASSERT( !m_popup
|| m_popup
== popup
); // Consistency check.
2101 wxSize adjustedSize
= m_popupInterface
->GetAdjustedSize(widthPopup
,
2102 m_heightPopup
<=0?DEFAULT_POPUP_HEIGHT
:m_heightPopup
,
2105 popup
->SetSize(adjustedSize
);
2107 m_popupInterface
->OnPopup();
2110 // Reposition and resize popup window
2113 wxSize szp
= popup
->GetSize();
2116 int popupY
= scrPos
.y
+ ctrlSz
.y
;
2118 // Default anchor is wxLEFT
2119 int anchorSide
= m_anchorSide
;
2121 anchorSide
= wxLEFT
;
2123 int rightX
= scrPos
.x
+ ctrlSz
.x
+ m_extRight
- szp
.x
;
2124 int leftX
= scrPos
.x
- m_extLeft
;
2126 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
2129 int screenWidth
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
);
2131 // If there is not enough horizontal space, anchor on the other side.
2132 // If there is no space even then, place the popup at x 0.
2133 if ( anchorSide
== wxRIGHT
)
2137 if ( (leftX
+szp
.x
) < screenWidth
)
2138 anchorSide
= wxLEFT
;
2145 if ( (leftX
+szp
.x
) >= screenWidth
)
2148 anchorSide
= wxRIGHT
;
2154 // Select x coordinate according to the anchor side
2155 if ( anchorSide
== wxRIGHT
)
2157 else if ( anchorSide
== wxLEFT
)
2162 int showFlags
= CanDeferShow
;
2164 if ( spaceBelow
< szp
.y
)
2166 popupY
= scrPos
.y
- szp
.y
;
2167 showFlags
|= ShowAbove
;
2170 #if INSTALL_TOPLEV_HANDLER
2171 // Put top level window event handler into place
2172 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
2174 if ( !m_toplevEvtHandler
)
2175 m_toplevEvtHandler
= new wxComboFrameEventHandler(this);
2177 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2179 ((wxComboFrameEventHandler
*)m_toplevEvtHandler
)->OnPopup();
2180 toplev
->PushEventHandler( m_toplevEvtHandler
);
2184 // Set string selection (must be this way instead of SetStringSelection)
2187 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2188 m_text
->SelectAll();
2190 m_popupInterface
->SetStringValue( m_text
->GetValue() );
2194 // This is neede since focus/selection indication may change when popup is shown
2198 // This must be after SetStringValue
2199 m_popupWinState
= Animating
;
2201 wxRect
popupWinRect( popupX
, popupY
, szp
.x
, szp
.y
);
2204 if ( (m_iFlags
& wxCC_IFLAG_DISABLE_POPUP_ANIM
) ||
2205 AnimateShow( popupWinRect
, showFlags
) )
2207 DoShowPopup( popupWinRect
, showFlags
);
2211 bool wxComboCtrlBase::AnimateShow( const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
) )
2216 void wxComboCtrlBase::DoShowPopup( const wxRect
& rect
, int WXUNUSED(flags
) )
2218 wxWindow
* winPopup
= m_winPopup
;
2220 if ( IsPopupWindowState(Animating
) )
2222 // Make sure the popup window is shown in the right position.
2223 // Should not matter even if animation already did this.
2225 // Some platforms (GTK) may like SetSize and Move to be separate
2226 // (though the bug was probably fixed).
2227 winPopup
->SetSize( rect
);
2229 #if USES_WXPOPUPTRANSIENTWINDOW
2230 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2231 ((wxPopupTransientWindow
*)winPopup
)->Popup(m_popup
);
2236 m_popupWinState
= Visible
;
2238 else if ( IsPopupWindowState(Hidden
) )
2240 // Animation was aborted
2242 wxASSERT( !winPopup
->IsShown() );
2244 m_popupWinState
= Hidden
;
2250 void wxComboCtrlBase::OnPopupDismiss(bool generateEvent
)
2252 // Just in case, avoid double dismiss
2253 if ( IsPopupWindowState(Hidden
) )
2256 // This must be set before focus - otherwise there will be recursive
2257 // OnPopupDismisses.
2258 m_popupWinState
= Hidden
;
2261 m_winPopup
->Disable();
2263 // Inform popup control itself
2264 m_popupInterface
->OnDismiss();
2266 if ( m_popupExtraHandler
)
2267 ((wxComboPopupExtraEventHandler
*)m_popupExtraHandler
)->OnPopupDismiss();
2269 #if INSTALL_TOPLEV_HANDLER
2270 // Remove top level window event handler
2271 if ( m_toplevEvtHandler
)
2273 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2275 toplev
->RemoveEventHandler( m_toplevEvtHandler
);
2279 m_timeCanAcceptClick
= ::wxGetLocalTimeMillis();
2281 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2282 m_timeCanAcceptClick
+= 150;
2284 // If cursor not on dropdown button, then clear its state
2285 // (technically not required by all ports, but do it for all just in case)
2286 if ( !m_btnArea
.Contains(ScreenToClient(::wxGetMousePosition())) )
2289 // Return parent's tab traversal flag.
2290 // See ShowPopup for notes.
2291 if ( m_iFlags
& wxCC_IFLAG_PARENT_TAB_TRAVERSAL
)
2293 wxWindow
* parent
= GetParent();
2294 parent
->SetWindowStyle( parent
->GetWindowStyle() | wxTAB_TRAVERSAL
);
2295 m_iFlags
&= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL
);
2298 // refresh control (necessary even if m_text)
2303 if ( generateEvent
)
2305 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_CLOSEUP
, GetId());
2306 event
.SetEventObject(this);
2307 HandleWindowEvent(event
);
2311 void wxComboCtrlBase::HidePopup(bool generateEvent
)
2313 // Should be able to call this without popup interface
2314 if ( IsPopupWindowState(Hidden
) )
2317 // transfer value and show it in textctrl, if any
2318 if ( !IsPopupWindowState(Animating
) )
2319 SetValue( m_popupInterface
->GetStringValue() );
2323 OnPopupDismiss(generateEvent
);
2326 // ----------------------------------------------------------------------------
2327 // customization methods
2328 // ----------------------------------------------------------------------------
2330 void wxComboCtrlBase::SetButtonPosition( int width
, int height
,
2331 int side
, int spacingX
)
2336 m_btnSpacingX
= spacingX
;
2341 wxSize
wxComboCtrlBase::GetButtonSize()
2343 if ( m_btnSize
.x
> 0 )
2346 wxSize
retSize(m_btnWid
,m_btnHei
);
2348 // Need to call CalculateAreas now if button size is
2349 // is not explicitly specified.
2350 if ( retSize
.x
<= 0 || retSize
.y
<= 0)
2354 retSize
= m_btnSize
;
2360 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap
& bmpNormal
,
2362 const wxBitmap
& bmpPressed
,
2363 const wxBitmap
& bmpHover
,
2364 const wxBitmap
& bmpDisabled
)
2366 m_bmpNormal
= bmpNormal
;
2367 m_blankButtonBg
= blankButtonBg
;
2369 if ( bmpPressed
.Ok() )
2370 m_bmpPressed
= bmpPressed
;
2372 m_bmpPressed
= bmpNormal
;
2374 if ( bmpHover
.Ok() )
2375 m_bmpHover
= bmpHover
;
2377 m_bmpHover
= bmpNormal
;
2379 if ( bmpDisabled
.Ok() )
2380 m_bmpDisabled
= bmpDisabled
;
2382 m_bmpDisabled
= bmpNormal
;
2387 void wxComboCtrlBase::SetCustomPaintWidth( int width
)
2391 // move textctrl accordingly
2392 wxRect r
= m_text
->GetRect();
2393 int inc
= width
- m_widthCustomPaint
;
2396 m_text
->SetSize( r
);
2399 m_widthCustomPaint
= width
;
2404 bool wxComboCtrlBase::DoSetMargins(const wxPoint
& margins
)
2406 // For general sanity's sake, we ignore top margin. Instead
2407 // we will always try to center the text vertically.
2410 if ( margins
.x
!= -1 )
2412 m_marginLeft
= margins
.x
;
2413 m_iFlags
|= wxCC_IFLAG_LEFT_MARGIN_SET
;
2417 m_marginLeft
= GetNativeTextIndent();
2418 m_iFlags
&= ~(wxCC_IFLAG_LEFT_MARGIN_SET
);
2421 if ( margins
.y
!= -1 )
2431 wxPoint
wxComboCtrlBase::DoGetMargins() const
2433 return wxPoint(m_marginLeft
, -1);
2436 #if WXWIN_COMPATIBILITY_2_8
2437 void wxComboCtrlBase::SetTextIndent( int indent
)
2441 m_marginLeft
= GetNativeTextIndent();
2442 m_iFlags
&= ~(wxCC_IFLAG_LEFT_MARGIN_SET
);
2446 m_marginLeft
= indent
;
2447 m_iFlags
|= wxCC_IFLAG_LEFT_MARGIN_SET
;
2453 wxCoord
wxComboCtrlBase::GetTextIndent() const
2455 return m_marginLeft
;
2459 wxCoord
wxComboCtrlBase::GetNativeTextIndent() const
2461 return DEFAULT_TEXT_INDENT
;
2464 // ----------------------------------------------------------------------------
2465 // methods forwarded to wxTextCtrl
2466 // ----------------------------------------------------------------------------
2468 wxString
wxComboCtrlBase::GetValue() const
2471 return m_text
->GetValue();
2472 return m_valueString
;
2475 void wxComboCtrlBase::SetValueWithEvent(const wxString
& value
, bool withEvent
)
2482 m_text
->SetValue(value
);
2484 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2485 m_text
->SelectAll();
2488 // Since wxComboPopup may want to paint the combo as well, we need
2489 // to set the string value here (as well as sometimes in ShowPopup).
2490 if ( m_valueString
!= value
)
2492 m_valueString
= value
;
2494 EnsurePopupControl();
2496 if (m_popupInterface
)
2497 m_popupInterface
->SetStringValue(value
);
2503 void wxComboCtrlBase::SetValue(const wxString
& value
)
2505 SetValueWithEvent(value
, false);
2508 // In this SetValue variant wxComboPopup::SetStringValue is not called
2509 void wxComboCtrlBase::SetText(const wxString
& value
)
2511 // Unlike in SetValue(), this must be called here or
2512 // the behaviour will no be consistent in readonlys.
2513 EnsurePopupControl();
2515 m_valueString
= value
;
2520 m_text
->SetValue( value
);
2526 void wxComboCtrlBase::Copy()
2532 void wxComboCtrlBase::Cut()
2538 void wxComboCtrlBase::Paste()
2544 void wxComboCtrlBase::SetInsertionPoint(long pos
)
2547 m_text
->SetInsertionPoint(pos
);
2550 void wxComboCtrlBase::SetInsertionPointEnd()
2553 m_text
->SetInsertionPointEnd();
2556 long wxComboCtrlBase::GetInsertionPoint() const
2559 return m_text
->GetInsertionPoint();
2564 long wxComboCtrlBase::GetLastPosition() const
2567 return m_text
->GetLastPosition();
2572 void wxComboCtrlBase::Replace(long from
, long to
, const wxString
& value
)
2575 m_text
->Replace(from
, to
, value
);
2578 void wxComboCtrlBase::Remove(long from
, long to
)
2581 m_text
->Remove(from
, to
);
2584 void wxComboCtrlBase::SetSelection(long from
, long to
)
2587 m_text
->SetSelection(from
, to
);
2590 void wxComboCtrlBase::Undo()
2596 bool wxComboCtrlBase::SetHint(const wxString
& hint
)
2601 res
= m_text
->SetHint(hint
);
2606 wxString
wxComboCtrlBase::GetHint() const
2611 #endif // wxUSE_COMBOCTRL