/* Move_Line.rexx */
/* Move the current line (at cursor location) using numeric entry. */
/* Move relative or absolute distance on the x and y axes. */

/* Begin numeric entry with "+" or "-" to move from current x and y. */
/* Enter numbers only to move to a specified x/y location on screen. */
/* Exception: [x or y = 0], will constrain movement on that axis. */
/* Ex. x=-10, y=0: Move line 10 pixels to the left. */

/* The current line x/y position is used as the default values for the next */
/* invocation of 'Move_Line', and can be used to UNDO the previous move. */


/* Requires Toaster_CG v4.03 */
/* By Ross Fenmore © 1995 Light Source Video & Graphics */

SIGNAL ON ERROR
SIGNAL ON SYNTAX

CALL addlib(CG_AREXX,0)

pagetype=get_page('type')
IF (pagetype='Scroll')|(pagetype='Crawl') THEN DO
	call req_tell("Move_Line is unavailable on a "||pagetype||" page")
	CALL OUTAHERE
END

type=get_line('TYPE')
text=get_line()
IF (type='Text')&(text="") THEN DO
	CALL req_tell("The entry cursor must be on a valid line")
	CALL OUTAHERE
END

CALL pickpage(NOT)
CALL req_bar("Move a line...")

tempfile='ENV:Move_Line.state'
version='Move_Line v1.0'

IF (exists(tempfile)) THEN DO
	IF (~open(state, tempfile, 'R')) THEN BREAK
	IF (readln(state) ~= version) THEN BREAK
	last_x=readln(state)
	last_y=readln(state)
	CALL close state
END

coords=get_line(spot)
PARSE var coords old_x old_y .

IF last_x="LAST_X" | last_x="" THEN last_x=old_x
IF last_y="LAST_Y" | last_y="" THEN last_y=old_y

new_x=req_string("Move X: Enter absolute or relative (+ or -) value",last_x)
new_x=compress(new_x)
IF new_x="" THEN new_x=last_x

SELECT
	WHEN (left(new_x,1)='+') THEN DO
		new_x=right(new_x,length(new_x)-1)

		IF ~datatype(new_x,'N') THEN DO
			CALL req_tell("You must enter a number for X.")
			CALL OUTAHERE
		END

		new_x=old_x+new_x
	END

	WHEN (left(new_x,1)='-') THEN DO
		new_x=right(new_x,length(new_x)-1)

		IF ~datatype(new_x,'N') THEN DO
			CALL req_tell("You must enter a number for X.")
			CALL OUTAHERE
		END


		new_x=old_x-new_x
	END

	OTHERWISE DO
		IF ~datatype(new_x,'N') THEN DO
			CALL req_tell("You must enter a number for X.")
			CALL OUTAHERE
		END

		IF new_x=0 THEN new_x=old_x
	END
END

linewidth=getlinewidth()
IF new_x<0 | (new_x+linewidth)>752 THEN DO
	CALL req_tell("Line cannot be moved to this x-position")
	CALL OUTAHERE
END

new_y=req_string("Move Y: Enter absolute or relative (+ or -) value",last_y)
new_y=compress(new_y)
IF new_y="" THEN new_y=last_y

SELECT
	WHEN (left(new_y,1)='+') THEN DO
		new_y=right(new_y,length(new_y)-1)

		IF ~datatype(new_y,'N') THEN DO
			CALL req_tell("You must enter a number for Y.")
			CALL OUTAHERE
		END

		new_y=old_y+new_y
	END

	WHEN (left(new_y,1)='-') THEN DO
		new_y=right(new_y,length(new_y)-1)

		IF ~datatype(new_y,'N') THEN DO
			CALL req_tell("You must enter a number for Y.")
			CALL OUTAHERE
		END


		new_y=old_y-new_y
	END

	OTHERWISE DO
		IF ~datatype(new_y,'N') THEN DO
			CALL req_tell("You must enter a number for Y.")
			CALL OUTAHERE
		END

		IF new_y=0 THEN new_y=old_y
	END
END

lineheight=get_line(TALL)
IF new_y<0 | (new_y+lineheight)>480 THEN DO
	CALL req_tell("Line cannot be moved to this y-position")
	CALL OUTAHERE
END


/* CALL req_tell("NEW_X = "||'"'new_x'"',"NEW_Y = "||'"'new_y'"') */

CALL pickline()
CALL set_line(SPOT,new_x,new_y)
CALL pickpage(NOT)

IF (open(outfile, tempfile, 'W')) THEN DO
	CALL writeln outfile, version
	CALL writeln outfile, strip(old_x)
	CALL writeln outfile, strip(old_y)
	CALL close outfile
END

CALL OUTAHERE
EXIT


/* Calculate the width of current line */

GETLINEWIDTH: PROCEDURE

type=get_line('type')

IF type='Text' THEN DO
	height=get_line(tall)
	length=get_line(size)
	restorespot=get_char(spot)

	linewidth=0
	current_char=1

	DO WHILE current_char <= length
		CALL set_char(spot,current_char)
		charwidth=get_char(size)
		linewidth=linewidth+charwidth+1
		current_char=current_char+1
	END

	CALL set_char(spot,restorespot+1)
END

ELSE DO
	boxsize=get_rect(size)
	PARSE var boxsize linewidth h
END

RETURN linewidth

OUTAHERE:
CALL req_bar("Toaster CG")
CALL remlib(CG_AREXX)
EXIT

ERROR:
SYNTAX:
errorcode = rc
line=SIGL
text='"'errortext(errorcode)'"'
call req_tell("ERROR= "||errorcode,"On line# "||line,text)
exit
