examples/AppFramework/txtshell/txtview.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 CViewControl::~CViewControl()
00021         {
00022         delete iTextView; // text view
00023         delete iLayout; // text layout
00024         delete iRichText; // contained text object
00025         delete iCharFormatLayer; // char format layer
00026         delete iParaFormatLayer; // para format layer
00027         delete iBitmap; // line cursor character bitmap
00028         }
00029 
00030 void CViewControl::UpdateModelL()
00031         {
00032         // Create all constant literal descriptors used in this function, 
00033         // e.g. for message text
00034 
00035         _LIT(KText1, "To be, or not to be, that is the question; \
00036 whether 'tis nobler in the mind to suffer the \
00037 slings and arrows of outrageous fortune, or \
00038 to stand against a sea of troubles, and by \
00039 opposing end them."); 
00040         _LIT(KText4,"Format band set. Num formatted lines=%d");
00041         _LIT(KText5,"Format band unset. Num formatted lines=%d");
00042         _LIT(KPathAndFile,"\\resource\\apps\\cursors.mbm");
00043         _LIT(KCDrive,"\\");
00044         _LIT(KZDrive,"Z:");
00045         _LIT(KStatus0,"Initialised - with grey fill colour");
00046         _LIT(KStatus1,"Added lots of text");
00047         _LIT(KStatus2,"Selected first paragraph");
00048         _LIT(KStatus3,"Formatted the selected text (and cancelled selection)");
00049         _LIT(KStatus6,"Set 20 pixel line cursor margin and 2 cursors");
00050         _LIT(KStatus7,"Reset the view rectangle - old wrapping still applies");
00051         _LIT(KStatus8,"Text wrapping set to new view rectangle");
00052         _LIT(KStatus9,"View rectangle and wrap width reset");
00053         _LIT(KStatus10,"Horizontal scroll left");
00054         _LIT(KStatus11,"Scrolling DOWN, blank space NOT scrolled");
00055         _LIT(KStatus12,"Back to top and horizontal scroll right");
00056         _LIT(KStatus13,"Reset();");
00057         _LIT(KStatusDefault,"(overshot!!)");
00058 
00059         switch (Phase())
00060                 {
00061         case 0:
00062                 {
00063                 // Preparation for following cases:
00064                 TCharFormat charFormat;
00065                 TCharFormatMask charFormatMask;
00066                 iUpdateModelDoesRedraw=ETrue; 
00067                         // NextPhaseL() should call DrawNow() for the first case only
00068                 // Create rich text object to be displayed in the text view
00069                 iParaFormatLayer=CParaFormatLayer::NewL(); // required para format
00070                 iCharFormatLayer=CCharFormatLayer::NewL(); // required char format
00071                                 // empty text objects
00072                 iRichText=CRichText::NewL(iParaFormatLayer, iCharFormatLayer);
00073                 // prerequisites for view - viewing rectangle
00074                 iViewRect=Rect();
00075                 iViewRect.Shrink(3,3);
00076                 // context and device
00077                 CWindowGc& gc=SystemGc(); // get graphics context
00078                 CBitmapDevice* device=(CBitmapDevice*) (gc.Device()); // device
00079                 // Construct the layout object used by the text view,
00080                 // specifying wrap width (= width of viewing rectangle)
00081                 iLayout=CTextLayout::NewL(iRichText,iViewRect.Width());
00082                 // Construct text view
00083                 iTextView=CTextView::NewL(iLayout, iViewRect,
00084                         device,
00085                         device,
00086                         &Window(),
00087                         &iCoeEnv->RootWin(), // window group, needed for cursor
00088                         &iCoeEnv->WsSession()
00089                         ); // new view
00090                 // Set paragraph fill color and font height.
00091                 // For visibility, set font height to 10 point.
00092                 charFormatMask.SetAttrib(EAttFontHeight); 
00093                         // height attribute only is relevant
00094                 charFormat.iFontSpec.iHeight=200; // set to 10 point (200 twips)
00095                 iRichText->ApplyCharFormatL(charFormat,charFormatMask,0,0);
00096                 // Apply grey paragraph fill colour 
00097                 CParaFormat* paraFormat=CParaFormat::NewL();
00098                 TParaFormatMask paraFormatMask;
00099                 paraFormat->iFillColor=TRgb(204,204,204); // Light grey
00100                 paraFormatMask.SetAttrib(EAttFillColor);
00101                         // fill colour attribute only is relevant
00102                 iRichText->ApplyParaFormatL(paraFormat,paraFormatMask,0,0);
00103                 iTextView->FormatTextL();
00104                 iFormObserver->NotifyStatus(KStatus0);
00105                 delete paraFormat;
00106                 break;
00107                 }
00108         case 1:
00109                 {
00110                 // Set some character formatting
00111                 iCharFormatMask.SetAttrib(EAttFontStrokeWeight);  
00112                 
00113                 iCharFormat.iFontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold);
00114 
00115                 // apply formatting - pos and length are irrelevent
00116                 iRichText->ApplyCharFormatL(iCharFormat,iCharFormatMask, 0,0);
00117                 
00118                 // insert lots of text
00119                 for (TInt count=0;count<5;count++) // insert a paragraph, *5
00120                         {       
00121                         iRichText->InsertL(iRichText->DocumentLength(),KText1); // text
00122                         iRichText->InsertL(iRichText->DocumentLength(),
00123                                 CEditableText::EParagraphDelimiter); // end para
00124                         };
00125                 TCursorSelection selection(0, iRichText->DocumentLength());
00126                         // start and length of the inserted block
00127                 iTextView->HandleInsertDeleteL(selection, 0); 
00128                         // zero deleted characters 
00129                 iFormObserver->NotifyStatus(KStatus1);
00130                 break;
00131                 }
00132         case 2:
00133                 {
00134                 // Select the first paragraph
00135                 TInt pos=1;
00136                 TUint scanMask=CPlainText::EScanToUnitEnd;
00137                 iRichText->ScanParas(pos,scanMask);
00138                         // get end position of current para
00139                 // move cursor to end of para and select it
00140                 iTextView->SetDocPosL(pos,ETrue); // ETrue = select the range
00141                 iFormObserver->NotifyStatus(KStatus2);
00142                 break;
00143                 }
00144         case 3:
00145                 {
00146                 // Apply italics to the selected region
00147                 TCursorSelection cursorSelection;
00148                 TCharFormat charFormat;
00149                 TCharFormatMask charFormatMask;
00150                 // Set italics
00151                 charFormat.iFontSpec.iFontStyle.SetPosture(EPostureItalic); 
00152                 charFormatMask.SetAttrib(EAttFontPosture); 
00153                         // font posture attribute only is relevant
00154                 cursorSelection=iTextView->Selection(); // get range of selection
00155                 TInt lowerPos=cursorSelection.LowerPos();
00156                 TInt length=cursorSelection.Length();
00157                 // Apply formatting to range
00158                 iRichText->ApplyCharFormatL(charFormat,charFormatMask,lowerPos,length);
00159                 iTextView->HandleRangeFormatChangeL(cursorSelection); // reformat from here
00160                 iTextView->CancelSelectionL(); // remove selection and redraw
00161                 iFormObserver->NotifyStatus(KStatus3);
00162                 break;
00163                 }
00164         case 4:
00165                 {
00166                 TBuf<80> message;
00167                 // Set formatting to the band only and display the number of formatted lines.
00168                 iLayout->SetAmountToFormat(CTextLayout::EFFormatBand);
00169                 iTextView->HandleGlobalChangeL(); // global layout change 
00170                 message.Format(KText4,iLayout->NumFormattedLines());
00171                 iFormObserver->NotifyStatus(message);
00172                 break;
00173                 }
00174         case 5:
00175                 {
00176                 TBuf<80> message;
00177                 // Set formatting to the whole document, then display the number of 
00178                 // formatted lines.
00179                 iLayout->SetAmountToFormat(CTextLayout::EFFormatAllText);
00180                 iTextView->HandleGlobalChangeL(); // global layout change 
00181                 message.Format(KText5,iLayout->NumFormattedLines());
00182                 iFormObserver->NotifyStatus(message);
00183                 break;
00184                 }
00185         case 6:
00186                 {
00187                 // Set line cursor margin and line cursor bitmap
00188                 iTextView->SetMarginWidths(0,20); // zero label margin, 20 pixel line cursor margin
00189                 iTextView->DrawL(iViewRect); // update the view (SetMarginWidths() doesn't redraw)
00190                 // Load in bitmap to represent line cursor.
00191                 iBitmap=new(ELeave) CFbsBitmap();
00192                 TBufC<40> pathandfile(KPathAndFile);
00193                 TParse    bitmapFileName;
00194                 TBufC<2>  c_drive(KCDrive);
00195                 TBufC<2>  z_drive(KZDrive);
00196 
00197             bitmapFileName.Set(pathandfile,&c_drive,NULL);
00198                 
00199                 if (iBitmap->Load(bitmapFileName.FullName())) 
00200                         // can't find cursor bitmap on C: drive, so try Z:
00201                         {
00202                         bitmapFileName.Set(pathandfile,&z_drive,NULL);
00203                         iBitmap->Load(bitmapFileName.FullName());
00204                         iTextView->SetLineCursorBitmap(iBitmap);
00205                         iTextView->SetCursorVisibilityL(TCursor::EFCursorVisible,
00206                                 TCursor::EFCursorFlashing); 
00207                         // both cursors now visible - flashing text cursor
00208                         iFormObserver->NotifyStatus(KStatus6);
00209                         }
00210                 break;
00211                 }
00212         case 7:
00213                 {
00214                 // Reset the view rectangle.
00215                 // Before doing this, clear the existing one,
00216                 // (the view should really be implemented as a control, in a container control,
00217                 // and then the background would get cleared nicely by the container).
00218                 ActivateGc();
00219                 SystemGc().Clear(iViewRect);
00220                 DeactivateGc();
00221                 // Shrink the view rectangle by 40 pixels both directions
00222                 iViewRect.Shrink(40,40);
00223                 iTextView->SetViewRect(iViewRect);
00224                 iTextView->DrawL(iViewRect); // Redraw the completely revamped view 
00225                         // (SetViewRect() does not do a redraw)
00226                 iFormObserver->NotifyStatus(KStatus7);
00227                 break;
00228                 }
00229         case 8:
00230                 {
00231                 // Wrap text to new view rectangle.
00232                 // New wrap width = new view rect width minus total margin width.
00233                 // First calculate the total margin width.
00234                 TInt labelMarginWidth;
00235                 TInt lineCursorMarginWidth;
00236                 iTextView->MarginWidths(labelMarginWidth,lineCursorMarginWidth);
00237                 iLayout->SetWrapWidth(iViewRect.Width()-(labelMarginWidth+lineCursorMarginWidth));
00238                 iLayout->ForceNoWrapping(EFalse); // Ensure wrapping on
00239                 iTextView->HandleGlobalChangeL();
00240                 iFormObserver->NotifyStatus(KStatus8);
00241                 break;
00242                 }
00243         case 9:
00244                 {
00245                 // Reset view rectangle back to old dimensions and set wrapping
00246                 // width accordingly
00247                 TInt labelMarginWidth;
00248                 TInt lineCursorMarginWidth;
00249                 iTextView->MarginWidths(labelMarginWidth,lineCursorMarginWidth);
00250                 iViewRect.Grow(40,40);
00251                         // Increase view rectangle by 40 pixels both directions
00252                 iTextView->SetViewRect(iViewRect);
00253                 iLayout->SetWrapWidth(iViewRect.Width()-(labelMarginWidth+lineCursorMarginWidth));
00254                 iTextView->HandleGlobalChangeL();
00255                 iFormObserver->NotifyStatus((KStatus9));
00256                 break;
00257                 }
00258         case 10:
00259                 // Horizontal scrolling
00260                 iTextView->SetHorizontalScrollJump(40); // scroll jump is 40 pixels
00261                 iTextView->ScrollDisplayL(TCursorPosition::EFLeft,CTextLayout::EFAllowScrollingBlankSpace); 
00262                         // scroll to the left (scrolling blank space is allowed)
00263                 iFormObserver->NotifyStatus(KStatus10);
00264                 break;
00265         case 11:
00266                 {
00267                 // Vertical scrolling
00268                 iFormObserver->NotifyStatus(KStatus11);
00269                 TInt pixelsScrolled;
00270                 // Scroll down to the bottom of the document, 
00271                 do
00272                         pixelsScrolled=iTextView->ScrollDisplayL(TCursorPosition::EFLineDown,
00273                                 CTextLayout::EFDisallowScrollingBlankSpace);
00274                 while (pixelsScrolled);
00275                 break;
00276                 }
00277         case 12:
00278                 {
00279                 // Horizontal scroll back to left margin and to top of doc
00280                 iFormObserver->NotifyStatus(KStatus12);
00281                 TInt pixelsScrolled;
00282                 do
00283                         pixelsScrolled=iTextView->ScrollDisplayL(TCursorPosition::EFLineUp);            
00284                 while (pixelsScrolled);
00285                 iTextView->ScrollDisplayL(TCursorPosition::EFRight); 
00286                         // scroll right (horizontal scroll jump value=40 pixels)
00287                 break;
00288                 }
00289         case 13:
00290                 {
00291                 // Reset document. 
00292                 iRichText->Reset(); // change whole document!
00293                 iTextView->FormatTextL(); // so format the whole lot
00294                 iTextView->DrawL(iViewRect); // and draw it afresh
00295                 iFormObserver->NotifyStatus(KStatus13);
00296                 break;
00297                 }
00298         default:
00299                 iFormObserver->NotifyStatus(KStatusDefault);
00300                 break;
00301                 }
00302         }
00303 
00304 void CViewControl::Draw(const TRect& aRect) const
00305         {
00306         // draw surround
00307         CGraphicsContext& gc=SystemGc(); // context to draw into
00308         TRect rect=Rect(); // screen boundary
00309         gc.DrawRect(rect); // outline screen boundary
00310         rect.Shrink(1,1);
00311         gc.SetPenColor(KRgbWhite);
00312         gc.DrawRect(rect);
00313         rect.Shrink(1,1);
00314         gc.SetPenColor(KRgbBlack);
00315         gc.DrawRect(rect);
00316         // draw editable text - will work unless OOM
00317         TInt err;
00318         TRAP(err,iTextView->DrawL(aRect));
00319         }
00320 

Generated by  doxygen 1.6.2