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;
381 if ( show
!= ptw
->IsShown() )
384 // We used to do wxPopupTransientWindow::Popup here,
385 // but this would hide normal Show, which we are
386 // also going to need.
397 bool wxComboPopupWindow::ProcessLeftDown(wxMouseEvent
& event
)
399 return wxPopupTransientWindow::ProcessLeftDown(event
);
402 // First thing that happens when a transient popup closes is that this method gets called.
403 void wxComboPopupWindow::OnDismiss()
405 wxComboCtrlBase
* combo
= (wxComboCtrlBase
*) GetParent();
406 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxComboCtrlBase
)),
407 wxT("parent might not be wxComboCtrl, but check IMPLEMENT_DYNAMIC_CLASS(2) macro for correctness") );
409 combo
->OnPopupDismiss();
411 #endif // USES_WXPOPUPTRANSIENTWINDOW
414 // ----------------------------------------------------------------------------
415 // wxComboPopupWindowEvtHandler does bulk of the custom event handling
416 // of a popup window. It is separate so we can have different types
418 // ----------------------------------------------------------------------------
420 class wxComboPopupWindowEvtHandler
: public wxEvtHandler
424 wxComboPopupWindowEvtHandler( wxComboCtrlBase
*parent
)
429 void OnSizeEvent( wxSizeEvent
& event
);
430 void OnKeyEvent(wxKeyEvent
& event
);
432 void OnActivate( wxActivateEvent
& event
);
436 wxComboCtrlBase
* m_combo
;
438 DECLARE_EVENT_TABLE()
442 BEGIN_EVENT_TABLE(wxComboPopupWindowEvtHandler
, wxEvtHandler
)
443 EVT_KEY_DOWN(wxComboPopupWindowEvtHandler::OnKeyEvent
)
444 EVT_KEY_UP(wxComboPopupWindowEvtHandler::OnKeyEvent
)
446 EVT_ACTIVATE(wxComboPopupWindowEvtHandler::OnActivate
)
448 EVT_SIZE(wxComboPopupWindowEvtHandler::OnSizeEvent
)
452 void wxComboPopupWindowEvtHandler::OnSizeEvent( wxSizeEvent
& WXUNUSED(event
) )
454 // Block the event so that the popup control does not get auto-resized.
457 void wxComboPopupWindowEvtHandler::OnKeyEvent( wxKeyEvent
& event
)
459 // Relay keyboard event to the main child controls
460 wxWindowList children
= m_combo
->GetPopupWindow()->GetChildren();
461 wxWindowList::iterator node
= children
.begin();
462 wxWindow
* child
= (wxWindow
*)*node
;
463 child
->AddPendingEvent(event
);
467 void wxComboPopupWindowEvtHandler::OnActivate( wxActivateEvent
& event
)
469 if ( !event
.GetActive() )
471 // Tell combo control that we are dismissed.
472 m_combo
->HidePopup();
480 // ----------------------------------------------------------------------------
483 // ----------------------------------------------------------------------------
485 wxComboPopup::~wxComboPopup()
489 void wxComboPopup::OnPopup()
493 void wxComboPopup::OnDismiss()
497 wxSize
wxComboPopup::GetAdjustedSize( int minWidth
,
499 int WXUNUSED(maxHeight
) )
501 return wxSize(minWidth
,prefHeight
);
504 void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase
* combo
,
505 wxDC
& dc
, const wxRect
& rect
)
507 if ( combo
->GetWindowStyle() & wxCB_READONLY
) // ie. no textctrl
509 combo
->PrepareBackground(dc
,rect
,0);
511 dc
.DrawText( combo
->GetValue(),
512 rect
.x
+ combo
->GetTextIndent(),
513 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
517 void wxComboPopup::PaintComboControl( wxDC
& dc
, const wxRect
& rect
)
519 DefaultPaintComboControl(m_combo
,dc
,rect
);
522 void wxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
527 void wxComboPopup::OnComboDoubleClick()
531 void wxComboPopup::SetStringValue( const wxString
& WXUNUSED(value
) )
535 bool wxComboPopup::LazyCreate()
540 void wxComboPopup::Dismiss()
542 m_combo
->HidePopup();
545 // ----------------------------------------------------------------------------
547 // ----------------------------------------------------------------------------
550 // This is pushed to the event handler queue of the child textctrl.
552 class wxComboBoxExtraInputHandler
: public wxEvtHandler
556 wxComboBoxExtraInputHandler( wxComboCtrlBase
* combo
)
561 virtual ~wxComboBoxExtraInputHandler() { }
562 void OnKey(wxKeyEvent
& event
);
563 void OnFocus(wxFocusEvent
& event
);
566 wxComboCtrlBase
* m_combo
;
569 DECLARE_EVENT_TABLE()
573 BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler
, wxEvtHandler
)
574 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey
)
575 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
576 EVT_KILL_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
580 void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent
& event
)
582 // Let the wxComboCtrl event handler have a go first.
583 wxComboCtrlBase
* combo
= m_combo
;
584 wxObject
* prevObj
= event
.GetEventObject();
586 event
.SetId(combo
->GetId());
587 event
.SetEventObject(combo
);
588 combo
->GetEventHandler()->ProcessEvent(event
);
590 event
.SetId(((wxWindow
*)prevObj
)->GetId());
591 event
.SetEventObject(prevObj
);
594 void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent
& event
)
596 // FIXME: This code does run when control is clicked,
597 // yet on Windows it doesn't select all the text.
598 if ( event
.GetEventType() == wxEVT_SET_FOCUS
&&
599 !(m_combo
->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT
) )
601 if ( m_combo
->GetTextCtrl() )
602 m_combo
->GetTextCtrl()->SelectAll();
604 m_combo
->SetSelection(-1,-1);
607 // Send focus indication to parent.
608 // NB: This is needed for cases where the textctrl gets focus
609 // instead of its parent. While this may trigger multiple
610 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
611 // from combo's focus event handler), they should be quite
613 wxFocusEvent
evt2(event
.GetEventType(),m_combo
->GetId());
614 evt2
.SetEventObject(m_combo
);
615 m_combo
->GetEventHandler()->ProcessEvent(evt2
);
622 // This is pushed to the event handler queue of the control in popup.
625 class wxComboPopupExtraEventHandler
: public wxEvtHandler
629 wxComboPopupExtraEventHandler( wxComboCtrlBase
* combo
)
633 m_beenInside
= false;
635 virtual ~wxComboPopupExtraEventHandler() { }
637 void OnMouseEvent( wxMouseEvent
& event
);
639 // Called from wxComboCtrlBase::OnPopupDismiss
640 void OnPopupDismiss()
642 m_beenInside
= false;
646 wxComboCtrlBase
* m_combo
;
651 DECLARE_EVENT_TABLE()
655 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler
, wxEvtHandler
)
656 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent
)
660 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent
& event
)
662 wxPoint pt
= event
.GetPosition();
663 wxSize sz
= m_combo
->GetPopupControl()->GetControl()->GetClientSize();
664 int evtType
= event
.GetEventType();
665 bool isInside
= pt
.x
>= 0 && pt
.y
>= 0 && pt
.x
< sz
.x
&& pt
.y
< sz
.y
;
666 bool relayToButton
= false;
670 if ( evtType
== wxEVT_MOTION
||
671 evtType
== wxEVT_LEFT_DOWN
||
672 evtType
== wxEVT_RIGHT_DOWN
)
674 // Block motion and click events outside the popup
675 if ( !isInside
|| !m_combo
->IsPopupShown() )
680 else if ( evtType
== wxEVT_LEFT_UP
)
682 if ( !m_combo
->IsPopupShown() )
685 relayToButton
= true;
687 else if ( !m_beenInside
)
695 relayToButton
= true;
703 // Some mouse events to popup that happen outside it, before cursor
704 // has been inside the popup, need to be ignored by it but relayed to
707 wxWindow
* eventSink
= m_combo
;
708 wxWindow
* btn
= m_combo
->GetButton();
712 eventSink
->GetEventHandler()->ProcessEvent(event
);
716 // ----------------------------------------------------------------------------
717 // wxComboCtrlTextCtrl
718 // ----------------------------------------------------------------------------
720 class wxComboCtrlTextCtrl
: public wxTextCtrl
723 wxComboCtrlTextCtrl() : wxTextCtrl() { }
724 virtual ~wxComboCtrlTextCtrl() { }
726 virtual wxWindow
*GetMainWindowOfCompositeControl()
728 wxComboCtrl
* combo
= (wxComboCtrl
*) GetParent();
730 // Returning this instead of just 'parent' lets FindFocus work
731 // correctly even when parent control is a child of a composite
732 // generic control (as is case with wxGenericDatePickerCtrl).
733 return combo
->GetMainWindowOfCompositeControl();
737 // ----------------------------------------------------------------------------
739 // ----------------------------------------------------------------------------
742 BEGIN_EVENT_TABLE(wxComboCtrlBase
, wxControl
)
743 EVT_TEXT(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
744 EVT_SIZE(wxComboCtrlBase::OnSizeEvent
)
745 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent
)
746 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent
)
747 EVT_IDLE(wxComboCtrlBase::OnIdleEvent
)
748 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
749 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent
)
750 EVT_TEXT_ENTER(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
751 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged
)
755 IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase
, wxControl
)
757 void wxComboCtrlBase::Init()
759 m_winPopup
= (wxWindow
*)NULL
;
760 m_popup
= (wxWindow
*)NULL
;
761 m_popupWinState
= Hidden
;
762 m_btn
= (wxWindow
*) NULL
;
763 m_text
= (wxTextCtrl
*) NULL
;
764 m_popupInterface
= (wxComboPopup
*) NULL
;
766 m_popupExtraHandler
= (wxEvtHandler
*) NULL
;
767 m_textEvtHandler
= (wxEvtHandler
*) NULL
;
769 #if INSTALL_TOPLEV_HANDLER
770 m_toplevEvtHandler
= (wxEvtHandler
*) NULL
;
773 m_mainCtrlWnd
= this;
776 m_widthMinPopup
= -1;
778 m_widthCustomPaint
= 0;
779 m_widthCustomBorder
= 0;
783 m_blankButtonBg
= false;
785 m_popupWinType
= POPUPWIN_NONE
;
786 m_btnWid
= m_btnHei
= -1;
794 m_timeCanAcceptClick
= 0;
796 m_resetFocus
= false;
799 bool wxComboCtrlBase::Create(wxWindow
*parent
,
801 const wxString
& value
,
805 const wxValidator
& validator
,
806 const wxString
& name
)
808 if ( !wxControl::Create(parent
,
812 style
| wxWANTS_CHARS
,
817 m_valueString
= value
;
821 m_absIndent
= GetNativeTextIndent();
823 m_iFlags
|= wxCC_IFLAG_CREATED
;
825 // If x and y indicate valid size, wxSizeEvent won't be
826 // emitted automatically, so we need to add artifical one.
827 if ( size
.x
> 0 && size
.y
> 0 )
829 wxSizeEvent
evt(size
,GetId());
830 GetEventHandler()->AddPendingEvent(evt
);
836 void wxComboCtrlBase::InstallInputHandlers()
840 m_textEvtHandler
= new wxComboBoxExtraInputHandler(this);
841 m_text
->PushEventHandler(m_textEvtHandler
);
846 wxComboCtrlBase::CreateTextCtrl(int style
, const wxValidator
& validator
)
848 if ( !(m_windowStyle
& wxCB_READONLY
) )
853 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
854 // not used by the wxPropertyGrid and therefore the tab is processed by
855 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
856 // navigation event is then sent to the wrong window.
857 style
|= wxTE_PROCESS_TAB
;
859 if ( HasFlag(wxTE_PROCESS_ENTER
) )
860 style
|= wxTE_PROCESS_ENTER
;
862 // Ignore EVT_TEXT generated by the constructor (but only
863 // if the event redirector already exists)
864 // NB: This must be " = 1" instead of "++";
865 if ( m_textEvtHandler
)
870 m_text
= new wxComboCtrlTextCtrl();
871 m_text
->Create(this, wxID_ANY
, m_valueString
,
872 wxDefaultPosition
, wxSize(10,-1),
877 void wxComboCtrlBase::OnThemeChange()
879 // Leave the default bg on the Mac so the area used by the focus ring will
880 // be the correct colour and themed brush. Instead we'll use
881 // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed.
883 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
887 wxComboCtrlBase::~wxComboCtrlBase()
892 #if INSTALL_TOPLEV_HANDLER
893 delete ((wxComboFrameEventHandler
*)m_toplevEvtHandler
);
894 m_toplevEvtHandler
= (wxEvtHandler
*) NULL
;
900 m_text
->RemoveEventHandler(m_textEvtHandler
);
902 delete m_textEvtHandler
;
906 // ----------------------------------------------------------------------------
908 // ----------------------------------------------------------------------------
910 // Recalculates button and textctrl areas
911 void wxComboCtrlBase::CalculateAreas( int btnWidth
)
913 wxSize sz
= GetClientSize();
914 int customBorder
= m_widthCustomBorder
;
915 int btnBorder
; // border for button only
917 // check if button should really be outside the border: we'll do it it if
918 // its platform default or bitmap+pushbutton background is used, but not if
919 // there is vertical size adjustment or horizontal spacing.
920 if ( ( (m_iFlags
& wxCC_BUTTON_OUTSIDE_BORDER
) ||
921 (m_bmpNormal
.Ok() && m_blankButtonBg
) ) &&
922 m_btnSpacingX
== 0 &&
925 m_iFlags
|= wxCC_IFLAG_BUTTON_OUTSIDE
;
928 else if ( (m_iFlags
& wxCC_BUTTON_COVERS_BORDER
) &&
929 m_btnSpacingX
== 0 && !m_bmpNormal
.Ok() )
931 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
936 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
937 btnBorder
= customBorder
;
940 // Defaul indentation
941 if ( m_absIndent
< 0 )
942 m_absIndent
= GetNativeTextIndent();
944 int butWidth
= btnWidth
;
947 butWidth
= m_btnWidDefault
;
949 m_btnWidDefault
= butWidth
;
954 int butHeight
= sz
.y
- btnBorder
*2;
956 // Adjust button width
961 // Adjust button width to match aspect ratio
962 // (but only if control is smaller than best size).
963 int bestHeight
= GetBestSize().y
;
964 int height
= GetSize().y
;
966 if ( height
< bestHeight
)
968 // Make very small buttons square, as it makes
969 // them accommodate arrow image better and still
972 butWidth
= (height
*butWidth
)/bestHeight
;
974 butWidth
= butHeight
;
978 // Adjust button height
980 butHeight
= m_btnHei
;
982 // Use size of normal bitmap if...
985 // button width is set to default and blank button bg is not drawn
986 if ( m_bmpNormal
.Ok() )
988 int bmpReqWidth
= m_bmpNormal
.GetWidth();
989 int bmpReqHeight
= m_bmpNormal
.GetHeight();
991 // If drawing blank button background, we need to add some margin.
992 if ( m_blankButtonBg
)
994 bmpReqWidth
+= BMP_BUTTON_MARGIN
*2;
995 bmpReqHeight
+= BMP_BUTTON_MARGIN
*2;
998 if ( butWidth
< bmpReqWidth
|| ( m_btnWid
== 0 && !m_blankButtonBg
) )
999 butWidth
= bmpReqWidth
;
1000 if ( butHeight
< bmpReqHeight
|| ( m_btnHei
== 0 && !m_blankButtonBg
) )
1001 butHeight
= bmpReqHeight
;
1003 // Need to fix height?
1004 if ( (sz
.y
-(customBorder
*2)) < butHeight
&& btnWidth
== 0 )
1006 int newY
= butHeight
+(customBorder
*2);
1007 SetClientSize(wxDefaultCoord
,newY
);
1008 if ( m_bmpNormal
.Ok() || m_btnArea
.width
!= butWidth
|| m_btnArea
.height
!= butHeight
)
1009 m_iFlags
|= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1011 m_iFlags
&= ~wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1017 int butAreaWid
= butWidth
+ (m_btnSpacingX
*2);
1019 m_btnSize
.x
= butWidth
;
1020 m_btnSize
.y
= butHeight
;
1022 m_btnArea
.x
= ( m_btnSide
==wxRIGHT
? sz
.x
- butAreaWid
- btnBorder
: btnBorder
);
1023 m_btnArea
.y
= btnBorder
+ FOCUS_RING
;
1024 m_btnArea
.width
= butAreaWid
;
1025 m_btnArea
.height
= sz
.y
- ((btnBorder
+FOCUS_RING
)*2);
1027 m_tcArea
.x
= ( m_btnSide
==wxRIGHT
? 0 : butAreaWid
) + customBorder
+ FOCUS_RING
;
1028 m_tcArea
.y
= customBorder
+ FOCUS_RING
;
1029 m_tcArea
.width
= sz
.x
- butAreaWid
- (customBorder
*2) - (FOCUS_RING
*2);
1030 m_tcArea
.height
= sz
.y
- ((customBorder
+FOCUS_RING
)*2);
1035 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
1036 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1041 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
)
1046 #if !TEXTCTRL_TEXT_CENTERED
1048 wxSize sz
= GetClientSize();
1050 int customBorder
= m_widthCustomBorder
;
1051 if ( (m_text
->GetWindowStyleFlag() & wxBORDER_MASK
) == wxNO_BORDER
)
1054 int tcSizeY
= m_text
->GetBestSize().y
;
1055 int diff
= sz
.y
- tcSizeY
;
1056 int y
= textCtrlYAdjust
+ (diff
/2);
1058 if ( y
< customBorder
)
1061 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
+ m_absIndent
+ textCtrlXAdjust
,
1063 m_tcArea
.width
- COMBO_MARGIN
-
1064 (textCtrlXAdjust
+ m_widthCustomPaint
+ m_absIndent
),
1067 // Make sure textctrl doesn't exceed the bottom custom border
1068 wxSize tsz
= m_text
->GetSize();
1069 diff
= (y
+ tsz
.y
) - (sz
.y
- customBorder
);
1072 tsz
.y
= tsz
.y
- diff
- 1;
1073 m_text
->SetSize(tsz
);
1077 #else // TEXTCTRL_TEXT_CENTERED
1078 wxUnusedVar(textCtrlXAdjust
);
1079 wxUnusedVar(textCtrlYAdjust
);
1080 #endif // !TEXTCTRL_TEXT_CENTERED/TEXTCTRL_TEXT_CENTERED
1082 // If it has border, have textctrl will the entire text field.
1083 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
,
1085 m_tcArea
.width
- m_widthCustomPaint
,
1090 wxSize
wxComboCtrlBase::DoGetBestSize() const
1092 wxSize
sizeText(150,0);
1095 sizeText
= m_text
->GetBestSize();
1097 // TODO: Better method to calculate close-to-native control height.
1101 fhei
= (m_font
.GetPointSize()*2) + 5;
1102 else if ( wxNORMAL_FONT
->Ok() )
1103 fhei
= (wxNORMAL_FONT
->GetPointSize()*2) + 5;
1105 fhei
= sizeText
.y
+ 4;
1107 // Need to force height to accomodate bitmap?
1108 int btnSizeY
= m_btnSize
.y
;
1109 if ( m_bmpNormal
.Ok() && fhei
< btnSizeY
)
1112 // Control height doesn't depend on border
1115 int border = m_windowStyle & wxBORDER_MASK;
1116 if ( border == wxSIMPLE_BORDER )
1118 else if ( border == wxNO_BORDER )
1119 fhei += (m_widthCustomBorder*2);
1125 // Final adjustments
1131 // these are the numbers from the HIG:
1132 switch ( m_windowVariant
)
1134 case wxWINDOW_VARIANT_NORMAL
:
1138 case wxWINDOW_VARIANT_SMALL
:
1141 case wxWINDOW_VARIANT_MINI
:
1147 fhei
+= 2 * FOCUS_RING
;
1148 int width
= sizeText
.x
+ FOCUS_RING
+ COMBO_MARGIN
+ DEFAULT_DROPBUTTON_WIDTH
;
1150 wxSize
ret(width
, fhei
);
1155 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent
& event
)
1160 // defined by actual wxComboCtrls
1166 // ----------------------------------------------------------------------------
1167 // standard operations
1168 // ----------------------------------------------------------------------------
1170 bool wxComboCtrlBase::Enable(bool enable
)
1172 if ( !wxControl::Enable(enable
) )
1176 m_btn
->Enable(enable
);
1178 m_text
->Enable(enable
);
1183 bool wxComboCtrlBase::Show(bool show
)
1185 if ( !wxControl::Show(show
) )
1197 bool wxComboCtrlBase::SetFont ( const wxFont
& font
)
1199 if ( !wxControl::SetFont(font
) )
1203 m_text
->SetFont(font
);
1209 void wxComboCtrlBase::DoSetToolTip(wxToolTip
*tooltip
)
1211 wxControl::DoSetToolTip(tooltip
);
1213 // Set tool tip for button and text box
1216 const wxString
&tip
= tooltip
->GetTip();
1217 if ( m_text
) m_text
->SetToolTip(tip
);
1218 if ( m_btn
) m_btn
->SetToolTip(tip
);
1222 if ( m_text
) m_text
->SetToolTip( (wxToolTip
*) NULL
);
1223 if ( m_btn
) m_btn
->SetToolTip( (wxToolTip
*) NULL
);
1226 #endif // wxUSE_TOOLTIPS
1228 #if wxUSE_VALIDATORS
1229 void wxComboCtrlBase::SetValidator(const wxValidator
& validator
)
1231 wxTextCtrl
* textCtrl
= GetTextCtrl();
1234 textCtrl
->SetValidator( validator
);
1237 wxValidator
* wxComboCtrlBase::GetValidator()
1239 wxTextCtrl
* textCtrl
= GetTextCtrl();
1242 return textCtrl
->GetValidator();
1244 return wxControl::GetValidator();
1246 #endif // wxUSE_VALIDATORS
1248 // ----------------------------------------------------------------------------
1250 // ----------------------------------------------------------------------------
1252 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1253 // prepare combo box background on area in a way typical on platform
1254 void wxComboCtrlBase::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
1256 wxSize sz
= GetClientSize();
1258 bool doDrawFocusRect
; // also selected
1260 // For smaller size control (and for disabled background) use less spacing
1264 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1267 isEnabled
= IsEnabled();
1268 doDrawFocusRect
= ShouldDrawFocus() & !(m_iFlags
& wxCC_FULL_BUTTON
);
1270 // Windows-style: for smaller size control (and for disabled background) use less spacing
1271 focusSpacingX
= isEnabled
? 2 : 1;
1272 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
1276 // Drawing a list item
1277 isEnabled
= true; // they are never disabled
1278 doDrawFocusRect
= flags
& wxCONTROL_SELECTED
? true : false;
1284 // Set the background sub-rectangle for selection, disabled etc
1285 wxRect
selRect(rect
);
1286 selRect
.y
+= focusSpacingY
;
1287 selRect
.height
-= (focusSpacingY
*2);
1291 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1292 wcp
+= m_widthCustomPaint
;
1294 selRect
.x
+= wcp
+ focusSpacingX
;
1295 selRect
.width
-= wcp
+ (focusSpacingX
*2);
1298 bool doDrawSelRect
= true;
1302 // If popup is hidden and this control is focused,
1303 // then draw the focus-indicator (selbgcolor background etc.).
1304 if ( doDrawFocusRect
)
1306 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1307 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1311 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
1312 #ifndef __WXMAC__ // see note in OnThemeChange
1313 doDrawSelRect
= false;
1314 bgCol
= GetBackgroundColour();
1316 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1322 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
1323 #ifndef __WXMAC__ // see note in OnThemeChange
1324 bgCol
= GetBackgroundColour();
1326 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1330 dc
.SetBrush( bgCol
);
1331 if ( doDrawSelRect
)
1334 dc
.DrawRectangle( selRect
);
1337 // Don't clip exactly to the selection rectangle so we can draw
1338 // to the non-selected area in front of it.
1339 wxRect
clipRect(rect
.x
,rect
.y
,
1340 (selRect
.x
+selRect
.width
)-rect
.x
,rect
.height
);
1341 dc
.SetClippingRegion(clipRect
);
1344 // Save the library size a bit for platforms that re-implement this.
1345 void wxComboCtrlBase::PrepareBackground( wxDC
&, const wxRect
&, int ) const
1350 void wxComboCtrlBase::DrawButton( wxDC
& dc
, const wxRect
& rect
, int flags
)
1352 int drawState
= m_btnState
;
1354 if ( (m_iFlags
& wxCC_BUTTON_STAYS_DOWN
) &&
1355 GetPopupWindowState() >= Animating
)
1356 drawState
|= wxCONTROL_PRESSED
;
1358 wxRect
drawRect(rect
.x
+m_btnSpacingX
,
1359 rect
.y
+((rect
.height
-m_btnSize
.y
)/2),
1363 // Make sure area is not larger than the control
1364 if ( drawRect
.y
< rect
.y
)
1365 drawRect
.y
= rect
.y
;
1366 if ( drawRect
.height
> rect
.height
)
1367 drawRect
.height
= rect
.height
;
1369 bool enabled
= IsEnabled();
1372 drawState
|= wxCONTROL_DISABLED
;
1374 if ( !m_bmpNormal
.Ok() )
1376 if ( flags
& Button_BitmapOnly
)
1379 // Need to clear button background even if m_btn is present
1380 if ( flags
& Button_PaintBackground
)
1384 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
1385 bgCol
= GetParent()->GetBackgroundColour();
1387 bgCol
= GetBackgroundColour();
1391 dc
.DrawRectangle(rect
);
1394 // Draw standard button
1395 wxRendererNative::Get().DrawComboBoxDropButton(this,
1407 pBmp
= &m_bmpDisabled
;
1408 else if ( m_btnState
& wxCONTROL_PRESSED
)
1409 pBmp
= &m_bmpPressed
;
1410 else if ( m_btnState
& wxCONTROL_CURRENT
)
1413 pBmp
= &m_bmpNormal
;
1415 if ( m_blankButtonBg
)
1417 // If using blank button background, we need to clear its background
1418 // with button face colour instead of colour for rest of the control.
1419 if ( flags
& Button_PaintBackground
)
1421 wxColour bgCol
= GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1422 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1425 dc
.DrawRectangle(rect
);
1428 if ( !(flags
& Button_BitmapOnly
) )
1430 wxRendererNative::Get().DrawPushButton(this,
1439 // Need to clear button background even if m_btn is present
1440 // (assume non-button background was cleared just before this call so brushes are good)
1441 if ( flags
& Button_PaintBackground
)
1442 dc
.DrawRectangle(rect
);
1445 // Draw bitmap centered in drawRect
1446 dc
.DrawBitmap(*pBmp
,
1447 drawRect
.x
+ (drawRect
.width
-pBmp
->GetWidth())/2,
1448 drawRect
.y
+ (drawRect
.height
-pBmp
->GetHeight())/2,
1453 void wxComboCtrlBase::RecalcAndRefresh()
1457 wxSizeEvent
evt(GetSize(),GetId());
1458 GetEventHandler()->ProcessEvent(evt
);
1463 // ----------------------------------------------------------------------------
1464 // miscellaneous event handlers
1465 // ----------------------------------------------------------------------------
1467 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent
& event
)
1469 if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1471 if ( m_ignoreEvtText
> 0 )
1478 // Change event id, object and string before relaying it forward
1479 event
.SetId(GetId());
1480 wxString s
= event
.GetString();
1481 event
.SetEventObject(this);
1486 // call if cursor is on button area or mouse is captured for the button
1487 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent
& event
,
1490 int type
= event
.GetEventType();
1492 if ( type
== wxEVT_MOTION
)
1494 if ( (flags
& wxCC_MF_ON_BUTTON
) &&
1495 IsPopupWindowState(Hidden
) )
1497 if ( !(m_btnState
& wxCONTROL_CURRENT
) )
1499 // Mouse hover begins
1500 m_btnState
|= wxCONTROL_CURRENT
;
1501 if ( HasCapture() ) // Retain pressed state.
1502 m_btnState
|= wxCONTROL_PRESSED
;
1506 else if ( (m_btnState
& wxCONTROL_CURRENT
) )
1509 m_btnState
&= ~(wxCONTROL_CURRENT
|wxCONTROL_PRESSED
);
1513 else if ( type
== wxEVT_LEFT_DOWN
|| type
== wxEVT_LEFT_DCLICK
)
1515 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1517 m_btnState
|= wxCONTROL_PRESSED
;
1520 if ( !(m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
) )
1523 // If showing popup now, do not capture mouse or there will be interference
1527 else if ( type
== wxEVT_LEFT_UP
)
1530 // Only accept event if mouse was left-press was previously accepted
1534 if ( m_btnState
& wxCONTROL_PRESSED
)
1536 // If mouse was inside, fire the click event.
1537 if ( m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
)
1539 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1543 m_btnState
&= ~(wxCONTROL_PRESSED
);
1547 else if ( type
== wxEVT_LEAVE_WINDOW
)
1549 if ( m_btnState
& (wxCONTROL_CURRENT
|wxCONTROL_PRESSED
) )
1551 m_btnState
&= ~(wxCONTROL_CURRENT
);
1554 if ( IsPopupWindowState(Hidden
) )
1556 m_btnState
&= ~(wxCONTROL_PRESSED
);
1564 // Never have 'hot' state when popup is being shown
1565 // (this is mostly needed because of the animation).
1566 if ( !IsPopupWindowState(Hidden
) )
1567 m_btnState
&= ~wxCONTROL_CURRENT
;
1572 // returns true if event was consumed or filtered
1573 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent
& event
,
1574 int WXUNUSED(flags
) )
1576 wxLongLong t
= ::wxGetLocalTimeMillis();
1577 int evtType
= event
.GetEventType();
1579 #if USES_WXPOPUPWINDOW || USES_WXDIALOG
1580 if ( m_popupWinType
!= POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1582 if ( IsPopupWindowState(Visible
) &&
1583 ( evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_RIGHT_DOWN
) )
1591 // Filter out clicks on button immediately after popup dismiss
1592 if ( evtType
== wxEVT_LEFT_DOWN
&& t
< m_timeCanAcceptClick
)
1594 event
.SetEventType(0);
1601 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent
& event
)
1603 int evtType
= event
.GetEventType();
1605 if ( (evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_LEFT_DCLICK
) &&
1606 (m_windowStyle
& wxCB_READONLY
) )
1608 if ( GetPopupWindowState() >= Animating
)
1610 #if USES_WXPOPUPWINDOW
1611 // Click here always hides the popup.
1612 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1618 if ( !(m_windowStyle
& wxCC_SPECIAL_DCLICK
) )
1620 // In read-only mode, clicking the text is the
1621 // same as clicking the button.
1624 else if ( /*evtType == wxEVT_LEFT_UP || */evtType
== wxEVT_LEFT_DCLICK
)
1626 //if ( m_popupInterface->CycleValue() )
1628 if ( m_popupInterface
)
1629 m_popupInterface
->OnComboDoubleClick();
1634 if ( IsPopupShown() )
1636 // relay (some) mouse events to the popup
1637 if ( evtType
== wxEVT_MOUSEWHEEL
)
1638 m_popup
->AddPendingEvent(event
);
1644 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent
& event
)
1646 if ( IsPopupShown() )
1648 // pass it to the popped up control
1649 GetPopupControl()->GetControl()->AddPendingEvent(event
);
1653 int keycode
= event
.GetKeyCode();
1655 if ( keycode
== WXK_TAB
)
1657 wxNavigationKeyEvent evt
;
1659 wxWindow
* mainCtrl
= GetMainWindowOfCompositeControl();
1661 evt
.SetFlags(wxNavigationKeyEvent::FromTab
|
1662 (!event
.ShiftDown() ? wxNavigationKeyEvent::IsForward
1663 : wxNavigationKeyEvent::IsBackward
));
1664 evt
.SetEventObject(mainCtrl
);
1665 evt
.SetCurrentFocus(mainCtrl
);
1666 mainCtrl
->GetParent()->GetEventHandler()->AddPendingEvent(evt
);
1670 if ( IsKeyPopupToggle(event
) )
1676 int comboStyle
= GetWindowStyle();
1677 wxComboPopup
* popupInterface
= GetPopupControl();
1679 if ( !popupInterface
)
1685 if ( (comboStyle
& wxCB_READONLY
) ||
1686 (keycode
!= WXK_RIGHT
&& keycode
!= WXK_LEFT
) )
1688 popupInterface
->OnComboKeyEvent(event
);
1695 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent
& event
)
1697 if ( event
.GetEventType() == wxEVT_SET_FOCUS
)
1699 wxWindow
* tc
= GetTextCtrl();
1700 if ( tc
&& tc
!= DoFindFocus() )
1702 m_resetFocus
= true;
1713 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent
& WXUNUSED(event
) )
1717 m_resetFocus
= false;
1718 wxWindow
* tc
= GetTextCtrl();
1724 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
1727 // indentation may also have changed
1728 if ( !(m_iFlags
& wxCC_IFLAG_INDENT_SET
) )
1729 m_absIndent
= GetNativeTextIndent();
1733 // ----------------------------------------------------------------------------
1735 // ----------------------------------------------------------------------------
1737 // Create popup window and the child control
1738 void wxComboCtrlBase::CreatePopup()
1740 wxComboPopup
* popupInterface
= m_popupInterface
;
1745 #ifdef wxComboPopupWindowBase2
1746 if ( m_iFlags
& wxCC_IFLAG_USE_ALT_POPUP
)
1749 m_winPopup
= new wxComboPopupWindowBase2( this, wxNO_BORDER
);
1751 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
, wxEmptyString
,
1752 wxPoint(-21,-21), wxSize(20, 20),
1755 m_popupWinType
= SECONDARY_POPUP_TYPE
;
1758 #endif // wxComboPopupWindowBase2
1760 m_winPopup
= new wxComboPopupWindow( this, wxNO_BORDER
);
1761 m_popupWinType
= PRIMARY_POPUP_TYPE
;
1763 m_popupWinEvtHandler
= new wxComboPopupWindowEvtHandler(this);
1764 m_winPopup
->PushEventHandler(m_popupWinEvtHandler
);
1767 popupInterface
->Create(m_winPopup
);
1768 m_popup
= popup
= popupInterface
->GetControl();
1770 m_popupExtraHandler
= new wxComboPopupExtraEventHandler(this);
1771 popup
->PushEventHandler( m_popupExtraHandler
);
1773 // This may be helpful on some platforms
1774 // (eg. it bypasses a wxGTK popupwindow bug where
1775 // window is not initially hidden when it should be)
1778 popupInterface
->m_iFlags
|= wxCP_IFLAG_CREATED
;
1781 // Destroy popup window and the child control
1782 void wxComboCtrlBase::DestroyPopup()
1787 m_popup
->RemoveEventHandler(m_popupExtraHandler
);
1789 delete m_popupExtraHandler
;
1791 delete m_popupInterface
;
1795 m_winPopup
->RemoveEventHandler(m_popupWinEvtHandler
);
1796 delete m_popupWinEvtHandler
;
1797 m_popupWinEvtHandler
= NULL
;
1798 m_winPopup
->Destroy();
1801 m_popupExtraHandler
= (wxEvtHandler
*) NULL
;
1802 m_popupInterface
= (wxComboPopup
*) NULL
;
1803 m_winPopup
= (wxWindow
*) NULL
;
1804 m_popup
= (wxWindow
*) NULL
;
1807 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup
* iface
)
1809 wxCHECK_RET( iface
, wxT("no popup interface set for wxComboCtrl") );
1813 iface
->InitBase(this);
1816 m_popupInterface
= iface
;
1818 if ( !iface
->LazyCreate() )
1824 m_popup
= (wxWindow
*) NULL
;
1827 // This must be done after creation
1828 if ( m_valueString
.length() )
1830 iface
->SetStringValue(m_valueString
);
1835 // Ensures there is atleast the default popup
1836 void wxComboCtrlBase::EnsurePopupControl()
1838 if ( !m_popupInterface
)
1839 SetPopupControl(NULL
);
1842 void wxComboCtrlBase::OnButtonClick()
1844 // Derived classes can override this method for totally custom
1849 void wxComboCtrlBase::ShowPopup()
1851 EnsurePopupControl();
1852 wxCHECK_RET( !IsPopupWindowState(Visible
), wxT("popup window already shown") );
1854 if ( IsPopupWindowState(Animating
) )
1859 // Space above and below
1865 wxSize ctrlSz
= GetSize();
1867 screenHeight
= wxSystemSettings::GetMetric( wxSYS_SCREEN_Y
);
1868 scrPos
= GetParent()->ClientToScreen(GetPosition());
1870 spaceAbove
= scrPos
.y
;
1871 spaceBelow
= screenHeight
- spaceAbove
- ctrlSz
.y
;
1873 maxHeightPopup
= spaceBelow
;
1874 if ( spaceAbove
> spaceBelow
)
1875 maxHeightPopup
= spaceAbove
;
1878 int widthPopup
= ctrlSz
.x
+ m_extLeft
+ m_extRight
;
1880 if ( widthPopup
< m_widthMinPopup
)
1881 widthPopup
= m_widthMinPopup
;
1883 wxWindow
* winPopup
= m_winPopup
;
1886 // Need to disable tab traversal of parent
1888 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1889 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1890 // that if transient popup is open, then tab traversal is to be ignored.
1891 // However, I think this code would still be needed for cases where
1892 // transient popup doesn't work yet (wxWinCE?).
1893 wxWindow
* parent
= GetParent();
1894 int parentFlags
= parent
->GetWindowStyle();
1895 if ( parentFlags
& wxTAB_TRAVERSAL
)
1897 parent
->SetWindowStyle( parentFlags
& ~(wxTAB_TRAVERSAL
) );
1898 m_iFlags
|= wxCC_IFLAG_PARENT_TAB_TRAVERSAL
;
1904 winPopup
= m_winPopup
;
1914 wxASSERT( !m_popup
|| m_popup
== popup
); // Consistency check.
1916 wxSize adjustedSize
= m_popupInterface
->GetAdjustedSize(widthPopup
,
1917 m_heightPopup
<=0?DEFAULT_POPUP_HEIGHT
:m_heightPopup
,
1920 popup
->SetSize(adjustedSize
);
1922 m_popupInterface
->OnPopup();
1925 // Reposition and resize popup window
1928 wxSize szp
= popup
->GetSize();
1931 int popupY
= scrPos
.y
+ ctrlSz
.y
;
1933 // Default anchor is wxLEFT
1934 int anchorSide
= m_anchorSide
;
1936 anchorSide
= wxLEFT
;
1938 int rightX
= scrPos
.x
+ ctrlSz
.x
+ m_extRight
- szp
.x
;
1939 int leftX
= scrPos
.x
- m_extLeft
;
1941 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
1944 int screenWidth
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
);
1946 // If there is not enough horizontal space, anchor on the other side.
1947 // If there is no space even then, place the popup at x 0.
1948 if ( anchorSide
== wxRIGHT
)
1952 if ( (leftX
+szp
.x
) < screenWidth
)
1953 anchorSide
= wxLEFT
;
1960 if ( (leftX
+szp
.x
) >= screenWidth
)
1963 anchorSide
= wxRIGHT
;
1969 // Select x coordinate according to the anchor side
1970 if ( anchorSide
== wxRIGHT
)
1972 else if ( anchorSide
== wxLEFT
)
1977 int showFlags
= CanDeferShow
;
1979 if ( spaceBelow
< szp
.y
)
1981 popupY
= scrPos
.y
- szp
.y
;
1982 showFlags
|= ShowAbove
;
1985 #if INSTALL_TOPLEV_HANDLER
1986 // Put top level window event handler into place
1987 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1989 if ( !m_toplevEvtHandler
)
1990 m_toplevEvtHandler
= new wxComboFrameEventHandler(this);
1992 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
1994 ((wxComboFrameEventHandler
*)m_toplevEvtHandler
)->OnPopup();
1995 toplev
->PushEventHandler( m_toplevEvtHandler
);
1999 // Set string selection (must be this way instead of SetStringSelection)
2002 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2003 m_text
->SelectAll();
2005 m_popupInterface
->SetStringValue( m_text
->GetValue() );
2009 // This is neede since focus/selection indication may change when popup is shown
2013 // This must be after SetStringValue
2014 m_popupWinState
= Animating
;
2016 wxRect
popupWinRect( popupX
, popupY
, szp
.x
, szp
.y
);
2019 if ( (m_iFlags
& wxCC_IFLAG_DISABLE_POPUP_ANIM
) ||
2020 AnimateShow( popupWinRect
, showFlags
) )
2022 DoShowPopup( popupWinRect
, showFlags
);
2026 bool wxComboCtrlBase::AnimateShow( const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
) )
2031 void wxComboCtrlBase::DoShowPopup( const wxRect
& rect
, int WXUNUSED(flags
) )
2033 wxWindow
* winPopup
= m_winPopup
;
2035 if ( IsPopupWindowState(Animating
) )
2037 // Make sure the popup window is shown in the right position.
2038 // Should not matter even if animation already did this.
2040 // Some platforms (GTK) may like SetSize and Move to be separate
2041 // (though the bug was probably fixed).
2042 winPopup
->SetSize( rect
);
2044 #if USES_WXPOPUPTRANSIENTWINDOW
2045 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2046 ((wxPopupTransientWindow
*)winPopup
)->Popup(m_popup
);
2051 m_popupWinState
= Visible
;
2053 else if ( IsPopupWindowState(Hidden
) )
2055 // Animation was aborted
2057 wxASSERT( !winPopup
->IsShown() );
2059 m_popupWinState
= Hidden
;
2065 void wxComboCtrlBase::OnPopupDismiss()
2067 // Just in case, avoid double dismiss
2068 if ( IsPopupWindowState(Hidden
) )
2071 // This must be set before focus - otherwise there will be recursive
2072 // OnPopupDismisses.
2073 m_popupWinState
= Hidden
;
2076 m_winPopup
->Disable();
2078 // Inform popup control itself
2079 m_popupInterface
->OnDismiss();
2081 if ( m_popupExtraHandler
)
2082 ((wxComboPopupExtraEventHandler
*)m_popupExtraHandler
)->OnPopupDismiss();
2084 #if INSTALL_TOPLEV_HANDLER
2085 // Remove top level window event handler
2086 if ( m_toplevEvtHandler
)
2088 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2090 toplev
->RemoveEventHandler( m_toplevEvtHandler
);
2094 m_timeCanAcceptClick
= ::wxGetLocalTimeMillis();
2096 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2097 m_timeCanAcceptClick
+= 150;
2099 // If cursor not on dropdown button, then clear its state
2100 // (technically not required by all ports, but do it for all just in case)
2101 if ( !m_btnArea
.Contains(ScreenToClient(::wxGetMousePosition())) )
2104 // Return parent's tab traversal flag.
2105 // See ShowPopup for notes.
2106 if ( m_iFlags
& wxCC_IFLAG_PARENT_TAB_TRAVERSAL
)
2108 wxWindow
* parent
= GetParent();
2109 parent
->SetWindowStyle( parent
->GetWindowStyle() | wxTAB_TRAVERSAL
);
2110 m_iFlags
&= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL
);
2113 // refresh control (necessary even if m_text)
2119 void wxComboCtrlBase::HidePopup()
2121 // Should be able to call this without popup interface
2122 if ( IsPopupWindowState(Hidden
) )
2125 // transfer value and show it in textctrl, if any
2126 if ( !IsPopupWindowState(Animating
) )
2127 SetValue( m_popupInterface
->GetStringValue() );
2134 // ----------------------------------------------------------------------------
2135 // customization methods
2136 // ----------------------------------------------------------------------------
2138 void wxComboCtrlBase::SetButtonPosition( int width
, int height
,
2139 int side
, int spacingX
)
2144 m_btnSpacingX
= spacingX
;
2149 wxSize
wxComboCtrlBase::GetButtonSize()
2151 if ( m_btnSize
.x
> 0 )
2154 wxSize
retSize(m_btnWid
,m_btnHei
);
2156 // Need to call CalculateAreas now if button size is
2157 // is not explicitly specified.
2158 if ( retSize
.x
<= 0 || retSize
.y
<= 0)
2162 retSize
= m_btnSize
;
2168 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap
& bmpNormal
,
2170 const wxBitmap
& bmpPressed
,
2171 const wxBitmap
& bmpHover
,
2172 const wxBitmap
& bmpDisabled
)
2174 m_bmpNormal
= bmpNormal
;
2175 m_blankButtonBg
= blankButtonBg
;
2177 if ( bmpPressed
.Ok() )
2178 m_bmpPressed
= bmpPressed
;
2180 m_bmpPressed
= bmpNormal
;
2182 if ( bmpHover
.Ok() )
2183 m_bmpHover
= bmpHover
;
2185 m_bmpHover
= bmpNormal
;
2187 if ( bmpDisabled
.Ok() )
2188 m_bmpDisabled
= bmpDisabled
;
2190 m_bmpDisabled
= bmpNormal
;
2195 void wxComboCtrlBase::SetCustomPaintWidth( int width
)
2199 // move textctrl accordingly
2200 wxRect r
= m_text
->GetRect();
2201 int inc
= width
- m_widthCustomPaint
;
2204 m_text
->SetSize( r
);
2207 m_widthCustomPaint
= width
;
2212 void wxComboCtrlBase::SetTextIndent( int indent
)
2216 m_absIndent
= GetNativeTextIndent();
2217 m_iFlags
&= ~(wxCC_IFLAG_INDENT_SET
);
2221 m_absIndent
= indent
;
2222 m_iFlags
|= wxCC_IFLAG_INDENT_SET
;
2228 wxCoord
wxComboCtrlBase::GetNativeTextIndent() const
2230 return DEFAULT_TEXT_INDENT
;
2233 // ----------------------------------------------------------------------------
2234 // methods forwarded to wxTextCtrl
2235 // ----------------------------------------------------------------------------
2237 wxString
wxComboCtrlBase::GetValue() const
2240 return m_text
->GetValue();
2241 return m_valueString
;
2244 void wxComboCtrlBase::SetValueWithEvent(const wxString
& value
, bool withEvent
)
2251 m_text
->SetValue(value
);
2253 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2254 m_text
->SelectAll();
2257 // Since wxComboPopup may want to paint the combo as well, we need
2258 // to set the string value here (as well as sometimes in ShowPopup).
2259 if ( m_valueString
!= value
)
2261 m_valueString
= value
;
2263 EnsurePopupControl();
2265 if (m_popupInterface
)
2266 m_popupInterface
->SetStringValue(value
);
2272 void wxComboCtrlBase::SetValue(const wxString
& value
)
2274 SetValueWithEvent(value
, false);
2277 // In this SetValue variant wxComboPopup::SetStringValue is not called
2278 void wxComboCtrlBase::SetText(const wxString
& value
)
2280 // Unlike in SetValue(), this must be called here or
2281 // the behaviour will no be consistent in readonlys.
2282 EnsurePopupControl();
2284 m_valueString
= value
;
2289 m_text
->SetValue( value
);
2295 void wxComboCtrlBase::Copy()
2301 void wxComboCtrlBase::Cut()
2307 void wxComboCtrlBase::Paste()
2313 void wxComboCtrlBase::SetInsertionPoint(long pos
)
2316 m_text
->SetInsertionPoint(pos
);
2319 void wxComboCtrlBase::SetInsertionPointEnd()
2322 m_text
->SetInsertionPointEnd();
2325 long wxComboCtrlBase::GetInsertionPoint() const
2328 return m_text
->GetInsertionPoint();
2333 long wxComboCtrlBase::GetLastPosition() const
2336 return m_text
->GetLastPosition();
2341 void wxComboCtrlBase::Replace(long from
, long to
, const wxString
& value
)
2344 m_text
->Replace(from
, to
, value
);
2347 void wxComboCtrlBase::Remove(long from
, long to
)
2350 m_text
->Remove(from
, to
);
2353 void wxComboCtrlBase::SetSelection(long from
, long to
)
2356 m_text
->SetSelection(from
, to
);
2359 void wxComboCtrlBase::Undo()
2365 #endif // wxUSE_COMBOCTRL