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()
202 delete m_handlerFocus
;
203 delete m_handlerPopup
;
206 void wxPopupTransientWindow::PopHandlers()
210 if ( !m_child
->RemoveEventHandler(m_handlerPopup
) )
212 // something is very wrong and someone else probably deleted our
213 // handler - so don't risk deleting it second time
214 m_handlerPopup
= NULL
;
217 m_child
->ReleaseMouse();
224 if ( !m_focus
->RemoveEventHandler(m_handlerFocus
) )
227 m_handlerFocus
= NULL
;
231 if ( !RemoveEventHandler(m_handlerFocus
) )
234 m_handlerFocus
= NULL
;
240 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
242 const wxWindowList
& children
= GetChildren();
243 if ( children
.GetCount() )
245 m_child
= children
.GetFirst()->GetData();
254 delete m_handlerPopup
;
255 m_handlerPopup
= new wxPopupWindowHandler(this);
257 m_child
->CaptureMouse();
258 m_child
->PushEventHandler(m_handlerPopup
);
260 m_focus
= winFocus
? winFocus
: this;
264 // MSW doesn't allow to set focus to the popup window, but we need to
265 // subclass the window which has the focus, and not winFocus passed in or
266 // otherwise everything else breaks down
267 m_focus
= FindFocus();
270 delete m_handlerFocus
;
271 m_handlerFocus
= new wxPopupFocusHandler(this);
273 m_focus
->PushEventHandler(m_handlerFocus
);
276 // GTK+ catches the activate events from the popup
277 // window, not the focus events from the child window
278 delete m_handlerFocus
;
279 m_handlerFocus
= new wxPopupFocusHandler(this);
280 PushEventHandler(m_handlerFocus
);
285 bool wxPopupTransientWindow::Show( bool show
)
289 gtk_grab_remove( m_widget
);
292 bool ret
= wxPopupWindow::Show( show
);
296 gtk_grab_add( m_widget
);
302 void wxPopupTransientWindow::Dismiss()
309 void wxPopupTransientWindow::DismissAndNotify()
316 void wxPopupTransientWindow::OnDismiss()
318 // nothing to do here - but it may be interesting for derived class
321 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
323 // no special processing here
327 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
329 // ----------------------------------------------------------------------------
330 // wxPopupComboWindow
331 // ----------------------------------------------------------------------------
333 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
334 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
337 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
338 : wxPopupTransientWindow(parent
)
343 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
347 return wxPopupWindow::Create(parent
);
350 void wxPopupComboWindow::PositionNearCombo()
352 // the origin point must be in screen coords
353 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0, 0));
355 #if 0 //def __WXUNIVERSAL__
356 // account for the fact that (0, 0) is not the top left corner of the
357 // window: there is also the border
358 wxRect rectBorders
= m_combo
->GetRenderer()->
359 GetBorderDimensions(m_combo
->GetBorder());
360 ptOrigin
.x
-= rectBorders
.x
;
361 ptOrigin
.y
-= rectBorders
.y
;
362 #endif // __WXUNIVERSAL__
364 // position below or above the combobox: the width is 0 to put it exactly
365 // below us, not to the left or to the right
366 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
369 void wxPopupComboWindow::OnDismiss()
371 m_combo
->OnDismiss();
374 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
376 m_combo
->ProcessEvent(event
);
379 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
381 // ----------------------------------------------------------------------------
382 // wxPopupWindowHandler
383 // ----------------------------------------------------------------------------
385 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
387 // let the window have it first (we're the first event handler in the chain
388 // of handlers for this window)
389 if ( m_popup
->ProcessLeftDown(event
) )
394 wxPoint pos
= event
.GetPosition();
396 // scrollbar on which the click occured
397 wxWindow
*sbar
= NULL
;
399 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
401 switch ( win
->HitTest(pos
.x
, pos
.y
) )
403 case wxHT_WINDOW_OUTSIDE
:
405 // do the coords translation now as after DismissAndNotify()
406 // m_popup may be destroyed
407 wxMouseEvent
event2(event
);
409 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
411 // clicking outside a popup dismisses it
412 m_popup
->DismissAndNotify();
414 // dismissing a tooltip shouldn't waste a click, i.e. you
415 // should be able to dismiss it and press the button with the
416 // same click, so repost this event to the window beneath us
417 wxWindow
*win
= wxFindWindowAtPoint(event2
.GetPosition());
420 // translate the event coords to the ones of the window
421 // which is going to get the event
422 win
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
424 event2
.SetEventObject(win
);
425 wxPostEvent(win
, event2
);
430 #ifdef __WXUNIVERSAL__
431 case wxHT_WINDOW_HORZ_SCROLLBAR
:
432 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
435 case wxHT_WINDOW_VERT_SCROLLBAR
:
436 sbar
= win
->GetScrollbar(wxVERTICAL
);
441 // forgot to update the switch after adding a new hit test code?
442 wxFAIL_MSG( _T("unexpected HitTest() return value") );
445 case wxHT_WINDOW_CORNER
:
446 // don't actually know if this one is good for anything, but let it
449 case wxHT_WINDOW_INSIDE
:
450 // let the normal processing take place
457 // translate the event coordinates to the scrollbar ones
458 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
460 // and give the event to it
461 wxMouseEvent event2
= event
;
465 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
469 // ----------------------------------------------------------------------------
470 // wxPopupFocusHandler
471 // ----------------------------------------------------------------------------
474 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
476 // when we lose focus we always disappear - unless it goes to the popup (in
477 // which case we don't really lose it)
478 wxWindow
*win
= event
.GetWindow();
481 if ( win
== m_popup
)
483 win
= win
->GetParent();
486 m_popup
->DismissAndNotify();
489 void wxPopupFocusHandler::OnActivate(wxActivateEvent
&event
)
491 if (event
.GetActive())
492 m_popup
->DismissAndNotify();
496 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
498 // let the window have it first, it might process the keys
499 if ( !m_popup
->ProcessEvent(event
) )
501 // by default, dismiss the popup
502 m_popup
->DismissAndNotify();
506 #endif // wxUSE_POPUPWIN