examples/AppFramework/UIControls/ControlFramework/src/ControlFrameworkView.cpp

00001 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // ControlFrameworkAppView.cpp
00015 //
00016 
00017 #include <eikenv.h>
00018 #include <controlframework.rsg>
00019 
00020 #include "ControlFrameworkView.h"
00021 #include "ControlFrameworkAppUi.h"
00022 #include "ControlFrameworkGlobals.h"
00023 
00024 // Maximum number of lines that the text can be split into 
00025 const TInt KMaxLines = 2;
00026 // Maximum number of characters that the text object can hold
00027 const TInt KMaxStringLength = 64;
00028 _LIT(KPointerEventMessage, "Pointer event %d at (%d,%d)");
00029 _LIT(KKeyEventMessage, "Key 0x%x, modifier 0x%x");
00030 
00031 CControlFrameworkView::CControlFrameworkView(CControlFrameworkAppUi& aAppUi) : iAppUi(aAppUi), iFont(TCoeFont::ELarge, TCoeFont::EPlain)
00032         {
00033         }
00034 
00035 CControlFrameworkView* CControlFrameworkView::NewLC(CControlFrameworkAppUi& aAppUi)
00036         {
00037         CControlFrameworkView* view = new(ELeave) CControlFrameworkView(aAppUi);
00038         CleanupStack::PushL(view);
00039         return view;
00040         }
00041 
00042 CControlFrameworkView::~CControlFrameworkView()
00043         {
00044         iRunInfoArray.Close();
00045         delete iBidiText;
00046         }
00047 
00048 // Called by the UI framework the first time the view is activated.
00049 // As much as possible of the code normally found in ConstructL() has 
00050 // been moved here to improve application startup time.
00051 void CControlFrameworkView::ViewConstructL()
00052         {
00053         // The control is window-owning.
00054         CreateWindowL();
00055 
00056         iBidiText = TBidiText::NewL(KMaxStringLength, KMaxLines);
00057         iRunInfoArray.OpenL();
00058         HBufC* message = iEikonEnv->AllocReadResourceLC(R_WELCOME_TEXT);
00059         iBidiText->SetText(*message, iRunInfoArray); // TDirectionality determined by resource
00060         CleanupStack::PopAndDestroy(message);
00061         
00062         // Set extent of the view. ClientRect calculates to ScreenSize minus Toolbar, Menubar, Statusbar etc.
00063         SetRect(iAppUi.ClientRect());
00064         
00065         // The control is ready to draw, so notify the UI framework.
00066         ActivateL();
00067         }       
00068 
00069 
00070 // Uniquely identifies the view 
00071 TVwsViewId CControlFrameworkView::ViewId() const
00072         {
00073         return TVwsViewId(KUidControlFrameworkAppUid, KUidControlFrameworkView);
00074         }
00075 
00076 
00077 void CControlFrameworkView::ViewActivatedL(const TVwsViewId& /*aPrevViewId*/, TUid /*aCustomMessageId*/, const TDesC8& /*aCustomMessage*/)
00078         {
00079         }
00080 
00081 void CControlFrameworkView::ViewDeactivated()
00082         {
00083         }
00084 
00085 // Receives pointer events and prepares to write a description of 
00086 // the pointer event to the screen.
00087 void CControlFrameworkView::HandlePointerEventL(const TPointerEvent& aPointerEvent)
00088         {
00089         TBuf<KMaxStringLength> text;
00090         text.Format(KPointerEventMessage, aPointerEvent.iType,aPointerEvent.iPosition.iX,aPointerEvent.iPosition.iY);
00091         iBidiText->SetText(text, TBidiText::ELeftToRight, iRunInfoArray);
00092         Window().Invalidate();
00093         }
00094 
00095 // Draws the border and the message text
00096 void CControlFrameworkView::Draw(const TRect& aRect) const
00097         {
00098         TRect rect = aRect;
00099         DrawBorder(rect);
00100         DrawMessage(rect);
00101         }
00102 
00103 // Draws a 5 pixel wide border around the window 
00104 void CControlFrameworkView::DrawBorder(TRect& aRect) const
00105         {
00106         CWindowGc& gc=SystemGc();
00107         gc.SetPenSize(TSize(5,5));
00108         gc.DrawRect(aRect);
00109         aRect.Shrink(5,5);
00110         }
00111 
00112 // Displays the message stored in iBidiText centered vertically and horizontally.
00113 void CControlFrameworkView::DrawMessage(const TRect& aRect) const
00114         {
00115         CWindowGc& gc=SystemGc();
00116 
00117         if (IsStrikethrough())
00118                 gc.SetStrikethroughStyle(EStrikethroughOn);
00119         
00120         if (IsUnderline())
00121                 gc.SetUnderlineStyle(EUnderlineOn);
00122 
00123         XCoeTextDrawer textDrawer(TextDrawer());
00124         textDrawer->SetAlignment(EHCenterVCenter);
00125         const CFont& font = ScreenFont(iFont);
00126         iBidiText->WrapText((aRect.iBr.iX - aRect.iTl.iX) , font, NULL, KMaxLines);
00127         textDrawer.DrawText(gc, *iBidiText, aRect, font);
00128         }
00129 
00130 /*
00131 Moves the window and prints key event information to the screen.
00132 The following key combinations cause the window to move: 
00133 1. Shift + KeyLeft arrow
00134 2. Shift + KeyRight arrow
00135 3. Shift + KeyDown arrow
00136 4. Shift + KeyUp arrow
00137 */
00138 TKeyResponse CControlFrameworkView::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
00139         {
00140         if (aType == EEventKey)
00141                 {
00142                 TInt modifiers=aKeyEvent.iModifiers;
00143                 TInt code=aKeyEvent.iCode;
00144 
00145                 // Write a description of the key event to the screen
00146                 TBuf<KMaxStringLength> text;
00147                 text.Format(KKeyEventMessage, code,modifiers);
00148                 iBidiText->SetText(text, TBidiText::ELeftToRight, iRunInfoArray);
00149                 Window().Invalidate();
00150                 
00151                 if (modifiers&EModifierShift)
00152                         {
00153                         TPoint pos=Position();
00154                         switch (code)
00155                                 {
00156                                 case EKeyLeftArrow:
00157                                         pos.iX--;
00158                                         break;
00159                                 case EKeyRightArrow:
00160                                         pos.iX++;
00161                                         break;
00162                                 case EKeyUpArrow:
00163                                         pos.iY--;
00164                                         break;
00165                                 case EKeyDownArrow:
00166                                         pos.iY++;
00167                                         break;
00168                                 default:
00169                                         break;
00170                                 }
00171                         if (pos != Position())
00172                                 {
00173                                 SetPosition(pos);
00174                                 return(EKeyWasConsumed);
00175                                 }
00176                         }
00177                 }
00178         return(EKeyWasNotConsumed);
00179         }
00180 
00181 TBool CControlFrameworkView::IsStrikethrough() const
00182         {
00183         return iFontFlags.IsSet(EStrikethrough);
00184         }
00185 
00186 void CControlFrameworkView::ToggleStrikethrough()
00187         {
00188         if (IsStrikethrough())
00189                 iFontFlags.Clear(EStrikethrough);
00190         else 
00191                 iFontFlags.Set(EStrikethrough);
00192         
00193         Window().Invalidate();
00194         }     
00195         
00196 TBool CControlFrameworkView::IsUnderline() const
00197         { 
00198         return iFontFlags.IsSet(EUnderline);
00199         }
00200 
00201 void CControlFrameworkView::ToggleUnderline()
00202         {
00203         if (IsUnderline())
00204                 iFontFlags.Clear(EUnderline);
00205         else 
00206                 iFontFlags.Set(EUnderline);
00207         
00208         Window().Invalidate();
00209         } 
00210         
00211 TBool CControlFrameworkView::IsBold() const
00212         {
00213         return iFontFlags.IsSet(EBold);
00214         }
00215 
00216 void CControlFrameworkView::ToggleBold()
00217         {
00218         if (IsBold())
00219                 {
00220                 iFontFlags.Clear(EBold);
00221                 iFontFlags.Clear(EItalic);
00222                 iFont = TCoeFont(iFont.LogicalSize(), TCoeFont::EPlain);
00223                 }
00224         else 
00225                 {
00226                 iFontFlags.Set(EBold);
00227                 iFontFlags.Clear(EItalic);
00228                 iFont = TCoeFont(iFont.LogicalSize(), TCoeFont::EBold);
00229                 }
00230         
00231         Window().Invalidate();
00232         }     
00233         
00234 TBool CControlFrameworkView::IsItalic() const
00235         {
00236         return iFontFlags.IsSet(EItalic);
00237         }
00238     
00239 
00240 void CControlFrameworkView::ToggleItalic()
00241         {
00242         if (IsItalic())
00243                 {
00244                 iFontFlags.Clear(EBold);
00245                 iFontFlags.Clear(EItalic);
00246                 iFont = TCoeFont(iFont.LogicalSize(), TCoeFont::EPlain);
00247                 }
00248         else 
00249                 {       
00250                 iFontFlags.Set(EItalic);
00251                 iFontFlags.Clear(EBold);
00252                 iFont = TCoeFont(iFont.LogicalSize(), TCoeFont::EItalic);
00253                 }
00254                 
00255         Window().Invalidate();
00256         }       

Generated by  doxygen 1.6.2