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