webengine/osswebengine/WebKit/s60/webview/WebPopupDrawer.cpp
changeset 0 dd21522fd290
child 13 10e98eab6f85
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2006 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 <aknutils.h>
       
    22 #include <gulicon.h>
       
    23 #include "WebPopupDrawer.h"
       
    24 #include "WebView.h"
       
    25 #include "WebCannedImages.h"
       
    26 #include "BrCtl.h"
       
    27 
       
    28 #include "StaticObjectsContainer.h"
       
    29 
       
    30 // constants
       
    31 const int KPopupTimeout = 5000 * 1000;
       
    32 const int KBorderSize = 8;
       
    33 const int KRowMargin = 5;
       
    34 const int KIconMargin = 5;
       
    35 const int KSpaceUnderneath = 30;
       
    36 
       
    37 using namespace WebCore;
       
    38 
       
    39 // ============================= LOCAL FUNCTIONS ===============================
       
    40 TInt removePopupCallback( TAny* aPtr )
       
    41 {
       
    42     static_cast<WebPopupDrawer*>(aPtr)->removePopup();
       
    43     return KErrNone;
       
    44 }
       
    45 
       
    46 // ============================ MEMBER FUNCTIONS ===============================
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // WebPopupDrawer::NewL
       
    50 // The two-phase Symbian constructor
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 WebPopupDrawer* WebPopupDrawer::NewL(WebView& webView, TPtrC& textToDraw, TRect& elRect )
       
    54 {
       
    55     WebPopupDrawer* self = new (ELeave) WebPopupDrawer( webView, elRect  );
       
    56     CleanupStack::PushL(self);
       
    57     self->constructL(textToDraw);
       
    58     CleanupStack::Pop(); //self
       
    59     return self;
       
    60 }
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // WebPopupDrawer::WebPopupDrawer
       
    64 // C++ default constructor can NOT contain any code, that
       
    65 // might leave.
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 WebPopupDrawer::WebPopupDrawer(WebView& webView, TRect& elRect )
       
    69 : m_webView( webView ) , m_elRect(elRect)
       
    70 {
       
    71 }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // WebPopupDrawer::constructL
       
    75 // The constructor that can contain code that might leave.
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 void WebPopupDrawer::constructL(TPtrC& textToDraw)
       
    79 {
       
    80     if (textToDraw.Length() == 0)
       
    81     {
       
    82         User::Leave(KErrArgument);
       
    83     }
       
    84     m_textToDraw = textToDraw.AllocL();
       
    85     m_removePopupPeriodic = CPeriodic::NewL( CActive::EPriorityLow );
       
    86     m_removePopupPeriodic->Start( KPopupTimeout, 0, TCallBack( &removePopupCallback, this ) );
       
    87     sizeChangedL();
       
    88     constructSprite();
       
    89 }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // Destructor
       
    93 // -----------------------------------------------------------------------------
       
    94 WebPopupDrawer::~WebPopupDrawer()
       
    95 {
       
    96     delete m_textToDraw;
       
    97     if (m_removePopupPeriodic)
       
    98     {
       
    99         m_removePopupPeriodic->Cancel();
       
   100         delete m_removePopupPeriodic;
       
   101     }
       
   102     delete m_lineWidthArray;
       
   103     delete m_wrappedArray;
       
   104     m_sprite.Close();
       
   105     delete m_spriteMaskBitmapDevice;
       
   106     delete m_spriteMaskBitmapContext;
       
   107     delete m_spriteMaskBitmap;
       
   108 }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // WebPopupDrawer::drawPopup
       
   112 // Draw the popup.
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 void WebPopupDrawer::drawPopup()
       
   116 {
       
   117     m_sprite.SetPosition(m_position);
       
   118 }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // WebPopupDrawer::removePopup
       
   122 // Stop displaying the popup.
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 void WebPopupDrawer::removePopup()
       
   126 {
       
   127     m_webView.removePopup();
       
   128 }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // WebPopupDrawer::sizeChangedL
       
   132 // Recreate the bitmap to fit in the new size
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 void WebPopupDrawer::sizeChangedL()
       
   136 {
       
   137     TRect rect(m_webView.Rect());
       
   138     if (rect != m_webViewRect)
       
   139     {
       
   140         if (rect.Width() > 0 && rect.Height() > 0)
       
   141         {
       
   142             m_webViewRect = rect;
       
   143             prepareTextL();
       
   144             createBitmapAndContextL();
       
   145         }
       
   146     }
       
   147 }
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // WebPopupDrawer::handleOfferKeyEventL
       
   151 // Handles key events
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 TKeyResponse WebPopupDrawer::handleOfferKeyEventL( const TKeyEvent& /*aKeyEvent*/, TEventCode eventCode )
       
   155 {
       
   156     if (eventCode == EEventKeyUp)
       
   157     {
       
   158         removePopup();
       
   159     }
       
   160     return EKeyWasConsumed;
       
   161 }
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // WebPopupDrawer::HandlePointerEventL
       
   165 // Handles pointer events
       
   166 // -----------------------------------------------------------------------------
       
   167 //
       
   168 void WebPopupDrawer::handlePointerEventL( const TPointerEvent& /*aPointerEvent*/)
       
   169 {
       
   170     m_webView.IgnoreEventsUntilNextPointerUp();
       
   171     removePopup();
       
   172 }
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // WebPopupDrawer::prepareTextL
       
   176 // Position and wrap the text called during construction and when the size of WebKit changes
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 void WebPopupDrawer::prepareTextL()
       
   180 {
       
   181     delete m_lineWidthArray;
       
   182     m_lineWidthArray = NULL;
       
   183     delete m_wrappedArray;
       
   184     m_wrappedArray = NULL;
       
   185     TInt width = (8 * m_webViewRect.Width()) / 10 - 2 * KBorderSize;
       
   186     createIconIfNeededL();
       
   187     m_font = LatinBold16();
       
   188     m_lineWidthArray = new( ELeave ) CArrayFixFlat<TInt>(2);
       
   189     m_lineWidthArray->AppendL(width - m_icon->Bitmap()->SizeInPixels().iWidth - KIconMargin);
       
   190     TInt maxNumberOfRows = m_webViewRect.Height() / (m_font->HeightInPixels() + KRowMargin);
       
   191     TInt i;
       
   192     for (i = 1; i < maxNumberOfRows; i++) {
       
   193         m_lineWidthArray->AppendL(width);
       
   194     }
       
   195     m_wrappedArray = new( ELeave ) CArrayFixFlat<TPtrC>( 10 );
       
   196     AknTextUtils::WrapToArrayL( *m_textToDraw, *m_lineWidthArray, *m_font, *m_wrappedArray );
       
   197     TInt rowHeight( m_font->HeightInPixels() +  KRowMargin );
       
   198     m_size.iWidth = (8 * m_webViewRect.Width()) / 10;
       
   199     m_size.iHeight = (m_wrappedArray->Count() + 1 ) * rowHeight;
       
   200     if (m_size.iHeight + 2 * KSpaceUnderneath > m_webViewRect.Height()) {
       
   201         TInt toRemove((m_webViewRect.Height() - m_size.iHeight + 2 * KSpaceUnderneath + (rowHeight - 1)) / rowHeight);
       
   202         m_wrappedArray->Delete((m_wrappedArray->Count() - toRemove) / 2, toRemove);
       
   203         m_wrappedArray->Compress();
       
   204         m_size.iHeight = (m_wrappedArray->Count() + 1 ) * rowHeight;
       
   205     }
       
   206     m_position.iX = m_webViewRect.Width() / 10;
       
   207     
       
   208     if ( m_elRect.iBr.iY > 0 && m_elRect.iBr.iY < m_webViewRect.Height() &&
       
   209         m_webViewRect.Height() - m_elRect.iBr.iY - m_size.iHeight - KSpaceUnderneath >= 0) {
       
   210         m_position.iY = m_elRect.iBr.iY;
       
   211     }
       
   212     else if (m_size.iHeight + KSpaceUnderneath <= m_elRect.iTl.iY) {
       
   213         m_position.iY = m_elRect.iTl.iY - m_size.iHeight;
       
   214     }
       
   215     else {
       
   216         m_position.iY = (m_webViewRect.Height() - m_size.iHeight) / 2;// - KSpaceUnderneath;
       
   217     }
       
   218 }
       
   219 
       
   220 // -----------------------------------------------------------------------------
       
   221 // WebPopupDrawer::CreateBitmapAndContextL
       
   222 // Create bitmap and bitmap context
       
   223 // -----------------------------------------------------------------------------
       
   224 //
       
   225 void WebPopupDrawer::createBitmapAndContextL()
       
   226 {
       
   227     delete m_spriteBitmapDevice;
       
   228     m_spriteBitmapDevice = NULL;
       
   229     delete m_spriteBitmapContext;
       
   230     m_spriteBitmapContext = NULL;
       
   231     delete m_spriteBitmap;
       
   232     m_spriteBitmap = NULL;
       
   233     m_spriteBitmap = new (ELeave) CFbsBitmap;
       
   234     User::LeaveIfError( m_spriteBitmap->Create( m_size, EColor16MA ) );
       
   235     m_spriteBitmapDevice = CFbsBitmapDevice::NewL( m_spriteBitmap );
       
   236     User::LeaveIfError( m_spriteBitmapDevice->CreateContext( m_spriteBitmapContext ) );
       
   237     //mask
       
   238     delete m_spriteMaskBitmapDevice;
       
   239     m_spriteMaskBitmapDevice = NULL;
       
   240     delete m_spriteMaskBitmapContext;
       
   241     m_spriteMaskBitmapContext = NULL;
       
   242     delete m_spriteMaskBitmap;
       
   243     m_spriteMaskBitmap = NULL;
       
   244     m_spriteMaskBitmap = new (ELeave) CFbsBitmap;
       
   245     User::LeaveIfError( m_spriteMaskBitmap->Create( m_size, EGray2 ) );
       
   246     m_spriteMaskBitmapDevice = CFbsBitmapDevice::NewL( m_spriteMaskBitmap );
       
   247     User::LeaveIfError( m_spriteMaskBitmapDevice->CreateContext( m_spriteMaskBitmapContext ) );
       
   248     TRect r( TPoint( 0, 0 ), m_spriteMaskBitmap->SizeInPixels());
       
   249     m_spriteMaskBitmapContext->SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   250     m_spriteMaskBitmapContext->SetPenStyle( CGraphicsContext::ESolidPen );
       
   251     m_spriteMaskBitmapContext->SetBrushColor( TRgb( 0, 0, 0) );
       
   252     m_spriteMaskBitmapContext->SetPenColor( TRgb( 0, 0, 0) );
       
   253     m_spriteMaskBitmapContext->DrawRoundRect( r, TSize(KBorderSize * 2, KBorderSize * 2) );
       
   254     if( m_wrappedArray->Count() ) {
       
   255         TRect r( TPoint( 0, 0 ), m_spriteBitmap->SizeInPixels());
       
   256         m_spriteBitmapContext->SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   257         m_spriteBitmapContext->SetPenStyle( CGraphicsContext::ESolidPen );
       
   258         m_spriteBitmapContext->SetBrushColor( TRgb( 0xFF, 0xFF, 0xdd) );
       
   259         m_spriteBitmapContext->SetPenColor( TRgb( 0, 0, 0) );
       
   260         m_spriteBitmapContext->DrawRoundRect( r, TSize(KBorderSize * 2, KBorderSize * 2) );
       
   261         m_spriteBitmapContext->UseFont( m_font );
       
   262         TInt rowHeight( m_font->HeightInPixels() +  KRowMargin );
       
   263         TInt x = KBorderSize;
       
   264         TInt y = KBorderSize + KRowMargin / 2;
       
   265         for( TInt i = 0; i < m_wrappedArray->Count(); i++, y += rowHeight ) {
       
   266             TPtrC text = (m_wrappedArray->At(i));           
       
   267             if (i == 0) {
       
   268                 // draw icon
       
   269                 m_spriteBitmapContext->BitBltMasked(TPoint(x, y - KRowMargin / 2), m_icon->Bitmap(),
       
   270                     TRect(TPoint(0, 0), m_icon->Bitmap()->SizeInPixels()), m_icon->Mask(), EFalse);
       
   271                 m_spriteBitmapContext->DrawText( text, TPoint( x + m_icon->Bitmap()->SizeInPixels().iWidth + KIconMargin, y + rowHeight - m_font->DescentInPixels()) );
       
   272             }
       
   273             else {
       
   274                 m_spriteBitmapContext->DrawText( text, TPoint( x, y + rowHeight - m_font->DescentInPixels()) );
       
   275             }
       
   276         }
       
   277         m_spriteBitmapContext->DiscardFont();
       
   278     }
       
   279 }
       
   280 
       
   281 void WebPopupDrawer::constructSprite()
       
   282 {        
       
   283     m_sprite = RWsSprite(m_webView.brCtl()->CCoeControlParent()->ControlEnv()->WsSession());
       
   284     m_sprite.Construct(m_webView.brCtl()->CCoeControlParent()->ControlEnv()->RootWin(), m_position,ESpriteNoChildClip);
       
   285     TSpriteMember spriteMem;
       
   286     spriteMem.iBitmap = m_spriteBitmap;
       
   287     spriteMem.iMaskBitmap = m_spriteMaskBitmap;
       
   288     spriteMem.iInvertMask = ETrue;
       
   289     m_sprite.AppendMember(spriteMem);
       
   290     m_sprite.Activate();
       
   291 }
       
   292 
       
   293 // -----------------------------------------------------------------------------
       
   294 // WebPopupDrawer::createIconIfNeededL
       
   295 // Determine which icon to load and load it
       
   296 // -----------------------------------------------------------------------------
       
   297 //
       
   298 void WebPopupDrawer::createIconIfNeededL()
       
   299 {
       
   300     if (m_icon == NULL) {
       
   301         _LIT(KHttpScheme, "http");
       
   302         _LIT(KHttpsScheme, "https");
       
   303         _LIT(KFileScheme, "file");
       
   304         _LIT(KMailToScheme, "mailto");
       
   305         _LIT(KTelScheme, "tel");
       
   306         _LIT(KRtspScheme, "rtsp");
       
   307         _LIT(KMmstoScheme, "mmsto");
       
   308         _LIT(KSmsScheme, "sms");
       
   309         WebCannedImages::TCannedImageNames imageType = WebCannedImages::EImageUrlPage;
       
   310         TInt i = m_textToDraw->Locate(':');
       
   311         if (i != KErrNotFound) {
       
   312             TPtrC schemePtr(m_textToDraw->Ptr(), i);
       
   313             if (KHttpScheme().CompareF(schemePtr) == 0) {
       
   314                 imageType = WebCannedImages::EImageUrlPage;
       
   315             }
       
   316             else if (KHttpsScheme().CompareF(schemePtr) == 0) {
       
   317                 imageType = WebCannedImages::EImageUrlPage;
       
   318             }
       
   319             else if (KFileScheme().CompareF(schemePtr) == 0) {
       
   320                 imageType = WebCannedImages::EImageUrlPage;
       
   321             }
       
   322             else if (KMailToScheme().CompareF(schemePtr) == 0) {
       
   323                 imageType = WebCannedImages::EImageUrlEmail;
       
   324             }
       
   325             else if (KTelScheme().CompareF(schemePtr) == 0) {
       
   326                 imageType = WebCannedImages::EImageUrlCall;
       
   327             }
       
   328             else if (KRtspScheme().CompareF(schemePtr) == 0) {
       
   329                 imageType = WebCannedImages::EImageUrlMm;
       
   330             }
       
   331             else if (KMmstoScheme().CompareF(schemePtr) == 0) {
       
   332                 imageType = WebCannedImages::EImageUrlMms;
       
   333             }
       
   334             else if (KSmsScheme().CompareF(schemePtr) == 0) {
       
   335                 imageType = WebCannedImages::EImageUrlEmail;
       
   336             }
       
   337         }
       
   338         TCannedImageData cannedImage = StaticObjectsContainer::instance()->webCannedImages()->getImage(imageType);
       
   339         if (cannedImage.m_msk) {
       
   340             m_icon = CGulIcon::NewL(cannedImage.m_img, cannedImage.m_msk);
       
   341         }
       
   342         else {
       
   343             m_icon = CGulIcon::NewL(cannedImage.m_img);
       
   344         }
       
   345     }
       
   346 }
       
   347 
       
   348 //  End of File