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 BEGIN_EVENT_TABLE(wxPopupTransientWindow
, wxPopupWindow
)
115 EVT_IDLE(wxPopupTransientWindow::OnIdle
)
119 // ============================================================================
121 // ============================================================================
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 wxPopupWindowBase::~wxPopupWindowBase()
129 // this destructor is required for Darwin
132 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
137 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
140 wxSize sizeScreen
= wxGetDisplaySize(),
141 sizeSelf
= GetSize();
143 // is there enough space to put the popup below the window (where we put it
145 wxCoord y
= ptOrigin
.y
+ size
.y
;
146 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
148 // check if there is enough space above
149 if ( ptOrigin
.y
> sizeSelf
.y
)
151 // do position the control above the window
152 y
-= size
.y
+ sizeSelf
.y
;
154 //else: not enough space below nor above, leave below
157 // now check left/right too
158 wxCoord x
= ptOrigin
.x
+ size
.x
;
159 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
161 // check if there is enough space to the left
162 if ( ptOrigin
.x
> sizeSelf
.x
)
164 // do position the control to the left
165 x
-= size
.x
+ sizeSelf
.x
;
167 //else: not enough space there neither, leave in default position
170 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
173 // ----------------------------------------------------------------------------
174 // wxPopupTransientWindow
175 // ----------------------------------------------------------------------------
177 void wxPopupTransientWindow::Init()
180 m_focus
= (wxWindow
*)NULL
;
182 m_handlerFocus
= NULL
;
183 m_handlerPopup
= NULL
;
186 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
190 (void)Create(parent
, style
);
193 wxPopupTransientWindow::~wxPopupTransientWindow()
198 void wxPopupTransientWindow::PopHandlers()
202 if ( m_handlerPopup
&& !m_child
->RemoveEventHandler(m_handlerPopup
) )
204 // something is very wrong and someone else probably deleted our
205 // handler - so don't risk deleting it second time
206 m_handlerPopup
= NULL
;
208 if (m_child
->HasCapture())
210 m_child
->ReleaseMouse();
218 if ( m_handlerFocus
&& !m_focus
->RemoveEventHandler(m_handlerFocus
) )
221 m_handlerFocus
= NULL
;
225 if ( m_handlerFocus
&& !RemoveEventHandler(m_handlerFocus
) )
228 m_handlerFocus
= NULL
;
232 // delete the handlers, they'll be created as necessary in Popup()
233 delete m_handlerPopup
;
234 m_handlerPopup
= NULL
;
235 delete m_handlerFocus
;
236 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 valid
256 wxASSERT_MSG(!m_handlerPopup
, wxT("Popup handler not deleted"));
257 wxASSERT_MSG(!m_handlerFocus
, wxT("Focus handler not deleted"));
259 delete m_handlerPopup
;
260 m_handlerPopup
= new wxPopupWindowHandler(this);
262 m_child
->PushEventHandler(m_handlerPopup
);
264 m_focus
= winFocus
? winFocus
: this;
268 // MSW doesn't allow to set focus to the popup window, but we need to
269 // subclass the window which has the focus, and not winFocus passed in or
270 // otherwise everything else breaks down
271 m_focus
= FindFocus();
274 delete m_handlerFocus
;
275 m_handlerFocus
= new wxPopupFocusHandler(this);
277 m_focus
->PushEventHandler(m_handlerFocus
);
280 // GTK+ catches the activate events from the popup
281 // window, not the focus events from the child window
282 delete m_handlerFocus
;
283 m_handlerFocus
= new wxPopupFocusHandler(this);
284 PushEventHandler(m_handlerFocus
);
287 // catch destroy events, if you close a program with a popup shown in MSW
288 // you get a segfault if m_child or m_focus are deleted before this is
289 m_child
->Connect(wxEVT_DESTROY
,
290 wxWindowDestroyEventHandler(wxPopupTransientWindow::OnDestroy
),
293 m_focus
->Connect(wxEVT_DESTROY
,
294 wxWindowDestroyEventHandler(wxPopupTransientWindow::OnDestroy
),
298 bool wxPopupTransientWindow::Show( bool show
)
303 gdk_pointer_ungrab( (guint32
)GDK_CURRENT_TIME
);
305 gtk_grab_remove( m_widget
);
312 XUngrabPointer( wxGlobalDisplay(), CurrentTime
);
317 if (!show
&& m_child
&& m_child
->HasCapture())
319 m_child
->ReleaseMouse();
323 bool ret
= wxPopupWindow::Show( show
);
328 gtk_grab_add( m_widget
);
330 gdk_pointer_grab( m_widget
->window
, TRUE
,
332 (GDK_BUTTON_PRESS_MASK
|
333 GDK_BUTTON_RELEASE_MASK
|
334 GDK_POINTER_MOTION_HINT_MASK
|
335 GDK_POINTER_MOTION_MASK
),
338 (guint32
)GDK_CURRENT_TIME
);
345 Window xwindow
= (Window
) m_clientWindow
;
347 /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow
,
349 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
,
361 // Assume that the mouse is outside the popup to begin with
362 m_child
->CaptureMouse();
369 void wxPopupTransientWindow::Dismiss()
375 void wxPopupTransientWindow::DismissAndNotify()
381 void wxPopupTransientWindow::OnDismiss()
383 // nothing to do here - but it may be interesting for derived class
386 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
388 // no special processing here
392 void wxPopupTransientWindow::OnDestroy(wxWindowDestroyEvent
& event
)
394 if (event
.GetEventObject() == m_child
)
396 if (event
.GetEventObject() == m_focus
)
401 void wxPopupTransientWindow::OnIdle(wxIdleEvent
& event
)
405 if (IsShown() && m_child
)
407 wxPoint pos
= ScreenToClient(wxGetMousePosition());
408 wxRect
rect(GetSize());
410 if ( rect
.Inside(pos
) )
412 if ( m_child
->HasCapture() )
414 m_child
->ReleaseMouse();
419 if ( !m_child
->HasCapture() )
421 m_child
->CaptureMouse();
429 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
431 // ----------------------------------------------------------------------------
432 // wxPopupComboWindow
433 // ----------------------------------------------------------------------------
435 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
436 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
439 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
440 : wxPopupTransientWindow(parent
)
445 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
449 return wxPopupWindow::Create(parent
);
452 void wxPopupComboWindow::PositionNearCombo()
454 // the origin point must be in screen coords
455 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0,0));
457 #if 0 //def __WXUNIVERSAL__
458 // account for the fact that (0, 0) is not the top left corner of the
459 // window: there is also the border
460 wxRect rectBorders
= m_combo
->GetRenderer()->
461 GetBorderDimensions(m_combo
->GetBorder());
462 ptOrigin
.x
-= rectBorders
.x
;
463 ptOrigin
.y
-= rectBorders
.y
;
464 #endif // __WXUNIVERSAL__
466 // position below or above the combobox: the width is 0 to put it exactly
467 // below us, not to the left or to the right
468 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
471 void wxPopupComboWindow::OnDismiss()
473 m_combo
->OnDismiss();
476 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
478 m_combo
->ProcessEvent(event
);
481 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
483 // ----------------------------------------------------------------------------
484 // wxPopupWindowHandler
485 // ----------------------------------------------------------------------------
486 wxPopupWindowHandler::~wxPopupWindowHandler()
488 if (m_popup
&& (m_popup
->m_handlerPopup
== this))
489 m_popup
->m_handlerPopup
= NULL
;
492 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
494 // let the window have it first (we're the first event handler in the chain
495 // of handlers for this window)
496 if ( m_popup
->ProcessLeftDown(event
) )
501 wxPoint pos
= event
.GetPosition();
503 // scrollbar on which the click occured
504 wxWindow
*sbar
= NULL
;
506 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
508 switch ( win
->HitTest(pos
.x
, pos
.y
) )
510 case wxHT_WINDOW_OUTSIDE
:
512 // do the coords translation now as after DismissAndNotify()
513 // m_popup may be destroyed
514 wxMouseEvent
event2(event
);
516 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
518 // clicking outside a popup dismisses it
519 m_popup
->DismissAndNotify();
521 // dismissing a tooltip shouldn't waste a click, i.e. you
522 // should be able to dismiss it and press the button with the
523 // same click, so repost this event to the window beneath us
524 wxWindow
*win
= wxFindWindowAtPoint(event2
.GetPosition());
527 // translate the event coords to the ones of the window
528 // which is going to get the event
529 win
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
531 event2
.SetEventObject(win
);
532 wxPostEvent(win
, event2
);
537 #ifdef __WXUNIVERSAL__
538 case wxHT_WINDOW_HORZ_SCROLLBAR
:
539 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
542 case wxHT_WINDOW_VERT_SCROLLBAR
:
543 sbar
= win
->GetScrollbar(wxVERTICAL
);
548 // forgot to update the switch after adding a new hit test code?
549 wxFAIL_MSG( _T("unexpected HitTest() return value") );
552 case wxHT_WINDOW_CORNER
:
553 // don't actually know if this one is good for anything, but let it
556 case wxHT_WINDOW_INSIDE
:
557 // let the normal processing take place
564 // translate the event coordinates to the scrollbar ones
565 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
567 // and give the event to it
568 wxMouseEvent event2
= event
;
572 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
576 // ----------------------------------------------------------------------------
577 // wxPopupFocusHandler
578 // ----------------------------------------------------------------------------
579 wxPopupFocusHandler::~wxPopupFocusHandler()
581 if (m_popup
&& (m_popup
->m_handlerFocus
== this))
582 m_popup
->m_handlerFocus
= NULL
;
585 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
587 // when we lose focus we always disappear - unless it goes to the popup (in
588 // which case we don't really lose it)
589 wxWindow
*win
= event
.GetWindow();
592 if ( win
== m_popup
)
594 win
= win
->GetParent();
597 m_popup
->DismissAndNotify();
600 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
602 // let the window have it first, it might process the keys
603 if ( !m_popup
->ProcessEvent(event
) )
605 // by default, dismiss the popup
606 m_popup
->DismissAndNotify();
610 #endif // wxUSE_POPUPWIN