// TXTRICH.H
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#if !defined(__TXTRICH_H__)
#define __TXTRICH_H__

#if !defined(__E32STD_H__)
#include <e32std.h>
#endif
#if !defined(__E32BASE_H__)
#include <e32base.h>
#endif

#if !defined(__TXTGLOBL_H__)
#include <txtglobl.h>
#endif
#if !defined(__TXTSTYLE_H__)
#include <txtstyle.h>
#endif

//
// Classes defined in this file
class CRichText;
//
// Forward references
class CRichTextIndex;
class CStreamStore;
class CStoreMap;
class MPictureFactory;
class MRichTextStoreResolver;

const TUid KRichTextStyleDataUid8={268435557};
const TUid KRichTextMarkupDataUid={268435558};
const TUid KClipboardUidTypeRichText8={268435508};
const TUid KClipboardUidTypeRichTextWithStyles8={268435509};

const TUid KRichTextStyleDataUid16={268450335};
const TUid KClipboardUidTypeRichText16={268450336};
const TUid KClipboardUidTypeRichTextWithStyles16={268450337};

#if defined(_UNICODE)
	#define KRichTextStyleDataUid KRichTextStyleDataUid16
	#define KClipboardUidTypeRichText KClipboardUidTypeRichText16
	#define KClipboardUidTypeRichTextWithStyles KClipboardUidTypeRichTextWithStyles16
#else
	#define KRichTextStyleDataUid KRichTextStyleDataUid8
	#define KClipboardUidTypeRichText KClipboardUidTypeRichText8
	#define KClipboardUidTypeRichTextWithStyles KClipboardUidTypeRichTextWithStyles8
#endif
//
// Defines the rich text component, where:
// each paragraph can in general have specific paragraph format, and
// each character can in general have specific character format.

class CRichText : public CGlobalText
	{
public:
	enum TParaType {ESinglePara,EMultiPara};
public:
	// Create new rich text component whose sole content is an end-of-document character.	
	IMPORT_C static CRichText* NewL(const CParaFormatLayer* aGlobalParaLayer,const CCharFormatLayer* aGlobalCharLayer,
									TDocumentStorage aStorage=ESegmentedStorage,
									TInt aDefaultTextGranularity=EDefaultTextGranularity,
									TParaType aParaType=EMultiPara);
	// Create new rich text component, supporting STYLES, whose sole content is an end-of-document character.
	IMPORT_C static CRichText* NewL(const CParaFormatLayer* aGlobalParaLayer,const CCharFormatLayer* aGlobalCharLayer,
									const CStyleList& aStyleList,
									TDocumentStorage aStorage=ESegmentedStorage,
									TInt aDefaultTextGranularity=EDefaultTextGranularity,
									TParaType aParaType=EMultiPara);
	// Restore into new rich text, using the specified global layers.
	IMPORT_C static CRichText* NewL(const CStreamStore& aStore,TStreamId aStreamId,
									const CParaFormatLayer* aGlobalParaLayer,const CCharFormatLayer* aGlobalCharLayer,
									MTextFieldFactory* aFieldFactory=NULL,
									TDocumentStorage aStorage=ESegmentedStorage);
	// Restore into new rich text, using the specified layers, store & picture factory.
	IMPORT_C static CRichText* NewL(const CStreamStore& aStore,TStreamId aStreamId,
									const CParaFormatLayer* aGlobalParaLayer,const CCharFormatLayer * aGlobalCharLayer,
									MPictureFactory* aPictureFactory,MRichTextStoreResolver* aStoreResolver,
									MTextFieldFactory* aFieldFactory=NULL,
									TDocumentStorage aStorage=ESegmentedStorage);
	IMPORT_C virtual ~CRichText();
	//
	// Default persist functions - Deferred loading of pictures
	IMPORT_C void RestoreWithStyleListL(const CStreamStore& aStore,TStreamId aStreamId,const CStyleList& aExternalStyleList);
	//
	IMPORT_C virtual void StoreComponentsL(CStreamStore& aStore,CStoreMap& aMap)const;
	IMPORT_C virtual void RestoreComponentsL(const CStreamStore& aStore);
	IMPORT_C virtual void ExternalizeL(RWriteStream& aStream)const;
	IMPORT_C virtual void InternalizeL(RReadStream& aStream);
	//
	// Custom persist functions
	IMPORT_C void ExternalizeStyleDataL(RWriteStream& aStream)const;
	IMPORT_C void InternalizeStyleDataL(RReadStream& aStream);
	//
	IMPORT_C void StoreMarkupComponentsL(CStreamStore& aStore,CStoreMap& aMap)const;
	IMPORT_C void ExternalizeMarkupDataL(RWriteStream& aStream)const;
	IMPORT_C void InternalizeMarkupDataL(RReadStream& aStream);
	//
	// Utility persist functions
	IMPORT_C void SetPictureFactory(MPictureFactory* aPictureFactory,MRichTextStoreResolver* aStoreResolver);
	inline MPictureFactory* PictureFactory()const;
	inline MRichTextStoreResolver* StoreResolver()const;
	IMPORT_C TPictureHeader PictureHeader(TInt aPos)const;
	IMPORT_C void DetachFromStoreL(CPicture::TDetach aDegree);
	IMPORT_C void DetachFromStoreL(CPicture::TDetach aDegree,TInt aPos,TInt aLength);
	IMPORT_C TBool HasMarkupData()const;
	//
	// Copy/Paste
	IMPORT_C virtual void CopyToStoreL(CStreamStore& aStore,CStreamDictionary& aDictionary,TInt aPos,TInt aLength)const;
	IMPORT_C virtual TInt PasteFromStoreL(const CStreamStore& aStore,const CStreamDictionary& aDictionary,TInt aPos);
	IMPORT_C TInt PasteFromStoreL(const CStreamStore& aStore,const CStreamDictionary& aDictionary,TInt aPos,CParagraphStyle::TStylePasteMode aStylePasteMode);
	//
	// Content modifier functions
	IMPORT_C virtual void Reset();
	IMPORT_C virtual void InsertL(TInt aPos,const TChar& aChar);  // Insert a single character.
	IMPORT_C virtual void InsertL(TInt aPos,const TDesC& aBuf);
	IMPORT_C void InsertL(TInt aPos,const TPictureHeader& aHeader);  // Insert a picture header.
	//
	// aPos is the position of the character being deleted, *not* the current cursor position!
	IMPORT_C virtual TBool DeleteL(TInt aPos,TInt aLength);  // Call this for a delete-forward.
	//
	// Leave-safe delete for removing content from *within* a single paragraph only
	IMPORT_C void DeleteFromParagraph(TInt aPos,TInt aLength);
	//
	// Leave-safe delete for removing *entire* paragraphs
	IMPORT_C void DeleteParagraph(TInt aPos,TInt aLength);
	//
	// Special behaviour format modifier functions.
	// Call this when applying a character format to a zero length selection, eg, turning bold on.
	IMPORT_C void SetInsertCharFormatL(const TCharFormat& aFormat,const TCharFormatMask& aMask,TInt aPos);
	//
	// Call this on every cursor movement, page up/down etc...
	IMPORT_C void CancelInsertCharFormat();
	//
	// Delete content, but preserve phrase format if aPos is at a phrase boundary.
	// aPos is the position of the character being deleted, *not* the current cursor position!
	IMPORT_C TBool DelSetInsertCharFormatL(TInt aPos,TInt aLength);
	//
	// MLaydoc implementation
	IMPORT_C virtual void GetParagraphFormatL(CParaFormat* aFormat,TInt aPos)const;
	IMPORT_C virtual void GetChars(TPtrC& aView,TCharFormat& aFormat,TInt aPos)const;
	IMPORT_C virtual TInt GetPictureSizeInTwips(TSize& aSize, TInt aPos)const;
	IMPORT_C virtual CPicture* PictureHandleL(TInt aPos,MLayDoc::TForcePictureLoad aForceLoad=MLayDoc::EForceLoadTrue)const;
	//
	// MFormatText implementation
	// Used only by dialog writers, since they are not interested in the text, but require knowledge
	// of attributes whose values change across the specified range.
	IMPORT_C virtual void GetParaFormatL(CParaFormat* aFormat,TParaFormatMask& aVaries,TInt aPos,TInt aLength,
										CParaFormat::TParaFormatGetMode aMode=CParaFormat::EAllAttributes)const;
	IMPORT_C virtual void ApplyParaFormatL(const CParaFormat* aFormat,const TParaFormatMask& aMask,TInt aPos,TInt aLength);
	IMPORT_C virtual void GetCharFormat(TCharFormat& aFormat,TCharFormatMask& aVaries,TInt aPos,TInt aLength)const;
	IMPORT_C virtual void ApplyCharFormatL(const TCharFormat& aFormat,const TCharFormatMask& aMask,TInt aPos,TInt aLength);
	//
	//
	IMPORT_C void GetSpecificCharFormat(TCharFormat& aFormat,TCharFormatMask& aMask,TInt aPos)const;
	//
	// Paragraph style implementation
	IMPORT_C void ApplyParagraphStyleL(const CParagraphStyle& aStyle,TInt aPos,TInt aLength,CParagraphStyle::TApplyParaStyleMode aMode);
	inline void NotifyStyleDeletedL(const CParagraphStyle* aStyle);
	IMPORT_C void NotifyStyleChangedL(const CParagraphStyle* aTo,const CParagraphStyle* aFrom);
	IMPORT_C const CParaFormatLayer* ParagraphStyle(TBool& aStyleChangesOverRange,TInt aPos,TInt aLength)const;
	//
	// StyleList implementation
	inline TBool StyleListPresent()const;
	inline CStyleList* StyleList()const;
	inline TInt StyleCount()const;
	inline void SetStyleListExternallyOwned(TBool aExternallyOwned);
	IMPORT_C void SetStyleListExternallyOwned(const CStyleList& aStyleList);
	inline TBool StyleListExternallyOwned()const;
	//
	// Utility functions
	IMPORT_C void RemoveSpecificParaFormatL(TInt aPos,TInt aLength);  
	IMPORT_C void RemoveSpecificCharFormatL(TInt aPos,TInt aLength);
	IMPORT_C TInt PictureCount()const;
	IMPORT_C virtual TInt ParagraphCount()const;
	IMPORT_C virtual TInt CharPosOfParagraph(TInt& aLength,TInt aParaOffset)const;
	IMPORT_C virtual TInt ParagraphNumberForPos(TInt& aPos)const;
	IMPORT_C virtual TEtextComponentInfo ComponentInfo()const;
	//
	// Text field implementation
	IMPORT_C virtual void UpdateFieldL(TInt aPos);  // updates the field at aPos
	// finds number of fields in range. Includes the field containing aPos, if applicable
	// aInfo is set to the first field in the range (if any are found)
	//
	// Speciality functions
	IMPORT_C void AppendTakingSolePictureOwnershipL(const CRichText& aSource);
	IMPORT_C void AppendParagraphL(TInt aReplicas=1);
	IMPORT_C virtual void SetHasChanged(TBool aHasChanged);
private:
	CRichText(const CRichText& aRichText);
	CRichText& operator=(const CRichText& aRichText);
	void KillStyleList();
	void KillIndex();
	TBool CreateEmptyMarkupComponentL();
	void CreateAndGenerateMarkupComponentL();
	void GenerateMarkupL();
	void CompletePastePlainTextL(TInt aPos,TInt aCharacterCount);
	TInt DoPasteRtFromStoreL(const CStreamStore& aStore,const CStreamDictionary& aDictionary,TInt aPos,CParagraphStyle::TStylePasteMode aStylePasteMode);
	TBool IndexPresent()const;
	inline void SetParaTypeIsSingle(TBool aBool);
	inline TBool ParaTypeIsSingle()const;
protected:
	IMPORT_C CRichText(const CParaFormatLayer* aGlobalParaLayer,const CCharFormatLayer* aGlobalCharLayer,
						CStyleList* aStyleList=NULL);
	// New constructL's - to be merged post 057
	IMPORT_C void ConstructL(TDocumentStorage aStorage,TInt aDefaultTextGranularity,TParaType aParaType);
	IMPORT_C void ConstructL(const CStreamStore& aStore,TStreamId aStreamId,MPictureFactory* aFactory,MRichTextStoreResolver* aStoreResolver,
							MTextFieldFactory* aFieldFactory=NULL,
							TDocumentStorage aStorage=ESegmentedStorage);
	//
	void InternalizeL(RReadStream& aStream,const CStyleList* aExternalStyleList);
	IMPORT_C void DoExternalizeStyleDataL(RWriteStream& aStream)const;
	IMPORT_C void DoInternalizeStyleDataL(RReadStream& aStream);
	void DoInternalizeStyleDataL(RReadStream& aStream,const CStyleList* aExternalStyleList);
	IMPORT_C void DoExternalizeMarkupDataL(RWriteStream& aStream)const;
	IMPORT_C void DoInternalizeMarkupDataL(RReadStream& aStream);
	void StoreStylesL(CStreamStore& aStore,CStoreMap& aMap)const;
	void RestoreStylesL(const CStreamStore& aStore,TStreamId aId,const CParaFormatLayer* aParaFormatLayer,const CCharFormatLayer* aCharFormatLayer);
	void StoreMarkupL(CStreamStore& aStore,CStoreMap& aMap)const;
	//
	IMPORT_C void RtInsertL(TInt aPos,const TDesC& aBuf);  
	//
	TStreamId DoCopyToStoreL(CStreamStore& aStore,TInt aPos,TInt aLength,TStreamId aGlobalTextId, TBool aCopyStyles)const;
	IMPORT_C void CopyComponentsL(CStreamStore& aStore,CStoreMap& aMap,TInt aPos,TInt aLength,TStreamId aGlobalTextId)const;
	IMPORT_C void CopyToStreamL(RWriteStream& aStream,TInt aPos,TInt aLength,TStreamId aGlobalTextId)const;
	IMPORT_C void CopyToStreamL(RWriteStream& aStream,TInt aPos,TInt aLength)const;
	void CopyToStreamL(RWriteStream& aStream,TInt aPos,TInt aLength,TStreamId aGlobalTextId, TBool aCopyStyles)const;
	TInt PasteRichTextFromStoreL(const CStreamStore& aStore,const CStreamDictionary& aDictionary,TStreamId& anId,TInt aPos,CParagraphStyle::TStylePasteMode aStylePasteMode);
	void CompletePasteRichTextFromStoreL(const CStreamStore& aStore,TStreamId& aRichTextStreamId,TInt aPos,CParagraphStyle::TStylePasteMode aStylePasteMode);
	void RbPasteRichTextFromStore(TInt aPos,TInt aLength,TInt aRet);
	TInt PastePlainTextFromStoreL(const CStreamStore& aStore,TStreamId& anId,TInt aPos);
	//
	// Append
	void PrepareAppendMarkupL(const CRichText& aSource);
	void DoAppendTakingSolePictureOwnershipL(const CRichText& aSource);
protected:
	enum {EDelimiterCharacterLength=1};
private:
	TSwizzle<CStyleList> iStyleList;
	TSwizzle<CRichTextIndex> iIndex;
	TUint32 iFlags;
	MPictureFactory* iPictureFactory;
	MRichTextStoreResolver* iStoreResolver;

	__DECLARE_TEST;  // Index consistency check with document length.
	};


#include <txtrich.inl>


#endif



