// COEMAIN.H
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#if !defined(__COEMAIN_H__)
#define __COEMAIN_H__

#if !defined(__E32BASE_H__)
#include <e32base.h>
#endif

#if !defined(__W32STD_H__)
#include <w32std.h>
#endif

#if !defined(__BARSC_H__)
#include <barsc.h>
#endif

#if !defined(__BADESCA_H__)
#include <badesca.h>
#endif

class CCoeAppUiBase;
class CCoeEnv;
class TResourceReader;
class RGenericPointerArray;

enum TActivePriority
	{ // an alternative set to the TPriority in E32BASE.H
	EActivePriorityClockTimer=300,
	EActivePriorityIpcEventsHigh=200,
	EActivePriorityFepLoader=150,
	EActivePriorityWsEvents=100,
	EActivePriorityRedrawEvents=50,
	EActivePriorityDefault=0,
	EActivePriorityLogonA=-10
	};

class CCoeEnvExtra;
class MCoeObserverOfFocusedFepAwareItem
	{
public:
	virtual void HandleChangeInFocusedFepAwareItem()=0;
private:
	IMPORT_C virtual void MCoeObserverOfFocusedFepAwareItem_Reserved_1();
	IMPORT_C virtual void MCoeObserverOfFocusedFepAwareItem_Reserved_2();
	};

class MCoeObserverOfLoadedFep
	{
public:
	virtual void HandleChangeInLoadedFep()=0;
private:
	IMPORT_C virtual void MCoeObserverOfLoadedFep_Reserved_1();
	IMPORT_C virtual void MCoeObserverOfLoadedFep_Reserved_2();
	};

class MCoeFocusObserver
	{
public:
	virtual void HandleChangeInFocus()=0;
private:
	IMPORT_C virtual void MCoeFocusObserver_Reserved_1();
	IMPORT_C virtual void MCoeFocusObserver_Reserved_2();
	};

class MCoeForegroundObserver
	{
public:
	virtual void HandleGainingForeground()=0;
	virtual void HandleLosingForeground()=0;
private:
	IMPORT_C virtual void MCoeForegroundObserver_Reserved_1();
	IMPORT_C virtual void MCoeForegroundObserver_Reserved_2();
	};

// classes defined in FEPBASE, declared here
class MCoeFepObserver;
typedef void (*TCoeFepObserverFunction)(MCoeFepObserver& aFepObserver);
class CCoeFepParameters; // must derive from CBase
class CCoeFep; // must derive from CBase
class MCoeFepAwareItem;
typedef TBool (*TCoeFepAwareItemIsFocusedFunction)(const MCoeFepAwareItem& aFepAwareItem);

class CCoeEnv : public CActive
	{
public:
	IMPORT_C ~CCoeEnv();
	IMPORT_C CCoeEnv();
	IMPORT_C virtual void DestroyEnvironment();
	IMPORT_C CCoeAppUiBase* SetAppUi(CCoeAppUiBase* aAppUi);
	IMPORT_C void ExecuteD();
	IMPORT_C void RunL();
	IMPORT_C void DoCancel();
	IMPORT_C virtual void HandleError(TInt aError);
	IMPORT_C void ConstructL();
	inline const TWsEvent& LastEvent() const;
	inline CCoeAppUiBase* AppUi() const;
	inline RFs& FsSession() const;
	inline RWsSession& WsSession() const;
	inline RWindowGroup& RootWin() const;
	inline CWindowGc& SystemGc() const;
	inline const CFont* NormalFont() const;
	inline CWsScreenDevice* ScreenDevice() const;
	IMPORT_C CWindowGc* CreateGcL();
	IMPORT_C void Flush(TTimeIntervalMicroSeconds32 aDelay=0);
	IMPORT_C CFbsFont* CreateDeviceFontL(CGraphicsDevice* aDevice,const TFontSpec& aFontSpec);
	IMPORT_C CFbsFont* CreateScreenFontL(const TFontSpec& aFontSpec);
	IMPORT_C void ReleaseScreenFont(CFont* aFont) const;
	IMPORT_C void CreateResourceReaderLC(TResourceReader& aReader,TInt aResourceId) const;
	inline void ReadResource(TDes& aDes,TInt aResourceId) const; 
	inline  HBufC* AllocReadResourceL(TInt aResourceId) const;
	inline HBufC* AllocReadResourceLC(TInt aResourceId) const;
	IMPORT_C TInt AddResourceFileL(const TDesC& aFileName);
	IMPORT_C void DeleteResourceFile(TInt aOffset);
	inline CDesCArrayFlat* ReadDesCArrayResourceL(TInt aResourceId);
	IMPORT_C void LeaveWithErrorText(const TDesC& aMsg,const TDesC* aContextText=NULL);
	IMPORT_C void Format128(TDes& aDes,TInt aResourceId,...);
	IMPORT_C void Format256(TDes& aDes,TInt aResourceId,...);
	IMPORT_C static TVersion Version();
	IMPORT_C static CCoeEnv* Static();
	IMPORT_C void SimulateKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType);
	IMPORT_C TBool IsWservEventPending() const;
	IMPORT_C TBool IsRedrawEventPending() const;
	IMPORT_C void PrepareToExit();
	IMPORT_C void BringOwnerToFront();
	IMPORT_C void ReadResourceAsDes8(TDes8& aDes,TInt aResourceId) const;
	IMPORT_C HBufC8* AllocReadResourceAsDes8L(TInt aResourceId) const;
	IMPORT_C HBufC8* AllocReadResourceAsDes8LC(TInt aResourceId) const;
	IMPORT_C CDesC8ArrayFlat* ReadDesC8ArrayResourceL(TInt aResourceId);
	IMPORT_C void SuppressNextFlush();
	IMPORT_C void ReadResourceAsDes16(TDes16& aDes,TInt aResourceId) const; // not available before Release 005
	IMPORT_C HBufC16* AllocReadResourceAsDes16L(TInt aResourceId) const; // not available before Release 005
	IMPORT_C HBufC16* AllocReadResourceAsDes16LC(TInt aResourceId) const; // not available before Release 005
	IMPORT_C CDesC16ArrayFlat* ReadDesC16ArrayResourceL(TInt aResourceId); // not available before Release 005
	IMPORT_C void AddObserverOfFocusedFepAwareItemL(MCoeObserverOfFocusedFepAwareItem& aObserverOfFocusedFepAwareItem); // not available before Release 005
	IMPORT_C void RemoveObserverOfFocusedFepAwareItem(MCoeObserverOfFocusedFepAwareItem& aObserverOfFocusedFepAwareItem); // not available before Release 005
	IMPORT_C void AddObserverOfLoadedFepL(MCoeObserverOfLoadedFep& aObserverOfLoadedFep); // not available before Release 005
	IMPORT_C void RemoveObserverOfLoadedFep(MCoeObserverOfLoadedFep& aObserverOfLoadedFep); // not available before Release 005
	IMPORT_C void AddFocusObserverL(MCoeFocusObserver& aFocusObserver); // not available before Release 005
	IMPORT_C void RemoveFocusObserver(MCoeFocusObserver& aFocusObserver); // not available before Release 005
	void NotifyFocusObserversOfChangeInFocus(); // not to be called from outside CONE
	IMPORT_C void AddForegroundObserverL(MCoeForegroundObserver& aForegroundObserver); // not available before Release 005
	IMPORT_C void RemoveForegroundObserver(MCoeForegroundObserver& aForegroundObserver); // not available before Release 005
	void NotifyForegroundObserversOfGainingForeground(); // not to be called from outside CONE
	void NotifyForegroundObserversOfLosingForeground(); // not to be called from outside CONE
	IMPORT_C void AddFepObserverL(MCoeFepObserver& aFepObserver); // not available before Release 005
	IMPORT_C void RemoveFepObserver(MCoeFepObserver& aFepObserver); // not available before Release 005
	IMPORT_C void ForEachFepObserverCall(TCoeFepObserverFunction aFepObserverFunction); // not available before Release 005
	IMPORT_C CCoeFepParameters* FepParameters(); // not available before Release 005
	void LoadFepL(); // not to be called from outside CONE
	IMPORT_C void InstallFepL(const TDesC& aFileNameOfFepDll); // not available before Release 005
	IMPORT_C HBufC* NameOfInstalledFepL(); // not available before Release 005
	IMPORT_C MDesCArray* FileNamesOfAvailableFepsL(); // not available before Release 005
	IMPORT_C void ExecuteFepSettingsDialogL(const TDesC& aFileNameOfFepDll); // not available before Release 005
	IMPORT_C CCoeFep* Fep() const; // not available before Release 005
	IMPORT_C void SetFocusedFepAwareItem(MCoeFepAwareItem* aFocusedFepAwareItem, TCoeFepAwareItemIsFocusedFunction aFepAwareItemIsFocusedFunction); // not available before Release 005
	IMPORT_C void FepAwareItemIsNotFocusedOrIsBeingDestroyed(MCoeFepAwareItem* aFepAwareItem); // not available before Release 005
	void NoFocusedFepAwareItem(); // not to be called from outside CONE
	IMPORT_C MCoeFepAwareItem* FocusedFepAwareItem() const; // not available before Release 005
	TCoeFepAwareItemIsFocusedFunction FepAwareItemIsFocusedFunction() const; // not to be called from outside CONE
	void IncrementNumberOfNestedSetFocusCalls(); // not to be called from outside CONE
	void DecrementNumberOfNestedSetFocusCalls(); // not to be called from outside CONE
	TInt NumberOfNestedSetFocusCalls() const; // not to be called from outside CONE
	IMPORT_C CWindowGc* SwapSystemGc(CWindowGc* aGc); // not available before Release 005
// reserved virtual function space
private:
	IMPORT_C virtual void Reserved_1();
	IMPORT_C virtual void Reserved_2();
public: // but not exported
	void ReadEvent();
protected:
	IMPORT_C virtual void DestroyScreen();
	IMPORT_C void SetUpFepL(CCoeFepParameters* aFepParameters, const TDesC& aFileNameOfDefaultFepDll); // not available before Release 005
	inline TDes& ErrorText();
	inline TDes& ErrorContextText();
private:
	IMPORT_C virtual void InitSystemFontsL();
	IMPORT_C virtual TInt ResourceFileVersionNumber() const;
	void CreateActiveSchedulerL();
	void ConnectToFileServerL();
	void ConnectToWindowServerL();
	void InitScreenL();
	void InitRootWindowL();
	void InitSystemGcL();
	RResourceFile& ResourceFileForId(TInt aResourceId) const;
	void DestroyAllResourceFiles();
	void AddObserverL(TAny* aObserver, RGenericPointerArray& aArray);
	void RemoveObserver(TAny* aObserver, RGenericPointerArray& aArray);
	void FocusedFepAwareItemHasChanged();
protected:
	CCoeAppUiBase* iAppUi;
	RFs iFsSession;
	RWsSession iWsSession;
	RWindowGroup iRootWin;
	CWindowGc* iSystemGc;
	const CFont* iNormalFont;
	CWsScreenDevice* iScreen;
	TWsEvent iLastEvent;
	CArrayFix<RResourceFile>* iResourceFileArray;
private:
	TDes* iErrorText;
	TDes* iErrorContextText;
	CCoeEnvExtra* iExtra;
	CTrapCleanup* iCleanup;
	};

inline const TWsEvent& CCoeEnv::LastEvent() const
	{ return(iLastEvent); }
inline CCoeAppUiBase* CCoeEnv::AppUi() const
	{ return(iAppUi); }
inline RFs& CCoeEnv::FsSession() const
	{ return((RFs&)iFsSession); }
inline RWsSession& CCoeEnv::WsSession() const
	{ return((RWsSession&)iWsSession); }
inline RWindowGroup& CCoeEnv::RootWin() const
	{ return((RWindowGroup&)iRootWin); }
inline CWindowGc& CCoeEnv::SystemGc() const
	{ return((CWindowGc&)*iSystemGc); }
inline const CFont* CCoeEnv::NormalFont() const
	{ return(iNormalFont); }
inline CWsScreenDevice* CCoeEnv::ScreenDevice() const
	{ return(iScreen); }
inline TDes& CCoeEnv::ErrorText() 
	{ return *iErrorText; }
inline TDes& CCoeEnv::ErrorContextText() 
	{ return *iErrorContextText; }

#if defined(_UNICODE)
inline void CCoeEnv::ReadResource(TDes& aDes,TInt aResourceId) const
	{ ReadResourceAsDes16(aDes,aResourceId); }
inline  HBufC* CCoeEnv::AllocReadResourceL(TInt aResourceId) const
	{ return AllocReadResourceAsDes16L(aResourceId); }
inline HBufC* CCoeEnv::AllocReadResourceLC(TInt aResourceId) const
	{ return AllocReadResourceAsDes16LC(aResourceId); }
inline CDesCArrayFlat* CCoeEnv::ReadDesCArrayResourceL(TInt aResourceId)
	{ return ReadDesC16ArrayResourceL(aResourceId); }
#else  // not UNICODE
inline void CCoeEnv::ReadResource(TDes& aDes,TInt aResourceId) const
	{ ReadResourceAsDes8(aDes,aResourceId); }
inline  HBufC* CCoeEnv::AllocReadResourceL(TInt aResourceId) const
	{ return AllocReadResourceAsDes8L(aResourceId); }
inline HBufC* CCoeEnv::AllocReadResourceLC(TInt aResourceId) const
	{ return AllocReadResourceAsDes8LC(aResourceId); }
inline CDesCArrayFlat* CCoeEnv::ReadDesCArrayResourceL(TInt aResourceId)
	{ return ReadDesC8ArrayResourceL(aResourceId); }
#endif // UNICODE

#endif
