File: medview.c

package info (click to toggle)
ncbi-tools6 6.1.20120620-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 241,628 kB
  • ctags: 101,236
  • sloc: ansic: 1,431,713; cpp: 6,248; pascal: 3,949; xml: 3,390; sh: 3,090; perl: 1,077; csh: 488; makefile: 449; ruby: 93; lisp: 81
file content (1217 lines) | stat: -rw-r--r-- 35,152 bytes parent folder | download | duplicates (13)
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
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
/*   medview.c
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*            National Center for Biotechnology Information (NCBI)
*
*  This software/database is a "United States Government Work" under the
*  terms of the United States Copyright Act.  It was written as part of
*  the author's official duties as a United States Government employee and
*  thus cannot be copyrighted.  This software/database is freely available
*  to the public for use. The National Library of Medicine and the U.S.
*  Government do not place any restriction on its use or reproduction.
*  We would, however, appreciate having the NCBI and the author cited in
*  any work or product based on this material
*
*  Although all reasonable efforts have been taken to ensure the accuracy
*  and reliability of the software and data, the NLM and the U.S.
*  Government do not and cannot warrant the performance or results that
*  may be obtained by using this software or data. The NLM and the U.S.
*  Government disclaim all warranties, express or implied, including
*  warranties of performance, merchantability or fitness for any particular
*  purpose.
*
* ===========================================================================
*
* File Name:  medview.c
*
* Author:  Jonathan Kans
*
* Version Creation Date:   4/30/95
*
* $Revision: 6.13 $
*
* File Description: 
*
* Modifications:  
* --------------------------------------------------------------------------
* Date     Name        Description of modification
* -------  ----------  -----------------------------------------------------
*
*
* ==========================================================================
*/

#include <medview.h>
#include <document.h>
#include <tomedlin.h>
#include <objmgr.h>
#include <accentr.h>
#include <prtutil.h>
#ifdef WIN_MOTIF
#include <netscape.h>
#endif

#define NUM_PAGES  6

typedef struct medlineviewform {
  FORM_MESSAGE_BLOCK

  Int2             currentPage;

  DoC              doc;
  TexT             text;
  GrouP            controls;
  Boolean          useScrollText;

  MedlineEntryPtr  mep;

  Boolean          cleanupObjectPtr;
  WndActnProc      activateForm;

  FonT             jourfnt;
  FonT             volfnt;
  FonT             pagesfnt;
  FonT             titlefnt;
  FonT             authorsfnt;
  FonT             affilfnt;
  FonT             abstractfnt;
  FonT             meshfnt;
  FonT             displayFont;
} MedlineViewForm, PNTR MedlineViewFormPtr;

static ParData medParFmt = {FALSE, FALSE, FALSE, FALSE, TRUE, 0, 0};
static ColData medColFmt = {0, 0, 80, 0, NULL, 'l', FALSE, FALSE, FALSE, FALSE, TRUE};

#define BUFSIZE 8192

static CharPtr  buffer;
static CharPtr  pos;

static void ClearString (void)

{
  pos = buffer;
  *pos = '\0';
}

static void AddString (CharPtr string)

{
  pos = StringMove (pos, string);
  *pos = '\0';
}

static ColData  colFmt [3] = {
  {0, 0, 0, 0, NULL, 'l', FALSE, FALSE, FALSE, FALSE, FALSE},
  {0, 0, 0, 0, NULL, 'l', FALSE, FALSE, FALSE, FALSE, FALSE},
  {0, 0, 0, 0, NULL, 'l', FALSE, FALSE, FALSE, FALSE, TRUE}
};

static ColData  mshFmt [1] = {
  {0, 0, 0, 0, NULL, 'l', FALSE, FALSE, FALSE, FALSE, TRUE}
};

static Boolean DisplayArticle (DoC d, MedlineEntryPtr mep, Boolean showMesh)

{
  size_t              len;
  MedlineViewFormPtr  mfp;
  MedlinePtr          mPtr;
  ParData             para;
  CharPtr             ptr;
  RecT                r;
  CharPtr             tmp;

  mfp = (MedlineViewFormPtr) GetObjectExtra (d);
  if (mfp != NULL) {
    mPtr = ParseMedline (mep);
    if (mPtr != NULL) {
      buffer = (CharPtr) MemNew (BUFSIZE);
      if (buffer != NULL) {
        para.openSpace = FALSE;
        para.keepWithNext = FALSE;
        para.keepTogether = FALSE;
        para.newPage = FALSE;
        para.tabStops = FALSE;
        para.minLines = 0;
        para.minHeight = 0;
        colFmt [0].font = mfp->jourfnt;
        colFmt [1].font = mfp->volfnt;
        colFmt [2].font = mfp->pagesfnt;
        ObjectRect (d, &r);
        InsetRect (&r, 4, 4);
        mshFmt [0].pixWidth = r.right - r.left;
        mshFmt [0].charWidth = 80;
        ClearString ();
        AddString (mPtr->journal);
        AddString ("\t");
        AddString (mPtr->volume);
        AddString (":\t");
        AddString (mPtr->pages);
        AddString ("  (");
        AddString (mPtr->year);
        AddString (")");
        AddString (" [");
        AddString (mPtr->uid);
        AddString ("]");
        AddString ("\n");
        AppendText (d, buffer, &para, colFmt, NULL);
        ClearString ();
        if (mPtr->title != NULL) {
          AddString (mPtr->title);
          AddString ("\n");
          AppendText (d, buffer, NULL, NULL, mfp->titlefnt);
          ClearString ();
        }
        if (mPtr->transl != NULL) {
          AddString ("[");
          AddString (mPtr->transl);
          AddString ("]\n");
          AppendText (d, buffer, NULL, NULL, mfp->titlefnt);
          ClearString ();
        }
        if (mPtr->title == NULL && mPtr->transl == NULL) {
        }
        AddString (mPtr->authors);
        AddString ("\n");
        AppendText (d, buffer, NULL, NULL, mfp->authorsfnt);
        ClearString ();
        if (mPtr->affil != NULL) {
          AddString (mPtr->affil);
          AddString ("\n");
          AppendText (d, buffer, NULL, NULL, mfp->affilfnt);
          ClearString ();
        }
        if (mPtr->abstract != NULL) {
          len = StringLen (mPtr->abstract);
          tmp = (CharPtr) MemNew (sizeof (Char) * (len + 10));
          ptr = StringMove (tmp, mPtr->abstract);
          ptr = StringMove (ptr, "\n");
          AppendText (d, tmp, NULL, NULL, mfp->abstractfnt);
          MemFree (tmp);
        }
        if (showMesh) {
          if (mPtr->mesh != NULL) {
            AppendText (d, "MeSH Terms:\n", NULL, NULL, mfp->meshfnt);
            AppendText (d, mPtr->mesh, &para, mshFmt, mfp->meshfnt);
          }
          if (mPtr->gene != NULL) {
            AppendText (d, "Gene Symbols:\n", NULL, NULL, mfp->meshfnt);
            AppendText (d, mPtr->gene, &para, mshFmt, mfp->meshfnt);
          }
          if (mPtr->substance != NULL) {
            AppendText (d, "Substances:\n", NULL, NULL, mfp->meshfnt);
            AppendText (d, mPtr->substance, &para, mshFmt, mfp->meshfnt);
          }
        }
        buffer = (CharPtr) MemFree (buffer);
      }
      mPtr = FreeMedline (mPtr);
    }
  }
  return TRUE;
}

static void MedlineEntryPtrToMedlineForm (ForM f, Pointer data)

{
  AsnIoPtr            aipout;
  FonT                fnt;
  FILE                *fp;
  MedlineEntryPtr     mep;
  MedlineViewFormPtr  mfp;
  Char                path [PATH_MAX];
  RecT                r;

  mfp = (MedlineViewFormPtr) GetObjectExtra (f);
  mep = (MedlineEntryPtr) data;
  if (mfp != NULL && mep != NULL) {
    if (mfp->useScrollText) {
      ObjectRect (mfp->text, &r);
    } else {
      ObjectRect (mfp->doc, &r);
    }
    InsetRect (&r, 4, 4);
    medColFmt.pixWidth = screenRect.right - screenRect.left;
    medColFmt.pixInset = 8;
    WatchCursor ();
    if (mfp->useScrollText) {
      Reset (mfp->text);
    } else {
      Reset (mfp->doc);
    }
    Update ();
    SetDocAutoAdjust (mfp->doc, FALSE);
    switch (mfp->currentPage) {
      case ABSTRACT_PAGE :
        if (mfp->useScrollText) {
          TmpNam (path);
          fp = FileOpen (path, "w");
          if (fp != NULL) {
            if (MedlineEntryToAbsFile (mep, fp)) {
              FileClose (fp);
              if (! FileToScrollText (mfp->text, path)) {
                SetTitle (mfp->text, "(Text is too large to be displayed in this control.)");
              }
            } else {
              FileClose (fp);
            }
          }
          FileRemove (path);
        } else {
          DisplayArticle (mfp->doc, mep, FALSE);
        }
        break;
      case CITATION_PAGE :
        if (mfp->useScrollText) {
          TmpNam (path);
          fp = FileOpen (path, "w");
          if (fp != NULL) {
            if (MedlineEntryToDocFile (mep, fp)) {
              FileClose (fp);
              if (! FileToScrollText (mfp->text, path)) {
                SetTitle (mfp->text, "(Text is too large to be displayed in this control.)");
              }
            } else {
              FileClose (fp);
            }
          }
          FileRemove (path);
        } else {
          DisplayArticle (mfp->doc, mep, TRUE);
        }
        break;
      case MEDLINE_PAGE :
        TmpNam (path);
        fp = FileOpen (path, "w");
        if (fp != NULL) {
          fnt = programFont;
          if (mfp->displayFont != NULL) {
            fnt = mfp->displayFont;
          }
          if (MedlineEntryToDataFile (mep, fp)) {
            FileClose (fp);
            if (mfp->useScrollText) {
              if (! FileToScrollText (mfp->text, path)) {
                SetTitle (mfp->text, "(Text is too large to be displayed in this control.)");
              }
            } else {
              DisplayFancy (mfp->doc, path, &medParFmt, &medColFmt, fnt, 4);
            }
          } else {
            FileClose (fp);
          }
        }
        FileRemove (path);
        break;
      case MEDASN1_PAGE :
        TmpNam (path);
        fp = FileOpen (path, "w");
        if (fp != NULL) {
          fnt = programFont;
          if (mfp->displayFont != NULL) {
            fnt = mfp->displayFont;
          }
          aipout = AsnIoNew (ASNIO_TEXT_OUT, fp, NULL, NULL, NULL);
          if (MedlineEntryAsnWrite (mep, aipout, NULL)) {
            AsnIoClose (aipout);
            if (mfp->useScrollText) {
              if (! FileToScrollText (mfp->text, path)) {
                SetTitle (mfp->text, "(Text is too large to be displayed in this control.)");
              }
            } else {
              DisplayFancy (mfp->doc, path, &medParFmt, &medColFmt, fnt, 4);
            }
          } else {
            AsnIoClose (aipout);
          }
        }
        FileRemove (path);
        break;
      case MEDXML_PAGE :
        TmpNam (path);
        aipout = AsnIoOpen (path, "wx");
        if (aipout != NULL) {
          fnt = programFont;
          if (mfp->displayFont != NULL) {
            fnt = mfp->displayFont;
          }
          if (MedlineEntryAsnWrite (mep, aipout, NULL)) {
            AsnIoClose (aipout);
            if (mfp->useScrollText) {
              if (! FileToScrollText (mfp->text, path)) {
                SetTitle (mfp->text, "(Text is too large to be displayed in this control.)");
              }
            } else {
              DisplayFancy (mfp->doc, path, &medParFmt, &medColFmt, fnt, 4);
            }
          } else {
            AsnIoClose (aipout);
          }
        }
        FileRemove (path);
        break;
      default :
        break;
    }
    if (! mfp->useScrollText) {
      SetDocAutoAdjust (mfp->doc, TRUE);
      AdjustDocScroll (mfp->doc);
      UpdateDocument (mfp->doc, 0, 0);
    }
    ArrowCursor ();
    Update ();
  }
}

static void ResizeMedlineForm (WindoW w)

{
  RecT                c;
  Int2                diff = 0;
  Int2                gap = 0;
  Int2                height;
  MedlineViewFormPtr  mfp;
  RecT                r;
  RecT                s;
  RecT                t;
  Int2                width;

  mfp = (MedlineViewFormPtr) GetObjectExtra (w);
  if (mfp == NULL) return;
  WatchCursor ();
  ObjectRect (w, &r);
  width = r.right - r.left;
  height = r.bottom - r.top;
  SafeHide (mfp->controls);
  if (mfp->controls != NULL) {
    GetPosition (mfp->controls, &c);
  }
  if (mfp->doc != NULL) {
    GetPosition (mfp->doc, &s);
    if (mfp->controls != NULL) {
      LoadRect (&t, c.left, c.top, c.right, c.bottom);
      diff = t.bottom - t.top;
      gap = t.top - s.bottom;
      t.bottom = height - s.left;
      t.top = t.bottom - diff;
      s.right = width - s.left;
      /*
      s.bottom = height - s.left;
      */
      s.bottom = t.top - gap;
      SetPosition (mfp->controls, &t);
      AdjustPrnt (mfp->controls, &t, FALSE);
    } else {
      s.right = width - s.left;
      s.bottom = height - s.left;
    }
    SetPosition (mfp->doc, &s);
    AdjustPrnt (mfp->doc, &s, FALSE);
  }
  if (mfp->text != NULL) {
    GetPosition (mfp->text, &s);
    if (mfp->controls != NULL) {
      LoadRect (&t, c.left, c.top, c.right, c.bottom);
      diff = t.bottom - t.top;
      gap = t.top - s.bottom;
      t.bottom = height - s.left;
      t.top = t.bottom - diff;
      s.right = width - s.left;
      /*
      s.bottom = height - s.left;
      */
      s.bottom = t.top - gap;
      SetPosition (mfp->controls, &t);
      AdjustPrnt (mfp->controls, &t, FALSE);
    } else {
      s.right = width - s.left;
      s.bottom = height - s.left;
    }
    SetPosition (mfp->text, &s);
    AdjustPrnt (mfp->text, &s, FALSE);
  }
  if (mfp->doc != NULL) {
    ObjectRect (mfp->doc, &s);
    InsetRect (&s, 4, 4);
    medColFmt.pixWidth = screenRect.right - screenRect.left;
    medColFmt.pixInset = 8;
    if (Visible (mfp->doc) && AllParentsVisible (mfp->doc)) {
      UpdateDocument (mfp->doc, 0, 0);
    }
  }
  SafeShow (mfp->controls);
  ArrowCursor ();
  Update ();
}

extern void LIBCALL CopyMedlineViewFormToClipboard (Pointer formDataPtr)

{
  FILE                *fp;
  MedlineViewFormPtr  mfp;
  Char                path [PATH_MAX];

  mfp = (MedlineViewFormPtr) formDataPtr;
  if (mfp != NULL && mfp->mep != NULL) {
    if (mfp->useScrollText) {
      if (mfp->text != NULL) {
        CopyText (mfp->text);
      }
    } else {
      TmpNam (path);
      fp = FileOpen (path, "w");
      if (fp != NULL) {
        SaveDocument (mfp->doc, fp);
        FileClose (fp);
        FileToClipboard (path);
      }
      FileRemove (path);
    }
  }
}

extern void LIBCALL ExportMedlineViewFormToFile (Pointer formDataPtr, CharPtr filename)

{
  Char                ch;
  Char                dfault [32];
  FILE                *f;
  Int2                j;
  Int2                k;
  MedlineViewFormPtr  mfp;
  Char                path [PATH_MAX];

  mfp = (MedlineViewFormPtr) formDataPtr;
  if (mfp != NULL && mfp->mep != NULL) {
    if (mfp->doc != NULL || mfp->text != NULL) {
      dfault [0] = '\0';
      GetTitle (mfp->form, dfault, sizeof (dfault));
      j = 0;
      k = 0;
      ch = dfault [j];
      while (j < sizeof (dfault) && ch != '\0') {
        if (ch <= ' ') {
          j++;
        } else {
          dfault [k] = dfault [j];
          k++;
          j++;
        }
        ch = dfault [j];
      }
      dfault [k] = '\0';
#ifdef WIN_MSWIN
      j = 0;
      ch = dfault [j];
      while (j < sizeof (dfault) && ch != '\0') {
        if (ch == '_' || IS_ALPHANUM (ch)) {
          j++;
          ch = dfault [j];
        } else {
          ch = '\0';
        }
      }
      dfault [j] = '\0';
#endif
      path [0] = '\0';
      StringNCpy_0 (path, filename, sizeof (path));
      if (path [0] != '\0' || GetOutputFileName (path, sizeof (path), dfault)) {
        WatchCursor ();
#ifdef WIN_MAC
        f = FileOpen (path, "r");
        if (f != NULL) {
          FileClose (f);
        } else {
          FileCreate (path, "TEXT", "ttxt");
        }
#endif
        if (filename == NULL || filename [0] == '\0') {
          f = FileOpen (path, "w");
        } else {
          f = FileOpen (path, "a");
        }
        if (f != NULL) {
          if (mfp->useScrollText) {
            ScrollTextToFile (mfp->text, path);
          } else {
            SaveDocument (mfp->doc, f);
          }
          FileClose (f);
        }
        ArrowCursor ();
      }
    }
  }
}

#ifdef WIN_MOTIF
extern CharPtr Nlm_XrmGetResource (const Char PNTR _resource);
#endif

extern void LIBCALL PrintMedlineViewForm (Pointer formDataPtr)

{
  MedlineViewFormPtr  mfp;
#ifdef WIN_MOTIF
  Char                cmmd [256];
  Int2                len;
  CharPtr             printCmd;
  Char                str [PATH_MAX];
#endif

  mfp = (MedlineViewFormPtr) formDataPtr;
  if (mfp != NULL && mfp->mep != NULL) {
    if (mfp->doc != NULL) {
      PrintDocument (mfp->doc);
    } else if (mfp->text != NULL) {
#ifdef WIN_MOTIF
      TmpNam (str);
      ScrollTextToFile (mfp->text, str);
      printCmd = Nlm_XrmGetResource ("printCommand");
      if (printCmd != NULL) {
        StringNCpy_0 (cmmd, printCmd, sizeof (cmmd) - 1);
      } else {
        StringCpy (cmmd, "lp -c");
      }
      MemFree (printCmd);
      len = (Int2) StringLen (cmmd);
      while (len > 0 && cmmd [len] == ' ') {
        cmmd [len] = '\0';
        len--;
      }
      StringCat (cmmd, " ");
      StringCat (cmmd, str);
      StringCat (cmmd, "; rm ");
      StringCat (cmmd, str);
      system (cmmd);
      /*
      FileRemove (str);
      */
#endif
    }
  }
}

static void SetMedlineImportExportItems (MedlineViewFormPtr mfp)

{
  IteM  exportItm;

  if (mfp == NULL) return;
  exportItm = FindFormMenuItem ((BaseFormPtr) mfp, VIB_MSG_EXPORT);
  if (exportItm == NULL) return;
  switch (mfp->currentPage) {
    case ABSTRACT_PAGE :
      SafeSetTitle (exportItm, "Export Abstract...");
      break;
    case CITATION_PAGE :
      SafeSetTitle (exportItm, "Export Citation...");
      break;
    case MEDLINE_PAGE :
      SafeSetTitle (exportItm, "Export MEDLINE...");
      break;
    case MEDASN1_PAGE :
      SafeSetTitle (exportItm, "Export ASN.1...");
      break;
    case MEDXML_PAGE :
      SafeSetTitle (exportItm, "Export XML...");
      break;
    default :
      SafeSetTitle (exportItm, "Export...");
      break;
  }
}

static void ChangeMedlineViewTabs (VoidPtr data, Int2 newval, Int2 oldval)

{
  MedlineEntryPtr     mep;
  MedlineViewFormPtr  mfp;

  mfp = (MedlineViewFormPtr) data;
  if (mfp != NULL && mfp->mep != NULL) {
    mep = mfp->mep;
    mfp->currentPage = newval;
    SafeHide (mfp->doc);
    SafeHide (mfp->text);
    WatchCursor ();
    Update ();
    Reset (mfp->doc);
    Reset (mfp->text);
    PointerToForm (mfp->form, (Pointer) mep);
    Update ();
    ArrowCursor ();
    SafeShow (mfp->doc);
    SafeShow (mfp->text);
    SetMedlineImportExportItems (mfp);
    Update ();
    if (mfp->activateForm != NULL) {
      mfp->activateForm ((WindoW) mfp->form);
    }
  }
}

static void ChangeMedlineViewGroup (GrouP g)

{
  MedlineViewFormPtr  mfp;
  Int2                val;

  mfp = (MedlineViewFormPtr) GetObjectExtra (g);
  if (mfp != NULL) {
    val = GetValue (g);
    ChangeMedlineViewTabs ((VoidPtr) mfp, val - 1, mfp->currentPage);
  }
}

static void ChangeMedlineViewPopup (PopuP p)

{
  MedlineViewFormPtr  mfp;
  Int2                val;

  mfp = (MedlineViewFormPtr) GetObjectExtra (p);
  if (mfp != NULL) {
    val = GetValue (p);
    ChangeMedlineViewTabs ((VoidPtr) mfp, val - 1, mfp->currentPage);
  }
}

static CharPtr  medlineViewFormTabs [] = {
  "Abstract", "Citation", "MEDLINE", NULL, NULL, NULL
};

static void CleanupMedlineForm (GraphiC g, VoidPtr data)

{
  MedlineViewFormPtr  mfp;

  mfp = (MedlineViewFormPtr) data;
  if (mfp != NULL && mfp->input_entityID > 0) {
    ObjMgrFreeUserData (mfp->input_entityID, mfp->procid, mfp->proctype, mfp->userkey);
  }
  if (mfp != NULL) {
    WatchCursor ();
    if (mfp->cleanupObjectPtr && mfp->objectDataPtr != NULL) {
      MedlineEntryFree ((MedlineEntryPtr) mfp->objectDataPtr);
    }
    ArrowCursor ();
  }
  StdCleanupFormProc (g, data);
}

static Boolean ExportMedlineViewForm (ForM f, CharPtr filename)

{
  ExportMedlineViewFormToFile (GetObjectExtra (f), filename);
  return TRUE;
}

static void MedlineViewFormMessage (ForM f, Int2 mssg)

{
  MedlineViewFormPtr   mfp;
  MedlineViewProcsPtr  mvpp;

  mfp = (MedlineViewFormPtr) GetObjectExtra (f);
  if (mfp != NULL) {
    switch (mssg) {
      case VIB_MSG_REDRAW :
        mvpp = (MedlineViewProcsPtr) GetAppProperty ("MedlineDisplayForm");
        if (mvpp != NULL) {
          mfp->jourfnt = mvpp->jourfnt;
          mfp->volfnt = mvpp->volfnt;
          mfp->pagesfnt = mvpp->pagesfnt;
          mfp->titlefnt = mvpp->titlefnt;
          mfp->authorsfnt = mvpp->authorsfnt;
          mfp->affilfnt = mvpp->affilfnt;
          mfp->abstractfnt = mvpp->abstractfnt;
          mfp->meshfnt = mvpp->meshfnt;
          mfp->displayFont = mvpp->displayFont;
          SafeHide (mfp->doc);
          SafeHide (mfp->text);
          Update ();
          Reset (mfp->doc);
          Reset (mfp->text);
          PointerToForm (mfp->form, (Pointer) mfp->mep);
          Update ();
          ArrowCursor ();
          SafeShow (mfp->doc);
          SafeShow (mfp->text);
        }
        break;
      case VIB_MSG_COPY :
        CopyMedlineViewFormToClipboard (mfp);
        break;
      case VIB_MSG_EXPORT :
        ExportMedlineViewForm (f, NULL);
        break;
      case VIB_MSG_PRINT :
        PrintMedlineViewForm (mfp);
        break;
      default :
        if (mfp->appmessage != NULL) {
          mfp->appmessage (f, mssg);
        }
        break;
    }
  }
}

static void MedlineViewFormActivate (WindoW w)

{
  MedlineViewFormPtr  mfp;

  mfp = (MedlineViewFormPtr) GetObjectExtra (w);
  if (mfp != NULL) {
    if (mfp->activate != NULL) {
      mfp->activate (w);
    }
    SetMedlineImportExportItems (mfp);
  }
}

/*
static void LaunchPubMedArticle (ButtoN b)

{
#ifndef WIN_MAC
  CharPtr             argv [8];
#endif
  CharPtr             browser;
  MedlineViewFormPtr  mfp;
  Char                str [256];
#ifdef WIN_MOTIF
  NS_Window          window = NULL;
#endif

  mfp = (MedlineViewFormPtr) GetObjectExtra (b);
  if (mfp == NULL) return;
  if (mfp->docuid < 1) return;
  browser = GetAppProperty ("MedviewBrowserPath");
  sprintf (str,
           "http://www.ncbi.nlm.nih.gov/htbin-post/Entrez/query?db=m&form=6&uid=%ld&Dopt=r",
           (long) mfp->docuid);
#ifdef WIN_MAC
  if (browser == NULL || StringHasNoText (browser)) {
    Nlm_SendURLAppleEvent (str, "MOSS", NULL);
  } else {
    Nlm_SendURLAppleEvent (str, NULL, browser);
  }
#endif
#ifdef WIN_MSWIN
  argv [0] = str;
  argv [1] = NULL;
  if (browser != NULL && (! StringHasNoText (browser))) {
    if (! Execv (browser, argv)) {
      Message (MSG_POST, "Unable to launch %s", browser);
    }
  } else {
    if (! Nlm_MSWin_OpenDocument (str)) {
      Message (MSG_POST, "Unable to launch browser");
    }
  }
#endif
#ifdef WIN_MOTIF
  argv [0] = str;
  argv [1] = NULL;
  if (browser != NULL && (! StringHasNoText (browser))) {
    if (! Execv (browser, argv)) {
      Message (MSG_POST, "Unable to launch %s", browser);
    }
  } else {
    if (! NS_OpenURL (&window, str, NULL, TRUE)) {
      Message (MSG_POST, "Unable to launch netscape");
    }
    NS_WindowFree (window);
  }
#endif
}
*/

static void LaunchPubMedArticle (ButtoN b)

{
  MedlineViewFormPtr  mfp;

  mfp = (MedlineViewFormPtr) GetObjectExtra (b);
  if (mfp == NULL) return;
  if (mfp->docuid < 1) return;
  LaunchEntrezURL ("PubMed", mfp->docuid, "Abstract");
}

static FonT SetFontIfNull (FonT f, CharPtr dfault)

{
  if (f != NULL) return f;
  f = ParseFont (dfault);
  return f;
}

static void SetupDefaultMvppFonts (MedlineViewProcsPtr mvpp)

{
  if (mvpp == NULL) return;

#ifdef WIN_MAC
  mvpp->jourfnt = SetFontIfNull (mvpp->jourfnt, "Geneva,10,i");
  mvpp->volfnt = SetFontIfNull (mvpp->volfnt, "Geneva,10,b");
  mvpp->pagesfnt = SetFontIfNull (mvpp->pagesfnt, "Geneva,10");
  mvpp->titlefnt = SetFontIfNull (mvpp->titlefnt, "Times,14,b");
  mvpp->authorsfnt = SetFontIfNull (mvpp->authorsfnt, "Times,14");
  mvpp->affilfnt = SetFontIfNull (mvpp->affilfnt, "Times,12");
  mvpp->abstractfnt = SetFontIfNull (mvpp->abstractfnt, "Geneva,10");
  mvpp->meshfnt = SetFontIfNull (mvpp->meshfnt, "Monaco,9");
  mvpp->displayFont = SetFontIfNull (mvpp->displayFont, "Monaco,9");
#endif
#ifdef WIN_MSWIN
  mvpp->jourfnt = SetFontIfNull (mvpp->jourfnt, "Arial,11,i");
  mvpp->volfnt = SetFontIfNull (mvpp->volfnt, "Arial,11,b");
  mvpp->pagesfnt = SetFontIfNull (mvpp->pagesfnt, "Arial,11");
  mvpp->titlefnt = SetFontIfNull (mvpp->titlefnt, "Times New Roman,14,b");
  mvpp->authorsfnt = SetFontIfNull (mvpp->authorsfnt, "Times New Roman,14");
  mvpp->affilfnt = SetFontIfNull (mvpp->affilfnt, "Times New Roman,11");
  mvpp->abstractfnt = SetFontIfNull (mvpp->abstractfnt, "Times New Roman,11");
  mvpp->meshfnt = SetFontIfNull (mvpp->meshfnt, "Times New Roman,9");
  mvpp->displayFont = SetFontIfNull (mvpp->displayFont, "Courier New,10");
#endif
#ifdef WIN_MOTIF
  mvpp->jourfnt = SetFontIfNull (mvpp->jourfnt, "Helvetica,12,i");
  mvpp->volfnt = SetFontIfNull (mvpp->volfnt, "Helvetica,12,b");
  mvpp->pagesfnt = SetFontIfNull (mvpp->pagesfnt, "Helvetica,12");
  mvpp->titlefnt = SetFontIfNull (mvpp->titlefnt, "Times,18,b");
  mvpp->authorsfnt = SetFontIfNull (mvpp->authorsfnt, "Times,18");
  mvpp->affilfnt = SetFontIfNull (mvpp->affilfnt, "Times,14");
  mvpp->abstractfnt = SetFontIfNull (mvpp->abstractfnt, "Times,14");
  mvpp->meshfnt = SetFontIfNull (mvpp->meshfnt, "Times,12");
  mvpp->displayFont = SetFontIfNull (mvpp->displayFont, "Courier,10");
#endif
}

extern ForM LIBCALL CreateMedlineViewForm (Int2 left, Int2 top, CharPtr title,
                                           MedlineEntryPtr mep,
                                           MedlineViewProcsPtr mvpp)

{
  ButtoN               b;
  WndActnProc          close;
  FonT                 fnt;
  GrouP                g;
  GrouP                h;
  Int2                 j;
  GrouP                k;
  MedViewControlsProc  makeControls = NULL;
  MedlineViewFormPtr   mfp;
  Int2                 minwid;
  Int2                 pixheight;
  Int2                 pixwidth;
  PopuP                pops;
  PrompT               ppt;
  RecT                 r;
  GrouP                rads;
  GrouP                s;
  DialoG               tbs;
  WindoW               w;

  w = NULL;
  mfp = (MedlineViewFormPtr) MemNew (sizeof (MedlineViewForm));
  if (mfp != NULL && mep != NULL) {
    close = StdCloseWindowProc;
    if (mvpp == NULL) {
      mvpp = (MedlineViewProcsPtr) GetAppProperty ("MedlineDisplayForm");
    }
    if (mvpp != NULL && mvpp->closeForm != NULL) {
      close = mvpp->closeForm;
    }
    w = DocumentWindow (left, top, -10, -10, title,
                        close, ResizeMedlineForm);
    SetObjectExtra (w, mfp, CleanupMedlineForm);
    mfp->form = (ForM) w;
    mfp->actproc = NULL;
    mfp->toform = MedlineEntryPtrToMedlineForm;
    mfp->exportform = ExportMedlineViewForm;
    mfp->formmessage = MedlineViewFormMessage;

    mfp->mep = mep;
    mfp->useScrollText = FALSE;
    if (mvpp != NULL) {
      SetupDefaultMvppFonts (mvpp);
      mfp->cleanupObjectPtr = mvpp->cleanupObjectPtr;
      mfp->activateForm = mvpp->activateForm;
      mfp->jourfnt = mvpp->jourfnt;
      mfp->volfnt = mvpp->volfnt;
      mfp->pagesfnt = mvpp->pagesfnt;
      mfp->titlefnt = mvpp->titlefnt;
      mfp->authorsfnt = mvpp->authorsfnt;
      mfp->affilfnt = mvpp->affilfnt;
      mfp->abstractfnt = mvpp->abstractfnt;
      mfp->meshfnt = mvpp->meshfnt;
      mfp->displayFont = mvpp->displayFont;
      mfp->useScrollText = mvpp->useScrollText;
      mfp->appmessage = mvpp->handleMessages;
      makeControls = mvpp->makeControls;
    }

    if (mvpp != NULL && mvpp->createMenus != NULL) {
      mvpp->createMenus (w);
    }

    g = HiddenGroup (w, -1, 0, NULL);
    SetGroupSpacing (g, 3, 10);

    if (mvpp != NULL && mvpp->showAsnPage) {
      minwid = 35 * stdCharWidth + 17;
    } else {
      minwid = 27 * stdCharWidth + 17;
    }
    pixwidth = minwid;
    pixheight = 14 * stdLineHeight;
    mfp->currentPage = 0;

    if (mvpp != NULL) {
      mfp->currentPage = mvpp->initPage;
      if (mvpp->initMedLabel != NULL) {
        for (j = 0; medlineViewFormTabs [j] != NULL; j++) {
          if (StringICmp (medlineViewFormTabs [j], mvpp->initMedLabel) == 0) {
            mfp->currentPage = j;
          }
        }
      }
      if (mvpp->showAsnPage) {
        medlineViewFormTabs [3] = "ASN.1";
        medlineViewFormTabs [4] = "XML";
      }
      switch (mvpp->useFolderTabs) {
        case CHANGE_VIEW_NOTABS :
          break;
        case CHANGE_VIEW_FOLDERTABS :
          tbs = CreateFolderTabs (g, medlineViewFormTabs, mfp->currentPage,
                                  0, 0, SYSTEM_FOLDER_TAB,
                                  ChangeMedlineViewTabs, (Pointer) mfp);
          ObjectRect (tbs, &r);
          pixwidth = r.right - 2 * r.left - Nlm_vScrollBarWidth;
          if (pixwidth < minwid) {
            pixwidth = minwid;
          }
          break;
        case CHANGE_VIEW_TEXTTABS :
          tbs = CreateTextTabs (g, medlineViewFormTabs, mfp->currentPage,
                                  0, 0, SYSTEM_TEXT_TAB,
                                  ChangeMedlineViewTabs, (Pointer) mfp);
          ObjectRect (tbs, &r);
          pixwidth = r.right - 2 * r.left - Nlm_vScrollBarWidth;
          if (pixwidth < minwid) {
            pixwidth = minwid;
          }
          break;
        case CHANGE_VIEW_RADIOBUTTONS :
          k = HiddenGroup (g, -3, 0, NULL);
          ppt = StaticPrompt (k, "Format:", 0, 0, programFont, 'l');
          rads = HiddenGroup (k, 8, 0, ChangeMedlineViewGroup);
          SetObjectExtra (rads, (Pointer) mfp, NULL);
          for (j = 0; medlineViewFormTabs [j] != NULL; j++) {
            RadioButton (rads, medlineViewFormTabs [j]);
          }
          SetValue (rads, mfp->currentPage + 1);
          ObjectRect (rads, &r);
          pixwidth = r.right - 2 * r.left - Nlm_vScrollBarWidth;
          if (pixwidth < minwid) {
            pixwidth = minwid;
          }
          b = PushButton (k, "PubMed", LaunchPubMedArticle);
          SetObjectExtra (b, (Pointer) mfp, NULL);
          AlignObjects (ALIGN_MIDDLE, (HANDLE) ppt, (HANDLE) rads, (HANDLE) b, NULL);
          break;
        case CHANGE_VIEW_POPUP :
          k = HiddenGroup (g, -3, 0, NULL);
          ppt = StaticPrompt (k, "Display Format", 0, popupMenuHeight, programFont, 'l');
          pops = PopupList (k, TRUE, ChangeMedlineViewPopup);
          SetObjectExtra (pops, (Pointer) mfp, NULL);
          for (j = 0; medlineViewFormTabs [j] != NULL; j++) {
            PopupItem (pops, medlineViewFormTabs [j]);
          }
          SetValue (pops, mfp->currentPage + 1);
          ObjectRect (pops, &r);
          pixwidth = r.right - 2 * r.left - Nlm_vScrollBarWidth;
          if (pixwidth < minwid) {
            pixwidth = minwid;
          }
          b = PushButton (k, "PubMed", LaunchPubMedArticle);
          SetObjectExtra (b, (Pointer) mfp, NULL);
          AlignObjects (ALIGN_MIDDLE, (HANDLE) ppt, (HANDLE) pops, (HANDLE) b, NULL);
          break;
        default :
          break;
      }
      medlineViewFormTabs [3] = NULL;
      medlineViewFormTabs [4] = NULL;
      pixwidth = MAX (pixwidth, mvpp->minPixelWidth);
      pixheight = MAX (pixheight, mvpp->minPixelHeight);
    }

    h = HiddenGroup (g, 0, 0, NULL);

    s = HiddenGroup (h, -1, 0, NULL);
    if (mfp->useScrollText) {
      fnt = programFont;
      if (mfp->displayFont != NULL) {
        fnt = mfp->displayFont;
      }
      mfp->text = ScrollText (s, pixwidth / stdCharWidth,
                                     pixheight / stdLineHeight, fnt, FALSE, NULL);
      SetObjectExtra (mfp->text, mfp, NULL);
    } else {
      mfp->doc = DocumentPanel (s, pixwidth, pixheight);
      SetObjectExtra (mfp->doc, mfp, NULL);
    }

    if (makeControls != NULL) {
      mfp->controls = makeControls (g, (BaseFormPtr) mfp, TYP_ML, mep->uid);
    }

    RealizeWindow (w);

    mfp->activate = NULL;
    if (mvpp != NULL) {
      mfp->activate = mvpp->activateForm;
    }
    SetActivate (w, MedlineViewFormActivate);
    Update ();
    MedlineViewFormActivate ((WindoW) mfp->form);

    SendMessageToForm (mfp->form, VIB_MSG_INIT);
    if (mep != NULL) {
      PointerToForm (mfp->form, (Pointer) mep);
    }
  }
  return (ForM) w;
}

CharPtr PersonIdPrint PROTO((PersonIdPtr pip, CharPtr buf)); /* in prtutil.h */

#define ART_JOURNAL     1
#define ART_BOOK        2

static void GetMedlineCaption (MedlineEntryPtr mep, CharPtr buf, size_t max)

{
  AuthListPtr  alp;
  AuthorPtr    ap;
  CitArtPtr    cap;
  CitBookPtr   cbp;
  CitJourPtr   cjp;
  DatePtr      dp;
  ImprintPtr   imp;
  ValNodePtr   names;
  PersonIdPtr  pid;
  Char         str [64];
  Char         tmp [16];

  if (mep == NULL || buf == NULL || max < 1) return;
  cap = mep->cit;
  if (cap == NULL) return;
  alp = cap->authors;
  if (alp == NULL) return;
  str [0] = '\0';
  tmp [0] = '\0';
  if (alp->choice == 1) {
    names = alp->names;
    if (names != NULL) {
      ap = names->data.ptrvalue;
      if (ap != NULL) {
        pid = ap->name;
        if (pid != NULL) {
          PersonIdPrint (pid, str);
        }
      }
    }
  } else if (alp->choice == 2 || alp->choice == 3) {
    names = alp->names;
    if (names != NULL) {
      StringNCpy_0 (str, names->data.ptrvalue, sizeof (str) - 2);
    }
  }
  imp = NULL;
  switch (cap->from) {
    case ART_JOURNAL :
      cjp = (CitJourPtr) cap->fromptr;
      if (cjp != NULL) {
        imp = cjp->imp;
      }
      break;
    case ART_BOOK :
      cbp = (CitBookPtr) cap->fromptr;
      if (cbp != NULL) {
        imp = cbp->imp;
      }
      break;
    default :
      break;
  }
  if (imp != NULL) {
    dp = imp->date;
    if (dp != NULL) {
      if (dp->data [0] == 1) {
        sprintf (tmp, "  %d", (dp->data [1] + 1900));
      } else if (dp->data [0] == 0 && dp->str != NULL) {
        sprintf (tmp, "  %s", dp->str);
      }
    }
  }
  StringCat (str, tmp);
  StringNCpy_0 (buf, str, max);
}

extern Int2 LIBCALLBACK MedlineViewGenFunc (Pointer data)

{
  MedlineEntryPtr     mep;
  MedlineViewFormPtr  mfp;
  OMProcControlPtr    ompcp;
  OMUserDataPtr       omudp;
  Char                str [64];
  WindoW              w;

  ompcp = (OMProcControlPtr) data;
  mep = NULL;
  if (ompcp == NULL || ompcp->proc == NULL) return OM_MSG_RET_ERROR;
  switch (ompcp->input_itemtype) {
    case OBJ_MEDLINE_ENTRY :
      mep = (MedlineEntryPtr) ompcp->input_data;
      break;
   case 0 :
      return OM_MSG_RET_ERROR;
    default :
      return OM_MSG_RET_ERROR;
  }
  if (mep == NULL) return OM_MSG_RET_ERROR;
  str [0] = '\0';
  GetMedlineCaption (mep, str, sizeof (str));
  w = (WindoW) CreateMedlineViewForm (-50, -33, str, mep, NULL);
  mfp = (MedlineViewFormPtr) GetObjectExtra (w);
  if (mfp != NULL) {
    mfp->input_entityID = ompcp->input_entityID;
    mfp->input_itemID = ompcp->input_itemID;
    mfp->input_itemtype = ompcp->input_itemtype;
    mfp->this_itemtype = OBJ_MEDLINE_ENTRY;
    mfp->this_subtype = 0;
    mfp->procid = ompcp->proc->procid;
    mfp->proctype = ompcp->proc->proctype;
    mfp->docuid = mep->uid;
    mfp->doctype = TYP_ML;
    mfp->userkey = OMGetNextUserKey ();
    omudp = ObjMgrAddUserData (ompcp->input_entityID, ompcp->proc->procid,
	                           OMPROC_VIEW, mfp->userkey);
    if (omudp != NULL) {
      omudp->userdata.ptrvalue = (Pointer) mfp;
      omudp->messagefunc = NULL;
    }
  }
  Show (w);
  Select (w);
  return OM_MSG_RET_DONE;
}