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"
36 #include "wx/textctrl.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 // Let's use wxFrame as a fall-back solution until wxMSW gets wxNonOwnedWindow
63 #define wxCC_GENERIC_TLW_IS_FRAME
64 #define wxComboCtrlGenericTLW wxFrame
66 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
67 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
68 // native controls work on it like normal.
69 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
70 #define TEXTCTRL_TEXT_CENTERED 0 // 1 if text in textctrl is vertically centered
71 #define FOCUS_RING 0 // No focus ring on wxMSW
73 //#undef wxUSE_POPUPWIN
74 //#define wxUSE_POPUPWIN 0
76 #elif defined(__WXGTK__)
78 // NB: It is not recommended to use wxDialog as popup on wxGTK, because of
79 // this bug: If wxDialog is hidden, its position becomes corrupt
80 // between hide and next show, but without internal coordinates being
81 // reflected (or something like that - atleast commenting out ->Hide()
82 // seemed to eliminate the position change).
84 #include "wx/dialog.h"
85 #define wxCC_GENERIC_TLW_IS_DIALOG
86 #define wxComboCtrlGenericTLW wxDialog
88 #include "wx/gtk/private.h"
90 // NB: Let's not be afraid to use wxGTK's wxPopupTransientWindow as a
91 // 'perfect' popup, as it can succesfully host child controls even in
92 // popups that are shown in modal dialogs.
94 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
95 #define TRANSIENT_POPUPWIN_IS_PERFECT 1 // wxPopupTransientWindow works, its child can have focus, and common
96 // native controls work on it like normal.
97 #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
98 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
99 #define FOCUS_RING 0 // No focus ring on wxGTK
101 #elif defined(__WXMAC__)
103 #include "wx/nonownedwnd.h"
104 #define wxCC_GENERIC_TLW_IS_NONOWNEDWINDOW
105 #define wxComboCtrlGenericTLW wxNonOwnedWindow
107 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
108 #define TRANSIENT_POPUPWIN_IS_PERFECT 1 // wxPopupTransientWindow works, its child can have focus, and common
109 // native controls work on it like normal.
110 #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
111 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
112 #define FOCUS_RING 3 // Reserve room for the textctrl's focus ring to display
114 #undef DEFAULT_DROPBUTTON_WIDTH
115 #define DEFAULT_DROPBUTTON_WIDTH 22
117 #define COMBO_MARGIN FOCUS_RING
121 #include "wx/dialog.h"
122 #define wxCC_GENERIC_TLW_IS_DIALOG
123 #define wxComboCtrlGenericTLW wxDialog
125 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
126 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
127 // native controls work on it like normal.
128 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
129 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
135 // Popupwin is really only supported on wxMSW (not WINCE) and wxGTK, regardless
136 // what the wxUSE_POPUPWIN says.
137 // FIXME: Why isn't wxUSE_POPUPWIN reliable any longer? (it was in wxW2.6.2)
138 #if (!defined(__WXMSW__) && !defined(__WXGTK__) && !defined(__WXMAC__)) || defined(__WXWINCE__)
139 #undef wxUSE_POPUPWIN
140 #define wxUSE_POPUPWIN 0
145 #include "wx/popupwin.h"
147 #undef USE_TRANSIENT_POPUP
148 #define USE_TRANSIENT_POPUP 0
152 // Define different types of popup windows
156 POPUPWIN_WXPOPUPTRANSIENTWINDOW
= 1,
157 POPUPWIN_WXPOPUPWINDOW
= 2,
158 POPUPWIN_GENERICTLW
= 3
162 #if USE_TRANSIENT_POPUP
163 // wxPopupTransientWindow is implemented
165 #define wxComboPopupWindowBase wxPopupTransientWindow
166 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPTRANSIENTWINDOW
167 #define USES_WXPOPUPTRANSIENTWINDOW 1
169 #if TRANSIENT_POPUPWIN_IS_PERFECT
171 #elif POPUPWIN_IS_PERFECT
172 #define wxComboPopupWindowBase2 wxPopupWindow
173 #define SECONDARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
174 #define USES_WXPOPUPWINDOW 1
176 #define wxComboPopupWindowBase2 wxComboCtrlGenericTLW
177 #define SECONDARY_POPUP_TYPE POPUPWIN_GENERICTLW
178 #define USES_GENERICTLW 1
182 // wxPopupWindow (but not wxPopupTransientWindow) is properly implemented
184 #define wxComboPopupWindowBase wxPopupWindow
185 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
186 #define USES_WXPOPUPWINDOW 1
188 #if !POPUPWIN_IS_PERFECT
189 #define wxComboPopupWindowBase2 wxComboCtrlGenericTLW
190 #define SECONDARY_POPUP_TYPE POPUPWIN_GENERICTLW
191 #define USES_GENERICTLW 1
195 // wxPopupWindow is not implemented
197 #define wxComboPopupWindowBase wxComboCtrlGenericTLW
198 #define PRIMARY_POPUP_TYPE POPUPWIN_GENERICTLW
199 #define USES_GENERICTLW 1
204 #ifndef USES_WXPOPUPTRANSIENTWINDOW
205 #define USES_WXPOPUPTRANSIENTWINDOW 0
208 #ifndef USES_WXPOPUPWINDOW
209 #define USES_WXPOPUPWINDOW 0
212 #ifndef USES_GENERICTLW
213 #define USES_GENERICTLW 0
217 #if USES_WXPOPUPWINDOW
218 #define INSTALL_TOPLEV_HANDLER 1
220 #define INSTALL_TOPLEV_HANDLER 0
226 // * wxComboPopupWindow for external use (ie. replace old wxUniv wxPopupComboWindow)
230 // ----------------------------------------------------------------------------
231 // wxComboFrameEventHandler takes care of hiding the popup when events happen
232 // in its top level parent.
233 // ----------------------------------------------------------------------------
235 #if INSTALL_TOPLEV_HANDLER
238 // This will no longer be necessary after wxTransientPopupWindow
239 // works well on all platforms.
242 class wxComboFrameEventHandler
: public wxEvtHandler
245 wxComboFrameEventHandler( wxComboCtrlBase
* pCb
);
246 virtual ~wxComboFrameEventHandler();
250 void OnIdle( wxIdleEvent
& event
);
251 void OnMouseEvent( wxMouseEvent
& event
);
252 void OnActivate( wxActivateEvent
& event
);
253 void OnResize( wxSizeEvent
& event
);
254 void OnMove( wxMoveEvent
& event
);
255 void OnMenuEvent( wxMenuEvent
& event
);
256 void OnClose( wxCloseEvent
& event
);
259 wxWindow
* m_focusStart
;
260 wxComboCtrlBase
* m_combo
;
263 DECLARE_EVENT_TABLE()
266 BEGIN_EVENT_TABLE(wxComboFrameEventHandler
, wxEvtHandler
)
267 EVT_IDLE(wxComboFrameEventHandler::OnIdle
)
268 EVT_LEFT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
269 EVT_RIGHT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
270 EVT_SIZE(wxComboFrameEventHandler::OnResize
)
271 EVT_MOVE(wxComboFrameEventHandler::OnMove
)
272 EVT_MENU_HIGHLIGHT(wxID_ANY
,wxComboFrameEventHandler::OnMenuEvent
)
273 EVT_MENU_OPEN(wxComboFrameEventHandler::OnMenuEvent
)
274 EVT_ACTIVATE(wxComboFrameEventHandler::OnActivate
)
275 EVT_CLOSE(wxComboFrameEventHandler::OnClose
)
278 wxComboFrameEventHandler::wxComboFrameEventHandler( wxComboCtrlBase
* combo
)
284 wxComboFrameEventHandler::~wxComboFrameEventHandler()
288 void wxComboFrameEventHandler::OnPopup()
290 m_focusStart
= ::wxWindow::FindFocus();
293 void wxComboFrameEventHandler::OnIdle( wxIdleEvent
& event
)
295 wxWindow
* winFocused
= ::wxWindow::FindFocus();
297 wxWindow
* popup
= m_combo
->GetPopupControl()->GetControl();
298 wxWindow
* winpopup
= m_combo
->GetPopupWindow();
301 winFocused
!= m_focusStart
&&
302 winFocused
!= popup
&&
303 winFocused
->GetParent() != popup
&&
304 winFocused
!= winpopup
&&
305 winFocused
->GetParent() != winpopup
&&
306 winFocused
!= m_combo
&&
307 winFocused
!= m_combo
->GetButton() // GTK (atleast) requires this
310 m_combo
->HidePopup(true);
316 void wxComboFrameEventHandler::OnMenuEvent( wxMenuEvent
& event
)
318 m_combo
->HidePopup(true);
322 void wxComboFrameEventHandler::OnMouseEvent( wxMouseEvent
& event
)
324 m_combo
->HidePopup(true);
328 void wxComboFrameEventHandler::OnClose( wxCloseEvent
& event
)
330 m_combo
->HidePopup(true);
334 void wxComboFrameEventHandler::OnActivate( wxActivateEvent
& event
)
336 m_combo
->HidePopup(true);
340 void wxComboFrameEventHandler::OnResize( wxSizeEvent
& event
)
342 m_combo
->HidePopup(true);
346 void wxComboFrameEventHandler::OnMove( wxMoveEvent
& event
)
348 m_combo
->HidePopup(true);
352 #endif // INSTALL_TOPLEV_HANDLER
354 // ----------------------------------------------------------------------------
355 // wxComboPopupWindow is, in essence, wxPopupWindow customized for
357 // ----------------------------------------------------------------------------
359 class wxComboPopupWindow
: public wxComboPopupWindowBase
363 wxComboPopupWindow( wxComboCtrlBase
*parent
,
365 #if USES_WXPOPUPWINDOW || USES_WXPOPUPTRANSIENTWINDOW
366 : wxComboPopupWindowBase(parent
,style
)
368 : wxComboPopupWindowBase(parent
,
379 #if USES_WXPOPUPTRANSIENTWINDOW
380 virtual bool Show( bool show
);
381 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
383 virtual void OnDismiss();
391 #if USES_WXPOPUPTRANSIENTWINDOW
392 bool wxComboPopupWindow::Show( bool show
)
394 // Guard against recursion
396 return wxComboPopupWindowBase::Show(show
);
400 wxASSERT( IsKindOf(CLASSINFO(wxPopupTransientWindow
)) );
402 wxPopupTransientWindow
* ptw
= (wxPopupTransientWindow
*) this;
404 if ( show
!= ptw
->IsShown() )
407 // We used to do wxPopupTransientWindow::Popup here,
408 // but this would hide normal Show, which we are
409 // also going to need.
420 bool wxComboPopupWindow::ProcessLeftDown(wxMouseEvent
& event
)
422 return wxPopupTransientWindow::ProcessLeftDown(event
);
425 // First thing that happens when a transient popup closes is that this method gets called.
426 void wxComboPopupWindow::OnDismiss()
428 wxComboCtrlBase
* combo
= (wxComboCtrlBase
*) GetParent();
429 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxComboCtrlBase
)),
430 wxT("parent might not be wxComboCtrl, but check IMPLEMENT_DYNAMIC_CLASS(2) macro for correctness") );
432 combo
->OnPopupDismiss(true);
434 #endif // USES_WXPOPUPTRANSIENTWINDOW
437 // ----------------------------------------------------------------------------
438 // wxComboPopupWindowEvtHandler does bulk of the custom event handling
439 // of a popup window. It is separate so we can have different types
441 // ----------------------------------------------------------------------------
443 class wxComboPopupWindowEvtHandler
: public wxEvtHandler
447 wxComboPopupWindowEvtHandler( wxComboCtrlBase
*parent
)
452 void OnSizeEvent( wxSizeEvent
& event
);
453 void OnKeyEvent(wxKeyEvent
& event
);
455 void OnActivate( wxActivateEvent
& event
);
459 wxComboCtrlBase
* m_combo
;
461 DECLARE_EVENT_TABLE()
465 BEGIN_EVENT_TABLE(wxComboPopupWindowEvtHandler
, wxEvtHandler
)
466 EVT_KEY_DOWN(wxComboPopupWindowEvtHandler::OnKeyEvent
)
467 EVT_KEY_UP(wxComboPopupWindowEvtHandler::OnKeyEvent
)
469 EVT_ACTIVATE(wxComboPopupWindowEvtHandler::OnActivate
)
471 EVT_SIZE(wxComboPopupWindowEvtHandler::OnSizeEvent
)
475 void wxComboPopupWindowEvtHandler::OnSizeEvent( wxSizeEvent
& WXUNUSED(event
) )
477 // Block the event so that the popup control does not get auto-resized.
480 void wxComboPopupWindowEvtHandler::OnKeyEvent( wxKeyEvent
& event
)
482 // Relay keyboard event to the main child controls
483 wxWindowList children
= m_combo
->GetPopupWindow()->GetChildren();
484 wxWindowList::iterator node
= children
.begin();
485 wxWindow
* child
= (wxWindow
*)*node
;
486 child
->GetEventHandler()->AddPendingEvent(event
);
490 void wxComboPopupWindowEvtHandler::OnActivate( wxActivateEvent
& event
)
492 if ( !event
.GetActive() )
494 // Tell combo control that we are dismissed.
495 m_combo
->HidePopup(true);
503 // ----------------------------------------------------------------------------
506 // ----------------------------------------------------------------------------
508 wxComboPopup::~wxComboPopup()
512 void wxComboPopup::OnPopup()
516 void wxComboPopup::OnDismiss()
520 wxComboCtrl
* wxComboPopup::GetComboCtrl() const
522 return wxStaticCast(m_combo
, wxComboCtrl
);
525 wxSize
wxComboPopup::GetAdjustedSize( int minWidth
,
527 int WXUNUSED(maxHeight
) )
529 return wxSize(minWidth
,prefHeight
);
532 void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase
* combo
,
533 wxDC
& dc
, const wxRect
& rect
)
535 if ( combo
->GetWindowStyle() & wxCB_READONLY
) // ie. no textctrl
537 combo
->PrepareBackground(dc
,rect
,0);
539 dc
.DrawText( combo
->GetValue(),
540 rect
.x
+ combo
->m_marginLeft
,
541 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
545 void wxComboPopup::PaintComboControl( wxDC
& dc
, const wxRect
& rect
)
547 DefaultPaintComboControl(m_combo
,dc
,rect
);
550 void wxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
555 void wxComboPopup::OnComboDoubleClick()
559 void wxComboPopup::SetStringValue( const wxString
& WXUNUSED(value
) )
563 bool wxComboPopup::LazyCreate()
568 void wxComboPopup::Dismiss()
570 m_combo
->HidePopup(true);
573 // ----------------------------------------------------------------------------
575 // ----------------------------------------------------------------------------
578 // This is pushed to the event handler queue of the child textctrl.
580 class wxComboBoxExtraInputHandler
: public wxEvtHandler
584 wxComboBoxExtraInputHandler( wxComboCtrlBase
* combo
)
589 virtual ~wxComboBoxExtraInputHandler() { }
590 void OnKey(wxKeyEvent
& event
);
591 void OnFocus(wxFocusEvent
& event
);
594 wxComboCtrlBase
* m_combo
;
597 DECLARE_EVENT_TABLE()
601 BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler
, wxEvtHandler
)
602 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey
)
603 EVT_KEY_UP(wxComboBoxExtraInputHandler::OnKey
)
604 EVT_CHAR(wxComboBoxExtraInputHandler::OnKey
)
605 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
606 EVT_KILL_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
610 void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent
& event
)
612 // Let the wxComboCtrl event handler have a go first.
613 wxComboCtrlBase
* combo
= m_combo
;
615 wxKeyEvent
redirectedEvent(event
);
616 redirectedEvent
.SetId(combo
->GetId());
617 redirectedEvent
.SetEventObject(combo
);
619 if ( !combo
->GetEventHandler()->ProcessEvent(redirectedEvent
) )
621 // Don't let TAB through to the text ctrl - looks ugly
622 if ( event
.GetKeyCode() != WXK_TAB
)
627 void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent
& event
)
629 // FIXME: This code does run when control is clicked,
630 // yet on Windows it doesn't select all the text.
631 if ( event
.GetEventType() == wxEVT_SET_FOCUS
&&
632 !(m_combo
->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT
) )
634 if ( m_combo
->GetTextCtrl() )
635 m_combo
->GetTextCtrl()->SelectAll();
637 m_combo
->SetSelection(-1,-1);
640 // Send focus indication to parent.
641 // NB: This is needed for cases where the textctrl gets focus
642 // instead of its parent. While this may trigger multiple
643 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
644 // from combo's focus event handler), they should be quite
646 wxFocusEvent
evt2(event
.GetEventType(),m_combo
->GetId());
647 evt2
.SetEventObject(m_combo
);
648 m_combo
->GetEventHandler()->ProcessEvent(evt2
);
655 // This is pushed to the event handler queue of the control in popup.
658 class wxComboPopupExtraEventHandler
: public wxEvtHandler
662 wxComboPopupExtraEventHandler( wxComboCtrlBase
* combo
)
666 m_beenInside
= false;
668 virtual ~wxComboPopupExtraEventHandler() { }
670 void OnMouseEvent( wxMouseEvent
& event
);
672 // Called from wxComboCtrlBase::OnPopupDismiss
673 void OnPopupDismiss()
675 m_beenInside
= false;
679 wxComboCtrlBase
* m_combo
;
684 DECLARE_EVENT_TABLE()
688 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler
, wxEvtHandler
)
689 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent
)
693 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent
& event
)
695 wxPoint pt
= event
.GetPosition();
696 wxSize sz
= m_combo
->GetPopupControl()->GetControl()->GetClientSize();
697 int evtType
= event
.GetEventType();
698 bool isInside
= pt
.x
>= 0 && pt
.y
>= 0 && pt
.x
< sz
.x
&& pt
.y
< sz
.y
;
699 bool relayToButton
= false;
703 if ( evtType
== wxEVT_MOTION
||
704 evtType
== wxEVT_LEFT_DOWN
||
705 evtType
== wxEVT_RIGHT_DOWN
)
707 // Block motion and click events outside the popup
708 if ( !isInside
|| !m_combo
->IsPopupShown() )
713 else if ( evtType
== wxEVT_LEFT_UP
)
715 if ( !m_combo
->IsPopupShown() )
718 relayToButton
= true;
720 else if ( !m_beenInside
)
728 relayToButton
= true;
736 // Some mouse events to popup that happen outside it, before cursor
737 // has been inside the popup, need to be ignored by it but relayed to
740 wxWindow
* eventSink
= m_combo
;
741 wxWindow
* btn
= m_combo
->GetButton();
745 eventSink
->GetEventHandler()->ProcessEvent(event
);
749 // ----------------------------------------------------------------------------
750 // wxComboCtrlTextCtrl
751 // ----------------------------------------------------------------------------
753 class wxComboCtrlTextCtrl
: public wxTextCtrl
756 wxComboCtrlTextCtrl() : wxTextCtrl() { }
757 virtual ~wxComboCtrlTextCtrl() { }
759 virtual wxWindow
*GetMainWindowOfCompositeControl()
761 wxComboCtrl
* combo
= (wxComboCtrl
*) GetParent();
763 // Returning this instead of just 'parent' lets FindFocus work
764 // correctly even when parent control is a child of a composite
765 // generic control (as is case with wxGenericDatePickerCtrl).
766 return combo
->GetMainWindowOfCompositeControl();
770 // ----------------------------------------------------------------------------
772 // ----------------------------------------------------------------------------
775 BEGIN_EVENT_TABLE(wxComboCtrlBase
, wxControl
)
776 EVT_TEXT(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
777 EVT_SIZE(wxComboCtrlBase::OnSizeEvent
)
778 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent
)
779 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent
)
780 EVT_IDLE(wxComboCtrlBase::OnIdleEvent
)
781 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
782 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent
)
783 EVT_TEXT_ENTER(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
784 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged
)
788 IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase
, wxControl
)
790 void wxComboCtrlBase::Init()
794 m_popupWinState
= Hidden
;
797 m_popupInterface
= NULL
;
799 m_popupExtraHandler
= NULL
;
800 m_textEvtHandler
= NULL
;
802 #if INSTALL_TOPLEV_HANDLER
803 m_toplevEvtHandler
= NULL
;
806 m_mainCtrlWnd
= this;
809 m_widthMinPopup
= -1;
811 m_widthCustomPaint
= 0;
812 m_widthCustomBorder
= 0;
816 m_blankButtonBg
= false;
818 m_popupWinType
= POPUPWIN_NONE
;
819 m_btnWid
= m_btnHei
= -1;
827 m_timeCanAcceptClick
= 0;
829 m_resetFocus
= false;
832 bool wxComboCtrlBase::Create(wxWindow
*parent
,
834 const wxString
& value
,
838 const wxValidator
& validator
,
839 const wxString
& name
)
841 if ( !wxControl::Create(parent
,
845 style
| wxWANTS_CHARS
,
850 m_valueString
= value
;
854 m_marginLeft
= GetNativeTextIndent();
856 m_iFlags
|= wxCC_IFLAG_CREATED
;
858 // If x and y indicate valid size, wxSizeEvent won't be
859 // emitted automatically, so we need to add artifical one.
860 if ( size
.x
> 0 && size
.y
> 0 )
862 wxSizeEvent
evt(size
,GetId());
863 GetEventHandler()->AddPendingEvent(evt
);
869 void wxComboCtrlBase::InstallInputHandlers()
873 m_textEvtHandler
= new wxComboBoxExtraInputHandler(this);
874 m_text
->PushEventHandler(m_textEvtHandler
);
879 wxComboCtrlBase::CreateTextCtrl(int style
, const wxValidator
& validator
)
881 if ( !(m_windowStyle
& wxCB_READONLY
) )
886 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
887 // not used by the wxPropertyGrid and therefore the tab is processed by
888 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
889 // navigation event is then sent to the wrong window.
890 style
|= wxTE_PROCESS_TAB
;
892 if ( HasFlag(wxTE_PROCESS_ENTER
) )
893 style
|= wxTE_PROCESS_ENTER
;
895 // Ignore EVT_TEXT generated by the constructor (but only
896 // if the event redirector already exists)
897 // NB: This must be " = 1" instead of "++";
898 if ( m_textEvtHandler
)
903 m_text
= new wxComboCtrlTextCtrl();
904 m_text
->Create(this, wxID_ANY
, m_valueString
,
905 wxDefaultPosition
, wxSize(10,-1),
907 m_text
->SetHint(m_hintText
);
911 void wxComboCtrlBase::OnThemeChange()
913 // Leave the default bg on the Mac so the area used by the focus ring will
914 // be the correct colour and themed brush. Instead we'll use
915 // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed.
917 #if defined(__WXMSW__) || defined(__WXGTK__)
918 wxVisualAttributes vattrs
= wxComboBox::GetClassDefaultAttributes();
920 wxVisualAttributes vattrs
;
921 vattrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
922 vattrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
925 // Only change the colours if application has not specified
929 SetOwnForegroundColour(vattrs
.colFg
);
934 SetOwnBackgroundColour(vattrs
.colBg
);
940 wxComboCtrlBase::~wxComboCtrlBase()
945 #if INSTALL_TOPLEV_HANDLER
946 delete ((wxComboFrameEventHandler
*)m_toplevEvtHandler
);
947 m_toplevEvtHandler
= NULL
;
953 m_text
->RemoveEventHandler(m_textEvtHandler
);
955 delete m_textEvtHandler
;
959 // ----------------------------------------------------------------------------
961 // ----------------------------------------------------------------------------
963 // Recalculates button and textctrl areas
964 void wxComboCtrlBase::CalculateAreas( int btnWidth
)
966 wxSize sz
= GetClientSize();
967 int customBorder
= m_widthCustomBorder
;
968 int btnBorder
; // border for button only
970 // check if button should really be outside the border: we'll do it it if
971 // its platform default or bitmap+pushbutton background is used, but not if
972 // there is vertical size adjustment or horizontal spacing.
973 if ( ( (m_iFlags
& wxCC_BUTTON_OUTSIDE_BORDER
) ||
974 (m_bmpNormal
.Ok() && m_blankButtonBg
) ) &&
975 m_btnSpacingX
== 0 &&
978 m_iFlags
|= wxCC_IFLAG_BUTTON_OUTSIDE
;
981 else if ( (m_iFlags
& wxCC_BUTTON_COVERS_BORDER
) &&
982 m_btnSpacingX
== 0 && !m_bmpNormal
.Ok() )
984 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
989 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
990 btnBorder
= customBorder
;
993 // Defaul indentation
994 if ( m_marginLeft
< 0 )
995 m_marginLeft
= GetNativeTextIndent();
997 int butWidth
= btnWidth
;
1000 butWidth
= m_btnWidDefault
;
1002 m_btnWidDefault
= butWidth
;
1004 if ( butWidth
<= 0 )
1007 int butHeight
= sz
.y
- btnBorder
*2;
1009 // Adjust button width
1011 butWidth
= m_btnWid
;
1014 // Adjust button width to match aspect ratio
1015 // (but only if control is smaller than best size).
1016 int bestHeight
= GetBestSize().y
;
1017 int height
= GetSize().y
;
1019 if ( height
< bestHeight
)
1021 // Make very small buttons square, as it makes
1022 // them accommodate arrow image better and still
1025 butWidth
= (height
*butWidth
)/bestHeight
;
1027 butWidth
= butHeight
;
1031 // Adjust button height
1033 butHeight
= m_btnHei
;
1035 // Use size of normal bitmap if...
1038 // button width is set to default and blank button bg is not drawn
1039 if ( m_bmpNormal
.Ok() )
1041 int bmpReqWidth
= m_bmpNormal
.GetWidth();
1042 int bmpReqHeight
= m_bmpNormal
.GetHeight();
1044 // If drawing blank button background, we need to add some margin.
1045 if ( m_blankButtonBg
)
1047 bmpReqWidth
+= BMP_BUTTON_MARGIN
*2;
1048 bmpReqHeight
+= BMP_BUTTON_MARGIN
*2;
1051 if ( butWidth
< bmpReqWidth
|| ( m_btnWid
== 0 && !m_blankButtonBg
) )
1052 butWidth
= bmpReqWidth
;
1053 if ( butHeight
< bmpReqHeight
|| ( m_btnHei
== 0 && !m_blankButtonBg
) )
1054 butHeight
= bmpReqHeight
;
1056 // Need to fix height?
1057 if ( (sz
.y
-(customBorder
*2)) < butHeight
&& btnWidth
== 0 )
1059 int newY
= butHeight
+(customBorder
*2);
1060 SetClientSize(wxDefaultCoord
,newY
);
1061 if ( m_bmpNormal
.Ok() || m_btnArea
.width
!= butWidth
|| m_btnArea
.height
!= butHeight
)
1062 m_iFlags
|= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1064 m_iFlags
&= ~wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1070 int butAreaWid
= butWidth
+ (m_btnSpacingX
*2);
1072 m_btnSize
.x
= butWidth
;
1073 m_btnSize
.y
= butHeight
;
1075 m_btnArea
.x
= ( m_btnSide
==wxRIGHT
? sz
.x
- butAreaWid
- btnBorder
: btnBorder
);
1076 m_btnArea
.y
= btnBorder
+ FOCUS_RING
;
1077 m_btnArea
.width
= butAreaWid
;
1078 m_btnArea
.height
= sz
.y
- ((btnBorder
+FOCUS_RING
)*2);
1080 m_tcArea
.x
= ( m_btnSide
==wxRIGHT
? 0 : butAreaWid
) + customBorder
+ FOCUS_RING
;
1081 m_tcArea
.y
= customBorder
+ FOCUS_RING
;
1082 m_tcArea
.width
= sz
.x
- butAreaWid
- (customBorder
*2) - (FOCUS_RING
*2);
1083 m_tcArea
.height
= sz
.y
- ((customBorder
+FOCUS_RING
)*2);
1088 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
1089 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1094 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
)
1099 wxSize sz
= GetClientSize();
1101 int customBorder
= m_widthCustomBorder
;
1102 if ( (m_text
->GetWindowStyleFlag() & wxBORDER_MASK
) == wxNO_BORDER
)
1106 if ( !m_widthCustomPaint
)
1108 // No special custom paint area - we can use 0 left margin
1110 if ( m_text
->SetMargins(0) )
1111 textCtrlXAdjust
= 0;
1112 x
= m_tcArea
.x
+ m_marginLeft
+ textCtrlXAdjust
;
1116 // There is special custom paint area - it is better to
1117 // use some margin with the wxTextCtrl.
1118 m_text
->SetMargins(m_marginLeft
);
1119 x
= m_tcArea
.x
+ m_widthCustomPaint
+
1120 m_marginLeft
+ textCtrlXAdjust
;
1123 // Centre textctrl vertically, if needed
1124 #if !TEXTCTRL_TEXT_CENTERED
1125 int tcSizeY
= m_text
->GetBestSize().y
;
1126 int diff0
= sz
.y
- tcSizeY
;
1127 int y
= textCtrlYAdjust
+ (diff0
/2);
1129 wxUnusedVar(textCtrlYAdjust
);
1133 if ( y
< customBorder
)
1138 m_tcArea
.width
- m_tcArea
.x
- x
,
1141 // Make sure textctrl doesn't exceed the bottom custom border
1142 wxSize tsz
= m_text
->GetSize();
1143 int diff1
= (y
+ tsz
.y
) - (sz
.y
- customBorder
);
1146 tsz
.y
= tsz
.y
- diff1
- 1;
1147 m_text
->SetSize(tsz
);
1152 // If it has border, have textctrl fill the entire text field.
1153 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
,
1155 m_tcArea
.width
- m_widthCustomPaint
,
1160 wxSize
wxComboCtrlBase::DoGetBestSize() const
1162 wxSize
sizeText(150,0);
1165 sizeText
= m_text
->GetBestSize();
1167 // TODO: Better method to calculate close-to-native control height.
1171 fhei
= (m_font
.GetPointSize()*2) + 5;
1172 else if ( wxNORMAL_FONT
->Ok() )
1173 fhei
= (wxNORMAL_FONT
->GetPointSize()*2) + 5;
1175 fhei
= sizeText
.y
+ 4;
1177 // Need to force height to accomodate bitmap?
1178 int btnSizeY
= m_btnSize
.y
;
1179 if ( m_bmpNormal
.Ok() && fhei
< btnSizeY
)
1182 // Control height doesn't depend on border
1185 int border = m_windowStyle & wxBORDER_MASK;
1186 if ( border == wxSIMPLE_BORDER )
1188 else if ( border == wxNO_BORDER )
1189 fhei += (m_widthCustomBorder*2);
1195 // Final adjustments
1201 // these are the numbers from the HIG:
1202 switch ( m_windowVariant
)
1204 case wxWINDOW_VARIANT_NORMAL
:
1208 case wxWINDOW_VARIANT_SMALL
:
1211 case wxWINDOW_VARIANT_MINI
:
1217 fhei
+= 2 * FOCUS_RING
;
1218 int width
= sizeText
.x
+ FOCUS_RING
+ COMBO_MARGIN
+ DEFAULT_DROPBUTTON_WIDTH
;
1220 wxSize
ret(width
, fhei
);
1225 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent
& event
)
1230 // defined by actual wxComboCtrls
1236 // ----------------------------------------------------------------------------
1237 // standard operations
1238 // ----------------------------------------------------------------------------
1240 bool wxComboCtrlBase::Enable(bool enable
)
1242 if ( !wxControl::Enable(enable
) )
1246 m_btn
->Enable(enable
);
1248 m_text
->Enable(enable
);
1255 bool wxComboCtrlBase::Show(bool show
)
1257 if ( !wxControl::Show(show
) )
1269 bool wxComboCtrlBase::SetFont ( const wxFont
& font
)
1271 if ( !wxControl::SetFont(font
) )
1276 // Without hiding the wxTextCtrl there would be some
1277 // visible 'flicker' (at least on Windows XP).
1279 m_text
->SetFont(font
);
1288 void wxComboCtrlBase::DoSetToolTip(wxToolTip
*tooltip
)
1290 wxControl::DoSetToolTip(tooltip
);
1292 // Set tool tip for button and text box
1295 const wxString
&tip
= tooltip
->GetTip();
1296 if ( m_text
) m_text
->SetToolTip(tip
);
1297 if ( m_btn
) m_btn
->SetToolTip(tip
);
1301 if ( m_text
) m_text
->SetToolTip( NULL
);
1302 if ( m_btn
) m_btn
->SetToolTip( NULL
);
1305 #endif // wxUSE_TOOLTIPS
1307 #if wxUSE_VALIDATORS
1308 void wxComboCtrlBase::SetValidator(const wxValidator
& validator
)
1310 wxTextCtrl
* textCtrl
= GetTextCtrl();
1313 textCtrl
->SetValidator( validator
);
1315 wxControl::SetValidator( validator
);
1318 wxValidator
* wxComboCtrlBase::GetValidator()
1320 wxTextCtrl
* textCtrl
= GetTextCtrl();
1322 return textCtrl
? textCtrl
->GetValidator() : wxControl::GetValidator();
1324 #endif // wxUSE_VALIDATORS
1326 bool wxComboCtrlBase::SetForegroundColour(const wxColour
& colour
)
1328 if ( wxControl::SetForegroundColour(colour
) )
1331 m_text
->SetForegroundColour(colour
);
1337 bool wxComboCtrlBase::SetBackgroundColour(const wxColour
& colour
)
1339 if ( wxControl::SetBackgroundColour(colour
) )
1342 m_text
->SetBackgroundColour(colour
);
1347 // ----------------------------------------------------------------------------
1349 // ----------------------------------------------------------------------------
1351 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1352 // prepare combo box background on area in a way typical on platform
1353 void wxComboCtrlBase::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
1355 wxSize sz
= GetClientSize();
1357 bool doDrawFocusRect
; // also selected
1359 // For smaller size control (and for disabled background) use less spacing
1363 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1366 isEnabled
= IsEnabled();
1367 doDrawFocusRect
= ShouldDrawFocus() && !(m_iFlags
& wxCC_FULL_BUTTON
);
1369 // Windows-style: for smaller size control (and for disabled background) use less spacing
1370 focusSpacingX
= isEnabled
? 2 : 1;
1371 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
1375 // Drawing a list item
1376 isEnabled
= true; // they are never disabled
1377 doDrawFocusRect
= (flags
& wxCONTROL_SELECTED
) != 0;
1383 // Set the background sub-rectangle for selection, disabled etc
1384 wxRect
selRect(rect
);
1385 selRect
.y
+= focusSpacingY
;
1386 selRect
.height
-= (focusSpacingY
*2);
1390 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1391 wcp
+= m_widthCustomPaint
;
1393 selRect
.x
+= wcp
+ focusSpacingX
;
1394 selRect
.width
-= wcp
+ (focusSpacingX
*2);
1399 bool doDrawSelRect
= true;
1401 // Determine foreground colour
1404 if ( doDrawFocusRect
)
1406 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1408 else if ( m_hasFgCol
)
1410 // Honour the custom foreground colour
1411 fgCol
= GetForegroundColour();
1415 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
1420 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
);
1423 // Determine background colour
1426 if ( doDrawFocusRect
)
1428 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1430 else if ( m_hasBgCol
)
1432 // Honour the custom background colour
1433 bgCol
= GetBackgroundColour();
1437 #ifndef __WXMAC__ // see note in OnThemeChange
1438 doDrawSelRect
= false;
1439 bgCol
= GetBackgroundColour();
1441 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1447 #ifndef __WXMAC__ // see note in OnThemeChange
1448 bgCol
= GetBackgroundColour();
1450 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1454 dc
.SetTextForeground( fgCol
);
1455 dc
.SetBrush( bgCol
);
1456 if ( doDrawSelRect
)
1459 dc
.DrawRectangle( selRect
);
1462 // Don't clip exactly to the selection rectangle so we can draw
1463 // to the non-selected area in front of it.
1464 wxRect
clipRect(rect
.x
,rect
.y
,
1465 (selRect
.x
+selRect
.width
)-rect
.x
,rect
.height
);
1466 dc
.SetClippingRegion(clipRect
);
1469 // Save the library size a bit for platforms that re-implement this.
1470 void wxComboCtrlBase::PrepareBackground( wxDC
&, const wxRect
&, int ) const
1475 void wxComboCtrlBase::DrawButton( wxDC
& dc
, const wxRect
& rect
, int flags
)
1477 int drawState
= m_btnState
;
1479 if ( (m_iFlags
& wxCC_BUTTON_STAYS_DOWN
) &&
1480 GetPopupWindowState() >= Animating
)
1481 drawState
|= wxCONTROL_PRESSED
;
1483 wxRect
drawRect(rect
.x
+m_btnSpacingX
,
1484 rect
.y
+((rect
.height
-m_btnSize
.y
)/2),
1488 // Make sure area is not larger than the control
1489 if ( drawRect
.y
< rect
.y
)
1490 drawRect
.y
= rect
.y
;
1491 if ( drawRect
.height
> rect
.height
)
1492 drawRect
.height
= rect
.height
;
1494 bool enabled
= IsEnabled();
1497 drawState
|= wxCONTROL_DISABLED
;
1499 if ( !m_bmpNormal
.Ok() )
1501 if ( flags
& Button_BitmapOnly
)
1504 // Need to clear button background even if m_btn is present
1505 if ( flags
& Button_PaintBackground
)
1509 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
1510 bgCol
= GetParent()->GetBackgroundColour();
1512 bgCol
= GetBackgroundColour();
1516 dc
.DrawRectangle(rect
);
1519 // Draw standard button
1520 wxRendererNative::Get().DrawComboBoxDropButton(this,
1532 pBmp
= &m_bmpDisabled
;
1533 else if ( m_btnState
& wxCONTROL_PRESSED
)
1534 pBmp
= &m_bmpPressed
;
1535 else if ( m_btnState
& wxCONTROL_CURRENT
)
1538 pBmp
= &m_bmpNormal
;
1540 if ( m_blankButtonBg
)
1542 // If using blank button background, we need to clear its background
1543 // with button face colour instead of colour for rest of the control.
1544 if ( flags
& Button_PaintBackground
)
1546 wxColour bgCol
= GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1547 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1550 dc
.DrawRectangle(rect
);
1553 if ( !(flags
& Button_BitmapOnly
) )
1555 wxRendererNative::Get().DrawPushButton(this,
1564 // Need to clear button background even if m_btn is present
1565 // (assume non-button background was cleared just before this call so brushes are good)
1566 if ( flags
& Button_PaintBackground
)
1567 dc
.DrawRectangle(rect
);
1570 // Draw bitmap centered in drawRect
1571 dc
.DrawBitmap(*pBmp
,
1572 drawRect
.x
+ (drawRect
.width
-pBmp
->GetWidth())/2,
1573 drawRect
.y
+ (drawRect
.height
-pBmp
->GetHeight())/2,
1578 void wxComboCtrlBase::RecalcAndRefresh()
1582 wxSizeEvent
evt(GetSize(),GetId());
1583 GetEventHandler()->ProcessEvent(evt
);
1588 // ----------------------------------------------------------------------------
1589 // miscellaneous event handlers
1590 // ----------------------------------------------------------------------------
1592 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent
& event
)
1594 if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1596 if ( m_ignoreEvtText
> 0 )
1603 // Change event id, object and string before relaying it forward
1604 event
.SetId(GetId());
1605 wxString s
= event
.GetString();
1606 event
.SetEventObject(this);
1611 // call if cursor is on button area or mouse is captured for the button
1612 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent
& event
,
1615 int type
= event
.GetEventType();
1617 if ( type
== wxEVT_MOTION
)
1619 if ( (flags
& wxCC_MF_ON_BUTTON
) &&
1620 IsPopupWindowState(Hidden
) )
1622 if ( !(m_btnState
& wxCONTROL_CURRENT
) )
1624 // Mouse hover begins
1625 m_btnState
|= wxCONTROL_CURRENT
;
1626 if ( HasCapture() ) // Retain pressed state.
1627 m_btnState
|= wxCONTROL_PRESSED
;
1631 else if ( (m_btnState
& wxCONTROL_CURRENT
) )
1634 m_btnState
&= ~(wxCONTROL_CURRENT
|wxCONTROL_PRESSED
);
1638 else if ( type
== wxEVT_LEFT_DOWN
|| type
== wxEVT_LEFT_DCLICK
)
1640 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1642 m_btnState
|= wxCONTROL_PRESSED
;
1645 if ( !(m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
) )
1648 // If showing popup now, do not capture mouse or there will be interference
1652 else if ( type
== wxEVT_LEFT_UP
)
1655 // Only accept event if mouse was left-press was previously accepted
1659 if ( m_btnState
& wxCONTROL_PRESSED
)
1661 // If mouse was inside, fire the click event.
1662 if ( m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
)
1664 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1668 m_btnState
&= ~(wxCONTROL_PRESSED
);
1672 else if ( type
== wxEVT_LEAVE_WINDOW
)
1674 if ( m_btnState
& (wxCONTROL_CURRENT
|wxCONTROL_PRESSED
) )
1676 m_btnState
&= ~(wxCONTROL_CURRENT
);
1679 if ( IsPopupWindowState(Hidden
) )
1681 m_btnState
&= ~(wxCONTROL_PRESSED
);
1689 // Never have 'hot' state when popup is being shown
1690 // (this is mostly needed because of the animation).
1691 if ( !IsPopupWindowState(Hidden
) )
1692 m_btnState
&= ~wxCONTROL_CURRENT
;
1697 // returns true if event was consumed or filtered
1698 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent
& event
,
1699 int WXUNUSED(flags
) )
1701 wxLongLong t
= ::wxGetLocalTimeMillis();
1702 int evtType
= event
.GetEventType();
1704 #if USES_WXPOPUPWINDOW || USES_GENERICTLW
1705 if ( m_popupWinType
!= POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1707 if ( IsPopupWindowState(Visible
) &&
1708 ( evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_RIGHT_DOWN
) )
1716 // Filter out clicks on button immediately after popup dismiss
1717 if ( evtType
== wxEVT_LEFT_DOWN
&& t
< m_timeCanAcceptClick
)
1719 event
.SetEventType(0);
1726 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent
& event
)
1728 int evtType
= event
.GetEventType();
1730 if ( (evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_LEFT_DCLICK
) &&
1731 (m_windowStyle
& wxCB_READONLY
) )
1733 if ( GetPopupWindowState() >= Animating
)
1735 #if USES_WXPOPUPWINDOW
1736 // Click here always hides the popup.
1737 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1743 if ( !(m_windowStyle
& wxCC_SPECIAL_DCLICK
) )
1745 // In read-only mode, clicking the text is the
1746 // same as clicking the button.
1749 else if ( /*evtType == wxEVT_LEFT_UP || */evtType
== wxEVT_LEFT_DCLICK
)
1751 //if ( m_popupInterface->CycleValue() )
1753 if ( m_popupInterface
)
1754 m_popupInterface
->OnComboDoubleClick();
1759 if ( IsPopupShown() )
1761 // relay (some) mouse events to the popup
1762 if ( evtType
== wxEVT_MOUSEWHEEL
)
1763 m_popup
->GetEventHandler()->AddPendingEvent(event
);
1769 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent
& event
)
1771 if ( IsPopupShown() )
1773 // pass it to the popped up control
1774 GetPopupControl()->GetControl()->GetEventHandler()->AddPendingEvent(event
);
1778 if ( GetParent()->HasFlag(wxTAB_TRAVERSAL
) &&
1779 HandleAsNavigationKey(event
) )
1782 if ( IsKeyPopupToggle(event
) )
1788 int comboStyle
= GetWindowStyle();
1789 wxComboPopup
* popupInterface
= GetPopupControl();
1791 if ( !popupInterface
)
1797 int keycode
= event
.GetKeyCode();
1799 if ( (comboStyle
& wxCB_READONLY
) ||
1800 (keycode
!= WXK_RIGHT
&& keycode
!= WXK_LEFT
) )
1802 popupInterface
->OnComboKeyEvent(event
);
1809 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent
& event
)
1811 if ( event
.GetEventType() == wxEVT_SET_FOCUS
)
1813 wxWindow
* tc
= GetTextCtrl();
1814 if ( tc
&& tc
!= DoFindFocus() )
1823 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent
& WXUNUSED(event
) )
1827 m_resetFocus
= false;
1828 wxWindow
* tc
= GetTextCtrl();
1834 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
1837 // left margin may also have changed
1838 if ( !(m_iFlags
& wxCC_IFLAG_LEFT_MARGIN_SET
) )
1839 m_marginLeft
= GetNativeTextIndent();
1843 // ----------------------------------------------------------------------------
1845 // ----------------------------------------------------------------------------
1847 // Create popup window and the child control
1848 void wxComboCtrlBase::CreatePopup()
1850 wxComboPopup
* popupInterface
= m_popupInterface
;
1855 #ifdef wxComboPopupWindowBase2
1856 if ( m_iFlags
& wxCC_IFLAG_USE_ALT_POPUP
)
1858 #if !USES_GENERICTLW
1859 m_winPopup
= new wxComboPopupWindowBase2( this, wxNO_BORDER
);
1861 int tlwFlags
= wxNO_BORDER
;
1862 #ifdef wxCC_GENERIC_TLW_IS_FRAME
1863 tlwFlags
|= wxFRAME_NO_TASKBAR
;
1866 #ifdef wxCC_GENERIC_TLW_IS_NONOWNEDWINDOW
1867 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
,
1868 wxPoint(-21,-21), wxSize(20, 20),
1871 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
, wxEmptyString
,
1872 wxPoint(-21,-21), wxSize(20, 20),
1876 m_popupWinType
= SECONDARY_POPUP_TYPE
;
1879 #endif // wxComboPopupWindowBase2
1881 m_winPopup
= new wxComboPopupWindow( this, wxNO_BORDER
);
1882 m_popupWinType
= PRIMARY_POPUP_TYPE
;
1884 m_popupWinEvtHandler
= new wxComboPopupWindowEvtHandler(this);
1885 m_winPopup
->PushEventHandler(m_popupWinEvtHandler
);
1888 popupInterface
->Create(m_winPopup
);
1889 m_popup
= popup
= popupInterface
->GetControl();
1891 m_popupExtraHandler
= new wxComboPopupExtraEventHandler(this);
1892 popup
->PushEventHandler( m_popupExtraHandler
);
1894 // This may be helpful on some platforms
1895 // (eg. it bypasses a wxGTK popupwindow bug where
1896 // window is not initially hidden when it should be)
1899 popupInterface
->m_iFlags
|= wxCP_IFLAG_CREATED
;
1902 // Destroy popup window and the child control
1903 void wxComboCtrlBase::DestroyPopup()
1908 m_popup
->RemoveEventHandler(m_popupExtraHandler
);
1910 delete m_popupExtraHandler
;
1912 delete m_popupInterface
;
1916 m_winPopup
->RemoveEventHandler(m_popupWinEvtHandler
);
1917 delete m_popupWinEvtHandler
;
1918 m_popupWinEvtHandler
= NULL
;
1919 m_winPopup
->Destroy();
1922 m_popupExtraHandler
= NULL
;
1923 m_popupInterface
= NULL
;
1928 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup
* iface
)
1930 wxCHECK_RET( iface
, wxT("no popup interface set for wxComboCtrl") );
1934 iface
->InitBase(this);
1937 m_popupInterface
= iface
;
1939 if ( !iface
->LazyCreate() )
1948 // This must be done after creation
1949 if ( m_valueString
.length() )
1951 iface
->SetStringValue(m_valueString
);
1956 // Ensures there is atleast the default popup
1957 void wxComboCtrlBase::EnsurePopupControl()
1959 if ( !m_popupInterface
)
1960 SetPopupControl(NULL
);
1963 void wxComboCtrlBase::OnButtonClick()
1965 // Derived classes can override this method for totally custom
1967 if ( !IsPopupWindowState(Visible
) )
1969 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_DROPDOWN
, GetId());
1970 event
.SetEventObject(this);
1971 HandleWindowEvent(event
);
1981 void wxComboCtrlBase::ShowPopup()
1983 EnsurePopupControl();
1984 wxCHECK_RET( !IsPopupWindowState(Visible
), wxT("popup window already shown") );
1986 if ( IsPopupWindowState(Animating
) )
1991 // Space above and below
1997 wxSize ctrlSz
= GetSize();
1999 screenHeight
= wxSystemSettings::GetMetric( wxSYS_SCREEN_Y
);
2000 scrPos
= GetParent()->ClientToScreen(GetPosition());
2002 spaceAbove
= scrPos
.y
;
2003 spaceBelow
= screenHeight
- spaceAbove
- ctrlSz
.y
;
2005 maxHeightPopup
= spaceBelow
;
2006 if ( spaceAbove
> spaceBelow
)
2007 maxHeightPopup
= spaceAbove
;
2010 int widthPopup
= ctrlSz
.x
+ m_extLeft
+ m_extRight
;
2012 if ( widthPopup
< m_widthMinPopup
)
2013 widthPopup
= m_widthMinPopup
;
2015 wxWindow
* winPopup
= m_winPopup
;
2018 // Need to disable tab traversal of parent
2020 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
2021 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
2022 // that if transient popup is open, then tab traversal is to be ignored.
2023 // However, I think this code would still be needed for cases where
2024 // transient popup doesn't work yet (wxWinCE?).
2025 wxWindow
* parent
= GetParent();
2026 int parentFlags
= parent
->GetWindowStyle();
2027 if ( parentFlags
& wxTAB_TRAVERSAL
)
2029 parent
->SetWindowStyle( parentFlags
& ~(wxTAB_TRAVERSAL
) );
2030 m_iFlags
|= wxCC_IFLAG_PARENT_TAB_TRAVERSAL
;
2036 winPopup
= m_winPopup
;
2046 wxASSERT( !m_popup
|| m_popup
== popup
); // Consistency check.
2048 wxSize adjustedSize
= m_popupInterface
->GetAdjustedSize(widthPopup
,
2049 m_heightPopup
<=0?DEFAULT_POPUP_HEIGHT
:m_heightPopup
,
2052 popup
->SetSize(adjustedSize
);
2054 m_popupInterface
->OnPopup();
2057 // Reposition and resize popup window
2060 wxSize szp
= popup
->GetSize();
2063 int popupY
= scrPos
.y
+ ctrlSz
.y
;
2065 // Default anchor is wxLEFT
2066 int anchorSide
= m_anchorSide
;
2068 anchorSide
= wxLEFT
;
2070 int rightX
= scrPos
.x
+ ctrlSz
.x
+ m_extRight
- szp
.x
;
2071 int leftX
= scrPos
.x
- m_extLeft
;
2073 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
2076 int screenWidth
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
);
2078 // If there is not enough horizontal space, anchor on the other side.
2079 // If there is no space even then, place the popup at x 0.
2080 if ( anchorSide
== wxRIGHT
)
2084 if ( (leftX
+szp
.x
) < screenWidth
)
2085 anchorSide
= wxLEFT
;
2092 if ( (leftX
+szp
.x
) >= screenWidth
)
2095 anchorSide
= wxRIGHT
;
2101 // Select x coordinate according to the anchor side
2102 if ( anchorSide
== wxRIGHT
)
2104 else if ( anchorSide
== wxLEFT
)
2109 int showFlags
= CanDeferShow
;
2111 if ( spaceBelow
< szp
.y
)
2113 popupY
= scrPos
.y
- szp
.y
;
2114 showFlags
|= ShowAbove
;
2117 #if INSTALL_TOPLEV_HANDLER
2118 // Put top level window event handler into place
2119 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
2121 if ( !m_toplevEvtHandler
)
2122 m_toplevEvtHandler
= new wxComboFrameEventHandler(this);
2124 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2126 ((wxComboFrameEventHandler
*)m_toplevEvtHandler
)->OnPopup();
2127 toplev
->PushEventHandler( m_toplevEvtHandler
);
2131 // Set string selection (must be this way instead of SetStringSelection)
2134 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2135 m_text
->SelectAll();
2137 m_popupInterface
->SetStringValue( m_text
->GetValue() );
2141 // This is neede since focus/selection indication may change when popup is shown
2145 // This must be after SetStringValue
2146 m_popupWinState
= Animating
;
2148 wxRect
popupWinRect( popupX
, popupY
, szp
.x
, szp
.y
);
2151 if ( (m_iFlags
& wxCC_IFLAG_DISABLE_POPUP_ANIM
) ||
2152 AnimateShow( popupWinRect
, showFlags
) )
2154 DoShowPopup( popupWinRect
, showFlags
);
2158 bool wxComboCtrlBase::AnimateShow( const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
) )
2163 void wxComboCtrlBase::DoShowPopup( const wxRect
& rect
, int WXUNUSED(flags
) )
2165 wxWindow
* winPopup
= m_winPopup
;
2167 if ( IsPopupWindowState(Animating
) )
2169 // Make sure the popup window is shown in the right position.
2170 // Should not matter even if animation already did this.
2172 // Some platforms (GTK) may like SetSize and Move to be separate
2173 // (though the bug was probably fixed).
2174 winPopup
->SetSize( rect
);
2176 #if USES_WXPOPUPTRANSIENTWINDOW
2177 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2178 ((wxPopupTransientWindow
*)winPopup
)->Popup(m_popup
);
2183 m_popupWinState
= Visible
;
2185 else if ( IsPopupWindowState(Hidden
) )
2187 // Animation was aborted
2189 wxASSERT( !winPopup
->IsShown() );
2191 m_popupWinState
= Hidden
;
2197 void wxComboCtrlBase::OnPopupDismiss(bool generateEvent
)
2199 // Just in case, avoid double dismiss
2200 if ( IsPopupWindowState(Hidden
) )
2203 // This must be set before focus - otherwise there will be recursive
2204 // OnPopupDismisses.
2205 m_popupWinState
= Hidden
;
2208 m_winPopup
->Disable();
2210 // Inform popup control itself
2211 m_popupInterface
->OnDismiss();
2213 if ( m_popupExtraHandler
)
2214 ((wxComboPopupExtraEventHandler
*)m_popupExtraHandler
)->OnPopupDismiss();
2216 #if INSTALL_TOPLEV_HANDLER
2217 // Remove top level window event handler
2218 if ( m_toplevEvtHandler
)
2220 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2222 toplev
->RemoveEventHandler( m_toplevEvtHandler
);
2226 m_timeCanAcceptClick
= ::wxGetLocalTimeMillis();
2228 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2229 m_timeCanAcceptClick
+= 150;
2231 // If cursor not on dropdown button, then clear its state
2232 // (technically not required by all ports, but do it for all just in case)
2233 if ( !m_btnArea
.Contains(ScreenToClient(::wxGetMousePosition())) )
2236 // Return parent's tab traversal flag.
2237 // See ShowPopup for notes.
2238 if ( m_iFlags
& wxCC_IFLAG_PARENT_TAB_TRAVERSAL
)
2240 wxWindow
* parent
= GetParent();
2241 parent
->SetWindowStyle( parent
->GetWindowStyle() | wxTAB_TRAVERSAL
);
2242 m_iFlags
&= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL
);
2245 // refresh control (necessary even if m_text)
2250 if ( generateEvent
)
2252 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_CLOSEUP
, GetId());
2253 event
.SetEventObject(this);
2254 HandleWindowEvent(event
);
2258 void wxComboCtrlBase::HidePopup(bool generateEvent
)
2260 // Should be able to call this without popup interface
2261 if ( IsPopupWindowState(Hidden
) )
2264 // transfer value and show it in textctrl, if any
2265 if ( !IsPopupWindowState(Animating
) )
2266 SetValue( m_popupInterface
->GetStringValue() );
2270 OnPopupDismiss(generateEvent
);
2273 // ----------------------------------------------------------------------------
2274 // customization methods
2275 // ----------------------------------------------------------------------------
2277 void wxComboCtrlBase::SetButtonPosition( int width
, int height
,
2278 int side
, int spacingX
)
2283 m_btnSpacingX
= spacingX
;
2288 wxSize
wxComboCtrlBase::GetButtonSize()
2290 if ( m_btnSize
.x
> 0 )
2293 wxSize
retSize(m_btnWid
,m_btnHei
);
2295 // Need to call CalculateAreas now if button size is
2296 // is not explicitly specified.
2297 if ( retSize
.x
<= 0 || retSize
.y
<= 0)
2301 retSize
= m_btnSize
;
2307 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap
& bmpNormal
,
2309 const wxBitmap
& bmpPressed
,
2310 const wxBitmap
& bmpHover
,
2311 const wxBitmap
& bmpDisabled
)
2313 m_bmpNormal
= bmpNormal
;
2314 m_blankButtonBg
= blankButtonBg
;
2316 if ( bmpPressed
.Ok() )
2317 m_bmpPressed
= bmpPressed
;
2319 m_bmpPressed
= bmpNormal
;
2321 if ( bmpHover
.Ok() )
2322 m_bmpHover
= bmpHover
;
2324 m_bmpHover
= bmpNormal
;
2326 if ( bmpDisabled
.Ok() )
2327 m_bmpDisabled
= bmpDisabled
;
2329 m_bmpDisabled
= bmpNormal
;
2334 void wxComboCtrlBase::SetCustomPaintWidth( int width
)
2338 // move textctrl accordingly
2339 wxRect r
= m_text
->GetRect();
2340 int inc
= width
- m_widthCustomPaint
;
2343 m_text
->SetSize( r
);
2346 m_widthCustomPaint
= width
;
2351 bool wxComboCtrlBase::DoSetMargins(const wxPoint
& margins
)
2353 // For general sanity's sake, we ignore top margin. Instead
2354 // we will always try to center the text vertically.
2357 if ( margins
.x
!= -1 )
2359 m_marginLeft
= margins
.x
;
2360 m_iFlags
|= wxCC_IFLAG_LEFT_MARGIN_SET
;
2364 m_marginLeft
= GetNativeTextIndent();
2365 m_iFlags
&= ~(wxCC_IFLAG_LEFT_MARGIN_SET
);
2368 if ( margins
.y
!= -1 )
2378 wxPoint
wxComboCtrlBase::DoGetMargins() const
2380 return wxPoint(m_marginLeft
, -1);
2383 #if WXWIN_COMPATIBILITY_2_8
2384 void wxComboCtrlBase::SetTextIndent( int indent
)
2388 m_marginLeft
= GetNativeTextIndent();
2389 m_iFlags
&= ~(wxCC_IFLAG_LEFT_MARGIN_SET
);
2393 m_marginLeft
= indent
;
2394 m_iFlags
|= wxCC_IFLAG_LEFT_MARGIN_SET
;
2400 wxCoord
wxComboCtrlBase::GetTextIndent() const
2402 return m_marginLeft
;
2406 wxCoord
wxComboCtrlBase::GetNativeTextIndent() const
2408 return DEFAULT_TEXT_INDENT
;
2411 // ----------------------------------------------------------------------------
2412 // methods forwarded to wxTextCtrl
2413 // ----------------------------------------------------------------------------
2415 wxString
wxComboCtrlBase::GetValue() const
2418 return m_text
->GetValue();
2419 return m_valueString
;
2422 void wxComboCtrlBase::SetValueWithEvent(const wxString
& value
, bool withEvent
)
2429 m_text
->SetValue(value
);
2431 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2432 m_text
->SelectAll();
2435 // Since wxComboPopup may want to paint the combo as well, we need
2436 // to set the string value here (as well as sometimes in ShowPopup).
2437 if ( m_valueString
!= value
)
2439 m_valueString
= value
;
2441 EnsurePopupControl();
2443 if (m_popupInterface
)
2444 m_popupInterface
->SetStringValue(value
);
2450 void wxComboCtrlBase::SetValue(const wxString
& value
)
2452 SetValueWithEvent(value
, false);
2455 // In this SetValue variant wxComboPopup::SetStringValue is not called
2456 void wxComboCtrlBase::SetText(const wxString
& value
)
2458 // Unlike in SetValue(), this must be called here or
2459 // the behaviour will no be consistent in readonlys.
2460 EnsurePopupControl();
2462 m_valueString
= value
;
2467 m_text
->SetValue( value
);
2473 void wxComboCtrlBase::Copy()
2479 void wxComboCtrlBase::Cut()
2485 void wxComboCtrlBase::Paste()
2491 void wxComboCtrlBase::SetInsertionPoint(long pos
)
2494 m_text
->SetInsertionPoint(pos
);
2497 void wxComboCtrlBase::SetInsertionPointEnd()
2500 m_text
->SetInsertionPointEnd();
2503 long wxComboCtrlBase::GetInsertionPoint() const
2506 return m_text
->GetInsertionPoint();
2511 long wxComboCtrlBase::GetLastPosition() const
2514 return m_text
->GetLastPosition();
2519 void wxComboCtrlBase::Replace(long from
, long to
, const wxString
& value
)
2522 m_text
->Replace(from
, to
, value
);
2525 void wxComboCtrlBase::Remove(long from
, long to
)
2528 m_text
->Remove(from
, to
);
2531 void wxComboCtrlBase::SetSelection(long from
, long to
)
2534 m_text
->SetSelection(from
, to
);
2537 void wxComboCtrlBase::Undo()
2543 bool wxComboCtrlBase::SetHint(const wxString
& hint
)
2548 res
= m_text
->SetHint(hint
);
2553 wxString
wxComboCtrlBase::GetHint() const
2558 #endif // wxUSE_COMBOCTRL