home *** CD-ROM | disk | FTP | other *** search
/ pc.louisiana.edu/pub/unix/ / Louisiana_UNIX.tar / Louisiana_UNIX / xspread3.0.zoo / scXstuff.c < prev    next >
C/C++ Source or Header  |  1994-06-02  |  10KB  |  294 lines

  1. /*
  2.  * Copyright (C) 1992  Board of Regents of the University of Wisconsin
  3.  * on behalf of the Department of Electrical Engineering and Computer
  4.  * Science, University of Wisconsin-Milwaukee, Milwaukee, WI 53201.
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * a copy of which is included here in file "GNU_GENERAL"
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  * The programs in this directory were developed by software engineering 
  22.  * teams as part of the course "Introduction to Software Engineering" 
  23.  * under the supervision of Professor G. Davida.
  24.  * This is a modification of a program written or modified by
  25.  * others.  The original copyrights, as per GNU General Public License,
  26.  * may still be applicable.  The UWM copyright is applicable only
  27.  * the those parts generated at UWM.
  28.  *
  29.  * Please send all changes, enhancements, and other comments about this
  30.  * software to
  31.  *             soft-eng@cs.uwm.edu
  32.  *
  33.  * No Warranty, expressed or implied, comes with this software.
  34.  * This software is intended to be used by not-for-profit
  35.  * organizations or by individuals for personal HOME use. 
  36.  * This software, or any of its parts, may not be used by for-profit
  37.  * organization, regardless of application or intended product or
  38.  * customer, without the permission of the Board of Regents of the 
  39.  * University  of Wisconsin is strictly forbidden. 
  40.  *
  41.  * Contact:    soft-eng@cs.uwm.edu
  42.  *            or
  43.  *        
  44.  *        Software Engineering Coordinator
  45.  *        Computer Science
  46.  *            Department of EECS
  47.  *        University of Wisconsin - Milwaukee
  48.  *        Milwaukee, WI  53201
  49.  *        414-229-4677
  50.  *
  51.  *        HISTORY,CLAIMS and CONTRIBUTIONS
  52.  */
  53.  
  54. /**********************************************************************
  55. /*                                                                    *
  56. /*      Error rountine modified by Mike Frey and Jim Cornelius.       *       
  57. /*                                              Fall, 1991            *
  58. /**********************************************************************
  59. /* scXstuff.c
  60. This file contains the code for initializing and keeping track of basic
  61. X-Windows information such as fonts, the size of the window, etc.  Functions
  62. and macros are explained as they are presented.  */
  63.  
  64. /* REVISION HISTORY */
  65. /* 7-19-91  B. Backman    Creation */
  66.  
  67. #include <X11/Xlib.h>
  68. #include <X11/Xutil.h>
  69. #include <stdio.h>  /*define FILE and NULL */
  70. #include "config.h"
  71. #include "sc.h"
  72.  
  73. unsigned long white, black;     /* white and black pixels */
  74. unsigned long bw;             /* border width */
  75. XGCValues gcv;                   /* struct for creating GC */
  76. XSizeHints   xsh;        /* size hints for window manager */
  77. XSetWindowAttributes xswa;    /* Temporary Set Window Attribute struct */
  78. XWindowAttributes xwa;            /* Temporary Window Attribute struct */
  79.  
  80. /* The following variables are declared in scXstuff.h */
  81. Display    *dpy;            /* X server (workstation) connection */
  82. Window     mainwin;         /* resource ID of main window */
  83. GC         maingc,          /* GC for mainwin */
  84.        maingcreversed,  /* Reverse-field GC for mainwin */
  85.        invertgc;        /* (invert) reverse-field GC for mainwin */
  86. XFontStruct *curfont;       /* Font descriptor struct for current font */
  87. Font       curfontid;       /* resource id of current font */
  88. int        curfontwidth,
  89.        curfontheight;   /* pixel dimensions of current font */
  90. char       *userfont;       /* User specifed font from command line */
  91.  
  92. extern int seenerr;        /* 1 if error just been displayed, 0 otherwise */
  93.  
  94. /* macros textrow() and textcol() compute the y-coordinate of the bottom of row
  95.    r and the x-coordinate of the left-hand side of column c, respectively. 
  96.    This is for use with XDrawImageString.  The coordinates are based on 
  97.    curfontheight and curfontwidth.  NOTE: textcol() will only work for a 
  98.    fixed-width font! Otherwise, it doesn't make sense to calculate column 
  99.    positions anyway because they change */
  100.  
  101. #define textrow(r)  ( ( ((r)+1) * curfontheight) - 1)
  102. #define textcol(c)  ( (c) * curfontwidth)
  103.  
  104. /***
  105.  function error() used to be a macro, but the X call is more complicated than 
  106.  the curses version was 
  107. ***/
  108. void error(errstring)
  109.   char *errstring;
  110. {
  111.  
  112. #ifdef PSC
  113.    fprintf(stderr, errstring);
  114. #else
  115. #ifndef DOINGX
  116.    move(1,0); clrtoeol();  printw();
  117. #else
  118.  
  119.   clearlines(1, 1);
  120.   XBell(dpy, 100);
  121.   /*fprintf(stderr,"\007");*/
  122.   XDrawImageString(dpy, mainwin, maingc,
  123.            textcol(0), textrow(1),
  124.            errstring, strlen(errstring));
  125.   XFlush(dpy);
  126.   seenerr = 1; 
  127.  
  128. #endif
  129. #endif
  130. }
  131.  
  132. /***
  133. the function usefont() takes a font structure as an argument
  134. and sets the global variables curfont, curfontid, curfontheignt, and
  135. Curfontwidth to the values appropriate values for the specified font. 
  136. ***/
  137. usefont(fontinfo)
  138.   XFontStruct *fontinfo;
  139. {
  140.   curfont = fontinfo;
  141.   curfontid = fontinfo->fid;
  142.   curfontheight = fontinfo->max_bounds.ascent + fontinfo->max_bounds.descent;
  143.   curfontwidth = fontinfo->max_bounds.width;
  144. } /* end of usefont() */
  145.  
  146. #ifndef SC_FONT
  147. #define SC_FONT "fixed"
  148. #endif
  149.  
  150. /***
  151. function sc_Xinit() initializes all of the global variables defined in 
  152. this file.  argc and argv are used to set some of the window parameters,
  153. if any X parameters were given on the command line.
  154. ***/
  155. sc_Xinit(argc, argv)
  156.   int    argc;
  157.   char **argv;
  158. {
  159.   extern char *version;
  160.  
  161.   /* open the display, using the DISPLAY env. variable as default */
  162.   if ((dpy = XOpenDisplay(NULL)) == NULL)
  163.   {
  164.     fprintf(stderr, "%s: Can't open display %s\n",argv[0], XDisplayName(NULL));
  165.     exit(1);
  166.   }
  167.  
  168. #ifdef DEBUG    /* Peter Doemel, 10-Feb-1993 */
  169.   XSynchronize( dpy, 1);    /* disable all buffering */
  170. #endif
  171.  
  172.   /* load the font to use */
  173.   if (userfont == NULL)
  174.      curfont = XLoadQueryFont(dpy, SC_FONT);
  175.   else
  176.      curfont = XLoadQueryFont(dpy, userfont);
  177.   if (curfont == NULL)
  178.   {
  179.     fprintf(stderr, "%s: Display %s doesn't know font \"%s\" \n",
  180.       progname, DisplayString(dpy), userfont == NULL ? SC_FONT : userfont);
  181.     exit(1);
  182.   }
  183.   /* initialize the font-related globals */
  184.   usefont(curfont);
  185.  
  186.   /* initialize pixel values  */
  187.   black = BlackPixel(dpy, DefaultScreen(dpy)); /* border */
  188.   white = WhitePixel(dpy, DefaultScreen(dpy)); /* background */
  189.  
  190.   /* border width of 1 */
  191.   bw = 1;
  192.  
  193.   /* fill in the window manager hints */
  194.   xsh.flags = ( PMinSize | PResizeInc | PPosition );
  195.   xsh.min_width= (MIN_COLS*curfontwidth);
  196.   xsh.width = (MIN_COLS * curfontwidth);
  197.   xsh.min_height = xsh.height = ((MIN_ROWS + 1) * curfontheight);
  198.   xsh.width_inc = curfontwidth;
  199.   xsh.height_inc = curfontheight;
  200.   xsh.x = xsh.y = 0; 
  201.  
  202.   /* create the main window and give the hints to the window manager */
  203.   mainwin = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy),
  204.                  xsh.x, xsh.y, xsh.min_width, xsh.min_height,
  205.                 bw, black, white);
  206.   XSetStandardProperties(dpy,mainwin, version, version, None, argv,argc,&xsh);
  207.   /* I don't think the following is necessary.  I think the previous line
  208.     took care of it.
  209.   XSetNormalHints(dpy,win,&xsh);  */
  210.  
  211.   /* Insure that the window's colormap points to the default colormap, and
  212.      set the window's bit gravity to NorthWest, because that is the origin 
  213.      of everything in the window */
  214.   xswa.colormap = DefaultColormap(dpy,DefaultScreen(dpy));
  215.   XChangeWindowAttributes(dpy,mainwin, CWColormap, &xswa);
  216.  
  217.   /* create the normal Graphics Context */
  218.   maingc = XCreateGC(dpy,mainwin, 0,0); /* create default GC */
  219.   XSetFont(dpy, maingc, curfontid);
  220.   XSetForeground(dpy, maingc, black);
  221.   XSetBackground(dpy, maingc, white);
  222.   /* and the reversed GC */
  223.   maingcreversed = XCreateGC(dpy,mainwin, 0,0);
  224.   XSetFont(dpy, maingcreversed, curfontid);
  225.   XSetForeground(dpy, maingcreversed, white);
  226.   XSetBackground(dpy, maingcreversed, black);
  227.  
  228.   /* and the (inverting) reversed GC */
  229.   invertgc=XCreateGC(dpy,mainwin,0,0);
  230.   XCopyGC(dpy,maingc, GCForeground | GCBackground, invertgc);
  231.   XSetFunction(dpy, invertgc, GXinvert);
  232.   XSetPlaneMask(dpy, invertgc, white^black);
  233.  
  234.   /* input event selection */
  235.   XSelectInput(dpy, mainwin, 
  236.    StructureNotifyMask | KeyPressMask | ButtonPressMask | 
  237.                  ExposureMask | PointerMotionMask | Button1MotionMask);
  238.  
  239.   /* map the window to make it visible */
  240.   XMapRaised(dpy, mainwin);
  241.  
  242.   /*determine the window's dimensions */
  243.   if (XGetWindowAttributes(dpy, mainwin, &xwa) == 0)
  244.   {
  245.     fprintf(stderr,"%s: Error. Cannot get attributes of main window.",
  246.         progname);
  247.     exit(1);
  248.   }
  249.   maintextcols = xwa.width / curfontwidth;
  250.   maintextrows = xwa.height / curfontheight - 1;
  251.   /* successful completion  */
  252.   return 0;
  253. }
  254.  
  255. /***
  256. function sc_handleresize() handles ConfigureNotify events, resetting the 
  257. global variables maintextrows and maintextcols 
  258. ***/
  259. sc_handleresize(event)
  260.   XEvent *event;
  261. {
  262.   if (event->type != ConfigureNotify) return(0);
  263.   maintextrows =  event->xconfigure.height / curfontheight - 1;
  264.   maintextcols =  event->xconfigure.width / curfontwidth;
  265.   return (0);
  266. }
  267.  
  268. /***
  269. function cleardownfrom() clears the window from row, down to the bottom.
  270. ***/
  271. cleardownfrom(row)
  272.   int row;
  273. {
  274.   XClearArea(dpy,mainwin,
  275.          0, textrow(row-1)+1,
  276.          0,                        /* 0 width => clear to right side */
  277.              0,                        /* 0 height => remaining window height*/
  278.          0);                       /* don't generate Expose events */
  279. }
  280.  
  281. /***
  282. function clearupfrom() clears the row r, and any lines above it 
  283. ***/
  284. clearupfrom(r)
  285.   int r;
  286. {
  287.   XClearArea(dpy,mainwin,
  288.          0,0,            /* top left corner */
  289.          0,              /* use width of window */
  290.              textrow(r),     /* go through row r */
  291.          0);             /* don't send Expose events */
  292. }
  293.  
  294.