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 // 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
38 #ifdef __WXUNIVERSAL__
39 #include "wx/univ/renderer.h"
40 #endif // __WXUNIVERSAL__
46 #include "wx/x11/private.h"
49 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow
, wxWindow
)
50 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow
, wxPopupWindow
)
52 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
53 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow
, wxPopupTransientWindow
)
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // event handlers which we use to intercept events which cause the popup to
62 class wxPopupWindowHandler
: public wxEvtHandler
65 wxPopupWindowHandler(wxPopupTransientWindow
*popup
) : m_popup(popup
) {}
69 void OnLeftDown(wxMouseEvent
& event
);
72 wxPopupTransientWindow
*m_popup
;
75 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler
)
78 class wxPopupFocusHandler
: public wxEvtHandler
81 wxPopupFocusHandler(wxPopupTransientWindow
*popup
) : m_popup(popup
) {}
84 void OnKillFocus(wxFocusEvent
& event
);
85 void OnKeyDown(wxKeyEvent
& event
);
88 wxPopupTransientWindow
*m_popup
;
91 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler
)
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
99 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
102 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
103 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
104 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown
)
107 BEGIN_EVENT_TABLE(wxPopupTransientWindow
, wxPopupWindow
)
109 EVT_IDLE(wxPopupTransientWindow::OnIdle
)
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()
189 if (m_handlerPopup
&& m_handlerPopup
->GetNextHandler())
192 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
193 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
195 delete m_handlerFocus
;
196 delete m_handlerPopup
;
199 void wxPopupTransientWindow::PopHandlers()
203 if ( !m_child
->RemoveEventHandler(m_handlerPopup
) )
205 // something is very wrong and someone else probably deleted our
206 // handler - so don't risk deleting it second time
207 m_handlerPopup
= NULL
;
209 if (m_child
->HasCapture())
211 m_child
->ReleaseMouse();
218 if ( !m_focus
->RemoveEventHandler(m_handlerFocus
) )
221 m_handlerFocus
= NULL
;
227 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
229 const wxWindowList
& children
= GetChildren();
230 if ( children
.GetCount() )
232 m_child
= children
.GetFirst()->GetData();
241 // There is is a problem if these are still in use
242 wxASSERT(!m_handlerFocus
|| !m_handlerFocus
->GetNextHandler());
243 wxASSERT(!m_handlerPopup
|| !m_handlerPopup
->GetNextHandler());
246 m_handlerPopup
= new wxPopupWindowHandler(this);
248 m_child
->PushEventHandler(m_handlerPopup
);
250 m_focus
= winFocus
? winFocus
: this;
254 // MSW doesn't allow to set focus to the popup window, but we need to
255 // subclass the window which has the focus, and not winFocus passed in or
256 // otherwise everything else breaks down
257 m_focus
= FindFocus();
258 #elif defined(__WXGTK__)
259 // GTK+ catches the activate events from the popup
260 // window, not the focus events from the child window
267 m_handlerFocus
= new wxPopupFocusHandler(this);
269 m_focus
->PushEventHandler(m_handlerFocus
);
273 bool wxPopupTransientWindow::Show( bool show
)
278 gdk_pointer_ungrab( (guint32
)GDK_CURRENT_TIME
);
280 gtk_grab_remove( m_widget
);
287 XUngrabPointer( wxGlobalDisplay(), CurrentTime
);
292 if (!show
&& m_child
&& m_child
->HasCapture())
294 m_child
->ReleaseMouse();
298 bool ret
= wxPopupWindow::Show( show
);
303 gtk_grab_add( m_widget
);
305 gdk_pointer_grab( m_widget
->window
, TRUE
,
307 (GDK_BUTTON_PRESS_MASK
|
308 GDK_BUTTON_RELEASE_MASK
|
309 GDK_POINTER_MOTION_HINT_MASK
|
310 GDK_POINTER_MOTION_MASK
),
313 (guint32
)GDK_CURRENT_TIME
);
320 Window xwindow
= (Window
) m_clientWindow
;
322 /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow
,
324 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
,
336 // Assume that the mouse is outside the popup to begin with
337 m_child
->CaptureMouse();
344 void wxPopupTransientWindow::Dismiss()
350 void wxPopupTransientWindow::DismissAndNotify()
356 void wxPopupTransientWindow::OnDismiss()
358 // nothing to do here - but it may be interesting for derived class
361 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
363 // no special processing here
368 void wxPopupTransientWindow::OnIdle(wxIdleEvent
& event
)
372 if (IsShown() && m_child
)
374 wxPoint pos
= ScreenToClient(wxGetMousePosition());
375 wxRect
rect(GetSize());
377 if ( rect
.Inside(pos
) )
379 if ( m_child
->HasCapture() )
381 m_child
->ReleaseMouse();
386 if ( !m_child
->HasCapture() )
388 m_child
->CaptureMouse();
396 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
398 // ----------------------------------------------------------------------------
399 // wxPopupComboWindow
400 // ----------------------------------------------------------------------------
402 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
403 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
406 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
407 : wxPopupTransientWindow(parent
)
412 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
416 return wxPopupWindow::Create(parent
);
419 void wxPopupComboWindow::PositionNearCombo()
421 // the origin point must be in screen coords
422 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0,0));
424 #if 0 //def __WXUNIVERSAL__
425 // account for the fact that (0, 0) is not the top left corner of the
426 // window: there is also the border
427 wxRect rectBorders
= m_combo
->GetRenderer()->
428 GetBorderDimensions(m_combo
->GetBorder());
429 ptOrigin
.x
-= rectBorders
.x
;
430 ptOrigin
.y
-= rectBorders
.y
;
431 #endif // __WXUNIVERSAL__
433 // position below or above the combobox: the width is 0 to put it exactly
434 // below us, not to the left or to the right
435 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
438 void wxPopupComboWindow::OnDismiss()
440 m_combo
->OnDismiss();
443 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
445 m_combo
->ProcessEvent(event
);
448 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
450 // ----------------------------------------------------------------------------
451 // wxPopupWindowHandler
452 // ----------------------------------------------------------------------------
454 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
456 // let the window have it first (we're the first event handler in the chain
457 // of handlers for this window)
458 if ( m_popup
->ProcessLeftDown(event
) )
463 wxPoint pos
= event
.GetPosition();
465 // scrollbar on which the click occurred
466 wxWindow
*sbar
= NULL
;
468 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
470 switch ( win
->HitTest(pos
.x
, pos
.y
) )
472 case wxHT_WINDOW_OUTSIDE
:
474 // do the coords translation now as after DismissAndNotify()
475 // m_popup may be destroyed
476 wxMouseEvent
event2(event
);
478 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
480 // clicking outside a popup dismisses it
481 m_popup
->DismissAndNotify();
483 // dismissing a tooltip shouldn't waste a click, i.e. you
484 // should be able to dismiss it and press the button with the
485 // same click, so repost this event to the window beneath us
486 wxWindow
*win
= wxFindWindowAtPoint(event2
.GetPosition());
489 // translate the event coords to the ones of the window
490 // which is going to get the event
491 win
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
493 event2
.SetEventObject(win
);
494 wxPostEvent(win
, event2
);
499 #ifdef __WXUNIVERSAL__
500 case wxHT_WINDOW_HORZ_SCROLLBAR
:
501 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
504 case wxHT_WINDOW_VERT_SCROLLBAR
:
505 sbar
= win
->GetScrollbar(wxVERTICAL
);
510 // forgot to update the switch after adding a new hit test code?
511 wxFAIL_MSG( _T("unexpected HitTest() return value") );
514 case wxHT_WINDOW_CORNER
:
515 // don't actually know if this one is good for anything, but let it
518 case wxHT_WINDOW_INSIDE
:
519 // let the normal processing take place
526 // translate the event coordinates to the scrollbar ones
527 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
529 // and give the event to it
530 wxMouseEvent event2
= event
;
534 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
538 // ----------------------------------------------------------------------------
539 // wxPopupFocusHandler
540 // ----------------------------------------------------------------------------
542 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
544 // when we lose focus we always disappear - unless it goes to the popup (in
545 // which case we don't really lose it)
546 wxWindow
*win
= event
.GetWindow();
549 if ( win
== m_popup
)
551 win
= win
->GetParent();
554 m_popup
->DismissAndNotify();
557 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
559 // let the window have it first, it might process the keys
560 if ( !m_popup
->ProcessEvent(event
) )
562 // by default, dismiss the popup
563 m_popup
->DismissAndNotify();
567 #endif // wxUSE_POPUPWIN