include	clib.inc
include	wsub.inc
include	fblk.inc
include	conio.inc
include	string.inc
include	dos.inc

	extrn	IDD_WOpenFile:DWORD

ID_CNT	= 13
ID_OK	= ID_CNT
ID_EXIT	= ID_CNT+1
ID_FILE	= ID_CNT+2
ID_PATH	= ID_CNT+3
ID_L_UP	= ID_CNT+4
ID_L_DN	= ID_CNT+5
O_PATH	= ID_PATH*16+16
O_FILE	= ID_FILE*16+16

FLAG_OPEN = 1
FLAG_LOCK = 2

_DATA	SEGMENT

o_list	dw ?
o_wsub	dw ?
a_open	db ?
dialog	dd ?
cp_save	db 'Save',0

_DATA	ENDS

_TEXT	SEGMENT

init_list PROC
	push	si
	push	di
	push	bp
	push	ds
	cld
	mov	cx,ID_CNT
	mov	bx,o_list
	mov	[bx.ll_numcel],0
	mov	ax,[bx.ll_index]
	shl	ax,2
	add	ax,word ptr [bx.ll_list]
	mov	dx,word ptr [bx.ll_list+2]
	mov	bp,bx
	les	di,dialog
	mov	bx,es:[di+4]
	add	bx,es:[di+20]
	add	di,16+to_data
	mov	ds,dx
	mov	si,ax
    event_list_loop:
	lodsw
	or	byte ptr es:[di-7],80h
	or	ax,ax
	jz	event_list_null
	push	es
	push	di
	push	cx
	les	di,[si-2]
	push	ss
	pop	ds
	mov	cx,28
	mov	al,es:[di]
	and	al,_A_SUBDIR
	mov	al,at_foreground[F_Dialog]
	jz	event_list_fg
	mov	al,at_foreground[F_DarkGray];F_DialogKey]
    event_list_fg:
	call	@scputfg
	inc	bh
	mov	ds,dx
	mov	ax,di
	pop	cx
	pop	di
	pop	es
	add	ax,fb_name
	and	byte ptr es:[di-7],not 80h
	inc	[bp.ll_numcel]
    event_list_null:
	stosw
	movsw
	add	di,12
	dec	cx
	jnz	event_list_loop
	pop	ds
	pop	bp
	pop	di
	pop	si
	ret
init_list ENDP

event_list PROC DIST
	call	init_list
	invoke	dlinit, dialog
	mov	ax,_C_NORMAL
	ret
event_list ENDP

read_wsub PROC
	xor	ax,ax
	mov	bx,o_list
	mov	[bx.ll_index],ax
	mov	[bx.ll_count],ax
	mov	[bx.ll_numcel],ax
	push    ss
	push	o_wsub
	call	wsread
	mov	bx,o_list
	mov	[bx.ll_count],ax
	ret
read_wsub ENDP

event_file PROC DIST
	les	bx,dialog
	mov	dx,es
	mov	ax,word ptr es:[bx].to_data[O_FILE]
	mov	bx,o_wsub
	test	a_open,FLAG_OPEN
	jnz	event_file_open
	push	dx
	push	ax
	invoke	strrchr, dx::ax, '*'
	pop	ax
	pop	dx
	jnz	event_file_open?
	invoke	strrchr, dx::ax, '?'
	jz	event_file_ret
    event_file_open?:
	test	a_open,FLAG_LOCK
	jnz	event_file_continue
    event_file_open:
	push	ss
	push	bx
	push	dx
	push	ax
	invoke	strncpy, [bx.ws_mask], dx::ax, 32
	call	read_wsub
	call	event_list
	call	wsearch
	inc	ax
	jz	event_file_continue
    event_file_ret:
	mov	ax,_C_RETURN
    event_file_end:
	ret
    event_file_continue:
	mov	ax,_C_NORMAL
	jmp	event_file_end
event_file ENDP

event_path PROC DIST
	call	read_wsub
	call	event_list
	mov	ax,_C_NORMAL
	ret
event_path ENDP

case_files PROC
	push	si
	push	di
	mov	bx,o_list
	mov	ax,[bx.ll_index]
	add	ax,[bx.ll_celoff]
	shl	ax,2
	add	ax,word ptr [bx.ll_list]
	mov	di,ax
	mov	ax,word ptr [bx.ll_list+2]
	mov	es,ax
	les	di,es:[di]
	mov	si,es
	mov	ax,es:[di]
	test	al,_A_SUBDIR
	jnz	case_directory
	les	bx,dialog
	mov	es:[bx.dl_index],ID_FILE
	pushm	es:[bx+O_FILE].to_data
	push	si
	add	di,fb_name
	push	di
	call	strcpy
	call	event_file
	cmp	ax,_C_RETURN
	jne	case_files_continue
	inc	ax
	jmp	case_files_end
    case_files_continue:
	xor	ax,ax
    case_files_end:
	pop	di
	pop	si
	ret
    case_directory:
	mov	bx,o_wsub
	push	ss
	push	word ptr [bx.ws_path]
	and	ax,_A_UPDIR
	jz	case_directory_add
	call	strfn
	jz	case_files_continue
	mov	si,ax
	xor	ax,ax
	mov	[si-1],al
	mov	bx,o_list
	mov	[bx.ll_celoff],ax
	call	event_path
	push	ss
	push	o_wsub
	push	ss
	push	si
	call	wsearch
	cmp	ax,-1
	je	case_files_continue
	mov	bx,o_list
	cmp	ax,ID_CNT
	jnb	case_directory_index
	les	bx,dialog
	mov	es:[bx.dl_index],al
	jmp	case_directory_event
    case_directory_index:
	mov	[bx.ll_index],ax
    case_directory_event:
	call	event_list
	jmp	case_files_continue
    case_directory_add:
	les	bx,dialog
	mov	es:[bx.dl_index],al
	push	ax
	push	ax
	push	si
	add	di,fb_name
	push	di
	call	strfcat
	call	event_path
	jmp	case_files_continue
case_files ENDP

_TEXT	ENDS

PPROC	wdlgopen, apath:dword, amask:dword, asave:word
local	wsub:S_WSUB
local	path:S_PATH
local	list:S_LOBJ
	push	si
	push	di
	mov	ax,asave
	mov	a_open,al
	mov	dx,ss
	mov	es,dx
	cld
	lea	di,list
	mov	o_list,di
	mov	cx,(size S_LOBJ + size S_PATH)/2
	xor	ax,ax
	rep	stosw
	mov	si,di
	mov	o_wsub,di
	lea	bx,path
	stosw		; ws_count
	mov	ax,5000
	stosw		; ws_maxfb
	mov	ax,bx
	stosw		; ws_flag
	mov	ax,dx
	stosw
	lea	ax,[bx.wp_mask]
	push	dx
	push	ax
	pushm	amask
	stosw
	mov	ax,dx
	stosw
	lea	ax,[bx.wp_path]
	push	dx
	push	ax
	pushm	apath
	stosw
	mov	ax,dx
	stosw
	lea	ax,[bx.wp_file]
	stosw
	mov	ax,dx
	stosw
	lea	ax,[bx.wp_arch]
	stosw
	mov	ax,dx
	stosw
	mov	di,cx
	call	strcpy
	call	strcpy
	push	dx
	push	si
	call	wsopen
	jz	wdlgopen_end
	invoke	rsopen, IDD_WOpenFile
	mov	si,dx
	jz	wdlgopen_wsclose
	stom	dialog
	invoke	dlshow, dx::ax
	LMOV	ax,cs
	LMOV	word ptr list.ll_proc+2,ax
	LMOV	word ptr es:[bx+O_FILE].to_proc+2,ax
	LMOV	word ptr es:[bx+O_PATH].to_proc+2,ax
	mov	es:[bx+O_PATH].to_count,16
	pushm	es:[bx+O_FILE].to_data
	push	ss
	push	word ptr wsub.ws_mask
	mov	word ptr es:[bx+O_FILE].to_proc,offset event_file
	mov	ax,word ptr wsub.ws_path
	mov	word ptr es:[bx+O_PATH].to_data,ax
	mov	word ptr es:[bx+O_PATH].to_data+2,ss
	mov	word ptr es:[bx+O_PATH].to_proc,offset event_path
	mov	word ptr list.ll_proc,offset event_list
	mov	list.ll_dcount,ID_CNT
	mov	list.ll_celoff,ID_CNT
	movmx	list.ll_list,wsub.ws_fcb
	test	a_open,FLAG_OPEN
	jnz	wdlgopen_open
	xor	cx,cx
	mov	bx,es:[bx+4]
	add	bl,21
	mov	ax,offset cp_save
	call	@scputds
    wdlgopen_open:
	call	strcpy
  ifdef __LFN__
	cmp	_ifsmgr,0
	je	wdlgopen_read
	mov	path.wp_flag,_W_LONGNAME
    wdlgopen_read:
  endif
	call	read_wsub
	call	init_list
	invoke	dlinit, dialog
    wdlgopen_event:
	invoke	dllevent, dialog, addr list
	jz	wdlgopen_twclose
	cmp	ax,ID_CNT
	ja	wdlgopen_break
	call	case_files
	jz	wdlgopen_event
    wdlgopen_break:
	les	bx,dialog
	push	ss
	push    word ptr wsub.ws_path
	push	0
	push	0
	push	si
	push    word ptr es:[bx+O_FILE].to_data
	call	strfcat
	mov	di,ax
	test	a_open,FLAG_LOCK
	jz	wdlgopen_twclose
	invoke	strrchr, dx::ax, '.'
	jnz	wdlgopen_twclose
    wdlgopen_addext:
	push	ss
	push	di
	push	word ptr amask+2
	mov	ax,word ptr amask
	inc	ax
	push	ax
	call	strcat
    wdlgopen_twclose:
	invoke	dlclose, dialog
    wdlgopen_wsclose:
	invoke	wsclose, addr wsub
	mov	ax,di
	mov	dx,ax
	or	ax,ax
	jz	wdlgopen_end
	mov	dx,ss
    wdlgopen_end:
	pop	di
	pop	si
	ret
PEND	wdlgopen

	END