-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxbs.c
More file actions
1573 lines (1336 loc) · 41 KB
/
xbs.c
File metadata and controls
1573 lines (1336 loc) · 41 KB
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
/*
Changes 3.8.2015 (M. Methfessel):
- Merged in PS output fix by Alessandro Mottura
- Merged in "tell" command from xbsa by Jan Labanowski
- Added animation, adapted from xbsa
- Animate as rotation with Ctrl-<cursor>
- Write PS files during animation with Ctrl-O
- Quick print with Ctrl-P
- Handle crazy perspective cases, eg. viewpoint inside a ball
* xbs: an X-Window ball&sticks plotting program.
* Copyright (C) 1995 Michael Methfessel
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* The author can be contacted as follows:
*
* Michael Methfessel
* methfessel@ihp-ffo.de
* Institute for Semiconductor Physics, PO Box 409,
* D-15204 Frankfurt (Oder), Germany
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/stat.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <math.h>
#include <time.h>
#include <string.h>
#define VERSION "2.0"
#define VDATE "August 6, 2015"
#define W_HEIGHT 450 /* startup size */
#define W_WIDTH 550
#define NAMAX 16384
#define NBMAX 262144
#define NBTMAX 200
#define NSPMAX 50
#define NLNMAX 50
#define PI 3.1415926
#define MAXRAD 100.0
#define GRAY0 0.5
#define PSFAC 1.42
#define NPOINTS 5
#define NCOL 31 /* max number of colors to be allocated */
#define FONT "8x13"
#define LABFONT "6x10"
#define SHADOW 5
#define BELL_LEVEL 30
#define SVINP 10
#define FBMAX 1000000 /* max for atoms times frames */
#define NFRMAX 2000000 /* max num of frames */
/* return codes for interpret_keypress routine */
#define K_NOP 0
#define K_QUIT 1
#define K_REPLOT 2
#define K_RESETUP 3
#define K_UPDATE 4
#define K_READ_MORE 5
#define K_READ_DONE 6
/* codes for gray modes */
#define G_STD 0
#define G_RAMP 1
#define G_LIGHT 2
struct ballstr {
float pos[3];
float rad;
float gray;
float r,g,b;
int col;
int special;
char lab[21];
};
struct stickstr {
int start;
int end;
float rad;
float gray;
float r, g, b;
int col;
};
/* --- global variables --- */
struct {
char lab[21];
float rad;
float r,g,b;
char cname[81];
int col;
float gray;
} spec[NSPMAX];
struct {
char lab[21];
float pos[3];
float pol[3];
} atom[NAMAX];
struct {
char lab1[21];
char lab2[21];
float min;
float max;
float rad;
float r,g,b;
char cname[81];
int col;
float gray;
} bonds [NBTMAX];
struct {
float a[3],b[3];
} xline [NLNMAX]; /* for extra lines */
int natom,nbond;
struct ballstr ball[NAMAX];
struct stickstr stick[NBMAX];
float arc[NPOINTS][2],xbot,xtop,ybot,ytop;
XtAppContext app_context;
Widget app_shell;
Display *dpy;
Window win;
Drawable drw;
Pixmap pixmap, bgrmap;
GC gc, gcbg, graygc[NCOL], shadowgc, labelgc, labbggc, lngc;
unsigned long fground, bground, gpx[NCOL];
int screen, depth, ncol;
Screen *screenptr;
Colormap cmap;
Pixmap stipl;
float p[NAMAX][3];
int nspec, nbas, nbonds, ngray, nxline;
int count;
int ipr=10;
int igeo, igx, igy, igw, igh;
float igs;
int midx, midy;
float alat, dist, dist0, amp, dalfa, scale, tmat[3][3], radfac, bndfac;
float taux, tauy, dtaux, dtauy, taux0, tauy0, bg;
float gslope, gz0, light[3];
float center[3];
/* for animation */
int animate, fps, msecs, framephase;
int animation_print = 0;
int anim_nwrite, anim_nskip, anim_rot_count;
XtIntervalId animation_timer = 0;
char anim_msg[64];
float frame[3][FBMAX];
char frstr[NFRMAX][81];
int nframe,iframe,saveframe;
FILE *outfp; /* for PS output file */
int hardcopy, usepixmap, numbers, grayvalues, wrinfo, fstep, wrhelp;
int bline, wire, withbonds, recenter, pmode, gmode, shadow, bondnums;
int color, autocolor, reverse, coords, stippled;
int showaxes, showlines;
int replot, resetup, startup, chginfo;
int num_print;
float pr_xoff, pr_yoff;
int lnum, xln;
char inf[81]="in.bs", outf[81]="Save.bs", prf[81]="Bs.ps";
char inmv[81], prfsave[81], wname[81], curf[81];
char gmsg[101], emsg[101];
char svinput[SVINP][257];
int svline, nsvline=1;
/* forward declarations */
void WriteStatus (Drawable draw);
void WriteInfo (Drawable draw);
void WriteHelp ();
void draw_plot ();
int match (char str[], char pat[]);
#include "intc.c"
#include "grsubs.h"
#include "hardcopy.h"
#include "subs.h"
#define stipple_width 8
#define stipple_height 8
static unsigned char stipple_bits[] = {
0x11, 0x00, 0x44, 0x00, 0x11, 0x00, 0x44, 0x00};
/* --- do_ConfigureNotify --- */
int do_ConfigureNotify (eventp)
XEvent *eventp;
{
XConfigureEvent *e = (XConfigureEvent *) eventp;
int x1, x2, y1, y2, mmx, mmy, h1, w1, xx;
/* x1 = midx + PSFAC*(taux+xbot);
x2 = midx + PSFAC*(taux+xtop);
y1 = midy + PSFAC*(tauy+ybot);
y2 = midy + PSFAC*(tauy+ytop);
printf ("old area: X %d %d Y %d %d\n", x1, x2, y1, y2);
*/
mmx = midx;
mmy = midy;
w1 = igw;
h1 = igh;
if ((e->height != igh) || (e->width != igw)) {
igw = e->width;
igh = e->height;
midx = igw/2;
midy = igh/2-20;
/* make new pixmaps but save contents of bgrmap */
XCopyArea (dpy, bgrmap, pixmap, gc, 0, 0, w1, h1, 0, 0);
XFreePixmap (dpy, bgrmap);
bgrmap = XCreatePixmap (dpy, win, igw, igh, depth);
XFillRectangle (dpy, bgrmap, gcbg, 0, 0, igw, igh);
XCopyArea (dpy, pixmap, bgrmap, gc, 0, 0, w1, h1, 0, 0);
XFreePixmap (dpy, pixmap);
pixmap = XCreatePixmap (dpy, win, igw, igh, depth);
if (num_print == 0) { /* normally put back to middle on resize */
taux = taux0 = 0;
tauy = tauy0 = 0;
}
else { /* but don't move plot about if building a print */
taux = taux - (midx-mmx)/PSFAC;
taux0 = taux0 - (midx-mmx)/PSFAC;
tauy = tauy - (midy-mmy-igh+h1)/PSFAC;
tauy0 = tauy0 - (midy-mmy-igh+h1)/PSFAC;
}
return 1;
}
else
return 0;
}
/* --- close_print --- */
void close_print(int helpme) {
if (helpme) {
sprintf (gmsg,"Usage: close - close current print file");
return;
}
if (num_print == 0) {
sprintf (emsg, "close: no print file is open");
return;
}
hardcopy_close();
XFillRectangle(dpy, bgrmap, gcbg, 0, 0, igw, igh);
num_print = 0;
}
/* --- handle_print --- */
int handle_print (char *msg1, char *msg2, int helpme)
{
char xx[81];
float x,y,x1,y1,z1,x2,y2,z2;
int ix,iy,dd;
Drawable drw1;
if (helpme) {
sprintf (gmsg,"Usage: print [-T] [-t title] [file] - print to PS file");
return 0;
}
sprintf (gmsg,"Print:");
if (num_print > 0 && strcmp(prf,prfsave)) {
close_print(0);
sprintf (xx, " %s closed,", prfsave);
strcat (gmsg, xx);
replot = 1;
}
if (num_print == 0) {
hardcopy_init (prf);
sprintf (xx, " %s opened,", prf);
strcat (gmsg, xx);
pr_xoff = 0;
pr_yoff = ybot-15;
} else {
hardcopy_redefine();
}
num_print++;
strcpy (prfsave, prf);
hardcopy = 1;
bs_transform (natom, ball);
bs_kernel (natom, ball, nbond, stick);
hardcopy = 0;
drw = bgrmap;
bs_kernel (natom, ball, nbond, stick);
if (strlen(msg1) > 0) {
x = pr_xoff + taux;
y = pr_yoff + tauy;
hardcopy_label (x, y, msg1);
ix = midx + PSFAC*x;
iy = midy - PSFAC*y;
dd = strlen(msg1)*6;
XDrawString (dpy, win, labelgc, ix-dd/2, iy, msg1, strlen(msg1));
XDrawString (dpy, bgrmap, labelgc, ix-dd/2, iy, msg1, strlen(msg1));
}
if (strlen(msg2) > 0) {
x = pr_xoff + taux;
y = pr_yoff + tauy - 10;
hardcopy_label (x, y, msg2);
ix = midx + PSFAC*x;
iy = midy - PSFAC*y;
dd = strlen(msg2)*6;
XDrawString (dpy, win, labelgc, ix-dd/2, iy, msg2, strlen(msg2));
XDrawString (dpy, bgrmap, labelgc, ix-dd/2, iy, msg2, strlen(msg2));
}
fflush (outfp);
sprintf(xx," write(%d) to %s", num_print, prf);
strcat (gmsg, xx);
}
/* --- update_from_file --- */
int update_from_file ()
{
char msg1[81],msg2[81],str[81],pat[81],w[8][41];
char *p;
float sh[3][6],cut[3],cut1,cut2;
int i,j,n,rc,nw,svstep,svrgb,helpme;
nbas=0; nbonds=0; nspec=0;
nframe=1;
if (!readclusterdata(inf)) {
sprintf (emsg, "Cannot update from file %s", inf);
return 0;
}
sprintf (curf, "%s ", inf);
clearline (win, 10, 8);
showline (win, 10, 8, "Reading ", inmv, " .. please wait");
XFlush(dpy);
sprintf(gmsg,"Updated from %s ", inf);
if(readclusterdata(inmv)) {
sprintf(gmsg,"Updated from %s and %s", inf, inmv);
sprintf(frstr[0], "%s", "start ..\0");
strcat (curf, inmv);
}
for (i=0;i<ncol;i++) FreeColorGC (i);
if (autocolor) set_auto_colors();
parse_all_colors ();
if (color)
SetColors ();
else {
if (stippled) SetStippled4x4 ();
else SetSmoothGrays ();
}
natom = ball_list(ball, 0);
nbond = stick_list (ball, stick);
putframe (ball, 0);
getframe (ball, iframe);
replot=1;
resetup=1;
return 1;
}
/* --- interpret_input --- */
void interpret_input (char inp[]) {
char msg1[81],msg2[81],str[81],pat[81],w[8][41];
char *p;
float sh[3][6],cut[3],cut1,cut2;
int i,j,n,rc,nw,svstep,svrgb,helpme;
nw = parse_args(inp, w);
if (nw==0) {
sprintf (gmsg, "Cancel input mode");
return;
}
helpme=0;
if (!strcmp(w[1],"?")) helpme=1;
if (!strcmp(w[1],"-h")) helpme=1;
if (abbrev(w[0],"help",4)) {
if (nw==1) {
sprintf (gmsg, "Try \'command ?\' or press key h");
return;
}
else {
helpme=1;
sprintf (inp, "%s", w[1]);
nw = parse_args(inp, w);
}
}
if (abbrev(w[0],"update",2)) {
if (helpme)
sprintf (gmsg,"Usage: update [-color] [-rv] [+rv] [-bw] [-st] [-auto] "
"[file] - update from file");
else {
i=1;
while (i<nw) {
if (abbrev(w[i],"-st",3)) { stippled=1; color=0; }
else if (abbrev(w[i],"-bw",3)) { stippled=0; color=0; }
else if (abbrev(w[i],"-color",4)) color=1;
else if (abbrev(w[i],"-auto",5)) { autocolor=1; color=1; }
else if (abbrev(w[i],"-rv",3)) reverse=1;
else if (abbrev(w[i],"+rv",3)) reverse=0;
else if (w[i][0]=='-') {
sprintf (emsg, "update: unknown flag %s", w[i]);
return;
}
else {
strext (inf, w[i], "bs", 0);
strext (inmv, inf, "mv", 1);
}
i++;
}
update_from_file();
}
}
else if(abbrev(w[0],"save",3)) {
if (helpme)
sprintf (gmsg,"Usage: save [-rgb] [-step n] file - save data");
else {
i=1; svstep=1; svrgb=0;
while (i<nw) {
if (abbrev(w[i],"-step",3)) {i++; svstep=atoi(w[i]); }
else if (abbrev(w[i],"-rgb",4)) svrgb=1;
else if (w[i][0]=='-') {
sprintf (emsg, "save: unknown flag %s", w[i]);
return;
}
else strext (outf, w[i], "bs", 0);
i++;
}
showline(win, 10, 8, "Saving .. ", "" ,"");
XFlush(dpy);
writeclusterdata(outf, svstep, svrgb);
}
}
else if (abbrev(w[0],"print",2)) {
if (helpme)
handle_print (msg1, msg2, helpme);
else {
i=1;
strcpy (msg1, "");
strcpy (msg2, "");
while (i<nw) {
if (!strcmp(w[i],"-t")) {
i++;
strcpy (msg1, w[i]);
}
else if (!strcmp(w[i],"-T")) {
sprintf(msg1,"%s frame %d of %d", inf, iframe+1, nframe);
if (strlen(frstr[iframe])>0) strip(msg2, frstr[iframe]);
}
else if (w[i][0]=='-') {
sprintf (emsg, "print: unknown flag %s", w[i]);
return;
}
else {
strext (prf, w[i], "ps", 0);
}
i++;
}
handle_print (msg1, msg2, helpme);
}
}
else if (abbrev(w[0],"close",2)) {
if (!helpme)
sprintf(gmsg, "Print: %s closed after %d write%s",
prf, num_print, num_print==1 ? "" : "s");
close_print (helpme);
replot=1;
}
else if (abbrev(w[0],"dup",3)) {
for (i=0;i<3;i++) for (j=0;j<6;j++) sh[i][j]=0;
sscanf(inp, "%*s %f %f %f %f %f %f %f %f %f %f %f %f",
&sh[0][0],&sh[1][0],&sh[2][0],&sh[0][1],&sh[1][1],&sh[2][1],
&sh[0][2],&sh[1][2],&sh[2][2],&sh[0][3],&sh[1][3],&sh[2][3],
&sh[0][4],&sh[1][4],&sh[2][4],&sh[0][5],&sh[1][5],&sh[2][5]);
if (duplicate_atoms (sh,helpme)) resetup=2;
}
else if (abbrev(w[0],"cut",3)) {
cut[0]=cut[1]=cut[2]=cut1=cut2=0;
sscanf(inp, "%*s %f %f %f %f %f",
&cut[0], &cut[1], &cut[2], &cut1, &cut2);
if (cut_atoms (cut, cut1, cut2, helpme)) resetup=2;
}
else if (abbrev(w[0],"color",3)) {
strcpy (pat,"");
sscanf(inp, "%*s %s %n", pat, &n);
for (i=n;i<strlen(inp)+1;i++) str[i-n]=inp[i];
if (NewSpecColor (pat,str,helpme)) resetup=1;
}
else {
rc=readclusterline(inp,helpme);
if (rc==1) replot=1;
if (rc==2) resetup=1;
}
}
/* --- print_animation_frame --- */
void print_animation_frame ()
{
char fname[64];
struct stat statbuf;
int rc, nrot;
if (! animation_print) return;
/* animation for frames goes to frm_xxxx.ps */
if (animate < 10)
sprintf (fname, "frm_%04d.ps", iframe+1);
/* animation for rotation goes to rot_xxxx.ps */
else {
anim_rot_count ++;
nrot = 360/dalfa;
sprintf (fname, "rot_%04d.ps", anim_rot_count);
if (anim_rot_count > nrot) {
sprintf (anim_msg, " - completed 360 degree turn");
return;
}
}
rc = stat(fname,&statbuf);
if (rc == 0) {
if (animate > 10)
sprintf (anim_msg, " - skip %s of %d", fname, nrot);
else
sprintf (anim_msg, " - skip %s", fname);
anim_nskip++;
return;
}
hardcopy_init (fname);
pr_xoff = 0;
pr_yoff = ybot-15;
hardcopy = 1;
bs_transform (natom, ball);
bs_kernel (natom, ball, nbond, stick);
hardcopy_close();
hardcopy = 0;
anim_nwrite ++;
if (animate > 10)
sprintf (anim_msg, " - write %s of %d", fname, nrot);
else
sprintf (anim_msg, " - write %s", fname);
}
/* --- animation_timer_cb --- */
void animation_timer_cb (Widget w, caddr_t client_data, caddr_t call_data)
{
/* frames shown as: 1, 2, ... N, 1, 2 .. */
if (animate == 1) {
iframe += fstep;
iframe = (iframe + 2 * nframe) % nframe;
}
/* frames shown as: N, N-1, ... 1, N, N-1 ... */
if (animate == 2) {
iframe -= fstep;
iframe = (iframe + 2 * nframe) % nframe;
}
/* frames shown as: 1, 2, ... N, N-1, N-2, ... 2, 1, 2, 3... */
if (animate == 3) {
iframe += fstep * framephase;
if ((iframe >= nframe) || (iframe < 0)) {
framephase *= -1;
iframe += 2*framephase*fstep;
}
}
if (animate > 10) {
if (animate == 11) rotmat (1, dalfa*PI/180.0);
else if (animate == 12) rotmat (1, -dalfa*PI/180.0);
else if (animate == 13) rotmat (2, -dalfa*PI/180.0);
else if (animate == 14) rotmat (2, dalfa*PI/180.0);
else if (animate == 15) rotmat (3, dalfa*PI/180.0);
else if (animate == 16) rotmat (3, -dalfa*PI/180.0);
draw_plot ();
}
else {
getframe (ball, iframe);
draw_plot ();
}
print_animation_frame ();
animation_timer = XtAppAddTimeOut (app_context, msecs,
(XtTimerCallbackProc)animation_timer_cb, NULL);
}
/* --- control_animation --- */
/*
mode: 1 for 'A', 2 for 'B', 3 for 'C', 0 to stop
Note: use ESC or 'r' to stop the animation
*/
void control_animation (int mode)
{
if (! animate) *anim_msg = anim_nwrite = anim_nskip = anim_rot_count = 0;
/* stop the animation */
if (mode == 0) {
if (animation_timer) XtRemoveTimeOut (animation_timer);
animation_timer = 0;
animate = 0;
if (animation_print)
sprintf (gmsg, "Stop animation - wrote %d, skipped %d files", anim_nwrite, anim_nskip);
else
sprintf (gmsg, "Stop animation");
return;
}
/* start animation as continuous rotation */
if (mode > 10) {
animate = mode;
if (! animation_timer)
animation_timer = XtAppAddTimeOut (app_context, msecs,
(XtTimerCallbackProc)animation_timer_cb, NULL);
return;
}
/* need at least two frames to animate */
if (nframe < 2) {
sprintf (gmsg, "No frames available for animation");
return;
}
/* enable framewise animation in selected mode */
/* be clever about initial direction in up/down mode */
if (mode == 3) {
if (animate == 1) framephase = 1;
if (animate == 2) framephase = -1;
if (animate == 3) framephase *= -1;
}
animate = mode;
print_animation_frame ();
if (! animation_timer)
animation_timer = XtAppAddTimeOut (app_context, msecs,
(XtTimerCallbackProc)animation_timer_cb, NULL);
}
/* --- interpret_keypress --- */
int interpret_keypress (XEvent *ev, int *inpmode, char input[],
int *ixyz, float *alfa)
{
int count, l, ctrl;
char buff[8],msg[81];
KeySym key;
count = XLookupString (&ev->xkey, buff, 8, &key, 0);
ctrl = (ev->xbutton.state & ControlMask);
if (*inpmode) {
l = strlen(input);
if (key == XK_Up) {
if (svline == 0) strcpy (svinput[0], input);
if (svline < nsvline-1) svline++;
strcpy (input, svinput[svline]);
return K_READ_MORE;
}
if (key == XK_Down) {
if (svline > 0) svline--;
strcpy (input, svinput[svline]);
return K_READ_MORE;
}
if (key == XK_Down) {
strcpy (input, "");
return K_READ_MORE;
}
if (key == XK_Return) {
*inpmode = 0;
return K_READ_DONE;
}
if (key == XK_BackSpace || key == XK_Left) {
if (l > 0) input[l-1] = '\0';
return K_READ_MORE;
}
if (key == XK_Escape) {
strcpy (input, "");
*inpmode = 0;
return K_READ_DONE;
}
if (count > 0) {
input[l] = buff[0];
input[l+1] = '\0';
}
return K_READ_MORE;
}
/* handle keys with Control pressed */
if (ctrl) {
/* start animation as continuous rotation */
if (key == XK_Right) control_animation (11);
if (key == XK_Left) control_animation (12);
if (key == XK_Up) control_animation (13);
if (key == XK_Down) control_animation (14);
if (key == XK_comma) control_animation (15);
if (key == XK_period) control_animation (16);
/* quick print */
if (key == XK_p) {
handle_print ("", "", 0);
close_print (0);
sprintf (gmsg, "Wrote %s", prf);
}
/* animation print on/off */
if (key == XK_o) {
if (animation_print) {
animation_print = 0;
*anim_msg = 0;
sprintf (gmsg, "Animation print off");
}
else {
animation_print = 1;
*anim_msg = 0;
sprintf (gmsg, "Animation print on");
}
}
return K_NOP;
}
/* next few keys control animation */
if (key == XK_A) { /* start animation 1,2,...N,1,2 */
control_animation (1);
return K_RESETUP;
}
if (key == XK_B) { /* start animation N,N-1,N-2..,1,N, */
control_animation (2);
return K_RESETUP;
}
if (key == XK_C) { /* start animation 1,2, ..., N,N-1,N-2, .. 3,1,2,3...*/
control_animation (3);
return K_RESETUP;
}
if (key == XK_less) {
fps -= (fps <= 10 ? 1 : 2);
if (fps < 1) fps = 1;
msecs = 1000 / fps;
sprintf(gmsg, "Animation uses %d fps", fps);
return K_NOP;
}
if (key == XK_greater) {
fps += (fps < 10 ? 1 : 2);
if (fps > 20) fps = 20;
msecs = 1000 / fps;
sprintf(gmsg, "Animation uses %d fps", fps);
return K_NOP;
}
if (key == XK_Escape) {
if (animate > 0) {
control_animation (0);
return K_NOP;
}
return K_NOP;
}
if (key == XK_bracketright) {
if (iframe == nframe-1) { sprintf(gmsg,"Last frame"); return K_NOP; }
iframe = iframe + fstep;
if (iframe >= nframe) iframe = nframe - 1;
return K_RESETUP;
}
if (key == XK_bracketleft) {
if (iframe == 0) {sprintf(gmsg,"First frame"); return K_NOP; }
iframe = iframe - fstep;
if (iframe <=0 ) iframe = 0;
return K_RESETUP;
}
if (key == XK_backslash) {
saveframe = iframe;
sprintf (gmsg, "Marked frame %d", saveframe+1);
return K_NOP;
}
if (key == XK_bar) {
if (saveframe==-99) {sprintf(emsg,"No frame was marked"); return K_NOP; }
if (saveframe<0 || saveframe>=nframe) {
sprintf(emsg,"Marked frame %d does not exist", saveframe+1); return K_NOP; }
if (iframe==saveframe) {sprintf(gmsg,"Already at frame %d", saveframe+1); return K_NOP; }
iframe=saveframe;
sprintf(gmsg,"Go back to frame %d", saveframe+1);
return K_RESETUP;
}
if (key == XK_braceleft) {iframe=0; return K_RESETUP; }
if (key == XK_braceright) {iframe=nframe-1; return K_RESETUP; }
if (key == XK_Q) return K_QUIT;
if (key == XK_U) return K_UPDATE;
if (key == XK_h) { wrhelp=1-wrhelp; return K_REPLOT;}
if (key == XK_i) {
if (animate) { control_animation (0); *gmsg = 0; }
strcpy(input, "");
*inpmode = 1;
svline=0;
return K_READ_MORE; }
*alfa=0; *ixyz=0;
if (key == XK_Right) {*alfa = dalfa; *ixyz=1; return K_REPLOT;}
if (key == XK_Left) {*alfa = -dalfa; *ixyz=1; return K_REPLOT;}
if (key == XK_Up) {*alfa = -dalfa; *ixyz=2; return K_REPLOT;}
if (key == XK_Down) {*alfa = dalfa; *ixyz=2; return K_REPLOT;}
if (key == XK_comma) {*alfa = dalfa; *ixyz=3; return K_REPLOT;}
if (key == XK_period) {*alfa = -dalfa; *ixyz=3; return K_REPLOT;}
if (key == XK_p) {pmode++; if (pmode==3) pmode=0; return K_REPLOT;}
if (key == XK_P) {pmode--; if (pmode==-1) pmode=2; return K_REPLOT;}
if (key == XK_d) { dist0 = dist0*1.05 ; return K_REPLOT;}
if (key == XK_D) { dist0 = dist0/1.05 ; return K_REPLOT;}
if (key == XK_r) { control_animation(0); return K_REPLOT;}
if (key == XK_plus || key == XK_KP_Add)
{scale = scale*1.05; return K_REPLOT;}
if (key==XK_minus || key==XK_KP_Subtract)
{scale = scale/1.05; return K_REPLOT;}
if (key == XK_KP_6) {taux = taux + dtaux; return K_REPLOT;}
if (key == XK_KP_4) {taux = taux - dtaux; return K_REPLOT;}
if (key == XK_KP_8) {tauy = tauy + dtauy; return K_REPLOT;}
if (key == XK_KP_2) {tauy = tauy - dtauy; return K_REPLOT;}
if (key == XK_KP_7) {taux = taux0; tauy = tauy0; return K_REPLOT;}
if (key == XK_KP_Multiply) {
taux0 = taux; tauy0 = tauy;
sprintf(gmsg, "New home position: %.2f %.2f", taux0, tauy0);
chginfo=1;
return K_NOP;
}
if (key == XK_l) {
bline=1-bline;
if (withbonds) return K_REPLOT;
chginfo=1;
if (bline) sprintf (gmsg, "Bonds as lines ");
else sprintf (gmsg, "Bonds as cylinders ");
return K_NOP;
}
if (key == XK_L) {
showlines=showlines+1;
if (showlines == 3) showlines=0;
chginfo=1;
if (showlines == 1) sprintf (gmsg, "Draw extra lines lines ");
if (showlines == 2) sprintf (gmsg, "Dash extra lines lines ");
if (showlines == 0) sprintf (gmsg, "Supress extra lines lines ");
if (nxline>0) return K_REPLOT;
}
if (key == XK_s) { shadow=1-shadow; return K_REPLOT; }
if (key == XK_a) { showaxes=1-showaxes; return K_REPLOT; }
if (key == XK_w) { wire=1-wire; return K_REPLOT; }
if (key == XK_x) {
chginfo=1;
usepixmap=1-usepixmap;
if(usepixmap) sprintf (gmsg, "Draw to pixmap buffer");
else sprintf (gmsg, "Draw to screen");
chginfo=1;
return K_NOP;
}
if (key == XK_space) {wrinfo=1-wrinfo; return K_REPLOT; }
if (key == XK_b) { withbonds=1-withbonds; return K_REPLOT; }
if (key == XK_n) {
numbers=numbers+1; if (numbers == 3) numbers=0; return K_REPLOT;
}
if (key == XK_c) { coords=1-coords; return K_REPLOT; }
if (key == XK_N) { bondnums=1-bondnums; return K_REPLOT;}
if (key == XK_g) { grayvalues=1-grayvalues; return K_REPLOT;}
return K_NOP;
}
/* --- wln --- */
void wln (char x[])
{
int xx;
if (!startup) {
xx=xln;
if (xx<1) xx=1;
XDrawString (dpy, drw, labelgc,xx, 10*lnum, x, strlen(x));
lnum++;
}
else
printf ("%s\n", x);
}
/* --- WriteHelp --- */
void WriteHelp ()
{
lnum=1;
/* commands */
xln=igw-250;
wln("COMMANDS for input line:");
wln(" help cmd - on-line help");
wln(" inc degrees - rotation increment");
wln(" tell ijkl - tell geometry");
wln(" pos x y - set position");
wln(" dpos dxy - shift increment");
wln(" dist d - set distance");
wln(" rfac fac - scale all radii");
wln(" bfac fac - scale all bonds");
wln(" scale fac - scale plot");
wln(" gramp slope mid - gray ramp");