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
)
91 void OnKillFocus(wxFocusEvent
& event
);
92 void OnKeyDown(wxKeyEvent
& event
);
95 wxPopupTransientWindow
*m_popup
;
98 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler
)
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
106 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
109 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
110 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
111 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown
)
114 // ============================================================================
116 // ============================================================================
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 wxPopupWindowBase::~wxPopupWindowBase()
124 // this destructor is required for Darwin
127 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
132 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
135 wxSize sizeScreen
= wxGetDisplaySize(),
136 sizeSelf
= GetSize();
138 // is there enough space to put the popup below the window (where we put it
140 wxCoord y
= ptOrigin
.y
+ size
.y
;
141 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
143 // check if there is enough space above
144 if ( ptOrigin
.y
> sizeSelf
.y
)
146 // do position the control above the window
147 y
-= size
.y
+ sizeSelf
.y
;
149 //else: not enough space below nor above, leave below
152 // now check left/right too
153 wxCoord x
= ptOrigin
.x
+ size
.x
;
154 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
156 // check if there is enough space to the left
157 if ( ptOrigin
.x
> sizeSelf
.x
)
159 // do position the control to the left
160 x
-= size
.x
+ sizeSelf
.x
;
162 //else: not enough space there neither, leave in default position
165 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
168 // ----------------------------------------------------------------------------
169 // wxPopupTransientWindow
170 // ----------------------------------------------------------------------------
172 void wxPopupTransientWindow::Init()
175 m_focus
= (wxWindow
*)NULL
;
177 m_handlerFocus
= NULL
;
178 m_handlerPopup
= NULL
;
181 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
185 (void)Create(parent
, style
);
188 wxPopupTransientWindow::~wxPopupTransientWindow()
193 void wxPopupTransientWindow::PopHandlers()
197 if ( m_handlerPopup
&& !m_child
->RemoveEventHandler(m_handlerPopup
) )
199 // something is very wrong and someone else probably deleted our
200 // handler - so don't risk deleting it second time
201 m_handlerPopup
= NULL
;
204 // m_child->ReleaseMouse();
211 if ( m_handlerFocus
&& !m_focus
->RemoveEventHandler(m_handlerFocus
) )
214 m_handlerFocus
= NULL
;
218 if ( m_handlerFocus
&& !RemoveEventHandler(m_handlerFocus
) )
221 m_handlerFocus
= NULL
;
225 // delete the handlers, they'll be created as necessary in Popup()
226 delete m_handlerPopup
;
227 m_handlerPopup
= NULL
;
228 delete m_handlerFocus
;
229 m_handlerFocus
= NULL
;
234 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
236 const wxWindowList
& children
= GetChildren();
237 if ( children
.GetCount() )
239 m_child
= children
.GetFirst()->GetData();
248 delete m_handlerPopup
;
249 m_handlerPopup
= new wxPopupWindowHandler(this);
251 // m_child->CaptureMouse();
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();
264 delete m_handlerFocus
;
265 m_handlerFocus
= new wxPopupFocusHandler(this);
267 m_focus
->PushEventHandler(m_handlerFocus
);
270 // GTK+ catches the activate events from the popup
271 // window, not the focus events from the child window
272 delete m_handlerFocus
;
273 m_handlerFocus
= new wxPopupFocusHandler(this);
274 PushEventHandler(m_handlerFocus
);
279 bool wxPopupTransientWindow::Show( bool show
)
284 gdk_pointer_ungrab( (guint32
)GDK_CURRENT_TIME
);
286 gtk_grab_remove( m_widget
);
293 XUngrabPointer( wxGlobalDisplay(), CurrentTime
);
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
,
334 void wxPopupTransientWindow::Dismiss()
341 void wxPopupTransientWindow::DismissAndNotify()
348 void wxPopupTransientWindow::OnDismiss()
350 // nothing to do here - but it may be interesting for derived class
353 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
355 // no special processing here
359 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
361 // ----------------------------------------------------------------------------
362 // wxPopupComboWindow
363 // ----------------------------------------------------------------------------
365 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
366 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
369 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
370 : wxPopupTransientWindow(parent
)
375 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
379 return wxPopupWindow::Create(parent
);
382 void wxPopupComboWindow::PositionNearCombo()
384 // the origin point must be in screen coords
385 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0,0));
387 #if 0 //def __WXUNIVERSAL__
388 // account for the fact that (0, 0) is not the top left corner of the
389 // window: there is also the border
390 wxRect rectBorders
= m_combo
->GetRenderer()->
391 GetBorderDimensions(m_combo
->GetBorder());
392 ptOrigin
.x
-= rectBorders
.x
;
393 ptOrigin
.y
-= rectBorders
.y
;
394 #endif // __WXUNIVERSAL__
396 // position below or above the combobox: the width is 0 to put it exactly
397 // below us, not to the left or to the right
398 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
401 void wxPopupComboWindow::OnDismiss()
403 m_combo
->OnDismiss();
406 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
408 m_combo
->ProcessEvent(event
);
411 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
413 // ----------------------------------------------------------------------------
414 // wxPopupWindowHandler
415 // ----------------------------------------------------------------------------
417 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
419 // let the window have it first (we're the first event handler in the chain
420 // of handlers for this window)
421 if ( m_popup
->ProcessLeftDown(event
) )
426 wxPoint pos
= event
.GetPosition();
428 // scrollbar on which the click occured
429 wxWindow
*sbar
= NULL
;
431 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
433 switch ( win
->HitTest(pos
.x
, pos
.y
) )
435 case wxHT_WINDOW_OUTSIDE
:
437 // do the coords translation now as after DismissAndNotify()
438 // m_popup may be destroyed
439 wxMouseEvent
event2(event
);
441 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
443 // clicking outside a popup dismisses it
444 m_popup
->DismissAndNotify();
446 // dismissing a tooltip shouldn't waste a click, i.e. you
447 // should be able to dismiss it and press the button with the
448 // same click, so repost this event to the window beneath us
449 wxWindow
*win
= wxFindWindowAtPoint(event2
.GetPosition());
452 // translate the event coords to the ones of the window
453 // which is going to get the event
454 win
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
456 event2
.SetEventObject(win
);
457 wxPostEvent(win
, event2
);
462 #ifdef __WXUNIVERSAL__
463 case wxHT_WINDOW_HORZ_SCROLLBAR
:
464 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
467 case wxHT_WINDOW_VERT_SCROLLBAR
:
468 sbar
= win
->GetScrollbar(wxVERTICAL
);
473 // forgot to update the switch after adding a new hit test code?
474 wxFAIL_MSG( _T("unexpected HitTest() return value") );
477 case wxHT_WINDOW_CORNER
:
478 // don't actually know if this one is good for anything, but let it
481 case wxHT_WINDOW_INSIDE
:
482 // let the normal processing take place
489 // translate the event coordinates to the scrollbar ones
490 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
492 // and give the event to it
493 wxMouseEvent event2
= event
;
497 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
501 // ----------------------------------------------------------------------------
502 // wxPopupFocusHandler
503 // ----------------------------------------------------------------------------
505 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
507 // when we lose focus we always disappear - unless it goes to the popup (in
508 // which case we don't really lose it)
509 wxWindow
*win
= event
.GetWindow();
512 if ( win
== m_popup
)
514 win
= win
->GetParent();
517 m_popup
->DismissAndNotify();
520 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
522 // let the window have it first, it might process the keys
523 if ( !m_popup
->ProcessEvent(event
) )
525 // by default, dismiss the popup
526 m_popup
->DismissAndNotify();
530 #endif // wxUSE_POPUPWIN