home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 199.lha / GimmeLib / intuistuff.c < prev    next >
C/C++ Source or Header  |  1988-12-27  |  3KB  |  140 lines

  1. /*
  2.  *  FILE: intuistuff.c
  3.  *  Support routines for creating some useful intuition-type structures.
  4.  *
  5.  *  Public Domain, but keep my name in it as the original author.
  6.  *  31-Aug-88    Jan Sven Trabandt   first release version
  7.  *  31-Oct-88    Jan Sven Trabandt   made gimmeBorder more flexible/efficient
  8.  */
  9.  
  10.  
  11. #define I_AM_INTUISTUFF
  12. #include "gimmelib/gimmefuncs.h"
  13. #include "gimmelib/intuistuff.h"
  14. #include "gimmelib/macros.h"
  15.  
  16.  
  17. /* WSL-type construct */
  18. #define GUESS        do {
  19. #define QUIF( cond )    if( cond ) break;
  20. #define ENDGUESS    } while( 0 );
  21.  
  22.  
  23. struct Border *gimmeBorder( mh, xsize, ysize )
  24.     void    **mh;
  25.     SHORT   xsize, ysize;
  26. {
  27.     register struct Border  *bp;
  28.     register SHORT        *r;
  29.     ULONG            size;
  30.     void            *mymh = NULL;
  31.     void            **mhdr = &mymh;
  32.  
  33.     GUESS
  34.     size = sizeof(struct Border);
  35.     if( !mh ) {
  36.         r = AllocMem( (ULONG)sizeof(SHORT) * 2 * 5, MEMF_PUBLIC );
  37.         QUIF( !r );
  38.         mhdr = NULL;
  39.     } else {
  40.         if( xsize > 0 && ysize > 0 ) {
  41.         /* need room for 5 pairs of shorts as well */
  42.         size = sizeof(struct Border) + sizeof(SHORT) * 2 * 5;
  43.         r = NULL;
  44.         }
  45.     }
  46.     bp = chainAllocMem( mhdr, (ULONG) size, MEMF_CLEAR | MEMF_PUBLIC );
  47.     QUIF( !bp );
  48.     bp->FrontPen = 1;
  49.     bp->DrawMode = JAM1;
  50.     if( xsize > 0 && ysize > 0 ) {
  51.         if( !r ) {
  52.         r = (SHORT *) (bp + 1);
  53.         }
  54.         bp->XY = r;
  55.         r[2] = r[4] = xsize - 1;
  56.         r[5] = r[7] = ysize - 1;
  57.         bp->Count = 5;
  58.     }
  59.     linkChainMem( mh, mymh );
  60.     return( bp );
  61.     ENDGUESS
  62.     if( mhdr ) {
  63.     chainFreeMem( mymh );
  64.     } else {
  65.     if( r ) {
  66.         FreeMem( r, (ULONG) sizeof(SHORT) * 2 * 5 );
  67.     }
  68.     }
  69.     return( NULL );
  70. } /* gimmeBorder */
  71.  
  72.  
  73. struct Image *gimmeImage( mh, depth, width, height )
  74.     void    **mh;
  75.     SHORT   depth, width, height;
  76. {
  77.     register struct Image   *ip;
  78.     ULONG   size;            /* image datasize (bytes) for allocation */
  79.     void    *mymh = NULL;
  80.     void    **mhdr = &mymh;
  81.     UBYTE   planepick;
  82.  
  83.     GUESS
  84.     if( !mh ) {
  85.         mhdr = NULL;
  86.     }
  87.     ip = chainAllocMem( mhdr, (ULONG)sizeof(struct Image),
  88.                 MEMF_CLEAR | MEMF_PUBLIC );
  89.     QUIF( !ip );
  90.     ip->Width = width;
  91.     ip->Height = height;
  92.     ip->Depth = depth;
  93.     if( depth > 0 && width > 0 && height > 0 ) {
  94.         size = GIM_IMAGESIZE(depth, width, height);
  95.         ip->ImageData = chainAllocMem( mhdr, size, MEMF_CLEAR|MEMF_CHIP );
  96.         QUIF( !ip->ImageData );
  97.         planepick = 1;
  98.         while( --depth > 0 ) {
  99.         planepick = (planepick << 1) + 1;
  100.         } /* while */
  101.         ip->PlanePick = planepick;
  102.     }
  103.     linkChainMem( mh, mymh );
  104.     return( ip );
  105.     ENDGUESS
  106.     if( mhdr ) {
  107.     chainFreeMem( mymh );
  108.     } else {
  109.     if( ip ) {
  110.         FreeMem( ip, (ULONG)sizeof(struct Image) );
  111.     }
  112.     }
  113.     return( NULL );
  114. } /* gimmeImage */
  115.  
  116.  
  117. struct IntuiText *gimmeIntuiText( mh, s, textattr, width )
  118.     void    **mh;
  119.     UBYTE   *s;
  120.     struct TextAttr *textattr;
  121.     SHORT   width;
  122. {
  123.     register struct IntuiText    *itp;
  124.  
  125.     GUESS
  126.     itp = chainAllocMem( mh, (ULONG)sizeof(struct IntuiText),
  127.                 MEMF_CLEAR | MEMF_PUBLIC );
  128.     QUIF( !itp );
  129.     itp->FrontPen = 1;
  130.     itp->DrawMode = JAM2;
  131.     itp->ITextFont = textattr;
  132.     itp->IText = s;
  133.     if( width > 0 ) {           /* if width given, centre text */
  134.         itp->LeftEdge = (width - IntuiTextLength(itp)) >> 1;
  135.     }
  136.     return( itp );
  137.     ENDGUESS
  138.     return( NULL );
  139. } /* gimmeIntuiText */
  140.