examples/SFExamples/Graphics_Images/src/TextControl.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 "TextControl.h"
00033 
00034 #include <aknutils.h> 
00035 
00036 
00037 // example text used for showing in this control
00038 
00039 // time value used for scrolling with the peridic timer 
00040 const TInt KScrollInterval = 200000;
00041 
00042 /*
00043 -----------------------------------------------------------------------------
00044 -----------------------------------------------------------------------------
00045 */ 
00046 CTextControl* CTextControl::NewL(const TDesC& aText)
00047         {
00048         CTextControl* self = CTextControl::NewLC(aText);
00049         CleanupStack::Pop( self );
00050         return self;
00051         }
00052 /*
00053 -----------------------------------------------------------------------------
00054 -----------------------------------------------------------------------------
00055 */ 
00056 CTextControl* CTextControl::NewLC(const TDesC& aText)
00057         {
00058         CTextControl* self = new ( ELeave ) CTextControl(aText);
00059         CleanupStack::PushL( self );
00060         self->ConstructL();
00061         return self;
00062         }
00063         
00064 /*
00065 -----------------------------------------------------------------------------
00066 -----------------------------------------------------------------------------
00067 */ 
00068 CTextControl::CTextControl(const TDesC& aText):iText(aText)
00069         {
00070         // nothing to do..
00071         }
00072 /*
00073 -----------------------------------------------------------------------------
00074 -----------------------------------------------------------------------------
00075 */ 
00076 CTextControl::~CTextControl()
00077         {
00078     if (iScrollTimer)
00079         {
00080         iScrollTimer->Cancel();
00081         }
00082         
00083     delete iScrollTimer;
00084         }
00085 /*
00086 -----------------------------------------------------------------------------
00087 -----------------------------------------------------------------------------
00088 */      
00089 void CTextControl::ConstructL(void)
00090         {
00091         iUseFont = AknLayoutUtils::FontFromId(EAknLogicalFontSecondaryFont);
00092 
00093         iCutOff = 0;
00094         iScrollTimer = CPeriodic::NewL(CActive::EPriorityStandard);
00095         iScrollTimer->Start(KScrollInterval, KScrollInterval, TCallBack(ScrollText, this));
00096         }
00097 /*
00098 -------------------------------------------------------------------------------
00099 -------------------------------------------------------------------------------
00100 */      
00101 TInt CTextControl::ScrollText(TAny* aPtr)
00102         {
00103         CTextControl* self = static_cast<CTextControl*>(aPtr);
00104         
00105         if(self->iUseFont)
00106                 {
00107                 self->iCutOff++;
00108                 
00109                 if(self->iText.Length() <= self->iCutOff)
00110                         {
00111                         self->iCutOff = 0;
00112                         }
00113                 
00114                 TInt TextWidth = self->iUseFont->TextWidthInPixels(self->iText.Right(self->iText.Length() - self->iCutOff));
00115                 
00116                 if(TextWidth < (self->Rect().Width() / 2))
00117                         {
00118                         self->iCutOff = 0;      
00119                         }
00120                 
00121                 self->DrawNow();
00122                 }
00123     
00124         return KErrNone;
00125         }
00126 /*
00127 -----------------------------------------------------------------------------
00128 normal Draw funtion for CCoeControl, which is used to draw to the screen
00129 -----------------------------------------------------------------------------
00130 */ 
00131 void CTextControl::Draw( const TRect& /*aRect*/ ) const
00132         {
00133         CWindowGc& gc = SystemGc();
00134         gc.SetBrushStyle(CGraphicsContext::ENullBrush);
00135         gc.Clear(Rect());
00136 
00137         if(iUseFont)
00138                 {
00139                 gc.SetPenColor(KRgbBlack);
00140                 gc.UseFont(iUseFont);
00141 
00142                 if(iCutOff <= 0)
00143                         {
00144                         gc.DrawText(iText,Rect(),iUseFont->AscentInPixels(), CGraphicsContext::ELeft, 0);
00145                         }
00146                 else if(iCutOff < iText.Length())
00147                         {
00148                         gc.DrawText(iText.Right(iText.Length() - iCutOff),Rect(),iUseFont->AscentInPixels(), CGraphicsContext::ELeft, 0);
00149                         }
00150                 
00151                 gc.DiscardFont();
00152                 }
00153         }
00154         
00155 // End of File

Generated by  doxygen 1.6.2