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