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
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__) || defined(__WXPM__)
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
;
79 class wxPopupFocusHandler
: public wxEvtHandler
82 wxPopupFocusHandler(wxPopupTransientWindow
*popup
)
87 // ignore the next few OnKillFocus() calls
88 m_creationTime
= time(NULL
);
94 void OnKillFocus(wxFocusEvent
& event
);
95 void OnKeyDown(wxKeyEvent
& event
);
98 wxPopupTransientWindow
*m_popup
;
100 // hack around wxGTK bug: we always get several kill focus events
101 // immediately after creation!
103 time_t m_creationTime
;
106 DECLARE_EVENT_TABLE()
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
114 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
117 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
118 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
119 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown
)
122 // ============================================================================
124 // ============================================================================
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 wxPopupWindowBase::~wxPopupWindowBase()
132 // this destructor is required for Darwin
135 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
140 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
143 wxSize sizeScreen
= wxGetDisplaySize(),
144 sizeSelf
= GetSize();
146 // is there enough space to put the popup below the window (where we put it
148 wxCoord y
= ptOrigin
.y
+ size
.y
;
149 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
151 // check if there is enough space above
152 if ( ptOrigin
.y
> sizeSelf
.y
)
154 // do position the control above the window
155 y
-= size
.y
+ sizeSelf
.y
;
157 //else: not enough space below nor above, leave below
160 // now check left/right too
161 wxCoord x
= ptOrigin
.x
+ size
.x
;
162 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
164 // check if there is enough space to the left
165 if ( ptOrigin
.x
> sizeSelf
.x
)
167 // do position the control to the left
168 x
-= size
.x
+ sizeSelf
.x
;
170 //else: not enough space there neither, leave in default position
173 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
176 // ----------------------------------------------------------------------------
177 // wxPopupTransientWindow
178 // ----------------------------------------------------------------------------
180 void wxPopupTransientWindow::Init()
183 m_focus
= (wxWindow
*)NULL
;
185 m_handlerFocus
= NULL
;
186 m_handlerPopup
= NULL
;
189 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
193 (void)Create(parent
, style
);
196 wxPopupTransientWindow::~wxPopupTransientWindow()
200 delete m_handlerFocus
;
201 delete m_handlerPopup
;
204 void wxPopupTransientWindow::PopHandlers()
208 if ( !m_child
->RemoveEventHandler(m_handlerPopup
) )
210 // something is very wrong and someone else probably deleted our
211 // handler - so don't risk deleting it second time
212 m_handlerPopup
= NULL
;
215 m_child
->ReleaseMouse();
222 if ( !m_focus
->RemoveEventHandler(m_handlerFocus
) )
225 m_handlerFocus
= NULL
;
232 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
234 const wxWindowList
& children
= GetChildren();
235 if ( children
.GetCount() )
237 m_child
= children
.GetFirst()->GetData();
244 // we can't capture mouse before the window is shown in wxGTK, so do it
248 delete m_handlerPopup
;
249 m_handlerPopup
= new wxPopupWindowHandler(this);
251 m_child
->CaptureMouse();
252 m_child
->PushEventHandler(m_handlerPopup
);
254 m_focus
= winFocus
? winFocus
: this;
260 // MSW doesn't allow to set focus to the popup window, but we need to
261 // subclass the window which has the focus, and not winFocus passed in or
262 // otherwise everything else breaks down
263 m_focus
= FindFocus();
267 delete m_handlerFocus
;
268 m_handlerFocus
= new wxPopupFocusHandler(this);
270 m_focus
->PushEventHandler(m_handlerFocus
);
276 void wxPopupTransientWindow::Dismiss()
283 void wxPopupTransientWindow::DismissAndNotify()
290 void wxPopupTransientWindow::OnDismiss()
292 // nothing to do here - but it may be interesting for derived class
295 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
297 // no special processing here
301 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
303 // ----------------------------------------------------------------------------
304 // wxPopupComboWindow
305 // ----------------------------------------------------------------------------
307 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
308 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
311 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
312 : wxPopupTransientWindow(parent
)
317 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
321 return wxPopupWindow::Create(parent
);
324 void wxPopupComboWindow::PositionNearCombo()
326 // the origin point must be in screen coords
327 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0, 0));
329 #if 0 //def __WXUNIVERSAL__
330 // account for the fact that (0, 0) is not the top left corner of the
331 // window: there is also the border
332 wxRect rectBorders
= m_combo
->GetRenderer()->
333 GetBorderDimensions(m_combo
->GetBorder());
334 ptOrigin
.x
-= rectBorders
.x
;
335 ptOrigin
.y
-= rectBorders
.y
;
336 #endif // __WXUNIVERSAL__
338 // position below or above the combobox: the width is 0 to put it exactly
339 // below us, not to the left or to the right
340 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
343 void wxPopupComboWindow::OnDismiss()
345 m_combo
->OnDismiss();
348 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
350 m_combo
->ProcessEvent(event
);
353 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
355 // ----------------------------------------------------------------------------
356 // wxPopupWindowHandler
357 // ----------------------------------------------------------------------------
359 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
361 // let the window have it first (we're the first event handler in the chain
362 // of handlers for this window)
363 if ( m_popup
->ProcessLeftDown(event
) )
368 wxPoint pos
= event
.GetPosition();
370 // scrollbar on which the click occured
371 wxWindow
*sbar
= NULL
;
373 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
375 switch ( win
->HitTest(pos
.x
, pos
.y
) )
377 case wxHT_WINDOW_OUTSIDE
:
379 // do the coords translation now as after DismissAndNotify()
380 // m_popup may be destroyed
381 wxMouseEvent
event2(event
);
383 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
385 // clicking outside a popup dismisses it
386 m_popup
->DismissAndNotify();
388 // dismissing a tooltip shouldn't waste a click, i.e. you
389 // should be able to dismiss it and press the button with the
390 // same click, so repost this event to the window beneath us
391 wxWindow
*win
= wxFindWindowAtPoint(event2
.GetPosition());
394 // translate the event coords to the ones of the window
395 // which is going to get the event
396 win
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
398 event2
.SetEventObject(win
);
399 wxPostEvent(win
, event2
);
404 #ifdef __WXUNIVERSAL__
405 case wxHT_WINDOW_HORZ_SCROLLBAR
:
406 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
409 case wxHT_WINDOW_VERT_SCROLLBAR
:
410 sbar
= win
->GetScrollbar(wxVERTICAL
);
415 // forgot to update the switch after adding a new hit test code?
416 wxFAIL_MSG( _T("unexpected HitTest() return value") );
419 case wxHT_WINDOW_CORNER
:
420 // don't actually know if this one is good for anything, but let it
423 case wxHT_WINDOW_INSIDE
:
424 // let the normal processing take place
431 // translate the event coordinates to the scrollbar ones
432 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
434 // and give the event to it
435 wxMouseEvent event2
= event
;
439 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
443 // ----------------------------------------------------------------------------
444 // wxPopupFocusHandler
445 // ----------------------------------------------------------------------------
447 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
450 // ignore the next OnKillFocus() call
451 if ( time(NULL
) < m_creationTime
+ 1 )
459 // when we lose focus we always disappear - unless it goes to the popup (in
460 // which case we don't really lose it)
461 wxWindow
*win
= event
.GetWindow();
464 if ( win
== m_popup
)
466 win
= win
->GetParent();
469 m_popup
->DismissAndNotify();
472 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
474 // let the window have it first, it might process the keys
475 if ( !m_popup
->ProcessEvent(event
) )
477 // by default, dismiss the popup
478 m_popup
->DismissAndNotify();
482 #endif // wxUSE_POPUPWIN