File: selection.c

package info (click to toggle)
xcircuit 2.0a6-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,960 kB
  • ctags: 3,202
  • sloc: ansic: 30,529; makefile: 51; sh: 2
file content (788 lines) | stat: -rw-r--r-- 24,483 bytes parent folder | download
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/*-------------------------------------------------------------------------*/
/* selection.c --- xcircuit routines handling element selection etc.	   */
/* Copyright (c) 1998  Tim Edwards, Johns Hopkins University       	   */
/*-------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------*/
/*      written by Tim Edwards, 8/13/93    				   */
/*-------------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <math.h>

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include "Xw/Xw.h"

/*-------------------------------------------------------------------------*/
/* Local includes							   */
/*-------------------------------------------------------------------------*/

#include "cursors.h"
#include "colordefs.h"
#include "xcircuit.h"

/*-------------------------------------------------------------------------*/
/* Global Variable definitions						   */
/*-------------------------------------------------------------------------*/

extern short eventmode;	/* keep track of the mode the screen is in */
extern Display	*dpy;   /* Works well to make this globally accessible */
extern Window win;
extern int *appcolors;
extern Cursor	appcursors[NUM_CURSORS];
extern Clientdata areastruct;
extern char _STR[150];
extern short attachto;
extern long finddist();

#define RADFAC 0.0174532925199   /* pi / 180 */

/*------------------------------------------------------------------------*/
/* Declarations of functions defined in selection.c	 	  	  */
/*------------------------------------------------------------------------*/

short *objectselect();
XtEventHandler trackselarea();

/*----------------------------------------------------------------------------*/
/* Look at select stack to see if there are any selects; call select, if not. */
/*----------------------------------------------------------------------------*/

Boolean checkselect(value)
  short value;
{
   short *check;

   if (areastruct.selects == 0) objectselect(value);
   if (areastruct.selects == 0) return False;
   for (check = areastruct.selectlist; check < areastruct.selectlist +
	areastruct.selects; check++)
      if (SELECTTYPE(check) & value) break;
   if (check == areastruct.selectlist + areastruct.selects) return False;
   else return True;
}

/*----------------------*/
/* Draw a selected item */
/*----------------------*/

void easydraw(instance, mode) 
  short instance;
  short mode;
{
   genericptr elementptr = *(objectdata->plist + instance);

   switch (SELECTTYPE(&instance)) {
      case OBJECT:
         UDrawObject((objinstptr)elementptr, 
	    ((objinstptr)elementptr)->thisobject, SINGLE, mode);
         break;
      case LABEL:
         UDrawString(areastruct.topobject, (labelptr)elementptr);
	 break;
      case ARC:
         UDrawArc(areastruct.topobject, (arcptr)elementptr);
	 break;
      case POLYGON:
         UDrawPolygon(areastruct.topobject, (polyptr)elementptr);
	 break;
      case SPLINE:
	 UDrawSpline(areastruct.topobject, (splineptr)elementptr);
	 break;
      case PATH:
	 UDrawPath(areastruct.topobject, (pathptr)elementptr);
   }
}

/*-------------------------------------------------*/
/* Draw a selected item, including selection color */
/*-------------------------------------------------*/

void drawselected(newselect)
  short *newselect;
{
   XSetFunction(dpy, areastruct.gc, GXcopy);
   XSetForeground(dpy, areastruct.gc, BACKGROUND);
   easydraw(*newselect, DOFORALL);
   XSetFunction(dpy, areastruct.gc, GXxor);
   XSetForeground(dpy, areastruct.gc, SELECTCOLOR ^ BACKGROUND);
   easydraw(*newselect, DOFORALL);
}

/*---------------------------------------------------*/
/* Allocate or reallocate memory for a new selection */
/*---------------------------------------------------*/

short *allocselect()
{
   short *newselect;

   if (areastruct.selects == 0)
      areastruct.selectlist = (short *) malloc(sizeof(short));
   else
      areastruct.selectlist = (short *) realloc(areastruct.selectlist,
	 (areastruct.selects + 1) * sizeof(short));

   newselect = areastruct.selectlist + areastruct.selects;
   areastruct.selects++;

   return newselect;
}

/*-------------------------------------------------*/
/* Set Options menu according to 1st selection	   */
/*-------------------------------------------------*/

setoptionmenu()
{
   short      *mselect;
   labelptr   mlabel;

   if (areastruct.selects == 0) {
      setallstylemarks(areastruct.style);
      setcolormark(areastruct.color);
      setdefaultfontmarks();
      return;
   }

   for (mselect = areastruct.selectlist; mselect < areastruct.selectlist +
	areastruct.selects; mselect++) {
      setcolormark(SELTOCOLOR(mselect));
      switch(SELECTTYPE(mselect)) {
	 case ARC:
	    setallstylemarks(SELTOARC(mselect)->style);
	    return;
	 case POLYGON:
	    setallstylemarks(SELTOPOLY(mselect)->style);
	    return;
	 case SPLINE:
            setallstylemarks(SELTOSPLINE(mselect)->style);
	    return;
	 case PATH:
            setallstylemarks(SELTOPATH(mselect)->style);
	    return;
	 case LABEL:
	    mlabel = SELTOLABEL(mselect);
	    setfontmarks((unsigned short)(*(mlabel->string + 1)) - FONT_START,
		mlabel->justify);
	    return;
      }
   }
}

/*--------------------------------------------*/
/* Search for selection among path components */
/*--------------------------------------------*/

Boolean pathselect(curgen, class)
  genericptr *curgen;
  short class;
{
   /*----------------------------------------------------------------------*/
   /* wirelim is the distance, in user-space units, at which an element is */
   /* considered for selection.						   */
   /*									   */
   /* wirelim = A + B / (scale + C)					   */
   /*									   */
   /* where A = minimum possible distance (expands range at close scale)   */
   /*       C = minimum possible scale    (contract range at far scale)	   */
   /*	    B   makes wirelim approx. 25 at default scale of 0.5, which	   */
   /*		is an empirical result.					   */
   /*----------------------------------------------------------------------*/

   float	wirelim = 2 + 11.5 / (*areastruct.vscale + 0.05);
   long		sqrwirelim = (int)(wirelim * wirelim);

   long		newdist;
   Boolean	selected = False;

   if ((*curgen)->type == (class & ARC)) {

      /* look among the arcs */

      fpointlist currentpt;
      XPoint nearpt[3];

      nearpt[2].x = nearpt[0].x = (short)(TOARC(curgen)->points[0].x);
      nearpt[2].y = nearpt[0].y = (short)(TOARC(curgen)->points[0].y);
      for (currentpt = TOARC(curgen)->points + 1; currentpt < TOARC(curgen)->points
              + TOARC(curgen)->number; currentpt++) {
	 nearpt[1].x = nearpt[0].x;
	 nearpt[1].y = nearpt[0].y;
	 nearpt[0].x = (short)(currentpt->x);
	 nearpt[0].y = (short)(currentpt->y);
	 newdist = finddist(&nearpt[0], &nearpt[1], &areastruct.save);
         if (newdist <= sqrwirelim) break;
      }
      if ((!(TOARC(curgen)->style & UNCLOSED)) && (newdist > sqrwirelim))
	 newdist = finddist(&nearpt[0], &nearpt[2], &areastruct.save);

      if (newdist <= sqrwirelim) selected = True;
   }

   else if ((*curgen)->type == (class & SPLINE)) {

      /* look among the splines --- look at polygon representation */

      fpointlist currentpt;
      XPoint nearpt[2];

      nearpt[0].x = (short)(TOSPLINE(curgen)->points[0].x);
      nearpt[0].y = (short)(TOSPLINE(curgen)->points[0].y);
      newdist = finddist(&(TOSPLINE(curgen)->ctrl[0]), &(nearpt[0]),
		   &areastruct.save);
      if (newdist > sqrwirelim) {
         for (currentpt = TOSPLINE(curgen)->points; currentpt <
		  TOSPLINE(curgen)->points + INTSEGS; currentpt++) {
	    nearpt[1].x = nearpt[0].x;
	    nearpt[1].y = nearpt[0].y;
	    nearpt[0].x = (short)(currentpt->x);
	    nearpt[0].y = (short)(currentpt->y);
	    newdist = finddist(&nearpt[0], &nearpt[1], &areastruct.save);
            if (newdist <= sqrwirelim) break;
	 }
	 if (newdist > sqrwirelim) {
	    newdist = finddist(&nearpt[0], &(TOSPLINE(curgen)->ctrl[3]),
			&areastruct.save);
            if ((!(TOSPLINE(curgen)->style & UNCLOSED)) && (newdist > sqrwirelim))
	       newdist = finddist(&(TOSPLINE(curgen)->ctrl[0]),
		     &(TOSPLINE(curgen)->ctrl[3]), &areastruct.save);
	 }
      }

      if (newdist <= sqrwirelim) selected = True;
   }

   else if ((*curgen)->type == (class & POLYGON)) {

      /* finally, look among the polygons */

      pointlist currentpt;

      for (currentpt = TOPOLY(curgen)->points; currentpt < TOPOLY(curgen)
		->points + TOPOLY(curgen)->number - 1; currentpt++) {
	 newdist = finddist(currentpt, currentpt + 1, &areastruct.save);
	 if (newdist <= sqrwirelim) break;
      }
      if ((!(TOPOLY(curgen)->style & UNCLOSED)) && (newdist > sqrwirelim))
	 newdist = finddist(currentpt, TOPOLY(curgen)->points,
		&areastruct.save);

      if (newdist <= sqrwirelim) selected = True;
   }
   return selected;
}

/*--------------------------------------*/
/* Select one of the elements on-screen */
/*--------------------------------------*/

short *objectselect(class)
  short 	class;
{
   short	*newselect, *foundtmp, *foundlist;

   genericptr	*curgen;

   XPoint 	bboxpts[4], newboxpts[4];
   XEvent 	event;
   XButtonEvent *buttonevent = (XButtonEvent *)(&event);
   Boolean	selected, unselect = False;
   short	stval, i, j, found = 0;
   long		newdist;
   Arg		wargs[2];

   XDefineCursor(dpy, win, QUESTION);

   /* Definition for unselecting an element */

   if (class < 0) {
      unselect = True;
      class = -class;
   }

   /* Loop through all elements found underneath the cursor */

   for (curgen = objectdata->plist; curgen < ENDPART; curgen++) {

      /* when selection element to attach to, don't select the element */
      /* to be attached. . .					       */

      if (attachto == 1 && eventmode != COPY2_MODE && eventmode !=
	    PRESS_MODE && curgen == EDITPART) continue;

      selected = False;

      /* check for position inside the bounding box of one of the objects */

      if ((*curgen)->type == (class & OBJECT)) {

#ifdef SCHEMA
#define lowerleft lleft2
#define width width2
#define height height2
#endif

         bboxpts[0].x = bboxpts[1].x = TOOBJINST(curgen)->thisobject->lowerleft.x;
         bboxpts[2].x = bboxpts[3].x = TOOBJINST(curgen)->thisobject->lowerleft.x + 
	    TOOBJINST(curgen)->thisobject->width;
         bboxpts[0].y = bboxpts[3].y = TOOBJINST(curgen)->thisobject->lowerleft.y;
         bboxpts[1].y = bboxpts[2].y = TOOBJINST(curgen)->thisobject->lowerleft.y + 
	    TOOBJINST(curgen)->thisobject->height;
         UTransformPoints(bboxpts, newboxpts, 4, TOOBJINST(curgen)->position,
		TOOBJINST(curgen)->scale, TOOBJINST(curgen)->rotation);

#ifdef SCHEMA
#undef lowerleft
#undef width
#undef height
#endif
         /* Look for an intersect of the boundingbox and pointer position. */
         /* This is a simple matter of rotating the pointer position with  */
         /*  respect to the origin of the bounding box segment, as if the  */
         /*  segment were rotated to 0 degrees.  The sign of the resulting */
         /*  point's y-position is the same for all bbox segments if the   */
         /*  pointer position is inside the bounding box.		   */

         stval = sign((newboxpts[0].x - newboxpts[3].x) * (areastruct.save.y - 
	    newboxpts[3].y) - (newboxpts[0].y - newboxpts[3].y) * (areastruct.save.x 
	    - newboxpts[3].x));

         for (j = 1; j < 4; j++) {
	    stval += sign((newboxpts[j].x - newboxpts[j - 1].x) * (areastruct.save.y
	    - newboxpts[j - 1].y) - (newboxpts[j].y - newboxpts[j - 1].y) * 
	    (areastruct.save.x - newboxpts[j - 1].x));
         }

         if (abs(stval) == 4) selected = True;
      }

      else if ((*curgen)->type == (class & LABEL)) {

         /* now look among the labels */

	 short tmplength = ULength(TOLABEL(curgen)->string, 0.0, 0);

         bboxpts[0].x = bboxpts[1].x = (TOLABEL(curgen)->justify & NOTLEFT ?
	    (TOLABEL(curgen)->justify & RIGHT ? -tmplength : -tmplength / 2) : 0);
	 bboxpts[2].x = bboxpts[3].x = bboxpts[0].x + tmplength;
         bboxpts[0].y = bboxpts[3].y = (TOLABEL(curgen)->justify & NOTBOTTOM ?
	    (TOLABEL(curgen)->justify & TOP ? -TEXTHEIGHT : -HALFHEIGHT) : 0);
	 bboxpts[1].y = bboxpts[2].y = bboxpts[0].y + TEXTHEIGHT;

#ifdef SCHEMA
	 if (TOLABEL(curgen)->pin) {
	     int i;
	     for (i = 0; i < 4; i++) 
		pinadjust(TOLABEL(curgen)->justify, &(bboxpts[i].x),
			&(bboxpts[i].y));
	 }
#endif

         UTransformPoints(bboxpts, newboxpts, 4, TOLABEL(curgen)->position,
		TOLABEL(curgen)->scale, TOLABEL(curgen)->rotation);

         /* check for point inside bounding box, as for objects */

         stval = sign((newboxpts[0].x - newboxpts[3].x) * (areastruct.save.y -
            newboxpts[3].y) - (newboxpts[0].y - newboxpts[3].y) * (areastruct.save.x 
	    - newboxpts[3].x));

         for (j = 1; j < 4; j++)
            stval += sign((newboxpts[j].x - newboxpts[j - 1].x) * (areastruct.save.y
            - newboxpts[j - 1].y) - (newboxpts[j].y - newboxpts[j - 1].y) *
            (areastruct.save.x - newboxpts[j - 1].x));

         if (abs(stval) == 4) selected = True;
      }


      else if ((*curgen)->type == (class & PATH)) {

         /* Check among the paths */

	 genericptr *pathp;
	 Boolean lselect;

	 /* Accept path if any subcomponent of the path is accepted */

 	 for (pathp = TOPATH(curgen)->plist; pathp < TOPATH(curgen)->plist +
		TOPATH(curgen)->parts; pathp++)
	    if (pathselect(pathp, SPLINE|ARC|POLYGON)) selected = True;
      }

      /* Check among polygons, arcs, and curves */

      else selected = pathselect(curgen, class);

      /* check if this object has already been selected */

      for (newselect = areastruct.selectlist; newselect <
             areastruct.selectlist + areastruct.selects; newselect++) 
         if (*newselect == (short)(curgen - objectdata->plist)) {
	    if (!unselect) selected = False;
	    break;
	 }

      if (unselect && (newselect == areastruct.selectlist + areastruct.selects))
	 selected = False;

      /* Add this object to the list of things found under the cursor */

      if (selected && (!unselect)) {

         if (found == 0)
            foundlist = (short *) malloc(sizeof(short));
         else
            foundlist = (short *) realloc(foundlist, (found + 1) * sizeof(short));

         newselect = foundlist + found;
         found++;
     
         if (selected) *newselect = (short)(curgen - objectdata->plist);
      }

      /* mechanism for unselecting elements under the cursor */

      else if (selected) {
         short *desel;

         XSetFunction(dpy, areastruct.gc, GXcopy);
         XTopSetForeground(SELTOCOLOR(newselect));
         easydraw(*newselect, DEFAULTCOLOR);
         areastruct.selects--;
         for (desel = newselect; desel < areastruct.selectlist +
	        areastruct.selects; desel++)
	    *desel = *(desel + 1);
         if (areastruct.selects == 0) free (areastruct.selectlist);
      }
      else newselect = NULL;

   } /* for-loop over all parts */

   /* Various options depending on how many things were found */

   switch (found) {
      case 0: 
	 newselect = NULL;
	 break;

      case 1: 
	 newselect = allocselect();
	 *newselect = *foundlist;
	 free(foundlist);
	 drawselected(newselect);
	 break;

      default: {
	 XSetFunction(dpy, areastruct.gc, GXcopy);
	 for (foundtmp = foundlist; foundtmp < foundlist + found; foundtmp++) {
            XSetForeground(dpy, areastruct.gc, QUERYCOLOR);
            easydraw(*foundtmp, DOFORALL);
	    sprintf(_STR, "Click to accept/reject: %d of %d",
		(int)(foundtmp - foundlist + 1), found);
	    Wprintf(_STR);

	    for(;;) {  	/* This flushes the X event queue */
	       XNextEvent(dpy, &event); 
	       if (event.type != ButtonPress && event.type != ButtonRelease
		    && event.type != MotionNotify && event.type != KeyPress)
	          XtDispatchEvent(&event);
	       else if (event.type == ButtonPress) break;
	    }
	    if (buttonevent->button == Button3) {
               XTopSetForeground(SELTOCOLOR(foundtmp));
               easydraw(*foundtmp, DEFAULTCOLOR);
	    }
	    else {
	       newselect = allocselect();
               *newselect = *foundtmp;
               XSetForeground(dpy, areastruct.gc, SELECTCOLOR);
               easydraw(*newselect, DOFORALL);
	    }
         }
      }  break;
   }

   if (found > 1) {
      short *nselect;

      XDefineCursor(dpy, win, CIRCLE);
      if (eventmode == PRESS_MODE)
	 Wprintf("Click and hold button 1 to continue move.");
      else
	 Wprintf("Selection complete");

      for(;;) {
	 XNextEvent(dpy, &event); 
	 if (event.type != ButtonPress && event.type != ButtonRelease &&
		event.type != MotionNotify && event.type != KeyPress)
	    XtDispatchEvent(&event);
	 else if ((event.type == ButtonPress) && (eventmode == PRESS_MODE))
	    break;
	 else if ((event.type == ButtonRelease) && (eventmode != PRESS_MODE))
	    break;
      }
      free(foundlist);

      /* make it work right for smooth xor'ing */

      XSetFunction(dpy, areastruct.gc, GXcopy);
      XSetForeground(dpy, areastruct.gc, BACKGROUND);
      for (nselect = areastruct.selectlist; nselect < areastruct.selectlist
	   + areastruct.selects; nselect++)
         easydraw(*nselect, DOFORALL);
      XSetFunction(dpy, areastruct.gc, GXxor);
      XSetForeground(dpy, areastruct.gc, SELECTCOLOR ^ BACKGROUND);
      for (nselect = areastruct.selectlist; nselect < areastruct.selectlist
	   + areastruct.selects; nselect++)
         easydraw(*nselect, DOFORALL);
   }

   setoptionmenu();

   if (eventmode == DELETE_MODE)
      XDefineCursor(dpy, win, SCISSORS);
   else
      XDefineCursor(dpy, win, CROSS);

   /* snap the coordinates */

   u2u_snap(&areastruct.save);
   return newselect;
}

/*----------------------------------------------------------------*/
/* select arc, curve, and polygon objects from a defined box area */
/*----------------------------------------------------------------*/

Boolean areaelement(curgen)
  genericptr *curgen;
{
   Boolean selected;
   pointlist    currentpt;

   switch((*curgen)->type) {

      case(ARC):
	   /* check center of arcs */

      	   selected = (TOARC(curgen)->position.x < areastruct.save.x) &&
	  	(TOARC(curgen)->position.x > areastruct.origin.x) &&
		(TOARC(curgen)->position.y < areastruct.save.y) &&
		(TOARC(curgen)->position.y > areastruct.origin.y);
	   break;

      case(POLYGON):
	   /* check each point of the polygons */

	   selected = False;
           for (currentpt = TOPOLY(curgen)->points; currentpt <
		TOPOLY(curgen)->points + TOPOLY(curgen)->number; currentpt++) {
              if ((currentpt->x < areastruct.save.x) && (currentpt->x >
	           areastruct.origin.x) && (currentpt->y < areastruct.save.y) &&
	           (currentpt->y > areastruct.origin.y)) {
	         selected = True;
		 break;
	      }
           }
	   break;

      case(SPLINE):
	   /* check each control point of the spline */
	
           selected = ((TOSPLINE(curgen)->ctrl[0].x < areastruct.save.x) && 
	   	(TOSPLINE(curgen)->ctrl[0].x > areastruct.origin.x) &&
		(TOSPLINE(curgen)->ctrl[0].y < areastruct.save.y) &&
		(TOSPLINE(curgen)->ctrl[0].y > areastruct.origin.y))
		|| ((TOSPLINE(curgen)->ctrl[3].x < areastruct.save.x) &&
		(TOSPLINE(curgen)->ctrl[3].x > areastruct.origin.x) &&
		(TOSPLINE(curgen)->ctrl[3].y < areastruct.save.y) &&
		(TOSPLINE(curgen)->ctrl[3].y > areastruct.origin.y));
	   break;
   }
   return selected;
}

/*--------------------------------------------*/
/* select all objects from a defined box area */
/*--------------------------------------------*/

void selectarea()
{
   short	*newselect;
   genericptr   *curgen, *pathgen;
   Boolean	selected;

   /* put points of area bounding box into proper order */

   if (areastruct.origin.y > areastruct.save.y) {
      short tmp;
      tmp = areastruct.origin.y;
      areastruct.origin.y = areastruct.save.y;
      areastruct.save.y = tmp;
   }
   if (areastruct.origin.x > areastruct.save.x) {
      short tmp;
      tmp = areastruct.origin.x;
      areastruct.origin.x = areastruct.save.x;
      areastruct.save.x = tmp;
   }
 
   for (curgen = objectdata->plist; curgen < objectdata->plist
	+ objectdata->parts; curgen++) {

      switch((*curgen)->type) {
	 case(OBJECT):
            /* check for instance of object inside area box */
            selected = (TOOBJINST(curgen)->position.x < areastruct.save.x) &&
		(TOOBJINST(curgen)->position.x > areastruct.origin.x) &&
		(TOOBJINST(curgen)->position.y < areastruct.save.y) &&
		(TOOBJINST(curgen)->position.y > areastruct.origin.y);
	    break;

        case(LABEL):
   	   /* check position point of each label */

      	   selected = (TOLABEL(curgen)->position.x < areastruct.save.x) &&
		(TOLABEL(curgen)->position.x > areastruct.origin.x) &&
		(TOLABEL(curgen)->position.y < areastruct.save.y) &&
		(TOLABEL(curgen)->position.y > areastruct.origin.y);
	   break;

	case(PATH):
	   /* check position point of each subpart of the path */

	   selected = False;
	   for (pathgen = TOPATH(curgen)->plist; pathgen < TOPATH(curgen)->plist
		  + TOPATH(curgen)->parts; pathgen++) {
	      if (areaelement(pathgen)) selected = True;
	   }
	   break;

	default:
	   selected = areaelement(curgen);
	   break;
      }

      /* check if this part has already been selected */

      if (selected)
         for (newselect = areastruct.selectlist; newselect <
              areastruct.selectlist + areastruct.selects; newselect++)
            if (*newselect == (short)(curgen - objectdata->plist))
                  selected = False;
	
      /* add to list of selections */

      if (selected) {
         newselect = allocselect();
         *newselect = (short)(curgen - objectdata->plist);
      }
   }
   setoptionmenu();

   /* Drawing of selected objects will take place when drawarea() is */
   /* executed after the button release.			     */
}

/*------------------------*/
/* start deselection mode */
/*------------------------*/

void startdesel(w, clientdata, nulldata)
  Widget w;
  caddr_t clientdata, nulldata;
{
   if (eventmode == NORMAL_MODE) {
      eventmode = DESEL_MODE;
      Wprintf("Click on element to deselect");
   }
}

/*------------------------------------------*/
/* deselect all objects on the select stack */
/*------------------------------------------*/

void deselect(w, clientdata, calldata)
  Widget w;
  caddr_t clientdata;
  caddr_t calldata;
{
   objectselect(-ANY);

   /* return Option menu to default state */

   setoptionmenu();
}

void objectdeselect()
{
   short *lastselect;

   if (areastruct.selects == 0) return;

   /* reset the graphics state */

   XSetFunction(dpy, areastruct.gc, GXcopy);

   for (lastselect = areastruct.selectlist; lastselect < areastruct.selectlist 
	+ areastruct.selects; lastselect++) {
      XTopSetForeground(SELTOCOLOR(lastselect)); 
      easydraw(*lastselect, SELTOCOLOR(lastselect));
   }

   areastruct.selects = 0;
   free(areastruct.selectlist);

   setallstylemarks(areastruct.style);
   setcolormark(areastruct.color);
   setdefaultfontmarks();
}

/*----------------------------------*/
/* Start drawing a select area box. */
/*----------------------------------*/

XtTimerCallbackProc startselect(id, clientdata)
  XtIntervalId  id;
  caddr_t       clientdata;
{
   if (eventmode == NORMAL_MODE) eventmode = SELAREA_MODE;
   areastruct.origin.x = areastruct.save.x;
   areastruct.origin.y = areastruct.save.y;
   UDrawBox(areastruct.origin, areastruct.save);
   XtAddEventHandler(areastruct.area, ButtonMotionMask, False, 
	(XtEventHandler)trackselarea, NULL);
}

/*-------------------------*/
/* Track a select area box */
/*-------------------------*/

XtEventHandler trackselarea(w, clientdata, event)
  Widget w;
  XtPointer clientdata;
  XMotionEvent *event;
{
   Window nullwin;
   int    nullint, xpos, ypos;
   XPoint       newpos;
   unsigned int   nullui;

   XQueryPointer(dpy, win, &nullwin, &nullwin, &nullint, &nullint, &xpos, &ypos,
	&nullui);
   snap(xpos, ypos, &newpos);
   if (newpos.x == areastruct.save.x && newpos.y == areastruct.save.y) return;

   UDrawBox(areastruct.origin, areastruct.save);
   UDrawBox(areastruct.origin, newpos);

   areastruct.save.x = newpos.x;
   areastruct.save.y = newpos.y;
}

/*-------------------------------------------------------------------------*/