        MAIN "",134

Win     CALL NOKEY
Winn    CALL Window
Arrow   LD A,#FF
        LD (ONumb),A
        LD (But),A
        INC A
        LD (KEY1),A
        LD (KEY2),A
        LD (LAST_KEY),A
        CALL AllSelect
arrow   LD HL,(HotAdr)
        CPHL0
        JR Z,arrps1
        CALL REDEF
        CALL HotKey
        JR C,arrex
arrps1  LD HL,(ImAdr)
        CPHL0
        CALL NZ,JPHL
        CALL BUTTON
        JP C,ExitWin
cexi    CALL Select
        CALL PUTARW
        XOR A
        LD (LAST_KEY),A
        EI 
        HALT 
        CALL DIRSCN
        CALL MOVES
        CALL GETARW
        LD A,(DIRECT)
        AND #10
        JR Z,arrow
        CALL BarOff
        CALL Button
        CALL Scroll
arrex   CALL Switch
        CALL Radio

; Обработчик - Попала ли стрелка на заголовок или нет
        BIT 2,(IX+6)
        JR Z,setnumb
        LD A,(DIRECT)
        AND #10
        JR Z,setnumb

        LD B,(IX+2)
        LD C,(IX+3)
        PUSH BC
        LD A,2
        BIT 0,(IX+6)
        JR NZ,jr1
        DEC A
jr1     LD (IX+3),A
        LD DE,(ARW_XC)
        CALL lookbox
        JR NC,MoveWin
        POP BC
        LD (IX+2),B
        LD (IX+3),C

setnumb BIT 3,(IX+6)
        JR Z,psar
        LD A,(But)
        INC A
        JR NZ,psar
        LD A,(Numb)
        INC A
        JR NZ,psar
        LD DE,(ARW_XC)
        CALL lookbox
        JP NC,arrow

psar    LD A,(But)
        CP #FF
        JR Z,setn
        LD B,0
AllBar  EQU $-1
        ADD A,B
        LD (Numb),A
setn    LD A,(Numb)
        RET 

ExitWin BIT 3,(IX+6)
        JP Z,cexi
        CALL BarOff
        LD A,#FF
        LD (Numb),A
        LD (But),A
        RET 

Ixx     DEFS 4

; Drag'n'Drop в худших традициях Windows'95

MoveWin POP BC

pup
        LD HL,(ARW_X)
        LD (ARWRR),HL
        LD H,(IX+1)
        LD L,(IX+0)
        DUP 3
        SLA H
        SLA L
        EDUP 
        LD A,(ARW_Y)
        SUB H
        LD H,A
        LD A,(ARW_X)
        SUB L
        LD L,A
        LD (ARWR),HL

        LD (IX+2),B
        LD (IX+3),C
        LD (Ix),IX

        PUSH IX
        POP HL
        LD DE,Ixx
        LD BC,4
        LDIR 

        LD L,(IX+0)
        LD H,(IX+1)
        LD (ARW_XC),HL
        CALL HLmul8
        LD (ARW_X),HL

        LD IX,Ixx
;       INC (IX+2)
;       INC (IX+2)
;       INC (IX+3)
;       INC (IX+3)

ContMov CALL RAMKA
Arr     CALL DIRSCN
        CALL MOVES
        HALT 
        LD A,(DIRECT)
        CP #10
        JR Z,Arr
        AND #10
        JR Z,ReMove

        CALL RAMKA
        LD HL,(ARW_XC)
        LD (IX+0),L
        LD (IX+1),H
        JR ContMov

ReMove  CALL RAMKA
        PUSH IX
        POP HL

        LD IX,0
Ix      EQU $-2

        LD A,(Ixx+0)
        ADD A,(IX+2)
        CP 31+2
        CALL NC,corx_
        LD A,(Ixx+1)
        ADD A,(IX+3)
        CP 23+2
        CALL NC,cory_

        LD A,(Ixx+0)
        CP (IX+0)
        JR NZ,okrm
        LD A,(Ixx+1)
        CP (IX+1)
        JR NZ,okrm

norm    LD HL,0
ARWRR   EQU $-2
        LD (ARW_X),HL
        JP Arrow

corx_   LD A,32
        SUB (IX+2)
        LD (Ixx+0),A
        RET 
cory_   LD A,24
        SUB (IX+3)
        LD (Ixx+1),A
        RET 

okrm    EXX 
        BIT 2,(IX+6)
        CALL NZ,RestBack
        EXX 

        LD DE,(Ix)
        LD BC,2
        LDIR 

        LD HL,0
ARWR    EQU $-2

        LD A,(IX+0)
        Amul8
        ADD A,L
        LD L,A
        LD A,(IX+1)
        Amul8
        ADD A,H
        LD H,A
        LD (ARW_X),HL
        DUP 3
        SRL L
        SRL H
        EDUP 
        LD (ARW_XC),HL
        CALL SetINXY
        SET 1,(IX+6)
        CALL Window
        JP Arrow

HLmul8  DUP 3
        SLA L
        SLA H
        EDUP 
        RET 

Adiv8   SRL A
        SRL A
        SRL A
        RET 

TextAdr  DW 0
BarAdr   DW 0
HotAdr   DW 0
ButAdr   DW 0
SwiAdr   DW 0
RadioAdr DW 0
ScrAdr   DW 0
CrAdr    DW 0
ImAdr    DW 0
Esp      EQU $-TextAdr/2

window2 LD DE,0
window4 LD (IX),E
        INC IX
        LD (IX),D
        INC IX
        DJNZ window4
        JR window3

; Инициализация окон

IniWin  PUSH IX
        PUSH IX
        POP HL
        LD B,Esp
        LD IX,TextAdr
        LD DE,8
        ADD HL,DE
window1 DEFHL1
        LD A,D
        CP #FF
        JR Z,window2
        LD (IX),E
        INC IX
        LD (IX),D
        INC IX
        DJNZ window1
window3 POP IX
        LD H,(IX+0)
        LD A,H
        LD (WinX),A
        LD L,(IX+1)
        LD A,L
        LD (WinY),A
        LD (WinXY),HL
        RET 

corx    LD A,32
        SUB (IX+2)
        LD (IX+0),A
        RET 
cory    LD A,24
        SUB (IX+3)
        LD (IX+1),A
        RET 

Window  LD (Ix),IX
        LD A,(IX+0)
        ADD A,(IX+2)
        CP 31+2
        CALL NC,corx
        LD A,(IX+1)
        ADD A,(IX+3)
        CP 23+2
        CALL NC,cory

        BIT 1,(IX+6)
        JR Z,pwi1
        BIT 2,(IX+6)
        CALL NZ,SaveBack

pwi1    CALL IniWin

        BIT 1,(IX+6)
        CALL NZ,Box

        CALL DrwBut
        CALL Print
        LD HL,(CrAdr)
        CPHL0
        CALL NZ,JPHL
        CALL DrwSwitch
        CALL DrwRadio
        CALL DrwScroll
        SET 1,(IX+6)
        RET 

; Обрабатываем радио-группу
Radio   PUSH IX
        LD HL,(RadioAdr)
        CPHL0
        JR Z,PopIX

        PUSH HL
        POP IX
radio   LD A,(IX+0)
;       CP #FF
        INC A
        JR Z,PopIX

        LD A,(Numb)
        LD B,(IX+2)
        CP B
        JR C,cradio
        LD A,B
        ADD A,(IX+3)
        LD B,A
        LD A,(Numb)
        CP B
        JR NC,cradio

        SUB (IX+2)
        LD (IX+4),A

        LD L,(IX+5)
        LD H,(IX+6)
        POP IX
        POP AF
        RES 1,(IX+6)
        CPHL0
        JR Z,nclr
        CALL JPHL
nclr    JP Win

cradio  LD BC,7
        ADD IX,BC
        JR radio

; Рисуем радио-группу
; x, y, номер опции, кол-во, текущий

DrwRadio
        PUSH IX
        LD HL,(RadioAdr)
        CPHL0
        JR Z,PopIX

        PUSH HL
        POP IX

drwradi LD A,(IX+0)
;       CP #FF
        INC A
        JR Z,PopIX

;       LD D,(IX+0)
;       LD E,(IX+1)
        CALL RealWin
        CALL RASCH

        XOR A
Lp17    PUSH AF
        CALL Rad
        POP AF
        INC A
        CP (IX+3)
        JR NZ,Lp17

        LD BC,7
        ADD IX,BC
        JR Z,drwradi

PopIX   POP IX
        RET 

ROn     LD A,RadioOn
        JR CHAR
Rad     CP (IX+4)
        JR Z,ROn
ROff    LD A,RadioOff
CHAR    LD D,'FONT
        LD E,A
        LD C,8
char    LD A,(DE)
        LD (HL),A
        INC H
        INC D
        DEC C
        JR NZ,char
        DEC H
        JP INC_Y

; Рисуем переключатели (SWITCH)
; x, y, номер опции, состояние

DrwSwitch
        PUSH IX
        LD HL,(SwiAdr)
        CPHL0
        JR Z,PopIX

        PUSH HL
        POP IX

drwswch LD A,(IX+0)
        INC A
;       CP #FF
        JR Z,PopIX

;       LD D,(IX+0)
;       LD E,(IX+1)
        CALL RealWin
        CALL RASCH

        LD A,(IX+3)
        CALL SO
        LD BC,6
        ADD IX,BC
        JR drwswch

SO      AND A
        JR Z,SOff
        LD A,SwchOn
        JR CHAR

SOff    LD A,SwchOff
        JR CHAR

; Обрабатываем переключатели (SWITCH)

Switch  PUSH IX
        LD HL,(SwiAdr)
        CPHL0
        JR Z,PopIX

        PUSH HL
        POP IX
switch  LD A,(IX+0)
;       CP #FF
        INC A
        JR Z,PopIX

        LD A,(Numb)
        CP (IX+2)
        JR NZ,cswitch

        LD A,(IX+3)
        CPL 
        LD (IX+3),A

        LD L,(IX+4)
        LD H,(IX+5)
        POP IX
        POP AF
        RES 1,(IX+6)
        CPHL0
        JR Z,nclsw
        CALL JPHL
nclsw   JP Win

cswitch LD BC,6
        ADD IX,BC
        JR switch

        INCLUDE "ScrBar6",138

; Кнопочки

DrwBut  PUSH IX
        LD HL,(ButAdr)
        CPHL0
        JR Z,PopIX1
        PUSH HL
        POP IX
        CALL drwbut
PopIX1  POP IX
        RET 

drwbut  LD A,(IX+0)
;       CP #FF
        INC A
        RET Z

        LD D,(IX+7)
        LD E,(IX+6)
        CALL ToScrOf

;       LD D,(IX+0)
;       LD E,(IX+1)
        CALL RealWin

        LD A,(IX+4)
        LD C,(IX+3)
        LD B,(IX+2)
        CALL SetAtr

        LD BC,8
        ADD IX,BC
        JR drwbut

; Обработчик кнопулечек

Button  PUSH IX
        CALL SetINXY
        XOR A
        LD (But),A
        CALL button
        POP IX
        RET 

NoBut   LD A,#FF
        LD (But),A
        RET 

button  LD HL,(ButAdr)
        CPHL0
        JR Z,NoBut

        LD (SelAdr),HL
blp     LD A,(HL)
;       CP #FF
        INC A
        JR Z,NoBut
        CALL LookBox
        JR C,contbut

        BIT 1,(IX+5)
        JR Z,NoBut

        BIT 0,(IX+5)
        RET Z
        LD D,(IX+7)
        LD E,(IX+6)
        PUSH DE
        CALL ToScrOn
        HALT 
        HALT 
        HALT 
        CALL NOKEY
        POP DE

; DE - адрес спрайта
; IX - параметры спрайта

ToScrOf LD HL,Ret
        LD (nop1),HL
        XOR A
        LD (nop2),A
ToScr   PUSH DE
        CALL RealWin
        CALL RASCH
        POP DE
        LD A,(IX+3)
        Amul8
        LD C,A
        CALL Ret
nop1    EQU $-2
Lp10    PUSH HL
        LD B,(IX+2)
        AND A
Lp9     LD A,(DE)
nop2    NOP 
        LD (HL),A
        INC DE
        INC L
        DJNZ Lp9
        POP HL
        CALL INC_Y
        DEC C
        JR NZ,Lp10
Ret     RET 

contbut LD HL,But
        INC (HL)
        LD HL,(SelAdr)
        LD BC,8
        ADD HL,BC
        LD (SelAdr),HL
        JR blp

ToScrOn LD HL,Broll
        LD (nop1),HL
        LD A,#1F
        LD (nop2),A
        JR ToScr

Broll   LD B,(IX+2)
        XOR A
        PUSH HL
Lp11    LD (HL),A
        INC L
        DJNZ Lp11
        POP HL
        INC H
        DEC C
        RET 

; Сколько всего подсветок-выборок
AllSelect
        XOR A
        LD (AllBar),A
        LD HL,(BarAdr)
        CPHL0
        RET Z
        LD BC,5
allse   LD A,(HL)
;       CP #FF
        INC A
        RET Z
        LD A,(AllBar)
        INC A
        LD (AllBar),A
        ADD HL,BC
        JR allse

; Обработчик подсветочек
Select  PUSH IX
        CALL SetINXY
        CALL select
rix     POP IX
        RET 

select  XOR A
        LD (Numb),A
        LD HL,(BarAdr)
        CPHL0
        JR Z,NoFF

sellp   LD (SelAdr),HL

        LD A,(HL)
;       CP #FF
        INC A
        JR Z,NoFF
        CALL LookBox
        JR C,contsel
        BIT 1,(IX+4)
        JP Z,contsel

        LD A,(ONumb)
        LD C,A
        LD A,(Numb)
        CP C
        RET Z

        LD HL,0
WasXY   EQU $-2
        LD (WasXY),DE
        CALL BarOff

        CALL RealWin
        CALL RASCH
        LD (AdrHL),HL

        LD A,(IX+4)
        LD (Af),A
        CALL Bar
        LD A,(Numb)
        LD (ONumb),A
        RET 

contsel LD HL,Numb
        INC (HL)
        LD HL,0
SelAdr  EQU $-2
        LD BC,5
        ADD HL,BC
        JR sellp

NoFF    CALL BarOff
        LD A,#FF
        LD (Numb),A
        LD (ONumb),A
        RET 

BarOff  LD A,0
ONumb   EQU $-1
;       CP #FF
        INC A
        RET Z
        LD HL,0
AdrHL   EQU $-2
        LD BC,0
AdrBC   EQU $-2
        JR bar

Bar     LD B,(IX+2)
        LD C,(IX+3)
        LD (AdrBC),BC

bar     LD A,0
Af      EQU $-1
        BIT 0,A
        RET Z
        LD A,C
        Amul8
        LD C,A
Lp7     PUSH HL
        PUSH BC
Lp6     LD A,(HL)
        CPL 
        LD (HL),A
        INC L
        DJNZ Lp6
        POP BC
        POP HL
        CALL INC_Y
        DEC C
        JR NZ,Lp7
        RET 

; DE -> адрес в окне
; DE <- адрес на экране

RealWin LD D,(IX+0)
        LD E,(IX+1)
realwin LD A,(WinXY+1)
        ADD A,D
        LD D,A
        LD A,(WinXY+0)
        ADD A,E
        LD E,A 
        RET 

; Проверяет, попал курсор на прямоугольную област или нэт...

SetINXY LD HL,(ARW_XC)
        LD A,L
        SUB 0
WinX    EQU $-1
        LD L,A
        LD A,H
        SUB 0
WinY    EQU $-1
        LD H,A
        LD (INXY),HL
        RET 

LookBox LD DE,0
INXY    EQU $-2
        PUSH HL
        POP IX
lookbox LD A,(IX+0)
        INC E
        CP E
        CCF 
        RET C
        ADD A,(IX+2)
        CP E
        RET C
        LD A,(IX+1)
        INC D
        CP D
        CCF 
        RET C
        ADD A,(IX+3)
        CP D
        RET 

Box     LD A,(IX+4)
        LD C,(IX+3)
        LD B,(IX+2)
        LD D,(IX+0)
        LD E,(IX+1)
        CALL SetAtr

        LD DE,0
WinXY   EQU $-2
        CALL RASCH

        BIT 0,(IX+6)
        JR Z,Pass1

        PUSH DE
        EXX 

        LD A,(IX+5)
        LD C,2
        LD B,(IX+2)
        DEC B
        DEC B
        LD D,(IX+0)
        LD E,(IX+1)
        INC B
        INC B
        CALL SetAtr

        EXX 
        PUSH HL
        LD B,(IX+2)
        LD A,#FF
        CALL Lp
        POP HL

        LD C,15
Lp14    CALL INC_Y
        PUSH HL
        LD (HL),%10000000
        INC L
        LD B,(IX+2)
        DEC B
        DEC B
        XOR A
        CALL Lp
        LD (HL),%00000001
        POP HL
        DEC C
        JR NZ,Lp14

        POP DE
        INC E
        INC E
        CALL RASCH
Pass1   PUSH HL
        CALL Lpz
        POP HL
        INC H

        LD (HL),%01111111
        INC L
        LD B,(IX+2)
        DEC B
        DEC B
        LD A,#FF
        CALL Lp
        LD (HL),%11111110

        LD A,(IX+3)
        DEC A
        DEC A
        LD C,A
        Amul8
        SUB 4
        BIT 0,(IX+6)
        JR NZ,Pass3
        ADD A,16
Pass3   LD C,A
Lp2     CALL INC_Y
        PUSH HL
        LD (HL),%00000010
        DEC L
        XOR A
        CALL Lp_
        LD (HL),%01000000
        POP HL
        DEC C
        JR NZ,Lp2
        INC H
        LD (HL),%11111110
        DEC L
        CPL 
        CALL Lp_
        LD (HL),%01111111
        INC H
Lpz     LD B,(IX+2)
        XOR A
Lp      LD (HL),A
        INC L
        DJNZ Lp
        RET 

Lp_     LD B,(IX+2)
        DEC B
        DEC B
lp_     LD (HL),A
        DEC L
        DJNZ lp_
        RET 

; HL - adress on screen
; C  - length of SPACE-text

PR_51   LD HL,(xyprn)
pr_51   LD A,C
        LD (lnprn),A
        AND A
        RET Z
        LD B,'FONT
        PUSH IX
        LD HX,8
Loop_1  PUSH HL
        LD LX,0
lnprn   EQU $-1
        LD DE,SPACE

Cont    LD A,(DE)
        INC DE
        LD C,A
        LD A,(BC)
        LD (HL),A
        DEC LX
        JR Z,Exi

        LD A,(DE)
        INC DE
        LD C,A
        LD A,(BC)
        LD C,A
        XOR A
        DUP 3
        SLA C
        RLA 
        EDUP 
        OR (HL)
        LD (HL),A
        INC L
        LD A,C
        LD (HL),A
        DEC LX
        JR Z,Exi

        LD A,(DE)
        INC DE
        LD C,A
        LD A,(BC)
        RRA 
        RRA 
        OR (HL)
        LD (HL),A
        DEC LX
        JR Z,Exi

        LD A,(DE)
        INC DE
        LD C,A
        LD A,(BC)
        LD C,A
        XOR A
        SLA C
        RLA 
        OR (HL)
        LD (HL),A
        INC L
        LD A,C
        LD (HL),A
        DEC LX
        JR Z,Exi

        LD A,(DE)
        INC DE
        LD C,A
        LD A,(BC)
        LD C,0
        RRA 
        RRA 
        RRA 
        RR C
        RRA 
        RR C
        OR (HL)
        LD (HL),A
        INC L
        LD A,C
        LD (HL),A
        DEC LX
        JR Z,Exi

        LD A,(DE)
        INC DE
        LD C,A
        LD A,(BC)
        RRA 
        OR (HL)
        LD (HL),A
        DEC LX
        JR Z,Exi

        LD A,(DE)
        INC DE
        LD C,A
        LD A,(BC)
        LD C,A
        XOR A
        SLA C
        RLA 
        SLA C
        RLA 
        OR (HL)
        LD (HL),A
        INC L
        LD A,C
        LD (HL),A
        DEC LX
        JR Z,Exi

        LD A,(DE)
        INC DE
        LD C,A
        LD A,(BC)
        RRA 
        RRA 
        RRA 
        OR (HL)
        LD (HL),A
        DEC LX
        JR Z,Exi
        INC L
        JP Cont

Exi     POP HL
        CALL INC_Y
        INC B
        DEC HX
        JP NZ,Loop_1
        POP IX
        RET 

Print   LD DE,(WinXY)
        INC D

        LD BC,HEAD0D
        BIT 0,(IX+6)
        JR NZ,Pass2
        LD BC,F0D
        INC E
Pass2   LD (OBROB+1),BC
        CALL RASCH
        LD (xyprn),HL

        LD HL,(TextAdr)
        CPHL0
        RET Z

Jp1     LD DE,SPACE
        LD C,0
Lp3     LD A,(HL)
        INC HL
        AND A
        JP Z,PR_51
        CP #16
        JR Z,SXYP
        CP #0D
OBROB   JP Z,F0D
        CP #0F
        JR Z,F0F
        LD (DE),A
        INC DE
        INC C
        JR Lp3

SXYP    PUSH DE
        LD A,(HL)
        LD D,A
        INC HL
        LD A,(HL)
        LD E,A
        INC HL
        PUSH HL
        CALL realwin
        CALL RASCH
        LD (xyprn),HL
        POP HL
        POP DE
        JR Lp3

F0F     CALL Dline
        JR Jp1

HEAD0D  PUSH HL
        LD HL,(xyprn)
        INC H
        INC H
        INC H
        INC H
        CALL pr_51
        LD HL,F0D
        LD (OBROB+1),HL
        LD HL,(xyprn)
        CALL INC_YY
        CALL INC_YY
        JR ZZ

F0D     PUSH HL
        LD HL,0
xyprn   EQU $-2
        CALL pr_51
        LD HL,(xyprn)
ZZ      CALL INC_YY
        LD (xyprn),HL
        POP HL
        JR Jp1

Dline   PUSH HL
        LD HL,(xyprn)
        INC H
        INC H
        INC H
        DEC L
        LD (HL),%01011111
        INC L
        LD B,(IX+2)
        DEC B
        DEC B
        LD A,%11111111
f0f_    LD (HL),A
        INC L
        DJNZ f0f_
        LD (HL),%11111010
        POP HL
        RET 

; Обработка горячих клавиш

HotKey  LD HL,(HotAdr)
hotkey  LD A,(HL)
        CP #FF                  ; не изменять!
        RET Z

        PUSH HL
        CALL FKEY
        JR NZ,NoFKey
        INC HL
        CALL FKEY
        JR NZ,NoFKey
FKey    POP HL
        INC HL
        INC HL
        HLFHL
        LD A,H
        AND A
        JR Z,GOpc
        POP AF
        EX DE,HL
        JP ExitGui
;       JP (HL)

GOpc    LD A,L
        PUSH AF
        LD A,(Numb)
        INC A
        CALL NZ,BarOff
        POP AF
        LD (Numb),A
        SCF 
        RET 

NoFKey  POP HL
        LD DE,4
        ADD HL,DE
        JR hotkey

REDEF   CALL #028E
        LD BC,KEYS
        PUSH DE
        CALL SKEY
        POP DE
        LD E,D
        CALL SKEY
        RET 

SKEY    LD A,E
        INC A
        JR Z,ZEROK
        CALL #0347
ZEROK   LD (BC),A
        INC BC
        RET 

FKEY    LD A,(KEY1)
        CP (HL)
        RET Z
        LD A,(KEY2)
        CP (HL)
        RET 

SaveBack
        LD HL,(ResAdr)
        JP (HL)

RestBack
        LD HL,(ResAdr)
        LD BC,3
rrs     ADD HL,BC
        JP (HL)

ExitGui LD HL,(ResAdr)
        LD BC,9
        JR rrs

