include	clib.inc
include	conio.inc
include	mouse.inc

_TEXT	SEGMENT

scr_ax	equ	word ptr [bp-4]
scr_dx	equ	word ptr [bp-2]
xcount	equ	word ptr [bp-6]
ycount	equ	word ptr [bp-8]
wcount	equ	word ptr [bp-10]
scwcnt	equ	word ptr [bp-12]

rcmove_init PROC
	xor	ax,ax
	mov	bx,ax
	mov	al,_scrcol
	add	ax,ax
	mov	scwcnt,ax
	mov	ax,bx
	mov	al,[bp+ARG1+6+rc_col]
	mov	xcount,ax
	add	ax,ax
	mov	wcount,ax
	mov	ax,bx
	mov	al,[bp+ARG1+6+rc_row]
	mov	ycount,ax
	mov	bx,[bp+ARG1+6]
	call	@rcsprc
	mov	[bp-4],ax
	mov	[bp-2],dx
	cld
	test	word ptr [bp+ARG1],_D_SHADE
	jz	init_00
	invoke	rcclrshade, [bp+ARG1+6], [bp+ARG1+2]
    init_00:
  ifdef __MOUSE__
	call	mousehide
  endif
	ret
rcmove_init ENDP

rcmove_exit PROC
  ifdef __MOUSE__
	call	mouseshow
  endif
	test	word ptr [bp+ARG1],_D_SHADE
	jz	exit_00
	push	ss
	pop	ds
        invoke	rcsetshade, [bp+ARG1+6], [bp+ARG1+2]
    exit_00:
	mov	dx,[bp+ARG1+6]
	mov	ax,1
	ret
rcmove_exit ENDP

_TEXT	ENDS

PPROC	rcmoveup, rc:dword, wc:dword, dfl:word
local	rcmovx[12]:byte
	push	ds
	push	si
	push	di
	xor	ax,ax
	mov	dx,word ptr rc
	cmp	dh,2
	jb	rcmoveup_end
	call	rcmove_init
	mov	ax,scwcnt
	sub	scr_ax,ax
	dec	rc.rc_y
	lds	si,wc
	mov	ax,xcount
	mov	dx,ycount
	mul	dl
	add	ax,ax
	add	ax,si
	sub	ax,wcount
	mov	si,ax
	les	di,[bp-4]
	mov	ax,ycount
	mov	dx,scwcnt
	mul	dx
	add	di,ax
	xor	bx,bx
	mov	cx,xcount
    rcmoveup_00:
	push	cx
	push	si
	push	di
	mov	cx,ycount
	mov	dx,[bx+si]
	cmp	cx,1
	jz	rcmoveup_02
    rcmoveup_01:
	push	bx
	sub	bx,wcount
	mov	ax,[bx+si]
	pop	bx
	mov	[bx+si],ax
    rcmoveup_02:
	mov	ax,es:[bx+di]
	mov	es:[bx+di],dx
	mov	dx,ax
	sub	di,scwcnt
	sub	si,wcount
	dec	cx
	jnz	rcmoveup_01
	mov	ax,es:[bx+di]
	mov	es:[bx+di],dx
	add	si,wcount
	mov	[bx+si],ax
	add	bx,2
	pop	di
	pop	si
	pop	cx
	dec	cx
	jnz	rcmoveup_00
	call	rcmove_exit
    rcmoveup_end:
	test	ax,ax
	pop	di
	pop	si
	pop	ds
	ret
PEND	rcmoveup

PPROC	rcmovedn, rc:dword, wc:dword, dfl:word
local	rcmovx[12]:byte
	push	ds
	push	si
	push	di
	xor	ax,ax
	mov	dx,word ptr rc
	cmp	dh,_scrrow
	je	rcmovedn_end
	call	rcmove_init
	inc	rc.rc_y
	mov	ax,scwcnt
	add	scr_ax,ax
	les	di,[bp-4]
	xor	bx,bx
	mov	cx,xcount
    rcmovedn_00:
	push	cx
	lds	si,wc
	mov	di,scr_ax
	sub	di,scwcnt
	mov	cx,ycount
	mov	dx,[bx+si]
    rcmovedn_01:
	mov	ax,es:[bx+di]
	mov	es:[bx+di],dx
	push	ax
	mov	dx,bx
	add	bx,wcount
	mov	ax,[bx+si]
	mov	bx,dx
	mov	[bx+si],ax
	add	di,scwcnt
	add	si,wcount
	pop	dx
	dec	cx
	jnz	rcmovedn_01
	mov	ax,es:[bx+di]
	mov	es:[bx+di],dx
	sub	si,wcount
	mov	[bx+si],ax
	add	bx,2
	pop	cx
	dec	cx
	jnz	rcmovedn_00
	call	rcmove_exit
    rcmovedn_end:
	test	ax,ax
	pop	di
	pop	si
	pop	ds
	ret
PEND	rcmovedn

PPROC	rcmoveleft, rc:dword, wc:dword, dfl:word
local	rcmovx[12]:byte
	push	ds
	push	si
	push	di
	xor	ax,ax
	mov	dx,word ptr rc
	cmp	dl,al
	je	rcmoveleft_end
	call	rcmove_init
	dec	rc.rc_x
	sub	scr_ax,2
	les	di,wc
	lds	dx,[bp-4]
	mov	cx,ycount
    rcmoveleft_00:
	mov	si,dx
	add	dx,scwcnt
	mov	ax,[si]
	add	si,2
	mov	bx,es:[di]
	mov	es:[di],ax
	add	di,2
	push	cx
	mov	cx,xcount
	dec	cx
	jnz	rcmoveleft_01
	mov	ax,[si]
	mov	[si],bx
	mov	[si-2],ax
	jmp	rcmoveleft_02
    rcmoveleft_01:
	mov	ax,[si]
	mov	[si-2],ax
	add	si,2
	mov	ax,bx
	mov	bx,es:[di]
	mov	es:[di],ax
	add	di,2
	dec	cx
	jnz	rcmoveleft_01
	mov	ax,[si]
	mov	[si-2],ax
	mov	[si],bx
	add	si,2
    rcmoveleft_02:
	pop	cx
	dec	cx
	jnz	rcmoveleft_00
	call	rcmove_exit
    rcmoveleft_end:
	test	ax,ax
	pop	di
	pop	si
	pop	ds
	ret
PEND	rcmoveleft

PPROC	rcmoveright, rc:dword, wc:dword, dfl:word
local	rcmovx[12]:byte
	push	ds
	push	si
	push	di
	xor	ax,ax
	mov	dx,word ptr rc
	mov	cl,dl
	inc	cl
	cmp	cl,_scrcol
	jae	rcmoveright_end
	call	rcmove_init
	inc	rc.rc_x
	add	scr_ax,2
	les	di,wc
	lds	dx,[bp-4]
	mov	cx,ycount
    rcmoveright_00:
	mov	si,dx
	add	dx,scwcnt
	push	cx
	mov	cx,xcount
	mov	bx,es:[di]
    rcmoveright_01:
	mov	ax,[si-2]
	mov	[si-2],bx
	mov	bx,ax
	mov	ax,[si]
	add	si,2
	mov	ax,es:[di+2]
	mov	es:[di],ax
	add	di,2
	dec	cx
	jnz	rcmoveright_01
	mov	ax,[si-2]
	mov	[si-2],bx
	mov	es:[di-2],ax
	pop	cx
	dec	cx
	jnz	rcmoveright_00
	call	rcmove_exit
    rcmoveright_end:
	test	ax,ax
	pop	di
	pop	si
	pop	ds
	ret
PEND	rcmoveright

ifdef __MOUSE__

ifdef __WCC__
PPROC	rcmsmove, rc:dword, wc:dword, dfl:word, offset_stack:word
else
PPROC	rcmsmove, rc:dword, wc:dword, dfl:word
endif
local	xpos:word
local	ypos:word
local	relx:word
local	rely:word
local	cursor:dword
	xor	ax,ax
	test	dfl,_D_SHADE
	jz	rcmsmove_00
	invoke	rcclrshade, rc, wc
    rcmsmove_00:
	call	mousey
	mov	ypos,ax
	mov	dx,ax
	call	mousex
	mov	xpos,ax
	mov	cx,word ptr rc
	sub	al,cl
	mov	relx,ax
	sub	dl,ch
	mov	rely,dx
	invoke	getcursor, addr cursor
	call	cursoroff
	jmp	rcmsmove_04
    rcmsmove_01:
	call	mousex
	cmp	ax,xpos
	jl	rcmsmove_02
	je	rcmsmove_03
	jmp	rcmsmove_08
    rcmsmove_02:
	test	word ptr rc,00FFh
	jne	rcmsmove_09
    rcmsmove_03:
	call	mousey
	cmp	ax,ypos
	jl	rcmsmove_06
	jne	rcmsmove_07
    rcmsmove_04:
	call	mousep
	cmp	ax,1
	je	rcmsmove_01
	invoke	setcursor, cursor
	test	dfl,_D_SHADE
	jz	rcmsmove_05
	invoke	rcsetshade, rc, wc
    rcmsmove_05:
	mov	ax,word ptr rc
	mov	dx,word ptr rc+2
  ifdef __WCC__
	mov	bx,offset_stack
	mov	[bx],ax
	mov	[bx+2],dx
	mov	ax,bx
  endif
	ret
    rcmsmove_06:
	mov	ax,offset rcmoveup
	jmp	rcmsmove_10
    rcmsmove_07:
	mov	ax,offset rcmovedn
	jmp	rcmsmove_10
    rcmsmove_08:
	mov	ax,offset rcmoveright
	jmp	rcmsmove_10
    rcmsmove_09:
	mov	ax,offset rcmoveleft
    rcmsmove_10:
	pushm	rc
	pushm	wc
	push	0
	LPUSH	cs
	call    ax
	mov	word ptr rc,dx
	mov	ax,rely
	add	al,dh
	mov	ypos,ax
	mov	ax,relx
	add	al,dl
	mov	xpos,ax
	call	mouseshow
	jmp	rcmsmove_04
PEND	rcmsmove

endif ; __MOUSE__

	END
