examples/AppFramework/txtshell/txtstyl.cpp

00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // License: "Symbian Foundation License v1.0" to Symbian Foundation
00004 // members and "Symbian Foundation End User License Agreement v1.0"
00005 // to non-members at the URL
00006 // "http://www.symbianfoundation.org/legal/licencesv10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 //
00015 
00016 #include "txtexamp.h"
00017 
00018 #include <txtrich.h>
00019 
00020 CStyleControl::~CStyleControl()
00021         {
00022         delete iTextView; // text view
00023         delete iLayout; // layout
00024         delete iRichText; // contained text object
00025         delete iNormalCharFormatLayer; // char format layer
00026         delete iNormalParaFormatLayer; // para format layer
00027         }
00028 
00029 void CStyleControl::CreateNormalL()
00030         {
00031         // Create the "Normal" style 
00032         // = the global format layers upon which all styles will be based.
00033         // Use default formatting except for an increased left margin and line spacing.
00034         CParaFormat* paraFormat=CParaFormat::NewLC();
00035         TParaFormatMask paraFormatMask;
00036         paraFormat->iLeftMarginInTwips=360;
00037         paraFormatMask.SetAttrib(EAttLeftMargin);
00038         paraFormat->iLineSpacingInTwips=300;
00039         paraFormatMask.SetAttrib(EAttLineSpacing);
00040         // Create the normal (global) style
00041         iNormalParaFormatLayer=CParaFormatLayer::NewL(paraFormat,paraFormatMask);
00042         CleanupStack::PopAndDestroy();  // paraFormat
00043         TCharFormat charFormat;
00044         TCharFormatMask charFormatMask;
00045         charFormatMask.SetAttrib(EAttFontHeight);
00046         iNormalCharFormatLayer=CCharFormatLayer::NewL(charFormat,charFormatMask); 
00047         }
00048 
00049 void CStyleControl::CreateStylesL()
00050         {
00051         // Create two new styles, with names "Warning" and "ListBullet", 
00052         // add some appropriate formatting and append them to a style list.
00053         _LIT(KStyle,"Warning");
00054         _LIT(KStyleTwo,"ListBullet");
00055         CParaFormat* paraFormat=CParaFormat::NewLC();
00056         TParaFormatMask paraFormatMask;
00057         TCharFormat charFormat;
00058         TCharFormatMask charFormatMask;
00059         // Style 1 - "Warning style"
00060         iStyleOne=CParagraphStyle::NewL(
00061                 *iNormalParaFormatLayer,*iNormalCharFormatLayer); // Base it on the "normal" layers
00062 
00063         // Enclose text in a paragraph border
00064         TParaBorder paraBorder;
00065         paraBorder.iLineStyle=TParaBorder::ESolid;
00066         paraBorder.iThickness=4; // border width
00067         paraBorder.iColor=KRgbGray; // set border color to gray
00068         paraBorder.iAutoColor=EFalse; // iColor overrides text color
00069         paraFormat->SetParaBorderL(CParaFormat::EParaBorderTop,paraBorder);
00070         paraFormat->SetParaBorderL(CParaFormat::EParaBorderBottom,paraBorder);
00071         paraFormat->SetParaBorderL(CParaFormat::EParaBorderLeft,paraBorder);
00072         paraFormat->SetParaBorderL(CParaFormat::EParaBorderRight,paraBorder);
00073         paraFormatMask.SetAttrib(EAttTopBorder);
00074         paraFormatMask.SetAttrib(EAttBottomBorder);
00075         paraFormatMask.SetAttrib(EAttRightBorder);
00076         paraFormatMask.SetAttrib(EAttLeftBorder);
00077         iStyleOne->SetL(paraFormat,paraFormatMask); 
00078         // Make it bold.
00079         // First, get handle to style 1's character format layer
00080         CCharFormatLayer* charStyleFormatLayer = iStyleOne->CharFormatLayer(); 
00081         charFormat.iFontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold);
00082         charFormatMask.SetAttrib(EAttFontStrokeWeight);
00083         // Set the style's character format layer
00084         charStyleFormatLayer->SetL(charFormat,charFormatMask);
00085         // Finally, set its name to "Warning"
00086         iStyleOne->iName=KStyle;
00087 
00088         // Style 2 - "listbullet"
00089         paraFormat->Strip(); // Clear paraformat ready for new style
00090         paraFormatMask.ClearAll(); // Clear mask
00091         iStyleTwo=CParagraphStyle::NewL(
00092                 *iNormalParaFormatLayer,*iNormalCharFormatLayer);  // Base it on the "normal" layers
00093         paraFormat->iBullet=new(ELeave)TBullet; 
00094         paraFormat->iBullet->iHeightInTwips=480; // Size of bullet point character=480 twips
00095         paraFormat->iBullet->iHangingIndent=ETrue; // Indent the rest of the paragraph from the bullet
00096         paraFormatMask.SetAttrib(EAttBullet);
00097         iStyleTwo->SetL(paraFormat,paraFormatMask); 
00098         // Finally, set its name to "ListBullet"
00099         iStyleTwo->iName=KStyleTwo;
00100 
00101         // Create style table and insert styles.
00102         iStyleList=CStyleList::NewL();
00103         RParagraphStyleInfo info1(iStyleOne);
00104         RParagraphStyleInfo info2(iStyleTwo);
00105         iStyleList->AppendL(&info1);
00106         iStyleList->AppendL(&info2);
00107         // Create rich text object based on normal layers, with the style table.
00108         iRichText=CRichText::NewL(iNormalParaFormatLayer,iNormalCharFormatLayer,*iStyleList);
00109         CleanupStack::PopAndDestroy();  // paraFormat
00110 }
00111 
00112 void CStyleControl::UpdateModelL()
00113         {
00114         // Create all constant literal descriptors used in this function, 
00115         // e.g. for message text
00116         _LIT(KText1,"Some text using normal style");
00117         _LIT(KText2,"Text in warning style - based on normal");
00118         _LIT(KText3,"Text in list bullet style");
00119         _LIT(KText4,"Some more text in list bullet style");
00120         _LIT(KText5,"Some text in italics");
00121         _LIT(KStatus0,"Initialised styles and view");
00122         _LIT(KStatus1,"Normal");
00123         _LIT(KStatus2,"Warning style");
00124         _LIT(KStatus3,"List bullet style");
00125         _LIT(KStatus4,"Character and paragraph formatting applied to styled text");
00126         _LIT(KStatus5,"All specific formatting removed from third paragraph");
00127         _LIT(KStatus6,"Inserted some normal text with specific formatting");
00128         _LIT(KStatus7,"Applied a style (warning) to text, retaining specific formatting");
00129         _LIT(KStatus8,"Reset();");
00130         _LIT(KStatusDefault,"(overshot!!)");
00131 
00132                 
00133         switch (Phase())
00134                 {
00135         // Apply paragraph styles
00136         case 0:
00137                 {
00138                 // Create the global format layers on which all styles are based.
00139                 CreateNormalL();        
00140                 // Create text object and styles.
00141                 CreateStylesL();        
00142                 // Prerequisites for view - viewing rectangle
00143                 iViewRect=Rect();
00144                 iViewRect.Shrink(3,3);
00145                 // context and device
00146                 CWindowGc& gc=SystemGc(); // get graphics context
00147                 CBitmapDevice* device=(CBitmapDevice*) (gc.Device()); // device
00148                 // layout
00149                 iLayout=CTextLayout::NewL(iRichText,iViewRect.Width()); // new layout
00150                         // construct layout, giving width of viewing rectangle
00151                 // text view
00152                 iTextView=CTextView::NewL(iLayout, iViewRect,
00153                         device,
00154                         device,
00155                         &Window(),
00156                         0, // no window group
00157                         &iCoeEnv->WsSession()
00158                         );
00159                 iFormObserver->NotifyStatus(KStatus0);
00160                 break;
00161                 }
00162         case 1:
00163                 // Set some character formatting - applied globally
00164                 iCharFormatMask.SetAttrib(EAttFontStrokeWeight);  
00165                 
00166                 iCharFormat.iFontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold);
00167 
00168                 // apply formatting - pos and length are irrelevent
00169                 iRichText->ApplyCharFormatL(iCharFormat,iCharFormatMask, 0,0);
00170                 
00171                 // Insert a paragraph using the "normal" (default) formatting.
00172                 iRichText->InsertL(0,KText1);
00173                 iRichText->InsertL(iRichText->DocumentLength(),
00174                         CEditableText::EParagraphDelimiter); 
00175                 iFormObserver->NotifyStatus(KStatus1);
00176                 break;
00177         case 2:
00178                 {
00179                 // Insert a paragraph in "warning" style
00180                 TInt length;
00181                 TInt startPos;
00182                 iRichText->InsertL(iRichText->DocumentLength(),KText2);
00183                 iRichText->InsertL(iRichText->DocumentLength(),
00184                         CEditableText::EParagraphDelimiter); 
00185                 // Get start position of second paragraph
00186                 startPos=iRichText->CharPosOfParagraph(length,1); // Not interested in length
00187                 // Apply warning style to second paragraph - single character is enough
00188                 iRichText->ApplyParagraphStyleL(*iStyleList->At(0).iStyle,startPos,1,
00189                         CParagraphStyle::ERetainAllSpecificFormats);
00190                 iFormObserver->NotifyStatus(KStatus2);
00191                 break;
00192                 }
00193         case 3:
00194         // Insert two paragraphs in list bullet style
00195                 {
00196                 TInt length;
00197                 TInt startPos;
00198                 // Insert some text in bullet point style
00199                 iRichText->InsertL(iRichText->DocumentLength(),KText3);
00200                 iRichText->InsertL(iRichText->DocumentLength(),
00201                         CEditableText::EParagraphDelimiter); 
00202                                 // end para
00203                 iRichText->InsertL(iRichText->DocumentLength(),KText4);
00204                 iRichText->InsertL(iRichText->DocumentLength(),
00205                         CEditableText::EParagraphDelimiter);
00206                                 // end para
00207                 // Get start position and length of third paragraph
00208                 startPos=iRichText->CharPosOfParagraph(length,2); // Interested in length
00209                 // Apply list bullet style to 3rd and 4th paragraphs, 
00210                 // from start of para 2, for length of para2 + 1 characters.
00211                 iRichText->ApplyParagraphStyleL(*iStyleList->At(1).iStyle,startPos,length+1,
00212                         CParagraphStyle::ERetainAllSpecificFormats);
00213                 iFormObserver->NotifyStatus(KStatus3);
00214                 break;
00215                 }
00216         case 4:
00217                 // Apply character and paragraph formatting 
00218                 // (underlining and right alignment) over a style
00219                 {
00220                 TInt startPos, length;
00221                 // Get start pos and length of third paragraph
00222                 startPos=iRichText->CharPosOfParagraph(length,2); 
00223                 // set underlining
00224                 TCharFormat charFormat;
00225                 TCharFormatMask charFormatMask;
00226                 charFormat.iFontPresentation.iUnderline=EUnderlineOn; 
00227                 charFormatMask.SetAttrib(EAttFontUnderline); // interested in underlining
00228                 // Apply character formatting, from start of para for para length 
00229                 // minus one char (don't want trailing paragraph delimiter underlined)
00230                 iRichText->ApplyCharFormatL(charFormat, charFormatMask,startPos,length-1);
00231                 CParaFormat* paraFormat=CParaFormat::NewL();
00232                 TParaFormatMask paraFormatMask;
00233                 paraFormat->iHorizontalAlignment=CParaFormat::ERightAlign; 
00234                 paraFormatMask.SetAttrib(EAttAlignment);
00235                 // Apply right alignment to first paragraph. A single character is enough
00236                 iRichText->ApplyParaFormatL(paraFormat, paraFormatMask,startPos,1);
00237                 iFormObserver->NotifyStatus(KStatus4);
00238                 delete paraFormat;
00239                 break;
00240                 }
00241         case 5:
00242                 // Remove all specific formatting from third para
00243                 {
00244                 TInt startPos, length;
00245                 startPos=iRichText->CharPosOfParagraph(length,2); 
00246                 // Remove all specific character formatting from the paragraph 
00247                 iRichText->RemoveSpecificCharFormatL(startPos,length);
00248                 // Remove all specific para formatting from the paragraph
00249                 iRichText->RemoveSpecificParaFormatL(startPos,2);
00250                 iFormObserver->NotifyStatus(KStatus5);
00251                 break;
00252                 }
00253         case 6:
00254                 // Insert text in italics at the end of the document
00255                 {
00256                 TCharFormat charFormat;
00257                 TCharFormatMask charFormatMask;
00258                 charFormat.iFontSpec.iFontStyle.SetPosture(EPostureItalic);     
00259                 charFormatMask.SetAttrib(EAttFontPosture); // interested in italic
00260                 // Set formatting for all text inserted at this position.
00261                 iRichText->SetInsertCharFormatL(charFormat,charFormatMask,iRichText->DocumentLength());
00262                 iRichText->InsertL(iRichText->DocumentLength(),KText5);
00263                 iRichText->CancelInsertCharFormat(); // "Insert pending" state cancelled
00264                 iRichText->InsertL(iRichText->DocumentLength(),
00265                         CEditableText::EParagraphDelimiter); // add a para delimiter at end of paragraph
00266                 iFormObserver->NotifyStatus(KStatus6);
00267                 break;
00268                 }
00269         case 7:
00270                 {
00271                 // Apply a style to text added in previous case; retain all its specific formatting.
00272                 TInt startPos, length;
00273                 // Get any document position in the 5th paragraph. Not interested in length.
00274                 startPos=iRichText->CharPosOfParagraph(length,4);
00275                 iRichText->ApplyParagraphStyleL(*iStyleList->At(0).iStyle,startPos,1,
00276                         CParagraphStyle::ERetainAllSpecificFormats);
00277                 iFormObserver->NotifyStatus(KStatus7);
00278                 break;
00279                 }
00280         case 8:
00281                 //reset document
00282                 iRichText->Reset();
00283                 // message to say what we did
00284                 iFormObserver->NotifyStatus(KStatus8);
00285                 break;
00286         default:
00287                 iFormObserver->NotifyStatus(KStatusDefault);
00288                 break;
00289                 }
00290         }
00291 
00292 void CStyleControl::Draw(const TRect& aRect) const
00293         {
00294         // draw surround
00295         CGraphicsContext& gc=SystemGc(); // context to draw into
00296         TRect rect=Rect(); // screen boundary
00297         gc.DrawRect(rect); // outline screen boundary
00298         rect.Shrink(1,1);
00299         gc.SetPenColor(KRgbWhite);
00300         gc.DrawRect(rect);
00301         rect.Shrink(1,1);
00302         gc.SetPenColor(KRgbBlack);
00303         gc.DrawRect(rect);
00304         // draw editable text - will work unless OOM
00305         TInt err;
00306         TRAP(err,iTextView->FormatTextL());
00307         if (err) return;
00308         TRAP(err,iTextView->DrawL(aRect));
00309         }
00310 

Generated by  doxygen 1.6.2