00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies). 00002 // All rights reserved. 00003 // This component and the accompanying materials are made available 00004 // under the terms of "Eclipse Public License v1.0" 00005 // which accompanies this distribution, and is available 00006 // at the URL "http://www.eclipse.org/legal/epl-v10.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 CRichControl::~CRichControl() 00021 { 00022 delete iTextView; // text view 00023 delete iLayout; // text layout 00024 delete iRichText; // contained text object 00025 delete iCharFormatLayer; // character format layer 00026 delete iParaFormatLayer; // and para format layer 00027 } 00028 00029 00030 void CRichControl::UpdateModelL() 00031 { 00032 // Create all constant literal descriptors used in this function, 00033 // e.g. for message text 00034 _LIT(KHamletSpeech, "To be, or not to be, that is the question; \ 00035 whether 'tis nobler in the mind to suffer the \ 00036 slings and arrows of outrageous fortune, or \ 00037 to stand against a sea of troubles, and by \ 00038 opposing end them."); 00039 _LIT(KPath,"\\richtxt.dat"); 00040 _LIT(KText1,"Rich text in bold. "); 00041 _LIT(KText2,"Much larger rich text."); 00042 _LIT(KText5,"New text has preserved formatting at insertion point. "); 00043 _LIT(KText7,"New text has lost formatting at insertion point. "); 00044 _LIT(KStatus0,"Initialised rich text object and text view"); 00045 _LIT(KStatus1,"InsertL() at position zero"); 00046 _LIT(KStatus2,"Inserted text with specific formatting applied"); 00047 _LIT(KStatus3,"Underlining applied to part of paragraph"); 00048 _LIT(KStatus4,"Deleted text, but preserved formatting ..."); 00049 _LIT(KStatus5,"... and inserted with same format"); 00050 _LIT(KStatus6,"Deleted text and forgot about formatting ..."); 00051 _LIT(KStatus7,"... and inserted with format before new text"); 00052 _LIT(KStatus8,"Added 2 new paragraphs with default formatting"); 00053 _LIT(KStatus9,"Set alignment for second paragraph"); 00054 _LIT(KStatus10,"Used StoreL() to store rich text and components"); 00055 _LIT(KStatus12,"Used RestoreL() to restore rich text and components"); 00056 _LIT(KStatusReset,"Reset();"); 00057 _LIT(KStatusDefault,"(overshot!!)"); 00058 00059 TBufC<28> name(KPath); 00060 00061 switch (Phase()) 00062 { 00063 case 0: 00064 { 00065 // Create text object, text view and layout. 00066 iParaFormatLayer=CParaFormatLayer::NewL(); // required para format layer 00067 iCharFormatLayer=CCharFormatLayer::NewL(); // required char format layer 00068 // Create an empty rich text object 00069 iRichText=CRichText::NewL(iParaFormatLayer, iCharFormatLayer); 00070 // prerequisites for view - viewing rectangle 00071 iViewRect=Rect(); 00072 iViewRect.Shrink(3,3); 00073 // context and device 00074 CWindowGc& gc=SystemGc(); // get graphics context 00075 CBitmapDevice *device=(CBitmapDevice*) (gc.Device()); // device 00076 // Create the text layout, (required by text view), 00077 // with the text object and a wrap width (=width of view rect) 00078 iLayout=CTextLayout::NewL(iRichText,iViewRect.Width()); 00079 // Create text view 00080 iTextView=CTextView::NewL(iLayout, iViewRect, 00081 device, 00082 device, 00083 &Window(), 00084 0, // no window group 00085 &iCoeEnv->WsSession() 00086 ); // new view 00087 // message to say what we did 00088 iFormObserver->NotifyStatus(KStatus0); 00089 break; 00090 } 00091 case 1: 00092 // Set some character formatting 00093 iCharFormatMask.SetAttrib(EAttFontStrokeWeight); 00094 00095 iCharFormat.iFontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold); 00096 00097 // apply formatting - pos and length are irrelevent 00098 iRichText->ApplyCharFormatL(iCharFormat,iCharFormatMask, 0,0); 00099 00100 // Insert some text 00101 iRichText->InsertL(0,KText1); 00102 iFormObserver->NotifyStatus(KStatus1); 00103 break; 00104 case 2: 00105 // Insert some more text with specific formatting 00106 { 00107 TCharFormat charFormat; 00108 TCharFormatMask charFormatMask; 00109 charFormatMask.SetAttrib(EAttFontHeight); // interested in font height 00110 charFormat.iFontSpec.iHeight=480; // (in twips) increase it from default to 1/3 inch 00111 TInt pos=iRichText->DocumentLength(); // insertion position = end of doc 00112 iRichText->SetInsertCharFormatL(charFormat, charFormatMask,pos); 00113 // Set formatting, when inserting at this position 00114 iRichText->InsertL(pos,KText2); // Insert text at this position 00115 iRichText->CancelInsertCharFormat(); // Cancel "insert pending" state 00116 // This is necessary before inserting anywhere else 00117 iFormObserver->NotifyStatus(KStatus2); 00118 break; 00119 } 00120 case 3: 00121 { 00122 // Apply underlining to text which uses a mixture of formatting. 00123 TCharFormat charFormat; 00124 TCharFormatMask charFormatMask; 00125 charFormatMask.SetAttrib(EAttFontUnderline); // interested in underline 00126 charFormat.iFontPresentation.iUnderline=EUnderlineOn; // set it on 00127 iRichText->ApplyCharFormatL(charFormat, charFormatMask,13,10); 00128 // apply this character formatting, from position 13, 10 characters 00129 iFormObserver->NotifyStatus(KStatus3); 00130 break; 00131 } 00132 case 4: 00133 // delete text, then insert new text at same point to 00134 // demonstrate preserving the deleted text's formatting. 00135 iRichText->DelSetInsertCharFormatL(19,(iRichText->DocumentLength()-19)); 00136 // delete from pos 19, for rest of document; retain formatting at pos 19 00137 iFormObserver->NotifyStatus(KStatus4); 00138 break; 00139 case 5: 00140 // ... and then insert with same format 00141 iRichText->InsertL(19,KText5); 00142 // inserted with old formatting 00143 iRichText->CancelInsertCharFormat(); // must cancel before inserting elsewhere 00144 iFormObserver->NotifyStatus(KStatus5); 00145 break; 00146 case 6: 00147 // delete some text ... 00148 iRichText->DeleteL(19,(iRichText->DocumentLength()-19)); 00149 // DeleteL() deletes, and forgets formatting 00150 iFormObserver->NotifyStatus(KStatus6); 00151 break; 00152 case 7: 00153 // ... then insert new text at that point to 00154 // demonstrate how DeleteL() differs from DelSetInsertCharFormatL(). 00155 iRichText->InsertL(19,KText7); 00156 // insert, inheriting current formatting from char before 10 00157 // (no need to cancel anything!) 00158 iFormObserver->NotifyStatus(KStatus7); 00159 break; 00160 case 8: 00161 { 00162 // Rich text paragraph formatting. 00163 // Insert another 2 paragraphs using default formatting. 00164 // First remove specific formatting from final paragraph delimiter, 00165 // otherwise new paragraphs would pick up this formatting. 00166 iRichText->RemoveSpecificCharFormatL(iRichText->DocumentLength()-1,1); 00167 iRichText->InsertL(iRichText->DocumentLength(), 00168 CEditableText::EParagraphDelimiter); // new para 00169 for (TInt count=0;count<2;count++) // insert lots of text, twice over 00170 { 00171 iRichText->InsertL(iRichText->DocumentLength(),KHamletSpeech); 00172 iRichText->InsertL(iRichText->DocumentLength(), 00173 CEditableText::EParagraphDelimiter); // end para 00174 }; 00175 iFormObserver->NotifyStatus(KStatus8); 00176 break; 00177 } 00178 case 9: 00179 { 00180 CParaFormat* paraFormat=CParaFormat::NewLC(); 00181 TParaFormatMask paraFormatMask; 00182 // make para 1 right-aligned (numbering starts at 0) 00183 TInt pos, length; 00184 paraFormatMask.SetAttrib(EAttAlignment); // interested in alignment 00185 paraFormat->iHorizontalAlignment=CParaFormat::ERightAlign; // right-align 00186 pos=iRichText->CharPosOfParagraph(length,1); // start of para 2 00187 iRichText->ApplyParaFormatL(paraFormat,paraFormatMask,pos,1); 00188 // apply format to entire para - even 1 char will do 00189 iFormObserver->NotifyStatus(KStatus9); 00190 CleanupStack::PopAndDestroy(); // paraFormat 00191 break; 00192 } 00193 // Storing and restoring 00194 case 10: 00195 // set up a file store 00196 { 00197 RFs theFs; 00198 CFileStore* theStore; 00199 TParse filestorename; 00200 // Make a connection to the file server 00201 theFs.Connect(); 00202 theFs.Parse(name,filestorename); 00203 theStore=CDirectFileStore::ReplaceLC(theFs,filestorename.FullName(),EFileRead|EFileWrite); 00204 theStore->SetTypeL(KDirectFileStoreLayoutUid); 00205 // store rich text to file store 00206 iStreamId=iRichText->StoreL(*theStore); 00207 // close the store 00208 CleanupStack::PopAndDestroy(); // pop and destroy store 00209 // Disconnect from file server 00210 theFs.Close(); 00211 iFormObserver->NotifyStatus(KStatus10); 00212 break; 00213 } 00214 case 11: 00215 // reset document, clearing it of all content 00216 iRichText->Reset(); 00217 iFormObserver->NotifyStatus(KStatusReset); 00218 break; 00219 case 12: 00220 // open the store 00221 { 00222 RFs theFs; 00223 CFileStore* theStore; 00224 TParse filestorename; 00225 00226 theFs.Connect(); 00227 theFs.Parse(name,filestorename); 00228 theStore=CDirectFileStore::OpenLC(theFs,filestorename.FullName(),EFileRead|EFileShareReadersOnly); 00229 if (theStore->Type()[0]!=KDirectFileStoreLayoutUid) 00230 User::Leave(KErrUnknown); 00231 // internalize from the store 00232 iRichText->RestoreL(*theStore,iStreamId); 00233 // close the store 00234 CleanupStack::PopAndDestroy(); // pop and destroy store 00235 theFs.Close(); 00236 iFormObserver->NotifyStatus(KStatus12); 00237 break; 00238 } 00239 case 13: 00240 //reset document 00241 iRichText->Reset(); 00242 // message to say what we did 00243 iFormObserver->NotifyStatus(KStatusReset); 00244 break; 00245 default: 00246 iFormObserver->NotifyStatus(KStatusDefault); 00247 break; 00248 } 00249 } 00250 00251 00252 void CRichControl::Draw(const TRect& aRect) const 00253 { 00254 // draw surround 00255 CGraphicsContext& gc=SystemGc(); // context to draw into 00256 TRect rect=Rect(); // screen boundary 00257 gc.DrawRect(rect); // outline screen boundary 00258 rect.Shrink(1,1); 00259 gc.SetPenColor(KRgbWhite); 00260 gc.DrawRect(rect); 00261 rect.Shrink(1,1); 00262 gc.SetPenColor(KRgbBlack); 00263 gc.DrawRect(rect); 00264 // draw editable text - will work unless OOM 00265 TInt err; 00266 TRAP(err,iTextView->FormatTextL()); 00267 if (err) return; 00268 TRAP(err,iTextView->DrawL(aRect)); 00269 } 00270 00271
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.