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"
32 #include "wx/dcclient.h"
33 #include "wx/settings.h"
34 #include "wx/dialog.h"
38 #include "wx/tooltip.h"
45 // ----------------------------------------------------------------------------
47 #define DEFAULT_DROPBUTTON_WIDTH 19
49 #define BMP_BUTTON_MARGIN 4
51 #define DEFAULT_POPUP_HEIGHT 400
53 #define DEFAULT_TEXT_INDENT 3
55 #define COMBO_MARGIN 2 // spacing right of wxTextCtrl
58 #if defined(__WXMSW__)
60 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
61 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
62 // native controls work on it like normal.
63 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
64 #define TEXTCTRL_TEXT_CENTERED 0 // 1 if text in textctrl is vertically centered
65 #define FOCUS_RING 0 // No focus ring on wxMSW
67 //#undef wxUSE_POPUPWIN
68 //#define wxUSE_POPUPWIN 0
70 #elif defined(__WXGTK__)
72 // NB: It is not recommended to use wxDialog as popup on wxGTK, because of
73 // this bug: If wxDialog is hidden, its position becomes corrupt
74 // between hide and next show, but without internal coordinates being
75 // reflected (or something like that - atleast commenting out ->Hide()
76 // seemed to eliminate the position change).
78 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
79 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
80 // native controls work on it like normal.
81 #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
82 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
83 #define FOCUS_RING 0 // No focus ring on wxGTK
85 #elif defined(__WXMAC__)
87 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
88 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
89 // native controls work on it like normal.
90 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
91 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
92 #define FOCUS_RING 3 // Reserve room for the textctrl's focus ring to display
94 #undef DEFAULT_DROPBUTTON_WIDTH
95 #define DEFAULT_DROPBUTTON_WIDTH 22
97 #define COMBO_MARGIN FOCUS_RING
101 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
102 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
103 // native controls work on it like normal.
104 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
105 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
111 // Popupwin is really only supported on wxMSW (not WINCE) and wxGTK, regardless
112 // what the wxUSE_POPUPWIN says.
113 // FIXME: Why isn't wxUSE_POPUPWIN reliable any longer? (it was in wxW2.6.2)
114 #if (!defined(__WXMSW__) && !defined(__WXGTK__)) || defined(__WXWINCE__)
115 #undef wxUSE_POPUPWIN
116 #define wxUSE_POPUPWIN 0
121 #include "wx/popupwin.h"
123 #undef USE_TRANSIENT_POPUP
124 #define USE_TRANSIENT_POPUP 0
128 // Define different types of popup windows
132 POPUPWIN_WXPOPUPTRANSIENTWINDOW
= 1,
133 POPUPWIN_WXPOPUPWINDOW
= 2,
134 POPUPWIN_WXDIALOG
= 3
138 #if USE_TRANSIENT_POPUP
139 // wxPopupTransientWindow is implemented
141 #define wxComboPopupWindowBase wxPopupTransientWindow
142 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPTRANSIENTWINDOW
143 #define USES_WXPOPUPTRANSIENTWINDOW 1
145 #if TRANSIENT_POPUPWIN_IS_PERFECT
147 #elif POPUPWIN_IS_PERFECT
148 #define wxComboPopupWindowBase2 wxPopupWindow
149 #define SECONDARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
150 #define USES_WXPOPUPWINDOW 1
152 #define wxComboPopupWindowBase2 wxDialog
153 #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG
154 #define USES_WXDIALOG 1
158 // wxPopupWindow (but not wxPopupTransientWindow) is properly implemented
160 #define wxComboPopupWindowBase wxPopupWindow
161 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
162 #define USES_WXPOPUPWINDOW 1
164 #if !POPUPWIN_IS_PERFECT
165 #define wxComboPopupWindowBase2 wxDialog
166 #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG
167 #define USES_WXDIALOG 1
171 // wxPopupWindow is not implemented
173 #define wxComboPopupWindowBase wxDialog
174 #define PRIMARY_POPUP_TYPE POPUPWIN_WXDIALOG
175 #define USES_WXDIALOG 1
180 #ifndef USES_WXPOPUPTRANSIENTWINDOW
181 #define USES_WXPOPUPTRANSIENTWINDOW 0
184 #ifndef USES_WXPOPUPWINDOW
185 #define USES_WXPOPUPWINDOW 0
188 #ifndef USES_WXDIALOG
189 #define USES_WXDIALOG 0
193 #if USES_WXPOPUPWINDOW
194 #define INSTALL_TOPLEV_HANDLER 1
196 #define INSTALL_TOPLEV_HANDLER 0
202 // * wxComboPopupWindow for external use (ie. replace old wxUniv wxPopupComboWindow)
206 // ----------------------------------------------------------------------------
207 // wxComboFrameEventHandler takes care of hiding the popup when events happen
208 // in its top level parent.
209 // ----------------------------------------------------------------------------
211 #if INSTALL_TOPLEV_HANDLER
214 // This will no longer be necessary after wxTransientPopupWindow
215 // works well on all platforms.
218 class wxComboFrameEventHandler
: public wxEvtHandler
221 wxComboFrameEventHandler( wxComboCtrlBase
* pCb
);
222 virtual ~wxComboFrameEventHandler();
226 void OnIdle( wxIdleEvent
& event
);
227 void OnMouseEvent( wxMouseEvent
& event
);
228 void OnActivate( wxActivateEvent
& event
);
229 void OnResize( wxSizeEvent
& event
);
230 void OnMove( wxMoveEvent
& event
);
231 void OnMenuEvent( wxMenuEvent
& event
);
232 void OnClose( wxCloseEvent
& event
);
235 wxWindow
* m_focusStart
;
236 wxComboCtrlBase
* m_combo
;
239 DECLARE_EVENT_TABLE()
242 BEGIN_EVENT_TABLE(wxComboFrameEventHandler
, wxEvtHandler
)
243 EVT_IDLE(wxComboFrameEventHandler::OnIdle
)
244 EVT_LEFT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
245 EVT_RIGHT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
246 EVT_SIZE(wxComboFrameEventHandler::OnResize
)
247 EVT_MOVE(wxComboFrameEventHandler::OnMove
)
248 EVT_MENU_HIGHLIGHT(wxID_ANY
,wxComboFrameEventHandler::OnMenuEvent
)
249 EVT_MENU_OPEN(wxComboFrameEventHandler::OnMenuEvent
)
250 EVT_ACTIVATE(wxComboFrameEventHandler::OnActivate
)
251 EVT_CLOSE(wxComboFrameEventHandler::OnClose
)
254 wxComboFrameEventHandler::wxComboFrameEventHandler( wxComboCtrlBase
* combo
)
260 wxComboFrameEventHandler::~wxComboFrameEventHandler()
264 void wxComboFrameEventHandler::OnPopup()
266 m_focusStart
= ::wxWindow::FindFocus();
269 void wxComboFrameEventHandler::OnIdle( wxIdleEvent
& event
)
271 wxWindow
* winFocused
= ::wxWindow::FindFocus();
273 wxWindow
* popup
= m_combo
->GetPopupControl()->GetControl();
274 wxWindow
* winpopup
= m_combo
->GetPopupWindow();
277 winFocused
!= m_focusStart
&&
278 winFocused
!= popup
&&
279 winFocused
->GetParent() != popup
&&
280 winFocused
!= winpopup
&&
281 winFocused
->GetParent() != winpopup
&&
282 winFocused
!= m_combo
&&
283 winFocused
!= m_combo
->GetButton() // GTK (atleast) requires this
286 m_combo
->HidePopup();
292 void wxComboFrameEventHandler::OnMenuEvent( wxMenuEvent
& event
)
294 m_combo
->HidePopup();
298 void wxComboFrameEventHandler::OnMouseEvent( wxMouseEvent
& event
)
300 m_combo
->HidePopup();
304 void wxComboFrameEventHandler::OnClose( wxCloseEvent
& event
)
306 m_combo
->HidePopup();
310 void wxComboFrameEventHandler::OnActivate( wxActivateEvent
& event
)
312 m_combo
->HidePopup();
316 void wxComboFrameEventHandler::OnResize( wxSizeEvent
& event
)
318 m_combo
->HidePopup();
322 void wxComboFrameEventHandler::OnMove( wxMoveEvent
& event
)
324 m_combo
->HidePopup();
328 #endif // INSTALL_TOPLEV_HANDLER
330 // ----------------------------------------------------------------------------
331 // wxComboPopupWindow is, in essence, wxPopupWindow customized for
333 // ----------------------------------------------------------------------------
335 class wxComboPopupWindow
: public wxComboPopupWindowBase
339 wxComboPopupWindow( wxComboCtrlBase
*parent
,
341 #if USES_WXPOPUPWINDOW || USES_WXPOPUPTRANSIENTWINDOW
342 : wxComboPopupWindowBase(parent
,style
)
344 : wxComboPopupWindowBase(parent
,
355 #if USES_WXPOPUPTRANSIENTWINDOW
356 virtual bool Show( bool show
);
357 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
358 virtual void OnDismiss();
366 #if USES_WXPOPUPTRANSIENTWINDOW
367 bool wxComboPopupWindow::Show( bool show
)
369 // Guard against recursion
371 return wxComboPopupWindowBase::Show(show
);
375 wxASSERT( IsKindOf(CLASSINFO(wxPopupTransientWindow
)) );
377 wxPopupTransientWindow
* ptw
= (wxPopupTransientWindow
*) this;
378 wxComboCtrlBase
* combo
= (wxComboCtrlBase
*) GetParent();
380 if ( show
!= ptw
->IsShown() )
383 ptw
->Popup(combo
->GetPopupControl()->GetControl());
393 bool wxComboPopupWindow::ProcessLeftDown(wxMouseEvent
& event
)
395 return wxPopupTransientWindow::ProcessLeftDown(event
);
398 // First thing that happens when a transient popup closes is that this method gets called.
399 void wxComboPopupWindow::OnDismiss()
401 wxComboCtrlBase
* combo
= (wxComboCtrlBase
*) GetParent();
402 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxComboCtrlBase
)),
403 wxT("parent might not be wxComboCtrl, but check IMPLEMENT_DYNAMIC_CLASS(2) macro for correctness") );
405 combo
->OnPopupDismiss();
407 #endif // USES_WXPOPUPTRANSIENTWINDOW
410 // ----------------------------------------------------------------------------
411 // wxComboPopupWindowEvtHandler does bulk of the custom event handling
412 // of a popup window. It is separate so we can have different types
414 // ----------------------------------------------------------------------------
416 class wxComboPopupWindowEvtHandler
: public wxEvtHandler
420 wxComboPopupWindowEvtHandler( wxComboCtrlBase
*parent
)
425 void OnSizeEvent( wxSizeEvent
& event
);
426 void OnKeyEvent(wxKeyEvent
& event
);
428 void OnActivate( wxActivateEvent
& event
);
432 wxComboCtrlBase
* m_combo
;
434 DECLARE_EVENT_TABLE()
438 BEGIN_EVENT_TABLE(wxComboPopupWindowEvtHandler
, wxEvtHandler
)
439 EVT_KEY_DOWN(wxComboPopupWindowEvtHandler::OnKeyEvent
)
440 EVT_KEY_UP(wxComboPopupWindowEvtHandler::OnKeyEvent
)
442 EVT_ACTIVATE(wxComboPopupWindowEvtHandler::OnActivate
)
444 EVT_SIZE(wxComboPopupWindowEvtHandler::OnSizeEvent
)
448 void wxComboPopupWindowEvtHandler::OnSizeEvent( wxSizeEvent
& WXUNUSED(event
) )
450 // Block the event so that the popup control does not get auto-resized.
453 void wxComboPopupWindowEvtHandler::OnKeyEvent( wxKeyEvent
& event
)
455 // Relay keyboard event to the main child controls
456 wxWindowList children
= m_combo
->GetPopupWindow()->GetChildren();
457 wxWindowList::iterator node
= children
.begin();
458 wxWindow
* child
= (wxWindow
*)*node
;
459 child
->AddPendingEvent(event
);
463 void wxComboPopupWindowEvtHandler::OnActivate( wxActivateEvent
& event
)
465 if ( !event
.GetActive() )
467 // Tell combo control that we are dismissed.
468 m_combo
->HidePopup();
476 // ----------------------------------------------------------------------------
479 // ----------------------------------------------------------------------------
481 wxComboPopup::~wxComboPopup()
485 void wxComboPopup::OnPopup()
489 void wxComboPopup::OnDismiss()
493 wxSize
wxComboPopup::GetAdjustedSize( int minWidth
,
495 int WXUNUSED(maxHeight
) )
497 return wxSize(minWidth
,prefHeight
);
500 void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase
* combo
,
501 wxDC
& dc
, const wxRect
& rect
)
503 if ( combo
->GetWindowStyle() & wxCB_READONLY
) // ie. no textctrl
505 combo
->PrepareBackground(dc
,rect
,0);
507 dc
.DrawText( combo
->GetValue(),
508 rect
.x
+ combo
->GetTextIndent(),
509 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
513 void wxComboPopup::PaintComboControl( wxDC
& dc
, const wxRect
& rect
)
515 DefaultPaintComboControl(m_combo
,dc
,rect
);
518 void wxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
523 void wxComboPopup::OnComboDoubleClick()
527 void wxComboPopup::SetStringValue( const wxString
& WXUNUSED(value
) )
531 bool wxComboPopup::LazyCreate()
536 void wxComboPopup::Dismiss()
538 m_combo
->HidePopup();
541 // ----------------------------------------------------------------------------
543 // ----------------------------------------------------------------------------
546 // This is pushed to the event handler queue of the child textctrl.
548 class wxComboBoxExtraInputHandler
: public wxEvtHandler
552 wxComboBoxExtraInputHandler( wxComboCtrlBase
* combo
)
557 virtual ~wxComboBoxExtraInputHandler() { }
558 void OnKey(wxKeyEvent
& event
);
559 void OnFocus(wxFocusEvent
& event
);
562 wxComboCtrlBase
* m_combo
;
565 DECLARE_EVENT_TABLE()
569 BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler
, wxEvtHandler
)
570 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey
)
571 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
575 void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent
& event
)
577 // Let the wxComboCtrl event handler have a go first.
578 wxComboCtrlBase
* combo
= m_combo
;
579 wxObject
* prevObj
= event
.GetEventObject();
581 event
.SetId(combo
->GetId());
582 event
.SetEventObject(combo
);
583 combo
->GetEventHandler()->ProcessEvent(event
);
585 event
.SetId(((wxWindow
*)prevObj
)->GetId());
586 event
.SetEventObject(prevObj
);
589 void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent
& event
)
591 // FIXME: This code does run when control is clicked,
592 // yet on Windows it doesn't select all the text.
593 if ( !(m_combo
->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT
) )
595 if ( m_combo
->GetTextCtrl() )
596 m_combo
->GetTextCtrl()->SelectAll();
598 m_combo
->SetSelection(-1,-1);
601 // Send focus indication to parent.
602 // NB: This is needed for cases where the textctrl gets focus
603 // instead of its parent. While this may trigger multiple
604 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
605 // from combo's focus event handler), they should be quite
607 wxFocusEvent
evt2(wxEVT_SET_FOCUS
,m_combo
->GetId());
608 evt2
.SetEventObject(m_combo
);
609 m_combo
->GetEventHandler()->ProcessEvent(evt2
);
616 // This is pushed to the event handler queue of the control in popup.
619 class wxComboPopupExtraEventHandler
: public wxEvtHandler
623 wxComboPopupExtraEventHandler( wxComboCtrlBase
* combo
)
627 m_beenInside
= false;
629 virtual ~wxComboPopupExtraEventHandler() { }
631 void OnMouseEvent( wxMouseEvent
& event
);
633 // Called from wxComboCtrlBase::OnPopupDismiss
634 void OnPopupDismiss()
636 m_beenInside
= false;
640 wxComboCtrlBase
* m_combo
;
645 DECLARE_EVENT_TABLE()
649 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler
, wxEvtHandler
)
650 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent
)
654 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent
& event
)
656 wxPoint pt
= event
.GetPosition();
657 wxSize sz
= m_combo
->GetPopupControl()->GetControl()->GetClientSize();
658 int evtType
= event
.GetEventType();
659 bool isInside
= pt
.x
>= 0 && pt
.y
>= 0 && pt
.x
< sz
.x
&& pt
.y
< sz
.y
;
661 if ( evtType
== wxEVT_MOTION
||
662 evtType
== wxEVT_LEFT_DOWN
||
663 evtType
== wxEVT_RIGHT_DOWN
)
665 // Block motion and click events outside the popup
666 if ( !isInside
|| !m_combo
->IsPopupShown() )
672 else if ( evtType
== wxEVT_LEFT_UP
)
674 if ( !m_combo
->IsPopupShown() )
689 // Some mouse events to popup that happen outside it, before cursor
690 // has been inside the popu, need to be ignored by it but relayed to
693 wxWindow
* btn
= m_combo
->GetButton();
695 btn
->GetEventHandler()->AddPendingEvent(event
);
697 m_combo
->GetEventHandler()->AddPendingEvent(event
);
709 // ----------------------------------------------------------------------------
711 // ----------------------------------------------------------------------------
714 BEGIN_EVENT_TABLE(wxComboCtrlBase
, wxControl
)
715 EVT_TEXT(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
716 EVT_SIZE(wxComboCtrlBase::OnSizeEvent
)
717 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent
)
718 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent
)
719 EVT_IDLE(wxComboCtrlBase::OnIdleEvent
)
720 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
721 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent
)
722 EVT_TEXT_ENTER(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
723 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged
)
727 IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase
, wxControl
)
729 void wxComboCtrlBase::Init()
731 m_winPopup
= (wxWindow
*)NULL
;
732 m_popup
= (wxWindow
*)NULL
;
733 m_popupWinState
= Hidden
;
734 m_btn
= (wxWindow
*) NULL
;
735 m_text
= (wxTextCtrl
*) NULL
;
736 m_popupInterface
= (wxComboPopup
*) NULL
;
738 m_popupExtraHandler
= (wxEvtHandler
*) NULL
;
739 m_textEvtHandler
= (wxEvtHandler
*) NULL
;
741 #if INSTALL_TOPLEV_HANDLER
742 m_toplevEvtHandler
= (wxEvtHandler
*) NULL
;
745 m_mainCtrlWnd
= this;
748 m_widthMinPopup
= -1;
750 m_widthCustomPaint
= 0;
751 m_widthCustomBorder
= 0;
755 m_blankButtonBg
= false;
757 m_popupWinType
= POPUPWIN_NONE
;
758 m_btnWid
= m_btnHei
= -1;
766 m_timeCanAcceptClick
= 0;
768 m_resetFocus
= false;
771 bool wxComboCtrlBase::Create(wxWindow
*parent
,
773 const wxString
& value
,
777 const wxValidator
& validator
,
778 const wxString
& name
)
780 if ( !wxControl::Create(parent
,
784 style
| wxWANTS_CHARS
,
789 m_valueString
= value
;
793 m_absIndent
= GetNativeTextIndent();
795 m_iFlags
|= wxCC_IFLAG_CREATED
;
797 // If x and y indicate valid size, wxSizeEvent won't be
798 // emitted automatically, so we need to add artifical one.
799 if ( size
.x
> 0 && size
.y
> 0 )
801 wxSizeEvent
evt(size
,GetId());
802 GetEventHandler()->AddPendingEvent(evt
);
808 void wxComboCtrlBase::InstallInputHandlers()
812 m_textEvtHandler
= new wxComboBoxExtraInputHandler(this);
813 m_text
->PushEventHandler(m_textEvtHandler
);
818 wxComboCtrlBase::CreateTextCtrl(int style
, const wxValidator
& validator
)
820 if ( !(m_windowStyle
& wxCB_READONLY
) )
825 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
826 // not used by the wxPropertyGrid and therefore the tab is processed by
827 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
828 // navigation event is then sent to the wrong window.
829 style
|= wxTE_PROCESS_TAB
;
831 if ( HasFlag(wxTE_PROCESS_ENTER
) )
832 style
|= wxTE_PROCESS_ENTER
;
834 // Ignore EVT_TEXT generated by the constructor (but only
835 // if the event redirector already exists)
836 // NB: This must be " = 1" instead of "++";
837 if ( m_textEvtHandler
)
842 m_text
= new wxTextCtrl(this, wxID_ANY
, m_valueString
,
843 wxDefaultPosition
, wxSize(10,-1),
848 void wxComboCtrlBase::OnThemeChange()
850 // Leave the default bg on the Mac so the area used by the focus ring will
851 // be the correct colour and themed brush. Instead we'll use
852 // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed.
854 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
858 wxComboCtrlBase::~wxComboCtrlBase()
863 #if INSTALL_TOPLEV_HANDLER
864 delete ((wxComboFrameEventHandler
*)m_toplevEvtHandler
);
865 m_toplevEvtHandler
= (wxEvtHandler
*) NULL
;
871 m_text
->RemoveEventHandler(m_textEvtHandler
);
873 delete m_textEvtHandler
;
877 // ----------------------------------------------------------------------------
879 // ----------------------------------------------------------------------------
881 // Recalculates button and textctrl areas
882 void wxComboCtrlBase::CalculateAreas( int btnWidth
)
884 wxSize sz
= GetClientSize();
885 int customBorder
= m_widthCustomBorder
;
886 int btnBorder
; // border for button only
888 // check if button should really be outside the border: we'll do it it if
889 // its platform default or bitmap+pushbutton background is used, but not if
890 // there is vertical size adjustment or horizontal spacing.
891 if ( ( (m_iFlags
& wxCC_BUTTON_OUTSIDE_BORDER
) ||
892 (m_bmpNormal
.Ok() && m_blankButtonBg
) ) &&
893 m_btnSpacingX
== 0 &&
896 m_iFlags
|= wxCC_IFLAG_BUTTON_OUTSIDE
;
901 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
902 btnBorder
= customBorder
;
905 // Defaul indentation
906 if ( m_absIndent
< 0 )
907 m_absIndent
= GetNativeTextIndent();
909 int butWidth
= btnWidth
;
912 butWidth
= m_btnWidDefault
;
914 m_btnWidDefault
= butWidth
;
919 int butHeight
= sz
.y
- btnBorder
*2;
921 // Adjust button width
926 // Adjust button width to match aspect ratio
927 // (but only if control is smaller than best size).
928 int bestHeight
= GetBestSize().y
;
929 int height
= GetSize().y
;
931 if ( height
< bestHeight
)
933 // Make very small buttons square, as it makes
934 // them accommodate arrow image better and still
937 butWidth
= (height
*butWidth
)/bestHeight
;
939 butWidth
= butHeight
;
943 // Adjust button height
945 butHeight
= m_btnHei
;
947 // Use size of normal bitmap if...
950 // button width is set to default and blank button bg is not drawn
951 if ( m_bmpNormal
.Ok() )
953 int bmpReqWidth
= m_bmpNormal
.GetWidth();
954 int bmpReqHeight
= m_bmpNormal
.GetHeight();
956 // If drawing blank button background, we need to add some margin.
957 if ( m_blankButtonBg
)
959 bmpReqWidth
+= BMP_BUTTON_MARGIN
*2;
960 bmpReqHeight
+= BMP_BUTTON_MARGIN
*2;
963 if ( butWidth
< bmpReqWidth
|| ( m_btnWid
== 0 && !m_blankButtonBg
) )
964 butWidth
= bmpReqWidth
;
965 if ( butHeight
< bmpReqHeight
|| ( m_btnHei
== 0 && !m_blankButtonBg
) )
966 butHeight
= bmpReqHeight
;
968 // Need to fix height?
969 if ( (sz
.y
-(customBorder
*2)) < butHeight
&& btnWidth
== 0 )
971 int newY
= butHeight
+(customBorder
*2);
972 SetClientSize(wxDefaultCoord
,newY
);
977 int butAreaWid
= butWidth
+ (m_btnSpacingX
*2);
979 m_btnSize
.x
= butWidth
;
980 m_btnSize
.y
= butHeight
;
982 m_btnArea
.x
= ( m_btnSide
==wxRIGHT
? sz
.x
- butAreaWid
- btnBorder
: btnBorder
);
983 m_btnArea
.y
= btnBorder
+ FOCUS_RING
;
984 m_btnArea
.width
= butAreaWid
;
985 m_btnArea
.height
= sz
.y
- ((btnBorder
+FOCUS_RING
)*2);
987 m_tcArea
.x
= ( m_btnSide
==wxRIGHT
? 0 : butAreaWid
) + customBorder
+ FOCUS_RING
;
988 m_tcArea
.y
= customBorder
+ FOCUS_RING
;
989 m_tcArea
.width
= sz
.x
- butAreaWid
- (customBorder
*2) - (FOCUS_RING
*2);
990 m_tcArea
.height
= sz
.y
- ((customBorder
+FOCUS_RING
)*2);
995 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
996 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1001 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
)
1006 wxSize sz
= GetClientSize();
1007 int customBorder
= m_widthCustomBorder
;
1009 #if !TEXTCTRL_TEXT_CENTERED
1010 if ( (m_text
->GetWindowStyleFlag() & wxBORDER_MASK
) == wxNO_BORDER
)
1013 int tcSizeY
= m_text
->GetBestSize().y
;
1014 int diff
= sz
.y
- tcSizeY
;
1015 int y
= textCtrlYAdjust
+ (diff
/2);
1017 if ( y
< customBorder
)
1020 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
+ m_absIndent
+ textCtrlXAdjust
,
1022 m_tcArea
.width
- COMBO_MARGIN
-
1023 (textCtrlXAdjust
+ m_widthCustomPaint
+ m_absIndent
),
1026 // Make sure textctrl doesn't exceed the bottom custom border
1027 wxSize tsz
= m_text
->GetSize();
1028 diff
= (y
+ tsz
.y
) - (sz
.y
- customBorder
);
1031 tsz
.y
= tsz
.y
- diff
- 1;
1032 m_text
->SetSize(tsz
);
1037 wxUnusedVar(textCtrlXAdjust
);
1038 wxUnusedVar(textCtrlYAdjust
);
1041 // If it has border, have textctrl will the entire text field.
1042 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
,
1044 m_tcArea
.width
- m_widthCustomPaint
,
1049 wxSize
wxComboCtrlBase::DoGetBestSize() const
1051 wxSize
sizeText(150,0);
1054 sizeText
= m_text
->GetBestSize();
1056 // TODO: Better method to calculate close-to-native control height.
1060 fhei
= (m_font
.GetPointSize()*2) + 5;
1061 else if ( wxNORMAL_FONT
->Ok() )
1062 fhei
= (wxNORMAL_FONT
->GetPointSize()*2) + 5;
1064 fhei
= sizeText
.y
+ 4;
1066 // Need to force height to accomodate bitmap?
1067 int btnSizeY
= m_btnSize
.y
;
1068 if ( m_bmpNormal
.Ok() && fhei
< btnSizeY
)
1071 // Control height doesn't depend on border
1074 int border = m_windowStyle & wxBORDER_MASK;
1075 if ( border == wxSIMPLE_BORDER )
1077 else if ( border == wxNO_BORDER )
1078 fhei += (m_widthCustomBorder*2);
1084 // Final adjustments
1090 // these are the numbers from the HIG:
1091 switch ( m_windowVariant
)
1093 case wxWINDOW_VARIANT_NORMAL
:
1097 case wxWINDOW_VARIANT_SMALL
:
1100 case wxWINDOW_VARIANT_MINI
:
1106 fhei
+= 2 * FOCUS_RING
;
1107 int width
= sizeText
.x
+ FOCUS_RING
+ COMBO_MARGIN
+ DEFAULT_DROPBUTTON_WIDTH
;
1109 wxSize
ret(width
, fhei
);
1114 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent
& event
)
1119 // defined by actual wxComboCtrls
1125 // ----------------------------------------------------------------------------
1126 // standard operations
1127 // ----------------------------------------------------------------------------
1129 bool wxComboCtrlBase::Enable(bool enable
)
1131 if ( !wxControl::Enable(enable
) )
1135 m_btn
->Enable(enable
);
1137 m_text
->Enable(enable
);
1142 bool wxComboCtrlBase::Show(bool show
)
1144 if ( !wxControl::Show(show
) )
1156 bool wxComboCtrlBase::SetFont ( const wxFont
& font
)
1158 if ( !wxControl::SetFont(font
) )
1162 m_text
->SetFont(font
);
1168 void wxComboCtrlBase::DoSetToolTip(wxToolTip
*tooltip
)
1170 wxControl::DoSetToolTip(tooltip
);
1172 // Set tool tip for button and text box
1175 const wxString
&tip
= tooltip
->GetTip();
1176 if ( m_text
) m_text
->SetToolTip(tip
);
1177 if ( m_btn
) m_btn
->SetToolTip(tip
);
1181 if ( m_text
) m_text
->SetToolTip( (wxToolTip
*) NULL
);
1182 if ( m_btn
) m_btn
->SetToolTip( (wxToolTip
*) NULL
);
1185 #endif // wxUSE_TOOLTIPS
1187 #if wxUSE_VALIDATORS
1188 void wxComboCtrlBase::SetValidator(const wxValidator
& validator
)
1190 wxTextCtrl
* textCtrl
= GetTextCtrl();
1193 textCtrl
->SetValidator( validator
);
1196 wxValidator
* wxComboCtrlBase::GetValidator()
1198 wxTextCtrl
* textCtrl
= GetTextCtrl();
1201 return textCtrl
->GetValidator();
1203 return wxControl::GetValidator();
1205 #endif // wxUSE_VALIDATORS
1207 // ----------------------------------------------------------------------------
1209 // ----------------------------------------------------------------------------
1211 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1212 // prepare combo box background on area in a way typical on platform
1213 void wxComboCtrlBase::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
1215 wxSize sz
= GetClientSize();
1217 bool isFocused
; // also selected
1219 // For smaller size control (and for disabled background) use less spacing
1223 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1226 isEnabled
= IsEnabled();
1227 isFocused
= ShouldDrawFocus();
1229 // Windows-style: for smaller size control (and for disabled background) use less spacing
1230 focusSpacingX
= isEnabled
? 2 : 1;
1231 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
1235 // Drawing a list item
1236 isEnabled
= true; // they are never disabled
1237 isFocused
= flags
& wxCONTROL_SELECTED
? true : false;
1243 // Set the background sub-rectangle for selection, disabled etc
1244 wxRect
selRect(rect
);
1245 selRect
.y
+= focusSpacingY
;
1246 selRect
.height
-= (focusSpacingY
*2);
1250 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1251 wcp
+= m_widthCustomPaint
;
1253 selRect
.x
+= wcp
+ focusSpacingX
;
1254 selRect
.width
-= wcp
+ (focusSpacingX
*2);
1260 // If popup is hidden and this control is focused,
1261 // then draw the focus-indicator (selbgcolor background etc.).
1264 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1265 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1269 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
1270 #ifndef __WXMAC__ // see note in OnThemeChange
1271 bgCol
= GetBackgroundColour();
1273 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1279 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
1280 #ifndef __WXMAC__ // see note in OnThemeChange
1281 bgCol
= GetBackgroundColour();
1283 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1287 dc
.SetBrush( bgCol
);
1289 dc
.DrawRectangle( selRect
);
1291 // Don't clip exactly to the selection rectangle so we can draw
1292 // to the non-selected area in front of it.
1293 wxRect
clipRect(rect
.x
,rect
.y
,
1294 (selRect
.x
+selRect
.width
)-rect
.x
,rect
.height
);
1295 dc
.SetClippingRegion(clipRect
);
1298 // Save the library size a bit for platforms that re-implement this.
1299 void wxComboCtrlBase::PrepareBackground( wxDC
&, const wxRect
&, int ) const
1304 void wxComboCtrlBase::DrawButton( wxDC
& dc
, const wxRect
& rect
, int paintBg
)
1306 int drawState
= m_btnState
;
1309 if ( GetPopupWindowState() >= Animating
)
1310 drawState
|= wxCONTROL_PRESSED
;
1313 wxRect
drawRect(rect
.x
+m_btnSpacingX
,
1314 rect
.y
+((rect
.height
-m_btnSize
.y
)/2),
1318 // Make sure area is not larger than the control
1319 if ( drawRect
.y
< rect
.y
)
1320 drawRect
.y
= rect
.y
;
1321 if ( drawRect
.height
> rect
.height
)
1322 drawRect
.height
= rect
.height
;
1324 bool enabled
= IsEnabled();
1327 drawState
|= wxCONTROL_DISABLED
;
1329 if ( !m_bmpNormal
.Ok() )
1331 // Need to clear button background even if m_btn is present
1336 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
1337 bgCol
= GetParent()->GetBackgroundColour();
1339 bgCol
= GetBackgroundColour();
1343 dc
.DrawRectangle(rect
);
1346 // Draw standard button
1347 wxRendererNative::Get().DrawComboBoxDropButton(this,
1359 pBmp
= &m_bmpDisabled
;
1360 else if ( m_btnState
& wxCONTROL_PRESSED
)
1361 pBmp
= &m_bmpPressed
;
1362 else if ( m_btnState
& wxCONTROL_CURRENT
)
1365 pBmp
= &m_bmpNormal
;
1367 if ( m_blankButtonBg
)
1369 // If using blank button background, we need to clear its background
1370 // with button face colour instead of colour for rest of the control.
1373 wxColour bgCol
= GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1374 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1377 dc
.DrawRectangle(rect
);
1380 wxRendererNative::Get().DrawPushButton(this,
1389 // Need to clear button background even if m_btn is present
1390 // (assume non-button background was cleared just before this call so brushes are good)
1392 dc
.DrawRectangle(rect
);
1395 // Draw bitmap centered in drawRect
1396 dc
.DrawBitmap(*pBmp
,
1397 drawRect
.x
+ (drawRect
.width
-pBmp
->GetWidth())/2,
1398 drawRect
.y
+ (drawRect
.height
-pBmp
->GetHeight())/2,
1403 void wxComboCtrlBase::RecalcAndRefresh()
1407 wxSizeEvent
evt(GetSize(),GetId());
1408 GetEventHandler()->ProcessEvent(evt
);
1413 // ----------------------------------------------------------------------------
1414 // miscellaneous event handlers
1415 // ----------------------------------------------------------------------------
1417 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent
& event
)
1419 if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1421 if ( m_ignoreEvtText
> 0 )
1428 // Change event id, object and string before relaying it forward
1429 event
.SetId(GetId());
1430 wxString s
= event
.GetString();
1431 event
.SetEventObject(this);
1436 // call if cursor is on button area or mouse is captured for the button
1437 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent
& event
,
1440 int type
= event
.GetEventType();
1442 if ( type
== wxEVT_MOTION
)
1444 if ( flags
& wxCC_MF_ON_BUTTON
)
1446 if ( !(m_btnState
& wxCONTROL_CURRENT
) )
1448 // Mouse hover begins
1449 m_btnState
|= wxCONTROL_CURRENT
;
1450 if ( HasCapture() ) // Retain pressed state.
1451 m_btnState
|= wxCONTROL_PRESSED
;
1455 else if ( (m_btnState
& wxCONTROL_CURRENT
) )
1458 m_btnState
&= ~(wxCONTROL_CURRENT
|wxCONTROL_PRESSED
);
1462 else if ( type
== wxEVT_LEFT_DOWN
|| type
== wxEVT_LEFT_DCLICK
)
1464 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1466 m_btnState
|= wxCONTROL_PRESSED
;
1469 if ( !(m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
) )
1472 // If showing popup now, do not capture mouse or there will be interference
1476 else if ( type
== wxEVT_LEFT_UP
)
1479 // Only accept event if mouse was left-press was previously accepted
1483 if ( m_btnState
& wxCONTROL_PRESSED
)
1485 // If mouse was inside, fire the click event.
1486 if ( m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
)
1488 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1492 m_btnState
&= ~(wxCONTROL_PRESSED
);
1496 else if ( type
== wxEVT_LEAVE_WINDOW
)
1498 if ( m_btnState
& (wxCONTROL_CURRENT
|wxCONTROL_PRESSED
) )
1500 m_btnState
&= ~(wxCONTROL_CURRENT
);
1503 if ( IsPopupWindowState(Hidden
) )
1505 m_btnState
&= ~(wxCONTROL_PRESSED
);
1516 // returns true if event was consumed or filtered
1517 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent
& event
,
1518 int WXUNUSED(flags
) )
1520 wxLongLong t
= ::wxGetLocalTimeMillis();
1521 int evtType
= event
.GetEventType();
1523 #if USES_WXPOPUPWINDOW || USES_WXDIALOG
1524 if ( m_popupWinType
!= POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1526 if ( IsPopupWindowState(Visible
) &&
1527 ( evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_RIGHT_DOWN
) )
1535 // Filter out clicks on button immediately after popup dismiss (Windows like behaviour)
1536 if ( evtType
== wxEVT_LEFT_DOWN
&& t
< m_timeCanAcceptClick
)
1538 event
.SetEventType(0);
1545 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent
& event
)
1547 int evtType
= event
.GetEventType();
1549 if ( (evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_LEFT_DCLICK
) &&
1550 (m_windowStyle
& wxCB_READONLY
) )
1552 if ( GetPopupWindowState() >= Animating
)
1554 #if USES_WXPOPUPWINDOW
1555 // Click here always hides the popup.
1556 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1562 if ( !(m_windowStyle
& wxCC_SPECIAL_DCLICK
) )
1564 // In read-only mode, clicking the text is the
1565 // same as clicking the button.
1568 else if ( /*evtType == wxEVT_LEFT_UP || */evtType
== wxEVT_LEFT_DCLICK
)
1570 //if ( m_popupInterface->CycleValue() )
1572 if ( m_popupInterface
)
1573 m_popupInterface
->OnComboDoubleClick();
1578 if ( IsPopupShown() )
1580 // relay (some) mouse events to the popup
1581 if ( evtType
== wxEVT_MOUSEWHEEL
)
1582 m_popup
->AddPendingEvent(event
);
1588 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent
& event
)
1590 if ( IsPopupShown() )
1592 // pass it to the popped up control
1593 GetPopupControl()->GetControl()->AddPendingEvent(event
);
1597 int keycode
= event
.GetKeyCode();
1599 if ( keycode
== WXK_TAB
)
1601 wxNavigationKeyEvent evt
;
1603 wxWindow
* mainCtrl
= GetMainWindowOfCompositeControl();
1605 evt
.SetFlags(wxNavigationKeyEvent::FromTab
|
1606 (!event
.ShiftDown() ? wxNavigationKeyEvent::IsForward
1607 : wxNavigationKeyEvent::IsBackward
));
1608 evt
.SetEventObject(mainCtrl
);
1609 evt
.SetCurrentFocus(mainCtrl
);
1610 mainCtrl
->GetParent()->GetEventHandler()->AddPendingEvent(evt
);
1614 if ( IsKeyPopupToggle(event
) )
1620 int comboStyle
= GetWindowStyle();
1621 wxComboPopup
* popupInterface
= GetPopupControl();
1623 if ( !popupInterface
)
1629 if ( (comboStyle
& wxCB_READONLY
) ||
1630 (keycode
!= WXK_RIGHT
&& keycode
!= WXK_LEFT
) )
1632 popupInterface
->OnComboKeyEvent(event
);
1639 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent
& event
)
1641 if ( event
.GetEventType() == wxEVT_SET_FOCUS
)
1643 wxWindow
* tc
= GetTextCtrl();
1644 if ( tc
&& tc
!= DoFindFocus() )
1646 m_resetFocus
= true;
1655 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent
& WXUNUSED(event
) )
1659 m_resetFocus
= false;
1660 wxWindow
* tc
= GetTextCtrl();
1666 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
1669 // indentation may also have changed
1670 if ( !(m_iFlags
& wxCC_IFLAG_INDENT_SET
) )
1671 m_absIndent
= GetNativeTextIndent();
1675 // ----------------------------------------------------------------------------
1677 // ----------------------------------------------------------------------------
1679 // Create popup window and the child control
1680 void wxComboCtrlBase::CreatePopup()
1682 wxComboPopup
* popupInterface
= m_popupInterface
;
1687 #ifdef wxComboPopupWindowBase2
1688 if ( m_iFlags
& wxCC_IFLAG_USE_ALT_POPUP
)
1691 m_winPopup
= new wxComboPopupWindowBase2( this, wxNO_BORDER
);
1693 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
, wxEmptyString
,
1694 wxPoint(-21,-21), wxSize(20, 20),
1697 m_popupWinType
= SECONDARY_POPUP_TYPE
;
1702 m_winPopup
= new wxComboPopupWindow( this, wxNO_BORDER
);
1703 m_popupWinType
= PRIMARY_POPUP_TYPE
;
1705 m_popupWinEvtHandler
= new wxComboPopupWindowEvtHandler(this);
1706 m_winPopup
->PushEventHandler(m_popupWinEvtHandler
);
1709 popupInterface
->Create(m_winPopup
);
1710 m_popup
= popup
= popupInterface
->GetControl();
1712 m_popupExtraHandler
= new wxComboPopupExtraEventHandler(this);
1713 popup
->PushEventHandler( m_popupExtraHandler
);
1715 // This may be helpful on some platforms
1716 // (eg. it bypasses a wxGTK popupwindow bug where
1717 // window is not initially hidden when it should be)
1720 popupInterface
->m_iFlags
|= wxCP_IFLAG_CREATED
;
1723 // Destroy popup window and the child control
1724 void wxComboCtrlBase::DestroyPopup()
1729 m_popup
->RemoveEventHandler(m_popupExtraHandler
);
1731 delete m_popupExtraHandler
;
1733 delete m_popupInterface
;
1737 m_winPopup
->RemoveEventHandler(m_popupWinEvtHandler
);
1738 delete m_popupWinEvtHandler
;
1739 m_popupWinEvtHandler
= NULL
;
1740 m_winPopup
->Destroy();
1743 m_popupExtraHandler
= (wxEvtHandler
*) NULL
;
1744 m_popupInterface
= (wxComboPopup
*) NULL
;
1745 m_winPopup
= (wxWindow
*) NULL
;
1746 m_popup
= (wxWindow
*) NULL
;
1749 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup
* iface
)
1751 wxCHECK_RET( iface
, wxT("no popup interface set for wxComboCtrl") );
1755 iface
->InitBase(this);
1758 m_popupInterface
= iface
;
1760 if ( !iface
->LazyCreate() )
1766 m_popup
= (wxWindow
*) NULL
;
1769 // This must be done after creation
1770 if ( m_valueString
.length() )
1772 iface
->SetStringValue(m_valueString
);
1777 // Ensures there is atleast the default popup
1778 void wxComboCtrlBase::EnsurePopupControl()
1780 if ( !m_popupInterface
)
1781 SetPopupControl(NULL
);
1784 void wxComboCtrlBase::OnButtonClick()
1786 // Derived classes can override this method for totally custom
1791 void wxComboCtrlBase::ShowPopup()
1793 EnsurePopupControl();
1794 wxCHECK_RET( !IsPopupWindowState(Visible
), wxT("popup window already shown") );
1796 if ( IsPopupWindowState(Animating
) )
1801 // Space above and below
1807 wxSize ctrlSz
= GetSize();
1809 screenHeight
= wxSystemSettings::GetMetric( wxSYS_SCREEN_Y
);
1810 scrPos
= GetParent()->ClientToScreen(GetPosition());
1812 spaceAbove
= scrPos
.y
;
1813 spaceBelow
= screenHeight
- spaceAbove
- ctrlSz
.y
;
1815 maxHeightPopup
= spaceBelow
;
1816 if ( spaceAbove
> spaceBelow
)
1817 maxHeightPopup
= spaceAbove
;
1820 int widthPopup
= ctrlSz
.x
+ m_extLeft
+ m_extRight
;
1822 if ( widthPopup
< m_widthMinPopup
)
1823 widthPopup
= m_widthMinPopup
;
1825 wxWindow
* winPopup
= m_winPopup
;
1828 // Need to disable tab traversal of parent
1830 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1831 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1832 // that if transient popup is open, then tab traversal is to be ignored.
1833 // However, I think this code would still be needed for cases where
1834 // transient popup doesn't work yet (wxWinCE?).
1835 wxWindow
* parent
= GetParent();
1836 int parentFlags
= parent
->GetWindowStyle();
1837 if ( parentFlags
& wxTAB_TRAVERSAL
)
1839 parent
->SetWindowStyle( parentFlags
& ~(wxTAB_TRAVERSAL
) );
1840 m_iFlags
|= wxCC_IFLAG_PARENT_TAB_TRAVERSAL
;
1846 winPopup
= m_winPopup
;
1856 wxASSERT( !m_popup
|| m_popup
== popup
); // Consistency check.
1858 wxSize adjustedSize
= m_popupInterface
->GetAdjustedSize(widthPopup
,
1859 m_heightPopup
<=0?DEFAULT_POPUP_HEIGHT
:m_heightPopup
,
1862 popup
->SetSize(adjustedSize
);
1864 m_popupInterface
->OnPopup();
1867 // Reposition and resize popup window
1870 wxSize szp
= popup
->GetSize();
1873 int popupY
= scrPos
.y
+ ctrlSz
.y
;
1875 // Default anchor is wxLEFT
1876 int anchorSide
= m_anchorSide
;
1878 anchorSide
= wxLEFT
;
1880 int rightX
= scrPos
.x
+ ctrlSz
.x
+ m_extRight
- szp
.x
;
1881 int leftX
= scrPos
.x
- m_extLeft
;
1882 int screenWidth
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
);
1884 // If there is not enough horizontal space, anchor on the other side.
1885 // If there is no space even then, place the popup at x 0.
1886 if ( anchorSide
== wxRIGHT
)
1890 if ( (leftX
+szp
.x
) < screenWidth
)
1891 anchorSide
= wxLEFT
;
1898 if ( (leftX
+szp
.x
) >= screenWidth
)
1901 anchorSide
= wxRIGHT
;
1907 // Select x coordinate according to the anchor side
1908 if ( anchorSide
== wxRIGHT
)
1910 else if ( anchorSide
== wxLEFT
)
1915 int showFlags
= CanDeferShow
;
1917 if ( spaceBelow
< szp
.y
)
1919 popupY
= scrPos
.y
- szp
.y
;
1920 showFlags
|= ShowAbove
;
1923 #if INSTALL_TOPLEV_HANDLER
1924 // Put top level window event handler into place
1925 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1927 if ( !m_toplevEvtHandler
)
1928 m_toplevEvtHandler
= new wxComboFrameEventHandler(this);
1930 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
1932 ((wxComboFrameEventHandler
*)m_toplevEvtHandler
)->OnPopup();
1933 toplev
->PushEventHandler( m_toplevEvtHandler
);
1937 // Set string selection (must be this way instead of SetStringSelection)
1940 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
1941 m_text
->SelectAll();
1943 m_popupInterface
->SetStringValue( m_text
->GetValue() );
1947 // This is neede since focus/selection indication may change when popup is shown
1951 // This must be after SetStringValue
1952 m_popupWinState
= Animating
;
1954 wxRect
popupWinRect( popupX
, popupY
, szp
.x
, szp
.y
);
1957 if ( (m_iFlags
& wxCC_IFLAG_DISABLE_POPUP_ANIM
) ||
1958 AnimateShow( popupWinRect
, showFlags
) )
1960 DoShowPopup( popupWinRect
, showFlags
);
1964 bool wxComboCtrlBase::AnimateShow( const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
) )
1969 void wxComboCtrlBase::DoShowPopup( const wxRect
& rect
, int WXUNUSED(flags
) )
1971 wxWindow
* winPopup
= m_winPopup
;
1973 if ( IsPopupWindowState(Animating
) )
1975 // Make sure the popup window is shown in the right position.
1976 // Should not matter even if animation already did this.
1978 // Some platforms (GTK) may like SetSize and Move to be separate
1979 // (though the bug was probably fixed).
1980 winPopup
->SetSize( rect
);
1984 m_popupWinState
= Visible
;
1986 else if ( IsPopupWindowState(Hidden
) )
1988 // Animation was aborted
1990 wxASSERT( !winPopup
->IsShown() );
1992 m_popupWinState
= Hidden
;
1996 void wxComboCtrlBase::OnPopupDismiss()
1998 // Just in case, avoid double dismiss
1999 if ( IsPopupWindowState(Hidden
) )
2002 // This must be set before focus - otherwise there will be recursive
2003 // OnPopupDismisses.
2004 m_popupWinState
= Hidden
;
2007 m_winPopup
->Disable();
2009 // Inform popup control itself
2010 m_popupInterface
->OnDismiss();
2012 if ( m_popupExtraHandler
)
2013 ((wxComboPopupExtraEventHandler
*)m_popupExtraHandler
)->OnPopupDismiss();
2015 #if INSTALL_TOPLEV_HANDLER
2016 // Remove top level window event handler
2017 if ( m_toplevEvtHandler
)
2019 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2021 toplev
->RemoveEventHandler( m_toplevEvtHandler
);
2025 m_timeCanAcceptClick
= ::wxGetLocalTimeMillis();
2027 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2028 m_timeCanAcceptClick
+= 150;
2030 // If cursor not on dropdown button, then clear its state
2031 // (technically not required by all ports, but do it for all just in case)
2032 if ( !m_btnArea
.Contains(ScreenToClient(::wxGetMousePosition())) )
2035 // Return parent's tab traversal flag.
2036 // See ShowPopup for notes.
2037 if ( m_iFlags
& wxCC_IFLAG_PARENT_TAB_TRAVERSAL
)
2039 wxWindow
* parent
= GetParent();
2040 parent
->SetWindowStyle( parent
->GetWindowStyle() | wxTAB_TRAVERSAL
);
2041 m_iFlags
&= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL
);
2044 // refresh control (necessary even if m_text)
2050 void wxComboCtrlBase::HidePopup()
2052 // Should be able to call this without popup interface
2053 if ( IsPopupWindowState(Hidden
) )
2056 // transfer value and show it in textctrl, if any
2057 if ( !IsPopupWindowState(Animating
) )
2058 SetValue( m_popupInterface
->GetStringValue() );
2065 // ----------------------------------------------------------------------------
2066 // customization methods
2067 // ----------------------------------------------------------------------------
2069 void wxComboCtrlBase::SetButtonPosition( int width
, int height
,
2070 int side
, int spacingX
)
2075 m_btnSpacingX
= spacingX
;
2080 wxSize
wxComboCtrlBase::GetButtonSize()
2082 if ( m_btnSize
.x
> 0 )
2085 wxSize
retSize(m_btnWid
,m_btnHei
);
2087 // Need to call CalculateAreas now if button size is
2088 // is not explicitly specified.
2089 if ( retSize
.x
<= 0 || retSize
.y
<= 0)
2093 retSize
= m_btnSize
;
2099 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap
& bmpNormal
,
2101 const wxBitmap
& bmpPressed
,
2102 const wxBitmap
& bmpHover
,
2103 const wxBitmap
& bmpDisabled
)
2105 m_bmpNormal
= bmpNormal
;
2106 m_blankButtonBg
= blankButtonBg
;
2108 if ( bmpPressed
.Ok() )
2109 m_bmpPressed
= bmpPressed
;
2111 m_bmpPressed
= bmpNormal
;
2113 if ( bmpHover
.Ok() )
2114 m_bmpHover
= bmpHover
;
2116 m_bmpHover
= bmpNormal
;
2118 if ( bmpDisabled
.Ok() )
2119 m_bmpDisabled
= bmpDisabled
;
2121 m_bmpDisabled
= bmpNormal
;
2126 void wxComboCtrlBase::SetCustomPaintWidth( int width
)
2130 // move textctrl accordingly
2131 wxRect r
= m_text
->GetRect();
2132 int inc
= width
- m_widthCustomPaint
;
2135 m_text
->SetSize( r
);
2138 m_widthCustomPaint
= width
;
2143 void wxComboCtrlBase::SetTextIndent( int indent
)
2147 m_absIndent
= GetNativeTextIndent();
2148 m_iFlags
&= ~(wxCC_IFLAG_INDENT_SET
);
2152 m_absIndent
= indent
;
2153 m_iFlags
|= wxCC_IFLAG_INDENT_SET
;
2159 wxCoord
wxComboCtrlBase::GetNativeTextIndent() const
2161 return DEFAULT_TEXT_INDENT
;
2164 // ----------------------------------------------------------------------------
2165 // methods forwarded to wxTextCtrl
2166 // ----------------------------------------------------------------------------
2168 wxString
wxComboCtrlBase::GetValue() const
2171 return m_text
->GetValue();
2172 return m_valueString
;
2175 void wxComboCtrlBase::SetValueWithEvent(const wxString
& value
, bool withEvent
)
2182 m_text
->SetValue(value
);
2183 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2184 m_text
->SelectAll();
2187 m_valueString
= value
;
2191 // Since wxComboPopup may want to paint the combo as well, we need
2192 // to set the string value here (as well as sometimes in ShowPopup).
2193 if ( m_valueString
!= value
&& m_popupInterface
)
2195 m_popupInterface
->SetStringValue(value
);
2199 void wxComboCtrlBase::SetValue(const wxString
& value
)
2201 SetValueWithEvent(value
, false);
2204 // In this SetValue variant wxComboPopup::SetStringValue is not called
2205 void wxComboCtrlBase::SetText(const wxString
& value
)
2207 // Unlike in SetValue(), this must be called here or
2208 // the behaviour will no be consistent in readonlys.
2209 EnsurePopupControl();
2211 m_valueString
= value
;
2216 m_text
->SetValue( value
);
2222 void wxComboCtrlBase::Copy()
2228 void wxComboCtrlBase::Cut()
2234 void wxComboCtrlBase::Paste()
2240 void wxComboCtrlBase::SetInsertionPoint(long pos
)
2243 m_text
->SetInsertionPoint(pos
);
2246 void wxComboCtrlBase::SetInsertionPointEnd()
2249 m_text
->SetInsertionPointEnd();
2252 long wxComboCtrlBase::GetInsertionPoint() const
2255 return m_text
->GetInsertionPoint();
2260 long wxComboCtrlBase::GetLastPosition() const
2263 return m_text
->GetLastPosition();
2268 void wxComboCtrlBase::Replace(long from
, long to
, const wxString
& value
)
2271 m_text
->Replace(from
, to
, value
);
2274 void wxComboCtrlBase::Remove(long from
, long to
)
2277 m_text
->Remove(from
, to
);
2280 void wxComboCtrlBase::SetSelection(long from
, long to
)
2283 m_text
->SetSelection(from
, to
);
2286 void wxComboCtrlBase::Undo()
2292 #endif // wxUSE_COMBOCTRL