1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/combocmn.cpp
3 // Purpose: wxComboCtrlBase
4 // Author: Jaakko Salli
6 // Created: Apr-30-2006
8 // Copyright: (c) 2005 Jaakko Salli
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
28 #include "wx/combobox.h"
32 #include "wx/dcclient.h"
33 #include "wx/settings.h"
34 #include "wx/dialog.h"
38 #include "wx/tooltip.h"
45 // ----------------------------------------------------------------------------
47 // Milliseconds to wait for two mouse-ups after focus inorder
48 // to trigger a double-click.
49 #define DOUBLE_CLICK_CONVERSION_TRESHOLD 500
51 #define DEFAULT_DROPBUTTON_WIDTH 19
53 #define BMP_BUTTON_MARGIN 4
55 #define DEFAULT_POPUP_HEIGHT 400
57 #define DEFAULT_TEXT_INDENT 3
59 #define COMBO_MARGIN 2 // spacing right of wxTextCtrl
62 #if defined(__WXMSW__)
64 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
65 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
66 // native controls work on it like normal.
67 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
68 #define TEXTCTRL_TEXT_CENTERED 0 // 1 if text in textctrl is vertically centered
70 //#undef wxUSE_POPUPWIN
71 //#define wxUSE_POPUPWIN 0
73 #elif defined(__WXGTK__)
75 // NB: It is not recommended to use wxDialog as popup on wxGTK, because of
76 // this bug: If wxDialog is hidden, its position becomes corrupt
77 // between hide and next show, but without internal coordinates being
78 // reflected (or something like that - atleast commenting out ->Hide()
79 // seemed to eliminate the position change).
81 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
82 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
83 // native controls work on it like normal.
84 #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
85 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
87 #elif defined(__WXMAC__)
89 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
90 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
91 // native controls work on it like normal.
92 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
93 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
97 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
98 #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
99 // native controls work on it like normal.
100 #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
101 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
106 // Popupwin is really only supported on wxMSW (not WINCE) and wxGTK, regardless
107 // what the wxUSE_POPUPWIN says.
108 // FIXME: Why isn't wxUSE_POPUPWIN reliable any longer? (it was in wxW2.6.2)
109 #if (!defined(__WXMSW__) && !defined(__WXGTK__)) || defined(__WXWINCE__)
110 #undef wxUSE_POPUPWIN
111 #define wxUSE_POPUPWIN 0
116 #include "wx/popupwin.h"
118 #undef USE_TRANSIENT_POPUP
119 #define USE_TRANSIENT_POPUP 0
123 // Define different types of popup windows
127 POPUPWIN_WXPOPUPTRANSIENTWINDOW
= 1,
128 POPUPWIN_WXPOPUPWINDOW
= 2,
129 POPUPWIN_WXDIALOG
= 3
133 #if USE_TRANSIENT_POPUP
134 // wxPopupTransientWindow is implemented
136 #define wxComboPopupWindowBase wxPopupTransientWindow
137 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPTRANSIENTWINDOW
138 #define USES_WXPOPUPTRANSIENTWINDOW 1
140 #if TRANSIENT_POPUPWIN_IS_PERFECT
142 #elif POPUPWIN_IS_PERFECT
143 #define wxComboPopupWindowBase2 wxPopupWindow
144 #define SECONDARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
145 #define USES_WXPOPUPWINDOW 1
147 #define wxComboPopupWindowBase2 wxDialog
148 #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG
149 #define USES_WXDIALOG 1
153 // wxPopupWindow (but not wxPopupTransientWindow) is properly implemented
155 #define wxComboPopupWindowBase wxPopupWindow
156 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
157 #define USES_WXPOPUPWINDOW 1
159 #if !POPUPWIN_IS_PERFECT
160 #define wxComboPopupWindowBase2 wxDialog
161 #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG
162 #define USES_WXDIALOG 1
166 // wxPopupWindow is not implemented
168 #define wxComboPopupWindowBase wxDialog
169 #define PRIMARY_POPUP_TYPE POPUPWIN_WXDIALOG
170 #define USES_WXDIALOG 1
175 #ifndef USES_WXPOPUPTRANSIENTWINDOW
176 #define USES_WXPOPUPTRANSIENTWINDOW 0
179 #ifndef USES_WXPOPUPWINDOW
180 #define USES_WXPOPUPWINDOW 0
183 #ifndef USES_WXDIALOG
184 #define USES_WXDIALOG 0
188 #if USES_WXPOPUPWINDOW
189 #define INSTALL_TOPLEV_HANDLER 1
191 #define INSTALL_TOPLEV_HANDLER 0
197 // * wxComboPopupWindow for external use (ie. replace old wxUniv wxPopupComboWindow)
201 // ----------------------------------------------------------------------------
202 // wxComboFrameEventHandler takes care of hiding the popup when events happen
203 // in its top level parent.
204 // ----------------------------------------------------------------------------
206 #if INSTALL_TOPLEV_HANDLER
209 // This will no longer be necessary after wxTransientPopupWindow
210 // works well on all platforms.
213 class wxComboFrameEventHandler
: public wxEvtHandler
216 wxComboFrameEventHandler( wxComboCtrlBase
* pCb
);
217 virtual ~wxComboFrameEventHandler();
221 void OnIdle( wxIdleEvent
& event
);
222 void OnMouseEvent( wxMouseEvent
& event
);
223 void OnActivate( wxActivateEvent
& event
);
224 void OnResize( wxSizeEvent
& event
);
225 void OnMove( wxMoveEvent
& event
);
226 void OnMenuEvent( wxMenuEvent
& event
);
227 void OnClose( wxCloseEvent
& event
);
230 wxWindow
* m_focusStart
;
231 wxComboCtrlBase
* m_combo
;
234 DECLARE_EVENT_TABLE()
237 BEGIN_EVENT_TABLE(wxComboFrameEventHandler
, wxEvtHandler
)
238 EVT_IDLE(wxComboFrameEventHandler::OnIdle
)
239 EVT_LEFT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
240 EVT_RIGHT_DOWN(wxComboFrameEventHandler::OnMouseEvent
)
241 EVT_SIZE(wxComboFrameEventHandler::OnResize
)
242 EVT_MOVE(wxComboFrameEventHandler::OnMove
)
243 EVT_MENU_HIGHLIGHT(wxID_ANY
,wxComboFrameEventHandler::OnMenuEvent
)
244 EVT_MENU_OPEN(wxComboFrameEventHandler::OnMenuEvent
)
245 EVT_ACTIVATE(wxComboFrameEventHandler::OnActivate
)
246 EVT_CLOSE(wxComboFrameEventHandler::OnClose
)
249 wxComboFrameEventHandler::wxComboFrameEventHandler( wxComboCtrlBase
* combo
)
255 wxComboFrameEventHandler::~wxComboFrameEventHandler()
259 void wxComboFrameEventHandler::OnPopup()
261 m_focusStart
= ::wxWindow::FindFocus();
264 void wxComboFrameEventHandler::OnIdle( wxIdleEvent
& event
)
266 wxWindow
* winFocused
= ::wxWindow::FindFocus();
268 wxWindow
* popup
= m_combo
->GetPopupControl()->GetControl();
269 wxWindow
* winpopup
= m_combo
->GetPopupWindow();
272 winFocused
!= m_focusStart
&&
273 winFocused
!= popup
&&
274 winFocused
->GetParent() != popup
&&
275 winFocused
!= winpopup
&&
276 winFocused
->GetParent() != winpopup
&&
277 winFocused
!= m_combo
&&
278 winFocused
!= m_combo
->GetButton() // GTK (atleast) requires this
281 m_combo
->HidePopup();
287 void wxComboFrameEventHandler::OnMenuEvent( wxMenuEvent
& event
)
289 m_combo
->HidePopup();
293 void wxComboFrameEventHandler::OnMouseEvent( wxMouseEvent
& event
)
295 m_combo
->HidePopup();
299 void wxComboFrameEventHandler::OnClose( wxCloseEvent
& event
)
301 m_combo
->HidePopup();
305 void wxComboFrameEventHandler::OnActivate( wxActivateEvent
& event
)
307 m_combo
->HidePopup();
311 void wxComboFrameEventHandler::OnResize( wxSizeEvent
& event
)
313 m_combo
->HidePopup();
317 void wxComboFrameEventHandler::OnMove( wxMoveEvent
& event
)
319 m_combo
->HidePopup();
323 #endif // INSTALL_TOPLEV_HANDLER
325 // ----------------------------------------------------------------------------
326 // wxComboPopupWindow is, in essence, wxPopupWindow customized for
328 // ----------------------------------------------------------------------------
330 class wxComboPopupWindow
: public wxComboPopupWindowBase
334 wxComboPopupWindow( wxComboCtrlBase
*parent
,
336 #if USES_WXPOPUPWINDOW || USES_WXPOPUPTRANSIENTWINDOW
337 : wxComboPopupWindowBase(parent
,style
)
339 : wxComboPopupWindowBase(parent
,
349 #if USES_WXPOPUPTRANSIENTWINDOW
350 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
351 virtual void OnDismiss();
357 #if USES_WXPOPUPTRANSIENTWINDOW
358 bool wxComboPopupWindow::ProcessLeftDown(wxMouseEvent
& event
)
360 return wxComboPopupWindowBase::ProcessLeftDown(event
);
363 // First thing that happens when a transient popup closes is that this method gets called.
364 void wxComboPopupWindow::OnDismiss()
366 wxComboCtrlBase
* combo
= (wxComboCtrlBase
*) GetParent();
367 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxComboCtrlBase
)),
368 wxT("parent might not be wxComboCtrl, but check IMPLEMENT_DYNAMIC_CLASS(2) macro for correctness") );
370 combo
->OnPopupDismiss();
372 #endif // USES_WXPOPUPTRANSIENTWINDOW
375 // ----------------------------------------------------------------------------
376 // wxComboPopupWindowEvtHandler does bulk of the custom event handling
377 // of a popup window. It is separate so we can have different types
379 // ----------------------------------------------------------------------------
381 class wxComboPopupWindowEvtHandler
: public wxEvtHandler
385 wxComboPopupWindowEvtHandler( wxComboCtrlBase
*parent
)
390 void OnKeyEvent(wxKeyEvent
& event
);
392 void OnActivate( wxActivateEvent
& event
);
396 wxComboCtrlBase
* m_combo
;
398 DECLARE_EVENT_TABLE()
402 BEGIN_EVENT_TABLE(wxComboPopupWindowEvtHandler
, wxEvtHandler
)
403 EVT_KEY_DOWN(wxComboPopupWindowEvtHandler::OnKeyEvent
)
404 EVT_KEY_UP(wxComboPopupWindowEvtHandler::OnKeyEvent
)
406 EVT_ACTIVATE(wxComboPopupWindowEvtHandler::OnActivate
)
411 void wxComboPopupWindowEvtHandler::OnKeyEvent( wxKeyEvent
& event
)
413 // Relay keyboard event to the main child controls
414 wxWindowList children
= m_combo
->GetPopupWindow()->GetChildren();
415 wxWindowList::iterator node
= children
.begin();
416 wxWindow
* child
= (wxWindow
*)*node
;
417 child
->AddPendingEvent(event
);
421 void wxComboPopupWindowEvtHandler::OnActivate( wxActivateEvent
& event
)
423 if ( !event
.GetActive() )
425 // Tell combo control that we are dismissed.
426 m_combo
->HidePopup();
434 // ----------------------------------------------------------------------------
437 // ----------------------------------------------------------------------------
439 wxComboPopup::~wxComboPopup()
443 void wxComboPopup::OnPopup()
447 void wxComboPopup::OnDismiss()
451 wxSize
wxComboPopup::GetAdjustedSize( int minWidth
,
453 int WXUNUSED(maxHeight
) )
455 return wxSize(minWidth
,prefHeight
);
458 void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase
* combo
,
459 wxDC
& dc
, const wxRect
& rect
)
461 if ( combo
->GetWindowStyle() & wxCB_READONLY
) // ie. no textctrl
463 combo
->PrepareBackground(dc
,rect
,0);
465 dc
.DrawText( combo
->GetValue(),
466 rect
.x
+ combo
->GetTextIndent(),
467 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
471 void wxComboPopup::PaintComboControl( wxDC
& dc
, const wxRect
& rect
)
473 DefaultPaintComboControl(m_combo
,dc
,rect
);
476 void wxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
481 void wxComboPopup::OnComboDoubleClick()
485 void wxComboPopup::SetStringValue( const wxString
& WXUNUSED(value
) )
489 bool wxComboPopup::LazyCreate()
494 void wxComboPopup::Dismiss()
496 m_combo
->HidePopup();
499 // ----------------------------------------------------------------------------
501 // ----------------------------------------------------------------------------
504 // This is pushed to the event handler queue of the child textctrl.
506 class wxComboBoxExtraInputHandler
: public wxEvtHandler
510 wxComboBoxExtraInputHandler( wxComboCtrlBase
* combo
)
515 virtual ~wxComboBoxExtraInputHandler() { }
516 void OnKey(wxKeyEvent
& event
);
517 void OnFocus(wxFocusEvent
& event
);
520 wxComboCtrlBase
* m_combo
;
523 DECLARE_EVENT_TABLE()
527 BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler
, wxEvtHandler
)
528 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey
)
529 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus
)
533 void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent
& event
)
535 // Let the wxComboCtrl event handler have a go first.
536 wxComboCtrlBase
* combo
= m_combo
;
537 wxObject
* prevObj
= event
.GetEventObject();
539 event
.SetId(combo
->GetId());
540 event
.SetEventObject(combo
);
541 combo
->GetEventHandler()->ProcessEvent(event
);
543 event
.SetId(((wxWindow
*)prevObj
)->GetId());
544 event
.SetEventObject(prevObj
);
547 void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent
& event
)
549 // FIXME: This code does run when control is clicked,
550 // yet on Windows it doesn't select all the text.
551 if ( !(m_combo
->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT
) )
553 if ( m_combo
->GetTextCtrl() )
554 m_combo
->GetTextCtrl()->SelectAll();
556 m_combo
->SetSelection(-1,-1);
559 // Send focus indication to parent.
560 // NB: This is needed for cases where the textctrl gets focus
561 // instead of its parent. While this may trigger multiple
562 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
563 // from combo's focus event handler), they should be quite
565 wxFocusEvent
evt2(wxEVT_SET_FOCUS
,m_combo
->GetId());
566 evt2
.SetEventObject(m_combo
);
567 m_combo
->GetEventHandler()->ProcessEvent(evt2
);
574 // This is pushed to the event handler queue of the control in popup.
577 class wxComboPopupExtraEventHandler
: public wxEvtHandler
581 wxComboPopupExtraEventHandler( wxComboCtrlBase
* combo
)
585 m_beenInside
= false;
587 virtual ~wxComboPopupExtraEventHandler() { }
589 void OnMouseEvent( wxMouseEvent
& event
);
591 // Called from wxComboCtrlBase::OnPopupDismiss
592 void OnPopupDismiss()
594 m_beenInside
= false;
598 wxComboCtrlBase
* m_combo
;
603 DECLARE_EVENT_TABLE()
607 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler
, wxEvtHandler
)
608 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent
)
612 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent
& event
)
614 wxPoint pt
= event
.GetPosition();
615 wxSize sz
= m_combo
->GetPopupControl()->GetControl()->GetClientSize();
616 int evtType
= event
.GetEventType();
617 bool isInside
= pt
.x
>= 0 && pt
.y
>= 0 && pt
.x
< sz
.x
&& pt
.y
< sz
.y
;
619 if ( evtType
== wxEVT_MOTION
||
620 evtType
== wxEVT_LEFT_DOWN
||
621 evtType
== wxEVT_RIGHT_DOWN
)
623 // Block motion and click events outside the popup
630 else if ( evtType
== wxEVT_LEFT_UP
)
632 // Don't let left-down events in if outside
633 if ( evtType
== wxEVT_LEFT_DOWN
)
648 // Some mouse events to popup that happen outside it, before cursor
649 // has been inside the popu, need to be ignored by it but relayed to
652 wxWindow
* btn
= m_combo
->GetButton();
654 btn
->GetEventHandler()->AddPendingEvent(event
);
656 m_combo
->GetEventHandler()->AddPendingEvent(event
);
668 // ----------------------------------------------------------------------------
670 // ----------------------------------------------------------------------------
673 BEGIN_EVENT_TABLE(wxComboCtrlBase
, wxControl
)
674 EVT_TEXT(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
675 EVT_SIZE(wxComboCtrlBase::OnSizeEvent
)
676 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent
)
677 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent
)
678 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
679 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent
)
680 EVT_TEXT_ENTER(wxID_ANY
,wxComboCtrlBase::OnTextCtrlEvent
)
681 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged
)
685 IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase
, wxControl
)
687 void wxComboCtrlBase::Init()
689 m_winPopup
= (wxWindow
*)NULL
;
690 m_popup
= (wxWindow
*)NULL
;
691 m_isPopupShown
= false;
692 m_btn
= (wxWindow
*) NULL
;
693 m_text
= (wxTextCtrl
*) NULL
;
694 m_popupInterface
= (wxComboPopup
*) NULL
;
696 m_popupExtraHandler
= (wxEvtHandler
*) NULL
;
697 m_textEvtHandler
= (wxEvtHandler
*) NULL
;
699 #if INSTALL_TOPLEV_HANDLER
700 m_toplevEvtHandler
= (wxEvtHandler
*) NULL
;
704 m_widthMinPopup
= -1;
706 m_widthCustomPaint
= 0;
707 m_widthCustomBorder
= 0;
711 m_blankButtonBg
= false;
713 m_popupWinType
= POPUPWIN_NONE
;
714 m_btnWid
= m_btnHei
= -1;
722 m_timeCanAcceptClick
= 0;
725 bool wxComboCtrlBase::Create(wxWindow
*parent
,
727 const wxString
& value
,
731 const wxValidator
& validator
,
732 const wxString
& name
)
734 if ( !wxControl::Create(parent
,
738 style
| wxWANTS_CHARS
,
743 m_valueString
= value
;
747 m_absIndent
= GetNativeTextIndent();
749 m_iFlags
|= wxCC_IFLAG_CREATED
;
751 // If x and y indicate valid size, wxSizeEvent won't be
752 // emitted automatically, so we need to add artifical one.
753 if ( size
.x
> 0 && size
.y
> 0 )
755 wxSizeEvent
evt(size
,GetId());
756 GetEventHandler()->AddPendingEvent(evt
);
762 void wxComboCtrlBase::InstallInputHandlers()
766 m_textEvtHandler
= new wxComboBoxExtraInputHandler(this);
767 m_text
->PushEventHandler(m_textEvtHandler
);
772 wxComboCtrlBase::CreateTextCtrl(int style
, const wxValidator
& validator
)
774 if ( !(m_windowStyle
& wxCB_READONLY
) )
779 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
780 // not used by the wxPropertyGrid and therefore the tab is processed by
781 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
782 // navigation event is then sent to the wrong window.
783 style
|= wxTE_PROCESS_TAB
;
785 if ( HasFlag(wxTE_PROCESS_ENTER
) )
786 style
|= wxTE_PROCESS_ENTER
;
788 // Ignore EVT_TEXT generated by the constructor (but only
789 // if the event redirector already exists)
790 // NB: This must be " = 1" instead of "++";
791 if ( m_textEvtHandler
)
796 m_text
= new wxTextCtrl(this, wxID_ANY
, m_valueString
,
797 wxDefaultPosition
, wxDefaultSize
,
800 // This is required for some platforms (GTK+ atleast)
801 m_text
->SetSizeHints(2,4);
805 void wxComboCtrlBase::OnThemeChange()
807 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
810 wxComboCtrlBase::~wxComboCtrlBase()
815 #if INSTALL_TOPLEV_HANDLER
816 delete ((wxComboFrameEventHandler
*)m_toplevEvtHandler
);
817 m_toplevEvtHandler
= (wxEvtHandler
*) NULL
;
823 m_text
->RemoveEventHandler(m_textEvtHandler
);
825 delete m_textEvtHandler
;
829 // ----------------------------------------------------------------------------
831 // ----------------------------------------------------------------------------
833 // Recalculates button and textctrl areas
834 void wxComboCtrlBase::CalculateAreas( int btnWidth
)
836 wxSize sz
= GetClientSize();
837 int customBorder
= m_widthCustomBorder
;
838 int btnBorder
; // border for button only
840 // check if button should really be outside the border: we'll do it it if
841 // its platform default or bitmap+pushbutton background is used, but not if
842 // there is vertical size adjustment or horizontal spacing.
843 if ( ( (m_iFlags
& wxCC_BUTTON_OUTSIDE_BORDER
) ||
844 (m_bmpNormal
.Ok() && m_blankButtonBg
) ) &&
845 m_btnSpacingX
== 0 &&
848 m_iFlags
|= wxCC_IFLAG_BUTTON_OUTSIDE
;
853 m_iFlags
&= ~(wxCC_IFLAG_BUTTON_OUTSIDE
);
854 btnBorder
= customBorder
;
857 // Defaul indentation
858 if ( m_absIndent
< 0 )
859 m_absIndent
= GetNativeTextIndent();
861 int butWidth
= btnWidth
;
864 butWidth
= m_btnWidDefault
;
866 m_btnWidDefault
= butWidth
;
871 int butHeight
= sz
.y
- btnBorder
*2;
873 // Adjust button width
878 // Adjust button width to match aspect ratio
879 // (but only if control is smaller than best size).
880 int bestHeight
= GetBestSize().y
;
881 int height
= GetSize().y
;
883 if ( height
< bestHeight
)
885 // Make very small buttons square, as it makes
886 // them accommodate arrow image better and still
889 butWidth
= (height
*butWidth
)/bestHeight
;
891 butWidth
= butHeight
;
895 // Adjust button height
897 butHeight
= m_btnHei
;
899 // Use size of normal bitmap if...
902 // button width is set to default and blank button bg is not drawn
903 if ( m_bmpNormal
.Ok() )
905 int bmpReqWidth
= m_bmpNormal
.GetWidth();
906 int bmpReqHeight
= m_bmpNormal
.GetHeight();
908 // If drawing blank button background, we need to add some margin.
909 if ( m_blankButtonBg
)
911 bmpReqWidth
+= BMP_BUTTON_MARGIN
*2;
912 bmpReqHeight
+= BMP_BUTTON_MARGIN
*2;
915 if ( butWidth
< bmpReqWidth
|| ( m_btnWid
== 0 && !m_blankButtonBg
) )
916 butWidth
= bmpReqWidth
;
917 if ( butHeight
< bmpReqHeight
|| ( m_btnHei
== 0 && !m_blankButtonBg
) )
918 butHeight
= bmpReqHeight
;
920 // Need to fix height?
921 if ( (sz
.y
-(customBorder
*2)) < butHeight
&& btnWidth
== 0 )
923 int newY
= butHeight
+(customBorder
*2);
924 SetClientSize(wxDefaultCoord
,newY
);
929 int butAreaWid
= butWidth
+ (m_btnSpacingX
*2);
931 m_btnSize
.x
= butWidth
;
932 m_btnSize
.y
= butHeight
;
934 m_btnArea
.x
= ( m_btnSide
==wxRIGHT
? sz
.x
- butAreaWid
- btnBorder
: btnBorder
);
935 m_btnArea
.y
= btnBorder
;
936 m_btnArea
.width
= butAreaWid
;
937 m_btnArea
.height
= sz
.y
- (btnBorder
*2);
939 m_tcArea
.x
= ( m_btnSide
==wxRIGHT
? 0 : butAreaWid
) + customBorder
;
940 m_tcArea
.y
= customBorder
;
941 m_tcArea
.width
= sz
.x
- butAreaWid
- (customBorder
*2);
942 m_tcArea
.height
= sz
.y
- (customBorder
*2);
947 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
948 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
953 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
)
958 wxSize sz
= GetClientSize();
959 int customBorder
= m_widthCustomBorder
;
961 #if !TEXTCTRL_TEXT_CENTERED
962 if ( (m_text
->GetWindowStyleFlag() & wxBORDER_MASK
) == wxNO_BORDER
)
965 int tcSizeY
= m_text
->GetBestSize().y
;
966 int diff
= sz
.y
- tcSizeY
;
967 int y
= textCtrlYAdjust
+ (diff
/2);
969 if ( y
< customBorder
)
972 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
+ m_absIndent
+ textCtrlXAdjust
,
974 m_tcArea
.width
- COMBO_MARGIN
-
975 (textCtrlXAdjust
+ m_widthCustomPaint
+ m_absIndent
),
978 // Make sure textctrl doesn't exceed the bottom custom border
979 wxSize tsz
= m_text
->GetSize();
980 diff
= (y
+ tsz
.y
) - (sz
.y
- customBorder
);
983 tsz
.y
= tsz
.y
- diff
- 1;
984 m_text
->SetSize(tsz
);
989 wxUnusedVar(textCtrlXAdjust
);
990 wxUnusedVar(textCtrlYAdjust
);
993 // If it has border, have textctrl will the entire text field.
994 m_text
->SetSize( m_tcArea
.x
+ m_widthCustomPaint
,
996 sz
.x
- m_btnArea
.width
- m_widthCustomPaint
- customBorder
,
997 sz
.y
-(customBorder
*2) );
1001 wxSize
wxComboCtrlBase::DoGetBestSize() const
1003 wxSize
sizeText(150,0);
1006 sizeText
= m_text
->GetBestSize();
1008 // TODO: Better method to calculate close-to-native control height.
1012 fhei
= (m_font
.GetPointSize()*2) + 5;
1013 else if ( wxNORMAL_FONT
->Ok() )
1014 fhei
= (wxNORMAL_FONT
->GetPointSize()*2) + 5;
1016 fhei
= sizeText
.y
+ 4;
1018 // Need to force height to accomodate bitmap?
1019 int btnSizeY
= m_btnSize
.y
;
1020 if ( m_bmpNormal
.Ok() && fhei
< btnSizeY
)
1023 // Control height doesn't depend on border
1026 int border = m_windowStyle & wxBORDER_MASK;
1027 if ( border == wxSIMPLE_BORDER )
1029 else if ( border == wxNO_BORDER )
1030 fhei += (m_widthCustomBorder*2);
1036 // Final adjustments
1041 wxSize
ret(sizeText
.x
+ COMBO_MARGIN
+ DEFAULT_DROPBUTTON_WIDTH
,
1048 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent
& event
)
1053 // defined by actual wxComboCtrls
1059 // ----------------------------------------------------------------------------
1060 // standard operations
1061 // ----------------------------------------------------------------------------
1063 bool wxComboCtrlBase::Enable(bool enable
)
1065 if ( !wxControl::Enable(enable
) )
1069 m_btn
->Enable(enable
);
1071 m_text
->Enable(enable
);
1076 bool wxComboCtrlBase::Show(bool show
)
1078 if ( !wxControl::Show(show
) )
1090 bool wxComboCtrlBase::SetFont ( const wxFont
& font
)
1092 if ( !wxControl::SetFont(font
) )
1096 m_text
->SetFont(font
);
1102 void wxComboCtrlBase::DoSetToolTip(wxToolTip
*tooltip
)
1104 wxControl::DoSetToolTip(tooltip
);
1106 // Set tool tip for button and text box
1109 const wxString
&tip
= tooltip
->GetTip();
1110 if ( m_text
) m_text
->SetToolTip(tip
);
1111 if ( m_btn
) m_btn
->SetToolTip(tip
);
1115 if ( m_text
) m_text
->SetToolTip( (wxToolTip
*) NULL
);
1116 if ( m_btn
) m_btn
->SetToolTip( (wxToolTip
*) NULL
);
1119 #endif // wxUSE_TOOLTIPS
1121 // ----------------------------------------------------------------------------
1123 // ----------------------------------------------------------------------------
1125 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1126 // prepare combo box background on area in a way typical on platform
1127 void wxComboCtrlBase::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
1129 wxSize sz
= GetClientSize();
1131 bool isFocused
; // also selected
1133 // For smaller size control (and for disabled background) use less spacing
1137 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1140 isEnabled
= IsEnabled();
1141 isFocused
= ShouldDrawFocus();
1143 // Windows-style: for smaller size control (and for disabled background) use less spacing
1144 focusSpacingX
= isEnabled
? 2 : 1;
1145 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
1149 // Drawing a list item
1150 isEnabled
= true; // they are never disabled
1151 isFocused
= flags
& wxCONTROL_SELECTED
? true : false;
1157 // Set the background sub-rectangle for selection, disabled etc
1158 wxRect
selRect(rect
);
1159 selRect
.y
+= focusSpacingY
;
1160 selRect
.height
-= (focusSpacingY
*2);
1164 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
1165 wcp
+= m_widthCustomPaint
;
1167 selRect
.x
+= wcp
+ focusSpacingX
;
1168 selRect
.width
-= wcp
+ (focusSpacingX
*2);
1174 // If popup is hidden and this control is focused,
1175 // then draw the focus-indicator (selbgcolor background etc.).
1178 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1179 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1183 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
1184 bgCol
= GetBackgroundColour();
1189 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
1190 bgCol
= GetBackgroundColour();
1193 dc
.SetBrush( bgCol
);
1195 dc
.DrawRectangle( selRect
);
1197 // Don't clip exactly to the selection rectangle so we can draw
1198 // to the non-selected area in front of it.
1199 wxRect
clipRect(rect
.x
,rect
.y
,
1200 (selRect
.x
+selRect
.width
)-rect
.x
,rect
.height
);
1201 dc
.SetClippingRegion(clipRect
);
1204 // Save the library size a bit for platforms that re-implement this.
1205 void wxComboCtrlBase::PrepareBackground( wxDC
&, const wxRect
&, int ) const
1210 void wxComboCtrlBase::DrawButton( wxDC
& dc
, const wxRect
& rect
, bool paintBg
)
1212 int drawState
= m_btnState
;
1215 if ( m_isPopupShown
)
1216 drawState
|= wxCONTROL_PRESSED
;
1219 wxRect
drawRect(rect
.x
+m_btnSpacingX
,
1220 rect
.y
+((rect
.height
-m_btnSize
.y
)/2),
1224 // Make sure area is not larger than the control
1225 if ( drawRect
.y
< rect
.y
)
1226 drawRect
.y
= rect
.y
;
1227 if ( drawRect
.height
> rect
.height
)
1228 drawRect
.height
= rect
.height
;
1230 bool enabled
= IsEnabled();
1233 drawState
|= wxCONTROL_DISABLED
;
1235 if ( !m_bmpNormal
.Ok() )
1237 // Need to clear button background even if m_btn is present
1242 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
1243 bgCol
= GetParent()->GetBackgroundColour();
1245 bgCol
= GetBackgroundColour();
1249 dc
.DrawRectangle(rect
);
1252 // Draw standard button
1253 wxRendererNative::Get().DrawComboBoxDropButton(this,
1265 pBmp
= &m_bmpDisabled
;
1266 else if ( m_btnState
& wxCONTROL_PRESSED
)
1267 pBmp
= &m_bmpPressed
;
1268 else if ( m_btnState
& wxCONTROL_CURRENT
)
1271 pBmp
= &m_bmpNormal
;
1273 if ( m_blankButtonBg
)
1275 // If using blank button background, we need to clear its background
1276 // with button face colour instead of colour for rest of the control.
1279 wxColour bgCol
= GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1280 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1283 dc
.DrawRectangle(rect
);
1286 wxRendererNative::Get().DrawPushButton(this,
1295 // Need to clear button background even if m_btn is present
1296 // (assume non-button background was cleared just before this call so brushes are good)
1298 dc
.DrawRectangle(rect
);
1301 // Draw bitmap centered in drawRect
1302 dc
.DrawBitmap(*pBmp
,
1303 drawRect
.x
+ (drawRect
.width
-pBmp
->GetWidth())/2,
1304 drawRect
.y
+ (drawRect
.height
-pBmp
->GetHeight())/2,
1309 void wxComboCtrlBase::RecalcAndRefresh()
1313 wxSizeEvent
evt(GetSize(),GetId());
1314 GetEventHandler()->ProcessEvent(evt
);
1319 // ----------------------------------------------------------------------------
1320 // miscellaneous event handlers
1321 // ----------------------------------------------------------------------------
1323 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent
& event
)
1325 if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1327 if ( m_ignoreEvtText
> 0 )
1334 // Change event id, object and string before relaying it forward
1335 event
.SetId(GetId());
1336 wxString s
= event
.GetString();
1337 event
.SetEventObject(this);
1342 // call if cursor is on button area or mouse is captured for the button
1343 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent
& event
,
1346 int type
= event
.GetEventType();
1348 if ( type
== wxEVT_MOTION
)
1350 if ( flags
& wxCC_MF_ON_BUTTON
)
1352 if ( !(m_btnState
& wxCONTROL_CURRENT
) )
1354 // Mouse hover begins
1355 m_btnState
|= wxCONTROL_CURRENT
;
1356 if ( HasCapture() ) // Retain pressed state.
1357 m_btnState
|= wxCONTROL_PRESSED
;
1361 else if ( (m_btnState
& wxCONTROL_CURRENT
) )
1364 m_btnState
&= ~(wxCONTROL_CURRENT
|wxCONTROL_PRESSED
);
1368 else if ( type
== wxEVT_LEFT_DOWN
)
1370 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1372 m_btnState
|= wxCONTROL_PRESSED
;
1375 if ( !(m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
) )
1378 // If showing popup now, do not capture mouse or there will be interference
1382 else if ( type
== wxEVT_LEFT_UP
)
1385 // Only accept event if mouse was left-press was previously accepted
1389 if ( m_btnState
& wxCONTROL_PRESSED
)
1391 // If mouse was inside, fire the click event.
1392 if ( m_iFlags
& wxCC_POPUP_ON_MOUSE_UP
)
1394 if ( flags
& (wxCC_MF_ON_CLICK_AREA
|wxCC_MF_ON_BUTTON
) )
1398 m_btnState
&= ~(wxCONTROL_PRESSED
);
1402 else if ( type
== wxEVT_LEAVE_WINDOW
)
1404 if ( m_btnState
& (wxCONTROL_CURRENT
|wxCONTROL_PRESSED
) )
1406 m_btnState
&= ~(wxCONTROL_CURRENT
);
1409 if ( !m_isPopupShown
)
1411 m_btnState
&= ~(wxCONTROL_PRESSED
);
1422 // returns true if event was consumed or filtered
1423 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent
& event
,
1424 int WXUNUSED(flags
) )
1426 wxLongLong t
= ::wxGetLocalTimeMillis();
1427 int evtType
= event
.GetEventType();
1429 #if USES_WXPOPUPWINDOW || USES_WXDIALOG
1430 if ( m_popupWinType
!= POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1432 if ( m_isPopupShown
&&
1433 ( evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_RIGHT_DOWN
) )
1441 // Filter out clicks on button immediately after popup dismiss (Windows like behaviour)
1442 if ( evtType
== wxEVT_LEFT_DOWN
&& t
< m_timeCanAcceptClick
)
1444 event
.SetEventType(0);
1451 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent
& event
)
1453 int evtType
= event
.GetEventType();
1455 if ( (evtType
== wxEVT_LEFT_DOWN
|| evtType
== wxEVT_LEFT_DCLICK
) &&
1456 (m_windowStyle
& wxCB_READONLY
) )
1458 if ( m_isPopupShown
)
1460 #if USES_WXPOPUPWINDOW
1461 // Click here always hides the popup.
1462 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1468 if ( !(m_windowStyle
& wxCC_SPECIAL_DCLICK
) )
1470 // In read-only mode, clicking the text is the
1471 // same as clicking the button.
1474 else if ( /*evtType == wxEVT_LEFT_UP || */evtType
== wxEVT_LEFT_DCLICK
)
1476 //if ( m_popupInterface->CycleValue() )
1478 if ( m_popupInterface
)
1479 m_popupInterface
->OnComboDoubleClick();
1484 if ( m_isPopupShown
)
1486 // relay (some) mouse events to the popup
1487 if ( evtType
== wxEVT_MOUSEWHEEL
)
1488 m_popup
->AddPendingEvent(event
);
1494 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent
& event
)
1496 if ( IsPopupShown() )
1498 // pass it to the popped up control
1499 GetPopupControl()->GetControl()->AddPendingEvent(event
);
1503 int keycode
= event
.GetKeyCode();
1505 if ( keycode
== WXK_TAB
)
1507 wxNavigationKeyEvent evt
;
1508 evt
.SetFlags(wxNavigationKeyEvent::FromTab
|
1509 (!event
.ShiftDown() ? wxNavigationKeyEvent::IsForward
1510 : wxNavigationKeyEvent::IsBackward
));
1511 evt
.SetEventObject(this);
1512 GetParent()->GetEventHandler()->AddPendingEvent(evt
);
1516 if ( IsKeyPopupToggle(event
) )
1522 int comboStyle
= GetWindowStyle();
1523 wxComboPopup
* popupInterface
= GetPopupControl();
1525 if ( !popupInterface
)
1531 if ( (comboStyle
& wxCB_READONLY
) ||
1532 (keycode
!= WXK_RIGHT
&& keycode
!= WXK_LEFT
) )
1534 popupInterface
->OnComboKeyEvent(event
);
1541 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent
& event
)
1543 if ( event
.GetEventType() == wxEVT_SET_FOCUS
)
1545 if ( m_text
&& m_text
!= ::wxWindow::FindFocus() )
1552 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
1555 // indentation may also have changed
1556 if ( !(m_iFlags
& wxCC_IFLAG_INDENT_SET
) )
1557 m_absIndent
= GetNativeTextIndent();
1561 // ----------------------------------------------------------------------------
1563 // ----------------------------------------------------------------------------
1565 // Create popup window and the child control
1566 void wxComboCtrlBase::CreatePopup()
1568 wxComboPopup
* popupInterface
= m_popupInterface
;
1573 #ifdef wxComboPopupWindowBase2
1574 if ( m_iFlags
& wxCC_IFLAG_USE_ALT_POPUP
)
1577 m_winPopup
= new wxComboPopupWindowBase2( this, wxNO_BORDER
);
1579 m_winPopup
= new wxComboPopupWindowBase2( this, wxID_ANY
, wxEmptyString
,
1580 wxPoint(-21,-21), wxSize(20, 20),
1583 m_popupWinType
= SECONDARY_POPUP_TYPE
;
1588 m_winPopup
= new wxComboPopupWindow( this, wxNO_BORDER
);
1589 m_popupWinType
= PRIMARY_POPUP_TYPE
;
1591 m_popupWinEvtHandler
= new wxComboPopupWindowEvtHandler(this);
1592 m_winPopup
->PushEventHandler(m_popupWinEvtHandler
);
1595 popupInterface
->Create(m_winPopup
);
1596 m_popup
= popup
= popupInterface
->GetControl();
1598 m_popupExtraHandler
= new wxComboPopupExtraEventHandler(this);
1599 popup
->PushEventHandler( m_popupExtraHandler
);
1601 // This may be helpful on some platforms
1602 // (eg. it bypasses a wxGTK popupwindow bug where
1603 // window is not initially hidden when it should be)
1606 popupInterface
->m_iFlags
|= wxCP_IFLAG_CREATED
;
1609 // Destroy popup window and the child control
1610 void wxComboCtrlBase::DestroyPopup()
1615 m_popup
->RemoveEventHandler(m_popupExtraHandler
);
1617 delete m_popupExtraHandler
;
1619 delete m_popupInterface
;
1623 m_winPopup
->RemoveEventHandler(m_popupWinEvtHandler
);
1624 delete m_popupWinEvtHandler
;
1625 m_popupWinEvtHandler
= NULL
;
1626 m_winPopup
->Destroy();
1629 m_popupExtraHandler
= (wxEvtHandler
*) NULL
;
1630 m_popupInterface
= (wxComboPopup
*) NULL
;
1631 m_winPopup
= (wxWindow
*) NULL
;
1632 m_popup
= (wxWindow
*) NULL
;
1635 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup
* iface
)
1637 wxCHECK_RET( iface
, wxT("no popup interface set for wxComboCtrl") );
1641 iface
->InitBase(this);
1644 m_popupInterface
= iface
;
1646 if ( !iface
->LazyCreate() )
1652 m_popup
= (wxWindow
*) NULL
;
1655 // This must be done after creation
1656 if ( m_valueString
.length() )
1658 iface
->SetStringValue(m_valueString
);
1663 // Ensures there is atleast the default popup
1664 void wxComboCtrlBase::EnsurePopupControl()
1666 if ( !m_popupInterface
)
1667 SetPopupControl(NULL
);
1670 void wxComboCtrlBase::OnButtonClick()
1672 // Derived classes can override this method for totally custom
1677 void wxComboCtrlBase::ShowPopup()
1679 EnsurePopupControl();
1680 wxCHECK_RET( !IsPopupShown(), wxT("popup window already shown") );
1684 // Space above and below
1690 wxSize ctrlSz
= GetSize();
1692 screenHeight
= wxSystemSettings::GetMetric( wxSYS_SCREEN_Y
);
1693 scrPos
= GetParent()->ClientToScreen(GetPosition());
1695 spaceAbove
= scrPos
.y
;
1696 spaceBelow
= screenHeight
- spaceAbove
- ctrlSz
.y
;
1698 maxHeightPopup
= spaceBelow
;
1699 if ( spaceAbove
> spaceBelow
)
1700 maxHeightPopup
= spaceAbove
;
1703 int widthPopup
= ctrlSz
.x
+ m_extLeft
+ m_extRight
;
1705 if ( widthPopup
< m_widthMinPopup
)
1706 widthPopup
= m_widthMinPopup
;
1708 wxWindow
* winPopup
= m_winPopup
;
1711 // Need to disable tab traversal of parent
1713 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1714 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1715 // that if transient popup is open, then tab traversal is to be ignored.
1716 // However, I think this code would still be needed for cases where
1717 // transient popup doesn't work yet (wxWinCE?).
1718 wxWindow
* parent
= GetParent();
1719 int parentFlags
= parent
->GetWindowStyle();
1720 if ( parentFlags
& wxTAB_TRAVERSAL
)
1722 parent
->SetWindowStyle( parentFlags
& ~(wxTAB_TRAVERSAL
) );
1723 m_iFlags
|= wxCC_IFLAG_PARENT_TAB_TRAVERSAL
;
1729 winPopup
= m_winPopup
;
1737 wxASSERT( !m_popup
|| m_popup
== popup
); // Consistency check.
1739 wxSize adjustedSize
= m_popupInterface
->GetAdjustedSize(widthPopup
,
1740 m_heightPopup
<=0?DEFAULT_POPUP_HEIGHT
:m_heightPopup
,
1743 popup
->SetSize(adjustedSize
);
1745 m_popupInterface
->OnPopup();
1748 // Reposition and resize popup window
1751 wxSize szp
= popup
->GetSize();
1754 int popupY
= scrPos
.y
+ ctrlSz
.y
;
1756 // Default anchor is wxLEFT
1757 int anchorSide
= m_anchorSide
;
1759 anchorSide
= wxLEFT
;
1761 int rightX
= scrPos
.x
+ ctrlSz
.x
+ m_extRight
- szp
.x
;
1762 int leftX
= scrPos
.x
- m_extLeft
;
1763 int screenWidth
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
);
1765 // If there is not enough horizontal space, anchor on the other side.
1766 // If there is no space even then, place the popup at x 0.
1767 if ( anchorSide
== wxRIGHT
)
1771 if ( (leftX
+szp
.x
) < screenWidth
)
1772 anchorSide
= wxLEFT
;
1779 if ( (leftX
+szp
.x
) >= screenWidth
)
1782 anchorSide
= wxRIGHT
;
1788 // Select x coordinate according to the anchor side
1789 if ( anchorSide
== wxRIGHT
)
1791 else if ( anchorSide
== wxLEFT
)
1796 if ( spaceBelow
< szp
.y
)
1798 popupY
= scrPos
.y
- szp
.y
;
1802 //wxLogDebug(wxT("popup scheduled position1: %i,%i"),ptp.x,ptp.y);
1803 //wxLogDebug(wxT("popup position1: %i,%i"),winPopup->GetPosition().x,winPopup->GetPosition().y);
1805 // Some platforms (GTK) may need these two to be separate
1806 winPopup
->SetSize( szp
.x
, szp
.y
);
1807 winPopup
->Move( popupX
, popupY
);
1809 //wxLogDebug(wxT("popup position2: %i,%i"),winPopup->GetPosition().x,winPopup->GetPosition().y);
1813 // Set string selection (must be this way instead of SetStringSelection)
1816 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
1817 m_text
->SelectAll();
1819 m_popupInterface
->SetStringValue( m_text
->GetValue() );
1823 // This is neede since focus/selection indication may change when popup is shown
1827 // This must be after SetStringValue
1828 m_isPopupShown
= true;
1831 #if USES_WXPOPUPTRANSIENTWINDOW
1832 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1833 ((wxPopupTransientWindow
*)winPopup
)->Popup(popup
);
1838 #if INSTALL_TOPLEV_HANDLER
1839 // Put top level window event handler into place
1840 if ( m_popupWinType
== POPUPWIN_WXPOPUPWINDOW
)
1842 if ( !m_toplevEvtHandler
)
1843 m_toplevEvtHandler
= new wxComboFrameEventHandler(this);
1845 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
1847 ((wxComboFrameEventHandler
*)m_toplevEvtHandler
)->OnPopup();
1848 toplev
->PushEventHandler( m_toplevEvtHandler
);
1854 void wxComboCtrlBase::OnPopupDismiss()
1856 // Just in case, avoid double dismiss
1857 if ( !m_isPopupShown
)
1860 // NB: Focus setting is really funny, atleast on wxMSW. First of all,
1861 // we need to have SetFocus at the end. Otherwise wxTextCtrl may
1862 // freeze until focus goes somewhere else. Second, wxTreeCtrl as
1863 // popup, when dismissing, "steals" focus back to itself unless
1864 // SetFocus is called also here, exactly before m_isPopupShown
1865 // is set to false. Which is truly weird since SetFocus is just
1866 // wxWindowMSW method and does not call event handler or anything like
1867 // that (ie. does not care about m_isPopupShown).
1871 // This should preferably be set before focus.
1872 m_isPopupShown
= false;
1874 // Inform popup control itself
1875 m_popupInterface
->OnDismiss();
1877 if ( m_popupExtraHandler
)
1878 ((wxComboPopupExtraEventHandler
*)m_popupExtraHandler
)->OnPopupDismiss();
1880 #if INSTALL_TOPLEV_HANDLER
1881 // Remove top level window event handler
1882 if ( m_toplevEvtHandler
)
1884 wxWindow
* toplev
= ::wxGetTopLevelParent( this );
1886 toplev
->RemoveEventHandler( m_toplevEvtHandler
);
1890 m_timeCanAcceptClick
= ::wxGetLocalTimeMillis() + 150;
1892 // If cursor not on dropdown button, then clear its state
1893 // (technically not required by all ports, but do it for all just in case)
1894 if ( !m_btnArea
.Contains(ScreenToClient(::wxGetMousePosition())) )
1897 // Return parent's tab traversal flag.
1898 // See ShowPopup for notes.
1899 if ( m_iFlags
& wxCC_IFLAG_PARENT_TAB_TRAVERSAL
)
1901 wxWindow
* parent
= GetParent();
1902 parent
->SetWindowStyle( parent
->GetWindowStyle() | wxTAB_TRAVERSAL
);
1903 m_iFlags
&= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL
);
1906 // refresh control (necessary even if m_text)
1912 void wxComboCtrlBase::HidePopup()
1914 // Should be able to call this without popup interface
1915 //wxCHECK_RET( m_popupInterface, _T("no popup interface") );
1916 if ( !m_isPopupShown
)
1919 // transfer value and show it in textctrl, if any
1920 SetValue( m_popupInterface
->GetStringValue() );
1922 #if USES_WXPOPUPTRANSIENTWINDOW
1923 if ( m_popupWinType
== POPUPWIN_WXPOPUPTRANSIENTWINDOW
)
1924 ((wxPopupTransientWindow
*)m_winPopup
)->Dismiss();
1932 // ----------------------------------------------------------------------------
1933 // customization methods
1934 // ----------------------------------------------------------------------------
1936 void wxComboCtrlBase::SetButtonPosition( int width
, int height
,
1937 int side
, int spacingX
)
1942 m_btnSpacingX
= spacingX
;
1947 wxSize
wxComboCtrlBase::GetButtonSize()
1949 if ( m_btnSize
.x
> 0 )
1952 wxSize
retSize(m_btnWid
,m_btnHei
);
1954 // Need to call CalculateAreas now if button size is
1955 // is not explicitly specified.
1956 if ( retSize
.x
<= 0 || retSize
.y
<= 0)
1960 retSize
= m_btnSize
;
1966 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap
& bmpNormal
,
1968 const wxBitmap
& bmpPressed
,
1969 const wxBitmap
& bmpHover
,
1970 const wxBitmap
& bmpDisabled
)
1972 m_bmpNormal
= bmpNormal
;
1973 m_blankButtonBg
= blankButtonBg
;
1975 if ( bmpPressed
.Ok() )
1976 m_bmpPressed
= bmpPressed
;
1978 m_bmpPressed
= bmpNormal
;
1980 if ( bmpHover
.Ok() )
1981 m_bmpHover
= bmpHover
;
1983 m_bmpHover
= bmpNormal
;
1985 if ( bmpDisabled
.Ok() )
1986 m_bmpDisabled
= bmpDisabled
;
1988 m_bmpDisabled
= bmpNormal
;
1993 void wxComboCtrlBase::SetCustomPaintWidth( int width
)
1997 // move textctrl accordingly
1998 wxRect r
= m_text
->GetRect();
1999 int inc
= width
- m_widthCustomPaint
;
2002 m_text
->SetSize( r
);
2005 m_widthCustomPaint
= width
;
2010 void wxComboCtrlBase::SetTextIndent( int indent
)
2014 m_absIndent
= GetNativeTextIndent();
2015 m_iFlags
&= ~(wxCC_IFLAG_INDENT_SET
);
2019 m_absIndent
= indent
;
2020 m_iFlags
|= wxCC_IFLAG_INDENT_SET
;
2026 wxCoord
wxComboCtrlBase::GetNativeTextIndent() const
2028 return DEFAULT_TEXT_INDENT
;
2031 // ----------------------------------------------------------------------------
2032 // methods forwarded to wxTextCtrl
2033 // ----------------------------------------------------------------------------
2035 wxString
wxComboCtrlBase::GetValue() const
2038 return m_text
->GetValue();
2039 return m_valueString
;
2042 void wxComboCtrlBase::SetValueWithEvent(const wxString
& value
, bool withEvent
)
2049 m_text
->SetValue(value
);
2050 if ( !(m_iFlags
& wxCC_NO_TEXT_AUTO_SELECT
) )
2051 m_text
->SelectAll();
2054 m_valueString
= value
;
2058 // Since wxComboPopup may want to paint the combo as well, we need
2059 // to set the string value here (as well as sometimes in ShowPopup).
2060 if ( m_valueString
!= value
&& m_popupInterface
)
2062 m_popupInterface
->SetStringValue(value
);
2066 void wxComboCtrlBase::SetValue(const wxString
& value
)
2068 SetValueWithEvent(value
, false);
2071 // In this SetValue variant wxComboPopup::SetStringValue is not called
2072 void wxComboCtrlBase::SetText(const wxString
& value
)
2074 // Unlike in SetValue(), this must be called here or
2075 // the behaviour will no be consistent in readonlys.
2076 EnsurePopupControl();
2078 m_valueString
= value
;
2083 m_text
->SetValue( value
);
2089 void wxComboCtrlBase::Copy()
2095 void wxComboCtrlBase::Cut()
2101 void wxComboCtrlBase::Paste()
2107 void wxComboCtrlBase::SetInsertionPoint(long pos
)
2110 m_text
->SetInsertionPoint(pos
);
2113 void wxComboCtrlBase::SetInsertionPointEnd()
2116 m_text
->SetInsertionPointEnd();
2119 long wxComboCtrlBase::GetInsertionPoint() const
2122 return m_text
->GetInsertionPoint();
2127 long wxComboCtrlBase::GetLastPosition() const
2130 return m_text
->GetLastPosition();
2135 void wxComboCtrlBase::Replace(long from
, long to
, const wxString
& value
)
2138 m_text
->Replace(from
, to
, value
);
2141 void wxComboCtrlBase::Remove(long from
, long to
)
2144 m_text
->Remove(from
, to
);
2147 void wxComboCtrlBase::SetSelection(long from
, long to
)
2150 m_text
->SetSelection(from
, to
);
2153 void wxComboCtrlBase::Undo()
2159 #endif // wxUSE_COMBOCTRL