1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/popupcmn.cpp
3 // Purpose: implementation of wxPopupTransientWindow
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/popupwin.h"
31 #include "wx/combobox.h" // wxComboCtrl
32 #include "wx/app.h" // wxPostEvent
36 #include "wx/display.h"
37 #include "wx/recguard.h"
39 #ifdef __WXUNIVERSAL__
40 #include "wx/univ/renderer.h"
41 #include "wx/scrolbar.h"
42 #endif // __WXUNIVERSAL__
46 #if GTK_CHECK_VERSION(2,0,0)
47 #include "wx/gtk/private/gtk2-compat.h"
49 #define gtk_widget_get_window(x) x->window
51 #elif defined(__WXMSW__)
52 #include "wx/msw/private.h"
53 #elif defined(__WXX11__)
54 #include "wx/x11/private.h"
57 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow
, wxWindow
)
58 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow
, wxPopupWindow
)
60 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
61 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow
, wxPopupTransientWindow
)
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // event handlers which we use to intercept events which cause the popup to
70 class wxPopupWindowHandler
: public wxEvtHandler
73 wxPopupWindowHandler(wxPopupTransientWindow
*popup
) : m_popup(popup
) {}
77 void OnLeftDown(wxMouseEvent
& event
);
78 void OnCaptureLost(wxMouseCaptureLostEvent
& event
);
81 wxPopupTransientWindow
*m_popup
;
84 wxDECLARE_NO_COPY_CLASS(wxPopupWindowHandler
);
87 class wxPopupFocusHandler
: public wxEvtHandler
90 wxPopupFocusHandler(wxPopupTransientWindow
*popup
) : m_popup(popup
) {}
93 void OnKillFocus(wxFocusEvent
& event
);
94 void OnChar(wxKeyEvent
& event
);
97 wxPopupTransientWindow
*m_popup
;
100 wxDECLARE_NO_COPY_CLASS(wxPopupFocusHandler
);
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
108 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
109 EVT_MOUSE_CAPTURE_LOST(wxPopupWindowHandler::OnCaptureLost
)
112 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
113 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
114 EVT_CHAR(wxPopupFocusHandler::OnChar
)
117 BEGIN_EVENT_TABLE(wxPopupTransientWindow
, wxPopupWindow
)
118 #if defined(__WXMSW__) || (defined(__WXMAC__) && wxOSX_USE_COCOA_OR_CARBON)
119 EVT_IDLE(wxPopupTransientWindow::OnIdle
)
123 // ============================================================================
125 // ============================================================================
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 wxPopupWindowBase::~wxPopupWindowBase()
133 // this destructor is required for Darwin
136 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
141 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
144 // determine the position and size of the screen we clamp the popup to
148 const int displayNum
= wxDisplay::GetFromPoint(ptOrigin
);
149 if ( displayNum
!= wxNOT_FOUND
)
151 const wxRect rectScreen
= wxDisplay(displayNum
).GetGeometry();
152 posScreen
= rectScreen
.GetPosition();
153 sizeScreen
= rectScreen
.GetSize();
155 else // outside of any display?
157 // just use the primary one then
158 posScreen
= wxPoint(0, 0);
159 sizeScreen
= wxGetDisplaySize();
163 const wxSize sizeSelf
= GetSize();
165 // is there enough space to put the popup below the window (where we put it
167 wxCoord y
= ptOrigin
.y
+ size
.y
;
168 if ( y
+ sizeSelf
.y
> posScreen
.y
+ sizeScreen
.y
)
170 // check if there is enough space above
171 if ( ptOrigin
.y
> sizeSelf
.y
)
173 // do position the control above the window
174 y
-= size
.y
+ sizeSelf
.y
;
176 //else: not enough space below nor above, leave below
179 // now check left/right too
180 wxCoord x
= ptOrigin
.x
;
182 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
184 // shift the window to the left instead of the right.
186 x
-= sizeSelf
.x
; // also shift it by window width.
192 if ( x
+ sizeSelf
.x
> posScreen
.x
+ sizeScreen
.x
)
194 // check if there is enough space to the left
195 if ( ptOrigin
.x
> sizeSelf
.x
)
197 // do position the control to the left
198 x
-= size
.x
+ sizeSelf
.x
;
200 //else: not enough space there neither, leave in default position
203 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
206 // ----------------------------------------------------------------------------
207 // wxPopupTransientWindow
208 // ----------------------------------------------------------------------------
210 void wxPopupTransientWindow::Init()
215 m_handlerFocus
= NULL
;
216 m_handlerPopup
= NULL
;
219 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
223 (void)Create(parent
, style
);
226 wxPopupTransientWindow::~wxPopupTransientWindow()
228 if (m_handlerPopup
&& m_handlerPopup
->GetNextHandler())
231 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
232 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
234 delete m_handlerFocus
;
235 delete m_handlerPopup
;
238 void wxPopupTransientWindow::PopHandlers()
242 if ( !m_child
->RemoveEventHandler(m_handlerPopup
) )
244 // something is very wrong and someone else probably deleted our
245 // handler - so don't risk deleting it second time
246 m_handlerPopup
= NULL
;
248 if (m_child
->HasCapture())
250 m_child
->ReleaseMouse();
257 if ( !m_focus
->RemoveEventHandler(m_handlerFocus
) )
260 m_handlerFocus
= NULL
;
266 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
268 // If we have a single child, we suppose that it must cover the entire
269 // popup window and hence we give the mouse capture to it instead of
270 // keeping it for ourselves.
272 // Notice that this works best for combobox-like popups which have a single
273 // control inside them and not so well for popups containing a single
274 // wxPanel with multiple children inside it but OTOH it does no harm in
275 // this case neither and we can't reliably distinguish between them.
276 const wxWindowList
& children
= GetChildren();
277 if ( children
.GetCount() == 1 )
279 m_child
= children
.GetFirst()->GetData();
288 // There is a problem if these are still in use
289 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
290 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
293 m_handlerPopup
= new wxPopupWindowHandler(this);
295 m_child
->PushEventHandler(m_handlerPopup
);
297 #if defined(__WXMSW__)
298 // Focusing on child of popup window does not work on MSW unless WS_POPUP
299 // style is set. We do not even want to try to set the focus, as it may
300 // provoke errors on some Windows versions (Vista and later).
301 if ( ::GetWindowLong(GetHwnd(), GWL_STYLE
) & WS_POPUP
)
304 m_focus
= winFocus
? winFocus
: this;
308 #if defined( __WXMSW__ ) || (defined( __WXMAC__) && wxOSX_USE_COCOA_OR_CARBON)
309 // MSW doesn't allow to set focus to the popup window, but we need to
310 // subclass the window which has the focus, and not winFocus passed in or
311 // otherwise everything else breaks down
312 m_focus
= FindFocus();
313 #elif defined(__WXGTK__)
314 // GTK+ catches the activate events from the popup
315 // window, not the focus events from the child window
322 m_handlerFocus
= new wxPopupFocusHandler(this);
324 m_focus
->PushEventHandler(m_handlerFocus
);
328 bool wxPopupTransientWindow::Show( bool show
)
334 GdkDisplay
* display
= gtk_widget_get_display(m_widget
);
335 GdkDeviceManager
* manager
= gdk_display_get_device_manager(display
);
336 GdkDevice
* device
= gdk_device_manager_get_client_pointer(manager
);
337 gdk_device_ungrab(device
, unsigned(GDK_CURRENT_TIME
));
339 gdk_pointer_ungrab( (guint32
)GDK_CURRENT_TIME
);
342 gtk_grab_remove( m_widget
);
349 XUngrabPointer( wxGlobalDisplay(), CurrentTime
);
353 #if defined( __WXMSW__ ) || defined( __WXMAC__)
354 if (!show
&& m_child
&& m_child
->HasCapture())
356 m_child
->ReleaseMouse();
360 bool ret
= wxPopupWindow::Show( show
);
365 gtk_grab_add( m_widget
);
367 const GdkEventMask mask
= GdkEventMask(
368 GDK_BUTTON_PRESS_MASK
|
369 GDK_BUTTON_RELEASE_MASK
|
370 GDK_POINTER_MOTION_HINT_MASK
|
371 GDK_POINTER_MOTION_MASK
);
372 GdkWindow
* window
= gtk_widget_get_window(m_widget
);
374 GdkDisplay
* display
= gdk_window_get_display(window
);
375 GdkDeviceManager
* manager
= gdk_display_get_device_manager(display
);
376 GdkDevice
* device
= gdk_device_manager_get_client_pointer(manager
);
377 gdk_device_grab(device
, window
,
378 GDK_OWNERSHIP_NONE
, false, mask
, NULL
, unsigned(GDK_CURRENT_TIME
));
380 gdk_pointer_grab( window
, true,
384 (guint32
)GDK_CURRENT_TIME
);
392 Window xwindow
= (Window
) m_clientWindow
;
394 /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow
,
396 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
,
405 #if defined( __WXMSW__ ) || defined( __WXMAC__)
408 // Assume that the mouse is outside the popup to begin with
409 m_child
->CaptureMouse();
416 bool wxPopupTransientWindow::Destroy()
418 // The popup window can be deleted at any moment, even while some events
419 // are still being processed for it, so delay its real destruction until
420 // the next idle time when we're sure that it's safe to really destroy it.
422 wxCHECK_MSG( !wxPendingDelete
.Member(this), false,
423 wxS("Shouldn't destroy the popup twice.") );
425 wxPendingDelete
.Append(this);
430 void wxPopupTransientWindow::Dismiss()
436 void wxPopupTransientWindow::DismissAndNotify()
442 void wxPopupTransientWindow::OnDismiss()
444 // nothing to do here - but it may be interesting for derived class
447 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
449 // no special processing here
453 #if defined(__WXMSW__) ||(defined(__WXMAC__) && wxOSX_USE_COCOA_OR_CARBON)
454 void wxPopupTransientWindow::OnIdle(wxIdleEvent
& event
)
458 if (IsShown() && m_child
)
460 // Store the last mouse position to minimize the number of calls to
461 // wxFindWindowAtPoint() which are quite expensive.
462 static wxPoint s_posLast
;
463 const wxPoint pos
= wxGetMousePosition();
464 if ( pos
!= s_posLast
)
468 wxWindow
* const winUnderMouse
= wxFindWindowAtPoint(pos
);
470 // We release the mouse capture while the mouse is inside the popup
471 // itself to allow using it normally with the controls inside it.
472 if ( wxGetTopLevelParent(winUnderMouse
) == this )
474 if ( m_child
->HasCapture() )
476 m_child
->ReleaseMouse();
479 else // And we reacquire it as soon as the mouse goes outside.
481 if ( !m_child
->HasCapture() )
483 m_child
->CaptureMouse();
489 #endif // wxOSX/Carbon
492 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
494 // ----------------------------------------------------------------------------
495 // wxPopupComboWindow
496 // ----------------------------------------------------------------------------
498 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
499 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
502 wxPopupComboWindow::wxPopupComboWindow(wxComboCtrl
*parent
)
503 : wxPopupTransientWindow(parent
)
508 bool wxPopupComboWindow::Create(wxComboCtrl
*parent
)
512 return wxPopupWindow::Create(parent
);
515 void wxPopupComboWindow::PositionNearCombo()
517 // the origin point must be in screen coords
518 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0,0));
520 #if 0 //def __WXUNIVERSAL__
521 // account for the fact that (0, 0) is not the top left corner of the
522 // window: there is also the border
523 wxRect rectBorders
= m_combo
->GetRenderer()->
524 GetBorderDimensions(m_combo
->GetBorder());
525 ptOrigin
.x
-= rectBorders
.x
;
526 ptOrigin
.y
-= rectBorders
.y
;
527 #endif // __WXUNIVERSAL__
529 // position below or above the combobox: the width is 0 to put it exactly
530 // below us, not to the left or to the right
531 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
534 void wxPopupComboWindow::OnDismiss()
536 m_combo
->OnPopupDismiss(true);
539 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
541 m_combo
->ProcessWindowEvent(event
);
544 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
546 // ----------------------------------------------------------------------------
547 // wxPopupWindowHandler
548 // ----------------------------------------------------------------------------
550 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
552 // let the window have it first (we're the first event handler in the chain
553 // of handlers for this window)
554 if ( m_popup
->ProcessLeftDown(event
) )
559 wxPoint pos
= event
.GetPosition();
561 // in non-Univ ports the system manages scrollbars for us
562 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
563 // scrollbar on which the click occurred
564 wxWindow
*sbar
= NULL
;
565 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
567 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
569 switch ( win
->HitTest(pos
.x
, pos
.y
) )
571 case wxHT_WINDOW_OUTSIDE
:
573 // do the coords translation now as after DismissAndNotify()
574 // m_popup may be destroyed
575 wxMouseEvent
event2(event
);
577 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
579 // clicking outside a popup dismisses it
580 m_popup
->DismissAndNotify();
582 // dismissing a tooltip shouldn't waste a click, i.e. you
583 // should be able to dismiss it and press the button with the
584 // same click, so repost this event to the window beneath us
585 wxWindow
*winUnder
= wxFindWindowAtPoint(event2
.GetPosition());
588 // translate the event coords to the ones of the window
589 // which is going to get the event
590 winUnder
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
592 event2
.SetEventObject(winUnder
);
593 wxPostEvent(winUnder
->GetEventHandler(), event2
);
598 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
599 case wxHT_WINDOW_HORZ_SCROLLBAR
:
600 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
603 case wxHT_WINDOW_VERT_SCROLLBAR
:
604 sbar
= win
->GetScrollbar(wxVERTICAL
);
606 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
609 // forgot to update the switch after adding a new hit test code?
610 wxFAIL_MSG( wxT("unexpected HitTest() return value") );
613 case wxHT_WINDOW_CORNER
:
614 // don't actually know if this one is good for anything, but let it
617 case wxHT_WINDOW_INSIDE
:
618 // let the normal processing take place
623 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
626 // translate the event coordinates to the scrollbar ones
627 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
629 // and give the event to it
630 wxMouseEvent event2
= event
;
634 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
636 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
640 wxPopupWindowHandler::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
642 m_popup
->DismissAndNotify();
644 // There is no need to skip the event here, normally we've already dealt
645 // with the focus loss.
648 // ----------------------------------------------------------------------------
649 // wxPopupFocusHandler
650 // ----------------------------------------------------------------------------
652 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
654 // when we lose focus we always disappear - unless it goes to the popup (in
655 // which case we don't really lose it)
656 wxWindow
*win
= event
.GetWindow();
659 if ( win
== m_popup
)
661 win
= win
->GetParent();
664 m_popup
->DismissAndNotify();
667 void wxPopupFocusHandler::OnChar(wxKeyEvent
& event
)
669 // we can be associated with the popup itself in which case we should avoid
670 // infinite recursion
672 wxRecursionGuard
guard(s_inside
);
673 if ( guard
.IsInside() )
679 // let the window have it first, it might process the keys
680 if ( !m_popup
->GetEventHandler()->ProcessEvent(event
) )
682 // by default, dismiss the popup
683 m_popup
->DismissAndNotify();
687 #endif // wxUSE_POPUPWIN