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