
;======================================;
;== Source To Program Name Interface ==;
;======================================;

#define bcall(label) RST 28h \ .dw label
#define bjump(label) call 50h \ .dw label

#include "ti83plus.inc"
#include "mirage.inc"
#include "mirgui.inc"

tempcoords      = GUIsaferam1+0    ;(2)    Holds the Coordinates for Options Cursor.
tempgoodprog    = GUIsaferam1+2    ;(2)    Temporary Pointer for the Current Good Program.
firstgoodprog   = GUIsaferam1+4    ;(2)    Pointer to the First Valid Program of the List.
lastgoodprog    = GUIsaferam1+6    ;(2)    Pointer to the Last Valid Program of the List.
GUI_Flags       = GUIsaferam1+8    ;(2)    Holds the states of any bits used as flags.

	.org $9d93
	.db $BB,$6D
	.db $C9,$02
	.db 3,27,"Program  Names",0

ProgName_Interface_Start:
	ld de, 3*256+11
	ld (cursorcoords), de
	ld a, 1
	ld (curprognum), a
	ld (topprognum), a
	ld hl, $0001
	ld (cur_element), hl
PN_Int_Restart_Interface:
	xor a
	ld (GUI_Flags), a
Desc_Restart_Int_After_Del:
	bcall(_grbufclr)
	call Find_Num_Good_Progs
	or a
	jr z, No_Good_Progs_Cont
	call Get_Curgoodprog_Ptr
	ld a, (topprognum)
	ld b, a
	call Skip_Forward_B_From_Top
	ld (topgoodprog), hl
	ld a, (numgoodprogs)
	SRL a
	jr nc, Get_Max_Elem_Even_Progs
Get_Max_Elem_Odd_Progs:
	inc a
Get_Max_Elem_Even_Progs:
	cp 7
	jr c, Get_Max_Elem_Cont1
	sub 5
	jr Get_Max_Elem_Cont2
Get_Max_Elem_Cont1:
	ld a, 1
Get_Max_Elem_Cont2:
	ld h, 0
	ld l, a
	ld (max_elements), hl
Find_Pointers:
	ld hl, $0000
	call getnextgoodprog
	ld (firstgoodprog), hl
Find_1st_Lst_Cur_GPs_Loop:
	call getnextgoodprog
	jr z, Find_1st_Lst_Cur_GPs_Loop
	ld (lastgoodprog), hl
No_Good_Progs_Cont:
	call Put_MirageOS_Header
	ld hl, Plotsscreen+(55*12)
	call Invert_1_Line
	ld hl, Plotsscreen+(63*12)
	call Invert_1_Line
	ld hl, 88*256+9
	ld b, 32
	call Put_ScrollBar
	call Initial_Scrollbar
	ld hl, 0*256+9
	ld de, 0*256+62
	call fastlineb
	ld hl, 95*256+56
	ld de, 95*256+62
	call fastlineb
	call Put_Folder_Name_Top
	ld a, (numgoodprogs)
	or a
	jp z, PN_Int_No_Good_Programs
	ld de, 11*256+8
	ld (tempcoords), de
	call PN_Int_Put_Names
	call PN_Int_Put_Cur_Prog_Desc
PN_Int_Cursor_After_Scroll:
	call Update_ScrollBar
	jr PN_Int_Cursor_Sequence
PN_Int_Cursor_Sequence_Desc:
	call PN_Int_Put_Cur_Prog_Desc
PN_Int_Cursor_Sequence:
	call PN_Int_XOR_Cursor
	ld hl, GUI_Flags
	RES 7, (hl)
	call fastcopy
PN_Int_Cursor_Getkey_Loop:
	bcall(_getcsc)
	cp $04
	jr z, PN_Int_Cursor_Up			; UP = Cursor Up / Scroll Up
	cp $01
	jp z, PN_Int_Cursor_Down		; DOWN = Cursor Down / Scroll Down
	cp $03
	jp z, PN_Int_Cursor_Right		; RIGHT = Cursor Right
	cp $02
	jp z, PN_Int_Cursor_Left		; LEFT = Cursor Left
	cp $32
	jp z, PN_Int_Page_Up			; TRACE = Page Up
	cp $31
	jp z, PN_Int_Page_Down			; GRAPH = Page Down
	cp $09
	jp z, PN_Int_Run_Program		; ENTER = Execute Current Program
	cp $36
	jp z, PN_Int_Run_Program		; 2ND = Execute Current Program
	cp $28
	jr z, PN_Int_Call_Folder_Menu	; X VAR = Goto Folders Menu
	cp $30
	jr z, PN_To_Options_screen		; ALPHA = Goto The Options Screen
	cp $2A
	jr z, PN_Int_Move_GUI_Prog		; STO = Move Current Prog to New Folder

	call General_Key_Routine		; MODE = Exit, (+) = Inc, (-) = Dec Contrast
	jr PN_Int_Cursor_Getkey_Loop

PN_To_Options_screen:
	call Options_screen
	jp PN_Int_Restart_Interface
PN_Int_Call_Folder_Menu:
	call Folder_Menu_Start
	jp ProgName_Interface_Start
PN_Int_Move_GUI_Prog:
	call Move_GUI_Prog
	jp ProgName_Interface_Start

Check_Paging_Flag:
	ld hl, GUI_Flags
	BIT 7, (hl)
	ret

PN_Int_Cursor_Up:
	ld de, (curgoodprog)
	ld hl, (firstgoodprog)
	call cphlde
	jr z, Goto_Getkey_Check_Page
	call getnextgoodprog
	ld de, (curgoodprog)
	call cphlde
	jr z, Goto_Getkey_Check_Page
;Check_Scroll_Bar_Up_Twice:
;	ld a, (numgoodprogs)
;	BIT 0, a
;	jr z, Dont_Scroll_Bar_Up_Twice
;	ld hl, (lastgoodprog)
;	call getprevgoodprog
;	ld de, (curgoodprog)
;	call cphlde
;	jr nz, Dont_Scroll_Bar_Up_Twice
;	call Decrease_Cur_Element
;Dont_Scroll_Bar_Up_Twice:
	ld hl, curprognum
	dec (hl)
	dec (hl)
	ld a, (cursorcoords)
	cp 11
	jp z, PN_Int_Scroll_Up
	call Skip_Back_2_Good_Progs
	call Check_Paging_Flag
	jr nz, PN_Int_Cont2_Cur_Up
	call PN_Int_Put_Cur_Prog_Desc
	call PN_Int_XOR_Cursor
PN_Int_Cont2_Cur_Up:
	ld a, (cursorcoords)
	sub 7
	ld (cursorcoords), a
	call Check_Paging_Flag
	ret nz
	jp PN_Int_Cursor_Sequence

Goto_Getkey_Check_Page:
	call Check_Paging_Flag
	jp z, PN_Int_Cursor_Getkey_Loop
	xor a
	ret

PN_Int_Cursor_Down:
	ld de, (curgoodprog)
	ld hl, (lastgoodprog)
	call cphlde
	jr z, Goto_Getkey_Check_Page
	call getprevgoodprog
	ld de, (curgoodprog)
	call cphlde
	jr nz, PN_Int_Cont1_Cur_Down
; 2nd to last prog
	ld de, (cursorcoords)
	ld a, d
	cp 46
	jr nz, Goto_Getkey_Check_Page
	ld a, e
	cp 39+1
	jr c, Goto_Getkey_Check_Page
	call Check_Paging_Flag
	call z, PN_Int_XOR_Cursor
	ld de, 46*256+39
	ld (cursorcoords), de
	ld hl, (curgoodprog)
	ld (tempgoodprog), hl
	call PN_Int_Put_Cur_Prog_Desc
	ld hl, (ptemp)
	ld (curgoodprog), hl
	jp PN_Int_Cont_Odd_Prog_Rt_Side
PN_Int_Cont1_Cur_Down:
	ld hl, curprognum
	inc (hl)
	inc (hl)
	ld a, (cursorcoords)
	cp 46
	jp z, PN_Int_Scroll_Down
	call Skip_Forward_2_Good_Progs
	call Check_Paging_Flag
	jr nz, PN_Int_Cont2_Cur_Down
	call PN_Int_Put_Cur_Prog_Desc
	call PN_Int_XOR_Cursor
PN_Int_Cont2_Cur_Down:
	ld a, (cursorcoords)
	add a, 7
	ld (cursorcoords), a
	call Check_Paging_Flag
	ret nz
	jp PN_Int_Cursor_Sequence

PN_Int_Cursor_Right:
	ld de, (curgoodprog)
	ld hl, (lastgoodprog)
	call cphlde
	jp z, PN_Int_Cursor_Getkey_Loop
	ld a, (cursorcoords+1)
	cp 46
	jp z, PN_Int_Cursor_Getkey_Loop
	ld hl, curprognum
	inc (hl)
	ld hl, (curgoodprog)
	call getnextgoodprog
	ld (curgoodprog), hl
	call PN_Int_Put_Cur_Prog_Desc
	call PN_Int_XOR_Cursor
	ld a, 46
	ld (cursorcoords+1), a
	jp PN_Int_Cursor_Sequence

PN_Int_Cursor_Left:
	ld a, (cursorcoords+1)
	cp 3
	jp z, PN_Int_Cursor_Getkey_Loop
	ld hl, curprognum
	dec (hl)
	ld hl, (curgoodprog)
	call getprevgoodprog
	ld (curgoodprog), hl
	call PN_Int_Put_Cur_Prog_Desc
	call PN_Int_XOR_Cursor
	ld a, 3
	ld (cursorcoords+1), a
	jp PN_Int_Cursor_Sequence

PN_Int_Scroll_Up:
	call Decrease_Cur_Element
	call Skip_Back_2_Top_Progs
	call Skip_Back_2_Good_Progs
	ld hl, topprognum
	dec (hl)
	dec (hl)
	call Check_Paging_Flag
	jr nz, PN_Int_Scroll_Up_Cont1
	call PN_Int_XOR_Cursor
 	call PN_Int_Put_Cur_Prog_Desc
PN_Int_Scroll_Up_Cont1:
	ld hl, Plotsscreen+538
	ld de, Plotsscreen+622
	ld b, 33
PN_Int_Scroll_Up_Loop1:
	ld c, 10
PN_Int_Scroll_Up_Loop2:
	ld a, (hl)
	ld (de), a
	dec de
	dec hl
	dec c
	jr nz, PN_Int_Scroll_Up_Loop2
	dec hl
	dec hl
	dec de
	dec de
	DJNZ PN_Int_Scroll_Up_Loop1
	ld hl, Plotsscreen+145
	ld b, 5
	xor a
PN_Int_Clear_Txt_Scroll_Up:
	ld c, 10
PN_Int_Clear_Txt_Up_Loop1:
	ld (hl), a
	inc hl
	dec c
	jr nz, PN_Int_Clear_Txt_Up_Loop1
	inc hl
	inc hl
	DJNZ PN_Int_Clear_Txt_Scroll_Up
	ld hl, (curgoodprog)
	ld a, (cursorcoords+1)
	cp 46
	call z, getprevgoodprog
	push hl
	call getinfo
	ld hl, OP2
	ld de, 11*256+8
	call setvputs
	pop hl
	call getnextgoodprog
	call getinfo
	ld hl, OP2
	ld de, 11*256+51
	call setvputs
	call Check_Paging_Flag
	ret nz
	jp PN_Int_Cursor_After_Scroll
	
PN_Int_Scroll_Down:
	call Skip_Forward_2_Good_Progs
	call Check_Paging_Flag
	jr nz, PN_Int_Scroll_Down_Cont1
	call PN_Int_XOR_Cursor
	call PN_Int_Put_Cur_Prog_Desc
PN_Int_Scroll_Down_Cont1:
	ld hl, (curgoodprog)
	ld (tempgoodprog), hl
PN_Int_Cont_Odd_Prog_Rt_Side:
	call Increase_Cur_Element
	ld hl, topprognum
	inc (hl)
	inc (hl)
	call Skip_Forward_2_Top_Progs
	ld hl, Plotsscreen+229
	ld de, Plotsscreen+145
	ld b, 33
PN_Int_Scroll_Down_Loop1:
	ld c, 10
PN_Int_Scroll_Down_Loop2:
	ld a, (hl)
	ld (de), a
	inc de
	inc hl
	dec c
	jr nz, PN_Int_Scroll_Down_Loop2
	inc hl
	inc hl
	inc de
	inc de
	DJNZ PN_Int_Scroll_Down_Loop1
	ld hl, Plotsscreen+565
	ld b, 5
	xor a
PN_Int_Clear_Txt_Scroll_Down:
	ld c, 10
PN_Int_Clear_Txt_Down_Loop1:
	ld (hl), a
	inc hl
	dec c
	jr nz, PN_Int_Clear_Txt_Down_Loop1
	inc hl
	inc hl
	DJNZ PN_Int_Clear_Txt_Scroll_Down
	ld hl, (curgoodprog)
	ld a, (cursorcoords+1)
	cp 46
	call z, getprevgoodprog
	push hl
	call getinfo
	ld hl, OP2
	ld de, 46*256+8
	call setvputs
	pop hl
	ld de, (lastgoodprog)
	call cphlde
	jr nz, PN_Int_Cont_Scroll_Down
	ld a, (numgoodprogs)
	BIT 0, a
	jr nz, PN_Int_Back_To_Cur_Seq
PN_Int_Cont_Scroll_Down:
	call getnextgoodprog
	call getinfo
	ld hl, OP2
	ld de, 46*256+51
	call setvputs
PN_Int_Back_To_Cur_Seq:
	ld hl, (tempgoodprog)
	ld (curgoodprog), hl
	call Check_Paging_Flag
	ret nz
	jp PN_Int_Cursor_After_Scroll

Clear_Key_Ret_Cur_Seq:
	ld hl, $8440
	ld (hl), 0
	jp PN_Int_Cursor_Sequence_Desc

PN_Int_Page_Up:
	call PN_Int_XOR_Cursor
	ld hl, (firstgoodprog)
	ld de, (curgoodprog)
	call cphlde
	jr z, Clear_Key_Ret_Cur_Seq
	call getnextgoodprog
	ld de, (curgoodprog)
	call cphlde
	jr z, Clear_Key_Ret_Cur_Seq
	ld hl, GUI_Flags
	SET 7, (hl)
	ld a, (cursorcoords)
	cp 11
	jr z, PN_Int_Cont_Page_Up
PN_Int_Page_Up_Loop1:
	ld a, (cursorcoords)
	cp 11
	jr z, Clear_Key_Ret_Cur_Seq
	call PN_Int_Cursor_Up
	jr PN_Int_Page_Up_Loop1
PN_Int_Cont_Page_Up:
	ld b, 5
PN_Int_Page_Up_Loop2:
	push bc
	call PN_Int_Cursor_Up
	pop bc
	jr z, PN_Int_Finish_Paging
	DJNZ PN_Int_Page_Up_Loop2
	jr PN_Int_Finish_Paging

PN_Int_Page_Down:
	call PN_Int_XOR_Cursor
	ld hl, GUI_Flags
	SET 7, (hl)
	ld a, (cursorcoords)
	cp 46
	jr z, PN_Int_Cont_Page_Down
PN_Int_Page_Down_Loop1:
	ld hl, (lastgoodprog)
	ld de, (curgoodprog)
	call cphlde
	jr z, Clear_Key_Ret_Cur_Seq
	call getprevgoodprog
	ld de, (curgoodprog)
	call cphlde
	jr z, Clear_Key_Ret_Cur_Seq
	ld a, (cursorcoords)
	cp 46
	jr z, Clear_Key_Ret_Cur_Seq
	call PN_Int_Cursor_Down
	jr PN_Int_Page_Down_Loop1
PN_Int_Cont_Page_Down:
	ld b, 5
PN_Int_Page_Down_Loop2:
	push bc
	call PN_Int_Cursor_Down
	pop bc
	jr z, PN_Int_Finish_Paging
	DJNZ PN_Int_Page_Down_Loop2
PN_Int_Finish_Paging:
	ld hl, $8440
	ld (hl), 0
	call PN_Int_Put_Cur_Prog_Desc
	jp PN_Int_Cursor_After_Scroll

PN_Int_Put_Cur_Prog_Desc:
	ld hl, 20*256+56
	ld de, 94*256+61
	xor a
	call FilledRectangle
	ld hl, (curgoodprog)
	call getinfo
	ld hl, ramdesc
	ld de, 56*256+3
	jp setvputs

PN_Int_Put_Names:
	ld a, (numgoodprogs)
	ld b, a
	cp 13
	jr c, PN_Int_Put_Names_Cont1
	ld b, 12
PN_Int_Put_Names_Cont1:
	ld de, 11*256+8
	ld hl, (topgoodprog)
PN_Int_Put_Names_Loop:
	push bc
	ld (tempcoords), de
	push hl
	call getinfo
	ld de, (tempcoords)
	ld hl, OP2
	call setvputs
	pop hl
	ld de, (lastgoodprog)
	call cphlde
	jr z, PN_Int_Quit_Put_Names
	call getnextgoodprog
	pop bc
	ld de, (tempcoords)
	ld a, e
	cp 51
	jr z, PN_Int_Put_Names_Col1
	ld e, 51
	DJNZ PN_Int_Put_Names_Loop
	ret
PN_Int_Put_Names_Col1:
	ld e, 8
	ld a, 7
	add a, d
	ld d, a
	DJNZ PN_Int_Put_Names_Loop
	ret
PN_Int_Quit_Put_Names:
	pop bc
	ret

PN_Int_XOR_Cursor:			;(D,E) = Top-Left Corner of Cursor Block
	ld hl, (cursorcoords)
	ld a, 40
	add a, h
	ld d, a
	ld a, 6
	add a, l
	ld e, a
	ld a, 2
	jp FilledRectangle

PN_Int_Run_Program:
	ld hl, scroll_vars
	ld de, save_scr_vars
	push hl
	push de
	ld bc, 11
	LDIR
	ld hl, (curgoodprog)
	call runprog
	pop hl
	pop de
	ld bc, 11
	LDIR
	jp PN_Int_Restart_Interface

Skip_Back_2_Good_Progs:
	ld hl, (curgoodprog)
	call getprevgoodprog
	call getprevgoodprog
	ld (curgoodprog), hl
	ret
Skip_Forward_2_Good_Progs:
	ld hl, (curgoodprog)
	call getnextgoodprog
	call getnextgoodprog
	ld (curgoodprog), hl
	ret
Skip_Back_2_Top_Progs:
	ld hl, (topgoodprog)
	call getprevgoodprog
	call getprevgoodprog
	ld (topgoodprog), hl
	ret
Skip_Forward_2_Top_Progs:
	ld hl, (topgoodprog)
	call getnextgoodprog
	call getnextgoodprog
	ld (topgoodprog), hl
	ret

No_Progs_Text:
	.db "This Folder Is Empty",0

PN_Int_No_Good_Programs:
	ld hl, No_Progs_Text
	ld de, 27*256+11
	call setvputs
	call fastcopy
PN_Int_No_Progs_Getkey_Loop:
	bcall(_getcsc)
	cp $09
	jr z, No_Progs_Call_Folder_Menu		; ENTER = Execute Current Program
	cp $36
	jr z, No_Progs_Call_Folder_Menu		; 2ND = Execute Current Program
	cp $28
	jr z, No_Progs_Call_Folder_Menu		; X VAR = Goto Folders Menu
	cp $30
	jp z, PN_To_Options_screen			; ALPHA = Goto The Options Screen

	call General_Key_Routine			; MODE = Exit, (+) = Inc, (-) = Dec Contrast
	jr PN_Int_No_Progs_Getkey_Loop

No_Progs_Call_Folder_Menu:
	jp PN_Int_Call_Folder_Menu

.end
