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 // in non-Univ ports the system manages scrollbars for us
466 #ifdef __WXUNIVERSAL__
467 // scrollbar on which the click occurred
468 wxWindow
*sbar
= NULL
;
469 #endif // __WXUNIVERSAL__
471 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
473 switch ( win
->HitTest(pos
.x
, pos
.y
) )
475 case wxHT_WINDOW_OUTSIDE
:
477 // do the coords translation now as after DismissAndNotify()
478 // m_popup may be destroyed
479 wxMouseEvent
event2(event
);
481 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
483 // clicking outside a popup dismisses it
484 m_popup
->DismissAndNotify();
486 // dismissing a tooltip shouldn't waste a click, i.e. you
487 // should be able to dismiss it and press the button with the
488 // same click, so repost this event to the window beneath us
489 wxWindow
*winUnder
= wxFindWindowAtPoint(event2
.GetPosition());
492 // translate the event coords to the ones of the window
493 // which is going to get the event
494 winUnder
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
496 event2
.SetEventObject(winUnder
);
497 wxPostEvent(winUnder
, event2
);
502 #ifdef __WXUNIVERSAL__
503 case wxHT_WINDOW_HORZ_SCROLLBAR
:
504 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
507 case wxHT_WINDOW_VERT_SCROLLBAR
:
508 sbar
= win
->GetScrollbar(wxVERTICAL
);
510 #endif // __WXUNIVERSAL__
513 // forgot to update the switch after adding a new hit test code?
514 wxFAIL_MSG( _T("unexpected HitTest() return value") );
517 case wxHT_WINDOW_CORNER
:
518 // don't actually know if this one is good for anything, but let it
521 case wxHT_WINDOW_INSIDE
:
522 // let the normal processing take place
527 #ifdef __WXUNIVERSAL__
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
);
540 #endif // __WXUNIVERSAL__
543 // ----------------------------------------------------------------------------
544 // wxPopupFocusHandler
545 // ----------------------------------------------------------------------------
547 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
549 // when we lose focus we always disappear - unless it goes to the popup (in
550 // which case we don't really lose it)
551 wxWindow
*win
= event
.GetWindow();
554 if ( win
== m_popup
)
556 win
= win
->GetParent();
559 m_popup
->DismissAndNotify();
562 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
564 // let the window have it first, it might process the keys
565 if ( !m_popup
->ProcessEvent(event
) )
567 // by default, dismiss the popup
568 m_popup
->DismissAndNotify();
572 #endif // wxUSE_POPUPWIN