Nokia N97 SDK
Example Applications Guide

RichTextEditorContainer.cpp

00001 /*
00002 * ============================================================================
00003 *  Name     : CRTEContainer from RichTextEditorContainer.cpp
00004 *  Part of  : RichTextEditor
00005 *  Created  : 05/08/2006 by Forum Nokia 
00006 *  Version  : 2.0
00007 *
00008 *  Copyright (c) 2006 - 2007 Nokia Corporation.
00009 *  This material, including documentation and any related
00010 *  computer programs, is protected by copyright controlled by
00011 *  Nokia Corporation.
00012 * ============================================================================
00013 */
00014 
00015 // INCLUDE FILES
00016 #include "RichTextEditorContainer.h"
00017 #include <txtrich.h>    //Richtext
00018 #include <eikrted.h>    //RichText Editor
00019 #include <avkon.mbg>    // Image Ids 
00020 #include <eikenv.h>     // ReadResource() 
00021 #include <RichTextEditor.rsg> // Demo Text Buffer
00022 #include "mypicture.h"   // CMyPicture 
00023 #include <AknUtils.h>   
00024 
00025 #ifdef __SERIES60_3X__
00026 #include <eikapp.h>
00027 #endif
00028 
00029 // ================= MEMBER FUNCTIONS =======================
00030 
00031 // Demo shows the T9 (Front End Processor) ,Scroller,
00032 // "Insert Picture", disable Copy-Paste feature
00033 // in RichText Editor
00034 
00035 // CONSTANTS
00036 _LIT( KImageBitmapFile, "picture.mbm");
00037 //Text and images positions.
00038 const TInt KLeftImagePositionInTheText  = 21;
00039 const TInt KRightImagePositionInTheText = 38;
00040 const TInt KEndofCenterFormat                   = 55;
00041 const TInt KLeftNokia                                   = 6;
00042 const TInt KRightNokia                                  = 11;
00043 const TInt KKImageWidth                                 = 283;
00044 const TInt KImageHeight                                 = 113;
00045 //buffer size
00046 const TInt KBufSize = 256;
00047 
00048 // ---------------------------------------------------------
00049 // CRTEContainer::Prepare()
00050 // Set the Editor flags
00051 // ---------------------------------------------------------
00052 //
00053 void CRTEContainer::Prepare()
00054         {
00055 
00056         // Lower Case 
00057         iRtEd->SetAknEditorCase(EAknEditorLowerCase);
00058         // Fixed case
00059         
00060         iRtEd->SetAknEditorFlags
00061                 (
00062                 EAknEditorFlagFixedCase| //Set up the Allowed Cases
00063                 EAknEditorFlagEnableScrollBars // Set up the scroller
00064                 );
00065         }
00066 
00067 // ---------------------------------------------------------
00068 //  CRTEContainer::PreparePicture()
00069 //  Append Bitmap to iBitmap Array
00070 // ---------------------------------------------------------
00071 //
00072 void CRTEContainer::PrepareBitmapL()
00073         {
00074         #ifndef __SERIES60_3X__
00075         TFileName name(KImageBitmapFile);
00076         CompleteWithAppPath( name );
00077         // windows build wants to install apps on rom drive ( "z:\" )
00078         // but we want the data from "c:\" instead
00079         #ifdef __WINS__
00080         name[ 0 ] = 'C';
00081         #endif
00082 
00083     #else // 3rd ed
00084 
00085     RFs fsSession;
00086     User::LeaveIfError(fsSession.Connect());
00087     CleanupClosePushL( fsSession );
00088     TFileName name;
00089     fsSession.PrivatePath( name );
00090     name.Append( KImageBitmapFile );
00091 
00092         #ifndef __WINS__
00093     // insert the drive to the private path
00094     TParsePtrC parse(
00095         (CEikonEnv::Static()->EikAppUi()->Application())->AppFullName() );
00096     name.Insert( 0, parse.Drive() );
00097     #endif
00098 
00099         #endif
00100 
00101         // Create and Load the Bitmap
00102         CFbsBitmap* bitmap = new( ELeave )CFbsBitmap;
00103         CleanupStack::PushL(bitmap);
00104         User::LeaveIfError(bitmap->Load(name));
00105         //we have to free the memory at the Destructor() -> store the bitmap memory address
00106         iBitmap->AppendL(bitmap);
00107 
00108         CleanupStack::Pop(); //bitmap - container free bitmap at the destructor
00109         #ifdef __SERIES60_3X__
00110     CleanupStack::PopAndDestroy(1, &fsSession);
00111     #endif
00112         // last item in iBitmap array is ready to Insert
00113         }
00114 
00115 // ---------------------------------------------------------
00116 //  CRTEContainer::InsertMyPictureL()
00117 //  Call Richtext's InsertL() with the CPicture
00118 // ---------------------------------------------------------
00119 //
00120 void CRTEContainer::InsertMyPictureL(TInt aPos)
00121         {
00122         CMyPicture* picture;
00123         // Create a CPicture derived class which will draw our image, depending this Size
00124         picture = new( ELeave )CMyPicture(TSize(KKImageWidth,KImageHeight),
00125                 *(iBitmap->At(iBitmap->Count()-1)/*process the last item of iBitmap*/));
00126         CleanupStack::PushL(picture);
00127         // Prepare the Picture header, which will be instered into the Richtext
00128         TPictureHeader header;
00129         header.iPicture =TSwizzle<CPicture>(picture);
00130         iRtEd->RichText()->InsertL( aPos,header); 
00131         CleanupStack::Pop(); // picture - Richtext take the ownership 
00132         }
00133 
00134 // ---------------------------------------------------------
00135 //  CRTEContainer::Demo()
00136 //  Create Demo Text Content 
00137 //
00138 //  Selecting text as holding the 'ABC'+ joystick 
00139 //  Copy/Paste working through longpress 'ABC' and selecting the 
00140 //  softkey which labelled as our need
00141 // 
00142 //  Clipboard content remain even when example closed
00143 // ---------------------------------------------------------
00144 //
00145 void CRTEContainer::DemoL()
00146         {
00147         iRtEd->SetCursorPosL(0,EFalse);
00148         // Set the Demo text to Richtext Editor
00149         TBuf<KBufSize> Demobuf;
00150         CEikonEnv::Static()->ReadResource(Demobuf, R_TYPE_II_DIALOG_TBUF_DEMO_TEXT);
00151         iRtEd->SetTextL(&Demobuf);
00152         
00153         // Insert a Bipmaps to Richtext 
00154         InsertMyPictureL(KLeftImagePositionInTheText);
00155         InsertMyPictureL(KRightImagePositionInTheText);
00156         
00157         //// To Compare the '\f' to EParagraphDelimiter change the comment status of the two following lines
00158         //iRtEd->RichText()->InsertL( KEndofCenterFormat , CEditableText::EParagraphDelimiter);
00159         _LIT(KFormat, "\f");
00160         iRtEd->RichText()->InsertL( KEndofCenterFormat , KFormat);
00161 
00162         // Basic Text Formatting border by EParagraphDelimiter, same as in Forum Nokia's Type - Example
00163         iRtEd->RichText()->InsertL( KLeftImagePositionInTheText , CEditableText::EParagraphDelimiter);
00164         
00165         // Do some color change
00166         SetSelectionL(KLeftImagePositionInTheText, KRightImagePositionInTheText);
00167         SetColor(KRgbRed);
00168         SetSelectionL(KLeftNokia, KRightNokia);
00169         SetColor(KRgbBlue);
00170         
00171         //set newline end of document
00172         iRtEd->RichText()->InsertL(iRtEd->Text()->DocumentLength() , CEditableText::ELineBreak);
00173         
00174         // Set the cursor end of the document
00175         iRtEd->SetCursorPosL(iRtEd->Text()->DocumentLength(), EFalse);
00176         }
00177         
00178 void CRTEContainer::SetupL()
00179         {
00180     // One way to remove the selection
00181         //iRtEd->ClearSelectionL();
00182         
00183         // Copy-Paste Support is allowed in Edwin but we can disable
00184         // to do that uncomment the following line
00185         //iRtEd->EnableCcpuSupportL(EFalse);
00186         iRtEd->EnableCcpuSupportL(ETrue);
00187         // Another way to remove the selection.
00188         iRtEd->SetCursorPosL(0, EFalse);
00189         
00190         iRtEd->UpdateScrollBarsL();
00191         SizeChanged();
00192         }
00193 
00194 // ---------------------------------------------------------
00195 // CRTEContainer::ConstructL(const TRect& aRect)
00196 // EPOC two phased constructor
00197 // ---------------------------------------------------------
00198 //
00199 void CRTEContainer::ConstructL(const TRect& aRect)
00200     {
00201         CreateWindowL();
00202 
00203         iBitmap = new (ELeave) CArrayFixFlat<CFbsBitmap *>(2); // DemoL() show two image        
00204         iRtEd = new (ELeave) CEikRichTextEditor;
00205         
00206         iStatusLine = new (ELeave) CEikLabel;
00207         iStatusLine->SetAlignment(EHCenterVCenter);
00208 
00209         Prepare();
00210         PrepareBitmapL();
00211         
00212         iRtEd->ConstructL(this,0,0,0);
00213         iRtEd->SetFocus(ETrue);
00214         
00215         SetRect(aRect);
00216 
00217         IntroL();
00218         ActivateL();
00219         SetupL();
00220     }
00221 
00222 // ---------------------------------------------------------
00223 // CRTEContainer::~CRTEContainer()
00224 // Destructor - Free the allocated resources
00225 // ---------------------------------------------------------
00226 
00227 CRTEContainer::~CRTEContainer()
00228     {
00229     if(iRtEd)
00230                 delete iRtEd;
00231         
00232         if(iStatusLine)
00233                 delete iStatusLine;
00234                 
00235         // Delete each image, which have reference in the Array
00236         for(int i=0;i<iBitmap->Count();i++)
00237                 {
00238                 if(iBitmap->At(i))
00239                         delete iBitmap->At(i);
00240                 }
00241         // Free the Array
00242         if(iBitmap)
00243                 delete iBitmap;
00244     }
00245 
00246 // ---------------------------------------------------------
00247 // CRTEContainer::SizeChanged()
00248 // Called by framework when the view size is changed
00249 // ---------------------------------------------------------
00250 //
00251 void CRTEContainer::SizeChanged()
00252     {
00253         TRect rect = Rect();
00254         TInt StatusLineHeight = iStatusLine->Font()->HeightInPixels()+6;
00255         iStatusLine->SetExtent(TPoint(0, 0),TSize(rect.Width(), StatusLineHeight));
00256         
00257         #ifdef __SERIES60_3X__
00258         TRect ScrollBarRect = iRtEd->ScrollBarFrame()->VerticalScrollBar()->Rect();
00259         //In 3rd edition CEikRichTextEditor draw the view for the whole rect and
00260         //the scrollbar doesn't show. That is a reason why CEikRichTextEditor Width() is
00261         //rect.Width()-ScrollBarRect.Width()
00262         iRtEd->SetExtent(TPoint(0,StatusLineHeight), 
00263                 TSize(rect.Width()-ScrollBarRect.Width(), rect.Height()-StatusLineHeight));
00264         #else
00265     iRtEd->SetExtent(TPoint(0,StatusLineHeight), 
00266         TSize(rect.Width(), rect.Height()-StatusLineHeight));
00267     #endif
00268     }
00269 
00270 // ---------------------------------------------------------
00271 // CRTEContainer::CountComponentControls() const
00272 // ---------------------------------------------------------
00273 //
00274 TInt CRTEContainer::CountComponentControls() const
00275     {
00276     return 2; // return number of controls inside this container
00277     }
00278 
00279 // ---------------------------------------------------------
00280 // CRTEContainer::ComponentControl(TInt aIndex) const
00281 // ---------------------------------------------------------
00282 //
00283 CCoeControl* CRTEContainer::ComponentControl(TInt aIndex) const
00284     {
00285     switch ( aIndex )
00286         {
00287         case 0:
00288                         return iRtEd;
00289                 case 1:
00290                         return iStatusLine;
00291         default:
00292             return NULL;
00293         }
00294     }
00295 
00296 // ---------------------------------------------------------
00297 // CRTEContainer::Draw(const TRect& aRect) const
00298 // ---------------------------------------------------------
00299 //
00300 void CRTEContainer::Draw(const TRect& aRect) const
00301     {
00302     CWindowGc& gc = SystemGc();
00303     gc.SetPenStyle(CGraphicsContext::ENullPen);
00304     gc.SetBrushColor(KRgbWhite);
00305     gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
00306     gc.DrawRect(aRect);
00307     }
00308 
00309 // ---------------------------------------------------------
00310 // OfferKeyEventL() 
00311 // Distribute the key event to the Editor
00312 // ---------------------------------------------------------
00313 //
00314 TKeyResponse CRTEContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
00315         {
00316         if (iRtEd->IsFocused())
00317                 {
00318                 return iRtEd->OfferKeyEventL(aKeyEvent, aType); 
00319                 }
00320         else
00321                 {
00322                 return EKeyWasNotConsumed;
00323                 }
00324         }
00325 
00326 // ----------------------------------------------------
00327 // SetAlignment(CParaFormat::TAlignment aAlignment)
00328 //
00329 // Alignment formatting apply to the actual paragraph
00330 // If there is only one, it apply for the whole text
00331 //
00332 // We expect one of the following input parameter value:
00333 //      CParaFormat::ELeftAlign
00334 //      CParaFormat::ECenterAlign
00335 //      CParaFormat::ERightAlign
00336 //
00337 // ----------------------------------------------------
00338 void CRTEContainer::SetAlignment(CParaFormat::TAlignment aAlignment)
00339         {
00340         CParaFormat     paraFormat;
00341         TParaFormatMask paraFormatMask;
00342 
00343         paraFormatMask.SetAttrib(EAttAlignment);    // set the mask
00344         paraFormat.iHorizontalAlignment = aAlignment;
00345 
00346         iRtEd->ApplyParaFormatL(&paraFormat, paraFormatMask);
00347         
00348         Echo(R_TYPE_DIALOG_TBUF_INDICATOR_ALIGNMENT);
00349         }
00350 
00351 // ----------------------------------------------------
00352 // SetSelectionL(TInt aCursorPos,TInt aAnchorPos)
00353 // Select a text in the Rich text editor
00354 // ----------------------------------------------------
00355 void CRTEContainer::SetSelectionL(TInt aCursorPos,TInt aAnchorPos)
00356         {
00357         iRtEd->SetSelectionL(aCursorPos,aAnchorPos);
00358         }
00359 
00360 // ----------------------------------------------------
00361 // SetCharacterBIUAttrib(CEikGlobalTextEditor::TFontStyleFlags aFontFlags)
00362 // 
00363 // Character formating apply for the selected text
00364 // If nothing is selected it apply to actual word
00365 // We expect one of the following input parameter value:
00366 //      CEikGlobalTextEditor::EBold
00367 //      CEikGlobalTextEditor::EItalic
00368 //      CEikGlobalTextEditor::EUnderline
00369 // ----------------------------------------------------
00370 
00371 void CRTEContainer::SetCharacterBIUAttribL(CEikGlobalTextEditor::TFontStyleFlags aFontFlags)
00372         {
00373         iRtEd->BoldItalicUnderlineEventL(aFontFlags);
00374         Echo(R_TYPE_DIALOG_TBUF_INDICATOR_CHARACTER_FORMAT);
00375         }
00376         
00377 #ifdef __SERIES60_3X__
00378 // ----------------------------------------------------
00379 // SetFont(TInt aFontId)
00380 // Change the text's font in the editor 
00381 // to the one specified as input parameter for the function 
00382 // ----------------------------------------------------
00383 void CRTEContainer::SetFont(TInt aFontId)
00384         {       
00385         // Get a logical font to base my font on:
00386         const CFont* logicalFont = AknLayoutUtils::FontFromId(aFontId);
00387         // Extract font information
00388         TFontSpec fontspec = logicalFont->FontSpecInTwips();
00389 
00390     TCharFormat charFormat(fontspec.iTypeface.iName, fontspec.iHeight);
00391     TCharFormatMask charFormatMask;
00392 
00393     charFormatMask.SetAttrib(EAttFontTypeface);
00394     charFormatMask.SetAttrib(EAttFontHeight);
00395 
00396         iRtEd->ApplyCharFormatL(charFormat, charFormatMask);
00397 
00398         Echo(R_TYPE_DIALOG_TBUF_INDICATOR_FONT);
00399         }
00400 #else
00401 // ----------------------------------------------------
00402 // SetFont(const CFont *aFont)
00403 // Change the text's font in the editor 
00404 // to the one specified as input parameter for the function 
00405 // ----------------------------------------------------
00406 void CRTEContainer::SetFont(const CFont *aFont)
00407         {       
00408                 /*
00409         TCursorSelection cs = iRtEd->Selection();
00410         
00411         SetSelectionL(cs.iCursorPos, cs.iAnchorPos-1);
00412         */
00413     TFontSpec fontspec = aFont->FontSpecInTwips();
00414     TCharFormat charFormat( fontspec.iTypeface.iName, fontspec.iHeight );
00415     TCharFormatMask charFormatMask;
00416 
00417     charFormatMask.SetAttrib(EAttFontTypeface);
00418     charFormatMask.SetAttrib(EAttFontHeight);
00419         iRtEd->ApplyCharFormatL(charFormat, charFormatMask);
00420 
00421         Echo(R_TYPE_DIALOG_TBUF_INDICATOR_FONT);
00422         }
00423 #endif
00424 // ----------------------------------------------------
00425 // Strike()
00426 // Toggle between strikethrough or not strikethrough text
00427 // It depend only bool switch in the code, which turn to opposite in every 
00428 // case Strikethrough selected from the menu.
00429 // ----------------------------------------------------
00430 void CRTEContainer::Strike()
00431         {
00432     TCharFormat charFormat;
00433         TCharFormatMask charFormatMask;
00434 
00435         iStrike=!iStrike;
00436         charFormat.iFontPresentation.iStrikethrough=iStrike?EStrikethroughOn:EStrikethroughOff;
00437     charFormatMask.SetAttrib(EAttFontStrikethrough);
00438         iRtEd->ApplyCharFormatL(charFormat, charFormatMask);
00439 
00440         if(iStrike)
00441                 {
00442                 Echo(R_TYPE_DIALOG_TBUF_INDICATOR_STRIKE_ON);
00443                 }
00444         else
00445                 {
00446                 Echo(R_TYPE_DIALOG_TBUF_INDICATOR_STRIKE_OFF);
00447                 }
00448         }
00449         
00450 // ----------------------------------------------------
00451 // SetColor(TRgb aColor)
00452 // Set the selected text color based on the input parameter
00453 // If nothing selected, text color changed only the cursor position.
00454 // ----------------------------------------------------
00455 void CRTEContainer::SetColor(TRgb aColor)
00456         {
00457         TCharFormat charFormat;
00458         TCharFormatMask charFormatMask;
00459 
00460         charFormat.iFontPresentation.iTextColor = aColor;
00461         charFormatMask.SetAttrib(EAttColor);
00462         iRtEd->ApplyCharFormatL(charFormat, charFormatMask);
00463         
00464 //      iRtEd->SetBackgroundColorL(aColor); 
00465 //  It is possible to change the background color - if foreground and background 
00466 //  color is same, the text became invisible.
00467         }
00468 
00469 // ----------------------------------------------------
00470 // Echo(TInt aMessage)
00471 // Echo the last operation to the status line
00472 // ----------------------------------------------------
00473 void CRTEContainer::Echo(TInt aMessage)
00474         {
00475         TBuf<32> buf;
00476         CEikonEnv::Static()->ReadResource(buf, aMessage);
00477         iStatusLine->SetTextL(buf);
00478         }
00479 
00480 // ----------------------------------------------------
00481 // IntroL()
00482 // Create Intro Text.
00483 // ----------------------------------------------------
00484 void CRTEContainer::IntroL()
00485         {
00486         TBuf<KBufSize> Introbuf;
00487         CEikonEnv::Static()->ReadResource(Introbuf, R_TYPE_DIALOG_TBUF_INTRO_TEXT);
00488 
00489         TBuf<KBufSize> HelpText;
00490         CEikonEnv::Static()->ReadResource(HelpText, R_TYPE_DIALOG_TBUF_INTRO_HELP_TEXT);
00491         HelpText.Append(CEditableText::ELineBreak);
00492 
00493         //Start the center format.
00494         SetAlignment(CParaFormat::ECenterAlign);
00495         
00496         //Set the text.
00497         iRtEd->SetTextL(&Introbuf);
00498         iRtEd->RichText()->InsertL(iRtEd->Text()->DocumentLength()/* at the end*/, HelpText);
00499         
00500         Echo(R_TYPE_DIALOG_TBUF_INDICATOR_DEMO);
00501         }
00502 
00503 // End of File

© Nokia 2009

Back to top