checked in just John's patch, will keep working my fixes...
[wxWidgets.git] / src / common / popupcmn.cpp
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>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "popupwinbase.h"
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
31 #if wxUSE_POPUPWIN
32
33 #include "wx/popupwin.h"
34
35 #ifndef WX_PRECOMP
36 #include "wx/combobox.h" // wxComboControl
37 #include "wx/app.h" // wxPostEvent
38 #include "wx/log.h"
39 #include "wx/app.h"
40 #endif //WX_PRECOMP
41
42 #ifdef __WXUNIVERSAL__
43 #include "wx/univ/renderer.h"
44 #endif // __WXUNIVERSAL__
45
46 #ifdef __WXGTK__
47 #include <gtk/gtk.h>
48 #endif
49 #ifdef __WXX11__
50 #include "wx/x11/private.h"
51 #endif
52
53 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow)
54 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow, wxPopupWindow)
55
56 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
57 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow, wxPopupTransientWindow)
58 #endif
59
60 // ----------------------------------------------------------------------------
61 // private classes
62 // ----------------------------------------------------------------------------
63
64 // event handlers which we use to intercept events which cause the popup to
65 // disappear
66 class wxPopupWindowHandler : public wxEvtHandler
67 {
68 public:
69 wxPopupWindowHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
70 ~wxPopupWindowHandler();
71
72 protected:
73 // event handlers
74 void OnLeftDown(wxMouseEvent& event);
75
76 private:
77 wxPopupTransientWindow *m_popup;
78
79 DECLARE_EVENT_TABLE()
80 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler)
81 };
82
83 class wxPopupFocusHandler : public wxEvtHandler
84 {
85 public:
86 wxPopupFocusHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
87 ~wxPopupFocusHandler();
88
89 protected:
90 void OnKillFocus(wxFocusEvent& event);
91 void OnKeyDown(wxKeyEvent& event);
92
93 private:
94 wxPopupTransientWindow *m_popup;
95
96 DECLARE_EVENT_TABLE()
97 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler)
98 };
99
100 // ----------------------------------------------------------------------------
101 // event tables
102 // ----------------------------------------------------------------------------
103
104 BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler)
105 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown)
106 END_EVENT_TABLE()
107
108 BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler)
109 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus)
110 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown)
111 END_EVENT_TABLE()
112
113 // ============================================================================
114 // implementation
115 // ============================================================================
116
117 // ----------------------------------------------------------------------------
118 // wxPopupWindowBase
119 // ----------------------------------------------------------------------------
120
121 wxPopupWindowBase::~wxPopupWindowBase()
122 {
123 // this destructor is required for Darwin
124 }
125
126 bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
127 {
128 return true;
129 }
130
131 void 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
171 void wxPopupTransientWindow::Init()
172 {
173 m_child =
174 m_focus = (wxWindow *)NULL;
175
176 m_handlerFocus = NULL;
177 m_handlerPopup = NULL;
178 }
179
180 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style)
181 {
182 Init();
183
184 (void)Create(parent, style);
185 }
186
187 wxPopupTransientWindow::~wxPopupTransientWindow()
188 {
189 PopHandlers();
190 }
191
192 void wxPopupTransientWindow::PopHandlers()
193 {
194 if ( m_child )
195 {
196 if ( m_handlerPopup && !m_child->RemoveEventHandler(m_handlerPopup) )
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
203 m_child = NULL;
204 }
205
206 #ifdef __WXMSW__
207 if ( m_focus )
208 {
209 if ( m_handlerFocus && !m_focus->RemoveEventHandler(m_handlerFocus) )
210 {
211 // see above
212 m_handlerFocus = NULL;
213 }
214 }
215 #else
216 if ( m_handlerFocus && !RemoveEventHandler(m_handlerFocus) )
217 {
218 // see above
219 m_handlerFocus = NULL;
220 }
221 #endif
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
229 m_focus = NULL;
230 }
231
232 void 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
244 Show();
245
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
250 delete m_handlerPopup;
251 m_handlerPopup = new wxPopupWindowHandler(this);
252
253 m_child->PushEventHandler(m_handlerPopup);
254
255 m_focus = winFocus ? winFocus : this;
256 m_focus->SetFocus();
257
258 #ifdef __WXMSW__
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
262 m_focus = FindFocus();
263 if ( m_focus )
264 {
265 delete m_handlerFocus;
266 m_handlerFocus = new wxPopupFocusHandler(this);
267
268 m_focus->PushEventHandler(m_handlerFocus);
269 }
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__
277
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);
286 }
287
288 bool wxPopupTransientWindow::Show( bool show )
289 {
290 #ifdef __WXGTK__
291 if (!show)
292 {
293 gdk_pointer_ungrab( (guint32)GDK_CURRENT_TIME );
294
295 gtk_grab_remove( m_widget );
296 }
297 #endif
298
299 #ifdef __WXX11__
300 if (!show)
301 {
302 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
303 }
304 #endif
305
306 bool ret = wxPopupWindow::Show( show );
307
308 #ifdef __WXGTK__
309 if (show)
310 {
311 gtk_grab_add( m_widget );
312
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 }
323 #endif
324
325 #ifdef __WXX11__
326 if (show)
327 {
328 Window xwindow = (Window) m_clientWindow;
329
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
340 return ret;
341 }
342
343 void wxPopupTransientWindow::Dismiss()
344 {
345 PopHandlers();
346
347 Hide();
348 }
349
350 void wxPopupTransientWindow::DismissAndNotify()
351 {
352 Dismiss();
353
354 OnDismiss();
355 }
356
357 void wxPopupTransientWindow::OnDismiss()
358 {
359 // nothing to do here - but it may be interesting for derived class
360 }
361
362 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
363 {
364 // no special processing here
365 return false;
366 }
367
368 void 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
376 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
377
378 // ----------------------------------------------------------------------------
379 // wxPopupComboWindow
380 // ----------------------------------------------------------------------------
381
382 BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow)
383 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown)
384 END_EVENT_TABLE()
385
386 wxPopupComboWindow::wxPopupComboWindow(wxComboControl *parent)
387 : wxPopupTransientWindow(parent)
388 {
389 m_combo = parent;
390 }
391
392 bool wxPopupComboWindow::Create(wxComboControl *parent)
393 {
394 m_combo = parent;
395
396 return wxPopupWindow::Create(parent);
397 }
398
399 void wxPopupComboWindow::PositionNearCombo()
400 {
401 // the origin point must be in screen coords
402 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0,0));
403
404 #if 0 //def __WXUNIVERSAL__
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
418 void wxPopupComboWindow::OnDismiss()
419 {
420 m_combo->OnDismiss();
421 }
422
423 void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event)
424 {
425 m_combo->ProcessEvent(event);
426 }
427
428 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
429
430 // ----------------------------------------------------------------------------
431 // wxPopupWindowHandler
432 // ----------------------------------------------------------------------------
433 wxPopupWindowHandler::~wxPopupWindowHandler()
434 {
435 if (m_popup && (m_popup->m_handlerPopup == this))
436 m_popup->m_handlerPopup = NULL;
437 }
438
439 void 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 }
447
448 wxPoint pos = event.GetPosition();
449
450 // scrollbar on which the click occured
451 wxWindow *sbar = NULL;
452
453 wxWindow *win = (wxWindow *)event.GetEventObject();
454
455 switch ( win->HitTest(pos.x, pos.y) )
456 {
457 case wxHT_WINDOW_OUTSIDE:
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 }
482 break;
483
484 #ifdef __WXUNIVERSAL__
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;
492 #endif
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 // ----------------------------------------------------------------------------
526 wxPopupFocusHandler::~wxPopupFocusHandler()
527 {
528 if (m_popup && (m_popup->m_handlerFocus == this))
529 m_popup->m_handlerFocus = NULL;
530 }
531
532 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
533 {
534 // when we lose focus we always disappear - unless it goes to the popup (in
535 // which case we don't really lose it)
536 wxWindow *win = event.GetWindow();
537 while ( win )
538 {
539 if ( win == m_popup )
540 return;
541 win = win->GetParent();
542 }
543
544 m_popup->DismissAndNotify();
545 }
546
547 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event)
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
553 m_popup->DismissAndNotify();
554 }
555 }
556
557 #endif // wxUSE_POPUPWIN
558