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,mgl}/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
45 #if defined(__WXMSW__) || defined(__WXMGL__)
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 wxPopupWindowBase::~wxPopupWindowBase()
113 // this destructor is required for Darwin
116 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
121 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
124 wxSize sizeScreen
= wxGetDisplaySize(),
125 sizeSelf
= GetSize();
127 // is there enough space to put the popup below the window (where we put it
129 wxCoord y
= ptOrigin
.y
+ size
.y
;
130 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
132 // check if there is enough space above
133 if ( ptOrigin
.y
> sizeSelf
.y
)
135 // do position the control above the window
136 y
-= size
.y
+ sizeSelf
.y
;
138 //else: not enough space below nor above, leave below
141 // now check left/right too
142 wxCoord x
= ptOrigin
.x
+ size
.x
;
143 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
145 // check if there is enough space to the left
146 if ( ptOrigin
.x
> sizeSelf
.x
)
148 // do position the control to the left
149 x
-= size
.x
+ sizeSelf
.x
;
151 //else: not enough space there neither, leave in default position
154 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
157 // ----------------------------------------------------------------------------
158 // wxPopupTransientWindow
159 // ----------------------------------------------------------------------------
161 void wxPopupTransientWindow::Init()
164 m_focus
= (wxWindow
*)NULL
;
167 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
171 (void)Create(parent
, style
);
174 wxPopupTransientWindow::~wxPopupTransientWindow()
179 void wxPopupTransientWindow::PopHandlers()
183 m_child
->PopEventHandler(TRUE
/* delete it */);
184 m_child
->ReleaseMouse();
190 m_focus
->PopEventHandler(TRUE
/* delete it */);
195 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
197 const wxWindowList
& children
= GetChildren();
198 if ( children
.GetCount() )
200 m_child
= children
.GetFirst()->GetData();
207 // we can't capture mouse before the window is shown in wxGTL
212 m_child
->CaptureMouse();
213 m_child
->PushEventHandler(new wxPopupWindowHandler(this));
219 m_focus
= winFocus
? winFocus
: this;
220 m_focus
->PushEventHandler(new wxPopupFocusHandler(this));
224 void wxPopupTransientWindow::Dismiss()
231 void wxPopupTransientWindow::DismissAndNotify()
238 void wxPopupTransientWindow::OnDismiss()
240 // nothing to do here - but it may be interesting for derived class
243 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
245 // no special processing here
249 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
251 // ----------------------------------------------------------------------------
252 // wxPopupComboWindow
253 // ----------------------------------------------------------------------------
255 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
256 : wxPopupTransientWindow(parent
)
261 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
265 return wxPopupWindow::Create(parent
);
268 void wxPopupComboWindow::PositionNearCombo()
270 // the origin point must be in screen coords
271 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0, 0));
273 #if 0 //def __WXUNIVERSAL__
274 // account for the fact that (0, 0) is not the top left corner of the
275 // window: there is also the border
276 wxRect rectBorders
= m_combo
->GetRenderer()->
277 GetBorderDimensions(m_combo
->GetBorder());
278 ptOrigin
.x
-= rectBorders
.x
;
279 ptOrigin
.y
-= rectBorders
.y
;
280 #endif // __WXUNIVERSAL__
282 // position below or above the combobox: the width is 0 to put it exactly
283 // below us, not to the left or to the right
284 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
287 void wxPopupComboWindow::OnDismiss()
289 m_combo
->OnDismiss();
292 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
294 // ----------------------------------------------------------------------------
295 // wxPopupWindowHandler
296 // ----------------------------------------------------------------------------
298 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
300 // let the window have it first (we're the first event handler in the chain
301 // of handlers for this window)
302 if ( m_popup
->ProcessLeftDown(event
) )
307 wxPoint pos
= event
.GetPosition();
309 // scrollbar on which the click occured
310 wxWindow
*sbar
= NULL
;
312 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
313 switch ( win
->HitTest(pos
.x
, pos
.y
) )
315 case wxHT_WINDOW_OUTSIDE
:
316 // clicking outside a popup dismisses it
317 m_popup
->DismissAndNotify();
320 #ifdef __WXUNIVERSAL__
321 case wxHT_WINDOW_HORZ_SCROLLBAR
:
322 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
325 case wxHT_WINDOW_VERT_SCROLLBAR
:
326 sbar
= win
->GetScrollbar(wxVERTICAL
);
331 // forgot to update the switch after adding a new hit test code?
332 wxFAIL_MSG( _T("unexpected HitTest() return value") );
335 case wxHT_WINDOW_CORNER
:
336 // don't actually know if this one is good for anything, but let it
339 case wxHT_WINDOW_INSIDE
:
340 // let the normal processing take place
347 // translate the event coordinates to the scrollbar ones
348 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
350 // and give the event to it
351 wxMouseEvent event2
= event
;
355 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
359 // ----------------------------------------------------------------------------
360 // wxPopupFocusHandler
361 // ----------------------------------------------------------------------------
363 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
365 // when we lose focus we always disappear
367 // But if m_popup was about to get the focus,
369 if (event
.GetWindow() != m_popup
)
370 m_popup
->DismissAndNotify();
373 #endif // wxUSE_POPUPWIN