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 m_child
->ReleaseMouse();
208 if ( m_handlerFocus
&& !m_focus
->RemoveEventHandler(m_handlerFocus
) )
211 m_handlerFocus
= NULL
;
215 if ( m_handlerFocus
&& !RemoveEventHandler(m_handlerFocus
) )
218 m_handlerFocus
= NULL
;
222 // delete the handlers, they'll be created as necessary in Popup()
223 delete m_handlerPopup
;
224 m_handlerPopup
= NULL
;
225 delete m_handlerFocus
;
226 m_handlerFocus
= NULL
;
231 void wxPopupTransientWindow::Popup(wxWindow
*winFocus
)
233 const wxWindowList
& children
= GetChildren();
234 if ( children
.GetCount() )
236 m_child
= children
.GetFirst()->GetData();
245 delete m_handlerPopup
;
246 m_handlerPopup
= new wxPopupWindowHandler(this);
248 // m_child->CaptureMouse();
249 m_child
->PushEventHandler(m_handlerPopup
);
251 m_focus
= winFocus
? winFocus
: this;
255 // MSW doesn't allow to set focus to the popup window, but we need to
256 // subclass the window which has the focus, and not winFocus passed in or
257 // otherwise everything else breaks down
258 m_focus
= FindFocus();
261 delete m_handlerFocus
;
262 m_handlerFocus
= new wxPopupFocusHandler(this);
264 m_focus
->PushEventHandler(m_handlerFocus
);
267 // GTK+ catches the activate events from the popup
268 // window, not the focus events from the child window
269 delete m_handlerFocus
;
270 m_handlerFocus
= new wxPopupFocusHandler(this);
271 PushEventHandler(m_handlerFocus
);
276 bool wxPopupTransientWindow::Show( bool show
)
280 gtk_grab_remove( m_widget
);
283 bool ret
= wxPopupWindow::Show( show
);
287 gtk_grab_add( m_widget
);
293 void wxPopupTransientWindow::Dismiss()
300 void wxPopupTransientWindow::DismissAndNotify()
307 void wxPopupTransientWindow::OnDismiss()
309 // nothing to do here - but it may be interesting for derived class
312 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent
& WXUNUSED(event
))
314 // no special processing here
318 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
320 // ----------------------------------------------------------------------------
321 // wxPopupComboWindow
322 // ----------------------------------------------------------------------------
324 BEGIN_EVENT_TABLE(wxPopupComboWindow
, wxPopupTransientWindow
)
325 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown
)
328 wxPopupComboWindow::wxPopupComboWindow(wxComboControl
*parent
)
329 : wxPopupTransientWindow(parent
)
334 bool wxPopupComboWindow::Create(wxComboControl
*parent
)
338 return wxPopupWindow::Create(parent
);
341 void wxPopupComboWindow::PositionNearCombo()
343 // the origin point must be in screen coords
344 wxPoint ptOrigin
= m_combo
->ClientToScreen(wxPoint(0, 0));
346 #if 0 //def __WXUNIVERSAL__
347 // account for the fact that (0, 0) is not the top left corner of the
348 // window: there is also the border
349 wxRect rectBorders
= m_combo
->GetRenderer()->
350 GetBorderDimensions(m_combo
->GetBorder());
351 ptOrigin
.x
-= rectBorders
.x
;
352 ptOrigin
.y
-= rectBorders
.y
;
353 #endif // __WXUNIVERSAL__
355 // position below or above the combobox: the width is 0 to put it exactly
356 // below us, not to the left or to the right
357 Position(ptOrigin
, wxSize(0, m_combo
->GetSize().y
));
360 void wxPopupComboWindow::OnDismiss()
362 m_combo
->OnDismiss();
365 void wxPopupComboWindow::OnKeyDown(wxKeyEvent
& event
)
367 m_combo
->ProcessEvent(event
);
370 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
372 // ----------------------------------------------------------------------------
373 // wxPopupWindowHandler
374 // ----------------------------------------------------------------------------
376 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent
& event
)
378 // let the window have it first (we're the first event handler in the chain
379 // of handlers for this window)
380 if ( m_popup
->ProcessLeftDown(event
) )
385 wxPoint pos
= event
.GetPosition();
387 // scrollbar on which the click occured
388 wxWindow
*sbar
= NULL
;
390 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
392 switch ( win
->HitTest(pos
.x
, pos
.y
) )
394 case wxHT_WINDOW_OUTSIDE
:
396 // do the coords translation now as after DismissAndNotify()
397 // m_popup may be destroyed
398 wxMouseEvent
event2(event
);
400 m_popup
->ClientToScreen(&event2
.m_x
, &event2
.m_y
);
402 // clicking outside a popup dismisses it
403 m_popup
->DismissAndNotify();
405 // dismissing a tooltip shouldn't waste a click, i.e. you
406 // should be able to dismiss it and press the button with the
407 // same click, so repost this event to the window beneath us
408 wxWindow
*win
= wxFindWindowAtPoint(event2
.GetPosition());
411 // translate the event coords to the ones of the window
412 // which is going to get the event
413 win
->ScreenToClient(&event2
.m_x
, &event2
.m_y
);
415 event2
.SetEventObject(win
);
416 wxPostEvent(win
, event2
);
421 #ifdef __WXUNIVERSAL__
422 case wxHT_WINDOW_HORZ_SCROLLBAR
:
423 sbar
= win
->GetScrollbar(wxHORIZONTAL
);
426 case wxHT_WINDOW_VERT_SCROLLBAR
:
427 sbar
= win
->GetScrollbar(wxVERTICAL
);
432 // forgot to update the switch after adding a new hit test code?
433 wxFAIL_MSG( _T("unexpected HitTest() return value") );
436 case wxHT_WINDOW_CORNER
:
437 // don't actually know if this one is good for anything, but let it
440 case wxHT_WINDOW_INSIDE
:
441 // let the normal processing take place
448 // translate the event coordinates to the scrollbar ones
449 pos
= sbar
->ScreenToClient(win
->ClientToScreen(pos
));
451 // and give the event to it
452 wxMouseEvent event2
= event
;
456 (void)sbar
->GetEventHandler()->ProcessEvent(event2
);
460 // ----------------------------------------------------------------------------
461 // wxPopupFocusHandler
462 // ----------------------------------------------------------------------------
464 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent
& event
)
466 // when we lose focus we always disappear - unless it goes to the popup (in
467 // which case we don't really lose it)
468 wxWindow
*win
= event
.GetWindow();
471 if ( win
== m_popup
)
473 win
= win
->GetParent();
476 m_popup
->DismissAndNotify();
479 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent
& event
)
481 // let the window have it first, it might process the keys
482 if ( !m_popup
->ProcessEvent(event
) )
484 // by default, dismiss the popup
485 m_popup
->DismissAndNotify();
489 #endif // wxUSE_POPUPWIN