home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / grphinit.c < prev    next >
C/C++ Source or Header  |  1990-04-20  |  12KB  |  354 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    grphinit.c (Color Graph Initialize)
  6.  * Purpose:    Initialize or reset color bar and graph
  7.  * Subroutine:    init_colorbox()            returns: void
  8.  * Subroutine:    init_main_colorbar()        returns: void
  9.  * Subroutine:    adjust_main_colorbar()        returns: void
  10.  * Subroutine:    init_graph_colorbar()        returns: void
  11.  * Subroutine:    adjust_graph_colorbar()        returns: void
  12.  * Subroutine:    map_halftone_colorbar()        returns: void
  13.  * Subroutine:    adjust_color_graph()        returns: void
  14.  * Extern:    cgraph in CgraphCtrl.c (from Cgraph.def)
  15.  * Xlib calls:    XCreateSimpleWindow(), XSelectInput(), XMapSubwindows()
  16.  * Xlib calls:    XUnmapWindow(), XMapWindow()
  17.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  18.  *        You may do anything you like with this file except remove
  19.  *        this copyright.  The Smithsonian Astrophysical Observatory
  20.  *        makes no representations about the suitability of this
  21.  *        software for any purpose.  It is provided "as is" without
  22.  *        express or implied warranty.
  23.  * Modified:    {0} Michael VanHilst    initial version         11 June 1989
  24.  *        {1} Jay Travisano (STScI)  VMS,IMTOOL changes    10 Nov  1989
  25.  *        {n} <who> -- <does what> -- <when>
  26.  */
  27.  
  28. #include <stdio.h>        /* stderr, NULL, etc. */
  29. #include <X11/Xlib.h>        /* X window stuff */
  30. #include <X11/Xutil.h>        /* X window manager stuff */
  31. #include "hfiles/struct.h"    /* declare structure types */
  32. #include "hfiles/extern.h"    /* extern main parameter structures */
  33. #include "hfiles/cgraph.h"
  34.  
  35. extern struct cgraphRec cgraph;
  36. extern struct colbarRec colorbar;
  37. /* flag to set color graph background black, else white */
  38. int black_graph_background = 0;
  39.  
  40. #if VMS && IMTOOL
  41. extern void XZ_ast();
  42. extern int  XZ_efn;
  43. #endif
  44.  
  45. /*
  46.  * Subroutine:    init_colorbox
  47.  * Purpose:    Handle program init-time color bar and graph initialization
  48.  */
  49. void init_colorbox ( )
  50. {
  51.   static void init_main_colorbar();
  52.  
  53.   cgraph.ncolors = color.ncolors;
  54.   cgraph.red.table = &color.ctable.red;
  55.   cgraph.green.table = &color.ctable.green;
  56.   cgraph.blue.table = &color.ctable.blue;
  57.   cgraph.red.draw = &color.gcset.red;
  58.   cgraph.green.draw = &color.gcset.green;
  59.   cgraph.blue.draw = &color.gcset.blue;
  60.   cgraph.queue[cgraph.red.queue_index] = &cgraph.red;
  61.   cgraph.queue[cgraph.green.queue_index] = &cgraph.green;
  62.   cgraph.queue[cgraph.blue.queue_index] = &cgraph.blue;
  63.   init_main_colorbar();
  64. }
  65.  
  66. /*
  67.  * Subroutine:    init_main_colorbar
  68.  * Purpose:    Initialize color bar and related params
  69.  * Xlib calls:    XCreateSimpleWindow(), XMapSubwindows()
  70.  */
  71. static void init_main_colorbar ( )
  72. {
  73.   char *calloc_errchk();
  74.   static void set_colorbar_image(), set_colorbar_params();
  75.  
  76.   colorbar.display = colorbox.display;
  77.   colorbar.ref_width = colorbox.width;
  78.   colorbar.ref_height = colorbox.height;
  79.   colorbar.image = &colorbox.image;
  80.   set_colorbar_params(&colorbar, colorbox.xwidth, colorbox.yheight);
  81.   colorbar.ID =
  82.    XCreateSimpleWindow(colorbar.display, colorbox.ID,
  83.                colorbox.xzero - BDRWDTH, colorbox.yzero - BDRWDTH,
  84.                colorbar.width, colorbar.height, BDRWDTH,
  85.                color.gcset.menu.foreground,
  86.                color.gcset.menu.background);
  87.   XMapWindow(colorbox.display, colorbar.ID);
  88.   set_colorbar_image(&colorbar, 0);
  89. }
  90.  
  91. /*  
  92.  * Subroutine:    adjust_main_colorbar
  93.  * Purpose:    Adjust color bar size params and/or color data if needed
  94.  */
  95. void adjust_main_colorbar ( )
  96. {
  97.   char *calloc_errchk();
  98.   static void set_colorbar_image(), set_colorbar_params();
  99.  
  100.   if( (colorbox.width != colorbar.ref_width) ||
  101.       (colorbox.height != colorbar.ref_height) ) {
  102.     set_colorbar_params(&colorbar, colorbox.xwidth, colorbox.yheight);
  103.     /* set flag in case somebody wants to coordinate */
  104.     XResizeWindow(colorbar.display, colorbar.ID, colorbar.width,
  105.           colorbar.height);
  106.     set_colorbar_image(&colorbar, 0);
  107.   } else if( color.ncolors != colorbar.ncolors )
  108.     set_colorbar_image(&colorbar, 0);
  109. }
  110.  
  111. /*
  112.  * Subroutine:    init_graph_colorbar
  113.  * Purpose:    Initialize color bar and related params
  114.  * Xlib calls:    XMapSubwindows()
  115.  */
  116. void init_graph_colorbar ( )
  117. {
  118.   Window create_cgraph_box();
  119.   char *calloc_errchk();
  120.   static void set_colorbar_image(), set_colorbar_params();
  121.  
  122.   cgraph.bar.display = graphbox.display;
  123.   cgraph.bar.ref_width = graphbox.width;
  124.   cgraph.bar.ref_height = graphbox.height;
  125.   cgraph.bar.image = &graphbox.image;
  126.   if( cgraph.vertical ) {
  127.     set_colorbar_params(&cgraph.bar,
  128.             cgraph.barlabel.width - 4, cgraph.graph.height);
  129.     cgraph.bar.ID =
  130.       create_cgraph_box(1, graphbox.yzero - 1,
  131.             cgraph.bar.width, cgraph.bar.height,
  132.             graphbox.display, graphbox.ID,
  133.             color.hard.true_black, NorthWestGravity);
  134.   } else {
  135.     set_colorbar_params(&cgraph.bar,
  136.             cgraph.graph.width, cgraph.barlabel.height - 4);
  137.     cgraph.bar.ID =
  138.       create_cgraph_box(graphbox.xzero - 1,
  139.             graphbox.height - cgraph.barlabel.height,
  140.             cgraph.bar.width, cgraph.bar.height,
  141.             graphbox.display, graphbox.ID,
  142.             color.hard.true_black, SouthWestGravity);
  143.   }
  144.   XMapWindow(graphbox.display, cgraph.bar.ID);
  145.   set_colorbar_image(&cgraph.bar, cgraph.vertical);
  146. }
  147.  
  148. /*  
  149.  * Subroutine:    adjust_graph_colorbar
  150.  * Purpose:    Adjust color bar size params and/or color data if needed
  151.  */
  152. void adjust_graph_colorbar ( )
  153. {
  154.   int resize = 0;
  155.   char *calloc_errchk();
  156.   static void set_colorbar_image(), set_colorbar_params();
  157.  
  158.   if( cgraph.vertical ) {
  159.     if( graphbox.height != cgraph.bar.ref_height ) {
  160.       set_colorbar_params(&cgraph.bar,
  161.               cgraph.barlabel.width - 5, cgraph.graph.height);
  162.       resize = 1;
  163.     }
  164.   } else {
  165.     if( graphbox.width != cgraph.bar.ref_width ) {
  166.       set_colorbar_params(&cgraph.bar,
  167.               cgraph.graph.width, cgraph.barlabel.height - 3);
  168.       resize = 1;
  169.     }
  170.   }
  171.   if( resize )
  172.     XResizeWindow(cgraph.bar.display, cgraph.bar.ID, cgraph.bar.width,
  173.           cgraph.bar.height);
  174.   /* if size was or the number of colors were changed */
  175.   if( resize || (color.ncolors != cgraph.bar.ncolors) )
  176.     set_colorbar_image(&cgraph.bar, cgraph.vertical);
  177. }
  178.  
  179. /*
  180.  * Subroutine:    map_halftone_colorbar
  181.  * Purpose:    Create and draw the color bar
  182.  */
  183. void map_halftone_colorbar ( disp, graph )
  184.      int disp;
  185.      int graph;    /* graphbox colorbar, else colorbox colorbar */
  186. {
  187.   struct colbarRec *bar;
  188.   void make_halftone_colorbar(), draw_colorbar();
  189.  
  190.   /* distinguish between two possible colorbars */
  191.   if( graph )
  192.     bar = &cgraph.bar;
  193.   else
  194.     bar = &colorbar;
  195.   make_halftone_colorbar((unsigned char *)bar->byte_data,
  196.              (unsigned char *)bar->bit_data,
  197.              (int)bar->width, (int)bar->height,
  198.              bar->bytes_per_bit_line);
  199.   if( disp )
  200.     draw_colorbar(graph);
  201. }
  202.  
  203. static void set_colorbar_params ( bar, width, height )
  204.      struct colbarRec *bar;
  205.      int width, height;
  206. {
  207.   int data_sz;
  208.   char *calloc_errchk();
  209.  
  210.   bar->width = width;
  211.   bar->height = height;
  212.   bar->bytes_per_bit_line = (bar->width + 7) / 8;
  213.   bar->image->width = bar->width;
  214.   bar->image->height = bar->height;
  215.   data_sz = bar->width * (bar->height + bar->bytes_per_bit_line);
  216.   if( data_sz > bar->data_size ) {
  217.     if( bar->byte_data != NULL )
  218.       free(bar->byte_data);
  219.     bar->byte_data = calloc_errchk(data_sz, sizeof(char), "color bar");
  220.     bar->bit_data = &bar->byte_data[bar->width * bar->height];
  221.     bar->data_size = data_sz;
  222.   }
  223. }
  224.  
  225. /*
  226.  * Subroutine:    set_colorbar_image
  227.  * Purpose:    Set the color bar for display of current color set
  228.  */
  229. static void set_colorbar_image ( bar, vertical )
  230.      struct colbarRec *bar;
  231.      int vertical;
  232. {
  233.   void fill_colorbar(), make_halftone_colorbar();
  234.  
  235.   cgraph.disp = &(color.gcset.disp);
  236.   cgraph.menu = &(color.gcset.menu);
  237.   bar->ncolors = color.ncolors;
  238.   if( color.ncolors > 1 ) {
  239.     fill_colorbar((unsigned char *)bar->byte_data,
  240.           (int)bar->width, (int)bar->height, 0, bar->ncolors - 1,
  241.           vertical, vertical, color.pixvalmap);
  242.     bar->image->data = bar->byte_data;
  243.     bar->image->bytes_per_line = bar->width;
  244.     bar->image->format = ZPixmap;
  245.     bar->image->depth = color.screen_depth;
  246.   } else {
  247.     fill_colorbar((unsigned char *)bar->byte_data,
  248.           (int)bar->width, (int)bar->height, 0, 255,
  249.           vertical, vertical, color.pixvalmap);
  250.     make_halftone_colorbar((unsigned char *)bar->byte_data,
  251.                (unsigned char *)bar->bit_data,
  252.                (int)bar->width, (int)bar->height,
  253.                bar->bytes_per_bit_line);
  254.     bar->image->data = bar->bit_data;
  255.     bar->image->bytes_per_line = bar->bytes_per_bit_line;
  256.     bar->image->format = XYBitmap;
  257.     bar->image->depth = 1;
  258.   }
  259. }
  260.  
  261. /*
  262.  * Subroutine:    adjust_color_graph
  263.  * Purpose:    Adjust color graph size params and/or graph if needed
  264.  * Xlib calls:    XCreateSimpleWindow(), XSelectInput(), XMapSubwindows()
  265.  */
  266. void adjust_color_graph ( )
  267. {
  268.   int resize, bkgd, graph_x;
  269.   void init_cgraph_lines(), init_cgraph_hash();
  270.  
  271.   if( (cgraph.graph.ref_width != graphbox.width) ||
  272.       (cgraph.graph.ref_height != graphbox.height) ) {
  273.     cgraph.graph.ref_width = graphbox.width;
  274.     cgraph.graph.ref_height = graphbox.height;
  275.     if( cgraph.vertical ) {
  276.       cgraph.graph.width = graphbox.xwidth - (cgraph.barlabel.width + 1);
  277.       cgraph.graph.height = graphbox.yheight;
  278.       graph_x = cgraph.barlabel.width;
  279.     } else {
  280.       cgraph.graph.width = graphbox.xwidth;
  281.       cgraph.graph.height = graphbox.yheight - cgraph.barlabel.height;
  282.       graph_x = graphbox.xzero - BDRWDTH;
  283.     }
  284.     /* server call placed as soon as possible to reduce delay */
  285.     if( cgraph.graph.ID == NULL ) {
  286.       cgraph.graph.display = graphbox.display;
  287.       /* black used to show where cgraph lines overlap (white if black bkgd) */
  288.       if( black_graph_background ) {
  289.     bkgd = color.hard.true_black;
  290.     cgraph.black = &(color.gcset.white);
  291.       } else {
  292.     bkgd = color.hard.std_white;
  293.     cgraph.black = &(color.gcset.black);
  294.       }
  295.       /* graph window takes defaults NorthWestGravity, and spectial events */
  296.       cgraph.graph.ID =
  297.     XCreateSimpleWindow(graphbox.display, graphbox.ID,
  298.                 graph_x, graphbox.yzero - BDRWDTH,
  299.                 cgraph.graph.width, cgraph.graph.height, BDRWDTH,
  300.                 color.hard.true_black, bkgd);
  301. #if VMS && IMTOOL
  302.       XSelectAsyncInput (cgraph.graph.display, cgraph.graph.ID,
  303.             ButtonPressMask | ButtonMotionMask | ButtonReleaseMask,
  304.             XZ_ast, XZ_efn);
  305. #endif
  306.       XSelectInput(cgraph.graph.display, cgraph.graph.ID,
  307.            ButtonPressMask | ButtonMotionMask | ButtonReleaseMask);
  308.       XMapSubwindows(graphbox.display, graphbox.ID);
  309.     } else {
  310.       XResizeWindow(cgraph.graph.display, cgraph.graph.ID,
  311.             cgraph.graph.width, cgraph.graph.height);
  312.     }
  313.     /* offset "min" from bottom */
  314.     cgraph.graphlabel.min_y = graphbox.height - cgraph.graphlabel.minmax_yoff;
  315.     if( cgraph.vertical ) {
  316.       cgraph.graph.xzero = INDENT;
  317.       cgraph.graph.xwidth = cgraph.graph.width - (INDENT + INDENT);
  318.       cgraph.graph.yzero = 0;
  319.       cgraph.graph.yheight = cgraph.graph.height;
  320.       cgraph.graphlabel.max_x = graphbox.width - cgraph.graphlabel.minmax_xoff;
  321.       cgraph.graphlabel.max_y = cgraph.graphlabel.min_y;
  322.     } else {
  323.       cgraph.graph.xzero = 0;
  324.       cgraph.graph.xwidth = cgraph.graph.width;
  325.       cgraph.graph.yzero = INDENT;
  326.       cgraph.graph.yheight = cgraph.graph.height - (INDENT + INDENT);
  327.     }
  328.     cgraph.graph.Xwidth = (double)cgraph.graph.xwidth;
  329.     cgraph.graph.Yheight = (double)cgraph.graph.yheight;
  330.     cgraph.graph.xmax = cgraph.graph.xzero + cgraph.graph.xwidth;
  331.     cgraph.graph.ymax = cgraph.graph.yzero + cgraph.graph.yheight;
  332.     resize = 1;
  333.   } else
  334.     resize = 0;
  335.   if( resize || (cgraph.graph.ncolors != color.ncolors) ) {
  336.     cgraph.graph.ncolors = color.ncolors;
  337.     cgraph.point_cnt = cgraph.graph.ncolors;
  338.     cgraph.graph.Xinc = cgraph.graph.Xwidth/(double)cgraph.graph.ncolors;
  339.     cgraph.graph.Yinc = cgraph.graph.Yheight/(double)cgraph.graph.ncolors;
  340.     init_cgraph_lines(cgraph.red.line, cgraph.green.line, cgraph.blue.line);
  341.     init_cgraph_hash();
  342.   }
  343. }
  344.                                   
  345.  
  346.                                                                
  347.                                                                
  348.                                                                
  349.                                                                
  350.                                                                
  351.                                                                
  352.                                                                
  353.                                            
  354.