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 // License: 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/recguard.h"
39 #ifdef __WXUNIVERSAL__
40 #include "wx/univ/renderer.h"
41 #include "wx/scrolbar.h"
42 #endif // __WXUNIVERSAL__
48 #include "wx/x11/private.h"
51 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow
, wxWindow
)
52 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow
, wxPopupWindow
)
54 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
55 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow
, wxPopupTransientWindow
)
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // event handlers which we use to intercept events which cause the popup to
64 class wxPopupWindowHandler
: public wxEvtHandler
67 wxPopupWindowHandler(wxPopupTransientWindow
*popup
) : m_popup(popup
) {}
71 void OnLeftDown(wxMouseEvent
& event
);
74 wxPopupTransientWindow
*m_popup
;
77 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler
)
80 class wxPopupFocusHandler
: public wxEvtHandler
83 wxPopupFocusHandler(wxPopupTransientWindow
*popup
) : m_popup(popup
) {}
86 void OnKillFocus(wxFocusEvent
& event
);
87 void OnKeyDown(wxKeyEvent
& event
);
90 wxPopupTransientWindow
*m_popup
;
93 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler
)
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
101 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
104 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
105 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
106 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown
)
109 BEGIN_EVENT_TABLE(wxPopupTransientWindow
, wxPopupWindow
)
110 #if defined( __WXMSW__ ) || defined( __WXMAC__)
111 EVT_IDLE(wxPopupTransientWindow::OnIdle
)
115 // ============================================================================
117 // ============================================================================
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 wxPopupWindowBase::~wxPopupWindowBase()
125 // this destructor is required for Darwin
128 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
133 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
136 wxSize sizeScreen
= wxGetDisplaySize(),
137 sizeSelf
= GetSize();
139 // is there enough space to put the popup below the window (where we put it
141 wxCoord y
= ptOrigin
.y
+ size
.y
;
142 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
144 // check if there is enough space above
145 if ( ptOrigin
.y
> sizeSelf
.y
)
147 // do position the control above the window
148 y
-= size
.y
+ sizeSelf
.y
;
150 //else: not enough space below nor above, leave below
153 // now check left/right too
154 wxCoord x
= ptOrigin
.x
;
156 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
158 // shift the window to the left instead of the right.
160 x
-= sizeSelf
.x
; // also shift it by window width.
166 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
168 // check if there is enough space to the left
169 if ( ptOrigin
.x
> sizeSelf
.x
)
171 // do position the control to the left
172 x
-= size
.x
+ sizeSelf
.x
;
174 //else: not enough space there neither, leave in default position
177 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
180 // ----------------------------------------------------------------------------
181 // wxPopupTransientWindow
182 // ----------------------------------------------------------------------------
184 void wxPopupTransientWindow::Init()
187 m_focus
= (wxWindow
*)NULL
;
189 m_handlerFocus
= NULL
;
190 m_handlerPopup
= NULL
;
193 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
197 (void)Create(parent
, style
);
200 wxPopupTransientWindow::~wxPopupTransientWindow()
202 if (m_handlerPopup
&& m_handlerPopup
->GetNextHandler())
205 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
206 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
208 delete m_handlerFocus
;
209 delete m_handlerPopup
;
212 void wxPopupTransientWindow::PopHandlers()
216 if ( !m_child
->RemoveEventHandler(m_handlerPopup
) )
218 // something is very wrong and someone else probably deleted our
219 // handler - so don't risk deleting it second time
220 m_handlerPopup
= NULL
;
222 if (m_child
->HasCapture())
224 m_child
->ReleaseMouse();
231 if ( !m_focus
->RemoveEventHandler(m_handlerFocus
) )
234 m_handlerFocus
= NULL
;
240 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
242 const wxWindowList
& children
= GetChildren();
243 if ( children
.GetCount() )
245 m_child
= children
.GetFirst()->GetData();
254 // There is is a problem if these are still in use
255 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
256 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
259 m_handlerPopup
= new wxPopupWindowHandler(this);
261 m_child
->PushEventHandler(m_handlerPopup
);
263 m_focus
= winFocus
? winFocus
: this;
266 #if defined( __WXMSW__ ) || defined( __WXMAC__)
267 // MSW doesn't allow to set focus to the popup window, but we need to
268 // subclass the window which has the focus, and not winFocus passed in or
269 // otherwise everything else breaks down
270 m_focus
= FindFocus();
271 #elif defined(__WXGTK__)
272 // GTK+ catches the activate events from the popup
273 // window, not the focus events from the child window
280 m_handlerFocus
= new wxPopupFocusHandler(this);
282 m_focus
->PushEventHandler(m_handlerFocus
);
286 bool wxPopupTransientWindow::Show( bool show
)
291 gdk_pointer_ungrab( (guint32
)GDK_CURRENT_TIME
);
293 gtk_grab_remove( m_widget
);
300 XUngrabPointer( wxGlobalDisplay(), CurrentTime
);
304 #if defined( __WXMSW__ ) || defined( __WXMAC__)
305 if (!show
&& m_child
&& m_child
->HasCapture())
307 m_child
->ReleaseMouse();
311 bool ret
= wxPopupWindow::Show( show
);
316 gtk_grab_add( m_widget
);
318 gdk_pointer_grab( m_widget
->window
, TRUE
,
320 (GDK_BUTTON_PRESS_MASK
|
321 GDK_BUTTON_RELEASE_MASK
|
322 GDK_POINTER_MOTION_HINT_MASK
|
323 GDK_POINTER_MOTION_MASK
),
326 (guint32
)GDK_CURRENT_TIME
);
333 Window xwindow
= (Window
) m_clientWindow
;
335 /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow
,
337 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
,
346 #if defined( __WXMSW__ ) || defined( __WXMAC__)
349 // Assume that the mouse is outside the popup to begin with
350 m_child
->CaptureMouse();
357 void wxPopupTransientWindow::Dismiss()
363 void wxPopupTransientWindow::DismissAndNotify()
369 void wxPopupTransientWindow::OnDismiss()
371 // nothing to do here - but it may be interesting for derived class
374 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
376 // no special processing here
380 #if defined( __WXMSW__ ) || defined( __WXMAC__)
381 void wxPopupTransientWindow::OnIdle(wxIdleEvent
& event
)
385 if (IsShown() && m_child
)
387 wxPoint pos
= ScreenToClient(wxGetMousePosition());
388 wxRect
rect(GetSize());
390 if ( rect
.Contains(pos
) )
392 if ( m_child
->HasCapture() )
394 m_child
->ReleaseMouse();
399 if ( !m_child
->HasCapture() )
401 m_child
->CaptureMouse();
409 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
411 // ----------------------------------------------------------------------------
412 // wxPopupComboWindow
413 // ----------------------------------------------------------------------------
415 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
416 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
419 wxPopupComboWindow::wxPopupComboWindow(wxComboCtrl
*parent
)
420 : wxPopupTransientWindow(parent
)
425 bool wxPopupComboWindow::Create(wxComboCtrl
*parent
)
429 return wxPopupWindow::Create(parent
);
432 void wxPopupComboWindow::PositionNearCombo()
434 // the origin point must be in screen coords
435 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0,0));
437 #if 0 //def __WXUNIVERSAL__
438 // account for the fact that (0, 0) is not the top left corner of the
439 // window: there is also the border
440 wxRect rectBorders
= m_combo
->GetRenderer()->
441 GetBorderDimensions(m_combo
->GetBorder());
442 ptOrigin
.x
-= rectBorders
.x
;
443 ptOrigin
.y
-= rectBorders
.y
;
444 #endif // __WXUNIVERSAL__
446 // position below or above the combobox: the width is 0 to put it exactly
447 // below us, not to the left or to the right
448 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
451 void wxPopupComboWindow::OnDismiss()
453 m_combo
->OnPopupDismiss();
456 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
458 m_combo
->ProcessEvent(event
);
461 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
463 // ----------------------------------------------------------------------------
464 // wxPopupWindowHandler
465 // ----------------------------------------------------------------------------
467 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
469 // let the window have it first (we're the first event handler in the chain
470 // of handlers for this window)
471 if ( m_popup
->ProcessLeftDown(event
) )
476 wxPoint pos
= event
.GetPosition();
478 // in non-Univ ports the system manages scrollbars for us
479 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
480 // scrollbar on which the click occurred
481 wxWindow
*sbar
= NULL
;
482 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
484 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
486 switch ( win
->HitTest(pos
.x
, pos
.y
) )
488 case wxHT_WINDOW_OUTSIDE
:
490 // do the coords translation now as after DismissAndNotify()
491 // m_popup may be destroyed
492 wxMouseEvent
event2(event
);
494 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
496 // clicking outside a popup dismisses it
497 m_popup
->DismissAndNotify();
499 // dismissing a tooltip shouldn't waste a click, i.e. you
500 // should be able to dismiss it and press the button with the
501 // same click, so repost this event to the window beneath us
502 wxWindow
*winUnder
= wxFindWindowAtPoint(event2
.GetPosition());
505 // translate the event coords to the ones of the window
506 // which is going to get the event
507 winUnder
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
509 event2
.SetEventObject(winUnder
);
510 wxPostEvent(winUnder
->GetEventHandler(), event2
);
515 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
516 case wxHT_WINDOW_HORZ_SCROLLBAR
:
517 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
520 case wxHT_WINDOW_VERT_SCROLLBAR
:
521 sbar
= win
->GetScrollbar(wxVERTICAL
);
523 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
526 // forgot to update the switch after adding a new hit test code?
527 wxFAIL_MSG( _T("unexpected HitTest() return value") );
530 case wxHT_WINDOW_CORNER
:
531 // don't actually know if this one is good for anything, but let it
534 case wxHT_WINDOW_INSIDE
:
535 // let the normal processing take place
540 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
543 // translate the event coordinates to the scrollbar ones
544 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
546 // and give the event to it
547 wxMouseEvent event2
= event
;
551 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
553 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
556 // ----------------------------------------------------------------------------
557 // wxPopupFocusHandler
558 // ----------------------------------------------------------------------------
560 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
562 // when we lose focus we always disappear - unless it goes to the popup (in
563 // which case we don't really lose it)
564 wxWindow
*win
= event
.GetWindow();
567 if ( win
== m_popup
)
569 win
= win
->GetParent();
572 m_popup
->DismissAndNotify();
575 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
577 // we can be associated with the popup itself in which case we should avoid
578 // infinite recursion
580 wxRecursionGuard
guard(s_inside
);
581 if ( guard
.IsInside() )
587 // let the window have it first, it might process the keys
588 if ( !m_popup
->GetEventHandler()->ProcessEvent(event
) )
590 // by default, dismiss the popup
591 m_popup
->DismissAndNotify();
595 #endif // wxUSE_POPUPWIN