]> git.saurik.com Git - wxWidgets.git/blob - src/common/popupcmn.cpp
Simpler constructor available.
[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 BEGIN_EVENT_TABLE(wxPopupTransientWindow, wxPopupWindow)
114 #ifdef __WXMSW__
115 EVT_IDLE(wxPopupTransientWindow::OnIdle)
116 #endif
117 END_EVENT_TABLE()
118
119 // ============================================================================
120 // implementation
121 // ============================================================================
122
123 // ----------------------------------------------------------------------------
124 // wxPopupWindowBase
125 // ----------------------------------------------------------------------------
126
127 wxPopupWindowBase::~wxPopupWindowBase()
128 {
129 // this destructor is required for Darwin
130 }
131
132 bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
133 {
134 return true;
135 }
136
137 void 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
177 void wxPopupTransientWindow::Init()
178 {
179 m_child =
180 m_focus = (wxWindow *)NULL;
181
182 m_handlerFocus = NULL;
183 m_handlerPopup = NULL;
184 }
185
186 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style)
187 {
188 Init();
189
190 (void)Create(parent, style);
191 }
192
193 wxPopupTransientWindow::~wxPopupTransientWindow()
194 {
195 PopHandlers();
196 }
197
198 void wxPopupTransientWindow::PopHandlers()
199 {
200 if ( m_child )
201 {
202 if ( m_handlerPopup && !m_child->RemoveEventHandler(m_handlerPopup) )
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 }
208 if (m_child->HasCapture())
209 {
210 m_child->ReleaseMouse();
211 }
212 m_child = NULL;
213 }
214
215 #ifdef __WXMSW__
216 if ( m_focus )
217 {
218 if ( m_handlerFocus && !m_focus->RemoveEventHandler(m_handlerFocus) )
219 {
220 // see above
221 m_handlerFocus = NULL;
222 }
223 }
224 #else
225 if ( m_handlerFocus && !RemoveEventHandler(m_handlerFocus) )
226 {
227 // see above
228 m_handlerFocus = NULL;
229 }
230 #endif
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
238 m_focus = NULL;
239 }
240
241 void 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
253 Show();
254
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
259 delete m_handlerPopup;
260 m_handlerPopup = new wxPopupWindowHandler(this);
261
262 m_child->PushEventHandler(m_handlerPopup);
263
264 m_focus = winFocus ? winFocus : this;
265 m_focus->SetFocus();
266
267 #ifdef __WXMSW__
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
271 m_focus = FindFocus();
272 if ( m_focus )
273 {
274 delete m_handlerFocus;
275 m_handlerFocus = new wxPopupFocusHandler(this);
276
277 m_focus->PushEventHandler(m_handlerFocus);
278 }
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__
286
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);
292 m_focus->Connect(wxEVT_DESTROY,
293 wxWindowDestroyEventHandler(wxPopupTransientWindow::OnDestroy),
294 NULL, this);
295 }
296
297 bool wxPopupTransientWindow::Show( bool show )
298 {
299 #ifdef __WXGTK__
300 if (!show)
301 {
302 gdk_pointer_ungrab( (guint32)GDK_CURRENT_TIME );
303
304 gtk_grab_remove( m_widget );
305 }
306 #endif
307
308 #ifdef __WXX11__
309 if (!show)
310 {
311 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
312 }
313 #endif
314
315 #ifdef __WXMSW__
316 if (!show && m_child && m_child->HasCapture())
317 {
318 m_child->ReleaseMouse();
319 }
320 #endif
321
322 bool ret = wxPopupWindow::Show( show );
323
324 #ifdef __WXGTK__
325 if (show)
326 {
327 gtk_grab_add( m_widget );
328
329 gdk_pointer_grab( m_widget->window, TRUE,
330 (GdkEventMask)
331 (GDK_BUTTON_PRESS_MASK |
332 GDK_BUTTON_RELEASE_MASK |
333 GDK_POINTER_MOTION_HINT_MASK |
334 GDK_POINTER_MOTION_MASK),
335 (GdkWindow *) NULL,
336 (GdkCursor *) NULL,
337 (guint32)GDK_CURRENT_TIME );
338 }
339 #endif
340
341 #ifdef __WXX11__
342 if (show)
343 {
344 Window xwindow = (Window) m_clientWindow;
345
346 /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow,
347 True,
348 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
349 GrabModeAsync,
350 GrabModeAsync,
351 None,
352 None,
353 CurrentTime );
354 }
355 #endif
356
357 #ifdef __WXMSW__
358 if (show && m_child)
359 {
360 // Assume that the mouse is outside the popup to begin with
361 m_child->CaptureMouse();
362 }
363 #endif
364
365 return ret;
366 }
367
368 void wxPopupTransientWindow::Dismiss()
369 {
370 Hide();
371 PopHandlers();
372 }
373
374 void wxPopupTransientWindow::DismissAndNotify()
375 {
376 Dismiss();
377 OnDismiss();
378 }
379
380 void wxPopupTransientWindow::OnDismiss()
381 {
382 // nothing to do here - but it may be interesting for derived class
383 }
384
385 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
386 {
387 // no special processing here
388 return false;
389 }
390
391 void wxPopupTransientWindow::OnDestroy(wxWindowDestroyEvent& event)
392 {
393 if (event.GetEventObject() == m_child)
394 m_child = NULL;
395 if (event.GetEventObject() == m_focus)
396 m_focus = NULL;
397 }
398
399 #ifdef __WXMSW__
400 void wxPopupTransientWindow::OnIdle(wxIdleEvent& event)
401 {
402 event.Skip();
403
404 if (IsShown() && m_child)
405 {
406 wxPoint pos = ScreenToClient(wxGetMousePosition());
407 wxRect rect(GetSize());
408
409 if ( rect.Inside(pos) )
410 {
411 if ( m_child->HasCapture() )
412 {
413 m_child->ReleaseMouse();
414 }
415 }
416 else
417 {
418 if ( !m_child->HasCapture() )
419 {
420 m_child->CaptureMouse();
421 }
422 }
423 }
424 }
425 #endif
426
427
428 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
429
430 // ----------------------------------------------------------------------------
431 // wxPopupComboWindow
432 // ----------------------------------------------------------------------------
433
434 BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow)
435 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown)
436 END_EVENT_TABLE()
437
438 wxPopupComboWindow::wxPopupComboWindow(wxComboControl *parent)
439 : wxPopupTransientWindow(parent)
440 {
441 m_combo = parent;
442 }
443
444 bool wxPopupComboWindow::Create(wxComboControl *parent)
445 {
446 m_combo = parent;
447
448 return wxPopupWindow::Create(parent);
449 }
450
451 void wxPopupComboWindow::PositionNearCombo()
452 {
453 // the origin point must be in screen coords
454 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0,0));
455
456 #if 0 //def __WXUNIVERSAL__
457 // account for the fact that (0, 0) is not the top left corner of the
458 // window: there is also the border
459 wxRect rectBorders = m_combo->GetRenderer()->
460 GetBorderDimensions(m_combo->GetBorder());
461 ptOrigin.x -= rectBorders.x;
462 ptOrigin.y -= rectBorders.y;
463 #endif // __WXUNIVERSAL__
464
465 // position below or above the combobox: the width is 0 to put it exactly
466 // below us, not to the left or to the right
467 Position(ptOrigin, wxSize(0, m_combo->GetSize().y));
468 }
469
470 void wxPopupComboWindow::OnDismiss()
471 {
472 m_combo->OnDismiss();
473 }
474
475 void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event)
476 {
477 m_combo->ProcessEvent(event);
478 }
479
480 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
481
482 // ----------------------------------------------------------------------------
483 // wxPopupWindowHandler
484 // ----------------------------------------------------------------------------
485 wxPopupWindowHandler::~wxPopupWindowHandler()
486 {
487 if (m_popup && (m_popup->m_handlerPopup == this))
488 m_popup->m_handlerPopup = NULL;
489 }
490
491 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent& event)
492 {
493 // let the window have it first (we're the first event handler in the chain
494 // of handlers for this window)
495 if ( m_popup->ProcessLeftDown(event) )
496 {
497 return;
498 }
499
500 wxPoint pos = event.GetPosition();
501
502 // scrollbar on which the click occured
503 wxWindow *sbar = NULL;
504
505 wxWindow *win = (wxWindow *)event.GetEventObject();
506
507 switch ( win->HitTest(pos.x, pos.y) )
508 {
509 case wxHT_WINDOW_OUTSIDE:
510 {
511 // do the coords translation now as after DismissAndNotify()
512 // m_popup may be destroyed
513 wxMouseEvent event2(event);
514
515 m_popup->ClientToScreen(&event2.m_x, &event2.m_y);
516
517 // clicking outside a popup dismisses it
518 m_popup->DismissAndNotify();
519
520 // dismissing a tooltip shouldn't waste a click, i.e. you
521 // should be able to dismiss it and press the button with the
522 // same click, so repost this event to the window beneath us
523 wxWindow *win = wxFindWindowAtPoint(event2.GetPosition());
524 if ( win )
525 {
526 // translate the event coords to the ones of the window
527 // which is going to get the event
528 win->ScreenToClient(&event2.m_x, &event2.m_y);
529
530 event2.SetEventObject(win);
531 wxPostEvent(win, event2);
532 }
533 }
534 break;
535
536 #ifdef __WXUNIVERSAL__
537 case wxHT_WINDOW_HORZ_SCROLLBAR:
538 sbar = win->GetScrollbar(wxHORIZONTAL);
539 break;
540
541 case wxHT_WINDOW_VERT_SCROLLBAR:
542 sbar = win->GetScrollbar(wxVERTICAL);
543 break;
544 #endif
545
546 default:
547 // forgot to update the switch after adding a new hit test code?
548 wxFAIL_MSG( _T("unexpected HitTest() return value") );
549 // fall through
550
551 case wxHT_WINDOW_CORNER:
552 // don't actually know if this one is good for anything, but let it
553 // pass just in case
554
555 case wxHT_WINDOW_INSIDE:
556 // let the normal processing take place
557 event.Skip();
558 break;
559 }
560
561 if ( sbar )
562 {
563 // translate the event coordinates to the scrollbar ones
564 pos = sbar->ScreenToClient(win->ClientToScreen(pos));
565
566 // and give the event to it
567 wxMouseEvent event2 = event;
568 event2.m_x = pos.x;
569 event2.m_y = pos.y;
570
571 (void)sbar->GetEventHandler()->ProcessEvent(event2);
572 }
573 }
574
575 // ----------------------------------------------------------------------------
576 // wxPopupFocusHandler
577 // ----------------------------------------------------------------------------
578 wxPopupFocusHandler::~wxPopupFocusHandler()
579 {
580 if (m_popup && (m_popup->m_handlerFocus == this))
581 m_popup->m_handlerFocus = NULL;
582 }
583
584 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
585 {
586 // when we lose focus we always disappear - unless it goes to the popup (in
587 // which case we don't really lose it)
588 wxWindow *win = event.GetWindow();
589 while ( win )
590 {
591 if ( win == m_popup )
592 return;
593 win = win->GetParent();
594 }
595
596 m_popup->DismissAndNotify();
597 }
598
599 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event)
600 {
601 // let the window have it first, it might process the keys
602 if ( !m_popup->ProcessEvent(event) )
603 {
604 // by default, dismiss the popup
605 m_popup->DismissAndNotify();
606 }
607 }
608
609 #endif // wxUSE_POPUPWIN
610