-- ImageNut.mesa   April 1, 1982 1:28 pm
-- Implemented by Gifford and Cattell, March 1982
--  Last edited by:
  -- Willie-Sue on: June 28, 1982 4:14 pm
  -- Cattell on: October 26, 1982 12:10 pm

DIRECTORY
  CIFS USING [GetFC, Open, read],
  Containers USING [ChildXBound, ChildYBound],
  DBView: TYPE USING [GetName],
  File USING [Capability],
  Graphics: TYPE USING [ImageRef, DrawImage, Box, GetBounds, SetCP, Scale],
  GraphicsOps: TYPE USING [NewAisImageFromCapability, ImageBox],
  GraphicsBasic,
  Nut: TYPE USING [Register, DisplayProc],
  Rope: TYPE USING [ROPE],
  ViewerClasses: TYPE USING [ViewerClass, ViewerClassRec, Viewer, ViewerRec, PaintProc],
  ViewerOps: TYPE USING [RegisterViewerClass, CreateViewer];
  
  
ImageNut: PROGRAM
  IMPORTS CIFS, Containers, DBView, Graphics, GraphicsOps, Nut, ViewerOps = {
  
  DisplayImage: Nut.DisplayProc = {
    -- display an image
    child: ViewerClasses.Viewer;
    fileName: Rope.ROPE ← DBView.GetName[e];
    cap: File.Capability← CIFS.GetFC[CIFS.Open[fileName, CIFS.read]];
    image: Graphics.ImageRef ← GraphicsOps.NewAisImageFromCapability[cap];
    -- create a child viewer
    info: ViewerClasses.ViewerRec←
      [parent: newV, wx: 0, wy: 0, ww: newV.cw, wh: newV.ch, data: image, border: FALSE];
    child ← ViewerOps.CreateViewer[flavor: $Image, info: info];
    Containers.ChildYBound[newV, child];
    Containers.ChildXBound[newV, child];
    };
      
  PaintImage: ViewerClasses.PaintProc = {
    -- paint an image viewer
    box: Graphics.Box;
    xmin, ymin, xmax, ymax, scale: REAL;
    image: GraphicsBasic.ImageRef ← LOOPHOLE[self.data];
    -- scale image to fit in the available space
    [xmin, ymin, xmax, ymax] ← GraphicsOps.ImageBox[image];
    box ← Graphics.GetBounds[context];
    scale ← MIN[(box.xmax - box.xmin)/(xmax - xmin), (box.ymax - box.ymin)/(ymax - ymin)];
    -- Put orign of picture in lower left corner
    Graphics.SetCP[self: context, x: box.xmin - xmin, y: box.ymin - ymin];
    Graphics.Scale[self: context, sx: scale, sy: scale];
    -- now paint image
    Graphics.DrawImage[context, image];
    };
  
 
   Init: PROC = {
    -- set up the world
    -- create a new type of viewer for images
    vc: ViewerClasses.ViewerClass ← NEW[ViewerClasses.ViewerClassRec];
    vc.paint ← PaintImage;
    ViewerOps.RegisterViewerClass[flavor: $Image, class: vc];
    -- register us as a nut!
    Nut.Register[domain: "Picture", display: DisplayImage];
    Nut.Register[domain: "Map", display: DisplayImage];
    };
    
  -- start trap does it all.
  
  Init[];
  
  
}.