examples/ForumNokia/BluetoothPMPExample/src/BluetoothPMPExampleRichTextEditorRTE.cpp

00001 /*
00002  * Copyright (c) 2009-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  */
00029 
00030 // INCLUDES
00031 #include "BluetoothPMPExamplerichtexteditorrte.h"
00032 #include <barsread.h> // TResourceReader
00033 #include <BtPmpEx.rsg> // resources
00034 #include <eikrted.h> // CEikRichTextEditor
00035 #include <txtrich.h> // CRichText
00036 
00037 _LIT(KTextLines, "---------------------------");
00038 #define NEW_TEXT_TO_TOP //If defined the new text will be written to
00039                         //the top of the container
00040 // ----------------------------------------------------------------------------
00041 // CRichTextEditorRTE::CRichTextEditorRTE(void)
00042 //
00043 // Standard symbian OS 2nd phase constructor
00044 // ----------------------------------------------------------------------------
00045 void CRichTextEditorRTE::ConstructL()
00046     {
00047     TResourceReader reader;
00048 
00049     // Construct RichTextEditor from resource
00050     iCoeEnv->CreateResourceReaderLC(reader, R_RICHTEXTEDITOR_RICH_TEXT_EDITOR);
00051 
00052     ConstructFromResourceL(reader);
00053 
00054     CleanupStack::PopAndDestroy(); // reader
00055 
00056     // Sets that the control has keyboard focus
00057     SetFocus(ETrue);
00058     }
00059 
00060 
00061 // ----------------------------------------------------------------------------
00062 // CRichTextEditorRTE::CRichTextEditorRTE(void)
00063 //
00064 // Constructor
00065 // ----------------------------------------------------------------------------
00066 CRichTextEditorRTE::CRichTextEditorRTE(void)
00067     {
00068     }
00069 
00070 // ----------------------------------------------------------------------------
00071 // CRichTextEditorRTE::AddCarriageReturnL()
00072 //
00073 // Insert one line break at the end of the text.
00074 // ----------------------------------------------------------------------------
00075 void CRichTextEditorRTE::AddCarriageReturnL(TInt pos)
00076     {
00077     CRichText* richText = RichText();
00078     TInt carriagePosition = pos;
00079     if(pos == KCarriageReturnToEnd)
00080         {
00081         carriagePosition = richText->DocumentLength();
00082         }
00083 
00084     richText->InsertL(carriagePosition, CEditableText::ELineBreak);
00085     }
00086 
00087 
00088 // ----------------------------------------------------------------------------
00089 // CRichTextEditorRTE::AddTextL(const TDesC& aText)
00090 //
00091 // Draws text using black color.
00092 // ----------------------------------------------------------------------------
00093 void CRichTextEditorRTE::AddTextL(const TDesC& aText)
00094     {
00095     CRichText* text = RichText();
00096     TInt textSize = text->DocumentLength();
00097     TInt position = textSize;
00098     #ifdef NEW_TEXT_TO_TOP
00099         position = 0;
00100     #endif
00101 
00102     // Interested in color
00103     iCharacterFormatMask.SetAttrib(EAttColor);
00104     // Set it to Black
00105     iCharacterFormat.iFontPresentation.iTextColor = KRgbBlack;
00106     
00107     text->InsertL (position, aText);
00108 
00109     // Apply formatting
00110     text->ApplyCharFormatL(iCharacterFormat, iCharacterFormatMask,
00111                            position,aText.Length());
00112 
00113     AddCarriageReturnL(position);
00114 
00115     HandleTextChangedL();
00116 
00117 
00118     #ifdef NEW_TEXT_TO_TOP
00119         //Keep displaying the top of the screen
00120         MoveCursorL (TCursorPosition::EFPageUp, EFalse);
00121     #else
00122         //Keep displaying the bottom of the screen
00123         MoveCursorL (TCursorPosition::EFPageDown, EFalse);
00124     #endif    
00125     }
00126 
00127 // ----------------------------------------------------------------------------
00128 // CRichTextEditorRTE::DrawTextWithoutCarriageL( const TDesC& aText )
00129 //
00130 // Draw text without adding one line break at the end of the text.
00131 // ----------------------------------------------------------------------------
00132 void CRichTextEditorRTE::DrawTextWithoutCarriageL( const TDesC& aText )
00133     {
00134     CRichText* text = RichText();
00135     TInt textSize = text->DocumentLength();
00136 
00137     // Interested in color
00138     iCharacterFormatMask.SetAttrib(EAttColor);
00139     // Set it to Black
00140     iCharacterFormat.iFontPresentation.iTextColor = KRgbBlack;
00141     text->InsertL (textSize, aText);
00142     // Apply formatting
00143     text->ApplyCharFormatL(iCharacterFormat, iCharacterFormatMask,
00144                            textSize,aText.Length());
00145     HandleTextChangedL();
00146     }
00147 
00148 
00149 // ----------------------------------------------------------------------------
00150 // CRichTextEditorRTE::DrawTextWithoutCarriageL( const TDesC& aText )
00151 //
00152 //  Sets underline on or off.  This will be applied to text added in AddTextL()
00153 // ----------------------------------------------------------------------------
00154 void CRichTextEditorRTE::SetTextUnderlineOn(TBool aUnderlineOn)
00155     {
00156     iCharacterFormatMask.SetAttrib(EAttFontUnderline);
00157     if (aUnderlineOn)
00158         {
00159         iCharacterFormat.iFontPresentation.iUnderline = EUnderlineOn;
00160         }
00161     else
00162         {
00163         iCharacterFormat.iFontPresentation.iUnderline = EUnderlineOff;
00164         }
00165     }
00166 
00167 // ----------------------------------------------------------------------------
00168 // CRichTextEditorRTE::DrawLineL()
00169 //
00170 // Draw one line.
00171 // ----------------------------------------------------------------------------
00172 void CRichTextEditorRTE::DrawLineL()
00173     {
00174     AddTextL( KTextLines );
00175     HandleTextChangedL();
00176     }
00177 
00178 // ----------------------------------------------------------------------------
00179 // CRichTextEditorRTE::NewL()
00180 //
00181 // Symbian OS 2nd phase constructor.
00182 // ----------------------------------------------------------------------------
00183 CRichTextEditorRTE* CRichTextEditorRTE::NewL()
00184     {
00185     CRichTextEditorRTE* self = CRichTextEditorRTE::NewLC();
00186     CleanupStack::Pop(self);
00187     return self;
00188     }
00189 
00190 // ----------------------------------------------------------------------------
00191 // CRichTextEditorRTE::NewLC()
00192 //
00193 // Symbian OS 2nd phase constructor.
00194 // ----------------------------------------------------------------------------
00195 CRichTextEditorRTE* CRichTextEditorRTE::NewLC()
00196     {
00197     CRichTextEditorRTE* self = new (ELeave) CRichTextEditorRTE;
00198     CleanupStack::PushL(self);
00199     self->ConstructL();
00200     return self;
00201     }
00202 
00203 // ----------------------------------------------------------------------------
00204 // TKeyResponse CRichTextEditorRTE::OfferKeyEventL(const TKeyEvent& aKeyEvent,
00205 //      TEventCode aType)
00206 //
00207 // Called by the framework whenever a key event occurs. Handles scrolling
00208 // events.
00209 // ----------------------------------------------------------------------------
00210 TKeyResponse CRichTextEditorRTE::OfferKeyEventL(const TKeyEvent& aKeyEvent,
00211                                                 TEventCode aType)
00212     {
00213 
00214     if (aType == EEventKey)
00215         {
00216         if (aKeyEvent.iCode == EKeyDownArrow)
00217             {
00218             MoveCursorL (TCursorPosition::EFPageDown, EFalse);
00219 
00220             return EKeyWasConsumed;
00221             }
00222         else if (aKeyEvent.iCode == EKeyUpArrow)
00223             {
00224             MoveCursorL (TCursorPosition::EFPageUp, EFalse);
00225 
00226             return EKeyWasConsumed;
00227             }
00228         else
00229             {
00230             return CEikRichTextEditor::OfferKeyEventL(aKeyEvent, aType);
00231             }
00232         }
00233 
00234     return EKeyWasNotConsumed;
00235     }
00236 
00237 void CRichTextEditorRTE::ClearScreenL()
00238     {
00239     CRichText* text = RichText();
00240     text->DeleteL(0, text->DocumentLength() );
00241     HandleTextChangedL();
00242     }

Generated by  doxygen 1.6.2