screengrabber/src/SGMainContainer.cpp
branchRCL_3
changeset 21 b3cee849fa46
parent 20 48060abbbeaf
child 22 fad26422216a
equal deleted inserted replaced
20:48060abbbeaf 21:b3cee849fa46
     1 /*
       
     2 * Copyright (c) 2009 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 "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 
       
    20 // INCLUDE FILES
       
    21 #include "SGMainContainer.h"
       
    22 
       
    23 
       
    24 #include <AknBidiTextUtils.h>
       
    25 #include <AknUtils.h>
       
    26 #include <aknnotewrappers.h>
       
    27 #include <eiksbfrm.h>
       
    28 #include <AknsDrawUtils.h> 
       
    29 #include <AknsBasicBackgroundControlContext.h>
       
    30 #include <akntitle.h>
       
    31 #include <eikspane.h>  
       
    32 #include <AknDef.h>
       
    33 
       
    34 const TUint KLeftMargin = 2;
       
    35 const TUint KRightMargin = 2;
       
    36 
       
    37 
       
    38 // ================= MEMBER FUNCTIONS =======================
       
    39 
       
    40 
       
    41 void CScreenGrabberMainContainer::ConstructL(const TRect& aRect)
       
    42     {
       
    43     iSkinContext = NULL;
       
    44 
       
    45     CreateWindowL();
       
    46     Window().SetShadowDisabled(EFalse);
       
    47     
       
    48     SetBlank();
       
    49     
       
    50     // set title of the app
       
    51     CEikStatusPane* statusPane = iEikonEnv->AppUiFactory()->StatusPane();
       
    52     CAknTitlePane* title = static_cast<CAknTitlePane*>( statusPane->ControlL( TUid::Uid( EEikStatusPaneUidTitle ) ) );
       
    53     title->SetTextL( _L("Screen Grabber") );
       
    54 
       
    55     iText = HBufC::NewL(200000);
       
    56     iCurrentLine = 0;
       
    57     iLineCount = 0;
       
    58     iNumberOfLinesFitsScreen = 0;
       
    59     iX_factor = 1;
       
    60     iY_factor = 1;
       
    61     iLeftDrawingPosition = KLeftMargin;
       
    62 
       
    63     // get font
       
    64     iFont = AknLayoutUtils::FontFromId(EAknLogicalFontSecondaryFont);
       
    65 
       
    66     iWrappedArray = new(ELeave) CArrayFixFlat<TPtrC>(500);
       
    67     
       
    68     SetRect(aRect);
       
    69     ActivateL(); 
       
    70 
       
    71     PrintText(_L("Please use the settings screen to define the capture mode and any other related settings such capture hotkey and saving format. Using the PNG format for screen shots is recommended, since it is compressed but lossless. The captures can be managed with the Media Gallery application.\n\nYou may start taking screenshots or capturing video now. Please press the Applications key to go back to the Application menu or send this application to the background via the Options menu. To be able to take screenshots or capture video, this applications needs to stay running in the background.\n"));
       
    72     }
       
    73 
       
    74 CScreenGrabberMainContainer::~CScreenGrabberMainContainer()
       
    75     {
       
    76     if (iSkinContext)
       
    77     	delete iSkinContext;
       
    78     
       
    79 	if (iWrappedArray)
       
    80 		{
       
    81     	iWrappedArray->Reset();
       
    82     	delete iWrappedArray;
       
    83 		}
       
    84 
       
    85 	if (iText)
       
    86     	delete iText;
       
    87 
       
    88     if (iScrollBarFrame)
       
    89         delete iScrollBarFrame;
       
    90 
       
    91     if (iWrapperString)
       
    92         delete iWrapperString;
       
    93     }
       
    94 
       
    95 
       
    96 // ---------------------------------------------------------
       
    97 // CScreenGrabberMainContainer::UpdateVisualContentL()
       
    98 // ---------------------------------------------------------
       
    99 //
       
   100 void CScreenGrabberMainContainer::UpdateVisualContentL(TBool aScrollToBottom)
       
   101     {
       
   102     TSize rectSize;
       
   103     AknLayoutUtils::LayoutMetricsSize(AknLayoutUtils::EMainPane, rectSize);
       
   104     TRect rect(rectSize);
       
   105 
       
   106     // set the correct drawing area for the skin background
       
   107     if(iSkinContext)
       
   108         {
       
   109         delete iSkinContext;
       
   110         iSkinContext = NULL;
       
   111         }  
       
   112     iSkinContext = CAknsBasicBackgroundControlContext::NewL(KAknsIIDQsnBgAreaMain, rectSize, EFalse);
       
   113 
       
   114     // init scroll bar if not yet done
       
   115     if (!iScrollBarFrame)
       
   116         {
       
   117         iScrollBarFrame = new(ELeave) CEikScrollBarFrame(this, this, ETrue);
       
   118 
       
   119         CAknAppUi* appUi = iAvkonAppUi;
       
   120         
       
   121 	    if (AknLayoutUtils::DefaultScrollBarType(appUi) == CEikScrollBarFrame::EDoubleSpan)
       
   122 	        {
       
   123 		    iScrollBarFrame->CreateDoubleSpanScrollBarsL(ETrue, EFalse);            
       
   124 		    iScrollBarFrame->SetTypeOfVScrollBar(CEikScrollBarFrame::EDoubleSpan);
       
   125 	        }
       
   126 	    else
       
   127 	        {
       
   128 		    iScrollBarFrame->SetTypeOfVScrollBar(CEikScrollBarFrame::EArrowHead);
       
   129 	        }  
       
   130 	    iScrollBarFrame->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
       
   131         }
       
   132 
       
   133     // drawing positions, needed for scalable ui
       
   134     iX_factor = TReal(rect.Width()) / 176;
       
   135     iY_factor = TReal(rect.Height()) / 144;
       
   136 
       
   137     iWrappedArray->Reset();
       
   138 
       
   139     if (iWrapperString)
       
   140         {
       
   141         delete iWrapperString;
       
   142         iWrapperString = NULL;
       
   143         }
       
   144 
       
   145     // define drawing width, some extra space needed if using double span scrolling bar
       
   146     TUint drawingWidth(0);
       
   147     if (iScrollBarFrame->TypeOfVScrollBar() == CEikScrollBarFrame::EDoubleSpan)
       
   148         {
       
   149         if (AknLayoutUtils::LayoutMirrored())  // scroll bar on 'left'
       
   150             {
       
   151             iLeftDrawingPosition = KLeftMargin + 12;
       
   152             drawingWidth = TInt( rect.Width() - iLeftDrawingPosition*iX_factor - (KRightMargin*iX_factor)); 
       
   153             }
       
   154         else // scroll bar on 'right'
       
   155             {
       
   156             iLeftDrawingPosition = KLeftMargin;  
       
   157             drawingWidth = TInt( rect.Width() - iLeftDrawingPosition*iX_factor - (KRightMargin*iX_factor + 7*iX_factor)); 
       
   158             }        
       
   159         }
       
   160     else
       
   161         drawingWidth = TInt( rect.Width() - KLeftMargin*iX_factor - KRightMargin*iX_factor);
       
   162 
       
   163     // wrap the text
       
   164     iWrapperString = AknBidiTextUtils::ConvertToVisualAndWrapToArrayL(
       
   165         iText->Des(),
       
   166         drawingWidth,
       
   167         *iFont,
       
   168         *iWrappedArray
       
   169         );
       
   170     
       
   171     iLineCount = iWrappedArray->Count();
       
   172 
       
   173     // count amount of lines fits to screen
       
   174     iNumberOfLinesFitsScreen = TInt(rect.Height() / (iFont->HeightInPixels()));
       
   175 
       
   176     // check if needed to scroll to the bottom
       
   177     if (aScrollToBottom && iCurrentLine < iLineCount - iNumberOfLinesFitsScreen)
       
   178         {
       
   179         iCurrentLine = iLineCount-iNumberOfLinesFitsScreen;
       
   180         }
       
   181 
       
   182     // update the scroll bars
       
   183 	TEikScrollBarModel horizontalBar;
       
   184     TEikScrollBarModel verticalBar;
       
   185     verticalBar.iThumbPosition = iCurrentLine;
       
   186     verticalBar.iScrollSpan = iLineCount - iNumberOfLinesFitsScreen + 1;
       
   187     verticalBar.iThumbSpan = 1;
       
   188 
       
   189     TEikScrollBarFrameLayout layout;
       
   190 	layout.iTilingMode = TEikScrollBarFrameLayout::EInclusiveRectConstant;
       
   191 
       
   192     if (iScrollBarFrame->TypeOfVScrollBar() == CEikScrollBarFrame::EDoubleSpan)
       
   193         {    
       
   194         // do not let scrollbar values overflow
       
   195         if (verticalBar.iThumbPosition + verticalBar.iThumbSpan > verticalBar.iScrollSpan)
       
   196             verticalBar.iThumbPosition = verticalBar.iScrollSpan - verticalBar.iThumbSpan;
       
   197 		
       
   198         TAknDoubleSpanScrollBarModel horizontalDSBar(horizontalBar);
       
   199         TAknDoubleSpanScrollBarModel verticalDSBar(verticalBar);
       
   200        
       
   201         iScrollBarFrame->TileL(&horizontalDSBar, &verticalDSBar, rect, rect, layout);        
       
   202         iScrollBarFrame->SetVFocusPosToThumbPos(verticalDSBar.FocusPosition());
       
   203 	    }  
       
   204 	else
       
   205 	    {
       
   206 		iScrollBarFrame->TileL( &horizontalBar, &verticalBar, rect, rect, layout );
       
   207 		iScrollBarFrame->SetVFocusPosToThumbPos( verticalBar.iThumbPosition );
       
   208 	    }
       
   209 
       
   210 
       
   211     // update the screen
       
   212     DrawNow();
       
   213     }
       
   214 
       
   215 // ---------------------------------------------------------
       
   216 // CScreenGrabberMainContainer::Draw(const TRect& aRect) const
       
   217 // ---------------------------------------------------------
       
   218 //
       
   219 void CScreenGrabberMainContainer::Draw(const TRect& aRect) const
       
   220     {
       
   221     CWindowGc& gc = SystemGc();
       
   222 
       
   223     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   224     if (iSkinContext)
       
   225 	    {
       
   226         // draws the skin background
       
   227 	    AknsDrawUtils::Background(skin, iSkinContext, this, gc, aRect);
       
   228 	    }
       
   229     else
       
   230         {
       
   231         gc.Clear(aRect);
       
   232         }
       
   233 
       
   234 
       
   235     // get the text color from the skin   
       
   236     TRgb skinPenColor;
       
   237     if (AknsUtils::GetCachedColor( skin, skinPenColor, KAknsIIDQsnTextColors, EAknsCIQsnTextColorsCG6 ) == KErrNone)
       
   238         {
       
   239 		gc.SetPenColor(skinPenColor);
       
   240 		}	
       
   241     else
       
   242         {
       
   243         gc.SetPenColor(KRgbBlack);
       
   244         }
       
   245 
       
   246     gc.UseFont(iFont);
       
   247 
       
   248     // draw the text
       
   249     for (TInt i=0; i<iNumberOfLinesFitsScreen; i++)
       
   250         {
       
   251         // index out of bounds check and then draw text
       
   252         if (i+iCurrentLine < iWrappedArray->Count())
       
   253             gc.DrawText(iWrappedArray->At(i+iCurrentLine), TPoint(TInt(iLeftDrawingPosition*iX_factor), TInt(iFont->HeightInPixels()*(i+1))));
       
   254         }
       
   255 
       
   256     gc.DiscardFont();
       
   257     }
       
   258 
       
   259 // ---------------------------------------------------------
       
   260 // CScreenGrabberMainContainer::HandleControlEventL(
       
   261 //     CCoeControl* aControl,TCoeEvent aEventType)
       
   262 // ---------------------------------------------------------
       
   263 //
       
   264 void CScreenGrabberMainContainer::HandleControlEventL(
       
   265     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
       
   266     {
       
   267     }
       
   268 
       
   269 // ---------------------------------------------------------
       
   270 // CScreenGrabberMainContainer::PrintText(const TDesC& aDes)
       
   271 // Print text into output window.
       
   272 // ---------------------------------------------------------
       
   273 //
       
   274 void CScreenGrabberMainContainer::PrintText(const TDesC& aDes)
       
   275     {
       
   276     iText->Des().Append( aDes );
       
   277     UpdateVisualContentL(EFalse);  // do not automatically scroll to the bottom
       
   278     }
       
   279 
       
   280 // ---------------------------------------------------------
       
   281 // CScreenGrabberMainContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,
       
   282 //                                    TEventCode aType)
       
   283 // Handle key event. Only up and down key arrow events are
       
   284 // consumed in order to enable scrolling in output window.
       
   285 // ---------------------------------------------------------
       
   286 //
       
   287 TKeyResponse CScreenGrabberMainContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
       
   288     {
       
   289     if(aType != EEventKey)
       
   290         return EKeyWasNotConsumed;
       
   291     
       
   292     if(aKeyEvent.iCode == EKeyUpArrow)
       
   293         {
       
   294         if (iCurrentLine > 0)
       
   295             {
       
   296             iCurrentLine--;
       
   297             UpdateVisualContentL(EFalse);
       
   298             }
       
   299 
       
   300         return EKeyWasConsumed;
       
   301         }
       
   302     
       
   303     else if(aKeyEvent.iCode == EKeyDownArrow)
       
   304         {
       
   305         if (iCurrentLine < iLineCount - iNumberOfLinesFitsScreen)
       
   306             {
       
   307             iCurrentLine++;
       
   308             UpdateVisualContentL(EFalse);
       
   309             }
       
   310 
       
   311         return EKeyWasConsumed;
       
   312         }
       
   313     
       
   314     return EKeyWasNotConsumed;
       
   315     }
       
   316 
       
   317 
       
   318 // ---------------------------------------------------------
       
   319 // CScreenGrabberMainContainer::ClearOutputWindow()
       
   320 // Clear the output window.
       
   321 // ---------------------------------------------------------
       
   322 //
       
   323 void CScreenGrabberMainContainer::ClearOutputWindow()
       
   324     {
       
   325     iText->Des() = _L("");
       
   326     UpdateVisualContentL(ETrue);
       
   327     }
       
   328 
       
   329 
       
   330 // ---------------------------------------------------------
       
   331 // CScreenGrabberMainContainer::HandleResourceChange(TInt aType)
       
   332 // Handle layout change of the screen
       
   333 // ---------------------------------------------------------
       
   334 //
       
   335 void CScreenGrabberMainContainer::HandleResourceChange(TInt aType)
       
   336     {
       
   337     if (aType == KEikDynamicLayoutVariantSwitch || aType == KAknsMessageSkinChange)
       
   338         {
       
   339         TRect mainPaneRect;
       
   340         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, mainPaneRect);
       
   341         SetRect(mainPaneRect);
       
   342 
       
   343         iCurrentLine = 0; // scroll to top
       
   344 
       
   345         // update font
       
   346         iFont = AknLayoutUtils::FontFromId(EAknLogicalFontSecondaryFont);
       
   347 
       
   348         // delete the scroll and update screen
       
   349         if (iScrollBarFrame)
       
   350             {
       
   351             delete iScrollBarFrame;
       
   352             iScrollBarFrame = NULL;
       
   353             }
       
   354         UpdateVisualContentL(EFalse);
       
   355         }
       
   356     else
       
   357         {
       
   358         CCoeControl::HandleResourceChange(aType);
       
   359         }
       
   360     }
       
   361 
       
   362 // ---------------------------------------------------------
       
   363 // CScreenGrabberMainContainer::MopSupplyObject(TTypeUid aId)
       
   364 // Pass skin information if needed
       
   365 // ---------------------------------------------------------
       
   366 //
       
   367 TTypeUid::Ptr CScreenGrabberMainContainer::MopSupplyObject(TTypeUid aId)
       
   368     {
       
   369     if (aId.iUid == MAknsControlContext::ETypeId && iSkinContext)
       
   370         {
       
   371         return MAknsControlContext::SupplyMopObject(aId, iSkinContext);
       
   372         }
       
   373 
       
   374     return CCoeControl::MopSupplyObject(aId);
       
   375     }
       
   376 
       
   377 
       
   378 // ---------------------------------------------------------
       
   379 // CScreenGrabberMainContainer::HandleScrollEventL()
       
   380 // Capture touch events on the scroll bar
       
   381 // ---------------------------------------------------------
       
   382 //
       
   383 void CScreenGrabberMainContainer::HandleScrollEventL(CEikScrollBar* aScrollBar, TEikScrollEvent aEventType)
       
   384     {
       
   385     // only on page up/down,scroll up/down and drag events
       
   386     if ((aEventType == EEikScrollPageDown) || (aEventType == EEikScrollPageUp) || 
       
   387        (aEventType == EEikScrollThumbDragVert) || (aEventType == EEikScrollUp) ||
       
   388        (aEventType == EEikScrollDown))
       
   389         {
       
   390         // get the current position from the scroll bar
       
   391         iCurrentLine = aScrollBar->ThumbPosition();
       
   392         
       
   393         // refresh now
       
   394         UpdateVisualContentL(EFalse);
       
   395         }
       
   396     }
       
   397 
       
   398     
       
   399 // End of File