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
->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_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
577 EVT_KILL_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
581 void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent
& event
)
583 // Let the wxComboCtrl event handler have a go first.
584 wxComboCtrlBase
* combo
= m_combo
;
585 wxObject
* prevObj
= event
.GetEventObject();
587 event
.SetId(combo
->GetId());
588 event
.SetEventObject(combo
);
589 combo
->GetEventHandler()->ProcessEvent(event
);
591 event
.SetId(((wxWindow
*)prevObj
)->GetId());
592 event
.SetEventObject(prevObj
);
595 void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent
& event
)
597 // FIXME: This code does run when control is clicked,
598 // yet on Windows it doesn't select all the text.
599 if ( event
.GetEventType() == wxEVT_SET_FOCUS
&&
600 !(m_combo
->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT
) )
602 if ( m_combo
->GetTextCtrl() )
603 m_combo
->GetTextCtrl()->SelectAll();
605 m_combo
->SetSelection(-1,-1);
608 // Send focus indication to parent.
609 // NB: This is needed for cases where the textctrl gets focus
610 // instead of its parent. While this may trigger multiple
611 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
612 // from combo's focus event handler), they should be quite
614 wxFocusEvent
evt2(event
.GetEventType(),m_combo
->GetId());
615 evt2
.SetEventObject(m_combo
);
616 m_combo
->GetEventHandler()->ProcessEvent(evt2
);
623 // This is pushed to the event handler queue of the control in popup.
626 class wxComboPopupExtraEventHandler
: public wxEvtHandler
630 wxComboPopupExtraEventHandler( wxComboCtrlBase
* combo
)
634 m_beenInside
= false;
636 virtual ~wxComboPopupExtraEventHandler() { }
638 void OnMouseEvent( wxMouseEvent
& event
);
640 // Called from wxComboCtrlBase::OnPopupDismiss
641 void OnPopupDismiss()
643 m_beenInside
= false;
647 wxComboCtrlBase
* m_combo
;
652 DECLARE_EVENT_TABLE()
656 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler
, wxEvtHandler
)
657 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent
)
661 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent
& event
)
663 wxPoint pt
= event
.GetPosition();
664 wxSize sz
= m_combo
->GetPopupControl()->GetControl()->GetClientSize();
665 int evtType
= event
.GetEventType();
666 bool isInside
= pt
.x
>= 0 && pt
.y
>= 0 && pt
.x
< sz
.x
&& pt
.y
< sz
.y
;
667 bool relayToButton
= false;
671 if ( evtType
== wxEVT_MOTION
||
672 evtType
== wxEVT_LEFT_DOWN
||
673 evtType
== wxEVT_RIGHT_DOWN
)
675 // Block motion and click events outside the popup
676 if ( !isInside
|| !m_combo
->IsPopupShown() )
681 else if ( evtType
== wxEVT_LEFT_UP
)
683 if ( !m_combo
->IsPopupShown() )
686 relayToButton
= true;
688 else if ( !m_beenInside
)
696 relayToButton
= true;
704 // Some mouse events to popup that happen outside it, before cursor
705 // has been inside the popup, need to be ignored by it but relayed to
708 wxWindow
* eventSink
= m_combo
;
709 wxWindow
* btn
= m_combo
->GetButton();
713 eventSink
->GetEventHandler()->ProcessEvent(event
);
717 // ----------------------------------------------------------------------------
718 // wxComboCtrlTextCtrl
719 // ----------------------------------------------------------------------------
721 class wxComboCtrlTextCtrl
: public wxTextCtrl
724 wxComboCtrlTextCtrl() : wxTextCtrl() { }
725 virtual ~wxComboCtrlTextCtrl() { }
727 virtual wxWindow
*GetMainWindowOfCompositeControl()
729 wxComboCtrl
* combo
= (wxComboCtrl
*) GetParent();
731 // Returning this instead of just 'parent' lets FindFocus work
732 // correctly even when parent control is a child of a composite
733 // generic control (as is case with wxGenericDatePickerCtrl).
734 return combo
->GetMainWindowOfCompositeControl();
738 // ----------------------------------------------------------------------------
740 // ----------------------------------------------------------------------------
743 BEGIN_EVENT_TABLE(wxComboCtrlBase
, wxControl
)
744 EVT_TEXT(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
745 EVT_SIZE(wxComboCtrlBase::OnSizeEvent
)
746 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent
)
747 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent
)
748 EVT_IDLE(wxComboCtrlBase::OnIdleEvent
)
749 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
750 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent
)
751 EVT_TEXT_ENTER(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
752 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged
)
756 IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase
, wxControl
)
758 void wxComboCtrlBase::Init()
760 m_winPopup
= (wxWindow
*)NULL
;
761 m_popup
= (wxWindow
*)NULL
;
762 m_popupWinState
= Hidden
;
763 m_btn
= (wxWindow
*) NULL
;
764 m_text
= (wxTextCtrl
*) NULL
;
765 m_popupInterface
= (wxComboPopup
*) NULL
;
767 m_popupExtraHandler
= (wxEvtHandler
*) NULL
;
768 m_textEvtHandler
= (wxEvtHandler
*) NULL
;
770 #if INSTALL_TOPLEV_HANDLER
771 m_toplevEvtHandler
= (wxEvtHandler
*) NULL
;
774 m_mainCtrlWnd
= this;
777 m_widthMinPopup
= -1;
779 m_widthCustomPaint
= 0;
780 m_widthCustomBorder
= 0;
784 m_blankButtonBg
= false;
786 m_popupWinType
= POPUPWIN_NONE
;
787 m_btnWid
= m_btnHei
= -1;
795 m_timeCanAcceptClick
= 0;
797 m_resetFocus
= false;
800 bool wxComboCtrlBase::Create(wxWindow
*parent
,
802 const wxString
& value
,
806 const wxValidator
& validator
,
807 const wxString
& name
)
809 if ( !wxControl::Create(parent
,
813 style
| wxWANTS_CHARS
,
818 m_valueString
= value
;
822 m_absIndent
= GetNativeTextIndent();
824 m_iFlags
|= wxCC_IFLAG_CREATED
;
826 // If x and y indicate valid size, wxSizeEvent won't be
827 // emitted automatically, so we need to add artifical one.
828 if ( size
.x
> 0 && size
.y
> 0 )
830 wxSizeEvent
evt(size
,GetId());
831 GetEventHandler()->AddPendingEvent(evt
);
837 void wxComboCtrlBase::InstallInputHandlers()
841 m_textEvtHandler
= new wxComboBoxExtraInputHandler(this);
842 m_text
->PushEventHandler(m_textEvtHandler
);
847 wxComboCtrlBase::CreateTextCtrl(int style
, const wxValidator
& validator
)
849 if ( !(m_windowStyle
& wxCB_READONLY
) )
854 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
855 // not used by the wxPropertyGrid and therefore the tab is processed by
856 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
857 // navigation event is then sent to the wrong window.
858 style
|= wxTE_PROCESS_TAB
;
860 if ( HasFlag(wxTE_PROCESS_ENTER
) )
861 style
|= wxTE_PROCESS_ENTER
;
863 // Ignore EVT_TEXT generated by the constructor (but only
864 // if the event redirector already exists)
865 // NB: This must be " = 1" instead of "++";
866 if ( m_textEvtHandler
)
871 m_text
= new wxComboCtrlTextCtrl();
872 m_text
->Create(this, wxID_ANY
, m_valueString
,
873 wxDefaultPosition
, wxSize(10,-1),
878 void wxComboCtrlBase::OnThemeChange()
880 // Leave the default bg on the Mac so the area used by the focus ring will
881 // be the correct colour and themed brush. Instead we'll use
882 // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed.
884 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
888 wxComboCtrlBase::~wxComboCtrlBase()
893 #if INSTALL_TOPLEV_HANDLER
894 delete ((wxComboFrameEventHandler
*)m_toplevEvtHandler
);
895 m_toplevEvtHandler
= (wxEvtHandler
*) NULL
;
901 m_text
->RemoveEventHandler(m_textEvtHandler
);
903 delete m_textEvtHandler
;
907 // ----------------------------------------------------------------------------
909 // ----------------------------------------------------------------------------
911 // Recalculates button and textctrl areas
912 void wxComboCtrlBase::CalculateAreas( int btnWidth
)
914 wxSize sz
= GetClientSize();
915 int customBorder
= m_widthCustomBorder
;
916 int btnBorder
; // border for button only
918 // check if button should really be outside the border: we'll do it it if
919 // its platform default or bitmap+pushbutton background is used, but not if
920 // there is vertical size adjustment or horizontal spacing.
921 if ( ( (m_iFlags
& wxCC_BUTTON_OUTSIDE_BORDER
) ||
922 (m_bmpNormal
.Ok() && m_blankButtonBg
) ) &&
923 m_btnSpacingX
== 0 &&
926 m_iFlags
|= wxCC_IFLAG_BUTTON_OUTSIDE
;
929 else if ( (m_iFlags
& wxCC_BUTTON_COVERS_BORDER
) &&
930 m_btnSpacingX
== 0 && !m_bmpNormal
.Ok() )
932 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
937 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
938 btnBorder
= customBorder
;
941 // Defaul indentation
942 if ( m_absIndent
< 0 )
943 m_absIndent
= GetNativeTextIndent();
945 int butWidth
= btnWidth
;
948 butWidth
= m_btnWidDefault
;
950 m_btnWidDefault
= butWidth
;
955 int butHeight
= sz
.y
- btnBorder
*2;
957 // Adjust button width
962 // Adjust button width to match aspect ratio
963 // (but only if control is smaller than best size).
964 int bestHeight
= GetBestSize().y
;
965 int height
= GetSize().y
;
967 if ( height
< bestHeight
)
969 // Make very small buttons square, as it makes
970 // them accommodate arrow image better and still
973 butWidth
= (height
*butWidth
)/bestHeight
;
975 butWidth
= butHeight
;
979 // Adjust button height
981 butHeight
= m_btnHei
;
983 // Use size of normal bitmap if...
986 // button width is set to default and blank button bg is not drawn
987 if ( m_bmpNormal
.Ok() )
989 int bmpReqWidth
= m_bmpNormal
.GetWidth();
990 int bmpReqHeight
= m_bmpNormal
.GetHeight();
992 // If drawing blank button background, we need to add some margin.
993 if ( m_blankButtonBg
)
995 bmpReqWidth
+= BMP_BUTTON_MARGIN
*2;
996 bmpReqHeight
+= BMP_BUTTON_MARGIN
*2;
999 if ( butWidth
< bmpReqWidth
|| ( m_btnWid
== 0 && !m_blankButtonBg
) )
1000 butWidth
= bmpReqWidth
;
1001 if ( butHeight
< bmpReqHeight
|| ( m_btnHei
== 0 && !m_blankButtonBg
) )
1002 butHeight
= bmpReqHeight
;
1004 // Need to fix height?
1005 if ( (sz
.y
-(customBorder
*2)) < butHeight
&& btnWidth
== 0 )
1007 int newY
= butHeight
+(customBorder
*2);
1008 SetClientSize(wxDefaultCoord
,newY
);
1009 if ( m_bmpNormal
.Ok() || m_btnArea
.width
!= butWidth
|| m_btnArea
.height
!= butHeight
)
1010 m_iFlags
|= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1012 m_iFlags
&= ~wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1018 int butAreaWid
= butWidth
+ (m_btnSpacingX
*2);
1020 m_btnSize
.x
= butWidth
;
1021 m_btnSize
.y
= butHeight
;
1023 m_btnArea
.x
= ( m_btnSide
==wxRIGHT
? sz
.x
- butAreaWid
- btnBorder
: btnBorder
);
1024 m_btnArea
.y
= btnBorder
+ FOCUS_RING
;
1025 m_btnArea
.width
= butAreaWid
;
1026 m_btnArea
.height
= sz
.y
- ((btnBorder
+FOCUS_RING
)*2);
1028 m_tcArea
.x
= ( m_btnSide
==wxRIGHT
? 0 : butAreaWid
) + customBorder
+ FOCUS_RING
;
1029 m_tcArea
.y
= customBorder
+ FOCUS_RING
;
1030 m_tcArea
.width
= sz
.x
- butAreaWid
- (customBorder
*2) - (FOCUS_RING
*2);
1031 m_tcArea
.height
= sz
.y
- ((customBorder
+FOCUS_RING
)*2);
1036 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
1037 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1042 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
)
1047 #if !TEXTCTRL_TEXT_CENTERED
1049 wxSize sz
= GetClientSize();
1051 int customBorder
= m_widthCustomBorder
;
1052 if ( (m_text
->GetWindowStyleFlag() & wxBORDER_MASK
) == wxNO_BORDER
)
1055 int tcSizeY
= m_text
->GetBestSize().y
;
1056 int diff
= sz
.y
- tcSizeY
;
1057 int y
= textCtrlYAdjust
+ (diff
/2);
1059 if ( y
< customBorder
)
1062 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
+ m_absIndent
+ textCtrlXAdjust
,
1064 m_tcArea
.width
- COMBO_MARGIN
-
1065 (textCtrlXAdjust
+ m_widthCustomPaint
+ m_absIndent
),
1068 // Make sure textctrl doesn't exceed the bottom custom border
1069 wxSize tsz
= m_text
->GetSize();
1070 diff
= (y
+ tsz
.y
) - (sz
.y
- customBorder
);
1073 tsz
.y
= tsz
.y
- diff
- 1;
1074 m_text
->SetSize(tsz
);
1078 #else // TEXTCTRL_TEXT_CENTERED
1079 wxUnusedVar(textCtrlXAdjust
);
1080 wxUnusedVar(textCtrlYAdjust
);
1081 #endif // !TEXTCTRL_TEXT_CENTERED/TEXTCTRL_TEXT_CENTERED
1083 // If it has border, have textctrl will the entire text field.
1084 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
,
1086 m_tcArea
.width
- m_widthCustomPaint
,
1091 wxSize
wxComboCtrlBase::DoGetBestSize() const
1093 wxSize
sizeText(150,0);
1096 sizeText
= m_text
->GetBestSize();
1098 // TODO: Better method to calculate close-to-native control height.
1102 fhei
= (m_font
.GetPointSize()*2) + 5;
1103 else if ( wxNORMAL_FONT
->Ok() )
1104 fhei
= (wxNORMAL_FONT
->GetPointSize()*2) + 5;
1106 fhei
= sizeText
.y
+ 4;
1108 // Need to force height to accomodate bitmap?
1109 int btnSizeY
= m_btnSize
.y
;
1110 if ( m_bmpNormal
.Ok() && fhei
< btnSizeY
)
1113 // Control height doesn't depend on border
1116 int border = m_windowStyle & wxBORDER_MASK;
1117 if ( border == wxSIMPLE_BORDER )
1119 else if ( border == wxNO_BORDER )
1120 fhei += (m_widthCustomBorder*2);
1126 // Final adjustments
1132 // these are the numbers from the HIG:
1133 switch ( m_windowVariant
)
1135 case wxWINDOW_VARIANT_NORMAL
:
1139 case wxWINDOW_VARIANT_SMALL
:
1142 case wxWINDOW_VARIANT_MINI
:
1148 fhei
+= 2 * FOCUS_RING
;
1149 int width
= sizeText
.x
+ FOCUS_RING
+ COMBO_MARGIN
+ DEFAULT_DROPBUTTON_WIDTH
;
1151 wxSize
ret(width
, fhei
);
1156 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent
& event
)
1161 // defined by actual wxComboCtrls
1167 // ----------------------------------------------------------------------------
1168 // standard operations
1169 // ----------------------------------------------------------------------------
1171 bool wxComboCtrlBase::Enable(bool enable
)
1173 if ( !wxControl::Enable(enable
) )
1177 m_btn
->Enable(enable
);
1179 m_text
->Enable(enable
);
1184 bool wxComboCtrlBase::Show(bool show
)
1186 if ( !wxControl::Show(show
) )
1198 bool wxComboCtrlBase::SetFont ( const wxFont
& font
)
1200 if ( !wxControl::SetFont(font
) )
1204 m_text
->SetFont(font
);
1210 void wxComboCtrlBase::DoSetToolTip(wxToolTip
*tooltip
)
1212 wxControl::DoSetToolTip(tooltip
);
1214 // Set tool tip for button and text box
1217 const wxString
&tip
= tooltip
->GetTip();
1218 if ( m_text
) m_text
->SetToolTip(tip
);
1219 if ( m_btn
) m_btn
->SetToolTip(tip
);
1223 if ( m_text
) m_text
->SetToolTip( (wxToolTip
*) NULL
);
1224 if ( m_btn
) m_btn
->SetToolTip( (wxToolTip
*) NULL
);
1227 #endif // wxUSE_TOOLTIPS
1229 #if wxUSE_VALIDATORS
1230 void wxComboCtrlBase::SetValidator(const wxValidator
& validator
)
1232 wxTextCtrl
* textCtrl
= GetTextCtrl();
1235 textCtrl
->SetValidator( validator
);
1237 wxControl::SetValidator( validator
);
1240 wxValidator
* wxComboCtrlBase::GetValidator()
1242 wxTextCtrl
* textCtrl
= GetTextCtrl();
1244 return textCtrl
? textCtrl
->GetValidator() : wxControl::GetValidator();
1246 #endif // wxUSE_VALIDATORS
1248 // ----------------------------------------------------------------------------
1250 // ----------------------------------------------------------------------------
1252 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1253 // prepare combo box background on area in a way typical on platform
1254 void wxComboCtrlBase::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
1256 wxSize sz
= GetClientSize();
1258 bool doDrawFocusRect
; // also selected
1260 // For smaller size control (and for disabled background) use less spacing
1264 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1267 isEnabled
= IsEnabled();
1268 doDrawFocusRect
= ShouldDrawFocus() && !(m_iFlags
& wxCC_FULL_BUTTON
);
1270 // Windows-style: for smaller size control (and for disabled background) use less spacing
1271 focusSpacingX
= isEnabled
? 2 : 1;
1272 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
1276 // Drawing a list item
1277 isEnabled
= true; // they are never disabled
1278 doDrawFocusRect
= (flags
& wxCONTROL_SELECTED
) != 0;
1284 // Set the background sub-rectangle for selection, disabled etc
1285 wxRect
selRect(rect
);
1286 selRect
.y
+= focusSpacingY
;
1287 selRect
.height
-= (focusSpacingY
*2);
1291 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1292 wcp
+= m_widthCustomPaint
;
1294 selRect
.x
+= wcp
+ focusSpacingX
;
1295 selRect
.width
-= wcp
+ (focusSpacingX
*2);
1298 bool doDrawSelRect
= true;
1302 // If popup is hidden and this control is focused,
1303 // then draw the focus-indicator (selbgcolor background etc.).
1304 if ( doDrawFocusRect
)
1306 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1307 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1311 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
1312 #ifndef __WXMAC__ // see note in OnThemeChange
1313 doDrawSelRect
= false;
1314 bgCol
= GetBackgroundColour();
1316 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1322 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
1323 #ifndef __WXMAC__ // see note in OnThemeChange
1324 bgCol
= GetBackgroundColour();
1326 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1330 dc
.SetBrush( bgCol
);
1331 if ( doDrawSelRect
)
1334 dc
.DrawRectangle( selRect
);
1337 // Don't clip exactly to the selection rectangle so we can draw
1338 // to the non-selected area in front of it.
1339 wxRect
clipRect(rect
.x
,rect
.y
,
1340 (selRect
.x
+selRect
.width
)-rect
.x
,rect
.height
);
1341 dc
.SetClippingRegion(clipRect
);
1344 // Save the library size a bit for platforms that re-implement this.
1345 void wxComboCtrlBase::PrepareBackground( wxDC
&, const wxRect
&, int ) const
1350 void wxComboCtrlBase::DrawButton( wxDC
& dc
, const wxRect
& rect
, int flags
)
1352 int drawState
= m_btnState
;
1354 if ( (m_iFlags
& wxCC_BUTTON_STAYS_DOWN
) &&
1355 GetPopupWindowState() >= Animating
)
1356 drawState
|= wxCONTROL_PRESSED
;
1358 wxRect
drawRect(rect
.x
+m_btnSpacingX
,
1359 rect
.y
+((rect
.height
-m_btnSize
.y
)/2),
1363 // Make sure area is not larger than the control
1364 if ( drawRect
.y
< rect
.y
)
1365 drawRect
.y
= rect
.y
;
1366 if ( drawRect
.height
> rect
.height
)
1367 drawRect
.height
= rect
.height
;
1369 bool enabled
= IsEnabled();
1372 drawState
|= wxCONTROL_DISABLED
;
1374 if ( !m_bmpNormal
.Ok() )
1376 if ( flags
& Button_BitmapOnly
)
1379 // Need to clear button background even if m_btn is present
1380 if ( flags
& Button_PaintBackground
)
1384 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
1385 bgCol
= GetParent()->GetBackgroundColour();
1387 bgCol
= GetBackgroundColour();
1391 dc
.DrawRectangle(rect
);
1394 // Draw standard button
1395 wxRendererNative::Get().DrawComboBoxDropButton(this,
1407 pBmp
= &m_bmpDisabled
;
1408 else if ( m_btnState
& wxCONTROL_PRESSED
)
1409 pBmp
= &m_bmpPressed
;
1410 else if ( m_btnState
& wxCONTROL_CURRENT
)
1413 pBmp
= &m_bmpNormal
;
1415 if ( m_blankButtonBg
)
1417 // If using blank button background, we need to clear its background
1418 // with button face colour instead of colour for rest of the control.
1419 if ( flags
& Button_PaintBackground
)
1421 wxColour bgCol
= GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1422 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1425 dc
.DrawRectangle(rect
);
1428 if ( !(flags
& Button_BitmapOnly
) )
1430 wxRendererNative::Get().DrawPushButton(this,
1439 // Need to clear button background even if m_btn is present
1440 // (assume non-button background was cleared just before this call so brushes are good)
1441 if ( flags
& Button_PaintBackground
)
1442 dc
.DrawRectangle(rect
);
1445 // Draw bitmap centered in drawRect
1446 dc
.DrawBitmap(*pBmp
,
1447 drawRect
.x
+ (drawRect
.width
-pBmp
->GetWidth())/2,
1448 drawRect
.y
+ (drawRect
.height
-pBmp
->GetHeight())/2,
1453 void wxComboCtrlBase::RecalcAndRefresh()
1457 wxSizeEvent
evt(GetSize(),GetId());
1458 GetEventHandler()->ProcessEvent(evt
);
1463 // ----------------------------------------------------------------------------
1464 // miscellaneous event handlers
1465 // ----------------------------------------------------------------------------
1467 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent
& event
)
1469 if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1471 if ( m_ignoreEvtText
> 0 )
1478 // Change event id, object and string before relaying it forward
1479 event
.SetId(GetId());
1480 wxString s
= event
.GetString();
1481 event
.SetEventObject(this);
1486 // call if cursor is on button area or mouse is captured for the button
1487 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent
& event
,
1490 int type
= event
.GetEventType();
1492 if ( type
== wxEVT_MOTION
)
1494 if ( (flags
& wxCC_MF_ON_BUTTON
) &&
1495 IsPopupWindowState(Hidden
) )
1497 if ( !(m_btnState
& wxCONTROL_CURRENT
) )
1499 // Mouse hover begins
1500 m_btnState
|= wxCONTROL_CURRENT
;
1501 if ( HasCapture() ) // Retain pressed state.
1502 m_btnState
|= wxCONTROL_PRESSED
;
1506 else if ( (m_btnState
& wxCONTROL_CURRENT
) )
1509 m_btnState
&= ~(wxCONTROL_CURRENT
|wxCONTROL_PRESSED
);
1513 else if ( type
== wxEVT_LEFT_DOWN
|| type
== wxEVT_LEFT_DCLICK
)
1515 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1517 m_btnState
|= wxCONTROL_PRESSED
;
1520 if ( !(m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
) )
1523 // If showing popup now, do not capture mouse or there will be interference
1527 else if ( type
== wxEVT_LEFT_UP
)
1530 // Only accept event if mouse was left-press was previously accepted
1534 if ( m_btnState
& wxCONTROL_PRESSED
)
1536 // If mouse was inside, fire the click event.
1537 if ( m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
)
1539 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1543 m_btnState
&= ~(wxCONTROL_PRESSED
);
1547 else if ( type
== wxEVT_LEAVE_WINDOW
)
1549 if ( m_btnState
& (wxCONTROL_CURRENT
|wxCONTROL_PRESSED
) )
1551 m_btnState
&= ~(wxCONTROL_CURRENT
);
1554 if ( IsPopupWindowState(Hidden
) )
1556 m_btnState
&= ~(wxCONTROL_PRESSED
);
1564 // Never have 'hot' state when popup is being shown
1565 // (this is mostly needed because of the animation).
1566 if ( !IsPopupWindowState(Hidden
) )
1567 m_btnState
&= ~wxCONTROL_CURRENT
;
1572 // returns true if event was consumed or filtered
1573 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent
& event
,
1574 int WXUNUSED(flags
) )
1576 wxLongLong t
= ::wxGetLocalTimeMillis();
1577 int evtType
= event
.GetEventType();
1579 #if USES_WXPOPUPWINDOW || USES_WXDIALOG
1580 if ( m_popupWinType
!= POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1582 if ( IsPopupWindowState(Visible
) &&
1583 ( evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_RIGHT_DOWN
) )
1591 // Filter out clicks on button immediately after popup dismiss
1592 if ( evtType
== wxEVT_LEFT_DOWN
&& t
< m_timeCanAcceptClick
)
1594 event
.SetEventType(0);
1601 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent
& event
)
1603 int evtType
= event
.GetEventType();
1605 if ( (evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_LEFT_DCLICK
) &&
1606 (m_windowStyle
& wxCB_READONLY
) )
1608 if ( GetPopupWindowState() >= Animating
)
1610 #if USES_WXPOPUPWINDOW
1611 // Click here always hides the popup.
1612 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1618 if ( !(m_windowStyle
& wxCC_SPECIAL_DCLICK
) )
1620 // In read-only mode, clicking the text is the
1621 // same as clicking the button.
1624 else if ( /*evtType == wxEVT_LEFT_UP || */evtType
== wxEVT_LEFT_DCLICK
)
1626 //if ( m_popupInterface->CycleValue() )
1628 if ( m_popupInterface
)
1629 m_popupInterface
->OnComboDoubleClick();
1634 if ( IsPopupShown() )
1636 // relay (some) mouse events to the popup
1637 if ( evtType
== wxEVT_MOUSEWHEEL
)
1638 m_popup
->AddPendingEvent(event
);
1644 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent
& event
)
1646 if ( IsPopupShown() )
1648 // pass it to the popped up control
1649 GetPopupControl()->GetControl()->AddPendingEvent(event
);
1653 if ( HandleAsNavigationKey(event
) )
1656 if ( IsKeyPopupToggle(event
) )
1662 int comboStyle
= GetWindowStyle();
1663 wxComboPopup
* popupInterface
= GetPopupControl();
1665 if ( !popupInterface
)
1671 int keycode
= event
.GetKeyCode();
1673 if ( (comboStyle
& wxCB_READONLY
) ||
1674 (keycode
!= WXK_RIGHT
&& keycode
!= WXK_LEFT
) )
1676 popupInterface
->OnComboKeyEvent(event
);
1683 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent
& event
)
1685 if ( event
.GetEventType() == wxEVT_SET_FOCUS
)
1687 wxWindow
* tc
= GetTextCtrl();
1688 if ( tc
&& tc
!= DoFindFocus() )
1690 m_resetFocus
= true;
1701 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent
& WXUNUSED(event
) )
1705 m_resetFocus
= false;
1706 wxWindow
* tc
= GetTextCtrl();
1712 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
1715 // indentation may also have changed
1716 if ( !(m_iFlags
& wxCC_IFLAG_INDENT_SET
) )
1717 m_absIndent
= GetNativeTextIndent();
1721 // ----------------------------------------------------------------------------
1723 // ----------------------------------------------------------------------------
1725 // Create popup window and the child control
1726 void wxComboCtrlBase::CreatePopup()
1728 wxComboPopup
* popupInterface
= m_popupInterface
;
1733 #ifdef wxComboPopupWindowBase2
1734 if ( m_iFlags
& wxCC_IFLAG_USE_ALT_POPUP
)
1737 m_winPopup
= new wxComboPopupWindowBase2( this, wxNO_BORDER
);
1739 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
, wxEmptyString
,
1740 wxPoint(-21,-21), wxSize(20, 20),
1743 m_popupWinType
= SECONDARY_POPUP_TYPE
;
1746 #endif // wxComboPopupWindowBase2
1748 m_winPopup
= new wxComboPopupWindow( this, wxNO_BORDER
);
1749 m_popupWinType
= PRIMARY_POPUP_TYPE
;
1751 m_popupWinEvtHandler
= new wxComboPopupWindowEvtHandler(this);
1752 m_winPopup
->PushEventHandler(m_popupWinEvtHandler
);
1755 popupInterface
->Create(m_winPopup
);
1756 m_popup
= popup
= popupInterface
->GetControl();
1758 m_popupExtraHandler
= new wxComboPopupExtraEventHandler(this);
1759 popup
->PushEventHandler( m_popupExtraHandler
);
1761 // This may be helpful on some platforms
1762 // (eg. it bypasses a wxGTK popupwindow bug where
1763 // window is not initially hidden when it should be)
1766 popupInterface
->m_iFlags
|= wxCP_IFLAG_CREATED
;
1769 // Destroy popup window and the child control
1770 void wxComboCtrlBase::DestroyPopup()
1775 m_popup
->RemoveEventHandler(m_popupExtraHandler
);
1777 delete m_popupExtraHandler
;
1779 delete m_popupInterface
;
1783 m_winPopup
->RemoveEventHandler(m_popupWinEvtHandler
);
1784 delete m_popupWinEvtHandler
;
1785 m_popupWinEvtHandler
= NULL
;
1786 m_winPopup
->Destroy();
1789 m_popupExtraHandler
= (wxEvtHandler
*) NULL
;
1790 m_popupInterface
= (wxComboPopup
*) NULL
;
1791 m_winPopup
= (wxWindow
*) NULL
;
1792 m_popup
= (wxWindow
*) NULL
;
1795 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup
* iface
)
1797 wxCHECK_RET( iface
, wxT("no popup interface set for wxComboCtrl") );
1801 iface
->InitBase(this);
1804 m_popupInterface
= iface
;
1806 if ( !iface
->LazyCreate() )
1812 m_popup
= (wxWindow
*) NULL
;
1815 // This must be done after creation
1816 if ( m_valueString
.length() )
1818 iface
->SetStringValue(m_valueString
);
1823 // Ensures there is atleast the default popup
1824 void wxComboCtrlBase::EnsurePopupControl()
1826 if ( !m_popupInterface
)
1827 SetPopupControl(NULL
);
1830 void wxComboCtrlBase::OnButtonClick()
1832 // Derived classes can override this method for totally custom
1837 void wxComboCtrlBase::ShowPopup()
1839 EnsurePopupControl();
1840 wxCHECK_RET( !IsPopupWindowState(Visible
), wxT("popup window already shown") );
1842 if ( IsPopupWindowState(Animating
) )
1847 // Space above and below
1853 wxSize ctrlSz
= GetSize();
1855 screenHeight
= wxSystemSettings::GetMetric( wxSYS_SCREEN_Y
);
1856 scrPos
= GetParent()->ClientToScreen(GetPosition());
1858 spaceAbove
= scrPos
.y
;
1859 spaceBelow
= screenHeight
- spaceAbove
- ctrlSz
.y
;
1861 maxHeightPopup
= spaceBelow
;
1862 if ( spaceAbove
> spaceBelow
)
1863 maxHeightPopup
= spaceAbove
;
1866 int widthPopup
= ctrlSz
.x
+ m_extLeft
+ m_extRight
;
1868 if ( widthPopup
< m_widthMinPopup
)
1869 widthPopup
= m_widthMinPopup
;
1871 wxWindow
* winPopup
= m_winPopup
;
1874 // Need to disable tab traversal of parent
1876 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1877 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1878 // that if transient popup is open, then tab traversal is to be ignored.
1879 // However, I think this code would still be needed for cases where
1880 // transient popup doesn't work yet (wxWinCE?).
1881 wxWindow
* parent
= GetParent();
1882 int parentFlags
= parent
->GetWindowStyle();
1883 if ( parentFlags
& wxTAB_TRAVERSAL
)
1885 parent
->SetWindowStyle( parentFlags
& ~(wxTAB_TRAVERSAL
) );
1886 m_iFlags
|= wxCC_IFLAG_PARENT_TAB_TRAVERSAL
;
1892 winPopup
= m_winPopup
;
1902 wxASSERT( !m_popup
|| m_popup
== popup
); // Consistency check.
1904 wxSize adjustedSize
= m_popupInterface
->GetAdjustedSize(widthPopup
,
1905 m_heightPopup
<=0?DEFAULT_POPUP_HEIGHT
:m_heightPopup
,
1908 popup
->SetSize(adjustedSize
);
1910 m_popupInterface
->OnPopup();
1913 // Reposition and resize popup window
1916 wxSize szp
= popup
->GetSize();
1919 int popupY
= scrPos
.y
+ ctrlSz
.y
;
1921 // Default anchor is wxLEFT
1922 int anchorSide
= m_anchorSide
;
1924 anchorSide
= wxLEFT
;
1926 int rightX
= scrPos
.x
+ ctrlSz
.x
+ m_extRight
- szp
.x
;
1927 int leftX
= scrPos
.x
- m_extLeft
;
1929 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
1932 int screenWidth
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
);
1934 // If there is not enough horizontal space, anchor on the other side.
1935 // If there is no space even then, place the popup at x 0.
1936 if ( anchorSide
== wxRIGHT
)
1940 if ( (leftX
+szp
.x
) < screenWidth
)
1941 anchorSide
= wxLEFT
;
1948 if ( (leftX
+szp
.x
) >= screenWidth
)
1951 anchorSide
= wxRIGHT
;
1957 // Select x coordinate according to the anchor side
1958 if ( anchorSide
== wxRIGHT
)
1960 else if ( anchorSide
== wxLEFT
)
1965 int showFlags
= CanDeferShow
;
1967 if ( spaceBelow
< szp
.y
)
1969 popupY
= scrPos
.y
- szp
.y
;
1970 showFlags
|= ShowAbove
;
1973 #if INSTALL_TOPLEV_HANDLER
1974 // Put top level window event handler into place
1975 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1977 if ( !m_toplevEvtHandler
)
1978 m_toplevEvtHandler
= new wxComboFrameEventHandler(this);
1980 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
1982 ((wxComboFrameEventHandler
*)m_toplevEvtHandler
)->OnPopup();
1983 toplev
->PushEventHandler( m_toplevEvtHandler
);
1987 // Set string selection (must be this way instead of SetStringSelection)
1990 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
1991 m_text
->SelectAll();
1993 m_popupInterface
->SetStringValue( m_text
->GetValue() );
1997 // This is neede since focus/selection indication may change when popup is shown
2001 // This must be after SetStringValue
2002 m_popupWinState
= Animating
;
2004 wxRect
popupWinRect( popupX
, popupY
, szp
.x
, szp
.y
);
2007 if ( (m_iFlags
& wxCC_IFLAG_DISABLE_POPUP_ANIM
) ||
2008 AnimateShow( popupWinRect
, showFlags
) )
2010 DoShowPopup( popupWinRect
, showFlags
);
2014 bool wxComboCtrlBase::AnimateShow( const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
) )
2019 void wxComboCtrlBase::DoShowPopup( const wxRect
& rect
, int WXUNUSED(flags
) )
2021 wxWindow
* winPopup
= m_winPopup
;
2023 if ( IsPopupWindowState(Animating
) )
2025 // Make sure the popup window is shown in the right position.
2026 // Should not matter even if animation already did this.
2028 // Some platforms (GTK) may like SetSize and Move to be separate
2029 // (though the bug was probably fixed).
2030 winPopup
->SetSize( rect
);
2032 #if USES_WXPOPUPTRANSIENTWINDOW
2033 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2034 ((wxPopupTransientWindow
*)winPopup
)->Popup(m_popup
);
2039 m_popupWinState
= Visible
;
2041 else if ( IsPopupWindowState(Hidden
) )
2043 // Animation was aborted
2045 wxASSERT( !winPopup
->IsShown() );
2047 m_popupWinState
= Hidden
;
2053 void wxComboCtrlBase::OnPopupDismiss()
2055 // Just in case, avoid double dismiss
2056 if ( IsPopupWindowState(Hidden
) )
2059 // This must be set before focus - otherwise there will be recursive
2060 // OnPopupDismisses.
2061 m_popupWinState
= Hidden
;
2064 m_winPopup
->Disable();
2066 // Inform popup control itself
2067 m_popupInterface
->OnDismiss();
2069 if ( m_popupExtraHandler
)
2070 ((wxComboPopupExtraEventHandler
*)m_popupExtraHandler
)->OnPopupDismiss();
2072 #if INSTALL_TOPLEV_HANDLER
2073 // Remove top level window event handler
2074 if ( m_toplevEvtHandler
)
2076 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2078 toplev
->RemoveEventHandler( m_toplevEvtHandler
);
2082 m_timeCanAcceptClick
= ::wxGetLocalTimeMillis();
2084 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2085 m_timeCanAcceptClick
+= 150;
2087 // If cursor not on dropdown button, then clear its state
2088 // (technically not required by all ports, but do it for all just in case)
2089 if ( !m_btnArea
.Contains(ScreenToClient(::wxGetMousePosition())) )
2092 // Return parent's tab traversal flag.
2093 // See ShowPopup for notes.
2094 if ( m_iFlags
& wxCC_IFLAG_PARENT_TAB_TRAVERSAL
)
2096 wxWindow
* parent
= GetParent();
2097 parent
->SetWindowStyle( parent
->GetWindowStyle() | wxTAB_TRAVERSAL
);
2098 m_iFlags
&= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL
);
2101 // refresh control (necessary even if m_text)
2107 void wxComboCtrlBase::HidePopup()
2109 // Should be able to call this without popup interface
2110 if ( IsPopupWindowState(Hidden
) )
2113 // transfer value and show it in textctrl, if any
2114 if ( !IsPopupWindowState(Animating
) )
2115 SetValue( m_popupInterface
->GetStringValue() );
2122 // ----------------------------------------------------------------------------
2123 // customization methods
2124 // ----------------------------------------------------------------------------
2126 void wxComboCtrlBase::SetButtonPosition( int width
, int height
,
2127 int side
, int spacingX
)
2132 m_btnSpacingX
= spacingX
;
2137 wxSize
wxComboCtrlBase::GetButtonSize()
2139 if ( m_btnSize
.x
> 0 )
2142 wxSize
retSize(m_btnWid
,m_btnHei
);
2144 // Need to call CalculateAreas now if button size is
2145 // is not explicitly specified.
2146 if ( retSize
.x
<= 0 || retSize
.y
<= 0)
2150 retSize
= m_btnSize
;
2156 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap
& bmpNormal
,
2158 const wxBitmap
& bmpPressed
,
2159 const wxBitmap
& bmpHover
,
2160 const wxBitmap
& bmpDisabled
)
2162 m_bmpNormal
= bmpNormal
;
2163 m_blankButtonBg
= blankButtonBg
;
2165 if ( bmpPressed
.Ok() )
2166 m_bmpPressed
= bmpPressed
;
2168 m_bmpPressed
= bmpNormal
;
2170 if ( bmpHover
.Ok() )
2171 m_bmpHover
= bmpHover
;
2173 m_bmpHover
= bmpNormal
;
2175 if ( bmpDisabled
.Ok() )
2176 m_bmpDisabled
= bmpDisabled
;
2178 m_bmpDisabled
= bmpNormal
;
2183 void wxComboCtrlBase::SetCustomPaintWidth( int width
)
2187 // move textctrl accordingly
2188 wxRect r
= m_text
->GetRect();
2189 int inc
= width
- m_widthCustomPaint
;
2192 m_text
->SetSize( r
);
2195 m_widthCustomPaint
= width
;
2200 void wxComboCtrlBase::SetTextIndent( int indent
)
2204 m_absIndent
= GetNativeTextIndent();
2205 m_iFlags
&= ~(wxCC_IFLAG_INDENT_SET
);
2209 m_absIndent
= indent
;
2210 m_iFlags
|= wxCC_IFLAG_INDENT_SET
;
2216 wxCoord
wxComboCtrlBase::GetNativeTextIndent() const
2218 return DEFAULT_TEXT_INDENT
;
2221 // ----------------------------------------------------------------------------
2222 // methods forwarded to wxTextCtrl
2223 // ----------------------------------------------------------------------------
2225 wxString
wxComboCtrlBase::GetValue() const
2228 return m_text
->GetValue();
2229 return m_valueString
;
2232 void wxComboCtrlBase::SetValueWithEvent(const wxString
& value
, bool withEvent
)
2239 m_text
->SetValue(value
);
2241 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2242 m_text
->SelectAll();
2245 // Since wxComboPopup may want to paint the combo as well, we need
2246 // to set the string value here (as well as sometimes in ShowPopup).
2247 if ( m_valueString
!= value
)
2249 m_valueString
= value
;
2251 EnsurePopupControl();
2253 if (m_popupInterface
)
2254 m_popupInterface
->SetStringValue(value
);
2260 void wxComboCtrlBase::SetValue(const wxString
& value
)
2262 SetValueWithEvent(value
, false);
2265 // In this SetValue variant wxComboPopup::SetStringValue is not called
2266 void wxComboCtrlBase::SetText(const wxString
& value
)
2268 // Unlike in SetValue(), this must be called here or
2269 // the behaviour will no be consistent in readonlys.
2270 EnsurePopupControl();
2272 m_valueString
= value
;
2277 m_text
->SetValue( value
);
2283 void wxComboCtrlBase::Copy()
2289 void wxComboCtrlBase::Cut()
2295 void wxComboCtrlBase::Paste()
2301 void wxComboCtrlBase::SetInsertionPoint(long pos
)
2304 m_text
->SetInsertionPoint(pos
);
2307 void wxComboCtrlBase::SetInsertionPointEnd()
2310 m_text
->SetInsertionPointEnd();
2313 long wxComboCtrlBase::GetInsertionPoint() const
2316 return m_text
->GetInsertionPoint();
2321 long wxComboCtrlBase::GetLastPosition() const
2324 return m_text
->GetLastPosition();
2329 void wxComboCtrlBase::Replace(long from
, long to
, const wxString
& value
)
2332 m_text
->Replace(from
, to
, value
);
2335 void wxComboCtrlBase::Remove(long from
, long to
)
2338 m_text
->Remove(from
, to
);
2341 void wxComboCtrlBase::SetSelection(long from
, long to
)
2344 m_text
->SetSelection(from
, to
);
2347 void wxComboCtrlBase::Undo()
2353 #endif // wxUSE_COMBOCTRL