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" // wxComboControl
33 #include "wx/app.h" // wxPostEvent
37 #ifdef __WXUNIVERSAL__
38 #include "wx/univ/renderer.h"
39 #endif // __WXUNIVERSAL__
45 #include "wx/x11/private.h"
48 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow
, wxWindow
)
49 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow
, wxPopupWindow
)
51 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
52 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow
, wxPopupTransientWindow
)
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // event handlers which we use to intercept events which cause the popup to
61 class wxPopupWindowHandler
: public wxEvtHandler
64 wxPopupWindowHandler(wxPopupTransientWindow
*popup
) : m_popup(popup
) {}
68 void OnLeftDown(wxMouseEvent
& event
);
71 wxPopupTransientWindow
*m_popup
;
74 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler
)
77 class wxPopupFocusHandler
: public wxEvtHandler
80 wxPopupFocusHandler(wxPopupTransientWindow
*popup
) : m_popup(popup
) {}
83 void OnKillFocus(wxFocusEvent
& event
);
84 void OnKeyDown(wxKeyEvent
& event
);
87 wxPopupTransientWindow
*m_popup
;
90 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler
)
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
98 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
101 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
102 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
103 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown
)
106 BEGIN_EVENT_TABLE(wxPopupTransientWindow
, wxPopupWindow
)
108 EVT_IDLE(wxPopupTransientWindow::OnIdle
)
112 // ============================================================================
114 // ============================================================================
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 wxPopupWindowBase::~wxPopupWindowBase()
122 // this destructor is required for Darwin
125 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
130 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
133 wxSize sizeScreen
= wxGetDisplaySize(),
134 sizeSelf
= GetSize();
136 // is there enough space to put the popup below the window (where we put it
138 wxCoord y
= ptOrigin
.y
+ size
.y
;
139 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
141 // check if there is enough space above
142 if ( ptOrigin
.y
> sizeSelf
.y
)
144 // do position the control above the window
145 y
-= size
.y
+ sizeSelf
.y
;
147 //else: not enough space below nor above, leave below
150 // now check left/right too
151 wxCoord x
= ptOrigin
.x
+ size
.x
;
152 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
154 // check if there is enough space to the left
155 if ( ptOrigin
.x
> sizeSelf
.x
)
157 // do position the control to the left
158 x
-= size
.x
+ sizeSelf
.x
;
160 //else: not enough space there neither, leave in default position
163 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
166 // ----------------------------------------------------------------------------
167 // wxPopupTransientWindow
168 // ----------------------------------------------------------------------------
170 void wxPopupTransientWindow::Init()
173 m_focus
= (wxWindow
*)NULL
;
175 m_handlerFocus
= NULL
;
176 m_handlerPopup
= NULL
;
179 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
183 (void)Create(parent
, style
);
186 wxPopupTransientWindow::~wxPopupTransientWindow()
188 if (m_handlerPopup
&& m_handlerPopup
->GetNextHandler())
191 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
192 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
194 delete m_handlerFocus
;
195 delete m_handlerPopup
;
198 void wxPopupTransientWindow::PopHandlers()
202 if ( !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();
217 if ( !m_focus
->RemoveEventHandler(m_handlerFocus
) )
220 m_handlerFocus
= NULL
;
226 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
228 const wxWindowList
& children
= GetChildren();
229 if ( children
.GetCount() )
231 m_child
= children
.GetFirst()->GetData();
240 // There is is a problem if these are still in use
241 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
242 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
245 m_handlerPopup
= new wxPopupWindowHandler(this);
247 m_child
->PushEventHandler(m_handlerPopup
);
249 m_focus
= winFocus
? winFocus
: this;
253 // MSW doesn't allow to set focus to the popup window, but we need to
254 // subclass the window which has the focus, and not winFocus passed in or
255 // otherwise everything else breaks down
256 m_focus
= FindFocus();
257 #elif defined(__WXGTK__)
258 // GTK+ catches the activate events from the popup
259 // window, not the focus events from the child window
266 m_handlerFocus
= new wxPopupFocusHandler(this);
268 m_focus
->PushEventHandler(m_handlerFocus
);
272 bool wxPopupTransientWindow::Show( bool show
)
277 gdk_pointer_ungrab( (guint32
)GDK_CURRENT_TIME
);
279 gtk_grab_remove( m_widget
);
286 XUngrabPointer( wxGlobalDisplay(), CurrentTime
);
291 if (!show
&& m_child
&& m_child
->HasCapture())
293 m_child
->ReleaseMouse();
297 bool ret
= wxPopupWindow::Show( show
);
302 gtk_grab_add( m_widget
);
304 gdk_pointer_grab( m_widget
->window
, TRUE
,
306 (GDK_BUTTON_PRESS_MASK
|
307 GDK_BUTTON_RELEASE_MASK
|
308 GDK_POINTER_MOTION_HINT_MASK
|
309 GDK_POINTER_MOTION_MASK
),
312 (guint32
)GDK_CURRENT_TIME
);
319 Window xwindow
= (Window
) m_clientWindow
;
321 /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow
,
323 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
,
335 // Assume that the mouse is outside the popup to begin with
336 m_child
->CaptureMouse();
343 void wxPopupTransientWindow::Dismiss()
349 void wxPopupTransientWindow::DismissAndNotify()
355 void wxPopupTransientWindow::OnDismiss()
357 // nothing to do here - but it may be interesting for derived class
360 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
362 // no special processing here
367 void wxPopupTransientWindow::OnIdle(wxIdleEvent
& event
)
371 if (IsShown() && m_child
)
373 wxPoint pos
= ScreenToClient(wxGetMousePosition());
374 wxRect
rect(GetSize());
376 if ( rect
.Inside(pos
) )
378 if ( m_child
->HasCapture() )
380 m_child
->ReleaseMouse();
385 if ( !m_child
->HasCapture() )
387 m_child
->CaptureMouse();
395 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
397 // ----------------------------------------------------------------------------
398 // wxPopupComboWindow
399 // ----------------------------------------------------------------------------
401 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
402 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
405 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
406 : wxPopupTransientWindow(parent
)
411 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
415 return wxPopupWindow::Create(parent
);
418 void wxPopupComboWindow::PositionNearCombo()
420 // the origin point must be in screen coords
421 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0,0));
423 #if 0 //def __WXUNIVERSAL__
424 // account for the fact that (0, 0) is not the top left corner of the
425 // window: there is also the border
426 wxRect rectBorders
= m_combo
->GetRenderer()->
427 GetBorderDimensions(m_combo
->GetBorder());
428 ptOrigin
.x
-= rectBorders
.x
;
429 ptOrigin
.y
-= rectBorders
.y
;
430 #endif // __WXUNIVERSAL__
432 // position below or above the combobox: the width is 0 to put it exactly
433 // below us, not to the left or to the right
434 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
437 void wxPopupComboWindow::OnDismiss()
439 m_combo
->OnDismiss();
442 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
444 m_combo
->ProcessEvent(event
);
447 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
449 // ----------------------------------------------------------------------------
450 // wxPopupWindowHandler
451 // ----------------------------------------------------------------------------
453 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
455 // let the window have it first (we're the first event handler in the chain
456 // of handlers for this window)
457 if ( m_popup
->ProcessLeftDown(event
) )
462 wxPoint pos
= event
.GetPosition();
464 // in non-Univ ports the system manages scrollbars for us
465 #ifdef __WXUNIVERSAL__
466 // scrollbar on which the click occurred
467 wxWindow
*sbar
= NULL
;
468 #endif // __WXUNIVERSAL__
470 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
472 switch ( win
->HitTest(pos
.x
, pos
.y
) )
474 case wxHT_WINDOW_OUTSIDE
:
476 // do the coords translation now as after DismissAndNotify()
477 // m_popup may be destroyed
478 wxMouseEvent
event2(event
);
480 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
482 // clicking outside a popup dismisses it
483 m_popup
->DismissAndNotify();
485 // dismissing a tooltip shouldn't waste a click, i.e. you
486 // should be able to dismiss it and press the button with the
487 // same click, so repost this event to the window beneath us
488 wxWindow
*winUnder
= wxFindWindowAtPoint(event2
.GetPosition());
491 // translate the event coords to the ones of the window
492 // which is going to get the event
493 winUnder
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
495 event2
.SetEventObject(winUnder
);
496 wxPostEvent(winUnder
, event2
);
501 #ifdef __WXUNIVERSAL__
502 case wxHT_WINDOW_HORZ_SCROLLBAR
:
503 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
506 case wxHT_WINDOW_VERT_SCROLLBAR
:
507 sbar
= win
->GetScrollbar(wxVERTICAL
);
509 #endif // __WXUNIVERSAL__
512 // forgot to update the switch after adding a new hit test code?
513 wxFAIL_MSG( _T("unexpected HitTest() return value") );
516 case wxHT_WINDOW_CORNER
:
517 // don't actually know if this one is good for anything, but let it
520 case wxHT_WINDOW_INSIDE
:
521 // let the normal processing take place
526 #ifdef __WXUNIVERSAL__
529 // translate the event coordinates to the scrollbar ones
530 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
532 // and give the event to it
533 wxMouseEvent event2
= event
;
537 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
539 #endif // __WXUNIVERSAL__
542 // ----------------------------------------------------------------------------
543 // wxPopupFocusHandler
544 // ----------------------------------------------------------------------------
546 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
548 // when we lose focus we always disappear - unless it goes to the popup (in
549 // which case we don't really lose it)
550 wxWindow
*win
= event
.GetWindow();
553 if ( win
== m_popup
)
555 win
= win
->GetParent();
558 m_popup
->DismissAndNotify();
561 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
563 // let the window have it first, it might process the keys
564 if ( !m_popup
->ProcessEvent(event
) )
566 // by default, dismiss the popup
567 m_popup
->DismissAndNotify();
571 #endif // wxUSE_POPUPWIN