home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 206.lha / C-Functions / MakeBox.c < prev    next >
C/C++ Source or Header  |  1996-02-14  |  8KB  |  235 lines

  1.  
  2. /* Program for drawing Assorted Boxes */
  3. /* (C)Copyright 1988 by Lars Thuring */
  4.  
  5. /* May be freely distributed and used for any purpose as long as
  6.    the Copyright notice is left unchanged. */
  7.  
  8. /* 20 feb 1988 V0.1  Created */
  9. /* 21 feb 1988 V0.2  Coordinates from subroutine,
  10.                      May call itself for better boxes */
  11. /* 28 feb 1988 V0.3  GetCoords: perspective box */
  12. /* 10 mar 1988 V0.4  Doesn't destroy command string,
  13.                      Passing of text to be printed,
  14.                      BackPen & FrontPen selectable,
  15.                      Box 3 */
  16. /* 24 apr 1988 V0.5  k*=10+mode[i++]-'0'; doesn't compile correct */
  17. /* 16 may 1988 V0.6  Bug; JAM1 -> JAM2;
  18.                      Solid background colour option added;
  19.                      F, C & J options affect target rp;
  20.                      Very persistant "bug"; adjust i-- after Adigit() */
  21.  
  22. #include <exec/types.h>
  23. #include <intuition/intuition.h>
  24.  
  25. /*#define DEBUG */
  26.  
  27. #define XOFF   0
  28. #define YOFF   1
  29. #define HSPACE 3
  30. #define VSPACE 3
  31.  
  32. VOID MakeBox();               /* Purpose of this ... */
  33. SHORT GetBoxCoordinates();    /* Spinoff product */
  34.  
  35.    /* No Globals */
  36.  
  37. VOID MakeBox(rp,x,y,w,h,mode) /* Draw a selected box in rp */
  38. struct RastPort *rp;          /* Rastport for controlling para's */
  39. SHORT x,y,                    /* Offsets into raster */
  40.       w,h;                    /* Size of (inner) box */
  41. UBYTE *mode;                  /* String with drawing options */
  42.    {
  43. BOOL   Adigit();              /* Return TRUE if ASCII digit */
  44.  
  45. int i,j,k,                    /* trash */
  46.     DrawDone=FALSE,           /* Flag for one draw only per call */
  47.     Solid=FALSE;              /* Default no fill background */
  48.  
  49. UBYTE *BoxTypes="0123",       /* Allowed boxtypes */
  50.       *p=NULL;                /* Flag and pointer */
  51.  
  52. UBYTE FrontPen=AUTOFRONTPEN,BackPen=AUTOBACKPEN; /* Default defaults */
  53. SHORT DrawMode = (SHORT) rp->DrawMode;
  54.  
  55. struct Border MyBorder;          /* Use as stencil */
  56. SHORT xy[15][2],                 /* Max number of turning points */
  57.       Count=0;                   /* Default is n */
  58.  
  59. struct IntuiText MyText;         /* Stencil for text */
  60.  
  61.    /* Here we go */
  62.  
  63. #ifdef DEBUG
  64.    printf("entry string = %s.\n", mode);
  65. #endif
  66.  
  67.    if (NOT( j=strlen(mode)))     /* Return if nothing to work with */
  68.       return;
  69.  
  70.    if (k=strinstr(mode,']'))     /* Is a text passed ? */
  71.       {                          /* Then set parameters not supplied */
  72.       if (h == 0)
  73.          h = rp->TxHeight+HSPACE*2-1;
  74.       if (w == 0)
  75.          w = (strlen(mode)-k)*rp->TxWidth+VSPACE*2-1;
  76.       }
  77.  
  78.    /* Then get caller wishes */
  79.  
  80.    for(i=0;i<j;i++)              /* Set options */
  81.       {
  82.       if (DrawDone)
  83.          break;
  84.       switch( mode[i] )
  85.          {
  86.          case 'B':                        /* Select new BackPen */
  87.             k=0;
  88.             while (Adigit(mode[++i]))     /* if followed by ASCII digit */
  89.                k=k*10+mode[i]-'0';        /* Add digit */
  90.             BackPen=k;                    /* Future: check against rp */
  91.             i--;
  92.             break;
  93.  
  94.          case 'C':
  95.             DrawMode=COMPLEMENT;
  96.             rp->DrawMode = (BYTE) DrawMode;
  97.             break;
  98.  
  99.          case 'F':                        /* Select new FrontPen */
  100.             k=0;
  101.             while (Adigit(mode[++i]))     /* if followed by ASCII digit */
  102.                k=k*10+mode[i]-'0';        /* Add digit */
  103.             FrontPen=k;                   /* Future: check against rp */
  104.             SetAPen( rp, FrontPen);
  105.             i--;
  106.             break;
  107.  
  108.          case 'J':                        /* The other DrawMode */
  109.             DrawMode=JAM2;
  110.             rp->DrawMode = (BYTE) DrawMode;
  111.             break;
  112.  
  113.          case 'S':                        /* Fill in background (Solid) */
  114.             Solid=TRUE;
  115.             break;
  116.  
  117.          case ']':            /* Then rest is text */
  118.  
  119.             p=mode+i+1;       /* Pointer to text */
  120.             DrawDone=TRUE;    /* Skip rest */
  121.             break;
  122.  
  123.          default:                                  /* Boxes ? */
  124.             if (k=strinstr(BoxTypes, mode[i]))
  125.                {
  126.                Count=GetBoxCoordinates(&xy[0][0],w,h,k-1,NULL);
  127.                MakeBox(rp,x,y,w,h,mode+i+1);      /* Get next before end */
  128.                DrawDone=TRUE;
  129.                }
  130.             break;
  131.          }
  132.       }
  133.  
  134.    /* Now render background, text and box, if any */
  135.  
  136.    if (Solid)           /* Fill background with solid colour */
  137.       {
  138.       rp->DrawMode = (BYTE) DrawMode;
  139.       RectFill(rp, x,y, x+w,y+h);
  140.       }
  141.  
  142.    if (p)               /* Print text if one was passed */
  143.       {
  144.       MyText.LeftEdge   = VSPACE;
  145.       MyText.TopEdge    = HSPACE;
  146.       MyText.FrontPen   = FrontPen;
  147.       MyText.BackPen    = BackPen;
  148.       MyText.DrawMode   = DrawMode;
  149.       MyText.ITextFont  = NULL;
  150.       MyText.IText      = p;
  151.       MyText.NextText   = NULL;
  152.  
  153.       PrintIText(rp,&MyText,x,y);
  154.       }
  155.  
  156.  
  157.    if (Count)             /* Don't draw if not got anything */
  158.       {
  159.       MyBorder.LeftEdge   = 0;
  160.       MyBorder.TopEdge    = 0;
  161.       MyBorder.FrontPen   = FrontPen;
  162.       MyBorder.BackPen    = BackPen;
  163.       MyBorder.DrawMode   = DrawMode;
  164.       MyBorder.Count      = Count;
  165.       MyBorder.XY         = &xy[0][0];
  166.       MyBorder.NextBorder = NULL;
  167.  
  168.       DrawBorder(rp,&MyBorder,x,y);       /* */
  169.       }
  170.  
  171.    }  /* End of MakeBox */
  172.  
  173.  
  174.  
  175. SHORT GetBoxCoordinates(xy,w,h,func,Size) /* Set Coordinates for Boxes */
  176. SHORT (*xy)[2];                           /* Where to store Coordinates */
  177. SHORT w,h;                                /* Size of (inner) box */
  178. SHORT Size;                               /* + size */
  179. int func;                                 /* Selected Box */
  180.    {
  181. SHORT Count=0;
  182.    switch (func)
  183.       {
  184.  
  185.       case 1:                                      /* 1 - Dropshadow */
  186.          xy[0][XOFF] = w+1,   xy[0][YOFF] = 3;
  187.          xy[1][XOFF] = w+1,   xy[1][YOFF] = h+1;
  188.          xy[2][XOFF] = 3,     xy[2][YOFF] = h+1;
  189.          xy[3][XOFF] = 3,     xy[3][YOFF] = h+2;
  190.          xy[4][XOFF] = w+2,   xy[4][YOFF] = h+2;
  191.          xy[5][XOFF] = w+2,   xy[5][YOFF] = 3;
  192.          xy[6][XOFF] = w+3,   xy[6][YOFF] = 3;
  193.          xy[7][XOFF] = w+3,   xy[7][YOFF] = h+3;
  194.          xy[8][XOFF] = 3,     xy[8][YOFF] = h+3;
  195.          Count=9;
  196.          break;
  197.  
  198.       case 2:                                /* 2 - Perspective box */
  199.          xy[0][XOFF] = 1,      xy[0][YOFF] = -1;
  200.          xy[1][XOFF] = w/10,   xy[1][YOFF] = -h/8;
  201.          xy[2][XOFF] = w*9/10, xy[2][YOFF] = -h/8;
  202.          xy[3][XOFF] = w-1,    xy[3][YOFF] = -1;
  203.          Count=4;
  204.          break;
  205.  
  206.       case 3:                                /* 3 - As 2 but fixed depth */
  207.          xy[0][XOFF] = 1,      xy[0][YOFF] = -1;
  208.          xy[1][XOFF] = 3,      xy[1][YOFF] = -3;
  209.          xy[2][XOFF] = w-3,    xy[2][YOFF] = -3;
  210.          xy[3][XOFF] = w-1,    xy[3][YOFF] = -1;
  211.          Count=4;
  212.          break;
  213.  
  214.       case -1:                                     /* = special */
  215.          Count=9;                      /* Current max Count possible */
  216.          break;
  217.  
  218.       case 0:
  219.       default:                                     /*   - Box */
  220.          xy[0][XOFF] = 0,   xy[0][YOFF] = 0;
  221.          xy[1][XOFF] = w,   xy[1][YOFF] = 0;
  222.          xy[2][XOFF] = w,   xy[2][YOFF] = h;
  223.          xy[3][XOFF] = 0,   xy[3][YOFF] = h;
  224.          xy[4][XOFF] = 0,   xy[4][YOFF] = 1;       /* else 0,0 twice */
  225.          Count=5;
  226.          break;
  227.       }
  228.  
  229.    return(Count);
  230.  
  231.    } /* End of GetBoxCoordinates() */
  232.  
  233.  
  234.  
  235.