1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/combocmn.cpp
3 // Purpose: wxComboCtrlBase
4 // Author: Jaakko Salli
6 // Created: Apr-30-2006
8 // Copyright: (c) 2005 Jaakko Salli
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
28 #include "wx/combobox.h"
33 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/dialog.h"
37 #include "wx/textctrl.h"
40 #include "wx/tooltip.h"
47 // ----------------------------------------------------------------------------
49 #define DEFAULT_DROPBUTTON_WIDTH 19
51 #define BMP_BUTTON_MARGIN 4
53 #define DEFAULT_POPUP_HEIGHT 400
55 #define DEFAULT_TEXT_INDENT 3
57 #define COMBO_MARGIN 2 // spacing right of wxTextCtrl
60 #if defined(__WXMSW__)
62 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
63 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
64 // native controls work on it like normal.
65 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
66 #define TEXTCTRL_TEXT_CENTERED 0 // 1 if text in textctrl is vertically centered
67 #define FOCUS_RING 0 // No focus ring on wxMSW
69 //#undef wxUSE_POPUPWIN
70 //#define wxUSE_POPUPWIN 0
72 #elif defined(__WXGTK__)
74 // NB: It is not recommended to use wxDialog as popup on wxGTK, because of
75 // this bug: If wxDialog is hidden, its position becomes corrupt
76 // between hide and next show, but without internal coordinates being
77 // reflected (or something like that - atleast commenting out ->Hide()
78 // seemed to eliminate the position change).
80 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
81 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
82 // native controls work on it like normal.
83 #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
84 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
85 #define FOCUS_RING 0 // No focus ring on wxGTK
87 #elif defined(__WXMAC__)
89 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
90 #define TRANSIENT_POPUPWIN_IS_PERFECT 1 // wxPopupTransientWindow works, its child can have focus, and common
91 // native controls work on it like normal.
92 #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
93 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
94 #define FOCUS_RING 3 // Reserve room for the textctrl's focus ring to display
96 #undef DEFAULT_DROPBUTTON_WIDTH
97 #define DEFAULT_DROPBUTTON_WIDTH 22
99 #define COMBO_MARGIN FOCUS_RING
103 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
104 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
105 // native controls work on it like normal.
106 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
107 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
113 // Popupwin is really only supported on wxMSW (not WINCE) and wxGTK, regardless
114 // what the wxUSE_POPUPWIN says.
115 // FIXME: Why isn't wxUSE_POPUPWIN reliable any longer? (it was in wxW2.6.2)
116 #if (!defined(__WXMSW__) && !defined(__WXGTK__) && !defined(__WXMAC__)) || defined(__WXWINCE__)
117 #undef wxUSE_POPUPWIN
118 #define wxUSE_POPUPWIN 0
123 #include "wx/popupwin.h"
125 #undef USE_TRANSIENT_POPUP
126 #define USE_TRANSIENT_POPUP 0
130 // Define different types of popup windows
134 POPUPWIN_WXPOPUPTRANSIENTWINDOW
= 1,
135 POPUPWIN_WXPOPUPWINDOW
= 2,
136 POPUPWIN_WXDIALOG
= 3
140 #if USE_TRANSIENT_POPUP
141 // wxPopupTransientWindow is implemented
143 #define wxComboPopupWindowBase wxPopupTransientWindow
144 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPTRANSIENTWINDOW
145 #define USES_WXPOPUPTRANSIENTWINDOW 1
147 #if TRANSIENT_POPUPWIN_IS_PERFECT
149 #elif POPUPWIN_IS_PERFECT
150 #define wxComboPopupWindowBase2 wxPopupWindow
151 #define SECONDARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
152 #define USES_WXPOPUPWINDOW 1
154 #define wxComboPopupWindowBase2 wxDialog
155 #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG
156 #define USES_WXDIALOG 1
160 // wxPopupWindow (but not wxPopupTransientWindow) is properly implemented
162 #define wxComboPopupWindowBase wxPopupWindow
163 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
164 #define USES_WXPOPUPWINDOW 1
166 #if !POPUPWIN_IS_PERFECT
167 #define wxComboPopupWindowBase2 wxDialog
168 #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG
169 #define USES_WXDIALOG 1
173 // wxPopupWindow is not implemented
175 #define wxComboPopupWindowBase wxDialog
176 #define PRIMARY_POPUP_TYPE POPUPWIN_WXDIALOG
177 #define USES_WXDIALOG 1
182 #ifndef USES_WXPOPUPTRANSIENTWINDOW
183 #define USES_WXPOPUPTRANSIENTWINDOW 0
186 #ifndef USES_WXPOPUPWINDOW
187 #define USES_WXPOPUPWINDOW 0
190 #ifndef USES_WXDIALOG
191 #define USES_WXDIALOG 0
195 #if USES_WXPOPUPWINDOW
196 #define INSTALL_TOPLEV_HANDLER 1
198 #define INSTALL_TOPLEV_HANDLER 0
204 // * wxComboPopupWindow for external use (ie. replace old wxUniv wxPopupComboWindow)
208 // ----------------------------------------------------------------------------
209 // wxComboFrameEventHandler takes care of hiding the popup when events happen
210 // in its top level parent.
211 // ----------------------------------------------------------------------------
213 #if INSTALL_TOPLEV_HANDLER
216 // This will no longer be necessary after wxTransientPopupWindow
217 // works well on all platforms.
220 class wxComboFrameEventHandler
: public wxEvtHandler
223 wxComboFrameEventHandler( wxComboCtrlBase
* pCb
);
224 virtual ~wxComboFrameEventHandler();
228 void OnIdle( wxIdleEvent
& event
);
229 void OnMouseEvent( wxMouseEvent
& event
);
230 void OnActivate( wxActivateEvent
& event
);
231 void OnResize( wxSizeEvent
& event
);
232 void OnMove( wxMoveEvent
& event
);
233 void OnMenuEvent( wxMenuEvent
& event
);
234 void OnClose( wxCloseEvent
& event
);
237 wxWindow
* m_focusStart
;
238 wxComboCtrlBase
* m_combo
;
241 DECLARE_EVENT_TABLE()
244 BEGIN_EVENT_TABLE(wxComboFrameEventHandler
, wxEvtHandler
)
245 EVT_IDLE(wxComboFrameEventHandler::OnIdle
)
246 EVT_LEFT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
247 EVT_RIGHT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
248 EVT_SIZE(wxComboFrameEventHandler::OnResize
)
249 EVT_MOVE(wxComboFrameEventHandler::OnMove
)
250 EVT_MENU_HIGHLIGHT(wxID_ANY
,wxComboFrameEventHandler::OnMenuEvent
)
251 EVT_MENU_OPEN(wxComboFrameEventHandler::OnMenuEvent
)
252 EVT_ACTIVATE(wxComboFrameEventHandler::OnActivate
)
253 EVT_CLOSE(wxComboFrameEventHandler::OnClose
)
256 wxComboFrameEventHandler::wxComboFrameEventHandler( wxComboCtrlBase
* combo
)
262 wxComboFrameEventHandler::~wxComboFrameEventHandler()
266 void wxComboFrameEventHandler::OnPopup()
268 m_focusStart
= ::wxWindow::FindFocus();
271 void wxComboFrameEventHandler::OnIdle( wxIdleEvent
& event
)
273 wxWindow
* winFocused
= ::wxWindow::FindFocus();
275 wxWindow
* popup
= m_combo
->GetPopupControl()->GetControl();
276 wxWindow
* winpopup
= m_combo
->GetPopupWindow();
279 winFocused
!= m_focusStart
&&
280 winFocused
!= popup
&&
281 winFocused
->GetParent() != popup
&&
282 winFocused
!= winpopup
&&
283 winFocused
->GetParent() != winpopup
&&
284 winFocused
!= m_combo
&&
285 winFocused
!= m_combo
->GetButton() // GTK (atleast) requires this
288 m_combo
->HidePopup();
294 void wxComboFrameEventHandler::OnMenuEvent( wxMenuEvent
& event
)
296 m_combo
->HidePopup();
300 void wxComboFrameEventHandler::OnMouseEvent( wxMouseEvent
& event
)
302 m_combo
->HidePopup();
306 void wxComboFrameEventHandler::OnClose( wxCloseEvent
& event
)
308 m_combo
->HidePopup();
312 void wxComboFrameEventHandler::OnActivate( wxActivateEvent
& event
)
314 m_combo
->HidePopup();
318 void wxComboFrameEventHandler::OnResize( wxSizeEvent
& event
)
320 m_combo
->HidePopup();
324 void wxComboFrameEventHandler::OnMove( wxMoveEvent
& event
)
326 m_combo
->HidePopup();
330 #endif // INSTALL_TOPLEV_HANDLER
332 // ----------------------------------------------------------------------------
333 // wxComboPopupWindow is, in essence, wxPopupWindow customized for
335 // ----------------------------------------------------------------------------
337 class wxComboPopupWindow
: public wxComboPopupWindowBase
341 wxComboPopupWindow( wxComboCtrlBase
*parent
,
343 #if USES_WXPOPUPWINDOW || USES_WXPOPUPTRANSIENTWINDOW
344 : wxComboPopupWindowBase(parent
,style
)
346 : wxComboPopupWindowBase(parent
,
357 #if USES_WXPOPUPTRANSIENTWINDOW
358 virtual bool Show( bool show
);
359 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
361 virtual void OnDismiss();
369 #if USES_WXPOPUPTRANSIENTWINDOW
370 bool wxComboPopupWindow::Show( bool show
)
372 // Guard against recursion
374 return wxComboPopupWindowBase::Show(show
);
378 wxASSERT( IsKindOf(CLASSINFO(wxPopupTransientWindow
)) );
380 wxPopupTransientWindow
* ptw
= (wxPopupTransientWindow
*) this;
382 if ( show
!= ptw
->IsShown() )
385 // We used to do wxPopupTransientWindow::Popup here,
386 // but this would hide normal Show, which we are
387 // also going to need.
398 bool wxComboPopupWindow::ProcessLeftDown(wxMouseEvent
& event
)
400 return wxPopupTransientWindow::ProcessLeftDown(event
);
403 // First thing that happens when a transient popup closes is that this method gets called.
404 void wxComboPopupWindow::OnDismiss()
406 wxComboCtrlBase
* combo
= (wxComboCtrlBase
*) GetParent();
407 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxComboCtrlBase
)),
408 wxT("parent might not be wxComboCtrl, but check IMPLEMENT_DYNAMIC_CLASS(2) macro for correctness") );
410 combo
->OnPopupDismiss();
412 #endif // USES_WXPOPUPTRANSIENTWINDOW
415 // ----------------------------------------------------------------------------
416 // wxComboPopupWindowEvtHandler does bulk of the custom event handling
417 // of a popup window. It is separate so we can have different types
419 // ----------------------------------------------------------------------------
421 class wxComboPopupWindowEvtHandler
: public wxEvtHandler
425 wxComboPopupWindowEvtHandler( wxComboCtrlBase
*parent
)
430 void OnSizeEvent( wxSizeEvent
& event
);
431 void OnKeyEvent(wxKeyEvent
& event
);
433 void OnActivate( wxActivateEvent
& event
);
437 wxComboCtrlBase
* m_combo
;
439 DECLARE_EVENT_TABLE()
443 BEGIN_EVENT_TABLE(wxComboPopupWindowEvtHandler
, wxEvtHandler
)
444 EVT_KEY_DOWN(wxComboPopupWindowEvtHandler::OnKeyEvent
)
445 EVT_KEY_UP(wxComboPopupWindowEvtHandler::OnKeyEvent
)
447 EVT_ACTIVATE(wxComboPopupWindowEvtHandler::OnActivate
)
449 EVT_SIZE(wxComboPopupWindowEvtHandler::OnSizeEvent
)
453 void wxComboPopupWindowEvtHandler::OnSizeEvent( wxSizeEvent
& WXUNUSED(event
) )
455 // Block the event so that the popup control does not get auto-resized.
458 void wxComboPopupWindowEvtHandler::OnKeyEvent( wxKeyEvent
& event
)
460 // Relay keyboard event to the main child controls
461 wxWindowList children
= m_combo
->GetPopupWindow()->GetChildren();
462 wxWindowList::iterator node
= children
.begin();
463 wxWindow
* child
= (wxWindow
*)*node
;
464 child
->GetEventHandler()->AddPendingEvent(event
);
468 void wxComboPopupWindowEvtHandler::OnActivate( wxActivateEvent
& event
)
470 if ( !event
.GetActive() )
472 // Tell combo control that we are dismissed.
473 m_combo
->HidePopup();
481 // ----------------------------------------------------------------------------
484 // ----------------------------------------------------------------------------
486 wxComboPopup::~wxComboPopup()
490 void wxComboPopup::OnPopup()
494 void wxComboPopup::OnDismiss()
498 wxSize
wxComboPopup::GetAdjustedSize( int minWidth
,
500 int WXUNUSED(maxHeight
) )
502 return wxSize(minWidth
,prefHeight
);
505 void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase
* combo
,
506 wxDC
& dc
, const wxRect
& rect
)
508 if ( combo
->GetWindowStyle() & wxCB_READONLY
) // ie. no textctrl
510 combo
->PrepareBackground(dc
,rect
,0);
512 dc
.DrawText( combo
->GetValue(),
513 rect
.x
+ combo
->GetTextIndent(),
514 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
518 void wxComboPopup::PaintComboControl( wxDC
& dc
, const wxRect
& rect
)
520 DefaultPaintComboControl(m_combo
,dc
,rect
);
523 void wxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
528 void wxComboPopup::OnComboDoubleClick()
532 void wxComboPopup::SetStringValue( const wxString
& WXUNUSED(value
) )
536 bool wxComboPopup::LazyCreate()
541 void wxComboPopup::Dismiss()
543 m_combo
->HidePopup();
546 // ----------------------------------------------------------------------------
548 // ----------------------------------------------------------------------------
551 // This is pushed to the event handler queue of the child textctrl.
553 class wxComboBoxExtraInputHandler
: public wxEvtHandler
557 wxComboBoxExtraInputHandler( wxComboCtrlBase
* combo
)
562 virtual ~wxComboBoxExtraInputHandler() { }
563 void OnKey(wxKeyEvent
& event
);
564 void OnFocus(wxFocusEvent
& event
);
567 wxComboCtrlBase
* m_combo
;
570 DECLARE_EVENT_TABLE()
574 BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler
, wxEvtHandler
)
575 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey
)
576 EVT_KEY_UP(wxComboBoxExtraInputHandler::OnKey
)
577 EVT_CHAR(wxComboBoxExtraInputHandler::OnKey
)
578 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
579 EVT_KILL_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
583 void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent
& event
)
585 // Let the wxComboCtrl event handler have a go first.
586 wxComboCtrlBase
* combo
= m_combo
;
588 wxKeyEvent
redirectedEvent(event
);
589 redirectedEvent
.SetId(combo
->GetId());
590 redirectedEvent
.SetEventObject(combo
);
592 if ( !combo
->GetEventHandler()->ProcessEvent(redirectedEvent
) )
594 // Don't let TAB through to the text ctrl - looks ugly
595 if ( event
.GetKeyCode() != WXK_TAB
)
600 void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent
& event
)
602 // FIXME: This code does run when control is clicked,
603 // yet on Windows it doesn't select all the text.
604 if ( event
.GetEventType() == wxEVT_SET_FOCUS
&&
605 !(m_combo
->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT
) )
607 if ( m_combo
->GetTextCtrl() )
608 m_combo
->GetTextCtrl()->SelectAll();
610 m_combo
->SetSelection(-1,-1);
613 // Send focus indication to parent.
614 // NB: This is needed for cases where the textctrl gets focus
615 // instead of its parent. While this may trigger multiple
616 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
617 // from combo's focus event handler), they should be quite
619 wxFocusEvent
evt2(event
.GetEventType(),m_combo
->GetId());
620 evt2
.SetEventObject(m_combo
);
621 m_combo
->GetEventHandler()->ProcessEvent(evt2
);
628 // This is pushed to the event handler queue of the control in popup.
631 class wxComboPopupExtraEventHandler
: public wxEvtHandler
635 wxComboPopupExtraEventHandler( wxComboCtrlBase
* combo
)
639 m_beenInside
= false;
641 virtual ~wxComboPopupExtraEventHandler() { }
643 void OnMouseEvent( wxMouseEvent
& event
);
645 // Called from wxComboCtrlBase::OnPopupDismiss
646 void OnPopupDismiss()
648 m_beenInside
= false;
652 wxComboCtrlBase
* m_combo
;
657 DECLARE_EVENT_TABLE()
661 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler
, wxEvtHandler
)
662 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent
)
666 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent
& event
)
668 wxPoint pt
= event
.GetPosition();
669 wxSize sz
= m_combo
->GetPopupControl()->GetControl()->GetClientSize();
670 int evtType
= event
.GetEventType();
671 bool isInside
= pt
.x
>= 0 && pt
.y
>= 0 && pt
.x
< sz
.x
&& pt
.y
< sz
.y
;
672 bool relayToButton
= false;
676 if ( evtType
== wxEVT_MOTION
||
677 evtType
== wxEVT_LEFT_DOWN
||
678 evtType
== wxEVT_RIGHT_DOWN
)
680 // Block motion and click events outside the popup
681 if ( !isInside
|| !m_combo
->IsPopupShown() )
686 else if ( evtType
== wxEVT_LEFT_UP
)
688 if ( !m_combo
->IsPopupShown() )
691 relayToButton
= true;
693 else if ( !m_beenInside
)
701 relayToButton
= true;
709 // Some mouse events to popup that happen outside it, before cursor
710 // has been inside the popup, need to be ignored by it but relayed to
713 wxWindow
* eventSink
= m_combo
;
714 wxWindow
* btn
= m_combo
->GetButton();
718 eventSink
->GetEventHandler()->ProcessEvent(event
);
722 // ----------------------------------------------------------------------------
723 // wxComboCtrlTextCtrl
724 // ----------------------------------------------------------------------------
726 class wxComboCtrlTextCtrl
: public wxTextCtrl
729 wxComboCtrlTextCtrl() : wxTextCtrl() { }
730 virtual ~wxComboCtrlTextCtrl() { }
732 virtual wxWindow
*GetMainWindowOfCompositeControl()
734 wxComboCtrl
* combo
= (wxComboCtrl
*) GetParent();
736 // Returning this instead of just 'parent' lets FindFocus work
737 // correctly even when parent control is a child of a composite
738 // generic control (as is case with wxGenericDatePickerCtrl).
739 return combo
->GetMainWindowOfCompositeControl();
743 // ----------------------------------------------------------------------------
745 // ----------------------------------------------------------------------------
748 BEGIN_EVENT_TABLE(wxComboCtrlBase
, wxControl
)
749 EVT_TEXT(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
750 EVT_SIZE(wxComboCtrlBase::OnSizeEvent
)
751 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent
)
752 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent
)
753 EVT_IDLE(wxComboCtrlBase::OnIdleEvent
)
754 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
755 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent
)
756 EVT_TEXT_ENTER(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
757 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged
)
761 IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase
, wxControl
)
763 void wxComboCtrlBase::Init()
767 m_popupWinState
= Hidden
;
770 m_popupInterface
= NULL
;
772 m_popupExtraHandler
= NULL
;
773 m_textEvtHandler
= NULL
;
775 #if INSTALL_TOPLEV_HANDLER
776 m_toplevEvtHandler
= NULL
;
779 m_mainCtrlWnd
= this;
782 m_widthMinPopup
= -1;
784 m_widthCustomPaint
= 0;
785 m_widthCustomBorder
= 0;
789 m_blankButtonBg
= false;
791 m_popupWinType
= POPUPWIN_NONE
;
792 m_btnWid
= m_btnHei
= -1;
800 m_timeCanAcceptClick
= 0;
802 m_resetFocus
= false;
805 bool wxComboCtrlBase::Create(wxWindow
*parent
,
807 const wxString
& value
,
811 const wxValidator
& validator
,
812 const wxString
& name
)
814 if ( !wxControl::Create(parent
,
818 style
| wxWANTS_CHARS
,
823 m_valueString
= value
;
827 m_absIndent
= GetNativeTextIndent();
829 m_iFlags
|= wxCC_IFLAG_CREATED
;
831 // If x and y indicate valid size, wxSizeEvent won't be
832 // emitted automatically, so we need to add artifical one.
833 if ( size
.x
> 0 && size
.y
> 0 )
835 wxSizeEvent
evt(size
,GetId());
836 GetEventHandler()->AddPendingEvent(evt
);
842 void wxComboCtrlBase::InstallInputHandlers()
846 m_textEvtHandler
= new wxComboBoxExtraInputHandler(this);
847 m_text
->PushEventHandler(m_textEvtHandler
);
852 wxComboCtrlBase::CreateTextCtrl(int style
, const wxValidator
& validator
)
854 if ( !(m_windowStyle
& wxCB_READONLY
) )
859 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
860 // not used by the wxPropertyGrid and therefore the tab is processed by
861 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
862 // navigation event is then sent to the wrong window.
863 style
|= wxTE_PROCESS_TAB
;
865 if ( HasFlag(wxTE_PROCESS_ENTER
) )
866 style
|= wxTE_PROCESS_ENTER
;
868 // Ignore EVT_TEXT generated by the constructor (but only
869 // if the event redirector already exists)
870 // NB: This must be " = 1" instead of "++";
871 if ( m_textEvtHandler
)
876 m_text
= new wxComboCtrlTextCtrl();
877 m_text
->Create(this, wxID_ANY
, m_valueString
,
878 wxDefaultPosition
, wxSize(10,-1),
883 void wxComboCtrlBase::OnThemeChange()
885 // Leave the default bg on the Mac so the area used by the focus ring will
886 // be the correct colour and themed brush. Instead we'll use
887 // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed.
889 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
893 wxComboCtrlBase::~wxComboCtrlBase()
898 #if INSTALL_TOPLEV_HANDLER
899 delete ((wxComboFrameEventHandler
*)m_toplevEvtHandler
);
900 m_toplevEvtHandler
= NULL
;
906 m_text
->RemoveEventHandler(m_textEvtHandler
);
908 delete m_textEvtHandler
;
912 // ----------------------------------------------------------------------------
914 // ----------------------------------------------------------------------------
916 // Recalculates button and textctrl areas
917 void wxComboCtrlBase::CalculateAreas( int btnWidth
)
919 wxSize sz
= GetClientSize();
920 int customBorder
= m_widthCustomBorder
;
921 int btnBorder
; // border for button only
923 // check if button should really be outside the border: we'll do it it if
924 // its platform default or bitmap+pushbutton background is used, but not if
925 // there is vertical size adjustment or horizontal spacing.
926 if ( ( (m_iFlags
& wxCC_BUTTON_OUTSIDE_BORDER
) ||
927 (m_bmpNormal
.Ok() && m_blankButtonBg
) ) &&
928 m_btnSpacingX
== 0 &&
931 m_iFlags
|= wxCC_IFLAG_BUTTON_OUTSIDE
;
934 else if ( (m_iFlags
& wxCC_BUTTON_COVERS_BORDER
) &&
935 m_btnSpacingX
== 0 && !m_bmpNormal
.Ok() )
937 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
942 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
943 btnBorder
= customBorder
;
946 // Defaul indentation
947 if ( m_absIndent
< 0 )
948 m_absIndent
= GetNativeTextIndent();
950 int butWidth
= btnWidth
;
953 butWidth
= m_btnWidDefault
;
955 m_btnWidDefault
= butWidth
;
960 int butHeight
= sz
.y
- btnBorder
*2;
962 // Adjust button width
967 // Adjust button width to match aspect ratio
968 // (but only if control is smaller than best size).
969 int bestHeight
= GetBestSize().y
;
970 int height
= GetSize().y
;
972 if ( height
< bestHeight
)
974 // Make very small buttons square, as it makes
975 // them accommodate arrow image better and still
978 butWidth
= (height
*butWidth
)/bestHeight
;
980 butWidth
= butHeight
;
984 // Adjust button height
986 butHeight
= m_btnHei
;
988 // Use size of normal bitmap if...
991 // button width is set to default and blank button bg is not drawn
992 if ( m_bmpNormal
.Ok() )
994 int bmpReqWidth
= m_bmpNormal
.GetWidth();
995 int bmpReqHeight
= m_bmpNormal
.GetHeight();
997 // If drawing blank button background, we need to add some margin.
998 if ( m_blankButtonBg
)
1000 bmpReqWidth
+= BMP_BUTTON_MARGIN
*2;
1001 bmpReqHeight
+= BMP_BUTTON_MARGIN
*2;
1004 if ( butWidth
< bmpReqWidth
|| ( m_btnWid
== 0 && !m_blankButtonBg
) )
1005 butWidth
= bmpReqWidth
;
1006 if ( butHeight
< bmpReqHeight
|| ( m_btnHei
== 0 && !m_blankButtonBg
) )
1007 butHeight
= bmpReqHeight
;
1009 // Need to fix height?
1010 if ( (sz
.y
-(customBorder
*2)) < butHeight
&& btnWidth
== 0 )
1012 int newY
= butHeight
+(customBorder
*2);
1013 SetClientSize(wxDefaultCoord
,newY
);
1014 if ( m_bmpNormal
.Ok() || m_btnArea
.width
!= butWidth
|| m_btnArea
.height
!= butHeight
)
1015 m_iFlags
|= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1017 m_iFlags
&= ~wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1023 int butAreaWid
= butWidth
+ (m_btnSpacingX
*2);
1025 m_btnSize
.x
= butWidth
;
1026 m_btnSize
.y
= butHeight
;
1028 m_btnArea
.x
= ( m_btnSide
==wxRIGHT
? sz
.x
- butAreaWid
- btnBorder
: btnBorder
);
1029 m_btnArea
.y
= btnBorder
+ FOCUS_RING
;
1030 m_btnArea
.width
= butAreaWid
;
1031 m_btnArea
.height
= sz
.y
- ((btnBorder
+FOCUS_RING
)*2);
1033 m_tcArea
.x
= ( m_btnSide
==wxRIGHT
? 0 : butAreaWid
) + customBorder
+ FOCUS_RING
;
1034 m_tcArea
.y
= customBorder
+ FOCUS_RING
;
1035 m_tcArea
.width
= sz
.x
- butAreaWid
- (customBorder
*2) - (FOCUS_RING
*2);
1036 m_tcArea
.height
= sz
.y
- ((customBorder
+FOCUS_RING
)*2);
1041 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
1042 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1047 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
)
1052 #if !TEXTCTRL_TEXT_CENTERED
1054 wxSize sz
= GetClientSize();
1056 int customBorder
= m_widthCustomBorder
;
1057 if ( (m_text
->GetWindowStyleFlag() & wxBORDER_MASK
) == wxNO_BORDER
)
1060 int tcSizeY
= m_text
->GetBestSize().y
;
1061 int diff
= sz
.y
- tcSizeY
;
1062 int y
= textCtrlYAdjust
+ (diff
/2);
1064 if ( y
< customBorder
)
1067 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
+ m_absIndent
+ textCtrlXAdjust
,
1069 m_tcArea
.width
- COMBO_MARGIN
-
1070 (textCtrlXAdjust
+ m_widthCustomPaint
+ m_absIndent
),
1073 // Make sure textctrl doesn't exceed the bottom custom border
1074 wxSize tsz
= m_text
->GetSize();
1075 diff
= (y
+ tsz
.y
) - (sz
.y
- customBorder
);
1078 tsz
.y
= tsz
.y
- diff
- 1;
1079 m_text
->SetSize(tsz
);
1083 #else // TEXTCTRL_TEXT_CENTERED
1084 wxUnusedVar(textCtrlXAdjust
);
1085 wxUnusedVar(textCtrlYAdjust
);
1086 #endif // !TEXTCTRL_TEXT_CENTERED/TEXTCTRL_TEXT_CENTERED
1088 // If it has border, have textctrl will the entire text field.
1089 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
,
1091 m_tcArea
.width
- m_widthCustomPaint
,
1096 wxSize
wxComboCtrlBase::DoGetBestSize() const
1098 wxSize
sizeText(150,0);
1101 sizeText
= m_text
->GetBestSize();
1103 // TODO: Better method to calculate close-to-native control height.
1107 fhei
= (m_font
.GetPointSize()*2) + 5;
1108 else if ( wxNORMAL_FONT
->Ok() )
1109 fhei
= (wxNORMAL_FONT
->GetPointSize()*2) + 5;
1111 fhei
= sizeText
.y
+ 4;
1113 // Need to force height to accomodate bitmap?
1114 int btnSizeY
= m_btnSize
.y
;
1115 if ( m_bmpNormal
.Ok() && fhei
< btnSizeY
)
1118 // Control height doesn't depend on border
1121 int border = m_windowStyle & wxBORDER_MASK;
1122 if ( border == wxSIMPLE_BORDER )
1124 else if ( border == wxNO_BORDER )
1125 fhei += (m_widthCustomBorder*2);
1131 // Final adjustments
1137 // these are the numbers from the HIG:
1138 switch ( m_windowVariant
)
1140 case wxWINDOW_VARIANT_NORMAL
:
1144 case wxWINDOW_VARIANT_SMALL
:
1147 case wxWINDOW_VARIANT_MINI
:
1153 fhei
+= 2 * FOCUS_RING
;
1154 int width
= sizeText
.x
+ FOCUS_RING
+ COMBO_MARGIN
+ DEFAULT_DROPBUTTON_WIDTH
;
1156 wxSize
ret(width
, fhei
);
1161 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent
& event
)
1166 // defined by actual wxComboCtrls
1172 // ----------------------------------------------------------------------------
1173 // standard operations
1174 // ----------------------------------------------------------------------------
1176 bool wxComboCtrlBase::Enable(bool enable
)
1178 if ( !wxControl::Enable(enable
) )
1182 m_btn
->Enable(enable
);
1184 m_text
->Enable(enable
);
1191 bool wxComboCtrlBase::Show(bool show
)
1193 if ( !wxControl::Show(show
) )
1205 bool wxComboCtrlBase::SetFont ( const wxFont
& font
)
1207 if ( !wxControl::SetFont(font
) )
1211 m_text
->SetFont(font
);
1217 void wxComboCtrlBase::DoSetToolTip(wxToolTip
*tooltip
)
1219 wxControl::DoSetToolTip(tooltip
);
1221 // Set tool tip for button and text box
1224 const wxString
&tip
= tooltip
->GetTip();
1225 if ( m_text
) m_text
->SetToolTip(tip
);
1226 if ( m_btn
) m_btn
->SetToolTip(tip
);
1230 if ( m_text
) m_text
->SetToolTip( NULL
);
1231 if ( m_btn
) m_btn
->SetToolTip( NULL
);
1234 #endif // wxUSE_TOOLTIPS
1236 #if wxUSE_VALIDATORS
1237 void wxComboCtrlBase::SetValidator(const wxValidator
& validator
)
1239 wxTextCtrl
* textCtrl
= GetTextCtrl();
1242 textCtrl
->SetValidator( validator
);
1244 wxControl::SetValidator( validator
);
1247 wxValidator
* wxComboCtrlBase::GetValidator()
1249 wxTextCtrl
* textCtrl
= GetTextCtrl();
1251 return textCtrl
? textCtrl
->GetValidator() : wxControl::GetValidator();
1253 #endif // wxUSE_VALIDATORS
1255 // ----------------------------------------------------------------------------
1257 // ----------------------------------------------------------------------------
1259 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1260 // prepare combo box background on area in a way typical on platform
1261 void wxComboCtrlBase::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
1263 wxSize sz
= GetClientSize();
1265 bool doDrawFocusRect
; // also selected
1267 // For smaller size control (and for disabled background) use less spacing
1271 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1274 isEnabled
= IsEnabled();
1275 doDrawFocusRect
= ShouldDrawFocus() && !(m_iFlags
& wxCC_FULL_BUTTON
);
1277 // Windows-style: for smaller size control (and for disabled background) use less spacing
1278 focusSpacingX
= isEnabled
? 2 : 1;
1279 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
1283 // Drawing a list item
1284 isEnabled
= true; // they are never disabled
1285 doDrawFocusRect
= (flags
& wxCONTROL_SELECTED
) != 0;
1291 // Set the background sub-rectangle for selection, disabled etc
1292 wxRect
selRect(rect
);
1293 selRect
.y
+= focusSpacingY
;
1294 selRect
.height
-= (focusSpacingY
*2);
1298 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1299 wcp
+= m_widthCustomPaint
;
1301 selRect
.x
+= wcp
+ focusSpacingX
;
1302 selRect
.width
-= wcp
+ (focusSpacingX
*2);
1305 bool doDrawSelRect
= true;
1309 // If popup is hidden and this control is focused,
1310 // then draw the focus-indicator (selbgcolor background etc.).
1311 if ( doDrawFocusRect
)
1313 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1314 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1318 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
1319 #ifndef __WXMAC__ // see note in OnThemeChange
1320 doDrawSelRect
= false;
1321 bgCol
= GetBackgroundColour();
1323 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1329 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
1330 #ifndef __WXMAC__ // see note in OnThemeChange
1331 bgCol
= GetBackgroundColour();
1333 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1337 dc
.SetBrush( bgCol
);
1338 if ( doDrawSelRect
)
1341 dc
.DrawRectangle( selRect
);
1344 // Don't clip exactly to the selection rectangle so we can draw
1345 // to the non-selected area in front of it.
1346 wxRect
clipRect(rect
.x
,rect
.y
,
1347 (selRect
.x
+selRect
.width
)-rect
.x
,rect
.height
);
1348 dc
.SetClippingRegion(clipRect
);
1351 // Save the library size a bit for platforms that re-implement this.
1352 void wxComboCtrlBase::PrepareBackground( wxDC
&, const wxRect
&, int ) const
1357 void wxComboCtrlBase::DrawButton( wxDC
& dc
, const wxRect
& rect
, int flags
)
1359 int drawState
= m_btnState
;
1361 if ( (m_iFlags
& wxCC_BUTTON_STAYS_DOWN
) &&
1362 GetPopupWindowState() >= Animating
)
1363 drawState
|= wxCONTROL_PRESSED
;
1365 wxRect
drawRect(rect
.x
+m_btnSpacingX
,
1366 rect
.y
+((rect
.height
-m_btnSize
.y
)/2),
1370 // Make sure area is not larger than the control
1371 if ( drawRect
.y
< rect
.y
)
1372 drawRect
.y
= rect
.y
;
1373 if ( drawRect
.height
> rect
.height
)
1374 drawRect
.height
= rect
.height
;
1376 bool enabled
= IsEnabled();
1379 drawState
|= wxCONTROL_DISABLED
;
1381 if ( !m_bmpNormal
.Ok() )
1383 if ( flags
& Button_BitmapOnly
)
1386 // Need to clear button background even if m_btn is present
1387 if ( flags
& Button_PaintBackground
)
1391 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
1392 bgCol
= GetParent()->GetBackgroundColour();
1394 bgCol
= GetBackgroundColour();
1398 dc
.DrawRectangle(rect
);
1401 // Draw standard button
1402 wxRendererNative::Get().DrawComboBoxDropButton(this,
1414 pBmp
= &m_bmpDisabled
;
1415 else if ( m_btnState
& wxCONTROL_PRESSED
)
1416 pBmp
= &m_bmpPressed
;
1417 else if ( m_btnState
& wxCONTROL_CURRENT
)
1420 pBmp
= &m_bmpNormal
;
1422 if ( m_blankButtonBg
)
1424 // If using blank button background, we need to clear its background
1425 // with button face colour instead of colour for rest of the control.
1426 if ( flags
& Button_PaintBackground
)
1428 wxColour bgCol
= GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1429 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1432 dc
.DrawRectangle(rect
);
1435 if ( !(flags
& Button_BitmapOnly
) )
1437 wxRendererNative::Get().DrawPushButton(this,
1446 // Need to clear button background even if m_btn is present
1447 // (assume non-button background was cleared just before this call so brushes are good)
1448 if ( flags
& Button_PaintBackground
)
1449 dc
.DrawRectangle(rect
);
1452 // Draw bitmap centered in drawRect
1453 dc
.DrawBitmap(*pBmp
,
1454 drawRect
.x
+ (drawRect
.width
-pBmp
->GetWidth())/2,
1455 drawRect
.y
+ (drawRect
.height
-pBmp
->GetHeight())/2,
1460 void wxComboCtrlBase::RecalcAndRefresh()
1464 wxSizeEvent
evt(GetSize(),GetId());
1465 GetEventHandler()->ProcessEvent(evt
);
1470 // ----------------------------------------------------------------------------
1471 // miscellaneous event handlers
1472 // ----------------------------------------------------------------------------
1474 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent
& event
)
1476 if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1478 if ( m_ignoreEvtText
> 0 )
1485 // Change event id, object and string before relaying it forward
1486 event
.SetId(GetId());
1487 wxString s
= event
.GetString();
1488 event
.SetEventObject(this);
1493 // call if cursor is on button area or mouse is captured for the button
1494 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent
& event
,
1497 int type
= event
.GetEventType();
1499 if ( type
== wxEVT_MOTION
)
1501 if ( (flags
& wxCC_MF_ON_BUTTON
) &&
1502 IsPopupWindowState(Hidden
) )
1504 if ( !(m_btnState
& wxCONTROL_CURRENT
) )
1506 // Mouse hover begins
1507 m_btnState
|= wxCONTROL_CURRENT
;
1508 if ( HasCapture() ) // Retain pressed state.
1509 m_btnState
|= wxCONTROL_PRESSED
;
1513 else if ( (m_btnState
& wxCONTROL_CURRENT
) )
1516 m_btnState
&= ~(wxCONTROL_CURRENT
|wxCONTROL_PRESSED
);
1520 else if ( type
== wxEVT_LEFT_DOWN
|| type
== wxEVT_LEFT_DCLICK
)
1522 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1524 m_btnState
|= wxCONTROL_PRESSED
;
1527 if ( !(m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
) )
1530 // If showing popup now, do not capture mouse or there will be interference
1534 else if ( type
== wxEVT_LEFT_UP
)
1537 // Only accept event if mouse was left-press was previously accepted
1541 if ( m_btnState
& wxCONTROL_PRESSED
)
1543 // If mouse was inside, fire the click event.
1544 if ( m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
)
1546 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1550 m_btnState
&= ~(wxCONTROL_PRESSED
);
1554 else if ( type
== wxEVT_LEAVE_WINDOW
)
1556 if ( m_btnState
& (wxCONTROL_CURRENT
|wxCONTROL_PRESSED
) )
1558 m_btnState
&= ~(wxCONTROL_CURRENT
);
1561 if ( IsPopupWindowState(Hidden
) )
1563 m_btnState
&= ~(wxCONTROL_PRESSED
);
1571 // Never have 'hot' state when popup is being shown
1572 // (this is mostly needed because of the animation).
1573 if ( !IsPopupWindowState(Hidden
) )
1574 m_btnState
&= ~wxCONTROL_CURRENT
;
1579 // returns true if event was consumed or filtered
1580 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent
& event
,
1581 int WXUNUSED(flags
) )
1583 wxLongLong t
= ::wxGetLocalTimeMillis();
1584 int evtType
= event
.GetEventType();
1586 #if USES_WXPOPUPWINDOW || USES_WXDIALOG
1587 if ( m_popupWinType
!= POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1589 if ( IsPopupWindowState(Visible
) &&
1590 ( evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_RIGHT_DOWN
) )
1598 // Filter out clicks on button immediately after popup dismiss
1599 if ( evtType
== wxEVT_LEFT_DOWN
&& t
< m_timeCanAcceptClick
)
1601 event
.SetEventType(0);
1608 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent
& event
)
1610 int evtType
= event
.GetEventType();
1612 if ( (evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_LEFT_DCLICK
) &&
1613 (m_windowStyle
& wxCB_READONLY
) )
1615 if ( GetPopupWindowState() >= Animating
)
1617 #if USES_WXPOPUPWINDOW
1618 // Click here always hides the popup.
1619 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1625 if ( !(m_windowStyle
& wxCC_SPECIAL_DCLICK
) )
1627 // In read-only mode, clicking the text is the
1628 // same as clicking the button.
1631 else if ( /*evtType == wxEVT_LEFT_UP || */evtType
== wxEVT_LEFT_DCLICK
)
1633 //if ( m_popupInterface->CycleValue() )
1635 if ( m_popupInterface
)
1636 m_popupInterface
->OnComboDoubleClick();
1641 if ( IsPopupShown() )
1643 // relay (some) mouse events to the popup
1644 if ( evtType
== wxEVT_MOUSEWHEEL
)
1645 m_popup
->GetEventHandler()->AddPendingEvent(event
);
1651 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent
& event
)
1653 if ( IsPopupShown() )
1655 // pass it to the popped up control
1656 GetPopupControl()->GetControl()->GetEventHandler()->AddPendingEvent(event
);
1660 if ( GetParent()->HasFlag(wxTAB_TRAVERSAL
) &&
1661 HandleAsNavigationKey(event
) )
1664 if ( IsKeyPopupToggle(event
) )
1670 int comboStyle
= GetWindowStyle();
1671 wxComboPopup
* popupInterface
= GetPopupControl();
1673 if ( !popupInterface
)
1679 int keycode
= event
.GetKeyCode();
1681 if ( (comboStyle
& wxCB_READONLY
) ||
1682 (keycode
!= WXK_RIGHT
&& keycode
!= WXK_LEFT
) )
1684 popupInterface
->OnComboKeyEvent(event
);
1691 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent
& event
)
1693 if ( event
.GetEventType() == wxEVT_SET_FOCUS
)
1695 wxWindow
* tc
= GetTextCtrl();
1696 if ( tc
&& tc
!= DoFindFocus() )
1698 m_resetFocus
= true;
1709 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent
& WXUNUSED(event
) )
1713 m_resetFocus
= false;
1714 wxWindow
* tc
= GetTextCtrl();
1720 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
1723 // indentation may also have changed
1724 if ( !(m_iFlags
& wxCC_IFLAG_INDENT_SET
) )
1725 m_absIndent
= GetNativeTextIndent();
1729 // ----------------------------------------------------------------------------
1731 // ----------------------------------------------------------------------------
1733 // Create popup window and the child control
1734 void wxComboCtrlBase::CreatePopup()
1736 wxComboPopup
* popupInterface
= m_popupInterface
;
1741 #ifdef wxComboPopupWindowBase2
1742 if ( m_iFlags
& wxCC_IFLAG_USE_ALT_POPUP
)
1745 m_winPopup
= new wxComboPopupWindowBase2( this, wxNO_BORDER
);
1747 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
, wxEmptyString
,
1748 wxPoint(-21,-21), wxSize(20, 20),
1751 m_popupWinType
= SECONDARY_POPUP_TYPE
;
1754 #endif // wxComboPopupWindowBase2
1756 m_winPopup
= new wxComboPopupWindow( this, wxNO_BORDER
);
1757 m_popupWinType
= PRIMARY_POPUP_TYPE
;
1759 m_popupWinEvtHandler
= new wxComboPopupWindowEvtHandler(this);
1760 m_winPopup
->PushEventHandler(m_popupWinEvtHandler
);
1763 popupInterface
->Create(m_winPopup
);
1764 m_popup
= popup
= popupInterface
->GetControl();
1766 m_popupExtraHandler
= new wxComboPopupExtraEventHandler(this);
1767 popup
->PushEventHandler( m_popupExtraHandler
);
1769 // This may be helpful on some platforms
1770 // (eg. it bypasses a wxGTK popupwindow bug where
1771 // window is not initially hidden when it should be)
1774 popupInterface
->m_iFlags
|= wxCP_IFLAG_CREATED
;
1777 // Destroy popup window and the child control
1778 void wxComboCtrlBase::DestroyPopup()
1783 m_popup
->RemoveEventHandler(m_popupExtraHandler
);
1785 delete m_popupExtraHandler
;
1787 delete m_popupInterface
;
1791 m_winPopup
->RemoveEventHandler(m_popupWinEvtHandler
);
1792 delete m_popupWinEvtHandler
;
1793 m_popupWinEvtHandler
= NULL
;
1794 m_winPopup
->Destroy();
1797 m_popupExtraHandler
= NULL
;
1798 m_popupInterface
= NULL
;
1803 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup
* iface
)
1805 wxCHECK_RET( iface
, wxT("no popup interface set for wxComboCtrl") );
1809 iface
->InitBase(this);
1812 m_popupInterface
= iface
;
1814 if ( !iface
->LazyCreate() )
1823 // This must be done after creation
1824 if ( m_valueString
.length() )
1826 iface
->SetStringValue(m_valueString
);
1831 // Ensures there is atleast the default popup
1832 void wxComboCtrlBase::EnsurePopupControl()
1834 if ( !m_popupInterface
)
1835 SetPopupControl(NULL
);
1838 void wxComboCtrlBase::OnButtonClick()
1840 // Derived classes can override this method for totally custom
1842 if ( !IsPopupWindowState(Visible
) )
1848 void wxComboCtrlBase::ShowPopup()
1850 EnsurePopupControl();
1851 wxCHECK_RET( !IsPopupWindowState(Visible
), wxT("popup window already shown") );
1853 if ( IsPopupWindowState(Animating
) )
1858 // Space above and below
1864 wxSize ctrlSz
= GetSize();
1866 screenHeight
= wxSystemSettings::GetMetric( wxSYS_SCREEN_Y
);
1867 scrPos
= GetParent()->ClientToScreen(GetPosition());
1869 spaceAbove
= scrPos
.y
;
1870 spaceBelow
= screenHeight
- spaceAbove
- ctrlSz
.y
;
1872 maxHeightPopup
= spaceBelow
;
1873 if ( spaceAbove
> spaceBelow
)
1874 maxHeightPopup
= spaceAbove
;
1877 int widthPopup
= ctrlSz
.x
+ m_extLeft
+ m_extRight
;
1879 if ( widthPopup
< m_widthMinPopup
)
1880 widthPopup
= m_widthMinPopup
;
1882 wxWindow
* winPopup
= m_winPopup
;
1885 // Need to disable tab traversal of parent
1887 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1888 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1889 // that if transient popup is open, then tab traversal is to be ignored.
1890 // However, I think this code would still be needed for cases where
1891 // transient popup doesn't work yet (wxWinCE?).
1892 wxWindow
* parent
= GetParent();
1893 int parentFlags
= parent
->GetWindowStyle();
1894 if ( parentFlags
& wxTAB_TRAVERSAL
)
1896 parent
->SetWindowStyle( parentFlags
& ~(wxTAB_TRAVERSAL
) );
1897 m_iFlags
|= wxCC_IFLAG_PARENT_TAB_TRAVERSAL
;
1903 winPopup
= m_winPopup
;
1913 wxASSERT( !m_popup
|| m_popup
== popup
); // Consistency check.
1915 wxSize adjustedSize
= m_popupInterface
->GetAdjustedSize(widthPopup
,
1916 m_heightPopup
<=0?DEFAULT_POPUP_HEIGHT
:m_heightPopup
,
1919 popup
->SetSize(adjustedSize
);
1921 m_popupInterface
->OnPopup();
1924 // Reposition and resize popup window
1927 wxSize szp
= popup
->GetSize();
1930 int popupY
= scrPos
.y
+ ctrlSz
.y
;
1932 // Default anchor is wxLEFT
1933 int anchorSide
= m_anchorSide
;
1935 anchorSide
= wxLEFT
;
1937 int rightX
= scrPos
.x
+ ctrlSz
.x
+ m_extRight
- szp
.x
;
1938 int leftX
= scrPos
.x
- m_extLeft
;
1940 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
1943 int screenWidth
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
);
1945 // If there is not enough horizontal space, anchor on the other side.
1946 // If there is no space even then, place the popup at x 0.
1947 if ( anchorSide
== wxRIGHT
)
1951 if ( (leftX
+szp
.x
) < screenWidth
)
1952 anchorSide
= wxLEFT
;
1959 if ( (leftX
+szp
.x
) >= screenWidth
)
1962 anchorSide
= wxRIGHT
;
1968 // Select x coordinate according to the anchor side
1969 if ( anchorSide
== wxRIGHT
)
1971 else if ( anchorSide
== wxLEFT
)
1976 int showFlags
= CanDeferShow
;
1978 if ( spaceBelow
< szp
.y
)
1980 popupY
= scrPos
.y
- szp
.y
;
1981 showFlags
|= ShowAbove
;
1984 #if INSTALL_TOPLEV_HANDLER
1985 // Put top level window event handler into place
1986 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1988 if ( !m_toplevEvtHandler
)
1989 m_toplevEvtHandler
= new wxComboFrameEventHandler(this);
1991 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
1993 ((wxComboFrameEventHandler
*)m_toplevEvtHandler
)->OnPopup();
1994 toplev
->PushEventHandler( m_toplevEvtHandler
);
1998 // Set string selection (must be this way instead of SetStringSelection)
2001 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2002 m_text
->SelectAll();
2004 m_popupInterface
->SetStringValue( m_text
->GetValue() );
2008 // This is neede since focus/selection indication may change when popup is shown
2012 // This must be after SetStringValue
2013 m_popupWinState
= Animating
;
2015 wxRect
popupWinRect( popupX
, popupY
, szp
.x
, szp
.y
);
2018 if ( (m_iFlags
& wxCC_IFLAG_DISABLE_POPUP_ANIM
) ||
2019 AnimateShow( popupWinRect
, showFlags
) )
2021 DoShowPopup( popupWinRect
, showFlags
);
2025 bool wxComboCtrlBase::AnimateShow( const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
) )
2030 void wxComboCtrlBase::DoShowPopup( const wxRect
& rect
, int WXUNUSED(flags
) )
2032 wxWindow
* winPopup
= m_winPopup
;
2034 if ( IsPopupWindowState(Animating
) )
2036 // Make sure the popup window is shown in the right position.
2037 // Should not matter even if animation already did this.
2039 // Some platforms (GTK) may like SetSize and Move to be separate
2040 // (though the bug was probably fixed).
2041 winPopup
->SetSize( rect
);
2043 #if USES_WXPOPUPTRANSIENTWINDOW
2044 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2045 ((wxPopupTransientWindow
*)winPopup
)->Popup(m_popup
);
2050 m_popupWinState
= Visible
;
2052 else if ( IsPopupWindowState(Hidden
) )
2054 // Animation was aborted
2056 wxASSERT( !winPopup
->IsShown() );
2058 m_popupWinState
= Hidden
;
2064 void wxComboCtrlBase::OnPopupDismiss()
2066 // Just in case, avoid double dismiss
2067 if ( IsPopupWindowState(Hidden
) )
2070 // This must be set before focus - otherwise there will be recursive
2071 // OnPopupDismisses.
2072 m_popupWinState
= Hidden
;
2075 m_winPopup
->Disable();
2077 // Inform popup control itself
2078 m_popupInterface
->OnDismiss();
2080 if ( m_popupExtraHandler
)
2081 ((wxComboPopupExtraEventHandler
*)m_popupExtraHandler
)->OnPopupDismiss();
2083 #if INSTALL_TOPLEV_HANDLER
2084 // Remove top level window event handler
2085 if ( m_toplevEvtHandler
)
2087 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2089 toplev
->RemoveEventHandler( m_toplevEvtHandler
);
2093 m_timeCanAcceptClick
= ::wxGetLocalTimeMillis();
2095 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2096 m_timeCanAcceptClick
+= 150;
2098 // If cursor not on dropdown button, then clear its state
2099 // (technically not required by all ports, but do it for all just in case)
2100 if ( !m_btnArea
.Contains(ScreenToClient(::wxGetMousePosition())) )
2103 // Return parent's tab traversal flag.
2104 // See ShowPopup for notes.
2105 if ( m_iFlags
& wxCC_IFLAG_PARENT_TAB_TRAVERSAL
)
2107 wxWindow
* parent
= GetParent();
2108 parent
->SetWindowStyle( parent
->GetWindowStyle() | wxTAB_TRAVERSAL
);
2109 m_iFlags
&= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL
);
2112 // refresh control (necessary even if m_text)
2118 void wxComboCtrlBase::HidePopup()
2120 // Should be able to call this without popup interface
2121 if ( IsPopupWindowState(Hidden
) )
2124 // transfer value and show it in textctrl, if any
2125 if ( !IsPopupWindowState(Animating
) )
2126 SetValue( m_popupInterface
->GetStringValue() );
2133 // ----------------------------------------------------------------------------
2134 // customization methods
2135 // ----------------------------------------------------------------------------
2137 void wxComboCtrlBase::SetButtonPosition( int width
, int height
,
2138 int side
, int spacingX
)
2143 m_btnSpacingX
= spacingX
;
2148 wxSize
wxComboCtrlBase::GetButtonSize()
2150 if ( m_btnSize
.x
> 0 )
2153 wxSize
retSize(m_btnWid
,m_btnHei
);
2155 // Need to call CalculateAreas now if button size is
2156 // is not explicitly specified.
2157 if ( retSize
.x
<= 0 || retSize
.y
<= 0)
2161 retSize
= m_btnSize
;
2167 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap
& bmpNormal
,
2169 const wxBitmap
& bmpPressed
,
2170 const wxBitmap
& bmpHover
,
2171 const wxBitmap
& bmpDisabled
)
2173 m_bmpNormal
= bmpNormal
;
2174 m_blankButtonBg
= blankButtonBg
;
2176 if ( bmpPressed
.Ok() )
2177 m_bmpPressed
= bmpPressed
;
2179 m_bmpPressed
= bmpNormal
;
2181 if ( bmpHover
.Ok() )
2182 m_bmpHover
= bmpHover
;
2184 m_bmpHover
= bmpNormal
;
2186 if ( bmpDisabled
.Ok() )
2187 m_bmpDisabled
= bmpDisabled
;
2189 m_bmpDisabled
= bmpNormal
;
2194 void wxComboCtrlBase::SetCustomPaintWidth( int width
)
2198 // move textctrl accordingly
2199 wxRect r
= m_text
->GetRect();
2200 int inc
= width
- m_widthCustomPaint
;
2203 m_text
->SetSize( r
);
2206 m_widthCustomPaint
= width
;
2211 void wxComboCtrlBase::SetTextIndent( int indent
)
2215 m_absIndent
= GetNativeTextIndent();
2216 m_iFlags
&= ~(wxCC_IFLAG_INDENT_SET
);
2220 m_absIndent
= indent
;
2221 m_iFlags
|= wxCC_IFLAG_INDENT_SET
;
2227 wxCoord
wxComboCtrlBase::GetNativeTextIndent() const
2229 return DEFAULT_TEXT_INDENT
;
2232 // ----------------------------------------------------------------------------
2233 // methods forwarded to wxTextCtrl
2234 // ----------------------------------------------------------------------------
2236 wxString
wxComboCtrlBase::GetValue() const
2239 return m_text
->GetValue();
2240 return m_valueString
;
2243 void wxComboCtrlBase::SetValueWithEvent(const wxString
& value
, bool withEvent
)
2250 m_text
->SetValue(value
);
2252 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2253 m_text
->SelectAll();
2256 // Since wxComboPopup may want to paint the combo as well, we need
2257 // to set the string value here (as well as sometimes in ShowPopup).
2258 if ( m_valueString
!= value
)
2260 m_valueString
= value
;
2262 EnsurePopupControl();
2264 if (m_popupInterface
)
2265 m_popupInterface
->SetStringValue(value
);
2271 void wxComboCtrlBase::SetValue(const wxString
& value
)
2273 SetValueWithEvent(value
, false);
2276 // In this SetValue variant wxComboPopup::SetStringValue is not called
2277 void wxComboCtrlBase::SetText(const wxString
& value
)
2279 // Unlike in SetValue(), this must be called here or
2280 // the behaviour will no be consistent in readonlys.
2281 EnsurePopupControl();
2283 m_valueString
= value
;
2288 m_text
->SetValue( value
);
2294 void wxComboCtrlBase::Copy()
2300 void wxComboCtrlBase::Cut()
2306 void wxComboCtrlBase::Paste()
2312 void wxComboCtrlBase::SetInsertionPoint(long pos
)
2315 m_text
->SetInsertionPoint(pos
);
2318 void wxComboCtrlBase::SetInsertionPointEnd()
2321 m_text
->SetInsertionPointEnd();
2324 long wxComboCtrlBase::GetInsertionPoint() const
2327 return m_text
->GetInsertionPoint();
2332 long wxComboCtrlBase::GetLastPosition() const
2335 return m_text
->GetLastPosition();
2340 void wxComboCtrlBase::Replace(long from
, long to
, const wxString
& value
)
2343 m_text
->Replace(from
, to
, value
);
2346 void wxComboCtrlBase::Remove(long from
, long to
)
2349 m_text
->Remove(from
, to
);
2352 void wxComboCtrlBase::SetSelection(long from
, long to
)
2355 m_text
->SetSelection(from
, to
);
2358 void wxComboCtrlBase::Undo()
2364 #endif // wxUSE_COMBOCTRL