COMPILE FUNCTION Comm_Application(Event,CtrlEntId,CtrlClassId,Parm1,Parm2,Parm3)

DECLARE SUBROUTINE Set_Property
DECLARE FUNCTION Get_Property

* Application wide event handlers
* initially for support of <ALT><O> and <ALT><C> functionality
* 9/27/1999 - John C. Henry - J.C. Henry, Inc.

EQU COL$ TO 1
EQU ROW$ TO 2


IF Event = 'READ' THEN
	Ctrls = CtrlEntId:@RM:CtrlEntId:@RM:CtrlEntId:@RM:CtrlEntId
	Props = '@PREV_ID':@RM:'@PREV_REC':@RM:'ID':@RM:'RECORD'

	Vals = Get_Property(Ctrls,Props)

	PrevID 	= Vals[1,@RM]
	PrevRec	= Vals[COL2()+1,@RM]
	ID		= Vals[COL2()+1,@RM]
	Record	= Vals[COL2()+1,@RM]
	
	IF PrevID	= '' THEN Set_Property(CtrlEntId,'@PREV_ID',ID)
	IF PrevRec	= '' THEN Set_Property(CtrlEntId,'@PREV_REC',Record)

	RETURN 1
END

IF Event = 'WRITE' THEN

	Ctrls = CtrlEntId:@RM:CtrlEntId
	Props = 'ID':@RM:'RECORD'

	Vals = Get_Property(Ctrls,Props)

	ID		= Vals[1,@RM]
	Record	= Vals[COL2()+1,@RM]

	Props	= '@PREV_ID':@RM:'@PREV_REC'
	Vals	= ID:@RM:Record

	Set_Property(Ctrls,Props,Vals)	;* Store off ID and record for defaults
	
	RETURN 1
END


IF Event = 'PREV_REC' THEN

	* <ALT><C> - Copy previous record
	
	Ctrls = @WINDOW:@RM:@WINDOW:@RM:@WINDOW
	Props = 'FOCUS':@RM:'@PREV_REC':@RM:'CTRLMAP'
	
	Vals = Get_Property(Ctrls,Props)
		
	CtrlName = Vals[1,@RM]
	PrevRec  = Vals[COL2()+1,@RM]
	CtrlMap  = Vals[COL2()+1,@RM]
	
	CONVERT @FM TO @RM IN CtrlMap
	
	PosList = Get_Property(CtrlMap,STR('POS':@RM,COUNT(CtrlMap,@RM)):'POS')
	ConvList = Get_Property(CtrlMap,STR('CONV':@RM,COUNT(CtrlMap,@RM)):'CONV')
	
	Ctrls = ''
	Props = ''
	Vals = ''
	
	FOR I = 1 TO COUNT(PosList,@RM) + (PosList NE '')
	
		Pos = FIELD(PosList,@RM,I)
		
		IF Pos NE '' AND Pos > 0 THEN
			Conv = FIELD(ConvList,@RM,I)
			Ctrl = FIELD(CtrlMap,@RM,I)
			
			IF INDEX(Pos,@SVM,1) THEN
			
				* We're in a Multivalued control i.e. edittable
				
				CtrlArray = ''
				
				FOR N = 1 TO COUNT(Pos,@SVM) + (Pos NE '')
					ColPos = FIELD(Pos,@SVM,N)
					ColConv = FIELD(Conv,@SVM,N)
					ColValues = PrevRec<ColPos>
					IF ColConv NE '' THEN
						ColValues = OCONV(ColValues,ColConv)
					END
					
					CtrlArray<N> = ColValues
				NEXT N
					
				Ctrls := Ctrl:@RM
				Props := 'DEFPROP':@RM
				Vals  := CtrlArray:@RM				
				
			END ELSE
			
				Value = PrevRec<Pos>
				
				IF Conv NE '' THEN Value = OCONV(Value,Conv)
							
				Ctrls := FIELD(CtrlMap,@RM,I):@RM
				Props := 'DEFPROP':@RM
				Vals  := Value:@RM
			END
			
		END
	NEXT I
	
	Ctrls[-1,1] = ''
	Props[-1,1] = ''
	Vals[-1,1] = ''

	Set_Property(Ctrls,Props,Vals)

	RETURN ''
	
END

IF Event = 'PREV_VAL' THEN

	* <ALT><O> - Copy previous field value, also works in the keys fields
	
	Ctrls = @WINDOW:@RM:@WINDOW:@RM:@WINDOW
	Props = 'FOCUS':@RM:'@PREV_ID':@RM:'@PREV_REC'
	
	Vals = Get_Property(Ctrls,Props)
	
	CtrlName = Vals[1,@RM]
	PrevID   = Vals[COL2()+1,@RM]
	PrevRec  = Vals[COL2()+1,@RM]	
	
	Ctrls = CtrlName:@RM:CtrlName:@RM:CtrlName
	Props = 'POS':@RM:'PART':@RM:'CONV'
	
	Vals = Get_Property(Ctrls,Props)
	
	Pos		= Vals[1,@RM]
	Part	= Vals[COL2()+1,@RM]
	Conv	= Vals[COL2()+1,@RM]

	IF Index(Pos,@SVM,1) THEN
		
		* Multi-Valued control i.e. We're in an edit table
		
		SelPos = Get_Property(CtrlName,'SELPOS')
		
		SelCol = SelPos<COL$>
		SelRow = SelPos<ROW$>
		
		ColPos = FIELD(Pos,@SVM,SelCol)
		ColConv = FIELD(Conv,@SVM,SelCol)
		
		Value = PrevRec<ColPos,SelRow>
		
		IF Conv NE '' THEN Value = OCONV(Value,ColConv)
		
		Set_Property(CtrlName,'DEFPROP',Value,SelPos)
		Set_Property(CtrlName,'SELECTION',1,@FM:LEN(Value))
	
		
	END ELSE
	
		* Single valued control
		
		IF Pos = 0 THEN
			Value = FIELD(PrevID,'*',Part)
		END ELSE
			Value = PrevRec<Pos>
		END
		
		IF Conv NE '' THEN Value = OCONV(Value,Conv)
		
		Set_Property(CtrlName,'DEFPROP',Value)
		Set_Property(CtrlName,'SELECTION',1,@FM:LEN(Value))
		
	END
	
	RETURN ''
	
END

RETURN 0

