Symbian3/SDK/Source/GUID-DC59BEAD-0047-4D7A-96D1-C5E3EC4F982B.dita
changeset 7 51a74ef9ed63
child 8 ae94777fff8f
equal deleted inserted replaced
6:43e37759235e 7:51a74ef9ed63
       
     1 <?xml version="1.0" encoding="utf-8"?>
       
     2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
       
     3 <!-- This component and the accompanying materials are made available under the terms of the License 
       
     4 "Eclipse Public License v1.0" which accompanies this distribution, 
       
     5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
       
     6 <!-- Initial Contributors:
       
     7     Nokia Corporation - initial contribution.
       
     8 Contributors: 
       
     9 -->
       
    10 <!DOCTYPE concept
       
    11   PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
       
    12 <concept id="GUID-DC59BEAD-0047-4D7A-96D1-C5E3EC4F982B" xml:lang="en"><title>Drawing
       
    13 in traditional architecture</title><prolog><metadata><keywords/></metadata></prolog><conbody>
       
    14 <p>The application framework calls<xref href="GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160.dita#GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160/GUID-63295719-90A0-3D53-A643-0C52BF4068A1"><apiname>CCoeControl::Draw()</apiname></xref> when
       
    15 a control area needs to be updated on the display. Controls may implement <xref href="GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160.dita#GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160/GUID-63295719-90A0-3D53-A643-0C52BF4068A1"><apiname>CCoeControl::Draw()</apiname></xref> or
       
    16 leave the drawing to their child controls. For more information on control
       
    17 hierarchies, see <xref href="GUID-E244744F-4837-5B46-8E37-4666A28BF0B7-GENID-1-8-1-3-1-1-7-1-7-1-5-1.dita">The
       
    18 run-time control hierarchy</xref>. The platform calls <xref href="GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160.dita#GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160/GUID-63295719-90A0-3D53-A643-0C52BF4068A1"><apiname>CCoeControl::Draw()</apiname></xref> for
       
    19 the parent control first, and then recursively for each control.</p>
       
    20 <p>Controls should override <xref href="GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160.dita#GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160/GUID-63295719-90A0-3D53-A643-0C52BF4068A1"><apiname>CCoeControl::Draw()</apiname></xref> to draw
       
    21 their content. The override should do nothing else and should be as fast as
       
    22 possible. For example, it is bad design to create fonts dynamically, and read
       
    23 any bitmaps or resources while drawing. A good rule of thumb is that there
       
    24 should not be trap handlers in the method override; any time-consuming functionality
       
    25 that can be done beforehand should be cached.</p>
       
    26 <p>In most cases controls are drawn on the display using the screen device
       
    27 graphics context, accessed with <xref href="GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160.dita#GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160/GUID-6586DB74-AF5C-330B-9D0A-E4637396A04E"><apiname>CCoeControl::SystemGc()</apiname></xref>.
       
    28 The graphics context provides a wide set of <xref href="GUID-E89F034F-C807-5FF9-B06B-F7CCD2441041.dita">GDI</xref> (Graphics
       
    29 Device Interface  - common Symbian platform graphics API) drawing primitives
       
    30 that can be used for drawing virtually anything on screen.</p>
       
    31 <p>An example of a basic override of <xref href="GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160.dita#GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160/GUID-63295719-90A0-3D53-A643-0C52BF4068A1"><apiname>CCoeControl::Draw()</apiname></xref> for
       
    32 a control that is a top-level window in an application is as follows:</p>
       
    33 <codeblock id="GUID-27363E40-E10A-4399-BF1A-A48305A1DC1B" xml:space="preserve">void CMyAppView::Draw( const TRect&amp; /*aRect*/ ) const
       
    34     {
       
    35     // Get the standard graphics context
       
    36     CWindowGc&amp; gc = SystemGc();
       
    37     gc.SetPenStyle( CGraphicsContext::ENullPen );
       
    38     gc.SetBrushColor( KRgbWhite);
       
    39     gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
       
    40 
       
    41     // Gets the control's extent
       
    42     TRect rect( Rect());
       
    43 
       
    44         {
       
    45         gc.Clear( rect );
       
    46         }
       
    47     }
       
    48 </codeblock>
       
    49 <p>, where</p>
       
    50 <ul>
       
    51 <li><p><parmname>CWindowGc&amp; gc = SystemGc();</parmname> gets
       
    52 the graphics context that is used when drawing the control.</p></li>
       
    53 <li><p><xref href="GUID-0AEE5955-C530-35F1-A904-69183331B294.dita#GUID-0AEE5955-C530-35F1-A904-69183331B294/GUID-026A1015-0D79-3A66-91BA-5FB343387EE0"><apiname>CWindowGc::SetPenStyle()</apiname></xref>, <xref href="GUID-0AEE5955-C530-35F1-A904-69183331B294.dita#GUID-0AEE5955-C530-35F1-A904-69183331B294/GUID-0A923CAA-7A89-3ED2-A844-2F4147B62FEC"><apiname>CWindowGc::SetBrushColor()</apiname></xref>,
       
    54 and <xref href="GUID-0AEE5955-C530-35F1-A904-69183331B294.dita#GUID-0AEE5955-C530-35F1-A904-69183331B294/GUID-363A9FB3-75F7-3EB7-A783-91BBAEBCE670"><apiname>CWindowGc::SetBrushStyle()</apiname></xref> are used to set the drawing
       
    55 primatives for the context</p></li>
       
    56 <li><p><xref href="GUID-101762DC-E498-3325-88AB-B0FF17DC62B6.dita"><apiname>TRect</apiname></xref> gets the size of the control rectangle</p>
       
    57 </li>
       
    58 <li><p><xref href="GUID-D22FD07E-59E0-346A-9BFA-8D109F509DB1.dita"><apiname>CWindowGc:Clear(rect)</apiname></xref> clears the control
       
    59 rectangle</p></li>
       
    60 </ul>
       
    61 <section id="GUID-F52B945E-F63A-40FC-B7EF-CBA4CFD415E8"><title>Double buffering</title>
       
    62 <p>For controls that perform intensive drawing operations, the drawing
       
    63 should be cached: a process also known as double-buffering. Here the drawing
       
    64 is done to a memory context first and then in the <xref href="GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160.dita#GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160/GUID-63295719-90A0-3D53-A643-0C52BF4068A1"><apiname>CCoeControl::Draw()</apiname></xref> method
       
    65 only the context's bitmap is passed to screen. In the Symbian platform, the
       
    66 easiest way to implement a double buffer is to create a <xref href="GUID-683A1D42-2764-3EB7-BD19-9E12559199AB.dita"><apiname>CFbsBitmap</apiname></xref> and
       
    67 then bind a graphics context to that  - this makes it possible to use the
       
    68 same GDI interface for drawing as the display context. The drawing is done
       
    69 to a memory bitmap buffer. Double-buffering is a common paradigm used in games,
       
    70 but can also be utilized in any application when performance of drawing controls
       
    71 is important.</p>
       
    72 <p>The following is a short example of how a double buffer is created and
       
    73 used:</p>
       
    74 <codeblock id="GUID-2FA4ADA3-E8F5-4BEE-86C4-40DE748BB91B" xml:space="preserve">iGcBmp = new (ELeave) CWsBitmap(iEikonEnv-&gt;WsSession());
       
    75 User::LeaveIfError(iGcBmp-&gt;Create(aClientRect.Size(), iEikonEnv-&gt;ScreenDevice()-&gt;DisplayMode()));
       
    76 iGcDevice = CFbsBitmapDevice::NewL(iGcBmp);
       
    77 User::LeaveIfError(iGcDevice-&gt;CreateBitmapContext(iGc));
       
    78 </codeblock>
       
    79 <p><parmname>iGcBmp</parmname> is a pointer to <xref href="GUID-17150D76-BB82-3A4B-8B1A-8BA93CB1A9EF.dita"><apiname>CWsBitmap</apiname></xref>,
       
    80 the bitmap memory buffer, that is created with the same width and height as
       
    81 the top-level window and with the same color bit depth as the display.<parmname> iGcDevice</parmname> is
       
    82 a pointer to the <xref href="GUID-2DEFEC47-F36E-3133-A08D-55F7C2534CC0.dita"><apiname>CBitmapDevice</apiname></xref> device class and the context <parmname>iGc</parmname> holds
       
    83 the <xref href="GUID-FC746873-0570-3900-AD89-42B205FDC0D3.dita"><apiname>CBitmapContext</apiname></xref> instance. <parmname>iGc</parmname> is
       
    84 then used instead of <parmname>CScreenGc</parmname>, obtained from the <xref href="GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160.dita#GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160/GUID-6586DB74-AF5C-330B-9D0A-E4637396A04E"><apiname>CCoeControl::SystemGc()</apiname></xref> method,
       
    85 when the control draws itself. The double-buffer drawing should be done outside
       
    86 of the <xref href="GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160.dita#GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160/GUID-63295719-90A0-3D53-A643-0C52BF4068A1"><apiname>CCoeControl::Draw()</apiname></xref> method and can be called directly
       
    87 when needed. Only at the end of the off-screen drawing is the memory buffer
       
    88 flushed to the screen by calling <xref href="GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160.dita#GUID-B06F99BD-F032-3B87-AB26-5DD6EBE8C160/GUID-9FB682AC-0209-302A-83F3-7BCB1162B998"><apiname>CCoeControl::DrawDeferred()</apiname></xref></p>
       
    89 <codeblock id="GUID-9474C692-0E2E-4B2C-A218-C4DF99E85C31" xml:space="preserve">void CMyDrawingExample::Draw(const TRect&amp; /*aRect*/) const
       
    90     {
       
    91     SystemGc().BitBlt(TPoint(0, 0), iGcBmp);
       
    92     }</codeblock>
       
    93 </section>
       
    94 </conbody></concept>