examples/Graphics/coverflow/src/ticker.cpp

Go to the documentation of this file.
00001 // ticker.cpp
00002 //
00003 // Copyright (c) 2005-2008 Symbian Ltd.  All rights reserved.
00004 //
00010 #include "ticker.h"
00011 #include <openfont.h>
00012 
00016 CTicker::CTicker(const TRect& aRect, const TDesC& aString):
00017         iRect(aRect),
00018         iString(aString)
00019         {
00020 #ifdef PORTRAIT_MODE
00021         iPos = TPoint(KFontHeight, 0);
00022 #else
00023         iPos = TPoint(aRect.iBr.iX, KFontHeight);
00024 #endif
00025         }
00026 
00033 CTicker* CTicker::NewL(const TRect& aRect, const TDesC& aString)
00034         {
00035         CTicker* self = new(ELeave) CTicker(aRect, aString);
00036         CleanupStack::PushL(self);
00037         self->ConstructL();
00038         CleanupStack::Pop(self);
00039         return self;
00040         }
00041 
00045 void CTicker::ConstructL()
00046         {
00047         // Connects the client session to the window server.
00048         User::LeaveIfError(iWs.Connect());
00049         
00050         // Constructs a new screen device attached to a particular window server session.
00051         iScr = new(ELeave) CWsScreenDevice(iWs);
00052         User::LeaveIfError(iScr->Construct());
00053         
00054         // Constructs a graphics context.
00055         iGc = new(ELeave) CWindowGc(iScr);
00056         User::LeaveIfError(iGc->Construct());
00057 
00058         // Creates a sessionless, uninitialised window group handle.
00059         iGrp = RWindowGroup(iWs);
00060         User::LeaveIfError(iGrp.Construct(0xdeadface, EFalse));
00061         iGrp.SetOrdinalPositionErr(0, 100);
00062         
00063         // Creates a sessionless, uninitialised window handle.
00064         iWin = RWindow(iWs);
00065         User::LeaveIfError(iWin.Construct(iGrp, (TInt)this));
00066         
00067         // Sets the window to be transparent using the alpha channel.
00068         iWin.SetTransparencyAlphaChannel();
00069         
00070         // Sets the background colour used for clearing in server-initiated redraws.
00071         iWin.SetBackgroundColor(TRgb(0,0,0,0));
00072         
00073         // Sets the size and position of a window.
00074         iWin.SetExtent(iRect.iTl, iRect.Size());
00075         
00076         // Displays the window and enables it to receive events.
00077         iWin.Activate();
00078         
00079         // Sets the ordinal position of a window.
00080         iWin.SetOrdinalPosition(-1);
00081         
00082         // Sets the window's visibility.
00083         iWin.SetVisible(EFalse);
00084         
00085         // Sends all pending commands in the buffer to the window server.
00086         iWs.Flush();
00087         
00088         // Specify the font specification that allows more attributes than TOpenFontSpec.
00089         TOpenFontSpec openSpec;
00090         
00091         // Sets whether the font should be drawn using anti-aliasing
00092     openSpec.SetBitmapType(EAntiAliasedGlyphBitmap);
00093     
00094     // Specify the font specification
00095         TFontSpec fs;
00096         
00097         // Gets the TFontSpec corresponding to this Open Font System font specification.
00098     openSpec.GetTFontSpec(fs);
00099     
00100     // Assign the font specification
00101     fs.iTypeface.iName = _L("SwissA");
00102     fs.iHeight = KFontHeight;
00103 
00104         // Gets the font which is the nearest to the given font specification.
00105         User::LeaveIfError(iScr->GetNearestFontInPixels(iFont, fs));
00106         iLen = iFont->TextWidthInPixels(iString);
00107         
00108         // Allocates and constructs a CPeriodic object. Assign a priority to it.
00109         iTimer = CPeriodic::NewL(CActive::EPriorityStandard);
00110         }
00111 
00115 CTicker::~CTicker()
00116         {
00117         Stop();
00118         delete iTimer;
00119         if(iScr)
00120                 {
00121                 // Releases a specified font.
00122                 iScr->ReleaseFont(iFont);
00123                 }
00124         delete iGc;
00125         delete iScr;
00126         
00127         // Close the nodes.
00128         iWin.Close();
00129         iGrp.Close();
00130         iWs.Close();
00131         }
00132 
00136 void CTicker::Start()
00137         {
00138         // Sets the window's visibility.
00139         iWin.SetVisible(ETrue);
00140         if (!iTimer->IsActive())
00141                 {
00142                 // Starts generating periodic events. Pass the OnTick() function to call when the CPeriodic is scheduled after a timer event.
00143                 iTimer->Start(0, 20*1000, TCallBack(CTicker::OnTick, this));
00144                 }
00145         }
00146         
00150 void CTicker::Stop()
00151         {
00152         // Sets the window's visibility.
00153         iWin.SetVisible(EFalse);
00154         
00155         // Sends all pending commands in the buffer to the window server.
00156         iWs.Flush();
00157         
00158         // Cancels the wait for completion of an outstanding request.
00159         iTimer->Cancel();
00160         }
00161 
00165 TInt CTicker::OnTick(TAny* aArg)
00166         {
00167         CTicker* self = (CTicker*)aArg;
00168         
00169         // Invalidates the entire window.
00170         self->iWin.Invalidate();
00171         
00172         // Begins redrawing the window's invalid region
00173         self->iWin.BeginRedraw();
00174         
00175         // Activates the context for a given window 
00176         self->iGc->Activate(self->iWin);
00177         self->Draw();
00178         
00179         // Frees the graphics context to be used with another window.
00180         self->iGc->Deactivate();
00181         
00182         // Ends the current redraw.
00183         self->iWin.EndRedraw();
00184         
00185         // Sends all pending commands in the buffer to the window server.
00186         self->iWs.Flush();
00187 
00188         return 0;
00189         }
00190 
00194 TBool CTicker::Draw()
00195         {
00196 #ifdef PORTRAIT_MODE
00197         ++iPos.iY;
00198 
00199         if (iPos.iY > iLen)
00200                 {
00201                 iPos.iY = 0;
00202                 return ETrue;
00203                 }
00204 #else
00205         --iPos.iX;
00206 
00207         if (iPos.iX < -iLen)
00208                 {
00209                 iPos.iX = iRect.iBr.iX;
00210                 return ETrue;
00211                 }
00212 
00213 #endif
00214         
00215         // Sets the line drawing size for the pen.
00216         iGc->SetPenSize(TSize(1,1));
00217         
00218         // Sets the pen colour.
00219         iGc->SetPenColor(TRgb(255,255,255,255));
00220         
00221         // Sets the line drawing style for the pen
00222         iGc->SetPenStyle(CGraphicsContext::ESolidPen);
00223         
00224         // Sets this context's font.
00225         iGc->UseFont(iFont);
00226 
00227 #ifdef PORTRAIT_MODE
00228         iGc->DrawTextVertical(iString, iPos, ETrue);
00229 #else
00230         iGc->DrawText(iString, iPos);
00231 #endif
00232         
00233         // Discards a font.
00234         iGc->DiscardFont();
00235 
00236         return EFalse;
00237         }

Generated by  doxygen 1.6.2