examples/SFExamples/Graphics_Images/src/SplashContainer.cpp

00001 /*
00002 Copyright (c) 2002-2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 #include <EIKENV.H>
00030 #include <EIKAPPUI.H>
00031 #include <e32std.h>
00032 #include <APGICNFL.H>
00033 #include <APGCLI.H>
00034 
00035 #include "SplashContainer.h"
00036 #include <AknUtils.h>
00037 
00038 
00039 // interval used with timer to update the progress bar
00040 const TInt KUpdateInterval = 100000; 
00041 // max number of progress steps.
00042 const TInt KUpdateMaxCount = 20;
00043 
00044 _LIT(KTxTitle           ,"Images Example");
00045 _LIT(KTxLine1           ,"version 1.00");
00046 
00047 // CMySplashContainer
00048 //
00049 /* 
00050 -----------------------------------------------------------------------------
00051 -----------------------------------------------------------------------------
00052 */
00053 CMySplashContainer* CMySplashContainer::NewL(MSplachScreenCallBack&     aCallBack)
00054     {
00055     CMySplashContainer* self = new(ELeave)CMySplashContainer(aCallBack);
00056     CleanupStack::PushL(self);
00057     self->ConstructL();
00058     CleanupStack::Pop(self);
00059     return self;
00060     }
00061 /*
00062 -----------------------------------------------------------------------------
00063 -----------------------------------------------------------------------------
00064 */
00065 CMySplashContainer::~CMySplashContainer()
00066         {
00067     if (iProgressTimer)
00068         {
00069         iProgressTimer->Cancel(); // remember to cancel timer before deleting it
00070         }
00071         
00072     delete iProgressTimer;
00073         delete iBgContext;
00074         }  
00075 
00076 /*
00077 -----------------------------------------------------------------------------
00078 -----------------------------------------------------------------------------
00079 */
00080 CMySplashContainer::CMySplashContainer(MSplachScreenCallBack&   aCallBack)
00081 :iCallBack(aCallBack)
00082         { 
00083         }
00084 /*
00085 -----------------------------------------------------------------------------
00086 -----------------------------------------------------------------------------
00087 */
00088 
00089 void CMySplashContainer::ConstructL(void)
00090     {
00091     CreateWindowL();
00092 
00093 
00094         // make first with no size at all, 
00095         // SetRect() will cause SizeChanged() to be called
00096         // and the real size & position is set in there
00097         
00098         // KAknsIIDQsnBgAreaMain tells system which skin we wish to use
00099         iBgContext = CAknsBasicBackgroundControlContext::NewL(KAknsIIDQsnBgAreaMain,TRect(0,0,1,1), ETrue);
00100         // Setting rect will cause SizeChanged to be called
00101         // and iBgContext size & position is updated accordingly.
00102         SetRect(CEikonEnv::Static()->EikAppUi()->ClientRect());
00103         
00104         ActivateL();    
00105         DrawNow();
00106 
00107         // reset progress counter
00108         iProgressCount = 0;
00109         // and start periodi timer for showing the progress
00110         iProgressTimer = CPeriodic::NewL(CActive::EPriorityStandard);
00111         iProgressTimer->Start(KUpdateInterval, KUpdateInterval, TCallBack(DoProgressL, this));
00112         }
00113 
00114 /*
00115 -----------------------------------------------------------------------------
00116 system calls this function when the size for the container has been chnaged
00117 -----------------------------------------------------------------------------
00118 */
00119 void CMySplashContainer::SizeChanged()
00120         {
00121         if ( iBgContext )
00122                 {
00123                 // set the rect as TRect(0,0,Width,Height)
00124                 iBgContext->SetRect(Rect());
00125 
00126                 if ( &Window() )
00127                         {       // set the position in the full screen
00128                         // so system knows which area of the selected skin is used with this container
00129                         iBgContext->SetParentPos( PositionRelativeToScreen() );
00130                         }
00131                 }
00132 
00133         }
00134 
00135 
00136 /*
00137 -----------------------------------------------------------------------------
00138 used for informinging about resource changes, 
00139 in S60 one usage is to know when the layout of the screen has been changed
00140 -----------------------------------------------------------------------------
00141 */
00142 void CMySplashContainer::HandleResourceChange(TInt aType)
00143         {
00144     if ( aType==KEikDynamicLayoutVariantSwitch )// layout change event
00145         {       
00146         // get new main panel rect and set it
00147         TRect rect;
00148         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
00149         SetRect(rect);
00150         }
00151 
00152         // forward events for CCoeControl's base class handler
00153         CCoeControl::HandleResourceChange(aType);
00154         }
00155 
00156 /*
00157 -----------------------------------------------------------------------------
00158 Skins require containers to provide this function
00159 -----------------------------------------------------------------------------
00160 */
00161 TTypeUid::Ptr CMySplashContainer::MopSupplyObject(TTypeUid aId)
00162         {       
00163         if (iBgContext)
00164                 {       
00165                 return MAknsControlContext::SupplyMopObject(aId, iBgContext );
00166                 }
00167         
00168         return CCoeControl::MopSupplyObject(aId);
00169         }
00170 
00171  
00172 
00173 /*
00174 -------------------------------------------------------------------------------
00175 progress timer callback function
00176 -------------------------------------------------------------------------------
00177 */      
00178 TInt CMySplashContainer::DoProgressL(TAny* aPtr)
00179         {
00180         CMySplashContainer* self = static_cast<CMySplashContainer*>(aPtr);
00181 
00182         // increase the value & draw it to the screen
00183         self->iProgressCount++;
00184         self->DrawNow();
00185         
00186     if (self->iProgressCount > KUpdateMaxCount)
00187         {       // if we got to the max value, we tell the owner that we are done
00188         self->iCallBack.SplashTimeIsUpL();
00189         }
00190     
00191         return KErrNone;
00192         }
00193 /*
00194 -----------------------------------------------------------------------------
00195 normal Draw funtion for CCoeControl, which is used to draw to the screen
00196 -----------------------------------------------------------------------------
00197 */
00198 void CMySplashContainer::Draw(const TRect& /*aRect*/) const
00199         {
00200         CWindowGc& gc = SystemGc();
00201         
00202    
00203         // draw background skin first.
00204         MAknsSkinInstance* skin = AknsUtils::SkinInstance();
00205         AknsDrawUtils::Background( skin, iBgContext, this, gc, Rect() );
00206 
00207 
00208         TRect myRect(Rect());
00209 
00210         TInt totalHNeed = (CEikonEnv::Static()->LegendFont()->HeightInPixels() + (CEikonEnv::Static()->TitleFont()->HeightInPixels() * 2));
00211         
00212         TInt gapp = ((myRect.Height() - totalHNeed) / 7);
00213         
00214         // set rects initially with total rect, so we get X values ok.
00215         TRect titleRect(myRect);
00216         TRect lineRect1(myRect);
00217         
00218         // then calculate the Y values
00219         titleRect.iTl.iY = titleRect.iTl.iY + (2 * gapp);
00220         titleRect.iBr.iY = (titleRect.iTl.iY  + CEikonEnv::Static()->TitleFont()->HeightInPixels());
00221         
00222         // title has 2 gaps before and after
00223         lineRect1.iTl.iY = titleRect.iBr.iY + (2 * gapp);
00224         lineRect1.iBr.iY = (lineRect1.iTl.iY  + CEikonEnv::Static()->LegendFont()->HeightInPixels());
00225 
00226         // set font for the title
00227         gc.UseFont(CEikonEnv::Static()->TitleFont());
00228 
00229         gc.DrawText(KTxTitle,titleRect,CEikonEnv::Static()->TitleFont()->AscentInPixels(), CGraphicsContext::ECenter, 0);
00230         
00231         // set font for the lines, this will Discard the title font
00232         gc.UseFont(CEikonEnv::Static()->LegendFont());
00233         
00234         gc.DrawText(KTxLine1,lineRect1,CEikonEnv::Static()->LegendFont()->AscentInPixels(), CGraphicsContext::ECenter, 0);
00235         
00236         // and finally let the system know that we are done with the font
00237         gc.DiscardFont();
00238 
00239         // calculate position for the progress bar
00240         TInt wGap = (Rect().Width() / 5);
00241         
00242         TRect progressRect((Rect().iTl.iX + wGap),0,(Rect().iBr.iX - wGap),0);
00243         
00244         progressRect.iTl.iY = (lineRect1.iBr.iY + (2 * gapp));
00245         progressRect.iBr.iY = (progressRect.iTl.iY + CEikonEnv::Static()->TitleFont()->HeightInPixels());
00246         
00247         // draw the progress background
00248         gc.SetPenColor(KRgbBlack);
00249         // Brish to Null, thus progress bar rect is transparent
00250         gc.SetBrushStyle(CGraphicsContext::ENullBrush);
00251         // draw the bounding rect for the progress bar
00252         gc.DrawRect(progressRect);
00253         
00254         // calculate the progress bar size
00255         TInt progressSize = ((progressRect.Width() * iProgressCount) / KUpdateMaxCount);
00256         progressRect.iBr.iX = (progressRect.iTl.iX + progressSize);
00257         
00258         // draw the progress
00259         gc.SetBrushColor(KRgbGreen);
00260         // brush set to solid to get the background of the progress bar visible
00261         gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
00262         gc.DrawRect(progressRect);
00263         
00264         // set brush to NULL so it wont efect any other drawing later on
00265         gc.SetBrushStyle(CGraphicsContext::ENullBrush);
00266         }
00267 

Generated by  doxygen 1.6.2