examples/SFExamples/Graphics_Images/src/ControlsContainer.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 <eiklabel.h>
00030 #include <eikenv.h>
00031 
00032 #include <aknutils.h> 
00033 
00034 
00035 #include "ControlsContainer.h"
00036 
00037 #include "Image_Control.h"
00038 #include "Animation_Control.h"
00039 
00040 /*
00041 -----------------------------------------------------------------------------
00042 -----------------------------------------------------------------------------
00043 */ 
00044 CControlsContainer* CControlsContainer::NewL( const TRect& aRect )
00045         {
00046         CControlsContainer* self = CControlsContainer::NewLC( aRect );
00047         CleanupStack::Pop( self );
00048         return self;
00049         }
00050 /*
00051 -----------------------------------------------------------------------------
00052 -----------------------------------------------------------------------------
00053 */ 
00054 CControlsContainer* CControlsContainer::NewLC( const TRect& aRect )
00055         {
00056         CControlsContainer* self = new ( ELeave ) CControlsContainer;
00057         CleanupStack::PushL( self );
00058         self->ConstructL( aRect );
00059         return self;
00060         }
00061 /*
00062 -----------------------------------------------------------------------------
00063 -----------------------------------------------------------------------------
00064 */ 
00065 CControlsContainer::~CControlsContainer()
00066         {
00067         delete iImageControl;
00068         delete iAnimationControl;
00069         delete iBitmap;
00070         }
00071 /*
00072 -----------------------------------------------------------------------------
00073 -----------------------------------------------------------------------------
00074 */      
00075 void CControlsContainer::ConstructL( const TRect& aRect )
00076         {
00077         CreateWindowL(); // create window for this container
00078         SetRect( aRect );// will set the size and system will call SizeChanged()-function
00079         
00080         ActivateL(); // activete this container
00081 
00082         // set the rects used for drawing the border frames as a base
00083         // they are calculated in the SizeChanged()-function
00084     TRect tmpAnimationRect(iAnimationRect);
00085     TRect tmpImageRect(iImageRect);
00086 
00087         // then shrink them a bit to get better visibility for the border frames
00088         tmpAnimationRect.Shrink(2,2);
00089         tmpImageRect.Shrink(2,2);       
00090                 
00091         TFindFile imageFile3(CCoeEnv::Static()->FsSession());
00092         if(KErrNone == imageFile3.FindByDir(KTxImage3, KNullDesC))
00093                 {       // and construct second image control
00094                 iImageControl = CImageControl::NewL(*this,tmpImageRect,imageFile3.File());
00095                 }
00096         
00097         TFindFile bitFile(CCoeEnv::Static()->FsSession());
00098         if(KErrNone == bitFile.FindByDir(KTxBitmap2, KNullDesC))
00099                 {       
00100                 // construct the bitmap
00101                 // which after we have valid bitmap instance
00102                 // i.e. the pointer is valid
00103                 iBitmap = new(ELeave)CFbsBitmap();
00104                 
00105                 // then load the actual image data to the bitmap
00106                 TInt err = iBitmap->Load(bitFile.File(),0);
00107                 // if Err == KErrNone, then it is ok
00108                 // othervise it is most likely invalidand Handle will show it
00109                 // we could also use User::LeaveIfError()
00110                 // then instead of getting blank screen without image
00111                 // we would have a leave during costruction
00112                 // which would cause application to crash
00113                 }
00114 
00115         TFindFile aniFile3(CCoeEnv::Static()->FsSession());
00116         if(KErrNone == aniFile3.FindByDir(KTxAnimation3, KNullDesC))
00117                 {       // and construct first animation control
00118                 iAnimationControl = CAnimationControl::NewL(*this,tmpAnimationRect,aniFile3.File());
00119                 }
00120         
00121         DrawNow();
00122         }
00123 
00124 /*
00125 -----------------------------------------------------------------------------
00126 normal CCoeControl function used to tell system how many 
00127 controls this container has 
00128 -----------------------------------------------------------------------------
00129 */ 
00130 TInt CControlsContainer::CountComponentControls() const
00131         {
00132         if(iImageControl
00133         &&  iAnimationControl )
00134                 { // if we have both controls we tell that to the system
00135                 return 2;
00136                 }
00137         else
00138                 {
00139                 return 0;
00140                 }
00141         }
00142 
00143 /*
00144 -----------------------------------------------------------------------------
00145 normal CCoeControl function used to give pointers 
00146 to the controls inside this container 
00147 -----------------------------------------------------------------------------
00148 */ 
00149 CCoeControl* CControlsContainer::ComponentControl( TInt aIndex ) const
00150         {
00151         switch (aIndex)
00152                 {
00153                 case 0:
00154                         return iImageControl;                   
00155                 
00156                 case 1:
00157                         return iAnimationControl;                       
00158                 
00159                 default:
00160                         return NULL;
00161                 }
00162         }
00163 /*
00164 -----------------------------------------------------------------------------
00165 normal key handling function for CCoeControl, gets all key events
00166 when this container is on focus (and it has been added to the control stack)
00167 -----------------------------------------------------------------------------
00168 */
00169 
00170 TKeyResponse CControlsContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode /*aType*/)
00171         {
00172         TKeyResponse  myRet = EKeyWasNotConsumed;
00173 
00174         switch (aKeyEvent.iCode)
00175             {
00176             case EKeyDevice3: 
00177                         {// ok button pressed, we could do something with the selected item
00178         /*                      switch(iSelected)
00179                                         {
00180                                         case 0: // First image
00181                                                 break;
00182                                         case 1: // first animation
00183                                                 break;
00184                                         case 2: // second animation
00185                                                 break;
00186                                         case 3: // second image
00187                                                 break;
00188                                         };*/
00189                         }
00190                         break;
00191                 // arrows are used for changing the selection
00192                 // DrawNow(); is called so all changes are also drawn into the screen
00193                 // calling DrawNow(); will make system to call Draw() function later.
00194         
00195                 case EKeyLeftArrow:
00196 
00197                         if(iSelected == 1)
00198                                 {
00199                                 iSelected = 0;
00200                                 }
00201                         else if(iSelected == 3)
00202                                 {
00203                                 iSelected = 2; 
00204                                 }
00205                         DrawNow();
00206                         break;
00207         
00208                 case EKeyRightArrow:
00209         
00210                         if(iSelected == 0)
00211                                 {
00212                                 iSelected = 1;
00213                                 }
00214                         else if(iSelected == 2)
00215                                 {
00216                                 iSelected = 3; 
00217                                 }
00218                         DrawNow();
00219                         break;
00220         
00221                 case EKeyUpArrow:
00222         
00223                         if(iSelected == 2)
00224                                 {
00225                                 iSelected = 0;
00226                                 }
00227                         else if(iSelected == 3)
00228                                 {
00229                                 iSelected = 1; 
00230                                 }
00231                         DrawNow();
00232                         break;
00233 
00234         
00235                 case EKeyDownArrow:
00236         
00237                         if(iSelected == 0)
00238                                 {
00239                                 iSelected = 2;
00240                                 }
00241                         else if(iSelected == 1)
00242                                 {
00243                                 iSelected = 3; 
00244                                 }
00245                         DrawNow();
00246                         break;
00247                 default:
00248                         break;
00249                 }        
00250 
00251         return myRet;
00252 }       
00253         
00254 /*
00255 -----------------------------------------------------------------------------
00256 normal Draw funtion for CCoeControl, which is used to draw to the screen
00257 -----------------------------------------------------------------------------
00258 */ 
00259 void CControlsContainer::Draw( const TRect& /*aRect*/ ) const
00260         {
00261         CWindowGc& gc = SystemGc();
00262         // clear the area with default brush (solid white)
00263         gc.Clear();
00264 
00265         // draw first image control border as lines with default pen (black & solid)
00266         gc.DrawLine(iBitmapRect2.iTl,TPoint(iBitmapRect2.iTl.iX,iBitmapRect2.iBr.iY));
00267         gc.DrawLine(iBitmapRect2.iTl,TPoint(iBitmapRect2.iBr.iX,iBitmapRect2.iTl.iY));
00268         gc.DrawLine(TPoint(iBitmapRect2.iTl.iX,iBitmapRect2.iBr.iY),iBitmapRect2.iBr);
00269         gc.DrawLine(TPoint(iBitmapRect2.iBr.iX,iBitmapRect2.iTl.iY),iBitmapRect2.iBr);
00270 
00271         // draw second image  control border as lines with default pen (black & solid)
00272         gc.DrawLine(iAnimationRect.iTl,TPoint(iAnimationRect.iTl.iX,iAnimationRect.iBr.iY));
00273         gc.DrawLine(iAnimationRect.iTl,TPoint(iAnimationRect.iBr.iX,iAnimationRect.iTl.iY));
00274         gc.DrawLine(TPoint(iAnimationRect.iTl.iX,iAnimationRect.iBr.iY),iAnimationRect.iBr);
00275         gc.DrawLine(TPoint(iAnimationRect.iBr.iX,iAnimationRect.iTl.iY),iAnimationRect.iBr);
00276 
00277         // draw first animation  control border as lines with default pen (black & solid)
00278         gc.DrawLine(iImageRect.iTl,TPoint(iImageRect.iTl.iX,iImageRect.iBr.iY));
00279         gc.DrawLine(iImageRect.iTl,TPoint(iImageRect.iBr.iX,iImageRect.iTl.iY));
00280         gc.DrawLine(TPoint(iImageRect.iTl.iX,iImageRect.iBr.iY),iImageRect.iBr);
00281         gc.DrawLine(TPoint(iImageRect.iBr.iX,iImageRect.iTl.iY),iImageRect.iBr);
00282 
00283         // draw second animation control border as lines with default pen (black & solid)
00284         gc.DrawLine(iBitmapRect.iTl,TPoint(iBitmapRect.iTl.iX,iBitmapRect.iBr.iY));
00285         gc.DrawLine(iBitmapRect.iTl,TPoint(iBitmapRect.iBr.iX,iBitmapRect.iTl.iY));
00286         gc.DrawLine(TPoint(iBitmapRect.iTl.iX,iBitmapRect.iBr.iY),iBitmapRect.iBr);
00287         gc.DrawLine(TPoint(iBitmapRect.iBr.iX,iBitmapRect.iTl.iY),iBitmapRect.iBr);
00288         
00289         // get the rect for the selected control for border drawing.
00290         TRect selRect(iBitmapRect2);
00291         switch(iSelected)
00292                 {
00293                 case 0: // First image
00294                         selRect = iBitmapRect2;
00295                         break;
00296                 case 1: // first animation
00297                         selRect = iAnimationRect;
00298                         break;
00299                 case 2: // second animation
00300                         selRect = iBitmapRect;
00301                         break;
00302                 case 3: // second image 
00303                         selRect = iImageRect;
00304                         break;
00305                 }       
00306         
00307         // set pen color for the drawing the border for selected control
00308         gc.SetPenColor(KRgbGreen);
00309 
00310         // draw first line of the border 
00311         gc.DrawLine(selRect.iTl,TPoint(selRect.iTl.iX,selRect.iBr.iY));
00312         gc.DrawLine(selRect.iTl,TPoint(selRect.iBr.iX,selRect.iTl.iY));
00313         gc.DrawLine(TPoint(selRect.iTl.iX,selRect.iBr.iY),selRect.iBr);
00314         gc.DrawLine(TPoint(selRect.iBr.iX,selRect.iTl.iY),selRect.iBr);
00315         
00316         // grow the rect for selected items border 
00317         selRect.Grow(1,1);
00318         // and draw the second line of the border 
00319         gc.DrawLine(selRect.iTl,TPoint(selRect.iTl.iX,selRect.iBr.iY));
00320         gc.DrawLine(selRect.iTl,TPoint(selRect.iBr.iX,selRect.iTl.iY));
00321         gc.DrawLine(TPoint(selRect.iTl.iX,selRect.iBr.iY),selRect.iBr);
00322         gc.DrawLine(TPoint(selRect.iBr.iX,selRect.iTl.iY),selRect.iBr); 
00323 
00324         TRect tmpBitmapRect(iBitmapRect);
00325         tmpBitmapRect.Shrink(2,2);
00326         
00327         TRect tmpBitmapRect2(iBitmapRect2);
00328         tmpBitmapRect2.Shrink(2,2);
00329         
00330     if(iBitmap)// check that we have  constructed the bitmap
00331                 {
00332                 // and that the bitmap has valid data for drawing
00333                 if(iBitmap->Handle())
00334                         {
00335                         // just draw the image to the whole screen
00336                         // without caring about the scaling issues
00337                         gc.DrawBitmap(tmpBitmapRect,iBitmap);   
00338                         gc.DrawBitmap(tmpBitmapRect2,iBitmap);  
00339                         }
00340                 }
00341         }
00342 
00343 
00344 
00345 /*
00346 -----------------------------------------------------------------------------
00347 used for informinging about resource changes, 
00348 in S60 one usage is to know when the layout of the screen has been changed
00349 -----------------------------------------------------------------------------
00350 */
00351 void CControlsContainer::HandleResourceChange(TInt aType)
00352         {
00353     if ( aType==KEikDynamicLayoutVariantSwitch )// layout change event
00354         {   // get new main panel rect and set it
00355         TRect rect;
00356         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
00357         SetRect(rect);
00358         }
00359 
00360         // forward events for CCoeControl's base class handler
00361         CCoeControl::HandleResourceChange(aType);
00362         }
00363 
00364 /*
00365 -----------------------------------------------------------------------------
00366 system calls this function when the size for the container has been chnaged
00367 -----------------------------------------------------------------------------
00368 */ 
00369 void CControlsContainer::SizeChanged()
00370         {
00371     TRect rect(Rect());
00372     
00373     rect.Shrink(2,2);
00374     
00375     iImageRect = iBitmapRect = iBitmapRect2 = iAnimationRect = rect;
00376     
00377     // calculate the half points for width & height
00378     TInt helfHeight= (rect.Height()/ 2);
00379     TInt helfWidth = (rect.Width() / 2);
00380     
00381     // set the bottom Y values for first row controls
00382     iBitmapRect2.iBr.iY = iAnimationRect.iBr.iY = (iAnimationRect.iTl.iY + helfHeight);
00383     
00384     // set the bottom Y values for second row controls
00385     iImageRect.iTl.iY = iBitmapRect.iTl.iY = iBitmapRect2.iBr.iY;
00386     
00387     // set the bottom X values for first row controls
00388     iBitmapRect2.iBr.iX = (iBitmapRect2.iTl.iX + helfWidth);
00389     iAnimationRect.iTl.iX = iBitmapRect2.iBr.iX;
00390     
00391     // set the bottom X values for second row controls
00392     iBitmapRect.iBr.iX = (iBitmapRect.iTl.iX + helfWidth);
00393     iImageRect.iTl.iX = iBitmapRect.iBr.iX;
00394     
00395     // set the rects used for drawinf the border frames as a base
00396     TRect tmpAnimationRect(iAnimationRect);
00397     TRect tmpImageRect(iImageRect);
00398    
00399         // then shrink them a bit to get better visibility for the border frames
00400         tmpAnimationRect.Shrink(2,2);
00401         tmpImageRect.Shrink(2,2);
00402           
00403     if(iAnimationControl)
00404         {       
00405         iAnimationControl->SetRect(tmpAnimationRect);
00406         }
00407     
00408     if(iImageControl)
00409         {
00410         iImageControl->SetRect(tmpImageRect);
00411         }
00412         }
00413  
00414 
00415         
00416 // End of File

Generated by  doxygen 1.6.2