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

Generated by  doxygen 1.6.2