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 #elif defined(__WXMSW__)
48 #include "wx/msw/private.h"
49 #elif defined(__WXX11__)
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
) {}
73 void OnLeftDown(wxMouseEvent
& event
);
76 wxPopupTransientWindow
*m_popup
;
79 wxDECLARE_NO_COPY_CLASS(wxPopupWindowHandler
);
82 class wxPopupFocusHandler
: public wxEvtHandler
85 wxPopupFocusHandler(wxPopupTransientWindow
*popup
) : m_popup(popup
) {}
88 void OnKillFocus(wxFocusEvent
& event
);
89 void OnChar(wxKeyEvent
& event
);
92 wxPopupTransientWindow
*m_popup
;
95 wxDECLARE_NO_COPY_CLASS(wxPopupFocusHandler
);
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
103 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
106 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
107 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
108 EVT_CHAR(wxPopupFocusHandler::OnChar
)
111 BEGIN_EVENT_TABLE(wxPopupTransientWindow
, wxPopupWindow
)
112 #if defined( __WXMSW__ ) || ( defined( __WXMAC__ ) && wxOSX_USE_CARBON )
113 EVT_IDLE(wxPopupTransientWindow::OnIdle
)
117 // ============================================================================
119 // ============================================================================
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 wxPopupWindowBase::~wxPopupWindowBase()
127 // this destructor is required for Darwin
130 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
135 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
138 // determine the position and size of the screen we clamp the popup to
142 const int displayNum
= wxDisplay::GetFromPoint(ptOrigin
);
143 if ( displayNum
!= wxNOT_FOUND
)
145 const wxRect rectScreen
= wxDisplay(displayNum
).GetGeometry();
146 posScreen
= rectScreen
.GetPosition();
147 sizeScreen
= rectScreen
.GetSize();
149 else // outside of any display?
151 // just use the primary one then
152 posScreen
= wxPoint(0, 0);
153 sizeScreen
= wxGetDisplaySize();
157 const wxSize sizeSelf
= GetSize();
159 // is there enough space to put the popup below the window (where we put it
161 wxCoord y
= ptOrigin
.y
+ size
.y
;
162 if ( y
+ sizeSelf
.y
> posScreen
.y
+ sizeScreen
.y
)
164 // check if there is enough space above
165 if ( ptOrigin
.y
> sizeSelf
.y
)
167 // do position the control above the window
168 y
-= size
.y
+ sizeSelf
.y
;
170 //else: not enough space below nor above, leave below
173 // now check left/right too
174 wxCoord x
= ptOrigin
.x
;
176 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
178 // shift the window to the left instead of the right.
180 x
-= sizeSelf
.x
; // also shift it by window width.
186 if ( x
+ sizeSelf
.x
> posScreen
.x
+ sizeScreen
.x
)
188 // check if there is enough space to the left
189 if ( ptOrigin
.x
> sizeSelf
.x
)
191 // do position the control to the left
192 x
-= size
.x
+ sizeSelf
.x
;
194 //else: not enough space there neither, leave in default position
197 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
200 // ----------------------------------------------------------------------------
201 // wxPopupTransientWindow
202 // ----------------------------------------------------------------------------
204 void wxPopupTransientWindow::Init()
209 m_handlerFocus
= NULL
;
210 m_handlerPopup
= NULL
;
213 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
217 (void)Create(parent
, style
);
220 wxPopupTransientWindow::~wxPopupTransientWindow()
222 if (m_handlerPopup
&& m_handlerPopup
->GetNextHandler())
225 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
226 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
228 delete m_handlerFocus
;
229 delete m_handlerPopup
;
232 void wxPopupTransientWindow::PopHandlers()
236 if ( !m_child
->RemoveEventHandler(m_handlerPopup
) )
238 // something is very wrong and someone else probably deleted our
239 // handler - so don't risk deleting it second time
240 m_handlerPopup
= NULL
;
242 if (m_child
->HasCapture())
244 m_child
->ReleaseMouse();
251 if ( !m_focus
->RemoveEventHandler(m_handlerFocus
) )
254 m_handlerFocus
= NULL
;
260 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
262 const wxWindowList
& children
= GetChildren();
263 if ( children
.GetCount() )
265 m_child
= children
.GetFirst()->GetData();
274 // There is a problem if these are still in use
275 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
276 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
279 m_handlerPopup
= new wxPopupWindowHandler(this);
281 m_child
->PushEventHandler(m_handlerPopup
);
283 #if defined(__WXMSW__)
284 // Focusing on child of popup window does not work on MSW unless WS_POPUP
285 // style is set. We do not even want to try to set the focus, as it may
286 // provoke errors on some Windows versions (Vista and later).
287 if ( ::GetWindowLong(GetHwnd(), GWL_STYLE
) & WS_POPUP
)
290 m_focus
= winFocus
? winFocus
: this;
294 #if defined( __WXMSW__ ) || (defined( __WXMAC__) && wxOSX_USE_CARBON)
295 // MSW doesn't allow to set focus to the popup window, but we need to
296 // subclass the window which has the focus, and not winFocus passed in or
297 // otherwise everything else breaks down
298 m_focus
= FindFocus();
299 #elif defined(__WXGTK__)
300 // GTK+ catches the activate events from the popup
301 // window, not the focus events from the child window
308 m_handlerFocus
= new wxPopupFocusHandler(this);
310 m_focus
->PushEventHandler(m_handlerFocus
);
314 bool wxPopupTransientWindow::Show( bool show
)
319 gdk_pointer_ungrab( (guint32
)GDK_CURRENT_TIME
);
321 gtk_grab_remove( m_widget
);
328 XUngrabPointer( wxGlobalDisplay(), CurrentTime
);
332 #if defined( __WXMSW__ ) || defined( __WXMAC__)
333 if (!show
&& m_child
&& m_child
->HasCapture())
335 m_child
->ReleaseMouse();
339 bool ret
= wxPopupWindow::Show( show
);
344 gtk_grab_add( m_widget
);
346 gdk_pointer_grab( m_widget
->window
, TRUE
,
348 (GDK_BUTTON_PRESS_MASK
|
349 GDK_BUTTON_RELEASE_MASK
|
350 GDK_POINTER_MOTION_HINT_MASK
|
351 GDK_POINTER_MOTION_MASK
),
354 (guint32
)GDK_CURRENT_TIME
);
361 Window xwindow
= (Window
) m_clientWindow
;
363 /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow
,
365 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
,
374 #if defined( __WXMSW__ ) || defined( __WXMAC__)
377 // Assume that the mouse is outside the popup to begin with
378 m_child
->CaptureMouse();
385 void wxPopupTransientWindow::Dismiss()
391 void wxPopupTransientWindow::DismissAndNotify()
397 void wxPopupTransientWindow::OnDismiss()
399 // nothing to do here - but it may be interesting for derived class
402 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
404 // no special processing here
408 #if defined( __WXMSW__ ) || ( defined( __WXMAC__ ) && wxOSX_USE_CARBON )
409 void wxPopupTransientWindow::OnIdle(wxIdleEvent
& event
)
413 if (IsShown() && m_child
)
415 wxPoint pos
= ScreenToClient(wxGetMousePosition());
416 wxRect
rect(GetSize());
418 if ( rect
.Contains(pos
) )
420 if ( m_child
->HasCapture() )
422 m_child
->ReleaseMouse();
427 if ( !m_child
->HasCapture() )
429 m_child
->CaptureMouse();
437 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
439 // ----------------------------------------------------------------------------
440 // wxPopupComboWindow
441 // ----------------------------------------------------------------------------
443 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
444 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
447 wxPopupComboWindow::wxPopupComboWindow(wxComboCtrl
*parent
)
448 : wxPopupTransientWindow(parent
)
453 bool wxPopupComboWindow::Create(wxComboCtrl
*parent
)
457 return wxPopupWindow::Create(parent
);
460 void wxPopupComboWindow::PositionNearCombo()
462 // the origin point must be in screen coords
463 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0,0));
465 #if 0 //def __WXUNIVERSAL__
466 // account for the fact that (0, 0) is not the top left corner of the
467 // window: there is also the border
468 wxRect rectBorders
= m_combo
->GetRenderer()->
469 GetBorderDimensions(m_combo
->GetBorder());
470 ptOrigin
.x
-= rectBorders
.x
;
471 ptOrigin
.y
-= rectBorders
.y
;
472 #endif // __WXUNIVERSAL__
474 // position below or above the combobox: the width is 0 to put it exactly
475 // below us, not to the left or to the right
476 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
479 void wxPopupComboWindow::OnDismiss()
481 m_combo
->OnPopupDismiss(true);
484 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
486 m_combo
->ProcessWindowEvent(event
);
489 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
491 // ----------------------------------------------------------------------------
492 // wxPopupWindowHandler
493 // ----------------------------------------------------------------------------
495 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
497 // let the window have it first (we're the first event handler in the chain
498 // of handlers for this window)
499 if ( m_popup
->ProcessLeftDown(event
) )
504 wxPoint pos
= event
.GetPosition();
506 // in non-Univ ports the system manages scrollbars for us
507 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
508 // scrollbar on which the click occurred
509 wxWindow
*sbar
= NULL
;
510 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
512 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
514 switch ( win
->HitTest(pos
.x
, pos
.y
) )
516 case wxHT_WINDOW_OUTSIDE
:
518 // do the coords translation now as after DismissAndNotify()
519 // m_popup may be destroyed
520 wxMouseEvent
event2(event
);
522 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
524 // clicking outside a popup dismisses it
525 m_popup
->DismissAndNotify();
527 // dismissing a tooltip shouldn't waste a click, i.e. you
528 // should be able to dismiss it and press the button with the
529 // same click, so repost this event to the window beneath us
530 wxWindow
*winUnder
= wxFindWindowAtPoint(event2
.GetPosition());
533 // translate the event coords to the ones of the window
534 // which is going to get the event
535 winUnder
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
537 event2
.SetEventObject(winUnder
);
538 wxPostEvent(winUnder
->GetEventHandler(), event2
);
543 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
544 case wxHT_WINDOW_HORZ_SCROLLBAR
:
545 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
548 case wxHT_WINDOW_VERT_SCROLLBAR
:
549 sbar
= win
->GetScrollbar(wxVERTICAL
);
551 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
554 // forgot to update the switch after adding a new hit test code?
555 wxFAIL_MSG( wxT("unexpected HitTest() return value") );
558 case wxHT_WINDOW_CORNER
:
559 // don't actually know if this one is good for anything, but let it
562 case wxHT_WINDOW_INSIDE
:
563 // let the normal processing take place
568 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
571 // translate the event coordinates to the scrollbar ones
572 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
574 // and give the event to it
575 wxMouseEvent event2
= event
;
579 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
581 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
584 // ----------------------------------------------------------------------------
585 // wxPopupFocusHandler
586 // ----------------------------------------------------------------------------
588 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
590 // when we lose focus we always disappear - unless it goes to the popup (in
591 // which case we don't really lose it)
592 wxWindow
*win
= event
.GetWindow();
595 if ( win
== m_popup
)
597 win
= win
->GetParent();
600 m_popup
->DismissAndNotify();
603 void wxPopupFocusHandler::OnChar(wxKeyEvent
& event
)
605 // we can be associated with the popup itself in which case we should avoid
606 // infinite recursion
608 wxRecursionGuard
guard(s_inside
);
609 if ( guard
.IsInside() )
615 // let the window have it first, it might process the keys
616 if ( !m_popup
->GetEventHandler()->ProcessEvent(event
) )
618 // by default, dismiss the popup
619 m_popup
->DismissAndNotify();
623 #endif // wxUSE_POPUPWIN