examples/Graphics/GraphicsShell/Draw.cpp

00001 /*
00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008 � list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010 � this list of conditions and the following disclaimer in the documentation
00011 � and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013 � may be used to endorse or promote products derived from this software
00014 � without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:� 
00028 */
00029 
00030 
00031 #include "GraphicsControl.h"
00032 
00033 #include <coemain.h>
00034 
00035 // Text printed to the console in UpdateModeL()
00036 _LIT(KTxtUpdateModelCase0,"point in center of screen");
00037 _LIT(KTxtUpdateModelCase1,"bolder point in center of screen");
00038 _LIT(KTxtUpdateModelCase2,"really bold point in center of screen");
00039 _LIT(KTxtUpdateModelCase3,"a line");
00040 _LIT(KTxtUpdateModelCase4,"a thicker line");
00041 _LIT(KTxtUpdateModelCase5,"really thick - note round ends and clipping");
00042 _LIT(KTxtUpdateModelCase6,"dotted line");
00043 _LIT(KTxtUpdateModelCase7,"dot-dash line");
00044 _LIT(KTxtUpdateModelCase8,"triangle using relative drawing");
00045 _LIT(KTxtUpdateModelCase9,"thick triangle - note rounded corners");
00046 _LIT(KTxtUpdateModelCase10,"dotted triangle - note pattern continuation");
00047 _LIT(KTxtUpdateModelCase11,"centered ellipse");
00048 _LIT(KTxtUpdateModelCase12,"arc - part of ellipse");
00049 _LIT(KTxtUpdateModelCase13,"arc - other part - see also construction elements");
00050 _LIT(KTxtUpdateModelCase14,"pie slices");
00051 _LIT(KTxtUpdateModelCase15,"centered rectangle with rounded corners");
00052 _LIT(KTxtUpdateModelCase16,"rounded rectangle showing corner ellipse");
00053 _LIT(KTxtUpdateModelCase17,"polyline");
00054 _LIT(KTxtUpdateModelCase18,"polygon with winding-fill rule");
00055 _LIT(KTxtUpdateModelCase19,"polygon using alternate-fill rule");
00056 _LIT(KTxtUpdateModelCase20,"copying using CopyRect()");
00057 _LIT(KTxtUpdateModelCase21,"left-justified boxed text");
00058 _LIT(KTxtUpdateModelCase22,"centered boxed text");
00059 _LIT(KTxtUpdateModelCase23,"right-justified offset text");
00060 _LIT(KTxtUpdateModelCase24,"cross-hatched box");
00061 _LIT(KTxtUpdateModelCase25,"teeny preview font!");
00062 _LIT(KTxtUpdateModelDefault,"overran!");
00063 
00064 static CArrayFix<TPoint>* CreatePointsArrayL();
00065 
00066 void CDrawControl::UpdateModelL()
00067         {
00068         switch (Phase())
00069                 {
00070                 case 0:
00071                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase0);
00072                         break;
00073                 case 1:
00074                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase1);
00075                         break;
00076                 case 2:
00077                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase2);
00078                         break;
00079                 case 3:
00080                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase3);
00081                         break;
00082                 case 4:
00083                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase4);
00084                         break;
00085                 case 5:
00086                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase5);
00087                         break;
00088                 case 6:
00089                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase6);
00090                         break;
00091                 case 7:
00092                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase7);
00093                         break;
00094                 case 8:
00095                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase8);
00096                         break;
00097                 case 9:
00098                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase9);
00099                         break;
00100                 case 10:
00101                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase10);
00102                         break;
00103                 case 11:
00104                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase11);
00105                         break;
00106                 case 12:
00107                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase12);
00108                         break;
00109                 case 13:
00110                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase13);
00111                         break;
00112                 case 14:
00113                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase14);
00114                         break;
00115                 case 15:
00116                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase15);
00117                         break;
00118                 case 16:
00119                         {
00120                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase16);
00121                         }
00122                         break;
00123                 case 17:
00124                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase17);
00125                         break;
00126                 case 18:
00127                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase18);
00128                         break;
00129                 case 19:
00130                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase19);
00131                         break;
00132                 case 20:
00133                         {
00134                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase20);
00135                         }
00136                         break;
00137                 case 21:
00138                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase21);
00139                         break;
00140                 case 22:
00141                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase22);
00142                         break;
00143                 case 23:
00144                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase23);
00145                         break;
00146                 case 24:
00147                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase24);
00148                         break;
00149                 case 25:
00150                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase25);
00151                         break;
00152                 default:
00153                         iGraphObserver->NotifyStatus(KTxtUpdateModelDefault);
00154                         break;
00155                 }
00156         }
00157 
00158 
00159 // Text printed to the console in UpdateModeL()
00160 _LIT(KTxtDrawCase21,"White text left justified in dark gray box");
00161 _LIT(KTxtDrawCase22,"White text centered in black box");
00162 _LIT(KTxtDrawCase23,"Dark gray text right justified in lite gray box");
00163 _LIT(KTxtDrawCase25,"This text overwrites the cleared area");
00164 
00165         
00166 void CDrawControl::Draw(const TRect& /* aRect */) const
00167         {
00168         // put the next line back in to see the individual drawing actions
00169         // (iCoeEnv->WsSession()).SetAutoFlush(ETrue);
00170         // draw surrounding rectangle
00171         CWindowGc& gc=SystemGc(); // graphics context we draw to
00172         gc.UseFont(iMessageFont); // use the system message font
00173         gc.Clear();
00174         SystemGc().DrawRect(Rect()); // surrounding rectangle to draw into
00175         TRect rect=Rect(); // a centered rectangle of the default size
00176         TRect ellipseRect=Rect(); // for arcs and ellipse
00177         ellipseRect.Shrink(10,10); // set size so inside the border rectangle
00178         TRect box=Rect(); // a smaller centered rectangle, for text in a box
00179         box.Shrink(10,10); // set size of text box
00180         TRect tinyBox=Rect(); // a tiny box to clear
00181         tinyBox.Shrink(220,90); // set size of tiny box to clear
00182         TInt offset=0; // offset, for text in a box
00183         TPoint screenCenterPoint=rect.Center(); // the center of the screen 
00184         // set up a pair of construction points for arc and pie slice drawing
00185         // set up the size for half a screen (divided vertically)
00186         TPoint constructionPoint1(15,15); // outside the construction ellipse
00187         TPoint constructionPoint2(200,150); // inside the construction ellipse
00188         // set up a pair of points for drawing diagonal lines
00189         TPoint startPoint(50,50);
00190         TPoint endPoint(590,190);
00191         // set up an array of points for drawing a polyline and a polygon etc
00192         // will be used relative to top left of rectangle
00193         CArrayFix<TPoint>* mypoints=NULL;
00194         TRAPD(err,mypoints = CreatePointsArrayL());
00195         if(err)
00196                 {
00197                 return;
00198                 }
00199         // set up a black, a dark gray, a lite gray and a white RGB color
00200         TRgb black(0,0,0);
00201         TRgb darkGray(85,85,85);
00202         TRgb liteGray(170,170,170);
00203         TRgb white(255,255,255); // appears as blank screen gray-green color
00204         // Set up a "bold" size for the pen tip to (default is 1,1)
00205         TSize penSizeBold(3,3);
00206         // Set up a "fat" size for the pen tip
00207         TSize penSizeFat(30,30);
00208         // decide what to do, and do it
00209         switch (Phase())
00210                 {
00211                 case 0:
00212                         // draw a single pixel point in the center of the screen
00213                         // it is so small that some text is needed to explain the screen...
00214                         gc.Plot(screenCenterPoint);
00215                         break;
00216                 case 1:
00217                         // draw a "bold" point 3 pixels across
00218                         gc.SetPenSize(penSizeBold);
00219                         gc.Plot(screenCenterPoint);
00220                         break;
00221                 case 2:
00222                         // draw a "fat" point (circular blob),
00223                         // illustrating the effect of a very wide pen
00224                         gc.SetPenSize(penSizeFat);
00225                         gc.Plot(screenCenterPoint);
00226                         break;
00227                 case 3:
00228                         // draw a thin line fromtop left to bottom right
00229                         gc.DrawLine(startPoint,endPoint);
00230                         break;
00231                 case 4:
00232                         // draw a "bold" line fromtop left to bottom right
00233                         gc.SetPenSize(penSizeBold);
00234                         gc.DrawLine(startPoint,endPoint);
00235                         break;
00236                 case 5:
00237                         // draw a rather wide line from top left to bottom right,
00238                         // illustrating rounded ends and their clipping
00239                         gc.SetPenSize(penSizeFat);
00240                         gc.DrawLine(startPoint,endPoint);
00241                         break;
00242                 case 6:
00243                         // draw a dotted line from top left to bottom right
00244                         gc.SetPenStyle(CGraphicsContext::EDottedPen);
00245                         gc.DrawLine(startPoint,endPoint);
00246                         break;
00247                 case 7:
00248                         // draw a dot-dash line from top left to bottom right
00249                         gc.SetPenStyle(CGraphicsContext::EDotDashPen);
00250                         gc.DrawLine(startPoint,endPoint);
00251                         break;
00252                 case 8:
00253                         // draw a triangle by relative drawing
00254                         gc.MoveTo(TPoint(300,50)); // drawing position (300,50)
00255                         gc.DrawLineBy(TPoint(205,100)); // drawing position (505,150)
00256                         gc.DrawLineBy(TPoint(-410,0)); // drawing position (95,150)
00257                         gc.DrawLineBy(TPoint(205,-100)); // drawing position (300,50)
00258                         break;
00259                 case 9:
00260                         // draw a triangle, by relative drawing
00261                         // illustrating rounded ends at corners when using very wide lines
00262                         gc.SetPenSize(penSizeFat);
00263                         gc.MoveTo(TPoint(300,50)); // drawing position (300,50)
00264                         gc.DrawLineBy(TPoint(205,100)); // drawing position (505,150)
00265                         gc.DrawLineBy(TPoint(-410,0)); // drawing position (95,150)
00266                         gc.DrawLineBy(TPoint(205,-100)); // drawing position (300,50)
00267                         break;
00268                 case 10:
00269                         // draw a triangle by sequential drawing between specified points,
00270                         // using dot-dash line style, illustrating line pattern continuation 
00271                         gc.SetPenStyle(CGraphicsContext::EDotDashPen);
00272                         gc.MoveTo(TPoint(300,50)); // drawing position (300,50)
00273                         gc.DrawLineTo(TPoint(505,150)); // drawing position (505,150)
00274                         gc.DrawLineTo(TPoint(95,150)); // drawing position (95,150)
00275                         gc.DrawLineTo(TPoint(300,50)); // drawing position (300,50)
00276                         break;
00277                 case 11:
00278                         // draw an ellipse centered in the rectangle
00279                         gc.DrawEllipse(ellipseRect);
00280                         break;
00281                 case 12:
00282                         // draw an arc centered in the rectangle
00283                         gc.DrawArc(ellipseRect,constructionPoint1,constructionPoint2);
00284                         // gc.Clear();
00285                         break;
00286                 case 13:
00287                         // draw an arc centered in the rectangle that is the other
00288                         // portion of the ellipse (arguments reversed)
00289                         gc.DrawArc(ellipseRect,constructionPoint2,constructionPoint1);
00290                         // draw construction lines and points
00291                         gc.SetPenStyle(CGraphicsContext::EDottedPen);
00292                         gc.MoveTo(constructionPoint1);
00293                         gc.DrawLineTo(screenCenterPoint);
00294                         gc.DrawLineTo(constructionPoint2);
00295                         gc.SetPenSize(penSizeBold);
00296                         gc.Plot(constructionPoint1);
00297                         gc.Plot(constructionPoint2);
00298                         gc.Plot(screenCenterPoint);
00299                         break;
00300                 case 14:
00301                         // draw a pie slice centered in the rectangle
00302                         gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
00303                         gc.SetBrushColor(white);
00304                         gc.DrawPie(ellipseRect,constructionPoint1,constructionPoint2);
00305                         // draw the other portion of the elliptical disc
00306                         gc.SetBrushStyle(CGraphicsContext::EVerticalHatchBrush);
00307                         gc.DrawPie(ellipseRect,constructionPoint2,constructionPoint1);
00308                         break;
00309                 case 15:
00310                         {
00311                         // draw a rectangle with rounded corners, centered in the rectangle
00312                         TSize cornerSize(20,20); // size of a rounded corner
00313                         gc.DrawRoundRect(box,cornerSize); // same rect as text box
00314                         }
00315                         break;
00316                 case 16:
00317                         {
00318                         // draw a rectangle with rounded corners,
00319                         //centered in the rectangle, showing a corner ellipse
00320                         TSize cornerSize(20,20); // size of a rounded corner
00321                         // rect for corner ellipse is twice the corner size
00322                         TSize cornerEllipseSize(cornerSize.iHeight*2,cornerSize.iWidth*2);
00323                         TRect cornerRectTl(box.iTl,cornerEllipseSize);
00324                         gc.DrawRoundRect(box,cornerSize);
00325                         gc.SetPenStyle(CGraphicsContext::EDottedPen);
00326                         gc.DrawEllipse(cornerRectTl); // corner construction ellipse
00327                         }
00328                         break;
00329                 case 17:
00330                         // draw a polyline
00331                         gc.DrawPolyLine(mypoints);
00332                         break;
00333                 case 18:
00334                         // draw self-crossing polygon using the winding fill rule
00335                         gc.SetBrushStyle(CGraphicsContext::ESquareCrossHatchBrush);
00336                         gc.SetBrushColor(black);
00337                         gc.DrawPolygon(mypoints,CGraphicsContext::EWinding);
00338                         break;
00339                 case 19:
00340                         // draw self-crossing polygon using the alternate fill rule
00341                         gc.SetBrushStyle(CGraphicsContext::EDiamondCrossHatchBrush);
00342                         gc.SetBrushColor(black);
00343                         gc.DrawPolygon(mypoints,CGraphicsContext::EAlternate);
00344                         break;
00345                 case 20:
00346                         {
00347                         // draw self-crossing polygon using the alternate fill rule,
00348                         // and copy the lhs to the rhs of the screen
00349                         gc.SetBrushStyle(CGraphicsContext::EDiamondCrossHatchBrush);
00350                         gc.SetBrushColor(black);
00351                         gc.DrawPolygon(mypoints,CGraphicsContext::EAlternate);
00352                         TPoint screenOrigin(0,0); // top left of the screen
00353                         TSize halfScreenLR(320,240); // size of vertical half of screen
00354                         rect.SetRect(screenOrigin,halfScreenLR); // lhs of screen
00355                         TPoint offset(halfScreenLR.iWidth,0); // half screen width offset
00356                         gc.CopyRect(offset,rect); //  copy lhs of screen to rhs
00357                         }
00358                         break;
00359                 case 21:
00360                         // draw some text left justified in a box,
00361                         // offset so text is just inside top of box
00362                         {
00363                         CFont* font=iMessageFont; // get the system message font
00364                         TInt fontAscent(font->AscentInPixels()); // system message font ascent
00365                         offset=fontAscent+3; // add a 3 pixel text line spacing
00366                         TInt margin=2; // left margin is two pixels
00367                         gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
00368                         gc.SetBrushColor(darkGray);
00369                         gc.SetPenColor(white);
00370                         gc.UseFont(iMessageFont);
00371                         gc.DrawText(KTxtDrawCase21,box,offset,CGraphicsContext::ELeft,margin);
00372                         }
00373                         break;
00374                 case 22:
00375                         // draw some text centered in a box, (margin is zero)
00376                         {
00377                         TInt boxHeight=box.Height(); // get height of text box
00378                         CFont* font=iMessageFont; // get the system message font
00379                         TInt textHeight(font->HeightInPixels()); // system message font height
00380                         offset=(textHeight+boxHeight)/2; // 1/2 font ht below halfway down box
00381                         TInt margin=0; // margin is zero
00382                         gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
00383                         gc.SetBrushColor(black);
00384                         gc.SetPenColor(white);
00385                         gc.UseFont(iMessageFont);
00386                         gc.DrawText(KTxtDrawCase22,box,offset,CGraphicsContext::ECenter,margin);
00387                         }
00388                         break;
00389                 case 23:
00390                         // draw some text right justified in a box,
00391                         // offset so text is just inside bottom of box
00392                         {
00393                         TInt boxHeight=box.Height(); // get height of text box
00394                         CFont* font=iMessageFont; // get the system message font
00395                         TInt fontDescent=font->DescentInPixels(); // system message font descent
00396                         offset=boxHeight-fontDescent-3;// offset, 3 pixel text line spacing 
00397                         TInt margin=2; // right margin is two pixels
00398                         gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
00399                         gc.SetBrushColor(liteGray);
00400                         gc.SetPenColor(darkGray);
00401                         gc.UseFont(iMessageFont);
00402                         gc.DrawText(KTxtDrawCase23,box,offset,CGraphicsContext::ERight,margin);
00403                         }
00404                         break;
00405                 case 24:
00406                         {
00407                         // draw a cross-hatched box
00408                         // then clear a small central rectangle
00409                         gc.SetBrushColor(darkGray);
00410                         gc.SetBrushStyle(CGraphicsContext::ESquareCrossHatchBrush);
00411                         gc.DrawRect(rect);
00412                         // clear a small rectangle
00413                         gc.SetBrushColor(liteGray); // change the brush color
00414                         gc.Clear(tinyBox); // clear to brush color
00415                         }
00416                         break;
00417                 case 25:
00418                         {
00419                         // draw a cross-hatched box
00420                         // then clear a small central rectangle
00421                         // and write some text in it in smallest Swiss font,
00422                         // (which is actually a tiny "block" print-preview font)
00423                         // starting bottom left (illustrating flicker, overlap, mess)
00424                         gc.SetBrushColor(darkGray);
00425                         gc.SetBrushStyle(CGraphicsContext::ESquareCrossHatchBrush);
00426                         gc.DrawRect(rect);
00427                         // clear a small rectangle
00428                         gc.SetBrushColor(liteGray); // change the brush color
00429                         gc.Clear(tinyBox); // clear to brush color
00430                         // get an alternative font
00431                         CFont* myFont;
00432                         _LIT(KTxtArial,"Arial");
00433                         TFontSpec myFontSpec(KTxtArial,1); // to get smallest Arial font
00434                         CGraphicsDevice* screenDevice=(iCoeEnv->ScreenDevice());
00435                         screenDevice->GetNearestFontInTwips(myFont,myFontSpec);
00436                         gc.UseFont(myFont);
00437                         // set the text drawing position & draw (demonstrating flicker)
00438                         TInt fontDescent=myFont->DescentInPixels();
00439                         TPoint pos(0,tinyBox.Height()-fontDescent);
00440                         pos+=tinyBox.iTl;
00441                         gc.DrawText(KTxtDrawCase25,pos);
00442                         // discard and destroy the font
00443                         gc.DiscardFont();
00444                         screenDevice->ReleaseFont(myFont);
00445                         }
00446                         break;
00447                 default:
00448                         break;
00449                 }
00450         delete mypoints; // an array must be destroyed after use
00451         }
00452 
00453 static CArrayFix<TPoint>* CreatePointsArrayL()
00454         {
00455         CArrayFixFlat<TPoint>* pointsArray = new CArrayFixFlat<TPoint>(5);
00456         
00457         TPoint point1(20,20);
00458         TPoint point2(100,190);
00459         TPoint point3(110,90);
00460         TPoint point4(50,150);
00461         TPoint point5(200,150);
00462         
00463         pointsArray->AppendL(point1);
00464         pointsArray->AppendL(point2);
00465         pointsArray->AppendL(point3);
00466         pointsArray->AppendL(point4);
00467         pointsArray->AppendL(point5);
00468         
00469         return pointsArray;
00470         }

Generated by  doxygen 1.6.2