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