examples/Graphics/WS/BackedUp/BackedUp.cpp

00001 // Copyright (c) 2005-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 //
00015 
00016 #include <w32std.h>
00017 #include "Base.h"
00018 #include "BackedUp.h"
00019 
00020 _LIT(KString1,"1");
00021 _LIT(KString2,"2");
00022 _LIT(KString3,"3");
00023 _LIT(KString4,"4");
00024 _LIT(KString5,"5");
00025 
00026 enum TBackedUpPanic
00027         {
00028         EConnectPanicRedrawToBackedUpWindow,
00029         };
00030 
00031 
00033 //                                       CNumberedWindow implementation
00035 
00036 /****************************************************************************\
00037 |       Function:       Constructor/Destructor for CNumberedWindow
00038 |       Input:          aClient         Client application that owns the window
00039 \****************************************************************************/
00040 CNumberedWindow::CNumberedWindow (CWsClient* aClient)
00041 : CWindow (aClient), iOldPos(0,0)
00042         {
00043         // iNumber is displayed on front of window.
00044         iNumber = iCount;
00045         iCount++;
00046         }
00047 
00048 
00049 CNumberedWindow::~CNumberedWindow ()
00050         {
00051         }
00052 
00053 
00054 /****************************************************************************\
00055 |       Function:       CNumberedWindow::Draw
00056 |       Purpose:        Redraws the contents of CNumberedWindow within a given
00057 |                               rectangle.  CNumberedWindow displays a number in the window.
00058 |       Input:          aRect   Rectangle that needs redrawing
00059 |       Output:         None
00060 \****************************************************************************/
00061 void CNumberedWindow::Draw(const TRect& aRect)
00062         {
00063         const TBufC<1> strings[5] = { *&KString1,
00064                                                           *&KString2,
00065                                                           *&KString3,
00066                                                           *&KString4,
00067                                                   *&KString5
00068                                                         };
00069 
00070         CWindowGc* gc=SystemGc(); // get a graphics context
00071         gc->SetClippingRect(aRect); // clip outside the redraw area
00072         gc->Clear(aRect); // clear the redraw area
00073         TSize size=iWindow.Size();
00074         TInt height=size.iHeight; // Need window height to calculate vertical text offset
00075         TInt ascent = Font()->AscentInPixels();
00076         TInt descent = Font()->DescentInPixels();
00077         TInt offset = (height + (ascent + descent)) / 2; // Calculate vertical text offset
00078         gc->SetPenColor(TRgb(0,0,0)); // Set pen to black
00079         gc->UseFont(Font());
00080         gc->DrawText(strings[iNumber], TRect(TPoint(0,0), size), offset, CGraphicsContext::ECenter);
00081         gc->DiscardFont();
00082         }
00083 
00084 /****************************************************************************\
00085 |       Function:       CNumberedWindow::HandlePointerEvent
00086 |       Purpose:        Handles pointer events for CNumberedWindow.
00087 |       Input:          aPointerEvent   The pointer event
00088 |       Output:         None
00089 \****************************************************************************/
00090 void CNumberedWindow::HandlePointerEvent (TPointerEvent& aPointerEvent)
00091         {       
00092         switch (aPointerEvent.iType)
00093                 {
00094                 case TPointerEvent::EDrag:
00095                         {
00096                         TPoint point = aPointerEvent.iParentPosition;
00097                         TPoint distToMove = point - iOldPos;
00098                         TPoint position = Window().Position();
00099                         Window().SetPosition (position + distToMove);
00100                         iOldPos = point;
00101                         break;
00102                         }
00103                 case TPointerEvent::EButton1Down:
00104                         {
00105                         // Request drag events 
00106                         Window().PointerFilter (EPointerFilterDrag,     0);
00107                         Window().SetOrdinalPosition (0);
00108                         iOldPos = aPointerEvent.iParentPosition;
00109                         break;
00110                         }
00111                 case TPointerEvent::EButton1Up:
00112                         {
00113                         // Cancel the request for drag events.
00114                         Window().PointerFilter (EPointerFilterDrag,     EPointerFilterDrag);
00115                         break;
00116                         }
00117                 default:
00118                         break;
00119                 }
00120         }
00121 
00122 
00124 //                                       CMainWindow implementation
00126 
00127 
00128 /****************************************************************************\
00129 |       Function:       Constructor/Destructor for CMainWindow
00130 |       Input:          aClient         Client application that owns the window
00131 \****************************************************************************/
00132 CMainWindow::CMainWindow (CWsClient* aClient)
00133 : CBackedUpWindow (aClient)
00134         {
00135         }
00136 
00137 
00138 CMainWindow::~CMainWindow ()
00139         {
00140         iWindow.Close();
00141         }
00142 
00143 /****************************************************************************\
00144 |       Function:       CMainWindow::Draw
00145 |       Purpose:        Redraws the contents of CMainWindow within a given
00146 |                               rectangle.
00147 |       Input:          aRect   Rectangle that needs redrawing
00148 |       Output:         None
00149 \****************************************************************************/
00150 
00151 void CMainWindow::Draw(const TRect& /*aRect*/)
00152         {
00153         _LIT(KPanicMsg,"BACKEDUP");
00154         User::Panic(KPanicMsg,EConnectPanicRedrawToBackedUpWindow);
00155         }
00156 
00157 void CMainWindow::HandlePointerMoveBufferReady ()
00158         {               
00159         TPoint pnts[KPointerMoveBufferSize];
00160         TPtr8 ptr((TUint8 *)&pnts,sizeof(pnts));
00161         TInt numPnts=Window().RetrievePointerMoveBuffer(ptr);
00162         CWindowGc* gc=SystemGc();
00163         gc->Activate(Window());
00164         for(TInt index=0;index<numPnts;index++) gc->DrawLineTo(pnts[index]);
00165         gc->Deactivate();
00166         }
00167 
00168 /****************************************************************************\
00169 |       Function:       CMainWindow::HandlePointerEvent
00170 |       Purpose:        Handles pointer events for CMainWindow.
00171 |       Input:          aPointerEvent   The pointer event!
00172 |       Output:         None
00173 \****************************************************************************/
00174 
00175 void CMainWindow::HandlePointerEvent (TPointerEvent& aPointerEvent)
00176         {       
00177         switch (aPointerEvent.iType)
00178                 {
00179                 case TPointerEvent::EButton1Down:
00180                         {
00181                         Window().SetShadowDisabled(EFalse);
00182                         Window().EnablePointerMoveBuffer();
00183                         CWindowGc* gc = SystemGc();
00184                         gc->Activate(Window());
00185                         gc->MoveTo(aPointerEvent.iPosition);
00186                         gc->Deactivate();
00187                         }
00188                 default:
00189                         break;
00190                 }
00191         }
00192 
00193 
00195 //                                       CExampleWsClient implementation
00197 
00198 CExampleWsClient* CExampleWsClient::NewL(const TRect& aRect)
00199         {
00200         // make new client
00201         CExampleWsClient* client=new (ELeave) CExampleWsClient(aRect); 
00202         CleanupStack::PushL(client); // push, just in case
00203         client->ConstructL(); // construct and run
00204         CleanupStack::Pop();
00205         return client;
00206         }
00207 
00208 /****************************************************************************\
00209 |       Function:       Constructor/Destructor for CExampleWsClient
00210 |                               Destructor deletes everything that was allocated by
00211 |                               ConstructMainWindowL()
00212 \****************************************************************************/
00213 
00214 CExampleWsClient::CExampleWsClient(const TRect& aRect)
00215 :iRect(aRect)
00216         {
00217         }
00218 
00219 CExampleWsClient::~CExampleWsClient ()
00220         {
00221         delete iMainWindow;
00222         delete iWindow1;
00223         }
00224 
00225 
00226 /****************************************************************************\
00227 |       Function:       CExampleWsClient::ConstructMainWindowL()
00228 |                               Called by base class's ConstructL
00229 |       Purpose:        Allocates and creates all the windows owned by this client
00230 |                               (See list of windows in CExampleWsCLient declaration).
00231 \****************************************************************************/
00232 
00233 void CExampleWsClient::ConstructMainWindowL()
00234         {
00235         // Resources allocated in this function are freed in the CExampleWsClient destructor
00236         
00237         iMainWindow=new (ELeave) CMainWindow(this);
00238         iMainWindow->ConstructL(iRect);
00239         iWindow1  = new (ELeave) CNumberedWindow (this);
00240         iWindow1->ConstructL (TRect (TPoint (100, 100), TSize (100, 100)), TRgb (100, 100, 100),
00241                 iMainWindow);
00242         }
00243 
00244 
00245 /****************************************************************************\
00246 |       Function:       CExampleWsClient::RunL()
00247 |                               Called by active scheduler when an even occurs
00248 |       Purpose:        Processes events according to their type
00249 |                               For key events: calls HandleKeyEventL() (global to client)
00250 |                               For pointer event: calls HandlePointerEvent() for window
00251 |                                  event occurred in.
00252 \****************************************************************************/
00253 void CExampleWsClient::RunL()
00254         {
00255         // get the event
00256         iWs.GetEvent(iWsEvent);
00257         TInt eventType=iWsEvent.Type();
00258         // take action on it
00259         switch (eventType)
00260                 {
00261         // events global within window group
00262         case EEventNull:
00263                 break;
00264         case EEventKey:
00265                 {
00266                 TKeyEvent& keyEvent=*iWsEvent.Key(); // get key event
00267                 HandleKeyEventL (keyEvent);
00268                 break;
00269                 }
00270         case EEventKeyUp:
00271         case EEventModifiersChanged:
00272         case EEventKeyDown:
00273         case EEventFocusLost:
00274         case EEventFocusGained:
00275         case EEventSwitchOn:
00276         case EEventPassword:
00277         case EEventWindowGroupsChanged:
00278         case EEventErrorMessage:
00279                 break;
00280         // events local to specific windows
00281         case EEventPointer:
00282                 {
00283                 CWindow* window=(CWindow*)(iWsEvent.Handle()); // get window
00284                 TPointerEvent& pointerEvent=*iWsEvent.Pointer();
00285                 window->HandlePointerEvent (pointerEvent);
00286                 break;
00287                 }
00288         case EEventPointerExit:
00289         case EEventPointerEnter:
00290         case EEventPointerBufferReady:
00291                 {
00292                 CWindow* window=(CWindow*)(iWsEvent.Handle()); // get window
00293                 window->HandlePointerMoveBufferReady ();
00294                 break;
00295                 }
00296         case EEventDragDrop:
00297                 break;
00298         default:
00299                 break;
00300                 }
00301         IssueRequest(); // maintain outstanding request
00302         }
00303 
00304 /****************************************************************************\
00305 |       Function:       CExampleWsClient::HandleKeyEventL()
00306 |       Purpose:        Processes key events for CExampleWsClient
00307 \****************************************************************************/
00308 void CExampleWsClient::HandleKeyEventL (TKeyEvent& /*aKeyEvent*/)
00309         {
00310         }
00311 
00312 

Generated by  doxygen 1.6.2