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 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // event handlers which we use to intercept events which cause the popup to
55 class wxPopupWindowHandler
: public wxEvtHandler
58 wxPopupWindowHandler(wxPopupTransientWindow
*popup
) { m_popup
= popup
; }
62 void OnLeftDown(wxMouseEvent
& event
);
65 wxPopupTransientWindow
*m_popup
;
70 class wxPopupFocusHandler
: public wxEvtHandler
73 wxPopupFocusHandler(wxPopupTransientWindow
*popup
) { m_popup
= popup
; }
77 void OnKillFocus(wxFocusEvent
& event
);
80 wxPopupTransientWindow
*m_popup
;
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
90 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
93 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
94 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
97 // ============================================================================
99 // ============================================================================
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
110 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
113 wxSize sizeScreen
= wxGetDisplaySize(),
114 sizeSelf
= GetSize();
116 // is there enough space to put the popup below the window (where we put it
118 wxCoord y
= ptOrigin
.y
+ size
.y
;
119 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
121 // check if there is enough space above
122 if ( ptOrigin
.y
> sizeSelf
.y
)
124 // do position the control above the window
125 y
-= size
.y
+ sizeSelf
.y
;
127 //else: not enough space below nor above, leave below
130 // now check left/right too
131 wxCoord x
= ptOrigin
.x
+ size
.x
;
132 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
134 // check if there is enough space to the left
135 if ( ptOrigin
.x
> sizeSelf
.x
)
137 // do position the control to the left
138 x
-= size
.x
+ sizeSelf
.x
;
140 //else: not enough space there neither, leave in default position
143 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
146 // ----------------------------------------------------------------------------
147 // wxPopupTransientWindow
148 // ----------------------------------------------------------------------------
150 void wxPopupTransientWindow::Init()
153 m_focus
= (wxWindow
*)NULL
;
156 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
160 (void)Create(parent
, style
);
163 wxPopupTransientWindow::~wxPopupTransientWindow()
168 void wxPopupTransientWindow::PopHandlers()
172 m_child
->PopEventHandler(TRUE
/* delete it */);
173 m_child
->ReleaseMouse();
179 m_focus
->PopEventHandler(TRUE
/* delete it */);
184 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
186 const wxWindowList
& children
= GetChildren();
187 if ( children
.GetCount() )
189 m_child
= children
.GetFirst()->GetData();
196 // we can't capture mouse before the window is shown in wxGTL
201 m_child
->CaptureMouse();
202 m_child
->PushEventHandler(new wxPopupWindowHandler(this));
208 m_focus
= winFocus
? winFocus
: this;
209 m_focus
->PushEventHandler(new wxPopupFocusHandler(this));
213 void wxPopupTransientWindow::Dismiss()
220 void wxPopupTransientWindow::DismissAndNotify()
227 void wxPopupTransientWindow::OnDismiss()
229 // nothing to do here - but it may be interesting for derived class
232 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
234 // no special processing here
238 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
240 // ----------------------------------------------------------------------------
241 // wxPopupComboWindow
242 // ----------------------------------------------------------------------------
244 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
245 : wxPopupTransientWindow(parent
)
250 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
254 return wxPopupWindow::Create(parent
);
257 void wxPopupComboWindow::PositionNearCombo()
259 // the origin point must be in screen coords
260 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0, 0));
262 #if 0 //def __WXUNIVERSAL__
263 // account for the fact that (0, 0) is not the top left corner of the
264 // window: there is also the border
265 wxRect rectBorders
= m_combo
->GetRenderer()->
266 GetBorderDimensions(m_combo
->GetBorder());
267 ptOrigin
.x
-= rectBorders
.x
;
268 ptOrigin
.y
-= rectBorders
.y
;
269 #endif // __WXUNIVERSAL__
271 // position below or above the combobox: the width is 0 to put it exactly
272 // below us, not to the left or to the right
273 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
276 void wxPopupComboWindow::OnDismiss()
278 m_combo
->OnDismiss();
281 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
283 // ----------------------------------------------------------------------------
284 // wxPopupWindowHandler
285 // ----------------------------------------------------------------------------
287 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
289 // let the window have it first (we're the first event handler in the chain
290 // of handlers for this window)
291 if ( m_popup
->ProcessLeftDown(event
) )
296 wxPoint pos
= event
.GetPosition();
298 // scrollbar on which the click occured
299 wxWindow
*sbar
= NULL
;
301 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
302 switch ( win
->HitTest(pos
.x
, pos
.y
) )
304 case wxHT_WINDOW_OUTSIDE
:
305 // clicking outside a popup dismisses it
306 m_popup
->DismissAndNotify();
309 #ifdef __WXUNIVERSAL__
310 case wxHT_WINDOW_HORZ_SCROLLBAR
:
311 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
314 case wxHT_WINDOW_VERT_SCROLLBAR
:
315 sbar
= win
->GetScrollbar(wxVERTICAL
);
320 // forgot to update the switch after adding a new hit test code?
321 wxFAIL_MSG( _T("unexpected HitTest() return value") );
324 case wxHT_WINDOW_CORNER
:
325 // don't actually know if this one is good for anything, but let it
328 case wxHT_WINDOW_INSIDE
:
329 // let the normal processing take place
336 // translate the event coordinates to the scrollbar ones
337 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
339 // and give the event to it
340 wxMouseEvent event2
= event
;
344 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
348 // ----------------------------------------------------------------------------
349 // wxPopupFocusHandler
350 // ----------------------------------------------------------------------------
352 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
354 // when we lose focus we always disappear
356 // But if m_popup was about to get the focus,
358 if (event
.GetWindow() != m_popup
)
359 m_popup
->DismissAndNotify();
362 #endif // wxUSE_POPUPWIN