/* $VER: SendtoTurboText.rexx 1.4 (12.06.95)
   Copyright 1995 Soft-Logik Publishing Corporation
   May not be distributed without Soft-Logik Publishing Corporation's express written permission */

OPTIONS RESULTS
TRACE OFF

/* Make sure rexx support is opened */
IF ~SHOW('L','rexxsupport.library') THEN
   CALL ADDLIB('rexxsupport.library',0,-30)

ADDRESS 'PAGESTREAM'

/* If text is not selected, select the current article */
getcursor characterstart sID characterend eID
if sID=eID then 'selecttext all'

/* Export the article to a temp file */
'lockinterface true'
'exporttext file t:HotLinkedArticle filter ascii textcode PageStream status force'
/* Eventually will just make article external, but not this version */
/* 'editarticle external file t:HotLinkedArticle' */
'refresh wait'

/* set the default TurboText path */
ttxpath='TurboText:TTX'
preffile='PageStream3:SendtoTurboText.prefs'

/* If TurboText doesn't exist in the default path, check SendtoTurboText.prefs */
if ~exists(ttxpath) then do
	if open(.ifile, preffile, 'R') then do
		ttxpath=readln(.ifile)
		call close(.ifile)
		askuser='no'
		end
	else do
		askuser='yes'
	end
	/* If TurboText doesn't exist in the set pref path, ask the user where it is */
	if ~exists(ttxpath) | askuser='yes' then do
		getfile TITLE "'Please locate TurboText'" load path "'TurboText:'" file 'TTX' posbutton 'Ok' negbutton 'Cancel'
		if RC=10 then signal cancel
		ttxpath=result
		if open(.ifile, preffile, 'W') then do
			call writeln(.ifile, ttxpath)
			call close(.ifile)
		end
	end
end


/* Run TurboText */
ADDRESS COMMAND
'run >nil:' ttxpath' t:HotLinkedArticle'

/* Wait until the ARexx port shows up */
do 20
	if ~show(P, 'TURBOTEXT') then call delay(50)
end

ADDRESS 'TURBOTEXT'
do 20
	getport 'HotLinkedArticle'
	portname=result
	if portname~=result then call delay(50)
end


/* If the ARexx port didn't show up, alert the user and exit */
if ~show(P, 'TURBOTEXT') then do
	ADDRESS 'PAGESTREAM'
	allocarexxrequester '"Send to Editor Error!"' 368 59
	reqhandle=result
	addarexxgadget reqhandle EXIT 286 42 70 label "_Exit"
		Exithandle=result
	addarexxgadget reqhandle TEXT 8 10 352 border none string "'TurboText could not be started.'"
	doarexxrequester reqhandle
	freearexxrequester reqhandle
	'refresh continue'
	'lockinterface false'
	EXIT
end

/* Wait for the user to finish in TurboText */
address 'TURBOTEXT'
do forever
	if show(P, portname) then do
		getport 'HotLinkedArticle'
		if rc=0 then call delay(50)
		else signal update
	end
	else signal update
end

/* Update PageStream and exit */
UPDATE:
ADDRESS 'PAGESTREAM'
'screentofront'
'revealwindow current'
/* Eventually will just make article external, but not this version */
/* 'editarticle path t:HotLinkedArticle' */
/* 'editarticle internal' */

'inserttext file t:HotLinkedArticle filter ascii textcode PageStream'

/* deselect the text if rquired */
if sID=eID then selecttext none

signal cancel

CANCEL:
ADDRESS 'PAGESTREAM'
'refresh continue'
'lockinterface false'
ADDRESS COMMAND
'delete t:HotLinkedArticle >NIL:'
EXIT
