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

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    grphlbl.c (Color Graph Label)
  6.  * Purpose:    Initialize or reset color bar and graph
  7.  * Subroutine:    label_colorbar()        returns: void
  8.  * Subroutine:    label_color_graph()        returns: void
  9.  * Subroutine:    label_gamma()            returns: void
  10.  * Subroutine:    init_graph_text()        returns: void
  11.  * Extern:    normform, minform in WndwDesktop.c
  12.  * Extern:    cgraph in CgraphCtrl.c (from Cgraph.def)
  13.  * Xlib call:    XDrawImageString(), XTextExtents(), XTextWidth()
  14.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  15.  *        You may do anything you like with this file except remove
  16.  *        this copyright.  The Smithsonian Astrophysical Observatory
  17.  *        makes no representations about the suitability of this
  18.  *        software for any purpose.  It is provided "as is" without
  19.  *        express or implied warranty.
  20.  * Modified:    {0} Michael VanHilst    initial version         22 June 1989
  21.  *        {n} <who> -- <does what> -- <when>
  22.  */
  23.  
  24. #include <stdio.h>        /* stderr, NULL, etc. */
  25. #include <X11/Xlib.h>        /* X window stuff */
  26. #include <X11/Xutil.h>        /* X window manager stuff */
  27. #include "hfiles/define.h"    /* define MIN, etc. */
  28. #include "hfiles/struct.h"    /* declare structure types */
  29. #include "hfiles/extern.h"    /* extern main parameter structures */
  30. #include "hfiles/cgraph.h"
  31.  
  32. extern struct cgraphRec cgraph;
  33.  
  34. /*
  35.  * Subroutine:    label_colorbar
  36.  * Purpose:    Label the maximum color level by that end of the color bar
  37.  * Xlib call:    XDrawImageString()
  38.  */
  39. void label_colorbar ( )
  40. {
  41.   char max[4];
  42.   GC gc, set_edit_gc();
  43.  
  44.   if( cgraph.graphlabel.active ) {
  45.     gc = set_edit_gc(cgraph.font, cgraph.menu->foreground,
  46.              cgraph.menu->background);
  47.     XDrawImageString(cgraph.bar.display, cgraph.barlabel.min_ID, gc,
  48.              cgraph.barlabel.min_x, cgraph.barlabel.min_y, "0", 1);
  49.     if( color.ncolors > 1 )
  50.       sprintf(max, "%3d", color.ncolors - 1);
  51.     else
  52.       sprintf(max, "255");
  53.     XDrawImageString(cgraph.bar.display, cgraph.barlabel.max_ID, gc,
  54.              cgraph.barlabel.max_x, cgraph.barlabel.max_y, max, 3);
  55.   }
  56. }
  57.  
  58. /*
  59.  * Subroutine:    label_color_graph
  60.  * Purpose:    Draw entire graph label
  61.  */
  62. void label_color_graph ( )
  63. {
  64.   GC gc, set_edit_gc();
  65.   void label_gamma();
  66.  
  67.   if( cgraph.graphlabel.active && graphbox.active ) {
  68.     gc = set_edit_gc(cgraph.font, cgraph.menu->foreground,
  69.              cgraph.menu->background);
  70.     if( cgraph.graphlabel.geq_active )
  71.       XDrawImageString(cgraph.graph.display, cgraph.graphlabel.geq_ID, gc,
  72.                cgraph.graphlabel.geq_text_x,
  73.                cgraph.graphlabel.geq_text_y, "g=", 2);
  74.     XDrawImageString(cgraph.graph.display, cgraph.graphlabel.minmax_ID, gc,
  75.              cgraph.graphlabel.min_x, cgraph.graphlabel.min_y,
  76.              "min", 3);
  77.     XDrawImageString(cgraph.graph.display, cgraph.graphlabel.minmax_ID, gc,
  78.              cgraph.graphlabel.max_x, cgraph.graphlabel.max_y,
  79.              "max", 3);
  80.     label_gamma(1, 1, 1);
  81.   }
  82. }
  83.  
  84. /*
  85.  * Subroutine:    label_gamma
  86.  * Purpose:    Label color gamma levels
  87.  * Xlib call:    XDrawImageString()
  88.  */
  89. void label_gamma ( red, green, blue )
  90.      int red, green, blue;
  91. {
  92.   double gamma;
  93.   char string[8];
  94.   GC gc, set_edit_gc();
  95.  
  96.   if( cgraph.graphlabel.active && graphbox.active ) {
  97.     gc = set_edit_gc(cgraph.font, cgraph.menu->foreground,
  98.              cgraph.menu->background);
  99.     if( red ) {
  100.       gamma = MIN(color.ctable.red.gamma, 9.99);
  101.       sprintf(string, "%3.2f", gamma);
  102.       XDrawImageString(cgraph.graph.display, cgraph.graphlabel.red_ID, gc,
  103.                cgraph.graphlabel.box_x, cgraph.graphlabel.box_y,
  104.                string, 4);
  105.     }
  106.     if( green ) {
  107.       if( color.ctable.green.gamma != gamma ) {
  108.     gamma = MIN(color.ctable.green.gamma, 9.99);
  109.     sprintf(string, "%3.2f", gamma);
  110.       }
  111.       XDrawImageString(cgraph.graph.display, cgraph.graphlabel.green_ID, gc,
  112.                cgraph.graphlabel.box_x, cgraph.graphlabel.box_y,
  113.                string, 4);
  114.     }
  115.     if( blue ) {
  116.       if( color.ctable.blue.gamma != gamma ) {
  117.     gamma = MIN(color.ctable.blue.gamma, 9.99);
  118.     sprintf(string, "%3.2f", gamma);
  119.       }
  120.       XDrawImageString(cgraph.graph.display, cgraph.graphlabel.blue_ID, gc,
  121.                cgraph.graphlabel.box_x, cgraph.graphlabel.box_y,
  122.                string, 4);
  123.     }
  124.   }
  125. }
  126.  
  127. /*
  128.  * Subroutine:    init_cgraph_text
  129.  * Purpose:    Set basic text parameters for color bar and graph labels
  130.  * Xlib calls:    XTextExtents(), XTextWidth()
  131.  * Note:    To be called before the color and graph windows are created
  132.  */
  133. void init_cgraph_text ( )
  134. {
  135.   int dir, ascent, descent;    /* l: character image bitmap extents */
  136.   XCharStruct charstruct;    /* l: character bitmap pattern extents */
  137.   XFontStruct *get_fontstruct();
  138.  
  139.   /* determine if colorbar will be vertical or horizontal */
  140.   if( graphbox.hints.height > graphbox.hints.width )
  141.     cgraph.vertical = 1;
  142.   else
  143.     cgraph.vertical = 0;
  144.   /* get measurements of string using mini (0) font (check for failure) */
  145.   if( (cgraph.fontstruct = get_fontstruct(0)) == NULL ) {
  146.     cgraph.graphlabel.active = 0;
  147.     return;
  148.   }
  149.   cgraph.graphlabel.active = 1;
  150.   cgraph.font = cgraph.fontstruct->fid;
  151.   XTextExtents(cgraph.fontstruct, "2.00", 4,
  152.            &dir, &ascent, &descent, &charstruct);
  153.   /* define a box which nicely encloses text bitmap for "2.00" */
  154.   cgraph.graphlabel.box_x = descent - (charstruct.lbearing);
  155.   cgraph.graphlabel.box_width =
  156.     descent + descent + (charstruct.rbearing - charstruct.lbearing);
  157.   if( charstruct.descent > 1 )
  158.     /* character's y position is defined high (above baseline) */
  159.     cgraph.graphlabel.box_y = descent + charstruct.ascent - 1;
  160.   else
  161.     cgraph.graphlabel.box_y = descent + charstruct.ascent;
  162.   cgraph.graphlabel.box_height = cgraph.graphlabel.box_y + descent;
  163.   cgraph.graphlabel.geq_height = cgraph.graphlabel.box_height;
  164.   if( cgraph.vertical ) {
  165.     /* all boxes leave one pixel between border and top and bottom of window */
  166.     cgraph.graphlabel.geq_y = 1;
  167.     cgraph.graphlabel.red_y = 1;
  168.     cgraph.graphlabel.green_y = 1;
  169.     cgraph.graphlabel.blue_y = 1;
  170.     cgraph.graphlabel.height = cgraph.graphlabel.box_height + 4;
  171.     /* barlabel area has same height, minimum width */
  172.     cgraph.barlabel.height = cgraph.graphlabel.height;
  173.     cgraph.barlabel.base_width = 2 + cgraph.graphlabel.box_width -
  174.       XTextWidth(cgraph.fontstruct, ".", 1);
  175.     cgraph.barlabel.width = cgraph.barlabel.base_width;
  176.     cgraph.barlabel.max_y = cgraph.graphlabel.box_y + 2;
  177.     cgraph.barlabel.min_y = cgraph.barlabel.max_y;
  178.     /* determine placement of "g=" */
  179.     cgraph.graphlabel.geq_width = XTextWidth(cgraph.fontstruct, "g=", 2) + 5;
  180.     cgraph.graphlabel.geq_text_x = 3;
  181.     cgraph.graphlabel.geq_text_y = cgraph.graphlabel.box_y;
  182.     /* limits are in x dimension (for labels and the color bar) */
  183.     cgraph.graphlabel.three_limit = (3 * cgraph.graphlabel.box_width) + 9 +
  184.       cgraph.barlabel.width;
  185.     cgraph.graphlabel.four_limit =
  186.       cgraph.graphlabel.three_limit + cgraph.graphlabel.geq_width + 4;
  187.     /* "min" and "max" y is offset from bottom, "max" is offset from right */
  188.     cgraph.graphlabel.minmax_xoff = XTextWidth(cgraph.fontstruct,"max", 3) + 3;
  189.     cgraph.graphlabel.minmax_yoff =
  190.       cgraph.graphlabel.height - (cgraph.graphlabel.box_y + 1);
  191.     cgraph.graphlabel.min_x = 4 - charstruct.lbearing + cgraph.barlabel.width;
  192.     /* max_x depends on the graphbox width and must be set later */
  193.     /* set window parameters tied to the text size */
  194.     graphbox.yzero = cgraph.graphlabel.height + BDRWDTH;
  195.     /* set recommended graphbox sizes in structure used do determine size */
  196.     graphbox.hints.min_width = cgraph.graphlabel.three_limit;
  197.     graphbox.hints.width = cgraph.graphlabel.four_limit;
  198.   } else {
  199.     /* all boxes leave one pixel between border and sides of subwindow */
  200.     cgraph.graphlabel.geq_x = 1;
  201.     cgraph.graphlabel.red_x = 1;
  202.     cgraph.graphlabel.green_x = 1;
  203.     cgraph.graphlabel.blue_x = 1;
  204.     cgraph.graphlabel.width = cgraph.graphlabel.box_width + 4;
  205.     /* barlabel area has same width, minimum height */
  206.     cgraph.barlabel.width = cgraph.graphlabel.width;
  207.     cgraph.barlabel.height = cgraph.graphlabel.box_height;
  208.     /* determine placement of "g=" */
  209.     cgraph.graphlabel.geq_width = cgraph.graphlabel.box_width;
  210.     cgraph.graphlabel.geq_text_x =
  211.       (cgraph.graphlabel.box_width -
  212.        XTextWidth(cgraph.fontstruct, "g=", 2)) / 2;
  213.     cgraph.graphlabel.geq_text_y = cgraph.graphlabel.box_y;
  214.     /* limits are in y dimension (enough for labels and the color bar) */
  215.     cgraph.graphlabel.three_limit = (3 * cgraph.graphlabel.box_height) + 10 +
  216.       cgraph.barlabel.height;
  217.     cgraph.graphlabel.four_limit = (4 * cgraph.graphlabel.box_height) + 12 +
  218.       cgraph.barlabel.height;
  219.     /* "min" and "max" x is offset from right, min y is offset from bottom */
  220.     cgraph.graphlabel.minmax_xoff =
  221.       XTextWidth(cgraph.fontstruct, "max", 3) + 3;
  222.     cgraph.graphlabel.minmax_yoff = descent + 3 + cgraph.barlabel.height;
  223.     cgraph.graphlabel.max_y = ascent + 2;
  224.     /* graphlabel.min_y is dependent on height and must be set later */
  225.     /* width is fixed so x's can be determined now */
  226.     cgraph.graphlabel.min_x =
  227.       cgraph.graphlabel.width - cgraph.graphlabel.minmax_xoff;
  228.     cgraph.graphlabel.max_x = cgraph.graphlabel.min_x;
  229.     /* 0 and ncolors-1 are placed near side toward colorbar */
  230.     cgraph.barlabel.max_x = 4 + charstruct.lbearing;
  231.     cgraph.barlabel.min_x =
  232.       cgraph.barlabel.width - (XTextWidth(cgraph.fontstruct, "0", 1) + 4);
  233.     /* set window parameters tied to the text size */
  234.     graphbox.xzero = cgraph.graphlabel.width + BDRWDTH;
  235.     graphbox.hints.min_height = cgraph.graphlabel.three_limit;
  236.     graphbox.hints.height = cgraph.graphlabel.four_limit;
  237.   }
  238. }
  239.