	load 'macros.dump'
	include 'driver.equ'
	include 'wp.equ'

;-----------------------------------------------
;
;   Imported addresses
;
;-----------------------------------------------

	IMPORT	W_CalcPages
	
	IMPORT	W_CheckDocPos
	
	IMPORT	W_CurDoc
	
	IMPORT	W_DelSelect
	IMPORT	W_DeleteRange
	IMPORT	D_DisableMItems
	IMPORT	X_DisposeWPScrap
	IMPORT	D_EnableMItems
	IMPORT	W_EndLine
	IMPORT	W_EndOffset
	IMPORT	W_EndPar
	
	IMPORT	W_LineToTopPixel
	IMPORT	W_MakeCaret
	IMPORT	W_MyCut
	IMPORT	W_PasteOffset
	IMPORT	W_PasteText
	
	IMPORT	W_PutOnScreen
	
	
	IMPORT	W_SelectOn
	IMPORT	W_SetFullREct
	IMPORT	D_SetUndoStr
	IMPORT	W_StartLine
	IMPORT	W_StartOffset
	IMPORT	W_StartPar
	
	IMPORT	W_UndoEndLine
	IMPORT	W_UndoEndOffset
	IMPORT	W_UndoEndPar
	IMPORT	W_UndoHandle
	IMPORT	W_UndoNames
	IMPORT	W_UndoStartLine
	IMPORT	W_UndoStartOffset
	IMPORT	W_UndoStartPar
	IMPORT	W_UpdateText
	IMPORT	W_WPUndoType
	IMPORT	W_WpCopy
	IMPORT	W_CalcDocRect
	IMPORT	caretOff
	IMPORT	W_Handle
	IMPORT	W_Selected

;-----------------------------------------------
;
;   Forward addresses and entries
;
;-----------------------------------------------

	ENTRY	W_UInsert
	ENTRY	W_UndoOff
	ENTRY	W_UndoOnND

********************************************************************
*
*	includes...
*
*
*
*
* Wednesday, May 3, 1989 5:05:30 AM
********************************************************************


		




****************************************************************
W_UndoOn	PROC		EXPORT
			;Using wpglobals

		input type:w
		begin

		cmpw type,W_WPUndoType
		beq UOExit

		jsl W_UndoOff

		pushword type
		jsl W_UndoOnND

UOExit
		return

		ENDP




****************************************************************
W_UndoOnND	PROC		EXPORT
			;Using wpglobals

		input type:w
		begin

		cmpw type,W_WPUndoType
		beq UOExit

		lda type
		sta W_WPUndoType
		and #$ff
		asl a
		asl a
		tax
		pushlong W_UndoNames:x
		lda type
		bpl NoRedo
		lda 3,s
		ora #$8000
		sta 3,s

NoRedo	pushlong W_CurDoc
		jsl D_SetUndoStr

GotStr
		pushword #UndoBit
		pushword #3
		jsl D_EnableMItems

UOExit
		return

		ENDP



****************************************************************
W_UndoOff	PROC		EXPORT
			;Using wpglobals

		begin

		lda W_WPUndoType
		beq UOExit

		dec a
		asl a
		adc #0
		asl a
		tax
		jsr (DisposeTable,x)

		stz W_WPUndoType

		pushword #UndoBit
		pushword #3
		jsl D_DisableMItems

		pushlong #0
		pushlong W_CurDoc
		jsl D_SetUndoStr

UOExit
		return

DisposeTable
	DC.W	none		;insert
	DC.W	RangeDisp	;redo insert
	DC.W	none		;delete
	DC.W	none		;redo delete
	DC.W	RangeDisp	;Range
	DC.W	none		;redo range
	DC.W	RangeDisp	;RangeInsert
	DC.W	RangeDisp	;redo rangeInsert
	DC.W	RangeDisp	;undo Style
	DC.W	RangeDisp	;redo Style
	DC.W	RangeDisp	;undo Case
	DC.W	RangeDisp	;redo Case
	DC.W	RangeDisp	;undo Cut
	DC.W	none		;redo Cut
	DC.W	RangeDisp	;undo Clear
	DC.W	none		;redo Clear
	DC.W	RangeDisp	;undo paste
	DC.W	RangeDisp	;redo paste

none	rts


RangeDisp
		cpzl W_UndoHandle
		beq NoDisp

		pushlong W_UndoHandle
		jsl X_DisposeWPScrap

		stzl W_UndoHandle

NoDisp	rts


		ENDP




*****************************************************************
* Undo was chosen for the active W_Document.
W_MyUndo	PROC		EXPORT
			;Using wpglobals
		EXPORT	RangeDispAct
		EXPORT	W_UInsert

		local rect:r,W_Handle:l
		begin +b


		pushlong W_CurDoc
		_SetPort

		jsl caretOff

		lda W_WPUndoType
		dec a
		asl a
		adc #0
		asl a
		tax
		jsr (UndoTable,x)

MUExit
		return


UndoTable
	DC.W	W_UInsert		;insert
	DC.W	RInsert		;redo insert
	DC.W	none		;delete
	DC.W	none		;redo delete
	DC.W	RangeDisp	;Range
	DC.W	RedoRange	;redo range
	DC.W	RIUndo		;RangeInsert
	DC.W	RIRedo		;redo rangeInsert

	DC.W	UnStyle		;undo style
	DC.W	UrStyle		;redo style
	DC.W	UnCase		;undo style
	DC.W	UrCase		;redo style

	DC.W	RangeDispCu	;Cut
	DC.W	RedoRangeCu	;redo Cut
	DC.W	RangeDispCl	;Clear
	DC.W	RedoRangeCl	;redo Clear
	DC.W	RPUndo		;Paste Undo
	DC.W	RPRedo		;Paste Redo

none	rts


;----------------------
RangeDispAct		;		
		pushword W_StartPar
		pushword W_StartLine
		pushlong #0
		jsl W_PutOnScreen

		pushlong !rect
		jsl W_CalcDocRect

		pha
		pha
		pushword W_StartPar
		pushword W_StartLine
		jsl W_LineToTopPixel
		pla
		pullword rect

		pha
		pha
		pha
		pushlong W_UndoHandle
		moveword W_StartPar,W_UndoStartPar
		pha 
		moveword W_StartLine,W_UndoStartLine
		pha
		pushword W_StartOffset
		jsl W_PasteText
		pullword W_EndOffset
		sta W_UndoEndOffset
		pullword W_EndLine
		sta W_UndoEndLine
		pullword W_EndPar
		sta W_UndoEndPar
		moveword W_PasteOffset,W_StartOffset
		sta W_UndoStartOffset

;			  jsl W_CalcNewLines

		pha
		pha
		pushword W_StartPar
		pushword W_StartLine
		jsl W_LineToTopPixel
		pla
		pullword rect

		stzl W_UndoHandle

		jsl W_SelectOn

		jsl W_CalcPages

		pushlong !rect
		_ClipRect

		pushlong !rect
		_EraseRect

		jsl W_UpdateText

		jsl W_SetFullREct

		jsl W_MakeCaret

		rts


;----------------------
InsertDispAct
		pushword W_StartPar
		pushword W_StartLine
		pushlong #0
		jsl W_PutOnScreen

		pushlong !rect
		jsl W_CalcDocRect

		pha
		pha
		pushword W_StartPar
		pushword W_StartLine
		jsl W_LineToTopPixel
		pla
		pullword rect

		pha
		pha
		pha
		pushlong W_UndoHandle
		moveword W_StartPar,W_UndoStartPar
		pha
		moveword W_StartLine,W_UndoStartLine
		pha
		pushword W_StartOffset
		jsl W_PasteText
		pullword W_StartOffset
		sta W_UndoEndOffset
		pullword W_StartLine
		sta W_UndoEndLine
		pullword W_StartPar
		sta W_UndoEndPar
		moveword W_PasteOffset,W_UndoStartOffset

		stzl W_UndoHandle

		jsl W_CalcPages

		pushlong !rect
		_ClipRect

		pushlong !rect
		_EraseRect

		jsl W_UpdateText

		jsl W_SetFullREct

		jsl W_MakeCaret

		rts



;----------------------
RangeDisp
		jsr RangeDispAct

		pushword #W_RangeUndo+$8000
		jsl W_UndoOn

		rts

;----------------------
RangeDispCu
		jsr RangeDispAct

		pushword #W_CutUndo+$8000
		jsl W_UndoOn

		rts

;----------------------
RangeDispCl
		jsr RangeDispAct

		pushword #W_ClearUndo+$8000
		jsl W_UndoOn

		rts


;----------------------
RedoRange

		pushword #W_RangeUndo
		jsl W_UndoOn

		jsr RedoRangeAct

		rts

;----------------------
RedoRangeCu

		pushword #W_CutUndo
		jsl W_UndoOn

		jsr RedoRangeAct

		rts

;----------------------
RedoRangeCl

		pushword #W_ClearUndo
		jsl W_UndoOn

		jsr RedoRangeAct

		rts

;----------------------
RedoRangeAct
		spacelong
		pushword W_StartPar
		pushword W_StartLine
		pushword W_StartOffset
		pushword W_EndPar
		pushword W_EndLine
		pushword W_EndOffset
		jsl W_WpCopy
		pulllong W_UndoHandle

		pushword W_StartPar
		pushword W_StartLine
		pushlong #0
		jsl W_PutOnScreen

		jsl W_DelSelect

		jsl W_MakeCaret

		rts


;----------------------
RIUndoGuts
		pushword W_UndoStartPar
		pushword W_UndoStartLine
		pushlong #0
		jsl W_PutOnScreen

		spacelong
		pushword W_UndoStartPar
		pushword W_UndoStartLine
		pushword W_UndoStartOffset
		pushword W_UndoEndPar
		pushword W_UndoEndLine
		pushword W_UndoEndOffset
		jsl W_WpCopy
		pullLong W_Handle

		pha
		pha
		pha
		pushword W_UndoStartPar
		pushword W_UndoStartLine
		pushword W_UndoStartOffset
		pushword W_UndoEndPar
		pushword W_UndoEndLine
		pushword W_UndoEndOffset
		jsl W_DeleteRange
		pullword W_StartOffset
		pullword W_StartLine
		pullword W_StartPar

		jsl W_MakeCaret

		jsr RangeDispAct

		rts

RIUndo
		jsr RIUndoGuts

		pushword #W_RTUndo+$8000
		jsl W_UndoOn

		movelong W_Handle,W_UndoHandle

		rts

RPUndo
		cpzl W_UndoHandle
		bne WithRange

		pushword #W_PasteUndo+$8000
		jsl W_UndoOnND

		jsr UInsertGuts

		rts

WithRange
		jsr RIUndoGuts

		pushword #W_PasteUndo+$8000
		jsl W_UndoOn

		movelong W_Handle,W_UndoHandle

		rts



;----------------------
RPRedo
		lda W_Selected
		bne WORange

		jsr InsertDispAct

		pushword #W_PasteUndo
		jsl W_UndoOn
		rts

WORange
		pushword #W_PasteUndo
		jsl W_UndoOnND
		bra RIRedoGuts

RIRedo
		pushword #W_RTUndo
		jsl W_UndoOnND

RIRedoGuts
		spacelong
		pushword W_StartPar
		pushword W_StartLine
		pushword W_StartOffset
		pushword W_EndPar
		pushword W_EndLine
		pushword W_EndOffset
		jsl W_WpCopy
		pulllong W_Handle

		pushword W_StartPar
		pushword W_StartLine
		pushlong #0
		jsl W_PutOnScreen

		jsl W_DelSelect

		jsl W_MakeCaret

		jsr InsertDispAct

		movelong W_Handle,W_UndoHandle

		rts



;------------------
W_UInsert		;		
		pushword #W_InsertUndo+$8000
		jsl W_UndoOnND

UInsertGuts
		pushword W_UndoStartPar
		pushword W_UndoStartLine
		pushlong #0
		jsl W_PutOnScreen

		spacelong
		pushword W_UndoStartPar
		pushword W_UndoStartLine
		pushword W_UndoStartOffset
		pushword W_UndoEndPar
		pushword W_UndoEndLine
		pushword W_UndoEndOffset
		jsl W_WpCopy
		pullLong W_Handle

		pha
		pha
		pha
		pushword W_UndoStartPar
		pushword W_UndoStartLine
		pushword W_UndoStartOffset
		pushword W_UndoEndPar
		pushword W_UndoEndLine
		pushword W_UndoEndOffset
		jsl W_DeleteRange
		pullword W_StartOffset
		pullword W_StartLine
		pullword W_StartPar

		jsl W_CalcPages

		pushlong !rect
		jsl W_CalcDocRect

		pha
		pha
		pushword W_StartPar
		pushword W_StartLine
		jsl W_LineToTopPixel
		pla
		pla
		sta rect

		pushlong !rect
		_ClipRect

		pushlong !rect
		_EraseRect

		jsl W_UpdateText

		jsl W_CheckDocPos

		jsl W_MakeCaret

		movelong W_Handle,W_UndoHandle

		rts



RInsert

		jsr InsertDispAct

		pushword #W_InsertUndo
		jsl W_UndoOn

		rts

UndoCut	rts
RedoCut
		pushlong W_CurDoc
		jsl W_MyCut
		rts


;--------------------
UStyle
		pushword W_StartPar
		pushword W_StartLine
		pushlong #0
		jsl W_PutOnScreen

		spacelong
		pushword W_StartPar
		pushword W_StartLine
		pushword W_StartOffset
		pushword W_EndPar
		pushword W_EndLine
		pushword W_EndOffset
		jsl W_WpCopy
		pullLong W_Handle

		jsl W_DelSelect

		jsl W_MakeCaret

		jsr RangeDispAct

		rts



UnStyle
		jsr UStyle

		pushword #W_StyleUndo+$8000
		jsl W_UndoOnND

		movelong W_Handle,W_UndoHandle

		rts



UrStyle
		jsr UStyle

		pushword #W_StyleUndo
		jsl W_UndoOnND

		movelong W_Handle,W_UndoHandle

		rts



UnCase
		jsr UStyle

		pushword #W_CaseUndo+$8000
		jsl W_UndoOnND

		movelong W_Handle,W_UndoHandle

		rts


UrCase
		jsr UStyle

		pushword #W_CaseUndo
		jsl W_UndoOnND

		movelong W_Handle,W_UndoHandle

		rts



		ENDP
		END

