! *viewer/papersize.pvc - Paper Size Object
! By Gord Davey
! (c) Copyright 2004-2005 Sage Software Canada Ltd.
! Version:
! Source Code File: $RCSfile: papersize.pvs,v $   Version: $Revision: 1.3 $
!      Last Edited: $Date: 2005/10/14 15:31:18 $   Author: $Author: gordd $
!
! This object is used to store and track info relating to a paper size
!
def class "*viewer/papersize" create OnCreate required delete OnDelete required

    property Version$ = mid(mid("$Revision: 1.3 $",12), 1, len(mid("$Revision: 1.3 $",12)) - 2)
    precision 4

    property PaperLength        get GetPaperLength  set SetPaperLength
    property PaperWidth         get GetPaperWidth   set SetPaperWidth
    property PaperName$                             set err
    property PaperSize                              set SetPaperSize

    property Orientation                            set SetOrientation       ! 0=Portrait, 1=Landscape
    property Orientation$                           set SetOrientationString ! "Portrait" or "Landscape"

    property Metric                                 set SetMetric ! 0=Imperial, 1=Metric
    function Metric(Value%)     SetMetric

    local PaperWidthInches, PaperLengthInches, PaperWidthmm, PaperLengthmm
    local Convert, PaperSizes
    function local FindMatchingPaper(LengthInches, WidthInches, Lengthmm, Widthmm, Metric)   FindMatchingPaper

fnend
!
!
OnCreate:
    if not(Convert) \
        then Convert = new("*viewer/convertlengths")
    if not(PaperSizes) \
        then PaperSizes = new("*viewer/papersizes")
    Orientation$ = "Portrait"
    PaperSize = PaperSizes'DMPAPER_LETTER
    PaperSizes'GetPaperSize(PaperSize, PaperName$, \
                            PaperWidthInches, PaperLengthInches, \
                            PaperWidthmm, PaperLengthmm)
    return 1
!
!
OnDelete:
    if Convert \
        then drop object Convert
    if PaperSizes \
        then drop object PaperSizes
!
!
GetPaperLength:
    if Orientation = 0 \
        then return tbl(Metric = 1, PaperLengthInches, PaperLengthmm) \
        else return tbl(Metric = 1, PaperWidthInches, PaperWidthmm)
!
!
GetPaperWidth:
    if Orientation = 0 \
        then return tbl(Metric = 1, PaperWidthInches, PaperWidthmm) \
        else return tbl(Metric = 1, PaperLengthInches, PaperLengthmm)
!
!
SetMetric:
    enter (Value%)
    Metric=not(not(Value%))
    return 1
!
!
SetOrientation:
    enter (Value%)
    Orientation = not(not(Value%))
    Orientation$ = tbl(Orientation = 1, "Portrait", "Landscape")
    return 1
!
!
SetOrientationString:
    enter (Value$)

    switch ucs(Value$)
        case "PORTRAIT"
            Orientation = 0
            Orientation$ = "Portrait"
            break

        case "LANDSCAPE"
            Orientation = 1
            Orientation$ = "Landscape"
            break

    esac
    return 1
!
!
SetPaperSize:
    enter Value%

    if not(PaperSizes'GetPaperSize(Value%, n$, wi, li, wm, lm)) \
        then return 0

    PaperSize = Value%
    PaperName$ = n$
    if PaperSize < PaperSizes'DMPAPER_USER \
        then PaperName$ = n$, \
             PaperWidthInches = wi, \
             PaperLengthInches = li, \
             PaperWidthmm = wm, \
             PaperLengthmm = lm

    return 1
!
!
SetPaperLength:
    enter (Value)

    switch Metric
        case 0
            Value = prc(Value, 3)
            if Value < 0 or Value > 99.9999 \
                then error = 48
            if Orientation = 0 then \
            {   li = Value
                wi = PaperWidthInches
                lm = Convert'InchesToMillimeters(li)
                wm = PaperWidthmm
            } else \
            {   li = PaperLengthInches
                wi = Value
                lm = PaperLengthmm
                wm = Convert'InchesToMillimeters(wi)
            }
            break

        case 1
            Value = prc(Value, 1)
            if Value < 0 or Value > 99999.9 \
                then error = 48
            if Orientation = 0 then \
            {   lm = Value
                wm = PaperWidthmm
                li = Convert'MillimetersToInches(lm)
                wi = PaperWidthInches
            } else \
            {   lm = PaperLengthmm
                wm = Value
                li = PaperLengthInches
                wi = Convert'MillimetersToInches(wm)
            }
            break

    esac

    if error \
        then return 0

    Result = _obj'FindMatchingPaper(li, wi, lm, wm, Metric)

    if Result = PaperSizes'DMPAPER_USER then \
    {   PaperSize = Result
        PaperSizes'GetPaperSize(PaperSize, PaperName$)
        PaperLengthInches = li
        PaperWidthInches = wi
        PaperLengthmm = lm
        PaperWidthmm = wm
    } else \
    {   _obj'PaperSize = Result
    }

    return 1
!
!
SetPaperWidth:
    enter (Value)

    switch Metric
        case 0
            Value = prc(Value, 3)
            if Value < 0 or Value > 99.9999 \
                then error = 48
            if Orientation = 0 then \
            {   li = PaperLengthInches
                wi = Value
                lm = PaperLengthmm
                wm = Convert'InchesToMillimeters(wi)
            } else \
            {   li = Value
                wi = PaperWidthInches
                lm = Convert'InchesToMillimeters(li)
                wm = PaperWidthmm
            }
            break

        case 1
            Value = prc(Value, 1)
            if Value < 0 or Value > 99999.9 \
                then error = 48
            if Orientation = 0 then \
            {   lm = PaperLengthmm
                wm = Value
                li = PaperLengthInches
                wi = Convert'MillimetersToInches(wm)
            } else \
            {   lm = Value
                wm = PaperWidthmm
                li = Convert'MillimetersToInches(lm)
                wi = PaperWidthInches
            }
            break

    esac

    if error \
        then return 0

    Result = _obj'FindMatchingPaper(li, wi, lm, wm, Metric)

    if Result = PaperSizes'DMPAPER_USER then \
    {   PaperSize = Result
        PaperSizes'GetPaperSize(PaperSize, PaperName$)
        PaperLengthInches = li
        PaperWidthInches = wi
        PaperLengthmm = lm
        PaperWidthmm = wm
    } else \
    {   _obj'PaperSize = Result
    }

    return 1
!
!
FindMatchingPaper:
    enter LengthInches, WidthInches, Lengthmm, Widthmm, Type

    if LengthInches = 0 or WidthInches = 0 or \
       Lengthmm = 0 or Widthmm = 0 \
        then return PaperSizes'DMPAPER_USER


    for x=1 to 255
        if not(PaperSizes'GetPaperSize(x, n$, wi, li, wm, lm)) \
            then continue

        switch Type
            case 0
                if li = LengthInches and wi = WidthInches \
                    then match = x
                break

            case 1
                if lm = Lengthmm and wm = Widthmm \
                    then match = x
                break

        esac

        if match \
            then break

    next

    return tbl(match = 0, match, PaperSizes'DMPAPER_USER)
