/* Creates a grid in the current window.
$VER: 1.02e (© R.Florac, 9 août 1998) */

options results

signal on error
signal on syntax

'WWIDTH(-1)'; lt = result
'WHEIGHT(-1)'; ht=result
clip=-1
FIRSTSEL; obj=result
if obj>0 then do
    'TYPE(FIRSTSEL)'; type=result
    if type=22 then do
	'CLIPUNIT(5)'; clip=result
	'COORDS(FIRSTSEL)'; coords=result
	PARSE VAR coords x0 ',' y0 ',' x1 ',' y1
	xg=minima(x0,x1); xd=maxima(x0,x1)
	yh=minima(y0,y1); yb=maxima(y0,y1)
	l=xd-x0+1; h=yb-y0+1
	'NEXTSEL('obj')'; obj=result
    end
end
else obj=1

if obj>0 then do
    'MESSAGE("Draw and select a"+CHR(10)+"box, the grid will"+CHR(10)+"be included in it.")'
    call quitter
end

'ASK("Horizontal axis"+CHR(10)+"Number of decades?")'
ndh = result
if ndh<=0 then call quitter
'SELECT("Scale type"+CHR(10)+"1- Linear"+CHR(10)+"2- Logarithm"+CHR(10)+"3- Antilog")'
tt=result
y1=y0+h
x1=x0+ndh*(l%ndh)

'SAVEALL(-1)'
if clip>=0 then 'MENU("Cut")'
/* Vertical lines */
select
    when tt=1 then do
	do i=1 to ndh
	    x2 = (x0)+i*(l/ndh)
	    x2 = x2%1
	    'DRAWMODE(-1)'
	    do c=1 to 9
		xc = x2-(l/ndh)/10*c
		xc=xc%1
		'DRAW('xc','y0','xc','y1')'
	    end
	    'DRAWMODE(-2):DRAW('x2','y1','x2','y0')'
	end
    end
    when tt=2 then do
	if ~show('L','rexxmathlib.library') then
	   call addlib('rexxmathlib.library',0,-30)
	x2=x0
	do i=1 to ndh
	    'DRAWMODE(-1)'
	    do c=2 to 9
		xc=(l/ndh)*log10(c)
		xc=(x2+xc)%1
		'DRAW('xc','y0','xc','y1')'
	    end
	    x2 = (x0)+i*(l/ndh)
	    x2 = x2%1
	    'DRAWMODE(-2):DRAW('x2','y1','x2','y0')'
	end
    end
    when tt=3 then do
	if ~show('L','rexxmathlib.library') then
	   call addlib('rexxmathlib.library',0,-30)
	x2=x1
	do i=1 to ndh
	    'DRAWMODE(-2):DRAW('x2','y1','x2','y0')'
	    'DRAWMODE(-1)'
	    do c=2 to 9
		xc=(l/ndh)*log10(c)
		xc=(x2-xc)%1
		'DRAW('xc','y0','xc','y1')'
	    end
	    x2 = (x1)-i*(l/ndh)
	    x2 = x2%1
	end
    end
    otherwise call quitter
end

'ASK("Vertical axis"+CHR(10)+"Number of decades?")'
ndv = result
if ndv<=0 then call quitter

y1=y0+h
x1=x0+ndh*(l%ndh)
/* Drawing borders */
'DRAWMODE(-2):DRAW('x0','y0','x1','y0'):DRAW('x0','y1','x0','y0')'

'SELECT("Scale type"+CHR(10)+"1- Linear"+CHR(10)+"2- Logarithm"+CHR(10)+"3- Antilog")'
tt=result

/* Horizontal lines */
select
    when tt=1 then do
	do i=1 to ndv
	    y2 = (y0)+i*(h/ndv)
	    y2 = y2%1
	    'DRAWMODE(-1)'
	    do c=1 to 9
		yc = y2-(h/ndv)/10*c
		yc=yc%1
		'DRAW('x0','yc','x1','yc')'
	    end
	    'DRAWMODE(-2):DRAW('x0','y2','x1','y2')'
	end
    end
    when tt=2 then do
	if ~show('L','rexxmathlib.library') then
	   call addlib('rexxmathlib.library',0,-30)
	y2=y1
	do i=1 to ndv
	    'DRAWMODE(-2):DRAW('x0','y2','x1','y2'):DRAWMODE(-1)'
	    do c=2 to 9
		yc=(h/ndv)*log10(c)
		yc=(y2-yc)%1
		'DRAW('x0','yc','x1','yc')'
	    end
	    y2 = y1-i*(h/ndv)
	    y2 = y2%1
	end
    end
    when tt=3 then do
	if ~show('L','rexxmathlib.library') then
	   call addlib('rexxmathlib.library',0,-30)

	y2=y0

	do i=1 to ndv
	    'DRAWMODE(-1)'
	    do c=2 to 9
		yc=(h/ndv)*log10(c)
		yc=(y2+yc)%1
		'DRAW('x0','yc','x1','yc')'
	    end
	    y2 = (y0)+i*(h/ndv)
	    y2 = y2%1
	    'DRAWMODE(-2):DRAW('x0','y2','x1','y2')'
	end
    end
    otherwise call quitter
end
call quitter

minima: procedure
    parse arg v1,v2
    if v1<v2 then return v1
    return v2
end

maxima: procedure
    parse arg v1,v2
    if v1>v2 then return v1
    return v2
end

quitter: procedure expose clip
    if clip>=0 then 'CLIPUNIT('clip')'
    exit

syntax:
erreur=RC
'MESSAGE("Script grid.AmiCAD"+CHR(10)+"Syntax error"+CHR(10)+"in line 'SIGL'"+CHR(10)+"'errortext(erreur)'")'
exit

error:
'MESSAGE("Script grid.AmiCAD"+CHR(10)+"Error in line 'SIGL'")'
call quitter
