home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / maths / plplot / plplot_2 / sys / amiga / src / pla_wn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-02  |  22.1 KB  |  921 lines

  1. /* $Id: pla_wn.c,v 1.11 1994/09/02 08:36:37 mjl Exp $
  2.  * $Log: pla_wn.c,v $
  3.  * Revision 1.11  1994/09/02  08:36:37  mjl
  4.  * Fixed bug encountered when changing screen modes -- FreeRaster call was
  5.  * given the wrong screen dimensions.
  6.  *
  7.  * Revision 1.10  1994/08/23  16:39:02  mjl
  8.  * Minor fixes to work with PLplot 4.99h distribution and other cleaning up.
  9.  *
  10.  * Revision 1.9  1994/05/23  22:11:59  mjl
  11.  * Minor incompatibilities with main sources fixed.
  12.  *
  13.  * Revision 1.8  1994/03/23  09:00:20  mjl
  14.  * Much rearrangement and decruftifying.  Added support for cmap1, color map
  15.  * state changes, solid polygon fills.  Screen now defaults to 16 color, and
  16.  * palette can be set by calling program.  Added hack to capture screen
  17.  * output into an iff file family without user intervention.
  18.  *
  19.  * Revision 1.7  1994/01/17  21:16:06  mjl
  20.  * Changed virt window from 25 to 16x larger than workbench window
  21.  * to fix problem with overflows (submitted by Wesley Ebisuzaki).
  22. */
  23.  
  24. /*    pla_wn.c
  25.  
  26.     PLPLOT Amiga window device driver.
  27. */
  28.  
  29. #include "plplotP.h"
  30. #include "drivers.h"
  31. #include "plamiga.h"
  32. #include "pla_menu.h"
  33.  
  34. #include <graphics/gfxmacros.h>
  35.  
  36. /* Function prototypes */
  37.  
  38. static void   WaitForPage    (PLStream *pls);
  39. static void   HandleEvents    (PLStream *pls);
  40. static void   fill_polygon    (PLStream *pls);
  41. static int    (*func)        (void);
  42. static void   setcmap        (PLStream *pls);
  43.  
  44. /* top level declarations */
  45. /* Keep these global since other Amiga related source files need them */
  46.  
  47. struct ReqToolsBase *ReqToolsBase;
  48. struct Process *myproc;
  49.  
  50. PlAmigaWin PlAmigadev;
  51. PlAmigaWin (*pla) = &PlAmigadev;
  52.  
  53. APTR oldwinptr;
  54. static struct DrawInfo *drInfo;
  55. static int tidy_when_done;
  56.  
  57. /* Data structure for polyline call */
  58.  
  59. static WORD polyTable[PL_MAXPOLY * 2];
  60.  
  61. /* Data structures for area fill calls */
  62.  
  63. #define MAX_VERTICES PL_MAXPOLY
  64. #define BUFFERT_SIZE PL_MAXPOLY * 5 / 2
  65.  
  66. static struct AreaInfo areaInfo;
  67. static struct TmpRas   tmpRas;
  68. static UWORD buffert[ BUFFERT_SIZE ];
  69. static PLANEPTR extra_space;
  70. static WORD ras_width, ras_height;
  71.  
  72. /*------------------------------------------------------------------------
  73.  *  Source machine generated by GadToolsBox V1.4
  74.  *  which is (c) Copyright 1991,92 Jaba Development
  75.  */
  76.  
  77. struct IntuiMessage    PlplotMsg;
  78.  
  79. struct TextAttr topaz11 = {
  80.     (STRPTR)"topaz.font", 11, 0x00, 0x42 };
  81.  
  82. struct TextAttr topaz8 = {
  83.     (STRPTR)"topaz.font", 8, 0x00, 0x02 };
  84.  
  85. struct ColorSpec ScreenColors[] = {
  86.     0, 0x09, 0x09, 0x08,
  87.     1, 0x00, 0x00, 0x00,
  88.     2, 0x0C, 0x0C, 0x0C,
  89.     3, 0x05, 0x06, 0x0B,
  90.     4, 0x00, 0x00, 0x0F,
  91.     5, 0x0F, 0x00, 0x0F,
  92.     6, 0x00, 0x0F, 0x0F,
  93.     7, 0x0F, 0x0F, 0x0F,
  94.     8, 0x06, 0x02, 0x00,
  95.     9, 0x0E, 0x05, 0x00,
  96.     10, 0x09, 0x0F, 0x01,
  97.     11, 0x0E, 0x0B, 0x00,
  98.     12, 0x05, 0x05, 0x0F,
  99.     13, 0x09, 0x02, 0x0F,
  100.     14, 0x00, 0x0B, 0x00,
  101.     15, 0x00, 0x03, 0x06,
  102.     ~0, 0x00, 0x00, 0x00 };
  103.  
  104. UWORD DriPens[] = {
  105.     0, 1, 1, 2, 1, 3, 1, 0, 2, (UWORD) ~0 };
  106.  
  107. /*----------------------------------------------------------------------*\
  108.  * Utility macros/functions
  109. \*----------------------------------------------------------------------*/
  110.  
  111. #define ami_cmd(code) \
  112. if (code) { fprintf(stderr, \
  113.         "Error in %s at line %d\n", \
  114.         __FILE__, __LINE__); \
  115.         return; }
  116.  
  117. static void myputs (char *str)
  118. {
  119.     Write (Output(), str, strlen(str));
  120. }
  121.  
  122. static PLINT
  123. xscale(PLINT x)
  124. {
  125.     return (PLINT) (pla->xoffset + x * pla->xscale);
  126. }
  127.  
  128. static PLINT
  129. yscale(PLINT y)
  130. {
  131.     return (PLINT) (pla->yoffset + pla->cur_height - y * pla->yscale);
  132. }
  133.  
  134. static void 
  135. setpen(PLINT color)
  136. {
  137.     SetAPen(pla->WRPort, color);
  138. }
  139.  
  140. static void 
  141. PLMove(PLINT x, PLINT y)
  142. {
  143.     Move(pla->WRPort, xscale(x), yscale(y));
  144. }
  145.  
  146. static void 
  147. PLDraw(PLINT x, PLINT y)
  148. {
  149.     Draw(pla->WRPort, xscale(x), yscale(y));
  150. }
  151.  
  152. /*----------------------------------------------------------------------*\
  153.  * Initialization of Menus follows.
  154. \*----------------------------------------------------------------------*/
  155.  
  156. struct NewMenu PlplotNewMenu[] = {
  157.  
  158. /********* Project menu *********/
  159.  
  160.     NM_TITLE, (STRPTR)"Project", NULL, 0, NULL, NULL,
  161.  
  162. /* Open */
  163.  
  164.     NM_ITEM, (STRPTR)"Open", (STRPTR)"O", NM_ITEMDISABLED, 0L,
  165.     (APTR)plamiga_Open,
  166.  
  167. /* Save */
  168.  
  169.     NM_ITEM, (STRPTR)"Save", (STRPTR)"S", NM_ITEMDISABLED, 0L,
  170.     (APTR)plamiga_Save,
  171.  
  172. /* Save As */
  173.  
  174.     NM_ITEM, (STRPTR)"Save As", (STRPTR)"A", 0, NULL, NULL,
  175.     NM_SUB, (STRPTR)"ILBM (IFF)", NULL, 0, 0L,
  176.     (APTR)plamiga_SaveAs_ILBM,
  177.  
  178. /* Print */
  179.  
  180.     NM_ITEM, (STRPTR)"Print", (STRPTR)"P", 0, NULL, NULL,
  181.     NM_SUB, (STRPTR)"Bitmap", NULL, 0, 0L,
  182.     (APTR)plamiga_Print_Bitmap,
  183.     NM_SUB, (STRPTR)"Full page (landscape)", NULL, 0, 0L,
  184.     (APTR)plamiga_Print_landscape,
  185.     NM_SUB, (STRPTR)"Full page (portrait)", NULL, 0, 0L,
  186.     (APTR)plamiga_Print_portrait,
  187.  
  188. /* About */
  189.  
  190.     NM_ITEM, (STRPTR)"About...", NULL, NM_ITEMDISABLED, 0L,
  191.     (APTR)plamiga_About,
  192.  
  193. /* Quit */
  194.  
  195.     NM_ITEM, (STRPTR)"Quit", (STRPTR)"Q", 0, 0L,
  196.     (APTR)plamiga_Quit,
  197.  
  198. /********* Settings menu *********/
  199.  
  200.     NM_TITLE, (STRPTR)"Settings", NULL, 0, NULL, NULL,
  201.  
  202. /* Bring up ScreenMode requester */
  203.  
  204.     NM_ITEM, (STRPTR)"Screen Mode", NULL, 0, 0L,
  205.     (APTR)plamiga_Screenmode,
  206.  
  207. /* Bring up palette requester */
  208.  
  209.     NM_ITEM, (STRPTR)"Palette", (STRPTR)"U", 0, 0L,
  210.     (APTR)plamiga_Palette0,
  211.  
  212. /* Bring up cmap 1 modifier (not ready yet) */
  213. /*
  214.     NM_ITEM, (STRPTR)"Palette 1", NULL, NM_ITEMDISABLED, 0L,
  215.     (APTR)plamiga_Palette1,
  216. */
  217.     NM_ITEM, (STRPTR)NM_BARLABEL, NULL, 0, 0L, NULL,
  218.  
  219. /* Load config file */
  220.  
  221.     NM_ITEM, (STRPTR)"Load Settings", NULL, 0, 0L,
  222.     (APTR)plamiga_LoadConfig,
  223.  
  224. /* Save config file */
  225.  
  226.     NM_ITEM, (STRPTR)"Save Settings", NULL, 0, 0L,
  227.     (APTR)plamiga_SaveConfigAs,
  228.     NM_ITEM, (STRPTR)"Save Settings As...", NULL, 0, 0L,
  229.     (APTR)plamiga_SaveConfig,
  230.  
  231. /********* User menu *********/
  232.  
  233.     NM_TITLE, (STRPTR)"User", NULL, 0, NULL, NULL,
  234.     NM_ITEM, (STRPTR)"DUMMY", NULL, NM_ITEMDISABLED, 0L,
  235.     (APTR)plamiga_DUMMY,
  236.  
  237. /* This is the end */
  238.  
  239.     NM_END, NULL, NULL, 0, 0L, NULL };
  240.  
  241. /*----------------------------------------------------------------------*\
  242.  * plD_init_amiwn()
  243.  *
  244.  * Initialize device.
  245. \*----------------------------------------------------------------------*/
  246.  
  247. void
  248. plD_init_amiwn(PLStream *pls)
  249. {
  250.     PLDev *dev;
  251.     PLFLT Initdpmx, Initdpmy;
  252.     struct Screen *wb_screen;
  253.     
  254.     pls->termin = 1;        /* is an interactive terminal */
  255.     pls->icol0 = 1;
  256.     pls->width = 1;
  257.     pls->bytecnt = 0;
  258.     pls->page = 0;
  259.     pls->plbuf_write = 1;
  260.     pls->dev_fill0 = 1;
  261.     tidy_when_done = 0;
  262.  
  263.     if (!pls->colorset)
  264.         pls->color = 1;
  265.  
  266. /* Open the required libraries. */
  267.  
  268.     pla_OpenLibs();
  269.     tidy_when_done = 1;
  270.  
  271. /* Check for hack mode */
  272.  
  273.     if (pls->hack) {
  274.     if (pls->FileName == NULL)
  275.         plP_sfnam(pls, "foo.iff");
  276.  
  277.     plFamInit(pls);
  278.     plP_getmember(pls);
  279.     fprintf(stderr, "Sending screen output to an IFF file family.\n");
  280.     fprintf(stderr, "First file: %s\n", pls->FileName);
  281.     }
  282.  
  283. /* Save old window pointer */
  284.  
  285.     myproc = (struct Process *) FindTask (NULL);
  286.     oldwinptr = myproc->pr_WindowPtr;
  287.  
  288. /*
  289.  * Set up default screen & window parameters.  Take the default screen
  290.  * parameters from Workbench -- the user can always change it and save the
  291.  * configuration.
  292.  */
  293.  
  294.     pla->scr_displayID    = INVALID_ID;
  295.     wb_screen = LockPubScreen("Workbench");
  296.  
  297.     if (wb_screen != NULL) {
  298.     pla->scr_displayID = GetVPModeID(&(wb_screen->ViewPort));
  299.     if (pla->scr_displayID != INVALID_ID) {
  300.         pla->scr_left    = wb_screen->LeftEdge;
  301.         pla->scr_top    = wb_screen->TopEdge;
  302.         pla->scr_width    = wb_screen->Width;
  303.         pla->scr_height    = wb_screen->Height;
  304.     }
  305.     UnlockPubScreen(NULL, wb_screen);
  306.     }
  307.  
  308. /* This is the fallback in case Workbench can't be found */
  309.  
  310.     if (pla->scr_displayID == INVALID_ID) {
  311.     pla->scr_left        = 0;
  312.     pla->scr_top        = 0;
  313.     pla->scr_width        = 640;
  314.     pla->scr_height        = 400;
  315.     pla->scr_displayID    = NTSC_MONITOR_ID | HIRESLACE_KEY;
  316.     }
  317.  
  318.     pla->scr_depth    = 4;
  319.     pla->maxcolors    = pow(2., (double) pla->scr_depth);
  320.     pla->scr_type    = CUSTOMSCREEN;
  321.  
  322.     pla_SetFont();
  323.  
  324. /* Initialize display */
  325.  
  326.     pla_InitDisplay();
  327.  
  328. /* Virtual screen is 16 times the actual one. */
  329.  
  330.     pla->init_width = pla->cur_width * 16;
  331.     pla->init_height = pla->cur_height * 16;
  332.  
  333. /* Set up initial plotting scale factors */
  334.  
  335.     pla->xscale = (double) pla->cur_width / pla->init_width;
  336.     pla->yscale = (double) pla->cur_height / pla->init_height;
  337.  
  338. /* Allocate and initialize device-specific data */
  339.  
  340.     dev = plAllocDev(pls);
  341.  
  342.     dev->xold = UNDEFINED;
  343.     dev->yold = UNDEFINED;
  344.     dev->xmin = 0;
  345.     dev->xmax = pla->init_width - 1;
  346.     dev->ymin = 0;
  347.     dev->ymax = pla->init_height - 1;
  348.  
  349. /* For borderless window, only the menubar width needs to be accounted for in */
  350. /* the offsets.  If the screen resolution changes later, tough!  Too late */
  351. /* to do anything about it. */
  352.  
  353.     pla->xoffset = 8;
  354.     pla->yoffset = pla->screen->BarHeight + 2;
  355.  
  356.     Initdpmx = GfxBase->NormalDPMX;
  357.     Initdpmy = GfxBase->NormalDPMY;
  358.     if (pla->screen->ViewPort.Modes & HIRES)
  359.     Initdpmx *= 2.;
  360.     if (pla->screen->ViewPort.Modes & LACE)
  361.     Initdpmy *= 2.;
  362.  
  363.     plP_setpxl((PLFLT) (Initdpmx / 40.), (PLFLT) (Initdpmy / 40.));
  364.     plP_setphy(0, (pla->init_width - 1), 0, (pla->init_height - 1));
  365. }
  366.  
  367. /*----------------------------------------------------------------------*\
  368.  * plD_line_amiwn()
  369.  *
  370.  * Draw a line in the current color from (x1,y1) to (x2,y2).
  371. \*----------------------------------------------------------------------*/
  372.  
  373. void 
  374. plD_line_amiwn(PLStream *pls, short x1a, short y1a, short x2a, short y2a)
  375. {
  376.     PLDev *dev = (PLDev *) pls->dev;
  377.     int x1=x1a, y1=y1a, x2=x2a, y2=y2a;
  378.     static long count = 0, max_count = 10;
  379.  
  380.     if ( (++count/max_count)*max_count == count) {
  381.     count = 0;
  382.     HandleEvents(pls);      /* Check for events */
  383.     }
  384.  
  385.     if (x1 == dev->xold && y1 == dev->yold) {
  386.     PLDraw(x2, y2);
  387.     }
  388.     else {
  389.     PLMove(x1, y1);
  390.     PLDraw(x2, y2);
  391.     }
  392.     dev->xold = x2;
  393.     dev->yold = y2;
  394. }
  395.  
  396. /*----------------------------------------------------------------------*\
  397.  * plD_polyline_amiwn()
  398.  *
  399.  * Draw a polyline in the current color.
  400. \*----------------------------------------------------------------------*/
  401.  
  402. void 
  403. plD_polyline_amiwn (PLStream *pls, short *xa, short *ya, PLINT npts)
  404. {
  405.     PLDev *dev = (PLDev *) pls->dev;
  406.     PLINT i, j;
  407.     static long count = 0, max_count = 5;
  408.  
  409.     if ( (++count/max_count)*max_count == count) {
  410.     count = 0;
  411.     HandleEvents(pls);      /* Check for events */
  412.     }
  413.  
  414.     for (i=0, j=0; i<npts; i++) {
  415.     polyTable[j++] = xscale(xa[i]);
  416.     polyTable[j++] = yscale(ya[i]);
  417.     }
  418.  
  419.     Move(pla->WRPort, polyTable[0], polyTable[1]);
  420.     PolyDraw(pla->WRPort, npts, polyTable);
  421.  
  422.     dev->xold = UNDEFINED;
  423.     dev->yold = UNDEFINED;
  424. }
  425.  
  426. /*----------------------------------------------------------------------*\
  427.  * plD_eop_amiwn()
  428.  *
  429.  * End of page. 
  430. \*----------------------------------------------------------------------*/
  431.  
  432. void 
  433. plD_eop_amiwn(PLStream *pls)
  434. {
  435.     if (pls->hack) {
  436.     if (plamiga_saveiff(pls->FileName)) {
  437.         fprintf(stderr, "Unable to save file %s, aborting.\n");
  438.         plexit("");
  439.     }
  440.     pls->member += pls->finc;
  441.     plP_getmember(pls);
  442.     }
  443.     else {
  444. /*    DisplayBeep(pla->screen);*/
  445.     WaitForPage(pls);
  446.     }
  447. }
  448.  
  449. /*----------------------------------------------------------------------*\
  450.  * plD_bop_amiwn()
  451.  *
  452.  * Set up for the next page.  
  453.  * Advance to next family file if necessary (file output).
  454. \*----------------------------------------------------------------------*/
  455.  
  456. void 
  457. plD_bop_amiwn(PLStream *pls)
  458. {
  459.     PLDev *dev = (PLDev *) pls->dev;
  460.  
  461.     setpen(0);
  462.     RectFill(pla->WRPort, pla->xoffset, pla->yoffset,
  463.          pla->cur_width + pla->xoffset, pla->cur_height + pla->yoffset);
  464.     setpen(pls->icol0);
  465.  
  466.     dev->xold = UNDEFINED;
  467.     dev->yold = UNDEFINED;
  468.     pls->page++;
  469. }
  470.  
  471. /*----------------------------------------------------------------------*\
  472.  * plD_tidy_amiwn()
  473.  *
  474.  * Close graphics file or otherwise clean up.
  475. \*----------------------------------------------------------------------*/
  476.  
  477. void 
  478. plD_tidy_amiwn(PLStream *pls)
  479. {
  480.     if (tidy_when_done) {
  481.     myproc->pr_WindowPtr = oldwinptr;
  482.  
  483.     pla_CloseWindow();
  484.     pla_CloseScreen();
  485.     pla_CloseLibs();
  486.     }
  487. }
  488.  
  489. /*----------------------------------------------------------------------*\
  490.  * plD_state_amiwn()
  491.  *
  492.  * Handle change in PLStream state (color, pen width, fill attribute, etc).
  493. \*----------------------------------------------------------------------*/
  494.  
  495. void 
  496. plD_state_amiwn(PLStream *pls, PLINT op)
  497. {
  498.     HandleEvents(pls);    /* Check for intuition messages */
  499.  
  500.     switch (op) {
  501.  
  502.     case PLSTATE_WIDTH:
  503.     break;
  504.  
  505. /* Set color map 0 index. */
  506. /* Color 0 is the background. */
  507.  
  508.     case PLSTATE_COLOR0:{
  509.     int icol0 = pls->icol0 % pla->maxcolors;
  510.     setpen(icol0);
  511.     break;
  512.     }
  513.  
  514. /* Set color map 1 index. */
  515.  
  516.     case PLSTATE_COLOR1:{
  517.     int icol1, ncol1;
  518.     if ((ncol1 = MIN(pla->maxcolors - pls->ncol0, pls->ncol1)) < 1)
  519.         break;
  520.  
  521.     icol1 = pls->ncol0 + (pls->icol1 * (ncol1-1)) / (pls->ncol1-1);
  522.     setpen(icol1);
  523.     break;
  524.     }
  525.  
  526. /* Set entire color map */
  527.  
  528.     case PLSTATE_CMAP0:
  529.     case PLSTATE_CMAP1:
  530.     setcmap(pls);
  531.     break;
  532.     }
  533. }
  534.  
  535. /*----------------------------------------------------------------------*\
  536.  * plD_esc_amiwn()
  537.  *
  538.  * Escape function.
  539. \*----------------------------------------------------------------------*/
  540.  
  541. void 
  542. plD_esc_amiwn(PLStream *pls, PLINT op, char *ptr)
  543. {
  544.     HandleEvents(pls);    /* Check for intuition messages */
  545.     switch (op) {
  546.       case PLESC_FILL:
  547.     fill_polygon(pls);
  548.     break;
  549.     }
  550. }
  551.  
  552. /*----------------------------------------------------------------------*\
  553.  * fill_polygon()
  554.  *
  555.  * Fill polygon described in points pls->dev_x[] and pls->dev_y[].
  556.  * Only solid color fill supported.
  557. \*----------------------------------------------------------------------*/
  558.  
  559. static void
  560. fill_polygon(PLStream *pls)
  561. {
  562.     int i;
  563.  
  564.     if ( AreaMove(pla->WRPort,
  565.           xscale(pls->dev_x[0]), yscale(pls->dev_y[0])) ) {
  566.     fprintf(stderr, "Error in %s at line %d\n", __FILE__, __LINE__);
  567.     goto end;
  568.     }
  569.  
  570.     for (i = 1; i < pls->dev_npts; i++) {
  571.     if ( AreaDraw(pla->WRPort,
  572.               xscale(pls->dev_x[i]), yscale(pls->dev_y[i])) ) {
  573.         fprintf(stderr, "Error in %s at line %d\n", __FILE__, __LINE__);
  574.         goto end;
  575.     }
  576.     }
  577.  
  578.  end:
  579.     ami_cmd( AreaEnd(pla->WRPort) );
  580. }
  581.  
  582. /*----------------------------------------------------------------------*\
  583.  * setcmap()
  584.  *
  585.  * Sets up color palette.
  586. \*----------------------------------------------------------------------*/
  587.  
  588. static void
  589. setcmap(PLStream *pls)
  590. {
  591.     int i, ncol1 = MIN(pla->maxcolors - pls->ncol0, pls->ncol1);
  592.     PLColor cmap1col;
  593.  
  594. /* Initialize cmap 0 colors */
  595.  
  596.     for (i = 0; i < pls->ncol0; i++) 
  597.     SetRGB4(pla->VPort, i,
  598.         pls->cmap0[i].r>>4, pls->cmap0[i].g>>4, pls->cmap0[i].b>>4);
  599.  
  600. /* Initialize any remaining slots for cmap1 */
  601.  
  602.     for (i = 0; i < ncol1; i++) {
  603.     plcol_interp(pls, &cmap1col, i, ncol1);
  604.     SetRGB4(pla->VPort, i + pls->ncol0,
  605.         cmap1col.r>>4, cmap1col.g>>4, cmap1col.b>>4);
  606.     }
  607. }
  608.  
  609. /*----------------------------------------------------------------------*\
  610.  * pla_InitDisplay()
  611.  *
  612.  * Initialize display and other associated parameters.
  613. \*----------------------------------------------------------------------*/
  614.  
  615. void 
  616. pla_InitDisplay(void)
  617. {
  618. /* Open screen & window */
  619.  
  620.     if (pla_OpenScreen())
  621.     exit(1);
  622.  
  623.     if (pla_OpenWindow())
  624.     exit(1);
  625.  
  626. /* Change the window pointer for requesters */
  627.  
  628.     myproc->pr_WindowPtr = pla->window;
  629.  
  630. /* Initialize amiga display state data structure */
  631.  
  632.     pla->SRPort = &(pla->screen->RastPort);
  633.     pla->WRPort = pla->window->RPort;
  634.     pla->VPort = &(pla->screen->ViewPort);
  635.     pla->CMap = pla->VPort->ColorMap;
  636.  
  637. /* Drawing info */
  638.  
  639.     setcmap(plsc);
  640.     setpen(1);
  641. /*
  642.     drInfo = GetScreenDrawInfo(pla->screen);
  643. */
  644. /* Inane initialization for area fills. */
  645. /* Get some space for the vertices and initialize the AreaInfo ptr: */
  646.  
  647.     InitArea( &areaInfo, buffert, MAX_VERTICES );
  648.     pla->WRPort->AreaInfo = &areaInfo;
  649.  
  650. /* Initialize the TmpRas structure: */
  651.  
  652.     pla->WRPort->TmpRas = (struct TmpRas *)
  653.     InitTmpRas( &tmpRas, extra_space, 
  654.            RASSIZE( ras_width, ras_height ) );
  655.  
  656.     BNDRYOFF(pla->WRPort);
  657.  
  658. /* Current window size. */
  659.  
  660.     pla->cur_width = pla->window->Width -
  661.     pla->window->BorderLeft - pla->window->BorderRight - 16;
  662.  
  663.     pla->cur_height = pla->window->Height -
  664.     pla->window->BorderTop - pla->window->BorderBottom - 16;
  665. }
  666.  
  667. /*----------------------------------------------------------------------*\
  668.  * pla_SetFont()
  669.  *
  670.  * Set up font for menus, screen bar.  May eventually be user-settable.
  671.  * For now, just select between topaz 8 and topaz 11 on the basis of number
  672.  * of lines in the display.
  673. \*----------------------------------------------------------------------*/
  674.  
  675. void
  676. pla_SetFont(void)
  677. {
  678.     if (pla->scr_height >= 350)
  679.     pla->font = &topaz11;
  680.     else
  681.     pla->font = &topaz8;
  682. }
  683.  
  684. /*----------------------------------------------------------------------*\
  685.  * pla_OpenScreen()
  686. \*----------------------------------------------------------------------*/
  687.  
  688. int pla_OpenScreen(void)
  689. {
  690.     if ( ! (pla->screen =
  691.         OpenScreenTags(NULL,
  692.                SA_Pens,        DriPens,
  693.                SA_Depth,        pla->scr_depth,
  694.                SA_Left,        pla->scr_left,
  695.                SA_Top,        pla->scr_top,
  696.                SA_Width,        pla->scr_width,
  697.                SA_Height,        pla->scr_height,
  698.                SA_Colors,        &ScreenColors[0],
  699.                SA_Font,        pla->font,
  700.                SA_Type,        pla->scr_type,
  701.                SA_DisplayID,    pla->scr_displayID,
  702.                SA_AutoScroll,    1,
  703.                SA_Overscan,        OSCAN_STANDARD,
  704.                SA_ShowTitle,    0,
  705.                TAG_DONE)))
  706.     return(1L);
  707.  
  708.     if ( ! (pla->visual = GetVisualInfo(pla->screen, TAG_DONE)))
  709.     return(2L);
  710.  
  711. /* Allocate some space that is needed to build up area fill objects */
  712.  
  713.     ras_width = pla->scr_width;
  714.     ras_height = pla->scr_height;
  715.     extra_space = (PLANEPTR) AllocRaster( ras_width, ras_height );
  716.     if( extra_space == NULL )
  717.     plexit( "Could NOT allocate enough memory for the temp raster!" );
  718.  
  719.     return(0L);
  720. }
  721.  
  722. /*----------------------------------------------------------------------*\
  723.  * pla_CloseScreen()
  724. \*----------------------------------------------------------------------*/
  725.  
  726. void pla_CloseScreen(void)
  727. {
  728.     if (extra_space) {
  729.     FreeRaster( extra_space, ras_width, ras_height );
  730.     extra_space = NULL;
  731.     }
  732.  
  733.     if (pla->visual) {
  734.     FreeVisualInfo(pla->visual);
  735.     pla->visual = NULL;
  736.     }
  737.  
  738.     if (pla->screen) {
  739.     CloseScreen(pla->screen);
  740.     pla->screen = NULL;
  741.     }
  742. }
  743.  
  744. /*----------------------------------------------------------------------*\
  745.  * pla_OpenWindow()
  746. \*----------------------------------------------------------------------*/
  747.  
  748. int pla_OpenWindow(void)
  749. {
  750.     if ( ! (pla->menus = CreateMenus(PlplotNewMenu, GTMN_FrontPen, 0L, TAG_DONE)))
  751.     return(3L);
  752.  
  753.     LayoutMenus(pla->menus, pla->visual, GTMN_TextAttr, pla->font, TAG_DONE);
  754.  
  755.     if ( ! (pla->window =
  756.         OpenWindowTags(NULL,
  757.                WA_Left,        pla->scr_left,
  758.                WA_Top,        pla->scr_top,
  759.                WA_Width,        pla->scr_width,
  760.                WA_Height,        pla->scr_height,
  761.                WA_IDCMP,        IDCMP_MENUPICK |
  762.                            IDCMP_VANILLAKEY | 
  763.                         IDCMP_REFRESHWINDOW,    
  764.                WA_Flags,        WFLG_SMART_REFRESH |
  765.                            WFLG_BACKDROP |
  766.                         WFLG_BORDERLESS |
  767.                            WFLG_ACTIVATE,
  768.                WA_ScreenTitle,    "Plplot 5.0",
  769.                WA_CustomScreen,    pla->screen,
  770.                TAG_DONE)))
  771.     return(4L);
  772.  
  773.     SetMenuStrip(pla->window, pla->menus);
  774.     GT_RefreshWindow(pla->window, NULL);
  775.  
  776.     return(0L);
  777. }
  778.  
  779. /*----------------------------------------------------------------------*\
  780.  * pla_CloseWindow()
  781. \*----------------------------------------------------------------------*/
  782.  
  783. void pla_CloseWindow(void)
  784. {
  785.     if (pla->menus) {
  786.     ClearMenuStrip(pla->window);
  787.     FreeMenus(pla->menus);
  788.     pla->menus = NULL;
  789.     }
  790.  
  791.     if (pla->window) {
  792.     CloseWindow(pla->window);
  793.     pla->window = NULL;
  794.     }
  795. }
  796.  
  797. /*----------------------------------------------------------------------*\
  798.  * WaitForPage()
  799.  *
  800.  * This routine waits for the user to advance the plot, while handling
  801.  * all other events.
  802. \*----------------------------------------------------------------------*/
  803.  
  804. static void
  805. WaitForPage(PLStream *pls)
  806. {
  807.     if (pls->nopause)
  808.     return;
  809.  
  810.     while (!pla->exit_eventloop) {
  811.     Wait(1L << pla->window->UserPort->mp_SigBit);
  812.     HandleEvents(pls);
  813.     }
  814.     pla->exit_eventloop = FALSE;
  815. }
  816.  
  817.  
  818. /*----------------------------------------------------------------------*\
  819.  * HandleEvents()
  820.  *
  821.  * Just a front-end to HandlePlplotIDCMP() to make sure the stream
  822.  * pointer is saved where the event-handling code can access it.
  823. \*----------------------------------------------------------------------*/
  824.  
  825. static void
  826. HandleEvents(PLStream *pls)
  827. {
  828.     HandlePlplotIDCMP();
  829. }
  830.  
  831. /*----------------------------------------------------------------------*\
  832.  * HandlePlplotIDCMP
  833.  *
  834.  * Responds to all events that PLPLOT cares about.
  835. \*----------------------------------------------------------------------*/
  836.  
  837. int HandlePlplotIDCMP(void)
  838. {
  839.     struct IntuiMessage    *m;
  840.     struct MenuItem    *n;
  841.     BOOL        running = TRUE;
  842.  
  843.     while(m = GT_GetIMsg(pla->window->UserPort)) {
  844.  
  845.     CopyMem((char *) m, (char *) &PlplotMsg,
  846.         (long)sizeof(struct IntuiMessage));
  847.  
  848.     GT_ReplyIMsg(m);
  849.  
  850.     switch (PlplotMsg.Class) {
  851.  
  852.     case    IDCMP_REFRESHWINDOW:
  853.         GT_BeginRefresh(pla->window);
  854.         GT_EndRefresh(pla->window, TRUE);
  855.         break;
  856.  
  857.     case    IDCMP_VANILLAKEY:
  858.         running = plamiga_KEY();
  859.         break;
  860.  
  861.     case    IDCMP_MENUPICK:
  862.         while(PlplotMsg.Code != MENUNULL) {
  863.         n = ItemAddress(pla->menus, PlplotMsg.Code);
  864.         func = (void *)(GTMENUITEM_USERDATA(n));
  865.         running = func();
  866.         if (pla->restart) {
  867.             pla->restart = 0;
  868.             return (running);
  869.         }
  870.         PlplotMsg.Code = n->NextSelect;
  871.         }
  872.         break;
  873.     }
  874.     }
  875.     return(running);
  876. }
  877.  
  878. /*----------------------------------------------------------------------*\
  879.  * Libraries
  880. \*----------------------------------------------------------------------*/
  881.  
  882. void 
  883. pla_OpenLibs(void)
  884. {
  885.     if (IntuitionBase =
  886.     (struct IntuitionBase *) OpenLibrary("intuition.library", 0L)) {
  887.  
  888.     if (GfxBase =
  889.         (struct GfxBase *) OpenLibrary("graphics.library", 0L)) {
  890.  
  891.         if (ReqToolsBase =
  892.         (struct ReqToolsBase *) OpenLibrary (REQTOOLSNAME,
  893.                              REQTOOLSVERSION))
  894.  
  895.         return;        /* All libraries successfully opened */
  896.         else
  897.         myputs ("You need reqtools.library V38 or higher!\n"
  898.             "Please install it in your Libs: drirectory.\n");
  899.  
  900.         CloseLibrary((struct Library *) GfxBase);
  901.     }
  902.     else
  903.         puts("\nError opening Graphics library.");
  904.  
  905.     CloseLibrary((struct Library *) IntuitionBase);
  906.     }
  907.     else
  908.     puts("\nError opening Intuition library.");
  909.  
  910.     plexit("Initialization failure");
  911. }
  912.  
  913. void 
  914. pla_CloseLibs(void)
  915. {
  916.     CloseLibrary((struct Library *) GfxBase);
  917.     CloseLibrary((struct Library *) IntuitionBase);
  918.     CloseLibrary((struct Library *) ReqToolsBase);
  919. }
  920.  
  921.