File : graphics_windows.ads


-----------------------------------------------------------------------
--                              Win_IO                               --
--     A simple set of packages for graphical input and output       --
--                                                                   --
--                       Copyright (C) 2001-2010                     --
--                 Universidad de Cantabria, SPAIN                   --
--                                                                   --
-- Author: Michael Gonzalez       mgh@unican.es                      --
--                                                                   --
-- This is free software; you can redistribute it and/or             --
-- modify it under the terms of the GNU General Public               --
-- License as published by the Free Software Foundation; either      --
-- version 2 of the License, or (at your option) any later version.  --
--                                                                   --
-- This software is distributed in the hope that it will be useful,  --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of    --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU --
-- General Public License for more details.                          --
--                                                                   --
-- You should have received a copy of the GNU General Public         --
-- License along with this program; if not, write to the             --
-- Free Software Foundation, Inc., 59 Temple Place - Suite 330,      --
-- Boston, MA 02111-1307, USA.                                       --
--                                                                   --
-----------------------------------------------------------------------
--                                                                   --
-- The Graphics_Windows package provides a simple window with        --
-- drawing capabilities.                                             --
--                                                                   --
-- The interface of this package is a modification of the "Canvas"   --
-- interface of the JEWL.Windows package, by John English.           --
-- See http://www.it.bton.ac/uk/staff/je/jewl for a complete copy    --
-- of JEWL (John English Windows Library)                            --
--                                                                   --
-- This package requires Gtkada-2.2.0 or later                       --
--                                                                   --
-----------------------------------------------------------------------

with Gdk;
with Gdk.Window;
with Gdk.GC;
with Pango.Font;
with Graphics_Window_Pkg; use Graphics_Window_Pkg;

package Graphics_Windows is

   -----------------------------------------------------------------------
   --
   --                           C A N V A S
   --
   --  A canvas is a window that provides a blank rectangle for drawing
   --  It also provides an OK button for which a Wait operation is
   --  provided.
   --  A canvas has an associated font size, a pen colour,
   --  a pen size for drawing lines (initially black, one pixel wide)
   --  and a fill colour used to colour closed shapes (initially white).
   --
   -----------------------------------------------------------------------

   type Canvas_Type is private;

   -----------------------------------------------------------------------
   --
   --                      S U P P O R T   T Y P E S
   --
   --  Point_Type      : (X,Y) coordinates of a point on the window
   --                    origin is at the top left corner
   --                    "+" and "-" operators defined
   --  Point_List      : An array of points, used by some operations
   --
   --  Image_Type      : Refers to an image, obtained through the Image
   --                    operation. The supported formats are:
   --                    JPG, JPEG, GIF, PNG, TIFF, XPM, PNM
   --
   --  Colour_Type     : A colour specified as an RGB value.
   --                    Several common colors are specified as constants
   --                    Operations Light and Dark provided for
   --                    convenience.
   --  Color_Type      : A renaming of the above, with American spelling
   --
   -----------------------------------------------------------------------

   subtype Colour_Range is Integer range 0..255;
   subtype Color_Range is Colour_Range;

   type Colour_Type  is record
      Red    : Colour_Range;
      Green  : Colour_Range;
      Blue   : Colour_Range;
   end record;
   subtype Color_Type  is Colour_Type;

   function Light (Colour : Colour_Type) return Colour_Type;
   function Dark  (Colour : Colour_Type) return Colour_Type;

   Black        : constant Colour_Type := (  0,  0,  0);
   White        : constant Colour_Type := (255,255,255);
   Red          : constant Colour_Type := (255,  0,  0);
   Green        : constant Colour_Type := (  0,255,  0);
   Blue         : constant Colour_Type := (  0,  0,255);
   Gray         : constant Colour_Type := (128,128,128);
   Yellow       : constant Colour_Type := (255,255,  0);
   Cyan         : constant Colour_Type := (  0,255,255);
   Magenta      : constant Colour_Type := (255,  0,255);

   type Point_Type is record
      X,Y : Integer;
   end record;

   function "+" (P1,P2 : Point_Type) return Point_Type;
   function "-" (P1,P2 : Point_Type) return Point_Type;

   type    Point_List     is array (Positive range <>) of Point_Type;

   type Image_Type is private;

   function Image    (Canvas : Canvas_Type;
                      Name   : String)   -- filename in JPG, GIF, ... format
                     return Image_Type;


   -----------------------------------------------------------------------
   --               Canvas creation and properties
   --
   --  Canvas  (Width,             -- the specified width
   --           Height,            -- the specified height
   --           Title)             -- the title of the window
   --
   --  Set_Colour     (Canvas,     -- set the pen colour for a canvas
   --                  Colour)
   --
   --  Set_Pen        (Canvas,     -- set the pen used to draw lines on the
   --                  Colour,     -- canvas to this colour (default: black)
   --                  Width)      -- and thickness (default: 1 pixel).
   --
   --  Set_Fill       (Canvas,     -- set the colour used to fill subsequent
   --                  Colour)     -- closed shapes drawn on the canvas.
   --  Set_Fill       (Canvas,     -- set or clear the colour used to fill
   --                  Filled)     -- subsequent closed shapes
   --
   --  Erase          (Canvas)     -- erase the drawing in a canvas
   --
   --  Set_FontSize   (Canvas,     -- set a new font size for all subsequent
   --                  Size)       --  text drawn on the canvas.
   -----------------------------------------------------------------------

   function Canvas (Width    : Integer;
                    Height   : Integer;
                    Title    : String:="Graphics Window")
                   return Canvas_Type;

   procedure Set_Colour (Canvas : in out Canvas_Type;
                         Colour : in Colour_Type := Black);

   procedure Set_Color(Canvas : in out Canvas_Type;
                       Color  : in Color_Type := Black) renames Set_Colour;

   procedure Erase      (Canvas   : in Canvas_Type);

   procedure Set_Font_Size
     (Canvas   : in out Canvas_Type;
      Size     : in Integer);

   procedure Set_Pen    (Canvas   : in out Canvas_Type;
                         Colour   : in Colour_Type := Black;
                         Width    : in Natural     := 1);

   procedure Set_Fill   (Canvas   : in out Canvas_Type;
                         Colour   : in Colour_Type);

   procedure Set_Fill   (Canvas   : in out Canvas_Type);
   -- resets the filled attribute to false

   procedure Set_Fill   (Canvas   : in out Canvas_Type;
                         Filled   : in Boolean);


   -----------------------------------------------------------------------
   --              Drawing operations
   --
   --  Draw_Text      (Canvas,     -- draw a text string on the canvas
   --                  From,       -- from this top-left point, where
   --                  Text)       -- this is the text to be drawn.
   --
   --  Draw_Point     (Canvas,     -- draw a point on the canvas
   --                  Point)      -- the point coordinates
   --
   --  Draw_Line      (Canvas,     -- draw a line on the canvas
   --                  From,       -- from this point
   --                  To)         -- to this one.
   --  Draw_Line_List (Canvas,     -- draw lines on the canvas connecting
   --                  Points)     -- the points in this list.
   --
   --  Draw_Rectangle (Canvas,     -- draw a rectangle on the canvas
   --                  From,       -- from this corner point
   --                  To)         -- to this diagonally-opposite point
   --  Draw_Rectangle (Canvas,     -- draw a rectangle on the canvas
   --                  From,       -- from this top-left point
   --                  Width,      -- with this width
   --                  Height)     -- and this height
   --
   --  Draw_Ellipse   (Canvas,     -- draw an ellipse on the canvas
   --                  From,       -- bounded by a rectangle from this point
   --                  To)         -- to this point.
   --  Draw_Ellipse   (Canvas,     -- draw an ellipse on the canvas
   --                  From,       -- bounded by a rectangle from this point
   --                  Width,      -- with this width.
   --                  Height)     -- and this height
   --  Draw_Circle    (Canvas,     -- draw a circle on the canvas
   --                  Centre,     -- with this centre point
   --                  Radius)     -- and this radius.
   --
   --  Draw_Arc       (Canvas,     -- draw an arc on the canvas;
   --                              -- it is part of an ellipse,
   --                  From,       -- bounded by a rectangle from this point,
   --                  Width,      -- with this width
   --                  Height,     -- and height;
   --                  Angle1,     -- the initial angle of the arc (in degrees)
   --                  Angle2)     -- the final angle of the arc (in degrees)
   --
   --  Draw_Polygon   (Canvas,     -- draw a polygon on the canvas
   --                  Points)     -- with vertices at these points.
   --
   --  Draw_Image     (Canvas,     -- draw an image on the canvas
   --                  From,       -- from this point
   --                  Image)      -- using this image object.
   --
   --  Draw           (Canvas)     -- draws the canvas and returns immediately
   --
   --  Wait           (Canvas)     -- draws the canvas and waits until the
   --                              -- OK button is pressed: then, the
   --                              -- Canvas is hid
   --
   --  Close          (Canvas)     -- destroys the canvas, which now becomes
   --                              -- unavailable
   ----------------------------------------------------------------------------

   procedure Draw_Point (Canvas   : in Canvas_Type;
                         Point    : in Point_Type);

   procedure Draw_Text  (Canvas   : in Canvas_Type;
                         From     : in Point_Type;
                         Text     : in String);

   procedure Draw_Line  (Canvas   : in Canvas_Type;
                         From     : in Point_Type;
                         To       : in Point_Type);

   procedure Draw_Line_List
     (Canvas   : in Canvas_Type;
      Points   : in Point_List);

   procedure Draw_Rectangle
     (Canvas   : in Canvas_Type;
      From     : in Point_Type;
      To       : in Point_Type);
   procedure Draw_Rectangle
     (Canvas   : in Canvas_Type;
      From     : in Point_Type;
      Width    : in Positive;
      Height   : in Positive);

   procedure Draw_Ellipse
     (Canvas   : in Canvas_Type;
      From     : in Point_Type;
      To       : in Point_Type);
   procedure Draw_Ellipse
     (Canvas   : in Canvas_Type;
      From     : in Point_Type;
      Width    : in Positive;
      Height   : in Positive);
   procedure Draw_Circle
     (Canvas   : in Canvas_Type;
      Centre   : in Point_Type;
      Radius   : in Positive);
   procedure Draw_Arc
     (Canvas   : in Canvas_Type;
      From     : in Point_Type;
      Width    : in Positive;
      Height   : in Positive;
      Angle1   : in Natural;  -- in degrees
      Angle2   : in Natural); -- in degrees

   procedure Draw_Polygon
     (Canvas   : in Canvas_Type;
      Points   : in Point_List);

   procedure Draw_Image (Canvas   : in Canvas_Type;
                         From     : in Point_Type;
                         Image    : in Image_Type);

   procedure Close
     (Canvas : in out Canvas_Type);

   procedure Draw
     (Canvas : in out Canvas_Type);

   procedure Wait
     (Canvas : in out Canvas_Type);

private

   type Canvas_Type_Record is record
      Win : Graphics_Window_Pkg.Graphics_Window_Access;
      Gdkw : Gdk.Window.Gdk_Window;
      Pxmap : Gdk.Gdk_Pixmap;
      GC   : Gdk.GC.Gdk_GC;
      Back_GC   : Gdk.GC.Gdk_GC;
      Font : Pango.Font.Pango_Font_Description;
      Filled : Boolean:=False;
   end record;

   type Canvas_Type is access Canvas_Type_Record;

   type Image_Type is record
      Pxmap : Gdk.Gdk_Pixmap;
   end record;

end Graphics_Windows;