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"
39 #include "wx/tooltip.h"
46 // ----------------------------------------------------------------------------
48 #define DEFAULT_DROPBUTTON_WIDTH 19
50 #define BMP_BUTTON_MARGIN 4
52 #define DEFAULT_POPUP_HEIGHT 400
54 #define DEFAULT_TEXT_INDENT 3
56 #define COMBO_MARGIN 2 // spacing right of wxTextCtrl
59 #if defined(__WXMSW__)
61 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
62 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
63 // native controls work on it like normal.
64 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
65 #define TEXTCTRL_TEXT_CENTERED 0 // 1 if text in textctrl is vertically centered
66 #define FOCUS_RING 0 // No focus ring on wxMSW
68 //#undef wxUSE_POPUPWIN
69 //#define wxUSE_POPUPWIN 0
71 #elif defined(__WXGTK__)
73 // NB: It is not recommended to use wxDialog as popup on wxGTK, because of
74 // this bug: If wxDialog is hidden, its position becomes corrupt
75 // between hide and next show, but without internal coordinates being
76 // reflected (or something like that - atleast commenting out ->Hide()
77 // seemed to eliminate the position change).
79 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
80 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
81 // native controls work on it like normal.
82 #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
83 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
84 #define FOCUS_RING 0 // No focus ring on wxGTK
86 #elif defined(__WXMAC__)
88 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
89 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
90 // native controls work on it like normal.
91 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
92 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
93 #define FOCUS_RING 3 // Reserve room for the textctrl's focus ring to display
95 #undef DEFAULT_DROPBUTTON_WIDTH
96 #define DEFAULT_DROPBUTTON_WIDTH 22
98 #define COMBO_MARGIN FOCUS_RING
102 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
103 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
104 // native controls work on it like normal.
105 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
106 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
112 // Popupwin is really only supported on wxMSW (not WINCE) and wxGTK, regardless
113 // what the wxUSE_POPUPWIN says.
114 // FIXME: Why isn't wxUSE_POPUPWIN reliable any longer? (it was in wxW2.6.2)
115 #if (!defined(__WXMSW__) && !defined(__WXGTK__)) || defined(__WXWINCE__)
116 #undef wxUSE_POPUPWIN
117 #define wxUSE_POPUPWIN 0
122 #include "wx/popupwin.h"
124 #undef USE_TRANSIENT_POPUP
125 #define USE_TRANSIENT_POPUP 0
129 // Define different types of popup windows
133 POPUPWIN_WXPOPUPTRANSIENTWINDOW
= 1,
134 POPUPWIN_WXPOPUPWINDOW
= 2,
135 POPUPWIN_WXDIALOG
= 3
139 #if USE_TRANSIENT_POPUP
140 // wxPopupTransientWindow is implemented
142 #define wxComboPopupWindowBase wxPopupTransientWindow
143 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPTRANSIENTWINDOW
144 #define USES_WXPOPUPTRANSIENTWINDOW 1
146 #if TRANSIENT_POPUPWIN_IS_PERFECT
148 #elif POPUPWIN_IS_PERFECT
149 #define wxComboPopupWindowBase2 wxPopupWindow
150 #define SECONDARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
151 #define USES_WXPOPUPWINDOW 1
153 #define wxComboPopupWindowBase2 wxDialog
154 #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG
155 #define USES_WXDIALOG 1
159 // wxPopupWindow (but not wxPopupTransientWindow) is properly implemented
161 #define wxComboPopupWindowBase wxPopupWindow
162 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
163 #define USES_WXPOPUPWINDOW 1
165 #if !POPUPWIN_IS_PERFECT
166 #define wxComboPopupWindowBase2 wxDialog
167 #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG
168 #define USES_WXDIALOG 1
172 // wxPopupWindow is not implemented
174 #define wxComboPopupWindowBase wxDialog
175 #define PRIMARY_POPUP_TYPE POPUPWIN_WXDIALOG
176 #define USES_WXDIALOG 1
181 #ifndef USES_WXPOPUPTRANSIENTWINDOW
182 #define USES_WXPOPUPTRANSIENTWINDOW 0
185 #ifndef USES_WXPOPUPWINDOW
186 #define USES_WXPOPUPWINDOW 0
189 #ifndef USES_WXDIALOG
190 #define USES_WXDIALOG 0
194 #if USES_WXPOPUPWINDOW
195 #define INSTALL_TOPLEV_HANDLER 1
197 #define INSTALL_TOPLEV_HANDLER 0
203 // * wxComboPopupWindow for external use (ie. replace old wxUniv wxPopupComboWindow)
207 // ----------------------------------------------------------------------------
208 // wxComboFrameEventHandler takes care of hiding the popup when events happen
209 // in its top level parent.
210 // ----------------------------------------------------------------------------
212 #if INSTALL_TOPLEV_HANDLER
215 // This will no longer be necessary after wxTransientPopupWindow
216 // works well on all platforms.
219 class wxComboFrameEventHandler
: public wxEvtHandler
222 wxComboFrameEventHandler( wxComboCtrlBase
* pCb
);
223 virtual ~wxComboFrameEventHandler();
227 void OnIdle( wxIdleEvent
& event
);
228 void OnMouseEvent( wxMouseEvent
& event
);
229 void OnActivate( wxActivateEvent
& event
);
230 void OnResize( wxSizeEvent
& event
);
231 void OnMove( wxMoveEvent
& event
);
232 void OnMenuEvent( wxMenuEvent
& event
);
233 void OnClose( wxCloseEvent
& event
);
236 wxWindow
* m_focusStart
;
237 wxComboCtrlBase
* m_combo
;
240 DECLARE_EVENT_TABLE()
243 BEGIN_EVENT_TABLE(wxComboFrameEventHandler
, wxEvtHandler
)
244 EVT_IDLE(wxComboFrameEventHandler::OnIdle
)
245 EVT_LEFT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
246 EVT_RIGHT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
247 EVT_SIZE(wxComboFrameEventHandler::OnResize
)
248 EVT_MOVE(wxComboFrameEventHandler::OnMove
)
249 EVT_MENU_HIGHLIGHT(wxID_ANY
,wxComboFrameEventHandler::OnMenuEvent
)
250 EVT_MENU_OPEN(wxComboFrameEventHandler::OnMenuEvent
)
251 EVT_ACTIVATE(wxComboFrameEventHandler::OnActivate
)
252 EVT_CLOSE(wxComboFrameEventHandler::OnClose
)
255 wxComboFrameEventHandler::wxComboFrameEventHandler( wxComboCtrlBase
* combo
)
261 wxComboFrameEventHandler::~wxComboFrameEventHandler()
265 void wxComboFrameEventHandler::OnPopup()
267 m_focusStart
= ::wxWindow::FindFocus();
270 void wxComboFrameEventHandler::OnIdle( wxIdleEvent
& event
)
272 wxWindow
* winFocused
= ::wxWindow::FindFocus();
274 wxWindow
* popup
= m_combo
->GetPopupControl()->GetControl();
275 wxWindow
* winpopup
= m_combo
->GetPopupWindow();
278 winFocused
!= m_focusStart
&&
279 winFocused
!= popup
&&
280 winFocused
->GetParent() != popup
&&
281 winFocused
!= winpopup
&&
282 winFocused
->GetParent() != winpopup
&&
283 winFocused
!= m_combo
&&
284 winFocused
!= m_combo
->GetButton() // GTK (atleast) requires this
287 m_combo
->HidePopup();
293 void wxComboFrameEventHandler::OnMenuEvent( wxMenuEvent
& event
)
295 m_combo
->HidePopup();
299 void wxComboFrameEventHandler::OnMouseEvent( wxMouseEvent
& event
)
301 m_combo
->HidePopup();
305 void wxComboFrameEventHandler::OnClose( wxCloseEvent
& event
)
307 m_combo
->HidePopup();
311 void wxComboFrameEventHandler::OnActivate( wxActivateEvent
& event
)
313 m_combo
->HidePopup();
317 void wxComboFrameEventHandler::OnResize( wxSizeEvent
& event
)
319 m_combo
->HidePopup();
323 void wxComboFrameEventHandler::OnMove( wxMoveEvent
& event
)
325 m_combo
->HidePopup();
329 #endif // INSTALL_TOPLEV_HANDLER
331 // ----------------------------------------------------------------------------
332 // wxComboPopupWindow is, in essence, wxPopupWindow customized for
334 // ----------------------------------------------------------------------------
336 class wxComboPopupWindow
: public wxComboPopupWindowBase
340 wxComboPopupWindow( wxComboCtrlBase
*parent
,
342 #if USES_WXPOPUPWINDOW || USES_WXPOPUPTRANSIENTWINDOW
343 : wxComboPopupWindowBase(parent
,style
)
345 : wxComboPopupWindowBase(parent
,
356 #if USES_WXPOPUPTRANSIENTWINDOW
357 virtual bool Show( bool show
);
358 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
360 virtual void OnDismiss();
368 #if USES_WXPOPUPTRANSIENTWINDOW
369 bool wxComboPopupWindow::Show( bool show
)
371 // Guard against recursion
373 return wxComboPopupWindowBase::Show(show
);
377 wxASSERT( IsKindOf(CLASSINFO(wxPopupTransientWindow
)) );
379 wxPopupTransientWindow
* ptw
= (wxPopupTransientWindow
*) this;
380 wxComboCtrlBase
* combo
= (wxComboCtrlBase
*) GetParent();
382 if ( show
!= ptw
->IsShown() )
385 ptw
->Popup(combo
->GetPopupControl()->GetControl());
395 bool wxComboPopupWindow::ProcessLeftDown(wxMouseEvent
& event
)
397 return wxPopupTransientWindow::ProcessLeftDown(event
);
400 // First thing that happens when a transient popup closes is that this method gets called.
401 void wxComboPopupWindow::OnDismiss()
403 wxComboCtrlBase
* combo
= (wxComboCtrlBase
*) GetParent();
404 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxComboCtrlBase
)),
405 wxT("parent might not be wxComboCtrl, but check IMPLEMENT_DYNAMIC_CLASS(2) macro for correctness") );
407 combo
->OnPopupDismiss();
409 #endif // USES_WXPOPUPTRANSIENTWINDOW
412 // ----------------------------------------------------------------------------
413 // wxComboPopupWindowEvtHandler does bulk of the custom event handling
414 // of a popup window. It is separate so we can have different types
416 // ----------------------------------------------------------------------------
418 class wxComboPopupWindowEvtHandler
: public wxEvtHandler
422 wxComboPopupWindowEvtHandler( wxComboCtrlBase
*parent
)
427 void OnSizeEvent( wxSizeEvent
& event
);
428 void OnKeyEvent(wxKeyEvent
& event
);
430 void OnActivate( wxActivateEvent
& event
);
434 wxComboCtrlBase
* m_combo
;
436 DECLARE_EVENT_TABLE()
440 BEGIN_EVENT_TABLE(wxComboPopupWindowEvtHandler
, wxEvtHandler
)
441 EVT_KEY_DOWN(wxComboPopupWindowEvtHandler::OnKeyEvent
)
442 EVT_KEY_UP(wxComboPopupWindowEvtHandler::OnKeyEvent
)
444 EVT_ACTIVATE(wxComboPopupWindowEvtHandler::OnActivate
)
446 EVT_SIZE(wxComboPopupWindowEvtHandler::OnSizeEvent
)
450 void wxComboPopupWindowEvtHandler::OnSizeEvent( wxSizeEvent
& WXUNUSED(event
) )
452 // Block the event so that the popup control does not get auto-resized.
455 void wxComboPopupWindowEvtHandler::OnKeyEvent( wxKeyEvent
& event
)
457 // Relay keyboard event to the main child controls
458 wxWindowList children
= m_combo
->GetPopupWindow()->GetChildren();
459 wxWindowList::iterator node
= children
.begin();
460 wxWindow
* child
= (wxWindow
*)*node
;
461 child
->AddPendingEvent(event
);
465 void wxComboPopupWindowEvtHandler::OnActivate( wxActivateEvent
& event
)
467 if ( !event
.GetActive() )
469 // Tell combo control that we are dismissed.
470 m_combo
->HidePopup();
478 // ----------------------------------------------------------------------------
481 // ----------------------------------------------------------------------------
483 wxComboPopup::~wxComboPopup()
487 void wxComboPopup::OnPopup()
491 void wxComboPopup::OnDismiss()
495 wxSize
wxComboPopup::GetAdjustedSize( int minWidth
,
497 int WXUNUSED(maxHeight
) )
499 return wxSize(minWidth
,prefHeight
);
502 void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase
* combo
,
503 wxDC
& dc
, const wxRect
& rect
)
505 if ( combo
->GetWindowStyle() & wxCB_READONLY
) // ie. no textctrl
507 combo
->PrepareBackground(dc
,rect
,0);
509 dc
.DrawText( combo
->GetValue(),
510 rect
.x
+ combo
->GetTextIndent(),
511 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
515 void wxComboPopup::PaintComboControl( wxDC
& dc
, const wxRect
& rect
)
517 DefaultPaintComboControl(m_combo
,dc
,rect
);
520 void wxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
525 void wxComboPopup::OnComboDoubleClick()
529 void wxComboPopup::SetStringValue( const wxString
& WXUNUSED(value
) )
533 bool wxComboPopup::LazyCreate()
538 void wxComboPopup::Dismiss()
540 m_combo
->HidePopup();
543 // ----------------------------------------------------------------------------
545 // ----------------------------------------------------------------------------
548 // This is pushed to the event handler queue of the child textctrl.
550 class wxComboBoxExtraInputHandler
: public wxEvtHandler
554 wxComboBoxExtraInputHandler( wxComboCtrlBase
* combo
)
559 virtual ~wxComboBoxExtraInputHandler() { }
560 void OnKey(wxKeyEvent
& event
);
561 void OnFocus(wxFocusEvent
& event
);
564 wxComboCtrlBase
* m_combo
;
567 DECLARE_EVENT_TABLE()
571 BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler
, wxEvtHandler
)
572 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey
)
573 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
577 void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent
& event
)
579 // Let the wxComboCtrl event handler have a go first.
580 wxComboCtrlBase
* combo
= m_combo
;
581 wxObject
* prevObj
= event
.GetEventObject();
583 event
.SetId(combo
->GetId());
584 event
.SetEventObject(combo
);
585 combo
->GetEventHandler()->ProcessEvent(event
);
587 event
.SetId(((wxWindow
*)prevObj
)->GetId());
588 event
.SetEventObject(prevObj
);
591 void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent
& event
)
593 // FIXME: This code does run when control is clicked,
594 // yet on Windows it doesn't select all the text.
595 if ( !(m_combo
->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT
) )
597 if ( m_combo
->GetTextCtrl() )
598 m_combo
->GetTextCtrl()->SelectAll();
600 m_combo
->SetSelection(-1,-1);
603 // Send focus indication to parent.
604 // NB: This is needed for cases where the textctrl gets focus
605 // instead of its parent. While this may trigger multiple
606 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
607 // from combo's focus event handler), they should be quite
609 wxFocusEvent
evt2(wxEVT_SET_FOCUS
,m_combo
->GetId());
610 evt2
.SetEventObject(m_combo
);
611 m_combo
->GetEventHandler()->ProcessEvent(evt2
);
618 // This is pushed to the event handler queue of the control in popup.
621 class wxComboPopupExtraEventHandler
: public wxEvtHandler
625 wxComboPopupExtraEventHandler( wxComboCtrlBase
* combo
)
629 m_beenInside
= false;
631 virtual ~wxComboPopupExtraEventHandler() { }
633 void OnMouseEvent( wxMouseEvent
& event
);
635 // Called from wxComboCtrlBase::OnPopupDismiss
636 void OnPopupDismiss()
638 m_beenInside
= false;
642 wxComboCtrlBase
* m_combo
;
647 DECLARE_EVENT_TABLE()
651 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler
, wxEvtHandler
)
652 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent
)
656 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent
& event
)
658 wxPoint pt
= event
.GetPosition();
659 wxSize sz
= m_combo
->GetPopupControl()->GetControl()->GetClientSize();
660 int evtType
= event
.GetEventType();
661 bool isInside
= pt
.x
>= 0 && pt
.y
>= 0 && pt
.x
< sz
.x
&& pt
.y
< sz
.y
;
663 if ( evtType
== wxEVT_MOTION
||
664 evtType
== wxEVT_LEFT_DOWN
||
665 evtType
== wxEVT_RIGHT_DOWN
)
667 // Block motion and click events outside the popup
668 if ( !isInside
|| !m_combo
->IsPopupShown() )
674 else if ( evtType
== wxEVT_LEFT_UP
)
676 if ( !m_combo
->IsPopupShown() )
691 // Some mouse events to popup that happen outside it, before cursor
692 // has been inside the popu, need to be ignored by it but relayed to
695 wxWindow
* btn
= m_combo
->GetButton();
697 btn
->GetEventHandler()->AddPendingEvent(event
);
699 m_combo
->GetEventHandler()->AddPendingEvent(event
);
711 // ----------------------------------------------------------------------------
713 // ----------------------------------------------------------------------------
716 BEGIN_EVENT_TABLE(wxComboCtrlBase
, wxControl
)
717 EVT_TEXT(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
718 EVT_SIZE(wxComboCtrlBase::OnSizeEvent
)
719 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent
)
720 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent
)
721 EVT_IDLE(wxComboCtrlBase::OnIdleEvent
)
722 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
723 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent
)
724 EVT_TEXT_ENTER(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
725 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged
)
729 IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase
, wxControl
)
731 void wxComboCtrlBase::Init()
733 m_winPopup
= (wxWindow
*)NULL
;
734 m_popup
= (wxWindow
*)NULL
;
735 m_popupWinState
= Hidden
;
736 m_btn
= (wxWindow
*) NULL
;
737 m_text
= (wxTextCtrl
*) NULL
;
738 m_popupInterface
= (wxComboPopup
*) NULL
;
740 m_popupExtraHandler
= (wxEvtHandler
*) NULL
;
741 m_textEvtHandler
= (wxEvtHandler
*) NULL
;
743 #if INSTALL_TOPLEV_HANDLER
744 m_toplevEvtHandler
= (wxEvtHandler
*) NULL
;
747 m_mainCtrlWnd
= this;
750 m_widthMinPopup
= -1;
752 m_widthCustomPaint
= 0;
753 m_widthCustomBorder
= 0;
757 m_blankButtonBg
= false;
759 m_popupWinType
= POPUPWIN_NONE
;
760 m_btnWid
= m_btnHei
= -1;
768 m_timeCanAcceptClick
= 0;
770 m_resetFocus
= false;
773 bool wxComboCtrlBase::Create(wxWindow
*parent
,
775 const wxString
& value
,
779 const wxValidator
& validator
,
780 const wxString
& name
)
782 if ( !wxControl::Create(parent
,
786 style
| wxWANTS_CHARS
,
791 m_valueString
= value
;
795 m_absIndent
= GetNativeTextIndent();
797 m_iFlags
|= wxCC_IFLAG_CREATED
;
799 // If x and y indicate valid size, wxSizeEvent won't be
800 // emitted automatically, so we need to add artifical one.
801 if ( size
.x
> 0 && size
.y
> 0 )
803 wxSizeEvent
evt(size
,GetId());
804 GetEventHandler()->AddPendingEvent(evt
);
810 void wxComboCtrlBase::InstallInputHandlers()
814 m_textEvtHandler
= new wxComboBoxExtraInputHandler(this);
815 m_text
->PushEventHandler(m_textEvtHandler
);
820 wxComboCtrlBase::CreateTextCtrl(int style
, const wxValidator
& validator
)
822 if ( !(m_windowStyle
& wxCB_READONLY
) )
827 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
828 // not used by the wxPropertyGrid and therefore the tab is processed by
829 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
830 // navigation event is then sent to the wrong window.
831 style
|= wxTE_PROCESS_TAB
;
833 if ( HasFlag(wxTE_PROCESS_ENTER
) )
834 style
|= wxTE_PROCESS_ENTER
;
836 // Ignore EVT_TEXT generated by the constructor (but only
837 // if the event redirector already exists)
838 // NB: This must be " = 1" instead of "++";
839 if ( m_textEvtHandler
)
844 m_text
= new wxTextCtrl(this, wxID_ANY
, m_valueString
,
845 wxDefaultPosition
, wxSize(10,-1),
850 void wxComboCtrlBase::OnThemeChange()
852 // Leave the default bg on the Mac so the area used by the focus ring will
853 // be the correct colour and themed brush. Instead we'll use
854 // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed.
856 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
860 wxComboCtrlBase::~wxComboCtrlBase()
865 #if INSTALL_TOPLEV_HANDLER
866 delete ((wxComboFrameEventHandler
*)m_toplevEvtHandler
);
867 m_toplevEvtHandler
= (wxEvtHandler
*) NULL
;
873 m_text
->RemoveEventHandler(m_textEvtHandler
);
875 delete m_textEvtHandler
;
879 // ----------------------------------------------------------------------------
881 // ----------------------------------------------------------------------------
883 // Recalculates button and textctrl areas
884 void wxComboCtrlBase::CalculateAreas( int btnWidth
)
886 wxSize sz
= GetClientSize();
887 int customBorder
= m_widthCustomBorder
;
888 int btnBorder
; // border for button only
890 // check if button should really be outside the border: we'll do it it if
891 // its platform default or bitmap+pushbutton background is used, but not if
892 // there is vertical size adjustment or horizontal spacing.
893 if ( ( (m_iFlags
& wxCC_BUTTON_OUTSIDE_BORDER
) ||
894 (m_bmpNormal
.Ok() && m_blankButtonBg
) ) &&
895 m_btnSpacingX
== 0 &&
898 m_iFlags
|= wxCC_IFLAG_BUTTON_OUTSIDE
;
903 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
904 btnBorder
= customBorder
;
907 // Defaul indentation
908 if ( m_absIndent
< 0 )
909 m_absIndent
= GetNativeTextIndent();
911 int butWidth
= btnWidth
;
914 butWidth
= m_btnWidDefault
;
916 m_btnWidDefault
= butWidth
;
921 int butHeight
= sz
.y
- btnBorder
*2;
923 // Adjust button width
928 // Adjust button width to match aspect ratio
929 // (but only if control is smaller than best size).
930 int bestHeight
= GetBestSize().y
;
931 int height
= GetSize().y
;
933 if ( height
< bestHeight
)
935 // Make very small buttons square, as it makes
936 // them accommodate arrow image better and still
939 butWidth
= (height
*butWidth
)/bestHeight
;
941 butWidth
= butHeight
;
945 // Adjust button height
947 butHeight
= m_btnHei
;
949 // Use size of normal bitmap if...
952 // button width is set to default and blank button bg is not drawn
953 if ( m_bmpNormal
.Ok() )
955 int bmpReqWidth
= m_bmpNormal
.GetWidth();
956 int bmpReqHeight
= m_bmpNormal
.GetHeight();
958 // If drawing blank button background, we need to add some margin.
959 if ( m_blankButtonBg
)
961 bmpReqWidth
+= BMP_BUTTON_MARGIN
*2;
962 bmpReqHeight
+= BMP_BUTTON_MARGIN
*2;
965 if ( butWidth
< bmpReqWidth
|| ( m_btnWid
== 0 && !m_blankButtonBg
) )
966 butWidth
= bmpReqWidth
;
967 if ( butHeight
< bmpReqHeight
|| ( m_btnHei
== 0 && !m_blankButtonBg
) )
968 butHeight
= bmpReqHeight
;
970 // Need to fix height?
971 if ( (sz
.y
-(customBorder
*2)) < butHeight
&& btnWidth
== 0 )
973 int newY
= butHeight
+(customBorder
*2);
974 SetClientSize(wxDefaultCoord
,newY
);
979 int butAreaWid
= butWidth
+ (m_btnSpacingX
*2);
981 m_btnSize
.x
= butWidth
;
982 m_btnSize
.y
= butHeight
;
984 m_btnArea
.x
= ( m_btnSide
==wxRIGHT
? sz
.x
- butAreaWid
- btnBorder
: btnBorder
);
985 m_btnArea
.y
= btnBorder
+ FOCUS_RING
;
986 m_btnArea
.width
= butAreaWid
;
987 m_btnArea
.height
= sz
.y
- ((btnBorder
+FOCUS_RING
)*2);
989 m_tcArea
.x
= ( m_btnSide
==wxRIGHT
? 0 : butAreaWid
) + customBorder
+ FOCUS_RING
;
990 m_tcArea
.y
= customBorder
+ FOCUS_RING
;
991 m_tcArea
.width
= sz
.x
- butAreaWid
- (customBorder
*2) - (FOCUS_RING
*2);
992 m_tcArea
.height
= sz
.y
- ((customBorder
+FOCUS_RING
)*2);
997 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
998 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1003 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
)
1008 #if !TEXTCTRL_TEXT_CENTERED
1010 wxSize sz
= GetClientSize();
1012 int customBorder
= m_widthCustomBorder
;
1013 if ( (m_text
->GetWindowStyleFlag() & wxBORDER_MASK
) == wxNO_BORDER
)
1016 int tcSizeY
= m_text
->GetBestSize().y
;
1017 int diff
= sz
.y
- tcSizeY
;
1018 int y
= textCtrlYAdjust
+ (diff
/2);
1020 if ( y
< customBorder
)
1023 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
+ m_absIndent
+ textCtrlXAdjust
,
1025 m_tcArea
.width
- COMBO_MARGIN
-
1026 (textCtrlXAdjust
+ m_widthCustomPaint
+ m_absIndent
),
1029 // Make sure textctrl doesn't exceed the bottom custom border
1030 wxSize tsz
= m_text
->GetSize();
1031 diff
= (y
+ tsz
.y
) - (sz
.y
- customBorder
);
1034 tsz
.y
= tsz
.y
- diff
- 1;
1035 m_text
->SetSize(tsz
);
1039 #else // TEXTCTRL_TEXT_CENTERED
1040 wxUnusedVar(textCtrlXAdjust
);
1041 wxUnusedVar(textCtrlYAdjust
);
1042 #endif // !TEXTCTRL_TEXT_CENTERED/TEXTCTRL_TEXT_CENTERED
1044 // If it has border, have textctrl will the entire text field.
1045 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
,
1047 m_tcArea
.width
- m_widthCustomPaint
,
1052 wxSize
wxComboCtrlBase::DoGetBestSize() const
1054 wxSize
sizeText(150,0);
1057 sizeText
= m_text
->GetBestSize();
1059 // TODO: Better method to calculate close-to-native control height.
1063 fhei
= (m_font
.GetPointSize()*2) + 5;
1064 else if ( wxNORMAL_FONT
->Ok() )
1065 fhei
= (wxNORMAL_FONT
->GetPointSize()*2) + 5;
1067 fhei
= sizeText
.y
+ 4;
1069 // Need to force height to accomodate bitmap?
1070 int btnSizeY
= m_btnSize
.y
;
1071 if ( m_bmpNormal
.Ok() && fhei
< btnSizeY
)
1074 // Control height doesn't depend on border
1077 int border = m_windowStyle & wxBORDER_MASK;
1078 if ( border == wxSIMPLE_BORDER )
1080 else if ( border == wxNO_BORDER )
1081 fhei += (m_widthCustomBorder*2);
1087 // Final adjustments
1093 // these are the numbers from the HIG:
1094 switch ( m_windowVariant
)
1096 case wxWINDOW_VARIANT_NORMAL
:
1100 case wxWINDOW_VARIANT_SMALL
:
1103 case wxWINDOW_VARIANT_MINI
:
1109 fhei
+= 2 * FOCUS_RING
;
1110 int width
= sizeText
.x
+ FOCUS_RING
+ COMBO_MARGIN
+ DEFAULT_DROPBUTTON_WIDTH
;
1112 wxSize
ret(width
, fhei
);
1117 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent
& event
)
1122 // defined by actual wxComboCtrls
1128 // ----------------------------------------------------------------------------
1129 // standard operations
1130 // ----------------------------------------------------------------------------
1132 bool wxComboCtrlBase::Enable(bool enable
)
1134 if ( !wxControl::Enable(enable
) )
1138 m_btn
->Enable(enable
);
1140 m_text
->Enable(enable
);
1145 bool wxComboCtrlBase::Show(bool show
)
1147 if ( !wxControl::Show(show
) )
1159 bool wxComboCtrlBase::SetFont ( const wxFont
& font
)
1161 if ( !wxControl::SetFont(font
) )
1165 m_text
->SetFont(font
);
1171 void wxComboCtrlBase::DoSetToolTip(wxToolTip
*tooltip
)
1173 wxControl::DoSetToolTip(tooltip
);
1175 // Set tool tip for button and text box
1178 const wxString
&tip
= tooltip
->GetTip();
1179 if ( m_text
) m_text
->SetToolTip(tip
);
1180 if ( m_btn
) m_btn
->SetToolTip(tip
);
1184 if ( m_text
) m_text
->SetToolTip( (wxToolTip
*) NULL
);
1185 if ( m_btn
) m_btn
->SetToolTip( (wxToolTip
*) NULL
);
1188 #endif // wxUSE_TOOLTIPS
1190 #if wxUSE_VALIDATORS
1191 void wxComboCtrlBase::SetValidator(const wxValidator
& validator
)
1193 wxTextCtrl
* textCtrl
= GetTextCtrl();
1196 textCtrl
->SetValidator( validator
);
1199 wxValidator
* wxComboCtrlBase::GetValidator()
1201 wxTextCtrl
* textCtrl
= GetTextCtrl();
1204 return textCtrl
->GetValidator();
1206 return wxControl::GetValidator();
1208 #endif // wxUSE_VALIDATORS
1210 // ----------------------------------------------------------------------------
1212 // ----------------------------------------------------------------------------
1214 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1215 // prepare combo box background on area in a way typical on platform
1216 void wxComboCtrlBase::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
1218 wxSize sz
= GetClientSize();
1220 bool isFocused
; // also selected
1222 // For smaller size control (and for disabled background) use less spacing
1226 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1229 isEnabled
= IsEnabled();
1230 isFocused
= ShouldDrawFocus();
1232 // Windows-style: for smaller size control (and for disabled background) use less spacing
1233 focusSpacingX
= isEnabled
? 2 : 1;
1234 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
1238 // Drawing a list item
1239 isEnabled
= true; // they are never disabled
1240 isFocused
= flags
& wxCONTROL_SELECTED
? true : false;
1246 // Set the background sub-rectangle for selection, disabled etc
1247 wxRect
selRect(rect
);
1248 selRect
.y
+= focusSpacingY
;
1249 selRect
.height
-= (focusSpacingY
*2);
1253 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1254 wcp
+= m_widthCustomPaint
;
1256 selRect
.x
+= wcp
+ focusSpacingX
;
1257 selRect
.width
-= wcp
+ (focusSpacingX
*2);
1263 // If popup is hidden and this control is focused,
1264 // then draw the focus-indicator (selbgcolor background etc.).
1267 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1268 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1272 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
1273 #ifndef __WXMAC__ // see note in OnThemeChange
1274 bgCol
= GetBackgroundColour();
1276 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1282 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
1283 #ifndef __WXMAC__ // see note in OnThemeChange
1284 bgCol
= GetBackgroundColour();
1286 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1290 dc
.SetBrush( bgCol
);
1292 dc
.DrawRectangle( selRect
);
1294 // Don't clip exactly to the selection rectangle so we can draw
1295 // to the non-selected area in front of it.
1296 wxRect
clipRect(rect
.x
,rect
.y
,
1297 (selRect
.x
+selRect
.width
)-rect
.x
,rect
.height
);
1298 dc
.SetClippingRegion(clipRect
);
1301 // Save the library size a bit for platforms that re-implement this.
1302 void wxComboCtrlBase::PrepareBackground( wxDC
&, const wxRect
&, int ) const
1307 void wxComboCtrlBase::DrawButton( wxDC
& dc
, const wxRect
& rect
, int paintBg
)
1309 int drawState
= m_btnState
;
1312 if ( GetPopupWindowState() >= Animating
)
1313 drawState
|= wxCONTROL_PRESSED
;
1316 wxRect
drawRect(rect
.x
+m_btnSpacingX
,
1317 rect
.y
+((rect
.height
-m_btnSize
.y
)/2),
1321 // Make sure area is not larger than the control
1322 if ( drawRect
.y
< rect
.y
)
1323 drawRect
.y
= rect
.y
;
1324 if ( drawRect
.height
> rect
.height
)
1325 drawRect
.height
= rect
.height
;
1327 bool enabled
= IsEnabled();
1330 drawState
|= wxCONTROL_DISABLED
;
1332 if ( !m_bmpNormal
.Ok() )
1334 // Need to clear button background even if m_btn is present
1339 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
1340 bgCol
= GetParent()->GetBackgroundColour();
1342 bgCol
= GetBackgroundColour();
1346 dc
.DrawRectangle(rect
);
1349 // Draw standard button
1350 wxRendererNative::Get().DrawComboBoxDropButton(this,
1362 pBmp
= &m_bmpDisabled
;
1363 else if ( m_btnState
& wxCONTROL_PRESSED
)
1364 pBmp
= &m_bmpPressed
;
1365 else if ( m_btnState
& wxCONTROL_CURRENT
)
1368 pBmp
= &m_bmpNormal
;
1370 if ( m_blankButtonBg
)
1372 // If using blank button background, we need to clear its background
1373 // with button face colour instead of colour for rest of the control.
1376 wxColour bgCol
= GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1377 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1380 dc
.DrawRectangle(rect
);
1383 wxRendererNative::Get().DrawPushButton(this,
1392 // Need to clear button background even if m_btn is present
1393 // (assume non-button background was cleared just before this call so brushes are good)
1395 dc
.DrawRectangle(rect
);
1398 // Draw bitmap centered in drawRect
1399 dc
.DrawBitmap(*pBmp
,
1400 drawRect
.x
+ (drawRect
.width
-pBmp
->GetWidth())/2,
1401 drawRect
.y
+ (drawRect
.height
-pBmp
->GetHeight())/2,
1406 void wxComboCtrlBase::RecalcAndRefresh()
1410 wxSizeEvent
evt(GetSize(),GetId());
1411 GetEventHandler()->ProcessEvent(evt
);
1416 // ----------------------------------------------------------------------------
1417 // miscellaneous event handlers
1418 // ----------------------------------------------------------------------------
1420 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent
& event
)
1422 if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1424 if ( m_ignoreEvtText
> 0 )
1431 // Change event id, object and string before relaying it forward
1432 event
.SetId(GetId());
1433 wxString s
= event
.GetString();
1434 event
.SetEventObject(this);
1439 // call if cursor is on button area or mouse is captured for the button
1440 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent
& event
,
1443 int type
= event
.GetEventType();
1445 if ( type
== wxEVT_MOTION
)
1447 if ( flags
& wxCC_MF_ON_BUTTON
)
1449 if ( !(m_btnState
& wxCONTROL_CURRENT
) )
1451 // Mouse hover begins
1452 m_btnState
|= wxCONTROL_CURRENT
;
1453 if ( HasCapture() ) // Retain pressed state.
1454 m_btnState
|= wxCONTROL_PRESSED
;
1458 else if ( (m_btnState
& wxCONTROL_CURRENT
) )
1461 m_btnState
&= ~(wxCONTROL_CURRENT
|wxCONTROL_PRESSED
);
1465 else if ( type
== wxEVT_LEFT_DOWN
|| type
== wxEVT_LEFT_DCLICK
)
1467 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1469 m_btnState
|= wxCONTROL_PRESSED
;
1472 if ( !(m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
) )
1475 // If showing popup now, do not capture mouse or there will be interference
1479 else if ( type
== wxEVT_LEFT_UP
)
1482 // Only accept event if mouse was left-press was previously accepted
1486 if ( m_btnState
& wxCONTROL_PRESSED
)
1488 // If mouse was inside, fire the click event.
1489 if ( m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
)
1491 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1495 m_btnState
&= ~(wxCONTROL_PRESSED
);
1499 else if ( type
== wxEVT_LEAVE_WINDOW
)
1501 if ( m_btnState
& (wxCONTROL_CURRENT
|wxCONTROL_PRESSED
) )
1503 m_btnState
&= ~(wxCONTROL_CURRENT
);
1506 if ( IsPopupWindowState(Hidden
) )
1508 m_btnState
&= ~(wxCONTROL_PRESSED
);
1519 // returns true if event was consumed or filtered
1520 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent
& event
,
1521 int WXUNUSED(flags
) )
1523 wxLongLong t
= ::wxGetLocalTimeMillis();
1524 int evtType
= event
.GetEventType();
1526 #if USES_WXPOPUPWINDOW || USES_WXDIALOG
1527 if ( m_popupWinType
!= POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1529 if ( IsPopupWindowState(Visible
) &&
1530 ( evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_RIGHT_DOWN
) )
1538 // Filter out clicks on button immediately after popup dismiss (Windows like behaviour)
1539 if ( evtType
== wxEVT_LEFT_DOWN
&& t
< m_timeCanAcceptClick
)
1541 event
.SetEventType(0);
1548 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent
& event
)
1550 int evtType
= event
.GetEventType();
1552 if ( (evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_LEFT_DCLICK
) &&
1553 (m_windowStyle
& wxCB_READONLY
) )
1555 if ( GetPopupWindowState() >= Animating
)
1557 #if USES_WXPOPUPWINDOW
1558 // Click here always hides the popup.
1559 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1565 if ( !(m_windowStyle
& wxCC_SPECIAL_DCLICK
) )
1567 // In read-only mode, clicking the text is the
1568 // same as clicking the button.
1571 else if ( /*evtType == wxEVT_LEFT_UP || */evtType
== wxEVT_LEFT_DCLICK
)
1573 //if ( m_popupInterface->CycleValue() )
1575 if ( m_popupInterface
)
1576 m_popupInterface
->OnComboDoubleClick();
1581 if ( IsPopupShown() )
1583 // relay (some) mouse events to the popup
1584 if ( evtType
== wxEVT_MOUSEWHEEL
)
1585 m_popup
->AddPendingEvent(event
);
1591 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent
& event
)
1593 if ( IsPopupShown() )
1595 // pass it to the popped up control
1596 GetPopupControl()->GetControl()->AddPendingEvent(event
);
1600 int keycode
= event
.GetKeyCode();
1602 if ( keycode
== WXK_TAB
)
1604 wxNavigationKeyEvent evt
;
1606 wxWindow
* mainCtrl
= GetMainWindowOfCompositeControl();
1608 evt
.SetFlags(wxNavigationKeyEvent::FromTab
|
1609 (!event
.ShiftDown() ? wxNavigationKeyEvent::IsForward
1610 : wxNavigationKeyEvent::IsBackward
));
1611 evt
.SetEventObject(mainCtrl
);
1612 evt
.SetCurrentFocus(mainCtrl
);
1613 mainCtrl
->GetParent()->GetEventHandler()->AddPendingEvent(evt
);
1617 if ( IsKeyPopupToggle(event
) )
1623 int comboStyle
= GetWindowStyle();
1624 wxComboPopup
* popupInterface
= GetPopupControl();
1626 if ( !popupInterface
)
1632 if ( (comboStyle
& wxCB_READONLY
) ||
1633 (keycode
!= WXK_RIGHT
&& keycode
!= WXK_LEFT
) )
1635 popupInterface
->OnComboKeyEvent(event
);
1642 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent
& event
)
1644 if ( event
.GetEventType() == wxEVT_SET_FOCUS
)
1646 wxWindow
* tc
= GetTextCtrl();
1647 if ( tc
&& tc
!= DoFindFocus() )
1649 m_resetFocus
= true;
1658 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent
& WXUNUSED(event
) )
1662 m_resetFocus
= false;
1663 wxWindow
* tc
= GetTextCtrl();
1669 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
1672 // indentation may also have changed
1673 if ( !(m_iFlags
& wxCC_IFLAG_INDENT_SET
) )
1674 m_absIndent
= GetNativeTextIndent();
1678 // ----------------------------------------------------------------------------
1680 // ----------------------------------------------------------------------------
1682 // Create popup window and the child control
1683 void wxComboCtrlBase::CreatePopup()
1685 wxComboPopup
* popupInterface
= m_popupInterface
;
1690 #ifdef wxComboPopupWindowBase2
1691 if ( m_iFlags
& wxCC_IFLAG_USE_ALT_POPUP
)
1694 m_winPopup
= new wxComboPopupWindowBase2( this, wxNO_BORDER
);
1696 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
, wxEmptyString
,
1697 wxPoint(-21,-21), wxSize(20, 20),
1700 m_popupWinType
= SECONDARY_POPUP_TYPE
;
1705 m_winPopup
= new wxComboPopupWindow( this, wxNO_BORDER
);
1706 m_popupWinType
= PRIMARY_POPUP_TYPE
;
1708 m_popupWinEvtHandler
= new wxComboPopupWindowEvtHandler(this);
1709 m_winPopup
->PushEventHandler(m_popupWinEvtHandler
);
1712 popupInterface
->Create(m_winPopup
);
1713 m_popup
= popup
= popupInterface
->GetControl();
1715 m_popupExtraHandler
= new wxComboPopupExtraEventHandler(this);
1716 popup
->PushEventHandler( m_popupExtraHandler
);
1718 // This may be helpful on some platforms
1719 // (eg. it bypasses a wxGTK popupwindow bug where
1720 // window is not initially hidden when it should be)
1723 popupInterface
->m_iFlags
|= wxCP_IFLAG_CREATED
;
1726 // Destroy popup window and the child control
1727 void wxComboCtrlBase::DestroyPopup()
1732 m_popup
->RemoveEventHandler(m_popupExtraHandler
);
1734 delete m_popupExtraHandler
;
1736 delete m_popupInterface
;
1740 m_winPopup
->RemoveEventHandler(m_popupWinEvtHandler
);
1741 delete m_popupWinEvtHandler
;
1742 m_popupWinEvtHandler
= NULL
;
1743 m_winPopup
->Destroy();
1746 m_popupExtraHandler
= (wxEvtHandler
*) NULL
;
1747 m_popupInterface
= (wxComboPopup
*) NULL
;
1748 m_winPopup
= (wxWindow
*) NULL
;
1749 m_popup
= (wxWindow
*) NULL
;
1752 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup
* iface
)
1754 wxCHECK_RET( iface
, wxT("no popup interface set for wxComboCtrl") );
1758 iface
->InitBase(this);
1761 m_popupInterface
= iface
;
1763 if ( !iface
->LazyCreate() )
1769 m_popup
= (wxWindow
*) NULL
;
1772 // This must be done after creation
1773 if ( m_valueString
.length() )
1775 iface
->SetStringValue(m_valueString
);
1780 // Ensures there is atleast the default popup
1781 void wxComboCtrlBase::EnsurePopupControl()
1783 if ( !m_popupInterface
)
1784 SetPopupControl(NULL
);
1787 void wxComboCtrlBase::OnButtonClick()
1789 // Derived classes can override this method for totally custom
1794 void wxComboCtrlBase::ShowPopup()
1796 EnsurePopupControl();
1797 wxCHECK_RET( !IsPopupWindowState(Visible
), wxT("popup window already shown") );
1799 if ( IsPopupWindowState(Animating
) )
1804 // Space above and below
1810 wxSize ctrlSz
= GetSize();
1812 screenHeight
= wxSystemSettings::GetMetric( wxSYS_SCREEN_Y
);
1813 scrPos
= GetParent()->ClientToScreen(GetPosition());
1815 spaceAbove
= scrPos
.y
;
1816 spaceBelow
= screenHeight
- spaceAbove
- ctrlSz
.y
;
1818 maxHeightPopup
= spaceBelow
;
1819 if ( spaceAbove
> spaceBelow
)
1820 maxHeightPopup
= spaceAbove
;
1823 int widthPopup
= ctrlSz
.x
+ m_extLeft
+ m_extRight
;
1825 if ( widthPopup
< m_widthMinPopup
)
1826 widthPopup
= m_widthMinPopup
;
1828 wxWindow
* winPopup
= m_winPopup
;
1831 // Need to disable tab traversal of parent
1833 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1834 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1835 // that if transient popup is open, then tab traversal is to be ignored.
1836 // However, I think this code would still be needed for cases where
1837 // transient popup doesn't work yet (wxWinCE?).
1838 wxWindow
* parent
= GetParent();
1839 int parentFlags
= parent
->GetWindowStyle();
1840 if ( parentFlags
& wxTAB_TRAVERSAL
)
1842 parent
->SetWindowStyle( parentFlags
& ~(wxTAB_TRAVERSAL
) );
1843 m_iFlags
|= wxCC_IFLAG_PARENT_TAB_TRAVERSAL
;
1849 winPopup
= m_winPopup
;
1859 wxASSERT( !m_popup
|| m_popup
== popup
); // Consistency check.
1861 wxSize adjustedSize
= m_popupInterface
->GetAdjustedSize(widthPopup
,
1862 m_heightPopup
<=0?DEFAULT_POPUP_HEIGHT
:m_heightPopup
,
1865 popup
->SetSize(adjustedSize
);
1867 m_popupInterface
->OnPopup();
1870 // Reposition and resize popup window
1873 wxSize szp
= popup
->GetSize();
1876 int popupY
= scrPos
.y
+ ctrlSz
.y
;
1878 // Default anchor is wxLEFT
1879 int anchorSide
= m_anchorSide
;
1881 anchorSide
= wxLEFT
;
1883 int rightX
= scrPos
.x
+ ctrlSz
.x
+ m_extRight
- szp
.x
;
1884 int leftX
= scrPos
.x
- m_extLeft
;
1886 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
1889 int screenWidth
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
);
1891 // If there is not enough horizontal space, anchor on the other side.
1892 // If there is no space even then, place the popup at x 0.
1893 if ( anchorSide
== wxRIGHT
)
1897 if ( (leftX
+szp
.x
) < screenWidth
)
1898 anchorSide
= wxLEFT
;
1905 if ( (leftX
+szp
.x
) >= screenWidth
)
1908 anchorSide
= wxRIGHT
;
1914 // Select x coordinate according to the anchor side
1915 if ( anchorSide
== wxRIGHT
)
1917 else if ( anchorSide
== wxLEFT
)
1922 int showFlags
= CanDeferShow
;
1924 if ( spaceBelow
< szp
.y
)
1926 popupY
= scrPos
.y
- szp
.y
;
1927 showFlags
|= ShowAbove
;
1930 #if INSTALL_TOPLEV_HANDLER
1931 // Put top level window event handler into place
1932 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1934 if ( !m_toplevEvtHandler
)
1935 m_toplevEvtHandler
= new wxComboFrameEventHandler(this);
1937 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
1939 ((wxComboFrameEventHandler
*)m_toplevEvtHandler
)->OnPopup();
1940 toplev
->PushEventHandler( m_toplevEvtHandler
);
1944 // Set string selection (must be this way instead of SetStringSelection)
1947 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
1948 m_text
->SelectAll();
1950 m_popupInterface
->SetStringValue( m_text
->GetValue() );
1954 // This is neede since focus/selection indication may change when popup is shown
1958 // This must be after SetStringValue
1959 m_popupWinState
= Animating
;
1961 wxRect
popupWinRect( popupX
, popupY
, szp
.x
, szp
.y
);
1964 if ( (m_iFlags
& wxCC_IFLAG_DISABLE_POPUP_ANIM
) ||
1965 AnimateShow( popupWinRect
, showFlags
) )
1967 DoShowPopup( popupWinRect
, showFlags
);
1971 bool wxComboCtrlBase::AnimateShow( const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
) )
1976 void wxComboCtrlBase::DoShowPopup( const wxRect
& rect
, int WXUNUSED(flags
) )
1978 wxWindow
* winPopup
= m_winPopup
;
1980 if ( IsPopupWindowState(Animating
) )
1982 // Make sure the popup window is shown in the right position.
1983 // Should not matter even if animation already did this.
1985 // Some platforms (GTK) may like SetSize and Move to be separate
1986 // (though the bug was probably fixed).
1987 winPopup
->SetSize( rect
);
1991 m_popupWinState
= Visible
;
1993 else if ( IsPopupWindowState(Hidden
) )
1995 // Animation was aborted
1997 wxASSERT( !winPopup
->IsShown() );
1999 m_popupWinState
= Hidden
;
2003 void wxComboCtrlBase::OnPopupDismiss()
2005 // Just in case, avoid double dismiss
2006 if ( IsPopupWindowState(Hidden
) )
2009 // This must be set before focus - otherwise there will be recursive
2010 // OnPopupDismisses.
2011 m_popupWinState
= Hidden
;
2014 m_winPopup
->Disable();
2016 // Inform popup control itself
2017 m_popupInterface
->OnDismiss();
2019 if ( m_popupExtraHandler
)
2020 ((wxComboPopupExtraEventHandler
*)m_popupExtraHandler
)->OnPopupDismiss();
2022 #if INSTALL_TOPLEV_HANDLER
2023 // Remove top level window event handler
2024 if ( m_toplevEvtHandler
)
2026 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2028 toplev
->RemoveEventHandler( m_toplevEvtHandler
);
2032 m_timeCanAcceptClick
= ::wxGetLocalTimeMillis();
2034 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2035 m_timeCanAcceptClick
+= 150;
2037 // If cursor not on dropdown button, then clear its state
2038 // (technically not required by all ports, but do it for all just in case)
2039 if ( !m_btnArea
.Contains(ScreenToClient(::wxGetMousePosition())) )
2042 // Return parent's tab traversal flag.
2043 // See ShowPopup for notes.
2044 if ( m_iFlags
& wxCC_IFLAG_PARENT_TAB_TRAVERSAL
)
2046 wxWindow
* parent
= GetParent();
2047 parent
->SetWindowStyle( parent
->GetWindowStyle() | wxTAB_TRAVERSAL
);
2048 m_iFlags
&= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL
);
2051 // refresh control (necessary even if m_text)
2057 void wxComboCtrlBase::HidePopup()
2059 // Should be able to call this without popup interface
2060 if ( IsPopupWindowState(Hidden
) )
2063 // transfer value and show it in textctrl, if any
2064 if ( !IsPopupWindowState(Animating
) )
2065 SetValue( m_popupInterface
->GetStringValue() );
2072 // ----------------------------------------------------------------------------
2073 // customization methods
2074 // ----------------------------------------------------------------------------
2076 void wxComboCtrlBase::SetButtonPosition( int width
, int height
,
2077 int side
, int spacingX
)
2082 m_btnSpacingX
= spacingX
;
2087 wxSize
wxComboCtrlBase::GetButtonSize()
2089 if ( m_btnSize
.x
> 0 )
2092 wxSize
retSize(m_btnWid
,m_btnHei
);
2094 // Need to call CalculateAreas now if button size is
2095 // is not explicitly specified.
2096 if ( retSize
.x
<= 0 || retSize
.y
<= 0)
2100 retSize
= m_btnSize
;
2106 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap
& bmpNormal
,
2108 const wxBitmap
& bmpPressed
,
2109 const wxBitmap
& bmpHover
,
2110 const wxBitmap
& bmpDisabled
)
2112 m_bmpNormal
= bmpNormal
;
2113 m_blankButtonBg
= blankButtonBg
;
2115 if ( bmpPressed
.Ok() )
2116 m_bmpPressed
= bmpPressed
;
2118 m_bmpPressed
= bmpNormal
;
2120 if ( bmpHover
.Ok() )
2121 m_bmpHover
= bmpHover
;
2123 m_bmpHover
= bmpNormal
;
2125 if ( bmpDisabled
.Ok() )
2126 m_bmpDisabled
= bmpDisabled
;
2128 m_bmpDisabled
= bmpNormal
;
2133 void wxComboCtrlBase::SetCustomPaintWidth( int width
)
2137 // move textctrl accordingly
2138 wxRect r
= m_text
->GetRect();
2139 int inc
= width
- m_widthCustomPaint
;
2142 m_text
->SetSize( r
);
2145 m_widthCustomPaint
= width
;
2150 void wxComboCtrlBase::SetTextIndent( int indent
)
2154 m_absIndent
= GetNativeTextIndent();
2155 m_iFlags
&= ~(wxCC_IFLAG_INDENT_SET
);
2159 m_absIndent
= indent
;
2160 m_iFlags
|= wxCC_IFLAG_INDENT_SET
;
2166 wxCoord
wxComboCtrlBase::GetNativeTextIndent() const
2168 return DEFAULT_TEXT_INDENT
;
2171 // ----------------------------------------------------------------------------
2172 // methods forwarded to wxTextCtrl
2173 // ----------------------------------------------------------------------------
2175 wxString
wxComboCtrlBase::GetValue() const
2178 return m_text
->GetValue();
2179 return m_valueString
;
2182 void wxComboCtrlBase::SetValueWithEvent(const wxString
& value
, bool withEvent
)
2189 m_text
->SetValue(value
);
2190 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2191 m_text
->SelectAll();
2194 m_valueString
= value
;
2198 // Since wxComboPopup may want to paint the combo as well, we need
2199 // to set the string value here (as well as sometimes in ShowPopup).
2200 if ( m_valueString
!= value
&& m_popupInterface
)
2202 m_popupInterface
->SetStringValue(value
);
2206 void wxComboCtrlBase::SetValue(const wxString
& value
)
2208 SetValueWithEvent(value
, false);
2211 // In this SetValue variant wxComboPopup::SetStringValue is not called
2212 void wxComboCtrlBase::SetText(const wxString
& value
)
2214 // Unlike in SetValue(), this must be called here or
2215 // the behaviour will no be consistent in readonlys.
2216 EnsurePopupControl();
2218 m_valueString
= value
;
2223 m_text
->SetValue( value
);
2229 void wxComboCtrlBase::Copy()
2235 void wxComboCtrlBase::Cut()
2241 void wxComboCtrlBase::Paste()
2247 void wxComboCtrlBase::SetInsertionPoint(long pos
)
2250 m_text
->SetInsertionPoint(pos
);
2253 void wxComboCtrlBase::SetInsertionPointEnd()
2256 m_text
->SetInsertionPointEnd();
2259 long wxComboCtrlBase::GetInsertionPoint() const
2262 return m_text
->GetInsertionPoint();
2267 long wxComboCtrlBase::GetLastPosition() const
2270 return m_text
->GetLastPosition();
2275 void wxComboCtrlBase::Replace(long from
, long to
, const wxString
& value
)
2278 m_text
->Replace(from
, to
, value
);
2281 void wxComboCtrlBase::Remove(long from
, long to
)
2284 m_text
->Remove(from
, to
);
2287 void wxComboCtrlBase::SetSelection(long from
, long to
)
2290 m_text
->SetSelection(from
, to
);
2293 void wxComboCtrlBase::Undo()
2299 #endif // wxUSE_COMBOCTRL