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
) {}
73 void OnLeftDown(wxMouseEvent
& event
);
76 wxPopupTransientWindow
*m_popup
;
79 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler
)
82 class wxPopupFocusHandler
: public wxEvtHandler
85 wxPopupFocusHandler(wxPopupTransientWindow
*popup
) : m_popup(popup
) {}
88 void OnKillFocus(wxFocusEvent
& event
);
89 void OnKeyDown(wxKeyEvent
& event
);
92 wxPopupTransientWindow
*m_popup
;
95 DECLARE_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_KEY_DOWN(wxPopupFocusHandler::OnKeyDown
)
111 BEGIN_EVENT_TABLE(wxPopupTransientWindow
, wxPopupWindow
)
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 wxSize sizeScreen
= wxGetDisplaySize(),
139 sizeSelf
= GetSize();
141 // is there enough space to put the popup below the window (where we put it
143 wxCoord y
= ptOrigin
.y
+ size
.y
;
144 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
146 // check if there is enough space above
147 if ( ptOrigin
.y
> sizeSelf
.y
)
149 // do position the control above the window
150 y
-= size
.y
+ sizeSelf
.y
;
152 //else: not enough space below nor above, leave below
155 // now check left/right too
156 wxCoord x
= ptOrigin
.x
+ size
.x
;
157 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
159 // check if there is enough space to the left
160 if ( ptOrigin
.x
> sizeSelf
.x
)
162 // do position the control to the left
163 x
-= size
.x
+ sizeSelf
.x
;
165 //else: not enough space there neither, leave in default position
168 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
171 // ----------------------------------------------------------------------------
172 // wxPopupTransientWindow
173 // ----------------------------------------------------------------------------
175 void wxPopupTransientWindow::Init()
178 m_focus
= (wxWindow
*)NULL
;
180 m_handlerFocus
= NULL
;
181 m_handlerPopup
= NULL
;
184 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
188 (void)Create(parent
, style
);
191 wxPopupTransientWindow::~wxPopupTransientWindow()
193 if (m_handlerPopup
&& m_handlerPopup
->GetNextHandler())
196 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
197 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
199 delete m_handlerFocus
;
200 delete m_handlerPopup
;
203 void wxPopupTransientWindow::PopHandlers()
207 if ( !m_child
->RemoveEventHandler(m_handlerPopup
) )
209 // something is very wrong and someone else probably deleted our
210 // handler - so don't risk deleting it second time
211 m_handlerPopup
= NULL
;
213 if (m_child
->HasCapture())
215 m_child
->ReleaseMouse();
222 if ( !m_focus
->RemoveEventHandler(m_handlerFocus
) )
225 m_handlerFocus
= NULL
;
231 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
233 const wxWindowList
& children
= GetChildren();
234 if ( children
.GetCount() )
236 m_child
= children
.GetFirst()->GetData();
245 // There is is a problem if these are still in use
246 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
247 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
250 m_handlerPopup
= new wxPopupWindowHandler(this);
252 m_child
->PushEventHandler(m_handlerPopup
);
254 m_focus
= winFocus
? winFocus
: this;
258 // MSW doesn't allow to set focus to the popup window, but we need to
259 // subclass the window which has the focus, and not winFocus passed in or
260 // otherwise everything else breaks down
261 m_focus
= FindFocus();
262 #elif defined(__WXGTK__)
263 // GTK+ catches the activate events from the popup
264 // window, not the focus events from the child window
271 m_handlerFocus
= new wxPopupFocusHandler(this);
273 m_focus
->PushEventHandler(m_handlerFocus
);
277 bool wxPopupTransientWindow::Show( bool show
)
282 gdk_pointer_ungrab( (guint32
)GDK_CURRENT_TIME
);
284 gtk_grab_remove( m_widget
);
291 XUngrabPointer( wxGlobalDisplay(), CurrentTime
);
296 if (!show
&& m_child
&& m_child
->HasCapture())
298 m_child
->ReleaseMouse();
302 bool ret
= wxPopupWindow::Show( show
);
307 gtk_grab_add( m_widget
);
309 gdk_pointer_grab( m_widget
->window
, TRUE
,
311 (GDK_BUTTON_PRESS_MASK
|
312 GDK_BUTTON_RELEASE_MASK
|
313 GDK_POINTER_MOTION_HINT_MASK
|
314 GDK_POINTER_MOTION_MASK
),
317 (guint32
)GDK_CURRENT_TIME
);
324 Window xwindow
= (Window
) m_clientWindow
;
326 /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow
,
328 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
,
340 // Assume that the mouse is outside the popup to begin with
341 m_child
->CaptureMouse();
348 void wxPopupTransientWindow::Dismiss()
354 void wxPopupTransientWindow::DismissAndNotify()
360 void wxPopupTransientWindow::OnDismiss()
362 // nothing to do here - but it may be interesting for derived class
365 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
367 // no special processing here
372 void wxPopupTransientWindow::OnIdle(wxIdleEvent
& event
)
376 if (IsShown() && m_child
)
378 wxPoint pos
= ScreenToClient(wxGetMousePosition());
379 wxRect
rect(GetSize());
381 if ( rect
.Inside(pos
) )
383 if ( m_child
->HasCapture() )
385 m_child
->ReleaseMouse();
390 if ( !m_child
->HasCapture() )
392 m_child
->CaptureMouse();
400 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
402 // ----------------------------------------------------------------------------
403 // wxPopupComboWindow
404 // ----------------------------------------------------------------------------
406 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
407 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
410 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
411 : wxPopupTransientWindow(parent
)
416 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
420 return wxPopupWindow::Create(parent
);
423 void wxPopupComboWindow::PositionNearCombo()
425 // the origin point must be in screen coords
426 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0,0));
428 #if 0 //def __WXUNIVERSAL__
429 // account for the fact that (0, 0) is not the top left corner of the
430 // window: there is also the border
431 wxRect rectBorders
= m_combo
->GetRenderer()->
432 GetBorderDimensions(m_combo
->GetBorder());
433 ptOrigin
.x
-= rectBorders
.x
;
434 ptOrigin
.y
-= rectBorders
.y
;
435 #endif // __WXUNIVERSAL__
437 // position below or above the combobox: the width is 0 to put it exactly
438 // below us, not to the left or to the right
439 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
442 void wxPopupComboWindow::OnDismiss()
444 m_combo
->OnDismiss();
447 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
449 m_combo
->ProcessEvent(event
);
452 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
454 // ----------------------------------------------------------------------------
455 // wxPopupWindowHandler
456 // ----------------------------------------------------------------------------
458 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
460 // let the window have it first (we're the first event handler in the chain
461 // of handlers for this window)
462 if ( m_popup
->ProcessLeftDown(event
) )
467 wxPoint pos
= event
.GetPosition();
469 // scrollbar on which the click occurred
470 wxWindow
*sbar
= NULL
;
472 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
474 switch ( win
->HitTest(pos
.x
, pos
.y
) )
476 case wxHT_WINDOW_OUTSIDE
:
478 // do the coords translation now as after DismissAndNotify()
479 // m_popup may be destroyed
480 wxMouseEvent
event2(event
);
482 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
484 // clicking outside a popup dismisses it
485 m_popup
->DismissAndNotify();
487 // dismissing a tooltip shouldn't waste a click, i.e. you
488 // should be able to dismiss it and press the button with the
489 // same click, so repost this event to the window beneath us
490 wxWindow
*win
= wxFindWindowAtPoint(event2
.GetPosition());
493 // translate the event coords to the ones of the window
494 // which is going to get the event
495 win
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
497 event2
.SetEventObject(win
);
498 wxPostEvent(win
, event2
);
503 #ifdef __WXUNIVERSAL__
504 case wxHT_WINDOW_HORZ_SCROLLBAR
:
505 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
508 case wxHT_WINDOW_VERT_SCROLLBAR
:
509 sbar
= win
->GetScrollbar(wxVERTICAL
);
514 // forgot to update the switch after adding a new hit test code?
515 wxFAIL_MSG( _T("unexpected HitTest() return value") );
518 case wxHT_WINDOW_CORNER
:
519 // don't actually know if this one is good for anything, but let it
522 case wxHT_WINDOW_INSIDE
:
523 // let the normal processing take place
530 // translate the event coordinates to the scrollbar ones
531 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
533 // and give the event to it
534 wxMouseEvent event2
= event
;
538 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
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