File: handlexevent.c

package info (click to toggle)
pgplot5 5.2-13
  • links: PTS
  • area: non-free
  • in suites: potato
  • size: 6,280 kB
  • ctags: 5,903
  • sloc: fortran: 37,938; ansic: 18,809; sh: 1,147; objc: 532; makefile: 363; perl: 234; pascal: 233; tcl: 178; awk: 51; csh: 25
file content (437 lines) | stat: -rw-r--r-- 13,013 bytes parent folder | download | duplicates (15)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* The handlexevent routine takes care of a single X event for the figdisp */
/* display server. */
/* Return Values: */
/* FAIL		If something went wrong */
/* SUCCEED	If the user requested an exit */

/* Sam Southard, Jr. */
/* Created: 3-Dec-1991 (from figdisp mainloop.c) */
/* Modification History: */
/* 30-Jan-1992	SNS/CIT	Keck mods merged back into standard display server */
/* 31-Jan-1992	SNS/CIT	All motion events until a button event are now read */
/*			before any action is taken.  This drastically */
/*			improves response times on X terminals */
/*  6-Apr-1992	SNS/CIT	Now uses large crosshairs cursor for line graphics. */
/*			Now handles the case when a button release event is */
/*			lost. */
/*  8-Apr-1992	SNS/CIT	Buttonrelease lost recovery code added to LUT */
/*			manipulation button.  No longer sets active line */
/*			graphics color to 1 when drawing cursor. */
/* 10-Apr-1992	SNS/CIT	Now handles lg.winxoff & lg.winyoff */
/*  7-May-1992	SNS/CIT	Now handles res.lgcross */
/* 27-Sep-1992	SNS/CIT	Return buffer now stored in network byte order */
/* 30-Sep-1992	SNS/CIT	LUT_WRAP no longer defined at compile time.  RCS id */
/*			string added.  Now allows the addition of an offset */
/*			into the color map. */
/*  4-Oct-1992	SNS/CIT	No longer needs #ifdef KECK */
/* 14-Oct-1992	SNS/CIT	Merged in changes from ARC/HI. */

#ifndef lint
static char rcsid[]="@(#)$Id: handlexevent.c,v 1.12 1993/10/08 00:02:48 figaro Exp figaro $";
#endif

/* The system include files */
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>

/* The X Window include files */
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>

/* The program include files */
#include "commands.h"
#include "figdisp.h"
#include "globals.h"
#include "messages.h"

int luttransoff=0;
int modluttransoff=0;

int handlexevent(event,go_on)
XEvent event;
int *go_on;	/* whether the calling routine shoudl exit successfully */
{
#ifndef PGDISP
	static int slitxs= -1,slitys;	/* the starting point for the slit */
	static int slitxe= -1,slitye;
	static int xorlinedrawn=0;
	int nluts;	/* the number of LUTs in the current image */
	static int moffx,moffy;	/* The starting coordinates for LUT offset */
				/* modification (Ctrl-M1) */
#endif
	int px,py;	/* The pointer X & Y position */
	XEvent event2;	/* A second event */
	static short retbuf[7];		/* A buffer for return values */
	static int retbuflen=0;		/* the actual length of the buffer */
	static int lgx= -1 ,lgy;	/* current line graphics line pos */
	Window windum;
	int dummy;
	unsigned int keys;

	void returnbuf();	/* return data to user program */
	void resizelgwin();	/* resize line graphics window */
	void exposelgwin();	/* expose the line graphics window */
	void updatelgtitle();	/* update the line graphics cursor position */
#ifndef PGDISP
	void updatetitle();
	void exposesee();	/* expose the seeing window */
	void resizebmwin();	/* resize the bitmap graphics window */
	void transluts();	/* map the LUT data into the LUTs we have */
	void transim();		/* translate the image window */
	void panpatch();	/* pan the patch window */
	void panloc();		/* pan the location window */
	void zoomim();		/* set the image zoom factor */
	void exposebmwin();	/* expose the bitmap graphics window */
	void exposepatch();	/* expose the patch window */
	void exposecmap();	/* expose the color map window */
	void doslit();		/* make a line plot */
#endif

	switch(event.type)
	{
	case SelectionClear:
		/* Unless this is a selection atom, ignore it */
		if (event.xselectionclear.selection != selatom) break;

		/* someone's grabbed the selection, get ready for some data */
		if (!selset)
		{
			if ((srcwin=XGetSelectionOwner(display, selatom))
			    == None)
			{
				(void)fprintf(stderr,MSG_NOSELOWN);
				return(FAIL);
			}
			selset=1;
		}
		XConvertSelection(display, selatom, XA_STRING, dataatom,
			lg.win, event.xselectionclear.time);
		break;
	case SelectionNotify: /* someone sent us data! */
		*go_on=getdata(event.xselection, &retbuf[0], &retbuflen,
			srcwin, &selset);
		break;
	case Expose:
		if (event.xexpose.window == lg.win) exposelgwin(event.xexpose);
#ifndef PGDISP
		else if (event.xexpose.window == bm.win)
			exposebmwin(event.xexpose.x, event.xexpose.y,
				event.xexpose.width, event.xexpose.height);
		else if (event.xexpose.window == patch.win) exposepatch();
		else if (event.xexpose.window == box.win) exposebox();
		else if (event.xexpose.window == loc.win) {
			if (updateloc()) return(FAIL);
		} else if (event.xexpose.window == cwin.win)
			exposecmap(event.xexpose);
		else if (event.xexpose.window == seeing.win) exposesee();
#endif
		break;
	case ConfigureNotify:
		if (event.xconfigure.window == lg.win)
			resizelgwin(event.xconfigure);
#ifndef PGDISP
		else if (event.xconfigure.window == bm.win)
			resizebmwin(event.xconfigure);
		else if (event.xconfigure.window == patch.win)
		{
			patch.width=event.xconfigure.width;
			patch.height=event.xconfigure.height;
		} else if (event.xconfigure.window == loc.win) {
			if (resizeloc(event.xconfigure.width,
				event.xconfigure.height)) return(FAIL);
		} else if (event.xconfigure.window == cwin.win)
			if (resizecmap(event.xconfigure.width,
				event.xconfigure.height)) return(FAIL);
#endif
		break;
#ifndef PGDISP
	case ButtonRelease:
		if (event.xbutton.window == bm.win)
		{
			if (event.xbutton.button == Button3)
			{
				bm.modlut=0;
				/* reset the color tables? */
				if (abs(event.xbutton.x -bm.mlx)< 10 &&
				    abs(event.xbutton.y - bm.mly) < 10)
				{
					if (bppix == 16) nluts=65536;
					else nluts=256;
					bm.slope=(nluts-1) /
						(double)(bm.colors-1);
					bm.offset=0;
					transluts();
				}
			} else if (event.xbutton.button == Button2) {
				/* it's possible that this has already been */
				/* taken care of, so drawing the line would */
				/* not be a good idea */
				if (slitxs < 0) break;
				/* erase the old line */
				if (xorlinedrawn) XDrawLine(display, bm.win,
					xorgc, slitxs, slitys, slitxe, slitye);
				doslit(display_to_imagecol(slitxs),
					 display_to_imagerow(slitys),
					 display_to_imagecol(event.xbutton.x),
					 display_to_imagerow(event.xbutton.y));
				slitxe=slitxs= -1;
				xorlinedrawn=0;
			} else if (event.xbutton.button == Button1
				   && modluttransoff) {
				modluttransoff=0;
				if (abs(event.xbutton.x - moffx) < 10 &&
				    abs(event.xbutton.y - moffy) < 10)
				{
					luttransoff=0;
				} else {
					if (bppix == 16) luttransoff=
						(65536* (event.xbutton.x -
							(int)bm.width/2))
							/((int)bm.width/2);
					else luttransoff= (256*
						(event.xbutton.x -
							(int)bm.width/2))
							/((int)bm.width/2);
				}
				transluts();
			}
		}
		break;
#endif
	case MotionNotify:
		if (event.xbutton.window == lg.win)
		{
			/* absorb all motion events inside this window */
			px=event.xbutton.x;
			py=event.xbutton.y;
			while(XCheckMaskEvent(display, PointerMotionMask,
				&event2) != False)
			{
				if (event2.xbutton.window != lg.win) break;
				px=event2.xbutton.x;
				py=event2.xbutton.y;
			}
			updatelgtitle(px-lg.winxoff,py-lg.winyoff);
			/* don't go further unless using crosshair cursor */
			if (!res.lgcross)
				break;
			/* update the croshairs cursor */
			if (lgx != -1)
			{
				/* first we clear the old lines */
				XDrawLine(display, lg.win, linegcclear, 0, lgy,
					lg.width-1, lgy);
				XDrawLine(display, lg.win, linegcclear, lgx, 0,
					lgx, lg.height-1);
				/* we know that lgy and lgx are at least */
				/* non-negative */
				if (lg.winyoff < lgy &&
				    lgy-lg.winyoff < lg.imheight)
					XCopyArea(display, lg.pixmap, lg.win,
						linegc, 0, lgy-lg.winyoff,
						lg.width, 1, 0, lgy);
				if (lg.winxoff < lgx &&
				    lgx-lg.winxoff < lg.imwidth)
					XCopyArea(display, lg.pixmap, lg.win,
						linegc, lgx-lg.winxoff, 0, 1,
						lg.height, lgx, 0);
			}
			lgx=px;
			lgy=py;
			XSetForeground(display, linegcclear, lg.pix[1]);
			XDrawLine(display, lg.win, linegcclear, 0, lgy,
				lg.width-1, lgy);
			XDrawLine(display, lg.win, linegcclear, lgx, 0, lgx,
				lg.height-1);
			XSetForeground(display, linegcclear, lg.pix[0]);
		}
#ifndef PGDISP
		if (event.xbutton.window != bm.win) break;
		/* absorb all motion events until the next button press or */
		/* release */
		px=event.xbutton.x;
		py=event.xbutton.y;
		while (XCheckMaskEvent(display,
			ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
			&event2) != False)
		{
			switch(event2.type)
			{
			case ButtonPressMask:	/* press or release ends */
			case ButtonReleaseMask:	/* processing */
				XPutBackEvent(display,&event2);
				break;
			default:
				px=event2.xbutton.x;
				py=event2.xbutton.y;
				continue;	/* the while loop */
				break;
			}
			/* this will break out of the while loop if we got a */
			/* press or release event */
			break;
		}

		if (modluttransoff)
		{
			modluttransoff=1;
			if (bppix == 16) luttransoff= (65536* (event.xbutton.x
				-(int)bm.width/2))/((int)bm.width/2);
			else luttransoff= (256* (event.xbutton.x
				-(int)bm.width/2))/((int)bm.width/2);
			transluts();
		}
		if (bm.modlut)
		{
			double scale;
			if (px < 0) px=0;
			if (px >= bm.width) px=bm.width-1;
			if ((py=bm.height-py-1) <= 0) py=1;
			if (py >= bm.height) py=bm.height-1;
			if (bppix==16) nluts=65536;
			else nluts=256;
			bm.slope=nluts/(1.5*py);
			scale=2*nluts*((double)px/bm.width -0.5);
			bm.offset=(1.0-scale)*bm.slope+(nluts>>1);
			scale=(nluts-1)/(double)(bm.colors-1);
			bm.offset = (bm.offset+0.5)/scale;
			transluts();
			/* Make sure the pointer hasn't been released */
			if (XQueryPointer(display, bm.win, &windum, &windum,
				&dummy, &dummy, &px, &py, &keys) == True
			    && !(keys & Button3Mask))
			{
				bm.modlut=0;
				/* reset the color tables? */
				if (abs(event.xbutton.x -bm.mlx)< 10 &&
				    abs(event.xbutton.y - bm.mly) < 10)
				{
					if (bppix == 16) nluts=65536;
					else nluts=256;
					bm.slope=(nluts-1) /
						(double)(bm.colors-1);
					bm.offset=0;
					transluts();
				}
			}
		} else updatetitle(px,py,0);
		if (slitxs > 0)
		{
			/* erase old line */
			if (xorlinedrawn) XDrawLine(display, bm.win, xorgc,
				slitxs, slitys, slitxe, slitye);
			slitxe=px;
			slitye=py;
			if (XQueryPointer(display, bm.win, &windum, &windum,
				&dummy, &dummy, &px, &py, &keys) == True
			    && !(keys & Button2Mask))
			{ /* the button was released, so handle it */
				doslit(display_to_imagecol(slitxs),
					 display_to_imagerow(slitys),
					 display_to_imagecol(slitxe),
					 display_to_imagerow(slitye));
				slitxe=slitxs= -1;
				xorlinedrawn=0;
			} else {
				/* draw a new line */
				XDrawLine(display, bm.win, xorgc, slitxs,
					slitys, slitxe, slitye);
				xorlinedrawn=1;
			}
		}
#endif
		break;
	case ButtonPress:	/* currently used only for cursor */
#ifndef PGDISP
		if (event.xbutton.window == bm.win)
		{
			if (event.xbutton.button == Button3)
			{
				if (mousemode == 0)
				{ /* color map */
					bm.modlut=1;
					bm.mlx=event.xbutton.x;
					bm.mly=event.xbutton.y;
				} else { /* box lower right */
					lr_x = display_to_imagecol(
						event.xbutton.x);
					lr_y = display_to_imagerow(
						event.xbutton.y);
					do_box();
				}
			} else if (event.xbutton.button == Button2){
				if (slitxs >= 0) break;
				xorlinedrawn=0;
				slitxs=event.xbutton.x;
				slitys=event.xbutton.y;
			} else if (event.xbutton.button == Button1) {
				if (mousemode == 0)
				{ /* new center */
					if (event.xbutton.state & ControlMask)
					{
						modluttransoff=1;
						moffx=event.xbutton.x;
						moffy=event.xbutton.y;
						if (bppix == 16) luttransoff=
							(65536*(event.xbutton.x
							- (int)bm.width/2))
							/((int)bm.width/2);
						else luttransoff= (256*
							(event.xbutton.x
							-(int)bm.width/2))
							/((int)bm.width/2);
						transluts();
					} else transim(event.xbutton);
				} else { /* box upper left */
					ul_x = display_to_imagecol(
						event.xbutton.x);
					ul_y = display_to_imagerow(
						event.xbutton.y);
					do_box();
				}
			}
			break;
		} else if (event.xbutton.window == patch.win) {
			panpatch(event.xbutton);
			break;
		} else if (event.xbutton.window == loc.win) {
			panloc(event.xbutton);
			break;
		}
#endif
	case KeyPress:
#ifndef PGDISP
		/* we could have fallen through */
		if (event.type == KeyPress)
		{
			if ((*go_on=dokey(event.xkey)) == 1) break;
			if (*go_on == 0) return(SUCCEED);
		}
#endif
		/* record this keypress for return to a user program if the */
		/* program is trying to get the cursor location */
		if (selset && (event.xany.window == lg.win && pgcursor(event)
#ifndef PGDISP
			|| event.xany.window == bm.win && bmcursor(event)
#endif
			))
		{
			if (retbuflen && (ntohs(retbuf[0]) == LG_CURS &&
				pggcurs(&retbuf[0])
#ifndef PGDISP
			    || ntohs(retbuf[0]) == BM_GET_CURS &&
				bmgcurs(&retbuf[0])
#endif
			    ))
			{ /* the user's asked for one */
				returnbuf(&retbuf[0],4,srcwin);
				retbuflen=0;
			}
		}
		break;
	}

	return(SUCCEED);
}