uifw/EikStd/coctlsrc/AknInputPolicy.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 #include <aknenv.h>
       
    18 #include <aknsettingcache.h>
       
    19 #include <eikedwin.h>
       
    20 #include <txtrich.h>
       
    21 #include "akncursormanager.h"
       
    22 #include "akninputpolicy.h"
       
    23 
       
    24 CAknInputPolicyManager* CAknInputPolicyManager::NewL(CEikEdwin* aEdwin)
       
    25 	{
       
    26 	CAknInputPolicyManager* self = new (ELeave) CAknInputPolicyManager(aEdwin);
       
    27 	CleanupStack::PushL(self);
       
    28 	self->ConstructL();
       
    29 	CleanupStack::Pop();
       
    30 	return self;
       
    31 	}
       
    32 
       
    33 CAknInputPolicyManager::~CAknInputPolicyManager()
       
    34 	{
       
    35 	}
       
    36 
       
    37 void CAknInputPolicyManager::HandleInputPolicyManagerEventL(TEventType aEvent, TInt /*aData1*/, TAny* /*aData2*/)
       
    38 	{
       
    39 	switch (aEvent)
       
    40 		{
       
    41 		case ECMEventNull:
       
    42 			break;
       
    43 		case ECMEventInitialise:
       
    44 			if (iEdwin->TextLength() == 0)
       
    45 				SetCursorUsingLanguageL();
       
    46 			break;
       
    47 		case ECMEventInlineEditUpdate:
       
    48 			break;
       
    49 		case ECMEventInsertion:
       
    50 			break;
       
    51 		case ECMEventDeletion:
       
    52 			if (iEdwin->TextLength() == 0)
       
    53 				SetCursorUsingLanguageL();
       
    54 			else
       
    55 				NormaliseCursorPositionFollowingDeleteL();
       
    56 			break;
       
    57 		case ECMEventNavigation:
       
    58 			NormaliseCursorPositionFollowingNavigationL();
       
    59 			break;
       
    60 		default:
       
    61 			break;
       
    62 		}
       
    63 	}
       
    64 
       
    65 CAknInputPolicyManager::CAknInputPolicyManager(CEikEdwin* aEdwin) :
       
    66 	iEdwin(aEdwin)
       
    67 	{
       
    68 	}
       
    69 
       
    70 void CAknInputPolicyManager::ConstructL()
       
    71 	{
       
    72 	}
       
    73 
       
    74 
       
    75 void CAknInputPolicyManager::NormaliseCursorPositionFollowingDeleteL()
       
    76 	{
       
    77 	CAknCursorManager* cursorManager = CAknCursorManager::NewL(iEdwin->EditorState());
       
    78 	CleanupStack::PushL(cursorManager);
       
    79 
       
    80 	TTmDocPosSpec pos = cursorManager->DocPos();
       
    81 	CTmTextLayout::TTmChunkDescription leftChunk;
       
    82 	CTmTextLayout::TTmChunkDescription rightChunk;
       
    83 	iEdwin->TextLayout()->TagmaTextLayout().FindAdjacentChunks(pos, leftChunk, rightChunk);
       
    84 
       
    85 	TBool isRTLPara = cursorManager->IsRightToLeftParagraph(pos);
       
    86 	if (!isRTLPara && leftChunk.iRightToLeft ||
       
    87 		isRTLPara && !rightChunk.iRightToLeft)
       
    88 		cursorManager->ToggleVisualDocPosL();
       
    89 	
       
    90 	CleanupStack::PopAndDestroy(); // cursorManager
       
    91 	}
       
    92 
       
    93 void CAknInputPolicyManager::NormaliseCursorPositionFollowingNavigationL()
       
    94 	{
       
    95 	CAknCursorManager* cursorManager = CAknCursorManager::NewL(iEdwin->EditorState());
       
    96 	CleanupStack::PushL(cursorManager);
       
    97 
       
    98 	if (cursorManager->VisualCursorBetweenOpposingChunks())
       
    99 		{
       
   100 		TTmDocPosSpec pos = cursorManager->DocPos();
       
   101 		CTmTextLayout::TTmChunkDescription leftChunk;
       
   102 		CTmTextLayout::TTmChunkDescription rightChunk;
       
   103 		iEdwin->TextLayout()->TagmaTextLayout().FindAdjacentChunks(pos, leftChunk, rightChunk);
       
   104 
       
   105 		CAknSettingCache& cache = CAknEnv::Static()->SettingCache();
       
   106 		TLanguage currentLanguage = cache.InputLanguage();
       
   107 
       
   108 		if (leftChunk.iStart == -1)
       
   109 			{ // left end of a line
       
   110 			if (rightChunk.iRightToLeft)
       
   111 				{ // set doc pos trailing at the end of the right chunk
       
   112 				pos.iType = TTmDocPosSpec::ETrailing;
       
   113 				pos.iPos = rightChunk.iEnd;
       
   114 				}
       
   115 			else // rightChunk is LTR
       
   116 				{ // set doc pos leading at start of right chunk
       
   117 				pos.iType = TTmDocPosSpec::ELeading;
       
   118 				pos.iPos = rightChunk.iStart;
       
   119 				}
       
   120 			}
       
   121 		else if (rightChunk.iStart == -1)
       
   122 			{ // right end of a line
       
   123 			if (leftChunk.iRightToLeft)
       
   124 				{ // set doc pos leading at start of left chunk
       
   125 				pos.iType = TTmDocPosSpec::ELeading;
       
   126 				pos.iPos = leftChunk.iStart;
       
   127 				}
       
   128 			else // leftChunk is LTR
       
   129 				{ // set doc pos trailing at the end of the left chunk
       
   130 				pos.iType = TTmDocPosSpec::ETrailing;
       
   131 				pos.iPos = leftChunk.iEnd;
       
   132 				}
       
   133 			}
       
   134 
       
   135 		else // middle of a line
       
   136 			{
       
   137 			currentLanguage &= KAknLanguageMask;
       
   138 			if (currentLanguage == ELangArabic || currentLanguage == ELangHebrew)
       
   139 				{
       
   140 				if (rightChunk.iRightToLeft)
       
   141 					{ // set doc pos trailing at the end of the right chunk
       
   142 					pos.iType = TTmDocPosSpec::ETrailing;
       
   143 					pos.iPos = rightChunk.iEnd;
       
   144 					}
       
   145 				else // rightChunk is LTR
       
   146 					{ // set doc pos leading at start of left chunk
       
   147 					pos.iType = TTmDocPosSpec::ELeading;
       
   148 					pos.iPos = leftChunk.iStart;
       
   149 					}
       
   150 				}
       
   151 			else // language is LTR
       
   152 				{
       
   153 				if (rightChunk.iRightToLeft)
       
   154 					{ // set doc pos trailing at the end of left chunk
       
   155 					pos.iType = TTmDocPosSpec::ETrailing;
       
   156 					pos.iPos = leftChunk.iEnd;
       
   157 					}
       
   158 				else // rightChunk is LTR
       
   159 					{ // set doc pos leading at the start of right chunk
       
   160 					pos.iType = TTmDocPosSpec::ELeading;
       
   161 					pos.iPos = rightChunk.iStart;
       
   162 					}
       
   163 				}
       
   164 			}
       
   165 		// set the doc position...
       
   166 		cursorManager->SetDocPosL(pos);
       
   167 		}
       
   168 	CleanupStack::PopAndDestroy(); // cursorManager
       
   169 	}
       
   170 
       
   171 void CAknInputPolicyManager::SetCursorUsingLanguageL()
       
   172 	{
       
   173 	CAknSettingCache& cache = CAknEnv::Static()->SettingCache();
       
   174 	TLanguage currentLanguage = cache.InputLanguage();
       
   175 
       
   176 	CRichText* rT = static_cast<CRichText*>(iEdwin->Text());
       
   177 	CParaFormat* paraF = CParaFormat::NewLC();
       
   178 	paraF->iLanguage = currentLanguage;
       
   179 	TParaFormatMask langMask;
       
   180 	langMask.SetAttrib(EAttParaLanguage);
       
   181 	rT->ApplyParaFormatL(paraF, langMask, iEdwin->CursorPos(), 1);
       
   182 	iEdwin->TextView()->HandleRangeFormatChangeL(TCursorSelection(0, iEdwin->TextLength()), ETrue);
       
   183 
       
   184 	CleanupStack::PopAndDestroy(); // paraF
       
   185 	}