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.
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 <config.h>
68 
69 #ifdef HAVE_X11_X_H	/* this code for now is X specific */
70 
71 #include <X11/Xlib.h>
72 #include <X11/Xutil.h>
73 #include <curses.h>  /*define FILE and NULL */
74 #include "sc.h"
75 #include "scXstuff.h"
76 
77 unsigned long bg, fg, hf;     /* white and black pixels */
78 unsigned long bw;         	/* border width */
79 XGCValues gcv;         	  	/* struct for creating GC */
80 XSizeHints   xsh;		/* size hints for window manager */
81 XSetWindowAttributes xswa;	/* Temporary Set Window Attribute struct */
82 XWindowAttributes xwa;	        /* Temporary Window Attribute struct */
83 
84 /* The following variables are declared in scXstuff.h */
85 Display    *dpy;            /* X server (workstation) connection */
86 Window     mainwin;         /* resource ID of main window */
87 GC         maingc,          /* GC for mainwin */
88 	   maingcreversed,  /* Reverse-field GC for mainwin */
89 	   invertgc;		/* (invert) reverse-field GC for mainwin */
90 XFontStruct *curfont;       /* Font descriptor struct for current font */
91 Font       curfontid;       /* resource id of current font */
92 int        curfontwidth,
93 	   curfontheight;   /* pixel dimensions of current font */
94 char	   *userfont;       /* User specifed font from command line */
95 
96 char backg[30];
97 char foreg[30];
98 
99 
100 XColor exactBl, exactOrange, Orange, Bl;
101 Colormap cmap;
102 
103 /* macros textrow() and textcol() compute the y-coordinate of the bottom of row
104    r and the x-coordinate of the left-hand side of column c, respectively.
105    This is for use with XDrawImageString.  The coordinates are based on
106    curfontheight and curfontwidth.  NOTE: textcol() will only work for a
107    fixed-width font! Otherwise, it doesn't make sense to calculate column
108    positions anyway because they change */
109 /*
110 #define textrow(r)  ( ( ((r)+1) * curfontheight) - 1)
111 #define textcol(c)  ( (c) * curfontwidth)
112 */
113 
114 /***
115 the function usefont() takes a font structure as an argument
116 and sets the global variables curfont, curfontid, curfontheignt, and
117 Curfontwidth to the values appropriate values for the specified font.
118 ***/
119 void
usefont(fontinfo)120 usefont(fontinfo)
121   XFontStruct *fontinfo;
122 {
123   curfont = fontinfo;
124   curfontid = fontinfo->fid;
125   curfontheight = fontinfo->max_bounds.ascent + fontinfo->max_bounds.descent;
126   curfontwidth = fontinfo->max_bounds.width;
127 } /* end of usefont() */
128 
129 #ifndef SC_FONT
130 #define SC_FONT "fixed"
131 #endif
132 
133 /***
134 function sc_Xinit() initializes all of the global variables defined in
135 this file.  argc and argv are used to set some of the window parameters,
136 if any X parameters were given on the command line.
137 returns TRUE if the X interface
138 ***/
139 int
sc_Xinit(argc,argv)140 sc_Xinit(argc, argv)
141   int    argc;
142   char **argv;
143 {
144   extern char *version;
145 
146   /* open the display, using the DISPLAY env. variable as default */
147   if ((dpy = XOpenDisplay(NULL)) == NULL)
148   {
149     fprintf(stderr, "%s: Can't open display %s\n",argv[0], XDisplayName(NULL));
150     return(FALSE);
151   }
152 
153 #ifdef DEBUG	/* Peter Doemel, 10-Feb-1993 */
154   XSynchronize( dpy, 1);	/* disable all buffering */
155 #endif
156 
157   /* load the font to use */
158   if (userfont == NULL)
159      curfont = XLoadQueryFont(dpy, SC_FONT);
160   else
161      curfont = XLoadQueryFont(dpy, userfont);
162   if (curfont == NULL)
163   {
164     fprintf(stderr, "%s: Display %s doesn't know font \"%s\" \n",
165 	  progname, DisplayString(dpy), userfont == NULL ? SC_FONT : userfont);
166     return(FALSE);
167   }
168   /* initialize the font-related globals */
169   usefont(curfont);
170 
171   /* initialize pixel values  */
172 
173 cmap = DefaultColormap (dpy, DefaultScreen (dpy));
174 /* if ((XAllocNamedColor (dpy, cmap, "Orange", &exactOrange, &Orange) != 0) && (XAllocNamedColor (dpy, cmap, "Black", &exactBlack, &Black) != 0)) */
175 if (backg && (XAllocNamedColor (dpy, cmap, backg, &exactOrange, &Orange) != 0))
176     {
177     bg = Orange.pixel;
178     }
179    else
180     {
181     bg = WhitePixel(dpy, DefaultScreen(dpy)); /* background */
182     }
183 if (foreg && (XAllocNamedColor (dpy, cmap, foreg, &exactBl, &Bl) != 0))
184     {
185     fg = Bl.pixel;
186     }
187    else
188     {
189     fg = BlackPixel(dpy, DefaultScreen(dpy)); /* foreground */
190     }
191 
192   /* border width of 1 */
193   bw = 1;
194 
195   /* fill in the window manager hints */
196   xsh.flags = ( PMinSize | PResizeInc | PPosition );
197   xsh.min_width= (MIN_COLS*curfontwidth);
198   xsh.width = (MIN_COLS * curfontwidth);
199   xsh.min_height = xsh.height = ((MIN_ROWS + 1) * curfontheight);
200   xsh.width_inc = curfontwidth;
201   xsh.height_inc = curfontheight;
202   xsh.x = xsh.y = 0;
203 
204   /* create the main window and give the hints to the window manager */
205   mainwin = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy),
206  				xsh.x, xsh.y, xsh.min_width, xsh.min_height,
207 				bw, fg, bg);
208   XSetStandardProperties(dpy,mainwin, version, version, None, argv,argc,&xsh);
209   /* I don't think the following is necessary.  I think the previous line
210     took care of it.
211   XSetNormalHints(dpy,win,&xsh);  */
212 
213   /* Insure that the window's colormap points to the default colormap, and
214      set the window's bit gravity to NorthWest, because that is the origin
215      of everything in the window */
216   xswa.colormap = DefaultColormap(dpy,DefaultScreen(dpy));
217   XChangeWindowAttributes(dpy,mainwin, CWColormap, &xswa);
218 
219   /* create the normal Graphics Context */
220   maingc = XCreateGC(dpy,mainwin, 0,0); /* create default GC */
221   XSetFont(dpy, maingc, curfontid);
222   XSetForeground(dpy, maingc, fg);
223   XSetBackground(dpy, maingc, bg);
224   /* and the reversed GC */
225   maingcreversed = XCreateGC(dpy,mainwin, 0,0);
226   XSetFont(dpy, maingcreversed, curfontid);
227   XSetForeground(dpy, maingcreversed, bg);
228   XSetBackground(dpy, maingcreversed, fg);
229 
230   /* and the (inverting) reversed GC */
231   invertgc = XCreateGC(dpy,mainwin, 0,0);
232   XCopyGC(dpy,maingc, GCForeground | GCBackground, invertgc);
233   XSetFunction(dpy, invertgc, GXinvert);
234   XSetPlaneMask(dpy, invertgc, bg^fg);
235 
236   /* input event selection */
237   XSelectInput(dpy, mainwin,
238    StructureNotifyMask | KeyPressMask | ButtonPressMask |
239 		         ExposureMask | PointerMotionMask | Button1MotionMask);
240 
241   /* map the window to make it visible */
242   XMapRaised(dpy, mainwin);
243 
244   /*determine the window's dimensions */
245   if (XGetWindowAttributes(dpy, mainwin, &xwa) == 0)
246   {
247     fprintf(stderr,"%s: Error. Cannot get attributes of main window.",
248 	    progname);
249     return(FALSE);
250   }
251   maintextcols = xwa.width / curfontwidth;
252   maintextrows = xwa.height / curfontheight - 1;
253 
254   /* successful completion  */
255   return(TRUE);
256 }
257 
258 /***
259 function sc_handleresize() handles ConfigureNotify events, resetting the
260 global variables maintextrows and maintextcols
261 ***/
262 void
sc_handleresize(event)263 sc_handleresize(event)
264   XEvent *event;
265 {
266   if (event->type != ConfigureNotify)
267 	return;
268   maintextrows =  event->xconfigure.height / curfontheight - 1;
269   maintextcols =  event->xconfigure.width / curfontwidth;
270 }
271 
272 /***
273 function cleardownfrom() clears the window from row, down to the bottom.
274 ***/
275 void
cleardownfrom(row)276 cleardownfrom(row)
277   int row;
278 {
279   XClearArea(dpy,mainwin,
280 	     0, textrow(row-1)+1,
281 	     0,                        /* 0 width => clear to right side */
282              0,                        /* 0 height => remaining window height*/
283 	     0);                       /* don't generate Expose events */
284 }
285 
286 /***
287 function clearupfrom() clears the row r, and any lines above it
288 ***/
289 void
clearupfrom(r)290 clearupfrom(r)
291   int r;
292 {
293   XClearArea(dpy,mainwin,
294 	     0,0,            /* top left corner */
295 	     0,              /* use width of window */
296              textrow(r),     /* go through row r */
297 	     0);             /* don't send Expose events */
298 }
299 
300 #endif /* HAVE_X11_X_H	this code for now is X specific */
301