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
) {}
70 ~wxPopupWindowHandler();
74 void OnLeftDown(wxMouseEvent
& event
);
77 wxPopupTransientWindow
*m_popup
;
80 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler
)
83 class wxPopupFocusHandler
: public wxEvtHandler
86 wxPopupFocusHandler(wxPopupTransientWindow
*popup
) : m_popup(popup
) {}
87 ~wxPopupFocusHandler();
90 void OnKillFocus(wxFocusEvent
& event
);
91 void OnKeyDown(wxKeyEvent
& event
);
94 wxPopupTransientWindow
*m_popup
;
97 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler
)
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
105 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
108 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
109 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
110 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown
)
113 // ============================================================================
115 // ============================================================================
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 wxPopupWindowBase::~wxPopupWindowBase()
123 // this destructor is required for Darwin
126 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
131 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
134 wxSize sizeScreen
= wxGetDisplaySize(),
135 sizeSelf
= GetSize();
137 // is there enough space to put the popup below the window (where we put it
139 wxCoord y
= ptOrigin
.y
+ size
.y
;
140 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
142 // check if there is enough space above
143 if ( ptOrigin
.y
> sizeSelf
.y
)
145 // do position the control above the window
146 y
-= size
.y
+ sizeSelf
.y
;
148 //else: not enough space below nor above, leave below
151 // now check left/right too
152 wxCoord x
= ptOrigin
.x
+ size
.x
;
153 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
155 // check if there is enough space to the left
156 if ( ptOrigin
.x
> sizeSelf
.x
)
158 // do position the control to the left
159 x
-= size
.x
+ sizeSelf
.x
;
161 //else: not enough space there neither, leave in default position
164 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
167 // ----------------------------------------------------------------------------
168 // wxPopupTransientWindow
169 // ----------------------------------------------------------------------------
171 void wxPopupTransientWindow::Init()
174 m_focus
= (wxWindow
*)NULL
;
176 m_handlerFocus
= NULL
;
177 m_handlerPopup
= NULL
;
180 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
184 (void)Create(parent
, style
);
187 wxPopupTransientWindow::~wxPopupTransientWindow()
192 void wxPopupTransientWindow::PopHandlers()
196 if ( m_handlerPopup
&& !m_child
->RemoveEventHandler(m_handlerPopup
) )
198 // something is very wrong and someone else probably deleted our
199 // handler - so don't risk deleting it second time
200 m_handlerPopup
= NULL
;
209 if ( m_handlerFocus
&& !m_focus
->RemoveEventHandler(m_handlerFocus
) )
212 m_handlerFocus
= NULL
;
216 if ( m_handlerFocus
&& !RemoveEventHandler(m_handlerFocus
) )
219 m_handlerFocus
= NULL
;
223 // delete the handlers, they'll be created as necessary in Popup()
224 delete m_handlerPopup
;
225 m_handlerPopup
= NULL
;
226 delete m_handlerFocus
;
227 m_handlerFocus
= NULL
;
232 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
234 const wxWindowList
& children
= GetChildren();
235 if ( children
.GetCount() )
237 m_child
= children
.GetFirst()->GetData();
246 // There is is a problem if these are still valid
247 wxASSERT_MSG(!m_handlerPopup
, wxT("Popup handler not deleted"));
248 wxASSERT_MSG(!m_handlerFocus
, wxT("Focus handler not deleted"));
250 delete m_handlerPopup
;
251 m_handlerPopup
= new wxPopupWindowHandler(this);
253 m_child
->PushEventHandler(m_handlerPopup
);
255 m_focus
= winFocus
? winFocus
: this;
259 // MSW doesn't allow to set focus to the popup window, but we need to
260 // subclass the window which has the focus, and not winFocus passed in or
261 // otherwise everything else breaks down
262 m_focus
= FindFocus();
265 delete m_handlerFocus
;
266 m_handlerFocus
= new wxPopupFocusHandler(this);
268 m_focus
->PushEventHandler(m_handlerFocus
);
271 // GTK+ catches the activate events from the popup
272 // window, not the focus events from the child window
273 delete m_handlerFocus
;
274 m_handlerFocus
= new wxPopupFocusHandler(this);
275 PushEventHandler(m_handlerFocus
);
278 // catch destroy events, if you close a program with a popup shown in MSW
279 // you get a segfault if m_child or m_focus are deleted before this is
280 m_child
->Connect(wxEVT_DESTROY
,
281 wxWindowDestroyEventHandler(wxPopupTransientWindow::OnDestroy
),
283 m_focus
->Connect(wxEVT_DESTROY
,
284 wxWindowDestroyEventHandler(wxPopupTransientWindow::OnDestroy
),
288 bool wxPopupTransientWindow::Show( bool show
)
293 gdk_pointer_ungrab( (guint32
)GDK_CURRENT_TIME
);
295 gtk_grab_remove( m_widget
);
302 XUngrabPointer( wxGlobalDisplay(), CurrentTime
);
306 bool ret
= wxPopupWindow::Show( show
);
311 gtk_grab_add( m_widget
);
313 gdk_pointer_grab( m_widget
->window
, TRUE
,
315 (GDK_BUTTON_PRESS_MASK
|
316 GDK_BUTTON_RELEASE_MASK
|
317 GDK_POINTER_MOTION_HINT_MASK
|
318 GDK_POINTER_MOTION_MASK
),
321 (guint32
)GDK_CURRENT_TIME
);
328 Window xwindow
= (Window
) m_clientWindow
;
330 /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow
,
332 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
,
343 void wxPopupTransientWindow::Dismiss()
350 void wxPopupTransientWindow::DismissAndNotify()
357 void wxPopupTransientWindow::OnDismiss()
359 // nothing to do here - but it may be interesting for derived class
362 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
364 // no special processing here
368 void wxPopupTransientWindow::OnDestroy(wxWindowDestroyEvent
& event
)
370 if (event
.GetEventObject() == m_child
)
372 if (event
.GetEventObject() == m_focus
)
376 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
378 // ----------------------------------------------------------------------------
379 // wxPopupComboWindow
380 // ----------------------------------------------------------------------------
382 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
383 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
386 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
387 : wxPopupTransientWindow(parent
)
392 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
396 return wxPopupWindow::Create(parent
);
399 void wxPopupComboWindow::PositionNearCombo()
401 // the origin point must be in screen coords
402 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0,0));
404 #if 0 //def __WXUNIVERSAL__
405 // account for the fact that (0, 0) is not the top left corner of the
406 // window: there is also the border
407 wxRect rectBorders
= m_combo
->GetRenderer()->
408 GetBorderDimensions(m_combo
->GetBorder());
409 ptOrigin
.x
-= rectBorders
.x
;
410 ptOrigin
.y
-= rectBorders
.y
;
411 #endif // __WXUNIVERSAL__
413 // position below or above the combobox: the width is 0 to put it exactly
414 // below us, not to the left or to the right
415 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
418 void wxPopupComboWindow::OnDismiss()
420 m_combo
->OnDismiss();
423 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
425 m_combo
->ProcessEvent(event
);
428 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
430 // ----------------------------------------------------------------------------
431 // wxPopupWindowHandler
432 // ----------------------------------------------------------------------------
433 wxPopupWindowHandler::~wxPopupWindowHandler()
435 if (m_popup
&& (m_popup
->m_handlerPopup
== this))
436 m_popup
->m_handlerPopup
= NULL
;
439 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
441 // let the window have it first (we're the first event handler in the chain
442 // of handlers for this window)
443 if ( m_popup
->ProcessLeftDown(event
) )
448 wxPoint pos
= event
.GetPosition();
450 // scrollbar on which the click occured
451 wxWindow
*sbar
= NULL
;
453 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
455 switch ( win
->HitTest(pos
.x
, pos
.y
) )
457 case wxHT_WINDOW_OUTSIDE
:
459 // do the coords translation now as after DismissAndNotify()
460 // m_popup may be destroyed
461 wxMouseEvent
event2(event
);
463 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
465 // clicking outside a popup dismisses it
466 m_popup
->DismissAndNotify();
468 // dismissing a tooltip shouldn't waste a click, i.e. you
469 // should be able to dismiss it and press the button with the
470 // same click, so repost this event to the window beneath us
471 wxWindow
*win
= wxFindWindowAtPoint(event2
.GetPosition());
474 // translate the event coords to the ones of the window
475 // which is going to get the event
476 win
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
478 event2
.SetEventObject(win
);
479 wxPostEvent(win
, event2
);
484 #ifdef __WXUNIVERSAL__
485 case wxHT_WINDOW_HORZ_SCROLLBAR
:
486 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
489 case wxHT_WINDOW_VERT_SCROLLBAR
:
490 sbar
= win
->GetScrollbar(wxVERTICAL
);
495 // forgot to update the switch after adding a new hit test code?
496 wxFAIL_MSG( _T("unexpected HitTest() return value") );
499 case wxHT_WINDOW_CORNER
:
500 // don't actually know if this one is good for anything, but let it
503 case wxHT_WINDOW_INSIDE
:
504 // let the normal processing take place
511 // translate the event coordinates to the scrollbar ones
512 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
514 // and give the event to it
515 wxMouseEvent event2
= event
;
519 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
523 // ----------------------------------------------------------------------------
524 // wxPopupFocusHandler
525 // ----------------------------------------------------------------------------
526 wxPopupFocusHandler::~wxPopupFocusHandler()
528 if (m_popup
&& (m_popup
->m_handlerFocus
== this))
529 m_popup
->m_handlerFocus
= NULL
;
532 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
534 // when we lose focus we always disappear - unless it goes to the popup (in
535 // which case we don't really lose it)
536 wxWindow
*win
= event
.GetWindow();
539 if ( win
== m_popup
)
541 win
= win
->GetParent();
544 m_popup
->DismissAndNotify();
547 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
549 // let the window have it first, it might process the keys
550 if ( !m_popup
->ProcessEvent(event
) )
552 // by default, dismiss the popup
553 m_popup
->DismissAndNotify();
557 #endif // wxUSE_POPUPWIN