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

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

Generated by  doxygen 1.6.2