]> git.saurik.com Git - wxWidgets.git/blame - src/common/popupcmn.cpp
use iterators in HtmlizeLinebreaks()
[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>
65571936 9// License: 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
1295f134
VZ
37#include "wx/recguard.h"
38
c02ee97f
VZ
39#ifdef __WXUNIVERSAL__
40 #include "wx/univ/renderer.h"
76fa9e02 41 #include "wx/scrolbar.h"
c02ee97f
VZ
42#endif // __WXUNIVERSAL__
43
537a0880
RR
44#ifdef __WXGTK__
45 #include <gtk/gtk.h>
46#endif
c51c4752
RR
47#ifdef __WXX11__
48#include "wx/x11/private.h"
49#endif
eb729cd3 50
537a0880 51IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow)
6c3422e9 52IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow, wxPopupWindow)
761df41e
VZ
53
54#if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
55 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow, wxPopupTransientWindow)
56#endif
6c3422e9 57
c02ee97f
VZ
58// ----------------------------------------------------------------------------
59// private classes
60// ----------------------------------------------------------------------------
61
62// event handlers which we use to intercept events which cause the popup to
63// disappear
64class wxPopupWindowHandler : public wxEvtHandler
65{
66public:
276612f7 67 wxPopupWindowHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
c02ee97f
VZ
68
69protected:
70 // event handlers
71 void OnLeftDown(wxMouseEvent& event);
72
73private:
74 wxPopupTransientWindow *m_popup;
75
76 DECLARE_EVENT_TABLE()
22f3361e 77 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler)
c02ee97f
VZ
78};
79
80class wxPopupFocusHandler : public wxEvtHandler
81{
82public:
276612f7 83 wxPopupFocusHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
c02ee97f
VZ
84
85protected:
c02ee97f 86 void OnKillFocus(wxFocusEvent& event);
d7cff34d 87 void OnKeyDown(wxKeyEvent& event);
c02ee97f
VZ
88
89private:
90 wxPopupTransientWindow *m_popup;
91
92 DECLARE_EVENT_TABLE()
22f3361e 93 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler)
c02ee97f
VZ
94};
95
96// ----------------------------------------------------------------------------
97// event tables
98// ----------------------------------------------------------------------------
99
100BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler)
101 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown)
102END_EVENT_TABLE()
103
104BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler)
105 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus)
d7cff34d 106 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown)
c02ee97f
VZ
107END_EVENT_TABLE()
108
414f2513 109BEGIN_EVENT_TABLE(wxPopupTransientWindow, wxPopupWindow)
9a890937 110#if defined( __WXMSW__ ) || defined( __WXMAC__)
414f2513
RD
111 EVT_IDLE(wxPopupTransientWindow::OnIdle)
112#endif
113END_EVENT_TABLE()
114
c02ee97f
VZ
115// ============================================================================
116// implementation
117// ============================================================================
118
119// ----------------------------------------------------------------------------
120// wxPopupWindowBase
121// ----------------------------------------------------------------------------
122
799ea011
GD
123wxPopupWindowBase::~wxPopupWindowBase()
124{
125 // this destructor is required for Darwin
126}
127
c02ee97f
VZ
128bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
129{
7e548f6b 130 return true;
c02ee97f
VZ
131}
132
133void 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
d2aa263f
VZ
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
c02ee97f
VZ
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
184void wxPopupTransientWindow::Init()
185{
186 m_child =
187 m_focus = (wxWindow *)NULL;
d7cff34d
VZ
188
189 m_handlerFocus = NULL;
190 m_handlerPopup = NULL;
c02ee97f
VZ
191}
192
758bce95 193wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style)
c02ee97f
VZ
194{
195 Init();
196
758bce95 197 (void)Create(parent, style);
c02ee97f
VZ
198}
199
200wxPopupTransientWindow::~wxPopupTransientWindow()
201{
0bf16170
MW
202 if (m_handlerPopup && m_handlerPopup->GetNextHandler())
203 PopHandlers();
17a1ebd1 204
0bf16170
MW
205 wxASSERT(!m_handlerFocus || !m_handlerFocus->GetNextHandler());
206 wxASSERT(!m_handlerPopup || !m_handlerPopup->GetNextHandler());
207
208 delete m_handlerFocus;
209 delete m_handlerPopup;
c02ee97f
VZ
210}
211
212void wxPopupTransientWindow::PopHandlers()
213{
214 if ( m_child )
215 {
0bf16170 216 if ( !m_child->RemoveEventHandler(m_handlerPopup) )
d7cff34d
VZ
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 }
414f2513 222 if (m_child->HasCapture())
17a1ebd1 223 {
414f2513
RD
224 m_child->ReleaseMouse();
225 }
c02ee97f
VZ
226 m_child = NULL;
227 }
228
229 if ( m_focus )
230 {
0bf16170 231 if ( !m_focus->RemoveEventHandler(m_handlerFocus) )
d7cff34d
VZ
232 {
233 // see above
234 m_handlerFocus = NULL;
235 }
c02ee97f 236 }
537a0880 237 m_focus = NULL;
c02ee97f
VZ
238}
239
240void 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
e4606ed9 252 Show();
e4606ed9 253
0bf16170
MW
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());
276612f7 257
0bf16170
MW
258 if (!m_handlerPopup)
259 m_handlerPopup = new wxPopupWindowHandler(this);
d7cff34d 260
d7cff34d 261 m_child->PushEventHandler(m_handlerPopup);
c02ee97f 262
c02ee97f 263 m_focus = winFocus ? winFocus : this;
c02ee97f 264 m_focus->SetFocus();
60178eb4 265
9a890937 266#if defined( __WXMSW__ ) || defined( __WXMAC__)
d7cff34d
VZ
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
516cdd54 270 m_focus = FindFocus();
8c624a14 271#elif defined(__WXGTK__)
0bf16170
MW
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
60178eb4
VZ
277 if ( m_focus )
278 {
0bf16170
MW
279 if (!m_handlerFocus)
280 m_handlerFocus = new wxPopupFocusHandler(this);
d7cff34d
VZ
281
282 m_focus->PushEventHandler(m_handlerFocus);
60178eb4 283 }
537a0880
RR
284}
285
286bool wxPopupTransientWindow::Show( bool show )
287{
288#ifdef __WXGTK__
289 if (!show)
f4bb632c
RR
290 {
291 gdk_pointer_ungrab( (guint32)GDK_CURRENT_TIME );
2997ca30 292
537a0880 293 gtk_grab_remove( m_widget );
f4bb632c 294 }
537a0880
RR
295#endif
296
c51c4752
RR
297#ifdef __WXX11__
298 if (!show)
299 {
300 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
301 }
302#endif
303
9a890937 304#if defined( __WXMSW__ ) || defined( __WXMAC__)
414f2513 305 if (!show && m_child && m_child->HasCapture())
17a1ebd1 306 {
414f2513
RD
307 m_child->ReleaseMouse();
308 }
309#endif
17a1ebd1 310
537a0880 311 bool ret = wxPopupWindow::Show( show );
2997ca30 312
537a0880
RR
313#ifdef __WXGTK__
314 if (show)
f4bb632c 315 {
537a0880 316 gtk_grab_add( m_widget );
2997ca30 317
f4bb632c
RR
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 }
537a0880
RR
328#endif
329
c51c4752
RR
330#ifdef __WXX11__
331 if (show)
332 {
333 Window xwindow = (Window) m_clientWindow;
2997ca30 334
c51c4752
RR
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
414f2513 345
9a890937 346#if defined( __WXMSW__ ) || defined( __WXMAC__)
414f2513
RD
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
537a0880 354 return ret;
c02ee97f
VZ
355}
356
357void wxPopupTransientWindow::Dismiss()
358{
c02ee97f 359 Hide();
414f2513 360 PopHandlers();
c02ee97f
VZ
361}
362
363void wxPopupTransientWindow::DismissAndNotify()
364{
365 Dismiss();
c02ee97f
VZ
366 OnDismiss();
367}
368
369void wxPopupTransientWindow::OnDismiss()
370{
371 // nothing to do here - but it may be interesting for derived class
372}
373
374bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
375{
376 // no special processing here
7e548f6b 377 return false;
c02ee97f
VZ
378}
379
9a890937 380#if defined( __WXMSW__ ) || defined( __WXMAC__)
414f2513
RD
381void wxPopupTransientWindow::OnIdle(wxIdleEvent& event)
382{
383 event.Skip();
384
385 if (IsShown() && m_child)
386 {
387 wxPoint pos = ScreenToClient(wxGetMousePosition());
80797568 388 wxRect rect(GetSize());
414f2513 389
22a35096 390 if ( rect.Contains(pos) )
414f2513
RD
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 }
17a1ebd1 403 }
414f2513
RD
404 }
405}
17a1ebd1 406#endif // __WXMSW__
414f2513
RD
407
408
a8132cd4 409#if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
410
411// ----------------------------------------------------------------------------
412// wxPopupComboWindow
413// ----------------------------------------------------------------------------
414
e0c83227
VZ
415BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow)
416 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown)
417END_EVENT_TABLE()
418
a57d600f 419wxPopupComboWindow::wxPopupComboWindow(wxComboCtrl *parent)
c02ee97f
VZ
420 : wxPopupTransientWindow(parent)
421{
422 m_combo = parent;
423}
424
a57d600f 425bool wxPopupComboWindow::Create(wxComboCtrl *parent)
c02ee97f
VZ
426{
427 m_combo = parent;
428
429 return wxPopupWindow::Create(parent);
430}
431
432void wxPopupComboWindow::PositionNearCombo()
433{
434 // the origin point must be in screen coords
c47addef 435 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0,0));
c02ee97f 436
89e7a223 437#if 0 //def __WXUNIVERSAL__
c02ee97f
VZ
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
451void wxPopupComboWindow::OnDismiss()
452{
a340b80d 453 m_combo->OnPopupDismiss();
c02ee97f
VZ
454}
455
e0c83227
VZ
456void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event)
457{
458 m_combo->ProcessEvent(event);
459}
460
a8132cd4 461#endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
462
463// ----------------------------------------------------------------------------
464// wxPopupWindowHandler
465// ----------------------------------------------------------------------------
466
467void 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 }
326c7ea8 475
c02ee97f
VZ
476 wxPoint pos = event.GetPosition();
477
af5c81b3 478 // in non-Univ ports the system manages scrollbars for us
9a6384ca 479#if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
3103e8a9 480 // scrollbar on which the click occurred
c02ee97f 481 wxWindow *sbar = NULL;
9a6384ca 482#endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
c02ee97f
VZ
483
484 wxWindow *win = (wxWindow *)event.GetEventObject();
326c7ea8 485
c02ee97f
VZ
486 switch ( win->HitTest(pos.x, pos.y) )
487 {
488 case wxHT_WINDOW_OUTSIDE:
326c7ea8
VZ
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
17a1ebd1
VZ
502 wxWindow *winUnder = wxFindWindowAtPoint(event2.GetPosition());
503 if ( winUnder )
326c7ea8
VZ
504 {
505 // translate the event coords to the ones of the window
506 // which is going to get the event
17a1ebd1 507 winUnder->ScreenToClient(&event2.m_x, &event2.m_y);
326c7ea8 508
17a1ebd1
VZ
509 event2.SetEventObject(winUnder);
510 wxPostEvent(winUnder, event2);
326c7ea8
VZ
511 }
512 }
c02ee97f
VZ
513 break;
514
9a6384ca 515#if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
c02ee97f
VZ
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;
9a6384ca 523#endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
c02ee97f
VZ
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
9a6384ca 540#if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
c02ee97f
VZ
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 }
9a6384ca 553#endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
c02ee97f
VZ
554}
555
556// ----------------------------------------------------------------------------
557// wxPopupFocusHandler
558// ----------------------------------------------------------------------------
559
560void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
561{
60178eb4
VZ
562 // when we lose focus we always disappear - unless it goes to the popup (in
563 // which case we don't really lose it)
36a56c65
VS
564 wxWindow *win = event.GetWindow();
565 while ( win )
566 {
567 if ( win == m_popup )
568 return;
569 win = win->GetParent();
570 }
326c7ea8 571
36a56c65 572 m_popup->DismissAndNotify();
60178eb4 573}
54800df8 574
d7cff34d 575void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event)
60178eb4 576{
1295f134
VZ
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
60178eb4 587 // let the window have it first, it might process the keys
06077aaf 588 if ( !m_popup->GetEventHandler()->ProcessEvent(event) )
60178eb4
VZ
589 {
590 // by default, dismiss the popup
54800df8 591 m_popup->DismissAndNotify();
60178eb4 592 }
c02ee97f
VZ
593}
594
595#endif // wxUSE_POPUPWIN