		
		SECTION	CreateGadGrid,CODE
		
CreateGadGrid:
	;We need a RastPort for using the TextLength() function
	;lets create a dummy one.
		MOVE.L	GraBase,A6
		LEA	DummyRastPort,A1
		JSR	_LVOInitRastPort(A6)
		
	;Set the font for the dummy rastport to our font
		MOVE.L	FontBase,A0
		LEA	DummyRastPort,A1
		JSR	_LVOSetFont(A6)

	;Calculate the height of the Gadgets
		MOVE.W	TopazHeight,GadHeight
		ADD.W	#4,GadHeight
		
	;Calculate the width of the Gadgets
		CLR.L	D0
		MOVE.W	WindowWidth,D0
		MOVE.W	GadColumns,D1			
		SUB.W	#8,D0
		DIVU	D1,D0
		MOVE.W	D0,Gadwidth
		
	;Get number of pixels extra (Remainder of Division)
		SWAP	D0
		MOVE.W	D0,GadAdjust
		
	;Put the Gadgets Height -1 into the Border Coordinates Array
		MOVE.W	GadHeight,D0
		SUB.W	#1,D0
		MOVE.W	D0,TopLeft+2
		MOVE.W	D0,BottomRight+2
		MOVE.W	D0,BottomRight+6
		
	;Add the Gadget Width into the Border Coordinates Array
		MOVE.W	Gadwidth,D0
		ADD.W	D0,TopLeft+8
		ADD.W	D0,BottomRight+4
		ADD.W	D0,BottomRight+8

	;Calculate How Many Gadgets We Need
		MOVE.L	MenuFileBase,A0
		CLR.L	D0
		SUB.L	#1,A0
		
CountGadsLoop:	ADD.L	#1,A0
		CMP.B	#1,(A0)		;End of File
		BEQ	DoneCounting
		CMP.B	#10,(A0)	;Check for a Line Feed
		BNE	CountGadsLoop
		CMP.B	#'#',1(A0)	;Check for a '#' following it
		BNE	CountGadsLoop
		ADD.L	#1,D0		;Got one, so add one to counter
		BRA	CountGadsLoop	
	
DoneCounting:	MOVE.W	D0,NoOfGads

	;Calculate How Many Gadget Rows That Is
		ADD.W	GadColumns,D0
		SUB.W	#1,D0
		DIVU	GadColumns,D0
		MOVE.W	D0,GadsDown

	;Work Out the Correct Window Height
CalcWinHeight:	MOVE.W	GadHeight,D0
		MULU.W	GadsDown,D0
		ADD.W	TitleBarSize,D0	;Allow for Height of Title Bar
		ADD.W	#1,D0		;which is GadHieght+1
		MOVE.W	D0,WindowHeight

	;Is WindowHeight bigger than workbench screen ?
		MOVE.L	IntBase,A6
		JSR	_LVOOpenWorkBench(A6)
		MOVE.L	D0,A0
		MOVE.W	WindowHeight,D0
		MOVE.W	sc_Height(A0),D1
		CMP.W	D1,D0
		BMI	NotTooBig

	;Calculate how many Rows of Gadgets will fit on screen
		CLR.L	D0
		MOVE.W	sc_Height(A0),D0
		SUB.W	TitleBarSize,D0		;Allow for the Title bar
		SUB.W	#1,D0	
		DIVU.W	GadHeight,D0
		MOVE.W	D0,GadsDown
		
	;Calculate how many gadgets to use (can't fit all on screen!)
		MULU.W	GadColumns,D0
		MOVE.W	D0,NoOfGads
		
	;Recalculate the window height
		BRA	CalcWinHeight
		
		
	;Calculate Total Memory Required
NotTooBig:	MOVE.W	NoOfGads,D0
		MULU	#64,D0
		MOVE.L	D0,GadMemSize
		
	;Allocate Memory for the Gadgets
		MOVE.L	4,A6
		MOVE.L	GadMemSize,D0
		MOVE.L	#MEMF_PUBLIC!MEMF_CLEAR,D1
		JSR	_LVOAllocMem(A6)
		MOVE.L	D0,GadgetBase
		BEQ	CGG_OutOfMem
	
	;Initialise the pointers
		MOVE.L	GadgetBase,A0		;Pointer to Gadget
		MOVE.L	A0,A1
		ADD.L	#gg_SIZEOF,A1		;Point A1 to IntuiText
		MOVE.W	#0,D0			;Gadget Column
		MOVE.W	#0,D1			;Gadget Y Position
		MOVE.W	#0,D2			;Gadget ID
		MOVE.W	#0,D4			;Adjust Value for X coord
		MOVE.L	MenuFileBase,A2		;Pointer to Menu File
	
	;Find the first Gadget Title in the Menu File		
CGG_Loop:	CMP.B	#10,(A2)+
		BEQ	CGG_Skip
		CMP.B	#0,-1(A2)
		BNE	CGG_Loop

CGG_Skip:	CMP.B	#'#',(A2)
		BNE	CGG_Loop
		ADD.L	#1,A2
		
		
	;Now find the LF and replace with NULL
		MOVE.L	A2,A3
CGG_Loop2:	CMP.B	#10,(A3)+
		BNE	CGG_Loop2
		MOVE.B	#0,-1(A3)
		
	;Store away all the useful registers
		MOVEM.L	A0-A2/D0-D4,-(A7)
		
	;Lets calculate how many characters of text we can fit in
	;the gadget box. (Done each time to allow for Prop Fonts)
		MOVE.L	#1,D0	;Start with 1 character
		MOVE.L	A2,A0	;A0 = Start of Gadget Text
		MOVE.L	#0,D5	;TextLength() result store

	;Check for a NULL byte
CGG_Loop3:	TST.B	-1(A0,D0)
		BEQ	ReachedNullByte
		
	;Get length of text in pixels
		MOVEM.L	A0/D0/D5,-(A7)	;Store registers first
		MOVE.L	GraBase,A6
		LEA	DummyRastPort,A1
		JSR	_LVOTextLength(A6)	
		MOVE.L	D0,D1		;Length in pixels
		ADD.L	#4,D1		;+4 for Gadget Border
		MOVEM.L	(A7)+,A0/D0/D5	;restore registers

	;Check if text string has passed gadget width yet
		MOVE.W	Gadwidth,D7
		CMP.W	D1,D7
		BMI	ReachedLength

	;Store the TextLength() result, we need penultimate value at end
		MOVE.L	D1,D5
		
	;Increase D0 by 1 and try again
		ADD.L	#1,D0
		BRA	CGG_Loop3
	
	;We reached max length before a NULL so fill rest of text with NULL
ReachedLength:	ADD.L	D0,A0
		SUB	#1,A0
		TST.B	(A0)		;May have reached a NULL and the
		BEQ	ReachedNullByte	;max length at same time!
NullEmOutLoop:	MOVE.B	#0,(A0)+
		TST.B	(A0)
		BNE	NullEmOutLoop

	;Now we work out the gadget texts X-Position
ReachedNullByte:MOVE.W	Gadwidth,D6
		SUB.W	D5,D6
		LSR.W	#1,D6		;Half of remaining pixels
		ADD.W	#2,D6		;Move past gadget border
		
	;Restore all the useful registser values
		MOVEM.L	(A7)+,A0-A2/D0-D4
		
		MOVE.W	D0,-(A7)
		MULU.W	Gadwidth,D0
		ADD.W	D4,D0		;Adjustment to X-Offset
		
		
	;Initialise the current Gadget's Structure
		MOVE.L	A0,gg_NextGadget(A0)
		ADD.L	#64,gg_NextGadget(A0)
		MOVE.W	D0,gg_LeftEdge(A0)
		MOVE.W	D1,gg_TopEdge(A0)	
		MOVE.W	Gadwidth,gg_Width(A0)	
		MOVE.W	GadHeight,gg_Height(A0)
		MOVE.W	#GADGHIMAGE,gg_Flags(A0)
		MOVE.W	#RELVERIFY,gg_Activation(A0)
		MOVE.W	#BOOLGADGET,gg_GadgetType(A0)
		MOVE.L	#NormalBorder,gg_GadgetRender(A0)
		MOVE.L	#SelectBorder,gg_SelectRender(A0)
		MOVE.L	A1,gg_GadgetText(A0)
		MOVE.W	D2,gg_GadgetID(A0)

		MOVE.W	(A7)+,D0
		
	;Initialise the current Gadget's IntuiText Structure
		MOVE.B	#1,it_FrontPen(A1)
		MOVE.B	#0,it_BackPen(A1)
		MOVE.B	#RP_JAM1,it_DrawMode(A1)
		MOVE.W	D6,it_LeftEdge(A1)
		MOVE.W	#2,it_TopEdge(A1)
		MOVE.L	#TopazFont,it_ITextFont(A1)
		MOVE.L	A2,it_IText(A1)

	;Check if any spare pixels left on right
		CMP.W	GadAdjust,D4
		BEQ	NoSparePixels		
	
	;If so bump up the X Coordinate by 1
		ADD.W	#1,D4
		
	;Increase the Gadget ID pointer
NoSparePixels:	ADD.W	#1,D2
		CMP.W	NoOfGads,D2
		BEQ	CGG_Done
		
	;Increase The Structure Pointers
		ADD.L	#64,A0
		ADD.L	#64,A1

	;Increase The Gadget Column Number
		ADD.W	#1,D0
		CMP.W	GadColumns,D0
		BNE	CGG_Loop
		
	;Increase the Y Position
		ADD.W	GadHeight,D1
		CLR.W	D0
		CLR.W	D4
		BRA	CGG_Loop		

	;Ensure last gadet is marked as the last.		
CGG_Done:	MOVE.L	#0,gg_NextGadget(A0)

		
CGG_OutOfMem:	RTS

		SECTION	CreateGadGrid,DATA

GadHeight:	DC.W	20		;Height of the Gadget
GadsDown:	DC.W	2		;Number of Gadget Rows
GadColumns:	DC.W	10		;Number of Gadget Columns
NoOfGads:	DC.W	0		;Total Number of Gadgets
GadMemSize:	DC.L	0		;Size of Memory for Structures
Gadwidth:	DC.W	63		;Width of a Gadget in Pixels
GadAdjust:	DC.W	0		;How many pixels are left over
					;on right of gadgets in window

NormalBorder:	DC.W	0,0		;Position
		DC.B	2,0		;Detail / Block Pen
		DC.B	RP_JAM1		;Draw Mode
		DC.B	3		;Number of Coords
		DC.L	TopLeft		;Coords for Top Left of Box
		DC.L	NormalBorder2	;Next Border Structure
		
NormalBorder2:	DC.W	0,0		;Position
		DC.B	1,0		;Detail / Block Pen
		DC.B	RP_JAM1		;Draw Mode
		DC.B	3		;Number of Coords
		DC.L	BottomRight	;Coords for Bottom Right of Box
		DC.L	0		;No More Border Structures

SelectBorder:	DC.W	0,0		;Position
		DC.B	1,0		;Detail / Block Pen
		DC.B	RP_JAM1		;Draw Mode
		DC.B	3		;Number of Coords
		DC.L	TopLeft		;Coords for Top Left of Box
		DC.L	SelectBorder2	;Next Border Structure
		
SelectBorder2:	DC.W	0,0		;Position
		DC.B	2,0		;Detail / Block Pen
		DC.B	RP_JAM1		;Draw Mode
		DC.B	3		;Number of Coords
		DC.L	BottomRight	;Coords for Bottom Right of Box
		DC.L	0		;No More Border Structures

;Note the 255 Values in this array are replaced with the GadgetHeight -1
;at the start of this INCLUDE file.

;The negative values have the Gadget Width added to them.

TopLeft:	DC.W	0,255, 0,0, -2,0
		
BottomRight:	DC.W	1,255, -1,255, -1,0

		SECTION	DummyRastPort,BSS
		
DummyRastPort:	DS.B	100
