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 licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "popupwinbase.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
37 #include "wx/app.h" // wxPostEvent
42 #ifdef __WXUNIVERSAL__
43 #include "wx/univ/renderer.h"
44 #endif // __WXUNIVERSAL__
50 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow
, wxWindow
)
51 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow
, wxPopupWindow
)
53 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
54 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow
, wxPopupTransientWindow
)
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // event handlers which we use to intercept events which cause the popup to
63 class wxPopupWindowHandler
: public wxEvtHandler
66 wxPopupWindowHandler(wxPopupTransientWindow
*popup
) { m_popup
= popup
; }
70 void OnLeftDown(wxMouseEvent
& event
);
73 wxPopupTransientWindow
*m_popup
;
76 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler
)
79 class wxPopupFocusHandler
: public wxEvtHandler
82 wxPopupFocusHandler(wxPopupTransientWindow
*popup
)
88 void OnKillFocus(wxFocusEvent
& event
);
89 void OnKeyDown(wxKeyEvent
& event
);
92 wxPopupTransientWindow
*m_popup
;
95 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler
)
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 BEGIN_EVENT_TABLE(wxPopupWindowHandler
, wxEvtHandler
)
103 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown
)
106 BEGIN_EVENT_TABLE(wxPopupFocusHandler
, wxEvtHandler
)
107 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus
)
108 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown
)
111 // ============================================================================
113 // ============================================================================
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 wxPopupWindowBase::~wxPopupWindowBase()
121 // this destructor is required for Darwin
124 bool wxPopupWindowBase::Create(wxWindow
* WXUNUSED(parent
), int WXUNUSED(flags
))
129 void wxPopupWindowBase::Position(const wxPoint
& ptOrigin
,
132 wxSize sizeScreen
= wxGetDisplaySize(),
133 sizeSelf
= GetSize();
135 // is there enough space to put the popup below the window (where we put it
137 wxCoord y
= ptOrigin
.y
+ size
.y
;
138 if ( y
+ sizeSelf
.y
> sizeScreen
.y
)
140 // check if there is enough space above
141 if ( ptOrigin
.y
> sizeSelf
.y
)
143 // do position the control above the window
144 y
-= size
.y
+ sizeSelf
.y
;
146 //else: not enough space below nor above, leave below
149 // now check left/right too
150 wxCoord x
= ptOrigin
.x
+ size
.x
;
151 if ( x
+ sizeSelf
.x
> sizeScreen
.x
)
153 // check if there is enough space to the left
154 if ( ptOrigin
.x
> sizeSelf
.x
)
156 // do position the control to the left
157 x
-= size
.x
+ sizeSelf
.x
;
159 //else: not enough space there neither, leave in default position
162 Move(x
, y
, wxSIZE_NO_ADJUSTMENTS
);
165 // ----------------------------------------------------------------------------
166 // wxPopupTransientWindow
167 // ----------------------------------------------------------------------------
169 void wxPopupTransientWindow::Init()
172 m_focus
= (wxWindow
*)NULL
;
174 m_handlerFocus
= NULL
;
175 m_handlerPopup
= NULL
;
178 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow
*parent
, int style
)
182 (void)Create(parent
, style
);
185 wxPopupTransientWindow::~wxPopupTransientWindow()
190 void wxPopupTransientWindow::PopHandlers()
194 if ( m_handlerPopup
&& !m_child
->RemoveEventHandler(m_handlerPopup
) )
196 // something is very wrong and someone else probably deleted our
197 // handler - so don't risk deleting it second time
198 m_handlerPopup
= NULL
;
201 if (m_child
->HasCapture())
202 m_child
->ReleaseMouse();
209 if ( m_handlerFocus
&& !m_focus
->RemoveEventHandler(m_handlerFocus
) )
212 m_handlerFocus
= NULL
;
216 if ( m_handlerFocus
&& !RemoveEventHandler(m_handlerFocus
) )
219 m_handlerFocus
= NULL
;
223 // delete the handlers, they'll be created as necessary in Popup()
224 delete m_handlerPopup
;
225 m_handlerPopup
= NULL
;
226 delete m_handlerFocus
;
227 m_handlerFocus
= NULL
;
232 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
234 const wxWindowList
& children
= GetChildren();
235 if ( children
.GetCount() )
237 m_child
= children
.GetFirst()->GetData();
246 delete m_handlerPopup
;
247 m_handlerPopup
= new wxPopupWindowHandler(this);
249 // m_child->CaptureMouse();
250 m_child
->PushEventHandler(m_handlerPopup
);
252 m_focus
= winFocus
? winFocus
: this;
256 // MSW doesn't allow to set focus to the popup window, but we need to
257 // subclass the window which has the focus, and not winFocus passed in or
258 // otherwise everything else breaks down
259 m_focus
= FindFocus();
262 delete m_handlerFocus
;
263 m_handlerFocus
= new wxPopupFocusHandler(this);
265 m_focus
->PushEventHandler(m_handlerFocus
);
268 // GTK+ catches the activate events from the popup
269 // window, not the focus events from the child window
270 delete m_handlerFocus
;
271 m_handlerFocus
= new wxPopupFocusHandler(this);
272 PushEventHandler(m_handlerFocus
);
277 bool wxPopupTransientWindow::Show( bool show
)
281 gtk_grab_remove( m_widget
);
284 bool ret
= wxPopupWindow::Show( show
);
288 gtk_grab_add( m_widget
);
294 void wxPopupTransientWindow::Dismiss()
301 void wxPopupTransientWindow::DismissAndNotify()
308 void wxPopupTransientWindow::OnDismiss()
310 // nothing to do here - but it may be interesting for derived class
313 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
315 // no special processing here
319 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
321 // ----------------------------------------------------------------------------
322 // wxPopupComboWindow
323 // ----------------------------------------------------------------------------
325 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
326 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
329 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
330 : wxPopupTransientWindow(parent
)
335 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
339 return wxPopupWindow::Create(parent
);
342 void wxPopupComboWindow::PositionNearCombo()
344 // the origin point must be in screen coords
345 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0, 0));
347 #if 0 //def __WXUNIVERSAL__
348 // account for the fact that (0, 0) is not the top left corner of the
349 // window: there is also the border
350 wxRect rectBorders
= m_combo
->GetRenderer()->
351 GetBorderDimensions(m_combo
->GetBorder());
352 ptOrigin
.x
-= rectBorders
.x
;
353 ptOrigin
.y
-= rectBorders
.y
;
354 #endif // __WXUNIVERSAL__
356 // position below or above the combobox: the width is 0 to put it exactly
357 // below us, not to the left or to the right
358 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
361 void wxPopupComboWindow::OnDismiss()
363 m_combo
->OnDismiss();
366 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
368 m_combo
->ProcessEvent(event
);
371 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
373 // ----------------------------------------------------------------------------
374 // wxPopupWindowHandler
375 // ----------------------------------------------------------------------------
377 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
379 // let the window have it first (we're the first event handler in the chain
380 // of handlers for this window)
381 if ( m_popup
->ProcessLeftDown(event
) )
386 wxPoint pos
= event
.GetPosition();
388 // scrollbar on which the click occured
389 wxWindow
*sbar
= NULL
;
391 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
393 switch ( win
->HitTest(pos
.x
, pos
.y
) )
395 case wxHT_WINDOW_OUTSIDE
:
397 // do the coords translation now as after DismissAndNotify()
398 // m_popup may be destroyed
399 wxMouseEvent
event2(event
);
401 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
403 // clicking outside a popup dismisses it
404 m_popup
->DismissAndNotify();
406 // dismissing a tooltip shouldn't waste a click, i.e. you
407 // should be able to dismiss it and press the button with the
408 // same click, so repost this event to the window beneath us
409 wxWindow
*win
= wxFindWindowAtPoint(event2
.GetPosition());
412 // translate the event coords to the ones of the window
413 // which is going to get the event
414 win
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
416 event2
.SetEventObject(win
);
417 wxPostEvent(win
, event2
);
422 #ifdef __WXUNIVERSAL__
423 case wxHT_WINDOW_HORZ_SCROLLBAR
:
424 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
427 case wxHT_WINDOW_VERT_SCROLLBAR
:
428 sbar
= win
->GetScrollbar(wxVERTICAL
);
433 // forgot to update the switch after adding a new hit test code?
434 wxFAIL_MSG( _T("unexpected HitTest() return value") );
437 case wxHT_WINDOW_CORNER
:
438 // don't actually know if this one is good for anything, but let it
441 case wxHT_WINDOW_INSIDE
:
442 // let the normal processing take place
449 // translate the event coordinates to the scrollbar ones
450 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
452 // and give the event to it
453 wxMouseEvent event2
= event
;
457 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
461 // ----------------------------------------------------------------------------
462 // wxPopupFocusHandler
463 // ----------------------------------------------------------------------------
465 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
467 // when we lose focus we always disappear - unless it goes to the popup (in
468 // which case we don't really lose it)
469 wxWindow
*win
= event
.GetWindow();
472 if ( win
== m_popup
)
474 win
= win
->GetParent();
477 m_popup
->DismissAndNotify();
480 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
482 // let the window have it first, it might process the keys
483 if ( !m_popup
->ProcessEvent(event
) )
485 // by default, dismiss the popup
486 m_popup
->DismissAndNotify();
490 #endif // wxUSE_POPUPWIN