uiacceltk/hitchcock/Client/src/alfcontrol.cpp
changeset 0 15bf7259bb7c
equal deleted inserted replaced
-1:000000000000 0:15bf7259bb7c
       
     1 /*
       
     2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Control base class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "alf/alfcontrol.h"
       
    21 #include "alf/alfcontrolgroup.h"
       
    22 #include "alf/alfdisplay.h"
       
    23 #include "alf/alfroster.h"
       
    24 #include "alf/alfenv.h"
       
    25 #include "alf/alfvisualfactory.h"
       
    26 #include "alfclient.h"
       
    27 #include "alf/alfgencomponent.h"
       
    28 #include "alf/alfconstants.h"
       
    29 #include "alf/alftimedvalue.h"
       
    30 #include "alflogger.h"
       
    31 
       
    32 #include <uiacceltk/HuiUtil.h>
       
    33 
       
    34 #include <eikappui.h>
       
    35 
       
    36 // Private structure
       
    37 struct CAlfControl::TPrivateData
       
    38     {
       
    39     CAlfEnv* iEnv;                      // Not owned.
       
    40     CAlfControlGroup* iOwnerGroup;      // Not owned.
       
    41     CAlfDisplay* iBoundDisplay;         // Not owned.
       
    42     TInt iId;                           // Owned.
       
    43     TInt iHostId;                       // Owned.
       
    44     TInt iRole;                         // Owned.
       
    45     RPointerArray<CAlfVisual> iVisuals; // Not owned.
       
    46     CAlfControl* iHost;                 // Not owned.
       
    47     struct SConnection
       
    48         {
       
    49         CAlfControl* iControl;          // Not owned.
       
    50         TInt iRole;                     // Owned.
       
    51         };
       
    52     RArray<SConnection> iConnections;   // Owned.
       
    53     TBool iFocusing;                    // Owned.
       
    54     TBool iHasFocus;                    // Owned.
       
    55     CAlfGenComponent* iComms;           // Owned
       
    56     };
       
    57 
       
    58 
       
    59 // ======== MEMBER FUNCTIONS ========
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // Constructor
       
    63 // ---------------------------------------------------------------------------
       
    64 //
       
    65 EXPORT_C CAlfControl::CAlfControl()
       
    66     {
       
    67     }
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // ConstructL
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 EXPORT_C void CAlfControl::ConstructL(CAlfEnv& aEnv)
       
    74     {
       
    75     iData = new (ELeave) TPrivateData;
       
    76     
       
    77     iData->iEnv = &aEnv;
       
    78     iData->iFocusing = EFalse;
       
    79     //iData->iId = CAlfStatic::GenerateId();  
       
    80         
       
    81     // NULL data
       
    82     iData->iOwnerGroup = NULL;
       
    83     iData->iBoundDisplay = NULL;
       
    84     iData->iId = 0;
       
    85     iData->iHostId = 0;
       
    86     iData->iRole = 0;
       
    87     iData->iVisuals.Reset();
       
    88     iData->iHost = NULL;
       
    89     iData->iConnections.Reset();
       
    90     iData->iHasFocus = EFalse;
       
    91     iData->iComms = 0;
       
    92     iData->iComms = CAlfGenComponent::NewL(aEnv,
       
    93             EAlfCntrlCreate, 
       
    94             0, 
       
    95             KNullDesC8);
       
    96 
       
    97     iData->iId = Identifier(); // to have unique value 
       
    98     }
       
    99 
       
   100 // ---------------------------------------------------------------------------
       
   101 // Destructor.
       
   102 // ---------------------------------------------------------------------------
       
   103 //
       
   104 EXPORT_C CAlfControl::~CAlfControl()
       
   105     {
       
   106     if ( iData )
       
   107         { 
       
   108         // Cancel scheduled commands for this control.
       
   109         Env().CancelCustomCommands(this);
       
   110         Env().CancelCommands(this);
       
   111 
       
   112         if(iData->iHost)
       
   113             {
       
   114             iData->iHost->RemoveConnection(this);
       
   115             iData->iHost = NULL;
       
   116             }
       
   117 
       
   118         for(TInt i = iData->iConnections.Count() - 1; i >= 0; --i)
       
   119             {
       
   120             RemoveConnection(iData->iConnections[i].iControl);
       
   121             }
       
   122         iData->iConnections.Reset();
       
   123 
       
   124         // The visuals are owned by the control.
       
   125         iData->iVisuals.ResetAndDestroy();
       
   126         delete iData->iComms;
       
   127         }
       
   128     delete iData;
       
   129     iData = NULL;
       
   130     }
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 // Returns server handle
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 EXPORT_C TInt CAlfControl::Identifier() const
       
   137     {
       
   138     return iData->iComms->Identifier();
       
   139     }
       
   140 
       
   141 // ---------------------------------------------------------------------------
       
   142 // Returns the env
       
   143 // ---------------------------------------------------------------------------
       
   144 //
       
   145 EXPORT_C CAlfEnv& CAlfControl::Env() const
       
   146     {
       
   147     return *iData->iEnv;
       
   148     }
       
   149 
       
   150 
       
   151 // ---------------------------------------------------------------------------
       
   152 // Returns control group that this control is in.
       
   153 // ---------------------------------------------------------------------------
       
   154 //
       
   155 EXPORT_C CAlfControlGroup* CAlfControl::ControlGroup() const
       
   156     {
       
   157     return iData->iOwnerGroup;
       
   158     }
       
   159     
       
   160 // ---------------------------------------------------------------------------
       
   161 // Sets the control group
       
   162 // ---------------------------------------------------------------------------
       
   163 //   
       
   164 void CAlfControl::SetControlGroup(CAlfControlGroup& aOwnerGroup)
       
   165     {
       
   166     iData->iOwnerGroup = &aOwnerGroup;
       
   167     }
       
   168     
       
   169 // ---------------------------------------------------------------------------
       
   170 // Returns bound display
       
   171 // ---------------------------------------------------------------------------
       
   172 //     
       
   173 EXPORT_C CAlfDisplay* CAlfControl::Display() const
       
   174     {
       
   175     return iData->iBoundDisplay;
       
   176     }
       
   177     
       
   178 // ---------------------------------------------------------------------------
       
   179 // Sets bound display
       
   180 // ---------------------------------------------------------------------------
       
   181 //   
       
   182 EXPORT_C void CAlfControl::BindDisplay(CAlfDisplay& aDisplay)
       
   183     {
       
   184     iData->iBoundDisplay = &aDisplay;
       
   185     }    
       
   186 
       
   187 // ---------------------------------------------------------------------------
       
   188 // Sets control ID.
       
   189 // ---------------------------------------------------------------------------
       
   190 //
       
   191 EXPORT_C void CAlfControl::SetId(TInt aId)
       
   192     {
       
   193     iData->iId = aId;
       
   194     }
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // Returns control ID.
       
   198 // ---------------------------------------------------------------------------
       
   199 //
       
   200 EXPORT_C TInt CAlfControl::Id() const
       
   201     {
       
   202     return iData->iId;
       
   203     }
       
   204 
       
   205 // ---------------------------------------------------------------------------
       
   206 // Appends new visual
       
   207 // ---------------------------------------------------------------------------
       
   208 //
       
   209 EXPORT_C TInt CAlfControl::Append(CAlfVisual* aVisual, CAlfLayout* aParentLayout)
       
   210     {
       
   211     TInt err = KErrNone;
       
   212     
       
   213     ASSERT(aVisual); // must not be null
       
   214     
       
   215     if(&aVisual->Owner() != this) // ownership cannot be altered here
       
   216         {
       
   217         return KErrNotSupported;
       
   218         }
       
   219     
       
   220     // prevent adding visual several times
       
   221     for (TInt i = iData->iVisuals.Count()-1;i>=0;i--)
       
   222         {
       
   223         if (iData->iVisuals[i] == aVisual)
       
   224             {
       
   225             return KErrAlreadyExists;
       
   226             }
       
   227         }
       
   228 
       
   229     // Append the visual to the visuals array.
       
   230     err = iData->iVisuals.Append(aVisual);
       
   231     if ( err )
       
   232         {
       
   233         __ALFLOGSTRING1( "CAlfControl::Append return error %d", err )
       
   234         return err;
       
   235         }
       
   236 
       
   237     if(aParentLayout)
       
   238         { // once parent layout has been set, the visual has already relation in shape 
       
   239           // server side, tell the layout that theres no need to synch that info to server 
       
   240           // anymore (latter param)
       
   241         err = aParentLayout->Append(aVisual, ETrue);  
       
   242         if ( err != KErrNone )
       
   243             {
       
   244             __ALFLOGSTRING1( "CAlfControl::Append return error %d", err )
       
   245             // Do cleanup
       
   246             iData->iVisuals.Remove(iData->iVisuals.Find(aVisual));
       
   247             return err;
       
   248             }
       
   249         }
       
   250      /*   
       
   251     if ( iData->iInformServerAboutAppendRemove )
       
   252         {
       
   253         TInt2 params(aVisual->Identifier(), aParentLayout ? aParentLayout ->Identifier() : 0 );
       
   254         TPckgC<TInt2> inPckg(params);
       
   255         TBuf8<1> dum;
       
   256         TInt err = iData->iComms->DoSynchronousCmd( EAlfCntrlAppendVisual, inPckg , dum );
       
   257         if ( err != KErrNone )
       
   258             {
       
   259             __ALFLOGSTRING1( "CAlfControl::Append return error %d", err )
       
   260             // Do cleanup
       
   261             if ( aParentLayout )
       
   262                 {
       
   263                 aParentLayout->Remove(aVisual);  
       
   264                 }
       
   265             iData->iVisuals.Remove(iData->iVisuals.Find(aVisual));
       
   266             return err;
       
   267             }
       
   268         }*/
       
   269 
       
   270     TRAP(err, VisualAddedL(aVisual));
       
   271     if ( err != KErrNone )
       
   272         {
       
   273         __ALFLOGSTRING1( "CAlfControl::Append return error %d", err )
       
   274         // Do cleanup
       
   275         if ( aParentLayout )
       
   276             {
       
   277             aParentLayout->Remove(aVisual);
       
   278             }
       
   279         Remove( aVisual );
       
   280         }
       
   281     return err;
       
   282     }
       
   283 
       
   284 // ---------------------------------------------------------------------------
       
   285 // Removes visual
       
   286 // ---------------------------------------------------------------------------
       
   287 //
       
   288 EXPORT_C void CAlfControl::Remove(CAlfVisual* aVisual)
       
   289     {
       
   290     /*
       
   291     if ( iData->iInformServerAboutAppendRemove && aVisual)
       
   292         {
       
   293         TPckgC<TInt> inPckg(aVisual->Identifier());
       
   294         TBuf8<1> dum;
       
   295         iData->iComms->DoSynchronousCmd( EAlfCntrlRemoveVisual, inPckg , dum );
       
   296         }*/
       
   297     
       
   298     TInt index = iData->iVisuals.Find(aVisual);
       
   299     if(index != KErrNotFound)
       
   300         {
       
   301         VisualRemoved(aVisual);
       
   302         iData->iVisuals.Remove(index);
       
   303         }
       
   304     }
       
   305 
       
   306 // --------------------------------------------------------------------------
       
   307 // Creates and appends new visual
       
   308 // ---------------------------------------------------------------------------
       
   309 //
       
   310 EXPORT_C CAlfVisual* CAlfControl::AppendVisualL(TAlfVisualType aVisualType,
       
   311                                                 CAlfLayout* aParentLayout,
       
   312                                                 TInt aImplementationUid )
       
   313     {
       
   314     CAlfVisual* visual = AlfVisualFactory::NewVisualLC(aVisualType, 
       
   315                                         aParentLayout, *this, *iData->iEnv, aImplementationUid);
       
   316                                                              
       
   317     TInt err = Append(visual, aParentLayout);
       
   318     User::LeaveIfError( err );
       
   319     CleanupStack::Pop(visual);
       
   320     return visual;
       
   321     }
       
   322 
       
   323 // ---------------------------------------------------------------------------
       
   324 // Creates and appends new layout
       
   325 // ---------------------------------------------------------------------------
       
   326 //
       
   327 EXPORT_C CAlfLayout* CAlfControl::AppendLayoutL(TAlfLayoutType aLayoutType,
       
   328                                                 CAlfLayout* aParentLayout,
       
   329                                                 TInt aImplementationUid)
       
   330     {
       
   331     CAlfLayout* layout = AlfVisualFactory::NewLayoutLC(aLayoutType,
       
   332                         aParentLayout, *this, *iData->iEnv, aImplementationUid);
       
   333      
       
   334     TInt err = Append(layout, aParentLayout);
       
   335     User::LeaveIfError( err );
       
   336     CleanupStack::Pop(layout);
       
   337     return layout;
       
   338     }
       
   339 
       
   340 // ---------------------------------------------------------------------------
       
   341 // Returns indexed visual
       
   342 // ---------------------------------------------------------------------------
       
   343 //
       
   344 EXPORT_C CAlfVisual& CAlfControl::Visual(TInt aIndex) const
       
   345     {
       
   346     return *iData->iVisuals[aIndex];
       
   347     }
       
   348 
       
   349 // ---------------------------------------------------------------------------
       
   350 // Returns visual count.
       
   351 // ---------------------------------------------------------------------------
       
   352 //
       
   353 EXPORT_C TInt CAlfControl::VisualCount() const
       
   354     {
       
   355     return iData->iVisuals.Count();
       
   356     }
       
   357 
       
   358 // ---------------------------------------------------------------------------
       
   359 // Finds visual with given tag.
       
   360 // ---------------------------------------------------------------------------
       
   361 //
       
   362 EXPORT_C CAlfVisual* CAlfControl::FindTag(const TDesC8& aTag) const
       
   363     {
       
   364     ASSERT(iData);
       
   365 
       
   366     for(TInt i = 0; i < iData->iVisuals.Count(); ++i)
       
   367         {
       
   368         if(iData->iVisuals[i]->TagMatches(aTag))
       
   369             {
       
   370             return iData->iVisuals[i];
       
   371             }
       
   372         }   
       
   373         
       
   374     return NULL;
       
   375     }
       
   376 
       
   377 
       
   378 // ---------------------------------------------------------------------------
       
   379 // Returns host.
       
   380 // ---------------------------------------------------------------------------
       
   381 //
       
   382 EXPORT_C CAlfControl* CAlfControl::Host() const
       
   383     {
       
   384     return iData->iHost;
       
   385     }
       
   386 
       
   387 // ---------------------------------------------------------------------------
       
   388 // Sets host.
       
   389 // ---------------------------------------------------------------------------
       
   390 //
       
   391 EXPORT_C void CAlfControl::SetHost(CAlfControl* aHost)
       
   392     {
       
   393     // do not call this function directly. This should be only called by the 
       
   394     // AddConnectionL and RemoveConnection functions.
       
   395     
       
   396     if ( aHost )
       
   397         {    
       
   398         // When adding a host, the host must be aware of this connection first.
       
   399         __ASSERT_ALWAYS( aHost->FindConnection(this) != KErrNotFound, USER_INVARIANT() );
       
   400         }
       
   401     
       
   402     TRAPD(err, HostChangingL(aHost));
       
   403     if(err != KErrNone)
       
   404         {
       
   405         if(aHost)
       
   406             {
       
   407             RemoveVisualsFromHostControl(*aHost);
       
   408             }
       
   409         return;
       
   410         }
       
   411 
       
   412     iData->iHost = aHost;
       
   413     }
       
   414 
       
   415 // ---------------------------------------------------------------------------
       
   416 // Adds control connection.
       
   417 // ---------------------------------------------------------------------------
       
   418 //
       
   419 EXPORT_C void CAlfControl::AddConnectionL(CAlfControl* aConnectedControl, TInt aRole)
       
   420     {
       
   421     // check that the connection does not exist:
       
   422     if ( FindConnection( aConnectedControl ) != KErrNotFound )
       
   423         {
       
   424         User::Leave( KErrAlreadyExists );
       
   425         }
       
   426     
       
   427     TPrivateData::SConnection client;
       
   428     client.iControl = aConnectedControl;
       
   429     client.iRole = aRole;
       
   430     User::LeaveIfError(iData->iConnections.Append(client));
       
   431 
       
   432     // This control is now the client's host.
       
   433     aConnectedControl->SetHost(this);
       
   434 
       
   435     ConnectionAddedL(aConnectedControl, aRole);
       
   436     }
       
   437 
       
   438 // ---------------------------------------------------------------------------
       
   439 // Removes control connection
       
   440 // ---------------------------------------------------------------------------
       
   441 //
       
   442 EXPORT_C void CAlfControl::RemoveConnection(CAlfControl* aConnectedControl)
       
   443     {
       
   444     for(TInt i = 0; i < iData->iConnections.Count(); ++i)
       
   445         {
       
   446         if(iData->iConnections[i].iControl == aConnectedControl)
       
   447             {
       
   448             aConnectedControl->SetHost(NULL);
       
   449             const TInt role = iData->iConnections[i].iRole;
       
   450             iData->iConnections.Remove(i);
       
   451             
       
   452             ConnectionRemoved(aConnectedControl, role);
       
   453             return;
       
   454             }
       
   455         }
       
   456     // The client must exist.
       
   457     USER_INVARIANT();
       
   458     }
       
   459 
       
   460 // ---------------------------------------------------------------------------
       
   461 // Returns connection count
       
   462 // ---------------------------------------------------------------------------
       
   463 //
       
   464 EXPORT_C TInt CAlfControl::ConnectionCount() const
       
   465     {
       
   466     return iData->iConnections.Count();
       
   467     }
       
   468 
       
   469 // ---------------------------------------------------------------------------
       
   470 // Returns indexed connection.
       
   471 // ---------------------------------------------------------------------------
       
   472 //
       
   473 EXPORT_C CAlfControl& CAlfControl::Connection(TInt aIndex) const
       
   474     {
       
   475     return *iData->iConnections[aIndex].iControl;
       
   476     }
       
   477 
       
   478 // ---------------------------------------------------------------------------
       
   479 // Returns ordinal-indexed connection.
       
   480 // @deprecated
       
   481 // ---------------------------------------------------------------------------
       
   482 //
       
   483 EXPORT_C CAlfControl& CAlfControl::ConnectionByOrdinal(TInt aOrdinal) const
       
   484     {
       
   485     // First look based on role.
       
   486     for(TInt i = 0; i < iData->iConnections.Count(); ++i)
       
   487         {
       
   488         if(iData->iConnections[i].iRole == aOrdinal + 1)
       
   489             {
       
   490             return *iData->iConnections[i].iControl;
       
   491             }
       
   492         }
       
   493 
       
   494     // Fall back to index.
       
   495     return Connection(aOrdinal);
       
   496     }
       
   497 
       
   498 // ---------------------------------------------------------------------------
       
   499 // Returns given connection index.
       
   500 // ---------------------------------------------------------------------------
       
   501 //
       
   502 EXPORT_C TInt CAlfControl::FindConnection(const CAlfControl* aConnected) const
       
   503     {
       
   504     for(TInt i = 0; i < iData->iConnections.Count(); ++i)
       
   505         {
       
   506         if(iData->iConnections[i].iControl == aConnected)
       
   507             {
       
   508             return i;
       
   509             }
       
   510         }
       
   511     return KErrNotFound;
       
   512     }
       
   513 
       
   514 // ---------------------------------------------------------------------------
       
   515 // Returns connection role
       
   516 // ---------------------------------------------------------------------------
       
   517 //
       
   518 EXPORT_C TInt CAlfControl::ConnectionRole(TInt aIndex) const
       
   519     {
       
   520     return iData->iConnections[aIndex].iRole;
       
   521     }
       
   522 
       
   523 // ---------------------------------------------------------------------------
       
   524 // Returns connection ordinal
       
   525 // @deprecated
       
   526 // ---------------------------------------------------------------------------
       
   527 //
       
   528 EXPORT_C TInt CAlfControl::ConnectionOrdinal(TInt aIndex) const
       
   529     {
       
   530     if(iData->iConnections[aIndex].iRole)
       
   531         {
       
   532         return iData->iConnections[aIndex].iRole - 1;
       
   533         }
       
   534     return aIndex;
       
   535     }
       
   536 
       
   537 // ---------------------------------------------------------------------------
       
   538 // Returns role
       
   539 // ---------------------------------------------------------------------------
       
   540 //
       
   541 EXPORT_C TInt CAlfControl::Role() const
       
   542     {
       
   543     /*if(iHost)
       
   544         {
       
   545         return iHost->ClientRole(iHost->FindClient(this));
       
   546         }
       
   547     // Zero is the default role.
       
   548     return 0;*/
       
   549     return iData->iRole;
       
   550     }
       
   551 
       
   552 // ---------------------------------------------------------------------------
       
   553 // Sets role
       
   554 // ---------------------------------------------------------------------------
       
   555 //
       
   556 EXPORT_C void CAlfControl::SetRole(TInt aRole)
       
   557     {
       
   558     iData->iRole = aRole;
       
   559     }
       
   560 
       
   561 // ---------------------------------------------------------------------------
       
   562 // Returns host ID.
       
   563 // ---------------------------------------------------------------------------
       
   564 //
       
   565 EXPORT_C TInt CAlfControl::HostId() const
       
   566     {
       
   567     return iData->iHostId;
       
   568     }
       
   569 
       
   570 // ---------------------------------------------------------------------------
       
   571 // Sets host ID
       
   572 // ---------------------------------------------------------------------------
       
   573 //
       
   574 EXPORT_C void CAlfControl::SetHostId(TInt aHostId)
       
   575     {
       
   576     // If adding automatic visual host, there cannot be already one defined.
       
   577     __ASSERT_ALWAYS( !iData->iHost, USER_INVARIANT() );
       
   578     
       
   579     iData->iHostId = aHostId;
       
   580     }
       
   581 
       
   582 // ---------------------------------------------------------------------------
       
   583 // Returns container layout if set.
       
   584 // ---------------------------------------------------------------------------
       
   585 //
       
   586 EXPORT_C CAlfLayout* CAlfControl::ContainerLayout(const CAlfControl* /*aConnected*/) const
       
   587     {
       
   588     // Generic controls aren't able to provide container layouts.
       
   589     return NULL;
       
   590     }
       
   591 
       
   592 // ---------------------------------------------------------------------------
       
   593 // Called when new visual is added.
       
   594 // ---------------------------------------------------------------------------
       
   595 //
       
   596 EXPORT_C void CAlfControl::VisualAddedL(CAlfVisual* aVisual)
       
   597     {
       
   598     // Add the new visual to the container layout.
       
   599     if(iData->iHost && !aVisual->Layout())
       
   600         {
       
   601         CAlfLayout* container = iData->iHost->ContainerLayout(this);
       
   602         if(container)
       
   603             {
       
   604             User::LeaveIfError( container->Append(aVisual) );
       
   605             }
       
   606         }
       
   607     else
       
   608         {
       
   609         // @todo Calling ShowVisualL panics the server, becuase it has
       
   610         // been called already on the construction.
       
   611         /*
       
   612         // If the control has been bound to a display (for example, when
       
   613         // the control is shown), new visuals will be automatically shown.
       
   614         if( iData->iBoundDisplay && 
       
   615                 (iData->iOwnerGroup && 
       
   616                  iData->iBoundDisplay->Roster().Find(
       
   617                     *iData->iOwnerGroup) != KErrNotFound) && 
       
   618            !aVisual->Layout() && !aVisual->Display())
       
   619             {
       
   620             iData->iBoundDisplay->Roster().ShowVisualL(*aVisual);
       
   621             }
       
   622             */
       
   623         }
       
   624     }
       
   625 
       
   626 // ---------------------------------------------------------------------------
       
   627 // Called when visual is removed.
       
   628 // ---------------------------------------------------------------------------
       
   629 //
       
   630 EXPORT_C void CAlfControl::VisualRemoved(CAlfVisual* aVisual)
       
   631     {
       
   632     // Add the new visual to the container layout.
       
   633     if(iData->iHost)
       
   634         {
       
   635         CAlfLayout* container = iData->iHost->ContainerLayout(this);
       
   636         if(container && aVisual->Layout() == container )
       
   637             {
       
   638             container->Remove(aVisual);
       
   639             }
       
   640         }
       
   641     }
       
   642 
       
   643 // ---------------------------------------------------------------------------
       
   644 // Called when connection is added.
       
   645 // ---------------------------------------------------------------------------
       
   646 //
       
   647 EXPORT_C void CAlfControl::ConnectionAddedL(CAlfControl* /*aConnectedControl*/, TInt /*aRole*/)
       
   648     {
       
   649     // Do nothing.
       
   650     }
       
   651 
       
   652 // ---------------------------------------------------------------------------
       
   653 // Called when connection is removed.
       
   654 // ---------------------------------------------------------------------------
       
   655 //
       
   656 EXPORT_C void CAlfControl::ConnectionRemoved(CAlfControl* /*aConnectedControl*/, TInt /*aRole*/)
       
   657     {
       
   658     // Do nothing.
       
   659     }
       
   660 
       
   661 // ---------------------------------------------------------------------------
       
   662 // Removes visual from layout.
       
   663 // ---------------------------------------------------------------------------
       
   664 //
       
   665 void CAlfControl::RemoveVisualsFromHostControl( CAlfControl& aHostControl )
       
   666     {
       
   667     __ASSERT_ALWAYS( &aHostControl != this, USER_INVARIANT() );
       
   668     
       
   669     // Remove the visuals.
       
   670     for(TInt i = 0; i < iData->iVisuals.Count(); ++i)
       
   671         {
       
   672         if ( iData->iVisuals[i]->Layout() && &iData->iVisuals[i]->Layout()->Owner() == &aHostControl )
       
   673             {
       
   674             iData->iVisuals[i]->Layout()->Remove(iData->iVisuals[i]);
       
   675             
       
   676             // If the own control group is showing still in some roster,
       
   677             // we need to add the root visuals over there
       
   678             /* // do we need this?
       
   679             if ( iBoundDisplay )
       
   680                 {
       
   681                 TRAP_IGNORE( iBoundDisplay->Roster().ShowVisualL( iVisuals[i] ) );
       
   682                 }*/
       
   683             }
       
   684         }
       
   685     }
       
   686 
       
   687 // ---------------------------------------------------------------------------
       
   688 // Called when the host is about to change
       
   689 // ---------------------------------------------------------------------------
       
   690 //
       
   691 EXPORT_C void CAlfControl::HostChangingL(CAlfControl* aNewHost)
       
   692     {
       
   693     CAlfLayout* newContainer = 0;
       
   694     TInt i = 0;
       
   695 
       
   696     if(aNewHost)
       
   697         {
       
   698         // The container layout provided by the new host.
       
   699         newContainer = aNewHost->ContainerLayout(this);
       
   700         }
       
   701 
       
   702     if(iData->iHost)
       
   703         {
       
   704         // There is a previous host.
       
   705         // Remove the visuals.
       
   706         RemoveVisualsFromHostControl(*iData->iHost);
       
   707         }
       
   708 
       
   709     if(newContainer)
       
   710         {
       
   711         for(i = 0; i < iData->iVisuals.Count(); ++i)
       
   712             {
       
   713             // Only the visuals that aren't already attached to a layout
       
   714             // are added to the container layout.
       
   715             if(!iData->iVisuals[i]->Layout())
       
   716                 {
       
   717                 User::LeaveIfError( newContainer->Append(iData->iVisuals[i]) );
       
   718                 }
       
   719             }
       
   720         }
       
   721     }
       
   722 
       
   723 // ---------------------------------------------------------------------------
       
   724 // Shows conttol on the given display
       
   725 // ---------------------------------------------------------------------------
       
   726 //
       
   727 EXPORT_C void CAlfControl::ShowL(CAlfDisplay& aDisplay)
       
   728     {
       
   729     if(HostId())
       
   730         {
       
   731         CAlfControl* host = Env().FindControl(HostId());
       
   732 
       
   733         if(host)
       
   734             {
       
   735             host->AddConnectionL(this, Role());
       
   736             }
       
   737         else
       
   738             {
       
   739             /** @todo Leave? */
       
   740             }
       
   741         }
       
   742 
       
   743     BindDisplay(aDisplay);
       
   744 
       
   745     // @todo: do we need this? the roster::show is already passed
       
   746     // to the server side, which call the same thing.
       
   747     
       
   748     // Show all the visuals on the specified display.
       
   749     for(TInt i = 0; i < iData->iVisuals.Count(); ++i)
       
   750         {
       
   751 
       
   752         // Set the display of the root visual
       
   753         if( !iData->iVisuals[i]->Layout() )
       
   754             {
       
   755             iData->iVisuals[i]->SetDisplay( &aDisplay );
       
   756             }
       
   757         }
       
   758         
       
   759     NotifyControlVisibility(ETrue, aDisplay);
       
   760     }
       
   761 
       
   762 // ---------------------------------------------------------------------------
       
   763 // Hides control from the given display
       
   764 // ---------------------------------------------------------------------------
       
   765 //
       
   766 void CAlfControl::Hide(CAlfDisplay& aDisplay)
       
   767     {
       
   768     NotifyControlVisibility(EFalse, aDisplay);
       
   769     
       
   770     // Hide all the visuals on the specified display.
       
   771     for(TInt i = 0; i < iData->iVisuals.Count(); ++i)
       
   772         {
       
   773         // The visuals that are part of a layout will be shown when the
       
   774         // layout is shown.
       
   775         if(!iData->iVisuals[i]->Layout() && iData->iVisuals[i]->Display() == &aDisplay)
       
   776             {
       
   777             aDisplay.Roster().HideVisual(*iData->iVisuals[i]);
       
   778             }
       
   779         }
       
   780 
       
   781     // Relinquish focus on this display
       
   782     CAlfRoster& roster = aDisplay.Roster();
       
   783     if ( roster.FocusedControl() == this )
       
   784         {
       
   785         roster.ClearFocus();
       
   786         }
       
   787     
       
   788     // Unbind from display?
       
   789     if(iData->iBoundDisplay == &aDisplay)
       
   790         {
       
   791         iData->iBoundDisplay = NULL;
       
   792         }
       
   793 
       
   794     // Unlink from parent control.
       
   795     if(Host() && HostId() )
       
   796         {
       
   797         Host()->RemoveConnection(this);
       
   798         }
       
   799     }
       
   800 
       
   801 // ---------------------------------------------------------------------------
       
   802 // Called when visibility changes.
       
   803 // ---------------------------------------------------------------------------
       
   804 //
       
   805 EXPORT_C void CAlfControl::NotifyControlVisibility(TBool /*aIsVisible*/, 
       
   806                                                    CAlfDisplay& /*aDisplay*/)
       
   807     {
       
   808     // Nothing to do by default.
       
   809     }
       
   810 
       
   811 // ---------------------------------------------------------------------------
       
   812 // Converts point from relative to absolute coordinates
       
   813 // ---------------------------------------------------------------------------
       
   814 //
       
   815 EXPORT_C TPoint CAlfControl::HostToDisplay(const TPoint& aPoint) const
       
   816     {
       
   817     if(!iData->iHost)
       
   818         {
       
   819         return aPoint;
       
   820         }
       
   821 
       
   822     CAlfLayout* container = iData->iHost->ContainerLayout(this);
       
   823 
       
   824     return container->LocalToDisplay(aPoint) + container->Pos().Target();
       
   825     }
       
   826 
       
   827 // ---------------------------------------------------------------------------
       
   828 // Converts point from absolute to relative coordinates
       
   829 // ---------------------------------------------------------------------------
       
   830 //
       
   831 EXPORT_C TPoint CAlfControl::DisplayToHost(const TPoint& aPoint) const
       
   832     {
       
   833     if(!iData->iHost)
       
   834         {
       
   835         return aPoint;
       
   836         }
       
   837 
       
   838     CAlfLayout* container = iData->iHost->ContainerLayout(this);
       
   839 
       
   840     return container->DisplayToLocal(aPoint) - container->Pos().Target();
       
   841     }
       
   842 
       
   843 // ---------------------------------------------------------------------------
       
   844 // Returns bounding rect.
       
   845 // ---------------------------------------------------------------------------
       
   846 //
       
   847 EXPORT_C TRect CAlfControl::Bounds() const
       
   848     {
       
   849     TPoint min;
       
   850     TPoint max;
       
   851 
       
   852     min.iX = KMaxTInt;
       
   853     min.iY = KMaxTInt;
       
   854     max.iX = KMinTInt;
       
   855     max.iY = KMinTInt;
       
   856 
       
   857     for(TInt i = 0; i < iData->iVisuals.Count(); ++i)
       
   858         {
       
   859         TRect visualRect = iData->iVisuals[i]->DisplayRectTarget();
       
   860 
       
   861         min.iX = Min(min.iX, visualRect.iTl.iX);
       
   862         min.iY = Min(min.iY, visualRect.iTl.iY);
       
   863         max.iX = Max(max.iX, visualRect.iBr.iX);
       
   864         max.iY = Max(max.iY, visualRect.iBr.iY);
       
   865         }
       
   866 
       
   867     return TRect(min, max);
       
   868     }
       
   869 
       
   870 // ---------------------------------------------------------------------------
       
   871 // Is the given point inside the control area?
       
   872 // ---------------------------------------------------------------------------
       
   873 //
       
   874 EXPORT_C TBool CAlfControl::HitTest(const TPoint& aPoint) const
       
   875     {
       
   876     return Bounds().Contains(aPoint);
       
   877     }
       
   878 
       
   879     
       
   880 // ---------------------------------------------------------------------------
       
   881 // Tries to get the focus.
       
   882 // ---------------------------------------------------------------------------
       
   883 //    
       
   884 EXPORT_C void CAlfControl::AcquireFocus()
       
   885     {
       
   886     if ( !AcceptInput() ) // Cannot take focus to this control
       
   887         {
       
   888         return;
       
   889         }
       
   890 
       
   891     // Focus is set set separately in each display the control has visuals in.
       
   892     // Find all the displays
       
   893     RPointerArray<CAlfDisplay> displays;
       
   894     for(TInt i = 0; i < iData->iVisuals.Count(); ++i)
       
   895         {
       
   896         if(iData->iVisuals[i]->Display())
       
   897             {
       
   898             // Do not allow duplicates. Ignore error.
       
   899             (void)displays.InsertInAddressOrder( iData->iVisuals[i]->Display() );
       
   900             }
       
   901         }
       
   902     
       
   903     // go through the displays
       
   904     for ( TInt d = 0 ; d < displays.Count() ; d++ )
       
   905         {
       
   906         if ( displays[d]->Roster().FocusedControl() != this )
       
   907             {
       
   908             displays[d]->Roster().SetFocus(*this );
       
   909             }
       
   910         }
       
   911     
       
   912     displays.Close();
       
   913     }   
       
   914 
       
   915 // ---------------------------------------------------------------------------
       
   916 // Attempt to remove the focus from this control
       
   917 // ---------------------------------------------------------------------------
       
   918 //    
       
   919 EXPORT_C void CAlfControl::RelinquishFocus()
       
   920     {
       
   921     // Focus needs to be removed from all displays that the control has visuals in
       
   922     for(TInt i = 0; i < iData->iVisuals.Count(); ++i)
       
   923         {
       
   924         if(iData->iVisuals[i]->Display())
       
   925             {
       
   926             // This method should have no effect on rosters where this does not match the currently focused control
       
   927             CAlfRoster& roster = iData->iVisuals[i]->Display()->Roster();
       
   928             if ( roster.FocusedControl() == this )
       
   929                 {
       
   930                 roster.ClearFocus(); // Potentially called repeatedly for each visual. Optimization is in CAlfRoster
       
   931                 }
       
   932             }
       
   933         }
       
   934     }   
       
   935 // ---------------------------------------------------------------------------
       
   936 // Does this control have focus?
       
   937 // ---------------------------------------------------------------------------
       
   938 //
       
   939 EXPORT_C TBool CAlfControl::Focus() const
       
   940     {
       
   941     return iData->iHasFocus;
       
   942     }
       
   943 
       
   944 // ---------------------------------------------------------------------------
       
   945 // Is one of the child focused?
       
   946 // ---------------------------------------------------------------------------
       
   947 //   
       
   948 EXPORT_C CAlfControl* CAlfControl::FocusedConnection() const
       
   949     {
       
   950     CAlfControl* focusChild = NULL;
       
   951     
       
   952     const TInt connectionCount = ConnectionCount();
       
   953     for ( TInt c = 0 ; c < connectionCount && !focusChild ; c++ )
       
   954         {
       
   955         CAlfControl& childConnection =  Connection(c);
       
   956         if ( childConnection.Focus() || childConnection.FocusedConnection() )
       
   957             {
       
   958             focusChild = &childConnection;
       
   959             }
       
   960         }
       
   961         
       
   962     return focusChild;
       
   963     }
       
   964 
       
   965 // ---------------------------------------------------------------------------
       
   966 //
       
   967 // ---------------------------------------------------------------------------
       
   968 //      
       
   969 void CAlfControl::FocusChainChanged( TBool aInFocusChain )
       
   970     {
       
   971     TAny* pInFocusChain = &aInFocusChain;
       
   972     PropertyOwnerExtension( KUidAlfPropOwnerExtControlFocusChainChanged, &pInFocusChain );
       
   973     }
       
   974 
       
   975 // ---------------------------------------------------------------------------
       
   976 // Sets focus
       
   977 // ---------------------------------------------------------------------------
       
   978 //
       
   979 void CAlfControl::SetFocus(CAlfDisplay& aDisplay, TBool aHasFocus)
       
   980     {
       
   981     if ( ( iData->iHasFocus && !aHasFocus ) || 
       
   982          ( !iData->iHasFocus && aHasFocus ) )
       
   983         {
       
   984         iData->iHasFocus = aHasFocus;
       
   985         FocusChanged(aDisplay, aHasFocus);
       
   986         }
       
   987     }
       
   988 
       
   989 // ---------------------------------------------------------------------------
       
   990 // Is focusing control?
       
   991 // ---------------------------------------------------------------------------
       
   992 //
       
   993 EXPORT_C TBool CAlfControl::IsFocusing() const
       
   994     {
       
   995     return iData->iFocusing;
       
   996     }
       
   997 
       
   998 // ---------------------------------------------------------------------------
       
   999 // Set is focusing control.
       
  1000 // ---------------------------------------------------------------------------
       
  1001 //
       
  1002 void CAlfControl::SetFocusing(TBool aFocusing)
       
  1003     {
       
  1004     iData->iFocusing = aFocusing;
       
  1005     }
       
  1006 
       
  1007 // ---------------------------------------------------------------------------
       
  1008 // Called when focus changes.
       
  1009 // ---------------------------------------------------------------------------
       
  1010 //
       
  1011 EXPORT_C void CAlfControl::FocusChanged(CAlfDisplay& /*aDisplay*/, TBool /*aFocused*/)
       
  1012     {
       
  1013     // Do nothing by default.
       
  1014     }
       
  1015 
       
  1016 // ---------------------------------------------------------------------------
       
  1017 // Does control group accept input?
       
  1018 // ---------------------------------------------------------------------------
       
  1019 //
       
  1020 EXPORT_C TBool CAlfControl::AcceptInput() const
       
  1021     {
       
  1022     if(iData->iOwnerGroup)
       
  1023         {
       
  1024         return iData->iOwnerGroup->AcceptInput();
       
  1025         }
       
  1026     return ETrue;    
       
  1027     }
       
  1028 
       
  1029 
       
  1030 // ---------------------------------------------------------------------------
       
  1031 // Called when event is received.
       
  1032 // ---------------------------------------------------------------------------
       
  1033 //
       
  1034 EXPORT_C TBool CAlfControl::OfferEventL(const TAlfEvent& /*aEvent*/)
       
  1035     {
       
  1036     return EFalse;
       
  1037     }
       
  1038 
       
  1039 // ---------------------------------------------------------------------------
       
  1040 // Returns control area.
       
  1041 // ---------------------------------------------------------------------------
       
  1042 //
       
  1043 EXPORT_C TRect CAlfControl::DisplayArea() const
       
  1044     {
       
  1045     if(iData->iBoundDisplay)
       
  1046         {
       
  1047         return iData->iBoundDisplay->VisibleArea();
       
  1048         }
       
  1049     
       
  1050     // The first visual shown on a display determines the display area.            
       
  1051     for(TInt i = 0; i < iData->iVisuals.Count(); ++i)
       
  1052         {
       
  1053         if(iData->iVisuals[i]->Display())
       
  1054             {
       
  1055             return iData->iVisuals[i]->Display()->VisibleArea();
       
  1056             }
       
  1057         }
       
  1058         
       
  1059     if(!Env().DisplayCount())
       
  1060         {
       
  1061         // No displays created in the environment yet. Assume device 
       
  1062         // native resolution.
       
  1063         //return TRect(TPoint(0, 0), AlfUtil::ScreenSize());
       
  1064         CCoeEnv* coe = CCoeEnv::Static();
       
  1065         if ( coe )
       
  1066             {
       
  1067             return static_cast<CEikAppUi*>(coe->AppUi())->ClientRect(); 
       
  1068             }
       
  1069         else
       
  1070             {
       
  1071             return TRect( 0,0,0,0 );
       
  1072             }
       
  1073         }
       
  1074         
       
  1075     // Assume it is the primary display, then.   
       
  1076     return Env().PrimaryDisplay().VisibleArea();
       
  1077     }
       
  1078 
       
  1079 // ---------------------------------------------------------------------------
       
  1080 // Returns the center point of the area.
       
  1081 // ---------------------------------------------------------------------------
       
  1082 //
       
  1083 EXPORT_C TAlfRealPoint CAlfControl::DisplayCenter() const
       
  1084     {
       
  1085     TRect area(DisplayArea());
       
  1086     return area.iTl + TAlfRealPoint(area.Width()/2.f, area.Height()/2.f);
       
  1087     }
       
  1088 
       
  1089 // ---------------------------------------------------------------------------
       
  1090 // Cancels all commands
       
  1091 // ---------------------------------------------------------------------------
       
  1092 //
       
  1093 EXPORT_C void CAlfControl::CancelAllCommands()
       
  1094     {
       
  1095     iData->iEnv->CancelCustomCommands(this);
       
  1096     iData->iEnv->CancelCommands(this);
       
  1097     
       
  1098     for(TInt i = 0; i < iData->iVisuals.Count(); ++i)
       
  1099         {
       
  1100         iData->iEnv->CancelCommands(iData->iVisuals[i]);
       
  1101         }
       
  1102     }
       
  1103     
       
  1104 // ---------------------------------------------------------------------------
       
  1105 // Clear flag.
       
  1106 // ---------------------------------------------------------------------------
       
  1107 //
       
  1108 void CAlfControl::ClearChanged()
       
  1109     { // todo: does client need to access this information
       
  1110     }
       
  1111 
       
  1112 // ---------------------------------------------------------------------------
       
  1113 // Called when layout changes.
       
  1114 // ---------------------------------------------------------------------------
       
  1115 //
       
  1116 EXPORT_C void CAlfControl::VisualDestroyed(CAlfVisual& aVisual)
       
  1117     {
       
  1118     // If called from CAlfVisual::RemoveAndDestroyAllD the server
       
  1119     // side object is removed also automatically. Otherwise not..
       
  1120     Remove(&aVisual);
       
  1121     }
       
  1122 
       
  1123 // ---------------------------------------------------------------------------
       
  1124 // Called when visual layout is updated (see visual flags)
       
  1125 // ---------------------------------------------------------------------------
       
  1126 //
       
  1127 EXPORT_C void CAlfControl::VisualLayoutUpdated(CAlfVisual& /*aVisual*/)
       
  1128     {
       
  1129     
       
  1130     }
       
  1131   
       
  1132 // ---------------------------------------------------------------------------
       
  1133 // Called when draw preparation failed (not implemented currenltly)
       
  1134 // ---------------------------------------------------------------------------
       
  1135 // 
       
  1136 EXPORT_C void CAlfControl::VisualPrepareDrawFailed(CAlfVisual& /*aVisual*/, TInt /*aErrorCode*/)
       
  1137     {
       
  1138     
       
  1139     }
       
  1140 
       
  1141 // ---------------------------------------------------------------------------
       
  1142 //  future proofing  
       
  1143 // ---------------------------------------------------------------------------
       
  1144 //  
       
  1145 EXPORT_C void CAlfControl::PropertyOwnerExtension(const TUid& aExtensionUid, TAny** aExtensionParams)
       
  1146     {
       
  1147     CAlfPropertyOwner::PropertyOwnerExtension(aExtensionUid, aExtensionParams);
       
  1148     }
       
  1149