examples/ForumNokia/DescriptorExample/src/DescriptorExContainer.cpp

00001 /*
00002  * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003  *    
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *    
00007  *  * Redistributions of source code must retain the above copyright notice, this
00008  *    list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright notice,
00010  *    this list of conditions and the following disclaimer in the documentation
00011  *    and/or other materials provided with the distribution.
00012  *  * Neither the name of Nokia Corporation nor the names of its contributors
00013  *    may be used to endorse or promote products derived from this software
00014  *    without specific prior written permission.
00015  *    
00016  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  *    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  *    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  *    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023  *    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024  *    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *    
00027  *    Description:  
00028  */
00029 
00030 
00031 // INCLUDE FILES
00032 #include <coemain.h>
00033 #include <eikenv.h>
00034 #include <gdi.h>
00035 #include <txtrich.h>
00036 #include <aknsbasicbackgroundcontrolcontext.h>
00037 
00038 #include "DescriptorExContainer.h"
00039 
00040 
00041 // -----------------------------------------------------------------------------
00042 // Two phase constructor. Constructs container containing one non editable
00043 // editor component.
00044 // -----------------------------------------------------------------------------
00045 void CDescriptorExContainer::ConstructL(const TRect& aRect)
00046     {
00047     CreateWindowL();
00048 
00049     // Create editable editor control
00050     iTextBox = new (ELeave) CEikEdwin();
00051     iTextBox->SetContainerWindowL( *this );
00052     iTextBox->ConstructL( CEikEdwin::ENoAutoSelection |
00053                           CEikEdwin::EDisplayOnly |
00054                           CEikEdwin::EReadOnly |
00055                           CEikEdwin::EAvkonEditor |
00056                           CEikEdwin::EInclusiveSizeFixed);
00057     iTextBox->CreateScrollBarFrameL()->SetScrollBarVisibilityL(
00058             CEikScrollBarFrame::EOff, CEikScrollBarFrame::EOn);
00059     
00060     iTextBox->UpdateScrollBarsL();
00061     
00062     // http://wiki.forum.nokia.com/index.php/TSS000578_-_How_to_ensure_that_the_scroll_bar_background_is_drawn_properly%3F
00063     // Skin support
00064     iSkinContext = CAknsBasicBackgroundControlContext::NewL(KAknsIIDQsnBgAreaMainAppsGrid,aRect,EFalse);         
00065     
00066     // Activate the window, which makes it ready to be drawn
00067     ActivateL();
00068 
00069     // Set the windows size
00070     // NOTE: To call this after ActivateL() is solution to get
00071     // scrollbarframe width in SizeChanged() method.
00072     SetRect(aRect);
00073     }
00074 
00075 // -----------------------------------------------------------------------------
00076 // Destroys all resources owned by this object.
00077 // -----------------------------------------------------------------------------
00078 CDescriptorExContainer::~CDescriptorExContainer()
00079     {
00080     delete iTextBox;
00081     delete iSkinContext;
00082     }
00083 
00084 // -----------------------------------------------------------------------------
00085 // Sets the data shown in editor and sets the focus on editor.
00086 // -----------------------------------------------------------------------------
00087 void CDescriptorExContainer::SetTextL(const TDesC& aText)
00088 {
00089     iTextBox->SetTextL( &aText );
00090     iTextBox->SetFocus( ETrue );
00091     
00092     iTextBox->HandleTextChangedL();
00093     iTextBox->UpdateScrollBarsL();
00094     
00095 }
00096 
00097 // -----------------------------------------------------------------------------
00098 // Return number of objects in this container
00099 // -----------------------------------------------------------------------------
00100 TInt CDescriptorExContainer::CountComponentControls() const
00101     {
00102     return 1; // return nbr of controls inside this container
00103     }
00104 
00105 // -----------------------------------------------------------------------------
00106 // Handle up and down keys to scroll the edwin control.
00107 // 
00108 // For some odd reason the edwin control doesn't process keyevent expect in
00109 // case that cursor is in the beginning or end of the document.
00110 // -----------------------------------------------------------------------------
00111 TKeyResponse CDescriptorExContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,
00112                                                     TEventCode aType)
00113 {
00114     TKeyResponse result = EKeyWasNotConsumed;
00115     switch(aKeyEvent.iScanCode)
00116     {
00117     case EStdKeyRightArrow:
00118     case EStdKeyDownArrow:
00119         {
00120             result = EKeyWasConsumed;
00121             if( aType == EEventKeyDown )
00122             {
00123                 iTextBox->MoveCursorL( TCursorPosition::EFLineDown, EFalse );
00124             }
00125             break;
00126         }
00127 
00128     case EStdKeyLeftArrow:
00129     case EStdKeyUpArrow:
00130         {
00131             result = EKeyWasConsumed;
00132             if( aType == EEventKeyDown )
00133             {
00134                 iTextBox->MoveCursorL( TCursorPosition::EFLineUp, EFalse );
00135             }
00136             break;
00137         }
00138     }
00139     return result;
00140 }
00141 
00142 // -----------------------------------------------------------------------------
00143 // Return component at requested index.
00144 // -----------------------------------------------------------------------------
00145 CCoeControl* CDescriptorExContainer::ComponentControl(TInt aIndex) const
00146     {
00147     switch ( aIndex )
00148         {
00149         case 0:
00150             return iTextBox;
00151         default:
00152             return NULL;
00153         }
00154     }
00155 
00156 TInt CDescriptorExContainer::GetScrollbarWidth() const
00157     {
00158     TInt scrollbarWidth = iTextBox->ScrollBarFrame()->
00159         ScrollBarBreadth(CEikScrollBar::EVertical);
00160     // If scrollbars are not drawn yet, the width remains zero. In this
00161     // case, an intentionally magical number is returned.
00162     if (scrollbarWidth == 0)
00163         {
00164         scrollbarWidth = 8;
00165         }
00166     
00167     return scrollbarWidth;
00168     }
00169 
00170 
00171 void CDescriptorExContainer::SizeChanged()
00172     {
00173     TRect rect = Rect();
00174     TInt scrollbarWidth = GetScrollbarWidth();
00175     iTextBox->SetExtent(TPoint(0, 0),
00176             TSize(rect.Width() - scrollbarWidth, rect.Height()));
00177 
00178     iTextBox->UpdateScrollBarsL();
00179     
00180     // Update new size for the skin
00181     iSkinContext->SetRect(rect);
00182     }
00183 
00184 
00185 TTypeUid::Ptr CDescriptorExContainer::MopSupplyObject(TTypeUid aId)
00186     {
00187     // That is for the skin support
00188     if( aId.iUid == MAknsControlContext::ETypeId && iSkinContext != NULL )
00189         {
00190         return MAknsControlContext::SupplyMopObject( aId, iSkinContext );
00191         }
00192     return CCoeControl::MopSupplyObject( aId );    
00193     }
00194 

Generated by  doxygen 1.6.2