; Copyright 1996 Acorn Computers Ltd
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
;     http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
;
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; In    r0 = window handle

; Out   dirviewer freed
;       r0 and flags preserved if no error

DeleteDirBlock EntryS "r0-r2, r4, r11"

 [ debug
 DREG r0, "DeleteDirBlock: window handle ",,Integer
 ]
        ADR     r1, ViewerList - d_link ; Need to keep pointer to previous node

01      LDR     r4, [r1, #d_link]
        CMP     r4, #Nowt
        EXITS   EQ

 [ debug
 DREG r4, "compare with block at "
 ]
        LDR     r14, [r4, #d_handle]
        TEQ     r14, r0
        MOVNE   r1, r4
        BNE     %BT01

        LDR     r14, [r4, #d_link]      ; Delink from ViewerList
        STR     r14, [r1, #d_link]

 [ debugalloc
 DREG r4, "Freeing block ",cc
 DREG r14, ", stored next^ ",cc
 DREG r1, " in block "
 ]
 [ ShowOpenDirs
        MOV     R0,#refresh_close
        LDR     R11, [R4, #d_dirnamestore]
        BL      RedrawParentIcon

        MOV     R2,R11                  ; Free dirname block
 |
        LDR     R2, [R4, #d_dirnamestore]
 ]
        BL      SFreeArea

        MOV     r2, r4                  ; Free dirviewer block
        BL      SFreeArea
        EXITS   VC

        STR     r0, [sp, #Proc_RegOffset]
        EXIT

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; In    r0 = window handle

; Out   dirviewer freed, window deleted (implicit close done by WindowManager)
;       r0 and flags preserved if no error

DeleteDir EntryS "r0"

        BL      DeleteDirBlock

        MOV     r14, r0
        LDR     r0, [sp, #Proc_RegOffset]
        STRVS   r14, [sp, #Proc_RegOffset]
        BL      DeleteWindow
        EXITS   VC

        STR     r0, [sp, #Proc_RegOffset]
        EXIT

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; In    r0 = window handle

; Out   r0 and flags preserved if no error

DeleteWindow EntryS "r0, r1"

        LDR     r14, sel_whandle        ; Destroying selection handle ?
        TEQ     r14, r0
        BNE     %FT10

        MOV     r14, #Nowt              ; Zap it then
        STR     r14, sel_whandle

        MOV     r1, #-1                 ; Collapse menu tree
        SWI     XWimp_CreateMenu
        EXIT    VS
        LDR     r0, [sp, #Proc_RegOffset] ; Thank you Mr. Window Manager


10      SUB     sp, sp, #u_windowstate  ; Temp frame
        MOV     r1, sp
        STR     r0, [r1, #u_handle]
 [ debugredraw
 DREG r0,"deleting window ",,Integer
 ]
        SWI     XWimp_DeleteWindow      ; Which does implicit CloseWindow
        ADD     sp, sp, #u_windowstate
        EXITS   VC

        STR     r0, [sp, #Proc_RegOffset]
        EXIT

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; In    r1 -> path name (eg. net#vax:, adfs::discname.$.jim)

; Out   windows relating to this and any subdirectories are closed

DeleteSubDirs Entry "r1-r5, r7", 4

 [ debugclosedir
        DSTRING r1,"delete sub dirs of ",cc
        DREG    r2, " with flags "
 ]
        TST     r2, #1
        BNE     %FT10

        ; Canonicalise the path before use
        MOV     r0, #FSControl_CanonicalisePath
        ADR     r2, userdata
        MOV     r3, #0
        MOV     r4, #0
        MOV     r5, #userdata_size
        SWI     XOS_FSControl
      [ version < 145
        EXIT    VS
      ]

        ADR     r1, userdata

10
 [ debugclosedir
        DSTRING r1, "Canonical form is "
 ]
        STR     r1, [sp]

        LDR     r7, lower_case_table

20      LDR     r2, ViewerList          ; Always start from head of list

30      CMP     r2, #Nowt
        EXIT    EQ

        LDR     r4, [r2, #d_dirname] ; r4 -> canonical dirname
 [ debugclosedir
        DSTRING r4,"trying against dir "
 ]
        LDR     r1, [sp]                ; r1 -> canonicalised pathname

40      LDRB    r3, [r1], #1
        LDRB    r3, [r7, r3]

        LDRB    r0, [r4], #1
        LDRB    r0, [r7, r0]

        TEQ     r3, #delete
        MOVEQ   r3, #0
        TEQ     r0, #delete
        MOVEQ   r0, #0

        CMP     r3, #" "                ; added by NK...
        MOVLT   r3, #0
        CMP     r0, #" "
        MOVLT   r0, #0

        TEQ     r0, #"&"
        MOVEQ   r0, #"$"                ; canonicalise returns X.$ (even for NetFS)

        TEQ     r3, #0
        TEQEQ   r0, #"."
        MOVEQ   r0, #0                  ; so fred matches fred and fred.$, but not freda

        CMP     r3, #0                  ; ended name ?
        CMPEQ   r0, #0                  ; Added NK
        BEQ     %FT50                   ; must have matched dirname up to now

        CMP     r3, r0
        BEQ     %BT40

        LDR     r2, [r2, #d_link]       ; failed match, try next dirviewer
        B       %BT30


50      LDR     r0, [r2, #d_handle]     ; we've logged off/dismounted disc
        BL      close_window_r0
        EXIT    VS
        B       %BT20                   ; Back to the beginning again
                                        ; (links moved)
        END