appfw/apparchitecture/tef/tRuleBasedApps/tRuleBasedApp4.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // This is a simple Application containing a single view with
       
    15 // text drawn on it.
       
    16 // The app includes code for displaying a very simple menu.
       
    17 // This source file contains the single exported function required by
       
    18 // all UI Applications and the E32Dll function which is also required
       
    19 // but is not used here.
       
    20 // The class definitions are:
       
    21 // CTRuleBasedApplication
       
    22 // CTRuleBasedAppUi
       
    23 // CTRuleBasedAppView
       
    24 // CTRuleBasedDocument
       
    25 // 
       
    26 //
       
    27 
       
    28 /**
       
    29  @file
       
    30  @internalComponent - Internal Symbian test code
       
    31 */
       
    32 
       
    33 #include <coeccntx.h>
       
    34 #include <eikenv.h>
       
    35 #include <eikappui.h>
       
    36 #include <eikapp.h>
       
    37 #include <eikdoc.h>
       
    38 #include <techview/eikmenup.h>
       
    39 #include <eikstart.h>
       
    40 
       
    41 #include <techview/eikon.hrh>
       
    42 
       
    43 #include <trulebasedapp4.rsg>
       
    44 #include "tRuleBasedApp.hrh"
       
    45 
       
    46 //const TInt KLeft 	= 0; // Display app window on the left
       
    47 const TInt KCentre 	= 1; // Display app window in the centre
       
    48 const TInt KRight 	= 2; // Display app window on the right
       
    49 
       
    50 //
       
    51 //
       
    52 // CTRuleBasedApplication
       
    53 //
       
    54 //
       
    55 
       
    56 class CTRuleBasedApplication : public CEikApplication
       
    57 	{
       
    58 private:
       
    59 	CApaDocument* CreateDocumentL();
       
    60 	TUid AppDllUid() const;
       
    61 	};
       
    62 	
       
    63 //
       
    64 //
       
    65 // CTRuleBasedAppView
       
    66 //
       
    67 //
       
    68 class CTRuleBasedAppView : public CCoeControl
       
    69  	{
       
    70 public:
       
    71 	static CTRuleBasedAppView* NewL(const TRect& aRect);
       
    72 	CTRuleBasedAppView();
       
    73 	~CTRuleBasedAppView();
       
    74 	void ConstructL(const TRect& aRect);
       
    75 public:
       
    76 	TInt iAppType;
       
    77 
       
    78 private:
       
    79 	// Inherited from CCoeControl
       
    80 	void Draw(const TRect& /*aRect*/) const;
       
    81 	const CFont* CreateCustomFontL( const TDesC& aFontName, TInt aHeightInPixels, TBool aBold ) const;
       
    82 
       
    83 private:
       
    84 	HBufC* iRuleBasedText;
       
    85  	};
       
    86 
       
    87 //
       
    88 //
       
    89 // CTRuleBasedAppUi
       
    90 //
       
    91 //
       
    92 class CTRuleBasedAppUi : public CEikAppUi
       
    93 	{
       
    94 public:
       
    95 	void ConstructL();
       
    96 	~CTRuleBasedAppUi();
       
    97 
       
    98 private:
       
    99 	// Inherirted from class CEikAppUi
       
   100 	void HandleCommandL(TInt aCommand);
       
   101 
       
   102 private:
       
   103 	CTRuleBasedAppView* iAppView;
       
   104 	};
       
   105 
       
   106 //
       
   107 //
       
   108 // CTRuleBasedDocument
       
   109 //
       
   110 //
       
   111 class CTRuleBasedDocument : public CEikDocument
       
   112 	{
       
   113 public:
       
   114 	static CTRuleBasedDocument* NewL(CEikApplication& aApp);
       
   115 	CTRuleBasedDocument(CEikApplication& aApp);
       
   116 	void ConstructL();
       
   117 private:
       
   118 	// Inherited from CEikDocument
       
   119 	CEikAppUi* CreateAppUiL();
       
   120 	};
       
   121 
       
   122 //
       
   123 //
       
   124 // Application Document class - CTRuleBasedAppDocument
       
   125 //
       
   126 //
       
   127 
       
   128 /**
       
   129 The constructor of the document class just passes the
       
   130 supplied reference to the constructor initialisation list.
       
   131 The document has no real work to do in this Application.
       
   132 */
       
   133 
       
   134 CTRuleBasedDocument::CTRuleBasedDocument(CEikApplication& aApp)
       
   135 	: CEikDocument(aApp)
       
   136 	{
       
   137 	}
       
   138 
       
   139 /**
       
   140 This is called by the UI framework as soon as the
       
   141 document has been created. It creates an instance
       
   142 of the ApplicationUI. The Application UI class is
       
   143 an instance of a CEikAppUi derived class.
       
   144 */
       
   145 
       
   146 CEikAppUi* CTRuleBasedDocument::CreateAppUiL()
       
   147 	{
       
   148  	return new(ELeave) CTRuleBasedAppUi;
       
   149 	}
       
   150 
       
   151 //
       
   152 //
       
   153 // Source for the implementation of the 
       
   154 // Application view class - CTRuleBasedAppView
       
   155 //
       
   156 //
       
   157 
       
   158 
       
   159 // Constructor for the view.
       
   160 
       
   161 CTRuleBasedAppView::CTRuleBasedAppView():iAppType(KCentre)
       
   162 	{
       
   163 	}
       
   164 /**
       
   165 Static NewL() function to start the standard two
       
   166 phase construction.
       
   167 */
       
   168 
       
   169 CTRuleBasedAppView* CTRuleBasedAppView::NewL(const TRect& aRect)
       
   170 	{
       
   171 	CTRuleBasedAppView* self = new(ELeave) CTRuleBasedAppView();
       
   172 	CleanupStack::PushL(self);
       
   173 	self->ConstructL(aRect);
       
   174 	CleanupStack::Pop();
       
   175 	return self;
       
   176 	}
       
   177 
       
   178 //
       
   179 // Destructor for the view.
       
   180 
       
   181 CTRuleBasedAppView::~CTRuleBasedAppView()
       
   182 	{
       
   183 	delete iRuleBasedText;
       
   184 	}
       
   185 
       
   186 /**
       
   187 Second phase construction.
       
   188 */
       
   189 
       
   190 void CTRuleBasedAppView::ConstructL(const TRect& aRect)
       
   191  	{
       
   192 	// Fetch the text from the resource file.
       
   193 	iRuleBasedText = iEikonEnv->AllocReadResourceL(R_RULEBASED_TEXT);
       
   194 	// Control is a window owning control
       
   195 
       
   196 	CreateWindowL();
       
   197 	// Extent of the control. This is
       
   198 	// the whole rectangle available to Application.
       
   199 	// The rectangle is passed to us from the Application UI.
       
   200 	SetRect(aRect);
       
   201 	// At this stage, the control is ready to draw so
       
   202 	// we tell the UI framework by activating it.
       
   203 	ActivateL();
       
   204 	}
       
   205 
       
   206 /**
       
   207 Drawing the view - in this example, 
       
   208 consists of drawing a simple outline rectangle
       
   209 and then drawing the text in the middle.
       
   210 
       
   211 In this example, we don't use the redraw
       
   212 region because it's easier to redraw to
       
   213 the whole client area.
       
   214 */
       
   215 
       
   216 const CFont* CTRuleBasedAppView::CreateCustomFontL( const TDesC& aFontName, TInt aHeightInPixels, TBool aBold ) const
       
   217 	 {
       
   218 	 // Create a Symbian font spec
       
   219 	 TFontSpec fontSpec;
       
   220 
       
   221 	 // Enable antialising
       
   222 	 TOpenFontSpec openSpec;
       
   223 
       
   224 	 openSpec.SetBitmapType(EAntiAliasedGlyphBitmap);
       
   225 
       
   226 	 openSpec.GetTFontSpec(fontSpec); // sets the antialiasing into the TFontSpec
       
   227 	 fontSpec.iTypeface.iName = aFontName;
       
   228 	 fontSpec.iHeight = aHeightInPixels;
       
   229 
       
   230 	 if (aBold)
       
   231 	 	{
       
   232 	 	fontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold );
       
   233 		}
       
   234 	 CFbsFont* font;
       
   235 	 CWsScreenDevice& screenDev=*(CEikonEnv::Static()->ScreenDevice());
       
   236 	 User::LeaveIfError(screenDev.GetNearestFontInPixels((CFont*&)font,fontSpec));
       
   237 	 return font;
       
   238 	 }
       
   239     
       
   240 void CTRuleBasedAppView::Draw(const TRect& aRect) const
       
   241 	{
       
   242 	// Window graphics context
       
   243 	CWindowGc& gc = SystemGc();
       
   244 	// Area in which we shall draw
       
   245 	TRect	drawRect = Rect();
       
   246 	
       
   247 	// Font used for drawing text
       
   248 	const CFont* fontUsed = NULL;
       
   249 	
       
   250 	// Start with a clear screen
       
   251 	gc.Clear();
       
   252 			
       
   253 	gc.DrawRect(drawRect);
       
   254 
       
   255 	TRAPD(err, fontUsed = CreateCustomFontL(_L("Times New Roman"),20,1));
       
   256 	if (err!=KErrNone)
       
   257 		{
       
   258 		return;
       
   259 		}
       
   260 		
       
   261 	gc.SetPenColor(KRgbYellow);
       
   262 	gc.UseFont(fontUsed);
       
   263 	// Draw the text in the middle of the rectangle.
       
   264 	TInt baselineOffset=(aRect.Height() - fontUsed->HeightInPixels())/2;
       
   265 	gc.DrawText(*iRuleBasedText,aRect,baselineOffset,CGraphicsContext::ECenter, 0);
       
   266 	// Finished using the font
       
   267 	gc.DiscardFont();
       
   268 	}
       
   269 
       
   270 //
       
   271 //
       
   272 // Application UI class - CTRuleBasedAppUi
       
   273 //
       
   274 //
       
   275 
       
   276 /**
       
   277 The second phase constructor of the Application UI class.
       
   278 The Application UI creates and owns the one and only view.
       
   279 */
       
   280 
       
   281 void CTRuleBasedAppUi::ConstructL()
       
   282 	{
       
   283 	// BaseConstructL() completes the UI framework's
       
   284 	// construction of the App UI.
       
   285 	BaseConstructL();
       
   286 	// Create the single Application view in which to
       
   287 	// draw the text to display the name of the Application that has been launched, passing into it
       
   288 	// the rectangle available to it.
       
   289 	
       
   290  	TRect cliRect = ClientRect();
       
   291  	const TInt width = 580/3;
       
   292  	
       
   293 	cliRect.SetWidth(width);
       
   294 	
       
   295 	iAppView = CTRuleBasedAppView::NewL(cliRect);
       
   296 
       
   297 	// The application window occupies 1/3 of the screen.
       
   298 	if (iAppView->iAppType == KCentre)
       
   299 		{
       
   300 		TPoint Origin( width,0);
       
   301 		iAppView->SetPosition(Origin);
       
   302 		}
       
   303 	else if (iAppView->iAppType == KRight)
       
   304 		{
       
   305 		TPoint Origin( (2*width),0);
       
   306 		iAppView->SetPosition(Origin);
       
   307 		}
       
   308 	}
       
   309 
       
   310 /**
       
   311 The App Ui owns the two views and is.
       
   312 therefore, responsible for destroying them
       
   313 */
       
   314 
       
   315 CTRuleBasedAppUi::~CTRuleBasedAppUi()
       
   316 	{
       
   317 	delete iAppView;
       
   318 	}
       
   319 
       
   320 //
       
   321 //
       
   322 // Implementation of the
       
   323 // Application class - CTRuleBasedApplication
       
   324 //
       
   325 //
       
   326 
       
   327 const TUid KUidRuleBasedApp = { 0X10210F77 }; 
       
   328 
       
   329 /**
       
   330 The function is called by the UI framework to ask for the
       
   331 Application's UID. The returned value is defined by the
       
   332 constant KUidRuleBasedAppe and must match the second value
       
   333 defined in the project definition file.
       
   334 */
       
   335 
       
   336 TUid CTRuleBasedApplication::AppDllUid() const
       
   337 	{
       
   338 	return KUidRuleBasedApp;
       
   339 	}
       
   340 
       
   341 /**
       
   342 This function is called by the UI framework at
       
   343 Application start-up. It creates an instance of the
       
   344 document class.
       
   345 */
       
   346 
       
   347 CApaDocument* CTRuleBasedApplication::CreateDocumentL()
       
   348 	{
       
   349 	return new (ELeave) CTRuleBasedDocument(*this);
       
   350 	}
       
   351 
       
   352 void CTRuleBasedAppUi::HandleCommandL(TInt aCommand)
       
   353 	{
       
   354 	switch (aCommand)
       
   355 		{
       
   356 	case EEikCmdExit:
       
   357 		Exit();
       
   358 		break;
       
   359 		}
       
   360 	}
       
   361 
       
   362 /**
       
   363 The entry point for the Application code. It creates
       
   364 an instance of the CApaApplication derived
       
   365 class, CTRuleBasedApplication.
       
   366 */
       
   367 
       
   368 LOCAL_C CApaApplication* NewApplication()
       
   369 	{
       
   370 	return new CTRuleBasedApplication;
       
   371 	}
       
   372 
       
   373 GLDEF_C TInt E32Main()
       
   374 	{
       
   375 	return EikStart::RunApplication( NewApplication );
       
   376 	}
       
   377 
       
   378 
       
   379 #if defined( __WINS__ ) && !defined(EKA2)
       
   380 GLDEF_C TInt E32Dll(TDllReason)
       
   381 	{
       
   382 	return KErrNone;
       
   383 	}
       
   384 	
       
   385 EXPORT_C TInt WinsMain( TDesC* aCommandLine )
       
   386 	{
       
   387 	return EikStart::RunApplication( NewApplication, aCommandLine );
       
   388 	}
       
   389 #endif
       
   390