home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 634.lha / ppdata_v1.0 / src.LZH / src / wintext.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-11  |  3.6 KB  |  141 lines

  1. /*
  2. *    wintext.c - version 0.1
  3. *
  4. *    Routines for font-independent window-text system, which allows
  5. *    writing of text based on character positions.
  6. *    Still being tweaked.
  7. *
  8. *    MWS 3/92.
  9. */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <graphics/gfxmacros.h>
  14. #include <intuition/intuition.h>
  15. #include <proto/exec.h>
  16. #include <proto/graphics.h>
  17. #include <proto/intuition.h>
  18.  
  19. #include <string.h>
  20. #include <stdarg.h>
  21. #include "sprintf.h"
  22.  
  23. #include "wintext.h"
  24.  
  25. /*
  26.  *    InitWinTextInfo
  27.  *    
  28.  *    Gets information about workbench screen (font and borders) and 
  29.  *    initialises passed WINTEXTINFO, and sets NewWindow's width and
  30.  *    height based on rows and columns required. Once caller has
  31.  *    opened window, he/she should do a SetFont on it's rastport with
  32.  *    the TextAttr stored in the WINTEXTINFO structure.
  33.  */
  34. BOOL InitWinTextInfo(WINTEXTINFO *wti,
  35.              struct NewWindow *nw,
  36.              UWORD  rows, UWORD columns)
  37. {
  38.     struct Screen screen;
  39.     struct TextFont *tf;
  40.     UWORD    i;
  41.  
  42.     if (GetScreenData(&screen, sizeof(screen), WBENCHSCREEN, NULL))
  43.     {
  44.         if (wti->spaces = AllocMem(columns+1,0L))
  45.         {
  46.             for (i = 0; i < columns; i++)
  47.                 wti->spaces[i] = ' ';    
  48.         }
  49.         else return FALSE;
  50.  
  51.         if (tf = OpenFont(wti->tattr = screen.Font))    /* have a peek */
  52.         {
  53.             wti->font_x = tf->tf_XSize;
  54.             wti->font_y = tf->tf_YSize;
  55.             wti->font_baseline = tf->tf_Baseline;
  56.             wti->toffset = screen.WBorTop + tf->tf_YSize + 2;
  57.             wti->loffset = screen.WBorLeft + 4;
  58.             wti->roffset = screen.WBorRight + 4;
  59.             wti->boffset = screen.WBorBottom + 1;
  60.  
  61.             wti->rows = rows; wti->columns = columns;
  62.  
  63.             nw->TopEdge = screen.WBorTop + tf->tf_YSize + 1;
  64.             nw->Height = rows*wti->font_y + wti->toffset + wti->boffset;
  65.             nw->Width =  columns*wti->font_x + wti->loffset + wti->roffset;
  66.  
  67.             CloseFont(tf);        /* should be set to rastport of window */
  68.             return TRUE;
  69.         }
  70.         FreeMem(wti->spaces, columns+1);    /* free memory */
  71.     }
  72.     return FALSE;
  73. }
  74.  
  75.  
  76. /*
  77.  *    FinishWinText
  78.  *    
  79.  *    Should be called after all use of WinText routines for a
  80.  *    particular window.
  81.  */
  82. void FinishWinText(WINTEXTINFO *wti)        /* end of WinText session - free mem */
  83. {
  84.     if (wti->spaces) FreeMem(wti->spaces, wti->columns+1);
  85. }
  86.  
  87.  
  88. /*
  89.  *    RenderWinTexts
  90.  *    
  91.  *    Writes the each member of the wintext list to the window. Preforms
  92.  *    clipping to window's rows and columns. If an overwriting drawmode is
  93.  *    specified in the WINTEXT structure, the remaining area after drawing
  94.  *    the text will be cleared by spaces (it is regardless of drawmode, but
  95.  *    JAM1 has no effect.
  96.  */
  97. void RenderWinTexts(WINTEXTINFO *info, WINTEXT *wt)
  98. {
  99.     struct RastPort *rp;
  100.     WORD    len,            /* string length */
  101.         columns;        /* space we've got to render string */
  102.  
  103.     while (wt)
  104.     {
  105.         if (wt->tpos < info->rows)    /* within row bounds */
  106.         {
  107.             columns = MIN(info->columns - wt->lpos - 1, wt->columns);
  108.             len = MIN(strlen(wt->text), columns);
  109.             rp = info->window->RPort;
  110.             SetAPen(rp, wt->pen);
  111.             SetDrMd(rp, wt->mode);
  112.             Move(rp, info->loffset + wt->lpos*info->font_x, 
  113.                  info->toffset + wt->tpos*info->font_y + info->font_baseline);
  114.             Text(rp, wt->text, len);
  115.             Text(rp, info->spaces, columns - len);
  116.         }
  117.         wt = wt->next;
  118.     }
  119. }
  120.  
  121. /*
  122.  *    RenderWinTextsFmt(WINTEXTINFO *, WINTEXT *, char *fmt, arg1, arg2, ... )
  123.  *    
  124.  *    Same as RenderWinTexts, but uses SPrintf to format string in
  125.  *    first WINTEXT of list, passing additional arguments to SPrintf.
  126.  *    Assumes that 1st WINTEXT->text contains a buffer large enough
  127.  *    for the SPrintf call. If fmt is zero, WINTEXT->text is cleared
  128.  *    (actually, 1st byte set to '\0').
  129.  *
  130.  */    
  131. void RenderWinTextsFmt(WINTEXTINFO *info, WINTEXT *wt, char *fmt, ...)
  132. {
  133.     va_list ap;
  134.     va_start(ap,fmt);
  135.  
  136.     if (fmt) VSPrintf(wt->text, fmt, (APTR)ap);
  137.     else wt->text[0] = '\0';
  138.  
  139.     RenderWinTexts(info,wt);
  140. }
  141.