home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2302 / StrInRect.c < prev   
Encoding:
C/C++ Source or Header  |  1990-12-28  |  6.7 KB  |  203 lines

  1. /*
  2.  * Author: Jason Baietto, jason@ssd.csd.harris.com
  3.  * xdiary Copyright 1990 Harris Corporation
  4.  *
  5.  * Permission to use, copy, modify, and distribute, this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation, and that the name of the copyright holder be used in
  10.  * advertising or publicity pertaining to distribution of the software with
  11.  * specific, written prior permission, and that no fee is charged for further
  12.  * distribution of this software, or any modifications thereof.  The copyright
  13.  * holder makes no representations about the suitability of this software for
  14.  * any purpose.  It is provided "as is" without express or implied warranty.
  15.  *
  16.  * THE COPYRIGHT HOLDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  17.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND IN NO
  18.  * EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  19.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM ITS USE,
  20.  * LOSS OF DATA, PROFITS, QPA OR GPA, WHETHER IN AN ACTION OF CONTRACT,
  21.  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
  22.  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <X11/StringDefs.h>
  27. #include <X11/IntrinsicP.h>
  28. #include "StrInRectP.h"
  29.  
  30. /*==========================================================================*/
  31. /*                        DrawStringInRect Routines:                        */
  32. /*==========================================================================*/
  33. void DrawStringInRect(display,
  34.                       drawable,
  35.                       gc,
  36.                       font,
  37.                       string,
  38.                       length,
  39.                       rectangle,
  40.                       gravity,
  41.                       xpad,
  42.                       ypad)
  43. Display      * display;
  44. Drawable       drawable;
  45. GC             gc;
  46. XFontStruct  * font;
  47. char         * string;
  48. int            length;       
  49. XRectangle   * rectangle;
  50. XtGravity      gravity;
  51. int            xpad;
  52. int            ypad;
  53. {
  54.  
  55.    int ascent        = font->ascent;
  56.    int descent       = font->descent;
  57.    int string_width  = XTextWidth(font, string, length);
  58.    int string_height = ascent + descent;
  59.    int x,y;
  60.  
  61.    switch (gravity) {
  62.       case NorthWest:
  63.          x = rectangle->x + xpad;
  64.          y = rectangle->y + ascent + ypad;
  65.          break;
  66.       case North:
  67.          x = rectangle->x + (rectangle->width - string_width)/2;
  68.          y = rectangle->y + ascent + ypad;
  69.          break;
  70.       case NorthEast:
  71.          x = (rectangle->x + rectangle->width - 1) - string_width - xpad;
  72.          y = rectangle->y + ascent + ypad;
  73.          break;
  74.       case West:
  75.          x = rectangle->x + xpad;
  76.          y = rectangle->y + (rectangle->height - string_height)/2 + ascent;
  77.          break;
  78.       case Center:
  79.          x = rectangle->x + (rectangle->width - string_width)/2;
  80.          y = rectangle->y + (rectangle->height - string_height)/2 + ascent;
  81.          break;
  82.       case East:
  83.          x = (rectangle->x + rectangle->width - 1) - string_width - xpad;
  84.          y = rectangle->y + (rectangle->height - string_height)/2 + ascent;
  85.          break;
  86.       case SouthWest:
  87.          x = rectangle->x + xpad;
  88.          y = (rectangle->y + rectangle->height - 1) - descent - 1 - ypad;
  89.          break;
  90.       case South:
  91.          x = rectangle->x + (rectangle->width - string_width)/2;
  92.          y = (rectangle->y + rectangle->height - 1) - descent - 1 - ypad;
  93.          break;
  94.       case SouthEast:
  95.          x = (rectangle->x + rectangle->width - 1) - string_width - xpad;
  96.          y = (rectangle->y + rectangle->height - 1) - descent - 1 - ypad;
  97.          break;
  98.       default:
  99.          fprintf(stderr, "DrawStringInRect: unknown gravity\n");
  100.          break;
  101.    }
  102.  
  103.    XDrawString(
  104.       display,
  105.       drawable,
  106.       gc,
  107.       x,y,
  108.       string,
  109.       length
  110.    );
  111. }
  112.  
  113.  
  114.  
  115.  
  116. void DrawStringsInRects(display,
  117.                         drawable,
  118.                         gc,
  119.                         font,
  120.                         strings,
  121.                         lengths,
  122.                         rectangles,
  123.                         count,
  124.                         gravity,
  125.                         xpad,
  126.                         ypad)
  127. Display      * display;
  128. Drawable       drawable;
  129. GC             gc;
  130. XFontStruct  * font;
  131. StringTable    strings;
  132. int          * lengths;
  133. XRectangle   * rectangles;
  134. int            count;
  135. XtGravity      gravity;
  136. int            xpad;
  137. int            ypad;
  138. {
  139.  
  140.    int ascent        = font->ascent;
  141.    int descent       = font->descent;
  142.    int string_height = ascent + descent;
  143.    int string_width;
  144.    int x,y;
  145.    int i;
  146.  
  147.    for (i=0; i < count; i++) {
  148.  
  149.       string_width  = XTextWidth(font, strings[i], lengths[i]);
  150.  
  151.       switch (gravity) {
  152.          case NorthWest:
  153.             x = rectangles[i].x + xpad;
  154.             y = rectangles[i].y + ascent + ypad;
  155.             break;
  156.          case North:
  157.             x = rectangles[i].x + (rectangles[i].width - string_width)/2;
  158.             y = rectangles[i].y + ascent + ypad;
  159.             break;
  160.          case NorthEast:
  161.             x = (rectangles[i].x + rectangles[i].width - 1) - string_width - xpad;
  162.             y = rectangles[i].y + ascent + ypad;
  163.             break;
  164.          case West:
  165.             x = rectangles[i].x + xpad;
  166.             y = rectangles[i].y + (rectangles[i].height - string_height)/2 + ascent;
  167.             break;
  168.          case Center:
  169.             x = rectangles[i].x + (rectangles[i].width - string_width)/2;
  170.             y = rectangles[i].y + (rectangles[i].height - string_height)/2 + ascent;
  171.             break;
  172.          case East:
  173.             x = (rectangles[i].x + rectangles[i].width - 1) - string_width - xpad;
  174.             y = rectangles[i].y + (rectangles[i].height - string_height)/2 + ascent;
  175.             break;
  176.          case SouthWest:
  177.             x = rectangles[i].x + xpad;
  178.             y = (rectangles[i].y + rectangles[i].height - 1) - descent - 1 - ypad;
  179.             break;
  180.          case South:
  181.             x = rectangles[i].x + (rectangles[i].width - string_width)/2;
  182.             y = (rectangles[i].y + rectangles[i].height - 1) - descent - 1 - ypad;
  183.             break;
  184.          case SouthEast:
  185.             x = (rectangles[i].x + rectangles[i].width - 1) - string_width - xpad;
  186.             y = (rectangles[i].y + rectangles[i].height - 1) - descent - 1 - ypad;
  187.             break;
  188.          default:
  189.             fprintf(stderr, "DrawStringsInRects: unknown gravity\n");
  190.             break;
  191.       }
  192.  
  193.       XDrawString(
  194.          display,
  195.          drawable,
  196.          gc,
  197.          x,y,
  198.          strings[i],
  199.          lengths[i]
  200.       );
  201.    }
  202. }
  203.