-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.c
4063 lines (3037 loc) · 104 KB
/
file.c
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
/*
Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Matthew P. Hodges
This file is part of XMakemol.
XMakemol 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, or (at your option)
any later version.
XMakemol 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 XMakemol; see the file COPYING. If not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define __FILE_C__
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if HAVE_ALLOCA_H
#include <alloca.h>
#endif
#include <ctype.h> /* aro - needed for isalnum() */
#include <dirent.h>
#include <locale.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* #include <libgen.h> */
char * gnu_basename (char *);
#include <Xm/FileSB.h>
#include <Xm/Label.h>
#include <Xm/List.h>
#include <Xm/MessageB.h> /* aro */
#include <Xm/PushB.h> /* aro */
#include <Xm/RowColumn.h>/* aro */
#include <Xm/TextF.h> /* aro */
#include <Xm/ToggleB.h> /* aro */
#include "globals.h"
#include "bonds.h"
#include "defs.h"
#include "draw.h"
#ifdef GL
#include "gl_funcs.h" /* aro - for visinfo */
#include "gl2ps.h"
#endif /* GL */
#include "view.h"
#include "bbox.h" /* wjq - for bbox_xyz */
#ifdef XPM
#include <X11/xpm.h>
#endif
void echo_to_message_area(char *);
struct frame * get_first_frame (void);
XmString get_current_directory (void);
void set_current_directory (XmString dir);
void place_dialog_cb (Widget, XtPointer, XtPointer);
static int no_elements;
static int bond_adjacency_lists_allocated_size;
//static int file_type = 0;
static int export_type = 0;
enum merge_types {MERGE_FRAME, MERGE_ALL};
static enum merge_types merge_type = MERGE_FRAME;
static Boolean aux_file_exists_p;
static Boolean aux_file_error_p;
Widget p_dialog = NULL;/* aro */
static struct frame *first_frame, *current_frame;
struct type
{
int elem; /* corresponding element */
char label[4];
struct type *next;
};
static struct type *first_type, *current_type;
/* directory that was accessed in any of the file selection dialogs */
static XmString current_directory = NULL;
void
file_cb(Widget widget, XtPointer client_data, XtPointer call_data)
{
void open_file(char *, Boolean);
/* aro:
Updates here: added print_file callback for selection box
containing list of printers.
Also added:
int i, j;
all char * and arrays;
FILE *printcap_file;
Widgets p_dialog, print_printer, temp_widget;
XmStringTable printer_xmstrings. */
void export_type_cb(Widget, XtPointer, XtPointer);
void file_type_cb(Widget, XtPointer, XtPointer);
void load_file_cb(Widget, XtPointer, XtPointer);
void save_file_cb(Widget, XtPointer, XtPointer);
void merge_file_cb(Widget, XtPointer, XtPointer);
void export_file_cb(Widget, XtPointer, XtPointer);
void print_file_cb(Widget, XtPointer, XtPointer);/* aro */
void print_file_select_cb(Widget, XtPointer, XtPointer);/* aro */
void cleanup_cb(Widget, XtPointer, XtPointer);/* aro */
int item_no=(int) client_data, i, j;/* aro */
char *printer_name, *line_ptr, line[1024], printer_strings[MAX_PRINTERS+1][80],
print_command_string[1024];/* aro */
FILE *printcap_file;/* aro */
static Widget o_dialog,s_dialog,m_dialog,e_dialog;
Widget rc[2], export_type_w, file_type_w, print_printer, print_command, temp_widget;/* aro */
XmString title, bw_fig, col_fig, as_all, as_fra, con_fra, eps_bw, eps_co, aux;
XmStringTable printer_xmstrings[MAX_PRINTERS];/* aro */
#ifdef XPM
XmString xpm;
#endif
#ifdef GL
XmString gl2ps_eps, gl2ps_pdf, gl2ps_svg;
#endif
switch(item_no){
case 0:
if (!o_dialog) {
o_dialog = XmCreateFileSelectionDialog
(toplevel, "file_sel", NULL, 0);
title=XmStringCreateLocalized ("Open File");
XtVaSetValues(o_dialog, XmNdialogTitle, title, NULL);
title = XmStringCreateLocalized("*.xyz");
XtVaSetValues(o_dialog, XmNpattern, title, NULL);
XmStringFree (title);
/* okCallback calls load_file_cb(), cancelCallback unmanages the
FileSelectionDialog widget */
XtAddCallback (o_dialog, XmNokCallback, load_file_cb, NULL);
XtAddCallback (o_dialog, XmNcancelCallback,
(XtCallbackProc)XtUnmanageChild, NULL);
/* No help available ... */
XtUnmanageChild
(XmFileSelectionBoxGetChild(o_dialog,XmDIALOG_HELP_BUTTON));
}
/* Change to a directory that was just recently operated in any of the
file selection dialogs and refresh the file list so that newly
created files do show up */
XtVaSetValues (o_dialog, XmNdirectory, get_current_directory (), NULL);
XmFileSelectionDoSearch (o_dialog, NULL);
XtAddCallback (o_dialog, XmNmapCallback, place_dialog_cb, NULL);
XtManageChild (o_dialog);
XtPopup (XtParent (o_dialog), XtGrabNone);
break;
case 1:
open_file(current_file_name, TRUE);
break;
case 2:
if (!s_dialog) {
s_dialog = XmCreateFileSelectionDialog (toplevel, "file_sel", NULL, 0);
title=XmStringCreateLocalized ("Save File");
XtVaSetValues(s_dialog, XmNdialogTitle, title, NULL);
/* okCallback calls save_file(), cancelCallback unmanages the
FileSelectionDialog widget */
as_all = XmStringCreateLocalized("XYZ (all)");
as_fra = XmStringCreateLocalized("XYZ (frame)");
con_fra = XmStringCreateLocalized("XYZ + connectivities (frame)");
aux = XmStringCreateLocalized ("Auxiliary info");
title=XmStringCreateLocalized ("File type");
file_type_w=XmVaCreateSimpleOptionMenu
(s_dialog,"File type",title,'F', 0, file_type_cb,
XmVaPUSHBUTTON, as_all, 'X', NULL, NULL,
XmVaPUSHBUTTON, as_fra, 'Y', NULL, NULL,
XmVaPUSHBUTTON, con_fra, 'Z', NULL, NULL,
XmVaPUSHBUTTON, aux, 'A', NULL, NULL,
NULL);
XtManageChild(file_type_w);
XmStringFree(as_all);
XmStringFree(as_fra);
XmStringFree(con_fra);
XmStringFree (aux);
XmStringFree(title);
XtAddCallback(s_dialog, XmNokCallback, save_file_cb, NULL);
XtAddCallback(s_dialog, XmNcancelCallback,
(XtCallbackProc)XtUnmanageChild, NULL);
/* No help available ... */
XtUnmanageChild
(XmFileSelectionBoxGetChild(s_dialog,XmDIALOG_HELP_BUTTON));
}
/* Change to a directory that was just recently operated in any of the
file selection dialogs and refresh the file list so that newly
created files do show up */
XtVaSetValues (s_dialog, XmNdirectory, get_current_directory (), NULL);
XmFileSelectionDoSearch (s_dialog, NULL);
XtAddCallback (s_dialog, XmNmapCallback, place_dialog_cb, NULL);
XtManageChild (s_dialog);
XtPopup (XtParent (s_dialog), XtGrabNone);
break;
case 3:
if (!m_dialog) {
m_dialog = XmCreateFileSelectionDialog
(toplevel, "file_sel", NULL, 0);
title=XmStringCreateLocalized ("Merge With File");
XtVaSetValues (m_dialog, XmNdialogTitle, title, NULL);
title = XmStringCreateLocalized ("*.xyz");
XtVaSetValues (m_dialog, XmNpattern, title, NULL);
{
void merge_type_cb (Widget, XtPointer, XtPointer);
Widget merge_type_w;
XmString merge_frame, merge_all;
merge_frame = XmStringCreateLocalized ("Use first frame");
merge_all = XmStringCreateLocalized ("Use all frames");
title = XmStringCreateLocalized ("Merge type");
merge_type_w = XmVaCreateSimpleOptionMenu
(m_dialog,
"Merge type",
title,
'M', 0, merge_type_cb,
XmVaPUSHBUTTON, merge_frame, 'f', NULL, NULL,
XmVaPUSHBUTTON, merge_all, 'a', NULL, NULL,
NULL);
XtManageChild (merge_type_w);
XmStringFree (merge_frame);
XmStringFree (merge_all);
}
XmStringFree (title);
XtAddCallback (m_dialog, XmNokCallback, merge_file_cb, NULL);
XtAddCallback (m_dialog, XmNcancelCallback,
(XtCallbackProc)XtUnmanageChild, NULL);
/* No help available ... */
XtUnmanageChild
(XmFileSelectionBoxGetChild(m_dialog,XmDIALOG_HELP_BUTTON));
}
/* Change to a directory that was just recently operated in any of the
file selection dialogs and refresh the file list so that newly
created files do show up */
XtVaSetValues (m_dialog, XmNdirectory, get_current_directory (), NULL);
XmFileSelectionDoSearch (m_dialog, NULL);
XtAddCallback (m_dialog, XmNmapCallback, place_dialog_cb, NULL);
XtManageChild (m_dialog);
XtPopup (XtParent (m_dialog), XtGrabNone);
break;
case 4:
if (!e_dialog) {
e_dialog = XmCreateFileSelectionDialog (toplevel, "file_sel", NULL, 0);
title=XmStringCreateLocalized ("Export File");
XtVaSetValues(e_dialog, XmNdialogTitle, title, NULL);
/* okCallback calls save_file(), cancelCallback unmanages the
FileSelectionDialog widget */
bw_fig = XmStringCreateLocalized("Fig (b/w)");
col_fig = XmStringCreateLocalized("Fig (colour)");
eps_bw = XmStringCreateLocalized("EPS (b/w)");
eps_co = XmStringCreateLocalized("EPS (colour)");
#ifdef XPM
xpm = XmStringCreateLocalized("XPM");
#endif
#ifdef GL
gl2ps_eps = XmStringCreateLocalized("GL2PS (EPS)");
gl2ps_pdf = XmStringCreateLocalized("GL2PS (PDF)");
gl2ps_svg = XmStringCreateLocalized("GL2PS (SVG)");
#endif
title=XmStringCreateLocalized ("File type");
export_type_w=XmVaCreateSimpleOptionMenu
(e_dialog,"File type",title,'F', 0, export_type_cb,
XmVaPUSHBUTTON, bw_fig, 'S', NULL, NULL,
XmVaPUSHBUTTON, col_fig, 'T', NULL, NULL,
XmVaPUSHBUTTON, eps_bw, 'E', NULL, NULL,
XmVaPUSHBUTTON, eps_co, 'P', NULL, NULL,
#ifdef XPM
XmVaPUSHBUTTON, xpm, 'X', NULL, NULL,
#endif
#ifdef GL
XmVaPUSHBUTTON, gl2ps_eps, 'G', NULL, NULL,
XmVaPUSHBUTTON, gl2ps_pdf, 'P', NULL, NULL,
XmVaPUSHBUTTON, gl2ps_svg, 'V', NULL, NULL,
#endif
NULL);
XtManageChild(export_type_w);
XmStringFree (bw_fig);
XmStringFree (col_fig);
XmStringFree(eps_bw);
XmStringFree(eps_co);
#ifdef XPM
XmStringFree(xpm);
#endif
#ifdef GL
XmStringFree (gl2ps_eps);
XmStringFree (gl2ps_pdf);
XmStringFree (gl2ps_svg);
#endif
XmStringFree(title);
XtAddCallback(e_dialog, XmNokCallback, export_file_cb, (XtPointer) 3);
XtAddCallback(e_dialog, XmNcancelCallback,
(XtCallbackProc)XtUnmanageChild, NULL);
/* No help available ... */
XtUnmanageChild
(XmFileSelectionBoxGetChild(e_dialog,XmDIALOG_HELP_BUTTON));
}
/* Change to a directory that was just recently operated in any of the
file selection dialogs and refresh the file list so that newly
created files do show up */
XtVaSetValues (e_dialog, XmNdirectory, get_current_directory (), NULL);
XmFileSelectionDoSearch (e_dialog, NULL);
XtAddCallback (e_dialog, XmNmapCallback, place_dialog_cb, NULL);
XtManageChild (e_dialog);
XtPopup (XtParent (e_dialog), XtGrabNone);
break;
case 5:/* aro: Create print dialog */
if (!p_dialog) {
p_dialog = XmCreateMessageDialog(toplevel, "print dialog", NULL, 0);
title=XmStringCreateLocalized ("Print File");
XtVaSetValues(p_dialog, XmNdialogTitle, title, NULL);
XmStringFree (title);
/* Change OK button label to "Print" */
temp_widget = XmMessageBoxGetChild(p_dialog, XmDIALOG_OK_BUTTON);
title=XmStringCreateLocalized("Print");
XtVaSetValues(temp_widget, XmNlabelString, title, NULL);
XmStringFree (title);
/* Main RowColumn */
rc[0] = XtVaCreateManagedWidget("rc_main",
xmRowColumnWidgetClass,
p_dialog,
XmNorientation, XmVERTICAL,
NULL);
/* First sub RowColumn */
rc[1] = XtVaCreateManagedWidget("rc_sub_1",
xmRowColumnWidgetClass,
rc[0],
XmNorientation, XmHORIZONTAL,
NULL);
XtVaCreateManagedWidget("File:",
xmLabelWidgetClass,
rc[1],
NULL);
/* If current_file_name is an empty string, assume no file
has been loaded */
if(strcmp(current_file_name, "") == 0)
title = XmStringCreateLocalized ("(no file loaded)");
else
title = XmStringCreateLocalized (current_file_name);
XtVaCreateManagedWidget("file_name",
xmLabelWidgetClass,
rc[1],
XmNlabelString, title,
NULL);
XmStringFree(title);
/* Second sub RowColumn */
rc[1] = XtVaCreateManagedWidget("rc_sub_2",
xmRowColumnWidgetClass,
rc[0],
XmNorientation, XmHORIZONTAL,
NULL);
XtVaCreateManagedWidget("Printer:",
xmLabelWidgetClass,
rc[1],
NULL);
/* Parse /etc/printcap to get list of available printers.
User can still type in an arbitrary name */
printcap_file = fopen("/etc/printcap", "r");
i = 0, j = 0;
if(printcap_file != NULL)
{
line_ptr = fgets(line, sizeof(line), printcap_file);
while(line_ptr != NULL && j < MAX_PRINTERS)
{
/* If line[i] is an alphanumeric character, assume it is
part of printer name */
if(isalnum((int)line[i]))
{
printer_strings[j][i] = line[i];
i++;
}
/* If line[i] is '|' or ':' then we've reached end of
printer name, append '\0' to printer_strings
and get next line */
else if(line[i] == '|' || line[i] == ':')
{
printer_strings[j][i] = '\0';
line_ptr = fgets(line, sizeof(line), printcap_file);
i = 0;
j++;
}
/* If line[i] is not an alphanumeric character and
it is not '|' or ':' then get next line of printcap_file */
else
{
line_ptr = fgets(line, sizeof(line), printcap_file);
i = 0;
}
}
fclose(printcap_file);
}
/* Set first empty element of printer_strings to "ENDLIST" to denote end.
If failure in opening printcap_file, then j == 0 */
strcpy(printer_strings[j], "ENDLIST");
/* Get default printer name if one of the environment
variables LPDEST or PRINTER is set */
printer_name = getenv("LPDEST");
if(printer_name == NULL)
printer_name = getenv("PRINTER");
if(printer_name == NULL)
{
printer_name = alloca(32);
strcpy(printer_name, "(no default)");
}
/* Create printer name text field initialized with name of
default printer */
print_printer = XtVaCreateManagedWidget("p_name",
xmTextFieldWidgetClass,
rc[1],
XmNcolumns, 20,
XmNvalue, printer_name,
NULL);
/* Third sub RowColumn */
rc[1] = XtVaCreateManagedWidget("rc_sub_3",
xmRowColumnWidgetClass,
rc[0],
NULL);
/* Convert printer strings to XmStrings */
for(j = 0; strcmp(printer_strings[j], "ENDLIST"); j++)
printer_xmstrings[j] = (XmString *)XmStringCreateLocalized(printer_strings[j]);
/* Create scrolled list with names of first MAX_PRINTERS printers from
print_cap file */
i = 0;
XtSetArg(args[i], XmNselectionPolicy, XmSINGLE_SELECT); i++;
/* if j = 0, then no printers were found in /etc/printcap,
so we don't want to set XmNitems to printer_xmstrings,
or we'll get a warning about XmNitemCount not agreeing
with XmNitems */
if(j != 0) {XtSetArg(args[i], XmNitems, printer_xmstrings); i++;}
XtSetArg(args[i], XmNitemCount, j); i++;
XtSetArg(args[i], XmNvisibleItemCount, 5); i++;
temp_widget = XmCreateScrolledList(rc[1], "Printers", args, i);
XtManageChild(temp_widget);
XtAddCallback(temp_widget, XmNsingleSelectionCallback,
print_file_select_cb, print_printer);
XtVaCreateManagedWidget("Copies:",
xmLabelWidgetClass,
rc[1],
NULL);
/* Create text field to let user input number of copies to
be printed */
XtVaCreateManagedWidget("num_copies",
xmTextFieldWidgetClass,
rc[1],
XmNvalue, "1",
XmNcolumns, 4,
NULL);
/* Create radio box to let user select color or black & white
output */
rc[1] = XmCreateRadioBox(rc[0],"color/bw",NULL,0);
XtVaSetValues(rc[1], XmNorientation, XmHORIZONTAL, NULL);
XtVaCreateManagedWidget("color",
xmToggleButtonWidgetClass,
rc[1],
XmNset, True,
NULL);
XtVaCreateManagedWidget("bw",
xmToggleButtonWidgetClass,
rc[1],
NULL);
XtManageChild(rc[1]);
/* Create Label and TextField to display current print command and format */
XtVaCreateManagedWidget("Print command and format:",
xmLabelWidgetClass,
rc[0],
NULL);
sprintf(print_command_string, "lpr -P%%printer%% -#%%num_copies%% %%filename%%");
print_command = XtVaCreateManagedWidget("print_command_label",
xmTextFieldWidgetClass,
rc[0],
XmNvalue, print_command_string,
NULL);
XtAddCallback(p_dialog, XmNokCallback, print_file_cb, print_command);
/* No help available ... */
XtUnmanageChild
(XmMessageBoxGetChild(p_dialog,XmDIALOG_HELP_BUTTON));
}else{
/* Update file name */
temp_widget = XtNameToWidget(p_dialog, "rc_main.rc_sub_1.file_name");
/* If current_file_name is an empty string, assume no file
has been loaded */
if(strcmp(current_file_name, "") == 0)
title = XmStringCreateLocalized ("(no file loaded)");
else
title = XmStringCreateLocalized (current_file_name);
XtVaSetValues(temp_widget, XmNlabelString, title, NULL);
XmStringFree(title);
}
XtAddCallback (p_dialog, XmNmapCallback, place_dialog_cb, NULL);
XtManageChild (p_dialog);
XtPopup (XtParent (p_dialog), XtGrabNone);
break;
case 6:
cleanup_cb(toplevel, NULL, NULL); /* aro - cleanup before exit */
exit (0);
break;
}
}
void
file_type_cb(Widget widget, XtPointer client_data, XtPointer call_data)
{
file_type = (int) client_data;
}
void
export_type_cb(Widget widget, XtPointer client_data, XtPointer call_data)
{
export_type = (int) client_data;
}
void
merge_type_cb (Widget widget,
XtPointer client_data,
XtPointer call_data)
{
merge_type = (enum merge_types) client_data;
}
void
load_file_cb(Widget dialog, XtPointer client_data, XtPointer call_data)
{
void open_file(char *, Boolean);
/* aro - moved char file_name[1024] to char current_file_name[1024]
in globals.h */
char *file = NULL;
XmFileSelectionBoxCallbackStruct *cbs =
(XmFileSelectionBoxCallbackStruct *) call_data;
if (cbs) {
if (!XmStringGetLtoR (cbs->value, XmFONTLIST_DEFAULT_TAG, &file))
return; /* internal error */
/* Save directory shown in the dialog so that all the other file selection
dialogs could be made to operate the very same directory */
set_current_directory (cbs->dir);
/* save file name and open the file */
(void) strcpy(current_file_name, file);
open_file(current_file_name, FALSE);
XtFree (file); /* free allocated data from XmStringGetLtoR() */
}
XtUnmanageChild(dialog);
}
void
save_file_cb(Widget dialog, XtPointer client_data, XtPointer call_data)
{
void write_file(char *);
char *file_name, message_string[1024];
XmFileSelectionBoxCallbackStruct *cbs =
(XmFileSelectionBoxCallbackStruct *) call_data;
if (cbs) {
if (!XmStringGetLtoR (cbs->value, XmFONTLIST_DEFAULT_TAG, &file_name))
return; /* internal error */
/* Save directory shown in the dialog so that all the other file selection
dialogs could be made to operate the very same directory */
set_current_directory (cbs->dir);
/* Check if we can open the file */
if(fopen(file_name, "w") == NULL)
{
sprintf(message_string, "Cannot open %s for write", file_name);
echo_to_message_area(message_string);
}
else
{
write_file(file_name);
}
XtFree (file_name); /* free allocated data from XmStringGetLtoR() */
}
XtUnmanageChild(dialog);
}
void
merge_file_cb (Widget dialog,
XtPointer client_data,
XtPointer call_data)
{
double get_angle_axis(double *);
void open_file(char *, Boolean);
void rotate_atoms(double *,double,Boolean,Boolean);
void frame_content_to_atoms (int);
struct frame * get_selected_frame (void);
int i, j, merge_no_atoms;
char *file = NULL, buf[BUFSIZ], eof_string[32] = "";
char merge_file_name[1024], *temp_file_name;
double angle, axis[3];
Boolean eof_merge_file = False;
FILE *merge_file, *temp_file;
struct frame *this_frame;
XmFileSelectionBoxCallbackStruct *cbs =
(XmFileSelectionBoxCallbackStruct *) call_data;
if (cbs) {
if (!XmStringGetLtoR (cbs->value, XmFONTLIST_DEFAULT_TAG, &file))
return; /* internal error */
(void) strcpy (merge_file_name, file);
XtFree (file); /* free allocated data from XmStringGetLtoR() */
/* Save directory shown in the dialog so that all the other file
selection dialogs could be made to operate the very same directory */
set_current_directory (cbs->dir);
}
XtUnmanageChild(dialog);
merge_file = fopen (merge_file_name, "r");
if(merge_file == NULL)
{
sprintf(buf,"No file %s",gnu_basename(merge_file_name));
echo_to_message_area(buf);
return;
}
/* Set up temporary file */
temp_file_name = alloca (32);
strcpy (temp_file_name, "/tmp/xmakemol.XXXXXX");
temp_file = fdopen (mkstemp (temp_file_name), "w");
if(temp_file == NULL)
{
sprintf(buf,"Cannot open %s",temp_file_name);
echo_to_message_area(buf);
return;
}
/* Make sure none of this is drawn */
inhibit_canvas_callback = 1;
/* Reset the angle_axis_matrix */
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
angle_axis_matrix[i][j] = global_matrix[i][j];
}
}
/* Loop over frames */
for(i = 0; i < no_frames; i++)
{
int frame_visible_atoms;
int count_visible_atoms_in_frame (struct frame *);
/* Rewind the merge file if appropriate */
if (merge_type == MERGE_FRAME)
{
rewind (merge_file);
}
/* Check to see if we have reached the end of the merge file */
if (eof_merge_file == False)
{
(void)fgets(buf, sizeof (buf), merge_file);
if (feof (merge_file))
{
merge_no_atoms = 0;
eof_merge_file = True;
sprintf (eof_string, " - merge stopped after frame %d", i);
}
else
{
sscanf (buf, "%d", &merge_no_atoms);
/* Gobble the comment line */
(void)fgets (buf, sizeof (buf), merge_file);
}
}
frame_content_to_atoms (i);
this_frame = get_selected_frame();
frame_visible_atoms =
count_visible_atoms_in_frame (this_frame);
fprintf(temp_file, "%d\n", frame_visible_atoms + merge_no_atoms);
fprintf(temp_file, "%s\n", this_frame->comment);
angle = get_angle_axis(axis);
rotate_atoms(axis, angle, 0, False);
for(j = 0; j < no_atoms; j++)
{
if(atoms[j].visi == 1)
{
/* We need to add the global_vector component as there
may have been translations */
fprintf(temp_file,"%2s %12.6f %12.6f %12.6f\n",
atoms[j].label,
atoms[j].x + global_vector[0],
atoms[j].y + global_vector[1],
atoms[j].z + global_vector[2]);
}
}
/* Now insert the merged frame if available */
if (eof_merge_file == False)
{
for (j = 0; j < merge_no_atoms; j++)
{
(void)fgets (buf, sizeof (buf), merge_file);
fputs (buf, temp_file);
}
}
}
fclose(temp_file);
inhibit_canvas_callback = 0;
open_file(temp_file_name, FALSE);
sprintf(buf,"Files merged%s", eof_string);
echo_to_message_area(buf);
unlink (temp_file_name);
/* If the Edit->Positions dialog is open, make the merged atoms the
only ones that we can move */
if (edit_posn_dialog != NULL)
{
for (i = 0; i < no_atoms; i++)
{
if (i < no_atoms - (merge_no_atoms))
{
atoms[i].edit = 0;
}
}
}
}
void
export_file_cb(Widget dialog, XtPointer client_data, XtPointer call_data)
{
void export_file(char *);
char *file_name, message_string[1024];
XmFileSelectionBoxCallbackStruct *cbs =
(XmFileSelectionBoxCallbackStruct *) call_data;
if (cbs) {
if (!XmStringGetLtoR (cbs->value, XmFONTLIST_DEFAULT_TAG, &file_name))
return; /* internal error */
/* Save directory shown in the dialog so that all the other file selection
dialogs could be made to operate the very same directory */
set_current_directory (cbs->dir);
/* Check if we can open the file */
if(fopen(file_name, "w") == NULL)
{
sprintf(message_string, "Cannot open %s for write", file_name);
echo_to_message_area(message_string);
}
else
{
export_file(file_name);
}
XtFree (file_name); /* free allocated data from XmStringGetLtoR() */
}
XtUnmanageChild(dialog);
}
void
print_file_cb(Widget widget, XtPointer client_data, XtPointer call_data)
{
void write_eps(char *, FILE *, Boolean);
void echo_to_message_area(char *);
char *string, *temp_file_name, *p_name, *copies, p_command[1024];
const char printer_str[] = "%printer%",
numcopies_str[] = "%num_copies%",
filename_str[] = "%filename%";
char *printer_pos, *numcopies_pos, *filename_pos;
int num_copies, i, j;
Boolean print_color;
Widget temp_w, print_command = (Widget) client_data;
FILE * temp_f, *eps_file;
XmString errorMsg;
/* Get widget ID of color toggle */
temp_w = XtNameToWidget(widget, "rc_main.color/bw.color");
/* Get value of color toggle (true or false) */
XtVaGetValues(temp_w, XmNset, &print_color, NULL);
/* Create unique file name in /tmp */
temp_file_name = alloca (32);
strcpy (temp_file_name, "/tmp/xmakemol.XXXXXX");
eps_file = fdopen (mkstemp (temp_file_name), "w");
/* Write eps file to /tmp for printing */
write_eps ("", eps_file, print_color);
/* Get the name of the printer selected */
temp_w = XtNameToWidget(widget, "rc_main.rc_sub_2.p_name");
XtVaGetValues(temp_w, XmNvalue, &p_name, NULL);
/* Get the number of copies to be printed */
temp_w = XtNameToWidget(widget, "rc_main.rc_sub_3.num_copies");
XtVaGetValues(temp_w, XmNvalue, &copies, NULL);
num_copies = atoi(copies);
/* Determine whether user is printing in color or black & white */
temp_w = XtNameToWidget(widget, "rc_main.color/bw.color");
XtVaGetValues(temp_w, XmNset, &print_color, NULL);
if(strcmp(p_name, "(no default)") == 0 ||
strcmp(p_name, "") == 0)
{
echo_to_message_area("Print Error - No Printer Selected");
}
else if(!(temp_f = fopen(current_file_name, "r")))
{
echo_to_message_area("Print Error - No File Loaded");
}
else
{
fclose(temp_f);