examples/Graphics/coverflow/src/ticker.cpp

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

Generated by  doxygen 1.6.2