home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 523b.lha / Drawmap_v2.25d / Sources.LZH / Sources / drawmap.c < prev    next >
C/C++ Source or Header  |  1991-06-10  |  98KB  |  2,885 lines

  1. /*  File drawmap.c  */
  2.  
  3. #include <intuition/intuition.h>
  4. #include <intuition/intuitionbase.h>
  5. #include <graphics/gfxmacros.h>
  6. #include <graphics/gfxbase.h>
  7. #include <devices/printer.h>
  8. #include <exec/io.h>
  9. #include <exec/memory.h>
  10. #include <stdio.h>
  11. #include <fcntl.h>
  12. #include <math.h>
  13. #include <libraries/dos.h>
  14. #include <libraries/dosextens.h>
  15. #include <workbench/workbench.h>
  16.  
  17. #ifdef LATTICE      /* Lattice supports prototypes ! */
  18. #define FDwAT
  19. #define LETTUCE
  20. #include <ios1.h>
  21. #include <workbench/startup.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <proto/all.h>
  25. int CXBRK(VOID) { return(0); }
  26. #endif
  27.  
  28. #include "iff/ilbm.h"
  29.  
  30. #include "drawmap.h"
  31. #include "drawmap-proto.h"
  32. #include "drawmap-req.h"
  33. #include "drawmap-menu.h"
  34.  
  35. #ifndef NO_ASL
  36. #include "libraries/aslbase.h"
  37. #endif
  38.  
  39. #define PROJECT 0
  40.    /* menu selection numbers */
  41.    /* this should look similar to menustrip */
  42.    #define ABOUT 0
  43. /* #define DRAW 1 */
  44.     #define MAP 2
  45.       #define FLAT     0
  46.       #define MERCATOR 1
  47.       #define BOX      2
  48.     #define SPHERE 3
  49.       #define SPHEREOFFSET 3
  50.       #define GLOBE    3
  51.       #define ORBITAL  4
  52.       #define ZOOM_IN  5
  53.       #define ZOOM_OUT 6
  54. /* #define IFF PICTURE 4 */
  55.     #define SAVE 5
  56.     #define SAVEAS 6
  57.  
  58.    #define PRINT 7
  59.    #define GRID 8
  60.    #define CLEAR 9
  61.  
  62.    #define QUIT 10
  63. #define EDIT 1
  64.    #define COLOR_F  0
  65.    #define COLOR 1
  66.    #define DRAW 2
  67.    #define FLOOD 3
  68.    #define TEXT 4
  69.    #define COLORPALETTE 5
  70. #define SPECIAL 2
  71.    #define SHADOW  0
  72.  
  73. short *map, *map_trig;                 /* workspaces for map  */
  74. short *td, *td_trig;
  75.  
  76. struct Screen *s;                      /* pointer to screen   */
  77. struct Window *w, *wt;                 /* pointers to Windows */
  78. struct RastPort *rp;                   /* pointer to RastPort */
  79. struct ViewPort *vp;                   /* pointer to ViewPort */
  80. struct AreaInfo mapAreaInfo;
  81. struct TmpRas mapTmpRas;
  82. struct GfxBase *GfxBase;
  83. struct IntuitionBase *IntuitionBase;
  84.  
  85. #ifndef NO_ASL
  86. extern struct AslBase *AslBase;
  87. #endif
  88. extern struct ArpBase *ArpBase;
  89. extern struct FileRequester *filereq;
  90. short areaArray[5*NUMPTS];             /* 5 words per point for drawing    */
  91. short area[2*NUMPTS];                  /* storage for each individual area */
  92.  
  93. #ifndef LATTICE
  94. unsigned short *arrow, *cross;         /* storage for mouse pointers */
  95. unsigned short *waiter, *transparent;
  96. unsigned short *close_data_c, *ilbm_data_c;
  97. #endif
  98.  
  99. UBYTE *bp[DEPTH];                      /* bitplane pointers         */
  100. /*  short ytable[SHEIGHT];                * under shadow() *
  101.     short rowoffset; */
  102. #define rowoffset (WWIDTH/8)
  103.  
  104. struct Requester req;
  105.  
  106.  
  107. /* ============================================================= */
  108. #ifdef LETTUCE
  109. void _main (line)
  110. register char *line;
  111. #else
  112. main ()
  113. #endif
  114.  
  115. {
  116.    struct IntuiMessage *msg;
  117.    PLANEPTR workspace;
  118.    LONG exitcode=RETURN_ERROR;
  119.    struct MsgPort *idcmpport;
  120.    struct Process *process;
  121.    extern void free_svector(), InitTextRequest();
  122.    extern int readmap();
  123.    extern int HandleEvent();
  124.    extern short *svector();
  125.    extern VOID FreeArpRequest(), FreeFileRequest();
  126.    int ix;
  127.  
  128. #ifdef LETTUCE      /* optimized for Lattice/SAS C (from umain.c)*/
  129.    extern struct UFB _ufbs[];
  130.    extern int _fmode,_iomode;
  131.    register int x;
  132.    struct FileHandle *handle;
  133.    extern int (*_ONBREAK)();
  134.    extern int CXBRK();
  135.  
  136.    if (*line=='\0'){    /* running under workbench */
  137.         _ufbs[0].ufbfh = Open("NIL:",MODE_NEWFILE);
  138.         _ufbs[1].ufbfh = _ufbs[0].ufbfh;
  139.         _ufbs[1].ufbflg = UFB_NC;
  140.         _ufbs[2].ufbfh = _ufbs[0].ufbfh;
  141.         _ufbs[2].ufbflg = UFB_NC;
  142.         handle = (struct FileHandle *)(_ufbs[0].ufbfh << 2);
  143.         process = (struct Process *)FindTask(0);
  144.         process->pr_ConsoleTask = (APTR)handle->fh_Type;
  145.         x = 0;
  146.    }
  147.    else                    /* running under CLI            */
  148.    {
  149.         _ufbs[0].ufbfh = Input();
  150.         _ufbs[1].ufbfh = Output();
  151.         _ufbs[2].ufbfh = Open("*", MODE_OLDFILE);
  152.         x = UFB_NC;                     /* do not close CLI defaults    */
  153.    }
  154.  
  155.    _ufbs[0].ufbflg |= UFB_RA | O_RAW | x;
  156.    _ufbs[1].ufbflg |= UFB_WA | O_RAW | x;
  157.    _ufbs[2].ufbflg |= UFB_RA | UFB_WA | O_RAW;
  158.  
  159.    x = (_fmode) ? 0 : _IOXLAT;
  160.    stdin->_file = 0;
  161.    stdin->_flag = _IOREAD | x;
  162.    stdout->_file = 1;
  163.    stdout->_flag = _IOWRT | x;
  164.    stderr->_file = 2;
  165.    stderr->_flag = _IORW | x;
  166.  
  167.    _ONBREAK = CXBRK;
  168.  
  169. #endif
  170.    if ((GfxBase = (struct GfxBase *) OpenLibrary ("graphics.library",0)) == NULL)  {
  171.       printf ("Can't open library");
  172.       goto end0;
  173.    }
  174.  
  175.    if ((IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library",0)) == NULL)  {
  176.       printf ("Can't open library");
  177.       goto end1;
  178.    }
  179.    if ((s = OpenScreen (&mapscreen)) == NULL)  {
  180.       printf ("Can't open screen or init windows");
  181.       goto end2;
  182.    }
  183.    if ((idcmpport=CreatePort(NULL,0))== NULL ){
  184.       printf ("Can't open screen or init windows");
  185.       goto end21;
  186.    }
  187.  
  188.    mapWindow.Screen = s;
  189.    if ((w = OpenWindow (&mapWindow)) == NULL)  {
  190.       printf ("Can't open screen or init windows");
  191.       goto end3;
  192.    }
  193.    maptitleWindow.Screen=s;
  194.    if ((wt=OpenWindow (&maptitleWindow)) == NULL){
  195.       printf ("Can't open screen or init windows");
  196.       goto end31;
  197.    }
  198.    w->UserPort=idcmpport;
  199.    wt->UserPort=idcmpport;
  200.    ModifyIDCMP(wt, CLOSEWINDOW|ACTIVEWINDOW);    /* Titlebar Window to get CLOSEWINDOW message*/
  201.    ModifyIDCMP(w, IDCMPFLAGS);
  202.  
  203.  
  204. #ifndef LATTICE
  205.    if (((arrow = (UWORD *) AllocMem (arrow_size, MEMF_CHIP)))==NULL){
  206.       goto end41;
  207.    }
  208.    for (ix=0; ix<arrow_size/2; ++ix)
  209.       arrow[ix] = arrow_data[ix];
  210.                                        /* init. mouse pointers */
  211.    if (((cross = (UWORD *) AllocMem (cross_size, MEMF_CHIP)))== NULL){
  212.       goto end42;
  213.    }
  214.    for (ix=0; ix<cross_size/2; ++ix)
  215.       cross[ix] = cross_data[ix];
  216.  
  217.    if (((waiter = (UWORD *) AllocMem (waiter_size, MEMF_CHIP))) == NULL){
  218.       goto end43;
  219.    }
  220.    for (ix=0; ix<waiter_size/2; ++ix)
  221.       waiter[ix] = waiter_data[ix];
  222.  
  223.    if (((transparent = (UWORD *) AllocMem (transparent_size, MEMF_CHIP))) == NULL){
  224.       goto end44;
  225.    }
  226.    for (ix=0; ix<transparent_size/2; ++ix)
  227.       transparent[ix] = transparent_data[ix];
  228.  
  229.    if (((close_data_c = (UWORD *) AllocMem (close_size, MEMF_CHIP))) == NULL){
  230.       goto end45;
  231.    }
  232.    CopyMem(close_data, close_data_c, close_size);
  233.    closeimage.ImageData=close_data_c;
  234.  
  235.    if (((ilbm_data_c = (UWORD *) AllocMem (ilbm_size, MEMF_CHIP))) == NULL){
  236.       goto end46;
  237.    }
  238.    CopyMem(ilbm_data,ilbm_data_c);
  239.    ilbmimage.ImageData=ilbm_data_c;
  240.  
  241. #endif
  242.  
  243.    vp = &(s->ViewPort);                /* pointer to viewport */
  244.    LoadRGB4 (vp, &mapcolors[0], 4);    /* init. color values  */
  245.    DrawImage(&s->RastPort,&closeimage,0,0);     /* looks nicer than */
  246.                                                 /* system gadgs. */
  247.    SetPointer (wt, arrow, arrow_size/4-2, 16, arrow_x_offset, arrow_y_offset);
  248.    SetPointer (w, arrow, arrow_size/4-2, 16, arrow_x_offset, arrow_y_offset);
  249.  
  250.    rp = w->RPort;
  251.    if ((workspace = (PLANEPTR) AllocRaster (WWIDTH,WHEIGHT)) == NULL)  {
  252.       printf ("Can't get space");
  253.       goto end4;
  254.    }
  255.    InitTmpRas (&mapTmpRas, workspace, RASSIZE(WWIDTH,WHEIGHT));
  256.    rp->TmpRas = &mapTmpRas;            /* link it to the RastPort */
  257.    InitArea (&mapAreaInfo, &areaArray[0], NUMPTS);
  258.    rp->AreaInfo = &mapAreaInfo;        /* link it to the RastPort */
  259.  
  260.    SetPointer (w, waiter, waiter_size/4-2, 16,
  261.                waiter_x_offset, waiter_y_offset);
  262.  
  263.    if ((map = (short *) AllocMem ((MAXVAL-1)*sizeof(short), MEMF_CLEAR| MEMF_PUBLIC)) == NULL)  {
  264.       printf ("Can't get space for map information");
  265.       goto end5;
  266.    }
  267.    if ((ix = readmap (map, mapname, MAXVAL)) != OK)  { /* read map file */
  268.       printf ("Error reading file :");
  269.       printf (mapname);
  270.       goto end6;
  271.    }
  272.    if ((map_trig = (short *) AllocMem ((MAXVAL-1)*sizeof(short), MEMF_CLEAR| MEMF_PUBLIC)) == NULL)  {
  273.       printf ("Can't get space for map information");
  274.       goto end6;
  275.    }
  276.    if ((ix = readmap (map_trig, mapname_trig, MAXVAL)) != OK)  { /* read map_trig file */
  277.       printf ("Error reading file :");
  278.       printf (mapname_trig);
  279.       goto end7;
  280.    }
  281.    if ((td = (short *) AllocMem ((MAXTIDY-1)*sizeof(short), MEMF_CLEAR| MEMF_PUBLIC)) == NULL)  {
  282.       printf ("Can't get space for map information");
  283.       goto end7;
  284.    }
  285.    if ((ix = readmap (td, tdname, MAXTIDY)) != OK)  {  /* read td file */
  286.       printf ("Error reading file :");
  287.       printf (tdname);
  288.       goto end8;
  289.    }
  290.    if ((td_trig = (short *) AllocMem ((MAXTIDY-1)*sizeof(short), MEMF_CLEAR| MEMF_PUBLIC)) == NULL)  {
  291.       printf ("Can't get space for map information");
  292.       goto end8;
  293.    }
  294.    if ((ix = readmap (td_trig, tdname_trig, MAXTIDY)) != OK)  { /* read td_trig file */
  295.       printf ("Error reading file :");
  296.       printf (tdname_trig);
  297.       goto end9;
  298.    }
  299.    SetPointer (w, arrow, arrow_size/4-2, 16, arrow_x_offset, arrow_y_offset);
  300.  
  301.  
  302.     /* redirect system requesters ("Printer trouble", ...)*/
  303. #ifndef LATTICE
  304.    process = (struct Process *)FindTask(NULL);   /* Finds our process */
  305. #endif
  306.    process->pr_WindowPtr = (APTR)w;
  307.  
  308.    DrawBorder (rp, &border_top, (WWIDTH-TWIDTH)/2, (WHEIGHT-THEIGHT)/2-THEIGHT/2);
  309.  
  310.    PrintIText(rp,&hailtext, (WWIDTH-TWIDTH)/2+13, (WHEIGHT-THEIGHT)/2+TEXTYOFFS);
  311.  
  312.    SetMenuStrip(w,&Menu1);       /* TATA !!! We set the MENUs */
  313.  
  314.    SetAPen (rp,ORANGE);                /* land  */
  315.    SetBPen (rp,BLUE);                  /* water */
  316.    SetDrMd (rp,JAM2);
  317.  
  318.    InitTextRequest ();                 /* initialize the string requester */
  319.  
  320.    view_height = VIEW_HEIGHT;          /* constants for globe view */
  321.    eta = view_height/RE;
  322.    facp = 1. + eta;
  323.    etap = 1./facp;
  324.  
  325.    for (ix=0; ix<DEPTH; ++ix)          /* initialize bitplane pointers */
  326.       bp[ix] = w->RPort->BitMap->Planes[ix];
  327.    /* wait for message from */
  328.    /*   Intuition           */
  329.    do {
  330.       WaitPort (w->UserPort);
  331.       msg = (struct IntuiMessage *)GetMsg (w->UserPort);
  332.       if (msg==NULL)
  333.         continue;
  334.    } while (HandleEvent (msg) == OK);   /* Replies msg !!! */
  335.    exitcode=0;
  336. #ifndef NO_ASL
  337.    if (AslBase !=NULL){
  338.         if (filereq)    FreeFileRequest(filereq);
  339.         CloseLibrary(AslBase);
  340.     }
  341.    else
  342. #endif
  343.       if (ArpBase != NULL){ /* i.e. if Filerequester has been initialized */
  344.         FreeArpRequest(filereq);
  345.     }
  346.  
  347. end9:
  348.    FreeMem (td_trig, (MAXTIDY-1)*sizeof(short) ); /* done, so clean up */
  349. end8:
  350.    FreeMem (td,  (MAXTIDY-1)*sizeof(short) );
  351. end7:
  352.    FreeMem (map_trig, (MAXVAL-1)*sizeof(short) );
  353. end6:
  354.    FreeMem (map, (MAXVAL-1)*sizeof(short) );
  355. end5:
  356.    FreeRaster (workspace, WWIDTH, WHEIGHT);
  357. #ifndef LATTICE
  358. end4:
  359.    FreeMem (ilbm_data_c, ilbm_size);
  360. end46:
  361.    FreeMem (close_data_c, close_size);
  362. end45:
  363.    FreeMem (transparent, transparent_size);
  364. end44:
  365.    FreeMem (waiter, waiter_size);
  366. end43:
  367.    FreeMem (cross, cross_size);
  368. end42:
  369.    FreeMem (arrow, arrow_size);
  370. #endif
  371.    ClearPointer (w);
  372.    Forbid();        /* Strip ALL IntuiMessages */
  373.    while (msg = (struct IntuiMessage *)GetMsg (w->UserPort)){
  374.     ReplyMsg((struct Messsage *)msg);
  375.    }
  376.    w->UserPort=NULL;
  377.    wt->UserPort=NULL;
  378.    ModifyIDCMP(wt, NULL);    /* Titlebar Window to get CLOSEWINDOW message*/
  379.    ModifyIDCMP(w, NULL);
  380.    Permit();
  381. #ifndef LATTICE
  382. end41:
  383. #else
  384. end4:
  385. #endif
  386.    CloseWindow(wt);
  387. end31:
  388.    CloseWindow(w);
  389. end3:
  390.    CloseScreen (s);
  391. end21:
  392.    DeletePort(idcmpport);
  393. end2:
  394.    CloseLibrary ((struct Library *)IntuitionBase);
  395. end1:
  396.    CloseLibrary ((struct Library *)GfxBase);
  397. end0:
  398.    printf("\nDrawmap closed.\n");
  399.    exit (exitcode);
  400. }
  401.  
  402. /* ============================================================= */
  403.  
  404. int readmap (ws, fname, num)                /* reads map files into memory */
  405.  
  406. short *ws;
  407. char *fname;
  408. int num;
  409.  
  410. {
  411.    BPTR handlebin;
  412.  
  413.    int disp, numrd;
  414.  
  415.    if ((handlebin = Open (fname, MODE_OLDFILE)) == NULL)
  416.       return (NOT_OK);
  417.    disp = 0;
  418.    FOREVER {
  419.       if ((numrd = Read (handlebin, (char*) &ws[disp], 4000)) <= 0)
  420.          break;
  421.       disp += numrd / sizeof(short);
  422.    }
  423.    Close (handlebin);
  424.    if (disp!=num)
  425.       return (NOT_OK);
  426.    return (OK);
  427. }
  428.  
  429. /* ============================================================= */
  430.  
  431. void InitTextRequest ()                /* initializes a string requester */
  432.  
  433. {
  434.    InitRequester (&req);               /* initialize the requester */
  435.    req.LeftEdge = TLEFT;
  436.    req.TopEdge = TTOP;
  437.    req.Width = TWIDTH;
  438.    req.Height = THEIGHT;
  439.    req.ReqGadget = &gad;
  440.    req.ReqText = &rtext;
  441.    req.BackFill = ORANGE;
  442.    req.Flags = 0;
  443.    req.ReqBorder = &border_top;
  444.  
  445.    strcpy (userinput, defaulttext); /* copy default text string */
  446. }
  447.  
  448. /* ============================================================= */
  449.  
  450. int HandleEvent (msg)                  /* processes main Intuition events */
  451.  
  452. struct IntuiMessage *msg;
  453.  
  454. {
  455.    static char title_FLAT[]     = "   Flat Map";
  456.    static char title_MERCATOR[] = "   Mercator";
  457.    static char title_GLOBE[]    = "   Globe...view from infinitely far away";
  458.    static char title_ORBITAL[60];   /* is filled in by sprintf */
  459.    static char title_PRINT[]    = "   Print";
  460.    static char PRINT_wait[]     = "   <-- Click here to CANCEL printing";
  461.    static char PRINT_error[]    = "   Print aborted";
  462.    static char title_SAVE[]     = "   Save";
  463.    static char SAVEAS_wait[]    = "   Please select save name for displayed map";
  464.    static char SAVE_wait[]      = "   Saving displayed map as IFF Picture";
  465.    static char SAVE_error[]     = "   Screen not successfully saved";
  466.  
  467.    static char title_GRID[]     = "   Grid";
  468.    static char title_FLOOD[]    = "   Flood Fill";
  469.    static char title_DRAW[]     = "   Draw Line";
  470.    static char title_SHADOW[]   = "   Shadow";
  471.    static char title_BOX[]      = "   Box";
  472.    static char title_COLORS[]   = "   Colors";
  473.    static char title_TEXT[]     = "   Text";
  474.    static char title_HELP[]     = "   About Drawmap";
  475.    static char title_CLEARS[]   = "   Clear Screen";
  476.    static char press_prompt[]   = "   Press LEFT button to select center point";
  477.    static char drag_prompt[]    = "   Press and drag LEFT button to select box";
  478.    static char flood_wait[]     = "   Flood fill...press LEFT button to fill area | RIGHT button to CANCEL";
  479.    static char draw_wait[]      = "   Draw...press LEFT button to draw a line | RIGHT button to CANCEL";
  480.    static char box_error[]      = "   Box of zero size not allowed";
  481.    static char grid_error[]     = "   Invalid map displayed for Grid option";
  482.    static char fmt_ORBITAL[]    = "   Orbital...view from %.2lf kilometers";
  483.    static char fmt_ZOOM_IN[]    = "   Zoom In...view from %.2lf kilometers";
  484.    static char fmt_ZOOM_OUT[]   = "   Zoom Out...view from %.2lf kilometers";
  485.    #define MAXLENGHT 255
  486.    static char picname[MAXLENGHT+1] = "DrawmapPIC";
  487.    static LONG lenghtpicname = 10;  /* strlen(picname)*/
  488.    static int picnum=0;
  489.  
  490.    struct IntuiMessage *msgf;
  491.    extern long PopChoose();
  492.    extern void fullmap(), globe(), stars(), floodfill(), drawline();
  493.    extern void box(), grid(), shadow(), do_text(), getcoord();
  494.    extern void displayhelp(), setwin();
  495.    extern LONG dump(), save(), saveas();
  496.    extern int getbox();
  497.  
  498.    static long oldval = -1L;
  499.    static short activepen= ORANGE;
  500.    static long fill = FILL;
  501.    static long color_offset = 0;
  502.    int x, y, x0, y0, result;
  503.    double lat[2], lam[2];
  504.    static double lamg, latg;
  505.    USHORT select,flag;
  506.    LONG menunum,itemnum,subnum;
  507.  
  508.    switch (msg->Class) {
  509.    case CLOSEWINDOW:
  510.       DisplayBeep(0);
  511.       ReplyMsg((struct Message *)msg);
  512.       return (NOT_OK);
  513.       break;
  514.    case ACTIVEWINDOW:         /* Titlebar (Closegad) window activated */
  515.       ActivateWindow (w);     /* Reactivate other Window */
  516.       break;
  517.    case RAWKEY:
  518.       if (msg->Code==0x5F){     /* HELP key pressed */
  519.         setwin (w, RMBTRAP, CLOSEWINDOW, title_HELP );
  520.         displayhelp();
  521.         setwin (w, 0, IDCMPFLAGS, (char *) -1 );
  522.       }
  523.       break;
  524.  
  525.    case MENUPICK:
  526.       select=msg->Code;
  527.       while (select != MENUNULL){
  528.          menunum=MENUNUM(select);
  529.          itemnum=ITEMNUM(select);
  530.          subnum=SUBNUM(select);
  531.          switch (menunum)  {
  532.             case PROJECT:
  533.                switch (itemnum){
  534.                   case ABOUT:
  535.                      setwin (w, RMBTRAP, CLOSEWINDOW, title_HELP );
  536.                      displayhelp();
  537.                      setwin (w, 0, IDCMPFLAGS, (char *) -1 );
  538.                      break;
  539.                   case MAP:
  540.                      switch (subnum){
  541.                         case FLAT:                    /* flat map */
  542.                            setwin (w, RMBTRAP, CLOSEWINDOW, title_FLAT );
  543.                            SetPointer (w, waiter, waiter_size/4-2, 16,
  544.                                  waiter_x_offset, waiter_y_offset);
  545.                            fullmap (map, FLAT, fill);
  546.                            SetPointer (w, arrow, arrow_size/4-2, 16,
  547.                            arrow_x_offset, arrow_y_offset);
  548.                            setwin (w, 0, IDCMPFLAGS, (char *) -1 );
  549.                            oldval = subnum;
  550.                            break;
  551.                         case MERCATOR:                /* Mercator map */
  552.                            setwin (w, RMBTRAP, CLOSEWINDOW, title_MERCATOR );
  553.                            SetPointer (w, waiter, waiter_size/4-2, 16,
  554.                            waiter_x_offset, waiter_y_offset);
  555.                            fullmap (map, MERCATOR, fill);
  556.                            SetPointer (w, arrow, arrow_size/4-2, 16,
  557.                            arrow_x_offset, arrow_y_offset);
  558.                            setwin (w, 0, IDCMPFLAGS, (char *) -1 );
  559.                            oldval = subnum;
  560.                            break;
  561.                         case BOX:                     /* box */
  562.                            setwin (w, RMBTRAP, IDCMPFLAGS, drag_prompt );
  563.                            if (oldval!=FLAT && oldval!=MERCATOR)  {
  564.                               ModifyIDCMP (w, CLOSEWINDOW);
  565.                               SetPointer (w, waiter, waiter_size/4-2, 16,
  566.                               waiter_x_offset, waiter_y_offset);
  567.                               fullmap (map, FLAT, fill);
  568.                               ModifyIDCMP (w, IDCMPFLAGS);
  569.                               oldval = FLAT;
  570.                            }
  571.                            SetPointer (w, cross, cross_size/4-2, 16,
  572.                            cross_x_offset, cross_y_offset);
  573.                            result = getbox (&x0, &y0, &x, &y);
  574.                            SetPointer (w, arrow, arrow_size/4-2, 16,
  575.                            arrow_x_offset, arrow_y_offset);
  576.                            if (result!=OK)  {
  577.                               setwin (w, 0, IDCMPFLAGS, box_error );
  578.                               DisplayBeep(s);
  579.                               break;
  580.                            }
  581.                            setwin (w, RMBTRAP, CLOSEWINDOW, title_BOX );
  582.                            SetPointer (w, waiter, waiter_size/4-2, 16,
  583.                            waiter_x_offset, waiter_y_offset);
  584.                            getcoord (x0, y0, oldval, &lat[0], &lam[0]);
  585.                            getcoord (x, y, oldval, &lat[1], &lam[1]);
  586.                            /* Move (rp, 0, 0);
  587.                            ClearScreen (rp);*/
  588.                            SetRast(rp,BLUE);
  589.                            box (map, lat, lam, fill);
  590.                            SetPointer (w, arrow, arrow_size/4-2, 16,
  591.                            arrow_x_offset, arrow_y_offset);
  592.                            setwin (w, 0, IDCMPFLAGS, (char *) -1 );
  593.                            oldval = subnum;
  594.                            break;
  595.                         default:
  596.                            break;
  597.                      }
  598.                      break;
  599.                   /* end of case MAP */
  600.                   case SPHERE:
  601.                      switch (subnum){
  602.                         case ZOOM_IN-SPHEREOFFSET:                 /* zoom views */
  603.                         case ZOOM_OUT-SPHEREOFFSET:
  604.                            if (subnum==ZOOM_IN-SPHEREOFFSET)  {
  605.                               view_height /= 2.;
  606.                               if (view_height<MIN_HEIGHT)
  607.                                     view_height = MIN_HEIGHT;
  608.                               sprintf (title_ORBITAL, fmt_ZOOM_IN, view_height);
  609.                            }
  610.                            else  {
  611.                               view_height *=2.;
  612.                               sprintf (title_ORBITAL, fmt_ZOOM_OUT, view_height);
  613.                            }
  614.                            eta = view_height/RE;
  615.                            facp = 1. + eta;
  616.                            etap = 1./facp;
  617.                         case GLOBE-SPHEREOFFSET:                 /* globe views */
  618.                         case ORBITAL-SPHEREOFFSET:
  619.                            setwin (w, RMBTRAP, IDCMPFLAGS, (char *)-1 );
  620.                            if ( (subnum!=ZOOM_IN-SPHEREOFFSET && subnum!=ZOOM_OUT-SPHEREOFFSET) || (oldval!=ZOOM_IN && oldval!=ZOOM_OUT && oldval!=GLOBE   && oldval!=ORBITAL) )  {
  621.                               SetWindowTitles (w, (char *) -1, press_prompt );
  622.                               if (oldval!=FLAT && oldval!=MERCATOR)  {
  623.                                  ModifyIDCMP (w, CLOSEWINDOW);
  624.                                  SetPointer (w, waiter, waiter_size/4-2, 16,
  625.                                              waiter_x_offset, waiter_y_offset);
  626.                                  fullmap (map, FLAT, fill);
  627.                                  ModifyIDCMP (w, IDCMPFLAGS);
  628.                                  oldval = FLAT;
  629.                               }
  630.                               SetPointer (w, cross, cross_size/4-2, 16,
  631.                                           cross_x_offset, cross_y_offset);
  632.                               do  {
  633.                                  WaitPort (w->UserPort);
  634.                                  msgf = (struct IntuiMessage *) GetMsg (w->UserPort);
  635.                                  if (msgf==NULL)
  636.                                     continue;
  637.                                  else if (msgf->Code== SELECTUP)
  638.                                     break;
  639.                                  ReplyMsg ((struct Message *)msgf);
  640.                               } while (1);
  641.                               x = msgf->MouseX;          /* get mouse position */
  642.                               y = msgf->MouseY;
  643.                               ReplyMsg ((struct Message *)msgf);
  644.                               getcoord (x, y, oldval, &latg, &lamg);
  645.                            }
  646.                            if (subnum==ORBITAL-SPHEREOFFSET)  {       /* re-initialize values for */
  647.                               view_height = VIEW_HEIGHT; /* orbital view          */
  648.                               eta = view_height/RE;
  649.                               facp = 1. + eta;
  650.                               etap = 1./facp;
  651.                               sprintf (title_ORBITAL, fmt_ORBITAL, view_height);
  652.                               setwin (w, RMBTRAP, CLOSEWINDOW, title_ORBITAL );
  653.                            }
  654.                            else if (subnum==GLOBE-SPHEREOFFSET)
  655.                               setwin (w, RMBTRAP, CLOSEWINDOW, title_GLOBE );
  656.                            else
  657.                               setwin (w, RMBTRAP, CLOSEWINDOW, title_ORBITAL);
  658.                            ModifyIDCMP (w, CLOSEWINDOW);
  659.                            SetPointer (w, waiter, waiter_size/4-2, 16,
  660.                                        waiter_x_offset, waiter_y_offset);
  661.                            /* Move (rp, 0, 0);
  662.                            ClearScreen (rp); crashes after do_text !*/
  663.                            SetRast (rp,BLACK);
  664.                            SetAPen (rp, BLUE);        /* blue globe */
  665.                            DrawEllipse (rp, CENTERX, CENTERY, HRADIUS, VRADIUS);
  666.                            Flood (rp, 1, CENTERX, CENTERY);
  667.                            SetAPen (rp, ORANGE);      /* reset colors */
  668.                            SetBPen (rp, BLUE);
  669.                            globe (map, map_trig, latg, lamg, subnum+SPHEREOFFSET, fill); /* draw map */
  670.                            stars();
  671.                            SetPointer (w, arrow, arrow_size/4-2, 16,
  672.                                        arrow_x_offset, arrow_y_offset);
  673.                            setwin (w, 0, IDCMPFLAGS, (char *) -1 );
  674.                            oldval = subnum+SPHEREOFFSET ;
  675.                            break;
  676.                         default:
  677.                            break;
  678.                     }
  679.                     break;
  680.                   case SAVE:
  681.                   case SAVEAS:
  682.                      /* number of screens saved yet */
  683.  
  684.                      if (itemnum==SAVE){
  685.                         if (picnum<999){ picnum++; } else { picnum=0; }
  686.                         if ( lenghtpicname < (MAXLENGHT-4) ){
  687.                             sprintf( picname+lenghtpicname ,".%03d",picnum);
  688.                             /* prints number into namestring */
  689.                         }
  690.                         else{
  691.                             setwin (w, 0, IDCMPFLAGS, SAVE_error );
  692.                             DisplayBeep(s);
  693.                         }
  694.                      }
  695.                      else{
  696.                         setwin (w, RMBTRAP, CLOSEWINDOW, SAVEAS_wait );
  697.                         lenghtpicname=saveas(&picname[0]);
  698.                         if (lenghtpicname==NULL || lenghtpicname > MAXLENGHT ){
  699.                             lenghtpicname=strlen(picname); /* old picname unchanged*/
  700.                             setwin (w, 0, IDCMPFLAGS, SAVE_error );
  701.                             DisplayBeep(s);
  702.                             break;
  703.                         }
  704.                      }
  705.  
  706.                      setwin (w, RMBTRAP, CLOSEWINDOW, SAVE_wait );
  707.                      SetPointer (w, waiter, waiter_size/4-2, 16,
  708.                            waiter_x_offset, waiter_y_offset);
  709.                      if (save(&picname[0])!=OK){
  710.                         setwin (w, 0, IDCMPFLAGS, SAVE_error );
  711.                         DisplayBeep(s);
  712.                      }
  713.                      else{
  714.                         setwin (w, 0, IDCMPFLAGS, title_SAVE );
  715.                      }
  716.                      if (itemnum==SAVE){     /* cut number of saved screen */
  717.                         *(picname+lenghtpicname)='\0';
  718.                      }
  719.  
  720.                      SetPointer (w, arrow, arrow_size/4-2, 16,
  721.                            arrow_x_offset, arrow_y_offset);
  722.                      break;
  723.                   case PRINT:
  724.                      setwin (w, RMBTRAP, CLOSEWINDOW, PRINT_wait );
  725.                      SetPointer (w, waiter, waiter_size/4-2, 16,
  726.                            waiter_x_offset, waiter_y_offset);
  727.                      if(dump(wt)!=OK){
  728.                         setwin (w, 0, IDCMPFLAGS, PRINT_error );
  729.                         DisplayBeep(s);
  730.                      }
  731.                      else{
  732.                         setwin (w, 0, IDCMPFLAGS, title_PRINT );
  733.                      }
  734.                      SetPointer (w, arrow, arrow_size/4-2, 16,
  735.                            arrow_x_offset, arrow_y_offset);
  736.  
  737.                      Forbid();
  738.                      if (IntuitionBase->ActiveWindow==wt){
  739.                         ActivateWindow(w);
  740.                         /* ... because titlebar window has been activated
  741.                            and all IDCMP Messages are eaten by dump() */
  742.                      }
  743.                      Permit();
  744.  
  745.                      break;
  746.                   case GRID:
  747.                      if (oldval!=FLAT     && oldval!=MERCATOR &&
  748.                             oldval!=GLOBE    && oldval!=ORBITAL  &&
  749.                             oldval!=ZOOM_IN  && oldval!=ZOOM_OUT){
  750.                         SetWindowTitles (w, (char *) -1, grid_error );
  751.                         DisplayBeep(s);
  752.                      }
  753.  
  754.                      else  {
  755.                         setwin (w, RMBTRAP , CLOSEWINDOW, title_GRID );
  756.                         SetPointer (w, waiter, waiter_size/4-2, 16,
  757.                                     waiter_x_offset, waiter_y_offset);
  758.                         grid (oldval, latg, lamg);
  759.                         SetPointer (w, arrow, arrow_size/4-2, 16,
  760.                                  arrow_x_offset, arrow_y_offset);
  761.                         setwin (w, 0, IDCMPFLAGS, (char *) -1 );
  762.                      }
  763.                      break;
  764.                   case CLEAR:                  /* clear screen */
  765.                      SetWindowTitles (w,(char *) -1, title_CLEARS);
  766.                      /* Move (rp, 0, 0);
  767.                      ClearScreen (rp); */
  768.                      SetRast(rp,BLUE);
  769.                      oldval = -1;
  770.                      break;
  771.                   case QUIT:
  772.                      ReplyMsg((struct Message *)msg);
  773.                      return(NOT_OK);
  774.                      break;
  775.                   default:
  776.                      break;
  777.                }
  778.                break;
  779.             case EDIT:
  780.                switch (itemnum){
  781.                   case COLOR_F:                 /* toggle color-fill flag */
  782.                      flag=((struct MenuItem *) ItemAddress(&Menu1, (LONG) select))->Flags;
  783.                      if (flag & CHECKED)
  784.                         fill = FILL;
  785.                      else
  786.                         fill = NOFILL;
  787.                      break;
  788.                   case COLOR:
  789.                      activepen=subnum; /* Set Active Edit Pen */
  790.                      break;
  791.                   case DRAW:
  792.                      setwin (w, RMBTRAP, IDCMPFLAGS, draw_wait );
  793.                      SetPointer (w, cross, cross_size/4-2, 16,
  794.                                  cross_x_offset, cross_y_offset);
  795.                      drawline(activepen);
  796.                      SetPointer (w, arrow, arrow_size/4-2, 16,
  797.                                  arrow_x_offset, arrow_y_offset);
  798.                      setwin (w, 0, IDCMPFLAGS, title_DRAW );
  799.                      break;
  800.                   case FLOOD:                   /* flood fill */
  801.                      setwin (w, RMBTRAP, MOUSEBUTTONS, flood_wait );
  802.                      SetPointer (w, cross, cross_size/4-2, 16,
  803.                                  cross_x_offset, cross_y_offset);
  804.                      floodfill (activepen);
  805.                      SetPointer (w, arrow, arrow_size/4-2, 16,
  806.                                  arrow_x_offset, arrow_y_offset);
  807.                      setwin (w, 0, IDCMPFLAGS, title_FLOOD );
  808.                      break;
  809.                   case TEXT:                    /* get user text */
  810.                      setwin (w, RMBTRAP, IDCMPFLAGS, title_TEXT);
  811.                      do_text (msg, activepen);
  812.                      setwin (w, 0, IDCMPFLAGS, title_TEXT );
  813.                      break;
  814.                   case COLORPALETTE:                  /* modify color table */
  815.                      SetWindowTitles (w, (char *) -1, title_COLORS );
  816.                      color_offset += 4;
  817.                      if (color_offset>=num_colors)
  818.                         color_offset = 0;
  819.                      LoadRGB4 (vp, &mapcolors[color_offset], 4);
  820.                      break;
  821.                   default:
  822.                      break;
  823.                }
  824.                break;
  825.             case SPECIAL:
  826.                switch (itemnum){
  827.                   case SHADOW:                  /* make shadows */
  828.                      setwin (w, RMBTRAP, CLOSEWINDOW, title_SHADOW );
  829.                      SetPointer (w, waiter, waiter_size/4-2, 16,
  830.                                  waiter_x_offset, waiter_y_offset);
  831.                      shadow ();
  832.                      SetPointer (w, arrow, arrow_size/4-2, 16,
  833.                                  arrow_x_offset, arrow_y_offset);
  834.                      setwin (w, 0, IDCMPFLAGS, (char *) -1 );
  835.                      break;
  836.                   default:
  837.                      break;
  838.                }
  839.                break;
  840.             default:
  841.                break;
  842.          }
  843.          select=((struct MenuItem *) ItemAddress(&Menu1, (LONG) select))->NextSelect;
  844.       } /* end of while */
  845.    /* end of case MENUPICK */
  846.    default:
  847.       break;
  848.    } /* end of switch (class) */
  849.    ReplyMsg((struct Message *)msg);
  850.    return (OK);
  851. }
  852.  
  853.  
  854. /* ============================================================= */
  855.  
  856. void fullmap (ws, type, fill)          /* draws flat map projections */
  857.  
  858. short *ws;
  859. int type, fill;
  860.  
  861. {
  862.  
  863.    extern void afill(), adraw(), plotpoint();
  864.    int j, i, k, h1, h2, narea;
  865.    double t;
  866.    long pen;
  867.    short np, xs, ys;
  868.  
  869.    /* Move (rp, 0, 0);                    * clear screen *
  870.    ClearScreen (rp); */
  871.    SetRast(rp,BLUE);
  872.    j = 0;
  873.    np = 0;
  874.    narea = 0;
  875.    pen = ORANGE;
  876.    SetAPen (rp, pen);
  877.    for (i=0; i<MAXVAL; i+=2)  {
  878.       if (ws[i]==0 && ws[i+1]==0)  {   /* check for end of area */
  879.          ++narea;
  880.          pen = ORANGE;
  881.          for (k=0; k<num_lakes; ++k)  {
  882.             if (narea==lakes[k])  {
  883.                pen = BLUE;
  884.                break;
  885.             }
  886.          }
  887.          SetAPen (rp, pen);
  888.          if (np<=1)  {
  889.             xs = CENTERX + area[j-2];
  890.             ys = CENTERY + area[j-1];
  891.             plotpoint (xs, ys);
  892.          }
  893.          else  {
  894.             if (fill==FILL)
  895.                afill (np, pen);
  896.             else
  897.                adraw (np);
  898.          }
  899.          j = 0;
  900.          np = 0;
  901.          area[0] = 0;
  902.          area[1] = 0;
  903.          pen = ORANGE;
  904.          SetAPen (rp, pen);
  905.       }
  906.       else  {                          /* get coords of new point */
  907.          t = ws[i];                    /* y = latitude */
  908.          if (type==FLAT)
  909.             t = (t/100.) * VFACTOR;
  910.          else if (type==MERCATOR)  {
  911.             t = (t/200. + 45.) * rad;
  912.             t = log (tan (t)) * M_VFACTOR;
  913.          }
  914.          if (t<0.)
  915.             t -= 0.5;
  916.          else
  917.             t += 0.5;
  918.          h2 = -t;
  919.          t = ws[i+1];                  /* x = longitude */
  920.          t = (t/100.) * HFACTOR;
  921.          if (t<0.)
  922.             t -= 0.5;
  923.          else
  924.             t += 0.5;
  925.          h1 = t;
  926.          if (np!=0)  {                 /* disallow identical adjacent pts */
  927.             if (h1==area[j-2] && h2==area[j-1])
  928.                continue;
  929.          }
  930.          xs = CENTERX + h1;
  931.          ys = CENTERY + h2;
  932.          plotpoint (xs, ys);
  933.          area[j] = h1;
  934.          area[j+1] = h2;
  935.          j += 2;
  936.          ++np;
  937.       }
  938.    }
  939.    if (np>0)  {                        /* check for last area */
  940.       pen = ORANGE;
  941.       SetAPen (rp, pen);
  942.       if (np==1)  {
  943.          xs = CENTERX + area[j-2];
  944.          ys = CENTERY + area[j-1];
  945.          plotpoint (xs, ys);
  946.       }
  947.       else  {
  948.          if (fill==FILL)
  949.             afill (np, pen);
  950.          else
  951.             adraw (np);
  952.       }
  953.    }
  954.    SetAPen (rp, ORANGE);
  955. }
  956.  
  957. /* ============================================================= */
  958.  
  959. void grid (val, lat0, lam0)            /* controls drawing of grids */
  960.  
  961. long val;
  962. double lat0, lam0;
  963.  
  964. {
  965.  
  966.    extern void globe_grid();
  967.    long oldpen, h1;
  968.    double lam, lat;
  969.    static double lat_interval = 20.;
  970.    static double lam_interval = 30.;
  971.  
  972.    oldpen = rp->FgPen;                 /* save original pen color */
  973.    if (val!=FLAT && val!=MERCATOR)     /* draw grid for globe     */
  974.       globe_grid (val, lat0, lam0);
  975.    else  {                             /* otherwise grid for flat */
  976.                                        /*   or Mercator map       */
  977.       SetAPen (rp, BLACK);             /* set grid color to black */
  978.       for (lam=-180.; lam<=+180.; lam += lam_interval)  {
  979.          h1 = lam*HFACTOR + CENTERX;
  980.          Move (rp, h1, 0);
  981.          Draw (rp, h1, WHEIGHT-1);
  982.       }
  983.       for (lat=80.; lat>=-80.; lat -= lat_interval)  {
  984.          if (val==FLAT)
  985.             h1 = -lat*VFACTOR;
  986.          else
  987.             h1 = -log (tan((lat/2.+45.)*rad)) * M_VFACTOR;
  988.          h1 += CENTERY;
  989.          Move (rp, 0, h1);
  990.          Draw (rp, WWIDTH-1, h1);
  991.       }
  992.       SetAPen (rp, WHITE);             /* draw coordinate axes in white */
  993.       Move (rp, CENTERX, 0);
  994.       Draw (rp, CENTERX, WHEIGHT-1);
  995.       Move (rp, 0, CENTERY);
  996.       Draw (rp, WWIDTH-1, CENTERY);
  997.    }
  998.    SetAPen (rp, oldpen);               /* restore pen color */
  999. }
  1000.  
  1001. /* ============================================================= */
  1002.  
  1003. void globe_grid (val, lat0, lam0)      /* controls drawing globe grid */
  1004.  
  1005. double lat0, lam0;
  1006. long val;
  1007.  
  1008. {
  1009.  
  1010.    extern void globe_grid_plot();
  1011.    extern int limit_lam(), limit_lat();
  1012.    double lat, lam, c0, s0, c1, s1, c2, s2, rlam, rlat;
  1013.    double hp, scale, fac3, dlat, dlam, ddelt;
  1014.    double lamp[2], latp[2];
  1015.    int k;
  1016.    char first;
  1017.    static double lat_interval = 20.;
  1018.    static double lam_interval = 30.;
  1019.    static double delta = 5.;
  1020.  
  1021.    lat0 *= rad;
  1022.    c0 = cos (lat0);
  1023.    s0 = sin (lat0);
  1024.    dlat = lat_interval;
  1025.    dlam = lam_interval;
  1026.    ddelt = delta;
  1027.    if (val==GLOBE)  {                  /* ordinary globe view */
  1028.       hp = 0.;
  1029.       scale = 1.;
  1030.       fac3 = 1.;
  1031.    }
  1032.    else  {                             /* orbital view */
  1033.       hp = etap;
  1034.       scale = sqrt (1.-etap*etap);
  1035.       fac3 = (facp/(facp-hp)) * scale;
  1036.       if (view_height<=1200.)  {
  1037.          dlat /= 4.;
  1038.          dlam /= 4.;
  1039.          ddelt /= 5.;
  1040.       }
  1041.    }
  1042.    SetAPen (rp, BLACK);                /* grid lines in black */
  1043.    for (lat=80.; lat>=-80.; lat-=dlat)  {  /* lines of equal latitude */
  1044.       rlat = lat*rad;
  1045.       if ((k=limit_lam (&lamp[0], rlat, lat0, hp))!=OK)
  1046.          continue;                     /* skip if entirely out of view */
  1047.       lamp[0] += lam0;
  1048.       lamp[1] += lam0;
  1049.       k = lamp[0]/ddelt - 1.;          /* express limits as multiple */
  1050.       lamp[0] = k*ddelt;               /*   of ddelt                 */
  1051.       k = lamp[1]/ddelt + 1.;
  1052.       lamp[1] = k*ddelt;
  1053.       c1 = cos (rlat);
  1054.       s1 = sin (rlat);
  1055.       first = TRUE;
  1056.       if (lat==0.)
  1057.          SetAPen (rp, WHITE);          /* draw equator in white */
  1058.       for (lam=lamp[0]; lam<=lamp[1]; lam+=ddelt)  {
  1059.          rlam = (lam-lam0)*rad;
  1060.          if (rlam<-pi)
  1061.             rlam += twopi;
  1062.          if (rlam>+pi)
  1063.             rlam -= twopi;
  1064.          c2 = cos (rlam);
  1065.          s2 = sin (rlam);
  1066.          globe_grid_plot (rlat, rlam, c0, s0, c1, s1, c2, s2,
  1067.                           val, hp, scale, fac3, &first);
  1068.       }
  1069.       if (lat==0.)
  1070.          SetAPen (rp, BLACK);          /* reset pen to black */
  1071.    }
  1072.    for (lam=-180.; lam<+180.; lam+=dlam)  {  /* meridian circles */
  1073.       rlam = (lam-lam0)*rad;
  1074.       if (rlam<-pi)
  1075.          rlam += twopi;
  1076.       if (rlam>+pi)
  1077.          rlam -= twopi;
  1078.       if ((k=limit_lat (&latp[0], lat0, rlam, hp))!=OK)
  1079.          continue;                     /* skip if entirely out of view */
  1080.       k = latp[0]/ddelt + 1.;          /* express limits as multiple */
  1081.       latp[0] = k*ddelt;               /*   of ddelt                 */
  1082.       k = latp[1]/ddelt - 1;
  1083.       latp[1] = k*ddelt;
  1084.       if (latp[0]>=90.)                /* exclude North polar point */
  1085.          latp[0] = 90. - ddelt;
  1086.       if (latp[1]<=-90.)               /* exclude South polar point */
  1087.          latp[1] = -90. + ddelt;
  1088.       c2 = cos (rlam);
  1089.       s2 = sin (rlam);
  1090.       first = TRUE;
  1091.       if (lam==0. || lam==-180.)       /* prime meridian in white */
  1092.          SetAPen (rp, WHITE);
  1093.       for (lat=latp[0]; lat>=latp[1]; lat-=ddelt)  {
  1094.          rlat = lat*rad;
  1095.          c1 = cos (rlat);
  1096.          s1 = sin (rlat);
  1097.          globe_grid_plot (rlat, rlam, c0, s0, c1, s1, c2, s2,
  1098.                           val, hp, scale, fac3, &first);
  1099.       }
  1100.       if (lam==0. || lam==-180.)
  1101.          SetAPen (rp, BLACK);          /* reset pen to black */
  1102.    }
  1103. }
  1104.  
  1105. /* ============================================================= */
  1106.  
  1107. void globe_grid_plot (lat, lam, c0, s0, c1, s1, c2, s2,
  1108.                       val, hp, scale, fac3, first)
  1109.  
  1110. double lat, lam, c0, s0, c1, s1, c2, s2;   /* draws globe grids */
  1111. long val;
  1112. double hp, scale, fac3;
  1113. char *first;
  1114.  
  1115. {
  1116.  
  1117.    extern void plotpoint(), globe_rim_fix();
  1118.    char in_view;
  1119.    short h1, h1c;                      /* x-dist. (pix) from center */
  1120.    short h2, h2c;                      /* y-dist. (pix) from center */
  1121.    long x, y;
  1122.    double lamc, dlam;                  /* longitude */
  1123.    double latc, dlat;                  /* latitude  */
  1124.    double zp, facz, h1d, h2d, fac2;
  1125.    static char prev_in_view;
  1126.    static short h1prev, h2prev;
  1127.    static double latprev, lamprev, zpprev;
  1128.  
  1129.    in_view = FALSE;                    /* get status of current point */
  1130.    zp = s1*s0 + c1*c0*c2;
  1131.    if (zp>=hp)  {                      /* zp > hp => in view */
  1132.       in_view = TRUE;
  1133.       h1d = HRADIUS * c1 * s2;
  1134.       h2d = -VRADIUS * (s1*c0 - c1*s0*c2);
  1135.       if (val!=GLOBE)  {
  1136.          fac2 = (facp/(facp-zp))*scale;
  1137.          h1d *= fac2;
  1138.          h2d *= fac2;
  1139.       }
  1140.       h1 = h1d;
  1141.       h2 = h2d;
  1142.    }
  1143.    if (*first==TRUE)  {                /* get status of first point */
  1144.       *first = FALSE;
  1145.       if (in_view==TRUE)  {            /* if first point is in view, */
  1146.          x = h1 + CENTERX;             /*   move pen to first point  */
  1147.          y = h2 + CENTERY;             /*   and plot it              */
  1148.          Move (rp, x, y);
  1149.          plotpoint (x, y);
  1150.          h1prev = h1;
  1151.          h2prev = h2;
  1152.       }
  1153.       prev_in_view = in_view;          /* save status of first point */
  1154.       latprev = lat;
  1155.       lamprev = lam;
  1156.       zpprev = zp;
  1157.       return;
  1158.    }
  1159.    if (in_view==TRUE)  {               /* if current point is in view, */
  1160.       if (prev_in_view==FALSE)  {      /*   but previous point was not */
  1161.          facz = zp / (zpprev-zp);      /*   in view, get rim point by  */
  1162.          latc = lat - (latprev-lat)*facz; /* linear interpolation      */
  1163.          lamc = lam - (lamprev-lam)*facz;
  1164.          dlat = fabs (lat-latprev);
  1165.          dlam = fabs (lam-lamprev);
  1166.          if ( fabs (latc-latprev)> dlat || /* if rim point not between */
  1167.               fabs (latc-lat)    > dlat)   /*   current and previous   */
  1168.             latc = (lat+latprev)/2.;       /*   points, use midpoint   */
  1169.          if ( fabs (lamc-lamprev)> dlam ||
  1170.               fabs (lamc-lam)    > dlam )
  1171.             lamc = (lam+lamprev)/2.;
  1172.          c1 = cos (latc);
  1173.          h1d = HRADIUS * c1 * sin (lamc);
  1174.          h2d = -VRADIUS * (sin (latc)*c0 - c1*s0*cos (lamc));
  1175.          if (val!=GLOBE)  {
  1176.             h1d *= fac3;
  1177.             h2d *= fac3;
  1178.          }
  1179.          h1c = h1d;
  1180.          h2c = h2d;
  1181.          globe_rim_fix (&h1c, &h2c);
  1182.          x = h1c + CENTERX;            /* move to rim point & plot it */
  1183.          y = h2c + CENTERY;
  1184.          Move (rp, x, y);
  1185.          plotpoint (x, y);
  1186.          h1prev = h1c;
  1187.          h2prev = h2c;
  1188.       }
  1189.       if (h1!=h1prev || h2!=h2prev)  {
  1190.          x = h1 + CENTERX;             /* draw to current point */
  1191.          y = h2 + CENTERY;
  1192.          Draw (rp, x, y);
  1193.       }
  1194.       h1prev = h1;
  1195.       h2prev = h2;
  1196.    }
  1197.    else  {                             /* current point is out of view   */
  1198.       if (prev_in_view==TRUE)  {       /* if previous point was in view, */
  1199.          facz = zp / (zpprev-zp);      /*   get rim point by linear      */
  1200.          latc = lat - (latprev-lat)*facz; /* interpolation               */
  1201.          lamc = lam - (lamprev-lam)*facz;
  1202.          dlat = fabs (lat-latprev);
  1203.          dlam = fabs (lam-lamprev);
  1204.          if ( fabs (latc-latprev)> dlat || /* if rim point not between */
  1205.               fabs (latc-lat)    > dlat)   /*   current and previous   */
  1206.             latc = (lat+latprev)/2.;       /*   points, use midpoint   */
  1207.          if ( fabs (lamc-lamprev)> dlam ||
  1208.               fabs (lamc-lam)    > dlam )
  1209.             lamc = (lam+lamprev)/2.;
  1210.          c1 = cos (latc);
  1211.          h1d = HRADIUS * c1 * sin (lamc);
  1212.          h2d = -VRADIUS * (c0*sin (latc) - c1*s0 * cos (lamc));
  1213.          if (val!=GLOBE)  {
  1214.             h1d *= fac3;
  1215.             h2d *= fac3;
  1216.          }
  1217.          h1c = h1d;
  1218.          h2c = h2d;
  1219.          globe_rim_fix (&h1c, &h2c);
  1220.          x = h1c + CENTERX;            /* draw to rim point */
  1221.          y = h2c + CENTERY;
  1222.          Draw (rp, x, y);
  1223.       }
  1224.    }
  1225.    prev_in_view = in_view;             /* save status of current point */
  1226.    latprev = lat;
  1227.    lamprev = lam;
  1228.    zpprev = zp;
  1229. }
  1230.  
  1231. /* ============================================================= */
  1232.  
  1233. int limit_lam (lam, lat, lat0, etap)   /* computes limits on longitude */
  1234.                                        /*   for constant latitude      */
  1235. double *lam, lat, lat0, etap;
  1236.  
  1237. {
  1238.    double alpha;
  1239.    alpha = (etap-sin(lat)*sin(lat0)) / (cos(lat)*cos(lat0)+0.00000001);
  1240.                  /* +0.00..01 prevents GURU 5 crashes (divide by zero) */
  1241.    if (alpha<=-1.)  {                  /* negative => lamda covers */
  1242.       lam[0] = -180.;                  /*   entire hemisphere      */
  1243.       lam[1] = +180.;
  1244.       return (OK);
  1245.    }
  1246.    else if (alpha>=+1.)                /* positive => nothing in view */
  1247.       return (NOT_OK);
  1248.    else  {                             /* otherwise, compute limits */
  1249.       lam[0] = -acos (alpha)/rad;
  1250.       lam[1] = -lam[0];
  1251.       return (OK);
  1252.    }
  1253. }
  1254.  
  1255. /* ============================================================= */
  1256.  
  1257. int limit_lat (lat, lat0, lam, etap)   /* computes limits on latitude */
  1258.                                        /*   for constant longitude    */
  1259. double *lat, lat0, lam, etap;
  1260.  
  1261. {
  1262.    double radical, a, b, sum, fac1;
  1263.  
  1264.    a = sin (lat0);
  1265.    b = cos (lat0) * cos (lam);
  1266.    sum = a*a + b*b;
  1267.    radical = sum - etap*etap;
  1268.    if (radical<=0.)                    /* no real solutions */
  1269.       return (NOT_OK);
  1270.    else  {                             /* two real solutions      */
  1271.       radical = sqrt (radical);        /* solve quadrtic equation */
  1272.       fac1 = (a*etap + b*radical)/sum;
  1273.       lat[0] = asin (fac1)/rad;
  1274.       fac1 = (a*etap - b*radical)/sum;
  1275.       lat[1] = asin (fac1)/rad;
  1276.       if (lat[0]<lat[1])  {            /* put in correct order */
  1277.          b = lat[0];
  1278.          lat[0] = lat[1];
  1279.          lat[1] = b;
  1280.       }
  1281.       if (a>etap)                      /* check North pole */
  1282.          lat[0] = 90.;
  1283.       if (a<-etap)                     /* check South pole */
  1284.          lat[1] = -90.;
  1285.       return (OK);
  1286.    }
  1287. }
  1288.  
  1289. /* ============================================================= */
  1290.  
  1291. void getcoord (x, y, val, lat, lam)    /* converts screen coordinates   */
  1292.                                        /*   into latitude and longitude */
  1293. int x, y;
  1294. long val;
  1295. double *lat, *lam;
  1296.  
  1297. {
  1298.  
  1299.    (*lam) = (x - CENTERX) / HFACTOR;
  1300.    if (val==FLAT)
  1301.       (*lat) = (CENTERY - y) / VFACTOR;
  1302.    else
  1303.       (*lat) = -90. + 2.*atan(exp((CENTERY-y)/M_VFACTOR))/rad;
  1304. }
  1305.  
  1306. /* ============================================================= */
  1307.  
  1308. void globe (ws, ws_trig, lat0, lam0, val, fill) /* draws globe projections */
  1309.  
  1310. short *ws, *ws_trig;
  1311. double lat0, lam0;
  1312. long val;
  1313. int fill;
  1314.  
  1315. {
  1316.  
  1317.    extern void plotpoint(), globe_fill(), globe_rim_fix(), globe_tidy();
  1318.    char first, prev_in_view, in_view, latzero;
  1319.    short h1, h1c, h1prev;              /* x-dist. (pix) from center */
  1320.    short h2, h2c, h2prev;              /* y-dist. (pix) from center */
  1321.    short np, narea,nrim_in, nrim_out;
  1322.    short xs, ys;
  1323.    int i, j;
  1324.    long x, y, pen, first_color;
  1325.    double lam, lamc, lamprev;          /* longitude */
  1326.    double lat, latc, latprev;          /* latitude  */
  1327.    double c0, s0, c1, s1, c2, zp, zpprev;
  1328.    double hp, fac2, fac3, facz, scale;
  1329.    double h1d, h2d, dlat, dlam, lat0p;
  1330.    static double fac = 1./32767.0;
  1331.  
  1332.    latzero = FALSE;
  1333.    if (val==GLOBE)  {                  /* ordinary globe view */
  1334.       hp = 0.;
  1335.       scale = 1.;
  1336.       fac3 = 1.;
  1337.    }
  1338.    else  {                             /* orbital globe view */
  1339.       hp = etap;
  1340.       scale = sqrt (1.-etap*etap);
  1341.       fac3 = (facp/(facp-hp)) * scale;
  1342.    }
  1343.    if (lat0==0.)  {
  1344.       c0 = 1.;
  1345.       s0 = 0.;
  1346.       if (val==GLOBE)
  1347.          latzero = TRUE;               /* equatorial, ordinary globe view */
  1348.    }
  1349.    else  {
  1350.       lat0p = lat0 * rad;
  1351.       c0 = cos (lat0p);
  1352.       s0 = sin (lat0p);
  1353.    }
  1354.    first = TRUE;
  1355.    pen = ORANGE;
  1356.    SetAPen (rp, pen);
  1357.    first_color = BLACK;
  1358.    j = 0;
  1359.    np = 0;
  1360.    narea = 0;
  1361.    nrim_in = 0;
  1362.    nrim_out = 0;
  1363.    for (i=0; i<MAXVAL; i+=2)  {
  1364.       if (ws[i]==0 && ws[i+1]==0)  {   /* if end of area, then     */
  1365.          ++narea;                      /*   color-fill and skip to */
  1366.          first = TRUE;                 /*    next area             */
  1367.          if (np>0 && fill==FILL)
  1368.             globe_fill (np, narea, nrim_in, nrim_out, first_color);
  1369.          j = 0;
  1370.          np = 0;
  1371.          nrim_in = 0;
  1372.          nrim_out = 0;
  1373.          first_color = BLACK;
  1374.          continue;
  1375.       }
  1376.       lat = ws[i];                     /* latitude */
  1377.       lat = (lat/100.) * rad;
  1378.       lam = ws[i+1];                   /* longitude */
  1379.       lam = (lam/100. - lam0) * rad;
  1380.       if (lam<-pi)
  1381.          lam += twopi;
  1382.       if (lam>pi)
  1383.          lam -= twopi;
  1384.       c1 = ws_trig[i];                 /* cosine of latitude */
  1385.       c1 *= fac;
  1386.       s1 = ws_trig[i+1];               /* sine of latitude */
  1387.       s1 *= fac;
  1388.       in_view = FALSE;                 /* get status of current point */
  1389.       if (latzero==TRUE)  {            /* equatorial globe view */
  1390.          zp = c1*cos (lam);
  1391.          if (lam>=-pi2 && lam<=+pi2)  {
  1392.             in_view = TRUE;
  1393.             h1 = HRADIUS * c1 * sin (lam);
  1394.             h2 = -VRADIUS * s1;
  1395.          }
  1396.       }
  1397.       else  {                          /* oblique earth view */
  1398.          c2 = cos (lam);
  1399.          zp = s1*s0 + c1*c0*c2;
  1400.          if (zp>=hp)  {                /* zp > hp => in view */
  1401.             in_view = TRUE;
  1402.             h1d = HRADIUS * c1 * sin (lam);
  1403.             h2d = -VRADIUS * (s1*c0 -c1*s0*c2);
  1404.             if (val!=GLOBE)  {
  1405.                fac2 = (facp/(facp-zp)) * scale;
  1406.                h1d *= fac2;
  1407.                h2d *= fac2;
  1408.             }
  1409.             h1 = h1d;
  1410.             h2 = h2d;
  1411.          }
  1412.       }
  1413.       if (first==TRUE)  {              /* get status of first point */
  1414.          first = FALSE;
  1415.          if (in_view==TRUE)  {         /* if first point is in view, */
  1416.             x = h1 + CENTERX;          /*   move pen to first point  */
  1417.             y = h2 + CENTERY;          /*   and plot it              */
  1418.             Move (rp, x, y);
  1419.             first_color = ReadPixel (rp, x, y);
  1420.             xs = x;
  1421.             ys = y;
  1422.             plotpoint (xs, ys);
  1423.             h1prev = h1;
  1424.             h2prev = h2;
  1425.             area[0] = h1;              /* save first point for later use */
  1426.             area[1] = h2;
  1427.             j = 2;
  1428.             np = 1;
  1429.          }
  1430.          prev_in_view = in_view;       /* save status of first point */
  1431.          latprev = lat;
  1432.          lamprev = lam;
  1433.          zpprev = zp;
  1434.          continue;
  1435.       }
  1436.       if (in_view==TRUE)  {            /* if current point is in view, */
  1437.          if (prev_in_view==FALSE)  {   /*   but previous point was not */
  1438.             facz = zp / (zpprev-zp);   /*   in view, get rim point by  */
  1439.             latc = lat - (latprev-lat)*facz; /* linear interpolation   */
  1440.             lamc = lam - (lamprev-lam)*facz;
  1441.             dlat = fabs (lat-latprev);
  1442.             dlam = fabs (lam-lamprev);
  1443.             if ( fabs (latc-latprev)> dlat || /* if rim point not between */
  1444.                  fabs (latc-lat)    > dlat)   /*   current and previous   */
  1445.                latc = (lat+latprev)/2.;       /*   point, use midpoint    */
  1446.             if ( fabs (lamc-lamprev)> dlam ||
  1447.                  fabs (lamc-lam)    > dlam )
  1448.                lamc = (lam+lamprev)/2.;
  1449.             if (latzero==TRUE)  {
  1450.                h1c = HRADIUS * cos (latc) * sin (lamc);
  1451.                h2c = -VRADIUS * sin (latc);
  1452.             }
  1453.             else  {
  1454.                c1 = cos (latc);
  1455.                h1d = HRADIUS * c1 * sin (lamc);
  1456.                h2d = -VRADIUS * (sin (latc)*c0 - c1*s0*cos (lamc));
  1457.                if (val!=GLOBE)  {
  1458.                   h1d *= fac3;
  1459.                   h2d *= fac3;
  1460.                }
  1461.                h1c = h1d;
  1462.                h2c = h2d;
  1463.             }
  1464.             globe_rim_fix (&h1c, &h2c); /* correct the computed rim point */
  1465.             x = h1c + CENTERX;         /* move to rim point & plot it */
  1466.             y = h2c + CENTERY;
  1467.             Move (rp, x, y);
  1468.             xs = x;
  1469.             ys = y;
  1470.             plotpoint (xs, ys);
  1471.             h1prev = h1c;
  1472.             h2prev = h2c;
  1473.             area[j] = h1;              /* save coords of rim point */
  1474.             area[j+1] = h2;
  1475.             j += 2;
  1476.             ++nrim_in;
  1477.             ++np;
  1478.          }
  1479.          if (h1!=h1prev || h2!=h2prev)  {
  1480.             x = h1 + CENTERX;          /* draw to current point */
  1481.             y = h2 + CENTERY;
  1482.             Draw (rp, x, y);
  1483.             area[j] = h1;              /* save coords of current point */
  1484.             area[j+1] = h2;
  1485.             j += 2;
  1486.             ++np;
  1487.          }
  1488.          h1prev = h1;
  1489.          h2prev = h2;
  1490.       }
  1491.       else  {                          /* current point is out of view   */
  1492.          if (prev_in_view==TRUE)  {    /* if previous point was in view, */
  1493.             facz = zp / (zpprev-zp);   /*   get rim point by linear      */
  1494.             latc = lat - (latprev-lat)*facz; /* interpolation            */
  1495.             lamc = lam - (lamprev-lam)*facz;
  1496.             dlat = fabs (lat-latprev);
  1497.             dlam = fabs (lam-lamprev);
  1498.             if ( fabs (latc-latprev)> dlat || /* if rim point not between */
  1499.                  fabs (latc-lat)    > dlat)   /*   current and previous   */
  1500.                latc = (lat+latprev)/2.;       /*   point, use midpoint    */
  1501.             if ( fabs (lamc-lamprev)> dlam ||
  1502.                  fabs (lamc-lam)    > dlam )
  1503.                lamc = (lam+lamprev)/2.;
  1504.             if (latzero==TRUE)  {
  1505.                h1c = HRADIUS * cos (latc) * sin (lamc);
  1506.                h2c = -VRADIUS * sin (latc);
  1507.             }
  1508.             else  {
  1509.                c1 = cos (latc);
  1510.                h1d = HRADIUS * c1 * sin (lamc);
  1511.                h2d = -VRADIUS * (c0*sin(latc) - c1*s0 * cos(lamc));
  1512.                if (val!=GLOBE)  {
  1513.                   h1d *= fac3;
  1514.                   h2d *= fac3;
  1515.                }
  1516.                h1c = h1d;
  1517.                h2c = h2d;
  1518.             }
  1519.             globe_rim_fix (&h1c, &h2c); /* correct the computed rim point */
  1520.             x = h1c + CENTERX;         /* draw to rim point */
  1521.             y = h2c + CENTERY;
  1522.             Draw (rp, x, y);
  1523.             area[j] = h1;              /* save coords of rim point */
  1524.             area[j+1] = h2;
  1525.             j += 2;
  1526.             ++nrim_out;
  1527.             ++np;
  1528.          }
  1529.       }
  1530.       prev_in_view = in_view;          /* save status of current point */
  1531.       latprev = lat;
  1532.       lamprev = lam;
  1533.       zpprev = zp;
  1534.    }
  1535.    if (fill == FILL)
  1536.       globe_tidy (td, td_trig, lat0, lam0, val);
  1537. }
  1538.  
  1539. /* ============================================================= */
  1540.  
  1541. void globe_fill (np, narea, nrim_in, nrim_out, first_color)
  1542.                                        /* fills area on globe */
  1543.  
  1544. short np, narea, nrim_in, nrim_out;
  1545. long first_color;
  1546.  
  1547. {
  1548.  
  1549.    extern void plotpoint ();
  1550.    int k;
  1551.    short xs, ys;
  1552.    char is_lake;
  1553.    static long pen = ORANGE;
  1554.  
  1555.    if (nrim_in!=nrim_out)
  1556.       return;
  1557.    if (nrim_in==0)  {                  /* fill areas with no rim points */
  1558.       is_lake = FALSE;
  1559.       for (k=0; k<num_lakes; ++k)      /* check for lake */
  1560.          if (narea==lakes[k])  {
  1561.             if (first_color==ORANGE)  {
  1562.                pen = BLUE;             /* make lake blue if drawn */
  1563.                is_lake = TRUE;         /*   into orange           */
  1564.                break;
  1565.             }
  1566.             else                       /* don't color-fill if drawn */
  1567.                return;                 /*   into blue               */
  1568.          }
  1569.       xs = CENTERX + area[0];
  1570.       ys = CENTERY + area[1];
  1571.       if (np==1)
  1572.          plotpoint (xs, ys);
  1573.       else if (np>=GLOBE_FILL_MIN)
  1574.          afill (np, pen);
  1575.       if (is_lake==TRUE)
  1576.          pen = ORANGE;
  1577.    }
  1578. }
  1579.  
  1580. /* ============================================================= */
  1581.  
  1582. void globe_rim_fix (h1, h2)            /* find nearest actual rim point */
  1583.  
  1584. short *h1, *h2;
  1585.  
  1586. {
  1587.  
  1588.    short inc1, inc2;
  1589.    int i;
  1590.    long x, y, color;
  1591.    static int itmax = 20;
  1592.  
  1593.    if ((*h1)>=0)                       /* right half */
  1594.       inc1 = +1;
  1595.    else                                /* left half */
  1596.       inc1 = -1;
  1597.    if ((*h2)>=0)                       /* bottom half */
  1598.       inc2 = +1;
  1599.    else                                /* top half */
  1600.       inc2 = -1;
  1601.    x = (*h1) + CENTERX + 5*inc1;       /* coords of test pixel */
  1602.    y = (*h2) + CENTERY + 5*inc2;
  1603.    for (i=0; i<itmax; ++i)  {          /* look for nearest black pixel */
  1604.       if ((color = ReadPixel (rp, x, y))==BLACK)
  1605.          break;
  1606.       x += inc1;
  1607.       y += inc2;
  1608.    }
  1609.    x -= inc1;
  1610.    y -= inc2;
  1611.    for (i=0; i<itmax; ++i)  {          /* look back for previous blue one */
  1612.       if ((color = ReadPixel (rp, x, y))==BLUE || color==ORANGE)
  1613.          break;
  1614.       x -= inc1;
  1615.       y -= inc2;
  1616.    }
  1617.    (*h1) = x - CENTERX;
  1618.    (*h2) = y - CENTERY;
  1619. }
  1620.  
  1621. /* ============================================================= */
  1622.  
  1623. void globe_tidy (td, td_trig, lat0, lam0, val)
  1624.  
  1625. short *td, *td_trig;                   /* color-fills partially-drawn */
  1626. double lat0, lam0;                     /*   regions                    */
  1627. long val;
  1628.  
  1629. {
  1630.  
  1631.    int i;
  1632.    double lat, lam, c0, s0, c1, s1, c2;
  1633.    double zp, hp, scale, fac2, fac3, h1d, h2d;
  1634.    short h1, h2;
  1635.    long x, y, color;
  1636.    static double fac = 1./32767.0;
  1637.  
  1638.    if (val==GLOBE)  {                  /* ordinary globe view */
  1639.       hp = 0.;
  1640.       scale = 1.;
  1641.       fac3 = 1.;
  1642.    }
  1643.    else  {                             /* orbital globe view */
  1644.       hp = etap;
  1645.       scale = sqrt (1.-etap*etap);
  1646.       fac3 = (facp/(facp-hp)) * scale;
  1647.    }
  1648.    lat0 *= rad;
  1649.    c0 = cos (lat0);
  1650.    s0 = sin (lat0);
  1651.    i = 0;
  1652.    while (1)  {
  1653.       if (td[i]==0 && td[i+1]==0)      /* check for end of array */
  1654.          break;
  1655.       lat = td[i];                     /* latitude */
  1656.       lat = (lat/100.) * rad;
  1657.       lam = td[i+1];                   /* longitude */
  1658.       lam = (lam/100.-lam0) * rad;
  1659.       if (lam<-pi)
  1660.          lam += twopi;
  1661.       if (lam>pi)
  1662.          lam -= twopi;
  1663.       c1 = td_trig[i];
  1664.       c1 *= fac;
  1665.       s1 = td_trig[i+1];
  1666.       s1 *= fac;
  1667.       c2 = cos (lam);
  1668.       zp = s1*s0 + c1*c0*c2;
  1669.       if (zp>=hp)  {                   /* in view, so get screen coords */
  1670.          h1d = HRADIUS * c1 * sin (lam);
  1671.          h2d = -VRADIUS * (s1*c0-c1*s0*c2);
  1672.          if (val!=GLOBE)  {            /* orbital view */
  1673.             fac2 = (facp/(facp-zp)) * scale;
  1674.             h1d *= fac2;
  1675.             h2d *= fac2;
  1676.          }
  1677.          h1 = h1d;                     /* flood fill */
  1678.          h2 = h2d;
  1679.          x = h1 + CENTERX;
  1680.          y = h2 + CENTERY;
  1681.          if ((color = ReadPixel (rp, x, y)) == BLUE)
  1682.             Flood (rp, 1, x, y);
  1683.       }
  1684.       i += 2;
  1685.    }
  1686. }
  1687.  
  1688.  /* ============================================================= */
  1689.  
  1690. void stars ()            /* draws stars into black background */
  1691. {
  1692.    static int nmax = 150;
  1693.    unsigned int t;
  1694.    short x, y;
  1695.    int nstars;
  1696.    long oldpen;
  1697.  
  1698.    oldpen = rp->FgPen;
  1699.    SetAPen (rp, WHITE);
  1700.    nstars = 0;
  1701.    do  {
  1702.       t = rand();       /* Random number: Range 0 to INT_MAX on Lattice, 0 to 32768 on Aztec */
  1703.       x = t % WWIDTH + 1;
  1704.       t = rand();
  1705.       y = t % WHEIGHT + 1;
  1706. /*      if (x<0)        * no need for this in a WINDOW *
  1707.          x = 0;
  1708.       if (x>WWIDTH-1)
  1709.          x = WWIDTH-1;
  1710.       if (y<0)
  1711.          y = 0;
  1712.       if (y>WHEIGHT-1)
  1713.          y = WHEIGHT-1; */
  1714.       if (ReadPixel (rp, x, y) == BLACK)  {
  1715.          WritePixel (rp, x, y);
  1716.          ++nstars;
  1717.       }
  1718.    } while (nstars<=nmax);
  1719.    SetAPen (rp, oldpen);
  1720. }
  1721.  
  1722. /* ============================================================= */
  1723.  
  1724. void floodfill (color)                      /* flood fills an area based */
  1725. short color;                                       /*   on mouse position       */
  1726. {
  1727.    struct IntuiMessage *msgf;
  1728.    short x, y, oldpen;
  1729.  
  1730.    while (1)  {
  1731.       WaitPort (w->UserPort);             /* wait for message */
  1732.       msgf = (struct IntuiMessage *) GetMsg (w->UserPort);
  1733.       if (msgf==NULL)
  1734.          continue;
  1735.       else if (msgf->Code==SELECTDOWN){
  1736.             x = msgf->MouseX;                   /*  get mouse coordinates  */
  1737.             y = msgf->MouseY;                   /*    and flood fill       */
  1738.             ReplyMsg( (struct Message *) msgf);
  1739.             oldpen=rp->FgPen;
  1740.             SetPointer (w, waiter, waiter_size/4-2, 16, waiter_x_offset, waiter_y_offset);
  1741.             SetAPen(rp,color);
  1742.             Flood (rp, 1, x, y);
  1743.             SetAPen(rp,oldpen);
  1744.             SetPointer (w, arrow, arrow_size/4-2, 16,arrow_x_offset, arrow_y_offset);
  1745.             break;
  1746.       }
  1747.       else if (msgf->Code==MENUDOWN){
  1748.             ReplyMsg((struct Message *) msgf);
  1749.             return;                         /* Return if RIGHT button pressed*/
  1750.       }
  1751.       else{
  1752.             ReplyMsg((struct Message *) msgf);  /* unknown message */
  1753.       }
  1754.    }
  1755. }
  1756.  
  1757. /* ============================================================= */
  1758.  
  1759. void shadow ()                         /* makes shadowed screens */
  1760.  
  1761. {
  1762.    int modb0, modb1, color0, color1;
  1763.    int bcolor0, bcolor1;
  1764.    int j, k, k2, m, bitlast;
  1765.    unsigned int byte0, byte1;
  1766.    unsigned int byte0b0, byte1b0, byte0b1, byte1b1;
  1767.    long color;
  1768.    static int blackcolor0, blackcolor1, disp;
  1769.    static int bluecolor0, bluecolor1, Ocolor0, Ocolor1;
  1770.    static unsigned int bitval[] = {1, 2, 4, 8, 16, 32, 64, 128};
  1771.    static short ytable[SHEIGHT];       /* offsets from beginning of */
  1772.                                  /*   each screen row  (WHY ?!?!?)  */
  1773.    static char first = NOT_OK;
  1774.  
  1775.    if (first==NOT_OK)  {               /* initialize color values once */
  1776.       first = OK;
  1777.       color = BLACK;
  1778.       blackcolor0 = color&1L;
  1779.       blackcolor1 = (color&2L)>>1;
  1780.       color = BLUE;
  1781.       bluecolor0 = color&1L;
  1782.       bluecolor1 = (color&2L)>>1;
  1783.       color = ORANGE;
  1784.       Ocolor0 = color&1L;
  1785.       Ocolor1 = (color&2L)>>1;
  1786.       disp = SHADOW_DISP * rowoffset;  /* offset to shadowed row */
  1787.       ytable[0] = 0;                   /* initialize screen offsets */
  1788.       /* rowoffset = WWIDTH/8;      * #defined above * */
  1789.       for (color=1; color<SHEIGHT; ++color)
  1790.         ytable[color] = ytable[color-1] + rowoffset;
  1791.    }
  1792.    for (k=ytable[WYOFFS]; k<ytable[WHEIGHT+WYOFFS-SHADOW_DISP-1]; k+=rowoffset)  {
  1793.                                        /* do each row                  */
  1794.       k2 = k + disp;                   /* displacement to shadowed row */
  1795.       for (j=0; j<rowoffset; ++j)  {   /* do each byte in row          */
  1796.          byte0 = bp[0][k+j];           /* current row, planes 0 and 1  */
  1797.          byte1 = bp[1][k+j];
  1798.          byte0b0 = bp[0][k2+j];        /* shadowed row, planes 0 and 1 */
  1799.          byte1b0 = bp[1][k2+j];
  1800.          byte0b1 = bp[0][k2+j+1];      /* shadowed row, adjacent byte, */
  1801.          byte1b1 = bp[1][k2+j+1];      /*   planes 0 and 1             */
  1802.                                        /* if last byte in row, don't   */
  1803.                                        /*   need adjacent bytes        */
  1804.          ((j<rowoffset-1) ? (bitlast=0) : (bitlast=SHADOW_DISP));
  1805.          modb0 = NOT_OK;
  1806.          modb1 = NOT_OK;
  1807.          for (m=7; m>=bitlast; --m)  { /* check each bit, left to right */
  1808.             color0 = BITVAL(byte0,m);  /* current pixel color           */
  1809.             color1 = BITVAL(byte1,m);
  1810.             if (color0==Ocolor0 && color1==Ocolor1)  {
  1811.                if (m>SHADOW_DISP-1)  {           /* get color of pixel in   */
  1812.                   bcolor0 = BITVAL(byte0b0,m-SHADOW_DISP);  /* shadowed row */
  1813.                   bcolor1 = BITVAL(byte1b0,m-SHADOW_DISP);
  1814.                }
  1815.                else  {                 /* use adjacent byte */
  1816.                   bcolor0 = BITVAL(byte0b1,m+8-SHADOW_DISP);
  1817.                   bcolor1 = BITVAL(byte1b1,m+8-SHADOW_DISP);
  1818.                }
  1819.                if (bcolor0==bluecolor0 && bcolor1==bluecolor1)  {
  1820.                   if (m>SHADOW_DISP-1)  {  /* if blue, set color to black */
  1821.                      BITSTORE(byte0b0,m-SHADOW_DISP,blackcolor0);
  1822.                      BITSTORE(byte1b0,m-SHADOW_DISP,blackcolor1);
  1823.                      modb0 = OK;
  1824.                   }
  1825.                   else  {              /* use adjacent byte */
  1826.                      BITSTORE(byte0b1,m+8-SHADOW_DISP,blackcolor0);
  1827.                      BITSTORE(byte1b1,m+8-SHADOW_DISP,blackcolor1);
  1828.                      modb1 = OK;
  1829.                   }
  1830.                }
  1831.             }
  1832.          }                             /* end bit test */
  1833.          if (modb0==OK)  {             /* restore only modified bytes */
  1834.             bp[0][k2+j] = byte0b0;
  1835.             bp[1][k2+j] = byte1b0;
  1836.          }
  1837.          if (modb1==OK)  {
  1838.             bp[0][k2+j+1] = byte0b1;
  1839.             bp[1][k2+j+1] = byte1b1;
  1840.          }
  1841.       }                                /* end of row */
  1842.    }                                   /* last row   */
  1843. }
  1844.  
  1845. /* ============================================================= */
  1846.  
  1847. int getbox (x0, y0, x, y)              /* selects a region to draw to */
  1848.                                        /*   larger scale              */
  1849. int *x0, *y0, *x, *y;
  1850.  
  1851. {
  1852.  
  1853.    extern void drawbox();
  1854.    struct IntuiMessage *msgf;
  1855.    int xd, yd, x1, x2, y1, y2;
  1856.    char selectbutton;
  1857.    selectbutton = FALSE;
  1858.    SetDrMd (rp, JAM2 | COMPLEMENT);    /* turn on complement mode and */
  1859.    ModifyIDCMP (w, MOUSEBUTTONS | MOUSEMOVE); /* enable mouse events    */
  1860.    while (1)  {
  1861.       WaitPort (w->UserPort);
  1862.       msgf = (struct IntuiMessage *) GetMsg (w->UserPort);
  1863.       if (msgf==NULL)
  1864.          continue;
  1865.       else if (msgf->Code==SELECTDOWN)  { /* if user pressed left button, */
  1866.          x1 = msgf->MouseX;               /*   get initial mouse position */
  1867.          y1 = msgf->MouseY;
  1868.          xd = x1;
  1869.          yd = y1;
  1870.          selectbutton = TRUE;
  1871.       }
  1872.       else if (selectbutton==TRUE && msgf->Class==MOUSEMOVE)  {
  1873.          x2 = msgf->MouseX;
  1874.          y2 = msgf->MouseY;
  1875.          drawbox (x1, y1, xd, yd);     /* erase old box */
  1876.          xd = x2;
  1877.          yd = y2;
  1878.          drawbox (x1, y1, xd, yd);     /* draw new box */
  1879.       }
  1880.       else if (selectbutton==TRUE && msgf->Code==SELECTUP)
  1881.          break;
  1882.  
  1883.       ReplyMsg((struct Message *)msgf);
  1884.    }
  1885.    x2 = msgf->MouseX;                  /* erase current box */
  1886.    y2 = msgf->MouseY;
  1887.    ReplyMsg((struct Message *)msgf);
  1888.    drawbox (x1, y1, xd, yd);
  1889.    SetDrMd (rp, JAM2);                 /* restore original drawing mode */
  1890.    ModifyIDCMP (w, IDCMPFLAGS);        /*   and disable mouse events    */
  1891.    *x0 = x1;
  1892.    *y0 = y1;
  1893.    *x = x2;
  1894.    *y = y2;
  1895.    if (x1==x2 || y1==y2)               /* error if box is of zero area */
  1896.       return (NOT_OK);
  1897.    if (x1>x2 && y1>y2)  {              /* ensure that the vertices of */
  1898.       *x0 = x2;                        /*   the box are in the proper */
  1899.       *y0 = y2;                        /*   order                     */
  1900.       *x = x1;
  1901.       *y = y1;
  1902.    }
  1903.    else if (x1<x2 && y1>y2)  {
  1904.       *x0 = x1;
  1905.       *y0 = y2;
  1906.       *x = x2;
  1907.       *y = y1;
  1908.    }
  1909.    else if (x1>x2 && y1<y2)  {
  1910.       *x0 = x2;
  1911.       *y0 = y1;
  1912.       *x = x1;
  1913.       *y = y2;
  1914.    }
  1915.    return (OK);
  1916. }
  1917.  
  1918. /* ============================================================= */
  1919.  
  1920. void drawbox (x1, y1, x2, y2)          /* draws a box */
  1921.  
  1922. int x1, y1, x2, y2;
  1923.  
  1924. {
  1925.  
  1926.    Move (rp, x1, y1);
  1927.    Draw (rp, x2, y1);
  1928.    Draw (rp, x2, y2);
  1929.    Draw (rp, x1, y2);
  1930.    Draw (rp, x1, y1);
  1931. }
  1932.  
  1933. /* ============================================================= */
  1934.  
  1935. void box (ws, latp, lamp, fill)        /* draws areas contained within */
  1936.                                        /*   a rectangular region       */
  1937. short *ws;
  1938. double *latp, *lamp;
  1939. int fill;
  1940.  
  1941. {
  1942.  
  1943.    extern void plotpoint(), drawbox(), box_tidy();
  1944.    char first, prev_in_view, in_view, is_lake;
  1945.    short h1, h1c, h1prev;              /* x-dist. (pix) from center */
  1946.    short h2, h2c, h2prev;              /* y-dist. (pix) from center */
  1947.    short np, xs, ys, nrim_in, nrim_out;
  1948.    int i, j, k, narea;
  1949.    long x, y, pen, first_color;
  1950.    double lam, lamc, lamprev;          /* longitude */
  1951.    double lat, latc, latprev;          /* latitude  */
  1952.    double bwidth, bheight, bcx, bcy, xscale, yscale;
  1953.    double lat1, lat2, lam1, lam2;
  1954.  
  1955.    lat1 = latp[0];                     /* store values for box corners */
  1956.    lat2 = latp[1];                     /*   locally                    */
  1957.    lam1 = lamp[0];
  1958.    lam2 = lamp[1];
  1959.    bwidth = lam2 - lam1;               /* box width (degrees)         */
  1960.    bheight = lat1 - lat2;              /* box height (degrees)        */
  1961.    bcx = (lam1 + lam2)/2.;             /* x-coord of box center (deg) */
  1962.    bcy = (lat1 + lat2)/2.;             /* y-coord of box center (deg) */
  1963.    xscale = WWIDTH / bwidth;           /* horizontal scale (pix/deg)  */
  1964.    yscale = (WHEIGHT) / bheight;    /* vertical scale (pix/deg)    */
  1965.    j = 0;
  1966.    np= 0;
  1967.    narea = 0;
  1968.    nrim_in=0;
  1969.    first = TRUE;
  1970.    pen = ORANGE;
  1971.    SetAPen (rp, pen);
  1972.    first_color = BLACK;
  1973.  
  1974.    drawbox (0, 0, WWIDTH-1, WHEIGHT); /* outline the box */
  1975.  
  1976.    for (i=0; i<MAXVAL; i+=2)  {
  1977.       if (ws[i]==0 && ws[i+1]==0)  {   /* if end of area, then */
  1978.          ++narea;
  1979.          if (np>0 && fill==FILL)  {    /* color-fill if requested */
  1980.             if (nrim_in==0)  {
  1981.                is_lake = FALSE;
  1982.                for (k=0; k<num_lakes; ++k)
  1983.                   if (narea==lakes[k])  {
  1984.                      is_lake = TRUE;
  1985.                      if (first_color==ORANGE)
  1986.                         pen = BLUE;
  1987.                      break;
  1988.                   }
  1989.                if (is_lake==FALSE || (is_lake==TRUE && pen==BLUE))  {
  1990.                   xs = CENTERX + area[0];
  1991.                   ys = CENTERY + area[1];
  1992.                   if (np==1)
  1993.                      plotpoint (xs, ys);
  1994.                   else
  1995.                      afill (np, pen);
  1996.                }
  1997.             }
  1998.          }
  1999.          j = 0;
  2000.          np = 0;
  2001.          nrim_in = 0;
  2002.          nrim_out = 0;
  2003.          pen = ORANGE;
  2004.          first_color = BLACK;
  2005.          first = TRUE;
  2006.          continue;                     /* skip to next point */
  2007.       }
  2008.       lat = ws[i];                     /* latitude */
  2009.       lat = lat/100.;
  2010.       lam = ws[i+1];                   /* longitude */
  2011.       lam = lam/100.;
  2012.       in_view = FALSE;                 /* get status of current point */
  2013.       if ( (lat<=lat1 && lat>=lat2)
  2014.         && (lam>=lam1 && lam<=lam2))  {
  2015.          in_view = TRUE;
  2016.          h1 = (lam-bcx) * xscale;
  2017.          h2 = - (lat-bcy) * yscale;
  2018.       }
  2019.       if (first==TRUE)  {              /* check first point */
  2020.          first = FALSE;
  2021.          if (in_view==TRUE)  {         /* if first point is in view, */
  2022.             x = h1 + CENTERX;          /*   move pen to first point  */
  2023.             y = h2 + CENTERY;          /*   and plot it              */
  2024.             Move (rp, x, y);
  2025.             first_color = ReadPixel (rp, x, y);
  2026.             xs = x;
  2027.             ys = y;
  2028.             plotpoint (xs, ys);
  2029.             h1prev = h1;
  2030.             h2prev = h2;
  2031.             area[0] = h1;
  2032.             area[1] = h2;
  2033.             j = 2;
  2034.             np = 1;
  2035.          }
  2036.          prev_in_view = in_view;       /* save status of first point */
  2037.          latprev = lat;
  2038.          lamprev = lam;
  2039.          continue;
  2040.       }
  2041.       if (in_view==TRUE)  {
  2042.          if (prev_in_view==FALSE)  {   /* if prev. point was not in view, */
  2043.             if (lamprev<=lam1)  {      /*   find rim point                */
  2044.                lamc = lam1;
  2045.                if (latprev<=lat2)      /* lower left */
  2046.                   latc = lat2;
  2047.                else if (latprev>=lat1) /* upper left */
  2048.                   latc = lat1;
  2049.                else                    /* left center */
  2050.                   latc = lat + (latprev-lat)*(lamc-lam)/(lamprev-lam);
  2051.             }
  2052.             else if (lamprev>=lam2)  {
  2053.                lamc = lam2;
  2054.                if (latprev>=lat1)      /* upper right */
  2055.                   latc = lat1;
  2056.                else if (latprev<=lat2) /* lower right */
  2057.                   latc = lat2;
  2058.                else                    /* right center */
  2059.                   latc = lat + (latprev-lat)*(lamc-lam)/(lamprev-lam);
  2060.             }
  2061.             else  {
  2062.                if (latprev>=lat1)      /* top center */
  2063.                   latc = lat1;
  2064.                else                    /* bottom center */
  2065.                   latc = lat2;
  2066.                lamc = lam + (lamprev-lam)*(latc-lat)/(latprev-lat);
  2067.             }
  2068.             h1c = (lamc-bcx) * xscale;
  2069.             h2c = - (latc-bcy) * yscale;
  2070.             x = h1c + CENTERX;         /* move to rim point & plot it */
  2071.             y = h2c + CENTERY;
  2072.             Move (rp, x, y);
  2073.             xs = x;
  2074.             ys = y;
  2075.             plotpoint (xs, ys);
  2076.             h1prev = h1c;
  2077.             h2prev = h2c;
  2078.             area[j] = h1;
  2079.             area[j+1] = h2;
  2080.             j += 2;
  2081.             ++nrim_in;
  2082.             ++np;
  2083.          }
  2084.          if (h1!=h1prev || h2!=h2prev)  {
  2085.             x = h1 + CENTERX;          /* draw to current point */
  2086.             y = h2 + CENTERY;
  2087.             Draw (rp, x, y);
  2088.             area[j] = h1;
  2089.             area[j+1] = h2;
  2090.             j += 2;
  2091.             ++np;
  2092.          }
  2093.          h1prev = h1;
  2094.          h2prev = h2;
  2095.       }
  2096.       else  {                          /* else out of view */
  2097.          if (prev_in_view==TRUE)  {    /* if previous point was in view, */
  2098.             if (lam<=lam1)  {          /*   find rim point                */
  2099.                lamc = lam1;
  2100.                if (lat<=lat2)          /* lower left */
  2101.                   latc = lat2;
  2102.                else if (lat>=lat1)     /* upper left */
  2103.                   latc = lat1;
  2104.                else                    /* left center */
  2105.                   latc = lat + (latprev-lat)*(lamc-lam)/(lamprev-lam);
  2106.             }
  2107.             else if (lam>=lam2)  {
  2108.                lamc = lam2;
  2109.                if (lat>=lat1)          /* upper right */
  2110.                   latc = lat1;
  2111.                else if (lat<=lat2)     /* lower right */
  2112.                   latc = lat2;
  2113.                else                    /* right center */
  2114.                   latc = lat + (latprev-lat)*(lamc-lam)/(lamprev-lam);
  2115.             }
  2116.             else  {
  2117.                if (lat>=lat1)          /* top center */
  2118.                   latc = lat1;
  2119.                else                    /* bottom center */
  2120.                   latc = lat2;
  2121.                lamc = lam + (lamprev-lam)*(latc-lat)/(latprev-lat);
  2122.             }
  2123.             h1c = (lamc-bcx) * xscale;
  2124.             h2c = - (latc-bcy) * yscale;
  2125.             x = h1c + CENTERX;         /* draw to rim point */
  2126.             y = h2c + CENTERY;
  2127.             Draw (rp, x, y);
  2128.             area[j] = h1;
  2129.             area[j+1] = h2;
  2130.             j += 2;
  2131.             ++nrim_out;
  2132.             ++np;
  2133.          }
  2134.       }
  2135.       prev_in_view = in_view;          /* save status of current point */
  2136.       latprev = lat;
  2137.       lamprev = lam;
  2138.    }
  2139.    if (fill==FILL)
  2140.       box_tidy (lat1, lam1, lat2, lam2, /* fill partial areas */
  2141.                 bcx, bcy, xscale, yscale);
  2142. }
  2143.  
  2144. /* ============================================================= */
  2145.  
  2146. void box_tidy (lat1, lam1, lat2, lam2, bcx, bcy, xscale, yscale)
  2147.                                        /* color-fills partially-drawn */
  2148.                                        /*   areas in box-view         */
  2149. double lat1, lam1, lat2, lam2, bcx, bcy, xscale, yscale;
  2150.  
  2151. {
  2152.    double lat, lam;
  2153.    int i;
  2154.    short h1, h2;
  2155.    long x, y, color;
  2156.  
  2157.    i = 0;
  2158.    while (1)  {                        /* check each special point */
  2159.       if (td[i]==0 && td[i+1]==0)      /* if end of special points, */
  2160.          break;                        /*   then all done           */
  2161.       lat = td[i];
  2162.       lat /= 100.;
  2163.       lam = td[i+1];
  2164.       lam /= 100.;
  2165.       if ((lat<=lat1 && lat>=lat2) &&  /* if point is in view,  */
  2166.           (lam>=lam1 && lam<=lam2))  { /*   then get its screen */
  2167.          h1 = (lam-bcx) * xscale;      /*   coordinates         */
  2168.          h2 = -(lat-bcy) * yscale;
  2169.          x = h1 + CENTERX;             /* color-fill if current color */
  2170.          y = h2 + CENTERY;             /*   is blue                   */
  2171.          if ((color = ReadPixel (rp, x, y)) == BLUE)
  2172.             Flood (rp, 1, x, y);
  2173.       }
  2174.       i += 2;
  2175.    }
  2176. }
  2177.  
  2178. /* ============================================================= */
  2179.  
  2180. void do_text (msgin, color)              /* get user text input */
  2181. struct IntuiMessage *msgin;
  2182. short color;
  2183. {
  2184.    extern void drawbox();
  2185.    struct IntuiMessage *msg, *msg1;
  2186.    struct Gadget *g;
  2187.    static char title_setdown[] = "   Press LEFT button to position text | RIGHT button to CANCEL";
  2188.    int x, y, xold, yold, pixlength;
  2189.    long oldpen;
  2190.    BOOL abort=FALSE;
  2191.  
  2192.    x = msgin->MouseX - GAD_LEFT-8;     /* position the requester */
  2193.    y = msgin->MouseY - GAD_TOP-12;
  2194.    if ((x+TWIDTH) >= WWIDTH)
  2195.       x = WWIDTH-TWIDTH-15;
  2196.    if (x < 10)
  2197.       x = 10;
  2198.    if ((y+THEIGHT) >= WHEIGHT)
  2199.       y = WHEIGHT-THEIGHT-15;
  2200.    if (y<5)
  2201.       y = 5;
  2202.    req.LeftEdge = x;
  2203.    req.TopEdge = y;
  2204.    oldpen = rp->FgPen;
  2205.    SetAPen(rp,WHITE);
  2206.    Request (&req, w);                  /* issue the request */
  2207.    ModifyIDCMP (w, GADGETUP);          /* disable all but gadgetup event */
  2208.    ActivateGadget (&gad, w, &req);
  2209.    do  {
  2210.       WaitPort (w->UserPort);             /* wait for gadgetup event */
  2211.       msg = (struct IntuiMessage *) GetMsg (w->UserPort);
  2212.       if (msg == NULL)
  2213.          continue;
  2214.       else if (msg->Class == GADGETUP)  { /* check for correct gadget */
  2215.          g = (struct Gadget *) (msg->IAddress);
  2216.          if (g->GadgetID != GAD_FIRST)
  2217.             continue;
  2218.          pixlength = TextLength (rp, userinput, strlen(userinput));
  2219.          SetWindowTitles (w, (char *) -1,title_setdown);
  2220.          SetDrMd (rp, JAM2 | COMPLEMENT);
  2221.          SetPointer (w, transparent, transparent_size/4-2, 16,
  2222.                      transparent_x_offset, transparent_y_offset);
  2223.          xold = msg->MouseX;           /* current mouse position */
  2224.          yold = msg->MouseY;
  2225.          drawbox (xold, yold, xold+pixlength, yold-8);
  2226.          ModifyIDCMP (w, MOUSEBUTTONS | MOUSEMOVE);
  2227.          do  {
  2228.             WaitPort (w->UserPort);       /* wait for mouse button  */
  2229.             msg1 = (struct IntuiMessage *) GetMsg (w->UserPort);
  2230.             if (msg1 == NULL)
  2231.                continue;
  2232.             else  {
  2233.                x = msg1->MouseX;       /* get current mouse position */
  2234.                y = msg1->MouseY;       /*   and erase old box        */
  2235.                if (msg1->Class == MOUSEBUTTONS )  {
  2236.                   drawbox (xold, yold, xold+pixlength, yold-8);
  2237.                                     /* done if select button pressed */
  2238.                   if (msg1->Code == SELECTDOWN)  {
  2239.                      SetDrMd (rp, JAM1);
  2240.                      SetAPen (rp, color);          /* draw text in active color */
  2241.                      Move (rp, x, y-1);              /* move to current mouse position */
  2242.                      Text (rp, userinput, strlen(userinput));
  2243.                      abort=TRUE;
  2244.                   }
  2245.                   else if (msg1->Code == MENUDOWN)  {
  2246.                      SetDrMd (rp, JAM1);
  2247.                      abort=TRUE;         /* right button cancels */
  2248.                   }
  2249.                }
  2250.                else if (msg1->Class == MOUSEMOVE)  {  /* else draw box at current position */
  2251.                   drawbox (xold, yold, xold+pixlength, yold-8);
  2252.                   drawbox (x, y, x+pixlength, y-8);
  2253.                   xold = x;
  2254.                   yold = y;
  2255.                }
  2256.             }
  2257.             ReplyMsg ( (struct Message *) msg1);
  2258.          } while(abort==FALSE);
  2259.          break;
  2260.       }
  2261.    } while (TRUE);
  2262.    SetAPen (rp, oldpen);
  2263.    SetPointer (w, arrow, arrow_size/4-2, 16,
  2264.                   arrow_x_offset, arrow_y_offset);
  2265.  
  2266.    ReplyMsg ( (struct Message *) msg);
  2267. }
  2268.  
  2269. /* ================================================================ */
  2270.  
  2271. void afill (pairs, pen)                /* draws and fills a region  */
  2272.                                        /*   using AreaDraw function */
  2273. short pairs;                            /* how many pairs of words */
  2274. long pen;                              /* pen number to use       */
  2275.  
  2276. {
  2277.  
  2278.    short i, j;
  2279.    long x, y, oldpen;
  2280.  
  2281.    oldpen = rp->FgPen;
  2282.    SetAPen (rp, pen);
  2283.    x = area[0] + CENTERX;
  2284.    y = area[1] + CENTERY;
  2285.  /*  if (x<0)     * unneeded in a WINDOW *
  2286.       x = 0;
  2287.    if (x>WWIDTH-1)
  2288.       x = WWIDTH-1;
  2289.    if (y<0)
  2290.       y = 0;
  2291.    if (y>WHEIGHT-1)
  2292.       y = WHEIGHT-1; */
  2293.    AreaMove (rp, x, y);
  2294.    j = 2;
  2295.    for (i=1; i<pairs; i++)  {
  2296.       if (area[j]==0 && area[j+1]==0)
  2297.          break;
  2298.       x = area[j] + CENTERX;
  2299.       y = area[j+1] + CENTERY;
  2300. /*      if (x<0)
  2301.          x = 0;
  2302.       if (x>WWIDTH-1)
  2303.          x = WWIDTH-1;
  2304.       if (y<0)
  2305.          y = 0;
  2306.       if (y>WHEIGHT-1)
  2307.          y = WHEIGHT-1; */
  2308.       AreaDraw (rp, x, y);
  2309.       j += 2;
  2310.    }
  2311.    AreaEnd (rp);
  2312.    SetAPen (rp, oldpen);
  2313. }
  2314.  
  2315. /* ============================================================= */
  2316.  
  2317. void adraw (pairs)                     /* draws area outlines */
  2318.  
  2319. short pairs;
  2320.  
  2321. {
  2322.  
  2323.    short i, j;
  2324.    long x, y, oldpen;
  2325.  
  2326.    oldpen = rp->FgPen;
  2327.    SetAPen (rp, ORANGE);
  2328.    x = area[0] + CENTERX;
  2329.    y = area[1] + CENTERY;
  2330.  /*  if (x<0)
  2331.       x = 0;
  2332.    if (x>WWIDTH-1)
  2333.       x = WWIDTH-1;
  2334.    if (y<0)
  2335.       y = 0;
  2336.    if (y>WHEIGHT-1)
  2337.       y = WHEIGHT-1; */
  2338.    Move (rp, x, y);
  2339.    j = 2;
  2340.    for (i=1; i<pairs; ++i)  {
  2341.       if (area[j]==0 && area[j+1]==0)
  2342.          break;
  2343.       x = area[j] + CENTERX;
  2344.       y = area[j+1] + CENTERY;
  2345.     /*  if (x<0)          * Unnecessary in a normal WINDOW *
  2346.          x = 0;
  2347.       if (x>WWIDTH-1)
  2348.          x = WWIDTH-1;
  2349.       if (y<0)
  2350.          y = 0;
  2351.       if (y>WHEIGHT-1)
  2352.          y = WHEIGHT-1; */
  2353.       Draw (rp, x, y);
  2354.       j += 2;
  2355.    }
  2356.    SetAPen (rp, oldpen);
  2357. }
  2358.  
  2359. /* ============================================================= */
  2360.  
  2361. void plotpoint (x, y)                  /* plots a single point */
  2362.  
  2363. short x, y;
  2364.  
  2365. {
  2366.  
  2367. /*   if (x<0)     * Unnecessary because we use a WINDOW *
  2368.       x = 0;
  2369.    if (x>WWIDTH-1)
  2370.       x = WWIDTH-1;
  2371.    if (y<0)
  2372.       y = 0;
  2373.    if (y>WHEIGHT-1)
  2374.       y = WHEIGHT-1; */
  2375.    WritePixel (rp, x, y);
  2376. }
  2377. /* ============================================================= */
  2378.  
  2379. void drawline(color)                /* draws a line on mouse positions */
  2380. short color;
  2381. {
  2382.     struct IntuiMessage *msg;
  2383.     short y,x, oldpen;
  2384.     ULONG code;
  2385.  
  2386.     msg=NULL;
  2387.     oldpen = rp->FgPen;
  2388.     ModifyIDCMP (w, MOUSEBUTTONS);
  2389.     do{
  2390.         WaitPort (w->UserPort);       /* wait for mouse button  */
  2391.         msg = (struct IntuiMessage *) GetMsg (w->UserPort);
  2392.         if (msg==NULL){
  2393.             continue;
  2394.         }
  2395.         code=msg->Code;        /* must be a MOUSEBUTTONS message */
  2396.         x = msg->MouseX;       /* get current mouse position */
  2397.         y = msg->MouseY;
  2398.         ReplyMsg((struct Message *) msg);
  2399.         if (code == SELECTDOWN){
  2400.             SetAPen(rp, color);
  2401.             WritePixel(rp,x,y);
  2402.             Move(rp,x,y);
  2403.             ModifyIDCMP(w, MOUSEBUTTONS | MOUSEMOVE );
  2404.             do {
  2405.                 WaitPort(w->UserPort);
  2406.                 msg = ((struct IntuiMessage *) GetMsg (w->UserPort));
  2407.                 if (msg == NULL){
  2408.                     continue;
  2409.                 }
  2410.  
  2411.                 if (msg->Class == MOUSEMOVE){
  2412.                     x = msg->MouseX;       /* get current mouse position */
  2413.                     y = msg->MouseY;
  2414.                     Draw(rp,x,y);
  2415.                     code=NULL;
  2416.                 }
  2417.                 else{
  2418.                     code=msg->Code;        /* This must be a MOUSEBUTTONs message*/
  2419.                 }
  2420.                 ReplyMsg((struct Message *) msg);
  2421.             } while (code != SELECTUP);
  2422.             continue;
  2423.         }
  2424.     } while (code != MENUDOWN);
  2425.     SetAPen (rp, oldpen);
  2426.     ModifyIDCMP (w, IDCMPFLAGS);  /* enable original event types */
  2427. }
  2428. /* ============================================================= */
  2429.  
  2430. VOID setwin(w, traprmb,idcmpflags,title)  /* sets window title, window */
  2431.                                               /*  and IDCMP flags */
  2432. struct Window* w;
  2433. ULONG traprmb;
  2434. ULONG idcmpflags;
  2435. UBYTE *title;
  2436. {
  2437.     Forbid();
  2438.     if (traprmb==RMBTRAP){
  2439.         w->Flags|=RMBTRAP;
  2440.     }
  2441.     else{
  2442.         w->Flags&=~RMBTRAP;
  2443.     }
  2444.     Permit();
  2445.     ModifyIDCMP (w, idcmpflags );
  2446.     SetWindowTitles (w, (char *) -1, title);
  2447.     SetWindowTitles (wt, (char *)-1, title);
  2448. }
  2449.  
  2450.  
  2451. /* ============================================================= */
  2452.  
  2453. LONG dump(win)                      /* dumps displayed map to printer */
  2454. struct Window *win;
  2455. {
  2456.    struct IODRPReq    *ioreq;
  2457.    struct MsgPort  *printerPort;
  2458.    ULONG signal,winsig, printsig;
  2459.    BYTE abort = NULL;
  2460.     #define NOABORT 1<<1
  2461.     #define U_ABORT 1<<2
  2462.     #define P_ABORT 1<<3
  2463.  
  2464.    LONG returncode= NOT_OK;
  2465.    struct IntuiMessage *msg;
  2466.    UWORD dpY;
  2467.    if(printerPort = CreatePort("drmapdump",0)) {
  2468.       if(ioreq=(struct IODRPReq *) CreateExtIO(printerPort,sizeof(struct IODRPReq))) {
  2469.          if(!(OpenDevice("printer.device",0,(struct IORequest *)ioreq,0))) {
  2470.             ioreq->io_Command = CMD_FLUSH;
  2471.             DoIO((struct IORequest *) ioreq);
  2472.             if (ioreq->io_Error == PDERR_NOERR){
  2473.                 winsig= 1 << win->UserPort->mp_SigBit;
  2474.                 printsig= 1 << printerPort->mp_SigBit;
  2475.                 ioreq->io_Command = PRD_DUMPRPORT;
  2476.                 ioreq->io_RastPort = rp;
  2477.                 ioreq->io_ColorMap = vp->ColorMap;
  2478.                 ioreq->io_Modes = HIRES|LACE;         /* vp->Modes;*/
  2479.             /*  ioreq->io_SrcX = 0; CLEARED by MEMF_CLEAR */
  2480.             /*  ioreq->io_SrcY = 0;                       */
  2481.                 ioreq->io_SrcWidth = WWIDTH;
  2482.                 ioreq->io_SrcHeight = WHEIGHT;
  2483.             /*  ioreq->io_DestCols = 0;                   */
  2484.             /*  ioreq->io_DestRows = 0;                   */
  2485.                 ioreq->io_Special = SPECIAL_ASPECT|SPECIAL_FULLROWS;
  2486.  
  2487.                 Forbid();
  2488.                 dpY=GfxBase->NormalDPMY;
  2489.                 GfxBase->NormalDPMY=1267;
  2490.                     /* Dots per Meter on Display for WHEIGHT=390 */
  2491.                 Permit();
  2492.  
  2493.  
  2494.                 SendIO((struct IORequest *)ioreq);
  2495.                 while (abort==NULL){
  2496.                     signal=Wait(printsig | winsig);
  2497.                     if (signal & winsig){
  2498.                         while (msg=(struct IntuiMessage *)GetMsg(win->UserPort)){
  2499.                             if (msg->Class==CLOSEWINDOW){
  2500.                                 setwin (w, RMBTRAP, CLOSEWINDOW, "   Printing canceled. Please wait ...");
  2501.                                 abort|=U_ABORT;
  2502.                             }
  2503.                             ReplyMsg((struct Message *) msg);
  2504.                         }
  2505.                     }
  2506.                     if (signal & printsig){
  2507.                         if (ioreq->io_Error != PDERR_NOERR){
  2508.                             setwin (w, RMBTRAP, CLOSEWINDOW, "   PRINTER ERROR. Screendump aborted");
  2509.                             abort|=P_ABORT;
  2510.                         }
  2511.                         else{
  2512.                             abort|=NOABORT;  /* ahem, not aborted, but ended OK */
  2513.                         }
  2514.                     }
  2515.                 }
  2516.                 if (abort == U_ABORT){
  2517.                 /* WAIT A MOMENT, crashes if printer.device is being loaded */
  2518.                 /* and printing is canceled */
  2519.                     DisplayBeep(0);
  2520.                     Delay (8 * TICKS_PER_SECOND);
  2521.                     AbortIO((struct IORequest *)ioreq);
  2522.                     WaitIO((struct IORequest *)ioreq);
  2523.                 }
  2524.                 else if (abort & NOABORT){
  2525.                     returncode=OK;
  2526.                 }
  2527.                 while ((struct MsgPort *)GetMsg(printerPort))
  2528.                             ;
  2529.  
  2530.                 Forbid();
  2531.                 GfxBase->NormalDPMY=dpY;
  2532.                 Permit();
  2533.             }
  2534.  
  2535.             CloseDevice((struct IORequest *)ioreq);
  2536.          }
  2537.          DeleteExtIO((struct IORequest *)ioreq);
  2538.       }
  2539.       DeletePort(printerPort);
  2540.    }
  2541.    return(returncode);
  2542. }
  2543.  
  2544. /* ============================================================= */
  2545.  
  2546. VOID CloseCon(conioreq)         /* support routines for displayhelp() */
  2547. struct IOStdReq *conioreq;
  2548. {
  2549.     struct MsgPort  *conPort;
  2550.  
  2551.     conPort=conioreq->io_Message.mn_ReplyPort;
  2552.     while ((struct MsgPort *)GetMsg(conPort))
  2553.                             ;
  2554.     CloseDevice(conioreq);
  2555.     DeleteExtIO(conioreq);
  2556.     DeletePort(conPort);
  2557. }
  2558.  
  2559. struct IOStdReq *OpenCon(win)
  2560. struct Window *win;
  2561. {
  2562.     struct IOStdReq *conioreq=NULL;
  2563.     struct MsgPort  *conPort=NULL;
  2564.     if(conPort = CreatePort("drmpcon",0)) {
  2565.         if(conioreq=(struct IOReq *)CreateExtIO(conPort, sizeof(struct IOStdReq))) {
  2566.             conioreq->io_Data = (APTR) win;
  2567.             conioreq->io_Length = sizeof (struct Window);
  2568.             if(!(OpenDevice("console.device",0,(struct IOStdRequest *)conioreq,0))) {
  2569.                 return(conioreq);
  2570.             }
  2571.             DeleteExtIO(conioreq);
  2572.         }
  2573.         DeletePort(conPort);
  2574.     }
  2575.     return(NULL);
  2576. }
  2577.  
  2578. VOID WriteCon(conioreq,data,length) /* length=-1 -> write till \0 */
  2579. struct IOStdReq *conioreq;
  2580. UBYTE *data;
  2581. LONG length;
  2582. {
  2583.     conioreq->io_Command = CMD_WRITE;
  2584.     conioreq->io_Data = (APTR) data;
  2585.     conioreq->io_Length = length;
  2586.     DoIO(conioreq);
  2587. }
  2588.  
  2589. /* ============================================================= */
  2590.  
  2591. VOID displayhelp()      /* help routine: displays file DRAWMAP.HELP (ANSI)*/
  2592. {
  2593.    struct IntuiMessage *msg;
  2594.    LONG errabort=TRUE;
  2595.    struct IOStdReq *conioreq;
  2596.    struct Window *helpw;
  2597.    UBYTE helpbuffer[HELPBUF];
  2598.    LONG helplenght;
  2599.  
  2600.    BPTR helpfile;
  2601.  
  2602.    extern struct Screen *s;
  2603.    extern struct NewWindow newhelpw;
  2604.    extern struct Window *w;
  2605.    extern void setwin();
  2606.  
  2607.    newhelpw.Screen=s;
  2608.    if ( helpw=OpenWindow(&newhelpw) ){
  2609.         DrawBorder(helpw->RPort, &border, 0,0 );
  2610.         if(NULL!=(conioreq=OpenCon(helpw))){
  2611.             WriteCon( conioreq,"\x9b\x30\x20\x70", -1);
  2612.                                /* cursor off   */
  2613.             helpfile=Open("Drawmap.help",MODE_OLDFILE);
  2614.             if (helpfile!=NULL){
  2615.                 do{
  2616.                     if ((helplenght=Read(helpfile,helpbuffer,HELPBUF))>0)
  2617.                         WriteCon(conioreq,helpbuffer, helplenght);
  2618.                 } while (HELPBUF == helplenght);
  2619.                 Close(helpfile);
  2620.  
  2621.                 do{
  2622.                     WaitPort(helpw->UserPort);
  2623.                     msg=(struct IntuiMessage *)GetMsg(helpw->UserPort);
  2624.                     if (msg==NULL)      continue;
  2625.                     if (msg->Class==MOUSEBUTTONS)
  2626.                         errabort=FALSE;
  2627.                     ReplyMsg((struct Message *) msg);
  2628.                 } while (errabort==TRUE);
  2629.             }
  2630.             CloseCon(conioreq);
  2631.         }
  2632.         CloseWindow (helpw);
  2633.    }
  2634.    if (errabort==TRUE){
  2635.     SetWindowTitles (w, (char *) -1, "   Can't display help information !");
  2636.    }
  2637.  
  2638. }
  2639.  
  2640. /* ============================================================= */
  2641.  
  2642.  
  2643.  
  2644. LONG save(filename)         /* saves displayed map as IFF FORM */
  2645. UBYTE    *filename;
  2646. {
  2647.    extern IFFP PutAnILBM();
  2648.    ULONG IconBase;
  2649.    LONG            file;
  2650.    struct BitMap    *picBitMap;
  2651.    ULONG            picViewModes;
  2652.    WORD             *picColorTable;
  2653.    extern struct Screen *s;
  2654.    IFFP            iffp = NO_FILE;
  2655.    LONG returncode=NOT_OK;
  2656.  
  2657.    if (!(file = Open(filename, MODE_NEWFILE)))
  2658.       return (returncode);
  2659.  
  2660.    Write(file,"x",1);  /* 1.1 so Seek to beginning works ? (?!?!???)*/
  2661.  
  2662.    picBitMap = (struct BitMap*)vp->RasInfo->BitMap;
  2663.    picColorTable = (WORD *)vp->ColorMap->ColorTable;
  2664.    picViewModes = (ULONG)vp->Modes;
  2665.  
  2666.    iffp=PutAnILBM(file, picBitMap,
  2667.                 picColorTable, picBitMap->Depth, picViewModes);
  2668.  
  2669.    Close(file);
  2670.  
  2671.    if (iffp == IFF_OKAY) {
  2672.       IconBase = (ULONG)OpenLibrary("icon.library",0);
  2673.       if (IconBase!=NULL) {
  2674.         if(NULL!=PutDiskObject(filename,&ILBMobject)) {
  2675.             returncode=OK;
  2676.         }
  2677.         else{
  2678.             DisplayBeep(s);
  2679.         }
  2680.       CloseLibrary((struct Library *) IconBase);
  2681.       }
  2682.    }
  2683.    return (returncode);
  2684. }
  2685.  
  2686.  
  2687. #define CkErr(expression)  {if (ifferr == IFF_OKAY) ifferr = (expression);}
  2688.  
  2689. IFFP PutAnILBM(file, bitmap, colorMap, depth,
  2690.                                 viewmodes)
  2691.       LONG file;
  2692.       struct BitMap *bitmap;
  2693.       WORD *colorMap; UBYTE depth;
  2694.       ULONG viewmodes;
  2695. {
  2696.    BYTE buffer[bufsize];
  2697.    BitMapHeader bmHdr;
  2698.    CamgChunk    camgChunk;
  2699.    GroupContext fileContext, formContext;
  2700.    IFFP ifferr;
  2701.  
  2702.    ifferr = InitBMHdr(&bmHdr, bitmap, mskNone,
  2703.                       cmpByteRun1, 0, WWIDTH, SHEIGHT);
  2704.    bmHdr.y = WYOFFS;
  2705.    bmHdr.h = WHEIGHT;
  2706.  
  2707.    camgChunk.ViewModes = viewmodes & CAMGMASK;
  2708.  
  2709.    CkErr( OpenWIFF(file, &fileContext, szNotYetKnown) );
  2710.    CkErr( StartWGroup(&fileContext, FORM, szNotYetKnown, ID_ILBM, &formContext));
  2711.  
  2712.    CkErr( PutBMHD(&formContext, &bmHdr) );
  2713.    CkErr( PutCAMG(&formContext, &camgChunk) );
  2714.    CkErr( PutCMAP(&formContext, colorMap, depth) );
  2715.    CkErr( PutBODY(&formContext, bitmap, mskNone, &bmHdr, buffer, bufsize) );
  2716.  
  2717.    CkErr( EndWGroup(&formContext) );
  2718.    CkErr( CloseWGroup(&fileContext) );
  2719.    return( ifferr );
  2720. }
  2721.  
  2722. /* ============================================================= */
  2723.  
  2724. #include "libraries/arp.h"
  2725.  
  2726. /* MUST be careful including ARP stuff if ARP is not used always */
  2727. /* (else the ARP functions for Close, Open would be used !!!) */
  2728.  
  2729.  
  2730. #ifdef NO_ASL
  2731. #include "libraries/arpbase.h"
  2732. /* -> do not include if using ASL, too */
  2733. /* -> compiler fails because of duplicate STRUCT FILEREQUEST definition */
  2734. #endif
  2735. /*  NOTE :   */
  2736. /*  Rename all rf_xxx to fr_xxx to use filerequester with ARP includes */
  2737.  
  2738. #ifndef NO_ASL
  2739. struct AslBase *AslBase;
  2740. #endif
  2741. struct ArpBase *ArpBase=NULL;
  2742. struct FileRequester *filereq=NULL;
  2743.  
  2744. VOID FreeArpRequest(freq)
  2745. struct FileRequester *freq;
  2746. {
  2747.     if (freq!=NULL){
  2748. #ifndef NO_ASL
  2749.         if (freq->rf_File!=NULL)    FreeMem (freq->rf_File, MAXLENGHT+1);
  2750.         if (freq->rf_Dir!=NULL)     FreeMem (freq->rf_Dir, MAXLENGHT+1);
  2751. #else
  2752.         if (freq->fr_File!=NULL)    FreeMem (freq->fr_File, MAXLENGHT+1);
  2753.         if (freq->fr_Dir!=NULL)     FreeMem (freq->fr_Dir, MAXLENGHT+1);
  2754. #endif
  2755.         FreeMem (freq, sizeof(struct FileRequester));
  2756.     }
  2757.     if (ArpBase!=NULL)    CloseLibrary(ArpBase);
  2758.     ArpBase=NULL;
  2759. }
  2760.  
  2761. /* ============================================================= */
  2762.  
  2763. struct FileRequester *AllocArpRequest(fname)
  2764. UBYTE *fname;
  2765. {
  2766.     extern struct ArpBase *ArpBase;
  2767.     extern struct Window *w;
  2768.  
  2769.     struct FileRequester *freq;
  2770.     UBYTE *filemem, *dirmem;
  2771.  
  2772.     if (NULL==(freq=AllocMem(sizeof(struct FileRequester), MEMF_CLEAR | MEMF_PUBLIC))){
  2773.         FreeArpRequest(NULL);
  2774.         return (NULL);
  2775.     }
  2776.     if (NULL==(filemem=AllocMem(MAXLENGHT+1, MEMF_CLEAR | MEMF_PUBLIC))){
  2777.         FreeArpRequest(freq);
  2778.         return (NULL);
  2779.     }
  2780.     stccpy( filemem, fname, MAXLENGHT);
  2781. #ifndef NO_ASL
  2782.     freq->rf_File=filemem;
  2783. #else
  2784.     freq->fr_File=filemem;
  2785. #endif
  2786.  
  2787.     if (NULL==(dirmem=AllocMem(MAXLENGHT+1, MEMF_CLEAR | MEMF_PUBLIC))){
  2788.         FreeArpRequest(freq);
  2789.         return (NULL);
  2790.     }
  2791. #ifndef NO_ASL
  2792.     freq->rf_Dir=dirmem;
  2793.  
  2794.     freq->rf_Window=w;
  2795.     freq->rf_Hail="+# Save As ";
  2796.     freq->rf_FuncFlags=RFF_DOCOLOR;
  2797.     freq->rf_Flags2=FR2F_LONGPATH;
  2798. #else
  2799.     freq->fr_Dir=dirmem;
  2800.  
  2801.     freq->fr_Window=w;
  2802.     freq->fr_Hail="+# Save As ";
  2803.     freq->fr_FuncFlags=FRF_DOCOLOR;
  2804. /*     freq->fr_Flags2=FR2F_LONGPATH; */
  2805. #endif
  2806.     return (freq);
  2807. }
  2808.  
  2809. /* ============================================================= */
  2810.  
  2811.  
  2812. LONG saveas(fname)    /* should return length of filenamestring */
  2813. UBYTE *fname;
  2814. {
  2815.     LONG ssize;
  2816.     extern struct FileRequester *filereq;
  2817.  
  2818.     if (ArpBase==NULL
  2819. #ifndef NO_ASL
  2820.                    && AslBase==NULL
  2821. #endif
  2822. #ifndef NO_ASL
  2823.                                        ){
  2824.         if (AslBase=(struct AslBase *)OpenLibrary("asl.library",0)){
  2825.             filereq=AllocFileRequest();
  2826.             if (filereq==NULL){
  2827.                 CloseLibrary(AslBase);
  2828.                 AslBase=NULL;
  2829.                 return (NULL);
  2830.             }
  2831.             stccpy( filereq->rf_File, fname, MAXLENGHT);
  2832.  
  2833.             filereq->rf_Window=w;
  2834.             filereq->rf_Hail="+# Save As ";
  2835.             filereq->rf_Flags2=FR2F_LONGPATH;
  2836.         }
  2837. #endif
  2838.         else if (NULL!=(ArpBase=(struct ArpBase *) OpenLibrary("arp.library", 34L))){
  2839.             filereq=AllocArpRequest(fname);
  2840.             if (filereq==NULL){ /* if NULL, arp.library is closed */
  2841.                 return (NULL);
  2842.             }
  2843.         }
  2844.         else{
  2845.             return(NULL);
  2846.         }
  2847.     }
  2848. #ifndef NO_ASL
  2849.     if (AslBase != NULL){
  2850.         if (RequestFile( filereq )==NULL)
  2851.             return (NULL);
  2852.     }
  2853.     else {
  2854. #endif
  2855.         if (FileRequest( filereq )==NULL)
  2856.             return (NULL);
  2857. #ifndef NO_ASL
  2858.     }
  2859. #endif
  2860.  
  2861.             /* I do not distinguish between Arp & Als Requesterstruct */
  2862.                         /* (because IT IS THE SAME structure)     :-? */
  2863. #ifndef NO_ASL
  2864.     ssize=stccpy(fname, filereq->rf_Dir, MAXLENGHT) -1;
  2865. #else
  2866.     ssize=stccpy(fname, filereq->fr_Dir, MAXLENGHT) -1;
  2867. #endif
  2868.     if (ssize!=0 &&  *(fname+(ssize-1))!=':'){
  2869.         /* if stccpy has copied more than one \0 byte and
  2870.            not only a volume (link "df0 :(!)") has been selected ...*/
  2871.         *(fname+ssize)='/'; /* add slash between dir + filename */
  2872.         ssize++;
  2873.     }
  2874.     /* allow only MAXLENGHT BYTES for full name */
  2875. #ifndef NO_ASL
  2876.     ssize+=stccpy(fname+(ssize), filereq->rf_File, MAXLENGHT-ssize)-1;
  2877. #else
  2878.     ssize+=stccpy(fname+(ssize), filereq->fr_File, MAXLENGHT-ssize)-1;
  2879. #endif
  2880.     return(ssize);
  2881. }
  2882.  
  2883.  
  2884.  
  2885.