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 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow
, wxWindow
)
51 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow
, wxPopupWindow
)
53 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
54 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow
, wxPopupTransientWindow
)
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // event handlers which we use to intercept events which cause the popup to
63 class wxPopupWindowHandler
: public wxEvtHandler
66 wxPopupWindowHandler(wxPopupTransientWindow
*popup
) { m_popup
= popup
; }
70 void OnLeftDown(wxMouseEvent
& event
);
73 wxPopupTransientWindow
*m_popup
;
76 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler
)
79 class wxPopupFocusHandler
: public wxEvtHandler
82 wxPopupFocusHandler(wxPopupTransientWindow
*popup
)
90 // Under MSW, we catch the kill focus event
91 void OnKillFocus(wxFocusEvent
& event
);
93 // Under GTK+, event a transient popup window
94 // is a toplevel window so we need to catch
96 void OnActivate(wxActivateEvent
&event
);
98 void OnKeyDown(wxKeyEvent
& event
);
101 wxPopupTransientWindow
*m_popup
;
103 DECLARE_EVENT_TABLE()
104 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler
)
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
112 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
115 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
117 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
119 EVT_ACTIVATE(wxPopupFocusHandler::OnActivate
)
121 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown
)
124 // ============================================================================
126 // ============================================================================
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
132 wxPopupWindowBase::~wxPopupWindowBase()
134 // this destructor is required for Darwin
137 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
142 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
145 wxSize sizeScreen
= wxGetDisplaySize(),
146 sizeSelf
= GetSize();
148 // is there enough space to put the popup below the window (where we put it
150 wxCoord y
= ptOrigin
.y
+ size
.y
;
151 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
153 // check if there is enough space above
154 if ( ptOrigin
.y
> sizeSelf
.y
)
156 // do position the control above the window
157 y
-= size
.y
+ sizeSelf
.y
;
159 //else: not enough space below nor above, leave below
162 // now check left/right too
163 wxCoord x
= ptOrigin
.x
+ size
.x
;
164 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
166 // check if there is enough space to the left
167 if ( ptOrigin
.x
> sizeSelf
.x
)
169 // do position the control to the left
170 x
-= size
.x
+ sizeSelf
.x
;
172 //else: not enough space there neither, leave in default position
175 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
178 // ----------------------------------------------------------------------------
179 // wxPopupTransientWindow
180 // ----------------------------------------------------------------------------
182 void wxPopupTransientWindow::Init()
185 m_focus
= (wxWindow
*)NULL
;
187 m_handlerFocus
= NULL
;
188 m_handlerPopup
= NULL
;
191 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
195 (void)Create(parent
, style
);
198 wxPopupTransientWindow::~wxPopupTransientWindow()
203 void wxPopupTransientWindow::PopHandlers()
207 if ( m_handlerPopup
&& !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
;
214 m_child
->ReleaseMouse();
221 if ( m_handlerFocus
&& !m_focus
->RemoveEventHandler(m_handlerFocus
) )
224 m_handlerFocus
= NULL
;
228 if ( m_handlerFocus
&& !RemoveEventHandler(m_handlerFocus
) )
231 m_handlerFocus
= NULL
;
235 // delete the handlers, they'll be created as necessary in Popup()
236 delete m_handlerPopup
;
237 m_handlerPopup
= NULL
;
238 delete m_handlerFocus
;
239 m_handlerFocus
= NULL
;
244 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
246 const wxWindowList
& children
= GetChildren();
247 if ( children
.GetCount() )
249 m_child
= children
.GetFirst()->GetData();
258 delete m_handlerPopup
;
259 m_handlerPopup
= new wxPopupWindowHandler(this);
261 m_child
->CaptureMouse();
262 m_child
->PushEventHandler(m_handlerPopup
);
264 m_focus
= winFocus
? winFocus
: this;
268 // MSW doesn't allow to set focus to the popup window, but we need to
269 // subclass the window which has the focus, and not winFocus passed in or
270 // otherwise everything else breaks down
271 m_focus
= FindFocus();
274 delete m_handlerFocus
;
275 m_handlerFocus
= new wxPopupFocusHandler(this);
277 m_focus
->PushEventHandler(m_handlerFocus
);
280 // GTK+ catches the activate events from the popup
281 // window, not the focus events from the child window
282 delete m_handlerFocus
;
283 m_handlerFocus
= new wxPopupFocusHandler(this);
284 PushEventHandler(m_handlerFocus
);
289 bool wxPopupTransientWindow::Show( bool show
)
293 gtk_grab_remove( m_widget
);
296 bool ret
= wxPopupWindow::Show( show
);
300 gtk_grab_add( m_widget
);
306 void wxPopupTransientWindow::Dismiss()
313 void wxPopupTransientWindow::DismissAndNotify()
320 void wxPopupTransientWindow::OnDismiss()
322 // nothing to do here - but it may be interesting for derived class
325 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
327 // no special processing here
331 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
333 // ----------------------------------------------------------------------------
334 // wxPopupComboWindow
335 // ----------------------------------------------------------------------------
337 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
338 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
341 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
342 : wxPopupTransientWindow(parent
)
347 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
351 return wxPopupWindow::Create(parent
);
354 void wxPopupComboWindow::PositionNearCombo()
356 // the origin point must be in screen coords
357 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0, 0));
359 #if 0 //def __WXUNIVERSAL__
360 // account for the fact that (0, 0) is not the top left corner of the
361 // window: there is also the border
362 wxRect rectBorders
= m_combo
->GetRenderer()->
363 GetBorderDimensions(m_combo
->GetBorder());
364 ptOrigin
.x
-= rectBorders
.x
;
365 ptOrigin
.y
-= rectBorders
.y
;
366 #endif // __WXUNIVERSAL__
368 // position below or above the combobox: the width is 0 to put it exactly
369 // below us, not to the left or to the right
370 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
373 void wxPopupComboWindow::OnDismiss()
375 m_combo
->OnDismiss();
378 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
380 m_combo
->ProcessEvent(event
);
383 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
385 // ----------------------------------------------------------------------------
386 // wxPopupWindowHandler
387 // ----------------------------------------------------------------------------
389 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
391 // let the window have it first (we're the first event handler in the chain
392 // of handlers for this window)
393 if ( m_popup
->ProcessLeftDown(event
) )
398 wxPoint pos
= event
.GetPosition();
400 // scrollbar on which the click occured
401 wxWindow
*sbar
= NULL
;
403 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
405 switch ( win
->HitTest(pos
.x
, pos
.y
) )
407 case wxHT_WINDOW_OUTSIDE
:
409 // do the coords translation now as after DismissAndNotify()
410 // m_popup may be destroyed
411 wxMouseEvent
event2(event
);
413 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
415 // clicking outside a popup dismisses it
416 m_popup
->DismissAndNotify();
418 // dismissing a tooltip shouldn't waste a click, i.e. you
419 // should be able to dismiss it and press the button with the
420 // same click, so repost this event to the window beneath us
421 wxWindow
*win
= wxFindWindowAtPoint(event2
.GetPosition());
424 // translate the event coords to the ones of the window
425 // which is going to get the event
426 win
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
428 event2
.SetEventObject(win
);
429 wxPostEvent(win
, event2
);
434 #ifdef __WXUNIVERSAL__
435 case wxHT_WINDOW_HORZ_SCROLLBAR
:
436 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
439 case wxHT_WINDOW_VERT_SCROLLBAR
:
440 sbar
= win
->GetScrollbar(wxVERTICAL
);
445 // forgot to update the switch after adding a new hit test code?
446 wxFAIL_MSG( _T("unexpected HitTest() return value") );
449 case wxHT_WINDOW_CORNER
:
450 // don't actually know if this one is good for anything, but let it
453 case wxHT_WINDOW_INSIDE
:
454 // let the normal processing take place
461 // translate the event coordinates to the scrollbar ones
462 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
464 // and give the event to it
465 wxMouseEvent event2
= event
;
469 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
473 // ----------------------------------------------------------------------------
474 // wxPopupFocusHandler
475 // ----------------------------------------------------------------------------
478 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
480 // when we lose focus we always disappear - unless it goes to the popup (in
481 // which case we don't really lose it)
482 wxWindow
*win
= event
.GetWindow();
485 if ( win
== m_popup
)
487 win
= win
->GetParent();
490 m_popup
->DismissAndNotify();
493 void wxPopupFocusHandler::OnActivate(wxActivateEvent
&event
)
495 if (event
.GetActive())
496 m_popup
->DismissAndNotify();
500 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
502 // let the window have it first, it might process the keys
503 if ( !m_popup
->ProcessEvent(event
) )
505 // by default, dismiss the popup
506 m_popup
->DismissAndNotify();
510 #endif // wxUSE_POPUPWIN