#!/bin/wksh
#root:x:0:3:0000-Admin(0000):/:/usr/bin/ksh
SEP=:
NF=7
DB=/etc/passwd

#
#	Make list of fields f1[f] f2[f] ... fNF[f]
#
i=0
fieldlist=""
while ((i < $NF))
do
	fieldlist="$fieldlist f$i[f]"
	((i=i+1))
done
#echo $fieldlist

#
#	Read database into arrays
#
SIFS="$IFS"
IFS=$SEP
f=0
while eval read $fieldlist
do
	((f=f+1))
done < $DB
IFS="$SIFS"
NR=$f

oi TOPLEVEL db db "$@"

cmw MAINFORM mainform form $TOPLEVEL \
	height:400 width:600 \

cmw BUTTONS buttons controlArea $MAINFORM \
	xAttachRight:true xVaryOffset:false \
	xResizable:true \
	center:true \

addbuttons $BUTTONS \
	"Insert" ins_record \
	"Append" app_record \
	"Delete" delete_record \
	"Modify" modify_record \
	"Quit" exit "Save" save_db

cmw SCROLLWINDOW scrollwindow scrolledWindow $MAINFORM \
	xAttachRight:true xVaryOffset:false \
	yAttachBottom:true yVaryOffset:false \
	yRefWidget:$BUTTONS yAddHeight:true yOffset:0 \
	xResizable:true yResizable:true \
	hSliderMoved:slide \
        recomputeHeight:true recomputeWidth:true \
	height:400 width:600 \

slide() {
	echo "$CALL_DATA_MORE_CB_PENDING"
	set|more
}

#
#	The header and the fields scroll in tandem. Make a form to hold them
#
cmw SCROLLFORM scrollform form $SCROLLWINDOW \

cmw HDR hdr staticText $SCROLLFORM \
	xAttachRight:true xVaryOffset:false \
	string:hdr

cmw FIELDS fields controlArea $SCROLLFORM \
	xAttachRight:true xVaryOffset:false \
	yAttachBottom:true yVaryOffset:false \
	yRefWidget:$HDR yAddHeight:true yOffset:0 \
	measure:$NF layoutType:fixedcols sameSize:columns \
	borderColor:red \
	borderWidth:0 \
	background:green \
	vSpace:2 vPad:0 \
	hSpace:0 hPad:0 \

zzz() {
	WID=$CB_WIDGET
	#echo "<$CB_WIDGET $1 $2>"
}
delete_record()
{
	#XtUnMapWidget $WID
	#XtUnrealizeWidget $WID
	XtDestroyWidget $WID
	#XtRealizeWidget $WINDOW
}
app_record()
{
	label=rick
		cmw XXX xxx \
			textField $FIELDS string:"$label" \
			charsVisible:"${#label}" \
			editType:textread \
			foreground:white \
			fontColor:blue \
			borderColor:red \
			background:green \
			inputFocusColor:darkgreen \
	XtRealizeWidget $XXX

}

r=0
while ((r < $NR))
do
	f=0
	while ((f < $NF))
	do
		lab="{f${f}[$r]}"
		eval label="\$$lab"
		cmw TF$r$f tf$r$f \
			textField $FIELDS string:"$label" \
			charsVisible:"${#label}" \
			editType:textread \
			foreground:white \
			fontColor:blue \
			borderColor:red \
			background:green \
			inputFocusColor:darkgreen \
			consumeEvent:"zzz m$r $f" \
			#buttons:"zzz b$r $f" \
			#keys:"zzz k$r $f" \
			#translations:"<FocusIn>: printf($r $f)" \

		((f=f+1))
	done
	((r=r+1))
done


rw $TOPLEVEL
XtGetValues $TF00 x:x y:y string:s width:w
echo "$x $y $s $w"

ml
