00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "txtexamp.h"
00017
00018 #include <fldset.h>
00019 #include <fldbase.h>
00020 #include <fldbltin.h>
00021 #include <flddef.h>
00022
00023
00024
00025
00026
00027 class TExampleFieldFactory : public MTextFieldFactory
00028
00029
00030 {
00031 public:
00032 virtual CTextField* NewFieldL(TUid aFieldType);
00033 };
00034
00035 CTextField* TExampleFieldFactory::NewFieldL(TUid aFieldType)
00036 {
00037 CTextField* field = NULL;
00038
00039
00040 if (aFieldType==KDateTimeFieldUid)
00041 field = (CTextField*)new(ELeave) CDateTimeField();
00042 return field;
00043 }
00044
00045
00046
00047
00048
00049 CGlobalControl::~CGlobalControl()
00050 {
00051 delete iTextView;
00052 delete iLayout;
00053 delete iGlobalText;
00054 delete iCharFormatLayer;
00055 delete iParaFormatLayer;
00056 }
00057
00058 void CGlobalControl::UpdateModelL()
00059 {
00060
00061
00062 _LIT(KPath,"\\globtxt.dat");
00063 _LIT(KText1,"Some global text.");
00064 _LIT(KText5, "To be, or not to be, that is the question; \
00065 whether 'tis nobler in the mind to suffer the \
00066 slings and arrows of outrageous fortune, or \
00067 to stand against a sea of troubles, and by \
00068 opposing end them.");
00069 _LIT(KText6,"Word at pos %d (\"%S\") has %d characters, starts at pos %d");
00070 _LIT(KText2," And some more global text.");
00071 _LIT(KText3,"A new paragraph.");
00072 _LIT(KText4,"Number of characters=%d, words=%d, paras=%d");
00073 _LIT(KText8,"%D%M%Y%H:%T:%S %1/%2/%3");
00074 _LIT(KStatus0,"Initialised global text object and text view");
00075 _LIT(KStatus1,"InsertL() at position zero");
00076 _LIT(KStatus2,"InsertL() at end of document");
00077 _LIT(KStatus3,"Insert new paragraph, and more text");
00078 _LIT(KStatus5,"Inserted lots of text");
00079 _LIT(KStatus7,"Inserted date time field");
00080 _LIT(KStatus8,"Centre aligned paragraphs");
00081 _LIT(KStatus9,"Set bold, italic and underline (and updated the field)");
00082 _LIT(KStatus10,"Used StoreL() to store text and fields");
00083 _LIT(KStatus12,"Used RestoreL() to restore text and fields");
00084 _LIT(KStatus13,"Deleted lots of text");
00085 _LIT(KStatus14,"Paragraph and character formatting reset to default values");
00086 _LIT(KStatusReset,"Reset();");
00087 _LIT(KStatusDefault,"(Overshot!!)");
00088
00089 TBufC<28> name(KPath);
00090
00091 switch (Phase())
00092 {
00093 case 0:
00094 {
00095
00096 iParaFormatLayer=CParaFormatLayer::NewL();
00097 iCharFormatLayer=CCharFormatLayer::NewL();
00098
00099 iGlobalText=CGlobalText::NewL(iParaFormatLayer, iCharFormatLayer);
00100
00101 iViewRect=Rect();
00102 iViewRect.Shrink(3,3);
00103
00104 CWindowGc& gc=SystemGc();
00105 CBitmapDevice *device=(CBitmapDevice*) (gc.Device());
00106
00107
00108 iLayout=CTextLayout::NewL(iGlobalText,iViewRect.Width());
00109
00110 iTextView=CTextView::NewL(iLayout, iViewRect,
00111 device,
00112 device,
00113 &Window(),
00114 0,
00115 &iCoeEnv->WsSession()
00116 );
00117
00118 iFormObserver->NotifyStatus(KStatus0);
00119 break;
00120 }
00121 case 1:
00122
00123 iCharFormatMask.SetAttrib(EAttFontStrokeWeight);
00124
00125
00126 iCharFormat.iFontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold);
00127
00128
00129 iGlobalText->ApplyCharFormatL(iCharFormat,iCharFormatMask, 0,0);
00130
00131
00132 iGlobalText->InsertL(0,KText1);
00133 iFormObserver->NotifyStatus(KStatus1);
00134 break;
00135 case 2:
00136
00137 iGlobalText->InsertL(iGlobalText->LdDocumentLength(),KText2);
00138 iFormObserver->NotifyStatus(KStatus2);
00139 break;
00140 case 3:
00141
00142 iGlobalText->InsertL(iGlobalText->LdDocumentLength(),CEditableText::EParagraphDelimiter);
00143
00144 iGlobalText->InsertL(iGlobalText->LdDocumentLength(),KText3);
00145 iFormObserver->NotifyStatus(KStatus3);
00146 break;
00147 case 4:
00148 {
00149
00150 TBuf<80> message;
00151 message.Format(KText4,
00152 iGlobalText->LdDocumentLength(),
00153
00154 iGlobalText->WordCount(),
00155 iGlobalText->ParagraphCount()
00156 );
00157 iFormObserver->NotifyStatus(message);
00158 break;
00159 }
00160 case 5:
00161
00162 iGlobalText->InsertL(iGlobalText->LdDocumentLength(),CEditableText::EParagraphDelimiter);
00163 iGlobalText->InsertL(iGlobalText->LdDocumentLength(),KText5);
00164 iFormObserver->NotifyStatus(KStatus5);
00165 break;
00166 case 6:
00167 {
00168
00169 TBuf<80> message;
00170
00171 TInt pos=iGlobalText->LdDocumentLength()/2;
00172 TInt startPos, length;
00173 iGlobalText->GetWordInfo(pos,startPos,length,EFalse,ETrue);
00174
00175
00176
00177 TPtrC ptr=iGlobalText->Read(startPos,length);
00178
00179 message.Format(KText6, pos, &ptr, length, startPos);
00180 iFormObserver->NotifyStatus(message);
00181 break;
00182 }
00183 case 7:
00184 {
00185
00186 TExampleFieldFactory* factory = new(ELeave) TExampleFieldFactory();
00187 CleanupStack::PushL(factory);
00188
00189 iGlobalText->SetFieldFactory(factory);
00190 CTextField* field = iGlobalText->NewTextFieldL(KDateTimeFieldUid);
00191
00192 ((CDateTimeField*)field)->SetFormat(KText8);
00193 iGlobalText->InsertFieldL(0,field,KDateTimeFieldUid);
00194
00195 iGlobalText->UpdateAllFieldsL();
00196
00197
00198 TFindFieldInfo info;
00199 TInt pos=0;
00200 TInt range=0;
00201 iGlobalText->FindFields(info,pos,range);
00202 iGlobalText->InsertL(info.iFirstFieldLen,CEditableText::EParagraphDelimiter);
00203 iFormObserver->NotifyStatus(KStatus7);
00204
00205 CleanupStack::PopAndDestroy();
00206 break;
00207 }
00208 case 8:
00209 {
00210
00211 CParaFormat* paraFormat=CParaFormat::NewLC();
00212 TParaFormatMask paraFormatMask;
00213
00214 paraFormat->iHorizontalAlignment=CParaFormat::ECenterAlign;
00215 paraFormatMask.SetAttrib(EAttAlignment);
00216
00217 iGlobalText->ApplyParaFormatL(paraFormat,paraFormatMask,0,0);
00218 iFormObserver->NotifyStatus(KStatus8);
00219 CleanupStack::PopAndDestroy();
00220 break;
00221 }
00222 case 9:
00223 {
00224
00225 TCharFormat charFormat;
00226 TCharFormatMask charFormatMask;
00227
00228 charFormatMask.SetAttrib(EAttFontUnderline);
00229 charFormatMask.SetAttrib(EAttFontPosture);
00230 charFormatMask.SetAttrib(EAttFontStrokeWeight);
00231
00232
00233 charFormat.iFontPresentation.iUnderline=EUnderlineOn;
00234 charFormat.iFontSpec.iFontStyle.SetPosture(EPostureItalic);
00235 charFormat.iFontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold);
00236
00237 iGlobalText->ApplyCharFormatL(charFormat,charFormatMask, 0,0);
00238
00239 iGlobalText->UpdateFieldL(0);
00240 iFormObserver->NotifyStatus(KStatus9);
00241 break;
00242 }
00243
00244 case 10:
00245
00246 {
00247 RFs theFs;
00248 CFileStore* theStore;
00249 TParse filestorename;
00250
00251 theFs.Connect();
00252 theFs.Parse(name,filestorename);
00253 theStore=CDirectFileStore::ReplaceLC(theFs,filestorename.FullName(),EFileRead|EFileWrite);
00254 theStore->SetTypeL(KDirectFileStoreLayoutUid);
00255
00256 iStreamId=iGlobalText->StoreL(*theStore);
00257
00258 CleanupStack::PopAndDestroy();
00259
00260 theFs.Close();
00261 iFormObserver->NotifyStatus(KStatus10);
00262 break;
00263 }
00264 case 11:
00265
00266 iGlobalText->Reset();
00267 iFormObserver->NotifyStatus(KStatusReset);
00268 break;
00269 case 12:
00270 {
00271
00272 RFs theFs;
00273 CFileStore* theStore;
00274 TParse filestorename;
00275
00276 theFs.Connect();
00277 theFs.Parse(name,filestorename);
00278 theStore=CDirectFileStore::OpenLC(theFs,filestorename.FullName(),EFileRead|EFileShareReadersOnly);
00279 if (theStore->Type()[0]!=KDirectFileStoreLayoutUid)
00280 User::Leave(KErrUnknown);
00281
00282 iGlobalText->RestoreL(*theStore,iStreamId);
00283
00284 CleanupStack::PopAndDestroy();
00285 theFs.Close();
00286 iGlobalText->UpdateFieldL(0);
00287 iFormObserver->NotifyStatus(KStatus12);
00288 break;
00289 }
00290 case 13:
00291
00292
00293
00294 {
00295 TInt pos=iGlobalText->LdDocumentLength()/2;
00296 iGlobalText->ToParagraphStart(pos);
00297 pos--;
00298 iGlobalText->DeleteL(pos, iGlobalText->LdDocumentLength()-pos);
00299
00300 iFormObserver->NotifyStatus(KStatus13);
00301 break;
00302 }
00303 case 14:
00304 {
00305
00306 TCharFormat charFormat;
00307 TCharFormatMask charFormatMask;
00308 CParaFormat* paraFormat=CParaFormat::NewLC();
00309 TParaFormatMask paraFormatMask;
00310
00311 charFormatMask.SetAll();
00312 iGlobalText->ApplyCharFormatL(charFormat,charFormatMask,0,1);
00313
00314 paraFormatMask.SetAll();
00315 iGlobalText->ApplyParaFormatL(paraFormat, paraFormatMask, 0,1);
00316
00317 iFormObserver->NotifyStatus(KStatus14);
00318 CleanupStack::PopAndDestroy();
00319 break;
00320 }
00321 case 15:
00322
00323 iGlobalText->Reset();
00324 iFormObserver->NotifyStatus(KStatusReset);
00325 break;
00326 default:
00327 iFormObserver->NotifyStatus(KStatusDefault);
00328 break;
00329 }
00330 }
00331
00332 void CGlobalControl::Draw(const TRect& aRect) const
00333 {
00334
00335 CGraphicsContext& gc=SystemGc();
00336 TRect rect=Rect();
00337 gc.DrawRect(rect);
00338 rect.Shrink(1,1);
00339 gc.SetPenColor(KRgbWhite);
00340 gc.DrawRect(rect);
00341 rect.Shrink(1,1);
00342 gc.SetPenColor(KRgbBlack);
00343 gc.DrawRect(rect);
00344
00345 TInt err;
00346 TRAP(err,iTextView->FormatTextL());
00347 if (err) return;
00348 TRAP(err,iTextView->DrawL(aRect));
00349 }
00350
00351