Symbian3/SDK/Source/GUID-BE871265-147B-45F3-8772-A4E091223EDB.dita
author Dominic Pinkman <Dominic.Pinkman@Nokia.com>
Thu, 21 Jan 2010 18:18:20 +0000
changeset 0 89d6a7a84779
permissions -rw-r--r--
Initial contribution of Documentation_content according to Feature bug 1266 bug 1268 bug 1269 bug 1270 bug 1372 bug 1374 bug 1375 bug 1379 bug 1380 bug 1381 bug 1382 bug 1383 bug 1385

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License 
"Eclipse Public License v1.0" which accompanies this distribution, 
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
    Nokia Corporation - initial contribution.
Contributors: 
-->
<!DOCTYPE concept
  PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="GUID-BE871265-147B-45F3-8772-A4E091223EDB" xml:lang="en"><title>Constructing
views in traditional architecture</title><prolog><metadata><keywords/></metadata></prolog><conbody>
<p>The call on the first phase constructor method of the view occurs in <xref href="GUID-FD2CDEB8-0784-4BE5-A775-170F57D71BBC.dita">UI controller</xref>. The
view serves as the top-level window under the UI controller.</p>
<p>The methods you need to implement for your <xref href="jar:GUID-35228542-8C95-4849-A73F-2B4F082F0C44.jar!/sdk/doc_source/reference/reference-cpp/Control_Environment/CCoeControlClass.html" format="application/java-archive"><parmname>CCoeControl</parmname></xref>-derived
view are as follows:</p>
<ul>
<li><p>C++ default constructor, which cannot contain code that leaves.
A common implementation is:</p>
<itemgroup>
<codeblock id="GUID-83E49AC1-37CD-43B0-AFEA-321BF8F62A92" xml:space="preserve">
CMyAppView::CMyAppView()
    {
    // No implementation required
    }</codeblock>
<p><draft-comment time="2007-03-06T13:00">Comment to reviewers: We will
link these Cleanup stack and new (ELeave) methods to the Symbian Developer
Library API Reference.</draft-comment></p>
</itemgroup>
</li>
<li><p>two-phase constructor, a common implementation is:</p>
<itemgroup>
<codeblock id="GUID-D1B0073F-A8C6-4F6B-8F31-9F9DA4F164D9" xml:space="preserve">CMyAppView* CMyAppView::NewL( const TRect&amp; aRect )
    {
    CMyAppView* self = CMyAppView::NewLC( aRect );
    CleanupStack::Pop( self );
    return self;
    }

CMyAppView* CMyAppView::NewLC( const TRect&amp; aRect )
    {
    CMyAppView* self = new ( ELeave ) CMyAppView;
    CleanupStack::PushL( self );
    self-&gt;ConstructL( aRect );
    return self;
    }
</codeblock>
<p>The declarations for <parmname>CMyAppView::NewL</parmname> and <parmname>CMyAppView::NewLC</parmname> in
the class header file needs to be public to support the construction method
required. <parmname>CMyAppView</parmname> is the default constructor for the <parmname>CMyAppView</parmname> class
and it is private.</p>
<p>In this approach, <parmname>CMyAppView::NewL</parmname> is called from
the UI controller. It creates a view object by calling <parmname>CMyAppView::NewLC</parmname>. <parmname>CMyAppView::NewLC</parmname> calls
new (<parmname>ELeave</parmname>) on the C++ default constructor <parmname>CMyAppView</parmname> to
create the object (and leave if it cannot), <xref href="jar:GUID-35228542-8C95-4849-A73F-2B4F082F0C44.jar!/sdk/doc_source/reference/reference-cpp/Kernel_Architecture_2/CCleanupClass.html#%3a%3aCCleanup%3a%3aPushL%28CBase%20%2a%29" format="application/java-archive">pushes</xref> a pointer to the clean-up stack in case the second phase construction method
leaves, and then calls the second phase construction method of the object.
When it returns to <parmname>CMyAppView::NewL</parmname>, the pointer pushed
to the cleanup stack is <xref href="jar:GUID-35228542-8C95-4849-A73F-2B4F082F0C44.jar!/sdk/doc_source/reference/reference-cpp/Kernel_Architecture_2/CCleanupClass.html#%3a%3aCCleanup%3a%3aPop%28%29" format="application/java-archive">removed</xref>.</p>
</itemgroup>
</li>
<li><p>Symbian 2nd phase constructor with code that might leave.
A common implementation is:</p>
<itemgroup>
<codeblock id="GUID-34901188-B037-4862-9DA4-80D9EDBDEC5F" xml:space="preserve">void CMyAppView::ConstructL( const TRect&amp; aRect )
    {
    // Create a window for this application view
    CreateWindowL();

     //add construction for other controls if required

	   // Set the windows size
    SetRect( aRect );

    // Activate the window, which makes it ready to be drawn
    ActivateL();
    }</codeblock>
<p><parmname>CMyAppView::ConstructL</parmname> is a private class providing
the second phase construction that accepts the rectangle the view is drawn
to.</p>
<p><xref href="jar:GUID-35228542-8C95-4849-A73F-2B4F082F0C44.jar!/sdk/doc_source/reference/reference-cpp/Control_Environment/CCoeControlClass.html#%3a%3aCCoeControl%3a%3aCreateWindowL%28%29" format="application/java-archive"><parmname>CCoeControl::CreateWindowL</parmname></xref> creates a window
for the control. Note that this window is a child of the UI controller. This
method makes the control a <xref href="GUID-352850A9-227F-45DB-8DCD-C6268954B4ED.dita">window-owning
control</xref>. While the use of window-owning controls is generally discouraged
to prevent the taxing of run-time resources, this is the top-level window
for the UI controller.</p>
<p>This is a simple control that does not contain other controls; other
controls could be added to the control between <xref href="jar:GUID-35228542-8C95-4849-A73F-2B4F082F0C44.jar!/sdk/doc_source/reference/reference-cpp/Control_Environment/CCoeControlClass.html#%3a%3aCCoeControl%3a%3aCreateWindowL%28%29" format="application/java-archive"><parmname>CCoeControl::CreateWindowL</parmname></xref> and <xref href="jar:GUID-35228542-8C95-4849-A73F-2B4F082F0C44.jar!/sdk/doc_source/reference/reference-cpp/Control_Environment/CCoeControlClass.html#%3a%3aCCoeControl%3a%3aSetRect%28const%20TRect%20%26amp%3b%29" format="application/java-archive"><parmname>CCoeControl::SetRect(aRect)</parmname></xref>. For more information,
see <xref href="GUID-97486385-88F7-4AF1-B880-D9A8AA4D7094.dita">Compound
controls in traditional architecture</xref>.</p>
<p><xref href="jar:GUID-35228542-8C95-4849-A73F-2B4F082F0C44.jar!/sdk/doc_source/reference/reference-cpp/Control_Environment/CCoeControlClass.html#%3a%3aCCoeControl%3a%3aSetRect%28const%20TRect%20%26amp%3b%29" format="application/java-archive"><parmname>CCoeControl::SetRect(aRect)</parmname></xref> sets the window
size according to the requirements of the mobile device. The top-level control
rectangle is set to the area that the framework provides for the application.
Calling <xref href="jar:GUID-35228542-8C95-4849-A73F-2B4F082F0C44.jar!/sdk/doc_source/reference/reference-cpp/Control_Environment/CCoeControlClass.html#%3a%3aCCoeControl%3a%3aSetRect%28const%20TRect%20%26amp%3b%29" format="application/java-archive"><parmname>CCoeControl::SetRect(aRect)</parmname></xref> calls the <xref href="jar:GUID-35228542-8C95-4849-A73F-2B4F082F0C44.jar!/sdk/doc_source/reference/reference-cpp/Control_Environment/CCoeControlClass.html#%3a%3aCCoeControl%3a%3aSizeChanged%28%29" format="application/java-archive"><parmname>CCoeControl::SizeChanged</parmname></xref> method, where
the control should set the position and size for any child controls and thus
adjust the control layout to the UI.</p>
<p><xref href="jar:GUID-35228542-8C95-4849-A73F-2B4F082F0C44.jar!/sdk/doc_source/reference/reference-cpp/Control_Environment/CCoeControlClass.html#%3a%3aCCoeControl%3a%3aActivateL%28%29" format="application/java-archive"><parmname>CCoeControl::ActivateL</parmname></xref> sets the control
as ready to be drawn.</p>
</itemgroup>
</li>
</ul>
<p>If required for your application, you may need to implement other methods
for your control. For top-level windows, you would need to implement <xref href="jar:GUID-35228542-8C95-4849-A73F-2B4F082F0C44.jar!/sdk/doc_source/reference/reference-cpp/Control_Environment/CCoeControlClass.html#%3a%3aCCoeControl%3a%3aSizeChanged%28%29" format="application/java-archive"><parmname>CCoeControl::SizeChanged</parmname></xref>  to respond to
changes to the size and position of the contents of this control. This is
called by the platform when a change occurs. A typical implementation for
a compound control is:</p>
<codeblock id="GUID-A100010E-DF65-4EB7-AC5D-707604805976" xml:space="preserve">void CMyAppView::SizeChanged()
    {
    // Control resize code
    iControl-&gt;SetExtent( const TPoint &amp;aPosition, const TSize &amp;aSize);
    }</codeblock>
</conbody></concept>