textrendering/textformatting/test/src/TTranspEditor.cpp
changeset 0 1fb32624e06b
child 16 748ec5531811
equal deleted inserted replaced
-1:000000000000 0:1fb32624e06b
       
     1 /*
       
     2 * Copyright (c) 2004-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 * TTranspEditor test source file. A base set of classes, neeeded for transparent editors
       
    16 * functionality testing, is defined here.
       
    17 * It is an "APP" test, where you can run TTranspEditor test application and check how the 
       
    18 * trransparency/opaque drawing works. Currently you may see the effect of opaque drawing
       
    19 * applied on texts, pictures, selections.
       
    20 *
       
    21 */
       
    22 
       
    23 
       
    24 #include <coecntrl.h>
       
    25 #include <coeccntx.h>
       
    26 #include <coemain.h>
       
    27 #include <techview/eikon.hrh>
       
    28 #include <eikappui.h>
       
    29 #include <eikapp.h>
       
    30 #include <eikdoc.h>
       
    31 #include <eikenv.h>
       
    32 #include <eikdef.h>
       
    33 #include <txtrich.h>
       
    34 #include <frmtview.h>
       
    35 #include <eikstart.h>
       
    36 #include "TTranspEditor.h"
       
    37 #include "TTranspEditor.hrh"
       
    38 #include <ttranspeditor.rsg>
       
    39 
       
    40 const TUid KAppUid = {0x13579ACE};
       
    41 
       
    42 ////////////////////////////////////////////////////////////////////////////////////////////
       
    43 //Picture
       
    44 //CTestPicture's instances can be inserted and displayed into the transparent text view,
       
    45 //used in the test.
       
    46 
       
    47 CTestPicture* CTestPicture::NewL()
       
    48 	{
       
    49 	CTestPicture* self = new (ELeave) CTestPicture;
       
    50 	CleanupStack::PushL(self);
       
    51 	self->ConstructL();
       
    52 	CleanupStack::Pop(self);
       
    53 	return self;
       
    54 	}
       
    55 
       
    56 CTestPicture::~CTestPicture()
       
    57 	{
       
    58 	delete iBitmap;
       
    59 	}
       
    60 
       
    61 CTestPicture::CTestPicture()
       
    62 	{
       
    63 	}
       
    64 
       
    65 void CTestPicture::ConstructL()
       
    66 	{
       
    67 	_LIT(KDataMbmFile, "z:\\system\\data\\TTrEdData.mbm");
       
    68 	iBitmap = new (ELeave) CFbsBitmap;
       
    69 	User::LeaveIfError(iBitmap->Load(KDataMbmFile, 0));	
       
    70 	}
       
    71 
       
    72 void CTestPicture::Draw(CGraphicsContext& aGc, const TPoint&, const TRect& aRc, MGraphicsDeviceMap*) const
       
    73 	{
       
    74 	aGc.DrawBitmap(aRc, iBitmap, aRc);
       
    75 	}
       
    76 
       
    77 void CTestPicture::ExternalizeL(RWriteStream&) const
       
    78 	{
       
    79 	}
       
    80 
       
    81 void CTestPicture::GetOriginalSizeInTwips(TSize& aSize) const
       
    82 	{
       
    83 	aSize = iBitmap->SizeInTwips();
       
    84 	}
       
    85 
       
    86 TInt CTestPicture::ScaleFactorWidth() const
       
    87 	{
       
    88 	return CPicture::ScaleFactorWidth() * 6;
       
    89 	}
       
    90 
       
    91 TInt CTestPicture::ScaleFactorHeight() const
       
    92 	{
       
    93 	return CPicture::ScaleFactorHeight() * 6;
       
    94 	}
       
    95 
       
    96 ////////////////////////////////////////////////////////////////////////////////////////////
       
    97 //Application
       
    98 
       
    99 CApaDocument* CTranspEditorApp::CreateDocumentL()
       
   100 	{
       
   101 	return new (ELeave) CTranspEditorDoc(*this);
       
   102 	}
       
   103 
       
   104 TUid CTranspEditorApp::AppDllUid() const
       
   105 	{
       
   106 	return KAppUid;
       
   107 	}
       
   108 
       
   109 ////////////////////////////////////////////////////////////////////////////////////////////
       
   110 //View1
       
   111 //It is used to create and display a background bitmap, needed for asserting opaque drawing
       
   112 //functionality.
       
   113 
       
   114 CTranspEditorView1* CTranspEditorView1::NewL()
       
   115 	{
       
   116 	CTranspEditorView1* self = new (ELeave) CTranspEditorView1;
       
   117 	CleanupStack::PushL(self);
       
   118 	self->ContructL();
       
   119 	CleanupStack::Pop(self);
       
   120 	return self;
       
   121 	}
       
   122 
       
   123 CTranspEditorView1::~CTranspEditorView1()
       
   124 	{
       
   125 	delete iBitmap;
       
   126 	}
       
   127 
       
   128 void CTranspEditorView1::ContructL()
       
   129 	{
       
   130 	_LIT(KDilbertMbmFile, "z:\\system\\data\\TTrEdDilbert.mbm");
       
   131 	iBitmap = new (ELeave) CFbsBitmap;
       
   132 	User::LeaveIfError(iBitmap->Load(KDilbertMbmFile, 0));
       
   133 
       
   134 	CreateWindowL();
       
   135 	TSize size = iEikonEnv->ScreenDevice()->SizeInPixels();
       
   136 	const TRect KViewRect(size);
       
   137 	SetRect(KViewRect);
       
   138 	ActivateL();
       
   139 	}
       
   140 
       
   141 void CTranspEditorView1::Draw(const TRect&) const
       
   142 	{
       
   143 	CWindowGc& gc = SystemGc();
       
   144 	TRect rc = Rect();
       
   145 	TRect rc2(rc.iTl.iX + 1, rc.iTl.iY + 1, rc.iBr.iX - 1, rc.iBr.iY - 1);
       
   146 
       
   147 	gc.Clear();
       
   148 
       
   149 	TRgb cl(0xFF, 0x50, 0x00);
       
   150 	gc.SetPenColor(cl);
       
   151 	gc.DrawRect(rc);
       
   152 
       
   153 	gc.DrawBitmap(rc2, iBitmap);
       
   154 	}
       
   155 
       
   156 /////////////////////////////////////////////////////////////////////////////////////////////
       
   157 //View2
       
   158 //Transparent text view.
       
   159 
       
   160 CTranspEditorView2* CTranspEditorView2::NewL()
       
   161 	{
       
   162 	CTranspEditorView2* self = new (ELeave) CTranspEditorView2;
       
   163 	CleanupStack::PushL(self);
       
   164 	self->ContructL();
       
   165 	CleanupStack::Pop(self);
       
   166 	return self;
       
   167 	}
       
   168 
       
   169 CTranspEditorView2::~CTranspEditorView2()
       
   170 	{
       
   171 	delete iTextView;
       
   172 	delete iLayout;
       
   173 	delete iRichText;
       
   174 	}
       
   175 
       
   176 //Correspond to EAppCmdInsertText user interface command.
       
   177 //Insert a text chunk into the transparent text view.
       
   178 void CTranspEditorView2::InsertTextL()
       
   179 	{
       
   180 	_LIT(KText, "ABC123");
       
   181 	iRichText->InsertL(iRichText->DocumentLength(), KText);
       
   182 
       
   183 	TViewYPosQualifier a1;
       
   184 	a1.SetFillScreen();
       
   185 	a1.SetMakeLineFullyVisible();
       
   186 	iTextView->HandleGlobalChangeL(a1);
       
   187 	}
       
   188 
       
   189 //Correspond to EAppCmdSwitchOpaque user interface command.
       
   190 //Switch on/off opaque drawing mode.
       
   191 void CTranspEditorView2::SwitchOpaque()
       
   192 	{
       
   193 	iOpaque = !iOpaque;
       
   194 	iTextView->SetOpaque(iOpaque);
       
   195 	DrawNow();
       
   196 	}
       
   197 
       
   198 //Correspond to EAppCmdSwitchSelect user interface command.
       
   199 //Switch on/off text selection.
       
   200 void CTranspEditorView2::SwitchSelectL()
       
   201 	{
       
   202 	iSelect = !iSelect;
       
   203 	if(iSelect)
       
   204 		{
       
   205 		TTmDocPosSpec docPosSpec(10, TTmDocPosSpec::ELeftToRight);
       
   206 		TTmPosInfo2 posInfo;
       
   207 		TBool res = iTextView->FindDocPosL(docPosSpec, posInfo);
       
   208 		if(res)
       
   209 			{
       
   210 			TCursorSelection sel(posInfo.iDocPos.iPos, 0);
       
   211 			iTextView->SetSelectionL(sel);
       
   212 			DrawNow();
       
   213 			}
       
   214 		}
       
   215 	else
       
   216 		{
       
   217 		iTextView->ClearSelectionL();
       
   218 		DrawNow();
       
   219 		}
       
   220 	}
       
   221 
       
   222 //Correspond to EAppCmdInsertPicture user interface command.
       
   223 //Create and insert a picture into the transparent text view.
       
   224 void CTranspEditorView2::InsertPictureL()
       
   225 	{
       
   226 	CTestPicture* pic = CTestPicture::NewL();
       
   227 	CleanupStack::PushL(pic);
       
   228 
       
   229 	TSize size;
       
   230 	pic->GetOriginalSizeInTwips(size);
       
   231 
       
   232 	TPictureHeader pictheader;
       
   233 	pictheader.iSize = size;
       
   234 	pictheader.iPicture = pic;
       
   235 
       
   236 	iRichText->InsertL(iRichText->DocumentLength(), pictheader);
       
   237 	CleanupStack::Pop(pic);
       
   238 
       
   239 	TViewYPosQualifier a1;
       
   240 	a1.SetFillScreen();
       
   241 	a1.SetMakeLineFullyVisible();
       
   242 	iTextView->HandleGlobalChangeL(a1);
       
   243 	}
       
   244 
       
   245 //Correspond to EAppCmdSetCharFormat user interface command.
       
   246 //Change the format of the selected text.
       
   247 void CTranspEditorView2::SetCharFormatL()
       
   248 	{
       
   249 	TCharFormat charFormat;
       
   250 	TCharFormatMask charFormatMask;
       
   251 	charFormat.iFontSpec.iFontStyle.SetPosture(EPostureItalic);
       
   252 	charFormatMask.SetAttrib(EAttFontPosture);
       
   253 	TCursorSelection select = iTextView->Selection();
       
   254 	if(select.Length() != 0)
       
   255 		{
       
   256 		iRichText->ApplyCharFormatL(charFormat, charFormatMask, select.LowerPos(), select.Length());
       
   257 		iTextView->HandleRangeFormatChangeL(select);
       
   258 		}
       
   259 	else
       
   260 		{
       
   261 		iRichText->SetInsertCharFormatL(charFormat, charFormatMask, select.iCursorPos);
       
   262 		}
       
   263 	}
       
   264 
       
   265 //Construct a transparent text view, which is displayed on top of the background bitmap, 
       
   266 //which makes the transparency effect very well visible.
       
   267 void CTranspEditorView2::ContructL()
       
   268 	{
       
   269 	CreateWindowL(); 
       
   270 	TSize size = iEikonEnv->ScreenDevice()->SizeInPixels();
       
   271 	const TRect KViewRect(15, 15, size.iWidth - 15, size.iHeight - 15);
       
   272 	SetRect(KViewRect);
       
   273 
       
   274 	iRichText = CRichText::NewL(iEikonEnv->SystemParaFormatLayerL(), iEikonEnv->SystemCharFormatLayerL());
       
   275 	iLayout = CTextLayout::NewL(iRichText, KViewRect.Width() - 2);
       
   276 
       
   277 	TRect rc(KViewRect);
       
   278 	rc.Shrink(1, 1);
       
   279 	rc.Move(-rc.iTl.iX + 1, -rc.iTl.iY + 1);
       
   280 	iTextView = CTextView::NewL(iLayout, 
       
   281 								rc, 
       
   282 								iEikonEnv->ScreenDevice(),
       
   283 								iEikonEnv->ScreenDevice(),
       
   284 								&Window(), 
       
   285 								&iEikonEnv->RootWin(), 
       
   286 								&iEikonEnv->WsSession());
       
   287 
       
   288 	iTextView->EnablePictureFrameL(ETrue);
       
   289 	const TRgb KTransparencyColor(85, 85, 85);
       
   290 	Window().SetTransparencyFactor(KTransparencyColor);
       
   291 
       
   292 	ActivateL();
       
   293 	}
       
   294 
       
   295 void CTranspEditorView2::Draw(const TRect&) const
       
   296 	{
       
   297 	CWindowGc& gc = SystemGc();
       
   298 	TRect rc = Rect();
       
   299 	TRect rc2(rc.iTl.iX + 1, rc.iTl.iY + 1, rc.iBr.iX - 1, rc.iBr.iY - 1);
       
   300 
       
   301 	gc.Clear();
       
   302 
       
   303 	const TRgb KPenColor(0x00, 0x00, 0xFF);
       
   304 	gc.SetPenColor(KPenColor);
       
   305 	gc.DrawRect(rc);
       
   306 
       
   307 	TRAPD(err, iTextView->DrawL(rc2));
       
   308     if(err != KErrNone)
       
   309 		{
       
   310 		SystemGc().Clear(rc2);
       
   311 		iEikonEnv->NotifyIdleErrorWhileRedrawing(err);
       
   312 		}
       
   313 	}
       
   314 
       
   315 ////////////////////////////////////////////////////////////////////////////////////////////
       
   316 //Document
       
   317 
       
   318 CTranspEditorDoc::CTranspEditorDoc(CEikApplication& aApp) :
       
   319 	CEikDocument(aApp)
       
   320 	{
       
   321 	}
       
   322 
       
   323 CEikAppUi* CTranspEditorDoc::CreateAppUiL()
       
   324 	{
       
   325 	return new (ELeave) CTranspEditorUi;
       
   326 	}
       
   327 
       
   328 ////////////////////////////////////////////////////////////////////////////////////////////
       
   329 //UI
       
   330 
       
   331 void CTranspEditorUi::ConstructL()
       
   332 	{
       
   333 	BaseConstructL();
       
   334 
       
   335 	iTranspEditorView1 = CTranspEditorView1::NewL();
       
   336 	AddToStackL(iTranspEditorView1);
       
   337 
       
   338 	iTranspEditorView2 = CTranspEditorView2::NewL();
       
   339 	AddToStackL(iTranspEditorView2);
       
   340 	}
       
   341 
       
   342 CTranspEditorUi::~CTranspEditorUi()
       
   343 	{
       
   344 	RemoveFromStack(iTranspEditorView2);
       
   345 	delete iTranspEditorView2;
       
   346 
       
   347 	RemoveFromStack(iTranspEditorView1);
       
   348 	delete iTranspEditorView1;
       
   349 	}
       
   350 
       
   351 void CTranspEditorUi::HandleCommandL(TInt aCommand)
       
   352 	{
       
   353 	switch(aCommand)
       
   354 		{
       
   355 		case EAppCmdExit:
       
   356 			CBaActiveScheduler::Exit();
       
   357 			break;
       
   358 		case EAppCmdInsertText:
       
   359 			iTranspEditorView2->InsertTextL();
       
   360 			break;
       
   361 		case EAppCmdSwitchOpaque:
       
   362 			iTranspEditorView2->SwitchOpaque();
       
   363 			break;
       
   364 		case EAppCmdSwitchSelect:
       
   365 			iTranspEditorView2->SwitchSelectL();
       
   366 			break;
       
   367 		case EAppCmdInsertPicture:
       
   368 			iTranspEditorView2->InsertPictureL();
       
   369 			break;
       
   370 		case EAppCmdSetCharFormat:
       
   371 			iTranspEditorView2->SetCharFormatL();
       
   372 			break;
       
   373 		default:
       
   374 			break;
       
   375 		}
       
   376 	}
       
   377 
       
   378 ////////////////////////////////////////////////////////////////////////////////////////////
       
   379 //
       
   380 
       
   381 	static CApaApplication* NewApplication()
       
   382 		{
       
   383 		return new CTranspEditorApp;
       
   384 		}
       
   385 
       
   386 	TInt E32Main()
       
   387 		{
       
   388 		return EikStart::RunApplication(&NewApplication);
       
   389 		}
       
   390 
       
   391