1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "popupwinbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/popupwin.h"
36 #include "wx/combobox.h" // wxComboControl
37 #include "wx/app.h" // wxPostEvent
42 #ifdef __WXUNIVERSAL__
43 #include "wx/univ/renderer.h"
44 #endif // __WXUNIVERSAL__
50 #include "wx/x11/private.h"
53 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow
, wxWindow
)
54 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow
, wxPopupWindow
)
56 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
57 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow
, wxPopupTransientWindow
)
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // event handlers which we use to intercept events which cause the popup to
66 class wxPopupWindowHandler
: public wxEvtHandler
69 wxPopupWindowHandler(wxPopupTransientWindow
*popup
) : m_popup(popup
) {}
70 ~wxPopupWindowHandler();
74 void OnLeftDown(wxMouseEvent
& event
);
77 wxPopupTransientWindow
*m_popup
;
80 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler
)
83 class wxPopupFocusHandler
: public wxEvtHandler
86 wxPopupFocusHandler(wxPopupTransientWindow
*popup
) : m_popup(popup
) {}
87 ~wxPopupFocusHandler();
90 void OnKillFocus(wxFocusEvent
& event
);
91 void OnKeyDown(wxKeyEvent
& event
);
94 wxPopupTransientWindow
*m_popup
;
97 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler
)
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
105 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
108 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
109 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
110 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown
)
113 // ============================================================================
115 // ============================================================================
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 wxPopupWindowBase::~wxPopupWindowBase()
123 // this destructor is required for Darwin
126 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
131 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
134 wxSize sizeScreen
= wxGetDisplaySize(),
135 sizeSelf
= GetSize();
137 // is there enough space to put the popup below the window (where we put it
139 wxCoord y
= ptOrigin
.y
+ size
.y
;
140 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
142 // check if there is enough space above
143 if ( ptOrigin
.y
> sizeSelf
.y
)
145 // do position the control above the window
146 y
-= size
.y
+ sizeSelf
.y
;
148 //else: not enough space below nor above, leave below
151 // now check left/right too
152 wxCoord x
= ptOrigin
.x
+ size
.x
;
153 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
155 // check if there is enough space to the left
156 if ( ptOrigin
.x
> sizeSelf
.x
)
158 // do position the control to the left
159 x
-= size
.x
+ sizeSelf
.x
;
161 //else: not enough space there neither, leave in default position
164 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
167 // ----------------------------------------------------------------------------
168 // wxPopupTransientWindow
169 // ----------------------------------------------------------------------------
171 BEGIN_EVENT_TABLE(wxPopupTransientWindow
, wxPopupWindow
)
173 EVT_ENTER_WINDOW(wxPopupTransientWindow::OnEnter
)
174 EVT_LEAVE_WINDOW(wxPopupTransientWindow::OnLeave
)
175 EVT_LEFT_DOWN(wxPopupTransientWindow::OnLeftDown
)
179 void wxPopupTransientWindow::Init()
182 m_focus
= (wxWindow
*)NULL
;
184 m_handlerFocus
= NULL
;
185 m_handlerPopup
= NULL
;
188 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
192 (void)Create(parent
, style
);
195 wxPopupTransientWindow::~wxPopupTransientWindow()
200 void wxPopupTransientWindow::PopHandlers()
204 if ( m_handlerPopup
&& !m_child
->RemoveEventHandler(m_handlerPopup
) )
206 // something is very wrong and someone else probably deleted our
207 // handler - so don't risk deleting it second time
208 m_handlerPopup
= NULL
;
221 if ( m_handlerFocus
&& !m_focus
->RemoveEventHandler(m_handlerFocus
) )
224 m_handlerFocus
= NULL
;
228 if ( m_handlerFocus
&& !RemoveEventHandler(m_handlerFocus
) )
231 m_handlerFocus
= NULL
;
235 // delete the handlers, they'll be created as necessary in Popup()
236 delete m_handlerPopup
;
237 m_handlerPopup
= NULL
;
238 delete m_handlerFocus
;
239 m_handlerFocus
= NULL
;
244 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
246 const wxWindowList
& children
= GetChildren();
247 if ( children
.GetCount() )
249 m_child
= children
.GetFirst()->GetData();
258 // There is is a problem if these are still valid
259 wxASSERT_MSG(!m_handlerPopup
, wxT("Popup handler not deleted"));
260 wxASSERT_MSG(!m_handlerFocus
, wxT("Focus handler not deleted"));
262 delete m_handlerPopup
;
263 m_handlerPopup
= new wxPopupWindowHandler(this);
265 m_child
->PushEventHandler(m_handlerPopup
);
267 m_focus
= winFocus
? winFocus
: this;
271 // MSW doesn't allow to set focus to the popup window, but we need to
272 // subclass the window which has the focus, and not winFocus passed in or
273 // otherwise everything else breaks down
274 m_focus
= FindFocus();
277 delete m_handlerFocus
;
278 m_handlerFocus
= new wxPopupFocusHandler(this);
280 m_focus
->PushEventHandler(m_handlerFocus
);
283 // GTK+ catches the activate events from the popup
284 // window, not the focus events from the child window
285 delete m_handlerFocus
;
286 m_handlerFocus
= new wxPopupFocusHandler(this);
287 PushEventHandler(m_handlerFocus
);
290 // catch destroy events, if you close a program with a popup shown in MSW
291 // you get a segfault if m_child or m_focus are deleted before this is
292 m_child
->Connect(wxEVT_DESTROY
,
293 wxWindowDestroyEventHandler(wxPopupTransientWindow::OnDestroy
),
295 m_focus
->Connect(wxEVT_DESTROY
,
296 wxWindowDestroyEventHandler(wxPopupTransientWindow::OnDestroy
),
299 // Assume that the mouse is currently outside of the popup window
302 // Connect the child Enter/Leave events too, incase the child completly
303 // covers the popup (because then the popup's enter/leave events won't be
307 m_child
->Connect(wxEVT_ENTER_WINDOW
,
308 wxMouseEventHandler(wxPopupTransientWindow::OnChildEnter
),
310 m_child
->Connect(wxEVT_LEAVE_WINDOW
,
311 wxMouseEventHandler(wxPopupTransientWindow::OnChildLeave
),
317 bool wxPopupTransientWindow::Show( bool show
)
322 gdk_pointer_ungrab( (guint32
)GDK_CURRENT_TIME
);
324 gtk_grab_remove( m_widget
);
331 XUngrabPointer( wxGlobalDisplay(), CurrentTime
);
335 bool ret
= wxPopupWindow::Show( show
);
340 gtk_grab_add( m_widget
);
342 gdk_pointer_grab( m_widget
->window
, TRUE
,
344 (GDK_BUTTON_PRESS_MASK
|
345 GDK_BUTTON_RELEASE_MASK
|
346 GDK_POINTER_MOTION_HINT_MASK
|
347 GDK_POINTER_MOTION_MASK
),
350 (guint32
)GDK_CURRENT_TIME
);
357 Window xwindow
= (Window
) m_clientWindow
;
359 /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow
,
361 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
,
372 void wxPopupTransientWindow::Dismiss()
379 void wxPopupTransientWindow::DismissAndNotify()
386 void wxPopupTransientWindow::OnDismiss()
388 // nothing to do here - but it may be interesting for derived class
391 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
393 // no special processing here
397 void wxPopupTransientWindow::OnDestroy(wxWindowDestroyEvent
& event
)
399 if (event
.GetEventObject() == m_child
)
401 if (event
.GetEventObject() == m_focus
)
405 void wxPopupTransientWindow::OnEnter(wxMouseEvent
& /*event*/)
413 void wxPopupTransientWindow::OnLeave(wxMouseEvent
& /*event*/)
418 void wxPopupTransientWindow::OnLeftDown(wxMouseEvent
& event
)
420 if (m_handlerPopup
&& m_child
&& m_child
!= this)
422 m_child
->GetEventHandler()->ProcessEvent(event
);
428 // If the child is the same size as the popup window then handle the event,
429 // otherwise assume that there is enough of the popup showing that it will get
430 // it's own Enter/Leave events. A more reliable way to detect this situation
431 // would be appreciated...
433 void wxPopupTransientWindow::OnChildEnter(wxMouseEvent
& event
)
437 wxSize cs
= m_child
->GetSize();
438 wxSize ps
= GetClientSize();
440 if ((cs
.x
* cs
.y
) >= (ps
.x
* ps
.y
))
445 void wxPopupTransientWindow::OnChildLeave(wxMouseEvent
& event
)
449 wxSize cs
= m_child
->GetSize();
450 wxSize ps
= GetClientSize();
452 if ((cs
.x
* cs
.y
) >= (ps
.x
* ps
.y
))
457 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
459 // ----------------------------------------------------------------------------
460 // wxPopupComboWindow
461 // ----------------------------------------------------------------------------
463 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
464 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
467 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
468 : wxPopupTransientWindow(parent
)
473 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
477 return wxPopupWindow::Create(parent
);
480 void wxPopupComboWindow::PositionNearCombo()
482 // the origin point must be in screen coords
483 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0,0));
485 #if 0 //def __WXUNIVERSAL__
486 // account for the fact that (0, 0) is not the top left corner of the
487 // window: there is also the border
488 wxRect rectBorders
= m_combo
->GetRenderer()->
489 GetBorderDimensions(m_combo
->GetBorder());
490 ptOrigin
.x
-= rectBorders
.x
;
491 ptOrigin
.y
-= rectBorders
.y
;
492 #endif // __WXUNIVERSAL__
494 // position below or above the combobox: the width is 0 to put it exactly
495 // below us, not to the left or to the right
496 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
499 void wxPopupComboWindow::OnDismiss()
501 m_combo
->OnDismiss();
504 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
506 m_combo
->ProcessEvent(event
);
509 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
511 // ----------------------------------------------------------------------------
512 // wxPopupWindowHandler
513 // ----------------------------------------------------------------------------
514 wxPopupWindowHandler::~wxPopupWindowHandler()
516 if (m_popup
&& (m_popup
->m_handlerPopup
== this))
517 m_popup
->m_handlerPopup
= NULL
;
520 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
522 // let the window have it first (we're the first event handler in the chain
523 // of handlers for this window)
524 if ( m_popup
->ProcessLeftDown(event
) )
529 wxPoint pos
= event
.GetPosition();
531 // scrollbar on which the click occured
532 wxWindow
*sbar
= NULL
;
534 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
536 switch ( win
->HitTest(pos
.x
, pos
.y
) )
538 case wxHT_WINDOW_OUTSIDE
:
540 // do the coords translation now as after DismissAndNotify()
541 // m_popup may be destroyed
542 wxMouseEvent
event2(event
);
544 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
546 // clicking outside a popup dismisses it
547 m_popup
->DismissAndNotify();
549 // dismissing a tooltip shouldn't waste a click, i.e. you
550 // should be able to dismiss it and press the button with the
551 // same click, so repost this event to the window beneath us
552 wxWindow
*win
= wxFindWindowAtPoint(event2
.GetPosition());
555 // translate the event coords to the ones of the window
556 // which is going to get the event
557 win
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
559 event2
.SetEventObject(win
);
560 wxPostEvent(win
, event2
);
565 #ifdef __WXUNIVERSAL__
566 case wxHT_WINDOW_HORZ_SCROLLBAR
:
567 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
570 case wxHT_WINDOW_VERT_SCROLLBAR
:
571 sbar
= win
->GetScrollbar(wxVERTICAL
);
576 // forgot to update the switch after adding a new hit test code?
577 wxFAIL_MSG( _T("unexpected HitTest() return value") );
580 case wxHT_WINDOW_CORNER
:
581 // don't actually know if this one is good for anything, but let it
584 case wxHT_WINDOW_INSIDE
:
585 // let the normal processing take place
592 // translate the event coordinates to the scrollbar ones
593 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
595 // and give the event to it
596 wxMouseEvent event2
= event
;
600 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
604 // ----------------------------------------------------------------------------
605 // wxPopupFocusHandler
606 // ----------------------------------------------------------------------------
607 wxPopupFocusHandler::~wxPopupFocusHandler()
609 if (m_popup
&& (m_popup
->m_handlerFocus
== this))
610 m_popup
->m_handlerFocus
= NULL
;
613 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
615 // when we lose focus we always disappear - unless it goes to the popup (in
616 // which case we don't really lose it)
617 wxWindow
*win
= event
.GetWindow();
620 if ( win
== m_popup
)
622 win
= win
->GetParent();
625 m_popup
->DismissAndNotify();
628 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
630 // let the window have it first, it might process the keys
631 if ( !m_popup
->ProcessEvent(event
) )
633 // by default, dismiss the popup
634 m_popup
->DismissAndNotify();
638 #endif // wxUSE_POPUPWIN