00001 // Copyright (c) 1997-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 <fldset.h> 00019 #include <fldbase.h> 00020 #include <fldbltin.h> 00021 #include <flddef.h> 00022 00023 // 00024 // field factory 00025 // 00026 00027 class TExampleFieldFactory : public MTextFieldFactory 00028 // A text field factory. 00029 // It creates date/time fields only. 00030 { 00031 public: 00032 virtual CTextField* NewFieldL(TUid aFieldType); 00033 }; 00034 00035 CTextField* TExampleFieldFactory::NewFieldL(TUid aFieldType) 00036 { 00037 CTextField* field = NULL; // NULL keeps GCC compiler happy 00038 // Factory only produces date time fields, if aFieldType is 00039 // not a date time field type, do nothing. 00040 if (aFieldType==KDateTimeFieldUid) 00041 field = (CTextField*)new(ELeave) CDateTimeField(); 00042 return field; 00043 } 00044 00045 // 00046 // CGlobalControl implementation 00047 // 00048 00049 CGlobalControl::~CGlobalControl() 00050 { 00051 delete iTextView; // text view 00052 delete iLayout; // layout 00053 delete iGlobalText; // contained text object 00054 delete iCharFormatLayer; // char format layer 00055 delete iParaFormatLayer; // and para format layer 00056 } 00057 00058 void CGlobalControl::UpdateModelL() 00059 { 00060 // Create all constant literal descriptors used in this function, 00061 // e.g. for message text 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 // Create text object, text view and layout. 00096 iParaFormatLayer=CParaFormatLayer::NewL(); // required para format layer 00097 iCharFormatLayer=CCharFormatLayer::NewL(); // required char format layer 00098 // Create an empty global text object 00099 iGlobalText=CGlobalText::NewL(iParaFormatLayer, iCharFormatLayer); 00100 // prerequisites for view - viewing rectangle 00101 iViewRect=Rect(); 00102 iViewRect.Shrink(3,3); 00103 // context and device 00104 CWindowGc& gc=SystemGc(); // get graphics context 00105 CBitmapDevice *device=(CBitmapDevice*) (gc.Device()); // device 00106 // Create the text layout, (required by text view), 00107 // with the text object and a wrap width (=width of view rect) 00108 iLayout=CTextLayout::NewL(iGlobalText,iViewRect.Width()); 00109 // Create text view 00110 iTextView=CTextView::NewL(iLayout, iViewRect, 00111 device, 00112 device, 00113 &Window(), 00114 0, // no window group 00115 &iCoeEnv->WsSession() 00116 ); // new view 00117 // message to say what we did 00118 iFormObserver->NotifyStatus(KStatus0); 00119 break; 00120 } 00121 case 1: 00122 // Set some character formatting - applied globally 00123 iCharFormatMask.SetAttrib(EAttFontStrokeWeight); // set weight (for bold) 00124 00125 // Set bold 00126 iCharFormat.iFontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold); 00127 00128 // apply formatting - pos and length are irrelevent 00129 iGlobalText->ApplyCharFormatL(iCharFormat,iCharFormatMask, 0,0); 00130 00131 // Insert some text 00132 iGlobalText->InsertL(0,KText1); 00133 iFormObserver->NotifyStatus(KStatus1); 00134 break; 00135 case 2: 00136 // Insert some more text 00137 iGlobalText->InsertL(iGlobalText->LdDocumentLength(),KText2); 00138 iFormObserver->NotifyStatus(KStatus2); 00139 break; 00140 case 3: 00141 // Insert a new paragraph 00142 iGlobalText->InsertL(iGlobalText->LdDocumentLength(),CEditableText::EParagraphDelimiter); 00143 // Insert text to follow new paragraph delimiter 00144 iGlobalText->InsertL(iGlobalText->LdDocumentLength(),KText3); 00145 iFormObserver->NotifyStatus(KStatus3); 00146 break; 00147 case 4: 00148 { 00149 // Display document info in status message 00150 TBuf<80> message; // for formatting status messages 00151 message.Format(KText4, 00152 iGlobalText->LdDocumentLength(), 00153 // length up to and excluding final para mark 00154 iGlobalText->WordCount(), // white-space delimited words 00155 iGlobalText->ParagraphCount() // number paras including the final one 00156 ); 00157 iFormObserver->NotifyStatus(message); 00158 break; 00159 } 00160 case 5: 00161 // Insert lots of text 00162 iGlobalText->InsertL(iGlobalText->LdDocumentLength(),CEditableText::EParagraphDelimiter); 00163 iGlobalText->InsertL(iGlobalText->LdDocumentLength(),KText5); 00164 iFormObserver->NotifyStatus(KStatus5); 00165 break; 00166 case 6: 00167 { 00168 // Display information about a random document position 00169 TBuf<80> message; // for formatting status messages 00170 // Print info about word at a document position 00171 TInt pos=iGlobalText->LdDocumentLength()/2; 00172 TInt startPos, length; // results of function 00173 iGlobalText->GetWordInfo(pos,startPos,length,EFalse,ETrue); 00174 // gets startPos and length, given pos 00175 // Insert all text into buffer from startPos (start of word at 00176 // position pos) onwards 00177 TPtrC ptr=iGlobalText->Read(startPos,length); 00178 // Print out word and word info 00179 message.Format(KText6, pos, &ptr, length, startPos); 00180 iFormObserver->NotifyStatus(message); 00181 break; 00182 } 00183 case 7: 00184 { 00185 // Insert date/time field into document 00186 TExampleFieldFactory* factory = new(ELeave) TExampleFieldFactory(); 00187 CleanupStack::PushL(factory); 00188 // Set up the field factory 00189 iGlobalText->SetFieldFactory(factory); 00190 CTextField* field = iGlobalText->NewTextFieldL(KDateTimeFieldUid); 00191 // Format date to hour:minute:second day/month/year 00192 ((CDateTimeField*)field)->SetFormat(KText8); 00193 iGlobalText->InsertFieldL(0,field,KDateTimeFieldUid); 00194 // Evaluate the field (makes contents visible) 00195 iGlobalText->UpdateAllFieldsL(); 00196 // Insert new para delimiter after field 00197 // First get length of field we've just inserted 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 // clean up 00205 CleanupStack::PopAndDestroy(); // factory 00206 break; 00207 } 00208 case 8: 00209 { 00210 // Set some paragraph formatting - applied globally 00211 CParaFormat* paraFormat=CParaFormat::NewLC(); 00212 TParaFormatMask paraFormatMask; 00213 // Set centre alignment 00214 paraFormat->iHorizontalAlignment=CParaFormat::ECenterAlign; 00215 paraFormatMask.SetAttrib(EAttAlignment); 00216 // apply formatting - pos and length are irrelevent 00217 iGlobalText->ApplyParaFormatL(paraFormat,paraFormatMask,0,0); 00218 iFormObserver->NotifyStatus(KStatus8); 00219 CleanupStack::PopAndDestroy(); // paraFormat 00220 break; 00221 } 00222 case 9: 00223 { 00224 // Set some character formatting - applied globally 00225 TCharFormat charFormat; 00226 TCharFormatMask charFormatMask; 00227 // Interested in underline, posture and weight 00228 charFormatMask.SetAttrib(EAttFontUnderline); // set underline 00229 charFormatMask.SetAttrib(EAttFontPosture); // and posture (for italic) 00230 charFormatMask.SetAttrib(EAttFontStrokeWeight); // set weight (for bold) 00231 00232 // Set bold, italics and underlining 00233 charFormat.iFontPresentation.iUnderline=EUnderlineOn; 00234 charFormat.iFontSpec.iFontStyle.SetPosture(EPostureItalic); 00235 charFormat.iFontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold); 00236 // apply formatting - pos and length are irrelevent 00237 iGlobalText->ApplyCharFormatL(charFormat,charFormatMask, 0,0); 00238 // And update the field 00239 iGlobalText->UpdateFieldL(0); 00240 iFormObserver->NotifyStatus(KStatus9); 00241 break; 00242 } 00243 // Storing and restoring 00244 case 10: 00245 // set up a file store 00246 { 00247 RFs theFs; 00248 CFileStore* theStore; 00249 TParse filestorename; 00250 // Make a connection to the file server 00251 theFs.Connect(); 00252 theFs.Parse(name,filestorename); 00253 theStore=CDirectFileStore::ReplaceLC(theFs,filestorename.FullName(),EFileRead|EFileWrite); 00254 theStore->SetTypeL(KDirectFileStoreLayoutUid); 00255 // store global text to file store 00256 iStreamId=iGlobalText->StoreL(*theStore); 00257 // close file store 00258 CleanupStack::PopAndDestroy(); // pop and destroy store 00259 // Disconnect from file server 00260 theFs.Close(); 00261 iFormObserver->NotifyStatus(KStatus10); 00262 break; 00263 } 00264 case 11: 00265 // reset document, clearing it of all content 00266 iGlobalText->Reset(); 00267 iFormObserver->NotifyStatus(KStatusReset); 00268 break; 00269 case 12: 00270 { 00271 // set up file store 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 // restore text and components from file store 00282 iGlobalText->RestoreL(*theStore,iStreamId); 00283 // close file store 00284 CleanupStack::PopAndDestroy(); // pop and destroy store 00285 theFs.Close(); 00286 iGlobalText->UpdateFieldL(0); 00287 iFormObserver->NotifyStatus(KStatus12); 00288 break; 00289 } 00290 case 13: 00291 // Delete all text from paragraph containing a random doc position to 00292 // end of document. Also delete preceding paragraph delimiter to keep 00293 // paragraph count correct. 00294 { 00295 TInt pos=iGlobalText->LdDocumentLength()/2; 00296 iGlobalText->ToParagraphStart(pos); // pos set to start of paragraph 00297 pos--; // decrement so pos = preceding para delimiter 00298 iGlobalText->DeleteL(pos, iGlobalText->LdDocumentLength()-pos); 00299 // delete rest of text 00300 iFormObserver->NotifyStatus(KStatus13); 00301 break; 00302 } 00303 case 14: 00304 { 00305 // Reset all formatting back to the default 00306 TCharFormat charFormat; 00307 TCharFormatMask charFormatMask; 00308 CParaFormat* paraFormat=CParaFormat::NewLC(); 00309 TParaFormatMask paraFormatMask; 00310 // Reset all character format attributes 00311 charFormatMask.SetAll(); 00312 iGlobalText->ApplyCharFormatL(charFormat,charFormatMask,0,1); 00313 // format is all empty, mask is all set: thus, everything to default 00314 paraFormatMask.SetAll(); 00315 iGlobalText->ApplyParaFormatL(paraFormat, paraFormatMask, 0,1); 00316 // ditto 00317 iFormObserver->NotifyStatus(KStatus14); 00318 CleanupStack::PopAndDestroy(); // paraFormat 00319 break; 00320 } 00321 case 15: 00322 // reset document, clearing it of all text content and fields 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 // draw surround 00335 CGraphicsContext& gc=SystemGc(); // context to draw into 00336 TRect rect=Rect(); // screen boundary 00337 gc.DrawRect(rect); // outline screen boundary 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 // draw editable text - will work unless OOM 00345 TInt err; 00346 TRAP(err,iTextView->FormatTextL()); 00347 if (err) return; 00348 TRAP(err,iTextView->DrawL(aRect)); 00349 } 00350 00351
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.