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
39 #ifdef __WXUNIVERSAL__
40 #include "wx/univ/renderer.h"
41 #endif // __WXUNIVERSAL__
43 // there is no src/msw/popupwin.cpp to put this in, so we do it here - BTW we
44 // probably could do it for all ports here just as well
46 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow
, wxWindow
)
49 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow
, wxPopupWindow
)
51 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
52 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow
, wxPopupTransientWindow
)
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // event handlers which we use to intercept events which cause the popup to
61 class wxPopupWindowHandler
: public wxEvtHandler
64 wxPopupWindowHandler(wxPopupTransientWindow
*popup
) { m_popup
= popup
; }
68 void OnLeftDown(wxMouseEvent
& event
);
71 wxPopupTransientWindow
*m_popup
;
76 class wxPopupFocusHandler
: public wxEvtHandler
79 wxPopupFocusHandler(wxPopupTransientWindow
*popup
) { m_popup
= popup
; }
83 void OnKillFocus(wxFocusEvent
& event
);
86 wxPopupTransientWindow
*m_popup
;
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
96 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
99 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
100 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
103 // ============================================================================
105 // ============================================================================
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
116 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
119 wxSize sizeScreen
= wxGetDisplaySize(),
120 sizeSelf
= GetSize();
122 // is there enough space to put the popup below the window (where we put it
124 wxCoord y
= ptOrigin
.y
+ size
.y
;
125 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
127 // check if there is enough space above
128 if ( ptOrigin
.y
> sizeSelf
.y
)
130 // do position the control above the window
131 y
-= size
.y
+ sizeSelf
.y
;
133 //else: not enough space below nor above, leave below
136 // now check left/right too
137 wxCoord x
= ptOrigin
.x
+ size
.x
;
138 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
140 // check if there is enough space to the left
141 if ( ptOrigin
.x
> sizeSelf
.x
)
143 // do position the control to the left
144 x
-= size
.x
+ sizeSelf
.x
;
146 //else: not enough space there neither, leave in default position
149 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
152 // ----------------------------------------------------------------------------
153 // wxPopupTransientWindow
154 // ----------------------------------------------------------------------------
156 void wxPopupTransientWindow::Init()
159 m_focus
= (wxWindow
*)NULL
;
162 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
166 (void)Create(parent
, style
);
169 wxPopupTransientWindow::~wxPopupTransientWindow()
174 void wxPopupTransientWindow::PopHandlers()
178 m_child
->PopEventHandler(TRUE
/* delete it */);
179 m_child
->ReleaseMouse();
185 m_focus
->PopEventHandler(TRUE
/* delete it */);
190 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
192 const wxWindowList
& children
= GetChildren();
193 if ( children
.GetCount() )
195 m_child
= children
.GetFirst()->GetData();
202 // we can't capture mouse before the window is shown in wxGTL
207 m_child
->CaptureMouse();
208 m_child
->PushEventHandler(new wxPopupWindowHandler(this));
214 m_focus
= winFocus
? winFocus
: this;
215 m_focus
->PushEventHandler(new wxPopupFocusHandler(this));
219 void wxPopupTransientWindow::Dismiss()
226 void wxPopupTransientWindow::DismissAndNotify()
233 void wxPopupTransientWindow::OnDismiss()
235 // nothing to do here - but it may be interesting for derived class
238 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
240 // no special processing here
244 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
246 // ----------------------------------------------------------------------------
247 // wxPopupComboWindow
248 // ----------------------------------------------------------------------------
250 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
251 : wxPopupTransientWindow(parent
)
256 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
260 return wxPopupWindow::Create(parent
);
263 void wxPopupComboWindow::PositionNearCombo()
265 // the origin point must be in screen coords
266 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0, 0));
268 #if 0 //def __WXUNIVERSAL__
269 // account for the fact that (0, 0) is not the top left corner of the
270 // window: there is also the border
271 wxRect rectBorders
= m_combo
->GetRenderer()->
272 GetBorderDimensions(m_combo
->GetBorder());
273 ptOrigin
.x
-= rectBorders
.x
;
274 ptOrigin
.y
-= rectBorders
.y
;
275 #endif // __WXUNIVERSAL__
277 // position below or above the combobox: the width is 0 to put it exactly
278 // below us, not to the left or to the right
279 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
282 void wxPopupComboWindow::OnDismiss()
284 m_combo
->OnDismiss();
287 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
289 // ----------------------------------------------------------------------------
290 // wxPopupWindowHandler
291 // ----------------------------------------------------------------------------
293 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
295 // let the window have it first (we're the first event handler in the chain
296 // of handlers for this window)
297 if ( m_popup
->ProcessLeftDown(event
) )
302 wxPoint pos
= event
.GetPosition();
304 // scrollbar on which the click occured
305 wxWindow
*sbar
= NULL
;
307 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
308 switch ( win
->HitTest(pos
.x
, pos
.y
) )
310 case wxHT_WINDOW_OUTSIDE
:
311 // clicking outside a popup dismisses it
312 m_popup
->DismissAndNotify();
315 #ifdef __WXUNIVERSAL__
316 case wxHT_WINDOW_HORZ_SCROLLBAR
:
317 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
320 case wxHT_WINDOW_VERT_SCROLLBAR
:
321 sbar
= win
->GetScrollbar(wxVERTICAL
);
326 // forgot to update the switch after adding a new hit test code?
327 wxFAIL_MSG( _T("unexpected HitTest() return value") );
330 case wxHT_WINDOW_CORNER
:
331 // don't actually know if this one is good for anything, but let it
334 case wxHT_WINDOW_INSIDE
:
335 // let the normal processing take place
342 // translate the event coordinates to the scrollbar ones
343 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
345 // and give the event to it
346 wxMouseEvent event2
= event
;
350 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
354 // ----------------------------------------------------------------------------
355 // wxPopupFocusHandler
356 // ----------------------------------------------------------------------------
358 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
360 // when we lose focus we always disappear
362 // But if m_popup was about to get the focus,
364 if (event
.GetWindow() != m_popup
)
365 m_popup
->DismissAndNotify();
368 #endif // wxUSE_POPUPWIN