include	clib.inc
include	errno.inc
include	string.inc
include	conio.inc
include	mouse.inc
include	keyb.inc
include	confirm.inc
include	math.inc

	public	progress_dobj
	public	progress_size

_DATA	SEGMENT

progress_64	dd 0
progress_size	dd 0
progress_name	dd 0
progress_xpos	dw 0
progress_args	db '%s',10,'  to',0
progress_dobj	dw _D_STDDLG
		db 0,0
dl_rc_ax	label word
dl_rc_x		db 4
dl_rc_y		db 9
dl_rc_dx	label word
dl_rc_col	db 72,6
		dd 0,0

_DATA	ENDS

_TEXT	SEGMENT

test_userabort proc
	call getesc
	.if ax
	    invoke confirm_continue, progress_name
	    .if ax
		mov ax,ER_USERABORT
	    .endif
	.endif
	ret
test_userabort endp

progress_open proc DIST pascal public ttl:dword, function:dword
      ifdef __386__
        xor eax,eax
	mov progress_size,eax
      else
        xor ax,ax
	mov word ptr progress_size,ax
	mov word ptr progress_size+2,ax
      endif
	mov progress_xpos,4
	.if word ptr function != ax
	    mov	progress_xpos,9
	.endif
	mov bl,at_background[B_Dialog]
	or  bl,at_foreground[F_Dialog]
      ifdef __386__
        mov eax,ttl
	mov progress_name,eax
	invoke dlopen, addr progress_dobj, bx, eax
      else
	lodm ttl
	stom progress_name
	invoke dlopen, addr progress_dobj, bx, dx::ax
      endif
	.if ax
	    invoke dlshow, addr progress_dobj
	    mov	bx,dl_rc_ax
	    add	bx,0204h
	    lodm function
	    .if	ax
		mov ch,at_background[B_Dialog]
		or ch,at_foreground[F_DialogKey]
		scputf@	bl, bh, ch, 68, offset progress_args, ax, dx
	    .endif
	    add	bh,2
	    scputc@ bl, bh, 64, '°'
	.endif
	ret
progress_open endp

progress_set proc DIST pascal public s1:dword, s2:dword, len:dword
	push dx
	push cx
	push bx
	sub ax,ax
	.if progress_dobj & _D_ONSCR
	    call mousehide
	    push si
	    mov bx,progress_xpos
	    mov si,68
	    sub si,bx
	    add bl,dl_rc_x
	    mov bh,dl_rc_y
	    add bh,2
	    .if word ptr s1
	      ifdef __386__
              	mov eax,len
                mov progress_size,eax
                shr eax,6
                mov progress_64,eax
              else
		mov ax,word ptr len+2
		mov word ptr progress_size+2,ax
		mov dl,al
		shr ax,6
		mov word ptr progress_64+2,ax
		mov ax,word ptr len
		mov word ptr progress_size,ax
		shr ax,6
		shl dl,2
		or ah,dl
		mov word ptr progress_64,ax
              endif
		scputw@ bl,bh,si,' '
		lodm s1
		call @scpath
		inc bh
		scputw@ bl,bh,cx,' '
		lodm s2
		call @scpath
		add bx,0104h
		sub bx,progress_xpos
		scputw@ bl,bh,64,'°'
	    .else
		inc bh
		scputw@ bl,bh,si,' '
		lodm s2
		call @scpath
	    .endif
	    invoke strfn, s1
	    pop si
	    stom progress_name
	    call tupdate
	    call mouseshow
	    call test_userabort
	.endif
	pop bx
	pop cx
	pop dx
	ret
progress_set endp

progress_close proc DIST public
	invoke dlclose, addr progress_dobj
	mov ax,dx
	ret
progress_close endp

progress_update proc DIST pascal public uses si di offs:dword
      ifdef __WCC__
	local result:dword
      endif
	.if !(progress_dobj & _D_ONSCR)
	    xor ax,ax
	    jmp update_end
	.endif

      ifdef __MOUSE_MOVE__
	call tupdate
	call mousep
	.if ax
	    call mousey
	    .if al == dl_rc_y
		call mousex
		mov dl,dl_rc_x
		.if al >= dl
		    add dl,dl_rc_col
		    .if dl >= al
		      ifdef __WCC__
			lea ax,result
			invoke rcmsmove, progress_dobj.dl_rect, \
			    progress_dobj.dl_wp, progress_dobj.dl_flag, ax
			lodm result
		      else
			invoke rcmsmove, progress_dobj.dl_rect, \
			    progress_dobj.dl_wp, progress_dobj.dl_flag
		      endif
			stom progress_dobj.dl_rect
		    .endif
		.endif
	    .endif
	.endif
      endif

	mov di,1
      ifdef __386__
	mov eax,progress_64
	mov edx,offs
	.repeat
	    .break .if eax >= edx
	    add eax,progress_64
      else
	lodm progress_64
	lodso offs,cx,bx
	.repeat
	    .if dx == cx
		.break .if ax >= bx
	    .endif
	    .break .if dx > cx
	    add ax,word ptr progress_64
	    adc	dx,word ptr progress_64+2
      endif
	    inc di
	.until di == 64
	mov bl,dl_rc_x
	add bl,4
	mov bh,dl_rc_y
	add bh,4
	scputw@	bl,bh,di,'Û'
	call test_userabort
    update_end:
	ret
progress_update endp

_TEXT	ENDS

	END
