define FOGR,0  rem  section dealing with graph as a whole
define FSGR,1
define FAGR,2
define TCGR,3
define LCGR,4
define LWGR,5
define PDGR,6
define PSGR,7
define BGGR,8
define PWGR,9
define PHGR,10
define BWGR,11
define DCOLGR,12
define GRGR,17
define ROWS,18
define COLUMNS,19
define TDGR,20
define GRAPHTYPE,21
define XLABEL,22
define YLABELS,23
define THICKNESSGR,29
define FRGR,30
define OLGR,31
define OTGR,32
define ORGR,33
define OBGR,34
define ILGR,35
define ITGR,36
define IRGR,37
define IBGR,38
define MTGR,39
define MSGR,40
define PIGR,41
define MAGR,42
define PSIZE,43
define FOA,0   rem  Section dealing with axis
define FSA,1
define FAA,2
define TCA,3
define LCA,4
define LWA,5
define STA,6
define LGA,7
define INA,8
define UPA,9
define LOA,10
define NSA,11
define AA,12
define BA,13
define WIDTHA,14
define HEIGHTA,15
define HOTXA,16
define HOTYA,17
define FORMATA,18
define LENGTHA,19
define PAXIS,20
define FOD,0    rem Section dealing with data set
define FSD,1
define FAD,2
define TCD,3
define LWD,4
define LCD,5
define PDD,6
define PSD,7
define FCD,8
define MTD,9
define MSD,10
define MCD,11
define FRD,12
define TDD,13
define PDATA, 14
define FOM,0    rem Section dealing with marker keys - text properties
define FSM,1
define FAM,2
define TCM,3
define LWM,4    rem line properties
define LCM,5
define PDM,6
define PSM,7
define MTM,8    rem marker properties- repeated 5 times
define MSM,9
define MCM,10
define LABELM,11
define XSM,28
define YSM,29
define XEM,30
define YEM,31
define NSM,32 
define MDATA,33
define FOC,0    rem Section dealing with colour keys - text properties
define FSC,1
define FAC,2
define TCC,3
define LWC,4    rem line properties
define LCC,5
define PDC,6
define PSC,7
define FCC,8    rem colour (repeated 16 times)
define XSC,20
define YSC,21
define XEC,22
define YEC,23
define CDATA,24
define LINES_ONLY,0
define POINTS_ONLY,1
define LINES_AND_POINTS,2
define VERTICAL_HISTOGRAM,3
define HORIZONTAL_HISTOGRAM,4
define BOXES,3
define STACKED_VERTICAL_HISTOGRAM,5
define STACKED_HORIZONTAL_HISTOGRAM,6
define PICTOGRAM,7
define PIE_CHART,8
define TWO_LINES,9
define BOX_AND_TAILS,10
define DISTRIBUTION,11
define GENERAL,12
define LINE_HISTO,13
define DRAG_ALLOWED, 0x1
define SELECT_ALLOWED, 0x2
define TEXT_EFFECTS, 0x4
define FILL_EFFECTS, 0x8
define LINE_EFFECTS, 0x10
define GRAPH_COLOUR_ALLOWED, 0x20
define THREE_DEE_ALLOWED, 0x40
define GRID_ALLOWED, 0x80
define EXPLODE_ALLOWED, 0x100
define COLOURS_ALLOWED, 0x200
define BAR_WIDTH_ALLOWED, 0x400
define POINT_DETAILS_ALLOWED, 0x800
define TICKS_ALLOWED, 0x1000
define DIMENSIONS_ALLOWED, 0x2000
define TOP_ALLOWED, 0x4000
define DELETE_ALLOWED, 0x8000
define ALL_SELECTED, 0x10000
define NONE_SELECTED, 0x20000
define REDRAW_NEEDED, 0x40000



rem : *******************************************************************
rem : General comments on legends

rem : A legend key serves to identify the various symbols used on a graph. 
rem : Legend keys are boxed rows of items, where each item consists of
rem : a symbol , a colon and a word.

rem : The symbol can be a short segment of line,  a marker or a 
rem : box of colour, depending on the
rem : type of graph.

rem : This module contains two groups of functions:

rem : 1.  For planning purposes, it is essential to know the width and 

rem : height of the legend box.  We use macro
rem :      size_of_legend

rem :      this macro takes four parameters:
rem :      *   The current window handle
rem :      *   The graph descriptor array   
rem :      *   The legend descriptor array          
rem :      *   The graph type - LINES_ONLY, POINTS_ONLY,LINES_AND_POINTS
rem :      *    or BOXES           
rem :      The legend descriptor array needs only the text parameters to 
rem :      be filled in

rem : 2.   Eventually the legend box must be drawn.  Again the function
rem :      provided is:
rem :       
rem :      draw_legend


rem :      this macros takes four parameters:
rem :      *   The current window handle
rem :      *   The graph descriptor array   
rem :      *   The legend descriptor array          
rem :      *   The graph type

rem : ******************************************************************
rem : size of legend.  This function calculates the size of the
rem : marker legend and leaves it in k(XEM,k(YEM) 
macro CRM_size_of_legend(w, p(), k(),type )
   local width , n, q,res,t,h,v
                        rem find width of widest legend
   width = 0
   for n = 0 to p(COLUMNS) -2
       res = CRM_finddims(w,k,p(YLABELS+n))
       t = CRM_extract(res,"x=")
       if t > width then width = t
   next n
                        rem :  + 3 * character height+20
   h =  width + 20  + 3 * k(FSM)*k(FAM)/100 
                        rem : If marker to be shown add its size; else 20
   if type = POINTS_ONLY or type = LINES_AND_POINTS then
       h = h + 2*k(MSM)
   else
       h=h+20
   endif        
                             
                        rem find vertical size. Take larger of text,marker
   v =  k(FSM)
   if  (type = POINTS_ONLY or type = LINES_AND_POINTS) and v < k(MSM) then 
      v = k(MSM)
   endif    
   v = 2*v* (p(COLUMNS)-1) + 10
   k(XEM) = h
   k(YEM)=v
   = 0
endmacro
     

rem : ***********************************************************
rem : draw_legend.  This macro draws the legend, starting
rem : at k(XSM) and k(YSM).
macro CRM_draw_legend(w, p(),k(),tag,type)
   local res,n, text_height, v, sign_offset,text,color
   local datapoints(1,1)
   on_error_exit
             rem draw enclosing box
   res = font(w,k(FOM),k(FSM),k(FSM)*k(FAM)/100)
   res = settag(tag,SELECT_ALLOWED+DRAG_ALLOWED+TEXT_EFFECTS+DELETE_ALLOWED+REDRAW_NEEDED)
   res = group(w)
   if k(NSM) = 1 then
      res = box(w,1,1,1,1,p(BGGR)):  rem dummy 
   else
       res = lineattributes(w,k(PSM),k(LWM),k(LCM),k(PDM))     
       res = box(w,k(XSM)-12,k(YSM),k(XEM),k(YEM),0xffffff00)
    
       text_height = 10 + k(YSM)
       v =  2*k(FSM)
       if  (type = POINTS_ONLY or type = LINES_AND_POINTS) and v < 2*k(MSM) then 
          v = 2*k(MSM)
       endif    
       
       for n = 0 to p(COLUMNS)-2
           
          datapoints(0,0) = k(XSM)+10
          datapoints(1,0) = text_height+k(FSM)/2
          if type = LINES_ONLY then
             res=lineattributes(w,1,2048,k(MCM+3*n),0)
             res=line(w,datapoints(0,0)-10,datapoints(1,0),40,0)
          endif   
          if type = POINTS_ONLY or type = LINES_AND_POINTS then
             res=lineattributes(w,p(LWGR),0,0,0)
             res = polymarker( w, datapoints,1,k(MTM+3*n),k(MSM+3*n),k(MCM+3*n))
          endif
          if type = LINE_HISTO or type = VERTICAL_HISTOGRAM  or type = HORIZONTAL_HISTOGRAM  or type = STACKED_VERTICAL_HISTOGRAM or type = STACKED_HORIZONTAL_HISTOGRAM then
             color = p(DCOLGR+n)
             res = lineattributes(w,1,p(LWGR),color,0)
             res = box (w,datapoints(0,0)-10,datapoints(1,0)-10, 2.2*k(FSM),1.7*k(FSM),color)
          endif    
          text = "  " cat p(YLABELS+n)
          res = string(w,k(XSM)+10+k(MSM), text_height,text,0,p(BGGR))
          
          text_height = text_height + v
       next n
   endif
   res = endgroup(w)
   =0
endmacro

        

