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 // Licence: 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/display.h"
38 #include "wx/recguard.h"
40 #ifdef __WXUNIVERSAL__
41 #include "wx/univ/renderer.h"
42 #include "wx/scrolbar.h"
43 #endif // __WXUNIVERSAL__
47 #if GTK_CHECK_VERSION(2,0,0)
48 #include "wx/gtk/private/gtk2-compat.h"
50 #define gtk_widget_get_window(x) x->window
52 #elif defined(__WXMSW__)
53 #include "wx/msw/private.h"
54 #elif defined(__WXX11__)
55 #include "wx/x11/private.h"
58 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow
, wxWindow
)
59 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow
, wxPopupWindow
)
61 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
62 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow
, wxPopupTransientWindow
)
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // event handlers which we use to intercept events which cause the popup to
71 class wxPopupWindowHandler
: public wxEvtHandler
74 wxPopupWindowHandler(wxPopupTransientWindow
*popup
) : m_popup(popup
) {}
78 void OnLeftDown(wxMouseEvent
& 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
)
111 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
112 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
113 EVT_CHAR(wxPopupFocusHandler::OnChar
)
116 BEGIN_EVENT_TABLE(wxPopupTransientWindow
, wxPopupWindow
)
117 #if defined( __WXMSW__ ) || ( defined( __WXMAC__ ) && wxOSX_USE_CARBON )
118 EVT_IDLE(wxPopupTransientWindow::OnIdle
)
122 // ============================================================================
124 // ============================================================================
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 wxPopupWindowBase::~wxPopupWindowBase()
132 // this destructor is required for Darwin
135 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
140 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
143 // determine the position and size of the screen we clamp the popup to
147 const int displayNum
= wxDisplay::GetFromPoint(ptOrigin
);
148 if ( displayNum
!= wxNOT_FOUND
)
150 const wxRect rectScreen
= wxDisplay(displayNum
).GetGeometry();
151 posScreen
= rectScreen
.GetPosition();
152 sizeScreen
= rectScreen
.GetSize();
154 else // outside of any display?
156 // just use the primary one then
157 posScreen
= wxPoint(0, 0);
158 sizeScreen
= wxGetDisplaySize();
162 const wxSize sizeSelf
= GetSize();
164 // is there enough space to put the popup below the window (where we put it
166 wxCoord y
= ptOrigin
.y
+ size
.y
;
167 if ( y
+ sizeSelf
.y
> posScreen
.y
+ sizeScreen
.y
)
169 // check if there is enough space above
170 if ( ptOrigin
.y
> sizeSelf
.y
)
172 // do position the control above the window
173 y
-= size
.y
+ sizeSelf
.y
;
175 //else: not enough space below nor above, leave below
178 // now check left/right too
179 wxCoord x
= ptOrigin
.x
;
181 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
183 // shift the window to the left instead of the right.
185 x
-= sizeSelf
.x
; // also shift it by window width.
191 if ( x
+ sizeSelf
.x
> posScreen
.x
+ sizeScreen
.x
)
193 // check if there is enough space to the left
194 if ( ptOrigin
.x
> sizeSelf
.x
)
196 // do position the control to the left
197 x
-= size
.x
+ sizeSelf
.x
;
199 //else: not enough space there neither, leave in default position
202 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
205 // ----------------------------------------------------------------------------
206 // wxPopupTransientWindow
207 // ----------------------------------------------------------------------------
209 void wxPopupTransientWindow::Init()
214 m_handlerFocus
= NULL
;
215 m_handlerPopup
= NULL
;
218 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
222 (void)Create(parent
, style
);
225 wxPopupTransientWindow::~wxPopupTransientWindow()
227 if (m_handlerPopup
&& m_handlerPopup
->GetNextHandler())
230 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
231 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
233 delete m_handlerFocus
;
234 delete m_handlerPopup
;
237 void wxPopupTransientWindow::PopHandlers()
241 if ( !m_child
->RemoveEventHandler(m_handlerPopup
) )
243 // something is very wrong and someone else probably deleted our
244 // handler - so don't risk deleting it second time
245 m_handlerPopup
= NULL
;
247 if (m_child
->HasCapture())
249 m_child
->ReleaseMouse();
256 if ( !m_focus
->RemoveEventHandler(m_handlerFocus
) )
259 m_handlerFocus
= NULL
;
265 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
267 const wxWindowList
& children
= GetChildren();
268 if ( children
.GetCount() )
270 m_child
= children
.GetFirst()->GetData();
279 // There is a problem if these are still in use
280 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
281 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
284 m_handlerPopup
= new wxPopupWindowHandler(this);
286 m_child
->PushEventHandler(m_handlerPopup
);
288 #if defined(__WXMSW__)
289 // Focusing on child of popup window does not work on MSW unless WS_POPUP
290 // style is set. We do not even want to try to set the focus, as it may
291 // provoke errors on some Windows versions (Vista and later).
292 if ( ::GetWindowLong(GetHwnd(), GWL_STYLE
) & WS_POPUP
)
295 m_focus
= winFocus
? winFocus
: this;
299 #if defined( __WXMSW__ ) || (defined( __WXMAC__) && wxOSX_USE_CARBON)
300 // MSW doesn't allow to set focus to the popup window, but we need to
301 // subclass the window which has the focus, and not winFocus passed in or
302 // otherwise everything else breaks down
303 m_focus
= FindFocus();
304 #elif defined(__WXGTK__)
305 // GTK+ catches the activate events from the popup
306 // window, not the focus events from the child window
313 m_handlerFocus
= new wxPopupFocusHandler(this);
315 m_focus
->PushEventHandler(m_handlerFocus
);
319 bool wxPopupTransientWindow::Show( bool show
)
324 gdk_pointer_ungrab( (guint32
)GDK_CURRENT_TIME
);
326 gtk_grab_remove( m_widget
);
333 XUngrabPointer( wxGlobalDisplay(), CurrentTime
);
337 #if defined( __WXMSW__ ) || defined( __WXMAC__)
338 if (!show
&& m_child
&& m_child
->HasCapture())
340 m_child
->ReleaseMouse();
344 bool ret
= wxPopupWindow::Show( show
);
349 gtk_grab_add( m_widget
);
351 gdk_pointer_grab( gtk_widget_get_window(m_widget
), true,
353 (GDK_BUTTON_PRESS_MASK
|
354 GDK_BUTTON_RELEASE_MASK
|
355 GDK_POINTER_MOTION_HINT_MASK
|
356 GDK_POINTER_MOTION_MASK
),
359 (guint32
)GDK_CURRENT_TIME
);
366 Window xwindow
= (Window
) m_clientWindow
;
368 /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow
,
370 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
,
379 #if defined( __WXMSW__ ) || defined( __WXMAC__)
382 // Assume that the mouse is outside the popup to begin with
383 m_child
->CaptureMouse();
390 void wxPopupTransientWindow::Dismiss()
396 void wxPopupTransientWindow::DismissAndNotify()
402 void wxPopupTransientWindow::OnDismiss()
404 // nothing to do here - but it may be interesting for derived class
407 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
409 // no special processing here
413 #if defined( __WXMSW__ ) || ( defined( __WXMAC__ ) && wxOSX_USE_CARBON )
414 void wxPopupTransientWindow::OnIdle(wxIdleEvent
& event
)
418 if (IsShown() && m_child
)
420 wxPoint pos
= ScreenToClient(wxGetMousePosition());
421 wxRect
rect(GetSize());
423 if ( rect
.Contains(pos
) )
425 if ( m_child
->HasCapture() )
427 m_child
->ReleaseMouse();
432 if ( !m_child
->HasCapture() )
434 m_child
->CaptureMouse();
442 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
444 // ----------------------------------------------------------------------------
445 // wxPopupComboWindow
446 // ----------------------------------------------------------------------------
448 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
449 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
452 wxPopupComboWindow::wxPopupComboWindow(wxComboCtrl
*parent
)
453 : wxPopupTransientWindow(parent
)
458 bool wxPopupComboWindow::Create(wxComboCtrl
*parent
)
462 return wxPopupWindow::Create(parent
);
465 void wxPopupComboWindow::PositionNearCombo()
467 // the origin point must be in screen coords
468 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0,0));
470 #if 0 //def __WXUNIVERSAL__
471 // account for the fact that (0, 0) is not the top left corner of the
472 // window: there is also the border
473 wxRect rectBorders
= m_combo
->GetRenderer()->
474 GetBorderDimensions(m_combo
->GetBorder());
475 ptOrigin
.x
-= rectBorders
.x
;
476 ptOrigin
.y
-= rectBorders
.y
;
477 #endif // __WXUNIVERSAL__
479 // position below or above the combobox: the width is 0 to put it exactly
480 // below us, not to the left or to the right
481 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
484 void wxPopupComboWindow::OnDismiss()
486 m_combo
->OnPopupDismiss(true);
489 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
491 m_combo
->ProcessWindowEvent(event
);
494 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
496 // ----------------------------------------------------------------------------
497 // wxPopupWindowHandler
498 // ----------------------------------------------------------------------------
500 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
502 // let the window have it first (we're the first event handler in the chain
503 // of handlers for this window)
504 if ( m_popup
->ProcessLeftDown(event
) )
509 wxPoint pos
= event
.GetPosition();
511 // in non-Univ ports the system manages scrollbars for us
512 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
513 // scrollbar on which the click occurred
514 wxWindow
*sbar
= NULL
;
515 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
517 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
519 switch ( win
->HitTest(pos
.x
, pos
.y
) )
521 case wxHT_WINDOW_OUTSIDE
:
523 // do the coords translation now as after DismissAndNotify()
524 // m_popup may be destroyed
525 wxMouseEvent
event2(event
);
527 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
529 // clicking outside a popup dismisses it
530 m_popup
->DismissAndNotify();
532 // dismissing a tooltip shouldn't waste a click, i.e. you
533 // should be able to dismiss it and press the button with the
534 // same click, so repost this event to the window beneath us
535 wxWindow
*winUnder
= wxFindWindowAtPoint(event2
.GetPosition());
538 // translate the event coords to the ones of the window
539 // which is going to get the event
540 winUnder
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
542 event2
.SetEventObject(winUnder
);
543 wxPostEvent(winUnder
->GetEventHandler(), event2
);
548 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
549 case wxHT_WINDOW_HORZ_SCROLLBAR
:
550 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
553 case wxHT_WINDOW_VERT_SCROLLBAR
:
554 sbar
= win
->GetScrollbar(wxVERTICAL
);
556 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
559 // forgot to update the switch after adding a new hit test code?
560 wxFAIL_MSG( wxT("unexpected HitTest() return value") );
563 case wxHT_WINDOW_CORNER
:
564 // don't actually know if this one is good for anything, but let it
567 case wxHT_WINDOW_INSIDE
:
568 // let the normal processing take place
573 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
576 // translate the event coordinates to the scrollbar ones
577 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
579 // and give the event to it
580 wxMouseEvent event2
= event
;
584 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
586 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
589 // ----------------------------------------------------------------------------
590 // wxPopupFocusHandler
591 // ----------------------------------------------------------------------------
593 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
595 // when we lose focus we always disappear - unless it goes to the popup (in
596 // which case we don't really lose it)
597 wxWindow
*win
= event
.GetWindow();
600 if ( win
== m_popup
)
602 win
= win
->GetParent();
605 m_popup
->DismissAndNotify();
608 void wxPopupFocusHandler::OnChar(wxKeyEvent
& event
)
610 // we can be associated with the popup itself in which case we should avoid
611 // infinite recursion
613 wxRecursionGuard
guard(s_inside
);
614 if ( guard
.IsInside() )
620 // let the window have it first, it might process the keys
621 if ( !m_popup
->GetEventHandler()->ProcessEvent(event
) )
623 // by default, dismiss the popup
624 m_popup
->DismissAndNotify();
628 #endif // wxUSE_POPUPWIN