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