1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/generic/scrlwing.cpp
3 // Purpose:     wxScrolledWindow implementation
4 // Author:      Julian Smart
5 // Modified by: Vadim Zeitlin on 31.08.00: wxScrollHelper allows to implement.
6 //              Ron Lee on 10.4.02:  virtual size / auto scrollbars et al.
7 // Created:     01/02/97
8 // Copyright:   (c) wxWidgets team
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 
24 #include "wx/scrolwin.h"
25 
26 #ifndef WX_PRECOMP
27     #include "wx/utils.h"
28     #include "wx/panel.h"
29     #include "wx/dcclient.h"
30     #include "wx/timer.h"
31     #include "wx/sizer.h"
32     #include "wx/settings.h"
33 #endif
34 
35 #ifdef __WXMAC__
36 #include "wx/scrolbar.h"
37 #endif
38 
39 #ifdef __WXMSW__
40     #include <windows.h> // for DLGC_WANTARROWS
41     #include "wx/msw/winundef.h"
42 #endif
43 
44 #ifdef __WXMOTIF__
45 // For wxRETAINED implementation
46 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
47                //This code switches off the compiler warnings
48 # pragma message disable nosimpint
49 #endif
50 #include <Xm/Xm.h>
51 #ifdef __VMS__
52 # pragma message enable nosimpint
53 #endif
54 #endif
55 
56 /*
57     TODO PROPERTIES
58         style wxHSCROLL | wxVSCROLL
59 */
60 
61 // ----------------------------------------------------------------------------
62 // wxScrollHelperEvtHandler: intercept the events from the window and forward
63 // them to wxScrollHelper
64 // ----------------------------------------------------------------------------
65 
66 class WXDLLEXPORT wxScrollHelperEvtHandler : public wxEvtHandler
67 {
68 public:
wxScrollHelperEvtHandler(wxScrollHelperBase * scrollHelper)69     wxScrollHelperEvtHandler(wxScrollHelperBase *scrollHelper)
70     {
71         m_scrollHelper = scrollHelper;
72     }
73 
74     virtual bool ProcessEvent(wxEvent& event) wxOVERRIDE;
75 
76 private:
77     wxScrollHelperBase *m_scrollHelper;
78 
79     wxDECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler);
80 };
81 
82 #if wxUSE_TIMER
83 // ----------------------------------------------------------------------------
84 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
85 // a captured mouse is held outside the window
86 // ----------------------------------------------------------------------------
87 
88 class wxAutoScrollTimer : public wxTimer
89 {
90 public:
91     wxAutoScrollTimer(wxWindow *winToScroll,
92                       wxScrollHelperBase *scroll,
93                       wxEventType eventTypeToSend,
94                       int pos, int orient);
95 
96     virtual void Notify() wxOVERRIDE;
97 
98 private:
99     wxWindow *m_win;
100     wxScrollHelperBase *m_scrollHelper;
101     wxEventType m_eventType;
102     int m_pos,
103         m_orient;
104 
105     wxDECLARE_NO_COPY_CLASS(wxAutoScrollTimer);
106 };
107 
108 // ============================================================================
109 // implementation
110 // ============================================================================
111 
112 // ----------------------------------------------------------------------------
113 // wxAutoScrollTimer
114 // ----------------------------------------------------------------------------
115 
wxAutoScrollTimer(wxWindow * winToScroll,wxScrollHelperBase * scroll,wxEventType eventTypeToSend,int pos,int orient)116 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow *winToScroll,
117                                      wxScrollHelperBase *scroll,
118                                      wxEventType eventTypeToSend,
119                                      int pos, int orient)
120 {
121     m_win = winToScroll;
122     m_scrollHelper = scroll;
123     m_eventType = eventTypeToSend;
124     m_pos = pos;
125     m_orient = orient;
126 }
127 
Notify()128 void wxAutoScrollTimer::Notify()
129 {
130     // only do all this as long as the window is capturing the mouse
131     if ( wxWindow::GetCapture() != m_win )
132     {
133         Stop();
134     }
135     else // we still capture the mouse, continue generating events
136     {
137         // first scroll the window if we are allowed to do it
138         wxScrollWinEvent event1(m_eventType, m_pos, m_orient);
139         event1.SetEventObject(m_win);
140         event1.SetId(m_win->GetId());
141         if ( m_scrollHelper->SendAutoScrollEvents(event1) &&
142                 m_win->GetEventHandler()->ProcessEvent(event1) )
143         {
144             // and then send a pseudo mouse-move event to refresh the selection
145             wxMouseEvent event2(wxEVT_MOTION);
146             event2.SetPosition(wxGetMousePosition());
147 
148             // the mouse event coordinates should be client, not screen as
149             // returned by wxGetMousePosition
150             wxWindow *parentTop = m_win;
151             while ( parentTop->GetParent() )
152                 parentTop = parentTop->GetParent();
153             wxPoint ptOrig = parentTop->GetPosition();
154             event2.m_x -= ptOrig.x;
155             event2.m_y -= ptOrig.y;
156 
157             event2.SetEventObject(m_win);
158 
159             wxMouseState mouseState = wxGetMouseState();
160 
161             event2.m_leftDown = mouseState.LeftIsDown();
162             event2.m_middleDown = mouseState.MiddleIsDown();
163             event2.m_rightDown = mouseState.RightIsDown();
164 
165             event2.m_shiftDown = mouseState.ShiftDown();
166             event2.m_controlDown = mouseState.ControlDown();
167             event2.m_altDown = mouseState.AltDown();
168             event2.m_metaDown = mouseState.MetaDown();
169 
170             m_win->GetEventHandler()->ProcessEvent(event2);
171         }
172         else // can't scroll further, stop
173         {
174             Stop();
175         }
176     }
177 }
178 #endif
179 
180 // ----------------------------------------------------------------------------
181 // wxScrollHelperEvtHandler
182 // ----------------------------------------------------------------------------
183 
184 // Notice that this method is currently duplicated in the method with the same
185 // name in wxVarScrollHelperEvtHandler class, until this is fixed, the other
186 // copy of the method needs to be modified every time this version is.
ProcessEvent(wxEvent & event)187 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent& event)
188 {
189     wxEventType evType = event.GetEventType();
190 
191     // Pass it on to the real handler: notice that we must not call
192     // ProcessEvent() on this object itself as it wouldn't pass it to the next
193     // handler (i.e. the real window) if we're called from a previous handler
194     // (as indicated by "process here only" flag being set) and we do want to
195     // execute the handler defined in the window we're associated with right
196     // now, without waiting until TryAfter() is called from wxEvtHandler.
197     bool processed = m_nextHandler->ProcessEvent(event);
198 
199     // always process the size events ourselves, even if the user code handles
200     // them as well, as we need to AdjustScrollbars()
201     //
202     // NB: it is important to do it after processing the event in the normal
203     //     way as HandleOnSize() may generate a wxEVT_SIZE itself if the
204     //     scrollbar[s] (dis)appear and it should be seen by the user code
205     //     after this one
206     if ( evType == wxEVT_SIZE )
207     {
208         m_scrollHelper->HandleOnSize((wxSizeEvent &)event);
209         return true;
210     }
211 
212     // For wxEVT_PAINT the user code can either handle this event as usual or
213     // override virtual OnDraw(), so if the event hasn't been handled we need
214     // to call this virtual function ourselves.
215     if (
216 #ifndef __WXUNIVERSAL__
217           // in wxUniversal "processed" will always be true, because
218           // all windows use the paint event to draw themselves.
219           // In this case we can't use this flag to determine if a custom
220           // paint event handler already drew our window and we just
221           // call OnDraw() anyway.
222           !processed &&
223 #endif // !__WXUNIVERSAL__
224             evType == wxEVT_PAINT )
225     {
226         m_scrollHelper->HandleOnPaint((wxPaintEvent &)event);
227         return true;
228     }
229 
230     // If the user code handled this event, it should prevent the default
231     // handling from taking place, so don't do anything else in this case.
232     if ( processed )
233         return true;
234 
235     if ( evType == wxEVT_CHILD_FOCUS )
236     {
237         m_scrollHelper->HandleOnChildFocus((wxChildFocusEvent &)event);
238         return true;
239     }
240 
241     // reset the skipped flag (which might have been set to true in
242     // ProcessEvent() above) to be able to test it below
243     bool wasSkipped = event.GetSkipped();
244     if ( wasSkipped )
245         event.Skip(false);
246 
247     if ( evType == wxEVT_SCROLLWIN_TOP ||
248          evType == wxEVT_SCROLLWIN_BOTTOM ||
249          evType == wxEVT_SCROLLWIN_LINEUP ||
250          evType == wxEVT_SCROLLWIN_LINEDOWN ||
251          evType == wxEVT_SCROLLWIN_PAGEUP ||
252          evType == wxEVT_SCROLLWIN_PAGEDOWN ||
253          evType == wxEVT_SCROLLWIN_THUMBTRACK ||
254          evType == wxEVT_SCROLLWIN_THUMBRELEASE )
255     {
256         m_scrollHelper->HandleOnScroll((wxScrollWinEvent &)event);
257         if ( !event.GetSkipped() )
258         {
259             // it makes sense to indicate that we processed the message as we
260             // did scroll the window (and also notice that wxAutoScrollTimer
261             // relies on our return value to stop scrolling when we are at top
262             // or bottom already)
263             processed = true;
264             wasSkipped = false;
265         }
266     }
267 
268     if ( evType == wxEVT_ENTER_WINDOW )
269     {
270         m_scrollHelper->HandleOnMouseEnter((wxMouseEvent &)event);
271     }
272     else if ( evType == wxEVT_LEAVE_WINDOW )
273     {
274         m_scrollHelper->HandleOnMouseLeave((wxMouseEvent &)event);
275     }
276 #if wxUSE_MOUSEWHEEL
277     // Use GTK's own scroll wheel handling in GtkScrolledWindow
278 #ifndef __WXGTK20__
279     else if ( evType == wxEVT_MOUSEWHEEL )
280     {
281         m_scrollHelper->HandleOnMouseWheel((wxMouseEvent &)event);
282         return true;
283     }
284 #endif
285 #endif // wxUSE_MOUSEWHEEL
286     else if ( evType == wxEVT_CHAR )
287     {
288         m_scrollHelper->HandleOnChar((wxKeyEvent &)event);
289         if ( !event.GetSkipped() )
290         {
291             processed = true;
292             wasSkipped = false;
293         }
294     }
295 
296     event.Skip(wasSkipped);
297 
298     // We called ProcessEvent() on the next handler, meaning that we explicitly
299     // worked around the request to process the event in this handler only. As
300     // explained above, this is unfortunately really necessary but the trouble
301     // is that the event will continue to be post-processed by the previous
302     // handler resulting in duplicate calls to event handlers. Call the special
303     // function below to prevent this from happening, base class DoTryChain()
304     // will check for it and behave accordingly.
305     //
306     // And if we're not called from DoTryChain(), this won't do anything anyhow.
307     event.DidntHonourProcessOnlyIn();
308 
309     return processed;
310 }
311 
312 // ============================================================================
313 // wxAnyScrollHelperBase and wxScrollHelperBase implementation
314 // ============================================================================
315 
316 // ----------------------------------------------------------------------------
317 // wxAnyScrollHelperBase
318 // ----------------------------------------------------------------------------
319 
wxAnyScrollHelperBase(wxWindow * win)320 wxAnyScrollHelperBase::wxAnyScrollHelperBase(wxWindow* win)
321 {
322     wxASSERT_MSG( win, wxT("associated window can't be NULL in wxScrollHelper") );
323 
324     m_win = win;
325     m_targetWindow = NULL;
326 
327     m_kbdScrollingEnabled = true;
328 }
329 
330 // ----------------------------------------------------------------------------
331 // wxScrollHelperBase construction
332 // ----------------------------------------------------------------------------
333 
wxScrollHelperBase(wxWindow * win)334 wxScrollHelperBase::wxScrollHelperBase(wxWindow *win)
335     : wxAnyScrollHelperBase(win)
336 {
337     m_xScrollPixelsPerLine =
338     m_yScrollPixelsPerLine =
339     m_xScrollPosition =
340     m_yScrollPosition =
341     m_xScrollLines =
342     m_yScrollLines =
343     m_xScrollLinesPerPage =
344     m_yScrollLinesPerPage = 0;
345 
346     m_xScrollingEnabled =
347     m_yScrollingEnabled = true;
348 
349     m_scaleX =
350     m_scaleY = 1.0;
351 #if wxUSE_MOUSEWHEEL
352     m_wheelRotation = 0;
353 #endif
354 
355     m_timerAutoScroll = NULL;
356 
357     m_handler = NULL;
358 
359     m_win->SetScrollHelper(static_cast<wxScrollHelper *>(this));
360 
361     // by default, the associated window is also the target window
362     DoSetTargetWindow(win);
363 }
364 
~wxScrollHelperBase()365 wxScrollHelperBase::~wxScrollHelperBase()
366 {
367     StopAutoScrolling();
368 
369     DeleteEvtHandler();
370 }
371 
372 // ----------------------------------------------------------------------------
373 // setting scrolling parameters
374 // ----------------------------------------------------------------------------
375 
SetScrollbars(int pixelsPerUnitX,int pixelsPerUnitY,int noUnitsX,int noUnitsY,int xPos,int yPos,bool noRefresh)376 void wxScrollHelperBase::SetScrollbars(int pixelsPerUnitX,
377                                        int pixelsPerUnitY,
378                                        int noUnitsX,
379                                        int noUnitsY,
380                                        int xPos,
381                                        int yPos,
382                                        bool noRefresh)
383 {
384     // Convert positions expressed in scroll units to positions in pixels.
385     int xPosInPixels = (xPos + m_xScrollPosition)*m_xScrollPixelsPerLine,
386         yPosInPixels = (yPos + m_yScrollPosition)*m_yScrollPixelsPerLine;
387 
388     bool do_refresh =
389     (
390       (noUnitsX != 0 && m_xScrollLines == 0) ||
391       (noUnitsX < m_xScrollLines && xPosInPixels > pixelsPerUnitX * noUnitsX) ||
392 
393       (noUnitsY != 0 && m_yScrollLines == 0) ||
394       (noUnitsY < m_yScrollLines && yPosInPixels > pixelsPerUnitY * noUnitsY) ||
395       (xPos != m_xScrollPosition) ||
396       (yPos != m_yScrollPosition)
397     );
398 
399     m_xScrollPixelsPerLine = pixelsPerUnitX;
400     m_yScrollPixelsPerLine = pixelsPerUnitY;
401     m_xScrollPosition = xPos;
402     m_yScrollPosition = yPos;
403 
404     int w = noUnitsX * pixelsPerUnitX;
405     int h = noUnitsY * pixelsPerUnitY;
406 
407     // For better backward compatibility we set persisting limits
408     // here not just the size.  It makes SetScrollbars 'sticky'
409     // emulating the old non-autoscroll behaviour.
410     //   m_targetWindow->SetVirtualSizeHints( w, h );
411 
412     // The above should arguably be deprecated, this however we still need.
413 
414     // take care not to set 0 virtual size, 0 means that we don't have any
415     // scrollbars and hence we should use the real size instead of the virtual
416     // one which is indicated by using wxDefaultCoord
417     m_targetWindow->SetVirtualSize( w ? w : wxDefaultCoord,
418                                     h ? h : wxDefaultCoord);
419 
420     if (do_refresh && !noRefresh)
421         m_targetWindow->Refresh(true, GetScrollRect());
422 
423 #ifndef __WXUNIVERSAL__
424     // If the target is not the same as the window with the scrollbars,
425     // then we need to update the scrollbars here, since they won't have
426     // been updated by SetVirtualSize().
427     if ( m_targetWindow != m_win )
428 #endif // !__WXUNIVERSAL__
429     {
430         AdjustScrollbars();
431     }
432 #ifndef __WXUNIVERSAL__
433     else
434     {
435         // otherwise this has been done by AdjustScrollbars, above
436     }
437 #endif // !__WXUNIVERSAL__
438 }
439 
440 // ----------------------------------------------------------------------------
441 // [target] window handling
442 // ----------------------------------------------------------------------------
443 
DeleteEvtHandler()444 void wxScrollHelperBase::DeleteEvtHandler()
445 {
446     // search for m_handler in the handler list
447     if ( m_win && m_handler )
448     {
449         if ( m_win->RemoveEventHandler(m_handler) )
450         {
451             delete m_handler;
452         }
453         //else: something is very wrong, so better [maybe] leak memory than
454         //      risk a crash because of double deletion
455 
456         m_handler = NULL;
457     }
458 }
459 
DoSetTargetWindow(wxWindow * target)460 void wxScrollHelperBase::DoSetTargetWindow(wxWindow *target)
461 {
462     m_targetWindow = target;
463 #ifdef __WXMAC__
464     target->MacSetClipChildren( true ) ;
465 #endif
466 
467     // install the event handler which will intercept the events we're
468     // interested in (but only do it for our real window, not the target window
469     // which we scroll - we don't need to hijack its events)
470     if ( m_targetWindow == m_win )
471     {
472         // if we already have a handler, delete it first
473         DeleteEvtHandler();
474 
475         m_handler = new wxScrollHelperEvtHandler(this);
476         m_targetWindow->PushEventHandler(m_handler);
477     }
478 }
479 
SetTargetWindow(wxWindow * target)480 void wxScrollHelperBase::SetTargetWindow(wxWindow *target)
481 {
482     wxCHECK_RET( target, wxT("target window must not be NULL") );
483 
484     if ( target == m_targetWindow )
485         return;
486 
487     DoSetTargetWindow(target);
488 }
489 
490 // ----------------------------------------------------------------------------
491 // scrolling implementation itself
492 // ----------------------------------------------------------------------------
493 
HandleOnScroll(wxScrollWinEvent & event)494 void wxScrollHelperBase::HandleOnScroll(wxScrollWinEvent& event)
495 {
496     int nScrollInc = CalcScrollInc(event);
497     if ( nScrollInc == 0 )
498     {
499         // can't scroll further
500         event.Skip();
501 
502         return;
503     }
504 
505     bool needsRefresh = false;
506     int dx = 0,
507         dy = 0;
508     int orient = event.GetOrientation();
509     if (orient == wxHORIZONTAL)
510     {
511        if ( m_xScrollingEnabled )
512        {
513            dx = -m_xScrollPixelsPerLine * nScrollInc;
514        }
515        else
516        {
517            needsRefresh = true;
518        }
519     }
520     else
521     {
522         if ( m_yScrollingEnabled )
523         {
524             dy = -m_yScrollPixelsPerLine * nScrollInc;
525         }
526         else
527         {
528             needsRefresh = true;
529         }
530     }
531 
532     if ( !needsRefresh )
533     {
534         // flush all pending repaints before we change m_{x,y}ScrollPosition, as
535         // otherwise invalidated area could be updated incorrectly later when
536         // ScrollWindow() makes sure they're repainted before scrolling them
537 #ifdef __WXMAC__
538         // wxWindowMac is taking care of making sure the update area is correctly
539         // set up, while not forcing an immediate redraw
540 #else
541         m_targetWindow->Update();
542 #endif
543     }
544 
545     if (orient == wxHORIZONTAL)
546     {
547         m_xScrollPosition += nScrollInc;
548         m_win->SetScrollPos(wxHORIZONTAL, m_xScrollPosition);
549     }
550     else
551     {
552         m_yScrollPosition += nScrollInc;
553         m_win->SetScrollPos(wxVERTICAL, m_yScrollPosition);
554     }
555 
556     if ( needsRefresh )
557     {
558         m_targetWindow->Refresh(true, GetScrollRect());
559     }
560     else
561     {
562         m_targetWindow->ScrollWindow(dx, dy, GetScrollRect());
563     }
564 }
565 
CalcScrollInc(wxScrollWinEvent & event)566 int wxScrollHelperBase::CalcScrollInc(wxScrollWinEvent& event)
567 {
568     int pos = event.GetPosition();
569     int orient = event.GetOrientation();
570 
571     int nScrollInc = 0;
572     if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
573     {
574             if (orient == wxHORIZONTAL)
575                 nScrollInc = - m_xScrollPosition;
576             else
577                 nScrollInc = - m_yScrollPosition;
578     } else
579     if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
580     {
581             if (orient == wxHORIZONTAL)
582                 nScrollInc = m_xScrollLines - m_xScrollPosition;
583             else
584                 nScrollInc = m_yScrollLines - m_yScrollPosition;
585     } else
586     if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
587     {
588             nScrollInc = -1;
589     } else
590     if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
591     {
592             nScrollInc = 1;
593     } else
594     if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
595     {
596             if (orient == wxHORIZONTAL)
597                 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
598             else
599                 nScrollInc = -GetScrollPageSize(wxVERTICAL);
600     } else
601     if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
602     {
603             if (orient == wxHORIZONTAL)
604                 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
605             else
606                 nScrollInc = GetScrollPageSize(wxVERTICAL);
607     } else
608     if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
609         (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
610     {
611             if (orient == wxHORIZONTAL)
612                 nScrollInc = pos - m_xScrollPosition;
613             else
614                 nScrollInc = pos - m_yScrollPosition;
615     }
616 
617     if (orient == wxHORIZONTAL)
618     {
619         if ( m_xScrollPosition + nScrollInc < 0 )
620         {
621             // As -ve as we can go
622             nScrollInc = -m_xScrollPosition;
623         }
624         else // check for the other bound
625         {
626             const int posMax = m_xScrollLines - m_xScrollLinesPerPage;
627             if ( m_xScrollPosition + nScrollInc > posMax )
628             {
629                 // As +ve as we can go
630                 nScrollInc = posMax - m_xScrollPosition;
631             }
632         }
633     }
634     else // wxVERTICAL
635     {
636         if ( m_yScrollPosition + nScrollInc < 0 )
637         {
638             // As -ve as we can go
639             nScrollInc = -m_yScrollPosition;
640         }
641         else // check for the other bound
642         {
643             const int posMax = m_yScrollLines - m_yScrollLinesPerPage;
644             if ( m_yScrollPosition + nScrollInc > posMax )
645             {
646                 // As +ve as we can go
647                 nScrollInc = posMax - m_yScrollPosition;
648             }
649         }
650     }
651 
652     return nScrollInc;
653 }
654 
DoPrepareDC(wxDC & dc)655 void wxScrollHelperBase::DoPrepareDC(wxDC& dc)
656 {
657     wxPoint pt = dc.GetDeviceOrigin();
658 #if defined(__WXGTK__) && !defined(__WXGTK3__)
659     // It may actually be correct to always query
660     // the m_sign from the DC here, but I leave the
661     // #ifdef GTK for now.
662     if (m_win->GetLayoutDirection() == wxLayout_RightToLeft)
663         dc.SetDeviceOrigin( pt.x + m_xScrollPosition * m_xScrollPixelsPerLine,
664                             pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
665     else
666 #endif
667         dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine,
668                             pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
669     dc.SetUserScale( m_scaleX, m_scaleY );
670 }
671 
SetScrollRate(int xstep,int ystep)672 void wxScrollHelperBase::SetScrollRate( int xstep, int ystep )
673 {
674     int old_x = m_xScrollPixelsPerLine * m_xScrollPosition;
675     int old_y = m_yScrollPixelsPerLine * m_yScrollPosition;
676 
677     m_xScrollPixelsPerLine = xstep;
678     m_yScrollPixelsPerLine = ystep;
679 
680     int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
681     int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
682 
683     m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
684     m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition );
685     m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
686 
687     AdjustScrollbars();
688 }
689 
GetScrollPixelsPerUnit(int * x_unit,int * y_unit) const690 void wxScrollHelperBase::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
691 {
692     if ( x_unit )
693         *x_unit = m_xScrollPixelsPerLine;
694     if ( y_unit )
695         *y_unit = m_yScrollPixelsPerLine;
696 }
697 
698 
GetScrollLines(int orient) const699 int wxScrollHelperBase::GetScrollLines( int orient ) const
700 {
701     if ( orient == wxHORIZONTAL )
702         return m_xScrollLines;
703     else
704         return m_yScrollLines;
705 }
706 
GetScrollPageSize(int orient) const707 int wxScrollHelperBase::GetScrollPageSize(int orient) const
708 {
709     if ( orient == wxHORIZONTAL )
710         return m_xScrollLinesPerPage;
711     else
712         return m_yScrollLinesPerPage;
713 }
714 
SetScrollPageSize(int orient,int pageSize)715 void wxScrollHelperBase::SetScrollPageSize(int orient, int pageSize)
716 {
717     if ( orient == wxHORIZONTAL )
718         m_xScrollLinesPerPage = pageSize;
719     else
720         m_yScrollLinesPerPage = pageSize;
721 }
722 
EnableScrolling(bool x_scroll,bool y_scroll)723 void wxScrollHelperBase::EnableScrolling (bool x_scroll, bool y_scroll)
724 {
725     m_xScrollingEnabled = x_scroll;
726     m_yScrollingEnabled = y_scroll;
727 }
728 
729 // Where the current view starts from
DoGetViewStart(int * x,int * y) const730 void wxScrollHelperBase::DoGetViewStart (int *x, int *y) const
731 {
732     if ( x )
733         *x = m_xScrollPosition;
734     if ( y )
735         *y = m_yScrollPosition;
736 }
737 
DoCalcScrolledPosition(int x,int y,int * xx,int * yy) const738 void wxScrollHelperBase::DoCalcScrolledPosition(int x, int y,
739                                                 int *xx, int *yy) const
740 {
741     if ( xx )
742         *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
743     if ( yy )
744         *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
745 }
746 
DoCalcUnscrolledPosition(int x,int y,int * xx,int * yy) const747 void wxScrollHelperBase::DoCalcUnscrolledPosition(int x, int y,
748                                                   int *xx, int *yy) const
749 {
750     if ( xx )
751         *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
752     if ( yy )
753         *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
754 }
755 
756 // ----------------------------------------------------------------------------
757 // geometry
758 // ----------------------------------------------------------------------------
759 
ScrollLayout()760 bool wxScrollHelperBase::ScrollLayout()
761 {
762     if ( m_win->GetSizer() && m_targetWindow == m_win )
763     {
764         // If we're the scroll target, take into account the
765         // virtual size and scrolled position of the window.
766 
767         wxSize size = m_win->GetVirtualSize();
768 
769         // However we should use the real window size in the direction in which
770         // scrolling is disabled, if any.
771         const wxSize clientSize = m_win->GetClientSize();
772         if ( !IsScrollbarShown(wxHORIZONTAL) )
773             size.x = clientSize.x;
774         if ( !IsScrollbarShown(wxVERTICAL) )
775             size.y = clientSize.y;
776 
777         m_win->GetSizer()->SetDimension(CalcScrolledPosition(wxPoint(0, 0)),
778                                         size);
779         return true;
780     }
781 
782     // fall back to default for LayoutConstraints
783     return m_win->wxWindow::Layout();
784 }
785 
ScrollDoSetVirtualSize(int x,int y)786 void wxScrollHelperBase::ScrollDoSetVirtualSize(int x, int y)
787 {
788     m_win->wxWindow::DoSetVirtualSize( x, y );
789     AdjustScrollbars();
790 
791     if (m_win->GetAutoLayout())
792         m_win->Layout();
793 }
794 
795 // wxWindow's GetBestVirtualSize returns the actual window size,
796 // whereas we want to return the virtual size
ScrollGetBestVirtualSize() const797 wxSize wxScrollHelperBase::ScrollGetBestVirtualSize() const
798 {
799     wxSize clientSize(m_win->GetClientSize());
800     if ( m_win->GetSizer() )
801         clientSize.IncTo(m_win->GetSizer()->CalcMin());
802 
803     return clientSize;
804 }
805 
806 // ----------------------------------------------------------------------------
807 // event handlers
808 // ----------------------------------------------------------------------------
809 
810 // Default OnSize resets scrollbars, if any
HandleOnSize(wxSizeEvent & WXUNUSED (event))811 void wxScrollHelperBase::HandleOnSize(wxSizeEvent& WXUNUSED(event))
812 {
813     if ( m_targetWindow->GetAutoLayout() )
814     {
815         wxSize size = m_targetWindow->GetBestVirtualSize();
816 
817         // This will call ::Layout() and ::AdjustScrollbars()
818         m_win->SetVirtualSize( size );
819     }
820     else
821     {
822         AdjustScrollbars();
823     }
824 }
825 
826 // This calls OnDraw, having adjusted the origin according to the current
827 // scroll position
HandleOnPaint(wxPaintEvent & WXUNUSED (event))828 void wxAnyScrollHelperBase::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
829 {
830     // don't use m_targetWindow here, this is always called for ourselves
831     wxPaintDC dc(m_win);
832     DoPrepareDC(dc);
833 
834     OnDraw(dc);
835 }
836 
837 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
838 // compatibility here - if we used OnKeyDown(), the programs which process
839 // arrows themselves in their OnChar() would never get the message and like
840 // this they always have the priority
HandleOnChar(wxKeyEvent & event)841 void wxAnyScrollHelperBase::HandleOnChar(wxKeyEvent& event)
842 {
843     if ( !m_kbdScrollingEnabled )
844     {
845         event.Skip();
846         return;
847     }
848 
849     // prepare the event this key press maps to
850     wxScrollWinEvent newEvent;
851 
852     newEvent.SetPosition(0);
853     newEvent.SetEventObject(m_win);
854     newEvent.SetId(m_win->GetId());
855 
856     // this is the default, it's changed to wxHORIZONTAL below if needed
857     newEvent.SetOrientation(wxVERTICAL);
858 
859     // some key events result in scrolling in both horizontal and vertical
860     // direction, e.g. Ctrl-{Home,End}, if this flag is true we should generate
861     // a second event in horizontal direction in addition to the primary one
862     bool sendHorizontalToo = false;
863 
864     switch ( event.GetKeyCode() )
865     {
866         case WXK_PAGEUP:
867             newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEUP);
868             break;
869 
870         case WXK_PAGEDOWN:
871             newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN);
872             break;
873 
874         case WXK_HOME:
875             newEvent.SetEventType(wxEVT_SCROLLWIN_TOP);
876 
877             sendHorizontalToo = event.ControlDown();
878             break;
879 
880         case WXK_END:
881             newEvent.SetEventType(wxEVT_SCROLLWIN_BOTTOM);
882 
883             sendHorizontalToo = event.ControlDown();
884             break;
885 
886         case WXK_LEFT:
887             newEvent.SetOrientation(wxHORIZONTAL);
888             wxFALLTHROUGH;
889 
890         case WXK_UP:
891             newEvent.SetEventType(wxEVT_SCROLLWIN_LINEUP);
892             break;
893 
894         case WXK_RIGHT:
895             newEvent.SetOrientation(wxHORIZONTAL);
896             wxFALLTHROUGH;
897 
898         case WXK_DOWN:
899             newEvent.SetEventType(wxEVT_SCROLLWIN_LINEDOWN);
900             break;
901 
902         default:
903             // not a scrolling key
904             event.Skip();
905             return;
906     }
907 
908     m_win->ProcessWindowEvent(newEvent);
909 
910     if ( sendHorizontalToo )
911     {
912         newEvent.SetOrientation(wxHORIZONTAL);
913         m_win->ProcessWindowEvent(newEvent);
914     }
915 }
916 
917 // ----------------------------------------------------------------------------
918 // autoscroll stuff: these functions deal with sending fake scroll events when
919 // a captured mouse is being held outside the window
920 // ----------------------------------------------------------------------------
921 
SendAutoScrollEvents(wxScrollWinEvent & event) const922 bool wxScrollHelperBase::SendAutoScrollEvents(wxScrollWinEvent& event) const
923 {
924     // only send the event if the window is scrollable in this direction
925     wxWindow *win = (wxWindow *)event.GetEventObject();
926     return win->HasScrollbar(event.GetOrientation());
927 }
928 
StopAutoScrolling()929 void wxScrollHelperBase::StopAutoScrolling()
930 {
931 #if wxUSE_TIMER
932     wxDELETE(m_timerAutoScroll);
933 #endif
934 }
935 
HandleOnMouseEnter(wxMouseEvent & event)936 void wxScrollHelperBase::HandleOnMouseEnter(wxMouseEvent& event)
937 {
938     StopAutoScrolling();
939 
940     event.Skip();
941 }
942 
HandleOnMouseLeave(wxMouseEvent & event)943 void wxScrollHelperBase::HandleOnMouseLeave(wxMouseEvent& event)
944 {
945     // don't prevent the usual processing of the event from taking place
946     event.Skip();
947 
948     // when a captured mouse leave a scrolled window we start generate
949     // scrolling events to allow, for example, extending selection beyond the
950     // visible area in some controls
951     if ( wxWindow::GetCapture() == m_targetWindow )
952     {
953         // where is the mouse leaving?
954         int pos, orient;
955         wxPoint pt = event.GetPosition();
956         if ( pt.x < 0 )
957         {
958             orient = wxHORIZONTAL;
959             pos = 0;
960         }
961         else if ( pt.y < 0 )
962         {
963             orient = wxVERTICAL;
964             pos = 0;
965         }
966         else // we're lower or to the right of the window
967         {
968             wxSize size = m_targetWindow->GetClientSize();
969             if ( pt.x > size.x )
970             {
971                 orient = wxHORIZONTAL;
972                 pos = m_xScrollLines;
973             }
974             else if ( pt.y > size.y )
975             {
976                 orient = wxVERTICAL;
977                 pos = m_yScrollLines;
978             }
979             else // this should be impossible
980             {
981                 // but seems to happen sometimes under wxMSW - maybe it's a bug
982                 // there but for now just ignore it
983 
984                 //wxFAIL_MSG( wxT("can't understand where has mouse gone") );
985 
986                 return;
987             }
988         }
989 
990         // only start the auto scroll timer if the window can be scrolled in
991         // this direction
992         if ( !m_targetWindow->HasScrollbar(orient) )
993             return;
994 
995 #if wxUSE_TIMER
996         delete m_timerAutoScroll;
997         m_timerAutoScroll = new wxAutoScrollTimer
998                                 (
999                                     m_targetWindow, this,
1000                                     pos == 0 ? wxEVT_SCROLLWIN_LINEUP
1001                                              : wxEVT_SCROLLWIN_LINEDOWN,
1002                                     pos,
1003                                     orient
1004                                 );
1005         m_timerAutoScroll->Start(50); // FIXME: make configurable
1006 #else
1007         wxUnusedVar(pos);
1008 #endif
1009     }
1010 }
1011 
1012 #if wxUSE_MOUSEWHEEL
1013 
HandleOnMouseWheel(wxMouseEvent & event)1014 void wxScrollHelperBase::HandleOnMouseWheel(wxMouseEvent& event)
1015 {
1016     m_wheelRotation += event.GetWheelRotation();
1017     int lines = m_wheelRotation / event.GetWheelDelta();
1018     m_wheelRotation -= lines * event.GetWheelDelta();
1019 
1020     if (lines != 0)
1021     {
1022 
1023         wxScrollWinEvent newEvent;
1024 
1025         newEvent.SetPosition(0);
1026         newEvent.SetOrientation( event.GetWheelAxis() == 0 ? wxVERTICAL : wxHORIZONTAL);
1027         newEvent.SetEventObject(m_win);
1028 
1029         if ( event.GetWheelAxis() == wxMOUSE_WHEEL_HORIZONTAL )
1030             lines = -lines;
1031 
1032         if (event.IsPageScroll())
1033         {
1034             if (lines > 0)
1035                 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEUP);
1036             else
1037                 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN);
1038 
1039             m_win->GetEventHandler()->ProcessEvent(newEvent);
1040         }
1041         else
1042         {
1043             lines *= event.GetLinesPerAction();
1044             if (lines > 0)
1045                 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEUP);
1046             else
1047                 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEDOWN);
1048 
1049             int times = abs(lines);
1050             for (; times > 0; times--)
1051                 m_win->GetEventHandler()->ProcessEvent(newEvent);
1052         }
1053     }
1054 }
1055 
1056 #endif // wxUSE_MOUSEWHEEL
1057 
HandleOnChildFocus(wxChildFocusEvent & event)1058 void wxScrollHelperBase::HandleOnChildFocus(wxChildFocusEvent& event)
1059 {
1060     // this event should be processed by all windows in parenthood chain,
1061     // e.g. so that nested wxScrolledWindows work correctly
1062     event.Skip();
1063 
1064     // find the immediate child under which the window receiving focus is:
1065     wxWindow *win = event.GetWindow();
1066 
1067     if ( win == m_targetWindow )
1068         return; // nothing to do
1069 
1070     if ( !ShouldScrollToChildOnFocus(win) )
1071     {
1072         // the window does not require to be scrolled into view
1073         return;
1074     }
1075 
1076     // Fixing ticket: https://trac.wxwidgets.org/ticket/9563
1077     // When a child inside a wxControlContainer receives a focus, the
1078     // wxControlContainer generates an artificial wxChildFocusEvent for
1079     // itself, telling its parent that 'it' received the focus. The effect is
1080     // that this->HandleOnChildFocus is called twice, first with the
1081     // artificial wxChildFocusEvent and then with the original event.  We need
1082     // to ignore the artificial event here or otherwise HandleOnChildFocus
1083     // would first scroll the target window to make the entire
1084     // wxControlContainer visible and immediately afterwards scroll the target
1085     // window again to make the child widget visible. This leads to ugly
1086     // flickering when using nested wxPanels/wxScrolledWindows.
1087     //
1088     // Ignore this event if 'win', or any of its ancestors, is derived from
1089     // wxControlContainer AND its parent is the m_targetWindow AND 'win' is not
1090     // actually receiving the focus (win != FindFocus).
1091     //
1092     // TODO: This affects all wxControlContainer objects, but
1093     // wxControlContainer is not part of the wxWidgets RTTI and so
1094     // wxDynamicCast(win, wxControlContainer) does not compile.  Find a way to
1095     // determine if 'win' derives from wxControlContainer. Until then, testing
1096     // if 'win' derives from wxPanel will probably get >90% of all cases.
1097 
1098     wxWindow * const actual_focus = wxWindow::FindFocus();
1099     for ( wxWindow* w = win; w; w = w->GetParent() )
1100     {
1101         if ( w != actual_focus &&
1102              wxDynamicCast(w, wxPanel) != NULL &&
1103              w->GetParent() == m_targetWindow )
1104         {
1105             // if it is a wxPanel and receives the focus, it should not be
1106             // scrolled into view
1107             return;
1108         }
1109     }
1110 
1111     const wxRect viewRect(m_targetWindow->GetClientRect());
1112 
1113     // For composite controls such as wxComboCtrl we should try to fit the
1114     // entire control inside the visible area of the target window, not just
1115     // the focused child of the control. Otherwise we'd make only the textctrl
1116     // part of a wxComboCtrl visible and the button would still be outside the
1117     // scrolled area.  But do so only if the parent fits *entirely* inside the
1118     // scrolled window. In other situations, such as nested wxPanel or
1119     // wxScrolledWindows, the parent might be way too big to fit inside the
1120     // scrolled window. If that is the case, then make only the focused window
1121     // visible
1122     if ( win->GetParent() != m_targetWindow)
1123     {
1124         wxWindow *parent=win->GetParent();
1125         wxSize parent_size=parent->GetSize();
1126         if (parent_size.GetWidth() <= viewRect.GetWidth() &&
1127             parent_size.GetHeight() <= viewRect.GetHeight())
1128             // make the immediate parent visible instead of the focused control
1129             win=parent;
1130     }
1131 
1132     // make win position relative to the m_targetWindow viewing area instead of
1133     // its parent
1134     const wxRect
1135         winRect(m_targetWindow->ScreenToClient(win->GetScreenPosition()),
1136                 win->GetSize());
1137 
1138     // check if it's fully visible
1139     if ( viewRect.Contains(winRect) )
1140     {
1141         // it is, nothing to do
1142         return;
1143     }
1144 
1145     // check if we can make it fully visible: this is only possible if it's not
1146     // larger than our view area
1147     if ( winRect.GetWidth() > viewRect.GetWidth() ||
1148             winRect.GetHeight() > viewRect.GetHeight() )
1149     {
1150         // we can't make it fit so avoid scrolling it at all, this is only
1151         // going to be confusing and not helpful
1152         return;
1153     }
1154 
1155 
1156     // do make the window fit inside the view area by scrolling to it
1157     int stepx, stepy;
1158     GetScrollPixelsPerUnit(&stepx, &stepy);
1159 
1160     int startx, starty;
1161     GetViewStart(&startx, &starty);
1162 
1163     // first in vertical direction:
1164     if ( stepy > 0 )
1165     {
1166         int diff = 0;
1167 
1168         if ( winRect.GetTop() < 0 )
1169         {
1170             diff = winRect.GetTop();
1171         }
1172         else if ( winRect.GetBottom() > viewRect.GetHeight() )
1173         {
1174             diff = winRect.GetBottom() - viewRect.GetHeight() + 1;
1175             // round up to next scroll step if we can't get exact position,
1176             // so that the window is fully visible:
1177             diff += stepy - 1;
1178         }
1179 
1180         starty = (starty * stepy + diff) / stepy;
1181     }
1182 
1183     // then horizontal:
1184     if ( stepx > 0 )
1185     {
1186         int diff = 0;
1187 
1188         if ( winRect.GetLeft() < 0 )
1189         {
1190             diff = winRect.GetLeft();
1191         }
1192         else if ( winRect.GetRight() > viewRect.GetWidth() )
1193         {
1194             diff = winRect.GetRight() - viewRect.GetWidth() + 1;
1195             // round up to next scroll step if we can't get exact position,
1196             // so that the window is fully visible:
1197             diff += stepx - 1;
1198         }
1199 
1200         startx = (startx * stepx + diff) / stepx;
1201     }
1202 
1203     Scroll(startx, starty);
1204 }
1205 
1206 
1207 #ifdef wxHAS_GENERIC_SCROLLWIN
1208 
1209 // ----------------------------------------------------------------------------
1210 // wxScrollHelper implementation
1211 // ----------------------------------------------------------------------------
1212 
wxScrollHelper(wxWindow * winToScroll)1213 wxScrollHelper::wxScrollHelper(wxWindow *winToScroll)
1214     : wxScrollHelperBase(winToScroll)
1215 {
1216     m_xVisibility =
1217     m_yVisibility = wxSHOW_SB_DEFAULT;
1218     m_adjustScrollFlagReentrancy = 0;
1219 }
1220 
IsScrollbarShown(int orient) const1221 bool wxScrollHelper::IsScrollbarShown(int orient) const
1222 {
1223     wxScrollbarVisibility visibility = orient == wxHORIZONTAL ? m_xVisibility
1224                                                               : m_yVisibility;
1225 
1226     return visibility != wxSHOW_SB_NEVER;
1227 }
1228 
DoShowScrollbars(wxScrollbarVisibility horz,wxScrollbarVisibility vert)1229 void wxScrollHelper::DoShowScrollbars(wxScrollbarVisibility horz,
1230                                       wxScrollbarVisibility vert)
1231 {
1232     if ( horz != m_xVisibility || vert != m_yVisibility )
1233     {
1234         m_xVisibility = horz;
1235         m_yVisibility = vert;
1236 
1237         AdjustScrollbars();
1238     }
1239 }
1240 
1241 void
DoAdjustScrollbar(int orient,int clientSize,int virtSize,int pixelsPerUnit,int & scrollUnits,int & scrollPosition,int & scrollLinesPerPage,wxScrollbarVisibility visibility)1242 wxScrollHelper::DoAdjustScrollbar(int orient,
1243                                   int clientSize,
1244                                   int virtSize,
1245                                   int pixelsPerUnit,
1246                                   int& scrollUnits,
1247                                   int& scrollPosition,
1248                                   int& scrollLinesPerPage,
1249                                   wxScrollbarVisibility visibility)
1250 {
1251     // scroll lines per page: if 0, no scrolling is needed
1252     // check if we need scrollbar in this direction at all
1253     if ( pixelsPerUnit == 0 || clientSize >= virtSize )
1254     {
1255         // scrolling is disabled or unnecessary
1256         scrollUnits =
1257         scrollPosition = 0;
1258         scrollLinesPerPage = 0;
1259     }
1260     else // might need scrolling
1261     {
1262         // Round up integer division to catch any "leftover" client space.
1263         scrollUnits = (virtSize + pixelsPerUnit - 1) / pixelsPerUnit;
1264 
1265         // Calculate the number of fully scroll units
1266         scrollLinesPerPage = clientSize / pixelsPerUnit;
1267 
1268         if ( scrollLinesPerPage >= scrollUnits )
1269         {
1270             // we're big enough to not need scrolling
1271             scrollUnits =
1272             scrollPosition = 0;
1273             scrollLinesPerPage = 0;
1274         }
1275         else // we do need a scrollbar
1276         {
1277             if ( scrollLinesPerPage < 1 )
1278                 scrollLinesPerPage = 1;
1279 
1280             // Correct position if greater than extent of canvas minus
1281             // the visible portion of it or if below zero
1282             const int posMax = scrollUnits - scrollLinesPerPage;
1283             if ( scrollPosition > posMax )
1284                 scrollPosition = posMax;
1285             else if ( scrollPosition < 0 )
1286                 scrollPosition = 0;
1287         }
1288     }
1289 
1290     // in wxSHOW_SB_NEVER case don't show the scrollbar even if it's needed, in
1291     // wxSHOW_SB_ALWAYS case show the scrollbar even if it's not needed by
1292     // passing a special range value to SetScrollbar()
1293     int range;
1294     switch ( visibility )
1295     {
1296         case wxSHOW_SB_NEVER:
1297             range = 0;
1298             break;
1299 
1300         case wxSHOW_SB_ALWAYS:
1301             range = scrollUnits ? scrollUnits : -1;
1302             break;
1303 
1304         default:
1305             wxFAIL_MSG( wxS("unknown scrollbar visibility") );
1306             wxFALLTHROUGH;
1307 
1308         case wxSHOW_SB_DEFAULT:
1309             range = scrollUnits;
1310             break;
1311 
1312     }
1313 
1314     m_win->SetScrollbar(orient, scrollPosition, scrollLinesPerPage, range);
1315 }
1316 
AdjustScrollbars()1317 void wxScrollHelper::AdjustScrollbars()
1318 {
1319     wxRecursionGuard guard(m_adjustScrollFlagReentrancy);
1320     if ( guard.IsInside() )
1321     {
1322         // don't reenter AdjustScrollbars() while another call to
1323         // AdjustScrollbars() is in progress because this may lead to calling
1324         // ScrollWindow() twice and this can really happen under MSW if
1325         // SetScrollbar() call below adds or removes the scrollbar which
1326         // changes the window size and hence results in another
1327         // AdjustScrollbars() call
1328         return;
1329     }
1330 
1331     int oldXScroll = m_xScrollPosition;
1332     int oldYScroll = m_yScrollPosition;
1333 
1334     // we may need to readjust the scrollbars several times as enabling one of
1335     // them reduces the area available for the window contents and so can make
1336     // the other scrollbar necessary now although it wasn't necessary before
1337     //
1338     // VZ: normally this loop should be over in at most 2 iterations, I don't
1339     //     know why do we need 5 of them
1340     for ( int iterationCount = 0; iterationCount < 5; iterationCount++ )
1341     {
1342         wxSize clientSize = GetTargetSize();
1343         const wxSize virtSize = m_targetWindow->GetVirtualSize();
1344 
1345         // this block of code tries to work around the following problem: the
1346         // window could have been just resized to have enough space to show its
1347         // full contents without the scrollbars, but its client size could be
1348         // not big enough because it does have the scrollbars right now and so
1349         // the scrollbars would remain even though we don't need them any more
1350         //
1351         // to prevent this from happening, check if we have enough space for
1352         // everything without the scrollbars and explicitly disable them then
1353         const wxSize availSize = GetSizeAvailableForScrollTarget(
1354             m_win->GetSize() - m_win->GetWindowBorderSize());
1355         if ( availSize != clientSize )
1356         {
1357             if ( availSize.x >= virtSize.x && availSize.y >= virtSize.y )
1358             {
1359                 // this will be enough to make the scrollbars disappear below
1360                 // and then the client size will indeed become equal to the
1361                 // full available size
1362                 clientSize = availSize;
1363             }
1364         }
1365 
1366 
1367         DoAdjustScrollbar(wxHORIZONTAL,
1368                           clientSize.x,
1369                           virtSize.x,
1370                           m_xScrollPixelsPerLine,
1371                           m_xScrollLines,
1372                           m_xScrollPosition,
1373                           m_xScrollLinesPerPage,
1374                           m_xVisibility);
1375 
1376         DoAdjustScrollbar(wxVERTICAL,
1377                           clientSize.y,
1378                           virtSize.y,
1379                           m_yScrollPixelsPerLine,
1380                           m_yScrollLines,
1381                           m_yScrollPosition,
1382                           m_yScrollLinesPerPage,
1383                           m_yVisibility);
1384 
1385 
1386         // If a scrollbar (dis)appeared as a result of this, we need to adjust
1387         // them again but if the client size didn't change, then we're done
1388         if ( GetTargetSize() == clientSize )
1389             break;
1390     }
1391 
1392 #ifdef __WXMOTIF__
1393     // Sorry, some Motif-specific code to implement a backing pixmap
1394     // for the wxRETAINED style. Implementing a backing store can't
1395     // be entirely generic because it relies on the wxWindowDC implementation
1396     // to duplicate X drawing calls for the backing pixmap.
1397 
1398     if ( m_targetWindow->GetWindowStyle() & wxRETAINED )
1399     {
1400         Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget());
1401 
1402         int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
1403         int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
1404         if (m_targetWindow->GetBackingPixmap() &&
1405            !((m_targetWindow->GetPixmapWidth() == totalPixelWidth) &&
1406              (m_targetWindow->GetPixmapHeight() == totalPixelHeight)))
1407         {
1408             XFreePixmap (dpy, (Pixmap) m_targetWindow->GetBackingPixmap());
1409             m_targetWindow->SetBackingPixmap((WXPixmap) 0);
1410         }
1411 
1412         if (!m_targetWindow->GetBackingPixmap() &&
1413            (m_xScrollLines != 0) && (m_yScrollLines != 0))
1414         {
1415             int depth = wxDisplayDepth();
1416             m_targetWindow->SetPixmapWidth(totalPixelWidth);
1417             m_targetWindow->SetPixmapHeight(totalPixelHeight);
1418             m_targetWindow->SetBackingPixmap((WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
1419               m_targetWindow->GetPixmapWidth(), m_targetWindow->GetPixmapHeight(), depth));
1420         }
1421 
1422     }
1423 #endif // Motif
1424 
1425     if (oldXScroll != m_xScrollPosition)
1426     {
1427        if (m_xScrollingEnabled)
1428             m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll - m_xScrollPosition), 0,
1429                                           GetScrollRect() );
1430        else
1431             m_targetWindow->Refresh(true, GetScrollRect());
1432     }
1433 
1434     if (oldYScroll != m_yScrollPosition)
1435     {
1436         if (m_yScrollingEnabled)
1437             m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition),
1438                                           GetScrollRect() );
1439         else
1440             m_targetWindow->Refresh(true, GetScrollRect());
1441     }
1442 }
1443 
DoScroll(int x_pos,int y_pos)1444 void wxScrollHelper::DoScroll( int x_pos, int y_pos )
1445 {
1446     if (!m_targetWindow)
1447         return;
1448 
1449     if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
1450         ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
1451 
1452     int w = 0, h = 0;
1453     GetTargetSize(&w, &h);
1454 
1455     // compute new position:
1456     int new_x = m_xScrollPosition;
1457     int new_y = m_yScrollPosition;
1458 
1459     if ((x_pos != -1) && (m_xScrollPixelsPerLine))
1460     {
1461         new_x = x_pos;
1462 
1463         // Calculate page size i.e. number of scroll units you get on the
1464         // current client window
1465         int noPagePositions = w/m_xScrollPixelsPerLine;
1466         if (noPagePositions < 1) noPagePositions = 1;
1467 
1468         // Correct position if greater than extent of canvas minus
1469         // the visible portion of it or if below zero
1470         new_x = wxMin( m_xScrollLines-noPagePositions, new_x );
1471         new_x = wxMax( 0, new_x );
1472     }
1473     if ((y_pos != -1) && (m_yScrollPixelsPerLine))
1474     {
1475         new_y = y_pos;
1476 
1477         // Calculate page size i.e. number of scroll units you get on the
1478         // current client window
1479         int noPagePositions = h/m_yScrollPixelsPerLine;
1480         if (noPagePositions < 1) noPagePositions = 1;
1481 
1482         // Correct position if greater than extent of canvas minus
1483         // the visible portion of it or if below zero
1484         new_y = wxMin( m_yScrollLines-noPagePositions, new_y );
1485         new_y = wxMax( 0, new_y );
1486     }
1487 
1488     if ( new_x == m_xScrollPosition && new_y == m_yScrollPosition )
1489         return; // nothing to do, the position didn't change
1490 
1491     // flush all pending repaints before we change m_{x,y}ScrollPosition, as
1492     // otherwise invalidated area could be updated incorrectly later when
1493     // ScrollWindow() makes sure they're repainted before scrolling them
1494     m_targetWindow->Update();
1495 
1496     // update the position and scroll the window now:
1497     if (m_xScrollPosition != new_x)
1498     {
1499         int old_x = m_xScrollPosition;
1500         m_xScrollPosition = new_x;
1501         m_win->SetScrollPos( wxHORIZONTAL, new_x );
1502         m_targetWindow->ScrollWindow( (old_x-new_x)*m_xScrollPixelsPerLine, 0,
1503                                       GetScrollRect() );
1504     }
1505 
1506     if (m_yScrollPosition != new_y)
1507     {
1508         int old_y = m_yScrollPosition;
1509         m_yScrollPosition = new_y;
1510         m_win->SetScrollPos( wxVERTICAL, new_y );
1511         m_targetWindow->ScrollWindow( 0, (old_y-new_y)*m_yScrollPixelsPerLine,
1512                                       GetScrollRect() );
1513     }
1514 }
1515 
1516 #endif // wxHAS_GENERIC_SCROLLWIN
1517 
1518 // ----------------------------------------------------------------------------
1519 // wxScrolled<T> and wxScrolledWindow implementation
1520 // ----------------------------------------------------------------------------
1521 
FilterBestSize(const wxWindow * win,const wxScrollHelper * helper,const wxSize & origBest)1522 wxSize wxScrolledT_Helper::FilterBestSize(const wxWindow *win,
1523                                           const wxScrollHelper *helper,
1524                                           const wxSize& origBest)
1525 {
1526     // NB: We don't do this in WX_FORWARD_TO_SCROLL_HELPER, because not
1527     //     all scrollable windows should behave like this, only those that
1528     //     contain children controls within scrollable area
1529     //     (i.e., wxScrolledWindow) and other some scrollable windows may
1530     //     have different DoGetBestSize() implementation (e.g. wxTreeCtrl).
1531 
1532     wxSize best = origBest;
1533 
1534     if ( win->GetAutoLayout() )
1535     {
1536         // Only use the content to set the window size in the direction
1537         // where there's no scrolling; otherwise we're going to get a huge
1538         // window in the direction in which scrolling is enabled
1539         int ppuX, ppuY;
1540         helper->GetScrollPixelsPerUnit(&ppuX, &ppuY);
1541 
1542         // NB: This code used to use *current* size if min size wasn't
1543         //     specified, presumably to get some reasonable (i.e., larger than
1544         //     minimal) size.  But that's a wrong thing to do in GetBestSize(),
1545         //     so we use minimal size as specified. If the app needs some
1546         //     minimal size for its scrolled window, it should set it and put
1547         //     the window into sizer as expandable so that it can use all space
1548         //     available to it.
1549         //
1550         //     See also https://github.com/wxWidgets/wxWidgets/commit/7e0f7539
1551 
1552         wxSize minSize = win->GetMinSize();
1553 
1554         if ( ppuX > 0 )
1555             best.x = minSize.x + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X, win);
1556 
1557         if ( ppuY > 0 )
1558             best.y = minSize.y + wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y, win);
1559     }
1560 
1561     return best;
1562 }
1563 
1564 #ifdef __WXMSW__
FilterMSWWindowProc(WXUINT nMsg,WXLRESULT rc)1565 WXLRESULT wxScrolledT_Helper::FilterMSWWindowProc(WXUINT nMsg, WXLRESULT rc)
1566 {
1567     // we need to process arrows ourselves for scrolling
1568     if ( nMsg == WM_GETDLGCODE )
1569     {
1570         rc |= DLGC_WANTARROWS;
1571     }
1572     return rc;
1573 }
1574 #endif // __WXMSW__
1575 
1576 // NB: skipping wxScrolled<T> in wxRTTI information because being a template,
1577 //     it doesn't and can't implement wxRTTI support
1578 wxIMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel);
1579