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 const wxWindowList
& children
= GetChildren();
269 if ( children
.GetCount() )
271 m_child
= children
.GetFirst()->GetData();
280 // There is a problem if these are still in use
281 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
282 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
285 m_handlerPopup
= new wxPopupWindowHandler(this);
287 m_child
->PushEventHandler(m_handlerPopup
);
289 #if defined(__WXMSW__)
290 // Focusing on child of popup window does not work on MSW unless WS_POPUP
291 // style is set. We do not even want to try to set the focus, as it may
292 // provoke errors on some Windows versions (Vista and later).
293 if ( ::GetWindowLong(GetHwnd(), GWL_STYLE
) & WS_POPUP
)
296 m_focus
= winFocus
? winFocus
: this;
300 #if defined( __WXMSW__ ) || (defined( __WXMAC__) && wxOSX_USE_COCOA_OR_CARBON)
301 // MSW doesn't allow to set focus to the popup window, but we need to
302 // subclass the window which has the focus, and not winFocus passed in or
303 // otherwise everything else breaks down
304 m_focus
= FindFocus();
305 #elif defined(__WXGTK__)
306 // GTK+ catches the activate events from the popup
307 // window, not the focus events from the child window
314 m_handlerFocus
= new wxPopupFocusHandler(this);
316 m_focus
->PushEventHandler(m_handlerFocus
);
320 bool wxPopupTransientWindow::Show( bool show
)
326 GdkDisplay
* display
= gtk_widget_get_display(m_widget
);
327 GdkDeviceManager
* manager
= gdk_display_get_device_manager(display
);
328 GdkDevice
* device
= gdk_device_manager_get_client_pointer(manager
);
329 gdk_device_ungrab(device
, unsigned(GDK_CURRENT_TIME
));
331 gdk_pointer_ungrab( (guint32
)GDK_CURRENT_TIME
);
334 gtk_grab_remove( m_widget
);
341 XUngrabPointer( wxGlobalDisplay(), CurrentTime
);
345 #if defined( __WXMSW__ ) || defined( __WXMAC__)
346 if (!show
&& m_child
&& m_child
->HasCapture())
348 m_child
->ReleaseMouse();
352 bool ret
= wxPopupWindow::Show( show
);
357 gtk_grab_add( m_widget
);
359 const GdkEventMask mask
= GdkEventMask(
360 GDK_BUTTON_PRESS_MASK
|
361 GDK_BUTTON_RELEASE_MASK
|
362 GDK_POINTER_MOTION_HINT_MASK
|
363 GDK_POINTER_MOTION_MASK
);
364 GdkWindow
* window
= gtk_widget_get_window(m_widget
);
366 GdkDisplay
* display
= gdk_window_get_display(window
);
367 GdkDeviceManager
* manager
= gdk_display_get_device_manager(display
);
368 GdkDevice
* device
= gdk_device_manager_get_client_pointer(manager
);
369 gdk_device_grab(device
, window
,
370 GDK_OWNERSHIP_NONE
, false, mask
, NULL
, unsigned(GDK_CURRENT_TIME
));
372 gdk_pointer_grab( window
, true,
376 (guint32
)GDK_CURRENT_TIME
);
384 Window xwindow
= (Window
) m_clientWindow
;
386 /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow
,
388 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
,
397 #if defined( __WXMSW__ ) || defined( __WXMAC__)
400 // Assume that the mouse is outside the popup to begin with
401 m_child
->CaptureMouse();
408 bool wxPopupTransientWindow::Destroy()
410 // The popup window can be deleted at any moment, even while some events
411 // are still being processed for it, so delay its real destruction until
412 // the next idle time when we're sure that it's safe to really destroy it.
414 wxCHECK_MSG( !wxPendingDelete
.Member(this), false,
415 wxS("Shouldn't destroy the popup twice.") );
417 wxPendingDelete
.Append(this);
422 void wxPopupTransientWindow::Dismiss()
428 void wxPopupTransientWindow::DismissAndNotify()
434 void wxPopupTransientWindow::OnDismiss()
436 // nothing to do here - but it may be interesting for derived class
439 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
441 // no special processing here
445 #if defined(__WXMSW__) ||(defined(__WXMAC__) && wxOSX_USE_COCOA_OR_CARBON)
446 void wxPopupTransientWindow::OnIdle(wxIdleEvent
& event
)
450 if (IsShown() && m_child
)
452 wxPoint pos
= ScreenToClient(wxGetMousePosition());
453 wxRect
rect(GetSize());
455 if ( rect
.Contains(pos
) )
457 if ( m_child
->HasCapture() )
459 m_child
->ReleaseMouse();
464 if ( !m_child
->HasCapture() )
466 m_child
->CaptureMouse();
471 #endif // wxOSX/Carbon
474 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
476 // ----------------------------------------------------------------------------
477 // wxPopupComboWindow
478 // ----------------------------------------------------------------------------
480 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
481 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
484 wxPopupComboWindow::wxPopupComboWindow(wxComboCtrl
*parent
)
485 : wxPopupTransientWindow(parent
)
490 bool wxPopupComboWindow::Create(wxComboCtrl
*parent
)
494 return wxPopupWindow::Create(parent
);
497 void wxPopupComboWindow::PositionNearCombo()
499 // the origin point must be in screen coords
500 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0,0));
502 #if 0 //def __WXUNIVERSAL__
503 // account for the fact that (0, 0) is not the top left corner of the
504 // window: there is also the border
505 wxRect rectBorders
= m_combo
->GetRenderer()->
506 GetBorderDimensions(m_combo
->GetBorder());
507 ptOrigin
.x
-= rectBorders
.x
;
508 ptOrigin
.y
-= rectBorders
.y
;
509 #endif // __WXUNIVERSAL__
511 // position below or above the combobox: the width is 0 to put it exactly
512 // below us, not to the left or to the right
513 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
516 void wxPopupComboWindow::OnDismiss()
518 m_combo
->OnPopupDismiss(true);
521 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
523 m_combo
->ProcessWindowEvent(event
);
526 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
528 // ----------------------------------------------------------------------------
529 // wxPopupWindowHandler
530 // ----------------------------------------------------------------------------
532 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
534 // let the window have it first (we're the first event handler in the chain
535 // of handlers for this window)
536 if ( m_popup
->ProcessLeftDown(event
) )
541 wxPoint pos
= event
.GetPosition();
543 // in non-Univ ports the system manages scrollbars for us
544 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
545 // scrollbar on which the click occurred
546 wxWindow
*sbar
= NULL
;
547 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
549 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
551 switch ( win
->HitTest(pos
.x
, pos
.y
) )
553 case wxHT_WINDOW_OUTSIDE
:
555 // do the coords translation now as after DismissAndNotify()
556 // m_popup may be destroyed
557 wxMouseEvent
event2(event
);
559 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
561 // clicking outside a popup dismisses it
562 m_popup
->DismissAndNotify();
564 // dismissing a tooltip shouldn't waste a click, i.e. you
565 // should be able to dismiss it and press the button with the
566 // same click, so repost this event to the window beneath us
567 wxWindow
*winUnder
= wxFindWindowAtPoint(event2
.GetPosition());
570 // translate the event coords to the ones of the window
571 // which is going to get the event
572 winUnder
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
574 event2
.SetEventObject(winUnder
);
575 wxPostEvent(winUnder
->GetEventHandler(), event2
);
580 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
581 case wxHT_WINDOW_HORZ_SCROLLBAR
:
582 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
585 case wxHT_WINDOW_VERT_SCROLLBAR
:
586 sbar
= win
->GetScrollbar(wxVERTICAL
);
588 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
591 // forgot to update the switch after adding a new hit test code?
592 wxFAIL_MSG( wxT("unexpected HitTest() return value") );
595 case wxHT_WINDOW_CORNER
:
596 // don't actually know if this one is good for anything, but let it
599 case wxHT_WINDOW_INSIDE
:
600 // let the normal processing take place
605 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
608 // translate the event coordinates to the scrollbar ones
609 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
611 // and give the event to it
612 wxMouseEvent event2
= event
;
616 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
618 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
622 wxPopupWindowHandler::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
624 m_popup
->DismissAndNotify();
626 // There is no need to skip the event here, normally we've already dealt
627 // with the focus loss.
630 // ----------------------------------------------------------------------------
631 // wxPopupFocusHandler
632 // ----------------------------------------------------------------------------
634 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
636 // when we lose focus we always disappear - unless it goes to the popup (in
637 // which case we don't really lose it)
638 wxWindow
*win
= event
.GetWindow();
641 if ( win
== m_popup
)
643 win
= win
->GetParent();
646 m_popup
->DismissAndNotify();
649 void wxPopupFocusHandler::OnChar(wxKeyEvent
& event
)
651 // we can be associated with the popup itself in which case we should avoid
652 // infinite recursion
654 wxRecursionGuard
guard(s_inside
);
655 if ( guard
.IsInside() )
661 // let the window have it first, it might process the keys
662 if ( !m_popup
->GetEventHandler()->ProcessEvent(event
) )
664 // by default, dismiss the popup
665 m_popup
->DismissAndNotify();
669 #endif // wxUSE_POPUPWIN