; 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.
;
; > Sources.Window

template_file_name
                DCB     "DisplayManager:Templates",0
                ALIGN

wn_display      DCB     "Display",0
                ALIGN
wn_mode         DCB     "Mode",0
                ALIGN
wn_info         DCB     "Info",0
                ALIGN

;---------------------------------------------------------------------------
; Window_Init
;
;       Out:    r0 corrupted
;
;       Create windows from Templates file.
;
Window_Init
        ROUT
        MOV     r0, #0                  ; So we can tell if we get an error at the end.
        Entry   "r0-r6"

        Debug   win,"Window_Init"

        ADR     r1, template_file_name
        SWI     XWimp_OpenTemplate
        STRVS   r0, [sp]
        EXIT    VS

        MOV     r3, #0                  ; r3 = largest template space required
        MOV     r4, #0                  ; r4 = largest indirected work space required

        ADR     r5, wn_display          ; Find space required.
        BL      add_template
        ADRVC   r5, wn_mode
        BLVC    add_template
        ADRVC   r5, wn_info
        BLVC    add_template
        STRVS   r0, [sp]
        BVS     %FT20

        Debug   win," space required =",r3,r4

        MOV     r0, #ModHandReason_Claim        ; Claim scratch space for templates.
        SWI     XOS_Module
        STRVS   r0, [sp]
        BVS     %FT20
        MOV     r1, r2                  ; r1 -> template space

        MOV     r3, r4                  ; Claim space for indirected data.
        SWI     XOS_Module
        STRVS   r0, [sp]
        BVS     %FT10
        STR     r2, indirected_data     ; r2 -> indirected data space

        Debug   win," space allocated at",r1,r2

        ADD     r3, r2, r3              ; r3 -> end of indirected data space

        ADR     r5, wn_display          ; Create display window.
        BL      load_template
        LDRVC   lr, [r1, #w_icons+i_size*ic_display_resolution+i_data+0]
        STRVC   lr, resolution_indirect
        LDRVC   lr, [r1, #w_icons+i_size*ic_display_resolution+i_data+8]
        STRVC   lr, resolution_size
        LDRVC   lr, [r1, #w_icons+i_size*ic_display_colours+i_data+0]
        STRVC   lr, colours_indirect
        LDRVC   lr, [r1, #w_icons+i_size*ic_display_colours+i_data+8]
        STRVC   lr, colours_size
 [ SelectFrameRate
        LDRVC   lr, [r1, #w_icons+i_size*ic_display_rate+i_data+0]
        STRVC   lr, rate_indirect
        LDRVC   lr, [r1, #w_icons+i_size*ic_display_rate+i_data+8]
        STRVC   lr, rate_size
 ]
        LDRVC   lr, [r1, #w_title]
        STRVC   lr, title_indirect
        LDRVC   lr, [r1, #w_title+8]
        STRVC   lr, title_size
        SWIVC   XWimp_CreateWindow
        STRVC   r0, display_handle
        BLVC    Window_SetTitle

        ADRVC   r5, wn_mode             ; Create mode window.
        BLVC    load_template
        LDRVC   lr, [r1, #w_icons+i_size*ic_mode_mode+i_data+0]
        STRVC   lr, mode_indirect
        LDRVC   lr, [r1, #w_icons+i_size*ic_mode_mode+i_data+8]
        STRVC   lr, mode_size
        SWIVC   XWimp_CreateWindow
        STRVC   r0, mode_handle

        ADRVC   r5, wn_info             ; Create info window.
        BLVC    load_template
        BLVC    patch_info_version
        SWIVC   XWimp_CreateWindow
        STRVC   r0, info_handle
        STRVS   r0, [sp]
10
        MOV     r0, #ModHandReason_Free ; Free scratch space for templates.
        MOV     r2, r1
        SWI     XOS_Module
20
        SWI     XWimp_CloseTemplate

        PullEnv
        TEQ     r0, #0
        SETV    NE
        MOV     pc, lr

patch_info_version
        Entry   "r1-r3"
        LDR     r2, [r1, #w_icons+i_size*ic_info_version+i_data+0]
        LDR     r3, [r1, #w_icons+i_size*ic_info_version+i_data+8]
        ADR     r1, Version_tag
        BL      MsgTrans_Lookup
        EXIT

Version_tag     DCB     "_Version",0

        ALIGN

;---------------------------------------------------------------------------
; add_template
;
;       In:     r3 = size of largest template so far
;               r4 = total size of indirected data so far
;               r5 -> template name
;       Out:    r0,r1,r2,r6 corrupted
;               r3 = updated template size
;               r4 = updated indirected data size
;               r5 -> actual name
;
;       Used to determine the amount of space needed to load templates.
;
add_template
        Entry   "r3,r4"
        MOV     r1, #-1
        BL      load_template
        EXIT    VS
        PullEnv
        CMP     r3, r1                  ; Only need space for largest template.
        MOVCC   r3, r1
        ADD     r4, r4, r2              ; But need space for all indirected data.
        MOV     pc, lr


;---------------------------------------------------------------------------
; load_template
;
;       Wrapper for Wimp_LoadTemplate.  Either loads a template or
;       determines its size.  Returns an error if the requested
;       template is not found.
;
load_template
        Entry
        MOV     r4, #-1
        MOV     r6, #0
        SWI     XWimp_LoadTemplate
        TEQ     r6, #0
        ADREQ   r0, ErrorBlock_Modes_NoTemplate
        BLEQ    MsgTrans_ErrorLookup
        EXIT

        MakeErrorBlock  Modes_NoTemplate


;---------------------------------------------------------------------------
; Window_Open
;
;       In:     r1 -> open window request block
;
;       The Wimp has asked us to open a window.
;
Window_Open
        Entry
        SWI     XWimp_OpenWindow
        EXIT


;---------------------------------------------------------------------------
; Window_OpenBehind
;
;       In:     r0 = window handle
;               r1 = handle of window to open behind
;               r2 = min x position of window
;               r3 = min y position of window
;       Out:    r0 corrupted
;
;       Open one window behind another.
;
Window_OpenBehind
        Entry   "r1-r6"

        MOV     r4, r1
        ADR     r1, user_data
        STR     r0, [r1]
        SWI     XWimp_GetWindowState
        EXIT    VS

        STR     r4, [r1, #u_bhandle]
        LDMIB   r1, {r4-r6,lr}
        SUB     r4, r6, r4
        ADD     r4, r2, r4
        SUB     r5, lr, r5
        ADD     r5, r3, r5
        STMIB   r1, {r2-r5}
        SWI     XWimp_OpenWindow

        EXIT


;---------------------------------------------------------------------------
; Window_Close
;
;       In:     r1 -> close window request block
;
;       The Wimp has asked us to close a window.
;
Window_Close
        Entry   "r0"
        SWI     XWimp_CloseWindow
        EXIT


;---------------------------------------------------------------------------
; Window_SetTitle
;
;       Out:    r0 corrupted
;
;       Set the window title to the monitor name supplied by the ScreenModes module.
;
Window_SetTitle
        ROUT
        Entry   "r1-r4"

        LDR     r2, title_indirect
        LDR     r3, title_size

        MOV     r0, #0
        SWI     XScreenModes_ReadInfo
        BVS     %FT20
10
        SUBS    r3, r3, #1
        STREQB  r3, [r2]
        LDRNEB  r1, [r0], #1
        STRNEB  r1, [r2], #1
        TEQNE   r1, #0
        BNE     %BT10
        B       %FT30
20
        ADRL    r1, banner
        BL      MsgTrans_Lookup
        EXIT    VS
30
        ADR     r1, user_data
        LDR     lr, display_handle
        STR     lr, [r1]
        SWI     XWimp_GetWindowOutline

        LDMVCIB r1, {r1-r4}
        MOVVC   r0, #-1
        SUBVC   r2, r4, #44
        SWIVC   XWimp_ForceRedraw

        CLRV
        EXIT


        END