javauis/lcdui_akn/lcdui/src/CMIDCanvasGraphicsItemPainter.cpp
branchRCL_3
changeset 14 04becd199f91
child 18 9ac0a0a7da70
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Implements the native part of Canvas Graphics Item painter.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // EXTERNAL INCLUDES
       
    20 #include <MMIDCustomComponentContainer.h>
       
    21 #include <lcdui.h>
       
    22 #include <fbs.h>
       
    23 #include <bitdev.h>
       
    24 #include <bitstd.h>
       
    25 #include <j2me/jdebug.h>
       
    26 
       
    27 // INTERNAL INCLUDES
       
    28 #include "CMIDCanvasGraphicsItem.h"
       
    29 #include "CMIDCanvasGraphicsItemPainter.h"
       
    30 
       
    31 // ---------------------------------------------------------------------------
       
    32 // CMIDCanvasGraphicsItemPainter::NewL
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 CMIDCanvasGraphicsItemPainter* CMIDCanvasGraphicsItemPainter::NewL(
       
    36     const TCtorParams& aParams)
       
    37 {
       
    38     CMIDCanvasGraphicsItemPainter* self =
       
    39         new(ELeave) CMIDCanvasGraphicsItemPainter(*aParams.iEnv);
       
    40 
       
    41     CleanupStack::PushL(self);
       
    42     self->ConstructL(aParams);
       
    43     CleanupStack::Pop(self);
       
    44 
       
    45     return self;
       
    46 }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // CMIDCanvasGraphicsItemPainter::ConstructL
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 void CMIDCanvasGraphicsItemPainter::ConstructL(
       
    53     const TCtorParams& aParams)
       
    54 {
       
    55     iContentRect = TRect(0, 0, aParams.iWidth, aParams.iHeight);
       
    56     SetFocusing(EFalse);
       
    57     SetSize(TSize(aParams.iWidth, aParams.iHeight));
       
    58     CreateBuffersL();
       
    59     iVisible = EFalse;
       
    60 }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // CMIDCanvasGraphicsItemPainter::CMIDCanvasGraphicsItemPainter
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 CMIDCanvasGraphicsItemPainter::CMIDCanvasGraphicsItemPainter(MMIDEnv& aEnv) :
       
    67         iEnv(aEnv)
       
    68 {
       
    69     // No implementation.
       
    70 }
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // CMIDCanvasGraphicsItemPainter::~CMIDCanvasGraphicsItemPainter
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 CMIDCanvasGraphicsItemPainter::~CMIDCanvasGraphicsItemPainter()
       
    77 {
       
    78     DEBUG("CMIDCanvasGraphicsItemPainter::~CMIDCanvasGraphicsItemPainter +");
       
    79 
       
    80     // Release buffers
       
    81     ResetBuffers();
       
    82 
       
    83     // Remove association from direct container.
       
    84     if (iDirectContainer)
       
    85     {
       
    86         iDirectContainer->MdcRemoveContent(this);
       
    87     }
       
    88 
       
    89     iItem = NULL;
       
    90 
       
    91     DEBUG("CMIDCanvasGraphicsItemPainter::~CMIDCanvasGraphicsItemPainter -");
       
    92 }
       
    93 
       
    94 enum TOpCode
       
    95 {
       
    96     ESync,
       
    97     ESyncRect
       
    98 };
       
    99 
       
   100 /** Copy the content (iFrameBuffer) into iOffScreenBuffer and then draw.
       
   101 Either do a full copy or copy only the clipped area.
       
   102 */
       
   103 #ifdef RD_JAVA_NGA_ENABLED
       
   104 TBool CMIDCanvasGraphicsItemPainter::ProcessL(
       
   105     const TMIDBufferOp*& aOp, const TMIDBufferOp* aEnd,
       
   106     TInt& /*aCycles*/, java::util::Monitor* /*aMonitor*/)
       
   107 #else
       
   108 TBool CMIDCanvasGraphicsItemPainter::ProcessL(
       
   109     const TMIDBufferOp*& aOp, const TMIDBufferOp* aEnd,
       
   110     TInt& /*aCycles*/, TRequestStatus* /*aStatus*/)
       
   111 #endif
       
   112 {
       
   113     DEBUG("< CMIDCanvasGraphicsItemPainter::ProcessL");
       
   114 
       
   115     while (aOp < aEnd)
       
   116     {
       
   117         switch (aOp->OpCode())
       
   118         {
       
   119         case ESync:
       
   120             DrawNow();
       
   121             break;
       
   122 
       
   123         case ESyncRect:
       
   124             TRect clip = *static_cast<const TRect*>(aOp->Data());
       
   125 
       
   126             clip.Move(iContentRect.iTl);
       
   127             clip.Intersection(Rect());
       
   128 
       
   129             ActivateGc();
       
   130             Draw(clip);
       
   131             DeactivateGc();
       
   132 
       
   133             break;
       
   134         }
       
   135         aOp += aOp->Size();
       
   136     }
       
   137 
       
   138     DEBUG("> CMIDCanvasGraphicsItemPainter::ProcessL");
       
   139     return EFalse;
       
   140 }
       
   141 
       
   142 void CMIDCanvasGraphicsItemPainter::AbortAsync()
       
   143 {
       
   144     // nope
       
   145 }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 // CMIDCanvasGraphicsItemPainter::SetDirectContainerL
       
   149 // (other items are commented in the header file)
       
   150 // ---------------------------------------------------------------------------
       
   151 //
       
   152 void CMIDCanvasGraphicsItemPainter::SetDirectContainerL(
       
   153     MDirectContainer* aDirectContainer)
       
   154 {
       
   155     // Remove association from the previous direct container.
       
   156     if (iDirectContainer)
       
   157     {
       
   158         iDirectContainer->MdcRemoveContent(this);
       
   159     }
       
   160 
       
   161     // Associate this component to the new direct container. Note that this
       
   162     // needs to be done in order to disable the direct screen access of
       
   163     // the MID parent component. This way it does not draw on top of
       
   164     // the canvas graphics item when repaint etc. methods are called.
       
   165     if (aDirectContainer)
       
   166     {
       
   167         aDirectContainer->MdcAddContent(this);
       
   168     }
       
   169 
       
   170     // Store new container. Or NULL if passed.
       
   171     iDirectContainer = aDirectContainer;
       
   172 }
       
   173 
       
   174 void CMIDCanvasGraphicsItemPainter::SetParentL(MMIDCustomComponentContainer* aComponentContainer)
       
   175 {
       
   176     CCoeControl& control = aComponentContainer->Control();
       
   177     SetContainerWindowL(control);
       
   178     ActivateL();
       
   179     if (iVisible)
       
   180     {
       
   181         // Paint the control
       
   182         iEnv.PostJavaEvent(*this, ECanvasGraphicsItemPainterEvent,
       
   183                            ECanvasGraphicsItemRepaint, 0);
       
   184     }
       
   185     else
       
   186     {
       
   187         MakeVisible(iVisible);
       
   188     }
       
   189 }
       
   190 
       
   191 void CMIDCanvasGraphicsItemPainter::SetVisibleL(TBool aVisible)
       
   192 {
       
   193     if (iVisible != aVisible)
       
   194     {
       
   195         if (aVisible)
       
   196         {
       
   197             DEBUG("CMIDCanvasGraphicsItemPainter::SetVisibleL, setting painter visible");
       
   198 
       
   199             // Show the graphics item when it is hidden.
       
   200             MakeVisible(aVisible);
       
   201             iVisible = aVisible;
       
   202             // Paint the control
       
   203             iEnv.PostJavaEvent(*this, ECanvasGraphicsItemPainterEvent,
       
   204                                ECanvasGraphicsItemRepaint, 0);
       
   205         }
       
   206         else if (!aVisible)
       
   207         {
       
   208             DEBUG("CMIDCanvasGraphicsItemPainter::SetVisibleL, setting painter hidden");
       
   209 
       
   210             // Hide the graphics item when it is visible.
       
   211             MakeVisible(aVisible);
       
   212             iVisible = aVisible;
       
   213         }
       
   214     }
       
   215 }
       
   216 
       
   217 // ---------------------------------------------------------------------------
       
   218 // CMIDCanvasGraphicsItem::SetPosition
       
   219 // (other items are commented in the header file)
       
   220 // ---------------------------------------------------------------------------
       
   221 //
       
   222 void CMIDCanvasGraphicsItemPainter::SetPosition(const TInt aX, const TInt aY)
       
   223 {
       
   224     DEBUG("CMIDCanvasGraphicsItemPainter::SetPosition +");
       
   225 
       
   226     //Previous position is moved to new location. New location
       
   227     //is relative to canvas point of origin (0,0).
       
   228     TInt moveX = aX - iContentRect.iTl.iX;
       
   229     TInt moveY = aY - iContentRect.iTl.iY;
       
   230     iContentRect.Move(moveX, moveY);
       
   231 
       
   232     SetRect(iContentRect);
       
   233 
       
   234     if (IsVisible())
       
   235     {
       
   236         iEnv.PostJavaEvent(*this, ECanvasGraphicsItemPainterEvent,
       
   237                            ECanvasGraphicsItemRepaint, 0);
       
   238     }
       
   239 
       
   240     DEBUG("CMIDCanvasGraphicsItemPainter::SetPosition -");
       
   241 }
       
   242 
       
   243 
       
   244 // ---------------------------------------------------------------------------
       
   245 // CMIDCanvasGraphicsItemPainter::SetItemSize
       
   246 // (other items are commented in the header file)
       
   247 // ---------------------------------------------------------------------------
       
   248 //
       
   249 void CMIDCanvasGraphicsItemPainter::SetItemSizeL(
       
   250     const TInt aWidth,
       
   251     const TInt aHeight)
       
   252 {
       
   253     DEBUG_INT2(
       
   254         "CMIDCanvasGraphicsItemPainter::SetItemSize +, aWidth=%d, aHeight=%d",
       
   255         aWidth,
       
   256         aHeight);
       
   257 
       
   258     if (iContentRect.Size().iWidth != aWidth ||
       
   259             iContentRect.Size().iHeight != aHeight)
       
   260     {
       
   261         iContentRect.SetSize(TSize(aWidth, aHeight));
       
   262 
       
   263         SetSize(iContentRect.Size());
       
   264 
       
   265         // Recreate buffers with new size
       
   266         CreateBuffersL();
       
   267 
       
   268         if (IsVisible())
       
   269         {
       
   270             iEnv.PostJavaEvent(*this, ECanvasGraphicsItemPainterEvent,
       
   271                                ESizeChanged, aWidth, aHeight, 0);
       
   272         }
       
   273     }
       
   274 
       
   275     DEBUG("CMIDCanvasGraphicsItemPainter::SetItemSize -");
       
   276 }
       
   277 
       
   278 
       
   279 // ---------------------------------------------------------------------------
       
   280 // CMIDCanvasGraphicsItemPainter::Dispose
       
   281 // (other items are commented in the header file)
       
   282 // ---------------------------------------------------------------------------
       
   283 //
       
   284 void CMIDCanvasGraphicsItemPainter::Dispose()
       
   285 {
       
   286     DEBUG("CMIDCanvasGraphicsItemPainter::Dispose +");
       
   287 
       
   288     delete this;
       
   289 
       
   290     DEBUG("CMIDCanvasGraphicsItemPainter::Dispose -");
       
   291 }
       
   292 
       
   293 // ---------------------------------------------------------------------------
       
   294 // From class MMIDComponent.
       
   295 // CMIDCanvas::Processor
       
   296 // Always returns this-> as buffer processor associated with this component.
       
   297 // ---------------------------------------------------------------------------
       
   298 //
       
   299 MMIDBufferProcessor* CMIDCanvasGraphicsItemPainter::Processor()
       
   300 {
       
   301     return this;
       
   302 }
       
   303 
       
   304 // ---------------------------------------------------------------------------
       
   305 // CMIDCanvasGraphicsItemPainter::MdcContainerWindowRectChanged
       
   306 // (other items are commented in the header file)
       
   307 // ---------------------------------------------------------------------------
       
   308 //
       
   309 
       
   310 void CMIDCanvasGraphicsItemPainter::MdcContainerWindowRectChanged(
       
   311     const TRect& /*aRect*/)
       
   312 {
       
   313     DEBUG("CMIDCanvasGraphicsItemPainter::MdcContainerWindowRectChanged +");
       
   314 
       
   315     // Not used at the moment.
       
   316 
       
   317     DEBUG("CMIDCanvasGraphicsItemPainter::MdcContainerWindowRectChanged -");
       
   318 }
       
   319 
       
   320 // ---------------------------------------------------------------------------
       
   321 // CMIDCanvasGraphicsItemPainter::MdcContainerVisibilityChanged
       
   322 // (other items are commented in the header file)
       
   323 // ---------------------------------------------------------------------------
       
   324 //
       
   325 void CMIDCanvasGraphicsItemPainter::MdcContainerVisibilityChanged(
       
   326     TBool /*aVisible*/)
       
   327 {
       
   328     DEBUG("CMIDCanvasGraphicsItemPainter::MdcContainerVisibilityChanged +");
       
   329 
       
   330     // Not used at the moment.
       
   331 
       
   332     DEBUG("CMIDCanvasGraphicsItemPainter::MdcContainerVisibilityChanged -");
       
   333 }
       
   334 
       
   335 // ---------------------------------------------------------------------------
       
   336 // CMIDCanvasGraphicsItemPainter::MdcContentBoundsChanged
       
   337 // (other items are commented in the header file)
       
   338 // ---------------------------------------------------------------------------
       
   339 //
       
   340 void CMIDCanvasGraphicsItemPainter::MdcContentBoundsChanged(
       
   341     const TRect& /*aRect*/)
       
   342 {
       
   343     DEBUG("CMIDCanvasGraphicsItemPainter::MdcContentBoundsChanged +");
       
   344 
       
   345     // Not used at the moment.
       
   346 
       
   347     DEBUG("CMIDCanvasGraphicsItemPainter::MdcContentBoundsChanged -");
       
   348 }
       
   349 
       
   350 // ---------------------------------------------------------------------------
       
   351 // CMIDCanvasGraphicsItemPainter::MdcItemContentRectChanged
       
   352 // (other items are commented in the header file)
       
   353 // ---------------------------------------------------------------------------
       
   354 //
       
   355 void CMIDCanvasGraphicsItemPainter::MdcItemContentRectChanged(
       
   356     const TRect& /*aContentRect*/,
       
   357     const TRect& /*aScreenRect*/)
       
   358 {
       
   359     DEBUG("CMIDCanvasGraphicsItemPainter::MdcItemContentRectChanged +");
       
   360 
       
   361     // Not used at the moment.
       
   362 
       
   363     DEBUG("CMIDCanvasGraphicsItemPainter::MdcItemContentRectChanged -");
       
   364 }
       
   365 
       
   366 // ---------------------------------------------------------------------------
       
   367 // CMIDCanvasGraphicsItemPainter::MdcContainerDestroyed
       
   368 // (other items are commented in the header file)
       
   369 // ---------------------------------------------------------------------------
       
   370 //
       
   371 void CMIDCanvasGraphicsItemPainter::MdcContainerDestroyed()
       
   372 {
       
   373     DEBUG("CMIDCanvasGraphicsItemPainter::MdcContainerDestroyed +");
       
   374 
       
   375     iDirectContainer = NULL;
       
   376 
       
   377     DEBUG("CMIDCanvasGraphicsItemPainter::MdcContainerDestroyed -");
       
   378 }
       
   379 
       
   380 // ---------------------------------------------------------------------------
       
   381 // CMIDCanvasGraphicsItemPainter::MdcAbortDSA
       
   382 // (other items are commented in the header file)
       
   383 // ---------------------------------------------------------------------------
       
   384 //
       
   385 void CMIDCanvasGraphicsItemPainter::MdcAbortDSA()
       
   386 {
       
   387     DEBUG("CMIDCanvasGraphicsItemPainter::MdcAbortDSA +");
       
   388 
       
   389     // Not used at the moment.
       
   390 
       
   391     DEBUG("CMIDCanvasGraphicsItemPainter::MdcAbortDSA -");
       
   392 }
       
   393 
       
   394 // ---------------------------------------------------------------------------
       
   395 // CMIDCanvasGraphicsItemPainter::MdcResumeDSA
       
   396 // (other items are commented in the header file)
       
   397 // ---------------------------------------------------------------------------
       
   398 //
       
   399 void CMIDCanvasGraphicsItemPainter::MdcResumeDSA()
       
   400 {
       
   401     DEBUG("CMIDCanvasGraphicsItemPainter::MdcResumeDSA +");
       
   402 
       
   403     // Not used at the moment.
       
   404 
       
   405     DEBUG("CMIDCanvasGraphicsItemPainter::MdcResumeDSA -");
       
   406 }
       
   407 
       
   408 // ---------------------------------------------------------------------------
       
   409 // CMIDCanvasGraphicsItemPainter::CreateBuffersL
       
   410 // (other items are commented in the header file)
       
   411 // ---------------------------------------------------------------------------
       
   412 //
       
   413 void CMIDCanvasGraphicsItemPainter::CreateBuffersL()
       
   414 {
       
   415     DEBUG("< CMIDCanvasGraphicsItemPainter::CreateBuffersL");
       
   416     ResetBuffers();
       
   417 
       
   418     TRect rect = TRect(
       
   419                      0,
       
   420                      0,
       
   421                      iContentRect.Size().iWidth,
       
   422                      iContentRect.Size().iHeight);
       
   423 
       
   424     iFrameBuffer = new(ELeave) CFbsBitmap;
       
   425     User::LeaveIfError(iFrameBuffer->Create(rect.Size(), iEnv.DisplayMode()));
       
   426 
       
   427     iFrameDevice = CFbsBitmapDevice::NewL(iFrameBuffer);
       
   428     User::LeaveIfError(iFrameDevice->CreateContext(iFrameContext));
       
   429 
       
   430     DEBUG("> CMIDCanvasGraphicsItemPainter::CreateBuffersL");
       
   431 }
       
   432 
       
   433 // ---------------------------------------------------------------------------
       
   434 // CMIDCanvasGraphicsItemPainter::ResetBuffers
       
   435 // (other items are commented in the header file)
       
   436 // ---------------------------------------------------------------------------
       
   437 //
       
   438 void CMIDCanvasGraphicsItemPainter::ResetBuffers()
       
   439 {
       
   440     delete iFrameBuffer;
       
   441     delete iFrameContext;
       
   442     delete iFrameDevice;
       
   443 
       
   444     iFrameBuffer  = NULL;
       
   445     iFrameContext = NULL;
       
   446     iFrameDevice  = NULL;
       
   447 }
       
   448 
       
   449 // ---------------------------------------------------------------------------
       
   450 // CMIDCanvasGraphicsItemPainter::FrameBuffer
       
   451 // (other items are commented in the header file)
       
   452 // ---------------------------------------------------------------------------
       
   453 //
       
   454 CFbsBitmap* CMIDCanvasGraphicsItemPainter::FrameBuffer() const
       
   455 {
       
   456     return iFrameBuffer;
       
   457 }
       
   458 
       
   459 // ---------------------------------------------------------------------------
       
   460 // CMIDCanvasGraphicsItemPainter::ResetFrameBuffer
       
   461 // (other items are commented in the header file)
       
   462 // ---------------------------------------------------------------------------
       
   463 //
       
   464 void CMIDCanvasGraphicsItemPainter::ResetFrameBuffer(
       
   465     const TPoint& aPosition, const TSize& aSize)
       
   466 {
       
   467     if (iFrameBuffer)
       
   468     {
       
   469         //calculate stride: scanline should have the same width as frame buffer,
       
   470         //size of one pixel is defined by frame buffer display mode
       
   471         TInt stride = iFrameBuffer->ScanLineLength(
       
   472                           iFrameBuffer->SizeInPixels().iWidth, iFrameBuffer->DisplayMode());
       
   473 
       
   474         iFrameBuffer->LockHeap();
       
   475 
       
   476         for (TInt i = aPosition.iY; i < aSize.iHeight; i++)
       
   477         {
       
   478             const void *srcAddr =
       
   479                 ((const char *) iFrameBuffer->DataAddress()) + i * stride;
       
   480             unsigned int *src = (unsigned int *) srcAddr;
       
   481             //starting adress should be shifted to clipping x-position
       
   482             src += aPosition.iX;
       
   483             for (TInt j = aPosition.iX; j < aSize.iWidth; j++)
       
   484             {
       
   485                 //reset pixel to fully transparent
       
   486                 *src++ = 0x00000000;
       
   487             }
       
   488         }
       
   489 
       
   490         iFrameBuffer->UnlockHeap();
       
   491     }
       
   492 }
       
   493 
       
   494 // ---------------------------------------------------------------------------
       
   495 // CMIDCanvasGraphicsItemPainter::Draw
       
   496 // (other items are commented in the header file)
       
   497 // ---------------------------------------------------------------------------
       
   498 //
       
   499 void CMIDCanvasGraphicsItemPainter::Draw(const TRect& /*aRect*/) const
       
   500 {
       
   501     DEBUG("+ CMIDCanvasGraphicsItemPainter::Draw");
       
   502     ASSERT(iFrameBuffer);
       
   503 
       
   504     if (IsVisible())
       
   505     {
       
   506         CWindowGc& gc = SystemGc();
       
   507 
       
   508         gc.BitBlt(iContentRect.iTl, iFrameBuffer);
       
   509     }
       
   510 
       
   511     DEBUG("- CMIDCanvasGraphicsItemPainter::Draw");
       
   512 }
       
   513 
       
   514 // End of file