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
40 #ifdef __WXUNIVERSAL__
41 #include "wx/univ/renderer.h"
42 #endif // __WXUNIVERSAL__
44 // there is no src/{msw,mgl}/popupwin.cpp to put this in, so we do it here - BTW we
45 // probably could do it for all ports here just as well
46 #if defined(__WXMSW__) || defined(__WXMGL__)
47 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow
, wxWindow
)
50 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow
, wxPopupWindow
)
52 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
53 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow
, wxPopupTransientWindow
)
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // event handlers which we use to intercept events which cause the popup to
62 class wxPopupWindowHandler
: public wxEvtHandler
65 wxPopupWindowHandler(wxPopupTransientWindow
*popup
) { m_popup
= popup
; }
69 void OnLeftDown(wxMouseEvent
& event
);
72 wxPopupTransientWindow
*m_popup
;
77 class wxPopupFocusHandler
: public wxEvtHandler
80 wxPopupFocusHandler(wxPopupTransientWindow
*popup
)
85 // ignore the next few OnKillFocus() calls
86 m_creationTime
= time(NULL
);
92 void OnKillFocus(wxFocusEvent
& event
);
93 void OnKeyDown(wxKeyEvent
& event
);
96 wxPopupTransientWindow
*m_popup
;
98 // hack around wxGTK bug: we always get several kill focus events
99 // immediately after creation!
101 time_t m_creationTime
;
104 DECLARE_EVENT_TABLE()
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
112 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
115 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
116 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
117 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown
)
120 // ============================================================================
122 // ============================================================================
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 wxPopupWindowBase::~wxPopupWindowBase()
130 // this destructor is required for Darwin
133 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
138 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
141 wxSize sizeScreen
= wxGetDisplaySize(),
142 sizeSelf
= GetSize();
144 // is there enough space to put the popup below the window (where we put it
146 wxCoord y
= ptOrigin
.y
+ size
.y
;
147 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
149 // check if there is enough space above
150 if ( ptOrigin
.y
> sizeSelf
.y
)
152 // do position the control above the window
153 y
-= size
.y
+ sizeSelf
.y
;
155 //else: not enough space below nor above, leave below
158 // now check left/right too
159 wxCoord x
= ptOrigin
.x
+ size
.x
;
160 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
162 // check if there is enough space to the left
163 if ( ptOrigin
.x
> sizeSelf
.x
)
165 // do position the control to the left
166 x
-= size
.x
+ sizeSelf
.x
;
168 //else: not enough space there neither, leave in default position
171 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
174 // ----------------------------------------------------------------------------
175 // wxPopupTransientWindow
176 // ----------------------------------------------------------------------------
178 void wxPopupTransientWindow::Init()
181 m_focus
= (wxWindow
*)NULL
;
183 m_handlerFocus
= NULL
;
184 m_handlerPopup
= NULL
;
187 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
191 (void)Create(parent
, style
);
194 wxPopupTransientWindow::~wxPopupTransientWindow()
198 delete m_handlerFocus
;
199 delete m_handlerPopup
;
202 void wxPopupTransientWindow::PopHandlers()
206 if ( !m_child
->RemoveEventHandler(m_handlerPopup
) )
208 // something is very wrong and someone else probably deleted our
209 // handler - so don't risk deleting it second time
210 m_handlerPopup
= NULL
;
213 m_child
->ReleaseMouse();
219 if ( !m_focus
->RemoveEventHandler(m_handlerFocus
) )
222 m_handlerFocus
= NULL
;
229 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
231 const wxWindowList
& children
= GetChildren();
232 if ( children
.GetCount() )
234 m_child
= children
.GetFirst()->GetData();
241 // we can't capture mouse before the window is shown in wxGTK, so do it
245 delete m_handlerPopup
;
246 m_handlerPopup
= new wxPopupWindowHandler(this);
248 m_child
->CaptureMouse();
249 m_child
->PushEventHandler(m_handlerPopup
);
251 m_focus
= winFocus
? winFocus
: this;
255 // MSW doesn't allow to set focus to the popup window, but we need to
256 // subclass the window which has the focus, and not winFocus passed in or
257 // otherwise everything else breaks down
258 m_focus
= FindFocus();
262 delete m_handlerFocus
;
263 m_handlerFocus
= new wxPopupFocusHandler(this);
265 m_focus
->PushEventHandler(m_handlerFocus
);
269 void wxPopupTransientWindow::Dismiss()
276 void wxPopupTransientWindow::DismissAndNotify()
283 void wxPopupTransientWindow::OnDismiss()
285 // nothing to do here - but it may be interesting for derived class
288 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
290 // no special processing here
294 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
296 // ----------------------------------------------------------------------------
297 // wxPopupComboWindow
298 // ----------------------------------------------------------------------------
300 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
301 : wxPopupTransientWindow(parent
)
306 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
310 return wxPopupWindow::Create(parent
);
313 void wxPopupComboWindow::PositionNearCombo()
315 // the origin point must be in screen coords
316 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0, 0));
318 #if 0 //def __WXUNIVERSAL__
319 // account for the fact that (0, 0) is not the top left corner of the
320 // window: there is also the border
321 wxRect rectBorders
= m_combo
->GetRenderer()->
322 GetBorderDimensions(m_combo
->GetBorder());
323 ptOrigin
.x
-= rectBorders
.x
;
324 ptOrigin
.y
-= rectBorders
.y
;
325 #endif // __WXUNIVERSAL__
327 // position below or above the combobox: the width is 0 to put it exactly
328 // below us, not to the left or to the right
329 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
332 void wxPopupComboWindow::OnDismiss()
334 m_combo
->OnDismiss();
337 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
339 // ----------------------------------------------------------------------------
340 // wxPopupWindowHandler
341 // ----------------------------------------------------------------------------
343 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
345 // let the window have it first (we're the first event handler in the chain
346 // of handlers for this window)
347 if ( m_popup
->ProcessLeftDown(event
) )
352 wxPoint pos
= event
.GetPosition();
354 // scrollbar on which the click occured
355 wxWindow
*sbar
= NULL
;
357 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
359 switch ( win
->HitTest(pos
.x
, pos
.y
) )
361 case wxHT_WINDOW_OUTSIDE
:
362 // clicking outside a popup dismisses it
363 m_popup
->DismissAndNotify();
366 #ifdef __WXUNIVERSAL__
367 case wxHT_WINDOW_HORZ_SCROLLBAR
:
368 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
371 case wxHT_WINDOW_VERT_SCROLLBAR
:
372 sbar
= win
->GetScrollbar(wxVERTICAL
);
377 // forgot to update the switch after adding a new hit test code?
378 wxFAIL_MSG( _T("unexpected HitTest() return value") );
381 case wxHT_WINDOW_CORNER
:
382 // don't actually know if this one is good for anything, but let it
385 case wxHT_WINDOW_INSIDE
:
386 // let the normal processing take place
393 // translate the event coordinates to the scrollbar ones
394 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
396 // and give the event to it
397 wxMouseEvent event2
= event
;
401 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
405 // ----------------------------------------------------------------------------
406 // wxPopupFocusHandler
407 // ----------------------------------------------------------------------------
409 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
412 // ignore the next OnKillFocus() call
413 if ( time(NULL
) < m_creationTime
+ 1 )
421 // when we lose focus we always disappear - unless it goes to the popup (in
422 // which case we don't really lose it)
423 wxWindow
*win
= event
.GetWindow();
426 if ( win
== m_popup
)
428 win
= win
->GetParent();
431 m_popup
->DismissAndNotify();
434 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
436 // let the window have it first, it might process the keys
437 if ( !m_popup
->ProcessEvent(event
) )
439 // by default, dismiss the popup
440 m_popup
->DismissAndNotify();
444 #endif // wxUSE_POPUPWIN