Constructing views in the view architecture

The call on the first phase constructor method of the view occurs in UI controller. The view serves as the top-level window under the UI controller.

The following are the methods that you need to derive for the view derived from CCoeControl :

  • Default constructor, which cannot contain code that leaves. A common implementation is:

    CMyAppView* CMyAppView::NewL( const TRect& aRect )
        CMyAppView* self = CMyAppView::NewLC( aRect );
        CleanupStack::Pop( self );
        return self;
    CMyAppView* CMyAppView::NewLC( const TRect& aRect )
        CMyAppView* self = new ( ELeave ) CMyAppView;
        CleanupStack::PushL( self );
        self->ConstructL( aRect );
        return self;
        // No implementation required

    The declarations for CMyAppView::NewL() and CMyAppView::NewLC() in the class header file needs to be public to support the construction method required.

    In this approach, CMyAppView::NewL() is called from the UI controller. It creates a view object by calling CMyAppView::NewLC() to create the object (and leave if it cannot), pushes 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 CMyAppView::NewL(), the pointer pushed to the cleanup stack is removed.

  • Symbian second phase constructor with code that might leave. A common implementation is:

    void CMyAppView::ConstructL( const TRect& aRect )
        // Create a window for this application view
         //add construction for other controls if required
    	   // Set the windows size
        SetRect( aRect );
        // Activate the window, which makes it ready to be drawn

    CMyAppView is a private class providing the second phase construction that accepts the rectangle the view is drawn to.

    CCoeControl::CreateWindowL() creates a window for the control. Note that this window is a child of the UI controller. This method makes the control a window-owning control. While, the use of window-owning controls is discouraged to prevent the taxing of run-time resources, this is the top-level window for the UI controller.

    While this is a simple control that does not contain other controls, other controls could be added to the control between CCoeControl::CreateWindowL() and CCoeControl::SetRect(aRect). For more information, see Compound controls in the view architecture.

    CCoeControl::SetRect(aRect) 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 CCoeControl::SetRect(aRect) calls the CCoeControl::SizeChanged() method, where the control should set the position and size for any child controls and thus adjust the control layout to the UI.

    CCoeControl::ActivateL() sets the control as ready to be drawn.

If required for your application, you may need to implement other methods for your control. For top-level windows, you would need to implement CCoeControl::SizeChanged() to respond to changes to the size and position of the contents of this control. This is called by the Symbian platform when a change occurs. A typical implementation for a compound control is:

void CMyAppView::SizeChanged()
    // Control resize code
    iControl->SetExtent( const TPoint &aPosition, const TSize &aSize);