
	SECTION	ReadParameters,CODE
	
ReadParameters:

	;Find this Task
		MOVE.L	4,A6
		SUB.L	A1,A1
		JSR	_LVOFindTask(A6)
		MOVE.L	D0,TaskPtr
		
	;Store the Current Directory Lock
		MOVE.L	TaskPtr,A0
		MOVE.L	pr_CurrentDir(A0),StartUpDirLock

	;Was program started from CLI ?
		TST.L	pr_CLI(A0)
		BNE	StartedFromCLI

	;Get the Message from Workbench
		LEA	pr_MsgPort(A0),A0
		JSR	_LVOWaitPort(A6)
		JSR	_LVOGetMsg(A6)
		MOVE.L	D0,Message

	;Get the default settings from the Tool Types.
		JSR	ReadToolTypes

	;Get the Workbench arguments
		MOVE.L	Message,A0
		MOVE.L	$24(A0),A0

	;Get pointer to Directory Lock of the Project File
		MOVE.L	8(A0),DirectoryLock
		BEQ	NoProjectFile
		
	;Get pointer to the Project File's Name
		MOVE.L	12(A0),MenuFileName
		
		BNE	NFP_Skip
		
	;No Project File, so use Default file name & Program DirLock
NoProjectFile:	MOVE.L	(A0),DirectoryLock
		MOVE.L	DefaultFilePtr,MenuFileName

	;Read the Project Files tool types now, unlike with CLI
	;this is done last, as from workbench the Project ToolTypes
	;overide the programs ToolTypes.
NFP_Skip:	JSR	ReadFileToolTypes
		RTS


		
	;Test which version of Dos we are using.
StartedFromCLI:	MOVE.L	DosBase,A0
		CMP.W	#37,20(A0)
		BLO	GetOldDosArgs
		
	;We Are Using AmigaDos 2.x Upwards, so use an Arg Template
		MOVE.L	DosBase,A6
		MOVE.L	#ArgTemplate,D1
		MOVE.L	#ArgArray,D2
		MOVE.L	#0,D3
		JSR	_LVOReadArgs(A6)
		MOVE.L	D0,RdArgs
		BEQ	ArgsError
		
	;Get the filename
		MOVE.L	DefaultFilePtr,MenuFileName	;Default name
		MOVE.L	AA_File,D0
		BEQ	NoAA_File
		MOVE.L	D0,MenuFileName

	;Read the filename.info file now, as CLI parameters will
	;overide any tooltypes set in the icon ToolTypes
		JSR	ReadFileToolTypes

	;Get the font name
NoAA_File:	MOVE.L	AA_Font,D0
		BEQ	NoAA_Font
		MOVE.L	D0,TopazFont
	
	;Get the font size

NoAA_Font:	MOVE.L	AA_Size,D0
		BEQ	NoAA_Size
		MOVE.L	D0,A0
		TST.W	2(A0)
		BEQ	NoAA_Size
		MOVE.W	2(A0),TopazFont+ta_YSize
	
	;Get the number of gadget columns	
NoAA_Size:	MOVE.L	AA_Cols,D0
		BEQ	NoAA_Cols
		MOVE.L	D0,A0
		TST.W	2(A0)
		BEQ	NoAA_Cols
		MOVE.W	2(A0),GadColumns
		
	;Get the width of the window
NoAA_Cols:	MOVE.L	AA_Width,D0
		BEQ	NoAA_Width
		MOVE.L	D0,A0
		TST.W	2(A0)
		BEQ	NoAA_Width
		MOVE.W	2(A0),WindowWidth
		
	;Get the name of the console output
NoAA_Width:	MOVE.L	AA_Console,D0
		BEQ	NoAA_Console
		MOVE.L	D0,ConsoleName
		
	;Test for "TOP" switch
NoAA_Console:	TST.L	AA_Top
		BEQ	NoAA_Top
		MOVE.B	#1,WindowY_Top
		BRA	AA_Skip1

	;Test for "BOTTOM" switch
NoAA_Top:	TST.L	AA_Bottom
		BEQ	NoAA_Bottom
		MOVE.B	#1,WindowY_Bottom
		BRA	AA_Skip1
		
	;No "TOP" or "BOTTOM" so set to middle
NoAA_Bottom:	MOVE.B	#1,WindowY_Middle
		
	;Test for "LEFT" switch
AA_Skip1:	TST.L	AA_Left
		BEQ	NoAA_Left
		MOVE.B	#1,WindowX_Left
		BRA	AA_Skip2
		
	;Test for "RIGHT" switch
NoAA_Left:	TST.B	AA_Right
		BEQ	NoAA_Right
		MOVE.B	#1,WindowX_Right
		BRA	AA_Skip2
		
	;No "LEFT" or "RIGHT" so set to middle
NoAA_Right:	MOVE.B	#1,WindowX_Middle
		
	;If run from CLI, the hide window will always be set to the 
	;Top-Left of the screen.
AA_Skip2:	MOVE.B	#1,HideX_Screen
		MOVE.B	#1,HideX_Left
		MOVE.B	#1,HideY_Screen
		MOVE.B	#1,HideY_Top

		BRA	SkipCliArgs
		

	;If running AmigaDos 1.x then the whole argument list is viewed
	;as the filename. Very basic, but allows Older Machines to at
	;least use this program with limited features!!
	
	;Firstly set the defaults to MIDDLE of screen & HIDE at Top/Left
GetOldDosArgs:	MOVE.B	#1,WindowX_Middle
		MOVE.B	#1,WindowY_Middle
		MOVE.B	#1,HideX_Screen
		MOVE.B	#1,HideX_Left
		MOVE.B	#1,HideY_Screen
		MOVE.B	#1,HideY_Top

	;Now get the filename from the CLI parameter.
		MOVE.L	CliParmPtr,A0
		ADD.L	CliParmLen,A0
		SUB.L	#1,A0
StripSpaceLoop:	SUB.L	#1,A0
		CMP.B	#' ',(A0)
		BEQ	StripSpaceLoop
		MOVE.B	#0,1(A0)
		MOVE.L	CliParmPtr,A0
		MOVE.L	A0,MenuFileName
		
	;If no parameter set to Default File Name
		TST.B	(A0)
		BNE	OldCLI_Skip1
		MOVE.L	DefaultFilePtr,MenuFileName
		
OldCLI_Skip1:	JSR	ReadFileToolTypes
		
SkipCliArgs:	RTS
	
	;Arguments didn't match the template
ArgsError:	MOVE.L	DosBase,A6
		MOVE.L	#ArgsErrorTxt,D1
		JSR	_LVOPutStr(A6)
		
	;A NULL MenuFileName indicates an error occured
		CLR.L	MenuFileName
		RTS	
	
	;The following routine checks for any tool types on the
	;GadGrid icon as changes the appropriate defaults.
		
	;Get the pointer to the directory lock & Program File
ReadToolTypes:	MOVE.L	Message,A0
		MOVE.L	$24(A0),A0

	;Move to the Correct Directory
		MOVE.L	DosBase,A6
		MOVE.L	(A0),D1		;Lock on ProgDir
		JSR	_LVOCurrentDir(A6)
		MOVE.L	D0,-(A7)	;Store Old Dir Lock for later
	
	;Load the Disk Object file
		MOVE.L	IconBase,A6
		MOVE.L	Message,A0
		MOVE.L	$24(A0),A0
		MOVE.L	4(A0),A0	;Pointer to Program Name
		JSR	_LVOGetDiskObject(A6)
		
	;Check we managed to load the icon file
		MOVE.L	D0,DiskObjectBase
		BEQ	NoIconFile
	
	;Fetch Pointer to the Tool Types Array
		MOVE.L	D0,A0
		MOVE.L	do_ToolTypes(A0),ToolTypeArray
		
	;Check for a "DEFAULT=<name>" Tool Type
		MOVE.L	IconBase,A6
		MOVE.L	ToolTypeArray,A0
		LEA	TT_Default,A1
		JSR	_LVOFindToolType(A6)
		TST.L	D0
		BEQ	NoDefault_TT
		
	;Store the Name as the Default File Name
		MOVE.L	D0,DefaultFilePtr
NoDefault_TT:

	;Check for "WindowY=Top"
		LEA	TT_WindowY,A1
		LEA	TV_Top,A0
		LEA	WindowY_Top,A2
		JSR	ScanToolType
		
	;Check for "WindowY=Middle"
		LEA	TT_WindowY,A1
		LEA	TV_Middle,A0
		LEA	WindowY_Middle,A2
		JSR	ScanToolType

	;Check for "WindowY=Bottom"
		LEA	TT_WindowY,A1
		LEA	TV_Bottom,A0
		LEA	WindowY_Bottom,A2
		JSR	ScanToolType
		
	;Check for "WindowX=Left"
		LEA	TT_WindowX,A1
		LEA	TV_Left,A0
		LEA	WindowX_Left,A2
		JSR	ScanToolType
		
	;Check for "WindowX=Middle"
		LEA	TT_WindowX,A1
		LEA	TV_Middle,A0
		LEA	WindowX_Middle,A2
		JSR	ScanToolType
		
	;Check for "WindowX=Right"
		LEA	TT_WindowX,A1
		LEA	TV_Right,A0
		LEA	WindowX_Right,A2
		JSR	ScanToolType

	;Check for "HideX=Screen"
		LEA	TT_HideX,A1
		LEA	TV_Screen,A0
		LEA	HideX_Screen,A2
		JSR	ScanToolType

	;Check for "HideX=Window"
		LEA	TT_HideX,A1
		LEA	TV_Window,A0
		LEA	HideX_Window,A2
		JSR	ScanToolType

	;Check for "HideX=Left"
		LEA	TT_HideX,A1
		LEA	TV_Left,A0
		LEA	HideX_Left,A2
		JSR	ScanToolType
		
	;Check for "HideX=Middle"
		LEA	TT_HideX,A1
		LEA	TV_Middle,A0
		LEA	HideX_Middle,A2
		JSR	ScanToolType
		
	;Check for "HideX=Right"
		LEA	TT_HideX,A1
		LEA	TV_Right,A0
		LEA	HideX_Right,A2
		JSR	ScanToolType

	;Check for "HideY=Screen"
		LEA	TT_HideY,A1
		LEA	TV_Screen,A0
		LEA	HideY_Screen,A2
		JSR	ScanToolType

	;Check for "HideY=Window"
		LEA	TT_HideY,A1
		LEA	TV_Window,A0
		LEA	HideY_Window,A2
		JSR	ScanToolType
		
	;Check for "HideY=Top"
		LEA	TT_HideY,A1
		LEA	TV_Top,A0
		LEA	HideY_Top,A2
		JSR	ScanToolType
		
	;Check for "HideY=Middle"
		LEA	TT_HideY,A1
		LEA	TV_Middle,A0
		LEA	HideY_Middle,A2
		JSR	ScanToolType
		
	;Check for "HideY=Bottom"
		LEA	TT_HideY,A1
		LEA	TV_Bottom,A0
		LEA	HideY_Bottom,A2
		JSR	ScanToolType
	
		
	;Check for a "FontName=<name>" Tool Type
		MOVE.L	IconBase,A6
		MOVE.L	ToolTypeArray,A0
		LEA	TT_FontName,A1
		JSR	_LVOFindToolType(A6)
		TST.L	D0
		BEQ	NoFontName_TT

	;Move the font name into our TextAttr structure.
		MOVE.L	D0,TopazFont

	;Check for a "FontSize=<size>" Tool Type
NoFontName_TT:	MOVE.L	IconBase,A6
		MOVE.L	ToolTypeArray,A0
		LEA	TT_FontSize,A1
		JSR	_LVOFindToolType(A6)
		TST.L	D0
		BEQ	NoFontSize_TT

	;Convert the Tool Type Value into a Number
		MOVE.L	D0,A0
		JSR	StringToNumber

	;Check there was a number there.
		TST.W	D0
		BEQ	NoFontSize_TT
		MOVE.W	D0,TopazFont+ta_YSize

	;Check for a "Columns=<number>" Tool Type
NoFontSize_TT:	MOVE.L	IconBase,A6
		MOVE.L	ToolTypeArray,A0
		LEA	TT_Columns,A1
		JSR	_LVOFindToolType(A6)
		TST.L	D0
		BEQ	NoColumns_TT

	;Convert the Tool Type Value into a Number
		MOVE.L	D0,A0
		JSR	StringToNumber

	;Check there was a number there.
		TST.W	D0
		BEQ	NoColumns_TT
		MOVE.W	D0,GadColumns

	;Check for a "WindowWidth=<number>" Tool Type
NoColumns_TT:	MOVE.L	IconBase,A6
		MOVE.L	ToolTypeArray,A0
		LEA	TT_WindowWidth,A1
		JSR	_LVOFindToolType(A6)
		TST.L	D0
		BEQ	NoWinWidth_TT

	;Convert the Tool Type Value into a Number
		MOVE.L	D0,A0
		JSR	StringToNumber

	;Check there was a number there.
		TST.W	D0
		BEQ	NoWinWidth_TT
		MOVE.W	D0,WindowWidth

	;Check for a "Console=<console-name>" Tool Type
NoWinWidth_TT:	MOVE.L	IconBase,A6
		MOVE.L	ToolTypeArray,A0
		LEA	TT_Console,A1
		JSR	_LVOFindToolType(A6)
		TST.L	D0
		BEQ	NoConsole_TT

	;Move the font name into our TextAttr structure.
		MOVE.L	D0,ConsoleName


NoConsole_TT:
	
	;Restore the Current Directory to original value
NoIconFile:	MOVE.L	DosBase,A6
		MOVE.L	(A7)+,D1
		JSR	_LVOCurrentDir(A6)
		
		RTS
	
	;This routine will scan the ToolTypes Array 
	;	A0 = Pointer to Type Variable to Check
	;	   = or Zero if just looking for the Tool Type itself
	;	A1 = Pointer to the ToolType to look for
	;	A2 = Pointer to the 1 Byte Indicator to set if found
	
	;Firstly Find the Tool Type	
ScanToolType:	MOVE.L	IconBase,A6
		MOVEM.L	A0/A2,-(A7)		;Store for later use
		MOVE.L	ToolTypeArray,A0
		JSR	_LVOFindToolType(A6)
		MOVEM.L	(A7)+,A0/A2		;Restore A0/A2 values
		TST.L	D0
		BEQ	STT_NotFound
	
	;Check if we need a Type Variable Too
		CMP.L	#0,A0
		BEQ	STT_Found		;No, Just the ToolType
	
	;Check for Type Variable
		MOVE.L	A0,A1
		MOVE.L	D0,A0
		MOVE.L	A2,-(A7)
		JSR	_LVOMatchToolValue(A6)
		MOVE.L	(A7)+,A2
		MOVE.B	#0,(A2)
		TST.L	D0
		BEQ	STT_NotFound
		
STT_Found:	MOVE.B	#1,(A2)			;Set the indicator flag

STT_NotFound:	RTS

	SECTION	ReadParameters,DATA
	
	
CliParmPtr:	DC.L	0	;For pointer to CLI arguments 
CliParmLen:	DC.L	0	;For length of CLI arguments
TaskPtr:	DC.L	0	;Pointer to Task Structure
StartUpDirLock:	DC.L	0	;Pointer to the Initial Directory Lock
Message:	DC.L	0	;Pointer to Workbench's StartUp Message
DirectoryLock:	DC.L	0	;Pointer to the Project Files Dir Lock
MenuFileName:	DC.L	0	;Pointer to the File Name to Use
RdArgs:		DC.L	0	;Pointer to the RdArg Structure
DefaultFilePtr:	DC.L	DefaultFileDef	;Pointer to Default File Name
ToolTypeArray:	DC.L	0	;Pointer to the Tool Types Array
DiskObjectBase:	DC.L	0	;Pointer to the Disk Object Structure

ArgArray:			;1 Long Word for each possible arg
AA_File:	DC.L	0	;Name of file to load
AA_Font:	DC.L	0	;Name of font to use
AA_Size:	DC.L	0	;Size of font to use
AA_Cols:	DC.L	0	;No of gadget Columns
AA_Width:	DC.L	0	;Width of window
AA_Console:	DC.L	0	;Console name for output
AA_Top:		DC.L	0	;Open window at screen top
AA_Bottom:	DC.L	0	;Open window at screen bottom
AA_Left:	DC.L	0	;Open window at screen left
AA_Right:	DC.L	0	;Open window at screen right

ArgTemplate:	DC.B	"FILE,FONT/K,SIZE/K/N,COLS/K/N,WIDTH/K/N,"
		DC.B	"CONSOLE/K,TOP/S,BOTTOM/S,LEFT/S,RIGHT/S,",0
ArgsErrorTxt:	DC.B	"GadGrid <Gadget-File>",0
DefaultFileDef:	DC.B	"default.grid",0

;Accepted Tool Types

TT_Default:	DC.B	"Default",0
TT_WindowY:	DC.B	"WindowY",0
TT_WindowX:	DC.B	"WindowX",0
TT_WindowWidth:	DC.B	"WindowWidth",0
TT_HideX:	DC.B	"HideX",0
TT_HideY:	DC.B	"HideY",0
TT_FontName:	DC.B	"FontName",0
TT_FontSize:	DC.B	"FontSize",0
TT_Columns:	DC.B	"Columns",0
TT_Console:	DC.B	"Console",0


;Accepted Tool Variables

TV_Left:	DC.B	"Left",0
TV_Middle:	DC.B	"Middle",0
TV_Right:	DC.B	"Right",0
TV_Top:		DC.B	"Top",0
TV_Bottom:	DC.B	"Bottom",0
TV_Max:		DC.B	"Max",0
TV_Window:	DC.B	"Window",0
TV_Screen:	DC.B	"Screen",0



;Flags set by Tool Type Parameters...
WindowY_Top:	DC.B	0	;Set if "WindowY=Top"
WindowY_Middle:	DC.B	0	;Set if "WindowY=Middle"
WindowY_Bottom:	DC.B	0	;Set if "WindowY=Bottom"
WindowX_Left:	DC.B	0	;Set if "WindowX=Left"
WindowX_Middle:	DC.B	0	;Set if "WindowX=Middle"
WindowX_Right:	DC.B	0	;Set if "WindowX=Right"

HideX_Screen:	DC.B	0	;Set if "HideX=Screen"
HideX_Window:	DC.B	0	;Set if "HideX=Window"
HideX_Left:	DC.B	0	;Set if "HideX=Left" 
HideX_Middle:	DC.B	0	;Set if "HideX=Middle"
HideX_Right:	DC.B	0	;Set if "HideX=Right"

HideY_Screen:	DC.B	0	;Set if "HideY=Screen"
HideY_Window:	DC.B	0	;Set if "HideY=Window"
HideY_Top:	DC.B	0	;Set if "HideY=Top" 
HideY_Middle:	DC.B	0	;Set if "HideY=Middle"
HideY_Bottom:	DC.B	0	;Set if "HideY=Bottom"
	