File: selection.c

package info (click to toggle)
xcircuit 2.5.3rev0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,292 kB
  • ctags: 3,497
  • sloc: ansic: 41,848; sh: 2,741; python: 473; makefile: 165
file content (933 lines) | stat: -rw-r--r-- 29,572 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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
/*-------------------------------------------------------------------------*/
/* selection.c --- xcircuit routines handling element selection etc.	   */
/* Copyright (c) 2002  Tim Edwards, Johns Hopkins University       	   */
/*-------------------------------------------------------------------------*/

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(DARWIN)
#include <sys/malloc.h>
#else
#include <malloc.h>
#endif
#include <math.h>

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

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

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

/*----------------------------------------------------------------------*/
/* Function prototype declarations                                      */
/*----------------------------------------------------------------------*/
#include "prototypes.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 short textpos, textend;
extern objectpair *pushlist;

/*----------------------------------------------------------------------*/
/* Change filter to determine what types can be selected		*/
/*----------------------------------------------------------------------*/

void selectfilter(Widget w, caddr_t value, caddr_t calldata)
{
   short bitwise = (int) value & 0xffff;
   Boolean bval = (areastruct.filter & bitwise) ? True : False;

   if (bval)
      areastruct.filter &= ~bitwise;
   else
      areastruct.filter |= bitwise;

   toggle(w, &bval, calldata);
}

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

Boolean checkselect(short value)
{
   short *check;
 
   value &= areastruct.filter;	/* apply the selection filter */

   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 geneasydraw(short instance, short mode, objectptr curobj, objinstptr curinst)
{
   genericptr elementptr = *(curobj->plist + instance);

   switch ((*(curobj->plist + instance))->type) {
      case ARC:
         UDrawArc(curinst, (arcptr)elementptr);
	 break;
      case POLYGON:
         UDrawPolygon(curinst, (polyptr)elementptr);
	 break;
      case SPLINE:
	 UDrawSpline(curinst, (splineptr)elementptr);
	 break;
      case PATH:
	 UDrawPath(curinst, (pathptr)elementptr);
	 break;
      case LABEL:
         UDrawString(curinst, (labelptr)elementptr, mode);
	 break;
      case OBJECT:
         UDrawObject((objinstptr)elementptr, 
	    ((objinstptr)elementptr)->thisobject, SINGLE, mode);
         break;
   }
}

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

void gendrawselected(short *newselect, objectptr curobj, objinstptr curinst)
{
   XSetFunction(dpy, areastruct.gc, GXcopy);
   XSetForeground(dpy, areastruct.gc, BACKGROUND);
   geneasydraw(*newselect, DOFORALL, curobj, curinst);
   indicateparams(*(curobj->plist + *newselect));

   XSetFunction(dpy, areastruct.gc, GXxor);
   XSetForeground(dpy, areastruct.gc, SELECTCOLOR ^ BACKGROUND);
   geneasydraw(*newselect, DOFORALL, curobj, curinst);

   XSetForeground(dpy, areastruct.gc, PARAMCOLOR ^ BACKGROUND);
   indicateparams(*(curobj->plist + *newselect));
}

/*---------------------------------------------------*/
/* 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	   */
/*-------------------------------------------------*/

void setoptionmenu()
{
   short      *mselect;
   labelptr   mlabel;

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

   for (mselect = areastruct.selectlist; mselect < areastruct.selectlist +
	areastruct.selects; mselect++) {
      setcolormark(SELTOCOLOR(mselect));
      setparammarks(SELTOGENERIC(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(mlabel->string->data.font, mlabel->justify);
	    return;
      }
   }
}

/*-------------------------------------*/
/* Test of point being inside of a box */
/*-------------------------------------*/

int test_insideness(int tx, int ty, XPoint *boxpoints)
{
   int i, stval = 0; 
   XPoint *pt1, *pt2;
   int stdir;   

   for (i = 0; i < 4; i++) {
      pt1 = boxpoints + i;
      pt2 = boxpoints + ((i + 1) % 4);
      stdir = (pt2->x - pt1->x) * (ty - pt1->y)
		- (pt2->y - pt1->y) * (tx - pt1->x);
      stval += sign(stdir);
   }
   return (abs(stval) == 4) ? 1 : 0;
}

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

Boolean pathselect(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;

   class &= areastruct.filter;	/* apply the selection filter */

   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 *selectobject(short class, objectptr selobj, objinstptr selinst)
{
   short	*newselect, *foundtmp, *foundlist;

   genericptr	*curgen;
   XPoint 	newboxpts[4];
   XEvent 	event;
   XButtonEvent *buttonevent = (XButtonEvent *)(&event);
   Boolean	selected, unselect = False;
   short	found = 0;

   XDefineCursor(dpy, win, QUESTION);

   /* Definition for unselecting an element */

   if (class < 0) {
      unselect = True;
      class = -class;
   }
   class &= areastruct.filter;	/* apply the selection filter */

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

   for (curgen = selobj->plist; curgen < selobj->plist + selobj->parts; 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)) {

	 objinstbbox(TOOBJINST(curgen), newboxpts);

         /* 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.		   */

	 selected = test_insideness(areastruct.save.x, areastruct.save.y,
		newboxpts);
      }

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

         /* Look among the labels */

	 labelptr curlab = TOLABEL(curgen);

#ifdef SCHEMA
	 /* Don't select temporary labels from schematic capture system */
	 if (curlab->string->type != FONT_NAME) continue;
#endif

	 labelbbox(curlab, newboxpts, selinst);

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

	 selected = test_insideness(areastruct.save.x, areastruct.save.y,
		newboxpts);
	 if (selected) textpos = textend = 0;
      }


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

         /* Check among the paths */

	 genericptr *pathp;

	 /* Accept path if any subcomponent of the path is accepted */
	 /* Record which part was selected in the "subeditpart" variable */

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

      /* 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 - selobj->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 - selobj->plist);
      }

      /* mechanism for unselecting elements under the cursor */

      else if (selected) {
         short *desel;

         XSetFunction(dpy, areastruct.gc, GXcopy);
         XTopSetForeground(GSELTOCOLOR(selobj, newselect));
         geneasydraw(*newselect, DEFAULTCOLOR, selobj, selinst);
         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);
	 gendrawselected(newselect, selobj, selinst);
	 break;

      default: {
	 newselect = NULL;
	 XSetFunction(dpy, areastruct.gc, GXcopy);
	 for (foundtmp = foundlist; foundtmp < foundlist + found; foundtmp++) {
            XSetForeground(dpy, areastruct.gc, QUERYCOLOR);
            geneasydraw(*foundtmp, DOFORALL, selobj, selinst);
	    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(GSELTOCOLOR(selobj, foundtmp));
               geneasydraw(*foundtmp, DEFAULTCOLOR, selobj, selinst);
	    }
	    else {
	       newselect = allocselect();
               *newselect = *foundtmp;
               XSetForeground(dpy, areastruct.gc, SELECTCOLOR);
               geneasydraw(*newselect, DOFORALL, selobj, selinst);
	    }
         }
      }  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++)
         geneasydraw(*nselect, DOFORALL, selobj, selinst);
      XSetFunction(dpy, areastruct.gc, GXxor);
      XSetForeground(dpy, areastruct.gc, SELECTCOLOR ^ BACKGROUND);
      for (nselect = areastruct.selectlist; nselect < areastruct.selectlist
	   + areastruct.selects; nselect++)
         geneasydraw(*nselect, DOFORALL, selobj, selinst);
   }

   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(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;
   stringpart	*strptr;
   int		locpos;

   /* 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;
   }

   textpos = textend = 0;
   for (curgen = objectdata->plist; curgen < objectdata->plist
	+ objectdata->parts; curgen++) {

      /* apply the selection filter */
      if (!((*curgen)->type & areastruct.filter)) continue;

      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): {
	   XPoint bboxpts[4], newboxpts[4], adj;
	   labelptr slab = TOLABEL(curgen);
	   short j, state, isect, tmpl1, tmpl2;
	   TextExtents tmpext;

	   selected = False;

	   /* create a 4-point polygon out of the select box information */
	   bboxpts[0].x = bboxpts[3].x = areastruct.origin.x;
	   bboxpts[0].y = bboxpts[1].y = areastruct.origin.y;
	   bboxpts[1].x = bboxpts[2].x = areastruct.save.x;
	   bboxpts[2].y = bboxpts[3].y = areastruct.save.y;

   	   /* translate select box into the coordinate system of the label */
	   InvTransformPoints(bboxpts, newboxpts, 4, slab->position,
		  slab->scale, slab->rotation);

#ifdef SCHEMA
	   if (slab->pin) {
	      if (!areastruct.schemon) continue;
	      for (j = 0; j < 4; j++) 
		 pinadjust(slab->justify, &(newboxpts[j].x),
			&(newboxpts[j].y), -1);
	   }
#endif
	   tmpext = ULength(slab->string, NORMINST, 0.0, 0, NULL);
	   adj.x = (slab->justify & NOTLEFT ? (slab->justify & RIGHT ? 
			tmpext.width : tmpext.width >> 1) : 0);
	   adj.y = (slab->justify & NOTBOTTOM ? (slab->justify & TOP ? 
			tmpext.ascent : (tmpext.ascent + tmpext.base) >> 1)
			: tmpext.base);
	   
	   /* Label selection:  For each character in the label string, */
	   /* do an insideness test with the select box.		*/

	   state = tmpl2 = 0;
	   for (j = 0; j < stringlength(slab->string, True, NORMINST); j++) {
	      strptr = findstringpart(j, &locpos, slab->string, NORMINST);
	      if (locpos < 0) continue;	  /* only look at printable characters */
	      if (strptr->type == RETURN) tmpl2 = 0;
	      tmpl1 = tmpl2;
	      tmpext = ULength(slab->string, NORMINST, 0.0, j + 1, NULL);
	      tmpl2 = tmpext.width;
	      isect = test_insideness(((tmpl1 + tmpl2) >> 1) - adj.x,
			(tmpext.base + (BASELINE >> 1)) - adj.y, newboxpts);

	      /* tiny state machine */
	      if (state == 0) {
		 if (isect) {
		    state = 1;
		    selected = True;
		    textend = j;
		    if ((textend > 1) && strptr->type != TEXT_STRING)
		       textend--;
		 }
	      }
	      else {
		 if (!isect) {
		    textpos = j;
		    state = 2;
		    break;
		 }
	      }
	   }
	   if (state == 1) textpos = j;   /* selection goes to end of string */
	   } 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();

   /* if none or > 1 label has been selected, cancel any textpos placement */

   if (!checkselect(LABEL) || areastruct.selects != 1 ||
	(areastruct.selects == 1 && SELECTTYPE(areastruct.selectlist) != LABEL)) {
      textpos = textend = 0;
   }

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

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

void startdesel(Widget w, caddr_t clientdata, caddr_t calldata)
{
   if (eventmode == NORMAL_MODE) {
      if (areastruct.selects == 0)
	 Wprintf("Nothing to deselect!");
      else if (areastruct.selects == 1)
	 objectdeselect();
      else {
         eventmode = DESEL_MODE;
         Wprintf("Click on element to deselect");
      }
   }
}

/*---------------------------------------------------------*/
/* Redraw all the selected objects in their normal colors. */
/*---------------------------------------------------------*/

void drawselects(objectptr thisobj, objinstptr thisinst)
{
   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)); 
      geneasydraw(*lastselect, SELTOCOLOR(lastselect), thisobj, thisinst);
   }
}

/*--------------------------------------------------------------*/
/* Free memory from the selection list and set menu/toolbar	*/
/* items back to default values.				*/
/*--------------------------------------------------------------*/

void clearselects()
{
   if (areastruct.selects == 0) return;

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

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

/*--------------------------------------------------------------*/
/* Unselect all the selected objects and free memory from the	*/
/* selection list.						*/
/*--------------------------------------------------------------*/

void objectdeselect()
{
   drawselects(objectdata, NORMINST);
   clearselects();
}

/*----------------------------------------------------------------------*/
/* Select the nearest polygon, searching the hierarchy if necessary.	*/
/* Return an objectpair pointer to a linked list containing the 	*/
/* hierarchy of objects, with the topmost objectpair also containing a	*/
/* pointer to the polygon found.					*/
/* Allocates memory for the returned linked list which must be freed by	*/
/* the calling routine.							*/
/*----------------------------------------------------------------------*/

genericptr recurselect(short class, objectpair **seltop)
{
   genericptr rgen = NULL;
   short *selectobj;
   objectptr selobj;
   objinstptr selinst;
   XPoint savesave, tmppt;

   if (*seltop == NULL) {
      fprintf(stderr, "Error: recurselect called with NULL objectpair pointer\n");
      return NULL;
   }

   selobj = (*seltop)->thisobject;
   selinst = (*seltop)->thisinst;

   if ((selectobj = selectobject(class, selobj, selinst)) != NULL) {
      rgen = *(selobj->plist + (*selectobj));
      /* printf("%s found in object %s\n", (rgen->type == LABEL) ? "label" : "polygon",
			selobj->name); */
      drawselects(selobj, selinst);
      clearselects();
   }
   else if ((selectobj = selectobject(OBJECT, selobj, selinst)) != NULL) {
      objectpair *selnew = (objectpair *)malloc(sizeof(objectpair));

      selinst = TOOBJINST(selobj->plist + (*selectobj));	
      drawselects(selobj, selinst);
      clearselects();

      /* Link hierarchy information to the objectpair linked list */
      selnew->thisinst = selinst;
      selnew->thisobject = selinst->thisobject;
      selnew->nextpair = NULL;
      (*seltop)->nextpair = selnew;
	 
      /* Translate areastruct.save into object's own coordinate system */
      savesave.x = areastruct.save.x;
      savesave.y = areastruct.save.y;
      InvTransformPoints(&areastruct.save, &tmppt, 1, selinst->position,
		selinst->scale, selinst->rotation);
      areastruct.save.x = tmppt.x;
      areastruct.save.y = tmppt.y;
      /* printf("objinst %s found in object %s; searching recursively\n",
		selinst->thisobject->name, selobj->name); */
      /* printf("cursor position originally (%d, %d); in new object is (%d, %d)\n",
		savesave.x, savesave.y,
		areastruct.save.x, areastruct.save.y); */
      UPushCTM();
      UPreMultCTM(DCTM, selinst->position, selinst->scale, selinst->rotation);
      rgen = recurselect(class, &selnew);
      UPopCTM();
      areastruct.save.x = savesave.x;
      areastruct.save.y = savesave.y;
   }
   /* else
      printf("nothing found in object %s\n", selobj->name); */

   return rgen;
}

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

void startselect(XtIntervalId id, caddr_t clientdata)
{
   if (eventmode != PENDING_MODE) return;
   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 */
/*-------------------------*/

void trackselarea(Widget w, XtPointer clientdata, XMotionEvent *event)
{
   XPoint newpos;
   u_int  nullui;

   newpos = UGetCursorPos();
   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;
}

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