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 if (GetParent()->GetWindowVariant() == wxWINDOW_VARIANT_SMALL
)
1351 drawState
|= wxCONTROL_SIZE_SMALL
;
1352 else if (GetParent()->GetWindowVariant() == wxWINDOW_VARIANT_MINI
)
1353 drawState
|= wxCONTROL_SIZE_MINI
;
1355 wxRendererNative::Get().DrawComboBoxDropButton(this,
1367 pBmp
= &m_bmpDisabled
;
1368 else if ( m_btnState
& wxCONTROL_PRESSED
)
1369 pBmp
= &m_bmpPressed
;
1370 else if ( m_btnState
& wxCONTROL_CURRENT
)
1373 pBmp
= &m_bmpNormal
;
1375 if ( m_blankButtonBg
)
1377 // If using blank button background, we need to clear its background
1378 // with button face colour instead of colour for rest of the control.
1381 wxColour bgCol
= GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1382 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1385 dc
.DrawRectangle(rect
);
1388 if (GetParent()->GetWindowVariant() == wxWINDOW_VARIANT_SMALL
)
1389 drawState
|= wxCONTROL_SIZE_SMALL
;
1390 else if (GetParent()->GetWindowVariant() == wxWINDOW_VARIANT_MINI
)
1391 drawState
|= wxCONTROL_SIZE_MINI
;
1393 wxRendererNative::Get().DrawPushButton(this,
1402 // Need to clear button background even if m_btn is present
1403 // (assume non-button background was cleared just before this call so brushes are good)
1405 dc
.DrawRectangle(rect
);
1408 // Draw bitmap centered in drawRect
1409 dc
.DrawBitmap(*pBmp
,
1410 drawRect
.x
+ (drawRect
.width
-pBmp
->GetWidth())/2,
1411 drawRect
.y
+ (drawRect
.height
-pBmp
->GetHeight())/2,
1416 void wxComboCtrlBase::RecalcAndRefresh()
1420 wxSizeEvent
evt(GetSize(),GetId());
1421 GetEventHandler()->ProcessEvent(evt
);
1426 // ----------------------------------------------------------------------------
1427 // miscellaneous event handlers
1428 // ----------------------------------------------------------------------------
1430 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent
& event
)
1432 if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1434 if ( m_ignoreEvtText
> 0 )
1441 // Change event id, object and string before relaying it forward
1442 event
.SetId(GetId());
1443 wxString s
= event
.GetString();
1444 event
.SetEventObject(this);
1449 // call if cursor is on button area or mouse is captured for the button
1450 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent
& event
,
1453 int type
= event
.GetEventType();
1455 if ( type
== wxEVT_MOTION
)
1457 if ( flags
& wxCC_MF_ON_BUTTON
)
1459 if ( !(m_btnState
& wxCONTROL_CURRENT
) )
1461 // Mouse hover begins
1462 m_btnState
|= wxCONTROL_CURRENT
;
1463 if ( HasCapture() ) // Retain pressed state.
1464 m_btnState
|= wxCONTROL_PRESSED
;
1468 else if ( (m_btnState
& wxCONTROL_CURRENT
) )
1471 m_btnState
&= ~(wxCONTROL_CURRENT
|wxCONTROL_PRESSED
);
1475 else if ( type
== wxEVT_LEFT_DOWN
|| type
== wxEVT_LEFT_DCLICK
)
1477 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1479 m_btnState
|= wxCONTROL_PRESSED
;
1482 if ( !(m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
) )
1485 // If showing popup now, do not capture mouse or there will be interference
1489 else if ( type
== wxEVT_LEFT_UP
)
1492 // Only accept event if mouse was left-press was previously accepted
1496 if ( m_btnState
& wxCONTROL_PRESSED
)
1498 // If mouse was inside, fire the click event.
1499 if ( m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
)
1501 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1505 m_btnState
&= ~(wxCONTROL_PRESSED
);
1509 else if ( type
== wxEVT_LEAVE_WINDOW
)
1511 if ( m_btnState
& (wxCONTROL_CURRENT
|wxCONTROL_PRESSED
) )
1513 m_btnState
&= ~(wxCONTROL_CURRENT
);
1516 if ( IsPopupWindowState(Hidden
) )
1518 m_btnState
&= ~(wxCONTROL_PRESSED
);
1529 // returns true if event was consumed or filtered
1530 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent
& event
,
1531 int WXUNUSED(flags
) )
1533 wxLongLong t
= ::wxGetLocalTimeMillis();
1534 int evtType
= event
.GetEventType();
1536 #if USES_WXPOPUPWINDOW || USES_WXDIALOG
1537 if ( m_popupWinType
!= POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1539 if ( IsPopupWindowState(Visible
) &&
1540 ( evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_RIGHT_DOWN
) )
1548 // Filter out clicks on button immediately after popup dismiss (Windows like behaviour)
1549 if ( evtType
== wxEVT_LEFT_DOWN
&& t
< m_timeCanAcceptClick
)
1551 event
.SetEventType(0);
1558 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent
& event
)
1560 int evtType
= event
.GetEventType();
1562 if ( (evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_LEFT_DCLICK
) &&
1563 (m_windowStyle
& wxCB_READONLY
) )
1565 if ( GetPopupWindowState() >= Animating
)
1567 #if USES_WXPOPUPWINDOW
1568 // Click here always hides the popup.
1569 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1575 if ( !(m_windowStyle
& wxCC_SPECIAL_DCLICK
) )
1577 // In read-only mode, clicking the text is the
1578 // same as clicking the button.
1581 else if ( /*evtType == wxEVT_LEFT_UP || */evtType
== wxEVT_LEFT_DCLICK
)
1583 //if ( m_popupInterface->CycleValue() )
1585 if ( m_popupInterface
)
1586 m_popupInterface
->OnComboDoubleClick();
1591 if ( IsPopupShown() )
1593 // relay (some) mouse events to the popup
1594 if ( evtType
== wxEVT_MOUSEWHEEL
)
1595 m_popup
->AddPendingEvent(event
);
1601 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent
& event
)
1603 if ( IsPopupShown() )
1605 // pass it to the popped up control
1606 GetPopupControl()->GetControl()->AddPendingEvent(event
);
1610 int keycode
= event
.GetKeyCode();
1612 if ( keycode
== WXK_TAB
)
1614 wxNavigationKeyEvent evt
;
1616 wxWindow
* mainCtrl
= GetMainWindowOfCompositeControl();
1618 evt
.SetFlags(wxNavigationKeyEvent::FromTab
|
1619 (!event
.ShiftDown() ? wxNavigationKeyEvent::IsForward
1620 : wxNavigationKeyEvent::IsBackward
));
1621 evt
.SetEventObject(mainCtrl
);
1622 evt
.SetCurrentFocus(mainCtrl
);
1623 mainCtrl
->GetParent()->GetEventHandler()->AddPendingEvent(evt
);
1627 if ( IsKeyPopupToggle(event
) )
1633 int comboStyle
= GetWindowStyle();
1634 wxComboPopup
* popupInterface
= GetPopupControl();
1636 if ( !popupInterface
)
1642 if ( (comboStyle
& wxCB_READONLY
) ||
1643 (keycode
!= WXK_RIGHT
&& keycode
!= WXK_LEFT
) )
1645 popupInterface
->OnComboKeyEvent(event
);
1652 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent
& event
)
1654 if ( event
.GetEventType() == wxEVT_SET_FOCUS
)
1656 wxWindow
* tc
= GetTextCtrl();
1657 if ( tc
&& tc
!= DoFindFocus() )
1659 m_resetFocus
= true;
1668 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent
& WXUNUSED(event
) )
1672 m_resetFocus
= false;
1673 wxWindow
* tc
= GetTextCtrl();
1679 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
1682 // indentation may also have changed
1683 if ( !(m_iFlags
& wxCC_IFLAG_INDENT_SET
) )
1684 m_absIndent
= GetNativeTextIndent();
1688 // ----------------------------------------------------------------------------
1690 // ----------------------------------------------------------------------------
1692 // Create popup window and the child control
1693 void wxComboCtrlBase::CreatePopup()
1695 wxComboPopup
* popupInterface
= m_popupInterface
;
1700 #ifdef wxComboPopupWindowBase2
1701 if ( m_iFlags
& wxCC_IFLAG_USE_ALT_POPUP
)
1704 m_winPopup
= new wxComboPopupWindowBase2( this, wxNO_BORDER
);
1706 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
, wxEmptyString
,
1707 wxPoint(-21,-21), wxSize(20, 20),
1710 m_popupWinType
= SECONDARY_POPUP_TYPE
;
1715 m_winPopup
= new wxComboPopupWindow( this, wxNO_BORDER
);
1716 m_popupWinType
= PRIMARY_POPUP_TYPE
;
1718 m_popupWinEvtHandler
= new wxComboPopupWindowEvtHandler(this);
1719 m_winPopup
->PushEventHandler(m_popupWinEvtHandler
);
1722 popupInterface
->Create(m_winPopup
);
1723 m_popup
= popup
= popupInterface
->GetControl();
1725 m_popupExtraHandler
= new wxComboPopupExtraEventHandler(this);
1726 popup
->PushEventHandler( m_popupExtraHandler
);
1728 // This may be helpful on some platforms
1729 // (eg. it bypasses a wxGTK popupwindow bug where
1730 // window is not initially hidden when it should be)
1733 popupInterface
->m_iFlags
|= wxCP_IFLAG_CREATED
;
1736 // Destroy popup window and the child control
1737 void wxComboCtrlBase::DestroyPopup()
1742 m_popup
->RemoveEventHandler(m_popupExtraHandler
);
1744 delete m_popupExtraHandler
;
1746 delete m_popupInterface
;
1750 m_winPopup
->RemoveEventHandler(m_popupWinEvtHandler
);
1751 delete m_popupWinEvtHandler
;
1752 m_popupWinEvtHandler
= NULL
;
1753 m_winPopup
->Destroy();
1756 m_popupExtraHandler
= (wxEvtHandler
*) NULL
;
1757 m_popupInterface
= (wxComboPopup
*) NULL
;
1758 m_winPopup
= (wxWindow
*) NULL
;
1759 m_popup
= (wxWindow
*) NULL
;
1762 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup
* iface
)
1764 wxCHECK_RET( iface
, wxT("no popup interface set for wxComboCtrl") );
1768 iface
->InitBase(this);
1771 m_popupInterface
= iface
;
1773 if ( !iface
->LazyCreate() )
1779 m_popup
= (wxWindow
*) NULL
;
1782 // This must be done after creation
1783 if ( m_valueString
.length() )
1785 iface
->SetStringValue(m_valueString
);
1790 // Ensures there is atleast the default popup
1791 void wxComboCtrlBase::EnsurePopupControl()
1793 if ( !m_popupInterface
)
1794 SetPopupControl(NULL
);
1797 void wxComboCtrlBase::OnButtonClick()
1799 // Derived classes can override this method for totally custom
1804 void wxComboCtrlBase::ShowPopup()
1806 EnsurePopupControl();
1807 wxCHECK_RET( !IsPopupWindowState(Visible
), wxT("popup window already shown") );
1809 if ( IsPopupWindowState(Animating
) )
1814 // Space above and below
1820 wxSize ctrlSz
= GetSize();
1822 screenHeight
= wxSystemSettings::GetMetric( wxSYS_SCREEN_Y
);
1823 scrPos
= GetParent()->ClientToScreen(GetPosition());
1825 spaceAbove
= scrPos
.y
;
1826 spaceBelow
= screenHeight
- spaceAbove
- ctrlSz
.y
;
1828 maxHeightPopup
= spaceBelow
;
1829 if ( spaceAbove
> spaceBelow
)
1830 maxHeightPopup
= spaceAbove
;
1833 int widthPopup
= ctrlSz
.x
+ m_extLeft
+ m_extRight
;
1835 if ( widthPopup
< m_widthMinPopup
)
1836 widthPopup
= m_widthMinPopup
;
1838 wxWindow
* winPopup
= m_winPopup
;
1841 // Need to disable tab traversal of parent
1843 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1844 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1845 // that if transient popup is open, then tab traversal is to be ignored.
1846 // However, I think this code would still be needed for cases where
1847 // transient popup doesn't work yet (wxWinCE?).
1848 wxWindow
* parent
= GetParent();
1849 int parentFlags
= parent
->GetWindowStyle();
1850 if ( parentFlags
& wxTAB_TRAVERSAL
)
1852 parent
->SetWindowStyle( parentFlags
& ~(wxTAB_TRAVERSAL
) );
1853 m_iFlags
|= wxCC_IFLAG_PARENT_TAB_TRAVERSAL
;
1859 winPopup
= m_winPopup
;
1869 wxASSERT( !m_popup
|| m_popup
== popup
); // Consistency check.
1871 wxSize adjustedSize
= m_popupInterface
->GetAdjustedSize(widthPopup
,
1872 m_heightPopup
<=0?DEFAULT_POPUP_HEIGHT
:m_heightPopup
,
1875 popup
->SetSize(adjustedSize
);
1877 m_popupInterface
->OnPopup();
1880 // Reposition and resize popup window
1883 wxSize szp
= popup
->GetSize();
1886 int popupY
= scrPos
.y
+ ctrlSz
.y
;
1888 // Default anchor is wxLEFT
1889 int anchorSide
= m_anchorSide
;
1891 anchorSide
= wxLEFT
;
1893 int rightX
= scrPos
.x
+ ctrlSz
.x
+ m_extRight
- szp
.x
;
1894 int leftX
= scrPos
.x
- m_extLeft
;
1896 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
1899 int screenWidth
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
);
1901 // If there is not enough horizontal space, anchor on the other side.
1902 // If there is no space even then, place the popup at x 0.
1903 if ( anchorSide
== wxRIGHT
)
1907 if ( (leftX
+szp
.x
) < screenWidth
)
1908 anchorSide
= wxLEFT
;
1915 if ( (leftX
+szp
.x
) >= screenWidth
)
1918 anchorSide
= wxRIGHT
;
1924 // Select x coordinate according to the anchor side
1925 if ( anchorSide
== wxRIGHT
)
1927 else if ( anchorSide
== wxLEFT
)
1932 int showFlags
= CanDeferShow
;
1934 if ( spaceBelow
< szp
.y
)
1936 popupY
= scrPos
.y
- szp
.y
;
1937 showFlags
|= ShowAbove
;
1940 #if INSTALL_TOPLEV_HANDLER
1941 // Put top level window event handler into place
1942 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1944 if ( !m_toplevEvtHandler
)
1945 m_toplevEvtHandler
= new wxComboFrameEventHandler(this);
1947 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
1949 ((wxComboFrameEventHandler
*)m_toplevEvtHandler
)->OnPopup();
1950 toplev
->PushEventHandler( m_toplevEvtHandler
);
1954 // Set string selection (must be this way instead of SetStringSelection)
1957 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
1958 m_text
->SelectAll();
1960 m_popupInterface
->SetStringValue( m_text
->GetValue() );
1964 // This is neede since focus/selection indication may change when popup is shown
1968 // This must be after SetStringValue
1969 m_popupWinState
= Animating
;
1971 wxRect
popupWinRect( popupX
, popupY
, szp
.x
, szp
.y
);
1974 if ( (m_iFlags
& wxCC_IFLAG_DISABLE_POPUP_ANIM
) ||
1975 AnimateShow( popupWinRect
, showFlags
) )
1977 DoShowPopup( popupWinRect
, showFlags
);
1981 bool wxComboCtrlBase::AnimateShow( const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
) )
1986 void wxComboCtrlBase::DoShowPopup( const wxRect
& rect
, int WXUNUSED(flags
) )
1988 wxWindow
* winPopup
= m_winPopup
;
1990 if ( IsPopupWindowState(Animating
) )
1992 // Make sure the popup window is shown in the right position.
1993 // Should not matter even if animation already did this.
1995 // Some platforms (GTK) may like SetSize and Move to be separate
1996 // (though the bug was probably fixed).
1997 winPopup
->SetSize( rect
);
2001 m_popupWinState
= Visible
;
2003 else if ( IsPopupWindowState(Hidden
) )
2005 // Animation was aborted
2007 wxASSERT( !winPopup
->IsShown() );
2009 m_popupWinState
= Hidden
;
2013 void wxComboCtrlBase::OnPopupDismiss()
2015 // Just in case, avoid double dismiss
2016 if ( IsPopupWindowState(Hidden
) )
2019 // This must be set before focus - otherwise there will be recursive
2020 // OnPopupDismisses.
2021 m_popupWinState
= Hidden
;
2024 m_winPopup
->Disable();
2026 // Inform popup control itself
2027 m_popupInterface
->OnDismiss();
2029 if ( m_popupExtraHandler
)
2030 ((wxComboPopupExtraEventHandler
*)m_popupExtraHandler
)->OnPopupDismiss();
2032 #if INSTALL_TOPLEV_HANDLER
2033 // Remove top level window event handler
2034 if ( m_toplevEvtHandler
)
2036 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2038 toplev
->RemoveEventHandler( m_toplevEvtHandler
);
2042 m_timeCanAcceptClick
= ::wxGetLocalTimeMillis();
2044 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2045 m_timeCanAcceptClick
+= 150;
2047 // If cursor not on dropdown button, then clear its state
2048 // (technically not required by all ports, but do it for all just in case)
2049 if ( !m_btnArea
.Contains(ScreenToClient(::wxGetMousePosition())) )
2052 // Return parent's tab traversal flag.
2053 // See ShowPopup for notes.
2054 if ( m_iFlags
& wxCC_IFLAG_PARENT_TAB_TRAVERSAL
)
2056 wxWindow
* parent
= GetParent();
2057 parent
->SetWindowStyle( parent
->GetWindowStyle() | wxTAB_TRAVERSAL
);
2058 m_iFlags
&= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL
);
2061 // refresh control (necessary even if m_text)
2067 void wxComboCtrlBase::HidePopup()
2069 // Should be able to call this without popup interface
2070 if ( IsPopupWindowState(Hidden
) )
2073 // transfer value and show it in textctrl, if any
2074 if ( !IsPopupWindowState(Animating
) )
2075 SetValue( m_popupInterface
->GetStringValue() );
2082 // ----------------------------------------------------------------------------
2083 // customization methods
2084 // ----------------------------------------------------------------------------
2086 void wxComboCtrlBase::SetButtonPosition( int width
, int height
,
2087 int side
, int spacingX
)
2092 m_btnSpacingX
= spacingX
;
2097 wxSize
wxComboCtrlBase::GetButtonSize()
2099 if ( m_btnSize
.x
> 0 )
2102 wxSize
retSize(m_btnWid
,m_btnHei
);
2104 // Need to call CalculateAreas now if button size is
2105 // is not explicitly specified.
2106 if ( retSize
.x
<= 0 || retSize
.y
<= 0)
2110 retSize
= m_btnSize
;
2116 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap
& bmpNormal
,
2118 const wxBitmap
& bmpPressed
,
2119 const wxBitmap
& bmpHover
,
2120 const wxBitmap
& bmpDisabled
)
2122 m_bmpNormal
= bmpNormal
;
2123 m_blankButtonBg
= blankButtonBg
;
2125 if ( bmpPressed
.Ok() )
2126 m_bmpPressed
= bmpPressed
;
2128 m_bmpPressed
= bmpNormal
;
2130 if ( bmpHover
.Ok() )
2131 m_bmpHover
= bmpHover
;
2133 m_bmpHover
= bmpNormal
;
2135 if ( bmpDisabled
.Ok() )
2136 m_bmpDisabled
= bmpDisabled
;
2138 m_bmpDisabled
= bmpNormal
;
2143 void wxComboCtrlBase::SetCustomPaintWidth( int width
)
2147 // move textctrl accordingly
2148 wxRect r
= m_text
->GetRect();
2149 int inc
= width
- m_widthCustomPaint
;
2152 m_text
->SetSize( r
);
2155 m_widthCustomPaint
= width
;
2160 void wxComboCtrlBase::SetTextIndent( int indent
)
2164 m_absIndent
= GetNativeTextIndent();
2165 m_iFlags
&= ~(wxCC_IFLAG_INDENT_SET
);
2169 m_absIndent
= indent
;
2170 m_iFlags
|= wxCC_IFLAG_INDENT_SET
;
2176 wxCoord
wxComboCtrlBase::GetNativeTextIndent() const
2178 return DEFAULT_TEXT_INDENT
;
2181 // ----------------------------------------------------------------------------
2182 // methods forwarded to wxTextCtrl
2183 // ----------------------------------------------------------------------------
2185 wxString
wxComboCtrlBase::GetValue() const
2188 return m_text
->GetValue();
2189 return m_valueString
;
2192 void wxComboCtrlBase::SetValueWithEvent(const wxString
& value
, bool withEvent
)
2199 m_text
->SetValue(value
);
2200 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2201 m_text
->SelectAll();
2204 m_valueString
= value
;
2208 // Since wxComboPopup may want to paint the combo as well, we need
2209 // to set the string value here (as well as sometimes in ShowPopup).
2210 if ( m_valueString
!= value
&& m_popupInterface
)
2212 m_popupInterface
->SetStringValue(value
);
2216 void wxComboCtrlBase::SetValue(const wxString
& value
)
2218 SetValueWithEvent(value
, false);
2221 // In this SetValue variant wxComboPopup::SetStringValue is not called
2222 void wxComboCtrlBase::SetText(const wxString
& value
)
2224 // Unlike in SetValue(), this must be called here or
2225 // the behaviour will no be consistent in readonlys.
2226 EnsurePopupControl();
2228 m_valueString
= value
;
2233 m_text
->SetValue( value
);
2239 void wxComboCtrlBase::Copy()
2245 void wxComboCtrlBase::Cut()
2251 void wxComboCtrlBase::Paste()
2257 void wxComboCtrlBase::SetInsertionPoint(long pos
)
2260 m_text
->SetInsertionPoint(pos
);
2263 void wxComboCtrlBase::SetInsertionPointEnd()
2266 m_text
->SetInsertionPointEnd();
2269 long wxComboCtrlBase::GetInsertionPoint() const
2272 return m_text
->GetInsertionPoint();
2277 long wxComboCtrlBase::GetLastPosition() const
2280 return m_text
->GetLastPosition();
2285 void wxComboCtrlBase::Replace(long from
, long to
, const wxString
& value
)
2288 m_text
->Replace(from
, to
, value
);
2291 void wxComboCtrlBase::Remove(long from
, long to
)
2294 m_text
->Remove(from
, to
);
2297 void wxComboCtrlBase::SetSelection(long from
, long to
)
2300 m_text
->SetSelection(from
, to
);
2303 void wxComboCtrlBase::Undo()
2309 #endif // wxUSE_COMBOCTRL