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 "popupwin.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/popupwin.h"
36 #include "wx/combobox.h" // wxComboControl
39 #ifdef __WXUNIVERSAL__
40 #include "wx/univ/renderer.h"
41 #endif // __WXUNIVERSAL__
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // event handlers which we use to intercept events which cause the popup to
49 class wxPopupWindowHandler
: public wxEvtHandler
52 wxPopupWindowHandler(wxPopupTransientWindow
*popup
) { m_popup
= popup
; }
56 void OnLeftDown(wxMouseEvent
& event
);
59 wxPopupTransientWindow
*m_popup
;
64 class wxPopupFocusHandler
: public wxEvtHandler
67 wxPopupFocusHandler(wxPopupTransientWindow
*popup
) { m_popup
= popup
; }
71 void OnKillFocus(wxFocusEvent
& event
);
74 wxPopupTransientWindow
*m_popup
;
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
84 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
87 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
88 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
91 // ============================================================================
93 // ============================================================================
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
104 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
107 wxSize sizeScreen
= wxGetDisplaySize(),
108 sizeSelf
= GetSize();
110 // is there enough space to put the popup below the window (where we put it
112 wxCoord y
= ptOrigin
.y
+ size
.y
;
113 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
115 // check if there is enough space above
116 if ( ptOrigin
.y
> sizeSelf
.y
)
118 // do position the control above the window
119 y
-= size
.y
+ sizeSelf
.y
;
121 //else: not enough space below nor above, leave below
124 // now check left/right too
125 wxCoord x
= ptOrigin
.x
+ size
.x
;
126 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
128 // check if there is enough space to the left
129 if ( ptOrigin
.x
> sizeSelf
.x
)
131 // do position the control to the left
132 x
-= size
.x
+ sizeSelf
.x
;
134 //else: not enough space there neither, leave in default position
137 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
140 // ----------------------------------------------------------------------------
141 // wxPopupTransientWindow
142 // ----------------------------------------------------------------------------
144 void wxPopupTransientWindow::Init()
147 m_focus
= (wxWindow
*)NULL
;
150 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
)
154 (void)Create(parent
);
157 wxPopupTransientWindow::~wxPopupTransientWindow()
162 void wxPopupTransientWindow::PopHandlers()
166 m_child
->PopEventHandler(TRUE
/* delete it */);
167 m_child
->ReleaseMouse();
173 m_focus
->PopEventHandler(TRUE
/* delete it */);
178 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
180 const wxWindowList
& children
= GetChildren();
181 if ( children
.GetCount() )
183 m_child
= children
.GetFirst()->GetData();
190 m_child
->CaptureMouse();
191 m_child
->PushEventHandler(new wxPopupWindowHandler(this));
195 m_focus
= winFocus
? winFocus
: this;
196 m_focus
->PushEventHandler(new wxPopupFocusHandler(this));
200 void wxPopupTransientWindow::Dismiss()
207 void wxPopupTransientWindow::DismissAndNotify()
214 void wxPopupTransientWindow::OnDismiss()
216 // nothing to do here - but it may be interesting for derived class
219 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
221 // no special processing here
227 // ----------------------------------------------------------------------------
228 // wxPopupComboWindow
229 // ----------------------------------------------------------------------------
231 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
232 : wxPopupTransientWindow(parent
)
237 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
241 return wxPopupWindow::Create(parent
);
244 void wxPopupComboWindow::PositionNearCombo()
246 // the origin point must be in screen coords
247 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0, 0));
249 #ifdef __WXUNIVERSAL__
250 // account for the fact that (0, 0) is not the top left corner of the
251 // window: there is also the border
252 wxRect rectBorders
= m_combo
->GetRenderer()->
253 GetBorderDimensions(m_combo
->GetBorder());
254 ptOrigin
.x
-= rectBorders
.x
;
255 ptOrigin
.y
-= rectBorders
.y
;
256 #endif // __WXUNIVERSAL__
258 // position below or above the combobox: the width is 0 to put it exactly
259 // below us, not to the left or to the right
260 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
263 void wxPopupComboWindow::OnDismiss()
265 m_combo
->OnDismiss();
268 #endif // wxUSE_COMBOBOX
270 // ----------------------------------------------------------------------------
271 // wxPopupWindowHandler
272 // ----------------------------------------------------------------------------
274 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
276 // let the window have it first (we're the first event handler in the chain
277 // of handlers for this window)
278 if ( m_popup
->ProcessLeftDown(event
) )
283 wxPoint pos
= event
.GetPosition();
285 // scrollbar on which the click occured
286 wxWindow
*sbar
= NULL
;
288 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
289 switch ( win
->HitTest(pos
.x
, pos
.y
) )
291 case wxHT_WINDOW_OUTSIDE
:
292 // clicking outside a popup dismisses it
293 m_popup
->DismissAndNotify();
296 case wxHT_WINDOW_HORZ_SCROLLBAR
:
297 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
300 case wxHT_WINDOW_VERT_SCROLLBAR
:
301 sbar
= win
->GetScrollbar(wxVERTICAL
);
305 // forgot to update the switch after adding a new hit test code?
306 wxFAIL_MSG( _T("unexpected HitTest() return value") );
309 case wxHT_WINDOW_CORNER
:
310 // don't actually know if this one is good for anything, but let it
313 case wxHT_WINDOW_INSIDE
:
314 // let the normal processing take place
321 // translate the event coordinates to the scrollbar ones
322 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
324 // and give the event to it
325 wxMouseEvent event2
= event
;
329 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
333 // ----------------------------------------------------------------------------
334 // wxPopupFocusHandler
335 // ----------------------------------------------------------------------------
337 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
339 // when we lose focus we always disappear
340 m_popup
->DismissAndNotify();
343 #endif // wxUSE_POPUPWIN