1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/combocmn.cpp
3 // Purpose: wxComboCtrlBase
4 // Author: Jaakko Salli
6 // Created: Apr-30-2006
8 // Copyright: (c) 2005 Jaakko Salli
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
28 #include "wx/combobox.h"
32 #include "wx/dcclient.h"
33 #include "wx/settings.h"
34 #include "wx/dialog.h"
38 #include "wx/tooltip.h"
45 // ----------------------------------------------------------------------------
47 #define DEFAULT_DROPBUTTON_WIDTH 19
49 #define BMP_BUTTON_MARGIN 4
51 #define DEFAULT_POPUP_HEIGHT 400
53 #define DEFAULT_TEXT_INDENT 3
55 #define COMBO_MARGIN 2 // spacing right of wxTextCtrl
58 #if defined(__WXMSW__)
60 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
61 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
62 // native controls work on it like normal.
63 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
64 #define TEXTCTRL_TEXT_CENTERED 0 // 1 if text in textctrl is vertically centered
65 #define FOCUS_RING 0 // No focus ring on wxMSW
67 //#undef wxUSE_POPUPWIN
68 //#define wxUSE_POPUPWIN 0
70 #elif defined(__WXGTK__)
72 // NB: It is not recommended to use wxDialog as popup on wxGTK, because of
73 // this bug: If wxDialog is hidden, its position becomes corrupt
74 // between hide and next show, but without internal coordinates being
75 // reflected (or something like that - atleast commenting out ->Hide()
76 // seemed to eliminate the position change).
78 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
79 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
80 // native controls work on it like normal.
81 #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
82 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
83 #define FOCUS_RING 0 // No focus ring on wxGTK
85 #elif defined(__WXMAC__)
87 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
88 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
89 // native controls work on it like normal.
90 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
91 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
92 #define FOCUS_RING 3 // Reserve room for the textctrl's focus ring to display
94 #undef DEFAULT_DROPBUTTON_WIDTH
95 #define DEFAULT_DROPBUTTON_WIDTH 22
97 #define COMBO_MARGIN FOCUS_RING
101 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
102 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
103 // native controls work on it like normal.
104 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
105 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
111 // Popupwin is really only supported on wxMSW (not WINCE) and wxGTK, regardless
112 // what the wxUSE_POPUPWIN says.
113 // FIXME: Why isn't wxUSE_POPUPWIN reliable any longer? (it was in wxW2.6.2)
114 #if (!defined(__WXMSW__) && !defined(__WXGTK__)) || defined(__WXWINCE__)
115 #undef wxUSE_POPUPWIN
116 #define wxUSE_POPUPWIN 0
121 #include "wx/popupwin.h"
123 #undef USE_TRANSIENT_POPUP
124 #define USE_TRANSIENT_POPUP 0
128 // Define different types of popup windows
132 POPUPWIN_WXPOPUPTRANSIENTWINDOW
= 1,
133 POPUPWIN_WXPOPUPWINDOW
= 2,
134 POPUPWIN_WXDIALOG
= 3
138 #if USE_TRANSIENT_POPUP
139 // wxPopupTransientWindow is implemented
141 #define wxComboPopupWindowBase wxPopupTransientWindow
142 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPTRANSIENTWINDOW
143 #define USES_WXPOPUPTRANSIENTWINDOW 1
145 #if TRANSIENT_POPUPWIN_IS_PERFECT
147 #elif POPUPWIN_IS_PERFECT
148 #define wxComboPopupWindowBase2 wxPopupWindow
149 #define SECONDARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
150 #define USES_WXPOPUPWINDOW 1
152 #define wxComboPopupWindowBase2 wxDialog
153 #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG
154 #define USES_WXDIALOG 1
158 // wxPopupWindow (but not wxPopupTransientWindow) is properly implemented
160 #define wxComboPopupWindowBase wxPopupWindow
161 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
162 #define USES_WXPOPUPWINDOW 1
164 #if !POPUPWIN_IS_PERFECT
165 #define wxComboPopupWindowBase2 wxDialog
166 #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG
167 #define USES_WXDIALOG 1
171 // wxPopupWindow is not implemented
173 #define wxComboPopupWindowBase wxDialog
174 #define PRIMARY_POPUP_TYPE POPUPWIN_WXDIALOG
175 #define USES_WXDIALOG 1
180 #ifndef USES_WXPOPUPTRANSIENTWINDOW
181 #define USES_WXPOPUPTRANSIENTWINDOW 0
184 #ifndef USES_WXPOPUPWINDOW
185 #define USES_WXPOPUPWINDOW 0
188 #ifndef USES_WXDIALOG
189 #define USES_WXDIALOG 0
193 #if USES_WXPOPUPWINDOW
194 #define INSTALL_TOPLEV_HANDLER 1
196 #define INSTALL_TOPLEV_HANDLER 0
202 // * wxComboPopupWindow for external use (ie. replace old wxUniv wxPopupComboWindow)
206 // ----------------------------------------------------------------------------
207 // wxComboFrameEventHandler takes care of hiding the popup when events happen
208 // in its top level parent.
209 // ----------------------------------------------------------------------------
211 #if INSTALL_TOPLEV_HANDLER
214 // This will no longer be necessary after wxTransientPopupWindow
215 // works well on all platforms.
218 class wxComboFrameEventHandler
: public wxEvtHandler
221 wxComboFrameEventHandler( wxComboCtrlBase
* pCb
);
222 virtual ~wxComboFrameEventHandler();
226 void OnIdle( wxIdleEvent
& event
);
227 void OnMouseEvent( wxMouseEvent
& event
);
228 void OnActivate( wxActivateEvent
& event
);
229 void OnResize( wxSizeEvent
& event
);
230 void OnMove( wxMoveEvent
& event
);
231 void OnMenuEvent( wxMenuEvent
& event
);
232 void OnClose( wxCloseEvent
& event
);
235 wxWindow
* m_focusStart
;
236 wxComboCtrlBase
* m_combo
;
239 DECLARE_EVENT_TABLE()
242 BEGIN_EVENT_TABLE(wxComboFrameEventHandler
, wxEvtHandler
)
243 EVT_IDLE(wxComboFrameEventHandler::OnIdle
)
244 EVT_LEFT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
245 EVT_RIGHT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
246 EVT_SIZE(wxComboFrameEventHandler::OnResize
)
247 EVT_MOVE(wxComboFrameEventHandler::OnMove
)
248 EVT_MENU_HIGHLIGHT(wxID_ANY
,wxComboFrameEventHandler::OnMenuEvent
)
249 EVT_MENU_OPEN(wxComboFrameEventHandler::OnMenuEvent
)
250 EVT_ACTIVATE(wxComboFrameEventHandler::OnActivate
)
251 EVT_CLOSE(wxComboFrameEventHandler::OnClose
)
254 wxComboFrameEventHandler::wxComboFrameEventHandler( wxComboCtrlBase
* combo
)
260 wxComboFrameEventHandler::~wxComboFrameEventHandler()
264 void wxComboFrameEventHandler::OnPopup()
266 m_focusStart
= ::wxWindow::FindFocus();
269 void wxComboFrameEventHandler::OnIdle( wxIdleEvent
& event
)
271 wxWindow
* winFocused
= ::wxWindow::FindFocus();
273 wxWindow
* popup
= m_combo
->GetPopupControl()->GetControl();
274 wxWindow
* winpopup
= m_combo
->GetPopupWindow();
277 winFocused
!= m_focusStart
&&
278 winFocused
!= popup
&&
279 winFocused
->GetParent() != popup
&&
280 winFocused
!= winpopup
&&
281 winFocused
->GetParent() != winpopup
&&
282 winFocused
!= m_combo
&&
283 winFocused
!= m_combo
->GetButton() // GTK (atleast) requires this
286 m_combo
->HidePopup();
292 void wxComboFrameEventHandler::OnMenuEvent( wxMenuEvent
& event
)
294 m_combo
->HidePopup();
298 void wxComboFrameEventHandler::OnMouseEvent( wxMouseEvent
& event
)
300 m_combo
->HidePopup();
304 void wxComboFrameEventHandler::OnClose( wxCloseEvent
& event
)
306 m_combo
->HidePopup();
310 void wxComboFrameEventHandler::OnActivate( wxActivateEvent
& event
)
312 m_combo
->HidePopup();
316 void wxComboFrameEventHandler::OnResize( wxSizeEvent
& event
)
318 m_combo
->HidePopup();
322 void wxComboFrameEventHandler::OnMove( wxMoveEvent
& event
)
324 m_combo
->HidePopup();
328 #endif // INSTALL_TOPLEV_HANDLER
330 // ----------------------------------------------------------------------------
331 // wxComboPopupWindow is, in essence, wxPopupWindow customized for
333 // ----------------------------------------------------------------------------
335 class wxComboPopupWindow
: public wxComboPopupWindowBase
339 wxComboPopupWindow( wxComboCtrlBase
*parent
,
341 #if USES_WXPOPUPWINDOW || USES_WXPOPUPTRANSIENTWINDOW
342 : wxComboPopupWindowBase(parent
,style
)
344 : wxComboPopupWindowBase(parent
,
355 #if USES_WXPOPUPTRANSIENTWINDOW
356 virtual bool Show( bool show
);
357 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
359 virtual void OnDismiss();
367 #if USES_WXPOPUPTRANSIENTWINDOW
368 bool wxComboPopupWindow::Show( bool show
)
370 // Guard against recursion
372 return wxComboPopupWindowBase::Show(show
);
376 wxASSERT( IsKindOf(CLASSINFO(wxPopupTransientWindow
)) );
378 wxPopupTransientWindow
* ptw
= (wxPopupTransientWindow
*) this;
379 wxComboCtrlBase
* combo
= (wxComboCtrlBase
*) GetParent();
381 if ( show
!= ptw
->IsShown() )
384 ptw
->Popup(combo
->GetPopupControl()->GetControl());
394 bool wxComboPopupWindow::ProcessLeftDown(wxMouseEvent
& event
)
396 return wxPopupTransientWindow::ProcessLeftDown(event
);
399 // First thing that happens when a transient popup closes is that this method gets called.
400 void wxComboPopupWindow::OnDismiss()
402 wxComboCtrlBase
* combo
= (wxComboCtrlBase
*) GetParent();
403 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxComboCtrlBase
)),
404 wxT("parent might not be wxComboCtrl, but check IMPLEMENT_DYNAMIC_CLASS(2) macro for correctness") );
406 combo
->OnPopupDismiss();
408 #endif // USES_WXPOPUPTRANSIENTWINDOW
411 // ----------------------------------------------------------------------------
412 // wxComboPopupWindowEvtHandler does bulk of the custom event handling
413 // of a popup window. It is separate so we can have different types
415 // ----------------------------------------------------------------------------
417 class wxComboPopupWindowEvtHandler
: public wxEvtHandler
421 wxComboPopupWindowEvtHandler( wxComboCtrlBase
*parent
)
426 void OnSizeEvent( wxSizeEvent
& event
);
427 void OnKeyEvent(wxKeyEvent
& event
);
429 void OnActivate( wxActivateEvent
& event
);
433 wxComboCtrlBase
* m_combo
;
435 DECLARE_EVENT_TABLE()
439 BEGIN_EVENT_TABLE(wxComboPopupWindowEvtHandler
, wxEvtHandler
)
440 EVT_KEY_DOWN(wxComboPopupWindowEvtHandler::OnKeyEvent
)
441 EVT_KEY_UP(wxComboPopupWindowEvtHandler::OnKeyEvent
)
443 EVT_ACTIVATE(wxComboPopupWindowEvtHandler::OnActivate
)
445 EVT_SIZE(wxComboPopupWindowEvtHandler::OnSizeEvent
)
449 void wxComboPopupWindowEvtHandler::OnSizeEvent( wxSizeEvent
& WXUNUSED(event
) )
451 // Block the event so that the popup control does not get auto-resized.
454 void wxComboPopupWindowEvtHandler::OnKeyEvent( wxKeyEvent
& event
)
456 // Relay keyboard event to the main child controls
457 wxWindowList children
= m_combo
->GetPopupWindow()->GetChildren();
458 wxWindowList::iterator node
= children
.begin();
459 wxWindow
* child
= (wxWindow
*)*node
;
460 child
->AddPendingEvent(event
);
464 void wxComboPopupWindowEvtHandler::OnActivate( wxActivateEvent
& event
)
466 if ( !event
.GetActive() )
468 // Tell combo control that we are dismissed.
469 m_combo
->HidePopup();
477 // ----------------------------------------------------------------------------
480 // ----------------------------------------------------------------------------
482 wxComboPopup::~wxComboPopup()
486 void wxComboPopup::OnPopup()
490 void wxComboPopup::OnDismiss()
494 wxSize
wxComboPopup::GetAdjustedSize( int minWidth
,
496 int WXUNUSED(maxHeight
) )
498 return wxSize(minWidth
,prefHeight
);
501 void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase
* combo
,
502 wxDC
& dc
, const wxRect
& rect
)
504 if ( combo
->GetWindowStyle() & wxCB_READONLY
) // ie. no textctrl
506 combo
->PrepareBackground(dc
,rect
,0);
508 dc
.DrawText( combo
->GetValue(),
509 rect
.x
+ combo
->GetTextIndent(),
510 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
514 void wxComboPopup::PaintComboControl( wxDC
& dc
, const wxRect
& rect
)
516 DefaultPaintComboControl(m_combo
,dc
,rect
);
519 void wxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
524 void wxComboPopup::OnComboDoubleClick()
528 void wxComboPopup::SetStringValue( const wxString
& WXUNUSED(value
) )
532 bool wxComboPopup::LazyCreate()
537 void wxComboPopup::Dismiss()
539 m_combo
->HidePopup();
542 // ----------------------------------------------------------------------------
544 // ----------------------------------------------------------------------------
547 // This is pushed to the event handler queue of the child textctrl.
549 class wxComboBoxExtraInputHandler
: public wxEvtHandler
553 wxComboBoxExtraInputHandler( wxComboCtrlBase
* combo
)
558 virtual ~wxComboBoxExtraInputHandler() { }
559 void OnKey(wxKeyEvent
& event
);
560 void OnFocus(wxFocusEvent
& event
);
563 wxComboCtrlBase
* m_combo
;
566 DECLARE_EVENT_TABLE()
570 BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler
, wxEvtHandler
)
571 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey
)
572 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
576 void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent
& event
)
578 // Let the wxComboCtrl event handler have a go first.
579 wxComboCtrlBase
* combo
= m_combo
;
580 wxObject
* prevObj
= event
.GetEventObject();
582 event
.SetId(combo
->GetId());
583 event
.SetEventObject(combo
);
584 combo
->GetEventHandler()->ProcessEvent(event
);
586 event
.SetId(((wxWindow
*)prevObj
)->GetId());
587 event
.SetEventObject(prevObj
);
590 void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent
& event
)
592 // FIXME: This code does run when control is clicked,
593 // yet on Windows it doesn't select all the text.
594 if ( !(m_combo
->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT
) )
596 if ( m_combo
->GetTextCtrl() )
597 m_combo
->GetTextCtrl()->SelectAll();
599 m_combo
->SetSelection(-1,-1);
602 // Send focus indication to parent.
603 // NB: This is needed for cases where the textctrl gets focus
604 // instead of its parent. While this may trigger multiple
605 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
606 // from combo's focus event handler), they should be quite
608 wxFocusEvent
evt2(wxEVT_SET_FOCUS
,m_combo
->GetId());
609 evt2
.SetEventObject(m_combo
);
610 m_combo
->GetEventHandler()->ProcessEvent(evt2
);
617 // This is pushed to the event handler queue of the control in popup.
620 class wxComboPopupExtraEventHandler
: public wxEvtHandler
624 wxComboPopupExtraEventHandler( wxComboCtrlBase
* combo
)
628 m_beenInside
= false;
630 virtual ~wxComboPopupExtraEventHandler() { }
632 void OnMouseEvent( wxMouseEvent
& event
);
634 // Called from wxComboCtrlBase::OnPopupDismiss
635 void OnPopupDismiss()
637 m_beenInside
= false;
641 wxComboCtrlBase
* m_combo
;
646 DECLARE_EVENT_TABLE()
650 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler
, wxEvtHandler
)
651 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent
)
655 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent
& event
)
657 wxPoint pt
= event
.GetPosition();
658 wxSize sz
= m_combo
->GetPopupControl()->GetControl()->GetClientSize();
659 int evtType
= event
.GetEventType();
660 bool isInside
= pt
.x
>= 0 && pt
.y
>= 0 && pt
.x
< sz
.x
&& pt
.y
< sz
.y
;
662 if ( evtType
== wxEVT_MOTION
||
663 evtType
== wxEVT_LEFT_DOWN
||
664 evtType
== wxEVT_RIGHT_DOWN
)
666 // Block motion and click events outside the popup
667 if ( !isInside
|| !m_combo
->IsPopupShown() )
673 else if ( evtType
== wxEVT_LEFT_UP
)
675 if ( !m_combo
->IsPopupShown() )
690 // Some mouse events to popup that happen outside it, before cursor
691 // has been inside the popu, need to be ignored by it but relayed to
694 wxWindow
* btn
= m_combo
->GetButton();
696 btn
->GetEventHandler()->AddPendingEvent(event
);
698 m_combo
->GetEventHandler()->AddPendingEvent(event
);
710 // ----------------------------------------------------------------------------
712 // ----------------------------------------------------------------------------
715 BEGIN_EVENT_TABLE(wxComboCtrlBase
, wxControl
)
716 EVT_TEXT(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
717 EVT_SIZE(wxComboCtrlBase::OnSizeEvent
)
718 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent
)
719 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent
)
720 EVT_IDLE(wxComboCtrlBase::OnIdleEvent
)
721 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
722 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent
)
723 EVT_TEXT_ENTER(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
724 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged
)
728 IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase
, wxControl
)
730 void wxComboCtrlBase::Init()
732 m_winPopup
= (wxWindow
*)NULL
;
733 m_popup
= (wxWindow
*)NULL
;
734 m_popupWinState
= Hidden
;
735 m_btn
= (wxWindow
*) NULL
;
736 m_text
= (wxTextCtrl
*) NULL
;
737 m_popupInterface
= (wxComboPopup
*) NULL
;
739 m_popupExtraHandler
= (wxEvtHandler
*) NULL
;
740 m_textEvtHandler
= (wxEvtHandler
*) NULL
;
742 #if INSTALL_TOPLEV_HANDLER
743 m_toplevEvtHandler
= (wxEvtHandler
*) NULL
;
746 m_mainCtrlWnd
= this;
749 m_widthMinPopup
= -1;
751 m_widthCustomPaint
= 0;
752 m_widthCustomBorder
= 0;
756 m_blankButtonBg
= false;
758 m_popupWinType
= POPUPWIN_NONE
;
759 m_btnWid
= m_btnHei
= -1;
767 m_timeCanAcceptClick
= 0;
769 m_resetFocus
= false;
772 bool wxComboCtrlBase::Create(wxWindow
*parent
,
774 const wxString
& value
,
778 const wxValidator
& validator
,
779 const wxString
& name
)
781 if ( !wxControl::Create(parent
,
785 style
| wxWANTS_CHARS
,
790 m_valueString
= value
;
794 m_absIndent
= GetNativeTextIndent();
796 m_iFlags
|= wxCC_IFLAG_CREATED
;
798 // If x and y indicate valid size, wxSizeEvent won't be
799 // emitted automatically, so we need to add artifical one.
800 if ( size
.x
> 0 && size
.y
> 0 )
802 wxSizeEvent
evt(size
,GetId());
803 GetEventHandler()->AddPendingEvent(evt
);
809 void wxComboCtrlBase::InstallInputHandlers()
813 m_textEvtHandler
= new wxComboBoxExtraInputHandler(this);
814 m_text
->PushEventHandler(m_textEvtHandler
);
819 wxComboCtrlBase::CreateTextCtrl(int style
, const wxValidator
& validator
)
821 if ( !(m_windowStyle
& wxCB_READONLY
) )
826 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
827 // not used by the wxPropertyGrid and therefore the tab is processed by
828 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
829 // navigation event is then sent to the wrong window.
830 style
|= wxTE_PROCESS_TAB
;
832 if ( HasFlag(wxTE_PROCESS_ENTER
) )
833 style
|= wxTE_PROCESS_ENTER
;
835 // Ignore EVT_TEXT generated by the constructor (but only
836 // if the event redirector already exists)
837 // NB: This must be " = 1" instead of "++";
838 if ( m_textEvtHandler
)
843 m_text
= new wxTextCtrl(this, wxID_ANY
, m_valueString
,
844 wxDefaultPosition
, wxSize(10,-1),
849 void wxComboCtrlBase::OnThemeChange()
851 // Leave the default bg on the Mac so the area used by the focus ring will
852 // be the correct colour and themed brush. Instead we'll use
853 // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed.
855 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
859 wxComboCtrlBase::~wxComboCtrlBase()
864 #if INSTALL_TOPLEV_HANDLER
865 delete ((wxComboFrameEventHandler
*)m_toplevEvtHandler
);
866 m_toplevEvtHandler
= (wxEvtHandler
*) NULL
;
872 m_text
->RemoveEventHandler(m_textEvtHandler
);
874 delete m_textEvtHandler
;
878 // ----------------------------------------------------------------------------
880 // ----------------------------------------------------------------------------
882 // Recalculates button and textctrl areas
883 void wxComboCtrlBase::CalculateAreas( int btnWidth
)
885 wxSize sz
= GetClientSize();
886 int customBorder
= m_widthCustomBorder
;
887 int btnBorder
; // border for button only
889 // check if button should really be outside the border: we'll do it it if
890 // its platform default or bitmap+pushbutton background is used, but not if
891 // there is vertical size adjustment or horizontal spacing.
892 if ( ( (m_iFlags
& wxCC_BUTTON_OUTSIDE_BORDER
) ||
893 (m_bmpNormal
.Ok() && m_blankButtonBg
) ) &&
894 m_btnSpacingX
== 0 &&
897 m_iFlags
|= wxCC_IFLAG_BUTTON_OUTSIDE
;
902 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
903 btnBorder
= customBorder
;
906 // Defaul indentation
907 if ( m_absIndent
< 0 )
908 m_absIndent
= GetNativeTextIndent();
910 int butWidth
= btnWidth
;
913 butWidth
= m_btnWidDefault
;
915 m_btnWidDefault
= butWidth
;
920 int butHeight
= sz
.y
- btnBorder
*2;
922 // Adjust button width
927 // Adjust button width to match aspect ratio
928 // (but only if control is smaller than best size).
929 int bestHeight
= GetBestSize().y
;
930 int height
= GetSize().y
;
932 if ( height
< bestHeight
)
934 // Make very small buttons square, as it makes
935 // them accommodate arrow image better and still
938 butWidth
= (height
*butWidth
)/bestHeight
;
940 butWidth
= butHeight
;
944 // Adjust button height
946 butHeight
= m_btnHei
;
948 // Use size of normal bitmap if...
951 // button width is set to default and blank button bg is not drawn
952 if ( m_bmpNormal
.Ok() )
954 int bmpReqWidth
= m_bmpNormal
.GetWidth();
955 int bmpReqHeight
= m_bmpNormal
.GetHeight();
957 // If drawing blank button background, we need to add some margin.
958 if ( m_blankButtonBg
)
960 bmpReqWidth
+= BMP_BUTTON_MARGIN
*2;
961 bmpReqHeight
+= BMP_BUTTON_MARGIN
*2;
964 if ( butWidth
< bmpReqWidth
|| ( m_btnWid
== 0 && !m_blankButtonBg
) )
965 butWidth
= bmpReqWidth
;
966 if ( butHeight
< bmpReqHeight
|| ( m_btnHei
== 0 && !m_blankButtonBg
) )
967 butHeight
= bmpReqHeight
;
969 // Need to fix height?
970 if ( (sz
.y
-(customBorder
*2)) < butHeight
&& btnWidth
== 0 )
972 int newY
= butHeight
+(customBorder
*2);
973 SetClientSize(wxDefaultCoord
,newY
);
978 int butAreaWid
= butWidth
+ (m_btnSpacingX
*2);
980 m_btnSize
.x
= butWidth
;
981 m_btnSize
.y
= butHeight
;
983 m_btnArea
.x
= ( m_btnSide
==wxRIGHT
? sz
.x
- butAreaWid
- btnBorder
: btnBorder
);
984 m_btnArea
.y
= btnBorder
+ FOCUS_RING
;
985 m_btnArea
.width
= butAreaWid
;
986 m_btnArea
.height
= sz
.y
- ((btnBorder
+FOCUS_RING
)*2);
988 m_tcArea
.x
= ( m_btnSide
==wxRIGHT
? 0 : butAreaWid
) + customBorder
+ FOCUS_RING
;
989 m_tcArea
.y
= customBorder
+ FOCUS_RING
;
990 m_tcArea
.width
= sz
.x
- butAreaWid
- (customBorder
*2) - (FOCUS_RING
*2);
991 m_tcArea
.height
= sz
.y
- ((customBorder
+FOCUS_RING
)*2);
996 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
997 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1002 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
)
1007 #if !TEXTCTRL_TEXT_CENTERED
1009 wxSize sz
= GetClientSize();
1011 int customBorder
= m_widthCustomBorder
;
1012 if ( (m_text
->GetWindowStyleFlag() & wxBORDER_MASK
) == wxNO_BORDER
)
1015 int tcSizeY
= m_text
->GetBestSize().y
;
1016 int diff
= sz
.y
- tcSizeY
;
1017 int y
= textCtrlYAdjust
+ (diff
/2);
1019 if ( y
< customBorder
)
1022 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
+ m_absIndent
+ textCtrlXAdjust
,
1024 m_tcArea
.width
- COMBO_MARGIN
-
1025 (textCtrlXAdjust
+ m_widthCustomPaint
+ m_absIndent
),
1028 // Make sure textctrl doesn't exceed the bottom custom border
1029 wxSize tsz
= m_text
->GetSize();
1030 diff
= (y
+ tsz
.y
) - (sz
.y
- customBorder
);
1033 tsz
.y
= tsz
.y
- diff
- 1;
1034 m_text
->SetSize(tsz
);
1038 #else // TEXTCTRL_TEXT_CENTERED
1039 wxUnusedVar(textCtrlXAdjust
);
1040 wxUnusedVar(textCtrlYAdjust
);
1041 #endif // !TEXTCTRL_TEXT_CENTERED/TEXTCTRL_TEXT_CENTERED
1043 // If it has border, have textctrl will the entire text field.
1044 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
,
1046 m_tcArea
.width
- m_widthCustomPaint
,
1051 wxSize
wxComboCtrlBase::DoGetBestSize() const
1053 wxSize
sizeText(150,0);
1056 sizeText
= m_text
->GetBestSize();
1058 // TODO: Better method to calculate close-to-native control height.
1062 fhei
= (m_font
.GetPointSize()*2) + 5;
1063 else if ( wxNORMAL_FONT
->Ok() )
1064 fhei
= (wxNORMAL_FONT
->GetPointSize()*2) + 5;
1066 fhei
= sizeText
.y
+ 4;
1068 // Need to force height to accomodate bitmap?
1069 int btnSizeY
= m_btnSize
.y
;
1070 if ( m_bmpNormal
.Ok() && fhei
< btnSizeY
)
1073 // Control height doesn't depend on border
1076 int border = m_windowStyle & wxBORDER_MASK;
1077 if ( border == wxSIMPLE_BORDER )
1079 else if ( border == wxNO_BORDER )
1080 fhei += (m_widthCustomBorder*2);
1086 // Final adjustments
1092 // these are the numbers from the HIG:
1093 switch ( m_windowVariant
)
1095 case wxWINDOW_VARIANT_NORMAL
:
1099 case wxWINDOW_VARIANT_SMALL
:
1102 case wxWINDOW_VARIANT_MINI
:
1108 fhei
+= 2 * FOCUS_RING
;
1109 int width
= sizeText
.x
+ FOCUS_RING
+ COMBO_MARGIN
+ DEFAULT_DROPBUTTON_WIDTH
;
1111 wxSize
ret(width
, fhei
);
1116 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent
& event
)
1121 // defined by actual wxComboCtrls
1127 // ----------------------------------------------------------------------------
1128 // standard operations
1129 // ----------------------------------------------------------------------------
1131 bool wxComboCtrlBase::Enable(bool enable
)
1133 if ( !wxControl::Enable(enable
) )
1137 m_btn
->Enable(enable
);
1139 m_text
->Enable(enable
);
1144 bool wxComboCtrlBase::Show(bool show
)
1146 if ( !wxControl::Show(show
) )
1158 bool wxComboCtrlBase::SetFont ( const wxFont
& font
)
1160 if ( !wxControl::SetFont(font
) )
1164 m_text
->SetFont(font
);
1170 void wxComboCtrlBase::DoSetToolTip(wxToolTip
*tooltip
)
1172 wxControl::DoSetToolTip(tooltip
);
1174 // Set tool tip for button and text box
1177 const wxString
&tip
= tooltip
->GetTip();
1178 if ( m_text
) m_text
->SetToolTip(tip
);
1179 if ( m_btn
) m_btn
->SetToolTip(tip
);
1183 if ( m_text
) m_text
->SetToolTip( (wxToolTip
*) NULL
);
1184 if ( m_btn
) m_btn
->SetToolTip( (wxToolTip
*) NULL
);
1187 #endif // wxUSE_TOOLTIPS
1189 #if wxUSE_VALIDATORS
1190 void wxComboCtrlBase::SetValidator(const wxValidator
& validator
)
1192 wxTextCtrl
* textCtrl
= GetTextCtrl();
1195 textCtrl
->SetValidator( validator
);
1198 wxValidator
* wxComboCtrlBase::GetValidator()
1200 wxTextCtrl
* textCtrl
= GetTextCtrl();
1203 return textCtrl
->GetValidator();
1205 return wxControl::GetValidator();
1207 #endif // wxUSE_VALIDATORS
1209 // ----------------------------------------------------------------------------
1211 // ----------------------------------------------------------------------------
1213 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1214 // prepare combo box background on area in a way typical on platform
1215 void wxComboCtrlBase::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
1217 wxSize sz
= GetClientSize();
1219 bool isFocused
; // also selected
1221 // For smaller size control (and for disabled background) use less spacing
1225 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1228 isEnabled
= IsEnabled();
1229 isFocused
= ShouldDrawFocus();
1231 // Windows-style: for smaller size control (and for disabled background) use less spacing
1232 focusSpacingX
= isEnabled
? 2 : 1;
1233 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
1237 // Drawing a list item
1238 isEnabled
= true; // they are never disabled
1239 isFocused
= flags
& wxCONTROL_SELECTED
? true : false;
1245 // Set the background sub-rectangle for selection, disabled etc
1246 wxRect
selRect(rect
);
1247 selRect
.y
+= focusSpacingY
;
1248 selRect
.height
-= (focusSpacingY
*2);
1252 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1253 wcp
+= m_widthCustomPaint
;
1255 selRect
.x
+= wcp
+ focusSpacingX
;
1256 selRect
.width
-= wcp
+ (focusSpacingX
*2);
1262 // If popup is hidden and this control is focused,
1263 // then draw the focus-indicator (selbgcolor background etc.).
1266 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1267 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1271 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
1272 #ifndef __WXMAC__ // see note in OnThemeChange
1273 bgCol
= GetBackgroundColour();
1275 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1281 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
1282 #ifndef __WXMAC__ // see note in OnThemeChange
1283 bgCol
= GetBackgroundColour();
1285 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1289 dc
.SetBrush( bgCol
);
1291 dc
.DrawRectangle( selRect
);
1293 // Don't clip exactly to the selection rectangle so we can draw
1294 // to the non-selected area in front of it.
1295 wxRect
clipRect(rect
.x
,rect
.y
,
1296 (selRect
.x
+selRect
.width
)-rect
.x
,rect
.height
);
1297 dc
.SetClippingRegion(clipRect
);
1300 // Save the library size a bit for platforms that re-implement this.
1301 void wxComboCtrlBase::PrepareBackground( wxDC
&, const wxRect
&, int ) const
1306 void wxComboCtrlBase::DrawButton( wxDC
& dc
, const wxRect
& rect
, int paintBg
)
1308 int drawState
= m_btnState
;
1311 if ( GetPopupWindowState() >= Animating
)
1312 drawState
|= wxCONTROL_PRESSED
;
1315 wxRect
drawRect(rect
.x
+m_btnSpacingX
,
1316 rect
.y
+((rect
.height
-m_btnSize
.y
)/2),
1320 // Make sure area is not larger than the control
1321 if ( drawRect
.y
< rect
.y
)
1322 drawRect
.y
= rect
.y
;
1323 if ( drawRect
.height
> rect
.height
)
1324 drawRect
.height
= rect
.height
;
1326 bool enabled
= IsEnabled();
1329 drawState
|= wxCONTROL_DISABLED
;
1331 if ( !m_bmpNormal
.Ok() )
1333 // Need to clear button background even if m_btn is present
1338 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
1339 bgCol
= GetParent()->GetBackgroundColour();
1341 bgCol
= GetBackgroundColour();
1345 dc
.DrawRectangle(rect
);
1348 // Draw standard button
1349 wxRendererNative::Get().DrawComboBoxDropButton(this,
1361 pBmp
= &m_bmpDisabled
;
1362 else if ( m_btnState
& wxCONTROL_PRESSED
)
1363 pBmp
= &m_bmpPressed
;
1364 else if ( m_btnState
& wxCONTROL_CURRENT
)
1367 pBmp
= &m_bmpNormal
;
1369 if ( m_blankButtonBg
)
1371 // If using blank button background, we need to clear its background
1372 // with button face colour instead of colour for rest of the control.
1375 wxColour bgCol
= GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1376 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1379 dc
.DrawRectangle(rect
);
1382 wxRendererNative::Get().DrawPushButton(this,
1391 // Need to clear button background even if m_btn is present
1392 // (assume non-button background was cleared just before this call so brushes are good)
1394 dc
.DrawRectangle(rect
);
1397 // Draw bitmap centered in drawRect
1398 dc
.DrawBitmap(*pBmp
,
1399 drawRect
.x
+ (drawRect
.width
-pBmp
->GetWidth())/2,
1400 drawRect
.y
+ (drawRect
.height
-pBmp
->GetHeight())/2,
1405 void wxComboCtrlBase::RecalcAndRefresh()
1409 wxSizeEvent
evt(GetSize(),GetId());
1410 GetEventHandler()->ProcessEvent(evt
);
1415 // ----------------------------------------------------------------------------
1416 // miscellaneous event handlers
1417 // ----------------------------------------------------------------------------
1419 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent
& event
)
1421 if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1423 if ( m_ignoreEvtText
> 0 )
1430 // Change event id, object and string before relaying it forward
1431 event
.SetId(GetId());
1432 wxString s
= event
.GetString();
1433 event
.SetEventObject(this);
1438 // call if cursor is on button area or mouse is captured for the button
1439 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent
& event
,
1442 int type
= event
.GetEventType();
1444 if ( type
== wxEVT_MOTION
)
1446 if ( flags
& wxCC_MF_ON_BUTTON
)
1448 if ( !(m_btnState
& wxCONTROL_CURRENT
) )
1450 // Mouse hover begins
1451 m_btnState
|= wxCONTROL_CURRENT
;
1452 if ( HasCapture() ) // Retain pressed state.
1453 m_btnState
|= wxCONTROL_PRESSED
;
1457 else if ( (m_btnState
& wxCONTROL_CURRENT
) )
1460 m_btnState
&= ~(wxCONTROL_CURRENT
|wxCONTROL_PRESSED
);
1464 else if ( type
== wxEVT_LEFT_DOWN
|| type
== wxEVT_LEFT_DCLICK
)
1466 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1468 m_btnState
|= wxCONTROL_PRESSED
;
1471 if ( !(m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
) )
1474 // If showing popup now, do not capture mouse or there will be interference
1478 else if ( type
== wxEVT_LEFT_UP
)
1481 // Only accept event if mouse was left-press was previously accepted
1485 if ( m_btnState
& wxCONTROL_PRESSED
)
1487 // If mouse was inside, fire the click event.
1488 if ( m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
)
1490 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1494 m_btnState
&= ~(wxCONTROL_PRESSED
);
1498 else if ( type
== wxEVT_LEAVE_WINDOW
)
1500 if ( m_btnState
& (wxCONTROL_CURRENT
|wxCONTROL_PRESSED
) )
1502 m_btnState
&= ~(wxCONTROL_CURRENT
);
1505 if ( IsPopupWindowState(Hidden
) )
1507 m_btnState
&= ~(wxCONTROL_PRESSED
);
1518 // returns true if event was consumed or filtered
1519 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent
& event
,
1520 int WXUNUSED(flags
) )
1522 wxLongLong t
= ::wxGetLocalTimeMillis();
1523 int evtType
= event
.GetEventType();
1525 #if USES_WXPOPUPWINDOW || USES_WXDIALOG
1526 if ( m_popupWinType
!= POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1528 if ( IsPopupWindowState(Visible
) &&
1529 ( evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_RIGHT_DOWN
) )
1537 // Filter out clicks on button immediately after popup dismiss (Windows like behaviour)
1538 if ( evtType
== wxEVT_LEFT_DOWN
&& t
< m_timeCanAcceptClick
)
1540 event
.SetEventType(0);
1547 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent
& event
)
1549 int evtType
= event
.GetEventType();
1551 if ( (evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_LEFT_DCLICK
) &&
1552 (m_windowStyle
& wxCB_READONLY
) )
1554 if ( GetPopupWindowState() >= Animating
)
1556 #if USES_WXPOPUPWINDOW
1557 // Click here always hides the popup.
1558 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1564 if ( !(m_windowStyle
& wxCC_SPECIAL_DCLICK
) )
1566 // In read-only mode, clicking the text is the
1567 // same as clicking the button.
1570 else if ( /*evtType == wxEVT_LEFT_UP || */evtType
== wxEVT_LEFT_DCLICK
)
1572 //if ( m_popupInterface->CycleValue() )
1574 if ( m_popupInterface
)
1575 m_popupInterface
->OnComboDoubleClick();
1580 if ( IsPopupShown() )
1582 // relay (some) mouse events to the popup
1583 if ( evtType
== wxEVT_MOUSEWHEEL
)
1584 m_popup
->AddPendingEvent(event
);
1590 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent
& event
)
1592 if ( IsPopupShown() )
1594 // pass it to the popped up control
1595 GetPopupControl()->GetControl()->AddPendingEvent(event
);
1599 int keycode
= event
.GetKeyCode();
1601 if ( keycode
== WXK_TAB
)
1603 wxNavigationKeyEvent evt
;
1605 wxWindow
* mainCtrl
= GetMainWindowOfCompositeControl();
1607 evt
.SetFlags(wxNavigationKeyEvent::FromTab
|
1608 (!event
.ShiftDown() ? wxNavigationKeyEvent::IsForward
1609 : wxNavigationKeyEvent::IsBackward
));
1610 evt
.SetEventObject(mainCtrl
);
1611 evt
.SetCurrentFocus(mainCtrl
);
1612 mainCtrl
->GetParent()->GetEventHandler()->AddPendingEvent(evt
);
1616 if ( IsKeyPopupToggle(event
) )
1622 int comboStyle
= GetWindowStyle();
1623 wxComboPopup
* popupInterface
= GetPopupControl();
1625 if ( !popupInterface
)
1631 if ( (comboStyle
& wxCB_READONLY
) ||
1632 (keycode
!= WXK_RIGHT
&& keycode
!= WXK_LEFT
) )
1634 popupInterface
->OnComboKeyEvent(event
);
1641 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent
& event
)
1643 if ( event
.GetEventType() == wxEVT_SET_FOCUS
)
1645 wxWindow
* tc
= GetTextCtrl();
1646 if ( tc
&& tc
!= DoFindFocus() )
1648 m_resetFocus
= true;
1657 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent
& WXUNUSED(event
) )
1661 m_resetFocus
= false;
1662 wxWindow
* tc
= GetTextCtrl();
1668 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
1671 // indentation may also have changed
1672 if ( !(m_iFlags
& wxCC_IFLAG_INDENT_SET
) )
1673 m_absIndent
= GetNativeTextIndent();
1677 // ----------------------------------------------------------------------------
1679 // ----------------------------------------------------------------------------
1681 // Create popup window and the child control
1682 void wxComboCtrlBase::CreatePopup()
1684 wxComboPopup
* popupInterface
= m_popupInterface
;
1689 #ifdef wxComboPopupWindowBase2
1690 if ( m_iFlags
& wxCC_IFLAG_USE_ALT_POPUP
)
1693 m_winPopup
= new wxComboPopupWindowBase2( this, wxNO_BORDER
);
1695 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
, wxEmptyString
,
1696 wxPoint(-21,-21), wxSize(20, 20),
1699 m_popupWinType
= SECONDARY_POPUP_TYPE
;
1704 m_winPopup
= new wxComboPopupWindow( this, wxNO_BORDER
);
1705 m_popupWinType
= PRIMARY_POPUP_TYPE
;
1707 m_popupWinEvtHandler
= new wxComboPopupWindowEvtHandler(this);
1708 m_winPopup
->PushEventHandler(m_popupWinEvtHandler
);
1711 popupInterface
->Create(m_winPopup
);
1712 m_popup
= popup
= popupInterface
->GetControl();
1714 m_popupExtraHandler
= new wxComboPopupExtraEventHandler(this);
1715 popup
->PushEventHandler( m_popupExtraHandler
);
1717 // This may be helpful on some platforms
1718 // (eg. it bypasses a wxGTK popupwindow bug where
1719 // window is not initially hidden when it should be)
1722 popupInterface
->m_iFlags
|= wxCP_IFLAG_CREATED
;
1725 // Destroy popup window and the child control
1726 void wxComboCtrlBase::DestroyPopup()
1731 m_popup
->RemoveEventHandler(m_popupExtraHandler
);
1733 delete m_popupExtraHandler
;
1735 delete m_popupInterface
;
1739 m_winPopup
->RemoveEventHandler(m_popupWinEvtHandler
);
1740 delete m_popupWinEvtHandler
;
1741 m_popupWinEvtHandler
= NULL
;
1742 m_winPopup
->Destroy();
1745 m_popupExtraHandler
= (wxEvtHandler
*) NULL
;
1746 m_popupInterface
= (wxComboPopup
*) NULL
;
1747 m_winPopup
= (wxWindow
*) NULL
;
1748 m_popup
= (wxWindow
*) NULL
;
1751 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup
* iface
)
1753 wxCHECK_RET( iface
, wxT("no popup interface set for wxComboCtrl") );
1757 iface
->InitBase(this);
1760 m_popupInterface
= iface
;
1762 if ( !iface
->LazyCreate() )
1768 m_popup
= (wxWindow
*) NULL
;
1771 // This must be done after creation
1772 if ( m_valueString
.length() )
1774 iface
->SetStringValue(m_valueString
);
1779 // Ensures there is atleast the default popup
1780 void wxComboCtrlBase::EnsurePopupControl()
1782 if ( !m_popupInterface
)
1783 SetPopupControl(NULL
);
1786 void wxComboCtrlBase::OnButtonClick()
1788 // Derived classes can override this method for totally custom
1793 void wxComboCtrlBase::ShowPopup()
1795 EnsurePopupControl();
1796 wxCHECK_RET( !IsPopupWindowState(Visible
), wxT("popup window already shown") );
1798 if ( IsPopupWindowState(Animating
) )
1803 // Space above and below
1809 wxSize ctrlSz
= GetSize();
1811 screenHeight
= wxSystemSettings::GetMetric( wxSYS_SCREEN_Y
);
1812 scrPos
= GetParent()->ClientToScreen(GetPosition());
1814 spaceAbove
= scrPos
.y
;
1815 spaceBelow
= screenHeight
- spaceAbove
- ctrlSz
.y
;
1817 maxHeightPopup
= spaceBelow
;
1818 if ( spaceAbove
> spaceBelow
)
1819 maxHeightPopup
= spaceAbove
;
1822 int widthPopup
= ctrlSz
.x
+ m_extLeft
+ m_extRight
;
1824 if ( widthPopup
< m_widthMinPopup
)
1825 widthPopup
= m_widthMinPopup
;
1827 wxWindow
* winPopup
= m_winPopup
;
1830 // Need to disable tab traversal of parent
1832 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1833 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1834 // that if transient popup is open, then tab traversal is to be ignored.
1835 // However, I think this code would still be needed for cases where
1836 // transient popup doesn't work yet (wxWinCE?).
1837 wxWindow
* parent
= GetParent();
1838 int parentFlags
= parent
->GetWindowStyle();
1839 if ( parentFlags
& wxTAB_TRAVERSAL
)
1841 parent
->SetWindowStyle( parentFlags
& ~(wxTAB_TRAVERSAL
) );
1842 m_iFlags
|= wxCC_IFLAG_PARENT_TAB_TRAVERSAL
;
1848 winPopup
= m_winPopup
;
1858 wxASSERT( !m_popup
|| m_popup
== popup
); // Consistency check.
1860 wxSize adjustedSize
= m_popupInterface
->GetAdjustedSize(widthPopup
,
1861 m_heightPopup
<=0?DEFAULT_POPUP_HEIGHT
:m_heightPopup
,
1864 popup
->SetSize(adjustedSize
);
1866 m_popupInterface
->OnPopup();
1869 // Reposition and resize popup window
1872 wxSize szp
= popup
->GetSize();
1875 int popupY
= scrPos
.y
+ ctrlSz
.y
;
1877 // Default anchor is wxLEFT
1878 int anchorSide
= m_anchorSide
;
1880 anchorSide
= wxLEFT
;
1882 int rightX
= scrPos
.x
+ ctrlSz
.x
+ m_extRight
- szp
.x
;
1883 int leftX
= scrPos
.x
- m_extLeft
;
1884 int screenWidth
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
);
1886 // If there is not enough horizontal space, anchor on the other side.
1887 // If there is no space even then, place the popup at x 0.
1888 if ( anchorSide
== wxRIGHT
)
1892 if ( (leftX
+szp
.x
) < screenWidth
)
1893 anchorSide
= wxLEFT
;
1900 if ( (leftX
+szp
.x
) >= screenWidth
)
1903 anchorSide
= wxRIGHT
;
1909 // Select x coordinate according to the anchor side
1910 if ( anchorSide
== wxRIGHT
)
1912 else if ( anchorSide
== wxLEFT
)
1917 int showFlags
= CanDeferShow
;
1919 if ( spaceBelow
< szp
.y
)
1921 popupY
= scrPos
.y
- szp
.y
;
1922 showFlags
|= ShowAbove
;
1925 #if INSTALL_TOPLEV_HANDLER
1926 // Put top level window event handler into place
1927 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1929 if ( !m_toplevEvtHandler
)
1930 m_toplevEvtHandler
= new wxComboFrameEventHandler(this);
1932 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
1934 ((wxComboFrameEventHandler
*)m_toplevEvtHandler
)->OnPopup();
1935 toplev
->PushEventHandler( m_toplevEvtHandler
);
1939 // Set string selection (must be this way instead of SetStringSelection)
1942 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
1943 m_text
->SelectAll();
1945 m_popupInterface
->SetStringValue( m_text
->GetValue() );
1949 // This is neede since focus/selection indication may change when popup is shown
1953 // This must be after SetStringValue
1954 m_popupWinState
= Animating
;
1956 wxRect
popupWinRect( popupX
, popupY
, szp
.x
, szp
.y
);
1959 if ( (m_iFlags
& wxCC_IFLAG_DISABLE_POPUP_ANIM
) ||
1960 AnimateShow( popupWinRect
, showFlags
) )
1962 DoShowPopup( popupWinRect
, showFlags
);
1966 bool wxComboCtrlBase::AnimateShow( const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
) )
1971 void wxComboCtrlBase::DoShowPopup( const wxRect
& rect
, int WXUNUSED(flags
) )
1973 wxWindow
* winPopup
= m_winPopup
;
1975 if ( IsPopupWindowState(Animating
) )
1977 // Make sure the popup window is shown in the right position.
1978 // Should not matter even if animation already did this.
1980 // Some platforms (GTK) may like SetSize and Move to be separate
1981 // (though the bug was probably fixed).
1982 winPopup
->SetSize( rect
);
1986 m_popupWinState
= Visible
;
1988 else if ( IsPopupWindowState(Hidden
) )
1990 // Animation was aborted
1992 wxASSERT( !winPopup
->IsShown() );
1994 m_popupWinState
= Hidden
;
1998 void wxComboCtrlBase::OnPopupDismiss()
2000 // Just in case, avoid double dismiss
2001 if ( IsPopupWindowState(Hidden
) )
2004 // This must be set before focus - otherwise there will be recursive
2005 // OnPopupDismisses.
2006 m_popupWinState
= Hidden
;
2009 m_winPopup
->Disable();
2011 // Inform popup control itself
2012 m_popupInterface
->OnDismiss();
2014 if ( m_popupExtraHandler
)
2015 ((wxComboPopupExtraEventHandler
*)m_popupExtraHandler
)->OnPopupDismiss();
2017 #if INSTALL_TOPLEV_HANDLER
2018 // Remove top level window event handler
2019 if ( m_toplevEvtHandler
)
2021 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2023 toplev
->RemoveEventHandler( m_toplevEvtHandler
);
2027 m_timeCanAcceptClick
= ::wxGetLocalTimeMillis();
2029 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2030 m_timeCanAcceptClick
+= 150;
2032 // If cursor not on dropdown button, then clear its state
2033 // (technically not required by all ports, but do it for all just in case)
2034 if ( !m_btnArea
.Contains(ScreenToClient(::wxGetMousePosition())) )
2037 // Return parent's tab traversal flag.
2038 // See ShowPopup for notes.
2039 if ( m_iFlags
& wxCC_IFLAG_PARENT_TAB_TRAVERSAL
)
2041 wxWindow
* parent
= GetParent();
2042 parent
->SetWindowStyle( parent
->GetWindowStyle() | wxTAB_TRAVERSAL
);
2043 m_iFlags
&= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL
);
2046 // refresh control (necessary even if m_text)
2052 void wxComboCtrlBase::HidePopup()
2054 // Should be able to call this without popup interface
2055 if ( IsPopupWindowState(Hidden
) )
2058 // transfer value and show it in textctrl, if any
2059 if ( !IsPopupWindowState(Animating
) )
2060 SetValue( m_popupInterface
->GetStringValue() );
2067 // ----------------------------------------------------------------------------
2068 // customization methods
2069 // ----------------------------------------------------------------------------
2071 void wxComboCtrlBase::SetButtonPosition( int width
, int height
,
2072 int side
, int spacingX
)
2077 m_btnSpacingX
= spacingX
;
2082 wxSize
wxComboCtrlBase::GetButtonSize()
2084 if ( m_btnSize
.x
> 0 )
2087 wxSize
retSize(m_btnWid
,m_btnHei
);
2089 // Need to call CalculateAreas now if button size is
2090 // is not explicitly specified.
2091 if ( retSize
.x
<= 0 || retSize
.y
<= 0)
2095 retSize
= m_btnSize
;
2101 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap
& bmpNormal
,
2103 const wxBitmap
& bmpPressed
,
2104 const wxBitmap
& bmpHover
,
2105 const wxBitmap
& bmpDisabled
)
2107 m_bmpNormal
= bmpNormal
;
2108 m_blankButtonBg
= blankButtonBg
;
2110 if ( bmpPressed
.Ok() )
2111 m_bmpPressed
= bmpPressed
;
2113 m_bmpPressed
= bmpNormal
;
2115 if ( bmpHover
.Ok() )
2116 m_bmpHover
= bmpHover
;
2118 m_bmpHover
= bmpNormal
;
2120 if ( bmpDisabled
.Ok() )
2121 m_bmpDisabled
= bmpDisabled
;
2123 m_bmpDisabled
= bmpNormal
;
2128 void wxComboCtrlBase::SetCustomPaintWidth( int width
)
2132 // move textctrl accordingly
2133 wxRect r
= m_text
->GetRect();
2134 int inc
= width
- m_widthCustomPaint
;
2137 m_text
->SetSize( r
);
2140 m_widthCustomPaint
= width
;
2145 void wxComboCtrlBase::SetTextIndent( int indent
)
2149 m_absIndent
= GetNativeTextIndent();
2150 m_iFlags
&= ~(wxCC_IFLAG_INDENT_SET
);
2154 m_absIndent
= indent
;
2155 m_iFlags
|= wxCC_IFLAG_INDENT_SET
;
2161 wxCoord
wxComboCtrlBase::GetNativeTextIndent() const
2163 return DEFAULT_TEXT_INDENT
;
2166 // ----------------------------------------------------------------------------
2167 // methods forwarded to wxTextCtrl
2168 // ----------------------------------------------------------------------------
2170 wxString
wxComboCtrlBase::GetValue() const
2173 return m_text
->GetValue();
2174 return m_valueString
;
2177 void wxComboCtrlBase::SetValueWithEvent(const wxString
& value
, bool withEvent
)
2184 m_text
->SetValue(value
);
2185 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2186 m_text
->SelectAll();
2189 m_valueString
= value
;
2193 // Since wxComboPopup may want to paint the combo as well, we need
2194 // to set the string value here (as well as sometimes in ShowPopup).
2195 if ( m_valueString
!= value
&& m_popupInterface
)
2197 m_popupInterface
->SetStringValue(value
);
2201 void wxComboCtrlBase::SetValue(const wxString
& value
)
2203 SetValueWithEvent(value
, false);
2206 // In this SetValue variant wxComboPopup::SetStringValue is not called
2207 void wxComboCtrlBase::SetText(const wxString
& value
)
2209 // Unlike in SetValue(), this must be called here or
2210 // the behaviour will no be consistent in readonlys.
2211 EnsurePopupControl();
2213 m_valueString
= value
;
2218 m_text
->SetValue( value
);
2224 void wxComboCtrlBase::Copy()
2230 void wxComboCtrlBase::Cut()
2236 void wxComboCtrlBase::Paste()
2242 void wxComboCtrlBase::SetInsertionPoint(long pos
)
2245 m_text
->SetInsertionPoint(pos
);
2248 void wxComboCtrlBase::SetInsertionPointEnd()
2251 m_text
->SetInsertionPointEnd();
2254 long wxComboCtrlBase::GetInsertionPoint() const
2257 return m_text
->GetInsertionPoint();
2262 long wxComboCtrlBase::GetLastPosition() const
2265 return m_text
->GetLastPosition();
2270 void wxComboCtrlBase::Replace(long from
, long to
, const wxString
& value
)
2273 m_text
->Replace(from
, to
, value
);
2276 void wxComboCtrlBase::Remove(long from
, long to
)
2279 m_text
->Remove(from
, to
);
2282 void wxComboCtrlBase::SetSelection(long from
, long to
)
2285 m_text
->SetSelection(from
, to
);
2288 void wxComboCtrlBase::Undo()
2294 #endif // wxUSE_COMBOCTRL