include	clib.inc
include	conio.inc
include	mouse.inc
include	keyb.inc
include	string.inc
include	format.inc

	EXTRN	IDD_EditColor:DWORD

_DATA	SEGMENT

format_X___3d	db "%X   %3d",0
cp_palinfo	db "Id   Init    Setup",0
cp_attinfo	db "Background  Foreground",0

_DATA	ENDS

_TEXT	SEGMENT

initidd PROC
	pop	ax
	push	bp
	push	si
	push	di
	sub	sp,44
	mov	bp,sp
	pushm	IDD_EditColor
	mov	di,ax
	call	rsopen
	mov	[bp+40],ax
	mov	[bp+42],dx
	jnz	initidd_00
	jmp	di
    initidd_00:
	invoke	memcpy, ss::bp, addr at_background, 40
	invoke	dlshow, [bp+40]
	or	ax,1
	jmp	di
initidd	ENDP

PUT_ATTRIB proc pascal uses si di dialog:dword
	les	bx,dialog
	mov	bx,es:[bx+4]
	add	bx,0422h
	mov	di,bx
	mov	cx,24
	mov	dx,8
	xor	si,si
    PUT_ATTRIB_00:
	mov	al,[si+at_background]
	or	al,[si+at_foreground+7]
	call	@scputa
	inc	bh
	inc	si
	dec	dx
	jnz	PUT_ATTRIB_00
	mov	dx,8
    PUT_ATTRIB_01:
	mov	al,[si+at_foreground]
	call	@scputfg
	inc	bh
	inc	si
	dec	dx
	jnz	PUT_ATTRIB_01
	mov	dx,3
	xor	si,si
    PUT_ATTRIB_02:
	mov	bx,di
	mov	ax,si
	add	bh,al
	inc	bh
	mov	al,[si+at_foreground+F_Desktop]
	call	@scputfg
	add	bh,8
	mov	al,[si+at_background+B_Desktop]
	call	@scputbg
	add	bh,4
	mov	al,[si+at_background+B_Desktop]
	call	@scputbg
	inc	si
	dec	dx
	jnz	PUT_ATTRIB_02
	mov	bx,di
	add	bh,F_TitleKey
	mov	al,[at_background+B_Title]
	call	@scputbg
	xor	ax,ax
	ret
PUT_ATTRIB endp

COPYCOLOR:
	invoke	memcpy, addr at_background, ss::bp, 40
	ret

UPDATE_ATTRIB proc pascal uses si dialog:dword
	les	bx,dialog
	mov	cx,3
	mov	bx,es:[bx+4]
	add	bx,0407h
	xor	si,si
    UPDATE_ATTRIB_00:
	mov	ah,0
	mov	al,[si+at_background]
	shr	ax,4
	push	ax
	mov	ax,offset format_X
	call	@scputf
	pop	ax
	inc	bh
	inc	si
	cmp	si,8
	jl	UPDATE_ATTRIB_00
	sub	bh,8
	add	bl,12
	xor	si,si
    UPDATE_ATTRIB_01:
	mov	ah,0
	mov	al,[si+at_foreground]
	push	ax
	mov	ch,[at_background+B_Dialog]
	or	ch,al
	mov	ax,offset format_X
	call	@scputf
	pop	ax
	inc	bh
	inc	si
	cmp	si,16
	jl	UPDATE_ATTRIB_01
	invoke	PUT_ATTRIB, dialog
	ret
UPDATE_ATTRIB endp

event_editpal PROC DIST
	push	si
	push	bp
	les	bx,tdialog
	mov	bx,es:[bx+4]
	add	bx,0412h
	mov	cx,3
	xor	si,si
    event_editpal_00:
	mov	al,[si+at_palett]
	mov	ah,0
	push	ax
	mov	ax,offset format_3d
	call	@scputf
	add	sp,2
	inc	bh
	inc	si
	cmp	si,16
	jb	event_editpal_00
	call	dlxcellevent
	mov	bp,ax
  ifdef __MOUSE__
	call	mousehide
  endif
	les	bx,tdialog
	xor	ax,ax
	mov	al,es:[bx].S_DOBJ.dl_index
	sub	ax,8
	mov	si,ax
	cmp	bp,KEY_PGUP
	jne	event_editpal_01
	inc	[si+at_palett]
	jmp	event_editpal_02
    event_editpal_01:
	cmp	bp,KEY_PGDN
	jne	event_editpal_03
	dec	[si+at_palett]
    event_editpal_02:
	mov	al,[si+at_palett]
	invoke	setpal, ax, si
    event_editpal_03:
  ifdef __MOUSE__
	call	mouseshow
  endif
	mov	ax,bp
	pop	bp
	pop	si
	ret
event_editpal	ENDP

event_editat PROC DIST
	push	si
	push	di
	les	bx,tdialog
	mov	cx,3
	mov	bx,es:[bx+4]
	add	bx,0407h
	xor	di,di
    event_editat_00:
	mov	ah,0
	mov	al,[di+at_background]
	shr	ax,4
	push	ax
	mov	ax,offset format_X
	call	@scputf
	pop	ax
	inc	bh
	inc	di
	cmp	di,8
	jl	event_editat_00
	add	bl,12
    event_editat_01:
	mov	ah,0
	mov	al,[di+at_foreground]
	push	ax
	mov	ax,offset format_X
	call	@scputf
	pop	ax
	inc	bh
	inc	di
	cmp	di,16
	jl	event_editat_01
	call	dlxcellevent
	xor	dx,dx
	mov	di,dx
	les	bx,tdialog
	mov	dl,es:[bx].S_DOBJ.dl_index
	mov	si,dx
	cmp	ax,KEY_PGUP
	jne	event_editat_03
	cmp	si,8
	jge	event_editat_02
	cmp	[si+at_background],70h
	jnb	event_editat_06
	add	[si+at_background],10h
	jmp	event_editat_05
    event_editat_02:
	cmp	[si+at_foreground-8],0Fh
	jnb	event_editat_06
	inc	[si+at_foreground-8]
	jmp	event_editat_05
    event_editat_03:
	cmp	ax,KEY_PGDN
	jne	event_editat_06
	cmp	si,8
	jge	event_editat_04
	cmp	[si+at_background],10h
	jb	event_editat_06
	sub	[si+at_background],10h
	jmp	event_editat_05
    event_editat_04:
	cmp	[si+at_foreground-8],00h
	je	event_editat_06
	dec	[si+at_foreground-8]
    event_editat_05:
	inc	di
    event_editat_06:
	test	di,di
	jz	event_editat_07
	push	ax
	invoke	dlhide, tdialog
	invoke	rsreload, tdialog, IDD_EditColor
	invoke	dlshow, tdialog
	invoke	UPDATE_ATTRIB, tdialog
	pop	ax
    event_editat_07:
	pop	di
	pop	si
	ret
event_editat ENDP

_TEXT	ENDS

DPROC	editpal
	call	initidd
	jnz	editpal_00
	jmp	editpal_05
    editpal_00:
	invoke	PUT_ATTRIB, es::bx
	les	bx,[bp+40]
	mov	bx,es:[bx+4]
	add	bx,0204h
	xor	cx,cx
	mov	ax,offset cp_palinfo
	call	@scputf
	inc	bl
	add	bh,2
	mov	cx,7
	xor	si,si
    editpal_01:
	mov	al,[si+at_palett]
	mov	ah,0
	push	ax
	push	si
	mov	ax,offset format_X___3d
	call	@scputf
	add	bl,13
	add	sp,2
	mov	ax,offset format_3d
	call	@scputf
	sub	bl,13
	pop	ax
	inc	bh
	inc	si
	cmp	si,16
	jb	editpal_01
	xor	si,si
	les	di,[bp+40]
	add	di,size S_DOBJ
    editpal_02:
	or	es:[di].S_TOBJ.to_flag,_O_STATE
	add	di,size S_TOBJ
	inc	si
	cmp	si,8
	jl	editpal_02
	mov	ax,offset event_editpal
    editpal_03:
	mov	word ptr es:[di].S_TOBJ.to_proc,ax
	LMOV	word ptr es:[di].S_TOBJ.to_proc+2,cs
	add	di,size S_TOBJ
	inc	si
	cmp	si,24
	jl	editpal_03
	invoke	rsevent, IDD_EditColor, [bp+40]
	invoke	dlclose, [bp+40]
	test	dx,dx
	jnz	editpal_04
	call	COPYCOLOR
	invoke	loadpal, addr at_palett
	xor	ax,ax
	jmp	editpal_05
    editpal_04:
	mov	ax,1
    editpal_05:
	add	sp,44
	pop	di
	pop	si
	pop	bp
	ret
PEND	editpal

DPROC	editattrib
	call	initidd
	jz	editattrib_02
	xor	cx,cx
	les	bx,[bp+40]
	mov	bx,es:[bx+4]
	add	bx,0204h
	mov	ax,offset cp_attinfo
	call	@scputf
	mov	cx,24
	les	bx,[bp+40]
	lea	bx,[bx+16].S_TOBJ.to_proc
	mov	dx,offset event_editat
    editattrib_00:
	mov	es:[bx],dx
	LMOV	es:[bx+2],cs
	add	bx,size S_TOBJ
	dec	cx
	jnz	editattrib_00
	invoke	UPDATE_ATTRIB, [bp+40]
	invoke	rsevent, IDD_EditColor, [bp+40]
	invoke	dlclose, [bp+40]
	test	dx,dx
	jnz	editattrib_01
	call	COPYCOLOR
	xor	ax,ax
	jmp	editattrib_02
    editattrib_01:
	mov	ax,1
    editattrib_02:
	add	sp,44
	pop	di
	pop	si
	pop	bp
	ret
PEND	editattrib

	END
