]> git.saurik.com Git - wxWidgets.git/blame - src/common/popupcmn.cpp
Ensure we pass the correct type of string to EscapeFileNameCharsInURL in wxWebViewArc...
[wxWidgets.git] / src / common / popupcmn.cpp
CommitLineData
c02ee97f 1///////////////////////////////////////////////////////////////////////////////
670f9935 2// Name: src/common/popupcmn.cpp
c02ee97f
VZ
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>
526954c5 9// Licence: wxWindows licence
c02ee97f
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c02ee97f
VZ
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
833a51f6 27#if wxUSE_POPUPWIN
c02ee97f
VZ
28
29#include "wx/popupwin.h"
30
31#ifndef WX_PRECOMP
a57d600f 32 #include "wx/combobox.h" // wxComboCtrl
6a0fab3a 33 #include "wx/app.h" // wxPostEvent
ee351013 34 #include "wx/log.h"
c02ee97f
VZ
35#endif //WX_PRECOMP
36
2ac51e16 37#include "wx/display.h"
1295f134
VZ
38#include "wx/recguard.h"
39
c02ee97f
VZ
40#ifdef __WXUNIVERSAL__
41 #include "wx/univ/renderer.h"
76fa9e02 42 #include "wx/scrolbar.h"
c02ee97f
VZ
43#endif // __WXUNIVERSAL__
44
537a0880
RR
45#ifdef __WXGTK__
46 #include <gtk/gtk.h>
04e40451
PC
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
ac52d2b0
VZ
52#elif defined(__WXMSW__)
53 #include "wx/msw/private.h"
54#elif defined(__WXX11__)
55 #include "wx/x11/private.h"
c51c4752 56#endif
eb729cd3 57
537a0880 58IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow)
6c3422e9 59IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow, wxPopupWindow)
761df41e
VZ
60
61#if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
62 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow, wxPopupTransientWindow)
63#endif
6c3422e9 64
c02ee97f
VZ
65// ----------------------------------------------------------------------------
66// private classes
67// ----------------------------------------------------------------------------
68
69// event handlers which we use to intercept events which cause the popup to
70// disappear
71class wxPopupWindowHandler : public wxEvtHandler
72{
73public:
276612f7 74 wxPopupWindowHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
c02ee97f
VZ
75
76protected:
77 // event handlers
78 void OnLeftDown(wxMouseEvent& event);
79
80private:
81 wxPopupTransientWindow *m_popup;
82
83 DECLARE_EVENT_TABLE()
c0c133e1 84 wxDECLARE_NO_COPY_CLASS(wxPopupWindowHandler);
c02ee97f
VZ
85};
86
87class wxPopupFocusHandler : public wxEvtHandler
88{
89public:
276612f7 90 wxPopupFocusHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
c02ee97f
VZ
91
92protected:
c02ee97f 93 void OnKillFocus(wxFocusEvent& event);
ffe42ca6 94 void OnChar(wxKeyEvent& event);
c02ee97f
VZ
95
96private:
97 wxPopupTransientWindow *m_popup;
98
99 DECLARE_EVENT_TABLE()
c0c133e1 100 wxDECLARE_NO_COPY_CLASS(wxPopupFocusHandler);
c02ee97f
VZ
101};
102
103// ----------------------------------------------------------------------------
104// event tables
105// ----------------------------------------------------------------------------
106
107BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler)
108 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown)
109END_EVENT_TABLE()
110
111BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler)
112 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus)
ffe42ca6 113 EVT_CHAR(wxPopupFocusHandler::OnChar)
c02ee97f
VZ
114END_EVENT_TABLE()
115
414f2513 116BEGIN_EVENT_TABLE(wxPopupTransientWindow, wxPopupWindow)
bd233387 117#if defined( __WXMSW__ ) || ( defined( __WXMAC__ ) && wxOSX_USE_CARBON )
414f2513
RD
118 EVT_IDLE(wxPopupTransientWindow::OnIdle)
119#endif
120END_EVENT_TABLE()
121
c02ee97f
VZ
122// ============================================================================
123// implementation
124// ============================================================================
125
126// ----------------------------------------------------------------------------
127// wxPopupWindowBase
128// ----------------------------------------------------------------------------
129
799ea011
GD
130wxPopupWindowBase::~wxPopupWindowBase()
131{
132 // this destructor is required for Darwin
133}
134
c02ee97f
VZ
135bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
136{
7e548f6b 137 return true;
c02ee97f
VZ
138}
139
140void wxPopupWindowBase::Position(const wxPoint& ptOrigin,
141 const wxSize& size)
142{
2ac51e16
VZ
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();
c02ee97f
VZ
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;
247f310d 167 if ( y + sizeSelf.y > posScreen.y + sizeScreen.y )
c02ee97f
VZ
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
d2aa263f 179 wxCoord x = ptOrigin.x;
2ac51e16 180
d2aa263f
VZ
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
2ac51e16 190
247f310d 191 if ( x + sizeSelf.x > posScreen.x + sizeScreen.x )
c02ee97f
VZ
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
209void wxPopupTransientWindow::Init()
210{
211 m_child =
d3b9f782 212 m_focus = NULL;
d7cff34d
VZ
213
214 m_handlerFocus = NULL;
215 m_handlerPopup = NULL;
c02ee97f
VZ
216}
217
758bce95 218wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style)
c02ee97f
VZ
219{
220 Init();
221
758bce95 222 (void)Create(parent, style);
c02ee97f
VZ
223}
224
225wxPopupTransientWindow::~wxPopupTransientWindow()
226{
0bf16170
MW
227 if (m_handlerPopup && m_handlerPopup->GetNextHandler())
228 PopHandlers();
17a1ebd1 229
0bf16170
MW
230 wxASSERT(!m_handlerFocus || !m_handlerFocus->GetNextHandler());
231 wxASSERT(!m_handlerPopup || !m_handlerPopup->GetNextHandler());
232
233 delete m_handlerFocus;
234 delete m_handlerPopup;
c02ee97f
VZ
235}
236
237void wxPopupTransientWindow::PopHandlers()
238{
239 if ( m_child )
240 {
0bf16170 241 if ( !m_child->RemoveEventHandler(m_handlerPopup) )
d7cff34d
VZ
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 }
414f2513 247 if (m_child->HasCapture())
17a1ebd1 248 {
414f2513
RD
249 m_child->ReleaseMouse();
250 }
c02ee97f
VZ
251 m_child = NULL;
252 }
253
254 if ( m_focus )
255 {
0bf16170 256 if ( !m_focus->RemoveEventHandler(m_handlerFocus) )
d7cff34d
VZ
257 {
258 // see above
259 m_handlerFocus = NULL;
260 }
c02ee97f 261 }
537a0880 262 m_focus = NULL;
c02ee97f
VZ
263}
264
265void 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
e4606ed9 277 Show();
e4606ed9 278
c9737636 279 // There is a problem if these are still in use
0bf16170
MW
280 wxASSERT(!m_handlerFocus || !m_handlerFocus->GetNextHandler());
281 wxASSERT(!m_handlerPopup || !m_handlerPopup->GetNextHandler());
276612f7 282
0bf16170
MW
283 if (!m_handlerPopup)
284 m_handlerPopup = new wxPopupWindowHandler(this);
d7cff34d 285
d7cff34d 286 m_child->PushEventHandler(m_handlerPopup);
c02ee97f 287
582699e1
JS
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 }
60178eb4 298
09a9eb20 299#if defined( __WXMSW__ ) || (defined( __WXMAC__) && wxOSX_USE_CARBON)
d7cff34d
VZ
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
516cdd54 303 m_focus = FindFocus();
8c624a14 304#elif defined(__WXGTK__)
0bf16170
MW
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
60178eb4
VZ
310 if ( m_focus )
311 {
0bf16170
MW
312 if (!m_handlerFocus)
313 m_handlerFocus = new wxPopupFocusHandler(this);
d7cff34d
VZ
314
315 m_focus->PushEventHandler(m_handlerFocus);
60178eb4 316 }
537a0880
RR
317}
318
319bool wxPopupTransientWindow::Show( bool show )
320{
321#ifdef __WXGTK__
322 if (!show)
f4bb632c
RR
323 {
324 gdk_pointer_ungrab( (guint32)GDK_CURRENT_TIME );
2997ca30 325
537a0880 326 gtk_grab_remove( m_widget );
f4bb632c 327 }
537a0880
RR
328#endif
329
c51c4752
RR
330#ifdef __WXX11__
331 if (!show)
332 {
333 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
334 }
335#endif
336
9a890937 337#if defined( __WXMSW__ ) || defined( __WXMAC__)
414f2513 338 if (!show && m_child && m_child->HasCapture())
17a1ebd1 339 {
414f2513
RD
340 m_child->ReleaseMouse();
341 }
342#endif
17a1ebd1 343
537a0880 344 bool ret = wxPopupWindow::Show( show );
2997ca30 345
537a0880
RR
346#ifdef __WXGTK__
347 if (show)
f4bb632c 348 {
537a0880 349 gtk_grab_add( m_widget );
2997ca30 350
385e8575 351 gdk_pointer_grab( gtk_widget_get_window(m_widget), true,
f4bb632c
RR
352 (GdkEventMask)
353 (GDK_BUTTON_PRESS_MASK |
354 GDK_BUTTON_RELEASE_MASK |
355 GDK_POINTER_MOTION_HINT_MASK |
356 GDK_POINTER_MOTION_MASK),
d3b9f782
VZ
357 NULL,
358 NULL,
f4bb632c
RR
359 (guint32)GDK_CURRENT_TIME );
360 }
537a0880
RR
361#endif
362
c51c4752
RR
363#ifdef __WXX11__
364 if (show)
365 {
366 Window xwindow = (Window) m_clientWindow;
2997ca30 367
c51c4752
RR
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
414f2513 378
9a890937 379#if defined( __WXMSW__ ) || defined( __WXMAC__)
414f2513
RD
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
537a0880 387 return ret;
c02ee97f
VZ
388}
389
390void wxPopupTransientWindow::Dismiss()
391{
c02ee97f 392 Hide();
414f2513 393 PopHandlers();
c02ee97f
VZ
394}
395
396void wxPopupTransientWindow::DismissAndNotify()
397{
398 Dismiss();
c02ee97f
VZ
399 OnDismiss();
400}
401
402void wxPopupTransientWindow::OnDismiss()
403{
404 // nothing to do here - but it may be interesting for derived class
405}
406
407bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
408{
409 // no special processing here
7e548f6b 410 return false;
c02ee97f
VZ
411}
412
bd233387 413#if defined( __WXMSW__ ) || ( defined( __WXMAC__ ) && wxOSX_USE_CARBON )
414f2513
RD
414void wxPopupTransientWindow::OnIdle(wxIdleEvent& event)
415{
416 event.Skip();
417
418 if (IsShown() && m_child)
419 {
420 wxPoint pos = ScreenToClient(wxGetMousePosition());
80797568 421 wxRect rect(GetSize());
414f2513 422
22a35096 423 if ( rect.Contains(pos) )
414f2513
RD
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 }
17a1ebd1 436 }
414f2513
RD
437 }
438}
17a1ebd1 439#endif // __WXMSW__
414f2513
RD
440
441
a8132cd4 442#if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
443
444// ----------------------------------------------------------------------------
445// wxPopupComboWindow
446// ----------------------------------------------------------------------------
447
e0c83227
VZ
448BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow)
449 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown)
450END_EVENT_TABLE()
451
a57d600f 452wxPopupComboWindow::wxPopupComboWindow(wxComboCtrl *parent)
c02ee97f
VZ
453 : wxPopupTransientWindow(parent)
454{
455 m_combo = parent;
456}
457
a57d600f 458bool wxPopupComboWindow::Create(wxComboCtrl *parent)
c02ee97f
VZ
459{
460 m_combo = parent;
461
462 return wxPopupWindow::Create(parent);
463}
464
465void wxPopupComboWindow::PositionNearCombo()
466{
467 // the origin point must be in screen coords
c47addef 468 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0,0));
c02ee97f 469
89e7a223 470#if 0 //def __WXUNIVERSAL__
c02ee97f
VZ
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
484void wxPopupComboWindow::OnDismiss()
485{
5d8a7943 486 m_combo->OnPopupDismiss(true);
c02ee97f
VZ
487}
488
e0c83227
VZ
489void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event)
490{
3b7fa206 491 m_combo->ProcessWindowEvent(event);
e0c83227
VZ
492}
493
a8132cd4 494#endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
495
496// ----------------------------------------------------------------------------
497// wxPopupWindowHandler
498// ----------------------------------------------------------------------------
499
500void 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 }
326c7ea8 508
c02ee97f
VZ
509 wxPoint pos = event.GetPosition();
510
af5c81b3 511 // in non-Univ ports the system manages scrollbars for us
9a6384ca 512#if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
3103e8a9 513 // scrollbar on which the click occurred
c02ee97f 514 wxWindow *sbar = NULL;
9a6384ca 515#endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
c02ee97f
VZ
516
517 wxWindow *win = (wxWindow *)event.GetEventObject();
326c7ea8 518
c02ee97f
VZ
519 switch ( win->HitTest(pos.x, pos.y) )
520 {
521 case wxHT_WINDOW_OUTSIDE:
326c7ea8
VZ
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
17a1ebd1
VZ
535 wxWindow *winUnder = wxFindWindowAtPoint(event2.GetPosition());
536 if ( winUnder )
326c7ea8
VZ
537 {
538 // translate the event coords to the ones of the window
539 // which is going to get the event
17a1ebd1 540 winUnder->ScreenToClient(&event2.m_x, &event2.m_y);
326c7ea8 541
17a1ebd1 542 event2.SetEventObject(winUnder);
6aab9279 543 wxPostEvent(winUnder->GetEventHandler(), event2);
326c7ea8
VZ
544 }
545 }
c02ee97f
VZ
546 break;
547
9a6384ca 548#if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
c02ee97f
VZ
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;
9a6384ca 556#endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
c02ee97f
VZ
557
558 default:
559 // forgot to update the switch after adding a new hit test code?
9a83f860 560 wxFAIL_MSG( wxT("unexpected HitTest() return value") );
c02ee97f
VZ
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
9a6384ca 573#if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
c02ee97f
VZ
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 }
9a6384ca 586#endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
c02ee97f
VZ
587}
588
589// ----------------------------------------------------------------------------
590// wxPopupFocusHandler
591// ----------------------------------------------------------------------------
592
593void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
594{
60178eb4
VZ
595 // when we lose focus we always disappear - unless it goes to the popup (in
596 // which case we don't really lose it)
36a56c65
VS
597 wxWindow *win = event.GetWindow();
598 while ( win )
599 {
600 if ( win == m_popup )
601 return;
602 win = win->GetParent();
603 }
326c7ea8 604
36a56c65 605 m_popup->DismissAndNotify();
60178eb4 606}
54800df8 607
ffe42ca6 608void wxPopupFocusHandler::OnChar(wxKeyEvent& event)
60178eb4 609{
1295f134
VZ
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
60178eb4 620 // let the window have it first, it might process the keys
06077aaf 621 if ( !m_popup->GetEventHandler()->ProcessEvent(event) )
60178eb4
VZ
622 {
623 // by default, dismiss the popup
54800df8 624 m_popup->DismissAndNotify();
60178eb4 625 }
c02ee97f
VZ
626}
627
628#endif // wxUSE_POPUPWIN