

	;Store away the CLI parameters
		MOVE.L	A0,CliParmPtr
		MOVE.L	D0,CliParmLen

	;Open the intuition library
		MOVE.L	4,A6
		LEA	IntName,A1
		CLR.L	D0
		JSR	_LVOOpenLibrary(A6)
		MOVE.L	D0,IntBase

	;Open the graphics library
		MOVE.L	4,A6
		LEA	GraName,A1
		CLR.L	D0
		JSR	_LVOOpenLibrary(A6)
		MOVE.L	D0,GraBase

	;Open the dos library
		MOVE.L	4,A6
		LEA	DosName,A1
		CLR.L	D0
		JSR	_LVOOpenLibrary(A6)
		MOVE.L	D0,DosBase

	;Open the icon library
		MOVE.L	4,A6
		LEA	IconName,A1
		CLR.L	D0
		JSR	_LVOOpenLibrary(A6)
		MOVE.L	D0,IconBase
		
	;Open the diskfont library
		MOVE.L	4,A6
		LEA	DiskFontName,A1
		CLR.L	D0
		JSR	_LVOOpenLibrary(A6)
		MOVE.L	D0,DiskFontBase
		
	;Get a pointer to the Workbench Screen
		MOVE.L	IntBase,A6
		JSR	_LVOOpenWorkBench(A6)
		MOVE.L	D0,A0
		
	;Store maximum width of the window
		MOVE.w	sc_Width(A0),WindowWidth
		
	;Store the Height of the Title Bar
		MOVE.B	#0,TitleBarSize
		MOVE.B	sc_BarHeight(A0),TitleBarSize+1
		ADD.W	#2,TitleBarSize
		
	;Get address of the Screen Font's TextAttr structure
		MOVE.L	sc_Font(A0),A0
		
	;Get the Height of the Font
		MOVE.W	ta_YSize(A0),D0	
		MOVE.W	D0,ScreenFontSize
		
	;Set GadHeight to Font Size + 4
		MOVE.W	D0,GadHeight
		ADD.W	#4,GadHeight
		
	;Set Height of Hide Window to Font Size + 3
		MOVE.W	D0,LittleWinHeight
		ADD.W	#3,LittleWinHeight
		
	;Get any Parameters
		JSR	ReadParameters

	;Test for error in parameters
		TST.L	MenuFileName
		BEQ	Exit

	;Read in the Menu File
		JSR	ReadMenuFile
		
	;If MenuFileHandle = 0 then Failed so Exit
		TST.L	MenuFileHandle
		BEQ	Exit
		
		
	;Check if we have the DiskFont library, if not don't open font
		TST.L	DiskFontBase
		BEQ	SkipFontOpen

	;Open the font
		MOVE.L	DiskFontBase,A6
		LEA	TopazFont,A0
		JSR	_LVOOpenDiskFont(A6)
		MOVE.L	D0,FontBase
		BNE	OpenFont_OK

	;Set back to Topaz.font 8 (This will give the Screen Font)
		MOVE.L	#TopazName,TopazFont
		MOVE.W	#8,TopazFont+ta_YSize
		
	;Open the font. (We need a pointer to the Font Structure)
		MOVE.L	GraBase,A6
		LEA	TopazFont,A0
		JSR	_LVOOpenFont(A6)
		MOVE.L	D0,FontBase
		BEQ	Exit		;No Topaz Font!!!
OpenFont_OK:
		
SkipFontOpen:		


	;Create the Gadget Grid
		JSR	CreateGadGrid
	
	;Calculate the correct position for the Main Window
		JSR	CalcWindowPos
		
	;Calculate the Hide Window's position
		JSR	CalcHideWindowPos
	
	;Open a window
OpenMainWin:	MOVE.L	IntBase,A6
		LEA	WindowDef,A0
		JSR	_LVOOpenWindow(A6)
		MOVE.L	D0,WindowBase
		BEQ	MainWinOpenErr
		
	;Set the window font
		MOVE.L	GraBase,A6
		MOVE.L	WindowBase,A1
		MOVE.L	wd_RPort(A1),A1
		MOVE.L	FontBase,A0
		JSR	_LVOSetFont(A6)
		
	;Correct the Title-Bar Menu Structure for font height.
		JSR	CorrectMenuStructure
	
	;Add its Menu
		MOVE.L	IntBase,A6
		MOVE.L	WindowBase,A0
		LEA	MenuDef,A1
		JSR	_LVOSetMenuStrip(A6)

	;Wait for a IDCMP Message
WaitIDCMP:	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,-(A7)
		MOVE.L	D0,A1
		JSR	_LVOReplyMsg(A6)
		MOVE.L	(A7)+,A1
		
	;Check if it was CLOSE GADGET Clicked.
		CMP.L	#IDCMP_CLOSEWINDOW,im_Class(A1)
		BEQ	ClosedWindow

	;Check if it was a MENU ITEM Selected
		CMP.L	#IDCMP_MENUPICK,im_Class(A1)
		BEQ	MenuSelected

	;Check if a gadget was clicked on.
		CMP.L	#IDCMP_GADGETUP,im_Class(A1)
		BEQ	GadgetClicked

		BRA	WaitIDCMP
		
	;A Gadget was clicked on, call routine to deal with this
GadgetClicked:	MOVE.L	im_IAddress(A1),A0
		JSR	DealWithGadget
		BRA	WaitIDCMP

	;Main Window Was Closed, We call HideMainWindow which displays
	;LittleWindow and waits for Both Mouse Buttons Or Close Gadget
	;On Return D0 = 0 if Close Gadget was Clicked
	;             = 1 if Both Mouse Buttons Were Clicked
ClosedWindow:	JSR	HideMainWindow
		TST.L	D0
		BEQ	ClosedLittWin
		BRA	OpenMainWin
		
	;First Check if user released RMB without selecting an actual
	;item from the Menu (IDCMP_MENUPICK still occurs with this!)
MenuSelected:	CMP.W	#MENUNULL,im_Code(A1)
		BEQ	WaitIDCMP

		AND.W	#$7FF,im_Code(A1)	;Mask All But Menu/Item
		
	;Check if Item 1 (Hide) was selected treat same as Close Gadget
		CMP.W	#32,im_Code(A1)
		BEQ	ClosedWindow
		
	;Check if Item 2 (Quit) was selected
		CMP.W	#64,im_Code(A1)
		BEQ	SelectedQuit

	;If not then mouse was positioned on information only part of menu
	;so do nothing.
		BRA	WaitIDCMP
		
	;Close the Main Window
SelectedQuit:	MOVE.L	IntBase,A6
		MOVE.L	WindowBase,A0
		JSR	_LVOCloseWindow(A6)
				
ClosedLittWin:

	;Free the RdArgs Structure (If there is one)
Exit:		TST.L	RdArgs
		BEQ	NoRdArgsToFree
		MOVE.L	DosBase,A6
		MOVE.L	RdArgs,D1
		JSR	_LVOFreeArgs(A6)
NoRdArgsToFree:

	;Revert to Original Current Dir.
		MOVE.L	DosBase,A6
		MOVE.L	StartUpDirLock,D1
		JSR	_LVOCurrentDir(A6)

	;Free the Disk Object Structure
		TST.L	DiskObjectBase
		BEQ	NoDiskObjectMem
		MOVE.L	IconBase,A6
		MOVE.L	DiskObjectBase,A0
		JSR	_LVOFreeDiskObject(A6)
NoDiskObjectMem

	;Free the project's Disk Object Structure
		TST.L	DiskObjectBase2
		BEQ	NoDiskObjectMem2
		MOVE.L	IconBase,A6
		MOVE.L	DiskObjectBase2,A0
		JSR	_LVOFreeDiskObject(A6)
NoDiskObjectMem2


	;Close the intuition library
		MOVE.L	4,A6
		MOVE.L	IntBase,A1
		JSR	_LVOCloseLibrary(A6)
	
	;Close the dos library
		MOVE.L	4,A6
		MOVE.L	DosBase,A1
		JSR	_LVOCloseLibrary(A6)

	;Close the icon library
		MOVE.L	4,A6
		MOVE.L	IconBase,A1
		JSR	_LVOCloseLibrary(A6)
		
	;Close the diskfont library
		TST.L	DiskFontBase
		BEQ	NoDiskFontLib
		MOVE.L	4,A6
		MOVE.L	DiskFontBase,A1
		JSR	_LVOCloseLibrary(A6)	
NoDiskFontLib:

	;free the Menu File Memory
		TST.L	MenuFileBase
		BEQ	NoMenuMemory
		MOVE.L	MenuFileBase,A1
		MOVE.L	MenuFileSize,D0
		ADD.L	#2,D0
		JSR	_LVOFreeMem(A6)
NoMenuMemory:
	
	;Free the Gadgets Structure Memory
		TST.L	GadgetBase
		BEQ	NoGadStructure
		MOVE.L	GadgetBase,A1
		MOVE.L	GadMemSize,D0
		JSR	_LVOFreeMem(A6)
NoGadStructure:
		
	;Check if we need to tell WorkBench we have finished
		TST.L	Message
		BEQ	NoWbMessage

	;Yes, so Reply to the Workbench StartUp Message
		MOVE.L	4,A6
		JSR	_LVOForbid(A6)
		MOVE.L	Message,A1
		JSR	_LVOReplyMsg(A6)
NoWbMessage:		

	;Exit with return code of ZERO.
		CLR.L	D0
		RTS

MainWinOpenErr:	MOVE.L	#MainWinErrText,ErrorMessage
		JSR	ReportError
		BRA	Exit

		
		SECTION data,data
		
WindowDef:	DC.W	0		;X Position
Window_Y:	DC.W	0		;Y Position
WindowWidth:	DC.W	640		;Width
WindowHeight:	DC.W	0		;Height
		DC.B	1,2		;Detail / Block Pen
		DC.L	IDCMP_CLOSEWINDOW!IDCMP_MENUPICK!IDCMP_GADGETUP!IDCMP_INACTIVEWINDOW	;IDCMP flags
		DC.L	WFLG_ACTIVATE!WFLG_DRAGBAR!WFLG_DEPTHGADGET!WFLG_CLOSEGADGET!WFLG_GIMMEZEROZERO		;Window Flags
GadgetBase:	DC.L	0		;Gadget List
		DC.L	0		;Check Mark
		DC.L	WindowTitle	;Title Bar Text
		DC.L	0		;Screen
		DC.L	0		;Bitmap
		DC.W	0,0,0,0		;Min,Max dimensions
		DC.W	WBENCHSCREEN	;Window Type
		
TopazFont:	DC.L	TopazName
TopazHeight:	DC.W	8,0
		
ScreenFontSize:	DC.L	0	
	
WindowTitle:	DC.B	"GadGrid                                             ©1997 by Mike Archer",0
IconName:	DC.B	"icon.library",0
GraName:	DC.B	"graphics.library",0
IntName:	DC.B	"intuition.library",0
DosName:	DC.B	"dos.library",0
DiskFontName:	DC.B	"diskfont.library",0
TopazName:	DC.B	"topaz.font",0
MainWinErrText:	DC.B	"Could not open window!",0



		SECTION	bss,bss
		
GraBase:	DS.L	1		
DiskFontBase:	DS.L	1
IntBase:	DS.L	1
DosBase:	DS.L	1
IconBase:	DS.L	1
WindowBase:	DS.L	1	;Pointer to main window structure
FontBase:	DS.L	1
TitleBarSize:	DS.L	1

	INCLUDE	"ReadParameters.s"
	INCLUDE "CreateGadGrid.s"
	INCLUDE	"ReadMenuFile.s"
	INCLUDE "MenuDefinition.s"
	INCLUDE "HideMainWindow.s"
	INCLUDE	"ReportError.s"
	INCLUDE	"CalcWindowPos.s"
	INCLUDE	"StringToNumber.s"
	INCLUDE "DealWithGadget.s"
	INCLUDE	"CheckRunBack.s"
	INCLUDE	"ReadFileToolTypes.s"
	
	INCLUDE	"Workbench/icon_lib.i"
	