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