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