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
)
84 // ignore the next few OnKillFocus() calls
85 m_creationTime
= time(NULL
);
91 void OnKillFocus(wxFocusEvent
& event
);
92 void OnKeyDown(wxKeyEvent
& event
);
95 wxPopupTransientWindow
*m_popup
;
97 // hack around wxGTK bug: we always get several kill focus events
98 // immediately after creation!
100 time_t m_creationTime
;
103 DECLARE_EVENT_TABLE()
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
111 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
114 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
115 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
116 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown
)
119 // ============================================================================
121 // ============================================================================
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 wxPopupWindowBase::~wxPopupWindowBase()
129 // this destructor is required for Darwin
132 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
137 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
140 wxSize sizeScreen
= wxGetDisplaySize(),
141 sizeSelf
= GetSize();
143 // is there enough space to put the popup below the window (where we put it
145 wxCoord y
= ptOrigin
.y
+ size
.y
;
146 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
148 // check if there is enough space above
149 if ( ptOrigin
.y
> sizeSelf
.y
)
151 // do position the control above the window
152 y
-= size
.y
+ sizeSelf
.y
;
154 //else: not enough space below nor above, leave below
157 // now check left/right too
158 wxCoord x
= ptOrigin
.x
+ size
.x
;
159 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
161 // check if there is enough space to the left
162 if ( ptOrigin
.x
> sizeSelf
.x
)
164 // do position the control to the left
165 x
-= size
.x
+ sizeSelf
.x
;
167 //else: not enough space there neither, leave in default position
170 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
173 // ----------------------------------------------------------------------------
174 // wxPopupTransientWindow
175 // ----------------------------------------------------------------------------
177 void wxPopupTransientWindow::Init()
180 m_focus
= (wxWindow
*)NULL
;
182 m_handlerFocus
= NULL
;
183 m_handlerPopup
= NULL
;
186 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
190 (void)Create(parent
, style
);
193 wxPopupTransientWindow::~wxPopupTransientWindow()
197 delete m_handlerFocus
;
198 delete m_handlerPopup
;
201 void wxPopupTransientWindow::PopHandlers()
205 if ( !m_child
->RemoveEventHandler(m_handlerPopup
) )
207 // something is very wrong and someone else probably deleted our
208 // handler - so don't risk deleting it second time
209 m_handlerPopup
= NULL
;
212 m_child
->ReleaseMouse();
218 if ( !m_focus
->RemoveEventHandler(m_handlerFocus
) )
221 m_handlerFocus
= NULL
;
228 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
230 const wxWindowList
& children
= GetChildren();
231 if ( children
.GetCount() )
233 m_child
= children
.GetFirst()->GetData();
240 // we can't capture mouse before the window is shown in wxGTK, so do it
244 delete m_handlerPopup
;
245 m_handlerPopup
= new wxPopupWindowHandler(this);
247 m_child
->CaptureMouse();
248 m_child
->PushEventHandler(m_handlerPopup
);
250 m_focus
= winFocus
? winFocus
: this;
254 // MSW doesn't allow to set focus to the popup window, but we need to
255 // subclass the window which has the focus, and not winFocus passed in or
256 // otherwise everything else breaks down
257 m_focus
= FindFocus();
261 delete m_handlerFocus
;
262 m_handlerFocus
= new wxPopupFocusHandler(this);
264 m_focus
->PushEventHandler(m_handlerFocus
);
268 void wxPopupTransientWindow::Dismiss()
275 void wxPopupTransientWindow::DismissAndNotify()
282 void wxPopupTransientWindow::OnDismiss()
284 // nothing to do here - but it may be interesting for derived class
287 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
289 // no special processing here
293 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
295 // ----------------------------------------------------------------------------
296 // wxPopupComboWindow
297 // ----------------------------------------------------------------------------
299 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
300 : wxPopupTransientWindow(parent
)
305 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
309 return wxPopupWindow::Create(parent
);
312 void wxPopupComboWindow::PositionNearCombo()
314 // the origin point must be in screen coords
315 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0, 0));
317 #if 0 //def __WXUNIVERSAL__
318 // account for the fact that (0, 0) is not the top left corner of the
319 // window: there is also the border
320 wxRect rectBorders
= m_combo
->GetRenderer()->
321 GetBorderDimensions(m_combo
->GetBorder());
322 ptOrigin
.x
-= rectBorders
.x
;
323 ptOrigin
.y
-= rectBorders
.y
;
324 #endif // __WXUNIVERSAL__
326 // position below or above the combobox: the width is 0 to put it exactly
327 // below us, not to the left or to the right
328 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
331 void wxPopupComboWindow::OnDismiss()
333 m_combo
->OnDismiss();
336 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
338 // ----------------------------------------------------------------------------
339 // wxPopupWindowHandler
340 // ----------------------------------------------------------------------------
342 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
344 // let the window have it first (we're the first event handler in the chain
345 // of handlers for this window)
346 if ( m_popup
->ProcessLeftDown(event
) )
351 wxPoint pos
= event
.GetPosition();
353 // scrollbar on which the click occured
354 wxWindow
*sbar
= NULL
;
356 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
357 switch ( win
->HitTest(pos
.x
, pos
.y
) )
359 case wxHT_WINDOW_OUTSIDE
:
360 // clicking outside a popup dismisses it
361 m_popup
->DismissAndNotify();
364 #ifdef __WXUNIVERSAL__
365 case wxHT_WINDOW_HORZ_SCROLLBAR
:
366 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
369 case wxHT_WINDOW_VERT_SCROLLBAR
:
370 sbar
= win
->GetScrollbar(wxVERTICAL
);
375 // forgot to update the switch after adding a new hit test code?
376 wxFAIL_MSG( _T("unexpected HitTest() return value") );
379 case wxHT_WINDOW_CORNER
:
380 // don't actually know if this one is good for anything, but let it
383 case wxHT_WINDOW_INSIDE
:
384 // let the normal processing take place
391 // translate the event coordinates to the scrollbar ones
392 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
394 // and give the event to it
395 wxMouseEvent event2
= event
;
399 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
403 // ----------------------------------------------------------------------------
404 // wxPopupFocusHandler
405 // ----------------------------------------------------------------------------
407 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
410 // ignore the next OnKillFocus() call
411 if ( time(NULL
) < m_creationTime
+ 1 )
419 // when we lose focus we always disappear - unless it goes to the popup (in
420 // which case we don't really lose it)
421 wxWindow
*win
= event
.GetWindow();
424 if ( win
== m_popup
)
426 win
= win
->GetParent();
428 m_popup
->DismissAndNotify();
431 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
433 // let the window have it first, it might process the keys
434 if ( !m_popup
->ProcessEvent(event
) )
436 // by default, dismiss the popup
437 m_popup
->DismissAndNotify();
441 #endif // wxUSE_POPUPWIN