{Portable Prolog EDWIN Graphics Interface}

{Compilation Flags: VAX APM EMAS I77}
{All systems execept EMAS require I77}

%record %format Point fm (%integer X, Y)

!VAX! %include "Edwin_dir:Specs.Inc"
!VAX! %include "Edwin_dir:Shapes.Inc"
!VAX! %include "Prolog_Interpreter:GDec.Inc"

{APM} %include "Edwin:Specs.Inc"
{APM} %include "Edwin:Shapes.Inc"
{APM} %include "IPlog:GDec.Inc"

%external %integer %spec X,
                         Skel 0,
                         Quote IA
%external %string (255) %function %spec Process Atom (%integer A)
%external %integer %function %spec Unify Arg (%integer A, B, C)

%constant %string (10) %array Colours (0 : 9) = %c
   "blank",
   "black",
   "blue",
   "green",
   "red",
   "purple",
   "yellow",
   "lime",
   "brown",
   "turquoise"

{-CALL EDWIN-}

%external %integer %function Call Edwin
   %integer Function Code,
            I
   %own %integer Initialised = 0
   %integer %array P (1:5)
!EMAS! %string %name Atom
{I77}  %string (*) %name Atom

   {-FISH-}

   %routine Fish (%integer Many)
      %integer I

      %for I = 1, 1, Many %cycle
         P (I) = Integer (X + V1 Of CF + (I*4))
         %if P (I) >= 0 %start
            Print String ("! Parameter ")
            Write (I + 1, 0)
            Print String (" invalid for call_edwin - need integer")
            New Line
            %signal 12
         %finish
{***         P (I) = P (I)&(\Int 0)*}
         P (I) = P (I)&16_1FFFFFFF %IF P (I) & 16_20000000 = 0
      %repeat
   %end {Fish}

   {-POKE-}

   %routine Poke (%integer Many)
      %integer I

      %for I = 1, 1, Many %cycle
         %if Unify Arg (X + V1 Of CF + I*4, P (I)!Int0, 0) = 0 %start
            Print String ("! call_edwin cannot unify")
            New Line
            %signal 12
         %finish
      %repeat
   %end {Poke}

   %on 12 %start
      %result = 0
   %finish

   %switch Function (0:63)

   Function Code = Integer (X + V1 Of CF)
   %if Function Code >= 0 %start
      Print String ("! Invalid Function code to call_edwin - should be integer")
      New Line
      %result = 0
   %finish
   Function Code = Function Code&(\Int 0)
   %if 0 <= Function Code <= 63 %start
      -> Function (Function Code)
   %else
   Function (*):
      Print String ("! Unknown Edwin function code")
      New Line
      %result = 0
   %finish

{--------------------------INITIALISATION---------------------}

Function (0):         {INITIALISE_EDWIN_FOR - given device number}
   Fish (1)
   Initialise For (P (1))
   Initialised = 1
   -> Init

Function (1):         {INITIALISE_EDWIN   - for default device}
   Initialise For (Default Device)
Init:
   Update
   Select Output (0)
   Print String ("[Edwin Graphics Package Initialised for " . %c
                 Device Data_Name . "]")
   New Line
   Initialised = 1
   %result = 1

Function (33):        {EDWIN_INITIALISED - test if already initialised}
   %result = Initialised

Function (2):         {VIEWPORT}
   Fish (4)
   View Port (P (1), P (3), P (2), P (4))   {N.B. note reordering of parameters}
   -> Bottom

Function (3):         {WINDOW}
   Fish (4)
   Window (P (1), P (3), P (2), P (4))      {N.B. note reordering of parameters}
   -> Bottom

{-------------------------DRAWING---------------------------}

Function (4):         {NEWFRAME}
   New Frame
   -> Bottom

Function (5):         {MOVE_ABS}
   Fish (2)
   Move Abs (P (1), P (2))
   -> Bottom

Function (6):         {MOVE_REL}
   Fish (2)
   Move Rel (P (1), P (2))
   -> Bottom

Function (7):         {LINE_ABS}
   Fish (2)
   Line Abs (P (1), P (2))
   -> Bottom

Function (8):         {LINE_REL}
   Fish (2)
   Line Rel (P (1), P (2))
   -> Bottom

Function (9):         {MARKER_ABS}
   Fish (3)
   Marker Abs (P (1), P (2), P (3))
   -> Bottom

Function (10):        {MARKER_REL}
   Fish (3)
   Marker Rel (P (1), P (2), P (3))
   -> Bottom
   
{-------------------------------STYLE------------------------------}

Function (11):         {SET_COLOUR}
   %if 0 < Integer (X + V2 Of CF) < Skel 0 %start
      Atom == String (Integer (X + V2 Of Cf) + St Of AE)
      %for I = 0, 1, 9 %cycle
         %if Atom = Colours (I) %start
            Set Colour (I)
            -> Bottom
         %finish
      %repeat
      Print String ("! '" . Atom . "' is not a valid colour")
      New Line
      %result = 0
   %else
      Fish (1)
      Set Colour (P (1))
   %finish
   -> Bottom

Function (12):         {SET_LINE_STYLE}
   Fish (1)
   Set Line Style (P (1))
   -> Bottom

Function (13):         {SET_SPEED}
   Fish (1)
   Set Speed (P (1))
   -> Bottom

Function (14):          {SET_SHADE_MODE}
   Fish (1)
   Set Shade Mode (P (1))
   -> Bottom

Function (34):          {SET_COLOUR_MODE}
   Fish (1)
   Set Colour Mode (P (1))
   -> Bottom

Function (15):          {SET_ARC_POINTS}
   Fish (1)
   Set Chord Step (P (1))
   -> Bottom

Function (31):          {DRIVE_DEV}
   Fish (3)
   Drive Device (P (1), P (2), P (3))

!{APM}   Drive Dev (P (1), P (2), P (3))
   -> Bottom

{-------------------------------SHAPES-----------------------------}

Function (16):          {RECTANGLE}
   Fish (4)
   Rectangle (P (1), P (2), P (3), P (4))
   -> Bottom

Function (17):          {CIRCLE}
   Fish (1)
   Circle (P (1))
   -> Bottom

Function (18):          {ARC}
   Fish (5)
   Arc (P (1), P (2), P (3), P (4), P (5))
   -> Bottom

Function (19):          {SECTOR}
   Fish (5)
   Sector (P (1), P (2), P (3), P (4), P (5))
   -> Bottom

{-------------------------------TEXT-------------------------------}

Function (20):          {TEXT}
   Quote IA = 0
Do Text:
   P (1) = Integer (X + V2 Of CF)
   %unless 0 <= P (1) <= Skel 0 %start
      Print String ("! Invalid Parameter to EDWIN Text - need an Atom")
      New Line
      %result = 0
   %finish
   Text (Process Atom (P (1)))
   -> Bottom

Function (21):          {TEXTQ}
   Quote IA = 1
   -> Do Text
   
Function (22):          {SET_CHAR_SIZE}
   Fish (1)
   Set Char Size (P (1))
   -> Bottom

Function (23):          {SET_CHAR_ROT}
   Fish (1)
   Set Char Rot (P (1))
   -> Bottom

Function (24):          {SET_CHAR_QUALITY}
   Fish (1)
   Set Char Quality (P (1))
   -> Bottom

Function (25):          {SET_CHAR_FONT}
   Fish (1)
   Set Char Font (P (1))
   -> Bottom

Function (26):          {SET_CHAR_SLANT}
   Fish (1)
   Set Char Slant (26)
   -> Bottom

{-------------------------------INQUIRIES--------------------------}

Function (27):          {CURSOR}
   Request Input (P (1), P (2), P (3))
   Poke (3)
   %result = 1

Function  (32):         {SAMPLE CURSOR}
   Sample Input (P (1), P(2), P (3))
   Poke (3)
   %result = 1

Function (28):          {INQUIRE_POSITION}
   Inquire Position (P (1), P (2))
   Poke (2)
   %result = 1

Function (29):          {INQUIRE_VIEWPORT}
   Inquire Viewport (P (1), P (3), P (2), P (4))
   Poke (4)
   %result = 1

Function (30):          {INQUIRE_WINDOW}
   Inquire Window (P (1), P (3), P (2), P (4))
   Poke (4)
   %result = 1

{-------------------------------TERMINATION------------------------}

Function (63):        {TERMINATE}
   Terminate Edwin
   Select Output (0)
   Print String ("[Edwin Graphics Terminated]")
   New Line
   Initialised = 0
   %result = 1

Bottom:
   Update
   %result = 1
%end {Call Edwin}

%end %of %file
