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 // ----------------------------------------------------------------------------
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 // we can't capture mouse before the window is shown in wxGTL
195 m_child
->CaptureMouse();
196 m_child
->PushEventHandler(new wxPopupWindowHandler(this));
202 m_focus
= winFocus
? winFocus
: this;
203 m_focus
->PushEventHandler(new wxPopupFocusHandler(this));
207 void wxPopupTransientWindow::Dismiss()
214 void wxPopupTransientWindow::DismissAndNotify()
221 void wxPopupTransientWindow::OnDismiss()
223 // nothing to do here - but it may be interesting for derived class
226 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
228 // no special processing here
232 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
234 // ----------------------------------------------------------------------------
235 // wxPopupComboWindow
236 // ----------------------------------------------------------------------------
238 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
239 : wxPopupTransientWindow(parent
)
244 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
248 return wxPopupWindow::Create(parent
);
251 void wxPopupComboWindow::PositionNearCombo()
253 // the origin point must be in screen coords
254 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0, 0));
256 #if 0 //def __WXUNIVERSAL__
257 // account for the fact that (0, 0) is not the top left corner of the
258 // window: there is also the border
259 wxRect rectBorders
= m_combo
->GetRenderer()->
260 GetBorderDimensions(m_combo
->GetBorder());
261 ptOrigin
.x
-= rectBorders
.x
;
262 ptOrigin
.y
-= rectBorders
.y
;
263 #endif // __WXUNIVERSAL__
265 // position below or above the combobox: the width is 0 to put it exactly
266 // below us, not to the left or to the right
267 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
270 void wxPopupComboWindow::OnDismiss()
272 m_combo
->OnDismiss();
275 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
277 // ----------------------------------------------------------------------------
278 // wxPopupWindowHandler
279 // ----------------------------------------------------------------------------
281 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
283 // let the window have it first (we're the first event handler in the chain
284 // of handlers for this window)
285 if ( m_popup
->ProcessLeftDown(event
) )
290 wxPoint pos
= event
.GetPosition();
292 // scrollbar on which the click occured
293 wxWindow
*sbar
= NULL
;
295 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
296 switch ( win
->HitTest(pos
.x
, pos
.y
) )
298 case wxHT_WINDOW_OUTSIDE
:
299 // clicking outside a popup dismisses it
300 m_popup
->DismissAndNotify();
303 #ifdef __WXUNIVERSAL__
304 case wxHT_WINDOW_HORZ_SCROLLBAR
:
305 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
308 case wxHT_WINDOW_VERT_SCROLLBAR
:
309 sbar
= win
->GetScrollbar(wxVERTICAL
);
314 // forgot to update the switch after adding a new hit test code?
315 wxFAIL_MSG( _T("unexpected HitTest() return value") );
318 case wxHT_WINDOW_CORNER
:
319 // don't actually know if this one is good for anything, but let it
322 case wxHT_WINDOW_INSIDE
:
323 // let the normal processing take place
330 // translate the event coordinates to the scrollbar ones
331 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
333 // and give the event to it
334 wxMouseEvent event2
= event
;
338 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
342 // ----------------------------------------------------------------------------
343 // wxPopupFocusHandler
344 // ----------------------------------------------------------------------------
346 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
348 // when we lose focus we always disappear
350 // But if m_popup was about to get the focus,
352 if (event
.GetWindow() != m_popup
)
353 m_popup
->DismissAndNotify();
356 #endif // wxUSE_POPUPWIN