/*
 * empty.mod
 *
 * USAGE: empty.mod
 *
 * empty.mod is the skeleton of an OLE module. Containing all settings to
 * build a GUI, a message port and other...
 *
 * HISTORY:
 * v1.17	aligned with the new server design v1.10
 *
 * $(C): (1994, Rocco Coluccelli, Bologna)
 * $VER: empty.mod 1.17 (30.Nov.1994)
 */

OPTIONS RESULTS

/*
 *	Read the startup clip
 *	start point for gadgets, font dimension, commands ports, ...
 */
PARSE ARG oleclip
PARSE VALUE GETCLIP(oleclip) WITH jobID modID box.left box.top char.w char.h olewin oleport olehost userscreen userport olepipe locale config modstatus .

/*
 *	Configuration
 */
IF ~SHOW('C',config) THEN DO
	/*				var1 =	
	 *	default		...		
	 */				varN =	
	END

ELSE PARSE VALUE GETCLIP(config) WITH var1',' '...' ','varN','

	/*						par1 =
	 *	fixed parameters	...
	 */						parN =

/*
 *	Send commands to OLE server
 */
ADDRESS VALUE oleport

/*
 *	Open the module commands port
 *	In case of fault terminate current job and exit
 */
IF OPENPORT(olehost) == NULL() THEN DO
	ERROR jobID modID 1 olehost
	SETJOB jobID 'end'
	EXIT 10
	END

/*
 *	Wait for messages at control port until the end
 */
rt_TAG = 'rt_pubscrname=' || userscreen 'rtez_flags=ezreqf_centertext'
elab = 1
st = GUIGads()
DO UNTIL st = 'end'

	CALL WAITPKT(olehost)
	pkt = GETPKT(olehost)

	IF pkt == NULL() THEN ITERATE

	/*
	 *	Extract and execute each command.
	 *	(cmd) command ID
	 *	(argv) general argument strig, may be empty
	 *	(n0) the number of gadget to update
	 *	(nn) gadget to activate
	 */
	PARSE VALUE GETARG(pkt) WITH cmd argv
	PARSE VALUE GETARG(pkt,1) WITH n0 nn .

	SELECT

		WHEN cmd = 'UNICONIFY' THEN DO
			cmd jobID modID
			CALL Gadgets(8,1,g_gads)
		END

		/*
		 *	TODO: fix this section
		 */
		WHEN cmd = 'HELP' THEN DO
			NOP
			/*
			REQASK modID 'þHELP' || st
			*/
		END

		WHEN cmd = 'START' | cmd = 'QUIT' THEN DO
			st = 'end'
			elab = ~(cmd = 'QUIT')
		END

		OTHERWISE NOP

	END	/* end select commands */

	IF n0 ~= '' THEN DO
		CALL Gadgets(2,n0)
		IF nn ~= '' THEN CALL ActivateGadget(olewin,GAD.nn)
		CALL SETCLIP(config,var1','...','varN',')
		END

	CALL REPLY(pkt,0)

END	/* end do until */

CALL CLOSEPORT(olehost)

/*
 *	Quit command from the server, abort operations
 */
IF ~elab THEN EXIT 0

/*
 *	Remove all gadgets and let the server launch the next module
 */
ADDRESS VALUE oleport
SETJOB jobID 'end'


ADDRESS VALUE userport

	/***********************\
	*						*
	*	DATA  ELABORATION	*
	*						*
	\***********************/

ADDRESS VALUE oleport
SETJOB jobID modID + 1

EXIT 0


/*
 *	Send commands to host program
 */
HostCmd:
PARSE ARG cmd

	ADDRESS VALUE userport
	cmd
	ADDRESS

RETURN result


GetLocale: PROCEDURE EXPOSE locale
ARG strID

	strID = 'þ'strID'þ'; PARSE VALUE GETCLIP(locale) WITH (strID)text'Þ'

RETURN text


/*
 *	Update the progress indicator
 */
Complete:

	ADDRESS VALUE oleport
	COMPLETE jobID modID ARG(1)
	ADDRESS

RETURN


/*
 *	g_str.	= string
 *	g_xpos.	= x position
 *	g_ypos.	= y position
 *	g_type.	= gadget type	(1=text 2=button 3=string 4=boolean)
 *	g_len.	= length
 *	g_msg.	= command
 */
GUIGads:

	/*
	 *	offsets for the correct position
	 */
	g_offx. = 2;	g_offx.1 = 0;			g_offx.3 = 2
	g_offy. = 2;	g_offy.1 = char.h + 1;	g_offy.3 = 3
	/*
	 *	values to fix gadget dimensions
	 */
	g_wid. = 8;				g_wid.1 = 0;			g_wid.3 = 12
	g_hei. = char.h + 4;	g_hei.1 = char.h + 1;	g_hei.3 = char.h + 6
	/*
	 *	space between gadgets and lines
	 */
	g_sx = char.w % 2;	g_sy = char.h % 4
	/*
	 *	gadget already drawn
	 */
	g_onoff. = 0

	box.left = box.left + g_sx; box.top = box.top + 2 * g_sy
	box.w = 36 * char.w

	n = 1

	/*
	 *	Set ID number for each page of gadgets
	 */
	nmain = 1; nborder = 2; nlocale = 3; npaths = 4; nbases = 5

	n1.nmain = n
	x = box.left; y = box.top
	CALL IniGad(2,2,0,'SETUP' nborder,CENTER('Window borders',30))
	y = y + g_hei.3 + 2 * g_sy
	CALL IniGad(2,2,0,'SETUP' nlocale,CENTER('Update catalogs',30))
	y = y + g_hei.3 + 2 * g_sy
	CALL IniGad(2,2,0,'SETUP' nbases,CENTER('Object names',30))
	y = y + g_hei.3 + 2 * g_sy
	CALL IniGad(2,2,0,'SETUP' npaths,CENTER('Setup paths',30))
	box.h = y
	n2.nmain = n - 1

	n1.nborder = n
	y = box.top
	CALL IniGad(3,2,0,'BORDER' bt '%1' n '%2%g',win.bt,3)
	CALL IniGad(1,0,1,,'Top')
	y = y + g_hei.3 + 4 * g_sy
	CALL IniGad(1,2,0,,'Gadgets area')
	y = y + g_hei.3 + 4 * g_sy
	CALL IniGad(1,1,0,,' Left')
	CALL IniGad(3,0,1,'BORDER' bl '%1' n '%2%g',win.bl,3)
	n1 = IniGad(3,0,0,'BORDER' br '%1' n '%2%g',win.br,3)
	n2 = IniGad(1,0,1,,'Right')
	x = box.left + box.w - x; g_xpos.n1 = g_xpos.n1 + x; g_xpos.n2 = g_xpos.n2 + x
	y = y + g_hei.3 + 4 * g_sy
	CALL IniGad(3,2,0,'BORDER' bb '%1' n '%2%g',win.bb,3)
	CALL IniGad(1,0,1,,'Bottom')
	box.h = MAX(box.h,y)
	n2.nborder = n - 1

	n1.nlocale = n
	y = box.top
	CALL IniFileGad('REQFILE ED',olefile.ed,'Choose an ASCII editor')
	y = y + g_hei.3 + 2 * g_sy
	CALL IniFileGad('REQFILE CT',olefile.ct,'Choose a source catalog')
	y = y + g_hei.3 + 2 * g_sy
	CALL IniGad(2,1,0,'CATALOG EDIT','Edit')
	CALL IniGad(2,0,1,'CATALOG MAKE','Compile')
	box.h = MAX(box.h,y)
	n2.nlocale = n - 1

	n1.npaths = n
	y = box.top
	CALL IniGad(1,2,0,,'Under construction')
	box.h = MAX(box.h,y)
	n2.npaths = n - 1

	n1.nbases = n
	y = box.top
	CALL IniGad(1,2,0,,'Under construction')
	box.h = MAX(box.h,y)
	n2.nbases = n - 1

	y = box.h + g_hei.3 + 2 * g_sy
	CALL IniGad(2,1,0,'OKAY','Okay')
	g_gads = IniGad(2,3,0,'HELP','Help')

	box.h = y + g_hei.2 + 2 * g_sy - box.top

	WINDOW jobID modID (box.w + 2 * g_sx) (box.h + 2 * g_sy) 1 1
	CALL Gadgets(4,g_gads - 1,g_gads)
	CALL Gadgets(4,n1.nmain,n2.nmain)

RETURN nmain


/*
 *	Gadgets(mode,gad1,...)
 *	(mode) 1 draw new gadgets		random mode (gad1,gad3,gad6,gad2)
 *	(mode) 2 update old gadgets			"
 *	(mode) 3 remove gadgets				"
 *	(mode) 4 draw new gadgets		range mode (gad1 TO gad5)
 *	(mode) 5 update old gadgets			"
 *	(mode) 6 remove gadgets				"
 *	(mode) 7 quick remove gadgets		"
 */
Gadgets:

	IF ARG(1) < 4 THEN
		DO i = 2 TO ARG(); n = ARG(i)
			IF ARG(1) ~= 1 THEN CALL DelGad(n,g_type.n)
			IF ARG(1) ~= 3 THEN CALL NewGad(n,g_type.n)
		END

	ELSE IF ARG(1) < 7 THEN
		DO n = ARG(2) TO ARG(3)
			IF ARG(1) ~= 4 THEN CALL DelGad(n,g_type.n)
			IF ARG(1) ~= 6 THEN CALL NewGad(n,g_type.n)
		END

	ELSE IF ARG(1) = 8 THEN
		DO n = ARG(2) TO ARG(3)
			IF g_onoff.n THEN CALL NewGad(n,g_type.n)
		END

	ELSE DO
		DO n = ARG(2) TO ARG(3)
			g_onoff.n = 0
			IF g_type.n ~= 1 THEN CALL RemoveGadget(olewin,GAD.n)
		END
		CALL SetAPen(olewin,0)
		CALL RectFill(olewin,box.left,box.top,box.left + box.w,box.top + box.h)
		CALL RefreshGadgets(olewin)
		END
RETURN


DelGad:
PARSE ARG n,t

	g_onoff.n = 0

	IF t ~= 1 THEN CALL RemoveGadget(olewin,GAD.n)

	x = g_xpos.n - g_offx.t; y = g_ypos.n - g_offy.t
	CALL SetAPen(olewin,0)
	CALL RectFill(olewin,x,y,x + g_len.n,y + g_hei.t)

RETURN


NewGad:
PARSE ARG n,t

	g_onoff.n = 1

	IF t = 2 THEN
		CALL AddGadGet(olewin,g_xpos.n,g_ypos.n,GAD.n,g_str.n,g_msg.n)

	ELSE IF t = 3 THEN
		CALL AddGadGet(olewin,g_xpos.n,g_ypos.n,GAD.n,g_str.n,g_msg.n,g_len.n - 4,"RIDGEBORDER")

	ELSE IF t = 4 THEN
		CALL AddGadGet(olewin,g_xpos.n,g_ypos.n,GAD.n,D2C(32 + g_str.n * 183),g_msg.n)

	ELSE DO
		CALL SetAPen(olewin,1)
		CALL Move(olewin,g_xpos.n,g_ypos.n)
		CALL Text(olewin,g_str.n)
		END
RETURN


/*
 * n = IniGad(t,na,nx,msg,str,...)
 * gadget of (t) type
 * (na) = 0,1,2,3 closer,left,center or right
 * (nx) = 0,1,... blanks
 * (msg) command
 * (str) text of gadget
 */
IniGad:
PARSE ARG t,na,nx,g_msg.n,g_str.n,var

	x = x + nx * g_sx

	IF t = 3 & var > 0 THEN
		g_len.n = var * char.w + g_wid.t
	ELSE IF t = 3 THEN
		g_len.n = box.left + box.w - x
	ELSE
		g_len.n = LENGTH(g_str.n) * char.w + g_wid.t

	IF na > 0 THEN x = box.left + (na - 1) * (box.w - g_len.n) % 2 + nx * g_sx

	g_xpos.n = x + g_offx.t; g_ypos.n = y + g_offy.t; g_type.n = t
	x = x + g_len.n
	n = n + 1

	IF t = 4 THEN CALL IniGad(1,0,1,,var)

RETURN n - 1


IniFileGad:
PARSE ARG msg,str,txt

	CALL IniGad(1,1,0,,txt)
	y = y + g_hei.1 + 2 * g_sy
	CALL IniGad(2,1,0,msg '%1' (n + 1),'F')
	CALL IniGad(3,0,1,msg '%1' n '%2%g',str)

RETURN n - 1
