// ResHandler.X.h
//
// Copyright 1998 Diehl Graphsoft, Inc.
// All Rights Reserved.
//
// Provides a cross-platform implementation for manipulating Macintosh resources.
//  On the Mac, this mostly merely calls the Mac Toolbox Resource Manager routines.
//  On Windows, it currently calls the Resource Manager routines in Altura.
//  Ultimately, it can be switched to use the routines in QuickTime for Windows.
//
// Started by Karim Shehadeh on 9/14/98.
//
// SourceSafe information for this version:
//    $Revision: 5 $
//    $Date: 10/24/00 9:39 AM $
//    $Author: Paul $

#ifndef _RESHANDLER_
#define _RESHANDLER_ 1
#pragma once

#include "UndefineMacResourcesAPI.h"

typedef CALLBACK_API( OSErr , ResourceEndianFilterPtr_GS )(Handle theResource, Boolean currentlyNativeEndian);

#include "GSString.X.h"
#include "GSString.h"
#include "MCMemory.h"


class TFileIdentifier;
struct FSSpec;


#if !_RESHANDLER_CPP_&& !GS_CORE_BUILD && 0  // NEVER ENABLE THESE ON THE MAC!  MFH 04/01

	/////////////////////////////////////////////////////////////////////////////////////////
	// From Dialogs.h

	enum  {
		ctrlItem		= 4,
		btnCtrl			= 0,
		chkCtrl			= 1,
		radCtrl			= 2,
		resCtrl			= 3,
		statText		= 8,
		editText		= 16,
		iconItem		= 32,
		picItem			= 64,
		userItem		= 0,
		itemDisable		= 128,
		ok				= 1,
		cancel			= 2,
		stopIcon		= 0,
		noteIcon		= 1,
		cautionIcon		= 2
	};


	/* new, more standard names for dialog item constants */
	enum {
		kControlDialogItem			= ctrlItem,
		kButtonDialogItem			= ctrlItem + btnCtrl,
		kCheckBoxDialogItem			= ctrlItem + chkCtrl,
		kRadioButtonDialogItem		= ctrlItem + radCtrl,
		kResourceControlDialogItem	= ctrlItem + resCtrl,
		kStaticTextDialogItem		= statText,
		kEditTextDialogItem			= editText,
		kIconDialogItem				= iconItem,
		kPictureDialogItem			= picItem,
		kUserDialogItem				= userItem,
		kItemDisableBit				= itemDisable,
		kStdOkItemIndex				= ok,
		kStdCancelItemIndex			= cancel,
		kStopIcon					= stopIcon,
		kNoteIcon					= noteIcon,
		kCautionIcon				= cautionIcon
	};

#endif // !_RESHANDLER_CPP_


namespace RsrcMgr {

	// Types
	// These are compatible with their Apple counterparts.
	#define GS_DECLARE_32BIT_OBJECT(theType) \
		typedef unsigned long theType
	#define GS_DECLARE_32BIT_HANDLE(theType) \
		typedef char** theType
	
	GS_DECLARE_32BIT_OBJECT(GSResType);
	GS_DECLARE_32BIT_HANDLE(GSResHandle);
	

	// File permissions. See comments in cpp file.
	enum {
		kReadPermission				= 0,	// fsCurPerm	= 0x00,   
		kWritePermission			= 3		// fsRdWrPerm	= 0x03
	};
	
	
	/////////////////////////////////////////////////////////////////////////////////////////
	// Mac Resource Routine Clones


	// Generic Resource Allocation/DeAllocation and Retrieval Functions.

	GSResHandle		MACAPI	GSGetResource (GSResType theType, short theID);
	Uint32			MACAPI	GSGetResourceHandleSize (GSResHandle theResource);
	void			MACAPI	GSReleaseResource (GSResHandle theResource);
	short			MACAPI	GSCountResources (GSResType theType);
	GSResHandle		MACAPI	GSGetNamedResource (GSResType theType, ConstGSStringPtr name);
	void			MACAPI	GSGetResInfo (GSResHandle theResource, short *theID, GSResType *theType,
							               GSStr255 name);
	void			MACAPI	GSDetachResource (GSResHandle theResource, MemHandle& theMemory);
	OSErr			MACAPI	GSResError (void);
	ResFileRefNum	MACAPI	GSCurResFile (void);
	void			MACAPI	GSUseResFile (ResFileRefNum refNum);
	short			MACAPI	GSCount1Resources (GSResType theType);
	GSResHandle		MACAPI	GSGet1Resource (GSResType theType, short theID);
	void			MACAPI	GSCloseResFile (ResFileRefNum refNum);
	short			MACAPI	GSHOpenResFile (const TFileIdentifier& inFile, char permission);
	void			MACAPI	GSChangedResource (GSResHandle theResource);
	Uint32			MACAPI	GSGetResHandleSize (GSResHandle theResource);
	
	void			MACAPI	GSWriteResource (GSResHandle theResource);
	void			MACAPI  GSRemoveResource (GSResHandle theResource);
	void			MACAPI  GSAddResource (GSResHandle theData, GSResType theType, short theID,
							                ConstStr255Param name);
	short			MACAPI	GSOpenRFPerm (ConstStr255Param fileName, short vRefNum, SInt8 permission);
	GSResHandle		MACAPI	GSGet1IndResource (GSResType theType, short index);
	GSResHandle		MACAPI	GSGet1NamedResource (GSResType theType, ConstStr255Param name);
	ResFileRefNum	MACAPI	GSHomeResFile (GSResHandle theResource);
	short			MACAPI	GSCount1Resources (GSResType theType);
	void			MACAPI	GSUpdateResFile (ResFileRefNum refNum);
	short			MACAPI	GSGetResFileAttrs (ResFileRefNum refNum);
	void			MACAPI	GSSetResLoad (Boolean load);
	GSResHandle		MACAPI	GSGetIndResource (GSResType theType, short index);
	void			MACAPI	GSSetResInfo (Handle theResource, short theID, ConstStr255Param name);

	void			MACAPI	GSFSpCreateResFile (const TFileIdentifier& fileID, OSType creator, OSType fileType,
							                     ScriptCode scriptTag);
	ResFileRefNum	MACAPI	GSFSpOpenResFile (const TFileIdentifier& fileID, SignedByte permission);


	// Text Utility Functions
#if GS_CORE_BUILD
	PStr255			MACAPI	GSGetRsrcText (short rsrcID, short Index, EEmptyHandling allowEmpty = eDontAllowEmptyResult);
#else
//	TXString		MACAPI	GetRsrcText(short rsrcID, short index);
	PStr255			MACAPI	GSGetRsrcText (short rsrcID, short Index, EEmptyHandling allowEmpty = eDontAllowEmptyResult);
#endif

	void			MACAPI	GSGetIndString (GSStr255 theString, short strListID, short index, EEmptyHandling allowEmpty = eDontAllowEmptyResult);
	void			GS_COMP_API	GSGetString (TXString &str, short strListID, short index, EEmptyHandling allowEmpty = eDontAllowEmptyResult);
	GSStringHandle	GS_COMP_API	GSGetString (short stringID, EEmptyHandling allowEmpty = eDontAllowEmptyResult);


	/////////////////////////////////////////////////////////////////////////////////////////
	// Our Unique Resource Routines
	
	ResFileRefNum	MACAPI	GSOpenResFileFID(const TFileIdentifier &file, char permission);
	ResFileRefNum	MACAPI	GSOpenResFile(ConstGSCStrPtr szFullFilePath, char permission);
	
	void			MACAPI	GSResLock(GSResHandle theResource);
	void			MACAPI	GSResUnlock(GSResHandle theResource);
	
	typedef Boolean (*ForEachResourceProcPtr) (GSResHandle theResource, void* env);
	
	Boolean					ForEachResource (GSResType kind, short startIndex,
							                  ForEachResourceProcPtr proc, void* env);

#ifdef __AFX_H__
	void					LoadCString(CString &cstr, short id, short index, EEmptyHandling allowEmpty = eDontAllowEmptyResult);
#endif

	/////////////////////////////////////////////////////////////////////////////////////////
	// Resource byte swapping.
	
	// Registering byteswappers with QuickTime
	OSErr			MACAPI	RegisterAllEndianFilters();	
	OSErr			MACAPI	RegisterCoreEndianFilters();/*XXX_JDW_ALTURA API?*/		
	OSErr			MACAPI	RegisterAndCheck(const OSType inType, ResourceEndianFilterPtr_GS inFilterProc);

#ifdef __AFX_H__
	class RsrcCString : public CString {
		public: RsrcCString(short id, short index, EEmptyHandling allowEmpty = eDontAllowEmptyResult);
	};
#endif
} // namespace RsrcMgr

#endif // _RESHANDLER_
