! *viewer/splashscreen.pvc - Splash Screen Object
! By Gord Davey
! (c) Copyright 2003-2005 Sage Software Canada Ltd.
! Version Info:
! Source Code File: $RCSfile: splashscreen.pvs,v $   Version: $Revision: 1.5 $
!      Last Edited: $Date: 2005/10/14 15:31:18 $   Author: $Author: gordd $
!
! This object contains the logic to create and control a splash screen
!
! It will create an AlwaysOnTop Dialogue, centered on the user's desktop
! It will size the window to the size of the image provided
!
!
!
def class "*viewer/splashscreen" create OnCreate required delete OnDelete required
    precision 4

    property Version$ = mid(mid("$Revision: 1.5 $",12), 1, len(mid("$Revision: 1.5 $",12)) - 2)
    local This
    local IsWindX, IsJavX, IsWindX$, WinAPI, mse$, obj$
    local PWhnd ! ProvideX Window Handle
    local Text$ ! ImageHandleName for the Status Text$
    local WinMon, mLeft, mTop, mRight, mBottom, wLeft, wTop, wRight, wBottom ! might as well remember them
    local iWidth, iHeight
    local xChar, yChar
    local Column, Columns, Row, Rows
    local DefaultFont$, DefaultColour$

    property Visible    set SetVisible        ! Set to 1=create, 0=destroy

    property Image$     set ValidImage     ! Path and Filename of image to use as splash
    ! If running locally this is the name of the local image
    ! if running in WindX, then this is the name of the image from WindX's point of view
    ! if running in JavX, then this is the local (server side) image name, to get sizing

    property JavXImage$   ! Path and Filename of image for JavX to retrieve
    ! if running locally or in WindX then this is not used
    ! if running in JavX, this is the file name to be passed to the picture command in JavX
    ! if null then Image$ is used. No validation is done on this filename,

    ! 0= no status, 1=bottom left (default), 2=bottom right, 3=top left, 4=top right
    property Status      set SetStatus
    property Status$     set SetStatusText   ! Text info to dislay in Status area
    property StatusFont$    set SetStatusFont ! Font Name and Size (Default to system Graphical Font)
    property StatusColour$   set SetStatusColour ! Colour for Font

    function local CreateSplash()   CreateSplash
    function local DestroySplash()  DestroySplash
    function local ValidImage(Image$)     ValidImage
    function local DrawText()       DrawText

fnend
!
!
OnCreate:
    This = _obj
    mse$ = mse
    obj$ = obj(0)

    Text$ = "SplashImage"+str(This)

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

    if pos("WIN" = ucs(sys),1) = 0 and tcb(88)=0 \
        then exit 99

    if not(IsJavX) \
        then WinAPI = new(IsWindX$ + "*obj/winapi")

    Status = 1

    enter (Image$), (JavXImage$), err=*next ! Can pass image on creation or not.

    if Image$ > "" \
        then This'ValidImage(Image$, err=BadPicture)

    return

    BadPicture:
    exit err
!
!
SetVisible:
    enter (Value%)

    if Value% < 0 or Value% > 1 \
        then exit 41

    if Value% = Visible \
        then return Visible

    if Image$ = "" \
        then exit 17 ! Must set the image first

    switch Value%
        case 0      ! Destroy Splash
            This'DestroySplash()
            break

        case 1      ! Create Splash
            This'CreateSplash()
            break
    esac

    Visible = Value%
    return Visible
!
!
SetStatus:
    enter (Status)
    print '-t',
    This'DrawText()
    wait 0
    print '+t',
    return Status
!
!
SetStatusText:
    enter (Status$)
    print '-t',
    This'DrawText()
    wait 0
    print '+t',
    return Status$
!
!
SetStatusFont:
    enter StatusFont$
    print '-t',
    This'DrawText()
    wait 0
    print '+t',
    return StatusFont$
!
!
SetStatusColour:
    enter StatusColour$
    print '-t',
    This'DrawText()
    wait 0
    print '+t',
    return StatusColour$
!
!
ValidImage:

    enter (Value$)

    if Value$ = "" \
        then exit 17

    call IsWindX$ + "*picture;get_size_pixels", err=NoValidImage, Value$, w, h
    Image$ = Value$
    iWidth = w
    iHeight = h

    if Visible then \
    {   print '-t','-U',
        This'Visible = 0
        This'Visible = 1
        print '+t','-U',
    }
    return 1

    NoValidImage:
    exit err
!
!
CreateSplash:
    if Visible \
        then return 0

    if not(IsJavX) and WinMon = 0 then \
    {   if WinMon = 0 then \
        {   w = WinAPI'GetUltimateParent(0)
            WinAPI'GetWindowPlacement(w, Left, Top, Right, Bottom)
            WinMon = WinAPI'GetMonitorFromRect(Left, Top, Right, Bottom)
            WinAPI'GetMonitorInfo(WinMon, mLeft, mTop, mRight, mBottom, wLeft, wTop, wRight, wBottom)
            DialogFrameX = WinAPI'GetSystemMetrics(7)
            DialogFrameY = WinAPI'GetSystemMetrics(8)
        }
    }

    if wLeft = 0 and wTop = 0 and wRight = 0 and wBottom = 0 then \
    {   wRight = dec($00$ + mid(mse$, 27, 2))
        wBottom = dec($00$ + mid(mse$, 29, 2))
    }

    xChar = dec($00$ + mse$(10, 1))
    yChar = dec($00$ + mse$(11, 1))

    Columns = int(iWidth / xChar)
    Rows = int(iHeight / yChar)

    Left = int((wRight - wLeft - (iWidth + (DialogFrameX * 2))) / 2 + wLeft)
    Top = int((wBottom - wTop - (iHeight + (DialogFrameY * 2))) /2 + wTop)

    Column = int(int((wRight - mLeft - iWidth) / 2) / xChar)
    Row = int(int((wBottom - mTop - iHeight) / 2) / yChar)

    pWhnd = hwn(0)
    print (0,err=*next)'-U','cursor'(1),'dialogue'(Column, Row, Columns, Rows, pWhnd, 'b?'+'df'+'cs', opt="^ch"),'sr',

    if IsJavX \
        then print (0,err=*next)'picture'(@x(0),@y(0),@x(Columns),@y(Rows),JavXimage$,0), \
        else print (0,err=*next)'picture'(@x(0),@y(0),@x(Columns),@y(Rows),image$,0),

    if not(IsJavX) then \
    {   w = dec(mid(obj(0),17,4))
        WinAPI'SetWindowPlacement(w, Left, Top, Left + iWidth + (DialogFrameX * 2), Top + iHeight + (DialogFrameY * 2))
    }

    if DefaultFont$ = "" then \
    {   x =31005
        button x,@(0,0,0,0)=""
        DefaultFont$ = x'Font$
        DefaultColour$ = 'F7'
        button remove x
    }

    This'DrawText()
    print (0,err=*next)'+U',
    wait 0

    Visible = 1

    return 1
!
!
DrawText:
    print (0,err=*next)'image'(delete Text$),

    if Status = 0 or Status$ = "" \
        then return 1

    switch Status
        case 1 ! Bottom Left
            x = 1.6, y = Rows - 1.2, l = Columns * 0.70, option$=""
            break
        case 2 ! Bottom Right
            y = Rows - 1.2, l = Columns * 0.70, x = Columns - 1.6 - l, option$="R"
            break
        case 3 ! Top Left
            x = 1.6, y = .3, l = Columns * 0.70, option$=""
            break
        case 4 ! Top Right
            y = .3, l = Columns * 0.70, x = Columns - 1.6 - l, option$="R"
            break
    esac

    Font$=tbl(StatusFont$ > "", DefaultFont$, StatusFont$)
    read data from Font$,sep="," to FontName$,FontSize,FontOpt$
    if option$="R" \
        then FontOpt$ += "R"

    print (0,err=*next)'image'(Text$), \
                       tbl(StatusColour$ > "", DefaultColour$, StatusColour$), \
                       'font'(FontName$,FontSize,stp(FontOpt$,3,quo)),
    switch option$
        case "R"
            print 'text'(@x(x),@y(y),@x(x+l),@y(y+1),Status$), \
                  'image'(""),
            break
        default
            print 'text'(@x(x),@y(y),@x(l),@y(y+1),Status$), \
                  'image'(""),
            break
    esac
    return 1
!
!
DestroySplash:

    if not(Visible) then \
    {   print '+U',
        return 0
    }

    print (0,err=*next)'image'(delete Text$),'image'(""),'drop'(pWhnd),
    pWhnd = 0

    print (0,err=*next)'cursor'(0),'+U',
    wait 0

    Visible = 0

    return 1
!
!
OnDelete:
    This'Visible = 0

    if WinAPI \
        then drop object WinAPI
    return
