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 license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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
41 #ifdef __WXUNIVERSAL__
42 #include "wx/univ/renderer.h"
43 #endif // __WXUNIVERSAL__
45 // there is no src/{msw,mgl}/popupwin.cpp to put this in, so we do it here - BTW we
46 // probably could do it for all ports here just as well
47 #if defined(__WXMSW__) || defined(__WXMGL__)
48 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
;
78 class wxPopupFocusHandler
: public wxEvtHandler
81 wxPopupFocusHandler(wxPopupTransientWindow
*popup
)
86 // ignore the next few OnKillFocus() calls
87 m_creationTime
= time(NULL
);
93 void OnKillFocus(wxFocusEvent
& event
);
94 void OnKeyDown(wxKeyEvent
& event
);
97 wxPopupTransientWindow
*m_popup
;
99 // hack around wxGTK bug: we always get several kill focus events
100 // immediately after creation!
102 time_t m_creationTime
;
105 DECLARE_EVENT_TABLE()
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
113 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
116 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
117 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
118 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown
)
121 // ============================================================================
123 // ============================================================================
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 wxPopupWindowBase::~wxPopupWindowBase()
131 // this destructor is required for Darwin
134 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
139 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
142 wxSize sizeScreen
= wxGetDisplaySize(),
143 sizeSelf
= GetSize();
145 // is there enough space to put the popup below the window (where we put it
147 wxCoord y
= ptOrigin
.y
+ size
.y
;
148 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
150 // check if there is enough space above
151 if ( ptOrigin
.y
> sizeSelf
.y
)
153 // do position the control above the window
154 y
-= size
.y
+ sizeSelf
.y
;
156 //else: not enough space below nor above, leave below
159 // now check left/right too
160 wxCoord x
= ptOrigin
.x
+ size
.x
;
161 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
163 // check if there is enough space to the left
164 if ( ptOrigin
.x
> sizeSelf
.x
)
166 // do position the control to the left
167 x
-= size
.x
+ sizeSelf
.x
;
169 //else: not enough space there neither, leave in default position
172 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
175 // ----------------------------------------------------------------------------
176 // wxPopupTransientWindow
177 // ----------------------------------------------------------------------------
179 void wxPopupTransientWindow::Init()
182 m_focus
= (wxWindow
*)NULL
;
184 m_handlerFocus
= NULL
;
185 m_handlerPopup
= NULL
;
188 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
192 (void)Create(parent
, style
);
195 wxPopupTransientWindow::~wxPopupTransientWindow()
199 delete m_handlerFocus
;
200 delete m_handlerPopup
;
203 void wxPopupTransientWindow::PopHandlers()
207 if ( !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_focus
->RemoveEventHandler(m_handlerFocus
) )
224 m_handlerFocus
= NULL
;
231 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
233 const wxWindowList
& children
= GetChildren();
234 if ( children
.GetCount() )
236 m_child
= children
.GetFirst()->GetData();
243 // we can't capture mouse before the window is shown in wxGTK, so do it
247 delete m_handlerPopup
;
248 m_handlerPopup
= new wxPopupWindowHandler(this);
250 m_child
->CaptureMouse();
251 m_child
->PushEventHandler(m_handlerPopup
);
253 m_focus
= winFocus
? winFocus
: this;
259 // MSW doesn't allow to set focus to the popup window, but we need to
260 // subclass the window which has the focus, and not winFocus passed in or
261 // otherwise everything else breaks down
262 m_focus
= FindFocus();
266 delete m_handlerFocus
;
267 m_handlerFocus
= new wxPopupFocusHandler(this);
269 m_focus
->PushEventHandler(m_handlerFocus
);
275 void wxPopupTransientWindow::Dismiss()
282 void wxPopupTransientWindow::DismissAndNotify()
289 void wxPopupTransientWindow::OnDismiss()
291 // nothing to do here - but it may be interesting for derived class
294 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
296 // no special processing here
300 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
302 // ----------------------------------------------------------------------------
303 // wxPopupComboWindow
304 // ----------------------------------------------------------------------------
306 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
307 : wxPopupTransientWindow(parent
)
312 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
316 return wxPopupWindow::Create(parent
);
319 void wxPopupComboWindow::PositionNearCombo()
321 // the origin point must be in screen coords
322 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0, 0));
324 #if 0 //def __WXUNIVERSAL__
325 // account for the fact that (0, 0) is not the top left corner of the
326 // window: there is also the border
327 wxRect rectBorders
= m_combo
->GetRenderer()->
328 GetBorderDimensions(m_combo
->GetBorder());
329 ptOrigin
.x
-= rectBorders
.x
;
330 ptOrigin
.y
-= rectBorders
.y
;
331 #endif // __WXUNIVERSAL__
333 // position below or above the combobox: the width is 0 to put it exactly
334 // below us, not to the left or to the right
335 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
338 void wxPopupComboWindow::OnDismiss()
340 m_combo
->OnDismiss();
343 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
345 // ----------------------------------------------------------------------------
346 // wxPopupWindowHandler
347 // ----------------------------------------------------------------------------
349 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
351 // let the window have it first (we're the first event handler in the chain
352 // of handlers for this window)
353 if ( m_popup
->ProcessLeftDown(event
) )
358 wxPoint pos
= event
.GetPosition();
360 // scrollbar on which the click occured
361 wxWindow
*sbar
= NULL
;
363 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
365 switch ( win
->HitTest(pos
.x
, pos
.y
) )
367 case wxHT_WINDOW_OUTSIDE
:
369 // do the coords translation now as after DismissAndNotify()
370 // m_popup may be destroyed
371 wxMouseEvent
event2(event
);
373 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
375 // clicking outside a popup dismisses it
376 m_popup
->DismissAndNotify();
378 // dismissing a tooltip shouldn't waste a click, i.e. you
379 // should be able to dismiss it and press the button with the
380 // same click, so repost this event to the window beneath us
381 wxWindow
*win
= wxFindWindowAtPoint(event2
.GetPosition());
384 // translate the event coords to the ones of the window
385 // which is going to get the event
386 win
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
388 event2
.SetEventObject(win
);
389 wxPostEvent(win
, event2
);
394 #ifdef __WXUNIVERSAL__
395 case wxHT_WINDOW_HORZ_SCROLLBAR
:
396 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
399 case wxHT_WINDOW_VERT_SCROLLBAR
:
400 sbar
= win
->GetScrollbar(wxVERTICAL
);
405 // forgot to update the switch after adding a new hit test code?
406 wxFAIL_MSG( _T("unexpected HitTest() return value") );
409 case wxHT_WINDOW_CORNER
:
410 // don't actually know if this one is good for anything, but let it
413 case wxHT_WINDOW_INSIDE
:
414 // let the normal processing take place
421 // translate the event coordinates to the scrollbar ones
422 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
424 // and give the event to it
425 wxMouseEvent event2
= event
;
429 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
433 // ----------------------------------------------------------------------------
434 // wxPopupFocusHandler
435 // ----------------------------------------------------------------------------
437 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
440 // ignore the next OnKillFocus() call
441 if ( time(NULL
) < m_creationTime
+ 1 )
449 // when we lose focus we always disappear - unless it goes to the popup (in
450 // which case we don't really lose it)
451 wxWindow
*win
= event
.GetWindow();
454 if ( win
== m_popup
)
456 win
= win
->GetParent();
459 m_popup
->DismissAndNotify();
462 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
464 // let the window have it first, it might process the keys
465 if ( !m_popup
->ProcessEvent(event
) )
467 // by default, dismiss the popup
468 m_popup
->DismissAndNotify();
472 #endif // wxUSE_POPUPWIN