webengine/osswebengine/webkit/s60/webview/WebPagePinchZoomHandler.cpp
changeset 65 5bfc169077b2
parent 42 d39add9822e2
child 66 cacf6ee57968
equal deleted inserted replaced
42:d39add9822e2 65:5bfc169077b2
     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:   
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <../bidi.h>
       
    21 #include "WebPagePinchZoomHandler.h"
       
    22 #include "WebView.h"
       
    23 
       
    24 
       
    25 const int KBitmapUpdateTimeout  = 100*1000;
       
    26 const int KPinchExitWaitTimeout = 300*1000;
       
    27 
       
    28 const int KPinchZoomStepSizeSmall  = 3;
       
    29 const int KPinchZoomStepSizeMedium = 4;
       
    30 const int KPinchZoomStepSizeLarge  = 5;
       
    31 const int KMinPinchFactor = 1;
       
    32 
       
    33 int pinchBitmapUpdateTimerCb( void* ptr );
       
    34 int pinchExitWaitTimerCb( void* ptr );
       
    35 
       
    36 
       
    37 // ============================ MEMBER FUNCTIONS ===============================
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // WebPagePinchZoomHandler::NewL
       
    41 // The two-phase Symbian constructor
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 WebPagePinchZoomHandler* WebPagePinchZoomHandler::NewL(WebView* webView)
       
    45 {
       
    46     WebPagePinchZoomHandler* self = new (ELeave) WebPagePinchZoomHandler(webView);
       
    47     CleanupStack::PushL(self);
       
    48     self->constructL();
       
    49     CleanupStack::Pop(); //self
       
    50     return self;
       
    51 }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // WebPagePinchZoomHandler::WebPointerEventHandler
       
    55 // C++ default constructor
       
    56 //
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 WebPagePinchZoomHandler::WebPagePinchZoomHandler(WebView* webView)
       
    60 : m_webView(webView)
       
    61 , m_zoomOutBaseLevel(0)
       
    62 , m_pinchCenterSet(false)
       
    63 , m_zoomStepSize(0)
       
    64 , m_pinchActive(false)
       
    65 , m_pinchCenter(0,0)
       
    66 {
       
    67 }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // WebPagePinchZoomHandler::constructL
       
    71 // The constructor that can contain code that might leave.
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 void WebPagePinchZoomHandler::constructL()
       
    75 {
       
    76     m_bitmapUpdateTimer = CPeriodic::NewL(CActive::EPriorityHigh);
       
    77     m_pinchExitWaitTimer = CPeriodic::NewL(CActive::EPriorityHigh);
       
    78 }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // WebPagePinchZoomHandler::~WebPagePinchZoomHandler
       
    82 // -----------------------------------------------------------------------------
       
    83 WebPagePinchZoomHandler::~WebPagePinchZoomHandler()
       
    84 {
       
    85     if (m_bitmapUpdateTimer->IsActive())
       
    86         m_bitmapUpdateTimer->Cancel();
       
    87     delete m_bitmapUpdateTimer;
       
    88     
       
    89     if (m_pinchExitWaitTimer->IsActive()) 
       
    90         m_pinchExitWaitTimer->Cancel();
       
    91     delete m_pinchExitWaitTimer;
       
    92 
       
    93 }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // getPinchZoomStepSize
       
    97 // -----------------------------------------------------------------------------
       
    98 TInt WebPagePinchZoomHandler::getPinchZoomStepSize()
       
    99 {
       
   100     TInt zoomStepSize = 0;
       
   101 
       
   102     TInt absPinchFactor = Abs (m_pinchFactor);
       
   103     if (absPinchFactor == 0)
       
   104         return zoomStepSize;
       
   105     
       
   106     if ( absPinchFactor >= KMinPinchFactor  &&  absPinchFactor <= 10 ) {
       
   107         zoomStepSize = KPinchZoomStepSizeSmall;
       
   108     }    
       
   109     else if ( absPinchFactor > 10 && absPinchFactor <= 20 ) {
       
   110         zoomStepSize = KPinchZoomStepSizeMedium;
       
   111     }  
       
   112     else if ( absPinchFactor > 20 ) {
       
   113         zoomStepSize = KPinchZoomStepSizeLarge;
       
   114     }
       
   115 
       
   116     return (m_pinchFactor/absPinchFactor) * zoomStepSize; 
       
   117 }
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 // handlePinchGestureEventL
       
   121 // -----------------------------------------------------------------------------
       
   122 void WebPagePinchZoomHandler::handlePinchGestureEventL(const TStmGestureEvent& aGesture)
       
   123 {
       
   124     if(aGesture.GestureState() == EGestureEnter) {
       
   125         m_pinchActive = true;
       
   126         handlePinchGestureL(aGesture);
       
   127     }
       
   128     else {
       
   129         m_pinchActive = false;
       
   130         handlePinchGestureExitL(aGesture);
       
   131     }
       
   132 
       
   133 }
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // handlePinchGestureL
       
   137 // -----------------------------------------------------------------------------
       
   138 void WebPagePinchZoomHandler::handlePinchGestureL(const TStmGestureEvent& aGesture)
       
   139 {
       
   140 
       
   141     m_pinchFactor = aGesture.Details();
       
   142     TInt zoomValue = 0;
       
   143     TInt currentZoom = m_webView->zoomLevel();
       
   144     TInt zoomStepSize = getPinchZoomStepSize();
       
   145 
       
   146     if (zoomStepSize == 0)
       
   147         return;
       
   148 
       
   149     zoomValue = currentZoom + zoomStepSize;
       
   150     if (m_zoomStepSize > 0 && zoomStepSize < 0)  m_zoomOutBaseLevel = currentZoom / 2;
       
   151     
       
   152     m_zoomStepSize = zoomStepSize;
       
   153 
       
   154     if(zoomValue >= m_webView->minZoomLevel() && zoomValue <= m_webView->maxZoomLevel()) {
       
   155         if (!m_pinchCenterSet) {
       
   156             TPoint pinchCenter = aGesture.PinchEndPos() + aGesture.CurrentPos(); 
       
   157             m_pinchCenter.iX = pinchCenter.iX / 2;
       
   158             m_pinchCenter.iY = pinchCenter.iY / 2;
       
   159             m_pinchCenterSet = true;
       
   160             if (zoomStepSize < 0)   m_zoomOutBaseLevel = currentZoom / 2;  
       
   161         }
       
   162         if (!(zoomStepSize < 0 && zoomValue < m_zoomOutBaseLevel))
       
   163             setZoomLevel(zoomValue);
       
   164 
       
   165     }
       
   166 }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // handlePinchGestureExitL
       
   170 // -----------------------------------------------------------------------------
       
   171 void WebPagePinchZoomHandler::handlePinchGestureExitL(const TStmGestureEvent& aGesture)
       
   172 {
       
   173     m_zoomOutBaseLevel  = 0;
       
   174     m_zoomStepSize = 0;
       
   175     m_pinchCenterSet = false;
       
   176     if (!m_bitmapUpdateTimer->IsActive()) 
       
   177         m_bitmapUpdateTimer->Start( KBitmapUpdateTimeout,KBitmapUpdateTimeout,TCallBack(&pinchBitmapUpdateTimerCb,this));
       
   178     
       
   179     if (!m_pinchExitWaitTimer->IsActive()) 
       
   180         m_pinchExitWaitTimer->Start( KPinchExitWaitTimeout,0,TCallBack(&pinchExitWaitTimerCb,this));
       
   181     
       
   182 }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // setZoomLevel
       
   186 // -----------------------------------------------------------------------------
       
   187 void WebPagePinchZoomHandler::setZoomLevel(int zoomLevel)
       
   188 {
       
   189     m_webView->setPinchBitmapZoomLevel(zoomLevel);
       
   190 }
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // isPinchActive
       
   194 // -----------------------------------------------------------------------------
       
   195 TBool WebPagePinchZoomHandler::isPinchActive()
       
   196 {
       
   197   bool pinchActive = false;
       
   198   if (m_pinchActive == true || m_pinchExitWaitTimer->IsActive())
       
   199       pinchActive = true;
       
   200   
       
   201   return pinchActive;
       
   202 
       
   203 }
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // updateBitmap
       
   207 // -----------------------------------------------------------------------------
       
   208 void WebPagePinchZoomHandler::updateBitmap(void)
       
   209 {
       
   210     m_bitmapUpdateTimer->Cancel();
       
   211     m_webView->restoreZoomLevel(m_webView->scalingFactor());
       
   212 }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // handlepinchExitWaitTimer
       
   216 // -----------------------------------------------------------------------------
       
   217 void WebPagePinchZoomHandler::handlepinchExitWaitTimer(void)
       
   218 {
       
   219     m_pinchExitWaitTimer->Cancel();
       
   220 }
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // pinchBitmapUpdateTimerCb
       
   224 // -----------------------------------------------------------------------------
       
   225 int pinchBitmapUpdateTimerCb(void* ptr)
       
   226 {
       
   227     ((WebPagePinchZoomHandler*)ptr)->updateBitmap();
       
   228     return 0;
       
   229 }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // pinchExitWaitTimerCb
       
   233 // -----------------------------------------------------------------------------
       
   234 int pinchExitWaitTimerCb(void* ptr)
       
   235 {
       
   236     ((WebPagePinchZoomHandler*)ptr)->handlepinchExitWaitTimer();
       
   237     return 0;
       
   238 }
       
   239 
       
   240 //  End of File
       
   241 
       
   242