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"
31 #if wxUSE_POPUPWIN && !defined(__WXMOTIF__)
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__
46 // there is no src/mgl/popupwin.cpp to put this in, so we do it here - BTW we
47 // probably could do it for all ports here just as well
48 #if defined(__WXMGL__)
49 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow
, wxWindow
)
52 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow
, wxPopupWindow
)
54 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
55 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow
, wxPopupTransientWindow
)
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // event handlers which we use to intercept events which cause the popup to
64 class wxPopupWindowHandler
: public wxEvtHandler
67 wxPopupWindowHandler(wxPopupTransientWindow
*popup
) { m_popup
= popup
; }
71 void OnLeftDown(wxMouseEvent
& event
);
74 wxPopupTransientWindow
*m_popup
;
77 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler
)
80 class wxPopupFocusHandler
: public wxEvtHandler
83 wxPopupFocusHandler(wxPopupTransientWindow
*popup
)
88 // ignore the next few OnKillFocus() calls
89 m_creationTime
= time(NULL
);
95 void OnKillFocus(wxFocusEvent
& event
);
96 void OnKeyDown(wxKeyEvent
& event
);
99 wxPopupTransientWindow
*m_popup
;
101 // hack around wxGTK bug: we always get several kill focus events
102 // immediately after creation!
104 time_t m_creationTime
;
107 DECLARE_EVENT_TABLE()
108 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler
)
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
116 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
119 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
120 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
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
;
234 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
236 const wxWindowList
& children
= GetChildren();
237 if ( children
.GetCount() )
239 m_child
= children
.GetFirst()->GetData();
246 // we can't capture mouse before the window is shown in wxGTK, so do it
250 delete m_handlerPopup
;
251 m_handlerPopup
= new wxPopupWindowHandler(this);
253 m_child
->CaptureMouse();
254 m_child
->PushEventHandler(m_handlerPopup
);
256 m_focus
= winFocus
? winFocus
: this;
262 // MSW doesn't allow to set focus to the popup window, but we need to
263 // subclass the window which has the focus, and not winFocus passed in or
264 // otherwise everything else breaks down
265 m_focus
= FindFocus();
269 delete m_handlerFocus
;
270 m_handlerFocus
= new wxPopupFocusHandler(this);
272 m_focus
->PushEventHandler(m_handlerFocus
);
278 void wxPopupTransientWindow::Dismiss()
285 void wxPopupTransientWindow::DismissAndNotify()
292 void wxPopupTransientWindow::OnDismiss()
294 // nothing to do here - but it may be interesting for derived class
297 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
299 // no special processing here
303 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
305 // ----------------------------------------------------------------------------
306 // wxPopupComboWindow
307 // ----------------------------------------------------------------------------
309 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
310 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
313 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
314 : wxPopupTransientWindow(parent
)
319 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
323 return wxPopupWindow::Create(parent
);
326 void wxPopupComboWindow::PositionNearCombo()
328 // the origin point must be in screen coords
329 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0, 0));
331 #if 0 //def __WXUNIVERSAL__
332 // account for the fact that (0, 0) is not the top left corner of the
333 // window: there is also the border
334 wxRect rectBorders
= m_combo
->GetRenderer()->
335 GetBorderDimensions(m_combo
->GetBorder());
336 ptOrigin
.x
-= rectBorders
.x
;
337 ptOrigin
.y
-= rectBorders
.y
;
338 #endif // __WXUNIVERSAL__
340 // position below or above the combobox: the width is 0 to put it exactly
341 // below us, not to the left or to the right
342 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
345 void wxPopupComboWindow::OnDismiss()
347 m_combo
->OnDismiss();
350 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
352 m_combo
->ProcessEvent(event
);
355 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
357 // ----------------------------------------------------------------------------
358 // wxPopupWindowHandler
359 // ----------------------------------------------------------------------------
361 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
363 // let the window have it first (we're the first event handler in the chain
364 // of handlers for this window)
365 if ( m_popup
->ProcessLeftDown(event
) )
370 wxPoint pos
= event
.GetPosition();
372 // scrollbar on which the click occured
373 wxWindow
*sbar
= NULL
;
375 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
377 switch ( win
->HitTest(pos
.x
, pos
.y
) )
379 case wxHT_WINDOW_OUTSIDE
:
381 // do the coords translation now as after DismissAndNotify()
382 // m_popup may be destroyed
383 wxMouseEvent
event2(event
);
385 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
387 // clicking outside a popup dismisses it
388 m_popup
->DismissAndNotify();
390 // dismissing a tooltip shouldn't waste a click, i.e. you
391 // should be able to dismiss it and press the button with the
392 // same click, so repost this event to the window beneath us
393 wxWindow
*win
= wxFindWindowAtPoint(event2
.GetPosition());
396 // translate the event coords to the ones of the window
397 // which is going to get the event
398 win
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
400 event2
.SetEventObject(win
);
401 wxPostEvent(win
, event2
);
406 #ifdef __WXUNIVERSAL__
407 case wxHT_WINDOW_HORZ_SCROLLBAR
:
408 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
411 case wxHT_WINDOW_VERT_SCROLLBAR
:
412 sbar
= win
->GetScrollbar(wxVERTICAL
);
417 // forgot to update the switch after adding a new hit test code?
418 wxFAIL_MSG( _T("unexpected HitTest() return value") );
421 case wxHT_WINDOW_CORNER
:
422 // don't actually know if this one is good for anything, but let it
425 case wxHT_WINDOW_INSIDE
:
426 // let the normal processing take place
433 // translate the event coordinates to the scrollbar ones
434 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
436 // and give the event to it
437 wxMouseEvent event2
= event
;
441 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
445 // ----------------------------------------------------------------------------
446 // wxPopupFocusHandler
447 // ----------------------------------------------------------------------------
449 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
452 // ignore the next OnKillFocus() call
453 if ( time(NULL
) < m_creationTime
+ 1 )
461 // when we lose focus we always disappear - unless it goes to the popup (in
462 // which case we don't really lose it)
463 wxWindow
*win
= event
.GetWindow();
466 if ( win
== m_popup
)
468 win
= win
->GetParent();
471 m_popup
->DismissAndNotify();
474 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
476 // let the window have it first, it might process the keys
477 if ( !m_popup
->ProcessEvent(event
) )
479 // by default, dismiss the popup
480 m_popup
->DismissAndNotify();
484 #endif // wxUSE_POPUPWIN