examples/Graphics/WS/BackedUp/BackedUp.cpp

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

Generated by  doxygen 1.6.2