; VLIR #5 Source Code for geoWrite 128 v2.2
; Copyright 1999
; By Todd S. Elliott

.noglbl
.noeqin
.if	Pass1		;Only need to include these files during the first pass
.include	WheelsSyms		;get GEOS definitions
.include	geosMac		;get GEOS macro definitions
.include	WheelsEquates
;.include	128sym		;get GEOS 128 definitions

;a7L - wheels 128 Flag
progDrive = $d7
dataDrive = $d8

; geoWrite equates - subject to change anytime
setDataDevice 	= $0dae	; calls setDevice using dataDrive variable
setProgDevice 	= $0daa	; calls setDevice using progDrive variable
toggleZP 		= $251b	; toggles the state of Zero Page
nameBuffer 	= $2e11	; filename buffer
CNameBuffer	= $2e00	; current dataname buffer

; VLIR #5 equates - may change
pageWidth		= $2df8	; current page width for a geoWrite VLIR record (word)
crflag		= $2dfa	; flag to determine if the copyright message has been printed.
CPageHeight	= $2dfb	; Copy of register e9 - Height of each page (word)
TSDFHdr		= $2dfd	; T/S of a datafile's file header (word)
gPFlag		= $2dff	; flag to indicate that geoPublish messed with the datafile
FirstPage		= $e2	; first page to print in document (word)
TitlePage		= $e4	; bit 7 set - first page is a title page
			; bit 6 set - NLQ fixed width spacing on
HeaderHeight	= $e5	; Height of each page's header (word)
FooterHeight	= $e7	; Height of each page's footer (word)
PageHeight	= $e9	; Height of each page
TSFlag		= $f7	; bit 7 set to indicate error or availability of text scrap
			; Z flag set to indicate no error or no text scrap available
TSiZe		= $41e4	; Size of text scrap
fileData		= $4310	; for v2.1 file data start address

; Wheels 128 equates
cableType		= $9073	; To determine whether a RAMLink is present.
.endif
.eqin
.glbl

lb3244:
	jmp RecoverFile	; (lb36d3)
	jmp InitGW128	; (lb325c) creates the create,open and quit dialog box and
			; handles its options, etc.
lb324a:
	jmp RenamFile
lb324d:
	jmp openServiceRoutine
lb3250:
	jmp printInfoBox	; prints out the geoWrite 128's info dialog box
lb3253:
	jmp runDA		; runs DA's
lb3256:
	jmp quitGeoWrite	; quits the application and enters the desktop
lb3259:
	jmp AdjPageWidths	; checks VLIR datafile record #63 and checks its integrity

InitGW128:		; initializes the geoWrite 128 v2.1 screen and jumps to the create, open and quit
		; dialog box.
	jsr $1baf	; turn off text cursor.
	jsr DrawStripes
	jsr i_GraphicsString
.byte $05,$02	; newPattern (checkered)
.byte $01,$00,$00,$24	; pen to x$0000,y$24
.byte $03,$7f,$02,$c7		; rectangle to x$027f,y$c7
.byte $00			; delimiter
	jsr printCopyrightMsg	; prints the copyright message.
	lda #<createOpenQuitDB
	ldx #>createOpenQuitDB
	jsr $2538		; creates a dialog box
	lda r0L		; checks to see what the user did on the dialog box
	cmp #$01		; did the user click on the CREATE icon?
	bne 1$
	jmp createDocRoutine	; this routine creates a blank document.

1$:	cmp #$05		; did the user click on the OPEN icon?
	bne 2$
	jmp fileRequestor	; calls the dbgetfiles DB, and opens the file.
2$:	jmp $0ec2		; the user clicked on the QUIT icon. - change this?
			; there is a QUIT handler - no need to go to VLIR #0

createOpenQuitDB:	; dialog box data - THE dialog box that shows when geoWrite first opens.
		; (lb328d)
.byte $81		; default position with solid pattern.
.byte $0b		; DBTXTSTR
.byte $10,$10	; x16,y16
.word lb3f03	; - pointer to text
.byte $12		; DBUSRICON (Create icon)
.byte $02,$18	; x2,y24
.word createIconTable
.byte $0b		; DBTXTSTR
.byte $47,$22	; x71,y34
.word lb3f1b	; - pointer to text (new document)
.byte $05		; OPEN icon
.byte $02,$30	; x2,y48
.byte $0b		; DBTXTSTR
.byte $47,$3a	; x71,y58
.word lb3f28	; - pointer to text (existing document)
.byte $12		; DBUSRICON (Quit icon)
.byte $02,$48	; x2,y72
.word quitIconTable
.byte $0b		; DBTXTSTR
.byte $47,$52	; x71,y82
.word lb3f3a	; - pointer to text (to deskTop)
.byte $00		; delimiter

createIconTable:	; Create icon table info
.word bitmapCreateIcon
.byte $00,$00	; default x,y
.byte $86,$10	; default values w/ bitmap doubling
.word iconCreateService

quitIconTable:	; Quit icon table info
.word bitmapQuitIcon
.byte $00,$00	; default x,y
.byte $86,$10	; default values w/ bitmap doubling
.word iconQuitService

iconCreateService:	; icon service routine
	lda #$01	; Create icon number
	bne label1
iconQuitService:	; icon service routine
	lda #$02	; Quit icon number
label1:	sta sysDBData
	jmp RstrFrmDialog

bitmapCreateIcon:	; create icon bitmap data
.byte $05,$ff,$82,$fe,$80,$04,$00,$82,$03,$80,$04,$00,$b8,$03,$8f,$80,$00,$00
.byte $00,$03,$98,$c0,$00,$00,$60
.byte $03,$98,$1f,$9e,$3c,$f1,$e3,$98,$1c,$33,$66,$63,$33,$98,$18,$33,$3e,$63
.byte $33,$98,$18,$3f,$66,$63,$f3,$98,$18,$30,$66
.byte $63,$03,$98,$d8,$33,$66,$63,$33,$8f,$98,$1e,$3e,$39,$e3,$80,$04,$00,$82
.byte $03,$80,$04,$00,$81,$03,$06,$ff,$81,$7f,$05,$ff

bitmapQuitIcon:	; quit icon bitmap data
.byte $05,$ff,$82,$fe,$80,$04,$00,$82,$03,$80,$04,$00,$be,$03,$80,$07,$c0,$0c
.byte $c0,$03,$80,$0c,$60,$00,$c0,$03,$80,$0c
.byte $6c,$dd,$e0,$03,$80,$0c,$6c,$cc,$c0,$03,$80,$0c,$6c,$cc,$c0,$03,$80,$0c
.byte $6c,$cc,$c0,$03,$80,$0c,$6c,$cc,$c0,$03,$80,$0d,$ed,$cc,$c0,$03,$80
.byte $07,$c7,$cc,$70,$03,$80,$00,$60,$00,$00
.byte $03,$80,$04,$00,$81,$03,$06,$ff,$81,$7f,$05,$ff

printCopyrightMsg:		; prints the copyright message. (lb3379)
	lda crflag		; set the copyright message flag
	bne 1$
	dec crflag
	lda #$20
	sta r1H
	lda #$09
	sta r1L		; r1 points to $2009?
	jsr $1f05		; calls system font in record 1
	lda #$00		; plain text
	sta currentMode
	jsr i_GraphicsString
; uses bitmap doubling
.byte $05,$00	; newPattern (blank)
.byte $01,$44,$80,$94	; pen to x$8044,y$94
.byte $03,$fb,$a0,$bf	; rectangle to x$a0fb,y$bf
.byte $01,$44,$80,$94	; pen to x$8044,y$94
.byte $07,$fb,$a0,$bf	; frames the rectangle
.byte $00		; delimiter
	jsr i_PutString
.byte 128,128,161	; x$8080,y$a1 w/ bitmap doubling
.byte 24,45,32,103,101,111,87,114,105,116,101,32,45,22,82,128,183
; BOLDON
; "- geoWrite -"
; GOTOXY x$8052,y$b7 (bitmap doubling)
.byte 27,67,111,112,121,114,105,103,104,116,32,49,57,56,55,32,66,101,114,107
.byte 101,108,101,121,32,83,111,102,116,119,111,114,107,115,0
; PLAINTEXT
; "Copyright 1987 Berkeley Softworks" and terminates
1$:	rts

createDocRoutine:		; the user wants to create a geoWrite document (lb33e1)
	lda #$12		; turn on DBUSRICON
	jsr queryFilename	; calls a dialog box prompting the user for filename, etc.
	cmp #$02		; did the user input a filename?
	beq 3$
	lda #>nameBuffer
	sta r6H
	lda #<nameBuffer
	sta r6L		; r6 points to the filename itself
	jsr $27b0		; FindFile
	cpx #$05		; FILE NOT FOUND error check
	beq 1$
	txa		; check for disk error
	bne 2$
	lda #<lb3f67		; file exists text prompt
	ldy #>lb3f67		; fix this as to replace it instead? - TEMP file concept?
	jsr $2314		; prints a textual dialog box
	clv
	bvc createDocRoutine	; BRA macro
1$:	jsr createNewFile	; creates the new datafile and opens it
	txa		; check error
	bne 2$
	stx $de		; what are these locations?
	stx $d3		; Is this a variable for current VLIR #?
	stx currentMode	; plain text
	stx TitlePage
	stx HeaderHeight
	stx HeaderHeight+1
	stx FooterHeight
	stx FooterHeight+1
	stx FirstPage+1
	inx
	stx FirstPage
	lda CPageHeight+1
	sta PageHeight+1
	lda CPageHeight
	sta PageHeight
	lda #$02
	sta pageWidth+1
	lda #$7f
	sta pageWidth		; pageWidth has the maximum value of 640
	lda #$00
	sta gPFlag		; geoPublish flag
	jsr $2393		; something to do with fonts - not sure
	jsr SetScrollSprite	; sets up sprite 6
	jmp printDataName	; prints the filename onscreen and RTS's back to record 0
2$:	lda #<lb404a
	ldy #>lb404a		; pointer to text (creating file)
	jsr $233a		; makes a dialog box
3$:	jmp InitGW128	; and repeats operation of the create, open and quit DB

setup4DB:			; sets up the dialog box table for four drives.
	lda #$00
	sta nameBuffer	; delimit the filename buffer immediately
	sta r7H
	sta setDiskIcon
	jsr checkDisk		; checks for a ramdisk, etc.
	bcc 1$
	lda #$06
	sta setDiskIcon	; restores the DISK icon entry
1$:	jsr setDataDevice
	jsr readDiskName	; reads in the disk name and stores it
	ldx #$08
2$:	lda iconLTable-8,x
	sta r0L
	lda iconHTable-8,x
	sta r0H
	lda driveType-8,x	; check to see if a drive is present
	beq 3$
	cpx curDrive		; is it the current disk device?
	bne 4$
	lda onLTable-8,x	; select an ON icon for that drive entry
	sta r1L
	lda onHTable-8,x
	sta r1H
	clv
	bvc 5$		; BRA macro
4$:	lda offLTable-8,x	; select an OFF icon for that drive entry
	sta r1L
	lda offHTable-8,x
	sta r1H
	clv
	bvc 5$		; BRA macro
3$:	lda #$00
	sta r1L
	sta r1H		; r1 points to nothing
5$:	ldy #$00
	lda r1L
	sta (r0L),y
	iny
	lda r1H
	sta (r0L),y		; sets up the icontable
	inx
	cpx #$0c		; are we done with 8-11 yet?
	bne 2$
	lda #>nameBuffer
	sta r5H
	lda #<nameBuffer
	sta r5L		; r5 points to the nameBuffer
	rts

fileRequestor:		; calls the file requestor and opens a datafile
; four drive support for GEOS platform
; Modified from the code as written by Maurice Randall and shown in Commodore World #7-8 issues.
	jsr setup4DB		; Init the drive table entries
	lda #$07
	sta r7L		; select application data files
	lda #>permName
	sta r10H
	lda #<permName
	sta r10L		; r10 points to permanent name (Write Image)
	ldy #$06
1$:	lda restoreDBTable,y
	sta dBTable,y
	dey
	bpl 1$
	lda #<dBTable
	ldx #>dBTable	; dialog box table w/ dbgetfiles
	jsr toggleZP		; necessary?
	jsr $2538		; issues a dialog box and shows the file requestor
	jsr toggleZP		; necessary?
	lda r0L		; what did the user click on?
	cmp #$02		; was it a CANCEL icon?
	bne 2$
	jmp InitGW128	; (lb356b?) - jumps back to the create, open and quit DB
2$:	cmp #$06
	bne 3$		; did the user click on the DISK selection?
	lda curType		; check out the disk device to determine if it's removable media.
	cmp #$20		; only CMD HD and above are exempt.
	bcs 4$
	lda #<lb3f45
	ldy #>lb3f45		; pointer to text (Insert new disk)
	jsr $2314		; does a simple dialog box
4$:	jsr $27ad		; OpenDisk the new disk
	jmp fileRequestor
3$:	cmp #$05		; did the user click on the OPEN icon?
	bne 5$
	jmp openServiceRoutine	; otherwise, handle the OPEN icon selection
5$:	sta dataDrive	; otherwise, a drive icon was clicked on.
	jsr setDataDevice	; sets the new disk device
	jsr $27ad		; openDisk- due to a bug in setDevice routine, this is called
	jmp fileRequestor	; and repeat the DB again.

; table of pointers to relevant data
iconLTable:
.byte [aDriveTable,[bDriveTable,[cDriveTable,[dDriveTable
iconHTable:
.byte ]aDriveTable,]bDriveTable,]cDriveTable,]dDriveTable
onLTable:
.byte [onAIcon,[onBIcon,[onCIcon,[onDIcon
onHTable:
.byte ]onAIcon,]onBIcon,]onCIcon,]onDIcon
offLTable:
.byte [offAIcon,[offBIcon,[offCIcon,[offDIcon
offHTable:
.byte ]offAIcon,]offBIcon,]offCIcon,]offDIcon

; Main DB Table
qDBTable:		; for the query filename dialog box.
.byte $81		; default DB position and pattern
.byte $0d		; DBGETSTRING
.byte $10		; xpos
.byte $30		; ypos

dBTable:
.byte $00,$00,$00,$00,$00,$00,$00
.byte $0b		; DBTXTSTR
.byte $82		; xpos
.byte $0a		; ypos
.word lb3fd2	; - pointer to text (on disk:)
.byte $0b		; DBTXTSTR
.byte $82		; xpos
.byte $14		; ypos
.word diskName	; - pointer to diskname
.byte $02		; CANCEL icon
.byte $11		; card xpos
.byte $4c		; ypos
renTable:		; for the NEW Name or Rename routines
.byte $12		; DBUSRICON
.byte $11		; card xpos
.byte $3b		; ypos
.word aDriveTable
.byte $12		; DBUSRICON
.byte $14		; card xpos
.byte $3b		; ypos
.word bDriveTable
.byte $12		; DBUSRICON
.byte $11		; card xpos
.byte $44		; ypos
.word cDriveTable
.byte $12		; DBUSRICON
.byte $14		; card xpos
.byte $44		; ypos
.word dDriveTable
setDiskIcon:	; modified by open DB routines
.byte $06		; DISK icon
.byte $11		; card xpos
.byte $2a		; ypos
.byte 0		; delimiter

; these are icon table entries for the dialog box and are self-modified to
; reflect whether a drive is present and also whether it is on.
aDriveTable:
.word offAIcon
.byte $00,$00	; default pos
.byte $83,$08	; height/width
.word iconADrive
bDriveTable:
.word offBIcon
.byte $00,$00	; default pos
.byte $83,$08	; height/width
.word iconBDrive
cDriveTable:
.word offCIcon
.byte $00,$00	; default pos
.byte $83,$08	; height/width
.word iconCDrive
dDriveTable:
.word offDIcon
.byte $00,$00	; default pos
.byte $83,$08	; height/width
.word iconDDrive

; these are the routines that get called when the user clicks on these icons
iconADrive:
	lda #$08
.byte $2c
iconBDrive:
	lda #$09
.byte $2c
iconCDrive:
	lda #$0a
.byte $2c
iconDDrive:
	lda #$0b
	sta sysDBData
	jmp RstrFrmDialog	; exits the DB and returns control to application

; icon bitmap data follows
; 33 bytes for each icon bitmap
offAIcon:
.byte $98		; 24 unique bytes follow
.byte %11111111,%01111110,%11111100
.byte %10000000,%10000001,%00000110
.byte %10000000,%10000001,%00000110
.byte %10000001,%11111111,%10000110
.byte %10000001,%00000000,%10000110
.byte %10000001,%00000000,%10000110
.byte %11111101,%01111110,%10111110
.byte %00111111,%11111111,%11111110

offBIcon:
.byte $98		; 24 unique bytes follow
.byte %11111101,%11111111,%01111110
.byte %10000001,%00000000,%10000011
.byte %10000001,%00000000,%10000011
.byte %10000001,%11111111,%00000011
.byte %10000001,%00000000,%10000011
.byte %10000001,%00000000,%10000011
.byte %11111101,%11111111,%01111111
.byte %00111111,%11111111,%11111111

offCIcon:
.byte $98		; 24 unique bytes follow
.byte %11111110,%11111111,%01111100
.byte %10000001,%00000000,%10000110
.byte %10000001,%00000000,%00000110
.byte %10000001,%00000000,%00000110
.byte %10000001,%00000000,%00000110
.byte %10000001,%00000000,%10000110
.byte %11111110,%11111111,%01111110
.byte %00111111,%11111111,%11111110

offDIcon:
.byte $98		; 24 unique bytes follow
.byte %11111101,%11111111,%01111110
.byte %10000001,%00000000,%10000011
.byte %10000001,%00000000,%10000011
.byte %10000001,%00000000,%10000011
.byte %10000001,%00000000,%10000011
.byte %10000001,%00000000,%10000011
.byte %11111101,%11111111,%01111111
.byte %00111111,%11111111,%11111111

onAIcon:
.byte $98		; 24 unique bytes follow
.byte %11111111,%10000001,%11111100
.byte %11111111,%01111110,%11111110
.byte %11111111,%01111110,%11111110
.byte %11111110,%00000000,%01111110
.byte %11111110,%11111111,%01111110
.byte %11111110,%11111111,%01111110
.byte %11111110,%11111111,%01111110
.byte %00111111,%11111111,%11111110

onBIcon:
.byte $98		; 24 unique bytes follow
.byte %11111110,%00000000,%11111110
.byte %11111110,%11111111,%01111111
.byte %11111110,%11111111,%01111111
.byte %11111110,%00000000,%11111111
.byte %11111110,%11111111,%01111111
.byte %11111110,%11111111,%01111111
.byte %11111110,%00000000,%11111111
.byte %00111111,%11111111,%11111111

onCIcon:
.byte $98		; 24 unique bytes follow
.byte %11111111,%00000000,%11111100
.byte %11111110,%11111111,%01111110
.byte %11111110,%11111111,%11111110
.byte %11111110,%11111111,%11111110
.byte %11111110,%11111111,%11111110
.byte %11111110,%11111111,%01111110
.byte %11111111,%00000000,%11111110
.byte %00111111,%11111111,%11111110

onDIcon:
.byte $98		; 24 unique bytes follow
.byte %11111110,%00000000,%11111110
.byte %11111110,%11111111,%01111111
.byte %11111110,%11111111,%01111111
.byte %11111110,%11111111,%01111111
.byte %11111110,%11111111,%01111111
.byte %11111110,%11111111,%01111111
.byte %11111110,%00000000,%11111111
.byte %00111111,%11111111,%11111111

openServiceRoutine:		; handles the OPEN selection
	jsr setDataDevice
	lda nameBuffer	; checks to see if the user clicked on a filename
	bne 1$
	jmp InitGW128	; (lb356b?) goes back to the create, open and quit DB
1$:	lda #>nameBuffer
	sta r6H
	lda #<nameBuffer
	sta r6L		; r6 points to filename
	jsr $27b0		; FindFile
	txa		; check error
	beq 2$
	jmp openFErr		; prints dialog box and exits
2$:	lda $8414		; get t/s for its file header
	sta TSDFHdr+1
	lda $8413		; in dirEntryBuf
	sta TSDFHdr
	ldy #$00
	lda (r5L),y		; check to see if write protect is on
	and #$40
	beq 3$
	lda r5H
	pha
	lda r5L
	pha
	lda #<lb35fc
	ldx #>lb35fc		; pointer to text warning that the file is write protected
	jsr $2538		; issues a dialog box
	pla
	sta r5L
	pla
	sta r5H
	lda r0L
	cmp #$02		; did the user click on the CANCEL icon?
	beq quit		; (lb356b) goes back to the create, open or quit DB
3$:	lda #$02
	sta pageWidth+1
	lda #$7f
	sta pageWidth	; pageWidth has maximum width of 640
	jsr CheckDFVer	; check the datafile's version
	php
	cpy #$00
	bne 7$
	plp
	beq 5$
	bcc 4$
	lda #<lb3f86
	ldy #>lb3f86		; pointer to text - version higher than 2.1
	jsr $2314		; issues a dialog box
	clv
	bvc quit		; BRA macro
4$:	ldy #$31
	jsr convertDataFile	; converts the file to version v2.1
	txa		; checks error
	bne quit
	beq 6$
5$:	jsr lb356e		; extracts some info from the file header and stashes it in $e2
6$:	jsr $260c		; r0 points to filename
	jsr toggleZP
	jsr OpenRecordFile	; opens the VLIR datafile
	jsr toggleZP
	txa		; check error
	bne openFErr
	jsr AdjPageWidths	; checks VLIR datafile record #63 and checks its integrity
	lda #$00
	sta $d3		; What are these locations?
	sta $de
	lda #$c7		; scanline 199
	sta windowBottom
	jsr SetScrollSprite	; sets up sprite #6
	jmp printDataName	; prints the filename onscreen and RTS's back to record 0

7$:	plp
	tya
	tax
openFErr:
	lda #<lb403d
	ldy #>lb403d		; pointer to text (opening file error)
	jsr $233a		; issues a dialog box

quit:	jmp InitGW128	; enters the create, open and quit db

lb356e:		; extracts some info in the file header and stashes it in $e2
		; contains global variables for each geoWrite document
	jsr i_MoveData
.byte $89,$81	; word 8189 SOURCE
.byte $e2,$00	; word $00e2 DEST
.byte $09,$00	; move 9 bytes
	rts

permName:	; appl-data permanent name
.byte 87,114,105,116,101,32,73,109,97,103,101,0
; "Write Image" and terminates

lb35fc:		; dialog box info
.byte $81		; default dialog box w/ solid pattern
.byte $0b		; DBTXTSTR
.byte $10		; xpos
.byte $10		; ypos
.word lb3fdd	; - pointer to text (WARNING!)
.byte $0b		; DBTXTSTR
.byte $10		; xpos
.byte $20		; ypos
.word lb3fe7	; - pointer to text - (file is write protected)
.byte $05		; OPEN icon
.byte $01		; card xpos
.byte $48		; ypos
.byte $02		; CANCEL icon
.byte $11		; card xpos
.byte $48		; ypos
.byte $00		; delimiter

checkDisk:		; checks for a ramdisk and whether a datafile is on the same disk
		; as the application is. (lb3676)
	ldy dataDrive
	cpy progDrive	; checks to see if both are on the same disk
	beq 3$
	lda $8486,y		; checks the dataDrive's disk device under driveType table
	bmi 1$		; checks for the presence of a ramdisk
2$:	sec		; It's not a ramdisk or that it is a separate disk device
	rts
1$:	cmp #$83		; is it RL1581 driver?
	bcc 3$		; branch if it's a 1541/71 ramdisk
	bne 2$		; a native ramdisk is being used, so branch out
	lda a7L		; check wheels flag first before calling wheels specific routines
	beq 3$		; if equal, then it's a GEOS 128 1581 ramdisk
	lda cableType	; check RAMLink status
	bmi 2$		; branch if it is a 1581 RL ramdisk
3$:	clc		; it's a ramdisk or that the datafile is in the same disk
	rts

readDiskName:		; reads in disk name and stores it (lb36a9)
	lda dataDrive		; check which disk device we are using right now
	cmp #$08		; dev. #8?
	bne 1$
	lda #]DrACurDkNm
	sta r0H
	lda #[DrACurDkNm
	sta r0L		; r0 points to DrACurDkNm
	bne 4$		; extractDiskName
1$:	cmp #$09		; dev. #9?
	bne 2$
	lda #]DrBCurDkNm
	sta r0H
	lda #[DrBCurDkNm
	sta r0L		; r0 points to DrBCurDkNm
	bne 4$		; extractDiskName
2$:	cmp #$0a		; dev. #10?
	bne 3$
	lda #]DrCCurDkNm
	sta r0H
	lda #[DrCCurDkNm
	sta r0L		; r0 points to DrCCurDkNm
	bne 4$		; extractDiskName
3$:	lda #]DrDCurDkNm
	sta r0H
	lda #[DrDCurDkNm
	sta r0L		; r0 points to DrDCurDkNm
4$:	ldy #$00
5$:	lda (r0L),y
	cmp #$a0		; check for padded spaces
	beq 6$
	sta diskName,y	; store in disk name.
	iny
	bne 5$
6$:	lda #$00
	sta diskName,y	; and delimit it
	rts

RecoverFile:		; (lb36d3)
	jsr GotoFirstMenu	; the first entry in the jump table
	bit $dd		; What's there?
	bpl 1$
	jsr i_MoveData
.word	CNameBuffer		; SOURCE
.word	nameBuffer		; DEST - data filename
.byte	$11,$00		; word $0011 -COUNT- move 17 bytes for filename, including NULL
	jmp openServiceRoutine
1$:	lda #<lb4005		; prints Cannot Recover DB
	ldy #>lb4005
	jmp $2314		; issues DB

; Rename File Routine
RenamFile:			; (lb36ed)
	jsr GotoFirstMenu
	lda #$00		; Turn off DBUSRICON
	jsr queryFilename
	cmp #$02		; Did the User CANCEL?
	beq 1$
	lda #]nameBuffer
	sta r6H
	lda #[nameBuffer
	sta r6L		; r6 points to filename
	jsr $27b0		; FindFile
	cpx #$05		; FILE NOT FOUND error check
	bne 2$
	lda #]CNameBuffer
	sta r6H
	lda #[CNameBuffer
	sta r6L		; r6 points to $2e00 - previous name buffer
	jsr $260c		; r0 points to nameBuffer w/ new filename
	jsr toggleZP
	jsr RenameFile
	jsr toggleZP
	jsr printDataName	; prints it out onscreen
3$:	jsr SetScrollSprite	; sets up sprite #6
1$:	rts

2$:	lda #<lb40a7
	ldy #>lb40a7		; File Exists error message
	jsr $2314		; issues DB
	clv
	bvc 3$		; BRA Macro

queryFilename:		; calls up the dialog box prompting the user for a new filename
	sta	renTable	; turn off/on the 4 drive icons
	jsr setup4DB		; sets up the DB table for 4 drives
	ldy #$06
1$:	lda replaceDBTable,y
	sta dBTable,y
	dey
	bpl	1$
	lda #<qDBTable
	ldx #>qDBTable	; queryDBTable - dialog box data located at $37a0
	jsr $2538		; does some vdc stuff and calls a doDlgBox
	lda r0L
	cmp #$0d		; was it DBGETSTRING?
	beq 4$
	cmp #$08		; have the drive icons been clicked on?
	bcc 2$
	sta dataDrive	; otherwise, a drive icon was clicked on.
	jsr setDataDevice	; sets the new disk device
	jsr $27ad		; openDisk- due to a bug in setDevice routine, this is called
	jmp queryFilename+3	; and repeat the DB again.
2$:	cmp #$06
	bne 5$		; has the user pressed on the DISK icon?
	lda curType		; check out the disk device to determine if it's removable media.
	cmp #$20		; only CMD HD and above are exempt.
	bcs 3$
	lda #<lb3f45
	ldy #>lb3f45		; (Insert new disk)
	jsr $2314		; preps the text pointers and calls doDlgBox
3$:	jsr $27ad		; OpenDisk - open the new disk and repeat cycle
	clv
	bvc queryFilename+3	; BRA instruction
4$:	lda nameBuffer	; checks the filename, making sure it is not empty.
	bne 5$
	lda #$02		; dialog box result
	rts
5$:	lda r0L
	rts

replaceDBTable:	; self-modifying code to modify dBTable
.byte r5L		; pointer to where user inputted text will be stored
.byte $10		; maximum of 16 chars
.byte $0b		; DBTXTSTR
.byte $10		; xpos
.byte $20		; ypos
.word lb4020	; pointer to text (enter new filename:)

restoreDBTable:	; self-modifying code to restore dBTable
.byte $81		; default DB position and pattern
.byte $10		; DBGETFILES
.byte $04
.byte $04		; default x,y pos
.byte $05		; OPEN icon
.byte $11		; card xpos
.byte $19		; ypos

createNewFile:		; creates a new VLIR datafile and processes it for the user
			; (lb37c1)
	lda CPageHeight+1
	sta lb38c4+1
	lda CPageHeight
	sta lb38c4		; modifies this record source
	lda #]nameBuffer
	sta dFileHeader+1
	lda #[nameBuffer
	sta dFileHeader	; points to the new filename
	lda #>dFileHeader
	sta r9H
	lda #<dFileHeader
	sta r9L		; r9 points to $3834 - the VLIR file header itself for a datafile
	lda #$00		; tell it to search for the first available direntry
	sta r10L
	jsr toggleZP
	jsr SaveFile		; creates an empty VLIR file with filename supplied by user.
	jsr toggleZP
	txa		; check for disk error
	bne 1$
	lda #]nameBuffer
	sta r6H
	lda #[nameBuffer
	sta r6L		; r6 points to filename
	jsr $27b0		; FindFile
	txa		; check disk error
	bne 1$
	jsr CheckDFVer	; check the datafile's version
	tya
	tax		; check error
	bne 1$
	lda $8414
	sta TSDFHdr+1
	lda $8413		; in dirEntryBuf
	sta TSDFHdr
	jsr $260c		; r0 points to filename
	jsr toggleZP
	jsr OpenRecordFile	; opens the datafile
	jsr toggleZP
	txa		; check error
	bne 1$
	lda #$80
2$:	pha
	jsr $27a7		; AppendRecord 128 times - creates blank ones
	pla
	sec
	sbc #$01
	bne 2$
	jsr $27aa		; UpdateRecordFile w/ time stamp info
	lda #$00		; VLIR record #0
	jmp PointRecord	; opens the actual record in the VLIR data file
1$:	rts

dFileHeader:	; VLIR file header for a geoWrite data file (256 bytes long).
		; looks incomplete to me (lb3834)
.byte $00,$00	; point to the filename
.byte $03,$15	; width/height of icon
.byte $bf,$ff,$ff,$ff,$80,$00,$01,$8f,$ff,$01,$88,$01,$01,$8b,$ff,$c1,$8a,$00
.byte $41,$8a,$ff,$f1,$8a,$80,$11,$8a,$8e,$11,$8a,$80				; icon bitmap data
.byte $11,$8a,$bf,$91,$8a,$80,$11,$8a,$9f,$11,$8a,$80,$11,$8a,$bf,$91,$8e,$80
.byte $11,$82,$bf,$91,$83,$80,$11,$80,$80,$11,$80,$ff,$f1,$ff,$ff,$ff
.byte $83		; USR filetype
.byte $07		; APPL DATA
.byte $01		; VLIR structure
.byte $00,$00	; start address
.byte $ff,$ff	; end address
.byte $00,$00	; initialization address
.byte 87,114,105,116,101,32,73,109,97,103,101,32,86,50,46,49
; application filename - "Write Image V2.1"
.block 24
.byte 103,101,111,87,114,105,116,101,32,32,32,32,86,50,46,49
; "geoWrite    V2.1" - name of parent application
.block 4
lb38bd:		; global variables for a document - used by the convert datafile routine
.byte $01
.block 6
lb38c4:		; Page Height - modified by create file routine.
.byte $f0,$02

convertDataFile:		; opens the datafile and converts the file to version v2.1 (lb38c6)
	sty lb3fbf		; modifies this file
	ldx #$00
	cmp #$31
	beq 1$
	dex
1$:	stx VerFlag		; version identifier
	lda #<convertFileDBTable
	ldx #>convertFileDBTable	; pointer to DB index table telling the user
			; it is converting the file to version v2.1
	jsr $2538		; do a dialog box
	lda sysDBData
	cmp #$02		; did the user click on the CANCEL icon?
	bne 2$
	jmp lb3968		; loads .x with $ff and RTS
2$:	lda #$32		; "2"
	sta $815a		; modifies the fileheader as to change its version string
	lda lb3fbf		; currently at "1"
	sta $815c
	bit VerFlag
	bmi 3$
	jsr i_MoveData
.word lb38bd		; SOURCE - global variables controlling document
.byte $89,$81		; word $8189 DEST - modifies the file header
.byte $09,$00		; move 9 bytes
3$:	jsr lb356e		; extracts global variables from file header into $e2
	lda TSDFHdr+1
	sta r1H
	lda TSDFHdr
	sta r1L
	lda #$81
	sta r4H
	lda #$00
	sta r4L		; r4 points to $8100
	jsr $27b6		; PutBlock the fileheader
	txa		; check error
	bne 10$
	jsr $260c		; r0 points to filename
	jsr toggleZP
	jsr OpenRecordFile	; opens the filename
	jsr toggleZP
	txa		; check error
	bne 10$
	lda #$00		; open the first record
	jsr PointRecord
	txa		; check error
	bne 10$
4$:	jsr lb3978		; reads in a record, modifies & writes it back
	txa		; check error
	bne 10$
	jsr NextRecord	; points to the next VLIR record in the datafile
	tya		; is it empty?
	bne 4$
	lda #$3d		; Load in the header record
	jsr PointRecord
	tya		; is it empty?
	beq 5$
	jsr lb3978		; reads in the record, modifies and writes it back
	txa		; check error
	bne 10$
5$:	lda #$3e		; Load in the footer record
	jsr PointRecord
	tya		; is it empty?
	beq 6$
	jsr lb3978		; reads in the record, modifies and writes it back
	txa		; check error
	bne 10$
6$:	jsr $279e		; closes the VLIR datafile
	txa		; check error
	bne 10$
	rts

; handles disk errors
10$:	lda #<lb3fc1
	ldy #>lb3fc1		; pointer to DB text (converting file error)
	jsr $233a		; issues a DB
	jsr $279e		; closes the VLIR datafile
lb3968:
	ldx #$ff
	rts

convertFileDBTable:		; DB index table
.byte $81		; default DB with solid pattern
.byte $0b		; DBTXTSTR
.byte $10		; xpos
.byte $10		; ypos
.word lb3fa0	; - pointer to text (converting file to v2.1)
.byte $01		; OK icon
.byte $01		; card xpos
.byte $48		; ypos
.byte $02		; CANCEL icon
.byte $11		; card xpos
.byte $48		; ypos
.byte $00		; delimiter

lb3978:			; reads in a record, modifies it, and writes it back.
	lda #]fileData; $43
	sta r7H
	lda #[fileData; $10
	sta r7L		; r7 points to $4310 - area where the record will go into RAM
	lda #$1b
	sta r2H
	lda #$58
	sta r2L		; r2 points to $1b58 - maximum bytes of each record
	bit VerFlag
	bmi 1$		; it is testing to see if it has a v1.x ruler escape or a v2.x one
	lda #]fileData
	sta r7H
	lda #[fileData+7
	sta r7L		; r7 points to $4317 - area where the record will go into RAM
	lda #$1b
	sta r2H
	lda #$51
	sta r2L		; r2 points to $1b51 - maximum bytes of each record
1$:	jsr toggleZP
	jsr ReadRecord	; reads the raw data
	jsr toggleZP
	txa		; check error
	bne 4$
	bit VerFlag
	bmi 2$
	jsr lb39da		; reads some info off the file and acts on it.
2$:	lda lb3fbf		; is it a version V2.'1' or V2.'0'?
	cmp #$30		; "0"
	beq 3$
	jsr lb3a09		; converts v2.0 ruler escapes to v2.1's
3$:	sec
	lda r7L
	sbc #[fileData
	sta r2L
	lda r7H
	sbc #]fileData
	sta r2H		; r2 points to actual data the record has in bytes
	lda #]fileData
	sta r7H
	lda #[fileData
	sta r7L		; r7 points to actual start location for geoWrite data
	jsr toggleZP
	jsr WriteRecord	; writes back the modified data
	jsr toggleZP
4$:	rts

lb39da:			; not sure yet, but acts on the info contained in the file
	ldy #$00		; we're dealing with v1.x ruler escapes.
1$:	lda fileData+7,y
	sta fileData+1,y
	iny
	cpy #$14		; move 20 bytes of a v1.x rule escape down to start of fileData
	bne 1$
	lda #$11		; ESC RULER
	sta fileData
	lda fileData+2	; left margin
	sta fileData+22
	lda fileData+1		; left margin
	sta fileData+21
	lda #$00
	sta fileData+23	; right margin
	lda #$00
	sta fileData+24	; right margin
	sta fileData+25	; first set of tabs
	sta fileData+26
	rts

lb3a09:		; Reads in a VLIR datafle record, skipping over formatting codes
			; and modifying only the ruler escapes
			; converts v2.0 ruler escapes to v2.1's
	lda r7H	; actual end address of loaded file data
	sta $d1
	lda r7L
	sta $d0
	jsr $25a1	; assigns $20 to point to $4310 -the pointer to actual geoWrite data
5$:	jsr $26d7	; CMPs $20 against $d0 - are we done reading the file data?
	bcc 4$
	beq 4$
	ldy #$00
	lda ($20),y	; read in an individual byte of geoWrite data contained in a record.
	cmp #$11	; is it a ESC RULER byte?
	beq 3$
	cmp #$10	; is it a ESC GRAPHICS byte?
	bne 1$
	jsr $25e1	; increments $20 once
1$:	cmp #$17	; is it a NEWCARDSET byte?
	bne 2$
	jsr $25db	; increments $20 three times
2$:	jsr $25e1	; increments $20 once
	clv
	bvc 5$	; BRA macro
3$:	jsr lb3a41	; adds $50 to values in ruler escapes?
	jsr $25f7	; adds $1b to values in $20 - to skip over the Ruler Escape
	clv
	bvc 5$	; BRA macro
4$:	rts

lb3a41:		; Actual conversion.
	ldy #$15
1$:	lda ($20),y	; reads in the ruler escape
	sta r1L
	iny
	lda ($20),y
	sta r1H
	lda r1H
	cmp #$01
	bne 2$
	lda r1L
	cmp #$df
2$:	bne 3$
	lda #$01
	sta r1H
	lda #$e0
	sta r1L
3$:	clc
	lda #$50	; add $50 for one inch
	adc r1L
	sta r1L
	bcc 4$
	inc r1H
4$:	lda r1H
	sta ($20),y	; store back the modified values into the ruler escape
	dey
	lda r1L
	sta ($20),y
	dey
	dey
	bpl 1$
	rts

printDataName:	; prints the actual filename onto the upper top right of the screen. (lb3a79)
	jsr i_MoveData
.word nameBuffer		; SOURCE - filename
.word CNameBuffer		; DEST - current filename
.byte $11,$00		; move 17 bytes
	jsr $1fba		; useSystemFont
	lda #$00
	sta currentMode	; plaintext
	jsr DrawStripes
	lda #]CNameBuffer
	sta r0H
	lda #[CNameBuffer
	sta r0L		; r0 points to filename
	jsr lb3af5		; calculate width of filename
	lda r1H
	cmp #$00
	bne 1$
	lda r1L
	cmp #$90		; max. width
1$:	bcc 2$
	lda #$00
	sta r1H
	lda #$90
	sta r1L		; make it the maximum width
2$:	lda #$90
	sec		; now does something to those values
	sbc r1L
	sta $18
	lda #$00
	sbc r1H
	sta $19
	lsr $18
	lda #$00
	sta $19
	clc
	lda #$e2
	adc $18
	sta $18
	lda #$01
	adc $19
	sta $19
	lda #$0a
	sta r1H
	lda #$02
	sta $38
	lda #$79		; set rightMargin
	sta $37
	lda #$20
	jsr PutChar		; output a space at position already calculated at r1
	lda #]CNameBuffer
	sta r0H
	lda #[CNameBuffer
	sta r0L		; print the filename onto the screen
	jsr PutString
	lda #$20		; prints a trailing space
	jsr PutChar
	lda #$02
	sta $38
	lda #$7f		; set rightMargin
	sta $37
	rts

lb3af5:			; calculate width of filename
	lda r2L
	pha
	lda #$00
	sta r1L
	sta r1H
	sta r2L
	sta $1c
1$:	ldy $1c
	lda (r0L),y		; get filename
	beq 2$
	inc r2L
	jsr GetCharWidth	; get the width of individual characters in filename
	clc
	adc r1L
	sta r1L
	bcc 3$
	inc r1H		; r1 has accumulated width of entire filename
3$:	inc $1c
	bne 1$
2$:	ldx r2L
	pla
	sta r2L
	rts

DrawStripes:		; I think this routine draws the stripes in the filename tab
			; (lb3b20)
	jsr i_GraphicsString
.byte $01,$df,$01,$03	; pen to x$01df,y$03
.byte $05,$09	; newPattern (horizontal stripes)
.byte $03,$79,$02,$0d	; rectangle to x$0279,y$0d
.byte $05,$00	; newPattern (blank)
.byte $00		; delimiter
	rts

lb3b31:		; DB indicating that a DA can't be run.
	pla
	lda #<lb406f
	ldy #>lb406f
	jmp $2314	; issues DB

runDA:			; Runs DA's (lb3b39)
	pha
	jsr GotoFirstMenu
	lda $d3		; What's there?
	cmp #$3d		; header record
	bcs lb3b31
	jsr $07c5		; saves ZP space and inverts a rectangle
	jsr $0d61		; closes the current datafile
	jsr saveTScrap
	jsr i_MoveData	; save sprite #6 bitmap data
.byte	$80, $8a		; .word $8a80 - SOURCE
.byte	$d8, $75		; .word $75d8 - DEST
.byte	$00, $01		; .word $0100 - COUNT
	ldx #$3c		; saves only the first 24 scanlines of the display screen.
			; to $a000
	jsr $21ba		; Master save routine
	pla
	sta r6L		; No idea yet what this routine is doing.
	asl
	asl
	asl
	asl
	clc
	adc r6L
	adc #$d9
	sta r6L
	lda #$00
	adc #$30
	sta r6H		; r6 points to filename of DA
	lda $01
	sta r15L
	lda #$35
	sta $01
	lda $d017		; YXPAND Sprite Register
	pha
	lda #$00		; don't expand sprites
	sta $d017		; YXPAND Sprite Register
	lda r15L
	sta $01
	lda $4c96		; What's at these locations?
	pha
	lda $4c95
	pha
	lda #$00
	sta r0L		; load at address specified in fileheader and run automatically
	sta r10L		; application will take care of restoring the screen display
	jsr setProgDevice	; calls DA on its application disk
	jsr toggleZP
	jsr GetFile		; loads and runs a DA
	jsr toggleZP
	pla
	sta $4c95
	pla
	sta $4c96		; What's at these locations?
	lda $01
	sta r15L
	lda #$35
	sta $01
	pla		; restore sprite size
	sta $d017		; YXPAND Sprite Register
	lda r15L
	sta $01
	txa		; catch disk error
	pha
	ldx #$3c		; restores the top 24 scanlines from $a000
	jsr $21bf		; Master recover routine
	jsr i_MoveData	; restore sprite data
.byte	$d8, $75		; .word $75d8 - SOURCE
.byte	$80, $8a		; .word $8a80 - DEST
.byte	$00, $01		; $0100 COUNT
	pla
	tax
	beq 1$
	cpx #$03		; INSUFF_SPACE error
	bne 2$
	lda #<lb3ecd		; Not enough free disk space
	ldy #>lb3ecd
	jsr $2314		; issues DB
	clv
	bvc 1$
2$:	lda #<lb4058		; Running desk accessory error
	ldy #>lb4058
	jsr $233a		; issues DB
1$:	jsr lb3c12		; not sure what this routine does
	jsr $22e3		; Sets up sprite #1 - the text cursor
	jsr loadTScrap	; Loads in a text scrap
	jsr $0d6c	; reloads the datafile and points to its VLIR record that was last worked upon.
	lda $851e		; default screen color
	sta r2L
	lda #$8c
	sta r1H
	lda #$00		; VIDEO_MATRIX
	sta r1L		; r1 points to $8c00
	lda #$03
	sta r0H
	lda #$e8
	sta r0L		; r0 points to $03e8
	jsr FillRam		; fills color data w/ default color value and I guess is designed
			; to restore color data after a DB exits, except that this
			; version can't do color yet.
	jsr $2555		; Don't know what this routine does, but 3 items are on the
			; stack after the JSR.
	jsr $1512		; I think this reads in the data and displays the characters
	jsr $2575		; restores those 3 items and other stuff from the stack
	jsr SetScrollSprite	; Sets up sprite #6
	jsr $1bbb		; turns on the text prompt and draws a couple of rectangles
	rts

lb3c12:		; No idea yet what this routine does
	lda $319c
	cmp #$02
	bcc 1$
	lda #$02
	sta $319c
1$:	rts

saveTScrap:
	bit	TSFlag	; Flag to indicate Text Scrap
	bmi	1$
	lda	TSiZe
	ora	TSiZe+1	; size of text scrap.
	beq	1$
	lda	#]TSiZe
	sta	lb3dff+1	; modifies something in this file
	lda	#[TSiZe	; Load address is at $41e4
	sta	lb3dff	; modifies something in this file
	jsr	lb3d57	; saves a Text Scrap.
1$:	rts

quitGeoWrite:		; quits geoWrite 128 v2.1 (formerly lb3c39)
	jsr	saveTScrap	; something to do with a text scrap
	jsr	setProgDevice
	jsr	toggleZP
	jmp	EnterDeskTop

SetScrollSprite:		; sets up sprite 6
	lda $01
	pha
	lda #$35	; i/o in
	sta $01
	lda #$00	; black
	sta $d02d	; sprite 6 color register
	pla
	sta $01
	jsr i_FillRam
.byte $40,$00	; fill 64 bytes
.byte $80,$8b	; fill sprite 6 data with $00
.byte $00		; clear pattern
	jsr i_MoveData
.word lb3c73	; SOURCE
.byte $80,$8b	; DEST -fill sprite 6
.byte $0b,$00	; move 11 bytes
	lda #$04
	sta $8bbf	; fill the last byte of sprite 6 - bug?
	lda $d01d
	ora #$40
	sta $d01d	; expand sprite 6 horizontally
	rts

lb3c73:		; sprite 6 data
.byte $7f,$fe,$00,$40,$02,$00,$40,$02,$00,$7f,$fe

AdjPageWidths:		; reads in a VLIR record #63 and adjusts geoPublish data
			; (lb3c7e)
	lda #$00
	sta gPFlag
	lda #$3f		; Reserved Record
	jsr PointRecord
	tya		; checks the data to see if it's there
	beq 5$
	txa		; check error
	bne 5$
	lda #$2b
	sta r7H
	lda #$66
	sta r7L		; r7 points to $2b66 as loading address
	lda #$00
	sta r2H
	lda #$f4
	sta r2L		; read a maximum of $f4 bytes
	jsr toggleZP
	jsr ReadRecord
	jsr toggleZP
	cpx #$0b	; BUFFER-OVERFLOW error
	beq 1$
	txa	; check error
	bne 5$
1$:	lda #$ff
	sta gPFlag
	ldy #$3c
2$:	lda $2c1d,y		; Compares the page width high byte
	cmp pageWidth+1	; against geoWrite's own page width high byte.
	bne 3$
	lda $2be0,y		; Compares the page width low byte
	cmp pageWidth	; against geoWrite's own page width low byte
3$:	bcc 4$		; branch if page width narrower than value in $2df8
	lda pageWidth	; otherwise, pageWidth controls.
	sta $2be0,y
	lda pageWidth+1
	sta $2c1d,y
4$:	dey
	bpl 2$
5$:	rts

printInfoBox:	; called by geoWrite info submenu selection.
	jsr GotoFirstMenu
	lda #<lb3cdf
	ldx #>lb3cdf
	jmp $2538	; issues a DB with the following data

lb3cdf:
.byte $81		; default DB
.byte $0e		; DBSYSOPV - causes a return to application
.byte $0b		; DBTXTSTR
.byte $10		; x offset
.byte $10		; y offset
.word infoBoxText	; pointer to info box text
.byte $00		; terminator

loadTScrap:		; Loads in a Text Scrap
	jsr setProgDevice
	lda #>lb40c1
	sta r6H
	lda #<lb40c1
	sta r6L		; r6 points to Text Scrap as filename
	jsr $27b0		; FindFile
	txa		; check error
	bne 3$
	lda r5H
	pha
	lda r5L		; save pointer to direntry in diskBlkBuf
	pha
	jsr CheckDFVer	; check the datafile's version identifier string
	bcc 1$
	beq 1$
	tya
	bne 3$
	pla
	pla
	lda #<lb40cd		; text scrap beyond V2.1
	ldy #>lb40cd
	jsr $2314		; issues DB
	clv
	bvc 3$
1$:	pla
	sta r5L
	pla
	sta r5H
	tya
	bne 3$
	ldy #$01
	lda (r5L),y		; get t/s of first datablock in text scrap
	sta r1L
	iny
	lda (r5L),y
	sta r1H
	jsr $2625		; r4 points to $8000
	jsr $27b3		; GetBlock
	txa
	beq 2$
	lda #<lb40e7		; reading Text Scrap
	ldy #>lb40e7
	jsr $233a		; issues a DB
	clv
	bvc 3$
2$:	lda $8003		; get length of text scrap
	sta TSiZe+1
	lda $8002
	sta TSiZe
	lda #$ff
	sta TSFlag
	rts

3$:	lda #$00
	sta TSiZe		; Zero bytes indicate no text scrap
	sta TSiZe+1
	sta TSFlag
	rts

lb3d57:			; saves a Text Scrap
	lda lb3dff
	clc
	adc TSiZe
	sta lb3e01
	lda lb3dff+1
	adc TSiZe+1		; saves the ending address of a text scrap
	sta lb3e01+1
	lda lb3e01
	clc
	adc #$02
	sta lb3e01
	bcc 1$
	inc lb3e01+1
1$:	jsr setProgDevice
	lda #]lb40c1
	sta r0H
	lda #[lb40c1
	sta r0L	; r0 points to $40c1 - pointer to filename (TextScrap)
	jsr toggleZP
	jsr DeleteFile		; deletes it
	lda #]lb40c1
	sta lb3db8+1
	lda #[lb40c1
	sta lb3db8	; for the savefile routine to point at filename to save as
	lda #>lb3db8
	sta r9H
	lda #<lb3db8
	sta r9L	; r9 points to file header for Text Scrap
	lda #$00	; use any available direntry
	sta r10L
	jsr SaveFile
	jsr toggleZP
	txa	; check error
	beq 2$
	lda #$00
	sta TSiZe		; Text scrap is saved and no longer exists
	sta TSiZe+1
	sta TSFlag
	rts

2$:	lda #$ff	; bit 7 set to indicate that text scrap is still there or something's wrong.
	sta TSFlag
	rts

lb3db8:		; file header for Text Scrap
.byte $00,$00	; Pointer to its filename
.byte $03,$15	; icon size
.byte $bf,$ff,$ff,$ff,$80,$40,$01,$80,$60,$01,$80,$60,$01,$80,$60,$0d,$80,$50
.byte $35,$80,$50,$d9
.byte $80,$53,$21,$80,$5c,$c1,$80,$59,$01,$87,$ce,$01,$9f,$c8,$01,$b8,$f8,$01
.byte $b1,$fc,$01,$bf,$6e,$01,$9c,$66,$01,$80,$66,$01,$80,$6c,$01,$80,$38
.byte $01,$80,$00,$01,$ff,$ff,$ff
.byte $83		; USR filetype
.byte $04		; SYSTEM geos filetype
.byte $00		; SEQUENTIAL program

lb3dff:		; modified by earlier routines
.byte $00,$00	; loading address
lb3e01:		; modified by earlier routines
.byte $00,$00	; ending address
.byte $00,$00	; no initialization address is specified
.byte 84,101,120,116,32,32,83,99,114,97,112,32,86,50,46,48; application filename
; "Text  Scrap V2.0"
.block 44

CheckDFVer:		; checks the datafile's version
	lda r5H
	sta r9H
	lda r5L
	sta r9L	; directory entry of the data file
	jsr toggleZP
	jsr GetFHdrInfo
	jsr toggleZP
	txa	; check error
	tay
	bne 1$
	lda $815a
	ldx $815c	; get version string
	cmp #$32	; is it 2.1?
	bne 1$
	cpx #$31
1$:	rts

infoBoxText:
.byte 24,103,101,111,87,114,105,116,101,32,98,121,32,84,111,110,121,32,82,101
.byte 113,117,105,115,116,22,160,0,64,86,101,114,115
; BOLDON, "geoWrite by Tony Requist", GOTOXY 160x,64y, "Vers
.byte 105,111,110,32,50,46,50,32,102,111,114,32,116,104,101,32,67,49,50,56,22
.byte 160,0,80
; "ion 2.2 for the C128", GOTOXY 160x,80y
; note the change from 2.1 to 2.2
.byte 67,111,112,121
.byte 114,105,103,104,116,32,49,57,56,53,44,32,49,57,56,54,44,32,49,57,56,55
; "Copyright 1985, 1986, 1987"
.byte 22,160,0,96,66,101,114,107,101,108,101,121,32,83,111,102,116,119,111,114
.byte 107,115,0
; GOTOXY 160x,96y, "Berkeley Softworks" and terminates

lb3ecd:
.byte 24,78,111,116,32,101,110,111,117,103,104,32,102,114,101,101,32,115,112,97
.byte 99,101,32,111,110,22,160,0,64,100,105,115,107,32,102,111,114,32,100,101
; BOLDON, "Not enough free space on", GOTOXY 160x,64y, "disk for de"
.byte 115,107,32,97,99,99,101,115,115,111,114,121,46,0
; "sk accessory." and terminates

lb3f03:	; dialog box text
.byte 24,80,108,101,97,115,101,32,83,101,108,101,99,116,32,79,112,116,105,111
.byte 110,58,27,0
; BOLDON, "Please Select Option:" and PLAINTEXT and finishes

lb3f1b:	; dialog box text
.byte 110,101,119,32,100,111,99,117,109,101,110,116,0
; "new document" and terminates

lb3f28:	; dialog box text
.byte 101,120,105,115,116,105,110,103,32,100,111,99,117,109,101,110,116,0
; "existing document" and terminates

lb3f3a:	; dialog box text
.byte 116,111,32,100,101,115,107,84,111,112,0
; "to deskTop" and terminates

lb3f45:	; text for a dialog box
.byte 24,73,110,115,101,114,116,32,110,101,119,32,100,105,115,107,32,105,110
.byte 116,111,32,100,105,115,107,32,100,114,105,118,101,46,0
; BOLDON, "Insert new disk into disk drive." and terminates

lb3f67:	; text for a dialog box
.byte 24,70,105,108,101,32,101,120,105,115,116,115,44,32,99,104,111,111,115
.byte 101,32,97,110,111,116,104,101,114,46,27,0
; BOLDON, "File exists, choose another.", PLAINTEXT and terminates

lb3f86:	; dialog box text
.byte 24,68,97,116,97,32,102,105,108,101,32,105,115,32,98,101,121,111,110,100
.byte 32,86,50,46,49,0
; BOLDON, "Data file is beyond V2.1" and terminates

lb3fa0:	; DB text
.byte 24,67,111,110,118,101,114,116,105,110,103,32,101,97,114,108,105,101,114
.byte 32,102,105,108,101,32,116,111,32,86,50,46
; BOLDON, "Converting earlier file to V2."
lb3fbf:	; modified by earlier routines
.byte 49	; currently 1
.byte 0	; and terminates

lb3fc1:	; DB text
.byte 99,111,110,118,101,114,116,105,110,103,32,102,105,108,101,46,0
; "converting file." and terminates

lb3fd2:	; dialog box text
.byte 24,79,110,32,100,105,115,107,58,27,0
; BOLDON, "On disk:", PLAINTEXT and terminates

lb3fdd:	; dialog box text
.byte 24,87,65,82,78,73,78,71,33,0
; BOLDON, "WARNING!" and terminates

lb3fe7:	; dialog box text
.byte 84,104,105,115,32,102,105,108,101,32,105,115,32,119,114,105,116,101,32
.byte 112,114,111,116,101,99,116,101,100,46,0
; "This file is write protected.", and terminates

lb4005:
.byte 24,67,97,110,110,111,116,32,114,101,99,111,118,101,114,32,102,114,111,109
.byte 32,102,105,108,101,46,0
; BOLDON, "Cannot recover from file." and terminates

lb4020:	; dialog box text
.byte 24,80,108,101,97,115,101,32,101,110,116,101,114,32,110,101,119,32,102,105
.byte 108,101,110,97,109,101,58,27,0
; BOLDON, "Please enter new filename:", PLAINTEXT and terminates

lb403d:	; dialog box text
.byte 111,112,101,110,105,110,103,32,102,105,108,101,0
; "opening file" and terminates

lb404a:	; dialog box text
.byte 99,114,101,97,116,105,110,103,32,102,105,108,101,0
; "creating file" and terminates

lb4058:
.byte 114,117,110,110,105,110,103,32,100,101,115,107,32,97,99,99,101,115,115,111
.byte 114,121,0
; "running desk accessory"

lb406f:
.byte 24,67,97,110,110,111,116,32,114,117,110,32,100,101,115,107,32,97,99,99,101
.byte 115,115,111,114,121,22,160,0,64,119,104,105,108,101,32,105,110,32,104,101
; BOLDON, "Cannot run desk accessory", GOTOXY 160x,64y, "while in he"
.byte 97,100,101,114,32,111,114,32,102,111,111,116,101,114,0
; "ader or footer" and terminates

lb40a7:
.byte 24,70,105,108,101,32,110,97,109,101,32,97,108,114,101,97,100,121,32,101
.byte 120,105,115,116,115,0
; BOLDON, "File name already exists", and terminates

lb40c1:
.byte 84,101,120,116,32,32,83,99,114,97,112,0
; "Text  Scrap" and terminates

lb40cd:
.byte 24,116,101,120,116,83,99,114,97,112,32,105,115,32,98,101,121,111,110,100
.byte 32,86,50,46,49,0
; BOLDON, "textScrap is beyond V2.1" and terminates

lb40e7:
.byte 114,101,97,100,105,110,103,32,116,101,120,116,83,99,114,97,112,0
; "reading textScrap" and terminates

diskName:
;.block	$11	; 17 bytes for the diskname w/ NULL terminator
VerFlag	==	$4171+$11			; flag to indicate version of datafile
		; the address is relocatable and must be noted when changes are made.
;.block	$01
		; end of VLIR record #5
		; technically, $401a comes after diskName

.end
