home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sources / misc / 4162 / xvertext.2.0 / example.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-12  |  7.7 KB  |  300 lines

  1. /* ************************************************************************ */
  2.  
  3.  
  4. /* This is an example program designed to utilise and manipulate the
  5.    `xvertext' routines.
  6.  
  7.    Copyright (c) 1992 Alan Richardson (mppa3@uk.ac.sussex.syma) */
  8.  
  9.  
  10. /* ************************************************************************ */
  11.  
  12.  
  13. #include <X11/Xlib.h>
  14. #include <X11/Xutil.h>
  15. #include <stdio.h>
  16. #include <signal.h>
  17. #include "rotated.h"
  18.  
  19.   
  20. /* ---------------------------------------------------------------------- */
  21.   
  22.  
  23. extern char    *getenv();
  24.  
  25. long         AllocNamedColor();
  26. void         ShowFontName();
  27. void         PaintMessage();
  28. void         Advance();
  29.  
  30.  
  31. /* ---------------------------------------------------------------------- */
  32.  
  33.  
  34. #define INFO_FONT \
  35.     "-adobe-helvetica-bold-o-normal--14-140-75-75-p-82-iso8859-1"
  36.  
  37. #define NFONTS    18
  38.  
  39. char        *fonts[]={
  40.     "-adobe-helvetica-medium-r-normal--18-180-75-75-p-98-iso8859-1",
  41.     "-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1",
  42.     "-adobe-helvetica-medium-r-normal--10-100-75-75-p-56-iso8859-1",
  43.     "-adobe-courier-bold-o-normal--18-180-75-75-m-110-iso8859-1",
  44.     "-adobe-courier-medium-o-normal--12-120-75-75-m-70-iso8859-1",
  45.     "-adobe-courier-bold-o-normal--10-100-75-75-m-60-iso8859-1",
  46.     "-adobe-times-medium-r-normal--18-180-75-75-p-94-iso8859-1",
  47.     "-adobe-times-bold-r-normal--12-120-75-75-p-67-iso8859-1",
  48.     "-adobe-times-medium-r-normal--10-100-75-75-p-54-iso8859-1",
  49.     "-adobe-helvetica-bold-o-normal--18-180-75-75-p-98-iso8859-1",
  50.     "-adobe-helvetica-medium-o-normal--12-120-75-75-p-67-iso8859-1",
  51.     "-adobe-helvetica-bold-o-normal--10-100-75-75-p-57-iso8859-1",
  52.     "-adobe-courier-medium-r-normal--18-180-75-75-m-110-iso8859-1",
  53.     "-adobe-courier-bold-r-normal--12-120-75-75-m-70-iso8859-1",
  54.     "-adobe-courier-medium-r-normal--10-100-75-75-m-60-iso8859-1",
  55.     "-adobe-times-bold-i-normal--18-180-75-75-p-94-iso8859-1",
  56.     "-adobe-times-medium-i-normal--12-120-75-75-p-63-iso8859-1",
  57.     "-adobe-times-bold-i-normal--10-100-75-75-p-52-iso8859-1"
  58. };
  59.  
  60. char        *message={"\
  61. This is a system for doing\n\
  62. (fairly) fast vertical and\n\
  63. upside down text\n\
  64. in an X Window.\n\
  65. It handles\n\
  66. both fixed width and\n\
  67. proportional fonts.\n\n\
  68. But it will cry\n\
  69. if you shout at it."
  70. };
  71.  
  72.  
  73. /* ---------------------------------------------------------------------- */
  74.   
  75.  
  76. GC info_gc, message_gc;
  77. Display    *dpy;
  78. Window window;  
  79. int depth, screen;
  80. XRotFontStruct *font1=NULL, *font2=NULL;
  81. int ifont, ipos;
  82.  
  83.  
  84. /* ---------------------------------------------------------------------- */
  85.  
  86.  
  87. /* *** Allocates a colour from the default colour map *** */
  88.  
  89. long AllocNamedColor(colname)  
  90.  char *colname;    
  91. {
  92.  XColor scrncol, excol;
  93.              
  94.  if(depth==1)
  95.      return WhitePixel(dpy, screen);
  96.  
  97.  if(XAllocNamedColor(dpy, DefaultColormap(dpy, screen), colname,
  98.                      &scrncol, &excol))
  99.      return scrncol.pixel;
  100.  
  101.  else
  102.      return WhitePixel(dpy, screen);
  103. }
  104.   
  105.  
  106. /* ---------------------------------------------------------------------- */
  107.  
  108.  
  109. /* *** Main routine *** */
  110.  
  111. main(argc, argv)
  112.  int argc;
  113.  char *argv[];
  114. {
  115.  XEvent ev;
  116.  char *host;
  117.  char buf[1];
  118.  float version;
  119.  char copyright[100];
  120.  
  121.  version=XRotVersion(copyright, 100);
  122.  fprintf(stderr, "%s - version %.1f\n", copyright, version);
  123.  
  124.  /* establish connection to display ... */
  125.  if((host=getenv("DISPLAY"))==NULL) {
  126.      fprintf(stderr,"%s : Error : Can't connect to host\n", argv[0]);
  127.      exit(); 
  128.  }
  129.  
  130.  /* open connection to display ... */
  131.  if((dpy=XOpenDisplay(host))==NULL) {
  132.      fprintf(stderr,"%s : Error : Can't open display\n", argv[0]);
  133.      exit(); 
  134.  }
  135.  
  136.  screen=DefaultScreen(dpy);
  137.  depth=DefaultDepth(dpy, screen);
  138.  
  139.  /* create a window ... */
  140.  window=XCreateWindow(dpy, DefaultRootWindow(dpy), 0, 0, 700, 700,
  141.                       0, depth, InputOutput, DefaultVisual(dpy, screen),
  142.                       0, NULL);
  143.  
  144.  XSelectInput(dpy, window, ExposureMask|KeyPressMask);
  145.  XSetWindowBackground(dpy, window, BlackPixel(dpy, screen));
  146.  XMapWindow(dpy, window);
  147.  
  148.  info_gc=XCreateGC(dpy, window, NULL, 0);
  149.  XSetBackground(dpy, info_gc, BlackPixel(dpy, screen));
  150.  XSetForeground(dpy, info_gc, AllocNamedColor("cyan"));
  151.  
  152.  message_gc=XCreateGC(dpy, window, NULL, 0);
  153.  if(depth>1)
  154.      XSetBackground(dpy, message_gc, AllocNamedColor("red"));
  155.  else
  156.      XSetBackground(dpy, message_gc, BlackPixel(dpy, screen));
  157.  XSetForeground(dpy, message_gc, AllocNamedColor("yellow"));
  158.  
  159.  /* load a font for showing the current font name ... */
  160.  font2=XRotLoadFont(dpy, INFO_FONT, 0.);
  161.  if(font2==NULL) {
  162.      if(xv_errno==XV_NOFONT) {
  163.          fprintf(stderr,"no font `%s'\n", INFO_FONT);
  164.          font2=XRotLoadFont(dpy, "fixed", 0.);
  165.      }
  166.      else {
  167.          if(xv_errno==XV_NOMEM)
  168.              fprintf(stderr, "malloc problem - can't continue\n");
  169.          else
  170.              fprintf(stderr, "XImage problem - can't continue\n");
  171.          exit(1);
  172.      }
  173.  }
  174.  
  175.  /* set the alarm for the first time ... */ 
  176.  ifont=-1;
  177.  ipos=8;  
  178.  Advance(0, 0, (struct sigcontext *)NULL);
  179.  
  180.  /* simple event loop ... */
  181.  for(;;) {
  182.      XNextEvent(dpy, &ev);
  183.  
  184.      switch(ev.type) {
  185.          case Expose:
  186.              if(ev.xexpose.count==0) {
  187.                  ShowFontName(False);
  188.                  PaintMessage(); 
  189.              }
  190.              break;
  191.  
  192.          case KeyPress: 
  193.              XLookupString(&ev.xkey, buf, 1, NULL, NULL);
  194.              if(buf[0]=='q' || buf[0]=='Q')
  195.                  exit(0);
  196.              break;
  197.  
  198.         default:
  199.              break;  
  200.      }
  201.  }
  202. }
  203.  
  204.  
  205. /* ---------------------------------------------------------------------- */
  206.    
  207.   
  208. /* *** Display the current font name in the top left corner *** */
  209.  
  210. void ShowFontName(loading)
  211.  Bool loading;
  212. {
  213.  char str[300];
  214.  
  215.  if(loading)
  216.      sprintf(str, "FONT: %s ... loading", fonts[ifont]);
  217.  else
  218.      sprintf(str, "FONT: %s                         ", fonts[ifont]);
  219.  XRotDrawAlignedImageString(dpy, font2, window, info_gc, 0, 0, str, TLEFT);
  220.  
  221.  XFlush(dpy);
  222. }
  223.    
  224.  
  225. /* ---------------------------------------------------------------------- */
  226.                      
  227.  
  228. /* *** Paint the message in the appropriate place *** */
  229.  
  230. void PaintMessage()
  231. {
  232.  static int alignment[]={
  233.     TLEFT, TCENTRE, TRIGHT, MLEFT, MCENTRE, MRIGHT, BLEFT, BCENTRE, BRIGHT
  234.  };
  235.  
  236.  /* draw some centered crosshairs ... */
  237.  XDrawLine(dpy, window, info_gc, 350, 200, 350, 500);
  238.  XDrawLine(dpy, window, info_gc, 200, 350, 500, 350);
  239.  
  240.  /* draw the message ... */
  241.  if(ifont%2==0)
  242.      XRotDrawAlignedString(dpy, font1, window, message_gc, 350, 350,
  243.                            message, alignment[ipos]);
  244.  else 
  245.      XRotDrawAlignedImageString(dpy, font1, window, message_gc, 350, 350,
  246.                                 message, alignment[ipos]);
  247.  
  248.  XFlush(dpy);
  249. }
  250.  
  251.  
  252. /* ---------------------------------------------------------------------- */
  253.  
  254.  
  255. /* *** Change the font, position and/or rotation *** */
  256.  
  257. void Advance(sig, code, scp)
  258.  int sig, code;
  259.  struct sigcontext * scp;
  260. {
  261.  /* advance position ... */
  262.  if((ipos+=2)>=9) {
  263.     if(++ifont==NFONTS) ifont=0;
  264.  
  265.     ipos=(ifont%2);
  266.  
  267.     /* finished with this font ... */
  268.     if(font1!=NULL)
  269.         XRotUnloadFont(dpy, font1);
  270.  
  271.     /* load a new font ... */
  272.     ShowFontName(True);
  273.  
  274.     font1=XRotLoadFont(dpy, fonts[ifont], (float)(ifont%4)*90.);
  275.     if(font1==NULL) {
  276.         if(xv_errno==XV_NOFONT) {
  277.             fprintf(stderr,"no font `%s'\n", fonts[ifont]);
  278.             font1=XRotLoadFont(dpy, "fixed", (float)(ifont%4)*90.);
  279.         }
  280.         else {
  281.             if(xv_errno==XV_NOMEM)
  282.                 fprintf(stderr, "malloc problem - can't continue\n");
  283.             else
  284.                 fprintf(stderr, "XImage problem - can't continue\n");
  285.             exit(1);
  286.         }
  287.     }
  288.     ShowFontName(False);
  289.  }
  290.  
  291.  /* delete old message and draw new ... */
  292.  XClearArea(dpy, window, 0, font2->height+1, 700, 700-font2->height, False);
  293.  PaintMessage();
  294.  
  295.  /* reset alarm ... */
  296.  signal(SIGALRM, Advance);
  297.  alarm(1);
  298. }
  299.  
  300.