os/textandloc/textrendering/textformatting/test/src/TTranspEditor.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/textandloc/textrendering/textformatting/test/src/TTranspEditor.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,391 @@
     1.4 +/*
     1.5 +* Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* TTranspEditor test source file. A base set of classes, neeeded for transparent editors
    1.19 +* functionality testing, is defined here.
    1.20 +* It is an "APP" test, where you can run TTranspEditor test application and check how the 
    1.21 +* trransparency/opaque drawing works. Currently you may see the effect of opaque drawing
    1.22 +* applied on texts, pictures, selections.
    1.23 +*
    1.24 +*/
    1.25 +
    1.26 +
    1.27 +#include <coecntrl.h>
    1.28 +#include <coeccntx.h>
    1.29 +#include <coemain.h>
    1.30 +#include <eikon.hrh>
    1.31 +#include <eikappui.h>
    1.32 +#include <eikapp.h>
    1.33 +#include <eikdoc.h>
    1.34 +#include <eikenv.h>
    1.35 +#include <eikdef.h>
    1.36 +#include <txtrich.h>
    1.37 +#include <frmtview.h>
    1.38 +#include <eikstart.h>
    1.39 +#include "TTranspEditor.h"
    1.40 +#include "TTranspEditor.hrh"
    1.41 +#include <ttranspeditor.rsg>
    1.42 +
    1.43 +const TUid KAppUid = {0x13579ACE};
    1.44 +
    1.45 +////////////////////////////////////////////////////////////////////////////////////////////
    1.46 +//Picture
    1.47 +//CTestPicture's instances can be inserted and displayed into the transparent text view,
    1.48 +//used in the test.
    1.49 +
    1.50 +CTestPicture* CTestPicture::NewL()
    1.51 +	{
    1.52 +	CTestPicture* self = new (ELeave) CTestPicture;
    1.53 +	CleanupStack::PushL(self);
    1.54 +	self->ConstructL();
    1.55 +	CleanupStack::Pop(self);
    1.56 +	return self;
    1.57 +	}
    1.58 +
    1.59 +CTestPicture::~CTestPicture()
    1.60 +	{
    1.61 +	delete iBitmap;
    1.62 +	}
    1.63 +
    1.64 +CTestPicture::CTestPicture()
    1.65 +	{
    1.66 +	}
    1.67 +
    1.68 +void CTestPicture::ConstructL()
    1.69 +	{
    1.70 +	_LIT(KDataMbmFile, "z:\\system\\data\\TTrEdData.mbm");
    1.71 +	iBitmap = new (ELeave) CFbsBitmap;
    1.72 +	User::LeaveIfError(iBitmap->Load(KDataMbmFile, 0));	
    1.73 +	}
    1.74 +
    1.75 +void CTestPicture::Draw(CGraphicsContext& aGc, const TPoint&, const TRect& aRc, MGraphicsDeviceMap*) const
    1.76 +	{
    1.77 +	aGc.DrawBitmap(aRc, iBitmap, aRc);
    1.78 +	}
    1.79 +
    1.80 +void CTestPicture::ExternalizeL(RWriteStream&) const
    1.81 +	{
    1.82 +	}
    1.83 +
    1.84 +void CTestPicture::GetOriginalSizeInTwips(TSize& aSize) const
    1.85 +	{
    1.86 +	aSize = iBitmap->SizeInTwips();
    1.87 +	}
    1.88 +
    1.89 +TInt CTestPicture::ScaleFactorWidth() const
    1.90 +	{
    1.91 +	return CPicture::ScaleFactorWidth() * 6;
    1.92 +	}
    1.93 +
    1.94 +TInt CTestPicture::ScaleFactorHeight() const
    1.95 +	{
    1.96 +	return CPicture::ScaleFactorHeight() * 6;
    1.97 +	}
    1.98 +
    1.99 +////////////////////////////////////////////////////////////////////////////////////////////
   1.100 +//Application
   1.101 +
   1.102 +CApaDocument* CTranspEditorApp::CreateDocumentL()
   1.103 +	{
   1.104 +	return new (ELeave) CTranspEditorDoc(*this);
   1.105 +	}
   1.106 +
   1.107 +TUid CTranspEditorApp::AppDllUid() const
   1.108 +	{
   1.109 +	return KAppUid;
   1.110 +	}
   1.111 +
   1.112 +////////////////////////////////////////////////////////////////////////////////////////////
   1.113 +//View1
   1.114 +//It is used to create and display a background bitmap, needed for asserting opaque drawing
   1.115 +//functionality.
   1.116 +
   1.117 +CTranspEditorView1* CTranspEditorView1::NewL()
   1.118 +	{
   1.119 +	CTranspEditorView1* self = new (ELeave) CTranspEditorView1;
   1.120 +	CleanupStack::PushL(self);
   1.121 +	self->ContructL();
   1.122 +	CleanupStack::Pop(self);
   1.123 +	return self;
   1.124 +	}
   1.125 +
   1.126 +CTranspEditorView1::~CTranspEditorView1()
   1.127 +	{
   1.128 +	delete iBitmap;
   1.129 +	}
   1.130 +
   1.131 +void CTranspEditorView1::ContructL()
   1.132 +	{
   1.133 +	_LIT(KDilbertMbmFile, "z:\\system\\data\\TTrEdDilbert.mbm");
   1.134 +	iBitmap = new (ELeave) CFbsBitmap;
   1.135 +	User::LeaveIfError(iBitmap->Load(KDilbertMbmFile, 0));
   1.136 +
   1.137 +	CreateWindowL();
   1.138 +	TSize size = iEikonEnv->ScreenDevice()->SizeInPixels();
   1.139 +	const TRect KViewRect(size);
   1.140 +	SetRect(KViewRect);
   1.141 +	ActivateL();
   1.142 +	}
   1.143 +
   1.144 +void CTranspEditorView1::Draw(const TRect&) const
   1.145 +	{
   1.146 +	CWindowGc& gc = SystemGc();
   1.147 +	TRect rc = Rect();
   1.148 +	TRect rc2(rc.iTl.iX + 1, rc.iTl.iY + 1, rc.iBr.iX - 1, rc.iBr.iY - 1);
   1.149 +
   1.150 +	gc.Clear();
   1.151 +
   1.152 +	TRgb cl(0xFF, 0x50, 0x00);
   1.153 +	gc.SetPenColor(cl);
   1.154 +	gc.DrawRect(rc);
   1.155 +
   1.156 +	gc.DrawBitmap(rc2, iBitmap);
   1.157 +	}
   1.158 +
   1.159 +/////////////////////////////////////////////////////////////////////////////////////////////
   1.160 +//View2
   1.161 +//Transparent text view.
   1.162 +
   1.163 +CTranspEditorView2* CTranspEditorView2::NewL()
   1.164 +	{
   1.165 +	CTranspEditorView2* self = new (ELeave) CTranspEditorView2;
   1.166 +	CleanupStack::PushL(self);
   1.167 +	self->ContructL();
   1.168 +	CleanupStack::Pop(self);
   1.169 +	return self;
   1.170 +	}
   1.171 +
   1.172 +CTranspEditorView2::~CTranspEditorView2()
   1.173 +	{
   1.174 +	delete iTextView;
   1.175 +	delete iLayout;
   1.176 +	delete iRichText;
   1.177 +	}
   1.178 +
   1.179 +//Correspond to EAppCmdInsertText user interface command.
   1.180 +//Insert a text chunk into the transparent text view.
   1.181 +void CTranspEditorView2::InsertTextL()
   1.182 +	{
   1.183 +	_LIT(KText, "ABC123");
   1.184 +	iRichText->InsertL(iRichText->DocumentLength(), KText);
   1.185 +
   1.186 +	TViewYPosQualifier a1;
   1.187 +	a1.SetFillScreen();
   1.188 +	a1.SetMakeLineFullyVisible();
   1.189 +	iTextView->HandleGlobalChangeL(a1);
   1.190 +	}
   1.191 +
   1.192 +//Correspond to EAppCmdSwitchOpaque user interface command.
   1.193 +//Switch on/off opaque drawing mode.
   1.194 +void CTranspEditorView2::SwitchOpaque()
   1.195 +	{
   1.196 +	iOpaque = !iOpaque;
   1.197 +	iTextView->SetOpaque(iOpaque);
   1.198 +	DrawNow();
   1.199 +	}
   1.200 +
   1.201 +//Correspond to EAppCmdSwitchSelect user interface command.
   1.202 +//Switch on/off text selection.
   1.203 +void CTranspEditorView2::SwitchSelectL()
   1.204 +	{
   1.205 +	iSelect = !iSelect;
   1.206 +	if(iSelect)
   1.207 +		{
   1.208 +		TTmDocPosSpec docPosSpec(10, TTmDocPosSpec::ELeftToRight);
   1.209 +		TTmPosInfo2 posInfo;
   1.210 +		TBool res = iTextView->FindDocPosL(docPosSpec, posInfo);
   1.211 +		if(res)
   1.212 +			{
   1.213 +			TCursorSelection sel(posInfo.iDocPos.iPos, 0);
   1.214 +			iTextView->SetSelectionL(sel);
   1.215 +			DrawNow();
   1.216 +			}
   1.217 +		}
   1.218 +	else
   1.219 +		{
   1.220 +		iTextView->ClearSelectionL();
   1.221 +		DrawNow();
   1.222 +		}
   1.223 +	}
   1.224 +
   1.225 +//Correspond to EAppCmdInsertPicture user interface command.
   1.226 +//Create and insert a picture into the transparent text view.
   1.227 +void CTranspEditorView2::InsertPictureL()
   1.228 +	{
   1.229 +	CTestPicture* pic = CTestPicture::NewL();
   1.230 +	CleanupStack::PushL(pic);
   1.231 +
   1.232 +	TSize size;
   1.233 +	pic->GetOriginalSizeInTwips(size);
   1.234 +
   1.235 +	TPictureHeader pictheader;
   1.236 +	pictheader.iSize = size;
   1.237 +	pictheader.iPicture = pic;
   1.238 +
   1.239 +	iRichText->InsertL(iRichText->DocumentLength(), pictheader);
   1.240 +	CleanupStack::Pop(pic);
   1.241 +
   1.242 +	TViewYPosQualifier a1;
   1.243 +	a1.SetFillScreen();
   1.244 +	a1.SetMakeLineFullyVisible();
   1.245 +	iTextView->HandleGlobalChangeL(a1);
   1.246 +	}
   1.247 +
   1.248 +//Correspond to EAppCmdSetCharFormat user interface command.
   1.249 +//Change the format of the selected text.
   1.250 +void CTranspEditorView2::SetCharFormatL()
   1.251 +	{
   1.252 +	TCharFormat charFormat;
   1.253 +	TCharFormatMask charFormatMask;
   1.254 +	charFormat.iFontSpec.iFontStyle.SetPosture(EPostureItalic);
   1.255 +	charFormatMask.SetAttrib(EAttFontPosture);
   1.256 +	TCursorSelection select = iTextView->Selection();
   1.257 +	if(select.Length() != 0)
   1.258 +		{
   1.259 +		iRichText->ApplyCharFormatL(charFormat, charFormatMask, select.LowerPos(), select.Length());
   1.260 +		iTextView->HandleRangeFormatChangeL(select);
   1.261 +		}
   1.262 +	else
   1.263 +		{
   1.264 +		iRichText->SetInsertCharFormatL(charFormat, charFormatMask, select.iCursorPos);
   1.265 +		}
   1.266 +	}
   1.267 +
   1.268 +//Construct a transparent text view, which is displayed on top of the background bitmap, 
   1.269 +//which makes the transparency effect very well visible.
   1.270 +void CTranspEditorView2::ContructL()
   1.271 +	{
   1.272 +	CreateWindowL(); 
   1.273 +	TSize size = iEikonEnv->ScreenDevice()->SizeInPixels();
   1.274 +	const TRect KViewRect(15, 15, size.iWidth - 15, size.iHeight - 15);
   1.275 +	SetRect(KViewRect);
   1.276 +
   1.277 +	iRichText = CRichText::NewL(iEikonEnv->SystemParaFormatLayerL(), iEikonEnv->SystemCharFormatLayerL());
   1.278 +	iLayout = CTextLayout::NewL(iRichText, KViewRect.Width() - 2);
   1.279 +
   1.280 +	TRect rc(KViewRect);
   1.281 +	rc.Shrink(1, 1);
   1.282 +	rc.Move(-rc.iTl.iX + 1, -rc.iTl.iY + 1);
   1.283 +	iTextView = CTextView::NewL(iLayout, 
   1.284 +								rc, 
   1.285 +								iEikonEnv->ScreenDevice(),
   1.286 +								iEikonEnv->ScreenDevice(),
   1.287 +								&Window(), 
   1.288 +								&iEikonEnv->RootWin(), 
   1.289 +								&iEikonEnv->WsSession());
   1.290 +
   1.291 +	iTextView->EnablePictureFrameL(ETrue);
   1.292 +	const TRgb KTransparencyColor(85, 85, 85);
   1.293 +	Window().SetTransparencyFactor(KTransparencyColor);
   1.294 +
   1.295 +	ActivateL();
   1.296 +	}
   1.297 +
   1.298 +void CTranspEditorView2::Draw(const TRect&) const
   1.299 +	{
   1.300 +	CWindowGc& gc = SystemGc();
   1.301 +	TRect rc = Rect();
   1.302 +	TRect rc2(rc.iTl.iX + 1, rc.iTl.iY + 1, rc.iBr.iX - 1, rc.iBr.iY - 1);
   1.303 +
   1.304 +	gc.Clear();
   1.305 +
   1.306 +	const TRgb KPenColor(0x00, 0x00, 0xFF);
   1.307 +	gc.SetPenColor(KPenColor);
   1.308 +	gc.DrawRect(rc);
   1.309 +
   1.310 +	TRAPD(err, iTextView->DrawL(rc2));
   1.311 +    if(err != KErrNone)
   1.312 +		{
   1.313 +		SystemGc().Clear(rc2);
   1.314 +		iEikonEnv->NotifyIdleErrorWhileRedrawing(err);
   1.315 +		}
   1.316 +	}
   1.317 +
   1.318 +////////////////////////////////////////////////////////////////////////////////////////////
   1.319 +//Document
   1.320 +
   1.321 +CTranspEditorDoc::CTranspEditorDoc(CEikApplication& aApp) :
   1.322 +	CEikDocument(aApp)
   1.323 +	{
   1.324 +	}
   1.325 +
   1.326 +CEikAppUi* CTranspEditorDoc::CreateAppUiL()
   1.327 +	{
   1.328 +	return new (ELeave) CTranspEditorUi;
   1.329 +	}
   1.330 +
   1.331 +////////////////////////////////////////////////////////////////////////////////////////////
   1.332 +//UI
   1.333 +
   1.334 +void CTranspEditorUi::ConstructL()
   1.335 +	{
   1.336 +	BaseConstructL();
   1.337 +
   1.338 +	iTranspEditorView1 = CTranspEditorView1::NewL();
   1.339 +	AddToStackL(iTranspEditorView1);
   1.340 +
   1.341 +	iTranspEditorView2 = CTranspEditorView2::NewL();
   1.342 +	AddToStackL(iTranspEditorView2);
   1.343 +	}
   1.344 +
   1.345 +CTranspEditorUi::~CTranspEditorUi()
   1.346 +	{
   1.347 +	RemoveFromStack(iTranspEditorView2);
   1.348 +	delete iTranspEditorView2;
   1.349 +
   1.350 +	RemoveFromStack(iTranspEditorView1);
   1.351 +	delete iTranspEditorView1;
   1.352 +	}
   1.353 +
   1.354 +void CTranspEditorUi::HandleCommandL(TInt aCommand)
   1.355 +	{
   1.356 +	switch(aCommand)
   1.357 +		{
   1.358 +		case EAppCmdExit:
   1.359 +			CBaActiveScheduler::Exit();
   1.360 +			break;
   1.361 +		case EAppCmdInsertText:
   1.362 +			iTranspEditorView2->InsertTextL();
   1.363 +			break;
   1.364 +		case EAppCmdSwitchOpaque:
   1.365 +			iTranspEditorView2->SwitchOpaque();
   1.366 +			break;
   1.367 +		case EAppCmdSwitchSelect:
   1.368 +			iTranspEditorView2->SwitchSelectL();
   1.369 +			break;
   1.370 +		case EAppCmdInsertPicture:
   1.371 +			iTranspEditorView2->InsertPictureL();
   1.372 +			break;
   1.373 +		case EAppCmdSetCharFormat:
   1.374 +			iTranspEditorView2->SetCharFormatL();
   1.375 +			break;
   1.376 +		default:
   1.377 +			break;
   1.378 +		}
   1.379 +	}
   1.380 +
   1.381 +////////////////////////////////////////////////////////////////////////////////////////////
   1.382 +//
   1.383 +
   1.384 +	static CApaApplication* NewApplication()
   1.385 +		{
   1.386 +		return new CTranspEditorApp;
   1.387 +		}
   1.388 +
   1.389 +	TInt E32Main()
   1.390 +		{
   1.391 +		return EikStart::RunApplication(&NewApplication);
   1.392 +		}
   1.393 +
   1.394 +