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