File: draw.c

package info (click to toggle)
chemtool 1.6.14-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 4,708 kB
  • sloc: ansic: 56,595; sh: 4,455; pascal: 825; makefile: 459; yacc: 288; sed: 16
file content (1110 lines) | stat: -rw-r--r-- 25,292 bytes parent folder | download | duplicates (4)
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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
/*
 *    draw functions inside the pixmap
 */

#include <string.h>

#include "ct1.h"
void
Add_atom (int event_x, int event_y)
/* reads the string from the text entry box and inserts it at the current
   cursor position in the current justification */
{
  struct xy_co *coord;
  struct dc *hpc;
  gchar *name;
  int x, y;
  int i;

  name = (char*)gtk_entry_get_text (GTK_ENTRY (textbox)); 
  if (strlen (name))
    {
      x = round_coord (event_x, size_factor);
      y = round_coord (event_y, size_factor);
      coord = position (x, y, x, y, No_angle);
      for (i = 0; i < (int)strlen (name); i++)
	{
	  if (name[i] == ' ')
	    name[i] = '\\';
	  if (name[i] == '*' && i > 0)
	    {			/* convert @* to @+dotsymbol */
	      if (name[i - 1] == '@')
		name[i] = '\267';
	    }
	}
      del_char (select_char (event_x, event_y, size_factor));
      add_char (coord->x, coord->y, name, text_direct, 0, curpen, serif_flag, curfontsize);
      modify = True;
      Display_Mol ();
    } else {
    hpc = select_char (event_x, event_y, size_factor);
	if (hpc) {
		hpc->direct=text_direct;
		hpc->color=curpen;
		hpc->font=serif_flag;
		hpc->size=curfontsize;
     		modify = True;
      		Display_Mol ();
	}
    } 
}

void
Fetch_atom (int event_x, int event_y)
/* loads a label from the current cursor position into the text entry box */
{
  struct dc *hpc;

  hpc = select_char (event_x, event_y, size_factor);
  if (hpc)
    gtk_entry_set_text (GTK_ENTRY (textbox), hpc->c);
}


void
Del_atom (int event_x, int event_y)
/* deletes the label at the current cursor position */
{
  del_char (select_char (event_x, event_y, size_factor));
  modify = True;
  Display_Mol ();
}

void
Set_vector (int event_x, int event_y, int curbond)
/* draws a circle of transient marker boxes corresponding to the current
   grid angles around the cursor position */
{
  int angle_count[] = { 0, 12, 10, 10, 8 };
  int d, dmax;
  struct xy_co *coord;

  hp->x = round_coord (event_x, size_factor);
  hp->y = round_coord (event_y, size_factor);
  if (curbond != 8){ /* snap to close bonds unless half arrow is drawn */
  coord = position (hp->x, hp->y, hp->x, hp->y, draw_angle);
  hp->x = coord->x;
  hp->y = coord->y;
  }
  hp->tx = hp->x;
  hp->ty = hp->y;
  dmax = angle_count[draw_angle];
  for (d = 0; d < dmax; d++)
    {
      coord = calc_angle (hp->x, hp->y, draw_angle, d, 1);
      gdk_draw_rectangle (picture, drawing_area->style->black_gc,
			  TRUE, coord->x * size_factor - 2,
			  coord->y * size_factor - 2, 4, 4);
#if 0
/* draw a second ring of markers farther out */
      gdk_draw_rectangle (picture, drawing_area->style->black_gc,
			  TRUE, (2*coord->x-hp->x)* size_factor - 2,
			  (2*coord->y-hp->y)*size_factor - 2, 4, 4);
      gdk_draw_rectangle (picture, drawing_area->style->black_gc,
			  TRUE, (3*coord->x-2*hp->x)* size_factor - 2,
			  (3*coord->y-2*hp->y)*size_factor - 2, 4, 4);
#endif
    }
  CopyPlane ();
}

void
Add_vector (int cbond)
/* adds a bond vector from the previous to the current cursor position */
{
  if ((hp->x - hp->tx) || (hp->y - hp->ty))
    {
      add_struct (hp->x, hp->y, hp->tx, hp->ty, cbond, 0, 0, 0,curpen);
  modify = True;
    }
  Display_Mol ();
}

void
Add_ring (int draw_angle, int curbond, int ringtype)
/* adds a polygon clockwise around the previous and current position */
{
  double length;		/* length of the drawn bond */
  double radius;		/* the radius of the polygon */
  int type;			/* type of ring */
  int i;			/* dummy */
  double angle;			/* absolute angle on drawing surface */
  double o_angle;		/* outside angle of the polygon */
  double i_angle;		/* inside angle of the polygon */
  double a;			/* temp angle used for aromates */
  double mx, my;		/* center point */
  int aromatic = 0;		/* logical: draw aromate? */
  float percent = 0.7;		/* percentage of ring radius/polygon radius */
  int x0, y0, x1, y1;		/* start and end points of the drawn side 
				   of the polygon */

/*  fprintf(stderr, "Begin: %d, %d, %d, %d\n", hp->x, hp->tx, hp->y, hp->ty);*/

  Unmark_all ();

  if (ringtype > 0)
    {
      type = ringtype;
      ringtype = 0;
    }
  else
    {
      switch (draw_angle)
	{
	case 1:		/* Hexagon */
	default:
	  type = 6;
	  break;
	  ;;
	case 2:		/* Pentagon */
	case 3:
	  type = 5;
	  break;
	  ;;
	case 4:		/* Octagon */
	  type = 8;
	  break;
	  ;;
	}
    }
  /* calculate the parameters of the polygon */
  o_angle = 2.0 * M_PI / type;

  i_angle = M_PI - o_angle;

  length =
    sqrt ((double)((hp->tx - hp->x) * (hp->tx - hp->x) +
	  (hp->ty - hp->y) * (hp->ty - hp->y)));

  /* 'radius' is one cathete of a triangle between one half of a side of the
   * polygon and the line between one point and the center */
  radius = 0.5 * length / cos (0.5 * i_angle);
/*  fprintf(stderr, "o_angle: %f, i_angle: %f, length: %f, radius: %f\n", 
 *   	    o_angle, i_angle, length, radius);*/

  /* check the bondstyle, treat rings specially */
  switch (curbond)
    {
    case 1:
      /* use only one type of kekule for uneven pointed rings 
       * otherwise, we'd wind up with one excess double bond */
      if (type % 2)
	{
	  aromatic = 2;
	}
      else
	{
	  aromatic = 1;
	}
      curbond = 0;
      break;
      ;;
    case 2:
      aromatic = 2;
      curbond = 0;
      break;
    case 11:
      /* circle bond */
      aromatic = 3;
      curbond = 0;
      break;
      ;;
    default:
      aromatic = 0;
      break;
      ;;
    }

  if ((hp->x - hp->tx) || (hp->y - hp->ty))
    {
      /* if angle > 180, subtract from 2pi */
      if (hp->ty > hp->y)
	{
	  /* first we calculate the angle of the given bond 
	   * relative to the drawing surface */
	  angle = (2 * M_PI - acos ((hp->tx - hp->x) / length));
	}
      else
	{
	  angle = (acos ((hp->tx - hp->x) / length));
	}
      /* cos() is the angle between two points on the polygon 
       * and the line from one of those points to the center 
       * 'angle' is used to compensate for arbitrary bonds drawn 
       * by the user */
      mx = hp->x + cos (angle - (0.5 * i_angle)) * radius;
      my = hp->y - sin (angle - (0.5 * i_angle)) * radius;
/*	fprintf(stderr, "mx: %f, my: %f, angle: %f\n", mx, my, angle);*/

      /* draw the polygon */
      for (i = 0; i < type; i++)
	{
	  x0 =
	    (int) rint (mx +
			radius * sin (i * o_angle +
				      (3 * M_PI / type - angle)));
	  y0 =
	    (int) rint (my -
			radius * cos (i * o_angle +
				      (3 * M_PI / type - angle)));
	  x1 =
	    (int) rint (mx +
			radius * sin ((i + 1) * o_angle +
				      (3 * M_PI / type - angle)));
	  y1 =
	    (int) rint (my -
			radius * cos ((i + 1) * o_angle +
				      (3 * M_PI / type - angle)));
	  /* if conjugated double bonds are selected, 
	   * draw kekule style rings 
	   * draw every second bond as double bond */
	  if ((aromatic == 1 && (i + 1) % 2) || (aromatic == 2 && i % 2))
	    {
	      /* get the angle of the current bond to the canvas */
	      if (y1 > y0)
		{
		  a = (2 * M_PI - acos ((x1 - x0) / length));
		}
	      else
		{
		  a = (acos ((x1 - x0) / length));
		}
	      add_struct (x0, y0, x1, y1, 2, 1, 1, 0, curpen);
	    }
	  else
	    {
	      add_struct (x0, y0, x1, y1, curbond, 1, 1, 0, curpen);
	    }
	}
      /* draw a benzene ring into the polygon if curbond is ring */
      if (aromatic == 3)
	{
	  /* get the middle of the drawn bond */
	  x0 = (hp->tx + hp->x) / 2;
	  y0 = (hp->ty + hp->y) / 2;
	  /* get the point on the line from the center 70% to the
	   * point above */
	  x0 = (int)((x0 + percent * mx) / (1.0 + percent));
	  y0 = (int)((y0 + percent * my) / (1.0 + percent));
	  add_struct ((int)mx, (int)my, x0, y0, 11, 1, 1, 0,curpen);
	}

    }
  mark.flag = 1;
  Display_Mol ();
  modify = True;
}

void
Put_vector (int event_x, int event_y)
/* draws the transient line from the previous to the current cursor position */
{
  int x, y;
  struct xy_co *coord;

  x = round_coord (event_x, size_factor);
  y = round_coord (event_y, size_factor);
  coord = position (x, y, hp->x, hp->y, draw_angle);
  x = coord->x;
  y = coord->y;
  if (x != hp->x || y != hp->y)
    {
      if (hp->x > 0)
	gdk_draw_line (picture, background_gc,
		       hp->x * size_factor, hp->y * size_factor,
		       hp->tx * size_factor, hp->ty * size_factor);
      hp->tx = x;
      hp->ty = y;
      if (hp->x > 0)
	gdk_draw_line (picture, drawing_area->style->black_gc,
		       hp->x * size_factor, hp->y * size_factor,
		       hp->tx * size_factor, hp->ty * size_factor);

      CopyPlane ();
    }
}

void
Del_vector (int event_x, int event_y)
/* deletes the bond vector closest to the current cursor position */
{
  del_struct (select_vector (event_x, event_y, size_factor));
  modify = True;
  Display_Mol ();
}

void
Invert_vector (int event_x, int event_y)
/* exchanges start and endpoint coordinates of a bond (to change direction of
   a wedge or arrow ) */
{
  struct data *hdc;
  int tmp_x, tmp_y;

  hdc = select_vector (event_x, event_y, size_factor);
  if (!hdc)
    return;
  tmp_x = hdc->x;
  tmp_y = hdc->y;
  hdc->x = hdc->tx;
  hdc->y = hdc->ty;
  hdc->tx = tmp_x;
  hdc->ty = tmp_y;
  modify = True;
  Display_Mol ();
}

void
Add_double (int event_x, int event_y)
/* Changes bondtype :
   0 - single
   1 - leftdouble
   2 - rightdouble
   3 - triple
   4 - middouble 
   5 - wedge
   6 - dashed wedge
   7 - wiggly
   8 - single arrow
   9 - half arrow
   10 - wide single
   11 - circle 
   12 - dotted line
   13 - crossing single line
   14 - left partial double
   15 - right partial double
   16 - dashed bond
   17 - curved half arrow (shortcut)
   18 - uniform triple
   19 - quadruple
   20 - orbital
   21 - filled orbital
 */
{
  struct data *hdc;
  int dx, dy;

  hdc = select_vector (event_x, event_y, size_factor);
  if (hdc)
    {
      dx = hdc->x - hdc->tx;
      dy = hdc->y - hdc->ty;
      if (hdc->bond >= 19)
	hdc->bond = 0;
      else
	++hdc->bond;
      if (hdc->bond==17) hdc->bond++; /* no bond can become a curved arrow */	
      if (hdc->bond>19) hdc->bond = 0; /* nor can it turn into an orbital */
      modify = True;
    }
  Display_Mol ();
}

void
Set_bondtype (int event_x, int event_y, int curbond)
{
  struct data *hdc;

  struct spline *hsp;
  struct xy_co *coord;
  int d;

  if (curbond == 17 || curbond > 19)
    return;			/* cannot change anything to 3-point spline */

  hdc = select_vector (event_x, event_y, size_factor);
  if (hdc)
    {
      hdc->bond = curbond;
      hdc->color = curpen; 
      modify = True;
    }
  else
    {
      if (hp->nsp)
	{
	  event_x = round_coord (event_x, size_factor);
	  event_y = round_coord (event_y, size_factor);
	  coord = position (event_x, event_y, event_x, event_y, No_angle);
	  hsp = sp_root.next;
	  for (d = 0; d < hp->nsp; d++)
	    {
/*
	fprintf(stderr,"event %d:%d, spline0 %d:%d ,spline3 %d:%d\n",
		coord->x,coord->y,hsp->x0, hsp->y0, hsp->x3, hsp->y3);
*/
	      if ((hsp->x0 == coord->x && hsp->y0 == coord->y)
		  || (hsp->x3 == coord->x && hsp->y3 == coord->y))
		{
		  switch (curbond)
		    {
		    case 0:
		      hsp->type = 0;
		      break;
		    case 8:
		      hsp->type = 1;
		      break;
		    case 9:
		      hsp->type = 2;
		      break;
		    case 10:
		      hsp->type = -1;
		      break;
		    case 12:
		      hsp->type = -2;
		      break;
		    default:
		      break;
		    }
		    hsp->color = curpen;
		  modify = True;
		}
	      hsp = hsp->next;
	    }
	}
    }
  Display_Mol ();
}

void
Set_start_rec (int event_x, int event_y)
/* defines first corner of the selection rectangle in mark mode */
{
  hp->x = event_x;
  hp->y = event_y;
  hp->tx = event_x;
  hp->ty = event_y;
  hp->startx = event_x;
  hp->starty= event_y;
}

void
Put_rec (int event_x, int event_y)
/* draws the rubberband rectangle in mark mode */
{
   gdk_draw_rectangle (picture, background_gc,
           FALSE, hp->x, hp->y, hp->tx - hp->x, hp->ty - hp->y);

  if (event_x > hp->x) /* motion right */
    {
      if (event_x >= hp->startx)  /* right of startpoint */
    {
      hp->tx = event_x;
      hp->x = hp->startx;  /* in case of too fast motion */
    }
      else
    {
      hp->x = event_x;  /* left of startpoint */
      hp->tx = hp->startx;
    }
    }
   else  /* motion left */
    {
      if (event_x > hp->startx)    /* right of startpoint */
    {
      hp->tx = event_x;
      hp->x = hp->startx;
    }
      else
    {
      hp->x = event_x;   /* left of startpoint */
      hp->tx = hp->startx;
    }
     }
   if (event_y > hp->y)   /* motion down */
    {
      if (event_y > hp->starty)   /* below startpoint */
    {
      hp->ty = event_y;
      hp->y = hp->starty;
    }
      else                    /* above startpoint */
    {
      hp->y = event_y;
      hp->ty = hp->starty;
    }
    }
   else        /* upward motion  */
    {
      if (event_y > hp->starty)     /* below startpoint */
    {
      hp->ty = event_y;
      hp->y = hp->starty;
    }
      else                    /* above startpoint */
    {
      hp->y = event_y;
      hp->ty = hp->starty;
    }
     }
  gdk_draw_rectangle (picture, drawing_area->style->black_gc,
              FALSE, hp->x, hp->y, hp->tx - hp->x, hp->ty - hp->y);

  CopyPlane ();
}

void
Del_rec ()
/* deletes the marked fragment inside the rectangle */
{
  if (hp->n + hp->nc + hp->nsp == 0)	/* is anything there at all ? */
    return;

  if (!mark.flag)
    return;			/* if nothing marked, nothing to do */

  modify = partial_delete();

/*  
  modify =
    partial_delete (round_coord (hp->x, size_factor),
		    round_coord (hp->y, size_factor), 
		    round_coord (hp->tx, size_factor),
		    round_coord (hp->ty, size_factor));
*/
  if (modify == True)
    mark.flag = False;

}

void
Mark_rec (int control_pressed)
/* mark all bonds and labels inside the rectangular selection area */
{
  struct data *hp_bond;
  struct dc *hp_atom;
  struct spline *hp_spline;
  int hn;
  int d;
  int marks = 0;

  if (hp->tx < hp->x)
    hp->tx = hp->x;
  if (hp->ty < hp->y)
    hp->ty = hp->y;
  gdk_draw_rectangle (picture, drawing_area->style->black_gc,
		      FALSE, hp->x, hp->y, hp->tx - hp->x, hp->ty - hp->y);
#ifdef LIBUNDO
  undo_snapshot ();
#endif
  CopyPlane ();
  if (hp->tx == hp->x && hp->ty == hp->y && !control_pressed)
    {
      mark.flag = False;
      Mark_atom(hp->x,hp->y);
      return;
    }
  else
    {
      mark.x = round_coord (hp->x, size_factor);
      mark.y = round_coord (hp->y, size_factor);
      mark.w = round_coord (hp->tx - hp->x, size_factor);
      mark.h = round_coord (hp->ty - hp->y, size_factor);
      mark.flag = True;

      hp_atom = dac_root.next;
      hn = hp->nc;
      for (d = 0; d < hn; d++)
	{
	  if (check_pos_rec
	      (hp_atom->x, hp_atom->y, mark.x, mark.y, mark.x + mark.w,
	       mark.y + mark.h))
	    {
	      hp_atom->marked = 1;
	      marks++;
	    }
	  else
	    {
	      if (!control_pressed)
		{
		  hp_atom->marked = 0;
		}
	    }
	  hp_atom = hp_atom->next;
	}

      hp_bond = da_root.next;
      hn = hp->n;
      for (d = 0; d < hn; d++)
	{
	  if (check_pos_rec
	      (hp_bond->x, hp_bond->y, mark.x, mark.y, mark.x + mark.w,
	       mark.y + mark.h))
	    {
	      hp_bond->smarked = 1;
	      marks++;
	    }
	  else
	    {
	      if (!control_pressed)
		{
		  hp_bond->smarked = 0;
		}
	    }
	  if (check_pos_rec
	      (hp_bond->tx, hp_bond->ty, mark.x, mark.y, mark.x + mark.w,
	       mark.y + mark.h))
	    {
	      hp_bond->tmarked = 1;
	      marks++;
	    }
	  else
	    {
	      if (!control_pressed)
		{
		  hp_bond->tmarked = 0;
		}
	    }
	  hp_bond = hp_bond->next;
	}

      hp_spline = sp_root.next;
      hn = hp->nsp;
      for (d = 0; d < hn; d++)
	{
	  if (check_pos_rec
	      (hp_spline->x0, hp_spline->y0, mark.x, mark.y, mark.x + mark.w,
	       mark.y + mark.h)
	      || check_pos_rec
	      (hp_spline->x1, hp_spline->y1, mark.x, mark.y, mark.x + mark.w,
	       mark.y + mark.h)
	      || check_pos_rec
	      (hp_spline->x2, hp_spline->y2, mark.x, mark.y, mark.x + mark.w,
	       mark.y + mark.h) || check_pos_rec
	      (hp_spline->x3, hp_spline->y3, mark.x, mark.y, mark.x + mark.w,
	       mark.y + mark.h))
	    {
	      hp_spline->marked = 1;
	      marks++;
	    }
	  else
	    {
	      if (!control_pressed)
		{
		  hp_spline->marked = 0;
		}
	    }
	  hp_spline = hp_spline->next;
	}
    }
	if (addflag ==1) /* check if marker is within region */
	  if (check_pos_rec
	      (refx, refy, mark.x, mark.y, mark.x + mark.w,
	       mark.y + mark.h)) refmark=1;
	       
/*reset mark.flag, if we did not find anything to mark */
/*fprintf(stderr,"marks %d\n",marks);*/
  if (marks == 0)
    mark.flag = False;
/*  if (marks == 1) {
  	Unmark_all();
  	Mark_atom(hp->x,hp->y);
  	}
*/
}

void
Unmark_all ()
/* remove all marks*/
{
  struct data *hp_bond;
  struct dc *hp_atom;
  struct spline *hp_spline;
  int hn;
  int d;

#ifdef LIBUNDO
  undo_snapshot ();
#endif
  mark.flag = False;

  hp_atom = dac_root.next;
  hn = hp->nc;
  for (d = 0; d < hn; d++)
    {
      hp_atom->marked = 0;
      hp_atom = hp_atom->next;
    }

  hp_bond = da_root.next;
  hn = hp->n;
  for (d = 0; d < hn; d++)
    {
      hp_bond->smarked = 0;
      hp_bond->tmarked = 0;
      hp_bond = hp_bond->next;
    }
      hp_spline = sp_root.next;
      hn = hp->nsp;
      for (d = 0; d < hn; d++)
	{
	hp_spline->marked = 0;
	hp_spline = hp_spline->next;
	}
    if (addflag==1) refmark=0;
}


void
Mark_atom (int t_x, int t_y)
/* marks a single position (for addition of fragments) */
{
  struct data *hdc;
  int d, dt,hn,n;

  refx=-1;
  refy=-1;
	t_x=round_coord(t_x,size_factor);
	t_y=round_coord(t_y,size_factor);
  hdc = da_root.next;
  hn = hp->n;
  for (n = 0; n < hn; n++)
    {
  d = calc_vector(abs(hdc->x-t_x),abs(hdc->y-t_y));
  dt = calc_vector(abs(hdc->tx-t_x),abs(hdc->ty-t_y));
	if (d <10*size_factor) {
	  refx = hdc->x;
  	  refy = hdc->y;
	  }else if (dt <10*size_factor) {
	  refx = hdc->tx;
  	  refy = hdc->ty;
	}
    	hdc=hdc->next;
    }
  if (refx<0 || refy<0)  {
/*  fprintf(stderr,"no match found for %d %d\n",t_x,t_y);*/
  addflag=0;
  return;
  }
  addflag = 1;
  Display_Mol ();
}

void
Center ()
/* callback function to center the molecule in the drawing area */
{
  FreePix ();
  CreatePix ();
  center_mol ();
  Display_Mol ();
  modify = True;
}

void
Set_pmove (int event_x, int event_y)
/*  sets the reference position for moving/rotating the selected fragment */
{
  struct xy_co *coord;
  struct data *hp_bond;
  struct dc *hp_atom;
  struct spline *hp_spline;

  int x, y;
  int d, dtmp;
  draw_ok=1;
  x = round_coord (event_x, size_factor);
  y = round_coord (event_y, size_factor);
  coord = position (x, y, x, y, No_angle);
  hp->x = coord->x;
  hp->y = coord->y;
/*  xcent = hp->x;
  ycent = hp->y;*/
  get_center_marked (&xcent, &ycent);

  dtmp = 0;
/*  if (tmpx)
    {
      free (tmpx);
      tmpx = NULL;
    }
  if (tmpy)
    {
      free (tmpy);
      tmpy = NULL;
    }*/
  tmpx = realloc (tmpx,(2 * hp->n + hp->nc + 4 * hp->nsp +1) * sizeof (int));
  tmpy = realloc (tmpy,(2 * hp->n + hp->nc + 4 * hp->nsp +1) * sizeof (int));
  hp_bond = da_root.next;
  for (d = 0; d < hp->n; d++)
    {

      if (hp_bond->smarked == 1)
	{
	  tmpx[dtmp] = hp_bond->x - xcent;
	  tmpy[dtmp++] = hp_bond->y - ycent;
	}
      if (hp_bond->tmarked == 1)
	{
	  tmpx[dtmp] = hp_bond->tx - xcent;
	  tmpy[dtmp++] = hp_bond->ty - ycent;
	}
      hp_bond = hp_bond->next;
    }

  hp_atom = dac_root.next;
  for (d = 0; d < hp->nc; d++)
    {
      if (hp_atom->marked == 1)
	{
	  tmpx[dtmp] = hp_atom->x - xcent;
	  tmpy[dtmp++] = hp_atom->y - ycent;
	}
      hp_atom = hp_atom->next;
    }

  hp_spline = sp_root.next;
  for (d = 0; d < hp->nsp; d++)
    {
      if (hp_spline->marked == 1)
	{
	  tmpx[dtmp] = hp_spline->x0 - xcent;
	  tmpy[dtmp++] = hp_spline->y0 - ycent;
	  tmpx[dtmp] = hp_spline->x1 - xcent;
	  tmpy[dtmp++] = hp_spline->y1 - ycent;
	  tmpx[dtmp] = hp_spline->x2 - xcent;
	  tmpy[dtmp++] = hp_spline->y2 - ycent;
	  tmpx[dtmp] = hp_spline->x3 - xcent;
	  tmpy[dtmp++] = hp_spline->y3 - ycent;
	}
      hp_spline = hp_spline->next;
    }
    	if (addflag==1 && refmark==1){
	  tmpx[dtmp] = refx - xcent;
	  tmpy[dtmp++] = refy - ycent;
    	}	
}

void
Put_pmove (int event_x, int event_y, int axis)
/* updates the position of the marked fragment as the mouse is dragged */
{
  int x, y;
  int dx, dy;
  x = round_coord (event_x, size_factor);
  y = round_coord (event_y, size_factor);
  if (importflag != 0)
    return;
  if (!draw_ok) return;  
  if (mark.flag)
    {
      dx = x - hp->x;
      dy = y - hp->y;
	if (axis == 1) dy = 0;
	if (axis == 2) dx = 0;
      if (partial_move (dx, dy))
	modify = True;
      mark.x = mark.x + dx;
      mark.y = mark.y + dy;
      hp->x = x;
      hp->y = y;
      Display_Mol ();
    }
  else
    {
      if (move_pos (hp->x, hp->y, x, y))
	{
	  hp->x = x;
	  hp->y = y;
	  Display_Mol ();
	  modify = True;
	} 
#if 0
	/* automatically switch to rubberband rectangle if nothing selected */
	else {
	Set_start_rec(event_x,event_y);
    	drawmode = 4;
    	}
#endif
    }
}

void
Put_protate (int event_x, int event_y, int control_pressed)
/* updates the position of the marked fragment as it pivots around the
   reference position proportional to the x movement of the mouse */
{
  int x, y;
  int dx;
  static float angle;
  int i;
  static float ang = 0.;
  double rotsin = 0.;
  double rotcos = 0.;
  if (!draw_ok) return;
  x = round_coord (event_x, size_factor);
  y = round_coord (event_y, size_factor);
  if (importflag != 0)
    {
      pdbrotate (x, y, control_pressed);
      return;
    }
  if (!mark.flag)
    return;			/* rotation only makes sense for blocks */
  mark.x = 0;
  mark.y = 0;
  mark.w = 800;
  mark.h = 800;
  dx = x - hp->x;

  ang += (float) dx / 2.;
  if (ang < -360.)
    ang = ang + 360.;
  if (ang > 360.)
    ang = ang - 360.;
  switch (draw_angle)
    {
    case 1:
      angle = 30.0;
      break;
    case 2:
    case 3:
      angle = 18.0;
      break;
    case 4:
      angle = 45.0;
      break;
    }
  for (i = 0; i < 30; i++)
    {
      if (fabs ((angle + (angle * (float) i)) - fabs(ang)) < 4.0 && control_pressed)
	{
	  rotsin = sin ((angle + (angle * (float) i)) * M_PI / 180.);
	  rotcos = cos ((angle + (angle * (float) i)) * M_PI / 180.);
	  i = 30;		/* break loop */
	}
      else
	{
	  rotsin = sin (ang * M_PI / 180.);
	  rotcos = cos (ang * M_PI / 180.);
	}
    }
  if (partial_rotate (/*xcent, ycent,*/ rotsin, rotcos))
    modify = True;
  hp->x = x;
  hp->y = y;
  Display_Mol ();


}

void
Set_pscale (int event_x, int event_y)
/*  sets the reference position for moving/rotating the selected fragment */
{
  struct xy_co *coord;
  struct data *hp_bond;
  struct dc *hp_atom;
  struct spline *hp_spline;

  int x, y;
  int d, dtmp;

  draw_ok=1;
  x = round_coord (event_x, size_factor);
  y = round_coord (event_y, size_factor);
  coord = position (x, y, x, y, No_angle);
  hp->x = coord->x;
  hp->y = coord->y;
  if (tmpx)
    {
      free (tmpx);
      tmpx = NULL;
    }
  if (tmpy)
    {
      free (tmpy);
      tmpy = NULL;
    }
  tmpx = malloc ((2 * hp->n + hp->nc + 4 * hp->nsp) * sizeof (int));
  tmpy = malloc ((2 * hp->n + hp->nc + 4 * hp->nsp) * sizeof (int));

  get_center_marked (&xcent, &ycent);

  dtmp = 0;
  hp_bond = da_root.next;
  for (d = 0; d < hp->n; d++)
    {

      if (hp_bond->smarked == 1)
	{
	  tmpx[dtmp] = hp_bond->x - xcent;
	  tmpy[dtmp++] = hp_bond->y - ycent;
	}
      if (hp_bond->tmarked == 1)
	{
	  tmpx[dtmp] = hp_bond->tx - xcent;
	  tmpy[dtmp++] = hp_bond->ty - ycent;
	}
      hp_bond = hp_bond->next;
    }

  hp_atom = dac_root.next;
  for (d = 0; d < hp->nc; d++)
    {
      if (hp_atom->marked == 1)
	{
	  tmpx[dtmp] = hp_atom->x - xcent;
	  tmpy[dtmp++] = hp_atom->y - ycent;
	}
      hp_atom = hp_atom->next;
    }

  hp_spline = sp_root.next;
  for (d = 0; d < hp->nsp; d++)
    {
      if (hp_spline->marked == 1)
	{
	  tmpx[dtmp] = hp_spline->x0 - xcent;
	  tmpy[dtmp++] = hp_spline->y0 - ycent;
	  tmpx[dtmp++] = hp_spline->x1 - xcent;
	  tmpy[dtmp++] = hp_spline->y1 - ycent;
	  tmpx[dtmp++] = hp_spline->x2 - xcent;
	  tmpy[dtmp++] = hp_spline->y2 - ycent;
	  tmpx[dtmp++] = hp_spline->x3 - xcent;
	  tmpy[dtmp++] = hp_spline->y3 - ycent;
	}
      hp_spline = hp_spline->next;
    }
}

void
Put_pscale (int event_x, int event_y, int aniso)
/* updates the position of the marked fragment as the mouse is dragged */
{
  int x;
  int dx;
  if (!draw_ok) return;
  x = round_coord (event_x, size_factor);

  if (mark.flag)
    {
      dx = x - hp->x;
      if (partial_rescale (dx, aniso))
	modify = True;
      Display_Mol ();
    }
}

void
Add_number (int event_x, int event_y)
/* insert an automatically incremented atom number at the current
   cursor position*/
{
  struct xy_co *coord;
  gchar name[10];
  int x, y;

  snprintf(name,9,"%d",++atnum); 
  if (strlen (name))
    {
      x = round_coord (event_x, size_factor);
      y = round_coord (event_y, size_factor);
      coord = position (x, y, x, y, No_angle);
      del_char (select_char (event_x, event_y, size_factor));
/*      add_char (coord->x, coord->y, name, text_direct, 0, curpen, serif_flag, curfontsize);*/
      add_char (x, y, name, Middle_Text, 0, curpen, serif_flag, 0);
      modify = True;
      Display_Mol ();
    }
}