webengine/osswebengine/WebKit/s60/webview/WebScrollingDecelerator.cpp
changeset 0 dd21522fd290
child 92 e1bea15f9a39
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Implementation of scrolling decelerator
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <../bidi.h>
       
    21 #include "WebScrollingDecelerator.h"
       
    22 #include "WebView.h"
       
    23 #include "WebFrame.h"
       
    24 #include "WebFrameView.h"
       
    25 #include "WebPageScrollHandler.h"
       
    26 #include "PlatformScrollbar.h"
       
    27 
       
    28 // constants
       
    29 const int KRecordSize = 4;
       
    30 
       
    31 // The following deceleration curve is generated by a script
       
    32 // It lists the timeout dt values in microseconds.
       
    33 const int KDecelCurveSize = 10;
       
    34 
       
    35 const int KScrollIntervalTimeout = 40000; // scroll timer interval in microseconds
       
    36 
       
    37 // ============================ MEMBER FUNCTIONS ===============================
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // WebScrollingDecelerator::NewL
       
    41 // The two-phase Symbian constructor
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 
       
    45 WebScrollingDecelerator* WebScrollingDecelerator::NewL(WebView& webView)
       
    46 {
       
    47     WebScrollingDecelerator* self = new (ELeave) WebScrollingDecelerator(webView);
       
    48 
       
    49     CleanupStack::PushL(self);
       
    50     self->ConstructL();
       
    51     CleanupStack::Pop(); //self
       
    52     return self;
       
    53 }
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // WebScrollingDecelerator::WebScrollingDecelerator
       
    57 // C++ default constructor can NOT contain any code, that
       
    58 // might leave.
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 WebScrollingDecelerator::WebScrollingDecelerator(WebView& webView)
       
    62 : CTimer(EPriorityHigh), m_webView( webView )
       
    63 {
       
    64 }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // WebScrollingDecelerator::ConstructL
       
    68 // -----------------------------------------------------------------------------
       
    69 void WebScrollingDecelerator::ConstructL()
       
    70 {
       
    71     CTimer::ConstructL();
       
    72     CActiveScheduler::Add( this );
       
    73     m_dragPosition.Reset();
       
    74 }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // Destructor
       
    78 // -----------------------------------------------------------------------------
       
    79 WebScrollingDecelerator::~WebScrollingDecelerator()
       
    80 {
       
    81     Cancel();
       
    82 }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // WebScrollingDecelerator::updatePos
       
    86 // update record of position
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 void WebScrollingDecelerator::updatePos()
       
    90 {
       
    91     if (m_dragPosition.Count() >= KRecordSize)
       
    92       {
       
    93         // pop item from front   
       
    94         m_dragPosition.Remove(0);
       
    95       }
       
    96     
       
    97     // and add this one to the end
       
    98     TPoint currPosition = m_webView.pageScrollHandler()->currentScrollingFrameView()->contentPos();
       
    99     m_dragPosition.Append(currPosition);
       
   100 }
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // WebScrollingDecelerator::startDecel
       
   104 // initialize and start deceleration
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 void WebScrollingDecelerator::startDecelL()
       
   108 {
       
   109     int count = m_dragPosition.Count();
       
   110         
       
   111     m_normalizedCurrentPosition.iX = m_webView.pageScrollHandler()->currentScrollingFrameView()->contentPos().iX * 100;
       
   112     m_normalizedCurrentPosition.iY = m_webView.pageScrollHandler()->currentScrollingFrameView()->contentPos().iY * 100;
       
   113     
       
   114     if (count > 0)
       
   115       {     
       
   116 
       
   117       // set the delta based upon the change span of points collected averaged over the span, so long as we have moved
       
   118         int dx = (m_dragPosition[count - 1].iX - m_dragPosition[0].iX)* 100;
       
   119         int dy = (m_dragPosition[count - 1].iY - m_dragPosition[0].iY) * 100;
       
   120  
       
   121         if (abs(dx) > 0 || abs(dy) > 0)
       
   122             {           
       
   123             m_scrolldelta.SetXY( (dx/count), (dy/count) );
       
   124             WebFrameView* view = m_webView.pageScrollHandler()->currentScrollingFrameView();
       
   125             
       
   126             m_scrolldelta = view->toDocCoords(m_scrolldelta);
       
   127             
       
   128             // init num steps
       
   129             m_numscrollsteps = 0;
       
   130 
       
   131             // call the RunL function directly, this first time. CTimer will call it from now on.
       
   132             m_decelelatorSwitch = ETrue;
       
   133             
       
   134             RunL();
       
   135             }
       
   136         }
       
   137         m_dragPosition.Reset();
       
   138 }
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // WebScrollingDecelerator::RunL
       
   142 // continue deceleration
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 void WebScrollingDecelerator::RunL()
       
   146 {
       
   147   WebFrameView* scrollingView = m_webView.pageScrollHandler()->currentScrollingFrameView();
       
   148     // use a pre-calulated deceleration curve to calculate dt
       
   149     if ((m_numscrollsteps >= KDecelCurveSize ) || !m_decelelatorSwitch)
       
   150       {
       
   151         // done
       
   152         Cancel();
       
   153         m_webView.setViewIsScrolling(false);
       
   154         return;
       
   155       }
       
   156     
       
   157     // step to next
       
   158     m_numscrollsteps++;
       
   159 
       
   160     // do the scrolling
       
   161     
       
   162     if (m_webView.pageScrollHandler() && scrollingView) {
       
   163         
       
   164         TPoint newpos;
       
   165         
       
   166         /* With each step decrease m_scrolldelta by 50% */
       
   167         m_scrolldelta.iX = m_scrolldelta.iX >> 1;
       
   168         m_scrolldelta.iY = m_scrolldelta.iY >> 1;
       
   169                 
       
   170         m_normalizedCurrentPosition += m_scrolldelta;
       
   171         newpos.iX = m_normalizedCurrentPosition.iX/100;
       
   172         newpos.iY = m_normalizedCurrentPosition.iY/100;
       
   173         scrollingView->scrollTo(newpos);
       
   174 
       
   175         if (scrollingView->vScrollbar()) {
       
   176             scrollingView->vScrollbar()->setValue(newpos.iY);
       
   177         }
       
   178         if (scrollingView->hScrollbar()) {
       
   179             scrollingView->hScrollbar()->setValue(newpos.iX);
       
   180         }
       
   181 
       
   182         After(KScrollIntervalTimeout);
       
   183     }
       
   184 }
       
   185 
       
   186 //  End of File