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),
910 void wxComboCtrlBase::OnThemeChange()
912 // Leave the default bg on the Mac so the area used by the focus ring will
913 // be the correct colour and themed brush. Instead we'll use
914 // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed.
916 #if defined(__WXMSW__) || defined(__WXGTK__)
917 wxVisualAttributes vattrs
= wxComboBox::GetClassDefaultAttributes();
919 wxVisualAttributes vattrs
;
920 vattrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
921 vattrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
924 // Only change the colours if application has not specified
928 SetOwnForegroundColour(vattrs
.colFg
);
933 SetOwnBackgroundColour(vattrs
.colBg
);
939 wxComboCtrlBase::~wxComboCtrlBase()
944 #if INSTALL_TOPLEV_HANDLER
945 delete ((wxComboFrameEventHandler
*)m_toplevEvtHandler
);
946 m_toplevEvtHandler
= NULL
;
952 m_text
->RemoveEventHandler(m_textEvtHandler
);
954 delete m_textEvtHandler
;
958 // ----------------------------------------------------------------------------
960 // ----------------------------------------------------------------------------
962 // Recalculates button and textctrl areas
963 void wxComboCtrlBase::CalculateAreas( int btnWidth
)
965 wxSize sz
= GetClientSize();
966 int customBorder
= m_widthCustomBorder
;
967 int btnBorder
; // border for button only
969 // check if button should really be outside the border: we'll do it it if
970 // its platform default or bitmap+pushbutton background is used, but not if
971 // there is vertical size adjustment or horizontal spacing.
972 if ( ( (m_iFlags
& wxCC_BUTTON_OUTSIDE_BORDER
) ||
973 (m_bmpNormal
.Ok() && m_blankButtonBg
) ) &&
974 m_btnSpacingX
== 0 &&
977 m_iFlags
|= wxCC_IFLAG_BUTTON_OUTSIDE
;
980 else if ( (m_iFlags
& wxCC_BUTTON_COVERS_BORDER
) &&
981 m_btnSpacingX
== 0 && !m_bmpNormal
.Ok() )
983 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
988 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
989 btnBorder
= customBorder
;
992 // Defaul indentation
993 if ( m_marginLeft
< 0 )
994 m_marginLeft
= GetNativeTextIndent();
996 int butWidth
= btnWidth
;
999 butWidth
= m_btnWidDefault
;
1001 m_btnWidDefault
= butWidth
;
1003 if ( butWidth
<= 0 )
1006 int butHeight
= sz
.y
- btnBorder
*2;
1008 // Adjust button width
1010 butWidth
= m_btnWid
;
1013 // Adjust button width to match aspect ratio
1014 // (but only if control is smaller than best size).
1015 int bestHeight
= GetBestSize().y
;
1016 int height
= GetSize().y
;
1018 if ( height
< bestHeight
)
1020 // Make very small buttons square, as it makes
1021 // them accommodate arrow image better and still
1024 butWidth
= (height
*butWidth
)/bestHeight
;
1026 butWidth
= butHeight
;
1030 // Adjust button height
1032 butHeight
= m_btnHei
;
1034 // Use size of normal bitmap if...
1037 // button width is set to default and blank button bg is not drawn
1038 if ( m_bmpNormal
.Ok() )
1040 int bmpReqWidth
= m_bmpNormal
.GetWidth();
1041 int bmpReqHeight
= m_bmpNormal
.GetHeight();
1043 // If drawing blank button background, we need to add some margin.
1044 if ( m_blankButtonBg
)
1046 bmpReqWidth
+= BMP_BUTTON_MARGIN
*2;
1047 bmpReqHeight
+= BMP_BUTTON_MARGIN
*2;
1050 if ( butWidth
< bmpReqWidth
|| ( m_btnWid
== 0 && !m_blankButtonBg
) )
1051 butWidth
= bmpReqWidth
;
1052 if ( butHeight
< bmpReqHeight
|| ( m_btnHei
== 0 && !m_blankButtonBg
) )
1053 butHeight
= bmpReqHeight
;
1055 // Need to fix height?
1056 if ( (sz
.y
-(customBorder
*2)) < butHeight
&& btnWidth
== 0 )
1058 int newY
= butHeight
+(customBorder
*2);
1059 SetClientSize(wxDefaultCoord
,newY
);
1060 if ( m_bmpNormal
.Ok() || m_btnArea
.width
!= butWidth
|| m_btnArea
.height
!= butHeight
)
1061 m_iFlags
|= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1063 m_iFlags
&= ~wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
;
1069 int butAreaWid
= butWidth
+ (m_btnSpacingX
*2);
1071 m_btnSize
.x
= butWidth
;
1072 m_btnSize
.y
= butHeight
;
1074 m_btnArea
.x
= ( m_btnSide
==wxRIGHT
? sz
.x
- butAreaWid
- btnBorder
: btnBorder
);
1075 m_btnArea
.y
= btnBorder
+ FOCUS_RING
;
1076 m_btnArea
.width
= butAreaWid
;
1077 m_btnArea
.height
= sz
.y
- ((btnBorder
+FOCUS_RING
)*2);
1079 m_tcArea
.x
= ( m_btnSide
==wxRIGHT
? 0 : butAreaWid
) + customBorder
+ FOCUS_RING
;
1080 m_tcArea
.y
= customBorder
+ FOCUS_RING
;
1081 m_tcArea
.width
= sz
.x
- butAreaWid
- (customBorder
*2) - (FOCUS_RING
*2);
1082 m_tcArea
.height
= sz
.y
- ((customBorder
+FOCUS_RING
)*2);
1087 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
1088 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1093 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
)
1098 wxSize sz
= GetClientSize();
1100 int customBorder
= m_widthCustomBorder
;
1101 if ( (m_text
->GetWindowStyleFlag() & wxBORDER_MASK
) == wxNO_BORDER
)
1105 if ( !m_widthCustomPaint
)
1107 // No special custom paint area - we can use 0 left margin
1109 if ( m_text
->SetMargins(0) )
1110 textCtrlXAdjust
= 0;
1111 x
= m_tcArea
.x
+ m_marginLeft
+ textCtrlXAdjust
;
1115 // There is special custom paint area - it is better to
1116 // use some margin with the wxTextCtrl.
1117 m_text
->SetMargins(m_marginLeft
);
1118 x
= m_tcArea
.x
+ m_widthCustomPaint
+
1119 m_marginLeft
+ textCtrlXAdjust
;
1122 // Centre textctrl vertically, if needed
1123 #if !TEXTCTRL_TEXT_CENTERED
1124 int tcSizeY
= m_text
->GetBestSize().y
;
1125 int diff0
= sz
.y
- tcSizeY
;
1126 int y
= textCtrlYAdjust
+ (diff0
/2);
1128 wxUnusedVar(textCtrlYAdjust
);
1132 if ( y
< customBorder
)
1137 m_tcArea
.width
- m_tcArea
.x
- x
,
1140 // Make sure textctrl doesn't exceed the bottom custom border
1141 wxSize tsz
= m_text
->GetSize();
1142 int diff1
= (y
+ tsz
.y
) - (sz
.y
- customBorder
);
1145 tsz
.y
= tsz
.y
- diff1
- 1;
1146 m_text
->SetSize(tsz
);
1151 // If it has border, have textctrl fill the entire text field.
1152 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
,
1154 m_tcArea
.width
- m_widthCustomPaint
,
1159 wxSize
wxComboCtrlBase::DoGetBestSize() const
1161 wxSize
sizeText(150,0);
1164 sizeText
= m_text
->GetBestSize();
1166 // TODO: Better method to calculate close-to-native control height.
1170 fhei
= (m_font
.GetPointSize()*2) + 5;
1171 else if ( wxNORMAL_FONT
->Ok() )
1172 fhei
= (wxNORMAL_FONT
->GetPointSize()*2) + 5;
1174 fhei
= sizeText
.y
+ 4;
1176 // Need to force height to accomodate bitmap?
1177 int btnSizeY
= m_btnSize
.y
;
1178 if ( m_bmpNormal
.Ok() && fhei
< btnSizeY
)
1181 // Control height doesn't depend on border
1184 int border = m_windowStyle & wxBORDER_MASK;
1185 if ( border == wxSIMPLE_BORDER )
1187 else if ( border == wxNO_BORDER )
1188 fhei += (m_widthCustomBorder*2);
1194 // Final adjustments
1200 // these are the numbers from the HIG:
1201 switch ( m_windowVariant
)
1203 case wxWINDOW_VARIANT_NORMAL
:
1207 case wxWINDOW_VARIANT_SMALL
:
1210 case wxWINDOW_VARIANT_MINI
:
1216 fhei
+= 2 * FOCUS_RING
;
1217 int width
= sizeText
.x
+ FOCUS_RING
+ COMBO_MARGIN
+ DEFAULT_DROPBUTTON_WIDTH
;
1219 wxSize
ret(width
, fhei
);
1224 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent
& event
)
1229 // defined by actual wxComboCtrls
1235 // ----------------------------------------------------------------------------
1236 // standard operations
1237 // ----------------------------------------------------------------------------
1239 bool wxComboCtrlBase::Enable(bool enable
)
1241 if ( !wxControl::Enable(enable
) )
1245 m_btn
->Enable(enable
);
1247 m_text
->Enable(enable
);
1254 bool wxComboCtrlBase::Show(bool show
)
1256 if ( !wxControl::Show(show
) )
1268 bool wxComboCtrlBase::SetFont ( const wxFont
& font
)
1270 if ( !wxControl::SetFont(font
) )
1275 // Without hiding the wxTextCtrl there would be some
1276 // visible 'flicker' (at least on Windows XP).
1278 m_text
->SetFont(font
);
1287 void wxComboCtrlBase::DoSetToolTip(wxToolTip
*tooltip
)
1289 wxControl::DoSetToolTip(tooltip
);
1291 // Set tool tip for button and text box
1294 const wxString
&tip
= tooltip
->GetTip();
1295 if ( m_text
) m_text
->SetToolTip(tip
);
1296 if ( m_btn
) m_btn
->SetToolTip(tip
);
1300 if ( m_text
) m_text
->SetToolTip( NULL
);
1301 if ( m_btn
) m_btn
->SetToolTip( NULL
);
1304 #endif // wxUSE_TOOLTIPS
1306 #if wxUSE_VALIDATORS
1307 void wxComboCtrlBase::SetValidator(const wxValidator
& validator
)
1309 wxTextCtrl
* textCtrl
= GetTextCtrl();
1312 textCtrl
->SetValidator( validator
);
1314 wxControl::SetValidator( validator
);
1317 wxValidator
* wxComboCtrlBase::GetValidator()
1319 wxTextCtrl
* textCtrl
= GetTextCtrl();
1321 return textCtrl
? textCtrl
->GetValidator() : wxControl::GetValidator();
1323 #endif // wxUSE_VALIDATORS
1325 // ----------------------------------------------------------------------------
1327 // ----------------------------------------------------------------------------
1329 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1330 // prepare combo box background on area in a way typical on platform
1331 void wxComboCtrlBase::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
1333 wxSize sz
= GetClientSize();
1335 bool doDrawFocusRect
; // also selected
1337 // For smaller size control (and for disabled background) use less spacing
1341 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1344 isEnabled
= IsEnabled();
1345 doDrawFocusRect
= ShouldDrawFocus() && !(m_iFlags
& wxCC_FULL_BUTTON
);
1347 // Windows-style: for smaller size control (and for disabled background) use less spacing
1348 focusSpacingX
= isEnabled
? 2 : 1;
1349 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
1353 // Drawing a list item
1354 isEnabled
= true; // they are never disabled
1355 doDrawFocusRect
= (flags
& wxCONTROL_SELECTED
) != 0;
1361 // Set the background sub-rectangle for selection, disabled etc
1362 wxRect
selRect(rect
);
1363 selRect
.y
+= focusSpacingY
;
1364 selRect
.height
-= (focusSpacingY
*2);
1368 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1369 wcp
+= m_widthCustomPaint
;
1371 selRect
.x
+= wcp
+ focusSpacingX
;
1372 selRect
.width
-= wcp
+ (focusSpacingX
*2);
1377 bool doDrawSelRect
= true;
1379 // Determine foreground colour
1382 if ( doDrawFocusRect
)
1384 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1386 else if ( m_hasFgCol
)
1388 // Honour the custom foreground colour
1389 fgCol
= GetForegroundColour();
1393 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
1398 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
);
1401 // Determine background colour
1404 if ( doDrawFocusRect
)
1406 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1408 else if ( m_hasBgCol
)
1410 // Honour the custom background colour
1411 bgCol
= GetBackgroundColour();
1415 #ifndef __WXMAC__ // see note in OnThemeChange
1416 doDrawSelRect
= false;
1417 bgCol
= GetBackgroundColour();
1419 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1425 #ifndef __WXMAC__ // see note in OnThemeChange
1426 bgCol
= GetBackgroundColour();
1428 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1432 dc
.SetTextForeground( fgCol
);
1433 dc
.SetBrush( bgCol
);
1434 if ( doDrawSelRect
)
1437 dc
.DrawRectangle( selRect
);
1440 // Don't clip exactly to the selection rectangle so we can draw
1441 // to the non-selected area in front of it.
1442 wxRect
clipRect(rect
.x
,rect
.y
,
1443 (selRect
.x
+selRect
.width
)-rect
.x
,rect
.height
);
1444 dc
.SetClippingRegion(clipRect
);
1447 // Save the library size a bit for platforms that re-implement this.
1448 void wxComboCtrlBase::PrepareBackground( wxDC
&, const wxRect
&, int ) const
1453 void wxComboCtrlBase::DrawButton( wxDC
& dc
, const wxRect
& rect
, int flags
)
1455 int drawState
= m_btnState
;
1457 if ( (m_iFlags
& wxCC_BUTTON_STAYS_DOWN
) &&
1458 GetPopupWindowState() >= Animating
)
1459 drawState
|= wxCONTROL_PRESSED
;
1461 wxRect
drawRect(rect
.x
+m_btnSpacingX
,
1462 rect
.y
+((rect
.height
-m_btnSize
.y
)/2),
1466 // Make sure area is not larger than the control
1467 if ( drawRect
.y
< rect
.y
)
1468 drawRect
.y
= rect
.y
;
1469 if ( drawRect
.height
> rect
.height
)
1470 drawRect
.height
= rect
.height
;
1472 bool enabled
= IsEnabled();
1475 drawState
|= wxCONTROL_DISABLED
;
1477 if ( !m_bmpNormal
.Ok() )
1479 if ( flags
& Button_BitmapOnly
)
1482 // Need to clear button background even if m_btn is present
1483 if ( flags
& Button_PaintBackground
)
1487 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
1488 bgCol
= GetParent()->GetBackgroundColour();
1490 bgCol
= GetBackgroundColour();
1494 dc
.DrawRectangle(rect
);
1497 // Draw standard button
1498 wxRendererNative::Get().DrawComboBoxDropButton(this,
1510 pBmp
= &m_bmpDisabled
;
1511 else if ( m_btnState
& wxCONTROL_PRESSED
)
1512 pBmp
= &m_bmpPressed
;
1513 else if ( m_btnState
& wxCONTROL_CURRENT
)
1516 pBmp
= &m_bmpNormal
;
1518 if ( m_blankButtonBg
)
1520 // If using blank button background, we need to clear its background
1521 // with button face colour instead of colour for rest of the control.
1522 if ( flags
& Button_PaintBackground
)
1524 wxColour bgCol
= GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1525 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1528 dc
.DrawRectangle(rect
);
1531 if ( !(flags
& Button_BitmapOnly
) )
1533 wxRendererNative::Get().DrawPushButton(this,
1542 // Need to clear button background even if m_btn is present
1543 // (assume non-button background was cleared just before this call so brushes are good)
1544 if ( flags
& Button_PaintBackground
)
1545 dc
.DrawRectangle(rect
);
1548 // Draw bitmap centered in drawRect
1549 dc
.DrawBitmap(*pBmp
,
1550 drawRect
.x
+ (drawRect
.width
-pBmp
->GetWidth())/2,
1551 drawRect
.y
+ (drawRect
.height
-pBmp
->GetHeight())/2,
1556 void wxComboCtrlBase::RecalcAndRefresh()
1560 wxSizeEvent
evt(GetSize(),GetId());
1561 GetEventHandler()->ProcessEvent(evt
);
1566 // ----------------------------------------------------------------------------
1567 // miscellaneous event handlers
1568 // ----------------------------------------------------------------------------
1570 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent
& event
)
1572 if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1574 if ( m_ignoreEvtText
> 0 )
1581 // Change event id, object and string before relaying it forward
1582 event
.SetId(GetId());
1583 wxString s
= event
.GetString();
1584 event
.SetEventObject(this);
1589 // call if cursor is on button area or mouse is captured for the button
1590 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent
& event
,
1593 int type
= event
.GetEventType();
1595 if ( type
== wxEVT_MOTION
)
1597 if ( (flags
& wxCC_MF_ON_BUTTON
) &&
1598 IsPopupWindowState(Hidden
) )
1600 if ( !(m_btnState
& wxCONTROL_CURRENT
) )
1602 // Mouse hover begins
1603 m_btnState
|= wxCONTROL_CURRENT
;
1604 if ( HasCapture() ) // Retain pressed state.
1605 m_btnState
|= wxCONTROL_PRESSED
;
1609 else if ( (m_btnState
& wxCONTROL_CURRENT
) )
1612 m_btnState
&= ~(wxCONTROL_CURRENT
|wxCONTROL_PRESSED
);
1616 else if ( type
== wxEVT_LEFT_DOWN
|| type
== wxEVT_LEFT_DCLICK
)
1618 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1620 m_btnState
|= wxCONTROL_PRESSED
;
1623 if ( !(m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
) )
1626 // If showing popup now, do not capture mouse or there will be interference
1630 else if ( type
== wxEVT_LEFT_UP
)
1633 // Only accept event if mouse was left-press was previously accepted
1637 if ( m_btnState
& wxCONTROL_PRESSED
)
1639 // If mouse was inside, fire the click event.
1640 if ( m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
)
1642 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1646 m_btnState
&= ~(wxCONTROL_PRESSED
);
1650 else if ( type
== wxEVT_LEAVE_WINDOW
)
1652 if ( m_btnState
& (wxCONTROL_CURRENT
|wxCONTROL_PRESSED
) )
1654 m_btnState
&= ~(wxCONTROL_CURRENT
);
1657 if ( IsPopupWindowState(Hidden
) )
1659 m_btnState
&= ~(wxCONTROL_PRESSED
);
1667 // Never have 'hot' state when popup is being shown
1668 // (this is mostly needed because of the animation).
1669 if ( !IsPopupWindowState(Hidden
) )
1670 m_btnState
&= ~wxCONTROL_CURRENT
;
1675 // returns true if event was consumed or filtered
1676 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent
& event
,
1677 int WXUNUSED(flags
) )
1679 wxLongLong t
= ::wxGetLocalTimeMillis();
1680 int evtType
= event
.GetEventType();
1682 #if USES_WXPOPUPWINDOW || USES_GENERICTLW
1683 if ( m_popupWinType
!= POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1685 if ( IsPopupWindowState(Visible
) &&
1686 ( evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_RIGHT_DOWN
) )
1694 // Filter out clicks on button immediately after popup dismiss
1695 if ( evtType
== wxEVT_LEFT_DOWN
&& t
< m_timeCanAcceptClick
)
1697 event
.SetEventType(0);
1704 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent
& event
)
1706 int evtType
= event
.GetEventType();
1708 if ( (evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_LEFT_DCLICK
) &&
1709 (m_windowStyle
& wxCB_READONLY
) )
1711 if ( GetPopupWindowState() >= Animating
)
1713 #if USES_WXPOPUPWINDOW
1714 // Click here always hides the popup.
1715 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1721 if ( !(m_windowStyle
& wxCC_SPECIAL_DCLICK
) )
1723 // In read-only mode, clicking the text is the
1724 // same as clicking the button.
1727 else if ( /*evtType == wxEVT_LEFT_UP || */evtType
== wxEVT_LEFT_DCLICK
)
1729 //if ( m_popupInterface->CycleValue() )
1731 if ( m_popupInterface
)
1732 m_popupInterface
->OnComboDoubleClick();
1737 if ( IsPopupShown() )
1739 // relay (some) mouse events to the popup
1740 if ( evtType
== wxEVT_MOUSEWHEEL
)
1741 m_popup
->GetEventHandler()->AddPendingEvent(event
);
1747 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent
& event
)
1749 if ( IsPopupShown() )
1751 // pass it to the popped up control
1752 GetPopupControl()->GetControl()->GetEventHandler()->AddPendingEvent(event
);
1756 if ( GetParent()->HasFlag(wxTAB_TRAVERSAL
) &&
1757 HandleAsNavigationKey(event
) )
1760 if ( IsKeyPopupToggle(event
) )
1766 int comboStyle
= GetWindowStyle();
1767 wxComboPopup
* popupInterface
= GetPopupControl();
1769 if ( !popupInterface
)
1775 int keycode
= event
.GetKeyCode();
1777 if ( (comboStyle
& wxCB_READONLY
) ||
1778 (keycode
!= WXK_RIGHT
&& keycode
!= WXK_LEFT
) )
1780 popupInterface
->OnComboKeyEvent(event
);
1787 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent
& event
)
1789 if ( event
.GetEventType() == wxEVT_SET_FOCUS
)
1791 wxWindow
* tc
= GetTextCtrl();
1792 if ( tc
&& tc
!= DoFindFocus() )
1801 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent
& WXUNUSED(event
) )
1805 m_resetFocus
= false;
1806 wxWindow
* tc
= GetTextCtrl();
1812 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
1815 // left margin may also have changed
1816 if ( !(m_iFlags
& wxCC_IFLAG_LEFT_MARGIN_SET
) )
1817 m_marginLeft
= GetNativeTextIndent();
1821 // ----------------------------------------------------------------------------
1823 // ----------------------------------------------------------------------------
1825 // Create popup window and the child control
1826 void wxComboCtrlBase::CreatePopup()
1828 wxComboPopup
* popupInterface
= m_popupInterface
;
1833 #ifdef wxComboPopupWindowBase2
1834 if ( m_iFlags
& wxCC_IFLAG_USE_ALT_POPUP
)
1836 #if !USES_GENERICTLW
1837 m_winPopup
= new wxComboPopupWindowBase2( this, wxNO_BORDER
);
1839 int tlwFlags
= wxNO_BORDER
;
1840 #ifdef wxCC_GENERIC_TLW_IS_FRAME
1841 tlwFlags
|= wxFRAME_NO_TASKBAR
;
1844 #ifdef wxCC_GENERIC_TLW_IS_NONOWNEDWINDOW
1845 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
,
1846 wxPoint(-21,-21), wxSize(20, 20),
1849 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
, wxEmptyString
,
1850 wxPoint(-21,-21), wxSize(20, 20),
1854 m_popupWinType
= SECONDARY_POPUP_TYPE
;
1857 #endif // wxComboPopupWindowBase2
1859 m_winPopup
= new wxComboPopupWindow( this, wxNO_BORDER
);
1860 m_popupWinType
= PRIMARY_POPUP_TYPE
;
1862 m_popupWinEvtHandler
= new wxComboPopupWindowEvtHandler(this);
1863 m_winPopup
->PushEventHandler(m_popupWinEvtHandler
);
1866 popupInterface
->Create(m_winPopup
);
1867 m_popup
= popup
= popupInterface
->GetControl();
1869 m_popupExtraHandler
= new wxComboPopupExtraEventHandler(this);
1870 popup
->PushEventHandler( m_popupExtraHandler
);
1872 // This may be helpful on some platforms
1873 // (eg. it bypasses a wxGTK popupwindow bug where
1874 // window is not initially hidden when it should be)
1877 popupInterface
->m_iFlags
|= wxCP_IFLAG_CREATED
;
1880 // Destroy popup window and the child control
1881 void wxComboCtrlBase::DestroyPopup()
1886 m_popup
->RemoveEventHandler(m_popupExtraHandler
);
1888 delete m_popupExtraHandler
;
1890 delete m_popupInterface
;
1894 m_winPopup
->RemoveEventHandler(m_popupWinEvtHandler
);
1895 delete m_popupWinEvtHandler
;
1896 m_popupWinEvtHandler
= NULL
;
1897 m_winPopup
->Destroy();
1900 m_popupExtraHandler
= NULL
;
1901 m_popupInterface
= NULL
;
1906 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup
* iface
)
1908 wxCHECK_RET( iface
, wxT("no popup interface set for wxComboCtrl") );
1912 iface
->InitBase(this);
1915 m_popupInterface
= iface
;
1917 if ( !iface
->LazyCreate() )
1926 // This must be done after creation
1927 if ( m_valueString
.length() )
1929 iface
->SetStringValue(m_valueString
);
1934 // Ensures there is atleast the default popup
1935 void wxComboCtrlBase::EnsurePopupControl()
1937 if ( !m_popupInterface
)
1938 SetPopupControl(NULL
);
1941 void wxComboCtrlBase::OnButtonClick()
1943 // Derived classes can override this method for totally custom
1945 if ( !IsPopupWindowState(Visible
) )
1947 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_DROPDOWN
, GetId());
1948 event
.SetEventObject(this);
1949 HandleWindowEvent(event
);
1959 void wxComboCtrlBase::ShowPopup()
1961 EnsurePopupControl();
1962 wxCHECK_RET( !IsPopupWindowState(Visible
), wxT("popup window already shown") );
1964 if ( IsPopupWindowState(Animating
) )
1969 // Space above and below
1975 wxSize ctrlSz
= GetSize();
1977 screenHeight
= wxSystemSettings::GetMetric( wxSYS_SCREEN_Y
);
1978 scrPos
= GetParent()->ClientToScreen(GetPosition());
1980 spaceAbove
= scrPos
.y
;
1981 spaceBelow
= screenHeight
- spaceAbove
- ctrlSz
.y
;
1983 maxHeightPopup
= spaceBelow
;
1984 if ( spaceAbove
> spaceBelow
)
1985 maxHeightPopup
= spaceAbove
;
1988 int widthPopup
= ctrlSz
.x
+ m_extLeft
+ m_extRight
;
1990 if ( widthPopup
< m_widthMinPopup
)
1991 widthPopup
= m_widthMinPopup
;
1993 wxWindow
* winPopup
= m_winPopup
;
1996 // Need to disable tab traversal of parent
1998 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1999 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
2000 // that if transient popup is open, then tab traversal is to be ignored.
2001 // However, I think this code would still be needed for cases where
2002 // transient popup doesn't work yet (wxWinCE?).
2003 wxWindow
* parent
= GetParent();
2004 int parentFlags
= parent
->GetWindowStyle();
2005 if ( parentFlags
& wxTAB_TRAVERSAL
)
2007 parent
->SetWindowStyle( parentFlags
& ~(wxTAB_TRAVERSAL
) );
2008 m_iFlags
|= wxCC_IFLAG_PARENT_TAB_TRAVERSAL
;
2014 winPopup
= m_winPopup
;
2024 wxASSERT( !m_popup
|| m_popup
== popup
); // Consistency check.
2026 wxSize adjustedSize
= m_popupInterface
->GetAdjustedSize(widthPopup
,
2027 m_heightPopup
<=0?DEFAULT_POPUP_HEIGHT
:m_heightPopup
,
2030 popup
->SetSize(adjustedSize
);
2032 m_popupInterface
->OnPopup();
2035 // Reposition and resize popup window
2038 wxSize szp
= popup
->GetSize();
2041 int popupY
= scrPos
.y
+ ctrlSz
.y
;
2043 // Default anchor is wxLEFT
2044 int anchorSide
= m_anchorSide
;
2046 anchorSide
= wxLEFT
;
2048 int rightX
= scrPos
.x
+ ctrlSz
.x
+ m_extRight
- szp
.x
;
2049 int leftX
= scrPos
.x
- m_extLeft
;
2051 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
2054 int screenWidth
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
);
2056 // If there is not enough horizontal space, anchor on the other side.
2057 // If there is no space even then, place the popup at x 0.
2058 if ( anchorSide
== wxRIGHT
)
2062 if ( (leftX
+szp
.x
) < screenWidth
)
2063 anchorSide
= wxLEFT
;
2070 if ( (leftX
+szp
.x
) >= screenWidth
)
2073 anchorSide
= wxRIGHT
;
2079 // Select x coordinate according to the anchor side
2080 if ( anchorSide
== wxRIGHT
)
2082 else if ( anchorSide
== wxLEFT
)
2087 int showFlags
= CanDeferShow
;
2089 if ( spaceBelow
< szp
.y
)
2091 popupY
= scrPos
.y
- szp
.y
;
2092 showFlags
|= ShowAbove
;
2095 #if INSTALL_TOPLEV_HANDLER
2096 // Put top level window event handler into place
2097 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
2099 if ( !m_toplevEvtHandler
)
2100 m_toplevEvtHandler
= new wxComboFrameEventHandler(this);
2102 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2104 ((wxComboFrameEventHandler
*)m_toplevEvtHandler
)->OnPopup();
2105 toplev
->PushEventHandler( m_toplevEvtHandler
);
2109 // Set string selection (must be this way instead of SetStringSelection)
2112 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2113 m_text
->SelectAll();
2115 m_popupInterface
->SetStringValue( m_text
->GetValue() );
2119 // This is neede since focus/selection indication may change when popup is shown
2123 // This must be after SetStringValue
2124 m_popupWinState
= Animating
;
2126 wxRect
popupWinRect( popupX
, popupY
, szp
.x
, szp
.y
);
2129 if ( (m_iFlags
& wxCC_IFLAG_DISABLE_POPUP_ANIM
) ||
2130 AnimateShow( popupWinRect
, showFlags
) )
2132 DoShowPopup( popupWinRect
, showFlags
);
2136 bool wxComboCtrlBase::AnimateShow( const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
) )
2141 void wxComboCtrlBase::DoShowPopup( const wxRect
& rect
, int WXUNUSED(flags
) )
2143 wxWindow
* winPopup
= m_winPopup
;
2145 if ( IsPopupWindowState(Animating
) )
2147 // Make sure the popup window is shown in the right position.
2148 // Should not matter even if animation already did this.
2150 // Some platforms (GTK) may like SetSize and Move to be separate
2151 // (though the bug was probably fixed).
2152 winPopup
->SetSize( rect
);
2154 #if USES_WXPOPUPTRANSIENTWINDOW
2155 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2156 ((wxPopupTransientWindow
*)winPopup
)->Popup(m_popup
);
2161 m_popupWinState
= Visible
;
2163 else if ( IsPopupWindowState(Hidden
) )
2165 // Animation was aborted
2167 wxASSERT( !winPopup
->IsShown() );
2169 m_popupWinState
= Hidden
;
2175 void wxComboCtrlBase::OnPopupDismiss(bool generateEvent
)
2177 // Just in case, avoid double dismiss
2178 if ( IsPopupWindowState(Hidden
) )
2181 // This must be set before focus - otherwise there will be recursive
2182 // OnPopupDismisses.
2183 m_popupWinState
= Hidden
;
2186 m_winPopup
->Disable();
2188 // Inform popup control itself
2189 m_popupInterface
->OnDismiss();
2191 if ( m_popupExtraHandler
)
2192 ((wxComboPopupExtraEventHandler
*)m_popupExtraHandler
)->OnPopupDismiss();
2194 #if INSTALL_TOPLEV_HANDLER
2195 // Remove top level window event handler
2196 if ( m_toplevEvtHandler
)
2198 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
2200 toplev
->RemoveEventHandler( m_toplevEvtHandler
);
2204 m_timeCanAcceptClick
= ::wxGetLocalTimeMillis();
2206 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
2207 m_timeCanAcceptClick
+= 150;
2209 // If cursor not on dropdown button, then clear its state
2210 // (technically not required by all ports, but do it for all just in case)
2211 if ( !m_btnArea
.Contains(ScreenToClient(::wxGetMousePosition())) )
2214 // Return parent's tab traversal flag.
2215 // See ShowPopup for notes.
2216 if ( m_iFlags
& wxCC_IFLAG_PARENT_TAB_TRAVERSAL
)
2218 wxWindow
* parent
= GetParent();
2219 parent
->SetWindowStyle( parent
->GetWindowStyle() | wxTAB_TRAVERSAL
);
2220 m_iFlags
&= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL
);
2223 // refresh control (necessary even if m_text)
2228 if ( generateEvent
)
2230 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_CLOSEUP
, GetId());
2231 event
.SetEventObject(this);
2232 HandleWindowEvent(event
);
2236 void wxComboCtrlBase::HidePopup(bool generateEvent
)
2238 // Should be able to call this without popup interface
2239 if ( IsPopupWindowState(Hidden
) )
2242 // transfer value and show it in textctrl, if any
2243 if ( !IsPopupWindowState(Animating
) )
2244 SetValue( m_popupInterface
->GetStringValue() );
2248 OnPopupDismiss(generateEvent
);
2251 // ----------------------------------------------------------------------------
2252 // customization methods
2253 // ----------------------------------------------------------------------------
2255 void wxComboCtrlBase::SetButtonPosition( int width
, int height
,
2256 int side
, int spacingX
)
2261 m_btnSpacingX
= spacingX
;
2266 wxSize
wxComboCtrlBase::GetButtonSize()
2268 if ( m_btnSize
.x
> 0 )
2271 wxSize
retSize(m_btnWid
,m_btnHei
);
2273 // Need to call CalculateAreas now if button size is
2274 // is not explicitly specified.
2275 if ( retSize
.x
<= 0 || retSize
.y
<= 0)
2279 retSize
= m_btnSize
;
2285 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap
& bmpNormal
,
2287 const wxBitmap
& bmpPressed
,
2288 const wxBitmap
& bmpHover
,
2289 const wxBitmap
& bmpDisabled
)
2291 m_bmpNormal
= bmpNormal
;
2292 m_blankButtonBg
= blankButtonBg
;
2294 if ( bmpPressed
.Ok() )
2295 m_bmpPressed
= bmpPressed
;
2297 m_bmpPressed
= bmpNormal
;
2299 if ( bmpHover
.Ok() )
2300 m_bmpHover
= bmpHover
;
2302 m_bmpHover
= bmpNormal
;
2304 if ( bmpDisabled
.Ok() )
2305 m_bmpDisabled
= bmpDisabled
;
2307 m_bmpDisabled
= bmpNormal
;
2312 void wxComboCtrlBase::SetCustomPaintWidth( int width
)
2316 // move textctrl accordingly
2317 wxRect r
= m_text
->GetRect();
2318 int inc
= width
- m_widthCustomPaint
;
2321 m_text
->SetSize( r
);
2324 m_widthCustomPaint
= width
;
2329 bool wxComboCtrlBase::DoSetMargins(const wxPoint
& margins
)
2331 // For general sanity's sake, we ignore top margin. Instead
2332 // we will always try to center the text vertically.
2335 if ( margins
.x
!= -1 )
2337 m_marginLeft
= margins
.x
;
2338 m_iFlags
|= wxCC_IFLAG_LEFT_MARGIN_SET
;
2342 m_marginLeft
= GetNativeTextIndent();
2343 m_iFlags
&= ~(wxCC_IFLAG_LEFT_MARGIN_SET
);
2346 if ( margins
.y
!= -1 )
2356 wxPoint
wxComboCtrlBase::DoGetMargins() const
2358 return wxPoint(m_marginLeft
, -1);
2361 #if WXWIN_COMPATIBILITY_2_8
2362 void wxComboCtrlBase::SetTextIndent( int indent
)
2366 m_marginLeft
= GetNativeTextIndent();
2367 m_iFlags
&= ~(wxCC_IFLAG_LEFT_MARGIN_SET
);
2371 m_marginLeft
= indent
;
2372 m_iFlags
|= wxCC_IFLAG_LEFT_MARGIN_SET
;
2378 wxCoord
wxComboCtrlBase::GetTextIndent() const
2380 return m_marginLeft
;
2384 wxCoord
wxComboCtrlBase::GetNativeTextIndent() const
2386 return DEFAULT_TEXT_INDENT
;
2389 // ----------------------------------------------------------------------------
2390 // methods forwarded to wxTextCtrl
2391 // ----------------------------------------------------------------------------
2393 wxString
wxComboCtrlBase::GetValue() const
2396 return m_text
->GetValue();
2397 return m_valueString
;
2400 void wxComboCtrlBase::SetValueWithEvent(const wxString
& value
, bool withEvent
)
2407 m_text
->SetValue(value
);
2409 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2410 m_text
->SelectAll();
2413 // Since wxComboPopup may want to paint the combo as well, we need
2414 // to set the string value here (as well as sometimes in ShowPopup).
2415 if ( m_valueString
!= value
)
2417 m_valueString
= value
;
2419 EnsurePopupControl();
2421 if (m_popupInterface
)
2422 m_popupInterface
->SetStringValue(value
);
2428 void wxComboCtrlBase::SetValue(const wxString
& value
)
2430 SetValueWithEvent(value
, false);
2433 // In this SetValue variant wxComboPopup::SetStringValue is not called
2434 void wxComboCtrlBase::SetText(const wxString
& value
)
2436 // Unlike in SetValue(), this must be called here or
2437 // the behaviour will no be consistent in readonlys.
2438 EnsurePopupControl();
2440 m_valueString
= value
;
2445 m_text
->SetValue( value
);
2451 void wxComboCtrlBase::Copy()
2457 void wxComboCtrlBase::Cut()
2463 void wxComboCtrlBase::Paste()
2469 void wxComboCtrlBase::SetInsertionPoint(long pos
)
2472 m_text
->SetInsertionPoint(pos
);
2475 void wxComboCtrlBase::SetInsertionPointEnd()
2478 m_text
->SetInsertionPointEnd();
2481 long wxComboCtrlBase::GetInsertionPoint() const
2484 return m_text
->GetInsertionPoint();
2489 long wxComboCtrlBase::GetLastPosition() const
2492 return m_text
->GetLastPosition();
2497 void wxComboCtrlBase::Replace(long from
, long to
, const wxString
& value
)
2500 m_text
->Replace(from
, to
, value
);
2503 void wxComboCtrlBase::Remove(long from
, long to
)
2506 m_text
->Remove(from
, to
);
2509 void wxComboCtrlBase::SetSelection(long from
, long to
)
2512 m_text
->SetSelection(from
, to
);
2515 void wxComboCtrlBase::Undo()
2521 #endif // wxUSE_COMBOCTRL