examples/Graphics/WS/PtBuffer/PointerMoveBuffer.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:�Hold down the SHIFT key and drag the mouse (with left mouse button down)
00028         for freehand drawing. 
00029 */
00030 
00031 #include <w32std.h>
00032 #include "Base.h"
00033 #include "PointerMoveBuffer.h"
00034 
00035 
00036 
00038 //                                       CMainWindow implementation
00040 
00041 
00042 /****************************************************************************\
00043 |       Function:       Constructor/Destructor for CMainWindow
00044 |       Input:          aClient         Client application that owns the window
00045 \****************************************************************************/
00046 CMainWindow::CMainWindow (CWsClient* aClient)
00047 : CWindow (aClient)
00048         {
00049         }
00050 
00051 
00052 CMainWindow::~CMainWindow ()
00053         {
00054         iWindow.Close();
00055         }
00056 
00057 /****************************************************************************\
00058 |       Function:       CMainWindow::Draw
00059 |       Purpose:        Redraws the contents of CMainWindow within a given
00060 |                               rectangle.
00061 |       Input:          aRect   Rectangle that needs redrawing
00062 |       Output:         None
00063 \****************************************************************************/
00064 
00065 void CMainWindow::Draw(const TRect& aRect)
00066         {
00067         CWindowGc* gc=SystemGc(); // get a gc
00068         gc->SetClippingRect(aRect); // clip outside this rect
00069         gc->Clear(aRect); // clear
00070 }
00071 
00072 /****************************************************************************\
00073 |       Function:       CMainWindow::HandlePointerBufferReady
00074 \****************************************************************************/
00075 
00076 void CMainWindow::HandlePointerMoveBufferReady ()
00077         {               
00078         TPoint pnts[KPointerMoveBufferSize];
00079         TPtr8 ptr((TUint8 *)&pnts,sizeof(pnts));
00080         TInt numPnts=Window().RetrievePointerMoveBuffer(ptr);
00081         TSize size = Window().Size();
00082         TPoint position = Window().Position();
00083         TRect redrawRect (position, size);
00084         CWindowGc* gc=SystemGc();
00085         gc->Activate(Window());
00086         Window().Invalidate();
00087         Window().BeginRedraw(redrawRect); // N.B. Redrawer::RunL() gets called with this 
00088                                                                           // rectangle - must give it a non-zero value
00089         for(TInt index=0;index<numPnts;index++) gc->DrawLineTo(pnts[index]);
00090         Window().EndRedraw();
00091         gc->Deactivate();
00092         }
00093 
00094 /****************************************************************************\
00095 |       Function:       CMainWindow::HandlePointerEvent
00096 |       Purpose:        Handles pointer events for CMainWindow.
00097 |       Input:          aPointerEvent   The pointer event!
00098 |       Output:         None
00099 \****************************************************************************/
00100 
00101 void CMainWindow::HandlePointerEvent (TPointerEvent& aPointerEvent)
00102         {       
00103         switch (aPointerEvent.iType)
00104                 {
00105                 case TPointerEvent::EButton1Down:
00106                         {
00107                         if (aPointerEvent.iModifiers&EModifierShift)
00108                                 {
00109                                 Window().EnablePointerMoveBuffer();
00110                                 CWindowGc* gc = SystemGc();
00111                                 gc->Activate(Window());
00112                                 gc->MoveTo(aPointerEvent.iPosition);
00113                                 gc->Deactivate();
00114                                 }
00115                         break;
00116                         }
00117                 case TPointerEvent::EButton1Up:
00118                         {
00119                         Window().DisablePointerMoveBuffer();
00120                         break;
00121                         }
00122                 default:
00123                         break;
00124                 }
00125 }
00126 
00127 
00129 //                                       CExampleWsClient implementation
00131 
00132 CExampleWsClient* CExampleWsClient::NewL(const TRect& aRect)
00133         {
00134         // make new client
00135         CExampleWsClient* client=new (ELeave) CExampleWsClient(aRect); 
00136         CleanupStack::PushL(client); // push, just in case
00137         client->ConstructL(); // construct and run
00138         CleanupStack::Pop();
00139         return client;
00140         }
00141 
00142 /****************************************************************************\
00143 |       Function:       Constructor/Destructor for CExampleWsClient
00144 |                               Destructor deletes everything that was allocated by
00145 |                               ConstructMainWindowL()
00146 \****************************************************************************/
00147 
00148 CExampleWsClient::CExampleWsClient(const TRect& aRect)
00149 :iRect(aRect)
00150         {
00151         }
00152 
00153 CExampleWsClient::~CExampleWsClient ()
00154         {
00155         delete iMainWindow;
00156         }
00157 
00158 
00159 /****************************************************************************\
00160 |       Function:       CExampleWsClient::ConstructMainWindowL()
00161 |                               Called by base class's ConstructL
00162 |       Purpose:        Allocates and creates all the windows owned by this client
00163 |                               (See list of windows in CExampleWsCLient declaration).
00164 \****************************************************************************/
00165 
00166 void CExampleWsClient::ConstructMainWindowL()
00167         {
00168         // Resources allocated in this function are freed in the CExampleWsClient destructor            
00169         iMainWindow=new (ELeave) CMainWindow(this);
00170         iMainWindow->ConstructL(iRect, TRgb (255,255,255));
00171         }
00172 
00173 /****************************************************************************\
00174 |       Function:       CExampleWsClient::HandleKeyEventL()
00175 |       Purpose:        Processes key events for CExampleWsClient
00176 \****************************************************************************/
00177 void CExampleWsClient::HandleKeyEventL (TKeyEvent& /*aKeyEvent*/)
00178         {
00179         }
00180 
00181 
00182 /****************************************************************************\
00183 |       Function:       CExampleWsClient::RunL()
00184 |                               Called by active scheduler when an even occurs
00185 |       Purpose:        Processes events according to their type
00186 |                               For key events: calls HandleKeyEventL() (global to client)
00187 |                               For pointer event: calls HandlePointerEvent() for window
00188 |                                  event occurred in.
00189 \****************************************************************************/
00190 void CExampleWsClient::RunL()
00191         {
00192         // get the event
00193         iWs.GetEvent(iWsEvent);
00194         TInt eventType=iWsEvent.Type();
00195         // take action on it
00196         switch (eventType)
00197                 {
00198         // events global within window group
00199         case EEventNull:
00200         case EEventKey:
00201         case EEventModifiersChanged:
00202         case EEventKeyUp:
00203         case EEventKeyDown:
00204         case EEventFocusLost:
00205         case EEventFocusGained:
00206         case EEventSwitchOn:
00207         case EEventPassword:
00208         case EEventWindowGroupsChanged:
00209         case EEventErrorMessage:
00210                 break;
00211         // events local to specific windows
00212         case EEventPointer:
00213                 {
00214                 CWindow* window=(CWindow*)(iWsEvent.Handle()); // get window
00215                 TPointerEvent& pointerEvent=*iWsEvent.Pointer();
00216                 window->HandlePointerEvent (pointerEvent);
00217                 break;
00218                 }
00219         case EEventPointerExit:
00220         case EEventPointerEnter:
00221                 break;
00222         case EEventPointerBufferReady:
00223                 {
00224                 CWindow* window=(CWindow*)(iWsEvent.Handle()); // get window
00225                 window->HandlePointerMoveBufferReady ();
00226                 break;
00227                 }
00228         case EEventDragDrop:
00229                 break;
00230         default:
00231                 break;
00232                 }
00233         IssueRequest(); // maintain outstanding request
00234         }

Generated by  doxygen 1.6.2