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
);
84 void OnKeyUp(wxKeyEvent
& event
);
87 wxPopupTransientWindow
*m_popup
;
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
97 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
100 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
101 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
102 EVT_KEY_UP(wxPopupFocusHandler::OnKeyUp
)
105 // ============================================================================
107 // ============================================================================
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 wxPopupWindowBase::~wxPopupWindowBase()
115 // this destructor is required for Darwin
118 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
123 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
126 wxSize sizeScreen
= wxGetDisplaySize(),
127 sizeSelf
= GetSize();
129 // is there enough space to put the popup below the window (where we put it
131 wxCoord y
= ptOrigin
.y
+ size
.y
;
132 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
134 // check if there is enough space above
135 if ( ptOrigin
.y
> sizeSelf
.y
)
137 // do position the control above the window
138 y
-= size
.y
+ sizeSelf
.y
;
140 //else: not enough space below nor above, leave below
143 // now check left/right too
144 wxCoord x
= ptOrigin
.x
+ size
.x
;
145 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
147 // check if there is enough space to the left
148 if ( ptOrigin
.x
> sizeSelf
.x
)
150 // do position the control to the left
151 x
-= size
.x
+ sizeSelf
.x
;
153 //else: not enough space there neither, leave in default position
156 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
159 // ----------------------------------------------------------------------------
160 // wxPopupTransientWindow
161 // ----------------------------------------------------------------------------
163 void wxPopupTransientWindow::Init()
166 m_focus
= (wxWindow
*)NULL
;
169 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
173 (void)Create(parent
, style
);
176 wxPopupTransientWindow::~wxPopupTransientWindow()
181 void wxPopupTransientWindow::PopHandlers()
185 m_child
->PopEventHandler(TRUE
/* delete it */);
186 m_child
->ReleaseMouse();
192 m_focus
->PopEventHandler(TRUE
/* delete it */);
197 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
199 const wxWindowList
& children
= GetChildren();
200 if ( children
.GetCount() )
202 m_child
= children
.GetFirst()->GetData();
209 // we can't capture mouse before the window is shown in wxGTK
214 m_child
->CaptureMouse();
215 m_child
->PushEventHandler(new wxPopupWindowHandler(this));
221 m_focus
= winFocus
? winFocus
: this;
224 // FIXME: I don't know why does this happen but sometimes SetFocus() simply
225 // refuses to work under MSW - no error happens but the focus is not
226 // given to the window, i.e. the assert below is triggered
228 // Try work around this as we can...
230 wxASSERT_MSG( FindFocus() == m_focus
, _T("setting focus failed") );
232 m_focus
= FindFocus();
237 m_focus
->PushEventHandler(new wxPopupFocusHandler(this));
241 void wxPopupTransientWindow::Dismiss()
248 void wxPopupTransientWindow::DismissAndNotify()
255 void wxPopupTransientWindow::OnDismiss()
257 // nothing to do here - but it may be interesting for derived class
260 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
262 // no special processing here
266 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
268 // ----------------------------------------------------------------------------
269 // wxPopupComboWindow
270 // ----------------------------------------------------------------------------
272 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
273 : wxPopupTransientWindow(parent
)
278 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
282 return wxPopupWindow::Create(parent
);
285 void wxPopupComboWindow::PositionNearCombo()
287 // the origin point must be in screen coords
288 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0, 0));
290 #if 0 //def __WXUNIVERSAL__
291 // account for the fact that (0, 0) is not the top left corner of the
292 // window: there is also the border
293 wxRect rectBorders
= m_combo
->GetRenderer()->
294 GetBorderDimensions(m_combo
->GetBorder());
295 ptOrigin
.x
-= rectBorders
.x
;
296 ptOrigin
.y
-= rectBorders
.y
;
297 #endif // __WXUNIVERSAL__
299 // position below or above the combobox: the width is 0 to put it exactly
300 // below us, not to the left or to the right
301 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
304 void wxPopupComboWindow::OnDismiss()
306 m_combo
->OnDismiss();
309 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
311 // ----------------------------------------------------------------------------
312 // wxPopupWindowHandler
313 // ----------------------------------------------------------------------------
315 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
317 // let the window have it first (we're the first event handler in the chain
318 // of handlers for this window)
319 if ( m_popup
->ProcessLeftDown(event
) )
324 wxPoint pos
= event
.GetPosition();
326 // scrollbar on which the click occured
327 wxWindow
*sbar
= NULL
;
329 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
330 switch ( win
->HitTest(pos
.x
, pos
.y
) )
332 case wxHT_WINDOW_OUTSIDE
:
333 // clicking outside a popup dismisses it
334 m_popup
->DismissAndNotify();
337 #ifdef __WXUNIVERSAL__
338 case wxHT_WINDOW_HORZ_SCROLLBAR
:
339 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
342 case wxHT_WINDOW_VERT_SCROLLBAR
:
343 sbar
= win
->GetScrollbar(wxVERTICAL
);
348 // forgot to update the switch after adding a new hit test code?
349 wxFAIL_MSG( _T("unexpected HitTest() return value") );
352 case wxHT_WINDOW_CORNER
:
353 // don't actually know if this one is good for anything, but let it
356 case wxHT_WINDOW_INSIDE
:
357 // let the normal processing take place
364 // translate the event coordinates to the scrollbar ones
365 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
367 // and give the event to it
368 wxMouseEvent event2
= event
;
372 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
376 // ----------------------------------------------------------------------------
377 // wxPopupFocusHandler
378 // ----------------------------------------------------------------------------
380 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
382 // when we lose focus we always disappear - unless it goes to the popup (in
383 // which case we don't really lose it)
384 if ( event
.GetWindow() != m_popup
)
385 m_popup
->DismissAndNotify();
388 void wxPopupFocusHandler::OnKeyUp(wxKeyEvent
& event
)
390 // let the window have it first, it might process the keys
391 if ( !m_popup
->ProcessEvent(event
) )
393 // by default, dismiss the popup
394 m_popup
->DismissAndNotify();
398 #endif // wxUSE_POPUPWIN