! *dev/preview - Graphical Print Preview Driver
! By Gord Davey
! (c) Copyright 2004-2005 Sage Software Canada Ltd.
! Version Info:
! Source Code File: $RCSfile: preview.pvs,v $   Version: $Revision: 1.9 $
!      Last Edited: $Date: 2006/07/19 16:45:58 $   Author: $Author: gordd $
!
! This driver contains the logic to instantiate and control the GUI Print Previewer
!   Version$ = mid(mid("$Revision: 1.9 $",12), 1, len(mid("$Revision: 1.9 $",12)) - 2)
!
Init:
    Chan = lfo
    Options$ = opt(Chan)
    FileName$ = fin(Chan, "FILENAME")

    if ucs(FileName$) = "*WINPRT*" then \
    {   winprtsetup read properties tmp$
        Options$ += ";" + tmp$
        Options$ = sub(Options$,"MARGINS=-1:-1:-1:-1","MARGINS=250:250:250:250")
        Options$ = sub(Options$,"MARGIN=-1:-1:-1:-1","MARGIN=250:250:250:250")
        ! This section deals with the viewer link file being run as a Windows Printer
        ! and will auto set the margins to 1/4" if the do not come in with a value.
    }

    Path$ = FileName$ + ";" + sub(stp(Options$, 2), ",", ";") + ";"

    if pos("WIN"=ucs(sys),1) \
        then IsWindows=1 \
        else IsUnix=1

    if tcb(88) \
        then IsWindX$="[WDX]";
             if ucs(mid(mse,32,1))="J" \
                 then IsJavX=tcb(88) \
                 else IsWindX=tcb(88)

    if IsJavX or (IsUnix and not(IsWindX)) \
        then exit 99

    close(Chan)
!
GetDiskFile:
    call "*fl.nme", DiskFile$
    if IsUnix and not(pos(dlm = DiskFile$, 1)) \
        then DiskFile$ = "/tmp/"+DiskFile$

    serial DiskFile$
!
SetLockUnnecessary:
    svlu = prm('LU')
    setparam 'LU' = 1
!
OpenDiskFile:
    open(Chan) DiskFile$
    print(Chan,err=*proceed)'-B',
    write record(Chan)"[Pvxrpt]"
    write record(Chan)"User="+uid
    write record(Chan)"Created="+fin(Chan,"UTC_CTIME")

    Page = new("*viewer/page")
!
ParseOptions:
    y = pos(";" = Path$, 1 , 0)
    for x = 1 to y
        z = pos(";" = Path$, 1)
        x$ = Path$(1, z - 1)
        Path$ = path$(z + 1)
        z = pos("=" = x$, 1)
        if z then \
        {   Var$ = stp(mid(x$, 1, z - 1) ,2), \
            Value$ = stp(mid(x$, z + 1), 2)
        } else \
        {   Var$ = x$, \
            Value$ = ""
        }

        if ucs(Var$) = "*PREVIEW*" or ucs(Var$) = "*VIEWER*" or Var$ = "" \
            then continue

        if ucs(Var$) = "INLINE" then \
        {   InLine = 1
            continue
        }

        if ucs(Var$) = "ONCLOSE" then \
        {   OnClose = 1
            continue
        }

        if ucs(Var$) = "PAPERSIZE" then \
        {   Page'PaperSize = num(Value$,err=*proceed)
        }

        if ucs(Var$) = "ORIENTATION" then \
        {   Page'Orientation$ = Value$
            n = num(Value$, err=*next);
                Page'Orientation = n
        }

        if ucs(Var$) = "MARGIN" or ucs(Var$) = "MARGINS" then \
        {
            m1 = -1, m2 = -1, m3 = -1, m4 = -1
            s = pos(":" = Value$, 1, 0)
            if s = 0 \
                then read data from Value$,sep=":" to m1
            if s = 1 \
                then read data from Value$,sep=":" to m1, m2
            if s = 2 \
                then read data from Value$,sep=":" to m1, m2, m3
            if s = 3 \
                then read data from Value$,sep=":" to m1, m2, m3, m4
            Page'MarginLeft = m1
            Page'MarginTop = m2
            Page'MarginRight = m3
            Page'MarginBottom = m4
        }

        write record(Chan) Var$+"="+Value$

    next x

!
SwitchToMarkup:
    write record(Chan)$0C$
    print(Chan,err=*proceed)'+B',
    print(Chan)'bm',
!
SetInitialPageSizing:
    Page'Metric = 0
    Width = Page'PaperWidth - ((Page'MarginLeft + Page'MarginRight) / 1000)
    Height = Page'PaperLength - ((Page'MarginTop + Page'MarginBottom) / 1000)
    print(chan)'lpi'(Height * -1), 'cpi'(Width * -1), \
               'font'("Courier New", -12),'df', \
               'lpi'(6),'cpi'(10),
    drop object Page

!
ResetLockUnnecessary:
    setparam 'LU' = svlu
!
FetchGlobalINIFileIfAny:
    ParametersSoftware = new("*viewer/parameters", "*viewer/software.prm")
    ParametersSoftware'AddVariable("Viewer","INI","ClientINIFile","S","","","","")
    ParametersSoftware'AddVariable("Viewer","INI","ServerINIFile","S","","","","")
    ParametersSoftware'GetParameters("Viewer","INI")
    ParametersSoftware'GetParameters("Viewer","Software")
    drop object ParametersSoftware
!
FetchTheINIFiles:
    if ClientINIFile$ = "" \
        then call IsWindX$ + "*windx.utl;Get_Val","ARG(-1)",ClientINIFile$

    if ServerINIFile$ = "" and pos("WIN" = ucs(SYS)) \
        then ServerINIFile$ = arg(-1)


!
ResolveArguments:
    ClientINIFile$ = quo + ClientINIFile$ + quo
    ServerINIFile$ = quo + ServerINIFile$ + quo

    DiskFile$ = quo + DiskFile$ + quo
    DeleteFile$ = "DELETE"

    if Software$ = "" \
        then Software$ = "Report Viewer"

    if InLine \
        then StartType$ = "INLINE" \
        else StartType$ = ""
!
!
DealWithOnClose:
    if OnClose or InLine then \
    {
        if ClientINIFile$ = ServerINIFile$ \
            then ServerINIFile$ = ""

        x$ = "*dev/preview;DoOnClose" + ";" + Software$ + ";" + ClientINIFile$ + ";" + ServerINIFile$ + ";" + \
                                        DiskFile$ + ";" + DeleteFile$ + ";" + StartType$
        mnemonic(Chan)'*X' = x$
        goto ExitDriver
    }
!
!
InvokeANewPreviewer:

    if InLine = 0 then \
    {
        if IsWindX and %APS then \
        {
            call "*server;spawn", "*viewer" + dlm + "viewer", Software$, "", ServerINIFile$, ClientINIFile$, \
                                  "H", "", DiskFile$ + " " + DeleteFile$
            goto ExitDriver
        }

        if IsWindX then \
        {
            call "*windx.utl;spawn", "*viewer" + dlm + "viewer -arg " + DiskFile$ + " " + DeleteFile$, ClientINIFile$, "", 1
            goto ExitDriver
        }

        invoke quo+arg(0)+quo+" -hd " + ServerINIFile$ + " *viewer\viewer -ARG " + DiskFile$ + DeleteFile$

        goto ExitDriver
    }

    ! Do an InLine instance of the Viewer. InLine means do not invoke a new session of ProvideX
    ! but use the current session. Will help for WindX serial dialup or WindX when you want to avoid
    ! using a different TCP socket.

    DiskFile$ = stp(DiskFile$, 3, quo)
    Preview = new("*viewer/preview")
    Preview'Visible = 1
    Preview'Init()
    Preview'Open(DiskFile$)
    Preview'Process()
    drop object Preview
    erase DiskFile$, err=*proceed
    goto ExitDriver
!
!
ExitDriver:
    exit
!
!
DoOnClose:
    x$ = pgm(-3)
    close(lfa)

    if tcb(88) \
        then IsWindX$="[WDX]";
             if ucs(mid(mse,32,1))="J" \
                 then IsJavX=tcb(88) \
                 else IsWindX=tcb(88)

    read data from x$,sep=";" to *, *, Software$, ClientINIFile$, ServerINIFile$, DiskFile$, \
                                 DeleteFile$, StartType$

    if ServerINIFile$ = "" \
        then ServerINIFile$ = ClientINIFile$

    if ucs(StartType$) = "INLINE" \
        then InLine = 1
    goto InvokeANewPreviewer
