
;   This source code is provided for reference only, do not use it to make
; your own unauthorised versions of MultiMenu. Doing so is a breach of my
; copyright. If I see unauthorised versions of any of my programs I will
; stop providing the source code with them. So Don't spoil it for others!


;*********************************
;**** Necessary Start Up Bits ****
;*********************************

	;Clear the BSS data to Zero
		LEA.L	BssPtr,A1
		MOVE.L	#BssLen-1,D1
ClearLoop:	CLR.L	(A1)+
		DBNE	D1,ClearLoop

	;Store away the CLI parameter pointers
		MOVE.L	A0,ParmPtr
		MOVE.L	D0,ParmLen

	;Find this Task
		MOVE.L	4,A6
		SUB.L	A1,A1
		JSR	_LVOFindTask(A6)
		MOVE.L	D0,TaskPtr
		
	;Create "SpecialID" string for Batch file
	;  This is based on the task address, If two copies of MultiMenu
	;  are running simultaneously they will be at different addresses
	;  so will create their own unique MM_Batch file.
		MOVE.L	D0,D2
		MOVE.L	D2,D1
		AND.L	#7,D1
		ADD.B	D1,SpecialID

		ASR.L	#3,D2
		MOVE.L	D2,D1
		AND.L	#7,D1
		ADD.B	D1,SpecialID+1 

		ASR.L	#3,D2
		MOVE.L	D2,D1
		AND.L	#7,D1
		ADD.B	D1,SpecialID+2
		
		ASR.L	#3,D2
		MOVE.L	D2,D1
		AND.L	#7,D1
		ADD.B	D1,SpecialID+3
		
	;Store to Current Directory Lock
		MOVE.L	D0,A4
		MOVE.L	pr_CurrentDir(A4),OldDirectory

	;Test if run from CLI or Workbench
		TST.L	pr_CLI(A4)
		BNE	FromCLI
		
	;Get the message from Workbench
		LEA	pr_MsgPort(A4),A0
		JSR	_LVOWaitPort(a6)
		JSR	_LVOGetMsg(A6)
		MOVE.L	D0,Message
FromCLI:		

;***************************************
;**** Open all the libraries needed ****
;***************************************

	;Open the Dos Library
		MOVE.L	4,A6
		LEA	DosLibName,A1
		CLR.L	D0
		JSR	_LVOOpenLibrary(A6)
		MOVE.L	D0,DosBase
		BEQ	ExitQuick
		
	;Open the Intuition library
		MOVE.L	4,A6
		LEA	IntLibName,A1
		CLR.L	D0
		JSR	_LVOOpenLibrary(A6)
		MOVE.L	D0,IntBase
		BEQ	ExitQuick
		
	;Open the Graphics library
		MOVE.L	4,A6
		LEA	GfxLibName,A1
		CLR.L	D0
		JSR	_LVOOpenLibrary(A6)
		MOVE.L	D0,GrxBase
		BEQ	ExitQuick
		
	;Open the Icon library
		MOVE.L	4,A6
		LEA	IconLibName,A1
		CLR.L	D0
		JSR	_LVOOpenLibrary(A6)
		MOVE.L	D0,IconBase
		BEQ	ExitQuick
		
;**********************************
;**** Interpret the parameters ****
;**********************************

	;Test if run from Workbench or CLI
		TST.L	Message
		BEQ	GetCliArgs
	
	;Get the Workbench arguments
		MOVE.L	Message,A0
		MOVE.L	$24(A0),A0
		MOVE.L	8(A0),DirectoryLock
		BEQ	NoProjFile
		MOVE.L	12(A0),FileNamePtr
		BNE	SkipCliArgs

	;No Project File so use "default" file
NoProjFile:	CLR.L	DirectoryLock
		JSR	UseDefaultName
		BRA	SkipCliArgs

	;Test which version of Dos we are using.		
GetCliArgs:	MOVE.L	DosBase,A0
		CMP.W	#37,20(A0)
		BLO	GetOldDosArgs
		
	;Using New Dos we should parse command line properly
		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	ArgArray,D0
		BEQ	NoArgGiven
		MOVE.L	D0,FileNamePtr
		BRA	SkipCliArgs
		
	;No argument given so set to "default" file name.
NoArgGiven:	JSR	UseDefaultName
		BRA	SkipCliArgs

	;For Old Dos the whole argument list is treated as the filename!
	;We must ensure that it is NULL terminated though.
GetOldDosArgs:	MOVE.L	ParmPtr,A0
		ADD.L	ParmLen,A0
		SUB.L	#1,A0
StripLoop:	SUB.L	#1,A0
		CMP.B	#' ',(A0)
		BEQ	StripLoop
		MOVE.B	#0,1(A0)
		MOVE.L	ParmPtr,A0
		MOVE.L	A0,FileNamePtr

	;If no parameter set to "default" filename
		TST.B	(A0)
		BNE	SkipCliArgs
		JSR	UseDefaultName
		
SkipCliArgs:
	;Test if a directory lock exists
		TST.L	DirectoryLock
		BNE	SkipDefaultDir
		
	;No so get one on the default directory
TryAgain:	MOVE.L	DosBase,A6
		MOVE.L	#DefaultPath,D1
		MOVE.L	#ACCESS_READ,D2
		JSR	_LVOLock(A6)
		MOVE.L	D0,DirectoryLock
		BNE	SkipDefaultDir
		
	;Failed to find default path so use the current directory
		CLR.L	DirectoryLock
		BRA	SkipChangeDir

	;Change to the directory	
SkipDefaultDir:	MOVE.L	DosBase,A6
		MOVE.L	DirectoryLock,D1
		JSR	_LVOCurrentDir(a6)

	;Get a LOCK on the filename
SkipChangeDir:	MOVE.L	DosBase,A6
		MOVE.L	FileNamePtr,D1
		MOVE.L	#ACCESS_READ,D2
		JSR	_LVOLock(A6)
		MOVE.L	D0,FileLock
		BNE	GotLock

	;We Couldn't Get a lock, are we using "default" ?
		CMP.L	#DefaultName,FileNamePtr
		BEQ	FailedToOpen
		
	;OK try to get "DEFAULT" instead
		MOVE.L	#DefaultName,FileNamePtr
		BRA	TryAgain
	
	;Examine the file
GotLock:	MOVE.L	DosBase,A6
		MOVE.L	FileLock,D1
		MOVE.L	#InfoBlock,D2
		JSR	_LVOExamine(A6)
		
	;Get the file length
		MOVE.L	InfoBlock+fib_Size,FileSize

	;Release the Lock on the file
		MOVE.L	DosBase,A6
		MOVE.L	FileLock,D1
		JSR	_LVOUnLock(A6)
	
	;Attempt to Allocate Memory
		MOVE.L	4,A6
		MOVE.L	FileSize,D0
		ADD.L	#3,D0
		MOVE.L	#MEMF_PUBLIC,D1
		JSR	_LVOAllocMem(A6)
		MOVE.L	D0,MemPtr
		BEQ	FileTooBig
		
	;Open the File for reading
		MOVE.L	DosBase,A6
		MOVE.L	FileNamePtr,D1
		MOVE.L	#1005,D2
		JSR	_LVOOpen(A6)
		MOVE.L	D0,Handle
		BEQ	FailedToOpen
		
	;Read the file into the allocated memory
		MOVE.L	DosBase,A6
		MOVE.L	Handle,D1
		MOVE.L	MemPtr,D2
		MOVE.L	FileSize,D3
		JSR	_LVORead(A6)

	;Close the filename
		MOVE.L	DosBase,A6
		MOVE.L	Handle,D1
		JSR	_LVOClose(A6)

	;Check if the very first character is '#'
		MOVE.L	MemPtr,A0
		CMP.B	#'#',(A0)
		BEQ	FileWasEmpty

	;Ensure the last 3 bytes of file are 10,'#',10
		ADD.L	FileSize,A0
		MOVE.B	#10,(A0)+
		MOVE.B	#'#',(A0)+
		MOVE.B	#10,(A0)+

	;Open a screen to put the menu on
		MOVE.L	IntBase,A6
		LEA	NewScreenDef,A0
		JSR	_LVOOpenScreen(A6)
		MOVE.L	D0,ScreenBase
		BEQ	FailedScreen

	;Open a window to put the menu on
		MOVE.L	IntBase,A6
		LEA	NewWindowDef,A0
		JSR	_LVOOpenWindow(A6)
		MOVE.L	D0,WindowBase
		BEQ	FailedWindow

	;Store away the RastPort address for the Window
		MOVE.L	WindowBase,A0
		MOVE.L	50(A0),RastBase
		
	;Add a menu to the window
		MOVE.L	WindowBase,A0
		LEA	MenuDef,A1
		JSR	_LVOSetMenuStrip(A6)

	;Initialise Values for Printing Routine
		MOVE.L	#8,Y_Pos
		MOVE.L	MemPtr,TextPtr
		
	;Set the A Pen colour
		MOVE.L	GrxBase,A6
		MOVE.L	RastBase,A1
		MOVE.L	#1,D0
		JSR	_LVOSetAPen(A6)
		
	;Position the Gfx Cursor	
PrintLoop:	MOVE.L	GrxBase,A6
		MOVE.L	#0,D0
		MOVE.L	Y_Pos,D1
		MOVE.L	RastBase,A1
  		JSR	_LVOMove(A6)

	;Increase the Y Position
		ADD.L	#8,Y_Pos
		
	;Calculate the Text Length
		MOVE.L	TextPtr,A0
CalcLoop:	CMP.B	#$A,(A0)+
		BNE	CalcLoop
		MOVE.L	A0,D0
		SUB.L	TextPtr,D0
		MOVE.L	D0,TextLen

	;Only write the text if Y_Pos < 260
		CMP.L	#264,Y_Pos
		BGT	SkipTextWrite
		
	;Write the text
		MOVE.L	GrxBase,A6
		MOVE.L	RastBase,A1
		MOVE.L	TextPtr,A0
		MOVE.L	TextLen,D0
		SUB.L	#1,D0		;Don't write the LF
		CMP.L	#80,D0
		BLE	LineLengthOK
		MOVE.L	#80,D0		;Only write 80 chars max.
LineLengthOK:	JSR	_LVOText(A6)

	;Calculate the new text pointer * Loop if needed
SkipTextWrite:	MOVE.L	TextPtr,A0
		ADD.L	TextLen,A0
		MOVE.L	A0,TextPtr
		CMP.B	#'#',(A0)
		BNE	PrintLoop
		
	;Set all the values in ValidKeys array for the keys to scan
		LEA	ValidKeys,A1
		MOVE.L	TextPtr,A0
		SUB.L	#1,A0
PullKeysLoop:	CMP.B	#10,(A0)+
		BNE	PullKeysLoop
		CMP.B	#'#',(A0)
		BNE	PullKeysLoop
		ADD.L	#1,A0
		CMP.B	#10,(A0)
		BEQ	DonePKLoop
		CLR.L	D0
		MOVE.B	(A0)+,D0
		MOVE.B	#1,0(A1,D0)
		BRA	PullKeysLoop
		
	;Ensure that ESC is always a valid key
DonePKLoop:	MOVE.B	#1,27(A1)
		
	;Wait for an IDCMP event to occur
WaitKeyLoop:	MOVE.L	WindowBase,A0
		MOVE.L	wd_UserPort(A0),A0
		MOVE.L	A0,-(A7)
		CLR.L	D1
		MOVE.B	MP_SIGBIT(A0),D1
		MOVE.L	#1,D0
		ASL.L	D1,D0
		MOVE.L	4,A6
		JSR	_LVOWait(A6)
		
	;Get the IDCMP message
		MOVE.L	(A7)+,A0
		JSR	_LVOGetMsg(A6)
		MOVE.L	D0,A1
		MOVE.W	im_Code(A1),EventCode
		MOVE.L	im_Class(A1),EventClass
	
	;Tell exec that we got the message
		JSR	_LVOReplyMsg(A6)

	;Test for Menu Item Picked
		CMP.L	#MENUPICK,EventClass
		BNE	NoMenuPicked
		CMP.W	#63488,EventCode
		BEQ	PickedEdit
		CMP.W	#63520,EventCode
		BEQ	PickedAbout
		CMP.W	#63552,EventCode
		BEQ	PickedQuit
		BRA	WaitKeyLoop

	;Display the About Requester
PickedAbout:	JSR	AboutThisProg
		BRA	WaitKeyLoop

	;If Edit was selected then Launch ED and exit
	;To do this we set EventCode to 0
	;And Exit as if the program was executing a Menu Selection
PickedEdit:	MOVE.W	#0,EventCode
		BRA	SkipKeyTest

	;If Quit was selected then we simply put ESC in the EventCode
	;And act as if ESC had been pressed
PickedQuit:	MOVE.W	#27,EventCode
		
	;Test for a valid key
NoMenuPicked:	LEA	ValidKeys,A0
		MOVE.W	EventCode,D0
TestItAgain:	TST.L	0(A0,D0.W)
		BNE	SkipKeyTest
		
	;If it was an UPPERCASE letter then make it lower & try again
		CMP.W	#'A',D0
		BMI	WaitKeyLoop
		CMP.W	#'Z',D0
		BGT	WaitKeyLoop
		ADD.W	#$20,D0
		MOVE.W	D0,EventCode
		BRA	TestItAgain
		
	;Close the Menu
SkipKeyTest:	MOVE.L	IntBase,A6
		MOVE.L	WindowBase,A0
		JSR	_LVOClearMenuStrip(A6)
		
	;Close the Window
		MOVE.L	IntBase,A6
		MOVE.L	WindowBase,A0
		JSR	_LVOCloseWindow(A6)

	;Close the Screen
FailedWindow:	MOVE.L	IntBase,a6
		MOVE.L	ScreenBase,A0
		JSR	_LVOCloseScreen(A6)

	;Test if ESC was pressed
		CMP.W	#27,EventCode
		BEQ	PressedEsc
		
	;Test if EDIT was selected
		TST.W	EventCode
		BNE	SkipDoEdit
	
	;Edit was selected so create a Batch to EDIT the menu file
		LEA	EditCommand,A0
		MOVE.L	A0,A1
		MOVE.W	#'ED',(A1)+
		MOVE.B	#' ',(A1)+
		
	;Transfer the FileName to our command
		MOVE.L	FileNamePtr,A2
TransLoop:	MOVE.B	(A2)+,(A1)+
		TST.B	(A2)
		BNE	TransLoop
		
		MOVE.B	#10,(A1)+
		MOVE.B	#'#',(A1)+
		MOVE.B	#10,(A1)+
				
		BRA	SkipOverIt	
		
	;Find the key in the Multi Menu file
SkipDoEdit:	MOVE.L	TextPtr,A0
		SUB.L	#1,A0
		MOVE.B	EventCode+1,D0
FindKeyLoop: 	CMP.B	#10,(A0)+
		BNE	FindKeyLoop
		CMP.B	#'#',(A0)
		BNE	FindKeyLoop
		ADD.L	#1,A0
		CMP.B	(A0),D0
		BNE	FindKeyLoop
		
	;We've found the entry, now skip past the Line Feed
SkipLF:		CMP.B	#10,(A0)+
		BNE	SkipLF
		
	;Store away the pointer
SkipOverIt:	MOVE.L	A0,TextPtr
		
	;Open a File to store the Batch Commands in.
		MOVE.L	DosBase,A6
		MOVE.L	#BatchName,D1
		MOVE.L	#MODE_NEWFILE,D2
		JSR	_LVOOpen(A6)
		MOVE.L	D0,Handle
		BEQ	NoBatchFile
		
	;Find the next #10,'#' after TextPtr
		MOVE.L	TextPtr,A0
FindEndLoop:	CMP.B	#10,(A0)+
		BNE	FindEndLoop
		CMP.B	#'#',(A0)
		BNE	FindEndLoop
		
	;Calculate the Length of Batch Text
		MOVE.L	A0,D3
		SUB.L	TextPtr,D3
		
	;Write the Batch File
		MOVE.L	DosBase,A6
		MOVE.L	Handle,D1
		MOVE.L	TextPtr,D2
		JSR	_LVOWrite(A6)
	
	;Close the file
		MOVE.L	DosBase,A6
		MOVE.L	Handle,D1
		JSR	_LVOClose(A6)

	;Check if the process was run from CLI or Workbench
		MOVE.L	TaskPtr,A0
		TST.L	pr_CLI(A0)
		BNE	GotOutputNow

	;Open the Default Output for running from Workbench
		MOVE.L	DosBase,A6
		MOVE.L	#ConName,D1
		MOVE.L	#MODE_NEWFILE,D2
		JSR	_LVOOpen(A6)
		MOVE.L	D0,ConHandle

	;Execute the Batch File
GotOutputNow:	MOVE.L	DosBase,A6
		MOVE.L	#Command,D1
		CLR.L	D2
		MOVE.L	ConHandle,D3
		JSR	_LVOExecute(A6)
			
	;Delete the Batch File
		MOVE.L	DosBase,A1
		MOVE.L	#BatchName,D1
		JSR	_LVODeleteFile(A6)
			
		BRA	PressedEsc

NoBatchFile:	LEA	NoBatchErr,A0
		JSR	ReportError

	;Free the memory
FileWasEmpty:
PressedEsc:	
FailedScreen:	MOVE.L	4,A6
		MOVE.L	MemPtr,A1
		MOVE.L	FileSize,D0
		ADD.L	#3,D0
		JSR	_LVOFreeMem(A6)

		BRA	ExitQuick
		
FailedToOpen:	LEA	FailedOpenErr,A0
		JSR	ReportError
		BRA	ExitQuick
		
FileTooBig:	LEA	FileTooBigErr,A0
		JSR	ReportError

;*****************************
;**** Close the libraries ****
;*****************************
	;Free the Lock on CD0:
ExitQuick:	MOVE.L	DosBase,A6
		MOVE.L	CdLock,D1
		BEQ	NoCdLock
		JSR	_LVOUnLock(A6)
		
	;Change back to the old directory
NoCdLock:	MOVE.L	DosBase,A6
		MOVE.L	OldDirectory,D1
		BEQ	NoOldDir
		JSR	_LVOCurrentDir(A6)

	;Free the RdArgs structure
NoOldDir:	MOVE.L	DosBase,A6
		MOVE.L	RdArgs,D1
		BEQ	NoRdArgs
		JSR	_LVOFreeArgs(A6)
		
	;Close the Icon library
NoRdArgs:	MOVE.L	4,A6
		MOVE.L	IconBase,A1
		BEQ	NoIconLib
		JSR	_LVOCloseLibrary(A6)
	
	;Close the Graphics library	
NoIconLib:	MOVE.L	4,A6
		MOVE.L	GrxBase,A1
		BEQ	NoGfxLib
		JSR	_LVOCloseLibrary(A6)
		
	;Close the Intuition library
NoGfxLib:	MOVE.L	4,A6
		MOVE.L	IntBase,A1
		BEQ	NoIntLib
		JSR	_LVOCloseLibrary(A6)
		
	;Close the Dos library
NoIntLib:	MOVE.L	4,A6
		MOVE.L	DosBase,A1
		BEQ	NoDosLib
		JSR	_LVOCloseLibrary(A6)
		
	;Test if program was run from Workbench
NoDosLib:	TST.L	Message
		BEQ	Exit
		
	;Yes so Reply to workbench that program has finished
		MOVE.L	4,A6
		JSR	_LVOForbid(A6)
		MOVE.L	Message,A1
		JSR	_LVOReplyMsg(A6)
		
	;Exit to CLI or Workbench with clear return code
Exit:		CLR.L	D0
		RTS
		
	;Incorrect arguments were given so give an error
ArgsError:	MOVE.L	DosBase,A6
		MOVE.L	#ArgErrorTxt,D1
		JSR	_LVOPutStr(A6)
		
		BRA	ExitQuick
	

;***********************************************************************
;**** This routine attempts to get name of CD0: if a MultiMenu file ****
;**** is found under this name then it is used, otherwise "Default" ****
;***********************************************************************

UseDefaultName:	
	;Disable Worbench Requesters while attempting to get the lock
		MOVE.L	TaskPtr,A0
		MOVE.L	pr_WindowPtr(A0),-(A7)
		MOVE.L	#-1,pr_WindowPtr(A0)
	
	;Get a Lock on CD0:
		MOVE.L	DosBase,A6
		MOVE.L	#CdName,D1
		MOVE.L	#ACCESS_READ,D2
		JSR	_LVOLock(A6)
		MOVE.L	D0,CdLock

	;Enable Workbench Requesters again
		MOVE.L	TaskPtr,A0
		MOVE.L	(A7)+,pr_WindowPtr(A0)

	;Check if we got a Lock or not
		TST.L	CdLock
		BEQ	UDN_NoLock
		
	;Examine the Lock
		MOVE.L	DosBase,A6
		MOVE.L	CdLock,D1
		MOVE.L	#CdInfoBlock,D2
		JSR	_LVOExamine(A6)

	;Get the Volume Name
		MOVE.L	#CdInfoBlock+8,FileNamePtr		
		RTS

SetToDefault:
UDN_NoLock:	MOVE.L	#DefaultName,FileNamePtr
		RTS

;*******************************************************************
;**** The following routine opens a small window containin info ****
;**** about this program & the author                           ****
;*******************************************************************

AboutThisProg:
	;Remove the Menu from the Window
		MOVE.L	IntBase,A6
		MOVE.L	WindowBase,A0
		JSR	_LVOClearMenuStrip(A6)

	;Open a window to display the information in
		MOVE.L	IntBase,A6
		LEA	ATP_WindowDef,A0
		MOVE.L	ScreenBase,ATP_ScreenBase
		JSR	_LVOOpenWindow(A6)
		MOVE.L	D0,ATP_WindowBase	
		BEQ	ATP_NoWindow

	;Get the windows RastPort
		MOVE.L	ATP_WindowBase,A0
		MOVE.L	50(A0),ATP_RastPort
		
	;Set A Pen to Colour 2
		MOVE.L	GrxBase,A6
		MOVE.L	ATP_RastPort,A1
		MOVE.L	#2,D0
		JSR	_LVOSetAPen(A6)
		
	;Move Graphics cursor to 0,0
		MOVE.L	GrxBase,A6
		MOVE.L	ATP_RastPort,A1
		CLR.L	D0
		CLR.L	D1
		JSR	_LVOMove(A6)
		
	;Draw a line across top of window
		MOVE.L	GrxBase,A6
		MOVE.L	ATP_RastPort,A1
		MOVE.L	#320,D0
		CLR.L	D1
		JSR	_LVODraw(A6)

	;Move Graphics cursor to 0,0
		MOVE.L	GrxBase,A6
		MOVE.L	ATP_RastPort,A1
		CLR.L	D0
		CLR.L	D1
		JSR	_LVOMove(A6)

	;Draw a line down left of window
		MOVE.L	GrxBase,A6
		MOVE.L	ATP_RastPort,A1
		CLR.L	D0
		MOVE.L	#100,D1
		JSR	_LVODraw(A6)

	;Set A Pen to Colour 1
		MOVE.L	GrxBase,A6
		MOVE.L	ATP_RastPort,A1
		MOVE.L	#1,D0
		JSR	_LVOSetAPen(A6)
		
	;Move Graphics cursor to 0,99
		MOVE.L	GrxBase,A6
		MOVE.L	ATP_RastPort,A1
		CLR.L	D0
		MOVE.L	#99,D1
		JSR	_LVOMove(A6)
		
	;Draw a line across top of window
		MOVE.L	GrxBase,A6
		MOVE.L	ATP_RastPort,A1
		MOVE.L	#320,D0
		MOVE.L	#99,D1
		JSR	_LVODraw(A6)

	;Move Graphics cursor to 319,0
		MOVE.L	GrxBase,A6
		MOVE.L	ATP_RastPort,A1
		MOVE.L	#319,D0
		CLR.L	D1
		JSR	_LVOMove(A6)

	;Draw a line down left of window
		MOVE.L	GrxBase,A6
		MOVE.L	ATP_RastPort,A1
		MOVE.L	#319,D0
		MOVE.L	#100,D1
		JSR	_LVODraw(A6)
		
	;Write the IntuiText to the window
		MOVE.L	IntBase,A6
		MOVE.L	ATP_RastPort,A0
		LEA	ATP_TextDef,A1
		MOVE.L	#0,D0
		MOVE.L	#0,D1
		JSR	_LVOPrintIText(A6)

	;Wait for an IDCMP event to occur
		MOVE.L	ATP_WindowBase,A0
		MOVE.L	wd_UserPort(A0),A0
		MOVE.L	A0,-(A7)
		CLR.L	D1
		MOVE.B	MP_SIGBIT(A0),D1
		MOVE.L	#1,D0
		ASL.L	D1,D0
		MOVE.L	4,A6
		JSR	_LVOWait(A6)

	;Get the IDCMP message
		MOVE.L	(A7)+,A0
		JSR	_LVOGetMsg(A6)
	
	;Tell exec that we got the message
		MOVE.L	D0,A1
		JSR	_LVOReplyMsg(A6)
		
	;Exit now, we don't need to check the IDCMP event as our window
	;Only checks for the gadget clicks & only has one gadget!
		
	;Close the window
		MOVE.L	IntBase,A6
		MOVE.L	ATP_WindowBase,A0
		JSR	_LVOCloseWindow(A6)

	;Add back the Menu Strip
		MOVE.L	IntBase,A6
		MOVE.L	WindowBase,A0
		LEA	MenuDef,A1
		JSR	_LVOSetMenuStrip(A6)
		
ATP_NoWindow:	RTS
		
		
;***************************************************************	
;**** This routine informs the user of an error.            ****
;****                                                       ****
;**** On entry A0 points to a NULL terminated error message ****
;***************************************************************

ReportError:	TST.L	Message
		BEQ	RE_ErrorToCLI
	
	;Create a Workbench Requestor with the error in it
		MOVE.L	IntBase,A6
		MOVE.L	A0,ErrorPtr
		MOVE.L	TaskPtr,A0
		MOVE.L	pr_WindowPtr(A0),A0
		LEA	ReqBodyText,A1
		LEA	ReqOkayText,A2
		LEA	ReqOkayText,A3
		CLR.L	D0
		CLR.L	D1
		MOVE.L	#640,D2
		MOVE.L	#150,D3
		JSR	_LVOAutoRequest(A6)
		RTS
		
	;Firstly we must find the length of the error message
RE_ErrorToCLI:	MOVE.L	A0,A1
FindNull:	TST.B	(A1)+
		BNE	FindNull
		SUB.L	#1,A1
		SUB.L	A0,A1
		
	;Store the Pointer & Length on the Stack
		MOVE.L	A0,-(A7)
		MOVE.L	A1,-(A7)
	
	;Get the Default Output
		MOVE.L	DosBase,A6
		JSR	_LVOOutput(A6)
		MOVE.L	D0,ErrorPtr
		
	;Write the Error message
		MOVE.L	DosBase,A6
		MOVE.L	D0,D1		;File Handle
		MOVE.L	(A7)+,D3	;Text Length
		MOVE.L	(A7)+,D2	;Text Start Address
		JSR	_LVOWrite(A6)
		
	;Write a Line Feed too.
		MOVE.L	DosBase,A6
		MOVE.L	ErrorPtr,D1
		MOVE.L	#LineFeed,D2
		MOVE.L	#1,D3
		JSR	_LVOWrite(A6)
		
		RTS
	
		SECTION data,data

ReqBodyText:	DC.B	1		;Front Pen
		DC.B	0		;Back Pen
		DC.B	RP_JAM1		;Draw Mode
		DC.W	0,0		;Left Edge/Top Edge
		DC.L	FontDef		;Font Pointer
ErrorPtr:	DC.L	0		;Text for item
		DC.L	0		;Next Structure

ReqOkayText:	DC.B	1		;Front Pen
		DC.B	0		;Back Pen
		DC.B	RP_JAM1		;Draw Mode
		DC.W	0,0		;Left Edge/Top Edge
		DC.L	FontDef		;Font Pointer
		DC.L	ATP_GadText	;Text for item
		DC.L	0		;Next Structure

	

NewScreenDef:	DC.W	0,0,640,258	;X,Y,W,H
		DC.W	2		;Depth
		DC.B	1,2		;Detail / Block Pen
		DC.W	V_HIRES		;View Modes
		DC.W	CUSTOMSCREEN	;Screen Type
		DC.L	FontDef		;Screen Font
		DC.L	0		;Screen Title
		DC.L	0		;Gadgets
		DC.L	0		;Bitmap

NewWindowDef:	DC.W	0,0,640,258	;X,Y,W,H
		DC.B	1,2		;Detail / Block Pen
		DC.L	IDCMP_VANILLAKEY+IDCMP_MENUPICK	;IDCMP Flags
		DC.L	WFLG_BORDERLESS+WFLG_ACTIVATE	;Window Flags
		DC.L	0		;Gadget
		DC.L	0		;Check Mark
		DC.L	0		;Title
ScreenBase:	DC.L	0		;Screen
		DC.L	0		;Bitmap
		DC.W	0,0,0,0		;Min W,Min H,Max W,Max H
		DC.W	CUSTOMSCREEN	;Window Type

ATP_WindowDef:	DC.W	160,78,320,100	;X,Y,W,H
		DC.B	1,0		;Detail / Block Pen
		DC.L	IDCMP_GADGETUP	;IDCMP Flags
		DC.L	WFLG_ACTIVATE+WFLG_BORDERLESS	;Window Flags
		DC.L	ATP_GadgetDef	;Gadget
		DC.L	0		;Check Mark
		DC.L	0		;Title
ATP_ScreenBase:	DC.L	0		;Screen
		DC.L	0		;Bitmap
		DC.W	0,0,0,0		;Min W,Min H,Max W,Max H
		DC.W	CUSTOMSCREEN	;Window Type

ATP_GadgetDef:	DC.L	0		;Pointer to Next Gadget
		DC.W	140,80,40,11	;X,Y,H,W of Click Region
		DC.W	GADGHIMAGE	;Gadget has a Click Image
		DC.W	RELVERIFY	;Flags
		DC.W	BOOLGADGET	;Gadget Type
		DC.L	ATP_GadBorder   ;Pointer to Border Definition
		DC.L	ATP_ActBorder	;Pointer to Active Border
		DC.L	ATP_GadTextDef	;Pointer to IntuiText
		DC.L	0,0,0,0		;Not used (Gadget ID = 0)

ATP_GadBorder:	DC.W	0,0		;X,Y
		DC.B	2,0		;Detail / Block Pen
		DC.B	RP_JAM1		;Draw Mode
		DC.B	3		;# of coords
		DC.L	ATP_GadCoords	;Pointer to the Coords
		DC.L	ATP_GadBorder2	;Pointer to Next Border Def

ATP_GadBorder2:	DC.W	0,0		;X,Y
		DC.B	1,0		;Detail / Block Pen
		DC.B	RP_JAM1		;Draw Mode
		DC.B	3		;# of coords
		DC.L	ATP_GadCoords2	;Pointer to the Coords
		DC.L	0		;Pointer to Next Border Def
		
ATP_ActBorder:	DC.W	0,0		;X,Y
		DC.B	1,0		;Detail / Block Pen
		DC.B	RP_JAM1		;Draw Mode
		DC.B	3		;# of coords
		DC.L	ATP_GadCoords	;Pointer to the Coords
		DC.L	ATP_ActBorder2	;Pointer to Next Border Def

ATP_ActBorder2:	DC.W	0,0		;X,Y
		DC.B	2,0		;Detail / Block Pen
		DC.B	RP_JAM1		;Draw Mode
		DC.B	3		;# of coords
		DC.L	ATP_GadCoords2	;Pointer to the Coords
		DC.L	0		;Pointer to Next Border Def
		
		
ATP_GadCoords:	DC.W	0,10, 0,0, 38,0
ATP_GadCoords2:	DC.W	1,10, 39,10, 39,0

ATP_GadTextDef:	DC.B	1		;Front Pen
		DC.B	0		;Back Pen
		DC.B	RP_JAM1		;Draw Mode
		DC.W	4,2		;Left Edge/Top Edge
		DC.L	FontDef		;Font Pointer
		DC.L	ATP_GadText	;Text for item
		DC.L	0		;Next Structure


FontDef:	DC.L	TopazName	;Pointer to font name
		DC.W	8		;Font Height
		DC.B	0		;Font Style
		DC.B	0		;Font Flags

MenuDef:	DC.L	0		;Pointer to next Menu Structure
		DC.W	0,0,100,0	;X,Y,W,H
		DC.W	MENUENABLED	;Flags
		DC.L	MenuName	;Pointer to MenuName
		DC.L	MenuItem1	;Pointer to first item
		DC.W	0,0,0,0		;Internal use
		
MenuItem1:	DC.L	MenuItem2	;Pointer to next Menu Item
		DC.W	0,0,95,11	;X,Y,W,H
		DC.W	ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP
		DC.L	0		;Mutual Exlude
		DC.L	ItemName1	;Item Fill
		DC.L	0		;Select Fill
		DC.B	'E'		;Hot Key
		DC.L	0		;Sub Item
		DC.W	0		;Next Select
		
ItemName1:	DC.B	1		;Front Pen
		DC.B	0		;Back Pen
		DC.B	RP_JAM1		;Draw Mode
		DC.W	2,2		;Left Edge/Top Edge
		DC.L	FontDef		;Font Pointer
		DC.L	ItemText1	;Text for item
		DC.L	0		;Next Structure

MenuItem2:	DC.L	MenuItem3	;Pointer to next Menu Item
		DC.W	0,12,95,11	;X,Y,W,H
		DC.W	ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP
		DC.L	0		;Mutual Exlude
		DC.L	ItemName2	;Item Fill
		DC.L	0		;Select Fill
		DC.B	'A'		;Hot Key
		DC.L	0		;Sub Item
		DC.W	0		;Next Select
		
ItemName2:	DC.B	1		;Front Pen
		DC.B	0		;Back Pen
		DC.B	RP_JAM1		;Drap Mode
		DC.W	2,2		;Left Edge/Top Edge
		DC.L	FontDef		;Font Pointer
		DC.L	ItemText2	;Text for item
		DC.L	0		;Next Structure

MenuItem3:	DC.L	0		;Pointer to next Menu Item
		DC.W	0,24,95,11	;X,Y,W,H
		DC.W	ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP
		DC.L	0		;Mutual Exlude
		DC.L	ItemName3	;Item Fill
		DC.L	0		;Select Fill
		DC.B	'Q'		;Hot Key
		DC.L	0		;Sub Item
		DC.W	0		;Next Select
		
ItemName3:	DC.B	1		;Front Pen
		DC.B	0		;Back Pen
		DC.B	RP_JAM1		;Drap Mode
		DC.W	2,2		;Left Edge/Top Edge
		DC.L	FontDef		;Font Pointer
		DC.L	ItemText3	;Text for item
		DC.L	0		;Next Structure

ATP_TextDef:	DC.B	1		;Front Pen
		DC.B	0		;Back Pen
		DC.B	RP_JAM1		;Draw Mode
		DC.W	4,8		;Left Edge/Top Edge
		DC.L	FontDef		;Font Pointer
		DC.L	ATP_Text1	;Text for item
		DC.L	ATP_TextDef2	;Next Structure

ATP_TextDef2:	DC.B	1		;Front Pen
		DC.B	0		;Back Pen
		DC.B	RP_JAM1		;Draw Mode
		DC.W	4,24		;Left Edge/Top Edge
		DC.L	FontDef		;Font Pointer
		DC.L	ATP_Text2	;Text for item
		DC.L	ATP_TextDef3	;Next Structure

ATP_TextDef3:	DC.B	1		;Front Pen
		DC.B	0		;Back Pen
		DC.B	RP_JAM1		;Draw Mode
		DC.W	4,56		;Left Edge/Top Edge
		DC.L	FontDef		;Font Pointer
		DC.L	ATP_Text3	;Text for item
		DC.L	0		;Next Structure


ATP_Text1:	DC.B	"            Multi Menu V1.0            ",0
ATP_Text2:	DC.B	"          ©1997 - Mike Archer          ",0
ATP_Text3:	DC.B	"       This program is FreeWare.       ",0
FailedOpenErr:	DC.B	"Could not open Menu File.",0
FileTooBigErr:	DC.B	"Ran out of memory loading menu file.",0
NoBatchErr:	DC.B	"Could not create temp batch file, check T: assignment.",0
ATP_GadText:	DC.B	"OKAY",0
LineFeed:	DC.B	10
MenuName:	DC.B	"Menu",0
ItemText1:	DC.B	"Edit",0
ItemText2:	DC.B	"About",0
ItemText3:	Dc.B	"Quit",0
TopazName:	DC.B	"topaz.font",0
DosLibName:	DC.B	"dos.library",0		
IconLibName:	DC.B	"icon.library",0
IntLibName:	DC.B	"intuition.library",0
GfxLibName:	DC.B	"graphics.library",0
CdName:		DC.B	"CD0:",0
ArgTemplate:	DC.B	"MENUNAME",0
ArgErrorTxt:	DC.B	"Incorrect parameters, please read the user guide.",10,0		
DefaultName:	DC.B	"Default",0		
DefaultPath:	DC.B	"S:MultiMenu",0
ConName:	DC.B    "CON:0/0/640/100/Multi Menu V1.0/WAIT/CLOSE/AUTO",0
Command:	DC.B	"EXECUTE "
BatchName:	DC.B	"T:MM_Batch."
SpecialID:	DC.B	"0000",0		
	
		SECTION	bss,bss
BssPtr:	
ParmPtr:	DS.L	1
ParmLen:	DS.L	1
DosBase:	DS.L	1
IconBase:	DS.L	1
GrxBase:	DS.L	1
IntBase:	DS.L	1
WindowBase:	DS.L	1
ATP_WindowBase:	DS.L	1
ATP_RastPort	DS.L	1
RastBase:	DS.L	1
TextLen:	DS.L	1
Message:	DS.L	1	
FileNamePtr:	DS.L	1	
MemPtr:		DS.L	1	
ArgArray:	DS.L	1		
RdArgs:		DS.L	1	
DirectoryLock:	DS.L	1
OldDirectory:	DS.L	1
CdLock:		DS.L	1
Handle:		DS.L	1
FileSize:	DS.L	1
Y_Pos:		DS.L	1
ConHandle:	DS.L	1
TextPtr:	DS.L	1
TaskPtr:	DS.L	1
EventClass:	DS.L	1

InfoBlock:	DS.B	260

CdInfoBlock:	DS.B	260


ValidKeys:	DS.B	256

EditCommand:	DS.B	350 	;A buffer to create the Edit Command in.

EventCode:	DS.W	1
	
BssLen		EQU	(*-BssPtr)/4
		
		