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
)
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
+ size
.x
;
155 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
157 // check if there is enough space to the left
158 if ( ptOrigin
.x
> sizeSelf
.x
)
160 // do position the control to the left
161 x
-= size
.x
+ sizeSelf
.x
;
163 //else: not enough space there neither, leave in default position
166 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
169 // ----------------------------------------------------------------------------
170 // wxPopupTransientWindow
171 // ----------------------------------------------------------------------------
173 void wxPopupTransientWindow::Init()
176 m_focus
= (wxWindow
*)NULL
;
178 m_handlerFocus
= NULL
;
179 m_handlerPopup
= NULL
;
182 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
186 (void)Create(parent
, style
);
189 wxPopupTransientWindow::~wxPopupTransientWindow()
191 if (m_handlerPopup
&& m_handlerPopup
->GetNextHandler())
194 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
195 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
197 delete m_handlerFocus
;
198 delete m_handlerPopup
;
201 void wxPopupTransientWindow::PopHandlers()
205 if ( !m_child
->RemoveEventHandler(m_handlerPopup
) )
207 // something is very wrong and someone else probably deleted our
208 // handler - so don't risk deleting it second time
209 m_handlerPopup
= NULL
;
211 if (m_child
->HasCapture())
213 m_child
->ReleaseMouse();
220 if ( !m_focus
->RemoveEventHandler(m_handlerFocus
) )
223 m_handlerFocus
= NULL
;
229 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
231 const wxWindowList
& children
= GetChildren();
232 if ( children
.GetCount() )
234 m_child
= children
.GetFirst()->GetData();
243 // There is is a problem if these are still in use
244 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
245 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
248 m_handlerPopup
= new wxPopupWindowHandler(this);
250 m_child
->PushEventHandler(m_handlerPopup
);
252 m_focus
= winFocus
? winFocus
: this;
256 // MSW doesn't allow to set focus to the popup window, but we need to
257 // subclass the window which has the focus, and not winFocus passed in or
258 // otherwise everything else breaks down
259 m_focus
= FindFocus();
260 #elif defined(__WXGTK__)
261 // GTK+ catches the activate events from the popup
262 // window, not the focus events from the child window
269 m_handlerFocus
= new wxPopupFocusHandler(this);
271 m_focus
->PushEventHandler(m_handlerFocus
);
275 bool wxPopupTransientWindow::Show( bool show
)
280 gdk_pointer_ungrab( (guint32
)GDK_CURRENT_TIME
);
282 gtk_grab_remove( m_widget
);
289 XUngrabPointer( wxGlobalDisplay(), CurrentTime
);
294 if (!show
&& m_child
&& m_child
->HasCapture())
296 m_child
->ReleaseMouse();
300 bool ret
= wxPopupWindow::Show( show
);
305 gtk_grab_add( m_widget
);
307 gdk_pointer_grab( m_widget
->window
, TRUE
,
309 (GDK_BUTTON_PRESS_MASK
|
310 GDK_BUTTON_RELEASE_MASK
|
311 GDK_POINTER_MOTION_HINT_MASK
|
312 GDK_POINTER_MOTION_MASK
),
315 (guint32
)GDK_CURRENT_TIME
);
322 Window xwindow
= (Window
) m_clientWindow
;
324 /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow
,
326 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
,
338 // Assume that the mouse is outside the popup to begin with
339 m_child
->CaptureMouse();
346 void wxPopupTransientWindow::Dismiss()
352 void wxPopupTransientWindow::DismissAndNotify()
358 void wxPopupTransientWindow::OnDismiss()
360 // nothing to do here - but it may be interesting for derived class
363 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
365 // no special processing here
370 void wxPopupTransientWindow::OnIdle(wxIdleEvent
& event
)
374 if (IsShown() && m_child
)
376 wxPoint pos
= ScreenToClient(wxGetMousePosition());
377 wxRect
rect(GetSize());
379 if ( rect
.Contains(pos
) )
381 if ( m_child
->HasCapture() )
383 m_child
->ReleaseMouse();
388 if ( !m_child
->HasCapture() )
390 m_child
->CaptureMouse();
398 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
400 // ----------------------------------------------------------------------------
401 // wxPopupComboWindow
402 // ----------------------------------------------------------------------------
404 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
405 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
408 wxPopupComboWindow::wxPopupComboWindow(wxComboCtrl
*parent
)
409 : wxPopupTransientWindow(parent
)
414 bool wxPopupComboWindow::Create(wxComboCtrl
*parent
)
418 return wxPopupWindow::Create(parent
);
421 void wxPopupComboWindow::PositionNearCombo()
423 // the origin point must be in screen coords
424 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0,0));
426 #if 0 //def __WXUNIVERSAL__
427 // account for the fact that (0, 0) is not the top left corner of the
428 // window: there is also the border
429 wxRect rectBorders
= m_combo
->GetRenderer()->
430 GetBorderDimensions(m_combo
->GetBorder());
431 ptOrigin
.x
-= rectBorders
.x
;
432 ptOrigin
.y
-= rectBorders
.y
;
433 #endif // __WXUNIVERSAL__
435 // position below or above the combobox: the width is 0 to put it exactly
436 // below us, not to the left or to the right
437 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
440 void wxPopupComboWindow::OnDismiss()
442 m_combo
->OnPopupDismiss();
445 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
447 m_combo
->ProcessEvent(event
);
450 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
452 // ----------------------------------------------------------------------------
453 // wxPopupWindowHandler
454 // ----------------------------------------------------------------------------
456 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
458 // let the window have it first (we're the first event handler in the chain
459 // of handlers for this window)
460 if ( m_popup
->ProcessLeftDown(event
) )
465 wxPoint pos
= event
.GetPosition();
467 // in non-Univ ports the system manages scrollbars for us
468 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
469 // scrollbar on which the click occurred
470 wxWindow
*sbar
= NULL
;
471 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
473 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
475 switch ( win
->HitTest(pos
.x
, pos
.y
) )
477 case wxHT_WINDOW_OUTSIDE
:
479 // do the coords translation now as after DismissAndNotify()
480 // m_popup may be destroyed
481 wxMouseEvent
event2(event
);
483 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
485 // clicking outside a popup dismisses it
486 m_popup
->DismissAndNotify();
488 // dismissing a tooltip shouldn't waste a click, i.e. you
489 // should be able to dismiss it and press the button with the
490 // same click, so repost this event to the window beneath us
491 wxWindow
*winUnder
= wxFindWindowAtPoint(event2
.GetPosition());
494 // translate the event coords to the ones of the window
495 // which is going to get the event
496 winUnder
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
498 event2
.SetEventObject(winUnder
);
499 wxPostEvent(winUnder
, event2
);
504 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
505 case wxHT_WINDOW_HORZ_SCROLLBAR
:
506 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
509 case wxHT_WINDOW_VERT_SCROLLBAR
:
510 sbar
= win
->GetScrollbar(wxVERTICAL
);
512 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
515 // forgot to update the switch after adding a new hit test code?
516 wxFAIL_MSG( _T("unexpected HitTest() return value") );
519 case wxHT_WINDOW_CORNER
:
520 // don't actually know if this one is good for anything, but let it
523 case wxHT_WINDOW_INSIDE
:
524 // let the normal processing take place
529 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
532 // translate the event coordinates to the scrollbar ones
533 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
535 // and give the event to it
536 wxMouseEvent event2
= event
;
540 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
542 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
545 // ----------------------------------------------------------------------------
546 // wxPopupFocusHandler
547 // ----------------------------------------------------------------------------
549 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
551 // when we lose focus we always disappear - unless it goes to the popup (in
552 // which case we don't really lose it)
553 wxWindow
*win
= event
.GetWindow();
556 if ( win
== m_popup
)
558 win
= win
->GetParent();
561 m_popup
->DismissAndNotify();
564 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
566 // we can be associated with the popup itself in which case we should avoid
567 // infinite recursion
569 wxRecursionGuard
guard(s_inside
);
570 if ( guard
.IsInside() )
576 // let the window have it first, it might process the keys
577 if ( !m_popup
->GetEventHandler()->ProcessEvent(event
) )
579 // by default, dismiss the popup
580 m_popup
->DismissAndNotify();
584 #endif // wxUSE_POPUPWIN