#! /bin/bash

# iotest.sh
# input-output test for RAPP

cat << EOF
::BEGIN
<?xml version="1.0" encoding="iso-8859-1"?>
<!--
	iotest.xml - input-output test
-->
<xhpd version="1.0">

	<!--{{{  property sets-->
	<propset id="cpref">
		<property name="background" value="@background" />
		<property name="foreground" value="@foreground" />
		<property name="highlight" value="@highlight" />
		<property name="shadow" value="@shadow" />
		<property name="iecolor" value="@extra" />
		<property name="tentry" value="@tentry" />
	</propset>
	<propset id="selcpref">
		<property name="sel-background" value="@selback" />
		<property name="sel-foreground" value="@selfore" />
	</propset>
	<propset id="helvfont">
		<property name="font" value="-*-helvetica-medium-r-*-*-12-*-*-*-*-*-iso8859-*" />
	</propset>
	<propset id="helvfont-small">
		<property name="font" value="-*-helvetica-medium-r-*-*-10-*-*-*-*-*-iso8859-*" />
	</propset>
	<propset id="helvbold">
		<property name="font" value="-adobe-helvetica-bold-r-*-*-12-*-*-*-*-*-*-*" />
	</propset>
	<!--}}}-->

	<!--{{{  iotest:UWGWindow-->
	<gadget type="UWGWindow" name="iotest">
		<property name="caption" value="iotest" />
		<property name="icon-name" value="iotest" />
		<property name="width" value="512" />
		<property name="height" value="384" />
		<property name="position" value="user" />
		<property name="style" value="border" />
		<property name="visible" value="true" />
		<property set="cpref" />

		<!--{{{  xpanel:UWGPanel-->
		<gadget type="UWGPanel" name="xpanel">
			<property name="geometry" value="0,0,512,384" />
			<property set="cpref" />

			<!--{{{  thelabel:UWGLabel-->
			<gadget type="UWGLabel" name="thelabel">
				<property name="geometry" value="16,16,480,24" />
				<property set="cpref" />
				<property name="caption" value="the label" />
			</gadget>
			<!--}}}-->
			<!--{{{  button1:UWGButton-->
			<gadget type="UWGButton" name="button1">
				<property name="geometry" value="16,64,128,32" />
				<property set="cpref" />
				<property name="caption" value="button-1" />

				<action name="on_click">
				</action>
			</gadget>
			<!--}}}-->
			<!--{{{  button2:UWGButton-->
			<gadget type="UWGButton" name="button2">
				<property name="geometry" value="160,64,128,32" />
				<property set="cpref" />
				<property name="caption" value="button-2" />

				<action name="on_click">
				</action>
			</gadget>
			<!--}}}-->
			<!--{{{  button3:UWGButton-->
			<gadget type="UWGButton" name="button3">
				<property name="geometry" value="16,100,128,32" />
				<property set="cpref" />
				<property name="caption" value="kill me" />

				<action name="on_click">
					<!-- let the code at the other end deal with it -->
				</action>
			</gadget>
			<!--}}}-->
			<!--{{{  button4:UWGButton-->
			<gadget type="UWGButton" name="button4">
				<property name="geometry" value="160,100,128,32" />
				<property set="cpref" />
				<property name="caption" value="dialogue" />

				<action name="on_click">
					<command name="@dialogbox">
						<arg value="title" />
						<arg value="this is a dialog-box!" />
						<arg value="@normal" />
						<arg value="yes,no,border,info" />
					</command>
				</action>
			</gadget>
			<!--}}}-->
			<!--{{{  button5:UWGButton-->
			<gadget type="UWGButton" name="button5">
				<property name="geometry" value="16,136,128,32" />
				<property set="cpref" />
				<property name="caption" value="new button" />

				<action name="on_click">
				</action>
			</gadget>
			<!--}}}-->
			<!--{{{  button7:UWGBitBtn-->
			<gadget type="UWGBitBtn" name="button7">
				<property name="geometry" value="160,172,128,32" />
				<property set="cpref" />
				<property name="caption" value="file dialogue" />
				<property name="img-up" value="rapp:///%s/uwg_open.xpm" />

				<action name="on_click">
					<command name="@filedlg">
						<arg value="open" />
						<arg value="" />
					</command>
				</action>
			</gadget>
			<!--}}}-->
			<!--{{{  button8:UWGButton-->
			<gadget type="UWGButton" name="button8">
				<property name="geometry" value="16,172,128,32" />
				<property set="cpref" />
				<property name="caption" value="wait a while" />

				<action name="on_click" options="wait">
				</action>
			</gadget>
			<!--}}}-->
		</gadget>
		<!--}}}-->

		<action name="on_delete">
			<command name="@termloop"><arg value="0" /></command>
		</action>

	</gadget>
	<!--}}}-->

</xhpd>

::END
EOF

while [ : ]; do
	# actually, for fun, a bit of awk
	read
	#printf '%s: got [%s]\n' "$0" "$REPLY" 1>&2
	maybespecial=$(printf '%s' "$REPLY" | cut -d ' ' -f 1-1)
	if [ "$maybespecial" = "@filedlg:ok" ]; then
		printf '::BEGIN\n'
		printf '<update name="thelabel">\n'
		printf '<property name="caption" value="filename selected: %s" />\n' $(printf '%s' "$REPLY" | sed -e 's/@filedlg:ok path:text:"\([^"]*\)"/\1/g')
		printf '</update>\n'
		printf '::END\n'
	else
		printf '%s\n' "$REPLY" | awk -F : '{
if ($1 != "iotest") {
 printf ("::BEGIN\n");
 printf ("<update name=\"thelabel\">\n");
 printf ("<property name=\"caption\" value=\"erk, not me! [%s]\" />\n", $0);
 printf ("</update>\n");
 printf ("::END\n");
} else {
 if ((NF == 2) && ($2 == "on_delete")) {
 exit 1
} else if ((NF == 4) && (($3 == "button1") || ($3 == "button2"))&& ($4 == "on_click")) {
 printf ("::BEGIN\n");
 printf ("<update name=\"thelabel\">\n");
 printf ("<property name=\"caption\" value=\"%s clicked!\" />\n", $3);
 printf ("</update>\n");
 printf ("::END\n");
} else if ((NF == 4) && ($3 == "button8") && ($4 == "on_click")) {
 system ("sleep 3");
 printf ("::BEGIN\n");
 printf ("<update name=\"thelabel\">\n");
 printf ("<property name=\"caption\" value=\"done waiting!\" />\n");
 printf ("</update>\n");
 printf ("::END\n");
} else if ((NF == 4) && ($3 == "button3") && ($4 == "on_click")) {
 printf ("::BEGIN\n");
 printf ("<command name=\"@termloop\"><arg value=\"0\" /></command>\n");
 printf ("::END\n");
} else if ((NF == 4) && ($3 == "button5") && ($4 == "on_click")) {
 printf ("::BEGIN\n");
 printf ("<newgadget parent=\"xpanel\">\n");
 printf ("<gadget name=\"button6\" type=\"UWGButton\">\n");
 printf ("<property name=\"geometry\" value=\"160,136,128,32\" />\n");
 printf ("<property set=\"cpref\" />\n");
 printf ("<property name=\"caption\" value=\"fresh-button!\" />\n");
 printf ("<action name=\"on_click\">\n");
 printf ("    <command name=\"@delgadget\">\n");
 printf ("        <arg value=\"button6\" />");
 printf ("    </command>\n");
 printf ("</action>\n");
 printf ("</gadget>\n");
 printf ("</newgadget>\n");
 printf ("::END\n");
}
}
}' || break
	fi

done

# sleep 2

