/** $VER: scmsg_rendezvous 1.1 (20.12.92)
 **                        1.0 (16.12.92)
 **
 ** A `rendezvous' rexx script for SAS/C's scmsg browser and TurboText.
 ** There's no need to (nor is it intended) run this script directly, as
 ** the supplied SCMSG configuration file will do it for you, if SCMSG
 ** cannot locate the SCMSG_TTX_RENDEZVOUS message port.  Nice job, SAS.
 **
 ** Written by Kari Sutela
 **/

options results

/*
 * We absolutely need rexxsupport.library for this to work.
 */
if ~Show('L','rexxsupport.library') then do
	if ~AddLib('rexxsupport.library',0,-30,0) then do
		say 'Cannot AddLib rexxsupport.library'
		exit 20
	end
end
/*
 * We need rexxarplib.library for some warning/error messages.
 */
if ~Show('L','rexxarplib.library') then do
	if ~AddLib('rexxarplib.library',0,-30,0) then do
		say 'Cannot AddLib rexxarplib.library (version 3.0)'
		exit 20
	end
end

/*
 * Open the rendezvous port
 */
port = 'SCMSG_TTX_RENDEZVOUS'
if ~OpenPort(port) then do
	say 'Cannot OpenPort('port')!'
	exit 20
end

goaway=0
do until goaway
	call WaitPkt(port)
	packet = GetPkt(port)
	if packet = Null() then do
		iterate
	end
	cmd = GetArg(packet,0)
	parse value cmd WITH cmd param '0d'x .
	cmd = upper(cmd)
	select
		when cmd = 'QUIT' then do
			call Reply(packet,0)
			goaway = 1
		end
		when cmd = 'GOTOFILE' then do
			/* GOTOLINE handles opening the file, too */
			call Reply(packet,0)
		end
		when cmd = 'GOTOLINE' then do
			/*
			 * The filename might contain blanks in which case SCMSG seems to
			 * automatically enclose it in quotes (this is not a documented
			 * feature, though).
			 */
			if Words(param) > 2 then
				parse value param WITH '"'file'"' line .
			else
				parse value param WITH file line .
			call Reply(packet,0)
			call TTX_it(file,line)
		end
		otherwise
			call Reply(packet,10)
	end
end
exit /* That's it! */

tail: PROCEDURE

	parse arg filename

	sep = lastpos('/',filename)
	if sep = 0 then
		sep = lastpos(':',filename)
	if sep = 0 then
		return filename
	return substr(filename,sep+1)

activateline: PROCEDURE

	parse arg port, line

	address value port
	'Move' line
	/* Uniconify window to ensure visibility */
	'GetWindowInfo'
	parse value result WITH icon wleft wtop wwidth wheight .
	if icon = 'ON' then do
		'IconifyWindow OFF'
		'GetWindowInfo'
		parse value result WITH icon wleft wtop wwidth wheight .
	end
	'CenterView'
	'GetScreenInfo'
	parse value result WITH sleft stop stotalwidth stotalheight . svisiblewidth svisibleheight .
	/* Is it mostly offscreen? */
	offscreen = 0
	sleft = -sleft; stop = -stop
	wright = wleft+wwidth; wbot = wtop+wheight;
	/* Calculate visible area */
	if wleft > sleft then
		visiblex = max((sleft + svisiblewidth) - wleft, 0)
	else
		visiblex = max((wleft + wwidth) - sleft, 0)
	if wtop > stop then
		visibley = max((stop + svisibleheight) - wtop, 0)
	else
		visibley = max((wtop + wheight) - stop, 0)
	visible = visiblex*visibley
	wsize = wwidth*wheight
	/* Let's say that it's offscreen if 1/2 or less is visible */
	if visible/wsize <= 0.5 then
		call Request(sleft,stop,'scmsg_rendezvous WARNING:\  TTX window mostly offscreen!',,,'OK')
	/* TTX window to front */
	'Window2Front'
	/* And scmsg over it */
	address SC_SCMSG 'show'
	/* Activate TTX window */
	'ActivateWindow'
	return


TTX_it: PROCEDURE

	parse arg filename, line

	filepart = tail(filename)
	/* TTX's GetPort does not properly handle non-unique documents */
	address TURBOTEXT
	'GetDocuments'
	docs = result
	do until docs = ''
		parse value docs WITH '"'docfile'"' port docs
		if docfile = filepart then do
			address value port
			'GetFilePath'
			docfile = result
			if docfile = filename then do
				call activateline(port,line)
				return
			end
		end
	end

	/* Got here, so we should open the file */
	address TURBOTEXT
	'OpenDoc NAME' filename
	if rc ~= 0 then do
		say 'Error' rc '('turbotext.lasterror') while opening "'filename'"'
		return
	end
	else do
		port = result
		call activateline(port,line)
		return
	end
