Constructing views in traditional 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 methods you need to implement for your CCoeControl-derived view are as follows:

  • C++ default constructor, which cannot contain code that leaves. A common implementation is:

    CMyAppView::CMyAppView()
        {
        // No implementation required
        }

  • two-phase constructor, 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;
        }
    

    The declarations for CMyAppView::NewL and CMyAppView::NewLC in the class header file needs to be public to support the construction method required. CMyAppView is the default constructor for the CMyAppView class and it is private.

    In this approach, CMyAppView::NewL is called from the UI controller. It creates a view object by calling CMyAppView::NewLC. CMyAppView::NewLC calls new (ELeave) on the C++ default constructor CMyAppView 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 removedremoved.

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

    void CMyAppView::ConstructL( const TRect& 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();
        }

    CMyAppView::ConstructL 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 generally discouraged to prevent the taxing of run-time resources, this is the top-level window for the UI controller.

    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 traditional 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 SetRect calls the 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 S60 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);
    }