examples/ForumNokia/DescriptorExample/src/DescriptorExContainer.cpp

00001 /*
00002  * Copyright © 2008 Nokia Corporation.
00003  */
00004 
00005 // INCLUDE FILES
00006 #include <coemain.h>
00007 #include <eikenv.h>
00008 #include <gdi.h>
00009 #include <txtrich.h>
00010 #include <aknsbasicbackgroundcontrolcontext.h>
00011 
00012 #include "DescriptorExContainer.h"
00013 
00014 
00015 // -----------------------------------------------------------------------------
00016 // Two phase constructor. Constructs container containing one non editable
00017 // editor component.
00018 // -----------------------------------------------------------------------------
00019 void CDescriptorExContainer::ConstructL(const TRect& aRect)
00020     {
00021     CreateWindowL();
00022 
00023     // Create editable editor control
00024     iTextBox = new (ELeave) CEikEdwin();
00025     iTextBox->SetContainerWindowL( *this );
00026     iTextBox->ConstructL( CEikEdwin::ENoAutoSelection |
00027                           CEikEdwin::EDisplayOnly |
00028                           CEikEdwin::EReadOnly |
00029                           CEikEdwin::EAvkonEditor |
00030                           CEikEdwin::EInclusiveSizeFixed);
00031     iTextBox->CreateScrollBarFrameL()->SetScrollBarVisibilityL(
00032             CEikScrollBarFrame::EOff, CEikScrollBarFrame::EOn);
00033     
00034     iTextBox->UpdateScrollBarsL();
00035     
00036     // http://wiki.forum.nokia.com/index.php/TSS000578_-_How_to_ensure_that_the_scroll_bar_background_is_drawn_properly%3F
00037     // Skin support
00038     iSkinContext = CAknsBasicBackgroundControlContext::NewL(KAknsIIDQsnBgAreaMainAppsGrid,aRect,EFalse);         
00039     
00040     // Activate the window, which makes it ready to be drawn
00041     ActivateL();
00042 
00043     // Set the windows size
00044     // NOTE: To call this after ActivateL() is solution to get
00045     // scrollbarframe width in SizeChanged() method.
00046     SetRect(aRect);
00047     }
00048 
00049 // -----------------------------------------------------------------------------
00050 // Destroys all resources owned by this object.
00051 // -----------------------------------------------------------------------------
00052 CDescriptorExContainer::~CDescriptorExContainer()
00053     {
00054     delete iTextBox;
00055     delete iSkinContext;
00056     }
00057 
00058 // -----------------------------------------------------------------------------
00059 // Sets the data shown in editor and sets the focus on editor.
00060 // -----------------------------------------------------------------------------
00061 void CDescriptorExContainer::SetTextL(const TDesC& aText)
00062 {
00063     iTextBox->SetTextL( &aText );
00064     iTextBox->SetFocus( ETrue );
00065     
00066     iTextBox->HandleTextChangedL();
00067     iTextBox->UpdateScrollBarsL();
00068     
00069 }
00070 
00071 // -----------------------------------------------------------------------------
00072 // Return number of objects in this container
00073 // -----------------------------------------------------------------------------
00074 TInt CDescriptorExContainer::CountComponentControls() const
00075     {
00076     return 1; // return nbr of controls inside this container
00077     }
00078 
00079 // -----------------------------------------------------------------------------
00080 // Handle up and down keys to scroll the edwin control.
00081 // 
00082 // For some odd reason the edwin control doesn't process keyevent expect in
00083 // case that cursor is in the beginning or end of the document.
00084 // -----------------------------------------------------------------------------
00085 TKeyResponse CDescriptorExContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,
00086                                                     TEventCode aType)
00087 {
00088     TKeyResponse result = EKeyWasNotConsumed;
00089     switch(aKeyEvent.iScanCode)
00090     {
00091     case EStdKeyRightArrow:
00092     case EStdKeyDownArrow:
00093         {
00094             result = EKeyWasConsumed;
00095             if( aType == EEventKeyDown )
00096             {
00097                 iTextBox->MoveCursorL( TCursorPosition::EFLineDown, EFalse );
00098             }
00099             break;
00100         }
00101 
00102     case EStdKeyLeftArrow:
00103     case EStdKeyUpArrow:
00104         {
00105             result = EKeyWasConsumed;
00106             if( aType == EEventKeyDown )
00107             {
00108                 iTextBox->MoveCursorL( TCursorPosition::EFLineUp, EFalse );
00109             }
00110             break;
00111         }
00112     }
00113     return result;
00114 }
00115 
00116 // -----------------------------------------------------------------------------
00117 // Return component at requested index.
00118 // -----------------------------------------------------------------------------
00119 CCoeControl* CDescriptorExContainer::ComponentControl(TInt aIndex) const
00120     {
00121     switch ( aIndex )
00122         {
00123         case 0:
00124             return iTextBox;
00125         default:
00126             return NULL;
00127         }
00128     }
00129 
00130 TInt CDescriptorExContainer::GetScrollbarWidth() const
00131     {
00132     TInt scrollbarWidth = iTextBox->ScrollBarFrame()->
00133         ScrollBarBreadth(CEikScrollBar::EVertical);
00134     // If scrollbars are not drawn yet, the width remains zero. In this
00135     // case, an intentionally magical number is returned.
00136     if (scrollbarWidth == 0)
00137         {
00138         scrollbarWidth = 8;
00139         }
00140     
00141     return scrollbarWidth;
00142     }
00143 
00144 
00145 void CDescriptorExContainer::SizeChanged()
00146     {
00147     TRect rect = Rect();
00148     TInt scrollbarWidth = GetScrollbarWidth();
00149     iTextBox->SetExtent(TPoint(0, 0),
00150             TSize(rect.Width() - scrollbarWidth, rect.Height()));
00151 
00152     iTextBox->UpdateScrollBarsL();
00153     
00154     // Update new size for the skin
00155     iSkinContext->SetRect(rect);
00156     }
00157 
00158 
00159 TTypeUid::Ptr CDescriptorExContainer::MopSupplyObject(TTypeUid aId)
00160     {
00161     // That is for the skin support
00162     if( aId.iUid == MAknsControlContext::ETypeId && iSkinContext != NULL )
00163         {
00164         return MAknsControlContext::SupplyMopObject( aId, iSkinContext );
00165         }
00166     return CCoeControl::MopSupplyObject( aId );    
00167     }
00168 

Generated by  doxygen 1.6.2