examples/Graphics/WS/transparent/transparent_util.cpp

Go to the documentation of this file.
00001 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // This provides various functions for window event handling, control and access.
00015 //
00016 
00017 
00018 
00023 #include "transparent.h"
00028 static TInt CallDoChangeTransparencyL(TAny *aPtr)
00029         {
00030         return (static_cast<CTransparentUtil *> (aPtr)->DoChangeTransparencyL());
00031         }
00032 
00037 static TInt CallDoSetTransparentRegion(TAny *aPtr)
00038         {
00039         return (static_cast<CTransparentUtil *> (aPtr)->DoSetTransparentRegion());
00040         }
00041 
00045 CTransparentUtil::CTransparentUtil()
00046         {
00047         }
00048 
00053 CTransparentUtil* CTransparentUtil::NewL()
00054         {
00055         CTransparentUtil* self = new(ELeave) CTransparentUtil();
00056         CleanupStack::PushL(self);
00057         self->ConstructL();
00058         CleanupStack::Pop(self);
00059         return self;
00060         }
00061 
00065 CTransparentUtil::~CTransparentUtil()
00066         {
00067         if(iPeriodicObject->IsActive())
00068             {
00069             iPeriodicObject->Cancel();
00070             }
00071         delete iPeriodicObject;
00072         delete iBitmapGc;
00073         delete iBitmapDevice;
00074         delete iEventHandler;
00075         
00076         iForegndWindowTrans.Close();
00077         iFgndWinTransRegion.Close();    
00078         iBackgndWindow.Close();
00079         iGroup.Close();
00080 
00081         delete iGc;
00082         delete iScr;
00083         iWs.Close();
00084         }
00085 
00093 void CTransparentUtil::ConstructL()
00094         {
00095         // Connect the client session to the window server.
00096         User::LeaveIfError(iWs.Connect());
00097 
00098         // Construct a new screen device attached to the window server session.
00099         iScr = new(ELeave) CWsScreenDevice(iWs);
00100         User::LeaveIfError(iScr->Construct(0)); // screen number (0 is first screen)
00101 
00102         // Create the graphics context  but do not initialise
00103         iGc = new(ELeave) CWindowGc(iScr);
00104 
00105         // Complete construction
00106         User::LeaveIfError(iGc->Construct());
00107 
00108         // Create an initialised window group handle within the session.
00109         iGroup  = RWindowGroup(iWs);
00110         User::LeaveIfError(iGroup.Construct((TUint)&iGroup, ETrue));
00111 
00112         // Initialise the background window handle
00113         iBackgndWindow = RWindow(iWs);
00114 
00115         // Initialise the foreground window handle to demonstrate TransparencyVariance(). 
00116         iForegndWindowTrans = RWindow(iWs);
00117 
00118     // Initialise the foreground window handle to demonstrate TransparentRegionL()
00119         iFgndWinTransRegion = RWindow(iWs);
00120         
00121         // Create the CPeriodic object to generate periodic events
00122         iPeriodicObject=CPeriodic::NewL(CActive::EPriorityUserInput);
00123         
00124     // Construct the event handler
00125     iEventHandler = CEventHandler::NewL(iWs);   
00126 
00127         // Construct foreground and background windows
00128         User::LeaveIfError(iBackgndWindow.Construct(iGroup, (TUint)&iBackgndWindow));
00129         User::LeaveIfError(iForegndWindowTrans.Construct(iGroup, (TUint)&iForegndWindowTrans));
00130         User::LeaveIfError(iFgndWinTransRegion.Construct(iGroup, (TUint)&iFgndWinTransRegion));
00131 
00132         iForegndWindowTrans.SetRequiredDisplayMode(EColor16MA); 
00133         iFgndWinTransRegion.SetRequiredDisplayMode(EColor16MA); 
00134         
00135     // Set background window size
00136     TSize bgRectSize(iScr->SizeInPixels().iWidth/2,iScr->SizeInPixels().iHeight);
00137     iBackgndWindow.SetExtent(TPoint(),bgRectSize);
00138     iBackgndWindow.Activate();
00139 
00140     // Set the size of the foreground window to span the upper half of the background window
00141     TSize fgRectSize(iScr->SizeInPixels().iWidth/2,iScr->SizeInPixels().iHeight/2);
00142     iForegndWindowTrans.SetExtent(TPoint(),fgRectSize);
00143     //Enable the use of alpha channel to vary foreground window's transparency
00144     User::LeaveIfError(iForegndWindowTrans.SetTransparencyAlphaChannel());
00145     iForegndWindowTrans.Activate();
00146     
00147     // Set the size of the foreground window to overlap the bottom half of the background window
00148     iFgndWinTransRegion.SetExtent(TPoint(0, iScr->SizeInPixels().iHeight/2),fgRectSize);
00149 
00150     //Enable the use of alpha channel to vary foreground window's transparency
00151     User::LeaveIfError(iFgndWinTransRegion.SetTransparencyAlphaChannel()); 
00152     iFgndWinTransRegion.Activate();
00153     
00154     TRect circleRect(TPoint(80,50), TSize(170, 170));   
00155     //Draw the background window using the colour specified by the second parameter 
00156     //and draw a circle on top of it with the colour specified by the third parameter. 
00157     _LIT(KBackGrndWindow, "Background window");
00158     DrawWindow(iBackgndWindow, TRgb(234,234,236), TRgb(0,255,0), circleRect , KBackGrndWindow);
00159     
00160     //issue request to the event handler. 
00161     iEventHandler->IssueRequest();
00162         }
00163 
00171 void CTransparentUtil::DrawWindow(RWindow& aWindow, TRgb aColor1, TRgb aColor2, TRect aRect, const TDesC& aText)
00172         {
00173         aWindow.Invalidate();
00174         aWindow.BeginRedraw();
00175         iGc->Activate(aWindow);
00176 
00177         iGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
00178         iGc->SetPenStyle(CGraphicsContext::ENullPen);
00179         iGc->SetBrushColor(aColor1);
00180         iGc->Clear();
00181         iGc->SetBrushColor(aColor2);
00182         iGc->DrawEllipse(aRect);
00183     iGc->UseFont(iEikonEnv->NormalFont());
00184     iGc->DrawText(aText, TPoint(10, aWindow.Size().iHeight/2 + 5));
00185         iGc->Deactivate();
00186         aWindow.EndRedraw();
00187         iWs.Flush();
00188         }
00189 
00197 void CTransparentUtil::TransparencyVarianceL()
00198         { 
00199         //Clear the foreground windows to the selected colour. 
00200         ClearWindow(iWs, iForegndWindowTrans, iGc, TRgb::Gray16(10));
00201               
00202         // Issue request to the active scheduler after receiving the event.
00203          if((iPeriodicObject->IsActive()))
00204              {       
00205              iPeriodicObject->Cancel();
00206              }
00207      iPeriodicObject->Start(0,KDelay,TCallBack(CallDoChangeTransparencyL,this));
00208         }
00209 
00217 void CTransparentUtil::ClearWindow(RWsSession& aSession, RWindow& aWindow, CWindowGc* aGc, TRgb aColor)
00218         {
00219         aWindow.Invalidate();
00220         aWindow.BeginRedraw();
00221         aGc->Activate(aWindow);
00222         aGc->SetBrushColor(aColor);
00223         aGc->Clear();
00224         aGc->Deactivate();
00225         aWindow.EndRedraw();
00226         aSession.Flush();
00227         }
00228 
00234 TBool CTransparentUtil::DoChangeTransparencyL()
00235         {
00236         //iAlphaValue is the alpha value and the foreground window is redrawn to show 
00237         //periodic transparency change. 
00238         iForegndWindowTrans.SetBackgroundColor( TRgb(255,0, 0, iAlphaValue));
00239 
00240         TRect drawEllipse(TPoint(170, 45), TSize(65, 45));
00241         _LIT(KForeGrndWindowforTrans, "Foreground window for Vary Tra...");
00242         DrawWindow(iForegndWindowTrans, TRgb(255,0,0,iAlphaValue), TRgb(100,100,100, iAlphaValue), drawEllipse, KForeGrndWindowforTrans);
00243 
00244         iAlphaValue += 20;
00245         if (iAlphaValue < 255)
00246                 {
00247                 return ETrue;
00248                 }
00249         else
00250                 {
00251                 iAlphaValue=0;
00252                 iPeriodicObject->Cancel();
00253                 return EFalse;
00254                 }
00255         }
00256 
00263 void CTransparentUtil::TransparentRegionL()
00264         {   
00265     //iBoolVisible is a boolean variable used to toggle the visibility of foreground
00266     //window on TransparentRegionL().. button press
00267     if(iBoolVisible)
00268         {
00269         iFgndWinTransRegion.SetVisible(EFalse);
00270         iBoolVisible = EFalse; 
00271         iWs.Flush();
00272         }
00273     else
00274         {  
00275         iFgndWinTransRegion.SetVisible(ETrue);       
00276         
00277         //set background colour of the foreground window as blue and alpha value as 50. 
00278         iFgndWinTransRegion.SetBackgroundColor(TRgb(0, 0 , 255 , 50));
00279     
00280         //create a rectsize which is half the size of the foreground window. 
00281         TSize rectsize1(iScr->SizeInPixels().iWidth/2,iScr->SizeInPixels().iHeight/4);
00282         TRect chopRect1(TPoint(0, iScr->SizeInPixels().iHeight/4), rectsize1);
00283         
00284         //Take a region which is the lower half of the foreground window. 
00285         iRegion = TRegionFix<2>(chopRect1);
00286         //Use SetTransparentRegion() to keep only the region specified by iRegion as transparent.
00287         iFgndWinTransRegion.SetTransparentRegion(iRegion);
00288     
00289         //Invalidate and begin redraw on the opaque region of the foreground window. 
00290         TRect chopRect2(TPoint(0, 0), rectsize1);
00291         //Draw a circle on the opaque region of the foreground window. 
00292         TRect circleRect2(TPoint(60,20), TSize(40, 40));
00293         _LIT(KOpqRegion, "Opaque region of the transparent window");
00294         DrawWindowRegion(iFgndWinTransRegion, TRgb(255, 255, 0), TRgb(100, 100, 0), chopRect2, circleRect2, KOpqRegion, TPoint(10, 15)); 
00295         
00296         //Invalidate and begin redraw on the transparent region of the foreground window. 
00297         TRect circleRect3(TPoint(110,80), TSize(40, 40));
00298         _LIT(KTransRegion, "Transparent region of the transparent window");
00299         DrawWindowRegion(iFgndWinTransRegion, TRgb(0, 0 , 255 , 50), TRgb(255,0,0, 100), chopRect1, circleRect3, KTransRegion, TPoint(10, 75)); 
00300         iBoolVisible = ETrue; 
00301         }
00302         }
00303 
00314 void CTransparentUtil::DrawWindowRegion(RWindow& aWindow, TRgb aColor1, TRgb aColor2, TRect aChopRect, TRect aRect, const TDesC& aText, const TPoint& aPoint)
00315     {
00316     aWindow.Invalidate(aChopRect);
00317     aWindow.BeginRedraw(aChopRect);
00318     iGc->Activate(aWindow);
00319 
00320     iGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
00321     iGc->SetPenStyle(CGraphicsContext::ENullPen);
00322 
00323     iGc->SetBrushColor(aColor1);
00324     iGc->Clear();
00325         
00326     iGc->SetBrushColor(aColor2);
00327     iGc->DrawEllipse(aRect);
00328     iGc->SetBrushColor(TRgb(0, 0, 0)); 
00329     iGc->UseFont(iEikonEnv->NormalFont());
00330     iGc->DrawText(aText, aPoint);
00331     iGc->Deactivate();
00332     aWindow.EndRedraw();
00333     iWs.Flush();
00334     }

Generated by  doxygen 1.6.2