include	clib.inc
ifdef __TE__
include	string.inc
include	conio.inc
include	stdio.inc
include	tinfo.inc
include	format.inc
include	malloc.inc
include	keyb.inc

	public	teoption

	extrn	IDD_TEOptions:dword

_TEXT	SEGMENT

telinesize PROC
	push	dx
	xor	cx,cx
	mov	dx,TIMINLINE
	and	al,0F0h
	cmp	ax,dx
	jb	telinesize_min
	cmp	ax,TIMAXLINE
	ja	telinesize_max
    telinesize_inc:
	inc	cx
	shl	ax,1
	jnc	telinesize_inc
	dec	cx
	mov	ax,8000h
	shr	ax,cl
	cmp	ax,dx
	ja	telinesize_end
	mov	ax,dx
    telinesize_end:
	pop	dx
	ret
    telinesize_min:
	mov	ax,TIMAXLINE
	jmp	telinesize_inc
    telinesize_max:
	mov	ax,TIMAXLINE
	jmp	telinesize_inc
telinesize ENDP

tememoryset PROC
	push	tepages
	mov	cx,bx
	mov	ax,offset format_u
	mov	dx,es:[bx+6*16+TO_SDATA]
	mov	bx,es:[bx+6*16+TO_ODATA]
	call	@sprintf
	push	telsize
	mov	ax,offset format_u
	mov	bx,cx
	mov	bx,es:[bx+7*16+TO_ODATA]
	call	@sprintf
	add	sp,4
	mov	bx,cx
	ret
tememoryset ENDP

tememoryupd PROC
	push	si
	push	di
	push	bp
	mov	di,bx
	push	es
	push	es:[di+6*16+TO_ODATA]
	call	strtol
	mov	si,ax
	push	es
	push	es:[di+7*16+TO_ODATA]
	call	strtol
	call	telinesize
	mov	bp,ax
	push	ax
	mov	ax,offset format_u
	mov	dx,es
	mov	bx,es:[di+7*16+TO_ODATA]
	call	@sprintf
	add	sp,2
    tememoryupd_page:
	cmp	si,EMSMINPAGES
	ja	tememoryupd_emm
	mov	si,EMSMINPAGES
    tememoryupd_emm:
	push	es
	call	emminit
	pop	es
	jz	tememoryupd_convensional
    tememoryupd_nolines:
	mov	bx,si
	dec	cx
	shl	bx,cl
	mov	ax,EMMPAGE
	mul	si
	mov	cx,dx
	mov	si,bx
	mov	bx,ax
	xor	dx,dx
	mov	ax,EMMPAGE
	or	si,si
	jnz	tememoryupd_put
	mov	si,64
	mov	tepages,si
	mov	telsize,TIMINLINE
	mov	bx,di
	call	tememoryset
	mov	cx,8
	jmp	tememoryupd_nolines
    tememoryupd_convensional:
	xor	si,si
	mov	ax,'0'
	mov	bx,es:[di+6*16+TO_ODATA]
	mov	es:[bx],ax
	xor	ax,ax
	mov	dx,4
	div	bp
	mov	si,ax
	xor	ax,ax
	mov	dx,4
	mov	cx,ax
	mov	bx,ax
    tememoryupd_put:
	push	es
	push	di
	push	0
	push	si
	push	dx
	push	ax
	push	cx
	push	bx
	mov	bx,es:[di+4]
	add	bx,0D15h
	xor	cx,cx
	mov	ax,offset format_8ld
	call	@scputf
	add	sp,4
	inc	bh
	mov	ax,offset format_8ld
	call	@scputf
	add	sp,4
	inc	bh
	mov	ax,offset format_8ld
	call	@scputf
	add	sp,4
	call	dlinit
	xor	ax,ax
	pop	bp
	pop	di
	pop	si
	ret
tememoryupd ENDP

tememory_event PROC DIST
	call	dlteditevent
	cmp	ax,KEY_ENTER
	jne	tememory_event_end
	les	bx,tdialog
	call	tememoryupd
    tememory_event_end:
	ret
tememory_event ENDP

teoption PROC DIST
	push	si
	push	di
	invoke	rsopen, IDD_TEOptions
	jz	teoption_end
	push	dx
	push	ax
	pushm	IDD_TEOptions
	push	dx
	push	ax
	push	dx
	push	ax
	call	tememoryset
	mov	es:[bx+6*16+TO_OPROC],offset tememory_event
	mov	es:[bx+7*16+TO_OPROC],offset tememory_event
	LMOV	es:[bx+6*16+TO_OPROC+2],cs
	LMOV	es:[bx+7*16+TO_OPROC+2],cs
	add	bx,SIZE S_DOBJ
	push	es
	push	bx
	push	5
	push	_O_FLAGB
	mov	ax,teflag
	mov	al,ah
	push	ax
	push	ax
	call	tosetbitflag
	call	dlshow
	call	tememoryupd
	call	rsevent
	or	ax,ax
	jz	teoption_end
	push	es
	add	bx,SIZE S_DOBJ
	push	bx
	push	5
	push	_O_FLAGB
	call	togetbitflag
	mov	byte ptr teflag+1,al
	push	es
	push	es:[bx+5*16+TO_ODATA]
	push	es
	push	es:[bx+6*16+TO_ODATA]
	call	strtol
	call	telinesize
	mov	telsize,ax
	call	strtol
	cmp	ax,EMSMINPAGES
	jb	teoption_end
	mov	tepages,ax
    teoption_set:
	mov	ax,1
    teoption_end:
	call	dlclose
	mov	ax,dx
	pop	di
	pop	si
	ret
teoption ENDP

_TEXT	ENDS
endif
	END
