1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/popupcmn.cpp
3 // Purpose: implementation of wxPopupTransientWindow
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/popupwin.h"
32 #include "wx/combobox.h" // wxComboCtrl
33 #include "wx/app.h" // wxPostEvent
37 #include "wx/recguard.h"
39 #ifdef __WXUNIVERSAL__
40 #include "wx/univ/renderer.h"
41 #include "wx/scrolbar.h"
42 #endif // __WXUNIVERSAL__
46 #elif defined(__WXMSW__)
47 #include "wx/msw/private.h"
48 #elif defined(__WXX11__)
49 #include "wx/x11/private.h"
52 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow
, wxWindow
)
53 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow
, wxPopupWindow
)
55 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
56 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow
, wxPopupTransientWindow
)
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // event handlers which we use to intercept events which cause the popup to
65 class wxPopupWindowHandler
: public wxEvtHandler
68 wxPopupWindowHandler(wxPopupTransientWindow
*popup
) : m_popup(popup
) {}
72 void OnLeftDown(wxMouseEvent
& event
);
75 wxPopupTransientWindow
*m_popup
;
78 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler
)
81 class wxPopupFocusHandler
: public wxEvtHandler
84 wxPopupFocusHandler(wxPopupTransientWindow
*popup
) : m_popup(popup
) {}
87 void OnKillFocus(wxFocusEvent
& event
);
88 void OnKeyDown(wxKeyEvent
& event
);
91 wxPopupTransientWindow
*m_popup
;
94 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler
)
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
102 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
105 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
106 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
107 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown
)
110 BEGIN_EVENT_TABLE(wxPopupTransientWindow
, wxPopupWindow
)
111 #if defined( __WXMSW__ ) || defined( __WXMAC__)
112 EVT_IDLE(wxPopupTransientWindow::OnIdle
)
116 // ============================================================================
118 // ============================================================================
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 wxPopupWindowBase::~wxPopupWindowBase()
126 // this destructor is required for Darwin
129 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
134 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
137 wxSize sizeScreen
= wxGetDisplaySize(),
138 sizeSelf
= GetSize();
140 // is there enough space to put the popup below the window (where we put it
142 wxCoord y
= ptOrigin
.y
+ size
.y
;
143 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
145 // check if there is enough space above
146 if ( ptOrigin
.y
> sizeSelf
.y
)
148 // do position the control above the window
149 y
-= size
.y
+ sizeSelf
.y
;
151 //else: not enough space below nor above, leave below
154 // now check left/right too
155 wxCoord x
= ptOrigin
.x
;
157 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
159 // shift the window to the left instead of the right.
161 x
-= sizeSelf
.x
; // also shift it by window width.
167 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
169 // check if there is enough space to the left
170 if ( ptOrigin
.x
> sizeSelf
.x
)
172 // do position the control to the left
173 x
-= size
.x
+ sizeSelf
.x
;
175 //else: not enough space there neither, leave in default position
178 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
181 // ----------------------------------------------------------------------------
182 // wxPopupTransientWindow
183 // ----------------------------------------------------------------------------
185 void wxPopupTransientWindow::Init()
190 m_handlerFocus
= NULL
;
191 m_handlerPopup
= NULL
;
194 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
198 (void)Create(parent
, style
);
201 wxPopupTransientWindow::~wxPopupTransientWindow()
203 if (m_handlerPopup
&& m_handlerPopup
->GetNextHandler())
206 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
207 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
209 delete m_handlerFocus
;
210 delete m_handlerPopup
;
213 void wxPopupTransientWindow::PopHandlers()
217 if ( !m_child
->RemoveEventHandler(m_handlerPopup
) )
219 // something is very wrong and someone else probably deleted our
220 // handler - so don't risk deleting it second time
221 m_handlerPopup
= NULL
;
223 if (m_child
->HasCapture())
225 m_child
->ReleaseMouse();
232 if ( !m_focus
->RemoveEventHandler(m_handlerFocus
) )
235 m_handlerFocus
= NULL
;
241 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
243 const wxWindowList
& children
= GetChildren();
244 if ( children
.GetCount() )
246 m_child
= children
.GetFirst()->GetData();
255 // There is is a problem if these are still in use
256 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
257 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
260 m_handlerPopup
= new wxPopupWindowHandler(this);
262 m_child
->PushEventHandler(m_handlerPopup
);
264 #if defined(__WXMSW__)
265 // Focusing on child of popup window does not work on MSW unless WS_POPUP
266 // style is set. We do not even want to try to set the focus, as it may
267 // provoke errors on some Windows versions (Vista and later).
268 if ( ::GetWindowLong(GetHwnd(), GWL_STYLE
) & WS_POPUP
)
271 m_focus
= winFocus
? winFocus
: this;
275 #if defined( __WXMSW__ ) || defined( __WXMAC__)
276 // MSW doesn't allow to set focus to the popup window, but we need to
277 // subclass the window which has the focus, and not winFocus passed in or
278 // otherwise everything else breaks down
279 m_focus
= FindFocus();
280 #elif defined(__WXGTK__)
281 // GTK+ catches the activate events from the popup
282 // window, not the focus events from the child window
289 m_handlerFocus
= new wxPopupFocusHandler(this);
291 m_focus
->PushEventHandler(m_handlerFocus
);
295 bool wxPopupTransientWindow::Show( bool show
)
300 gdk_pointer_ungrab( (guint32
)GDK_CURRENT_TIME
);
302 gtk_grab_remove( m_widget
);
309 XUngrabPointer( wxGlobalDisplay(), CurrentTime
);
313 #if defined( __WXMSW__ ) || defined( __WXMAC__)
314 if (!show
&& m_child
&& m_child
->HasCapture())
316 m_child
->ReleaseMouse();
320 bool ret
= wxPopupWindow::Show( show
);
325 gtk_grab_add( m_widget
);
327 gdk_pointer_grab( m_widget
->window
, TRUE
,
329 (GDK_BUTTON_PRESS_MASK
|
330 GDK_BUTTON_RELEASE_MASK
|
331 GDK_POINTER_MOTION_HINT_MASK
|
332 GDK_POINTER_MOTION_MASK
),
335 (guint32
)GDK_CURRENT_TIME
);
342 Window xwindow
= (Window
) m_clientWindow
;
344 /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow
,
346 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
,
355 #if defined( __WXMSW__ ) || defined( __WXMAC__)
358 // Assume that the mouse is outside the popup to begin with
359 m_child
->CaptureMouse();
366 void wxPopupTransientWindow::Dismiss()
372 void wxPopupTransientWindow::DismissAndNotify()
378 void wxPopupTransientWindow::OnDismiss()
380 // nothing to do here - but it may be interesting for derived class
383 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
385 // no special processing here
389 #if defined( __WXMSW__ ) || defined( __WXMAC__)
390 void wxPopupTransientWindow::OnIdle(wxIdleEvent
& event
)
394 if (IsShown() && m_child
)
396 wxPoint pos
= ScreenToClient(wxGetMousePosition());
397 wxRect
rect(GetSize());
399 if ( rect
.Contains(pos
) )
401 if ( m_child
->HasCapture() )
403 m_child
->ReleaseMouse();
408 if ( !m_child
->HasCapture() )
410 m_child
->CaptureMouse();
418 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
420 // ----------------------------------------------------------------------------
421 // wxPopupComboWindow
422 // ----------------------------------------------------------------------------
424 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
425 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
428 wxPopupComboWindow::wxPopupComboWindow(wxComboCtrl
*parent
)
429 : wxPopupTransientWindow(parent
)
434 bool wxPopupComboWindow::Create(wxComboCtrl
*parent
)
438 return wxPopupWindow::Create(parent
);
441 void wxPopupComboWindow::PositionNearCombo()
443 // the origin point must be in screen coords
444 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0,0));
446 #if 0 //def __WXUNIVERSAL__
447 // account for the fact that (0, 0) is not the top left corner of the
448 // window: there is also the border
449 wxRect rectBorders
= m_combo
->GetRenderer()->
450 GetBorderDimensions(m_combo
->GetBorder());
451 ptOrigin
.x
-= rectBorders
.x
;
452 ptOrigin
.y
-= rectBorders
.y
;
453 #endif // __WXUNIVERSAL__
455 // position below or above the combobox: the width is 0 to put it exactly
456 // below us, not to the left or to the right
457 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
460 void wxPopupComboWindow::OnDismiss()
462 m_combo
->OnPopupDismiss();
465 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
467 m_combo
->ProcessWindowEvent(event
);
470 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
472 // ----------------------------------------------------------------------------
473 // wxPopupWindowHandler
474 // ----------------------------------------------------------------------------
476 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
478 // let the window have it first (we're the first event handler in the chain
479 // of handlers for this window)
480 if ( m_popup
->ProcessLeftDown(event
) )
485 wxPoint pos
= event
.GetPosition();
487 // in non-Univ ports the system manages scrollbars for us
488 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
489 // scrollbar on which the click occurred
490 wxWindow
*sbar
= NULL
;
491 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
493 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
495 switch ( win
->HitTest(pos
.x
, pos
.y
) )
497 case wxHT_WINDOW_OUTSIDE
:
499 // do the coords translation now as after DismissAndNotify()
500 // m_popup may be destroyed
501 wxMouseEvent
event2(event
);
503 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
505 // clicking outside a popup dismisses it
506 m_popup
->DismissAndNotify();
508 // dismissing a tooltip shouldn't waste a click, i.e. you
509 // should be able to dismiss it and press the button with the
510 // same click, so repost this event to the window beneath us
511 wxWindow
*winUnder
= wxFindWindowAtPoint(event2
.GetPosition());
514 // translate the event coords to the ones of the window
515 // which is going to get the event
516 winUnder
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
518 event2
.SetEventObject(winUnder
);
519 wxPostEvent(winUnder
->GetEventHandler(), event2
);
524 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
525 case wxHT_WINDOW_HORZ_SCROLLBAR
:
526 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
529 case wxHT_WINDOW_VERT_SCROLLBAR
:
530 sbar
= win
->GetScrollbar(wxVERTICAL
);
532 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
535 // forgot to update the switch after adding a new hit test code?
536 wxFAIL_MSG( _T("unexpected HitTest() return value") );
539 case wxHT_WINDOW_CORNER
:
540 // don't actually know if this one is good for anything, but let it
543 case wxHT_WINDOW_INSIDE
:
544 // let the normal processing take place
549 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
552 // translate the event coordinates to the scrollbar ones
553 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
555 // and give the event to it
556 wxMouseEvent event2
= event
;
560 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
562 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
565 // ----------------------------------------------------------------------------
566 // wxPopupFocusHandler
567 // ----------------------------------------------------------------------------
569 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
571 // when we lose focus we always disappear - unless it goes to the popup (in
572 // which case we don't really lose it)
573 wxWindow
*win
= event
.GetWindow();
576 if ( win
== m_popup
)
578 win
= win
->GetParent();
581 m_popup
->DismissAndNotify();
584 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
586 // we can be associated with the popup itself in which case we should avoid
587 // infinite recursion
589 wxRecursionGuard
guard(s_inside
);
590 if ( guard
.IsInside() )
596 // let the window have it first, it might process the keys
597 if ( !m_popup
->GetEventHandler()->ProcessEvent(event
) )
599 // by default, dismiss the popup
600 m_popup
->DismissAndNotify();
604 #endif // wxUSE_POPUPWIN