File: scXstuff.c

package info (click to toggle)
xspread 3.1.1c-9
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,836 kB
  • ctags: 1,426
  • sloc: ansic: 19,342; yacc: 499; lisp: 231; makefile: 198; sh: 28; sed: 4
file content (300 lines) | stat: -rw-r--r-- 10,326 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * Copyright (C) 1992  Board of Regents of the University of Wisconsin
 * on behalf of the Department of Electrical Engineering and Computer
 * Science, University of Wisconsin-Milwaukee, Milwaukee, WI 53201.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * a copy of which is included here in file "GNU_GENERAL"
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * The programs in this directory were developed by software engineering 
 * teams as part of the course "Introduction to Software Engineering" 
 * under the supervision of Professor G. Davida.
 * This is a modification of a program written or modified by
 * others.  The original copyrights, as per GNU General Public License,
 * may still be applicable.  The UWM copyright is applicable only
 * the those parts generated at UWM.
 *
 * Please send all changes, enhancements, and other comments about this
 * software to
 *     		soft-eng@cs.uwm.edu
 *
 * No Warranty, expressed or implied, comes with this software.
 * This software is intended to be used by not-for-profit
 * organizations or by individuals for personal HOME use. 
 * This software, or any of its parts, may not be used by for-profit
 * organization, regardless of application or intended product or
 * customer, without the permission of the Board of Regents of the 
 * University  of Wisconsin. 
 *
 * Contact:	soft-eng@cs.uwm.edu
 *			or
 *		
 *		Software Engineering Coordinator
 *		Computer Science
 *    		Department of EECS
 *		University of Wisconsin - Milwaukee
 *		Milwaukee, WI  53201
 *		414-229-4677
 *
 *		HISTORY,CLAIMS and CONTRIBUTIONS
 */

/**********************************************************************
 *                                                                    *
 *      Error rountine modified by Mike Frey and Jim Cornelius.       *       
 *                                              Fall, 1991            *
 **********************************************************************/
/* scXstuff.c
This file contains the code for initializing and keeping track of basic
X-Windows information such as fonts, the size of the window, etc.  Functions
and macros are explained as they are presented.  */

/* REVISION HISTORY */
/* 7-19-91  B. Backman    Creation */

#include <config.h>

#ifdef HAVE_X11_X_H	/* this code for now is X specific */

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <curses.h>  /*define FILE and NULL */
#include "sc.h"
#include "scXstuff.h"

unsigned long bg, fg, hf;     /* white and black pixels */
unsigned long bw;         	/* border width */
XGCValues gcv;         	  	/* struct for creating GC */
XSizeHints   xsh;		/* size hints for window manager */
XSetWindowAttributes xswa;	/* Temporary Set Window Attribute struct */
XWindowAttributes xwa;	        /* Temporary Window Attribute struct */

/* The following variables are declared in scXstuff.h */
Display    *dpy;            /* X server (workstation) connection */
Window     mainwin;         /* resource ID of main window */
GC         maingc,          /* GC for mainwin */
	   maingcreversed,  /* Reverse-field GC for mainwin */
	   invertgc;		/* (invert) reverse-field GC for mainwin */
XFontStruct *curfont;       /* Font descriptor struct for current font */
Font       curfontid;       /* resource id of current font */
int        curfontwidth,
	   curfontheight;   /* pixel dimensions of current font */
char	   *userfont;       /* User specifed font from command line */

char backg[30];
char foreg[30];


XColor exactBl, exactOrange, Orange, Bl;
Colormap cmap;

/* macros textrow() and textcol() compute the y-coordinate of the bottom of row
   r and the x-coordinate of the left-hand side of column c, respectively. 
   This is for use with XDrawImageString.  The coordinates are based on 
   curfontheight and curfontwidth.  NOTE: textcol() will only work for a 
   fixed-width font! Otherwise, it doesn't make sense to calculate column 
   positions anyway because they change */
/*
#define textrow(r)  ( ( ((r)+1) * curfontheight) - 1)
#define textcol(c)  ( (c) * curfontwidth)
*/

/***
the function usefont() takes a font structure as an argument
and sets the global variables curfont, curfontid, curfontheignt, and
Curfontwidth to the values appropriate values for the specified font. 
***/
void
usefont(fontinfo)
  XFontStruct *fontinfo;
{
  curfont = fontinfo;
  curfontid = fontinfo->fid;
  curfontheight = fontinfo->max_bounds.ascent + fontinfo->max_bounds.descent;
  curfontwidth = fontinfo->max_bounds.width;
} /* end of usefont() */

#ifndef SC_FONT
#define SC_FONT "fixed"
#endif

/***
function sc_Xinit() initializes all of the global variables defined in 
this file.  argc and argv are used to set some of the window parameters,
if any X parameters were given on the command line.
returns TRUE if the X interface 
***/
int
sc_Xinit(argc, argv)
  int    argc;
  char **argv;
{
  extern char *version;

  /* open the display, using the DISPLAY env. variable as default */
  if ((dpy = XOpenDisplay(NULL)) == NULL)
  {
    fprintf(stderr, "%s: Can't open display %s\n",argv[0], XDisplayName(NULL));
    return(FALSE);
  }

#ifdef DEBUG	/* Peter Doemel, 10-Feb-1993 */
  XSynchronize( dpy, 1);	/* disable all buffering */
#endif

  /* load the font to use */
  if (userfont == NULL)
     curfont = XLoadQueryFont(dpy, SC_FONT);
  else
     curfont = XLoadQueryFont(dpy, userfont);
  if (curfont == NULL)
  {
    fprintf(stderr, "%s: Display %s doesn't know font \"%s\" \n",
	  progname, DisplayString(dpy), userfont == NULL ? SC_FONT : userfont);
    return(FALSE);
  }
  /* initialize the font-related globals */
  usefont(curfont);

  /* initialize pixel values  */

cmap = DefaultColormap (dpy, DefaultScreen (dpy));
/* if ((XAllocNamedColor (dpy, cmap, "Orange", &exactOrange, &Orange) != 0) && (XAllocNamedColor (dpy, cmap, "Black", &exactBlack, &Black) != 0)) */
if (backg && (XAllocNamedColor (dpy, cmap, backg, &exactOrange, &Orange) != 0)) 
    {
    bg = Orange.pixel;
    }
   else
    {
    bg = WhitePixel(dpy, DefaultScreen(dpy)); /* background */
    }
if (foreg && (XAllocNamedColor (dpy, cmap, foreg, &exactBl, &Bl) != 0))
    {
    fg = Bl.pixel; 
    }
   else
    {
    fg = BlackPixel(dpy, DefaultScreen(dpy)); /* foreground */
    }

  /* border width of 1 */
  bw = 1;

  /* fill in the window manager hints */
  xsh.flags = ( PMinSize | PResizeInc | PPosition );
  xsh.min_width= (MIN_COLS*curfontwidth);
  xsh.width = (MIN_COLS * curfontwidth);
  xsh.min_height = xsh.height = ((MIN_ROWS + 1) * curfontheight);
  xsh.width_inc = curfontwidth;
  xsh.height_inc = curfontheight;
  xsh.x = xsh.y = 0; 

  /* create the main window and give the hints to the window manager */
  mainwin = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy),
 				xsh.x, xsh.y, xsh.min_width, xsh.min_height,
				bw, fg, bg);
  XSetStandardProperties(dpy,mainwin, version, version, None, argv,argc,&xsh);
  /* I don't think the following is necessary.  I think the previous line
    took care of it.
  XSetNormalHints(dpy,win,&xsh);  */

  /* Insure that the window's colormap points to the default colormap, and
     set the window's bit gravity to NorthWest, because that is the origin 
     of everything in the window */
  xswa.colormap = DefaultColormap(dpy,DefaultScreen(dpy));
  XChangeWindowAttributes(dpy,mainwin, CWColormap, &xswa);

  /* create the normal Graphics Context */
  maingc = XCreateGC(dpy,mainwin, 0,0); /* create default GC */
  XSetFont(dpy, maingc, curfontid);
  XSetForeground(dpy, maingc, fg);
  XSetBackground(dpy, maingc, bg);
  /* and the reversed GC */
  maingcreversed = XCreateGC(dpy,mainwin, 0,0);
  XSetFont(dpy, maingcreversed, curfontid);
  XSetForeground(dpy, maingcreversed, bg);
  XSetBackground(dpy, maingcreversed, fg);

  /* and the (inverting) reversed GC */
  invertgc = XCreateGC(dpy,mainwin, 0,0);
  XCopyGC(dpy,maingc, GCForeground | GCBackground, invertgc);
  XSetFunction(dpy, invertgc, GXinvert);
  XSetPlaneMask(dpy, invertgc, bg^fg);

  /* input event selection */
  XSelectInput(dpy, mainwin, 
   StructureNotifyMask | KeyPressMask | ButtonPressMask | 
		         ExposureMask | PointerMotionMask | Button1MotionMask);

  /* map the window to make it visible */
  XMapRaised(dpy, mainwin);

  /*determine the window's dimensions */
  if (XGetWindowAttributes(dpy, mainwin, &xwa) == 0)
  {
    fprintf(stderr,"%s: Error. Cannot get attributes of main window.",
	    progname);
    return(FALSE);
  }
  maintextcols = xwa.width / curfontwidth;
  maintextrows = xwa.height / curfontheight - 1;

  /* successful completion  */
  return(TRUE);
}

/***
function sc_handleresize() handles ConfigureNotify events, resetting the 
global variables maintextrows and maintextcols 
***/
void
sc_handleresize(event)
  XEvent *event;
{
  if (event->type != ConfigureNotify)
	return;
  maintextrows =  event->xconfigure.height / curfontheight - 1;
  maintextcols =  event->xconfigure.width / curfontwidth;
}

/***
function cleardownfrom() clears the window from row, down to the bottom.
***/
void
cleardownfrom(row)
  int row;
{
  XClearArea(dpy,mainwin,
	     0, textrow(row-1)+1,
	     0,                        /* 0 width => clear to right side */
             0,                        /* 0 height => remaining window height*/
	     0);                       /* don't generate Expose events */
}

/***
function clearupfrom() clears the row r, and any lines above it 
***/
void
clearupfrom(r)
  int r;
{
  XClearArea(dpy,mainwin,
	     0,0,            /* top left corner */
	     0,              /* use width of window */
             textrow(r),     /* go through row r */
	     0);             /* don't send Expose events */
}

#endif /* HAVE_X11_X_H	this code for now is X specific */