javauis/eswt_akn/org.eclipse.ercp.swt.s60/native/src/swtcontrolbase.cpp
branchRCL_3
changeset 66 2455ef1f5bbc
child 77 7cee158cb8cd
equal deleted inserted replaced
65:ae942d28ec0e 66:2455ef1f5bbc
       
     1 /*******************************************************************************
       
     2  * Copyright (c) 2005, 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved. This program and the accompanying materials
       
     4  * are made available under the terms of the Eclipse Public License v1.0
       
     5  * which accompanies this distribution, and is available at
       
     6  * http://www.eclipse.org/legal/epl-v10.html
       
     7  *
       
     8  * Contributors:
       
     9  *     Nokia Corporation - S60 implementation
       
    10  *******************************************************************************/
       
    11 
       
    12 
       
    13 #include <biditext.h>
       
    14 #include <aknappui.h>
       
    15 #include <aknnavi.h>
       
    16 #include <aknnavide.h>
       
    17 #include <akntabgrp.h>
       
    18 #include <coecntrl.h>
       
    19 #include <eikon.hrh>
       
    20 #include <AknsDrawUtils.h>
       
    21 #include <swtlaffacade.h>
       
    22 #include <coreuiavkoneswt.h>
       
    23 #include "eswtmobileextensions.h"
       
    24 #include "swtcontrolbase.h"
       
    25 #include "swtuiutils.h"
       
    26 #include "swtjavabufferdrawer.h"
       
    27 
       
    28 
       
    29 // ======== MEMBER FUNCTIONS ========
       
    30 
       
    31 
       
    32 EXPORT_C ASwtControlBase::ASwtControlBase(MSwtDisplay& aDisplay, TSwtPeer aPeer,
       
    33         MSwtComposite* aParent, TInt aStyle)
       
    34         : iDisplay(aDisplay)
       
    35         , iPeer(aPeer)
       
    36         , iParent(aParent)
       
    37         , iCtrlFlags(0)
       
    38         , iStyle(aStyle)
       
    39         , iFont(NULL)
       
    40         , iPopupMenu(NULL)
       
    41         , iIsOnControlStack(EFalse)
       
    42         , iStackedSetRedrawFalseNb(0)
       
    43         , iBackgrdDefaultUsed(ETrue)
       
    44         , iFocusFrame(NULL)
       
    45         , iFocusChanged(EFalse)
       
    46 {
       
    47     ASSERT(iPeer!=NULL);
       
    48 
       
    49     // Orientation not specified, find default
       
    50     TInt orientation = iStyle & KSwtOrientationMask;
       
    51     if (orientation!=KSwtStyleLeftToRight && orientation!=KSwtStyleRightToLeft)
       
    52     {
       
    53         if (iParent != NULL) // Inherit from parent
       
    54             orientation = iParent->Control()->Style() & KSwtOrientationMask;
       
    55         else                 // Based on user's locale
       
    56             orientation = (TBidiText::ScriptDirectionality(User::Language())==TBidiText::ELeftToRight) ?
       
    57                           KSwtStyleLeftToRight : KSwtStyleRightToLeft;
       
    58 
       
    59         iStyle &= ~KSwtOrientationMask;
       
    60         iStyle |= orientation;
       
    61     }
       
    62 }
       
    63 
       
    64 void ASwtControlBase::CreateFocusBackgroundL()
       
    65 {
       
    66     ASSERT(!iFocusFrame);
       
    67 
       
    68     TRect rect(CoeControl().Rect());
       
    69     TRect innerRect;
       
    70     innerRect.iTl = CSwtLafFacade::GetLayoutRect(
       
    71                         CSwtLafFacade::EInputFieldSkinPlacingGeneralLine2, rect, 0).Rect().iBr;
       
    72     innerRect.iBr = CSwtLafFacade::GetLayoutRect(
       
    73                         CSwtLafFacade::EInputFieldSkinPlacingGeneralLine5, rect, 0).Rect().iTl;
       
    74 
       
    75     iFocusFrame = CAknsFrameBackgroundControlContext::NewL(
       
    76                       KAknsIIDQsnFrInput, rect, innerRect, EFalse /*parent absolute*/);
       
    77 
       
    78     if (iBackgrdDefaultUsed)
       
    79     {
       
    80         iFocusFrame->SetParentContext(SwtMopSupplyCtrlContext(EFalse));
       
    81     }
       
    82 }
       
    83 
       
    84 CAknsBasicBackgroundControlContext* ASwtControlBase::FocusBackgroundNow() const
       
    85 {
       
    86     if (HasHighlight(EFalse)) // not checking parents
       
    87     {
       
    88         return iFocusFrame;
       
    89     }
       
    90     else
       
    91     {
       
    92         return NULL;
       
    93     }
       
    94 }
       
    95 
       
    96 void ASwtControlBase::RedrawFocusBackground()
       
    97 {
       
    98     MSwtCaptionedControl* captCtrl = GetNearestCaptionedControl();
       
    99     if (captCtrl)
       
   100     {
       
   101         captCtrl->Composite()->Control()->Redraw();
       
   102     }
       
   103     else
       
   104     {
       
   105         Redraw();
       
   106     }
       
   107 }
       
   108 
       
   109 void ASwtControlBase::InitControlBaseL()
       
   110 {
       
   111     RegisterWithParentL();
       
   112 }
       
   113 
       
   114 EXPORT_C ASwtControlBase::~ASwtControlBase()
       
   115 {
       
   116     delete iFocusFrame;
       
   117 
       
   118     // RemoveRef here in case it goes through destruction before being
       
   119     // registered (and Dispose() is not called)
       
   120     if (iFont != NULL)
       
   121         iFont->RemoveRef();
       
   122 
       
   123 #ifdef _lint
       
   124     //lint -esym(1740, ASwtControlBase::iPeer)
       
   125     //lint -esym(1740, ASwtControlBase::iParent)
       
   126     iFont      = NULL;
       
   127     iPopupMenu = NULL;
       
   128 #endif
       
   129 }
       
   130 
       
   131 // From class MCoeControlBackground.
       
   132 // Draw the Background. This method was called by the eSWT and Symbian drawing framework.
       
   133 // If the control is a Composite then make sure not to draw the background to
       
   134 // the areas that will be painted by the children. This is to avoid painting
       
   135 // over them as this would cause flickering.
       
   136 //
       
   137 EXPORT_C void ASwtControlBase::Draw(CWindowGc& aGc, const CCoeControl& aControl,
       
   138                                     const TRect& aRect) const
       
   139 {
       
   140     RRegion* clippingRegion = NULL;
       
   141     const MSwtComposite* composite = CompositeInterface();
       
   142     if (composite)
       
   143     {
       
   144         clippingRegion = SetupChildClippingRegion(composite, aGc, aRect);
       
   145     }
       
   146 
       
   147     TBool isDialogShell = IsShell() && iParent;
       
   148     TBool isTaskTip = isDialogShell && ShellInterface()->IsTaskTip();
       
   149 
       
   150     // Draw the background.
       
   151     if (!IsDefaultBackgroundUse()
       
   152 #ifdef RD_JAVA_S60_RELEASE_9_2
       
   153             // Fill the round corners
       
   154             || (isDialogShell && !isTaskTip)
       
   155 #endif // RD_JAVA_S60_RELEASE_9_2
       
   156        )
       
   157     {
       
   158         aGc.SetBrushColor(GetBackground());
       
   159         aGc.Clear(aRect);
       
   160     }
       
   161 
       
   162     // Hopefully the highlight back is always semi transparent,
       
   163     // otherwise the back color is lost.
       
   164     MAknsControlContext* cc = SwtMopSupplyCtrlContext();
       
   165     AknsDrawUtils::Background(AknsUtils::SkinInstance(), cc, &aControl, aGc, aRect);
       
   166 
       
   167     // Cancel & destroy the clipping region
       
   168     CancelChildClippingRegion(clippingRegion, aGc);
       
   169 }
       
   170 
       
   171 EXPORT_C void ASwtControlBase::DrawBorder(const TRect& /*aRect*/) const
       
   172 {
       
   173     TInt borderWidth = GetBorderWidth();
       
   174     if (borderWidth > 0)
       
   175     {
       
   176         CWindowGc& gc = CoeControl().SystemGc();
       
   177         gc.SetBrushStyle(CGraphicsContext::ENullBrush);
       
   178         gc.SetPenColor(UiUtils().GetSystemColor(ESwtColorWidgetBorder));
       
   179         gc.SetPenStyle(CGraphicsContext::ESolidPen);
       
   180         gc.SetPenSize(TSize(borderWidth, borderWidth));
       
   181         gc.DrawRect(CoeControl().Rect());
       
   182     }
       
   183 }
       
   184 
       
   185 // In eSWT all border should have the same width/height.
       
   186 EXPORT_C TInt ASwtControlBase::GetBorderWidth() const
       
   187 {
       
   188     if (HasBorderStyle())
       
   189     {
       
   190         return 1;
       
   191     }
       
   192     else
       
   193     {
       
   194         return 0;
       
   195     }
       
   196 }
       
   197 
       
   198 // Window coords
       
   199 EXPORT_C TRect ASwtControlBase::ClientRect() const
       
   200 {
       
   201     return BorderInnerRect();
       
   202 }
       
   203 
       
   204 TBool ASwtControlBase::HasBorderStyle() const
       
   205 {
       
   206     TBool res = (iStyle & KSwtStyleBorder);
       
   207     const MSwtShell* shell = ShellInterface();
       
   208     if (shell)
       
   209     {
       
   210         // TaskTip shells have the border.
       
   211         res = shell->IsTaskTip() || (res && !(iStyle & KSwtStyleNoTrim) && iParent);
       
   212     }
       
   213     return res;
       
   214 }
       
   215 
       
   216 TSize ASwtControlBase::BorderSizeDelta() const
       
   217 {
       
   218     return TSize(GetBorderWidth()*2, GetBorderWidth()*2) ;
       
   219 }
       
   220 
       
   221 // Window coords
       
   222 EXPORT_C TRect ASwtControlBase::BorderInnerRect() const
       
   223 {
       
   224     TRect borderInnerRect = CoeControl().Rect();
       
   225 
       
   226     if (borderInnerRect.Height() != 0 && borderInnerRect.Width() != 0)
       
   227     {
       
   228         TInt w(GetBorderWidth());
       
   229         borderInnerRect.Shrink(w, w);
       
   230     }
       
   231 
       
   232     return borderInnerRect;
       
   233 }
       
   234 
       
   235 EXPORT_C TRect ASwtControlBase::BorderOuterRect(const TRect& aRect) const
       
   236 {
       
   237     TRect borderOuterRect(aRect);
       
   238     borderOuterRect.Grow(GetBorderWidth(), GetBorderWidth());
       
   239 
       
   240     return borderOuterRect;
       
   241 }
       
   242 
       
   243 TKeyResponse ASwtControlBase::HandleDiagonalKeysL(const TKeyEvent& aKeyEvent, TEventCode aType,
       
   244         TBool aTraversalDoIt, const TUint key1, const TStdScanCode scanCode1,
       
   245         const TUint key2, const TStdScanCode scanCode2)
       
   246 {
       
   247     TKeyEvent keyTemp;
       
   248     // send first event
       
   249     keyTemp.iRepeats = 0;
       
   250     keyTemp.iScanCode = scanCode1;
       
   251     keyTemp.iModifiers = 32768;
       
   252     keyTemp.iCode = 0;
       
   253     HandleKeyL(keyTemp, EEventKeyDown, aTraversalDoIt);     //DOWN
       
   254     keyTemp = aKeyEvent;
       
   255     keyTemp.iCode = key1;
       
   256     keyTemp.iScanCode = scanCode1;
       
   257     TKeyResponse firstResponse = HandleKeyL(keyTemp, aType, aTraversalDoIt);     // KEYCODE
       
   258     keyTemp.iRepeats = 0;
       
   259     keyTemp.iScanCode = scanCode1;
       
   260     keyTemp.iModifiers = 32768;
       
   261     keyTemp.iCode = 0;
       
   262     HandleKeyL(keyTemp, EEventKeyUp, aTraversalDoIt);      // UP
       
   263 
       
   264     // send second event
       
   265     keyTemp.iRepeats = 0;
       
   266     keyTemp.iScanCode = scanCode2;
       
   267     keyTemp.iModifiers = 32768;
       
   268     keyTemp.iCode = 0;
       
   269     HandleKeyL(keyTemp, EEventKeyDown, aTraversalDoIt);     // DOWN
       
   270     keyTemp = aKeyEvent;
       
   271     keyTemp.iCode = key2;
       
   272     keyTemp.iScanCode = scanCode2;
       
   273     TKeyResponse secondResponse = HandleKeyL(keyTemp, aType, aTraversalDoIt);     // KEYCODE
       
   274     keyTemp.iRepeats = 0;
       
   275     keyTemp.iScanCode = scanCode2;
       
   276     keyTemp.iModifiers = 32768;
       
   277     keyTemp.iCode = 0;
       
   278     HandleKeyL(keyTemp, EEventKeyUp, aTraversalDoIt);       // UP
       
   279 
       
   280     TKeyResponse response = EKeyWasNotConsumed;
       
   281     if (EKeyWasConsumed == firstResponse && EKeyWasConsumed == secondResponse)
       
   282     {
       
   283         response = EKeyWasConsumed;
       
   284     }
       
   285 
       
   286     return response;
       
   287 }
       
   288 
       
   289 void ASwtControlBase::DrawComponentControls(const CCoeControl& aControl,
       
   290         const TRect& aRect, CWindowGc& aGc, TBool aCanDrawOutsideRect)
       
   291 {
       
   292     const TInt count = aControl.CountComponentControls();
       
   293     for (TInt i=0; i<count; ++i)
       
   294     {
       
   295         CCoeControl& compCtrl = *aControl.ComponentControl(i);
       
   296         if (compCtrl.IsVisible())
       
   297         {
       
   298             if (compCtrl.OwnsWindow())
       
   299             {
       
   300             }
       
   301             else
       
   302             {
       
   303                 TRect drawRect(compCtrl.Rect());
       
   304                 drawRect.Intersection(aRect);
       
   305                 if (!drawRect.IsEmpty())
       
   306                 {
       
   307                     aGc.Reset();
       
   308                     aGc.SetClippingRect(drawRect);
       
   309                     compCtrl.DrawBackground(drawRect);
       
   310                     compCtrl.DrawForeground(drawRect);
       
   311                 }
       
   312 
       
   313                 if (aCanDrawOutsideRect)
       
   314                 {
       
   315                     DrawComponentControls(compCtrl, aRect, aGc, aCanDrawOutsideRect);
       
   316                 }
       
   317                 else if (!drawRect.IsEmpty())
       
   318                 {
       
   319                     DrawComponentControls(compCtrl, drawRect, aGc, aCanDrawOutsideRect);
       
   320                 }
       
   321             }
       
   322         }
       
   323     }
       
   324 }
       
   325 
       
   326 TBool ASwtControlBase::RectContainsRect(const TRect& aContainer, const TRect& aContainee)
       
   327 {
       
   328     return (aContainer.iTl.iX <= aContainee.iTl.iX
       
   329             && aContainer.iTl.iY <= aContainee.iTl.iY
       
   330             && aContainer.iBr.iX >= aContainee.iBr.iX
       
   331             && aContainer.iBr.iY >= aContainee.iBr.iY);
       
   332 }
       
   333 
       
   334 void ASwtControlBase::FlushJavaBuffer(MSwtGc* aGc, TInt* aIntParams, TInt aIntCount, HBufC* aStrParams, RWindow* aWindow, const TRect& aDrawRect) const
       
   335 {
       
   336     iDisplay.JavaBufferDrawer().Draw(aGc, aIntParams, aIntCount, aStrParams, aWindow, aDrawRect);
       
   337 }
       
   338 
       
   339 EXPORT_C void ASwtControlBase::PostMouseEventL(const TPointerEvent& aPointerEvent)
       
   340 {
       
   341     TPoint eventPosition(aPointerEvent.iPosition);
       
   342 
       
   343     // Is is essential that the coordinates posted to the queue are in screen
       
   344     // coordinates to ensure the case where late mouse events are handled
       
   345     // after the control has moved away from the current position.
       
   346     TPoint pos(WindowToScreen(eventPosition));
       
   347 
       
   348     // Compute the state mask
       
   349     TInt stateMask = UiUtils().GetPointerEventStateMask(aPointerEvent.iModifiers);
       
   350 
       
   351     // Handle double click (must be issued between Down and Up)
       
   352     if (aPointerEvent.iModifiers & EModifierDoubleClick)
       
   353         iDisplay.PostMouseEventL(iPeer, ESwtEventMouseDoubleClick, 0, pos, stateMask);
       
   354 
       
   355     // Compute the event type and button number
       
   356     TSwtEventType type;
       
   357     TInt          button = 0;
       
   358     switch (aPointerEvent.iType)
       
   359     {
       
   360     case TPointerEvent::EButton1Down:
       
   361         type   = ESwtEventMouseDown;
       
   362         button = KSwtMouseButton1;
       
   363         break;
       
   364     case TPointerEvent::EButton1Up:
       
   365         type   = ESwtEventMouseUp;
       
   366         button = KSwtMouseButton1;
       
   367         break;
       
   368     case TPointerEvent::EButton2Down:
       
   369         type   = ESwtEventMouseDown;
       
   370         button = KSwtMouseButton2;
       
   371         break;
       
   372     case TPointerEvent::EButton2Up:
       
   373         type   = ESwtEventMouseUp;
       
   374         button = KSwtMouseButton2;
       
   375         break;
       
   376     case TPointerEvent::EButton3Down:
       
   377         type   = ESwtEventMouseDown;
       
   378         button = KSwtMouseButton3;
       
   379         break;
       
   380     case TPointerEvent::EButton3Up:
       
   381         type   = ESwtEventMouseUp;
       
   382         button = KSwtMouseButton3;
       
   383         break;
       
   384     case TPointerEvent::EDrag:
       
   385     case TPointerEvent::EMove:
       
   386         type = ESwtEventMouseMove;
       
   387         break;
       
   388     default:
       
   389         return;
       
   390     }
       
   391 
       
   392     iDisplay.PostMouseEventL(iPeer, type, button, pos, stateMask);
       
   393 }
       
   394 
       
   395 EXPORT_C void ASwtControlBase::EnableFocusHighlight(TBool /*aEnable*/)
       
   396 {
       
   397     // Method is used only on 9.2 for list controls (Lists, ListView,
       
   398     // ListBox and Table)
       
   399 }
       
   400 
       
   401 TPoint ASwtControlBase::ComputeLocationToSet(const TPoint& aLocation)
       
   402 {
       
   403     ASSERT(!IsShell());
       
   404     return aLocation + iParent->Scrollable()->Control()->ClientRect().iTl; //lint !e613
       
   405 }
       
   406 
       
   407 EXPORT_C MSwtShell& ASwtControlBase::GetShell() const
       
   408 {
       
   409     if (!iShell)
       
   410     {
       
   411         MSwtShell* selfAsShell = const_cast<MSwtShell*>(ShellInterface());
       
   412         if (selfAsShell)
       
   413             iShell = selfAsShell;
       
   414         else
       
   415             iShell = &iParent->Control()->GetShell();
       
   416     }
       
   417     ASSERT(iShell);
       
   418     return *iShell;
       
   419 }
       
   420 
       
   421 void ASwtControlBase::Invalidate(const TRect& aRect)
       
   422 {
       
   423     if (!aRect.IsEmpty() && CoeControl().IsVisible() && !(iCtrlFlags & MSwtControl::EFlagDoNotDraw))
       
   424         ForceInvalidate(aRect);
       
   425 }
       
   426 
       
   427 void ASwtControlBase::ForceInvalidate(const TRect& aRect)
       
   428 {
       
   429     if (aRect.IsEmpty())
       
   430     {
       
   431         return;
       
   432     }
       
   433 
       
   434     MSwtShell& shell = GetShell();
       
   435     MSwtControl* urgentPaintControl = shell.UrgentPaintControl();
       
   436     if (urgentPaintControl)
       
   437     {
       
   438         // Redraw requests outside urgently painted controls are ignored.
       
   439         if (IsDescentOf(*urgentPaintControl))
       
   440         {
       
   441             urgentPaintControl->PaintUrgently(aRect);
       
   442         }
       
   443     }
       
   444     else
       
   445     {
       
   446         if (shell.AsyncPainting())
       
   447         {
       
   448             TRect dirtyRect(aRect);
       
   449             MSwtControl* controlShell = shell.Control();
       
   450 
       
   451             // Convert to Screen Coordinate
       
   452             dirtyRect.Move(controlShell->CoeControl().PositionRelativeToScreen());
       
   453 
       
   454             TBool mergeable = !(iStyle & KSwtStyleNoMergePaints);
       
   455             TRAP_IGNORE(iDisplay.PostPaintEventL(JavaPeer(), controlShell->JavaPeer(), dirtyRect, mergeable));
       
   456         }
       
   457         else
       
   458         {
       
   459             static_cast<RWindow*>(shell.Control()->CoeControl().DrawableWindow())->Invalidate(aRect);
       
   460         }
       
   461     }
       
   462 }
       
   463 
       
   464 EXPORT_C void ASwtControlBase::DoPaint(const TRect& aRect) const
       
   465 {
       
   466     ASSERT(!IsShell());
       
   467     const CCoeControl& ctrl = CoeControl();
       
   468     ctrl.DrawBackground(aRect);
       
   469     ctrl.DrawForeground(aRect);
       
   470 }
       
   471 
       
   472 EXPORT_C void ASwtControlBase::SetColorL(TLogicalColor aLogicalColor, const MSwtColor* aColor)
       
   473 {
       
   474     CCoeControl& coeCtrl = CoeControl();
       
   475     TRgb color((aColor!=NULL) ? aColor->RgbValue() : iDisplay.CoeEnv()->Color(aLogicalColor));
       
   476     coeCtrl.OverrideColorL(aLogicalColor, color);
       
   477 }
       
   478 
       
   479 EXPORT_C CSwtUiUtils& ASwtControlBase::UiUtils() const
       
   480 {
       
   481     return static_cast<CSwtUiUtils&>(iDisplay.UiUtils());
       
   482 }
       
   483 
       
   484 EXPORT_C void ASwtControlBase::DoDelete()
       
   485 {
       
   486     delete this;
       
   487 }
       
   488 
       
   489 EXPORT_C TRect ASwtControlBase::VisibleRect(TBool aVisibleBounds) const
       
   490 {
       
   491     // WINDOW COORDINATES!
       
   492     if (!CoeControl().IsVisible())
       
   493         return TRect(); // This is (0,0,0,0)
       
   494 
       
   495     TRect rect((aVisibleBounds)? CoeControl().Rect() : ClientRect());
       
   496 
       
   497     if (!IsShell())
       
   498     {
       
   499         rect = ClipToVisibleRect(rect);
       
   500     }
       
   501 
       
   502     return rect;
       
   503 }
       
   504 
       
   505 EXPORT_C TBool ASwtControlBase::HasHighlight(TBool aIncludingParents /*= ETrue*/) const
       
   506 {
       
   507     TBool res = iHighlightEnabled;
       
   508     if (!res && aIncludingParents)
       
   509     {
       
   510         res = res || (iParent && iParent->Control()->HasHighlight(ETrue));
       
   511     }
       
   512     return res;
       
   513 }
       
   514 
       
   515 EXPORT_C void ASwtControlBase::SetHighlight(TBool aEnabled)
       
   516 {
       
   517     iHighlightEnabled = aEnabled;
       
   518 
       
   519     // Prepare the frame
       
   520     if (aEnabled && !iFocusFrame)
       
   521     {
       
   522         TRAP_IGNORE(const_cast<ASwtControlBase*>(this)->CreateFocusBackgroundL());
       
   523     }
       
   524 
       
   525     HandleHighlightChange();
       
   526 }
       
   527 
       
   528 EXPORT_C void ASwtControlBase::HandleHighlightChange()
       
   529 {
       
   530     // Do nothing here. Inherit where needed.
       
   531 }
       
   532 
       
   533 EXPORT_C void ASwtControlBase::PrepareForTraverse()
       
   534 {
       
   535     // Do nothing here. Inherit where needed.
       
   536 }
       
   537 
       
   538 TRect ASwtControlBase::ClipToVisibleRect(const TRect& aRect) const
       
   539 {
       
   540     // WINDOW COORDINATES!
       
   541     TRect rect = aRect;
       
   542     MSwtComposite* composite = iParent;
       
   543     while (composite != NULL)
       
   544     {
       
   545         MSwtControl* ctrl = composite->Control();
       
   546         rect.Intersection(ctrl->ClientRect());
       
   547         composite = (!ctrl->IsShell()) ? ctrl->GetParent() : NULL;
       
   548     }
       
   549     return rect;
       
   550 }
       
   551 
       
   552 // Hurts the performance of non overlapping drawing when there are a lot
       
   553 // of control sibblings and does not really work for overlapping drawing
       
   554 #if 0
       
   555 void ASwtControlBase::VisibleRegion(RRegion& aRegion, TBool aVisibleBounds) const
       
   556 {
       
   557     // SCREEN COORDINATES!
       
   558     aRegion.AddRect(TRect(CoeControl().PositionRelativeToScreen(),
       
   559                           aVisibleBounds ? CoeControl().Size() : ClientRect().Size()));
       
   560     MSwtComposite* parent = iParent;
       
   561     const MSwtControl*  children = this;
       
   562 
       
   563     while (parent && !children->IsShell())
       
   564     {
       
   565         const RSwtControlArray* siblings = parent->Children();
       
   566 
       
   567         for (TInt i = siblings->Find(children)-1; i >= 0; i--)
       
   568         {
       
   569             MSwtControl* control = (*siblings)[i];
       
   570 
       
   571             if (!control->IsShell() && control->CoeControl().IsVisible())
       
   572             {
       
   573                 aRegion.SubRect(TRect(control->CoeControl().PositionRelativeToScreen(), control->CoeControl().Size()));
       
   574             }
       
   575         }
       
   576 
       
   577         children = parent->Control();
       
   578         parent = parent->Control()->GetParent();
       
   579     }
       
   580 }
       
   581 #endif
       
   582 
       
   583 TPoint ASwtControlBase::WindowToScreen(const TPoint& aPoint) const
       
   584 {
       
   585     const CCoeControl& coeControl = CoeControl();
       
   586     const TPoint posInScreen(coeControl.PositionRelativeToScreen());
       
   587     const TPoint posInWindow((coeControl.OwnsWindow()) ? TPoint(0,0) : coeControl.Position());
       
   588 
       
   589     TInt x = (aPoint.iX - posInWindow.iX) + posInScreen.iX;
       
   590     TInt y = (aPoint.iY - posInWindow.iY) + posInScreen.iY;
       
   591     return TPoint(x,y);
       
   592 }
       
   593 
       
   594 void ASwtControlBase::FocusabilityChanged()
       
   595 {
       
   596     CCoeControl& coeCtrl = CoeControl();
       
   597     MSwtShell& shell = GetShell();
       
   598 
       
   599     if (!IsFocusable(EFalse))
       
   600     {
       
   601         if (coeCtrl.IsFocused())
       
   602         {
       
   603             // Composite losing focusability can mean that it has received a new child.
       
   604             // Otherwise it means that the Composite itself has been disabled or hidden.
       
   605             // If a Control loses focusability, let the parent find a new focus target.
       
   606             // If a Composite loses focusability, find new target first in children then ancestors.
       
   607             MSwtControl* newFocus = NULL;
       
   608             MSwtComposite* comp = CompositeInterface();
       
   609             if (comp)
       
   610             {
       
   611                 TRAP_IGNORE((newFocus = shell.FindTraversalTargetL(ESwtTraverseArrowNext, *this)));
       
   612             }
       
   613             if (!newFocus)
       
   614             {
       
   615                 TRAP_IGNORE((newFocus = shell.FindTraversalTargetL(ESwtTraverseParent, *this)));
       
   616             }
       
   617             if (newFocus)
       
   618             {
       
   619                 newFocus->CoeControl().SetFocus(ETrue);
       
   620             }
       
   621             else
       
   622             {
       
   623                 // This control was focused but now there's nothing focusable anymore
       
   624                 coeCtrl.SetFocus(EFalse);
       
   625                 shell.SetControlGainingFocus(NULL);
       
   626                 shell.SetControlGoingToStack(NULL);
       
   627                 if (UiUtils().NextFocusedControl() == this)
       
   628                 {
       
   629                     UiUtils().SetNextFocusedControl(NULL);
       
   630                 }
       
   631             }
       
   632         }
       
   633         ASSERT(!coeCtrl.IsFocused());
       
   634     }
       
   635     else
       
   636     {
       
   637         if (UiUtils().GetActiveShell() == &shell && shell.FocusControl() == NULL)
       
   638             coeCtrl.SetFocus(ETrue);
       
   639     }
       
   640 }
       
   641 
       
   642 EXPORT_C void ASwtControlBase::HandleFocusChanged(TDrawNow /*aDrawNow*/)
       
   643 {
       
   644     CCoeControl&  coeCtrl     = CoeControl();
       
   645     TBool         focused     = coeCtrl.IsFocused();
       
   646     CSwtUiUtils&  uiUtils     = UiUtils();
       
   647     MSwtShell*    activeShell = uiUtils.GetActiveShell();
       
   648     MSwtControl*  oldFocus    = (activeShell!=NULL) ? activeShell->FocusControl() : NULL;
       
   649     TBool         appFocus    = uiUtils.IsAppFocused();
       
   650 
       
   651     // Detect when SetFocus() has been called twice with the same parameter
       
   652     if (oldFocus && ((focused && oldFocus == this)
       
   653                      || (!focused && oldFocus != this)))
       
   654     {
       
   655         return;
       
   656     }
       
   657 
       
   658     MSwtShell& shell = GetShell();
       
   659 
       
   660     // Make sure that UiUtils' information of the control that most
       
   661     // recently had focus is up-to-date. This way the focus will be
       
   662     // returned to the correct control once we regain application
       
   663     // focus after losing it.
       
   664     if (oldFocus == this)
       
   665     {
       
   666         uiUtils.SetNextFocusedControl(oldFocus);
       
   667     }
       
   668 
       
   669     // if app fails on next line check this issue:
       
   670     // caller should call MSwtControl::SetFocus() or MSwtControl::ForceFocus()
       
   671     // or Overridden function if posssible,
       
   672     // instead of CCoeControl::SetFoucus directly
       
   673     ASSERT(!(focused && !appFocus));
       
   674 
       
   675     if (!focused)
       
   676     {
       
   677         // Remove current control from ControlStack since the control becomes unfocused
       
   678         RemoveFromControlStack();
       
   679     }
       
   680 
       
   681     // Update the focus control
       
   682     if (focused)
       
   683     {
       
   684         // Clear the urgent paint control if focus goes outside it.
       
   685         // There cannot be an urgently painted control that is not
       
   686         // focused or does not have a focused child.
       
   687         MSwtControl* urgentPaintControl = shell.UrgentPaintControl();
       
   688         if (urgentPaintControl && urgentPaintControl != this
       
   689                 && !IsDescentOf(*urgentPaintControl))
       
   690         {
       
   691             shell.SetUrgentPaintControl(NULL);
       
   692         }
       
   693 
       
   694         // Activate our shell and remove focus from current owner
       
   695         if (activeShell != &shell)
       
   696         {
       
   697             // This removes focus from oldFocus
       
   698             uiUtils.SetActiveShell(shell, EFalse);
       
   699         }
       
   700         else
       
   701         {
       
   702             if (oldFocus)
       
   703             {
       
   704                 // first add control before removing the control from controlstack
       
   705                 // so that it stop calling focusChanged of shell.
       
   706                 TRAP_IGNORE(AddToControlStackL(ECoeStackPriorityDefault));
       
   707                 oldFocus->CoeControl().SetFocus(EFalse);
       
   708             }
       
   709         }
       
   710 
       
   711         ASSERT(!oldFocus || !oldFocus->CoeControl().IsFocused());
       
   712 
       
   713         // Set ourselves as the new focus control
       
   714         // Can't register a Shell with itself
       
   715         if (shell.Control() != this)
       
   716         {
       
   717             shell.SetFocusControl(this);
       
   718         }
       
   719     }
       
   720     else
       
   721     {
       
   722         shell.SetFocusControl(NULL);
       
   723     }
       
   724 
       
   725     // Notify parent of our new focus
       
   726     MSwtComposite* parent = GetParent();
       
   727     if (parent)
       
   728     {
       
   729         parent->ChildFocusChanged(*this);
       
   730     }
       
   731 
       
   732 
       
   733     // Notify Menu arranger
       
   734     if (focused)
       
   735     {
       
   736         iDisplay.MenuArranger().ControlFocusGained(*this);
       
   737     }
       
   738     else
       
   739     {
       
   740         iDisplay.MenuArranger().ControlFocusLost();
       
   741     }
       
   742 
       
   743     // Notify Command arranger
       
   744     MSwtCommandArranger* commandArranger = iDisplay.CommandArranger();
       
   745     if (commandArranger)   // commandArranger may be null as Mobile Extensions is optional
       
   746     {
       
   747         if (focused)
       
   748         {
       
   749             commandArranger->ControlFocusGained(*this);
       
   750         }
       
   751         else
       
   752         {
       
   753             commandArranger->ControlFocusLost();
       
   754         }
       
   755     }
       
   756 
       
   757     // Post the focus event
       
   758     TSwtEventType type((focused) ? ESwtEventFocusIn : ESwtEventFocusOut);
       
   759     TRAP_IGNORE(iDisplay.PostFocusEventL(JavaPeer(), type));
       
   760 
       
   761     if (focused)
       
   762     {
       
   763         // Add to ControlStack since the control becomes focused
       
   764         TRAP_IGNORE(AddToControlStackL(ECoeStackPriorityDefault));
       
   765     }
       
   766 
       
   767     shell.UpdateHighlight();
       
   768 }
       
   769 
       
   770 TKeyResponse ASwtControlBase::OfferKeyEventToCommandAndMenuArrangersL(
       
   771     const TKeyEvent& aKeyEvent,
       
   772     TEventCode aType)
       
   773 {
       
   774     if ((aType == EEventKeyDown) || (aType==EEventKeyUp))
       
   775     {
       
   776         return EKeyWasNotConsumed;
       
   777     }
       
   778 
       
   779     MSwtShell* activeTopShell = iDisplay.UiUtils().GetParentTopShellOfActiveShell();
       
   780     MSwtMenuArranger& menuArranger = iDisplay.MenuArranger();
       
   781     if (activeTopShell
       
   782             && activeTopShell->FullScreenMode()
       
   783             && menuArranger.IsOptionsCommandAdded())
       
   784     {
       
   785         if (aKeyEvent.iCode == EKeyCBA1 || aKeyEvent.iCode == EKeyCBA2)
       
   786         {
       
   787             menuArranger.TryDisplayMenuBarL(EFalse);
       
   788             return EKeyWasConsumed;
       
   789         }
       
   790     }
       
   791 
       
   792     // If the OK and backspace keys are not used by the control, HandleKeyL() returns EKeyWasNotConsumed.
       
   793     // Thus, this allows the command arranger calling the operations associated to these keys
       
   794     MSwtCommandArranger* commandArranger = iDisplay.CommandArranger();
       
   795     if (commandArranger != NULL)
       
   796     {
       
   797         // Clear command is called when backspace key is pressed
       
   798         if (aKeyEvent.iCode == EKeyBackspace &&
       
   799                 !IsKeyUsed(EKeyBackspace) &&
       
   800                 commandArranger->IsClearOperationSet())
       
   801         {
       
   802             if (commandArranger->DoClearOperationL())
       
   803             {
       
   804                 return EKeyWasConsumed;
       
   805             }
       
   806         }
       
   807         else if (!AknLayoutUtils::MSKEnabled() ||
       
   808                  (activeTopShell && activeTopShell->FullScreenMode()))
       
   809         {
       
   810             // When the selection key is pressed, the default command is called otherwise
       
   811             // a menu with the OK and SELECT commands is displayed
       
   812             if (aKeyEvent.iCode == EKeyOK
       
   813                     && !IsKeyUsed(EKeyOK)
       
   814                     && commandArranger->IsContextSensitiveOperationSet())
       
   815             {
       
   816                 if (commandArranger->DoContextSensitiveOperationL())
       
   817                 {
       
   818                     return EKeyWasConsumed;
       
   819                 }
       
   820             }
       
   821         }
       
   822         else if (aKeyEvent.iCode == EKeyEnter && !IsKeyUsed(EKeyEnter))
       
   823         {
       
   824             if (commandArranger->HandleEnterKeyL())
       
   825             {
       
   826                 return EKeyWasConsumed;
       
   827             }
       
   828         }
       
   829     }
       
   830 
       
   831     return EKeyWasNotConsumed;
       
   832 }
       
   833 
       
   834 TKeyResponse ASwtControlBase::HandleKeyL(const TKeyEvent& aKeyEvent,
       
   835         TEventCode aType, TBool aTraversalDoIt)
       
   836 {
       
   837     // Follows implementation of diagonal Keys. Native diagonal keys are in eSWT
       
   838     // replaced by two corresponding key events check if key is diagonal, if yes,
       
   839     // change the traversalDoIt state
       
   840     if (aKeyEvent.iCode == EKeyLeftUpArrow
       
   841             || aKeyEvent.iCode == EKeyRightUpArrow
       
   842             || aKeyEvent.iCode == EKeyLeftDownArrow
       
   843             || aKeyEvent.iCode == EKeyRightDownArrow)
       
   844     {
       
   845         aTraversalDoIt = EFalse;
       
   846     }
       
   847 
       
   848     switch (aKeyEvent.iCode)
       
   849     {
       
   850     case EKeyLeftUpArrow:
       
   851         return HandleDiagonalKeysL(aKeyEvent, aType, aTraversalDoIt, EKeyLeftArrow,
       
   852                                    EStdKeyLeftArrow, EKeyUpArrow, EStdKeyUpArrow);
       
   853     case EKeyLeftDownArrow:
       
   854         return HandleDiagonalKeysL(aKeyEvent, aType, aTraversalDoIt, EKeyLeftArrow,
       
   855                                    EStdKeyLeftArrow, EKeyDownArrow, EStdKeyDownArrow);
       
   856     case EKeyRightUpArrow:
       
   857         return HandleDiagonalKeysL(aKeyEvent, aType, aTraversalDoIt, EKeyRightArrow,
       
   858                                    EStdKeyRightArrow, EKeyUpArrow, EStdKeyUpArrow);
       
   859     case EKeyRightDownArrow:
       
   860         return HandleDiagonalKeysL(aKeyEvent, aType, aTraversalDoIt, EKeyRightArrow,
       
   861                                    EStdKeyRightArrow, EKeyDownArrow, EStdKeyDownArrow);
       
   862     }
       
   863 
       
   864     // Offer key event to command arranger first
       
   865     TKeyResponse keyResponse = OfferKeyEventToCommandAndMenuArrangersL(aKeyEvent, aType);
       
   866     if (keyResponse == EKeyWasConsumed)
       
   867     {
       
   868         iTraverse = EFalse;
       
   869         return EKeyWasConsumed;
       
   870     }
       
   871 
       
   872     // Traversal only on key event, not key down nor key up
       
   873     if (aType != EEventKey)
       
   874     {
       
   875         if (!iTraverse
       
   876 
       
   877 #ifndef RD_JAVA_S60_RELEASE_9_2
       
   878                 && !(AknLayoutUtils::MSKEnabled()
       
   879                      && aKeyEvent.iScanCode == EStdKeyDevice3
       
   880                      && iDisplay.CommandArranger()->IsDefaultCommandSet())
       
   881 #endif
       
   882            )
       
   883         {
       
   884             iDisplay.PostKeyEventL(*this, aKeyEvent, aType);
       
   885         }
       
   886         iTraverse = EFalse;
       
   887         return EKeyWasConsumed;
       
   888     }
       
   889 
       
   890     // In any case cancel the traverse state
       
   891     iTraverse = EFalse;
       
   892 
       
   893     TSwtTraversal detail(ESwtTraverseNone);
       
   894 
       
   895     const TInt modifiers = aKeyEvent.iModifiers & ECtrlFuncShiftMask;
       
   896 
       
   897     // If we press left or right arrow key and if there is a TabGroup displayed,
       
   898     // we let the TabGroup manage the keys.
       
   899     if (aTraversalDoIt && modifiers == 0
       
   900             && (aKeyEvent.iCode == EKeyRightArrow || aKeyEvent.iCode == EKeyLeftArrow))
       
   901     {
       
   902         keyResponse = OfferKeyEventToTabGroupL(aKeyEvent, aType);
       
   903         if (keyResponse == EKeyWasConsumed)
       
   904         {
       
   905             return EKeyWasConsumed;
       
   906         }
       
   907     }
       
   908 
       
   909     // Tab
       
   910     if (aKeyEvent.iCode == EKeyTab)
       
   911     {
       
   912         switch (modifiers)
       
   913         {
       
   914         case 0:                                // Tab
       
   915         case EModifierCtrl:                    // Ctrl+Tab
       
   916             detail = ESwtTraverseTabNext;
       
   917             break;
       
   918         case EModifierShift:                   // Shift+Tab
       
   919         case EModifierCtrl|EModifierShift:     // Ctrl+Shift+Tab
       
   920             detail = ESwtTraverseTabPrevious;
       
   921             break;
       
   922         default:
       
   923             break;
       
   924         }
       
   925     }
       
   926     else if (aKeyEvent.iCode == EKeyRightArrow ||
       
   927              aKeyEvent.iCode == EKeyDownArrow)
       
   928     {
       
   929         detail = ESwtTraverseArrowNext;
       
   930     }
       
   931     else if (aKeyEvent.iCode == EKeyLeftArrow ||
       
   932              aKeyEvent.iCode == EKeyUpArrow)
       
   933     {
       
   934         detail = ESwtTraverseArrowPrevious;
       
   935     }
       
   936 
       
   937     /*
       
   938      * These two conditions are because workaround in Text. We simulate
       
   939      * EEventKey from EEventKeyDown, so in this moment we haven't
       
   940      * aKeyEvent.iCode value, only iScanCode.
       
   941      */
       
   942     if (aType == EEventKey && aKeyEvent.iCode == 0)
       
   943     {
       
   944         if (aKeyEvent.iScanCode == EStdKeyLeftArrow)
       
   945         {
       
   946             detail = ESwtTraverseArrowPrevious;
       
   947         }
       
   948         if (aKeyEvent.iScanCode == EStdKeyRightArrow)
       
   949         {
       
   950             detail = ESwtTraverseArrowNext;
       
   951         }
       
   952     }
       
   953 
       
   954     // Return (only if there is a default button or a default command)
       
   955     if ((aKeyEvent.iCode==EKeyEnter || aKeyEvent.iCode==EKeyOK) && modifiers==0)
       
   956     {
       
   957         const MSwtShell& shell = GetShell();
       
   958         if ((!ButtonInterface() || ((Style() & KSwtStylePush) == 0))
       
   959                 && const_cast<MSwtShell&>(shell).Decorations()->DefaultButton())
       
   960         {
       
   961             detail = ESwtTraverseReturn;
       
   962         }
       
   963 
       
   964         if (shell.DefaultCommand())
       
   965         {
       
   966             detail = ESwtTraverseReturn;
       
   967         }
       
   968     }
       
   969 
       
   970     // Escape (only for non top-level Shells)
       
   971     if (aKeyEvent.iCode==EKeyEscape && modifiers==0)
       
   972     {
       
   973         if (GetShell().GetParentShell() != NULL)
       
   974             detail = ESwtTraverseEscape;
       
   975     }
       
   976 
       
   977     // Send event
       
   978     if (detail != ESwtTraverseNone)
       
   979     {
       
   980         iTraverse = aTraversalDoIt;
       
   981         MSwtComposite* parent = this->GetParent();
       
   982         if (parent)
       
   983         {
       
   984             // if the parent does not have a tablist.
       
   985             // it means the parent should not be used as a composite.
       
   986             // So don't post traverse event
       
   987             CSwtPeerArray* tabList = parent->GetTabListL();
       
   988             if (tabList)
       
   989             {
       
   990                 delete tabList;
       
   991                 tabList = NULL;
       
   992                 iDisplay.PostTraverseEventL(*this, detail, aKeyEvent, aType, aTraversalDoIt);
       
   993             }
       
   994         }
       
   995         else
       
   996         {
       
   997             iDisplay.PostTraverseEventL(*this, detail, aKeyEvent, aType, aTraversalDoIt);
       
   998         }
       
   999     }
       
  1000     else
       
  1001     {
       
  1002         iDisplay.PostKeyEventL(*this, aKeyEvent, aType);
       
  1003     }
       
  1004 
       
  1005     return EKeyWasConsumed;
       
  1006 }
       
  1007 
       
  1008 TKeyResponse ASwtControlBase::OfferKeyEventToTabGroupL(const TKeyEvent& aKeyEvent,TEventCode aType)
       
  1009 {
       
  1010     CEikStatusPane *sp = ((CAknAppUi*)iDisplay.CoeEnv()->EikAppUi())->StatusPane();
       
  1011     ASSERT(sp != NULL);
       
  1012     CAknNavigationControlContainer* naviCtrlCtnr = static_cast<CAknNavigationControlContainer*>
       
  1013             (sp->ControlL(TUid::Uid(EEikStatusPaneUidNavi)));
       
  1014     ASSERT(naviCtrlCtnr != NULL);
       
  1015     CAknNavigationDecorator* navi = naviCtrlCtnr->Top();
       
  1016     if (navi != NULL && navi->ControlType() == CAknNavigationDecorator::ETabGroup) // if there is a tab group
       
  1017     {
       
  1018         CAknTabGroup* tabGroup = static_cast<CAknTabGroup*>(navi->DecoratedControl());
       
  1019         if (tabGroup != NULL && tabGroup->OfferKeyEventL(aKeyEvent, aType) == EKeyWasConsumed)
       
  1020         {
       
  1021             TSwtTraversal detail(ESwtTraverseNone);
       
  1022             if (AknLayoutUtils::LayoutMirrored())
       
  1023             {
       
  1024                 detail = (aKeyEvent.iCode == EKeyRightArrow ?
       
  1025                           ESwtTraversePagePrevious : ESwtTraversePageNext);
       
  1026             }
       
  1027             else
       
  1028             {
       
  1029                 detail = (aKeyEvent.iCode == EKeyRightArrow ?
       
  1030                           ESwtTraversePageNext : ESwtTraversePagePrevious);
       
  1031             }
       
  1032             iDisplay.PostTraverseEventL(*this, detail, aKeyEvent, aType, ETrue);
       
  1033             // The tab has consumed the key event.
       
  1034             return EKeyWasConsumed;
       
  1035         }
       
  1036     }
       
  1037     return EKeyWasNotConsumed;
       
  1038 }
       
  1039 
       
  1040 EXPORT_C void ASwtControlBase::RegisterWithParentL()
       
  1041 {
       
  1042     if (iParent != NULL)
       
  1043     {
       
  1044         // eSWT hierarchy controls.
       
  1045         iParent->RegisterChildL(*this);
       
  1046     }
       
  1047 
       
  1048     // Set our DoNotDraw flag according to our parent flag
       
  1049     if (iParent != NULL)
       
  1050     {
       
  1051         UpdateDoNotDrawFlag();
       
  1052     }
       
  1053 }
       
  1054 
       
  1055 EXPORT_C TTypeUid::Ptr ASwtControlBase::SwtMopSupplyObject(TTypeUid aId)
       
  1056 {
       
  1057     // MAknsControlContext is ask.
       
  1058     if (aId.iUid == MAknsControlContext::ETypeId)
       
  1059     {
       
  1060         MAknsControlContext* bckgd = SwtMopSupplyCtrlContext();
       
  1061 
       
  1062         // If a parent have is own background context and the color background was not set on it.
       
  1063         // Return the Control Context of his background.
       
  1064         if (bckgd != NULL)
       
  1065         {
       
  1066             return MAknsControlContext::SupplyMopObject(aId, bckgd);
       
  1067         }
       
  1068     }
       
  1069 
       
  1070     // Else return Null
       
  1071     return TTypeUid::Null();
       
  1072 }
       
  1073 
       
  1074 MAknsControlContext* ASwtControlBase::SwtMopSupplyCtrlContext(TBool aBeginSearchToThis) const
       
  1075 {
       
  1076     MAknsControlContext* background = NULL;
       
  1077     if (aBeginSearchToThis)
       
  1078     {
       
  1079         background = FocusBackgroundNow();
       
  1080         if (background)
       
  1081         {
       
  1082             return background;
       
  1083         }
       
  1084     }
       
  1085 
       
  1086     MSwtComposite* parent = GetParent();
       
  1087     TInt policy = FocusBackgroundPolicy();
       
  1088     if (IsDefaultBackgroundUse())
       
  1089     {
       
  1090         background = aBeginSearchToThis ? GetControlContext() : NULL;
       
  1091         while (!background && parent)
       
  1092         {
       
  1093             // If policy is ENoFocusBackgroundInCaptionedControl, bypass the caption highlight.
       
  1094             if (policy == ENoFocusBackgroundInCaptionedControl
       
  1095                     && parent->Control()->CaptionedControlInterface())
       
  1096             {
       
  1097                 parent = parent->Control()->GetParent();
       
  1098                 ASSERT(parent);
       
  1099             }
       
  1100 
       
  1101             background = parent->Control()->GetControlContext();
       
  1102 
       
  1103             // If the parent is a Shell we stop on it.
       
  1104             if (parent->Control()->IsShell())
       
  1105             {
       
  1106                 parent = NULL;
       
  1107             }
       
  1108             else
       
  1109             {
       
  1110                 parent = parent->Control()->GetParent();
       
  1111             }
       
  1112         }
       
  1113     }
       
  1114 
       
  1115     return background;
       
  1116 }
       
  1117 
       
  1118 RRegion* ASwtControlBase::SetupChildClippingRegion(const MSwtComposite* aComposite,
       
  1119         CWindowGc& aGc,
       
  1120         const TRect& aDrawRect,
       
  1121         const TPoint& aOffset /*= TPoint( 0, 0 )*/) const
       
  1122 {
       
  1123     ASSERT(aComposite);
       
  1124 
       
  1125     TBool clippingRegionSet(EFalse);
       
  1126     RRegion* clippingRegion = NULL;
       
  1127     if (aComposite)
       
  1128     {
       
  1129         TInt childCount(0);
       
  1130         const RSwtControlArray* children = aComposite->Children();
       
  1131         if (children)
       
  1132         {
       
  1133             childCount = children->Count();
       
  1134         }
       
  1135         // If this control has children then proceed with setting
       
  1136         // a clipping region to exclude them.
       
  1137         if (childCount > 0)
       
  1138         {
       
  1139             clippingRegion = new RRegion(aDrawRect, childCount + 1);
       
  1140             if (clippingRegion)
       
  1141             {
       
  1142                 for (TInt i = 0; i < childCount; ++i)
       
  1143                 {
       
  1144                     MSwtControl* child = (*children)[ i ];
       
  1145                     // Shells are window owning so they will get clipped anyway.
       
  1146                     // No point in adding overhead by handling them manually here.
       
  1147                     // Also controls that are not visible should not be clipped.
       
  1148                     if (child && !child->IsShell() && child->CoeControl().IsVisible()
       
  1149                             && !child->HasDoNotDrawFlag())
       
  1150                     {
       
  1151                         clippingRegion->SubRect(child->CoeControl().Rect());
       
  1152                     }
       
  1153                 }
       
  1154                 if (!clippingRegion->CheckError())
       
  1155                 {
       
  1156                     if (aOffset != TPoint(0, 0))
       
  1157                     {
       
  1158                         clippingRegion->Offset(aOffset);
       
  1159                     }
       
  1160                     if (aGc.SetClippingRegion(*clippingRegion) == KErrNone)
       
  1161                     {
       
  1162                         clippingRegionSet = ETrue;
       
  1163                     }
       
  1164                 }
       
  1165                 if (!clippingRegionSet)
       
  1166                 {
       
  1167                     if (clippingRegion)
       
  1168                     {
       
  1169                         clippingRegion->Destroy();
       
  1170                     }
       
  1171                     clippingRegion = NULL;
       
  1172                 }
       
  1173             }
       
  1174         }
       
  1175     }
       
  1176     return clippingRegion;
       
  1177 }
       
  1178 
       
  1179 void ASwtControlBase::CancelChildClippingRegion(RRegion* aClippingRegion,
       
  1180         CWindowGc& aGc) const
       
  1181 {
       
  1182     // Cancel & destroy the clipping region
       
  1183     if (aClippingRegion)
       
  1184     {
       
  1185         aGc.CancelClippingRegion();
       
  1186         aClippingRegion->Destroy();
       
  1187     }
       
  1188 }
       
  1189 
       
  1190 TPoint ASwtControlBase::ClientToWindow(const TPoint& aPoint) const
       
  1191 {
       
  1192     return aPoint + ClientRect().iTl;
       
  1193 }
       
  1194 
       
  1195 TRect ASwtControlBase::ClientToWindow(const TRect& aRect)  const
       
  1196 {
       
  1197     const TRect clientRect(ClientRect());
       
  1198     return TRect(aRect.iTl+clientRect.iTl, aRect.iBr+clientRect.iTl);
       
  1199 }
       
  1200 
       
  1201 TPoint ASwtControlBase::WindowToClient(const TPoint& aPoint) const
       
  1202 {
       
  1203     return aPoint - ClientRect().iTl;
       
  1204 }
       
  1205 
       
  1206 EXPORT_C const MSwtFont& ASwtControlBase::DefaultFont() const
       
  1207 {
       
  1208     return *(iDisplay.Device().GetSystemFont());
       
  1209 }
       
  1210 
       
  1211 void ASwtControlBase::HandleRectChanged()
       
  1212 {
       
  1213     // Rebound the focus highlight
       
  1214     if (iFocusFrame)
       
  1215     {
       
  1216         TRect rect(CoeControl().Rect());
       
  1217         TRect innerRect;
       
  1218         innerRect.iTl = CSwtLafFacade::GetLayoutRect(
       
  1219                             CSwtLafFacade::EInputFieldSkinPlacingGeneralLine2, rect, 0).Rect().iBr;
       
  1220         innerRect.iBr = CSwtLafFacade::GetLayoutRect(
       
  1221                             CSwtLafFacade::EInputFieldSkinPlacingGeneralLine5, rect, 0).Rect().iTl;
       
  1222         iFocusFrame->SetFrameRects(rect, innerRect);
       
  1223     }
       
  1224 }
       
  1225 
       
  1226 EXPORT_C void ASwtControlBase::HandleSizeChanged()
       
  1227 {
       
  1228     if (iPrevCoeSize == CoeControl().Size())
       
  1229     {
       
  1230         return;
       
  1231     }
       
  1232 
       
  1233     if (iPrevCoeSize != CoeControl().Size())
       
  1234     {
       
  1235         iPrevCoeSize = CoeControl().Size();
       
  1236         TRAP_IGNORE(iDisplay.PostResizeEventL(iPeer));
       
  1237         HandleRectChanged();
       
  1238     }
       
  1239 }
       
  1240 
       
  1241 EXPORT_C void ASwtControlBase::HandlePositionChanged()
       
  1242 {
       
  1243     if (iPrevSwtPos == GetLocation() && iPrevCoePos == CoeControl().Position())
       
  1244     {
       
  1245         return;
       
  1246     }
       
  1247 
       
  1248     if (iPrevSwtPos != GetLocation())
       
  1249     {
       
  1250         iPrevSwtPos = GetLocation();
       
  1251         TRAP_IGNORE(iDisplay.PostMoveEventL(iPeer));
       
  1252     }
       
  1253 
       
  1254     if (iPrevCoePos != CoeControl().Position())
       
  1255     {
       
  1256         iPrevCoePos = CoeControl().Position();
       
  1257         HandleRectChanged();
       
  1258     }
       
  1259 }
       
  1260 
       
  1261 //
       
  1262 // Virtual methods from MSwtDrawable
       
  1263 //
       
  1264 EXPORT_C MSwtGc* ASwtControlBase::NewGcL()
       
  1265 {
       
  1266     CCoeControl& coeCtrl = CoeControl();
       
  1267 
       
  1268     // Create the native GC
       
  1269     CWsScreenDevice& device = static_cast<CWsScreenDevice&>(GraphicsDevice());
       
  1270     CWindowGc* nativeGc = new(ELeave) CWindowGc(&device);
       
  1271     TInt error = nativeGc->Construct();
       
  1272     if (error)
       
  1273     {
       
  1274         delete nativeGc;
       
  1275         User::Leave(error);
       
  1276     }
       
  1277     nativeGc->Activate(*coeCtrl.DrawableWindow());
       
  1278 
       
  1279     // Set the GC's origin to the top-left of the client area
       
  1280     const TRect clientRect(ClientRect()); // in window coords
       
  1281     nativeGc->SetOrigin(clientRect.iTl);
       
  1282 
       
  1283     // Compute the boundary rect (in GC coords)
       
  1284     TRect boundaryRect(clientRect);
       
  1285     if (!IsShell())
       
  1286     {
       
  1287         MSwtComposite* parent = GetParent();
       
  1288         if (parent != NULL)
       
  1289             boundaryRect.Intersection(static_cast<ASwtControlBase*>(parent->Control())->VisibleRect());
       
  1290     }
       
  1291     boundaryRect.Move(-clientRect.iTl);
       
  1292 
       
  1293     // Compute the GC rect in screen coords
       
  1294     const TRect gcScreenRect(WindowToScreen(clientRect.iTl), clientRect.Size());
       
  1295 
       
  1296     // Get the colours & the font
       
  1297     CEikonEnv* eikenv = iDisplay.CoeEnv();
       
  1298     TRgb penCol(eikenv->ControlColor(EColorControlText,       coeCtrl));
       
  1299     TRgb brushCol(eikenv->ControlColor(EColorControlBackground, coeCtrl));
       
  1300     const MSwtFont* font = GetFont();
       
  1301     ASSERT(font!=NULL);
       
  1302 
       
  1303     return iDisplay.Factory().NewWindowGcL(*this, nativeGc, DestroyNativeGc,
       
  1304                                            penCol, brushCol, *font, boundaryRect, gcScreenRect);
       
  1305 }
       
  1306 
       
  1307 EXPORT_C CBitmapDevice& ASwtControlBase::GraphicsDevice()
       
  1308 {
       
  1309     return *static_cast<CWsScreenDevice*>(iDisplay.CoeEnv()->SystemGc().Device());
       
  1310 }
       
  1311 
       
  1312 EXPORT_C void ASwtControlBase::HandleUpdate()
       
  1313 {
       
  1314     // Nothing to do for controls
       
  1315 }
       
  1316 
       
  1317 void ASwtControlBase::DestroyNativeGc(CBitmapContext* aGc)
       
  1318 {
       
  1319     static_cast<CWindowGc*>(aGc)->Deactivate();
       
  1320     delete aGc;
       
  1321 }
       
  1322 
       
  1323 //
       
  1324 // Virtual methods from MSwtControl
       
  1325 //
       
  1326 EXPORT_C TSwtPeer ASwtControlBase::Dispose()
       
  1327 {
       
  1328     // Note that unregistration is done here rather than in the destructor.
       
  1329     // The reason for it is that an object may go through destruction before
       
  1330     // being registered.
       
  1331     if (iParent)   // This may be NULL because of top-level Shells
       
  1332     {
       
  1333         iParent->UnregisterChild(*this);
       
  1334     }
       
  1335     // If the control has a bad parent, such as combo, table or caption control
       
  1336     // UnregisterChild does not kill the focus
       
  1337     if (CoeControl().IsFocused())
       
  1338     {
       
  1339         CoeControl().SetFocus(EFalse);
       
  1340         ASSERT(!CoeControl().IsFocused());
       
  1341     }
       
  1342 
       
  1343     // RemoveRef here in case iFont would be pointing to an MSwtFont
       
  1344     // that is disposed in destructor of the object (e.g. iDefaultFont)
       
  1345     if (iFont)
       
  1346     {
       
  1347         iFont->RemoveRef();
       
  1348         iFont = NULL;
       
  1349     }
       
  1350 
       
  1351     Invalidate(CoeControl().Rect());
       
  1352 
       
  1353     if (UiUtils().PointerCaptureControl() == this)
       
  1354     {
       
  1355         UiUtils().SetPointerCaptureControl(NULL);
       
  1356     }
       
  1357     if (UiUtils().NextFocusedControl() == this)
       
  1358     {
       
  1359         UiUtils().SetNextFocusedControl(NULL);
       
  1360     }
       
  1361     if (iDisplay.LongTapAnimationControl() == this)
       
  1362     {
       
  1363         iDisplay.CancelLongTapAnimation();
       
  1364     }
       
  1365 
       
  1366     RemoveFromControlStack();
       
  1367 
       
  1368     GetShell().ControlDisposing(*this);
       
  1369 
       
  1370     TSwtPeer peer(iPeer);
       
  1371     DoDelete();
       
  1372     return peer;
       
  1373 }
       
  1374 
       
  1375 EXPORT_C TSwtPeer ASwtControlBase::JavaPeer() const
       
  1376 {
       
  1377     return iPeer;
       
  1378 }
       
  1379 
       
  1380 EXPORT_C TUint ASwtControlBase::Flags() const
       
  1381 {
       
  1382     return iCtrlFlags;
       
  1383 }
       
  1384 
       
  1385 EXPORT_C void ASwtControlBase::Paint(const TRect& aRect,
       
  1386                                      TBool aPaintNatively /*= ETrue*/,
       
  1387                                      MSwtGc* aGc /*= NULL*/,
       
  1388                                      TInt* aIntParams /*= NULL*/,
       
  1389                                      TInt aIntCount /*= 0*/,
       
  1390                                      HBufC* aStrParams /*= NULL*/) const
       
  1391 {
       
  1392     // While emergency paints are required, painting is done by PaintUrgently
       
  1393     // It is essential that paint events coming from Java are not served as
       
  1394     // they can slow down considerably the kinetic scrolling.
       
  1395     // The only reason for letting non-urgent paint controls still paint is
       
  1396     // that there might be late paint event arriving after the urgent paint
       
  1397     // has started as in the case of focus changes.
       
  1398     MSwtShell& shell = GetShell();
       
  1399     if (shell.UrgentPaintControl() == this)
       
  1400     {
       
  1401         return;
       
  1402     }
       
  1403 
       
  1404     if (iCtrlFlags & MSwtControl::EFlagDoNotDraw)
       
  1405     {
       
  1406         return;
       
  1407     }
       
  1408 
       
  1409     // Except Shells, Controls cannot draw over invisible parents.
       
  1410     if (!GetVisibleRecursively())
       
  1411     {
       
  1412         return;
       
  1413     }
       
  1414 
       
  1415     const CCoeControl& coeCtrl = CoeControl();
       
  1416     RWindow* window = static_cast<RWindow*>(coeCtrl.DrawableWindow());
       
  1417     ASSERT(window);
       
  1418 
       
  1419     TRect dirtyRect(aRect);                     // screen coordinates
       
  1420     dirtyRect.Move(-window->Position());        // window coordinates
       
  1421     dirtyRect.Intersection(VisibleRect(ETrue));
       
  1422     if (dirtyRect.IsEmpty())
       
  1423     {
       
  1424         return;
       
  1425     }
       
  1426 
       
  1427     CWindowGc& systemGc = coeCtrl.SystemGc();
       
  1428     MSwtComposite* comp = CompositeInterface();
       
  1429 
       
  1430     window->Invalidate(dirtyRect);
       
  1431     window->BeginRedraw(dirtyRect);
       
  1432 
       
  1433     // Native controls draw here
       
  1434     if (aPaintNatively)
       
  1435     {
       
  1436         systemGc.Activate(*window);
       
  1437         systemGc.Reset();
       
  1438         systemGc.SetClippingRect(dirtyRect);
       
  1439         DoPaint(dirtyRect);
       
  1440 
       
  1441         TBool isChildShell(EFalse);
       
  1442         if (comp)
       
  1443         {
       
  1444             // GC intentionally reset only once for both scrollbar back and border.
       
  1445             systemGc.Reset();
       
  1446             systemGc.SetClippingRect(dirtyRect);
       
  1447             comp->DrawScrollBarBackground(dirtyRect);
       
  1448 
       
  1449             // For child Shells, the border is drawn right after the background.
       
  1450             isChildShell = IsShell() && iParent;
       
  1451             if (isChildShell)
       
  1452             {
       
  1453                 if (HasBorderStyle())
       
  1454                 {
       
  1455                     DrawBorder(dirtyRect);
       
  1456                 }
       
  1457             }
       
  1458         }
       
  1459 
       
  1460         // GC is reset inside DrawComponentControls
       
  1461         DrawComponentControls(coeCtrl, dirtyRect, systemGc, CanDrawOutsideRect());
       
  1462 
       
  1463         if (!isChildShell)
       
  1464         {
       
  1465             if (HasBorderStyle())
       
  1466             {
       
  1467                 systemGc.Reset();
       
  1468                 systemGc.SetClippingRect(dirtyRect);
       
  1469                 DrawBorder(dirtyRect);
       
  1470             }
       
  1471         }
       
  1472     }
       
  1473 
       
  1474     // Java paint listener draws here
       
  1475     if (aGc)
       
  1476     {
       
  1477         // Workaround, corrects the clipping region. Currently without this workaround
       
  1478         // if we do a BitBlt from the command buffer and the GCs origin has been set to > (0, 0)
       
  1479         // then clipping region will be wrong. Origin is applied to it twice. Adding some
       
  1480         // operation here, before the potential BitBlt, will correct the problem. This is
       
  1481         // a platform issue in 3.2 with pre-activated graphics contexts.
       
  1482 #ifndef RD_SCALABLE_UI_V2
       
  1483         aGc->BitmapContext().Plot(TPoint(-1,-1));
       
  1484 #endif //RD_SCALABLE_UI_V2
       
  1485 
       
  1486         // Clip Java GC so that it does not need to draw under children of the composite.
       
  1487         CWindowGc& javaGc= static_cast<CWindowGc&>(aGc->BitmapContext());
       
  1488         RRegion* javaGcClipRegion = NULL;
       
  1489         TRect javaGcRect(dirtyRect);             // window coordinates
       
  1490         javaGcRect.Intersection(ClientRect());   // window coordinates
       
  1491         if (comp)
       
  1492         {
       
  1493             javaGcClipRegion = SetupChildClippingRegion(
       
  1494                                    comp,
       
  1495                                    javaGc,
       
  1496                                    javaGcRect,
       
  1497                                    -ClientRect().iTl);              // Java GC coordinates (relative to client area)
       
  1498         }
       
  1499 
       
  1500         // Flush Java GC
       
  1501         FlushJavaBuffer(aGc, aIntParams, aIntCount, aStrParams, window, javaGcRect);
       
  1502         CancelChildClippingRegion(javaGcClipRegion, javaGc);
       
  1503     }
       
  1504 
       
  1505     window->EndRedraw();
       
  1506 
       
  1507     if (aPaintNatively)
       
  1508     {
       
  1509         systemGc.Deactivate();
       
  1510     }
       
  1511 
       
  1512 #ifdef RD_JAVA_S60_RELEASE_9_2
       
  1513     iDisplay.CoeEnv()->WsSession().Finish();
       
  1514 #endif
       
  1515 }
       
  1516 
       
  1517 EXPORT_C void ASwtControlBase::PaintUrgently(const TRect& aRect, TBool aInitDraw /*= ETrue*/) const
       
  1518 {
       
  1519     if (iCtrlFlags & MSwtControl::EFlagDoNotDraw)
       
  1520     {
       
  1521         return;
       
  1522     }
       
  1523 
       
  1524     // Except Shells, Controls cannot draw over invisible parents.
       
  1525     if (!GetVisibleRecursively())
       
  1526     {
       
  1527         return;
       
  1528     }
       
  1529 
       
  1530     TRect dirtyRect(aRect);   // window coordinates
       
  1531     dirtyRect.Intersection(VisibleRect(ETrue));
       
  1532     if (dirtyRect.IsEmpty())
       
  1533     {
       
  1534         return;
       
  1535     }
       
  1536 
       
  1537     const CCoeControl& coeCtrl = CoeControl();
       
  1538     RWindow* window = static_cast<RWindow*>(coeCtrl.DrawableWindow());
       
  1539     ASSERT(window);
       
  1540     CWindowGc& systemGc = coeCtrl.SystemGc();
       
  1541 
       
  1542     if (aInitDraw)
       
  1543     {
       
  1544         systemGc.Activate(*window);
       
  1545         window->Invalidate(dirtyRect);
       
  1546         window->BeginRedraw(dirtyRect);
       
  1547     }
       
  1548 
       
  1549     systemGc.Reset();
       
  1550     systemGc.SetClippingRect(dirtyRect);
       
  1551     DoPaint(dirtyRect);
       
  1552 
       
  1553     TBool isChildShell(EFalse);
       
  1554     MSwtComposite* comp = CompositeInterface();
       
  1555     if (comp)
       
  1556     {
       
  1557         // GC intentionally reset only once for both scrollbar back and border.
       
  1558         systemGc.Reset();
       
  1559         systemGc.SetClippingRect(dirtyRect);
       
  1560         comp->DrawScrollBarBackground(dirtyRect);
       
  1561 
       
  1562         // For child Shells, the border is drawn right after the background.
       
  1563         isChildShell = IsShell() && iParent;
       
  1564         if (isChildShell)
       
  1565         {
       
  1566             if (HasBorderStyle())
       
  1567             {
       
  1568                 DrawBorder(dirtyRect);
       
  1569             }
       
  1570         }
       
  1571     }
       
  1572 
       
  1573     // GC is reset inside DrawComponentControls
       
  1574     DrawComponentControls(coeCtrl, dirtyRect, systemGc, CanDrawOutsideRect());
       
  1575 
       
  1576     if (!isChildShell)
       
  1577     {
       
  1578         if (HasBorderStyle())
       
  1579         {
       
  1580             systemGc.Reset();
       
  1581             systemGc.SetClippingRect(dirtyRect);
       
  1582             DrawBorder(dirtyRect);
       
  1583         }
       
  1584     }
       
  1585 
       
  1586     if (comp)
       
  1587     {
       
  1588         const RSwtControlArray* children = comp->Children();
       
  1589         const TInt count = children->Count();
       
  1590         for (TInt i = 0; i < count; i++)
       
  1591         {
       
  1592             MSwtControl* child = (*children)[i];
       
  1593             if (!child->IsShell())
       
  1594                 child->PaintUrgently(dirtyRect, EFalse);
       
  1595         }
       
  1596     }
       
  1597 
       
  1598     if (aInitDraw)
       
  1599     {
       
  1600         window->EndRedraw();
       
  1601         systemGc.Deactivate();
       
  1602 #ifdef RD_JAVA_S60_RELEASE_9_2
       
  1603         iDisplay.CoeEnv()->WsSession().Finish();
       
  1604 #endif
       
  1605     }
       
  1606 }
       
  1607 
       
  1608 EXPORT_C TBool ASwtControlBase::IsPaintingUrgently() const
       
  1609 {
       
  1610     MSwtControl* urgentPaintControl = GetShell().UrgentPaintControl();
       
  1611     return urgentPaintControl
       
  1612            && (urgentPaintControl == this || IsDescentOf(*urgentPaintControl));
       
  1613 }
       
  1614 
       
  1615 EXPORT_C TBool ASwtControlBase::IsFocusable(TInt aReason /*=KSwtFocusByApi*/) const
       
  1616 {
       
  1617     const CCoeControl& coeCtrl = CoeControl();
       
  1618 
       
  1619     // Invisible or disabled control can never get the focus (not even forced)
       
  1620     if (!coeCtrl.IsVisible() || coeCtrl.IsDimmed())
       
  1621         return EFalse;
       
  1622 
       
  1623     MSwtComposite* parent = GetParent();
       
  1624 
       
  1625     // Go through the whole ancestor chain, if any of the ancestors is
       
  1626     // disabled or invisible, return EFalse.
       
  1627     while (parent)
       
  1628     {
       
  1629         MSwtControl* parentCtrl = parent->Control();
       
  1630 
       
  1631         if (!parentCtrl->GetEnabled() || !parentCtrl->GetVisible())
       
  1632         {
       
  1633             return EFalse;
       
  1634         }
       
  1635 
       
  1636         if (!parentCtrl->IsShell())
       
  1637         {
       
  1638             parent = parentCtrl->GetParent();
       
  1639         }
       
  1640         else
       
  1641         {
       
  1642             parent = NULL;
       
  1643         }
       
  1644     }
       
  1645 
       
  1646     return ((aReason == KSwtFocusByForce) || !coeCtrl.IsNonFocusing());
       
  1647 }
       
  1648 
       
  1649 EXPORT_C TBool ASwtControlBase::SetSwtFocus(TInt aReason /*=KSwtFocusByApi*/)
       
  1650 {
       
  1651     ASSERT(!IsShell());
       
  1652 
       
  1653     CCoeControl& coeCtrl = CoeControl();
       
  1654     if (coeCtrl.IsFocused())
       
  1655     {
       
  1656         return ETrue;
       
  1657     }
       
  1658 
       
  1659     MSwtShell& shell = GetShell();
       
  1660     if (!IsFocusable(aReason))
       
  1661     {
       
  1662         // remember the control which is not focusible because its parent shell is invisible currently
       
  1663         // the control can get the focus later whenever the shell becomes visible.
       
  1664 
       
  1665         if (shell.Control()->GetVisible())
       
  1666         {
       
  1667             // don't need to remember the control when the control itself is a shell
       
  1668             // or its parent shell is visible.
       
  1669             return EFalse;
       
  1670         }
       
  1671 
       
  1672         const CCoeControl& coeCtrl = CoeControl();
       
  1673         if ((aReason != KSwtFocusByForce) && coeCtrl.IsNonFocusing())
       
  1674         {
       
  1675             // don't remember non-focusing control
       
  1676             return EFalse;
       
  1677         }
       
  1678 
       
  1679         // Go through the whole ancestor chain, if any of the ancestors is
       
  1680         // disabled, don't remember the control.
       
  1681         TBool rememberIt(ETrue);
       
  1682         MSwtComposite* parent = GetParent();
       
  1683         while (parent)
       
  1684         {
       
  1685             MSwtControl* parentCtrl = parent->Control();
       
  1686             if (!parentCtrl->GetEnabled())
       
  1687             {
       
  1688                 rememberIt = EFalse;
       
  1689                 break;
       
  1690             }
       
  1691 
       
  1692             if (!parentCtrl->IsShell())
       
  1693             {
       
  1694                 parent = parentCtrl->GetParent();
       
  1695             }
       
  1696             else
       
  1697             {
       
  1698                 break;
       
  1699             }
       
  1700         }
       
  1701 
       
  1702         if (rememberIt)
       
  1703         {
       
  1704             shell.SetToBeFocusedControl(this);
       
  1705         }
       
  1706 
       
  1707         return EFalse;
       
  1708     }
       
  1709 
       
  1710 
       
  1711     CSwtUiUtils& uiUtils = UiUtils();
       
  1712 
       
  1713     if (uiUtils.GetActiveShell()==&shell || uiUtils.CanBeActive(shell))
       
  1714     {
       
  1715         if (uiUtils.IsAppFocused())
       
  1716         {
       
  1717             // Focus if we can (Shell must be able to be active)
       
  1718             coeCtrl.SetFocus(ETrue);
       
  1719         }
       
  1720         else
       
  1721         {
       
  1722             // Set next focused control
       
  1723             uiUtils.SetNextFocusedControl(this);
       
  1724         }
       
  1725     }
       
  1726 
       
  1727     return coeCtrl.IsFocused();
       
  1728 }
       
  1729 
       
  1730 EXPORT_C TInt ASwtControlBase::FocusBackgroundPolicy() const
       
  1731 {
       
  1732     return EDefaultFocusBackground;
       
  1733 };
       
  1734 
       
  1735 EXPORT_C TInt ASwtControlBase::PressBackgroundPolicy() const
       
  1736 {
       
  1737     return ENoPressBackground;
       
  1738 };
       
  1739 
       
  1740 EXPORT_C TBool ASwtControlBase::GetEnabled() const
       
  1741 {
       
  1742     return (CoeControl().IsDimmed()) ? EFalse : ETrue;
       
  1743 }
       
  1744 
       
  1745 EXPORT_C TBool ASwtControlBase::GetVisible() const
       
  1746 {
       
  1747     return !(iCtrlFlags & MSwtControl::EFlagInvisible);
       
  1748 }
       
  1749 
       
  1750 EXPORT_C TBool ASwtControlBase::GetVisibleRecursively() const
       
  1751 {
       
  1752     if (!IsShell() && iParent && !iParent->Control()->GetVisibleRecursively())
       
  1753     {
       
  1754         return EFalse;
       
  1755     }
       
  1756     return GetVisible();
       
  1757 }
       
  1758 
       
  1759 EXPORT_C TBool ASwtControlBase::IsDefaultBackgroundUse() const
       
  1760 {
       
  1761     return iBackgrdDefaultUsed;
       
  1762 }
       
  1763 
       
  1764 EXPORT_C MAknsControlContext* ASwtControlBase::GetControlContext() const
       
  1765 {
       
  1766     if (CaptionedControlInterface())
       
  1767     {
       
  1768         return FocusBackgroundNow();
       
  1769     }
       
  1770     else
       
  1771     {
       
  1772         return NULL;
       
  1773     }
       
  1774 }
       
  1775 
       
  1776 EXPORT_C TRgb ASwtControlBase::GetBackground() const
       
  1777 {
       
  1778     return iDisplay.CoeEnv()->ControlColor(EColorControlBackground, CoeControl());
       
  1779 }
       
  1780 
       
  1781 EXPORT_C TRgb ASwtControlBase::GetForeground() const
       
  1782 {
       
  1783     return iDisplay.CoeEnv()->ControlColor(EColorControlText, CoeControl());
       
  1784 }
       
  1785 
       
  1786 EXPORT_C void ASwtControlBase::SetBackgroundL(const MSwtColor* aColor)
       
  1787 {
       
  1788     DoSetBackgroundL(aColor);
       
  1789     Redraw();
       
  1790 }
       
  1791 
       
  1792 void ASwtControlBase::DoSetBackgroundL(const MSwtColor* aColor)
       
  1793 {
       
  1794     SetColorL(EColorControlBackground, aColor);
       
  1795     if (aColor)
       
  1796     {
       
  1797         iBackgrdDefaultUsed = EFalse;
       
  1798         // Not using FocusBackgroundNow() intentionally
       
  1799         if (iFocusFrame)
       
  1800         {
       
  1801             iFocusFrame->SetParentContext(NULL);
       
  1802         }
       
  1803     }
       
  1804     else
       
  1805     {
       
  1806         iBackgrdDefaultUsed = ETrue;
       
  1807         // Not using FocusBackgroundNow() intentionally
       
  1808         if (iFocusFrame)
       
  1809         {
       
  1810             iFocusFrame->SetParentContext(SwtMopSupplyCtrlContext(EFalse));
       
  1811         }
       
  1812     }
       
  1813 }
       
  1814 
       
  1815 EXPORT_C void ASwtControlBase::SetForegroundL(const MSwtColor* aColor)
       
  1816 {
       
  1817     DoSetForegroundL(aColor);
       
  1818     Redraw();
       
  1819 }
       
  1820 
       
  1821 void ASwtControlBase::DoSetForegroundL(const MSwtColor* aColor)
       
  1822 {
       
  1823     SetColorL(EColorControlText, aColor);
       
  1824 }
       
  1825 
       
  1826 EXPORT_C const MSwtFont* ASwtControlBase::GetFont() const
       
  1827 {
       
  1828     return (iFont!=NULL) ? iFont : &DefaultFont();
       
  1829 }
       
  1830 
       
  1831 EXPORT_C TRect ASwtControlBase::GetBounds() const
       
  1832 {
       
  1833     return TRect(GetLocation(), CoeControl().Size());
       
  1834 }
       
  1835 
       
  1836 EXPORT_C TPoint ASwtControlBase::GetLocation() const
       
  1837 {
       
  1838     TRect parentClientRect(iParent->Scrollable()->Control()->ClientRect()); //lint !e613
       
  1839     return CoeControl().Position() - parentClientRect.iTl;
       
  1840 }
       
  1841 
       
  1842 EXPORT_C const MSwtMenu* ASwtControlBase::GetPopupMenu() const
       
  1843 {
       
  1844     return iPopupMenu;
       
  1845 }
       
  1846 
       
  1847 EXPORT_C const MSwtMenu* ASwtControlBase::GetControlMenu() const
       
  1848 {
       
  1849     return NULL;
       
  1850 }
       
  1851 
       
  1852 EXPORT_C MSwtComposite* ASwtControlBase::GetParent() const
       
  1853 {
       
  1854     return iParent;
       
  1855 }
       
  1856 
       
  1857 EXPORT_C TBool ASwtControlBase::IsDescentOf(const MSwtControl& aControl) const
       
  1858 {
       
  1859     MSwtComposite* aParent = aControl.CompositeInterface();
       
  1860     if (!aParent)
       
  1861     {
       
  1862         return EFalse;
       
  1863     }
       
  1864 
       
  1865     MSwtComposite* parent = GetParent();
       
  1866     while (parent)
       
  1867     {
       
  1868         if (parent == aParent)
       
  1869         {
       
  1870             return ETrue;
       
  1871         }
       
  1872         parent = parent->Control()->GetParent();
       
  1873     }
       
  1874 
       
  1875     return EFalse;
       
  1876 }
       
  1877 
       
  1878 EXPORT_C MSwtShell& ASwtControlBase::GetTopShell() const
       
  1879 {
       
  1880     const MSwtControl* ctrl = this;
       
  1881     while (ctrl && ctrl->GetParent())
       
  1882     {
       
  1883         ctrl = ctrl->GetParent()->Control();
       
  1884     }
       
  1885     MSwtShell* shell = const_cast<MSwtShell*>(ctrl->ShellInterface());
       
  1886     ASSERT(shell);
       
  1887     return *shell;
       
  1888 }
       
  1889 
       
  1890 EXPORT_C MSwtCaptionedControl* ASwtControlBase::GetNearestCaptionedControl(TBool aIncludeSelf /*= ETrue*/) const
       
  1891 {
       
  1892     MSwtCaptionedControl* res = NULL;
       
  1893     const MSwtControl* ctrl = aIncludeSelf ? this : (GetParent() ? GetParent()->Control() : NULL);
       
  1894     while (ctrl)
       
  1895     {
       
  1896         if (ctrl->CaptionedControlInterface())
       
  1897         {
       
  1898             return ctrl->CaptionedControlInterface();
       
  1899         }
       
  1900         ctrl = ctrl->GetParent() ? ctrl->GetParent()->Control() : NULL;
       
  1901     }
       
  1902     return res;
       
  1903 }
       
  1904 
       
  1905 EXPORT_C TSize ASwtControlBase::GetWidgetSize() const
       
  1906 {
       
  1907     return CoeControl().Size();
       
  1908 }
       
  1909 
       
  1910 EXPORT_C void ASwtControlBase::SetWidgetSize(const TSize& aSize)
       
  1911 {
       
  1912     ASSERT(!IsShell());
       
  1913 
       
  1914     CCoeControl& coeCtrl = CoeControl();
       
  1915     TRect oldRect(coeCtrl.Rect());
       
  1916     TRect newRect(coeCtrl.Position(), aSize);
       
  1917 
       
  1918     if (newRect == oldRect)
       
  1919     {
       
  1920         return;
       
  1921     }
       
  1922 
       
  1923     MSwtShell& shell = GetShell();
       
  1924     if (shell.UrgentPaintControl() == this)
       
  1925     {
       
  1926         shell.SetUrgentPaintControl(NULL);
       
  1927     }
       
  1928 
       
  1929     TBool urgent = EFalse;
       
  1930     if (iParent->ScrolledCompositeContent() == this
       
  1931             && shell.UrgentPaintControl() == iParent->Control())
       
  1932     {
       
  1933         if (iParent->ScrolledCompositePysicsAction() != KSwtPhysicsNoAction)
       
  1934         {
       
  1935             // Cannot change the bounds of the content while flicking or panning
       
  1936             return;
       
  1937         }
       
  1938         urgent = ETrue;
       
  1939     }
       
  1940 
       
  1941     if (!urgent)
       
  1942     {
       
  1943         // Do not invalidate outside parent's visible client area.
       
  1944         if (iParent)
       
  1945         {
       
  1946             TRect parentVisibleRect(static_cast<ASwtControlBase*>(
       
  1947                                         iParent->Control())->VisibleRect());
       
  1948             oldRect.Intersection(parentVisibleRect);
       
  1949             newRect.Intersection(parentVisibleRect);
       
  1950         }
       
  1951 
       
  1952         // Invalidate uncovered regions.
       
  1953         RRegion invalidRegion;
       
  1954         invalidRegion.Clear();
       
  1955         invalidRegion.AddRect(oldRect);
       
  1956         invalidRegion.SubRect(newRect);
       
  1957         if (!invalidRegion.IsEmpty())
       
  1958         {
       
  1959             ASSERT(invalidRegion.Count() <= 2);
       
  1960             for (TInt i = 0; i < invalidRegion.Count(); i++)
       
  1961             {
       
  1962                 Invalidate(invalidRegion[i]);
       
  1963             }
       
  1964         }
       
  1965         invalidRegion.Close();
       
  1966     }
       
  1967 
       
  1968     // Set and redraw new rect
       
  1969     coeCtrl.SetSize(aSize);
       
  1970     if (!(iStyle & KSwtStyleNoRedrawResize))
       
  1971     {
       
  1972         if (urgent)
       
  1973         {
       
  1974             iParent->Control()->PaintUrgently(iParent->Control()->CoeControl().Rect());
       
  1975         }
       
  1976         else
       
  1977         {
       
  1978             Redraw();
       
  1979         }
       
  1980     }
       
  1981 }
       
  1982 
       
  1983 EXPORT_C TBool ASwtControlBase::IsFocusControl()
       
  1984 {
       
  1985     ASSERT(!IsShell());
       
  1986     return CoeControl().IsFocused();
       
  1987 }
       
  1988 
       
  1989 EXPORT_C void ASwtControlBase::MoveAbove(const MSwtControl* aControl)
       
  1990 {
       
  1991     ASSERT(!IsShell());
       
  1992     ASSERT(iParent!=NULL);
       
  1993     iParent->MoveChildAbove(*this, aControl);
       
  1994 }
       
  1995 
       
  1996 EXPORT_C void ASwtControlBase::MoveBelow(const MSwtControl* aControl)
       
  1997 {
       
  1998     ASSERT(!IsShell());
       
  1999     ASSERT(iParent!=NULL);
       
  2000     iParent->MoveChildBelow(*this, aControl);
       
  2001 }
       
  2002 
       
  2003 EXPORT_C void ASwtControlBase::Redraw()
       
  2004 {
       
  2005     RedrawRegion(TRect(TPoint(0, 0), CoeControl().Size()));
       
  2006 }
       
  2007 
       
  2008 // aRect is in Control coords / relative to itself!
       
  2009 EXPORT_C void ASwtControlBase::RedrawRegion(const TRect& aRect)
       
  2010 {
       
  2011     if (!GetVisibleRecursively() || (iCtrlFlags & MSwtControl::EFlagDoNotDraw))
       
  2012     {
       
  2013         return;
       
  2014     }
       
  2015 
       
  2016     CCoeControl& coeCtrl = CoeControl();
       
  2017     TRect rectInWindow(aRect);
       
  2018     if (!coeCtrl.OwnsWindow())
       
  2019     {
       
  2020         rectInWindow.Move(coeCtrl.Position());   // In window coords
       
  2021     }
       
  2022     TRect dirtyRect(VisibleRect(ETrue));     // In window coords
       
  2023     dirtyRect.Intersection(rectInWindow);    // In window coords
       
  2024     if (dirtyRect.IsEmpty())
       
  2025     {
       
  2026         return;
       
  2027     }
       
  2028 
       
  2029     MSwtShell& shell = GetShell();
       
  2030     MSwtControl* urgentPaintControl = shell.UrgentPaintControl();
       
  2031     if (urgentPaintControl)
       
  2032     {
       
  2033         // Redraw requests outside urgently painted controls are ignored.
       
  2034         if (urgentPaintControl == this || IsDescentOf(*urgentPaintControl))
       
  2035         {
       
  2036             PaintUrgently(dirtyRect);
       
  2037         }
       
  2038     }
       
  2039     else
       
  2040     {
       
  2041         if (shell.AsyncPainting())
       
  2042         {
       
  2043             TRect ctrlRect(coeCtrl.Rect());
       
  2044             MSwtControl* controlShell = shell.Control();
       
  2045             TSwtPeer peerOfTheTargetControl(controlShell->JavaPeer());
       
  2046             if (RectContainsRect(ctrlRect, dirtyRect))
       
  2047             {
       
  2048                 peerOfTheTargetControl = JavaPeer();
       
  2049             }
       
  2050 
       
  2051             dirtyRect.Move(controlShell->CoeControl().PositionRelativeToScreen());   // Screen coords
       
  2052             TBool mergeable = !(iStyle & KSwtStyleNoMergePaints);
       
  2053             TRAP_IGNORE(iDisplay.PostPaintEventL(JavaPeer(), peerOfTheTargetControl, dirtyRect, mergeable));
       
  2054         }
       
  2055         else
       
  2056         {
       
  2057             PaintUrgently(dirtyRect);
       
  2058         }
       
  2059     }
       
  2060 }
       
  2061 
       
  2062 EXPORT_C void ASwtControlBase::SetRedraw(TBool aRedraw)
       
  2063 {
       
  2064     if (aRedraw)
       
  2065     {
       
  2066         if (iStackedSetRedrawFalseNb > 0)
       
  2067         {
       
  2068             --iStackedSetRedrawFalseNb;
       
  2069         }
       
  2070     }
       
  2071     else
       
  2072     {
       
  2073         ++iStackedSetRedrawFalseNb;
       
  2074     }
       
  2075 
       
  2076     UpdateDoNotDrawFlag();
       
  2077 
       
  2078     if (!(iCtrlFlags & MSwtControl::EFlagDoNotDraw))
       
  2079     {
       
  2080 
       
  2081         //For Shell the drawing are doing in Java Side.
       
  2082         if (!IsShell())
       
  2083         {
       
  2084             // We mark this Control as needed to be redraw.
       
  2085             // Redraw() is prefered instead DrawDeferred() because it directly post a
       
  2086             // PaintEvent on this Control whereas DrawDeferred
       
  2087             // notify the Windows server that the windows container need to be redraw.
       
  2088             Redraw();
       
  2089         }
       
  2090     }
       
  2091 }
       
  2092 
       
  2093 TBool ASwtControlBase::ParentHasDoNotDrawFlag()
       
  2094 {
       
  2095     MSwtControl* parent = NULL;
       
  2096 
       
  2097     if (iParent!=NULL)
       
  2098         parent = iParent->Control();
       
  2099 
       
  2100     while (parent != NULL)
       
  2101     {
       
  2102         if (parent->Flags() & MSwtControl::EFlagDoNotDraw)
       
  2103         {
       
  2104             return ETrue;
       
  2105         }
       
  2106         else if (!parent->GetParent() || parent->IsShell())
       
  2107         {
       
  2108             return EFalse;
       
  2109         }
       
  2110         else
       
  2111         {
       
  2112             parent = parent->GetParent()->Control();
       
  2113         }
       
  2114     }
       
  2115 
       
  2116     return EFalse;
       
  2117 }
       
  2118 
       
  2119 EXPORT_C TBool ASwtControlBase::HasDoNotDrawFlag() const
       
  2120 {
       
  2121     return (iCtrlFlags & MSwtControl::EFlagDoNotDraw);
       
  2122 }
       
  2123 
       
  2124 EXPORT_C void ASwtControlBase::UpdateDoNotDrawFlag()
       
  2125 {
       
  2126     if (iStackedSetRedrawFalseNb == 0 && !ParentHasDoNotDrawFlag())
       
  2127     {
       
  2128         iCtrlFlags &= ~MSwtControl::EFlagDoNotDraw;
       
  2129     }
       
  2130     else
       
  2131     {
       
  2132         iCtrlFlags |= MSwtControl::EFlagDoNotDraw;
       
  2133     }
       
  2134 }
       
  2135 
       
  2136 EXPORT_C void ASwtControlBase::SetFontL(const MSwtFont* aFont)
       
  2137 {
       
  2138     DoSetFontL(aFont);
       
  2139     Redraw();
       
  2140 }
       
  2141 
       
  2142 void ASwtControlBase::DoSetFontL(const MSwtFont* aFont)
       
  2143 {
       
  2144     if (iFont != NULL)
       
  2145         iFont->RemoveRef();
       
  2146     iFont = aFont;
       
  2147     if (iFont != NULL)
       
  2148         iFont->AddRef();
       
  2149 }
       
  2150 
       
  2151 EXPORT_C void ASwtControlBase::SetVisible(TBool aVisible)
       
  2152 {
       
  2153     CCoeControl& coeCtrl = CoeControl();
       
  2154     MSwtShell& shell = GetShell();
       
  2155     TBool redrawFocusedCtrl = (&shell == UiUtils().GetActiveShell())
       
  2156                               && (shell.FocusControl() != this);
       
  2157     TInt oldCount = 0;
       
  2158     TInt newCount = 0;
       
  2159 
       
  2160     if (redrawFocusedCtrl)
       
  2161     {
       
  2162         oldCount = shell.Composite()->CountFocusableChildren(2);
       
  2163     }
       
  2164 
       
  2165     if (aVisible)
       
  2166     {
       
  2167         iCtrlFlags &= ~MSwtControl::EFlagInvisible;
       
  2168     }
       
  2169     else
       
  2170     {
       
  2171         if (shell.UrgentPaintControl() == this)
       
  2172         {
       
  2173             shell.SetUrgentPaintControl(NULL);
       
  2174         }
       
  2175     }
       
  2176 
       
  2177     coeCtrl.MakeVisible(aVisible);
       
  2178 
       
  2179     if (!aVisible)
       
  2180         iCtrlFlags |= MSwtControl::EFlagInvisible;
       
  2181 
       
  2182     if (aVisible)
       
  2183     {
       
  2184         Redraw();
       
  2185     }
       
  2186     else
       
  2187     {
       
  2188         Invalidate(coeCtrl.Rect());
       
  2189     }
       
  2190 
       
  2191     if (!IsShell())
       
  2192     {
       
  2193         FocusabilityChanged();
       
  2194     }
       
  2195 
       
  2196     if (redrawFocusedCtrl)
       
  2197     {
       
  2198         newCount = shell.Composite()->CountFocusableChildren(2);
       
  2199     }
       
  2200 
       
  2201     if (redrawFocusedCtrl
       
  2202             && ((oldCount >= 2 && newCount == 1)
       
  2203                 || (oldCount == 1 && newCount >= 2)))
       
  2204     {
       
  2205         MSwtControl* focusedCtrl = GetShell().FocusControl();
       
  2206         if (focusedCtrl)
       
  2207         {
       
  2208             static_cast<ASwtControlBase*>(focusedCtrl)->RedrawFocusBackground();
       
  2209         }
       
  2210     }
       
  2211 }
       
  2212 
       
  2213 EXPORT_C void ASwtControlBase::SetLocation(const TPoint& aPoint)
       
  2214 {
       
  2215     ASSERT(!IsShell());
       
  2216 
       
  2217     TPoint oldLocation(GetLocation());
       
  2218     TPoint newLocation = ComputeLocationToSet(aPoint);
       
  2219     CCoeControl& coeCtrl = CoeControl();
       
  2220     TRect oldRect(coeCtrl.Rect());
       
  2221     TRect newRect(newLocation, coeCtrl.Size());
       
  2222 
       
  2223     if (oldLocation == newLocation)
       
  2224     {
       
  2225         return;
       
  2226     }
       
  2227 
       
  2228     MSwtShell& shell = GetShell();
       
  2229     if (shell.UrgentPaintControl() == this)
       
  2230     {
       
  2231         shell.SetUrgentPaintControl(NULL);
       
  2232     }
       
  2233 
       
  2234     TBool urgent = EFalse;
       
  2235     if (iParent->ScrolledCompositeContent() == this
       
  2236             && shell.UrgentPaintControl() == iParent->Control())
       
  2237     {
       
  2238         if (iParent->ScrolledCompositePysicsAction() != KSwtPhysicsNoAction)
       
  2239         {
       
  2240             // Cannot change the bounds of the content while flicking or panning
       
  2241             return;
       
  2242         }
       
  2243         urgent = ETrue;
       
  2244     }
       
  2245 
       
  2246     if (!urgent)
       
  2247     {
       
  2248         // Do not invalidate outside parent's visible client area.
       
  2249         if (iParent)
       
  2250         {
       
  2251             TRect parentVisibleRect(static_cast<ASwtControlBase*>(
       
  2252                                         iParent->Control())->VisibleRect());
       
  2253             oldRect.Intersection(parentVisibleRect);
       
  2254             newRect.Intersection(parentVisibleRect);
       
  2255         }
       
  2256 
       
  2257         // Invalidate uncovered regions.
       
  2258         if (newRect.Intersects(oldRect))
       
  2259         {
       
  2260             RRegion invalidRegion;
       
  2261             invalidRegion.Clear();
       
  2262             invalidRegion.AddRect(oldRect);
       
  2263             invalidRegion.SubRect(newRect);
       
  2264             ASSERT(invalidRegion.Count() <= 2);
       
  2265             for (TInt i = 0; i < invalidRegion.Count(); i++)
       
  2266             {
       
  2267                 Invalidate(invalidRegion[i]);
       
  2268             }
       
  2269             invalidRegion.Close();
       
  2270         }
       
  2271         else
       
  2272         {
       
  2273             Invalidate(oldRect);
       
  2274         }
       
  2275     }
       
  2276 
       
  2277     // Set and redraw new rect
       
  2278     coeCtrl.SetPosition(newLocation);
       
  2279     if (!(iStyle & KSwtStyleNoRedrawResize))
       
  2280     {
       
  2281         if (urgent)
       
  2282         {
       
  2283             iParent->Control()->PaintUrgently(iParent->Control()->CoeControl().Rect());
       
  2284         }
       
  2285         else
       
  2286         {
       
  2287             Redraw();
       
  2288         }
       
  2289     }
       
  2290 }
       
  2291 
       
  2292 EXPORT_C void ASwtControlBase::SetEnabled(TBool aEnabled)
       
  2293 {
       
  2294     CCoeControl& coeCtrl = CoeControl();
       
  2295     MSwtShell& shell = GetShell();
       
  2296     TBool redrawFocusedCtrl = (&shell == UiUtils().GetActiveShell())
       
  2297                               && (shell.FocusControl() != this);
       
  2298     TInt oldCount = 0;
       
  2299     TInt newCount = 0;
       
  2300 
       
  2301     if (redrawFocusedCtrl)
       
  2302     {
       
  2303         oldCount = shell.Composite()->CountFocusableChildren(2);
       
  2304     }
       
  2305 
       
  2306     if (aEnabled)
       
  2307     {
       
  2308         iCtrlFlags &= ~MSwtControl::EFlagDisabled;
       
  2309     }
       
  2310 
       
  2311     coeCtrl.SetFocusing(aEnabled);
       
  2312     coeCtrl.SetDimmed(!aEnabled);
       
  2313 
       
  2314     if (!aEnabled)
       
  2315         iCtrlFlags |= MSwtControl::EFlagDisabled;
       
  2316 
       
  2317     if (!IsShell())
       
  2318     {
       
  2319         // If we got disabled this will find a new focused item.
       
  2320         FocusabilityChanged();
       
  2321     }
       
  2322 
       
  2323     Redraw();
       
  2324 
       
  2325     if (redrawFocusedCtrl)
       
  2326     {
       
  2327         newCount = shell.Composite()->CountFocusableChildren(2);
       
  2328     }
       
  2329 
       
  2330     if (redrawFocusedCtrl
       
  2331             && ((oldCount >= 2 && newCount == 1)
       
  2332                 || (oldCount == 1 && newCount >= 2)))
       
  2333     {
       
  2334         MSwtControl* focusedCtrl = GetShell().FocusControl();
       
  2335         if (focusedCtrl)
       
  2336         {
       
  2337             static_cast<ASwtControlBase*>(focusedCtrl)->RedrawFocusBackground();
       
  2338         }
       
  2339     }
       
  2340 }
       
  2341 
       
  2342 EXPORT_C void ASwtControlBase::SetBounds(const TRect& aRect)
       
  2343 {
       
  2344     // Must NOT call SetExtent if we want component controls to be relocated.
       
  2345     ASSERT(!IsShell());
       
  2346 
       
  2347     CCoeControl& coeCtrl = CoeControl();
       
  2348     TSize oldSize(coeCtrl.Size());
       
  2349     TSize newSize(aRect.Size());
       
  2350     TPoint oldLocation(GetLocation());
       
  2351     TPoint newLocation(ComputeLocationToSet(aRect.iTl));
       
  2352     TRect oldRect(coeCtrl.Rect());
       
  2353     TRect newRect(newLocation, newSize);
       
  2354 
       
  2355     if (oldLocation == newLocation && oldSize == newSize)
       
  2356     {
       
  2357         return;
       
  2358     }
       
  2359 
       
  2360     MSwtShell& shell = GetShell();
       
  2361     if (shell.UrgentPaintControl() == this)
       
  2362     {
       
  2363         shell.SetUrgentPaintControl(NULL);
       
  2364     }
       
  2365 
       
  2366     TBool urgent = EFalse;
       
  2367     if (iParent->ScrolledCompositeContent() == this
       
  2368             && shell.UrgentPaintControl() == iParent->Control())
       
  2369     {
       
  2370         if (iParent->ScrolledCompositePysicsAction() != KSwtPhysicsNoAction)
       
  2371         {
       
  2372             // Cannot change the bounds of the content while flicking or panning
       
  2373             return;
       
  2374         }
       
  2375         urgent = ETrue;
       
  2376     }
       
  2377 
       
  2378     if (!urgent)
       
  2379     {
       
  2380         // Do not invalidate outside parent's visible client area.
       
  2381         if (iParent)
       
  2382         {
       
  2383             TRect parentVisibleRect(static_cast<ASwtControlBase*>(
       
  2384                                         iParent->Control())->VisibleRect());
       
  2385             oldRect.Intersection(parentVisibleRect);
       
  2386             newRect.Intersection(parentVisibleRect);
       
  2387         }
       
  2388 
       
  2389         // Invalidate uncovered regions.
       
  2390         if (newRect.Intersects(oldRect))
       
  2391         {
       
  2392             RRegion invalidRegion;
       
  2393             invalidRegion.Clear();
       
  2394             invalidRegion.AddRect(oldRect);
       
  2395             invalidRegion.SubRect(newRect);
       
  2396             ASSERT(invalidRegion.Count() <= 4);
       
  2397             for (TInt i = 0; i < invalidRegion.Count(); i++)
       
  2398             {
       
  2399                 Invalidate(invalidRegion[i]);
       
  2400             }
       
  2401             invalidRegion.Close();
       
  2402         }
       
  2403         else
       
  2404         {
       
  2405             Invalidate(oldRect);
       
  2406         }
       
  2407     }
       
  2408 
       
  2409     // Set and invalidate new rect
       
  2410     if (oldLocation != newLocation)
       
  2411     {
       
  2412         coeCtrl.SetPosition(newLocation);
       
  2413     }
       
  2414     if (oldSize != newSize)
       
  2415     {
       
  2416         coeCtrl.SetSize(newSize);
       
  2417     }
       
  2418 
       
  2419     if (!(iStyle & KSwtStyleNoRedrawResize))
       
  2420     {
       
  2421         if (urgent)
       
  2422         {
       
  2423             iParent->Control()->PaintUrgently(iParent->Control()->CoeControl().Rect());
       
  2424         }
       
  2425         else
       
  2426         {
       
  2427             Redraw();
       
  2428         }
       
  2429     }
       
  2430 }
       
  2431 
       
  2432 EXPORT_C void ASwtControlBase::SetPopupMenuL(const MSwtMenu* aMenu)
       
  2433 {
       
  2434     if (aMenu)
       
  2435     {
       
  2436         GetShell().InstallCba();
       
  2437     }
       
  2438 
       
  2439     const MSwtMenu* oldMenu = iPopupMenu;
       
  2440     iPopupMenu = aMenu;
       
  2441 
       
  2442     if (iPopupMenu == NULL && oldMenu != NULL)
       
  2443     {
       
  2444         iDisplay.MenuArranger().HandleMenuChangedL(*oldMenu, MSwtMenuArranger::ESwtMenuRemoved);
       
  2445     }
       
  2446     else if (iPopupMenu != NULL)
       
  2447     {
       
  2448         iDisplay.MenuArranger().HandleMenuChangedL(*iPopupMenu, MSwtMenuArranger::ESwtMenuAdded);
       
  2449     }
       
  2450 }
       
  2451 
       
  2452 EXPORT_C void ASwtControlBase::SetCapture(TBool aCapture)
       
  2453 {
       
  2454     if (aCapture)
       
  2455     {
       
  2456         UiUtils().SetPointerCaptureControl(this);
       
  2457     }
       
  2458     else
       
  2459     {
       
  2460         if (UiUtils().PointerCaptureControl() == this)
       
  2461         {
       
  2462             UiUtils().SetPointerCaptureControl(NULL);
       
  2463         }
       
  2464     }
       
  2465 }
       
  2466 
       
  2467 EXPORT_C TInt ASwtControlBase::Style() const
       
  2468 {
       
  2469     return iStyle;
       
  2470 }
       
  2471 
       
  2472 EXPORT_C void ASwtControlBase::Update() const
       
  2473 {
       
  2474     if (iCtrlFlags & MSwtControl::EFlagDoNotDraw)
       
  2475         return;
       
  2476 }
       
  2477 
       
  2478 EXPORT_C void ASwtControlBase::ComputeTabListL(RSwtControlArray& aList)
       
  2479 {
       
  2480     User::LeaveIfError(aList.Append(this));
       
  2481 }
       
  2482 
       
  2483 EXPORT_C TPoint ASwtControlBase::ToControl(const TPoint& aPoint) const
       
  2484 {
       
  2485     const TPoint clientOriginInScreen(WindowToScreen(ClientRect().iTl));
       
  2486     return aPoint - clientOriginInScreen;
       
  2487 }
       
  2488 
       
  2489 EXPORT_C TPoint ASwtControlBase::ToDisplay(const TPoint& aPoint) const
       
  2490 {
       
  2491     const TPoint clientOriginInScreen(WindowToScreen(ClientRect().iTl));
       
  2492     return aPoint + clientOriginInScreen;
       
  2493 }
       
  2494 
       
  2495 EXPORT_C TBool ASwtControlBase::CbaButtonIsUsed(TInt /*aCbaButtonIndex*/) const
       
  2496 {
       
  2497     return EFalse;
       
  2498 }
       
  2499 
       
  2500 EXPORT_C TBool ASwtControlBase::IsKeyUsed(TUint /*aKeyCode*/) const
       
  2501 {
       
  2502     // By default, we assume that the control doesn't use key
       
  2503     return EFalse;
       
  2504 }
       
  2505 
       
  2506 EXPORT_C TBool ASwtControlBase::MSKSelCmdEnabled() const
       
  2507 {
       
  2508     return EFalse;
       
  2509 }
       
  2510 
       
  2511 EXPORT_C TBool ASwtControlBase::ForceTraverseEvent(TSwtTraversal aDetail, TBool aDoIt)
       
  2512 {
       
  2513     TRAPD(ignore, iDisplay.ForceTraverseEventL(*this, aDetail, aDoIt));
       
  2514     return (ignore == KErrNone);
       
  2515 }
       
  2516 
       
  2517 EXPORT_C void ASwtControlBase::AddToControlStackL(TInt aPriority)
       
  2518 {
       
  2519     if (!iIsOnControlStack)
       
  2520     {
       
  2521         iIsOnControlStack = ETrue;
       
  2522         TRAPD(error, iDisplay.CoeEnv()->EikAppUi()->AddToStackL(&CoeControl(), aPriority));
       
  2523         if (error!=KErrNone)
       
  2524         {
       
  2525             iIsOnControlStack = EFalse;
       
  2526             User::Leave(error);
       
  2527         }
       
  2528         if (!IsShell())
       
  2529         {
       
  2530             GetShell().SetControlGoingToStack(this);
       
  2531             GetShell().SetControlGainingFocus(NULL);
       
  2532         }
       
  2533     }
       
  2534 }
       
  2535 EXPORT_C HBufC* ASwtControlBase::MSKLabelL() const
       
  2536 {
       
  2537     return KNullDesC().Alloc();
       
  2538 }
       
  2539 
       
  2540 EXPORT_C void ASwtControlBase::RemoveFromControlStack()
       
  2541 {
       
  2542     if (iIsOnControlStack)
       
  2543     {
       
  2544         iDisplay.CoeEnv()->EikAppUi()->RemoveFromStack(&CoeControl());
       
  2545         iIsOnControlStack = EFalse;
       
  2546         if (!IsShell())
       
  2547         {
       
  2548             if (
       
  2549                 iDisplay.MenuArranger().EikMenuBar()->IsDisplayed() ||
       
  2550                 iDisplay.CoeEnv()->EikAppUi()->IsDisplayingControlBetweenPriorities(
       
  2551                     ECoeStackPriorityMenu, ECoeStackPriorityCba))
       
  2552             {
       
  2553                 GetShell().SetControlGainingFocus(this);
       
  2554             }
       
  2555         }
       
  2556     }
       
  2557 }
       
  2558 
       
  2559 EXPORT_C TBool ASwtControlBase::IsOnControlStack() const
       
  2560 {
       
  2561     return iIsOnControlStack;
       
  2562 }
       
  2563 
       
  2564 #ifdef RD_SCALABLE_UI_V2
       
  2565 EXPORT_C void ASwtControlBase::HandlePointerEventL(const TPointerEvent& aPointerEvent)
       
  2566 {
       
  2567     CoeControl().HandlePointerEventL(aPointerEvent);
       
  2568     PostMouseEventL(aPointerEvent);
       
  2569 }
       
  2570 #else
       
  2571 EXPORT_C void ASwtControlBase::HandlePointerEventL(const TPointerEvent& /*aPointerEvent*/)
       
  2572 {
       
  2573     ASSERT(EFalse);
       
  2574 }
       
  2575 #endif
       
  2576 
       
  2577 #ifdef RD_SCALABLE_UI_V2
       
  2578 EXPORT_C TBool ASwtControlBase::IsLongTapAnimationCandidate(const TPointerEvent& aPointerEvent) const
       
  2579 {
       
  2580     return (ClientRect().Contains(aPointerEvent.iPosition)
       
  2581             && iDisplay.CommandArranger()->ControlHasStylusPopupMenu(*this));
       
  2582 }
       
  2583 #else
       
  2584 EXPORT_C TBool ASwtControlBase::IsLongTapAnimationCandidate(const TPointerEvent& /*aPointerEvent*/) const
       
  2585 {
       
  2586     return EFalse;
       
  2587 }
       
  2588 #endif
       
  2589 
       
  2590 TBool ASwtControlBase::IsFocusedOrChildIsFocused() const
       
  2591 {
       
  2592     return (GetShell().FocusControl() == this) || ChildIsFocused();
       
  2593 }
       
  2594 
       
  2595 TBool ASwtControlBase::ChildIsFocused() const
       
  2596 {
       
  2597     MSwtControl* focusedControl = GetShell().FocusControl();
       
  2598     return focusedControl && focusedControl->IsDescentOf(*this);
       
  2599 }
       
  2600 
       
  2601 EXPORT_C void ASwtControlBase::DoControlSpecificFeedback(
       
  2602     const TBool& /*aFirstTap*/, const TBool& /*aTappedToChildRect*/,
       
  2603     const TPointerEvent& /*aPointerEvent*/) const
       
  2604 {
       
  2605     //Do nothing here. Each child class will implement
       
  2606     //this function according to its needs.
       
  2607     //Note that this function is not called by platform.
       
  2608 }