idlehomescreen/xmluirendering/renderingplugins/xnvolumecontrolfactory/src/xnvolumecontroladapter.cpp
changeset 0 f72a12da539e
child 1 5315654608de
equal deleted inserted replaced
-1:000000000000 0:f72a12da539e
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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:  Implementation for wrapper for a volume control component
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <coecntrl.h>
       
    21 #include <barsread.h>
       
    22 #include <barsc.h>
       
    23 #include <Aknvolumecontrol.h>
       
    24 
       
    25 #include "xnnodepluginif.h"
       
    26 #include "xndompropertyvalue.h"
       
    27 #include "xndomproperty.h"
       
    28 #include "xndomlist.h"
       
    29 #include "xnproperty.h"
       
    30 #include "xncomponent.h"
       
    31 #include "xnuienginepluginif.h"
       
    32 #include "xncomponentnodeimpl.h"
       
    33 #include "xncontroladapter.h"
       
    34 #include "xntype.h"
       
    35 
       
    36 #include "xnvolumecontroladapter.h"
       
    37 #include "xnvolumecontrolobserver.h"
       
    38 #include "xnvolumecontrol.rsg"
       
    39 
       
    40 _LIT( KResourceFileName, "z:\\resource\\plugins\\xn3volumecontrol.rsc" );
       
    41 
       
    42 // ============================ MEMBER FUNCTIONS ===============================
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CXnVolumeControlAdapter::NewL
       
    46 // Symbian static 1st phase constructor
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CXnVolumeControlAdapter* CXnVolumeControlAdapter::NewL(CXnControlAdapter* aParent, 
       
    50                                                        CXnNodePluginIf& aNode)
       
    51     {
       
    52 	CXnVolumeControlAdapter* self = new( ELeave ) CXnVolumeControlAdapter(aParent, aNode);
       
    53     CleanupStack::PushL( self );
       
    54     self->ConstructL();
       
    55     CleanupStack::Pop();
       
    56     return self;	
       
    57     }
       
    58     
       
    59 // -----------------------------------------------------------------------------
       
    60 // CXnVolumeControlAdapter::CXnVolumeControlAdapter
       
    61 // C++ default constructor
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 CXnVolumeControlAdapter::CXnVolumeControlAdapter(CXnControlAdapter* aParent, 
       
    65                                                  CXnNodePluginIf& aNode)
       
    66     : iParent( aParent ), iNode( aNode )
       
    67     {
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CXnVolumeControlAdapter::~CXnVolumeControlAdapter
       
    72 // C++ destructor
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 CXnVolumeControlAdapter::~CXnVolumeControlAdapter()
       
    76     {
       
    77     delete iVolumeControl;
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // CXnVolumeControlAdapter::ConstructL
       
    82 // Symbian 2nd phase constructor can leave.
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 void CXnVolumeControlAdapter::ConstructL()
       
    86     { 
       
    87     ConstructVolumeControlL();
       
    88     
       
    89     iVolumeControl->SetValue( ValueL() );
       
    90     
       
    91     CXnControlAdapter::ConstructL( iNode );
       
    92     }
       
    93     
       
    94 // -----------------------------------------------------------------------------
       
    95 // CXnVolumeControlAdapter::ConstructVolumeControL
       
    96 // -----------------------------------------------------------------------------
       
    97 //    
       
    98 void CXnVolumeControlAdapter::ConstructVolumeControlL()
       
    99 	{	
       
   100 	delete iVolumeControl;
       
   101 	iVolumeControl = NULL;
       
   102 	
       
   103     iVolumeControl = new ( ELeave ) CAknVolumeControl;
       
   104     iVolumeControl->SetContainerWindowL( *iParent );		
       
   105     iVolumeControl->SetBackground( this );
       
   106 		
       
   107 	RFs fsSession;
       
   108 	
       
   109 	TInt err( fsSession.Connect() );
       
   110 
       
   111     if( err )	    
       
   112 	    {
       
   113 	    User::Leave( err );
       
   114 	    }
       
   115 		
       
   116 	CleanupClosePushL( fsSession );
       
   117 	
       
   118 	RResourceFile resourceFile;		
       
   119 	resourceFile.OpenL( fsSession, KResourceFileName );	
       
   120 	CleanupClosePushL( resourceFile );
       
   121     
       
   122 	HBufC8* resource = resourceFile.AllocReadLC( R_XNVOLUMECONTROLDYNAMIC );
       
   123 	
       
   124 	TResourceReader reader;	
       
   125 	reader.SetBuffer( resource );
       
   126 	
       
   127 	iVolumeControl->ConstructFromResourceL( reader );
       
   128 	iVolumeControl->SetRect( iNode.Rect() );
       
   129 	
       
   130 	CleanupStack::PopAndDestroy( 3, &fsSession  ); // resource, resourceFile, fsSession
       
   131 				
       
   132 	SetVolumeControlPropertiesL();		
       
   133 	}
       
   134     
       
   135 // -----------------------------------------------------------------------------
       
   136 // CXnVolumeControlAdapter::SetObserver
       
   137 // -----------------------------------------------------------------------------
       
   138 //      
       
   139 void CXnVolumeControlAdapter::SetObserver( MXnVolumeControlObserver* aObserver )
       
   140     {
       
   141     iObserver = aObserver;
       
   142     }
       
   143     
       
   144 // -----------------------------------------------------------------------------
       
   145 // CXnVolumeControlAdapter::SetValue
       
   146 // -----------------------------------------------------------------------------
       
   147 //    
       
   148 void CXnVolumeControlAdapter::SetValueL( TInt aValue )
       
   149 	{
       
   150 	TInt minRange;
       
   151 	TInt maxRange;
       
   152 
       
   153 	GetRangeL( minRange, maxRange );
       
   154 
       
   155 	if( aValue >= minRange && aValue <= maxRange )
       
   156 		{		    	
       
   157 		CXnDomStringPool& sp = iNode.UiEngineL()->StringPool();		
       
   158 		CXnDomPropertyValue* value = CXnDomPropertyValue::NewL( sp );
       
   159 		CleanupStack::PushL( value );
       
   160 		value->SetFloatValueL( CXnDomPropertyValue::ENumber, aValue );
       
   161 		CXnProperty* valueProperty = CXnProperty::NewL( XnPropertyNames::action::KValue, value, sp );
       
   162 		CleanupStack::Pop( value );
       
   163 		CleanupStack::PushL( valueProperty );
       
   164 		iNode.SetPropertyL( valueProperty );
       
   165 		CleanupStack::Pop( valueProperty );
       
   166 		}
       
   167 	}
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // CXnVolumeControlAdapter::Value
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 TInt CXnVolumeControlAdapter::ValueL()
       
   174 	{
       
   175 	return GetCurrentValueL();
       
   176 	}
       
   177 	
       
   178 // -----------------------------------------------------------------------------
       
   179 // CXnVolumeControlAdapter::SetRange
       
   180 // -----------------------------------------------------------------------------
       
   181 //	
       
   182 void CXnVolumeControlAdapter::SetRangeL( TInt aMinimumValue, TInt aMaximumValue )
       
   183 	{
       
   184 	CXnDomStringPool& sp = iNode.UiEngineL()->StringPool();
       
   185 	
       
   186 	// Set min value property
       
   187 	CXnDomPropertyValue* minValue = CXnDomPropertyValue::NewL( sp );
       
   188 	CleanupStack::PushL( minValue );
       
   189 	minValue->SetFloatValueL( CXnDomPropertyValue::ENumber, aMinimumValue );
       
   190 	CXnProperty* minValueProperty = CXnProperty::NewL( XnPropertyNames::volumecontrol::KMinRange, minValue, sp );
       
   191 	CleanupStack::Pop( minValue );
       
   192 	CleanupStack::PushL( minValueProperty );
       
   193 	iNode.SetPropertyL( minValueProperty );
       
   194 	CleanupStack::Pop( minValueProperty );
       
   195 
       
   196 	// Set max value property
       
   197 	CXnDomPropertyValue* maxValue = CXnDomPropertyValue::NewL( sp );
       
   198 	CleanupStack::PushL( maxValue );
       
   199 	maxValue->SetFloatValueL( CXnDomPropertyValue::ENumber, aMaximumValue );
       
   200 	CXnProperty* maxValueProperty = CXnProperty::NewL( XnPropertyNames::volumecontrol::KMaxRange, maxValue, sp );
       
   201 	CleanupStack::Pop( maxValue );
       
   202 	CleanupStack::PushL( maxValueProperty );
       
   203 	iNode.SetPropertyL( maxValueProperty );
       
   204 	CleanupStack::Pop( maxValueProperty );
       
   205 	
       
   206 	iVolumeControl->SetRange( aMinimumValue, aMaximumValue );
       
   207 	iSteps = aMaximumValue - aMinimumValue;
       
   208 	}
       
   209 	
       
   210 // -----------------------------------------------------------------------------
       
   211 // CXnVolumeControlAdapter::GetRange
       
   212 // -----------------------------------------------------------------------------
       
   213 //	
       
   214 void CXnVolumeControlAdapter::GetRangeL( TInt& aMinimumValue, TInt& aMaximumValue )
       
   215 	{
       
   216 	CXnProperty* minRange = iNode.GetPropertyL( XnPropertyNames::volumecontrol::KMinRange );
       
   217 	aMinimumValue = static_cast<TInt>( minRange->FloatValueL() );
       
   218 	
       
   219 	CXnProperty* maxRange = iNode.GetPropertyL( XnPropertyNames::volumecontrol::KMaxRange );
       
   220 	aMaximumValue = static_cast<TInt>( maxRange->FloatValueL() );
       
   221 	}
       
   222 
       
   223 // -----------------------------------------------------------------------------
       
   224 // CXnVolumeControlAdapter::DoHandlePropertyChangeL
       
   225 // -----------------------------------------------------------------------------
       
   226 //
       
   227 void CXnVolumeControlAdapter::DoHandlePropertyChangeL( CXnProperty* aProperty )
       
   228     {
       
   229     if (aProperty)
       
   230     	{
       
   231     TInt minValue;
       
   232     TInt maxValue;
       
   233     GetRangeL( minValue, maxValue );
       
   234     
       
   235     if( aProperty->Property()->Name() == XnPropertyNames::volumecontrol::KVolumeLevelAdjustment )
       
   236     	{
       
   237     	CXnDomStringPool& sp = iNode.UiEngineL()->StringPool();
       
   238     	CXnDomPropertyValue* value = CXnDomPropertyValue::NewL( sp );
       
   239     	CleanupStack::PushL( value );
       
   240     	
       
   241     	const TDesC8& strValue = aProperty->StringValue();
       
   242     	// Increase volume
       
   243     	if( strValue == XnPropertyNames::volumecontrol::KIncrease )
       
   244     		{
       
   245     		TInt newValue = ValueL() + 1;
       
   246     		if( newValue >= minValue && newValue <= maxValue )
       
   247     			{
       
   248     			value->SetFloatValueL( CXnDomPropertyValue::ENumber, ValueL() + 1 );
       
   249     			}
       
   250     		else
       
   251     			{
       
   252     			CleanupStack::PopAndDestroy( value );    			
       
   253     			return;
       
   254     			}
       
   255     		}
       
   256     	// Decrease volume
       
   257     	else if( strValue == XnPropertyNames::volumecontrol::KDecrease )
       
   258     		{
       
   259     		TInt newValue = ValueL() - 1;
       
   260     		if( newValue >= minValue && newValue <= maxValue )
       
   261     			{    		
       
   262     			value->SetFloatValueL( CXnDomPropertyValue::ENumber, ValueL() - 1 );
       
   263     			}
       
   264     		else
       
   265     			{
       
   266     			CleanupStack::PopAndDestroy( value );    			
       
   267     			return;
       
   268     			}    			
       
   269     		}
       
   270     	// Mute
       
   271     	else if( strValue == XnPropertyNames::volumecontrol::KMute )
       
   272     		{
       
   273     		if( iMuted )
       
   274     			{
       
   275     			value->SetFloatValueL( CXnDomPropertyValue::ENumber, iMutedValue );
       
   276     			iMuted = EFalse;
       
   277     			}
       
   278     		else
       
   279     			{
       
   280     			value->SetFloatValueL( CXnDomPropertyValue::ENumber, minValue );
       
   281     			iMutedValue = GetCurrentValueL();
       
   282     			iMuted = ETrue;
       
   283     			}
       
   284     		}
       
   285     	CXnProperty* valueProperty = CXnProperty::NewL( XnPropertyNames::action::KValue, value, sp );
       
   286     	CleanupStack::Pop( value );
       
   287     	CleanupStack::PushL( valueProperty );
       
   288     	iNode.SetPropertyL( valueProperty );
       
   289     	CleanupStack::Pop( valueProperty );  	
       
   290     	}
       
   291    	else if( aProperty->Property()->Name() == XnPropertyNames::action::KValue )
       
   292    		{
       
   293    		TInt newValue = static_cast<TInt>( aProperty->FloatValueL() );
       
   294    		if( newValue >= minValue && newValue <= maxValue )
       
   295    			{
       
   296    			iVolumeControl->SetValue( newValue );
       
   297    			NotifyObserverL();
       
   298 	   			}
       
   299    			}
       
   300     	}
       
   301     else
       
   302     	{
       
   303     	return;
       
   304    		}
       
   305     }
       
   306 
       
   307 // -----------------------------------------------------------------------------
       
   308 // CXnVolumeControlAdapter::HandleScreenDeviceChangedL
       
   309 // -----------------------------------------------------------------------------
       
   310 //
       
   311 void CXnVolumeControlAdapter::HandleScreenDeviceChangedL()
       
   312     {
       
   313     
       
   314     ConstructVolumeControlL();
       
   315             
       
   316     CXnControlAdapter::HandleScreenDeviceChangedL();
       
   317     }
       
   318 
       
   319 // ---------------------------------------------------------
       
   320 // CXnVolumeControlAdapter::SizeChanged()
       
   321 // Called by framework when the view size is changed
       
   322 // (other items were commented in a header).
       
   323 // ---------------------------------------------------------
       
   324 //
       
   325 void CXnVolumeControlAdapter::SizeChanged()
       
   326     {
       
   327     CXnControlAdapter::SizeChanged();
       
   328     if( iVolumeControl )
       
   329         {
       
   330         iVolumeControl->SetRect( iNode.Rect() );
       
   331         }	
       
   332     }
       
   333     
       
   334 // -----------------------------------------------------------------------------
       
   335 // CXnVolumeControlAdapter::CountComponentControls
       
   336 // Returns the number of component controls.
       
   337 // -----------------------------------------------------------------------------
       
   338 //    
       
   339 TInt CXnVolumeControlAdapter::CountComponentControls() const
       
   340 	{
       
   341     return 1;
       
   342 	}
       
   343 	
       
   344 // -----------------------------------------------------------------------------
       
   345 // CXnVolumeControlAdapter::ComponentControl
       
   346 // Returns the component control of the given index
       
   347 // -----------------------------------------------------------------------------
       
   348 //	
       
   349 CCoeControl* CXnVolumeControlAdapter::ComponentControl( TInt aIndex ) const
       
   350 	{
       
   351 	if( aIndex == 0 )
       
   352 	    {
       
   353 	    return iVolumeControl;
       
   354 	    }
       
   355 	
       
   356 	return NULL;
       
   357 	}	
       
   358 
       
   359 // -----------------------------------------------------------------------------
       
   360 // CXnNewstickerAdapter::Draw
       
   361 // -----------------------------------------------------------------------------
       
   362 // 
       
   363 void CXnVolumeControlAdapter::Draw(CWindowGc& aGc, const CCoeControl& /*aControl*/,
       
   364     const TRect& aRect) const
       
   365     {
       
   366     CXnControlAdapter::Draw( aRect, aGc );
       
   367     }
       
   368   	           
       
   369 // -----------------------------------------------------------------------------
       
   370 // CXnVolumeControlAdapter::Draw
       
   371 // Draws the text component
       
   372 // -----------------------------------------------------------------------------
       
   373 //
       
   374 void CXnVolumeControlAdapter::Draw(const TRect& aRect) const
       
   375     {
       
   376     CXnControlAdapter::Draw( aRect );
       
   377     }  
       
   378 
       
   379 // ----------------------------------------------------------------------------
       
   380 // CXnVolumeControlAdapter::HandlePointerEventL
       
   381 // Handles the pointer events.
       
   382 // ----------------------------------------------------------------------------
       
   383 //
       
   384 void CXnVolumeControlAdapter::HandlePointerEventL( const TPointerEvent& aPointerEvent )
       
   385     {    
       
   386     TRect rect( iVolumeControl->Rect() );
       
   387     
       
   388     TReal incrementWidth( TReal( rect.Width() ) / iSteps );
       
   389                                     
       
   390     TInt leftX( rect.iTl.iX );
       
   391        
       
   392     if( aPointerEvent.iType == TPointerEvent::EButton1Down || 
       
   393         aPointerEvent.iType == TPointerEvent::EMove && iButton1Down )        
       
   394         {   
       
   395         // By default change the value
       
   396         TBool valueSet( EFalse );
       
   397                             
       
   398         for( TInt i = 1; i <= iSteps && !valueSet; i++ )
       
   399             {            
       
   400             // Calculate pixel range 
       
   401             TReal minX( leftX + ( ( i - 1 ) * incrementWidth ) );
       
   402             TReal maxX( minX + incrementWidth );
       
   403                        
       
   404             if( aPointerEvent.iPosition.iX >= minX && aPointerEvent.iPosition.iX <= maxX )                
       
   405                 {                     
       
   406                 TBool changeValue( ETrue );
       
   407                              
       
   408                 if( iHitRect == TRect::EUninitialized )
       
   409                     {
       
   410                     // Initialize                                    
       
   411                     iHitRect = TRect( minX, rect.iTl.iY, maxX, rect.iBr.iY );                       
       
   412                     }
       
   413                 else if( !( iHitRect.Contains( aPointerEvent.iPosition ) ) )
       
   414                     {
       
   415                     // Reset, as position is changed
       
   416                     iHitRect = TRect( TRect::EUninitialized );
       
   417                     changeValue = EFalse;
       
   418                     }                        
       
   419                 else
       
   420                     {
       
   421                     // Still in same slot, do not change value
       
   422                     changeValue = EFalse;
       
   423                     }                        
       
   424                 
       
   425                 if( changeValue )
       
   426                     {
       
   427                     TInt value = ValueL();
       
   428                     
       
   429                     if( value == i )
       
   430                         {
       
   431                         value = value - 1;
       
   432                         }
       
   433                     else
       
   434                         {
       
   435                         value = i;
       
   436                         }                    
       
   437                                                   
       
   438                     //SetValueL will notify also the observer
       
   439                     SetValueL( value );                       
       
   440                     DrawDeferred();                      
       
   441                     valueSet = ETrue;                                        
       
   442                     }
       
   443                 }                                        
       
   444             }            
       
   445         }
       
   446                 
       
   447     if( aPointerEvent.iType == TPointerEvent::EButton1Down )
       
   448         {
       
   449         iButton1Down = ETrue;
       
   450         }
       
   451         
       
   452     else if( aPointerEvent.iType == TPointerEvent::EButton1Up )
       
   453         {
       
   454         // Reset
       
   455         iButton1Down = EFalse;        
       
   456         iHitRect = TRect( TRect::EUninitialized );
       
   457         }        
       
   458 	}
       
   459 	
       
   460 // ----------------------------------------------------------------------------
       
   461 // CXnVolumeControlAdapter::SetVolumeControlProperties
       
   462 // Sets CSS properties for the volume control
       
   463 // ----------------------------------------------------------------------------
       
   464 //	
       
   465 void CXnVolumeControlAdapter::SetVolumeControlPropertiesL()
       
   466 	{
       
   467 	// Read minrange property
       
   468 	CXnProperty* minRange = iNode.GetPropertyL( XnPropertyNames::volumecontrol::KMinRange );
       
   469 	TInt minValue = 0;
       
   470 	
       
   471 	if( minRange )
       
   472 		{
       
   473 		minValue = static_cast<TInt>( minRange->FloatValueL() );
       
   474 		}
       
   475 		
       
   476 	// Read maxrange property
       
   477 	CXnProperty* maxRange = iNode.GetPropertyL( XnPropertyNames::volumecontrol::KMaxRange );
       
   478 	TInt maxValue = 20;
       
   479 	
       
   480 	if( maxRange )
       
   481 		{
       
   482 		maxValue = static_cast<TInt>( maxRange->FloatValueL() );
       
   483 		}
       
   484 				
       
   485 	SetRangeL( minValue, maxValue );
       
   486 	
       
   487 	TInt midRange( iSteps / 2 );
       
   488 	TInt volValue( 0 );
       
   489 	
       
   490 	if( iVolumeControl->Value() < minValue ||
       
   491 	    iVolumeControl->Value() > maxValue )
       
   492 		{
       
   493 		// Set around to mid
       
   494 		volValue = midRange;	
       
   495 		}
       
   496 			
       
   497     // Read value property
       
   498 	CXnProperty* value = iNode.GetPropertyL( XnPropertyNames::action::KValue );
       
   499 	
       
   500 	if( value )
       
   501 		{
       
   502 		TInt valueProp = static_cast< TInt >( value->FloatValueL() );
       
   503 		
       
   504 		if( valueProp >= minValue && valueProp <= maxValue )
       
   505 		    {
       
   506 		    volValue = valueProp;
       
   507 		    }
       
   508 		}
       
   509     	
       
   510     SetValueL( volValue );			
       
   511 	}
       
   512 	
       
   513 // ----------------------------------------------------------------------------
       
   514 // CXnVolumeControlAdapter::GetCurrentValue
       
   515 // ----------------------------------------------------------------------------
       
   516 //		
       
   517 TInt CXnVolumeControlAdapter::GetCurrentValueL()
       
   518 	{
       
   519 	CXnProperty* valueProperty = iNode.GetPropertyL( XnPropertyNames::action::KValue );
       
   520     return static_cast<TInt>( valueProperty->FloatValueL() );    
       
   521 	}
       
   522 	
       
   523 // ----------------------------------------------------------------------------
       
   524 // CXnVolumeControlAdapter::NotifyObserver
       
   525 // ----------------------------------------------------------------------------
       
   526 //	
       
   527 void CXnVolumeControlAdapter::NotifyObserverL()
       
   528 	{	
       
   529 	if( iObserver )
       
   530 		{
       
   531 		iObserver->VolumeChanged( GetCurrentValueL() );
       
   532 		}
       
   533 	}