]> git.saurik.com Git - wxWidgets.git/blame - src/common/popupcmn.cpp
Fixed doxygen warnings regarding the XRC File Format overview using <> tags in sectio...
[wxWidgets.git] / src / common / popupcmn.cpp
CommitLineData
c02ee97f 1///////////////////////////////////////////////////////////////////////////////
670f9935 2// Name: src/common/popupcmn.cpp
c02ee97f
VZ
3// Purpose: implementation of wxPopupTransientWindow
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 06.01.01
7// RCS-ID: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// License: wxWindows licence
c02ee97f
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c02ee97f
VZ
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
833a51f6 27#if wxUSE_POPUPWIN
c02ee97f
VZ
28
29#include "wx/popupwin.h"
30
31#ifndef WX_PRECOMP
a57d600f 32 #include "wx/combobox.h" // wxComboCtrl
6a0fab3a 33 #include "wx/app.h" // wxPostEvent
ee351013 34 #include "wx/log.h"
c02ee97f
VZ
35#endif //WX_PRECOMP
36
1295f134
VZ
37#include "wx/recguard.h"
38
c02ee97f
VZ
39#ifdef __WXUNIVERSAL__
40 #include "wx/univ/renderer.h"
76fa9e02 41 #include "wx/scrolbar.h"
c02ee97f
VZ
42#endif // __WXUNIVERSAL__
43
537a0880
RR
44#ifdef __WXGTK__
45 #include <gtk/gtk.h>
46#endif
c51c4752
RR
47#ifdef __WXX11__
48#include "wx/x11/private.h"
49#endif
eb729cd3 50
537a0880 51IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow)
6c3422e9 52IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow, wxPopupWindow)
761df41e
VZ
53
54#if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
55 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow, wxPopupTransientWindow)
56#endif
6c3422e9 57
c02ee97f
VZ
58// ----------------------------------------------------------------------------
59// private classes
60// ----------------------------------------------------------------------------
61
62// event handlers which we use to intercept events which cause the popup to
63// disappear
64class wxPopupWindowHandler : public wxEvtHandler
65{
66public:
276612f7 67 wxPopupWindowHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
c02ee97f
VZ
68
69protected:
70 // event handlers
71 void OnLeftDown(wxMouseEvent& event);
72
73private:
74 wxPopupTransientWindow *m_popup;
75
76 DECLARE_EVENT_TABLE()
22f3361e 77 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler)
c02ee97f
VZ
78};
79
80class wxPopupFocusHandler : public wxEvtHandler
81{
82public:
276612f7 83 wxPopupFocusHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
c02ee97f
VZ
84
85protected:
c02ee97f 86 void OnKillFocus(wxFocusEvent& event);
d7cff34d 87 void OnKeyDown(wxKeyEvent& event);
c02ee97f
VZ
88
89private:
90 wxPopupTransientWindow *m_popup;
91
92 DECLARE_EVENT_TABLE()
22f3361e 93 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler)
c02ee97f
VZ
94};
95
96// ----------------------------------------------------------------------------
97// event tables
98// ----------------------------------------------------------------------------
99
100BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler)
101 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown)
102END_EVENT_TABLE()
103
104BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler)
105 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus)
d7cff34d 106 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown)
c02ee97f
VZ
107END_EVENT_TABLE()
108
414f2513 109BEGIN_EVENT_TABLE(wxPopupTransientWindow, wxPopupWindow)
9a890937 110#if defined( __WXMSW__ ) || defined( __WXMAC__)
414f2513
RD
111 EVT_IDLE(wxPopupTransientWindow::OnIdle)
112#endif
113END_EVENT_TABLE()
114
c02ee97f
VZ
115// ============================================================================
116// implementation
117// ============================================================================
118
119// ----------------------------------------------------------------------------
120// wxPopupWindowBase
121// ----------------------------------------------------------------------------
122
799ea011
GD
123wxPopupWindowBase::~wxPopupWindowBase()
124{
125 // this destructor is required for Darwin
126}
127
c02ee97f
VZ
128bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
129{
7e548f6b 130 return true;
c02ee97f
VZ
131}
132
133void wxPopupWindowBase::Position(const wxPoint& ptOrigin,
134 const wxSize& size)
135{
136 wxSize sizeScreen = wxGetDisplaySize(),
137 sizeSelf = GetSize();
138
139 // is there enough space to put the popup below the window (where we put it
140 // by default)?
141 wxCoord y = ptOrigin.y + size.y;
142 if ( y + sizeSelf.y > sizeScreen.y )
143 {
144 // check if there is enough space above
145 if ( ptOrigin.y > sizeSelf.y )
146 {
147 // do position the control above the window
148 y -= size.y + sizeSelf.y;
149 }
150 //else: not enough space below nor above, leave below
151 }
152
153 // now check left/right too
d2aa263f
VZ
154 wxCoord x = ptOrigin.x;
155
156 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
157 {
158 // shift the window to the left instead of the right.
159 x -= size.x;
160 x -= sizeSelf.x; // also shift it by window width.
161 }
162 else
163 x += size.x;
164
165
c02ee97f
VZ
166 if ( x + sizeSelf.x > sizeScreen.x )
167 {
168 // check if there is enough space to the left
169 if ( ptOrigin.x > sizeSelf.x )
170 {
171 // do position the control to the left
172 x -= size.x + sizeSelf.x;
173 }
174 //else: not enough space there neither, leave in default position
175 }
176
177 Move(x, y, wxSIZE_NO_ADJUSTMENTS);
178}
179
180// ----------------------------------------------------------------------------
181// wxPopupTransientWindow
182// ----------------------------------------------------------------------------
183
184void wxPopupTransientWindow::Init()
185{
186 m_child =
187 m_focus = (wxWindow *)NULL;
d7cff34d
VZ
188
189 m_handlerFocus = NULL;
190 m_handlerPopup = NULL;
c02ee97f
VZ
191}
192
758bce95 193wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style)
c02ee97f
VZ
194{
195 Init();
196
758bce95 197 (void)Create(parent, style);
c02ee97f
VZ
198}
199
200wxPopupTransientWindow::~wxPopupTransientWindow()
201{
0bf16170
MW
202 if (m_handlerPopup && m_handlerPopup->GetNextHandler())
203 PopHandlers();
17a1ebd1 204
0bf16170
MW
205 wxASSERT(!m_handlerFocus || !m_handlerFocus->GetNextHandler());
206 wxASSERT(!m_handlerPopup || !m_handlerPopup->GetNextHandler());
207
208 delete m_handlerFocus;
209 delete m_handlerPopup;
c02ee97f
VZ
210}
211
212void wxPopupTransientWindow::PopHandlers()
213{
214 if ( m_child )
215 {
0bf16170 216 if ( !m_child->RemoveEventHandler(m_handlerPopup) )
d7cff34d
VZ
217 {
218 // something is very wrong and someone else probably deleted our
219 // handler - so don't risk deleting it second time
220 m_handlerPopup = NULL;
221 }
414f2513 222 if (m_child->HasCapture())
17a1ebd1 223 {
414f2513
RD
224 m_child->ReleaseMouse();
225 }
c02ee97f
VZ
226 m_child = NULL;
227 }
228
229 if ( m_focus )
230 {
0bf16170 231 if ( !m_focus->RemoveEventHandler(m_handlerFocus) )
d7cff34d
VZ
232 {
233 // see above
234 m_handlerFocus = NULL;
235 }
c02ee97f 236 }
537a0880 237 m_focus = NULL;
c02ee97f
VZ
238}
239
240void wxPopupTransientWindow::Popup(wxWindow *winFocus)
241{
242 const wxWindowList& children = GetChildren();
243 if ( children.GetCount() )
244 {
245 m_child = children.GetFirst()->GetData();
246 }
247 else
248 {
249 m_child = this;
250 }
251
e4606ed9 252 Show();
e4606ed9 253
0bf16170
MW
254 // There is is a problem if these are still in use
255 wxASSERT(!m_handlerFocus || !m_handlerFocus->GetNextHandler());
256 wxASSERT(!m_handlerPopup || !m_handlerPopup->GetNextHandler());
276612f7 257
0bf16170
MW
258 if (!m_handlerPopup)
259 m_handlerPopup = new wxPopupWindowHandler(this);
d7cff34d 260
d7cff34d 261 m_child->PushEventHandler(m_handlerPopup);
c02ee97f 262
582699e1
JS
263#if defined(__WXMSW__)
264 // Focusing on child of popup window does not work on MSW unless WS_POPUP
265 // style is set. We do not even want to try to set the focus, as it may
266 // provoke errors on some Windows versions (Vista and later).
267 if ( ::GetWindowLong(GetHwnd(), GWL_STYLE) & WS_POPUP )
268#endif
269 {
270 m_focus = winFocus ? winFocus : this;
271 m_focus->SetFocus();
272 }
60178eb4 273
9a890937 274#if defined( __WXMSW__ ) || defined( __WXMAC__)
d7cff34d
VZ
275 // MSW doesn't allow to set focus to the popup window, but we need to
276 // subclass the window which has the focus, and not winFocus passed in or
277 // otherwise everything else breaks down
516cdd54 278 m_focus = FindFocus();
8c624a14 279#elif defined(__WXGTK__)
0bf16170
MW
280 // GTK+ catches the activate events from the popup
281 // window, not the focus events from the child window
282 m_focus = this;
283#endif
284
60178eb4
VZ
285 if ( m_focus )
286 {
0bf16170
MW
287 if (!m_handlerFocus)
288 m_handlerFocus = new wxPopupFocusHandler(this);
d7cff34d
VZ
289
290 m_focus->PushEventHandler(m_handlerFocus);
60178eb4 291 }
537a0880
RR
292}
293
294bool wxPopupTransientWindow::Show( bool show )
295{
296#ifdef __WXGTK__
297 if (!show)
f4bb632c
RR
298 {
299 gdk_pointer_ungrab( (guint32)GDK_CURRENT_TIME );
2997ca30 300
537a0880 301 gtk_grab_remove( m_widget );
f4bb632c 302 }
537a0880
RR
303#endif
304
c51c4752
RR
305#ifdef __WXX11__
306 if (!show)
307 {
308 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
309 }
310#endif
311
9a890937 312#if defined( __WXMSW__ ) || defined( __WXMAC__)
414f2513 313 if (!show && m_child && m_child->HasCapture())
17a1ebd1 314 {
414f2513
RD
315 m_child->ReleaseMouse();
316 }
317#endif
17a1ebd1 318
537a0880 319 bool ret = wxPopupWindow::Show( show );
2997ca30 320
537a0880
RR
321#ifdef __WXGTK__
322 if (show)
f4bb632c 323 {
537a0880 324 gtk_grab_add( m_widget );
2997ca30 325
f4bb632c
RR
326 gdk_pointer_grab( m_widget->window, TRUE,
327 (GdkEventMask)
328 (GDK_BUTTON_PRESS_MASK |
329 GDK_BUTTON_RELEASE_MASK |
330 GDK_POINTER_MOTION_HINT_MASK |
331 GDK_POINTER_MOTION_MASK),
332 (GdkWindow *) NULL,
333 (GdkCursor *) NULL,
334 (guint32)GDK_CURRENT_TIME );
335 }
537a0880
RR
336#endif
337
c51c4752
RR
338#ifdef __WXX11__
339 if (show)
340 {
341 Window xwindow = (Window) m_clientWindow;
2997ca30 342
c51c4752
RR
343 /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow,
344 True,
345 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
346 GrabModeAsync,
347 GrabModeAsync,
348 None,
349 None,
350 CurrentTime );
351 }
352#endif
414f2513 353
9a890937 354#if defined( __WXMSW__ ) || defined( __WXMAC__)
414f2513
RD
355 if (show && m_child)
356 {
357 // Assume that the mouse is outside the popup to begin with
358 m_child->CaptureMouse();
359 }
360#endif
361
537a0880 362 return ret;
c02ee97f
VZ
363}
364
365void wxPopupTransientWindow::Dismiss()
366{
c02ee97f 367 Hide();
414f2513 368 PopHandlers();
c02ee97f
VZ
369}
370
371void wxPopupTransientWindow::DismissAndNotify()
372{
373 Dismiss();
c02ee97f
VZ
374 OnDismiss();
375}
376
377void wxPopupTransientWindow::OnDismiss()
378{
379 // nothing to do here - but it may be interesting for derived class
380}
381
382bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
383{
384 // no special processing here
7e548f6b 385 return false;
c02ee97f
VZ
386}
387
9a890937 388#if defined( __WXMSW__ ) || defined( __WXMAC__)
414f2513
RD
389void wxPopupTransientWindow::OnIdle(wxIdleEvent& event)
390{
391 event.Skip();
392
393 if (IsShown() && m_child)
394 {
395 wxPoint pos = ScreenToClient(wxGetMousePosition());
80797568 396 wxRect rect(GetSize());
414f2513 397
22a35096 398 if ( rect.Contains(pos) )
414f2513
RD
399 {
400 if ( m_child->HasCapture() )
401 {
402 m_child->ReleaseMouse();
403 }
404 }
405 else
406 {
407 if ( !m_child->HasCapture() )
408 {
409 m_child->CaptureMouse();
410 }
17a1ebd1 411 }
414f2513
RD
412 }
413}
17a1ebd1 414#endif // __WXMSW__
414f2513
RD
415
416
a8132cd4 417#if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
418
419// ----------------------------------------------------------------------------
420// wxPopupComboWindow
421// ----------------------------------------------------------------------------
422
e0c83227
VZ
423BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow)
424 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown)
425END_EVENT_TABLE()
426
a57d600f 427wxPopupComboWindow::wxPopupComboWindow(wxComboCtrl *parent)
c02ee97f
VZ
428 : wxPopupTransientWindow(parent)
429{
430 m_combo = parent;
431}
432
a57d600f 433bool wxPopupComboWindow::Create(wxComboCtrl *parent)
c02ee97f
VZ
434{
435 m_combo = parent;
436
437 return wxPopupWindow::Create(parent);
438}
439
440void wxPopupComboWindow::PositionNearCombo()
441{
442 // the origin point must be in screen coords
c47addef 443 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0,0));
c02ee97f 444
89e7a223 445#if 0 //def __WXUNIVERSAL__
c02ee97f
VZ
446 // account for the fact that (0, 0) is not the top left corner of the
447 // window: there is also the border
448 wxRect rectBorders = m_combo->GetRenderer()->
449 GetBorderDimensions(m_combo->GetBorder());
450 ptOrigin.x -= rectBorders.x;
451 ptOrigin.y -= rectBorders.y;
452#endif // __WXUNIVERSAL__
453
454 // position below or above the combobox: the width is 0 to put it exactly
455 // below us, not to the left or to the right
456 Position(ptOrigin, wxSize(0, m_combo->GetSize().y));
457}
458
459void wxPopupComboWindow::OnDismiss()
460{
a340b80d 461 m_combo->OnPopupDismiss();
c02ee97f
VZ
462}
463
e0c83227
VZ
464void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event)
465{
466 m_combo->ProcessEvent(event);
467}
468
a8132cd4 469#endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
470
471// ----------------------------------------------------------------------------
472// wxPopupWindowHandler
473// ----------------------------------------------------------------------------
474
475void wxPopupWindowHandler::OnLeftDown(wxMouseEvent& event)
476{
477 // let the window have it first (we're the first event handler in the chain
478 // of handlers for this window)
479 if ( m_popup->ProcessLeftDown(event) )
480 {
481 return;
482 }
326c7ea8 483
c02ee97f
VZ
484 wxPoint pos = event.GetPosition();
485
af5c81b3 486 // in non-Univ ports the system manages scrollbars for us
9a6384ca 487#if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
3103e8a9 488 // scrollbar on which the click occurred
c02ee97f 489 wxWindow *sbar = NULL;
9a6384ca 490#endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
c02ee97f
VZ
491
492 wxWindow *win = (wxWindow *)event.GetEventObject();
326c7ea8 493
c02ee97f
VZ
494 switch ( win->HitTest(pos.x, pos.y) )
495 {
496 case wxHT_WINDOW_OUTSIDE:
326c7ea8
VZ
497 {
498 // do the coords translation now as after DismissAndNotify()
499 // m_popup may be destroyed
500 wxMouseEvent event2(event);
501
502 m_popup->ClientToScreen(&event2.m_x, &event2.m_y);
503
504 // clicking outside a popup dismisses it
505 m_popup->DismissAndNotify();
506
507 // dismissing a tooltip shouldn't waste a click, i.e. you
508 // should be able to dismiss it and press the button with the
509 // same click, so repost this event to the window beneath us
17a1ebd1
VZ
510 wxWindow *winUnder = wxFindWindowAtPoint(event2.GetPosition());
511 if ( winUnder )
326c7ea8
VZ
512 {
513 // translate the event coords to the ones of the window
514 // which is going to get the event
17a1ebd1 515 winUnder->ScreenToClient(&event2.m_x, &event2.m_y);
326c7ea8 516
17a1ebd1 517 event2.SetEventObject(winUnder);
6aab9279 518 wxPostEvent(winUnder->GetEventHandler(), event2);
326c7ea8
VZ
519 }
520 }
c02ee97f
VZ
521 break;
522
9a6384ca 523#if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
c02ee97f
VZ
524 case wxHT_WINDOW_HORZ_SCROLLBAR:
525 sbar = win->GetScrollbar(wxHORIZONTAL);
526 break;
527
528 case wxHT_WINDOW_VERT_SCROLLBAR:
529 sbar = win->GetScrollbar(wxVERTICAL);
530 break;
9a6384ca 531#endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
c02ee97f
VZ
532
533 default:
534 // forgot to update the switch after adding a new hit test code?
535 wxFAIL_MSG( _T("unexpected HitTest() return value") );
536 // fall through
537
538 case wxHT_WINDOW_CORNER:
539 // don't actually know if this one is good for anything, but let it
540 // pass just in case
541
542 case wxHT_WINDOW_INSIDE:
543 // let the normal processing take place
544 event.Skip();
545 break;
546 }
547
9a6384ca 548#if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
c02ee97f
VZ
549 if ( sbar )
550 {
551 // translate the event coordinates to the scrollbar ones
552 pos = sbar->ScreenToClient(win->ClientToScreen(pos));
553
554 // and give the event to it
555 wxMouseEvent event2 = event;
556 event2.m_x = pos.x;
557 event2.m_y = pos.y;
558
559 (void)sbar->GetEventHandler()->ProcessEvent(event2);
560 }
9a6384ca 561#endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
c02ee97f
VZ
562}
563
564// ----------------------------------------------------------------------------
565// wxPopupFocusHandler
566// ----------------------------------------------------------------------------
567
568void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
569{
60178eb4
VZ
570 // when we lose focus we always disappear - unless it goes to the popup (in
571 // which case we don't really lose it)
36a56c65
VS
572 wxWindow *win = event.GetWindow();
573 while ( win )
574 {
575 if ( win == m_popup )
576 return;
577 win = win->GetParent();
578 }
326c7ea8 579
36a56c65 580 m_popup->DismissAndNotify();
60178eb4 581}
54800df8 582
d7cff34d 583void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event)
60178eb4 584{
1295f134
VZ
585 // we can be associated with the popup itself in which case we should avoid
586 // infinite recursion
587 static int s_inside;
588 wxRecursionGuard guard(s_inside);
589 if ( guard.IsInside() )
590 {
591 event.Skip();
592 return;
593 }
594
60178eb4 595 // let the window have it first, it might process the keys
06077aaf 596 if ( !m_popup->GetEventHandler()->ProcessEvent(event) )
60178eb4
VZ
597 {
598 // by default, dismiss the popup
54800df8 599 m_popup->DismissAndNotify();
60178eb4 600 }
c02ee97f
VZ
601}
602
603#endif // wxUSE_POPUPWIN