;--------------------------------------------------------------------------------------------------
;			Document DE 7
;			by Kerm Martian, christopher@cemetech.net
;			May 20, 2010
;--------------------------------------------------------------------------------------------------
; Notes:
;    Oldsize contains a size only if there is an open file that is already saved.  Otherwise 0
;    SavedFilePtr contains a location if there is an already-saved file open. Otherwise 0
;    OpenFileFlag contains 0 for NO edit context, 1 for an edit context.  If this is 1 and
;		(SavedFilePtr) is 0, then the saveas routine needs to be invoked.
;--------------------------------------------------------------------------------------------------

.nolist
#define TASM
#include "ti83plus.inc"
#include "dcs7.inc"
OpenFileFlag	.equ AppBackupScreen+94		;byte
SavedFilePtr	.equ AppBackupScreen+95		;word
OldSize			.equ AppBackupScreen+97		;word
MseY			.equ $989E
MseX			.equ MseY+1


.list
	.org progstart
#IFDEF TI83P
	.db $BB,$6D
#ENDIF
INIT:
	xor d
#IFDEF TI83P
	.db $C9
#ENDIF
	jr Start
	.dw $0000
	.db $07,$00
	.dw Icon
	.dw $0000
	.dw Apstart			;the routine to open files.  DCS will start you here instead of at $9D95
						;if a file is pending
	.db $31,$7F			;argh, this be an APMain
	.db $02				;number of accepted filetypes
MyType1:
	.db 0,1,1			;plaintext
MyType2:
	.db 0,1,2			;DDE6 text
Icon:				;a 16x16 icon
	.db %11111111,%11111000
	.db %10000000,%00001100
	.db %10111100,%00001010
	.db %10010010,%00001111
	.db %10010010,%01010001
	.db %10010010,%10101001
	.db %10111100,%01010101
	.db %10000000,%00000001
	.db %10101010,%10101001
	.db %10010101,%01010101
	.db %10101010,%10101001
	.db %10000000,%00000001
	.db %10010101,%01010101
	.db %10101010,%10101001
	.db %10000000,%00000001
	.db %11111111,%11111111
IconAP:				;a 16x16 icon
	.db $3F,$F0,$20,$18
	.db $E0,$14,$A0,$1E
	.db $A0,$02,$AF,$C2
	.db $A6,$62,$A6,$32
	.db $A6,$32,$A6,$32
	.db $A6,$62,$AF,$C2
	.db $A0,$02,$BF,$FE
	.db $80,$08,$FF,$F8
START:                          	;main routines
	;first, set up pointers
	xor a
	ld h,a
	ld l,a
	ld (OpenFileFlag),a
	ld (SavedFilePtr),hl
	ld (OldSize),hl
RenderMain:
	call OpenGUIStack
	;now, set it up
	ld hl,LargeWinData
	call PushGUIStacks
RenderLoop:
	ld a,(OpenFileFlag)
	or a
	jr nz,DispTextbox
	ld hl,NullBox
	ld de,NullBoxE-NullBox
	ld a,$16
	call PushGUIStack
	jr RenderMain2
DispTextbox:
	ld hl,(SavedFilePtr)
	ld a,h
	or l
	jr nz,DispTextboxContents
	ld hl,Textboxheader
	ld de,TextboxheaderE-Textboxheader
	ld a,$0f
	call PushGUIStack
	jr RenderMain2
DispTextboxContents:
	ld hl,(OldSize)
	ld de,TextboxHeaderE-TextboxHeader-1-8		;-1 for zterm that is extraneous, -8 for file header
	add hl,de
	ld de,TextboxHeader
	ld a,$0f
	ex de,hl
	call PushGUIStack
	call GetTextboxData
	push hl
	ld de,(SavedFilePtr)
	ld hl,8
	add hl,de
	pop de
	;ex de,hl
	ldir					;bc set by GetTextboxData
RenderMain2:
	ld hl,0
	ld (Op6),hl				;for my terrible horrible insertion hack
	call GUIMouse
DocDEClose:
	call ResetAppPage
	call CheckSaveInvocation
	call CloseGUIStack
	ret
Apstart:										;set up ap pointers now
	push ix
		pop hl
	ld de,-8
	add hl,de
	ld (SavedFilePtr),hl
	dec hl
	dec hl
	bcall(_ldhlind)
	ld (OldSize),hl
	ld a,1
	ld (OpenFileFlag),a
	jp RenderMain
NewFile:
	call ResetAppPage
	call CheckSaveInvocation
	ld a,1
	ld (OpenFileFlag),a
	ld hl,0
	ld (SavedFilePtr),hl
	ld (OldSize),hl
	call CloseGUIStack
	jp RenderMain
OpenFile:
	call ResetAppPage
	call CheckSaveInvocation
	xor a							;Only show files for DocDE
	call FileOpen
	ld a,h
	or l
	jp z,RenderMain
	push de
		ld de,-8
		add hl,de
		ld (SavedFilePtr),hl
		pop hl
	bcall(_ldhlind)
	ld (OldSize),hl
	ld a,1
	ld (OpenFileFlag),a
	jp RenderMain
SaveFileAs:
	call ResetAppPage
	ld a,(OpenFileFlag)
	or a
	jp z,RenderMain
	ld hl,(SavedFilePtr)
	ld a,h
	or l
	call nz,CheckSaveInvocation
	;*** do setup
	call getTextboxData			;sets hl and bc
	ld de,MyType2
	xor a
	call FileSaveAs
	ld a,h
	or l
	jp z,RenderMain2
	push de
		inc de
		inc de
		ld (SavedFilePtr),de
		pop hl
	bcall(_ldhlind)
	ld (OldSize),hl
	ld a,1
	ld (OpenFileFlag),a
	;*** MUST CloseGUIStack and update ptrs from result
	call CloseGUIStack
	jp RenderMain
SymbolWin:
	call ResetAppPage
	ld a,(OpenFileFlag)
	or a
	jp z,RenderMain2
	ld hl,SymbolSmallWindow
	call PushGUIStacks
	ld hl,0
	call GUIMouse
SymbolWinCancel:
	call ResetAppPage
	ld b,4
	call PopGUIStacks
	jp RenderMain2
SymbolWinGo:
	call ResetAppPage
	ld b,4
	call PopGUIStacks
	set textWrite,(iy+sgrFlags)
	ld hl,(9*256)+2
	ld (pencol),hl
	ld de,(mseY)
	inc e
	;find it, insert it
	
	ld b,1
SymbolWinGoLoop:
	push de
		call SymbolRenderB
		pop de
	ld hl,(pencol)
	ld a,l						;pencol
	cp d						;mseX
	jr c,SymbolWinGoLoopNext
	ld a,h						;penrow
	cp e						;mseY
	jr nc,SymbolWinGoLoopNext
	add a,6
	cp e
	jr nc,SymbolWinGoFound
SymbolWinGoLoopNext:
	inc b
	ld a,$e1
	cp b
	jr nz,SymbolWinGoLoop
SymbolWinGoFound:
	push bc
		call GetTextboxData
		push hl
			dec hl
			ld d,(hl)
			dec hl
			ld e,(hl)
			push de
				ld de,-7
				add hl,de
				push hl
					bcall(_ldhlind)			;get element size
					inc hl
					pop de
				ex de,hl
				ld (hl),e
				inc hl
				ld (hl),d					;update element size
				pop de
			pop hl
		add hl,de
		ld de,(Op6)							;cheap terrible horrible hacks!
		add hl,de							;go to offset
		ld de,1
		ex de,hl
		bcall(_insertmem)
		pop af
	ld (de),a								;insert the char
	call OpenGUIStack						;get AV name into Op1! Clever, eh?
	bcall(_chkfindsym)
	ex de,hl
	ld e,(hl)
	inc hl
	ld d,(hl)
	inc de
	ld (hl),d
	dec hl
	ld (hl),e								;increment appvar size
	jp RenderMain2
SymbolWinCustomRender:
	set textWrite,(iy+sgrFlags)
	ld hl,(9*256)+2
	ld (pencol),hl
	ld b,1
SymbolWinCustomRenderLoop:
	call SymbolRenderB
	inc b
	ld a,b
	cp $e1
	jr nz,SymbolWinCustomRenderLoop
	ret

SymbolRenderB:
	ld a,b
	cp $D6
	ret z
	cp '0'
	jr c,SymbolWinCustomRenderLoopDisp
	cp 'z'+1
	jr nc,SymbolWinCustomRenderLoopDisp
	cp '9'+1
	ret c
	cp 'a'
	ret nc
	cp 'A'
	jr c,SymbolWinCustomRenderLoopDisp
	cp 'Z'+1
	ret c
SymbolWinCustomRenderLoopDisp:
	bcall(_vputmap)
	ld a,(pencol)
	cp 90
	ret c
	ld a,(penrow)
	add a,6
	ld h,a
	ld l,2
	ld (pencol),hl
	ret

CheckSaveInvocation:
	ld a,(OpenFileFlag)
	or a
	ret z
	ld hl,(SavedFilePtr)
	ld a,h
	or l
	jr nz,CheckSaveUpdate

	call GetTextboxData
	ld de,MyType2
	xor a
	call FileSaveAs

	ld a,h
	or l
	jr nz,CheckSaveInvocation_Saved
	xor a
	ld (OpenFileFlag),a
	ret
CheckSaveInvocation_Saved:
	push de
		inc de
		inc de
		ld (SavedFilePtr),de
		pop hl
	bcall(_ldhlind)
	ld (OldSize),hl
	ld a,1
	ld (OpenFileFlag),a
	ret
CheckSaveUpdate:
	;*** update existing pointers and data
	ld hl,(SavedFilePtr)
	ld de,8
	add hl,de
	push hl
		ld hl,-8
		ld de,(OldSize)
		add hl,de
		pop de
	push de
		ex de,hl
		bcall(_deletemem)
		;ld hl,8
		;ld (SavedFilePtr),hl
		;*** update size bytes
		call GetTextboxData
		push bc
			pop hl
		ld de,8
		add hl,de
		ld (OldSize),hl
		push bc
			pop hl
		pop de
	push de
		bcall(_insertmem)
		ld hl,(SavedFilePtr)
		dec hl
		dec hl
		ld de,(OldSize)
		ld (hl),e
		inc hl
		ld (hl),d
		;*** insert required # of byte
		call GetTextboxData
		pop de
	ldir
	;*** now copy those bytes
	;*** update size bytes
	;second, add bc bytes at hl
	ret
GetTextboxData:
	call GUIFindFirst
	ld b,7
CheckSaveUpdateLoop:

	push bc
		call GUIFindNext
		pop bc
	djnz CheckSaveUpdateLoop
	push hl
		bcall(_ldhlind)
		ld de,-9					;7+size word+type byte
		add hl,de
		pop de
	ex de,hl
	push de
		ld de,9
		add hl,de
		pop bc
	; hl = data location
	; bc = data size
	ret
DispAbout:
	call ResetAppPage
	ld hl,SmallWinAbout
	call PushGUIStacks
	ld hl,0
	call GUIMouse
DispAboutClose:
	call ResetAppPage
	ld b,6
	call PopGUIStacks
	jp RenderMain2

LargeWinData:
	.dw LargeWinDataE-LargeWinData
	.db GUIRLargeWin
	.db %11100000
	.db %10110000
	.db %10010000
	.db %10010000
	.db %11110000
	.db "Document DE 7",0
LargeWinDataE:
WinButtons:
	.dw WinButtonsE-WinButtons
	.db GUIRWinButtons
	;[which {-,[],X,00000}, onclick[-] {word}, onclick[] {word}, onclick[X] {word}]
	.db %00100000
	.dw 0,0
	.dw DocDEClose
WinButtonsE:
HotspotNew:
	.dw HotspotOpen-HotspotNew
	.db GUIRHotspot
	.db 0,0,7,7
	.dw NewFile
HotspotOpen:
	.dw HotspotSaveAs-HotspotOpen
	.db GUIRHotspot
	.db 8,0,11,7
	.dw OpenFile
HotspotSaveAs:
	.dw HotspotAbout-HotspotSaveAs
	.db GUIRHotspot
	.db 19,0,9,7
	.dw SaveFileAs
HotspotAbout:
	.dw HotspotAboutE-HotspotAbout
	.db GUIRHotspot
	.db 28,0,9,7
	.dw DispAbout
HotspotAboutE:
HotspotSymbols:
	.dw HotspotSymbolsE-HotspotSymbols
	.db GUIRHotspot
	.db 37,0,8,7
	.dw SymbolWin
HotspotSymbolsE:
TaskbarIcons:
	.dw TaskbarIconsE-TaskbarIcons
	.db GUIRLargeSprite
	.db 0,0,6,8
	.db %00000001,%00000000,%00100000,%00010000,%00001000,%00001000
	.db %01111001,%00110000,%00100111,%11010011,%11001000,%00001000
	.db %01001101,%01001111,%10101100,%11010110,%01101001,%10101000
	.db %01000101,%01000000,%10101111,%11010000,%11001010,%01001000
	.db %01000101,%01000000,%10101000,%01010001,%10001010,%01001000
	.db %01000101,%01000000,%10101000,%01010000,%00001010,%01001000
	.db %01111101,%01111111,%10101111,%11010001,%10001001,%10101000
	.db %00000001,%00000000,%00100000,%00010000,%00001000,%00001000
TaskbarIconsE:
	.db $ff,$ff
NullBox:
	.db 0,9,94,45,%10101010
NullBoxE:
TextBoxHeader:
	.db -1,8,7,95,0,0,0
TextBoxHeaderE:

SmallWinAbout:
	.dw SmallWinAboutE-SmallWinAbout
	.db GUIRSmallWin
	.db 8,8
	.db %11100000
	.db %10110000
	.db %10010000
	.db %10010000
	.db %11110000
	.db "DocDE7 | About",0
SmallWinAboutE:
SmallWinButtons:
	.dw 7+3
	.db GUIRWinButtons
	;[which {-,[],X,00000}, onclick[-] {word}, onclick[] {word}, onclick[X] {word}]
	.db %00100000
	.dw 0,0
	.dw DispAboutClose
SmallWinButtonsE:
SmallWinAboutT1:
	.dw SmallWinAboutT2-SmallWinAboutT1
	.db GUIRText
	.db 1,1,0,"Document DE v7.0",0
SmallWinAboutT2:
	.dw SmallWinAboutT3-SmallWinAboutT2
	.db GUIRText
	.db 1,8,0,"By Kerm Martian",0
SmallWinAboutT3:
	.dw SmallWinAboutT4-SmallWinAboutT3
	.db GUIRText
	.db 1,15,0,"July 25, 2010",0
SmallWinAboutT4:
	.dw SmallWinAboutT5-SmallWinAboutT4
	.db GUIRText
	.db 1,22,0,"www.cemetech.net",0
SmallWinAboutT5:
	.db $ff,$ff

SymbolSmallWindow:
	.dw SymbolSMWinButtons-SymbolSmallWindow
	.db GUIRLargeWin
;	.db 8,8
	.db $e0,$b0,$90,$90,$f0
	.db "DocDE7 | Insert Symbol",0
SymbolSMWinButtons:
	.dw 7+3
	.db GUIRWinButtons
	.db 32
	.dw 0,0,SymbolWinCancel
SymbolSMRender:
	.dw 2+3
	.db GUIRCustom
	.dw SymbolWinCustomRender
SymbolSMHotspot:
	.dw SymbolSMHotspotE-SymbolSMHotspot
	.db GUIRHotspot
	.db 0,0,94,54
	.dw SymbolWinGo
SymbolSMHotspotE:
	.db $ff,$ff

.end
END