Nokia N97 SDK
Example Applications Guide

guiengineappview.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : guiengineappview.cpp
00004 *  Part of     : GuiEngine
00005 *  Interface   :
00006 *  Description :
00007 *  Version     :
00008 *
00009 *  Copyright (c) 2004 - 2006 Nokia Corporation.
00010 *  This material, including documentation and any related
00011 *  computer programs, is protected by copyright controlled by
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 
00017 // INCLUDE FILES
00018 #include <coemain.h>
00019 #include <eikenv.h>
00020 #include <AknUtils.h>
00021 
00022 #include "GuiEngine.pan"
00023 #include "GuiEngineAppView.h"
00024 #include "GuiEngineDocument.h"
00025 #include "Circle.h"
00026 #include "Rectangle.h"
00027 
00028 
00029 using namespace NShapes;
00030 
00031 // ========================= MEMBER FUNCTIONS ==================================
00032 
00033 // ------------------------------------------------------------------------------
00034 // CGuiEngineAppView::CGuiEngineAppView()
00035 // C++ default constructor can NOT contain any code, that might leave.
00036 // ------------------------------------------------------------------------------
00037 //
00038 CGuiEngineAppView::CGuiEngineAppView( CGuiEngineDocument* aDocument )
00039 : iDocument(aDocument), iPosition ( 10, 10 )
00040     {
00041     // No implementation required
00042     }
00043 
00044 // -----------------------------------------------------------------------------
00045 // CGuiEngineAppView::NewL()
00046 // Two-phased constructor.
00047 // -----------------------------------------------------------------------------
00048 //
00049 CGuiEngineAppView* CGuiEngineAppView::NewL( const TRect& aRect,
00050                                             CGuiEngineDocument* aDocument )
00051     {
00052     CGuiEngineAppView* self = NewLC( aRect, aDocument );
00053     CleanupStack::Pop( self );
00054     return self;
00055     }
00056 
00057 // -----------------------------------------------------------------------------
00058 // CGuiEngineAppView::NewLC()
00059 // Two-phased constructor.
00060 // -----------------------------------------------------------------------------
00061 //
00062 CGuiEngineAppView* CGuiEngineAppView::NewLC( const TRect& aRect,
00063                                              CGuiEngineDocument* aDocument )
00064     {
00065     CGuiEngineAppView* self = new ( ELeave ) CGuiEngineAppView( aDocument );
00066     CleanupStack::PushL( self );
00067     self->ConstructL( aRect );
00068     return self;
00069     }
00070 
00071 // ------------------------------------------------------------------------------
00072 // CGuiEngineAppView::ConstructL()
00073 // Symbian 2nd phase constructor can leave.
00074 // ------------------------------------------------------------------------------
00075 //
00076 void CGuiEngineAppView::ConstructL( const TRect& aRect )
00077     {
00078     // Create a window for this application view
00079     CreateWindowL();
00080 
00081     // Set the windows size
00082     SetRect( aRect );
00083 
00084     // Activate the window, which makes it ready to be drawn
00085     ActivateL();
00086 
00087     Window().SetPointerCursor( KBlackPointer );
00088     }
00089 
00090 // -----------------------------------------------------------------------------
00091 // CGuiEngineAppView::~CGuiEngineAppView()
00092 // Destructor.
00093 // -----------------------------------------------------------------------------
00094 //
00095 CGuiEngineAppView::~CGuiEngineAppView()
00096     {
00097     iDocument = NULL;   //  Not the owner
00098     }
00099 
00100 // -----------------------------------------------------------------------------
00101 // CGuiEngineAppView::Draw()
00102 // Draws the display
00103 // -----------------------------------------------------------------------------
00104 //
00105 void CGuiEngineAppView::Draw( const TRect& /*aRect*/ ) const
00106     {
00107 
00108     CWindowGc& graphicsContext = SystemGc();
00109 
00110     // Clear the application view
00111     graphicsContext.Clear();
00112 
00113         // Gets the control's extent
00114         TRect rect( Rect() );
00115         graphicsContext.DrawRect( rect );
00116 
00117     // Draw the 'cursor' crosshair, size 10 pixels by 10 pixels
00118     graphicsContext.SetPenSize( TSize( 1,1 ) );
00119 
00120     //graphicsContext.SetPenColor( RgbBlack );
00121     graphicsContext.DrawLine ( TPoint( iPosition.iX - 10, iPosition.iY ),
00122                                TPoint( iPosition.iX + 10, iPosition.iY ) );
00123 
00124     graphicsContext.DrawLine ( TPoint( iPosition.iX, iPosition.iY - 10 ),
00125                                TPoint( iPosition.iX, iPosition.iY + 10 ) );
00126 
00127 
00128     // Draw all the current shapes
00129     TShape* shape = iDocument->Model()->GetNextShape();
00130     while ( shape )
00131     {
00132         shape->Draw( graphicsContext );
00133         shape = iDocument->Model()->GetNextShape();
00134         }
00135     }
00136 
00137 // -----------------------------------------------------------------------------
00138 // CGuiEngineAppView::Clear()
00139 // Clears the display
00140 // -----------------------------------------------------------------------------
00141 //
00142 void CGuiEngineAppView::Clear()
00143     {
00144     iDocument->Model()->Clear();
00145     DrawNow();
00146     }
00147 
00148 // -----------------------------------------------------------------------------
00149 // CGuiEngineAppView::SetBrushShapeType()
00150 // Sets the BrushShapeType
00151 // -----------------------------------------------------------------------------
00152 //
00153 void CGuiEngineAppView::SetBrushShapeType( TBrushShapeType aShapeType )
00154     {
00155     iBrushShapeType = aShapeType;
00156     }
00157 
00158 // -----------------------------------------------------------------------------
00159 // CGuiEngineAppView::BrushShapeType()
00160 // Get the BrushShapeType
00161 // -----------------------------------------------------------------------------
00162 //
00163 CGuiEngineAppView::TBrushShapeType CGuiEngineAppView::BrushShapeType() const
00164     {
00165     return iBrushShapeType;
00166     }
00167 
00168 // -----------------------------------------------------------------------------
00169 // CGuiEngineAppView::ExternalizeL()
00170 // Externalizes BrushShapeType (write)
00171 // -----------------------------------------------------------------------------
00172 //
00173 void CGuiEngineAppView::ExternalizeL( RWriteStream& aStream ) const
00174     {
00175     aStream.WriteInt8L( static_cast<TInt8>( iBrushShapeType ) );
00176     }
00177 
00178 // -----------------------------------------------------------------------------
00179 // CGuiEngineAppView::InternalizeL()
00180 // Internalize BrushShapeType (read)
00181 // -----------------------------------------------------------------------------
00182 //
00183 void CGuiEngineAppView::InternalizeL( RReadStream& aStream )
00184     {
00185     iBrushShapeType = static_cast<TBrushShapeType>( aStream.ReadInt8L() );
00186     }
00187 
00188 
00189 
00190 // -----------------------------------------------------------------------------
00191 // CGuiEngineAppView::OfferKeyEventL()
00192 // Notify key events.
00193 // -----------------------------------------------------------------------------
00194 //
00195 TKeyResponse CGuiEngineAppView::OfferKeyEventL( const TKeyEvent& aKeyEvent,
00196                                                 TEventCode aType)
00197     {
00198     if ( aType != EEventKey )
00199         {
00200         return EKeyWasNotConsumed;
00201         }
00202 
00203     // Move left
00204     if ( aKeyEvent.iScanCode == EStdKeyLeftArrow )
00205         {
00206         if ( iPosition.iX >  ( Rect().iTl.iX + 10 ) )
00207             {
00208             --iPosition.iX;
00209             DrawNow();
00210             }
00211         return EKeyWasConsumed;
00212         }
00213 
00214     // Move right
00215     else if ( aKeyEvent.iScanCode == EStdKeyRightArrow )
00216         {
00217         if ( iPosition.iX < ( Rect().iBr.iX - 10 ) )
00218             {
00219             ++iPosition.iX;
00220             DrawNow();
00221             }
00222         return EKeyWasConsumed;
00223         }
00224 
00225     // Move up
00226     else if ( aKeyEvent.iScanCode == EStdKeyUpArrow )
00227         {
00228         if (iPosition.iY > ( Rect().iTl.iY + 10 ) )
00229             {
00230             --iPosition.iY;
00231             DrawNow();
00232             }
00233         return EKeyWasConsumed;
00234         }
00235 
00236     // Move down
00237     else if ( aKeyEvent.iScanCode == EStdKeyDownArrow )
00238         {
00239         if ( iPosition.iY < ( Rect().iBr.iY - 10 ) )
00240             {
00241             ++iPosition.iY;
00242             DrawNow();
00243             }
00244         return EKeyWasConsumed;
00245         }
00246 
00247 
00248     // Place a shape
00249     else if ( aKeyEvent.iScanCode == EStdKeyDevice3 )
00250         {
00251 
00252         TShape* newShape = NULL;
00253         // Update the co-ordinates in the model to the position at which the
00254         // pointer event occurred.
00255 
00256         switch ( iBrushShapeType )
00257             {
00258             case ECircle:
00259 
00260                 newShape = new ( ELeave ) TCircle( iPosition, KBrushRadius );
00261 
00262                 //  Takes ownership
00263                 iDocument->Model()->AddShapeL( newShape );
00264                 break;
00265 
00266             case ERectangle:
00267                 newShape = new ( ELeave ) TRectangle( iPosition, KBrushHeight, KBrushWidth );
00268 
00269                 //  Takes ownership
00270                 iDocument->Model()->AddShapeL( newShape );
00271                 break;
00272 
00273             default:
00274                 User::Panic( KGuiEnginePanicName, EGuiEngineInvalidBrushType );
00275             }
00276 
00277         DrawNow();
00278         return EKeyWasConsumed;
00279         }
00280 
00281     return EKeyWasNotConsumed;
00282     }
00283 
00284 
00285 
00286 // -----------------------------------------------------------------------------
00287 // CGuiEngineAppView::HandleResourceChange()
00288 // Handles responses to screen size changes (and resource changes)
00289 // -----------------------------------------------------------------------------
00290 //
00291 void CGuiEngineAppView::HandleResourceChange( TInt aType )
00292         {
00293         CCoeControl::HandleResourceChange( aType );
00294     // ADDED FOR SCALABLE UI SUPPORT
00295     // *****************************
00296     if ( aType==KEikDynamicLayoutVariantSwitch )
00297         {
00298         TRect rect;
00299         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
00300         SetRect(rect);
00301         }
00302         }
00303 
00304 
00305 // End of File

© Nokia 2009

Back to top