home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / cliptext.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  14.4 KB  |  309 lines

  1. ;/* cliptext.c - Execute me to compile me with Lattice 5.10a
  2. LC -cfistq -v -y -j73 cliptext.c
  3. Blink FROM LIB:c.o,cliptext.o TO cliptext LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit ;
  5. **
  6. ** The following example, cliptext.c, renders the contents of a text
  7. ** file to a Workbench window.  This example gets the new aspect ratio
  8. ** for a font by asking the display database what the aspect ratio of
  9. ** the current display mode is.
  10. */
  11. #include <exec/types.h>
  12. #include <dos/rdargs.h>
  13. #include <dos/dosextens.h>
  14. #include <intuition/intuition.h>
  15. #include <graphics/text.h>
  16. #include <graphics/displayinfo.h>
  17. #include <graphics/regions.h>
  18. #include <graphics/gfx.h>
  19. #include <libraries/diskfont.h>
  20. #include <libraries/diskfonttag.h>
  21. #include <utility/tagitem.h>
  22. #include <clib/exec_protos.h>
  23. #include <clib/dos_protos.h>
  24. #include <clib/layers_protos.h>
  25. #include <clib/alib_stdio_protos.h>
  26. #include <clib/intuition_protos.h>
  27. #include <clib/graphics_protos.h>
  28. #include <clib/diskfont_protos.h>
  29.  
  30. #ifdef LATTICE
  31. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  32. int chkabort(void) { return(0); }
  33. #endif
  34.  
  35. UBYTE *vers = "\0$VER: cliptext 37.2";
  36.  
  37. #define BUFSIZE          4096
  38. #define FONT_NAME        0
  39. #define FONT_SIZE        1
  40. #define FILE_NAME        2
  41. #define JAM_MODE         3
  42. #define XASP             4
  43. #define YASP             5
  44. #define NUM_ARGS         6
  45. #define DEFAULTFONTSIZE  11L
  46. #define DEFAULTJAMMODE   0L
  47. #define DEFAULTXASP      0L
  48. #define DEFAULTYASP      0L
  49.  
  50. void MainLoop(void);
  51.  
  52. LONG args[NUM_ARGS];
  53. struct TagItem tagitem[2];
  54. UBYTE buffer[BUFSIZE];
  55. BPTR myfile;
  56. struct Library *DiskfontBase, *IntuitionBase, *LayersBase, *GfxBase;
  57. struct IntuiMessage *mymsg;
  58. struct DrawInfo *mydrawinfo;
  59. struct Window *mywin;
  60. struct RastPort *myrp;
  61. struct TTextAttr myta;
  62. struct TextFont *myfont;
  63. struct Rectangle myrectangle;
  64. struct Region *new_region;
  65.  
  66. void main(int argc, char **argv)
  67. {
  68.   struct RDArgs *myrda;
  69.   struct DisplayInfo mydi;
  70.   ULONG mymodeid;
  71.  
  72.   LONG mydefaultfontsize = DEFAULTFONTSIZE;
  73.   LONG mydefaultJAMMode = DEFAULTJAMMODE;
  74.   LONG mydefaultXASP = 0L;
  75.   LONG mydefaultYASP = 0L;
  76.   args[FONT_NAME] = (LONG)"topaz.font";
  77.   args[FONT_SIZE] = (LONG)&mydefaultfontsize;
  78.   args[FILE_NAME] = (LONG)"s:startup-sequence";
  79.   args[JAM_MODE]  = (LONG)&mydefaultJAMMode;
  80.   args[XASP]      = (LONG)&mydefaultXASP;
  81.   args[YASP]      = (LONG)&mydefaultYASP;
  82.  
  83.   /* dos.library standard command line parsing--See the dos.library Autodoc for details */
  84.   if (myrda = ReadArgs("FontName,FontSize/N,FileName,Jam/N,XASP/N,YASP/N\n", args, NULL))
  85.   {
  86.     if (myfile = Open((UBYTE *)args[FILE_NAME], MODE_OLDFILE) )   /* Open the file to display. */
  87.     {
  88.       if (DiskfontBase = OpenLibrary("diskfont.library", 37L))          /* Open the libraries. */
  89.       {
  90.         if (IntuitionBase = OpenLibrary("intuition.library", 37L))
  91.         {
  92.           if (GfxBase = OpenLibrary("graphics.library", 37L))
  93.           {
  94.             if (LayersBase = OpenLibrary("layers.library", 37L))
  95.             {
  96.               if (mywin = OpenWindowTags(NULL,                            /* Open that window. */
  97.                      WA_MinWidth,     100,     /* This application wants to hear about three   */
  98.                      WA_MinHeight,    100,     /* things: 1) When the user clicks the window's */
  99.                      WA_SmartRefresh, TRUE,    /* close gadget, 2) when the user starts to     */
  100.                      WA_SizeGadget,   TRUE,    /* resize the window, 3) and when the user has  */
  101.                      WA_CloseGadget,  TRUE,    /* finished resizing the window.                */
  102.                      WA_IDCMP,       IDCMP_CLOSEWINDOW | IDCMP_NEWSIZE | IDCMP_SIZEVERIFY,
  103.                      WA_DragBar,     TRUE,
  104.                      WA_DepthGadget, TRUE,
  105.                      WA_Title,       (ULONG)args[FILE_NAME],
  106.                      TAG_END))
  107.               {
  108.                 tagitem[0].ti_Tag = OT_DeviceDPI;
  109.  
  110.                 /* See if there is a non-zero value in the XASP or YASP fields. Diskfont.library */
  111.                 /* will get a divide by zero GURU if you give it a zero XDPI or YDPI value.      */
  112.  
  113.                 /* if there is a zero value in one of them... */
  114.                 if (  ( (*(ULONG *)args[XASP]) == 0) || ( (*(ULONG *)args[YASP]) == 0)  )
  115.                 {
  116.                   /* ...then use the aspect ratio of the current display as a default... */
  117.                   mymodeid = GetVPModeID(&(mywin->WScreen->ViewPort));
  118.                   if (GetDisplayInfoData( NULL, (UBYTE *)&mydi,
  119.                                           sizeof(struct DisplayInfo), DTAG_DISP, mymodeid))
  120.                   {
  121.                     mydefaultXASP = mydi.Resolution.x;
  122.                     mydefaultYASP = mydi.Resolution.y;
  123.                     printf("XAsp = %ld    YAsp = %ld\n", mydefaultXASP, mydefaultYASP);
  124.                     /* Notice that the X and Y get _swapped_ to keep the look of the    */
  125.                     /* font glyphs the same using screens with different aspect ratios. */
  126.                     args[YASP]    = (LONG)&mydefaultXASP;
  127.                     args[XASP]    = (LONG)&mydefaultYASP;
  128.                   }
  129.                   else /* ...unless something is preventing us from getting the screen  */
  130.                        /* screens resolution.  In that case, forget about the DPI tag.  */
  131.                        tagitem[0].ti_Tag = TAG_END;
  132.                 }
  133.                 /* Here we have to put the X and Y DPI into the OT_DeviceDPI tags data field.  */
  134.                 /* THESE ARE NOT REAL X AND Y DPI VALUES FOR THIS FONT OR DISPLAY. They only   */
  135.                 /* serve to supply the diskfont.library with values to calculate the aspect    */
  136.                 /* ratio.  The X value gets stored in the upper word of the tag value and the Y*/
  137.                 /* DPI gets stored in the lower word.  Because ReadArgs() stores the _address_ */
  138.                 /* of integers it gets from the command line, you have to dereference the      */
  139.                 /* pointer it puts into the argument array, which results in some ugly casting.*/
  140.  
  141.                 tagitem[0].ti_Data = (ULONG)( ( (UWORD) *( (ULONG *)args[XASP] ) << 16) |
  142.                                                ((UWORD) *( (ULONG *)args[YASP]) ) );
  143.                 tagitem[1].ti_Tag = TAG_END;
  144.  
  145.                 myta.tta_Name = (STRPTR)args[FONT_NAME];           /* Set up the TTextAttr     */
  146.                 myta.tta_YSize = *((LONG *)args[FONT_SIZE]);       /* structure to match the   */
  147.                 myta.tta_Style = FSF_TAGGED;                       /* font the user requested. */
  148.                 myta.tta_Flags = 0L;
  149.                 myta.tta_Tags = tagitem;
  150.  
  151.                 if (myfont = OpenDiskFont(&myta))       /* open that font */
  152.                 {
  153.                  /* This is for the layers.library clipping region that gets attached to the   */
  154.                  /* window.  This prevents the application from unnecessarily rendering beyond */
  155.                   myrectangle.MinX = mywin->BorderLeft; /* the bounds of the inner part of     */
  156.                   myrectangle.MinY = mywin->BorderTop;  /* the window.  For now, you can       */
  157.                   myrectangle.MaxX = mywin->Width -     /* ignore the layers stuff if you are  */
  158.                       (mywin->BorderRight + 1);         /* just interested in learning about   */
  159.                   myrectangle.MaxY = mywin->Height -    /* using text.  For more information   */
  160.                       (mywin->BorderBottom + 1);        /* on clipping regions and layers, see */
  161.                                                         /* the Layers chapter of this manual.  */
  162.  
  163.                   if (new_region = NewRegion())                           /* more layers stuff */
  164.                   {
  165.                     if (OrRectRegion(new_region, &myrectangle));     /* Even more layers stuff */
  166.                     {
  167.                       InstallClipRegion(mywin->WLayer, new_region);
  168.                       /* Obtain a pointer to the window's rastport and set up some of the      */
  169.                       myrp = mywin->RPort;    /* rastport attributes. This example obtains the */
  170.                       SetFont(myrp, myfont);  /* text pen for the window's screen using        */
  171.                       if (mydrawinfo =GetScreenDrawInfo(mywin->WScreen)) /* GetScreenDrawInfo()*/
  172.                       {
  173.                         SetAPen(myrp, mydrawinfo->dri_Pens[TEXTPEN]);
  174.                         FreeScreenDrawInfo(mywin->WScreen, mydrawinfo);
  175.                       }
  176.                       SetDrMd(myrp, (BYTE)(*((LONG *)args[JAM_MODE])));
  177.  
  178.                       MainLoop();
  179.                     }
  180.                     DisposeRegion(new_region);
  181.                   }
  182.                   CloseFont(myfont);
  183.                 }
  184.                 CloseWindow(mywin);
  185.               }
  186.               CloseLibrary(LayersBase);
  187.             }
  188.             CloseLibrary(GfxBase);
  189.           }
  190.           CloseLibrary(IntuitionBase);
  191.         }
  192.         CloseLibrary(DiskfontBase);
  193.       }
  194.       Close(myfile);
  195.     }
  196.     FreeArgs(myrda);
  197.   }
  198.   else VPrintf("Error parsing arguments\n", NULL);
  199. }
  200.  
  201.  
  202. void MainLoop(void)
  203. {
  204.     LONG count, actual, position;
  205.     BOOL aok = TRUE, waitfornewsize = FALSE;
  206.     struct Task *mytask;
  207.  
  208.     mytask = FindTask(NULL);
  209.     Move(myrp, mywin->BorderLeft + 1, mywin->BorderTop + myfont->tf_YSize + 1);
  210.  
  211.     while (((actual = Read(myfile, buffer, BUFSIZE)) > 0) && aok) /* While there's something   */
  212.     {                                                             /* to read, fill the buffer. */
  213.         position = 0;
  214.         count = 0;
  215.  
  216.         while (position <= actual)
  217.         {
  218.            if (!(waitfornewsize))
  219.            {
  220.                while ( ((buffer[count] >= myfont->tf_LoChar) &&
  221.                        (buffer[count] <= myfont->tf_HiChar)) && (count <= actual) )
  222.                    count++;
  223.  
  224.                Text(myrp, &(buffer[position]), (count)-position);
  225.  
  226.                while ( ((buffer[count] < myfont->tf_LoChar) ||
  227.                        (buffer[count] > myfont->tf_HiChar)) && (count <= actual) )
  228.                {
  229.                    if (buffer[count] == 0x0A)
  230.                        Move(myrp, mywin->BorderLeft, myrp->cp_y + myfont->tf_YSize + 1);
  231.                    count++;
  232.                }
  233.                position = count;
  234.            }
  235.            else WaitPort(mywin->UserPort);
  236.  
  237.            while (mymsg = (struct IntuiMessage *)GetMsg(mywin->UserPort))
  238.            {
  239.                if (mymsg->Class == IDCMP_CLOSEWINDOW)     /* The user clicked the close gadget */
  240.                {
  241.                    aok = FALSE;
  242.                    position = actual + 1;
  243.                    ReplyMsg((struct Message *)mymsg);
  244.                }                                                     /* The user picked up the */
  245.                else if (mymsg->Class == IDCMP_SIZEVERIFY)            /* window's sizing gadget */
  246.                {
  247.                   /* When the user has picked up the window's sizing gadget when the           */
  248.                   /* IDCMP_SIZEVERIFY flag is set, the application has to reply to this message*/
  249.                   /* to tell Intuition to allow the user to move the sizing gadget and resize  */
  250.                   /* the window.  The reason for using this here is because the user can resize*/
  251.                   /* the window while cliptext.c is rendering text to the window. Cliptext.c   */
  252.                   /* has to stop rendering text when it receives an IDCMP_SIZEVERIFY message.  */
  253.                   /*                                                                           */
  254.                   /* if this example had instead asked to hear about IDCMP events that could   */
  255.                   /* take place between SIZEVERIFY and NEWSIZE events (especially INTUITICKS), */
  256.                   /* it should turn off those events here using ModifyIDCMP().                 */
  257.                   /*                                                                           */
  258.                   /* After we allow the user to resize the window, we cannot write into the    */
  259.                   /* window until the user has finished resizing it because we need the        */
  260.                   /* window's new size to adjust the clipping area.  Specifically, we have     */
  261.                   /* to wait for an IDCMP_NEWSIZE message which Intuition will send when the   */
  262.                   /* user lets go of the resize gadget.  For now, we set the waitfornewsize    */
  263.                   /* flag to stop rendering until we get that NEWSIZE message.                 */
  264.  
  265.                    waitfornewsize = TRUE;
  266.                    WaitBlit();
  267.  
  268.                    ReplyMsg((struct Message *)mymsg);            /* The blitter is done, let the */
  269.                }                                                 /* user resize the window.      */
  270.                else
  271.                {
  272.                    ReplyMsg((struct Message *)mymsg);
  273.                    waitfornewsize = FALSE;
  274.                           /* The user has resized the window, so get the new window dimensions */
  275.                    myrectangle.MinX = mywin->BorderLeft;      /* and readjust the layers       */
  276.                    myrectangle.MinY = mywin->BorderTop;       /* clipping region accordingly.  */
  277.                    myrectangle.MaxX = mywin->Width - (mywin->BorderRight + 1);
  278.                    myrectangle.MaxY = mywin->Height - (mywin->BorderBottom + 1);
  279.                    InstallClipRegion(mywin->WLayer, NULL);
  280.                    ClearRegion(new_region);
  281.                    if (OrRectRegion(new_region, &myrectangle))
  282.                        InstallClipRegion(mywin->WLayer, new_region);
  283.                    else
  284.                    {
  285.                        aok = FALSE;
  286.                        position = actual + 1;
  287.                    }
  288.                }
  289.            }
  290.            if (mytask->tc_SigRecvd & SIGBREAKF_CTRL_C)              /* Check for user break.   */
  291.            {
  292.                aok = FALSE;
  293.                position = actual + 1;
  294.            }
  295.  
  296.            if (myrp->cp_y > (mywin->Height - (mywin->BorderBottom + 2))) /* if we reached the  */
  297.            {                                               /* bottom of the page, clear the    */
  298.                Delay(25);                                  /* rastport and move back to the top*/
  299.  
  300.                SetRast(myrp, 0);      /* Set the entire rastport to color zero.  This will not */
  301.                Move(myrp,             /* the window borders because of the layers clipping.    */
  302.                mywin->BorderLeft + 1,
  303.                mywin->BorderTop + myfont->tf_YSize + 1);
  304.             }
  305.         }
  306.     }
  307.     if (actual < 0) VPrintf("Error while reading\n", NULL);
  308. }
  309.