00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "txtexamp.h"
00017
00018 #include <txtrich.h>
00019
00020 CRichControl::~CRichControl()
00021 {
00022 delete iTextView;
00023 delete iLayout;
00024 delete iRichText;
00025 delete iCharFormatLayer;
00026 delete iParaFormatLayer;
00027 }
00028
00029
00030 void CRichControl::UpdateModelL()
00031 {
00032
00033
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
00066 iParaFormatLayer=CParaFormatLayer::NewL();
00067 iCharFormatLayer=CCharFormatLayer::NewL();
00068
00069 iRichText=CRichText::NewL(iParaFormatLayer, iCharFormatLayer);
00070
00071 iViewRect=Rect();
00072 iViewRect.Shrink(3,3);
00073
00074 CWindowGc& gc=SystemGc();
00075 CBitmapDevice *device=(CBitmapDevice*) (gc.Device());
00076
00077
00078 iLayout=CTextLayout::NewL(iRichText,iViewRect.Width());
00079
00080 iTextView=CTextView::NewL(iLayout, iViewRect,
00081 device,
00082 device,
00083 &Window(),
00084 0,
00085 &iCoeEnv->WsSession()
00086 );
00087
00088 iFormObserver->NotifyStatus(KStatus0);
00089 break;
00090 }
00091 case 1:
00092
00093 iCharFormatMask.SetAttrib(EAttFontStrokeWeight);
00094
00095 iCharFormat.iFontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold);
00096
00097
00098 iRichText->ApplyCharFormatL(iCharFormat,iCharFormatMask, 0,0);
00099
00100
00101 iRichText->InsertL(0,KText1);
00102 iFormObserver->NotifyStatus(KStatus1);
00103 break;
00104 case 2:
00105
00106 {
00107 TCharFormat charFormat;
00108 TCharFormatMask charFormatMask;
00109 charFormatMask.SetAttrib(EAttFontHeight);
00110 charFormat.iFontSpec.iHeight=480;
00111 TInt pos=iRichText->DocumentLength();
00112 iRichText->SetInsertCharFormatL(charFormat, charFormatMask,pos);
00113
00114 iRichText->InsertL(pos,KText2);
00115 iRichText->CancelInsertCharFormat();
00116
00117 iFormObserver->NotifyStatus(KStatus2);
00118 break;
00119 }
00120 case 3:
00121 {
00122
00123 TCharFormat charFormat;
00124 TCharFormatMask charFormatMask;
00125 charFormatMask.SetAttrib(EAttFontUnderline);
00126 charFormat.iFontPresentation.iUnderline=EUnderlineOn;
00127 iRichText->ApplyCharFormatL(charFormat, charFormatMask,13,10);
00128
00129 iFormObserver->NotifyStatus(KStatus3);
00130 break;
00131 }
00132 case 4:
00133
00134
00135 iRichText->DelSetInsertCharFormatL(19,(iRichText->DocumentLength()-19));
00136
00137 iFormObserver->NotifyStatus(KStatus4);
00138 break;
00139 case 5:
00140
00141 iRichText->InsertL(19,KText5);
00142
00143 iRichText->CancelInsertCharFormat();
00144 iFormObserver->NotifyStatus(KStatus5);
00145 break;
00146 case 6:
00147
00148 iRichText->DeleteL(19,(iRichText->DocumentLength()-19));
00149
00150 iFormObserver->NotifyStatus(KStatus6);
00151 break;
00152 case 7:
00153
00154
00155 iRichText->InsertL(19,KText7);
00156
00157
00158 iFormObserver->NotifyStatus(KStatus7);
00159 break;
00160 case 8:
00161 {
00162
00163
00164
00165
00166 iRichText->RemoveSpecificCharFormatL(iRichText->DocumentLength()-1,1);
00167 iRichText->InsertL(iRichText->DocumentLength(),
00168 CEditableText::EParagraphDelimiter);
00169 for (TInt count=0;count<2;count++)
00170 {
00171 iRichText->InsertL(iRichText->DocumentLength(),KHamletSpeech);
00172 iRichText->InsertL(iRichText->DocumentLength(),
00173 CEditableText::EParagraphDelimiter);
00174 };
00175 iFormObserver->NotifyStatus(KStatus8);
00176 break;
00177 }
00178 case 9:
00179 {
00180 CParaFormat* paraFormat=CParaFormat::NewLC();
00181 TParaFormatMask paraFormatMask;
00182
00183 TInt pos, length;
00184 paraFormatMask.SetAttrib(EAttAlignment);
00185 paraFormat->iHorizontalAlignment=CParaFormat::ERightAlign;
00186 pos=iRichText->CharPosOfParagraph(length,1);
00187 iRichText->ApplyParaFormatL(paraFormat,paraFormatMask,pos,1);
00188
00189 iFormObserver->NotifyStatus(KStatus9);
00190 CleanupStack::PopAndDestroy();
00191 break;
00192 }
00193
00194 case 10:
00195
00196 {
00197 RFs theFs;
00198 CFileStore* theStore;
00199 TParse filestorename;
00200
00201 theFs.Connect();
00202 theFs.Parse(name,filestorename);
00203 theStore=CDirectFileStore::ReplaceLC(theFs,filestorename.FullName(),EFileRead|EFileWrite);
00204 theStore->SetTypeL(KDirectFileStoreLayoutUid);
00205
00206 iStreamId=iRichText->StoreL(*theStore);
00207
00208 CleanupStack::PopAndDestroy();
00209
00210 theFs.Close();
00211 iFormObserver->NotifyStatus(KStatus10);
00212 break;
00213 }
00214 case 11:
00215
00216 iRichText->Reset();
00217 iFormObserver->NotifyStatus(KStatusReset);
00218 break;
00219 case 12:
00220
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
00232 iRichText->RestoreL(*theStore,iStreamId);
00233
00234 CleanupStack::PopAndDestroy();
00235 theFs.Close();
00236 iFormObserver->NotifyStatus(KStatus12);
00237 break;
00238 }
00239 case 13:
00240
00241 iRichText->Reset();
00242
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
00255 CGraphicsContext& gc=SystemGc();
00256 TRect rect=Rect();
00257 gc.DrawRect(rect);
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
00265 TInt err;
00266 TRAP(err,iTextView->FormatTextL());
00267 if (err) return;
00268 TRAP(err,iTextView->DrawL(aRect));
00269 }
00270
00271