examples/Graphics/WS/Ordinal/Ordinal.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 "Ordinal.h"
00034 
00035 _LIT(KString1,"1");
00036 _LIT(KString2,"2");
00037 _LIT(KString3,"3");
00038 _LIT(KString4,"4");
00039 _LIT(KString5,"5");
00040 
00042 //                                       CNumberedWindow implementation
00044 
00045 /****************************************************************************\
00046 |       Function:       Constructor/Destructor for CNumberedWindow
00047 |       Input:          aClient         Client application that owns the window
00048 \****************************************************************************/
00049 CNumberedWindow::CNumberedWindow (CWsClient* aClient, TInt aNum)
00050 : CWindow (aClient), iNumber(aNum), iOldPos(0,0)
00051         {
00052         }
00053 
00054 
00055 CNumberedWindow::~CNumberedWindow ()
00056         {
00057         }
00058 
00059 
00060 /****************************************************************************\
00061 |       Function:       CNumberedWindow::Draw
00062 |       Purpose:        Redraws the contents of CNumberedWindow within a given
00063 |                               rectangle.  CNumberedWindow displays a number in the window.
00064 |       Input:          aRect   Rectangle that needs redrawing
00065 |       Output:         None
00066 \****************************************************************************/
00067 void CNumberedWindow::Draw(const TRect& aRect)
00068         {
00069         const TBufC<1> strings[5] = { *&KString1,
00070                                                           *&KString2,
00071                                                           *&KString3,
00072                                                           *&KString4,
00073                                                           *&KString5
00074                                                         };
00075 
00076         CWindowGc* gc=SystemGc(); // get a graphics context
00077         gc->SetClippingRect(aRect); // clip outside the redraw area
00078         gc->Clear(aRect); // clear the redraw area
00079         TSize size=iWindow.Size();
00080         TInt height=size.iHeight; // Need window height to calculate vertical text offset
00081         TInt ascent = Font()->AscentInPixels();
00082         TInt descent = Font()->DescentInPixels();
00083         TInt offset = (height + (ascent + descent)) / 2; // Calculate vertical text offset
00084         gc->SetPenColor(TRgb(0,0,0)); // Set pen to black
00085         gc->UseFont(Font());
00086         gc->DrawText(strings[iNumber], TRect(TPoint(0,0), size), offset, CGraphicsContext::ECenter);
00087         gc->DiscardFont();
00088         }
00089 
00090 /****************************************************************************\
00091 |       Function:       CNumberedWindow::HandlePointerEvent
00092 |       Purpose:        Handles pointer events for CNumberedWindow.
00093 |       Input:          aPointerEvent   The pointer event
00094 |       Output:         None
00095 \****************************************************************************/
00096 void CNumberedWindow::HandlePointerEvent (TPointerEvent& aPointerEvent)
00097         {       
00098         switch (aPointerEvent.iType)
00099                 {
00100                 case TPointerEvent::EDrag:
00101                         {
00102                         // Move the window position as the pointer is dragged.
00103                         TPoint point = aPointerEvent.iParentPosition;
00104                         TPoint distToMove = point - iOldPos;
00105                         TPoint position = Window().Position();
00106                         Window().SetPosition (position + distToMove);
00107                         iOldPos = point;
00108                         break;
00109                         }
00110                 case TPointerEvent::EButton1Down:
00111                         {
00112                         // Move window to front
00113                         Window().SetOrdinalPosition (0);
00114                         // Request drag events 
00115                         Window().PointerFilter (EPointerFilterDrag,     0);
00116                         // Initialize starting point for dragging
00117                         iOldPos = aPointerEvent.iParentPosition;
00118                         break;
00119                         }
00120                 case TPointerEvent::EButton1Up:
00121                         {
00122                         // Cancel the request for drag events.
00123                         Window().PointerFilter (EPointerFilterDrag,     EPointerFilterDrag);
00124                         break;
00125                         }
00126                 case TPointerEvent::EButton3Down:
00127                         {
00128                         // Cascade windows in top left corner.
00129                         // Window at the front should be cascaded last.
00130                         // The window at the front (ordinal position 0) is given by Child(), and 
00131                         // each window behind it is given by NextSibling(). We need to go down
00132                         // the sibling list till we get to the last sibling, and move this to 
00133                         // the top left corner. Then go back up the list and move each previous 
00134                         // sibling on top of the last, displaced by an offset (of TPoint(10,10)).
00135                         TPoint point (0,0);
00136                         TUint32 nextSib, prevSib;
00137                         CWindow* childWindow;
00138                         TInt    numChildren = 0;
00139                         TUint32 child = Window().Child();
00140                         if (child)
00141                                 {
00142                                 childWindow = (CWindow*)child;
00143                                 numChildren++;
00144                                 nextSib = childWindow->Window().NextSibling();
00145                                 while (nextSib)
00146                                         {
00147                                         numChildren++;
00148                                         childWindow = (CWindow*)nextSib;
00149                                         nextSib = childWindow->Window().NextSibling();
00150                                         }
00151                                 for (TInt i=numChildren; i>0; i--)
00152                                         {
00153                                         childWindow->Window().SetPosition (point);
00154                                         prevSib = childWindow->Window().PrevSibling();
00155                                         if (prevSib)
00156                                                 {
00157                                                 childWindow = (CWindow*)prevSib;
00158                                                 point += TPoint(10,10);
00159                                                 }
00160                                         }
00161                                 }
00162                         break;
00163                         }
00164                 default:
00165                         break;
00166                 }
00167         }
00168 
00169 
00171 //                                       CMainWindow implementation
00173 
00174 
00175 /****************************************************************************\
00176 |       Function:       Constructor/Destructor for CMainWindow
00177 |       Input:          aClient         Client application that owns the window
00178 \****************************************************************************/
00179 CMainWindow::CMainWindow (CWsClient* aClient)
00180 : CWindow (aClient)
00181         {
00182         }
00183 
00184 
00185 CMainWindow::~CMainWindow ()
00186         {
00187         iWindow.Close();
00188         }
00189 
00190 /****************************************************************************\
00191 |       Function:       CMainWindow::Draw
00192 |       Purpose:        Redraws the contents of CMainWindow within a given
00193 |                               rectangle.
00194 |       Input:          aRect   Rectangle that needs redrawing
00195 |       Output:         None
00196 \****************************************************************************/
00197 
00198 void CMainWindow::Draw(const TRect& aRect)
00199         {
00200         CWindowGc* gc=SystemGc(); // get a gc
00201         gc->SetClippingRect(aRect); // clip outside this rect
00202         gc->Clear(aRect); // clear
00203         }
00204 
00205 
00206 /****************************************************************************\
00207 |       Function:       CMainWindow::HandlePointerEvent
00208 |       Purpose:        Handles pointer events for CMainWindow.
00209 |       Input:          aPointerEvent   The pointer event!
00210 |       Output:         None
00211 \****************************************************************************/
00212 
00213 void CMainWindow::HandlePointerEvent (TPointerEvent& aPointerEvent)
00214         {       
00215         switch (aPointerEvent.iType)
00216                 {
00217                 case TPointerEvent::EButton3Down:
00218                         {
00219                         // Cascade windows in top left corner
00220                         // Window at the front should be cascaded last.
00221                         // The window at the front (ordinal position 0) is given by Child(), and 
00222                         // each window behind it is given by NextSibling(). We need to go down
00223                         // the sibling list till we get to the last sibling, and move this to 
00224                         // the top left corner. Then go back up the list and move each previous 
00225                         // sibling on top of the last, displaced by an offset (of TPoint(10,10)).
00226                         TPoint point (0,0);
00227                         TUint32 nextSib, prevSib;
00228                         CWindow* childWindow;
00229                         TInt    numChildren = 0;
00230                         TUint32 child = Window().Child();
00231                         if (child)
00232                                 {
00233                                 childWindow = (CWindow*)child;
00234                                 numChildren++;
00235                                 nextSib = childWindow->Window().NextSibling();
00236                                 while (nextSib)
00237                                         {
00238                                         numChildren++;
00239                                         childWindow = (CWindow*)nextSib;
00240                                         nextSib = childWindow->Window().NextSibling();
00241                                         }
00242                                 for (TInt i=numChildren; i>0; i--)
00243                                         {
00244                                         childWindow->Window().SetPosition (point);
00245                                         prevSib = childWindow->Window().PrevSibling();
00246                                         if (prevSib)
00247                                                 {
00248                                                 childWindow = (CWindow*)prevSib;
00249                                                 point += TPoint(10,10);
00250                                                 }
00251                                         }
00252                                 }
00253                         break;
00254                         }
00255                 default:
00256                         break;
00257                 }
00258         }
00259 
00260 
00262 //                                       CExampleWsClient implementation
00264 
00265 CExampleWsClient* CExampleWsClient::NewL(const TRect& aRect)
00266         {
00267         // make new client
00268         CExampleWsClient* client=new (ELeave) CExampleWsClient(aRect); 
00269         CleanupStack::PushL(client); // push, just in case
00270         client->ConstructL(); // construct and run
00271         CleanupStack::Pop();
00272         return client;
00273         }
00274 
00275 /****************************************************************************\
00276 |       Function:       Constructor/Destructor for CExampleWsClient
00277 |                               Destructor deletes everything that was allocated by
00278 |                               ConstructMainWindowL()
00279 \****************************************************************************/
00280 
00281 CExampleWsClient::CExampleWsClient(const TRect& aRect)
00282 :iRect(aRect)
00283         {
00284         }
00285 
00286 CExampleWsClient::~CExampleWsClient ()
00287         {
00288         delete iWindow1;
00289         delete iWindow2;
00290         delete iWindow3;
00291         delete iWindow4;
00292         delete iWindow5;
00293         delete iMainWindow;
00294         }
00295 
00296 
00297 /****************************************************************************\
00298 |       Function:       CExampleWsClient::ConstructMainWindowL()
00299 |                               Called by base class's ConstructL
00300 |       Purpose:        Allocates and creates all the windows owned by this client
00301 |                               (See list of windows in CExampleWsCLient declaration).
00302 \****************************************************************************/
00303 
00304 void CExampleWsClient::ConstructMainWindowL()
00305         {
00306         // Resources allocated in this function are freed in the CExampleWsClient destructor
00307                         
00308         iMainWindow=new (ELeave) CMainWindow(this);
00309         iMainWindow->ConstructL(iRect, TRgb (255,255,255));
00310         TInt count=0;
00311         TRect rect(0,0,40,40);
00312         iWindow1  = new (ELeave) CNumberedWindow (this, count++);
00313         iWindow1->ConstructL (rect, TRgb (50, 50, 50),iMainWindow);
00314         iWindow2  = new (ELeave) CNumberedWindow (this, count++);
00315         iWindow2->ConstructL (rect, TRgb (100, 100, 100),iMainWindow);
00316         iWindow3  = new (ELeave) CNumberedWindow (this, count++);
00317         iWindow3->ConstructL (rect, TRgb (150, 150, 150),iMainWindow);
00318         iWindow4  = new (ELeave) CNumberedWindow (this, count++);
00319         rect.Shrink(10,10);
00320         iWindow4->ConstructL (rect, TRgb (200, 200, 200),iWindow1);
00321         iWindow5  = new (ELeave) CNumberedWindow (this, count++);
00322         iWindow5->ConstructL (rect, TRgb (150, 150, 150),iWindow1);
00323         }
00324 
00325 
00326 /****************************************************************************\
00327 |       Function:       CExampleWsClient::RunL()
00328 |                               Called by active scheduler when an even occurs
00329 |       Purpose:        Processes events according to their type
00330 |                               For key events: calls HandleKeyEventL() (global to client)
00331 |                               For pointer event: calls HandlePointerEvent() for window
00332 |                                  event occurred in.
00333 \****************************************************************************/
00334 void CExampleWsClient::RunL()
00335         {
00336         // get the event
00337         iWs.GetEvent(iWsEvent);
00338         TInt eventType=iWsEvent.Type();
00339         // take action on it
00340         switch (eventType)
00341                 {
00342         // events global within window group
00343         case EEventNull:
00344                 break;
00345         case EEventKey:
00346                 {
00347                 TKeyEvent& keyEvent=*iWsEvent.Key(); // get key event
00348                 HandleKeyEventL (keyEvent);
00349                 break;
00350                 }
00351         case EEventKeyUp:
00352         case EEventModifiersChanged:
00353         case EEventKeyDown:
00354         case EEventFocusLost:
00355         case EEventFocusGained:
00356         case EEventSwitchOn:
00357         case EEventPassword:
00358         case EEventWindowGroupsChanged:
00359         case EEventErrorMessage:
00360                 break;
00361         // events local to specific windows
00362         case EEventPointer:
00363                 {
00364                 CWindow* window=(CWindow*)(iWsEvent.Handle()); // get window
00365                 TPointerEvent& pointerEvent=*iWsEvent.Pointer();
00366                 window->HandlePointerEvent (pointerEvent);
00367                 break;
00368                 }
00369         case EEventPointerExit:
00370         case EEventPointerEnter:
00371         case EEventPointerBufferReady:
00372                 break;
00373         case EEventDragDrop:
00374                 break;
00375         default:
00376                 break;
00377                 }
00378         IssueRequest(); // maintain outstanding request
00379         }
00380 
00381 /****************************************************************************\
00382 |       Function:       CExampleWsClient::HandleKeyEventL()
00383 |       Purpose:        Processes key events for CExampleWsClient
00384 |                               Gets the key code from the key event.  Exits on 'Escape'
00385 \****************************************************************************/
00386 void CExampleWsClient::HandleKeyEventL (TKeyEvent& /*aKeyEvent*/)
00387         {
00388         }
00389 

Generated by  doxygen 1.6.2