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();
220 if ( !m_focus
->RemoveEventHandler(m_handlerFocus
) )
223 m_handlerFocus
= NULL
;
230 void wxPopupTransientWindow
::Popup(wxWindow
*winFocus
)
232 const wxWindowList
& children
= GetChildren();
233 if ( children
.GetCount() )
235 m_child
= children
.GetFirst()->GetData();
242 // we can't capture mouse before the window is shown in wxGTK, so do it
246 delete m_handlerPopup
;
247 m_handlerPopup
= new wxPopupWindowHandler(this);
249 m_child
->CaptureMouse();
250 m_child
->PushEventHandler(m_handlerPopup
);
252 m_focus
= winFocus ? winFocus
: this;
258 // MSW doesn't allow to set focus to the popup window, but we need to
259 // subclass the window which has the focus, and not winFocus passed in or
260 // otherwise everything else breaks down
261 m_focus
= FindFocus();
265 delete m_handlerFocus
;
266 m_handlerFocus
= new wxPopupFocusHandler(this);
268 m_focus
->PushEventHandler(m_handlerFocus
);
274 void wxPopupTransientWindow
::Dismiss()
281 void wxPopupTransientWindow
::DismissAndNotify()
288 void wxPopupTransientWindow
::OnDismiss()
290 // nothing to do here - but it may be interesting for derived class
293 bool wxPopupTransientWindow
::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
295 // no special processing here
299 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
301 // ----------------------------------------------------------------------------
302 // wxPopupComboWindow
303 // ----------------------------------------------------------------------------
305 wxPopupComboWindow
::wxPopupComboWindow(wxComboControl
*parent
)
306 : wxPopupTransientWindow(parent
)
311 bool wxPopupComboWindow
::Create(wxComboControl
*parent
)
315 return wxPopupWindow
::Create(parent
);
318 void wxPopupComboWindow
::PositionNearCombo()
320 // the origin point must be in screen coords
321 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0, 0));
323 #if 0 //def __WXUNIVERSAL__
324 // account for the fact that (0, 0) is not the top left corner of the
325 // window: there is also the border
326 wxRect rectBorders
= m_combo
->GetRenderer()->
327 GetBorderDimensions(m_combo
->GetBorder());
328 ptOrigin
.x
-= rectBorders
.x
;
329 ptOrigin
.y
-= rectBorders
.y
;
330 #endif // __WXUNIVERSAL__
332 // position below or above the combobox: the width is 0 to put it exactly
333 // below us, not to the left or to the right
334 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
337 void wxPopupComboWindow
::OnDismiss()
339 m_combo
->OnDismiss();
342 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
344 // ----------------------------------------------------------------------------
345 // wxPopupWindowHandler
346 // ----------------------------------------------------------------------------
348 void wxPopupWindowHandler
::OnLeftDown(wxMouseEvent
& event
)
350 // let the window have it first (we're the first event handler in the chain
351 // of handlers for this window)
352 if ( m_popup
->ProcessLeftDown(event
) )
357 wxPoint pos
= event
.GetPosition();
359 // scrollbar on which the click occured
360 wxWindow
*sbar
= NULL
;
362 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
364 switch ( win
->HitTest(pos
.x
, pos
.y
) )
366 case wxHT_WINDOW_OUTSIDE
:
367 // clicking outside a popup dismisses it
368 m_popup
->DismissAndNotify();
371 #ifdef __WXUNIVERSAL__
372 case wxHT_WINDOW_HORZ_SCROLLBAR
:
373 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
376 case wxHT_WINDOW_VERT_SCROLLBAR
:
377 sbar
= win
->GetScrollbar(wxVERTICAL
);
382 // forgot to update the switch after adding a new hit test code?
383 wxFAIL_MSG( _T("unexpected HitTest() return value") );
386 case wxHT_WINDOW_CORNER
:
387 // don't actually know if this one is good for anything, but let it
390 case wxHT_WINDOW_INSIDE
:
391 // let the normal processing take place
398 // translate the event coordinates to the scrollbar ones
399 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
401 // and give the event to it
402 wxMouseEvent event2
= event
;
406 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
410 // ----------------------------------------------------------------------------
411 // wxPopupFocusHandler
412 // ----------------------------------------------------------------------------
414 void wxPopupFocusHandler
::OnKillFocus(wxFocusEvent
& event
)
417 // ignore the next OnKillFocus() call
418 if ( time(NULL
) < m_creationTime
+ 1 )
426 // when we lose focus we always disappear - unless it goes to the popup (in
427 // which case we don't really lose it)
428 wxWindow
*win
= event
.GetWindow();
431 if ( win
== m_popup
)
433 win
= win
->GetParent();
436 m_popup
->DismissAndNotify();
439 void wxPopupFocusHandler
::OnKeyDown(wxKeyEvent
& event
)
441 // let the window have it first, it might process the keys
442 if ( !m_popup
->ProcessEvent(event
) )
444 // by default, dismiss the popup
445 m_popup
->DismissAndNotify();
449 #endif // wxUSE_POPUPWIN