home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 6 / FreshFish_September1994.bin / new / dev / c / hce / examples / amiga / pools / gfx.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  13KB  |  426 lines

  1. /*
  2.  * Copyright (c) 1994. Author: Jason Petty.
  3.  *
  4.  * Permission is granted to anyone to use this software for any purpose
  5.  * on any computer system, and to redistribute it freely, with the
  6.  * following restrictions:
  7.  * 1) No charge may be made other than reasonable charges for reproduction.
  8.  * 2) Modified versions must be clearly marked as such.
  9.  * 3) The authors are not responsible for any harmful consequences
  10.  *    of using this software, even if they result from defects in it.
  11.  *
  12.  *
  13.  *     Gfx.c:
  14.  *
  15.  *            Opens libraries/screen/window and printer device.
  16.  *            Does all graphics related stuff.
  17.  *            Does Help messages.
  18.  *            Does all Printer related stuff.
  19.  */
  20.  
  21. #include <exec/types.h>
  22. #include <exec/errors.h>
  23. #include <exec/memory.h>
  24. #include <exec/io.h>
  25. #include <libraries/dos.h>
  26. #include <devices/printer.h>
  27.  
  28. #ifndef  GRAPHICS_GFXBASE_H
  29. #include <graphics/gfxbase.h>
  30. #endif
  31.  
  32. #ifndef  INTUITION_INTUITION_H
  33. #include <intuition/intuition.h>
  34. #endif
  35.  
  36. #include <graphics/gfxmacros.h>
  37.  
  38. #ifndef STDIO_H
  39. #include <clib/stdio.h>
  40. #endif
  41.  
  42. #ifndef STRING_H
  43. #include <clib/string.h>
  44. #endif
  45.  
  46. #include "pools.h"
  47.  
  48.    /**** All Structure definitions start Here *****/
  49.  
  50. chip struct NewScreen my_new_screen=
  51. {
  52.   0,            /* LeftEdge */
  53.   0,            /* TopEdge  */
  54.   641,
  55.   266,
  56.   4,            /* 4 = 16 colours.   */
  57.   1,            /* DetailPen Text should be printed with colour reg. 1   */
  58.   5,            /* BlockPen  Blocks should be printed with colour reg. 0 */
  59.   HIRES,        /* ViewModes.                            */
  60.   CUSTOMSCREEN|SCREENQUIET, /* Type. Your own customized screen. */
  61.   NULL,         /* Font      Default font.               */
  62.   NULL,         /* UBYTE Pools-V1.0.  Title.               */
  63.   NULL,         /* Gadget                                */
  64.   NULL          /* BitMap    No special CustomBitMap.    */
  65. };
  66.  
  67. /* Covers hole of screen and is used for graphics. */
  68. struct NewWindow gfx_new_win=
  69. {
  70.   0,1,
  71.   641, 259,                             /* Use screens sizes.   */
  72.   1,0,                          /* Detailpen, Blockpen. */
  73.   IDCMP_CLOSEWINDOW|IDCMP_CLOSEWINDOW|IDCMP_GADGETDOWN|IDCMP_GADGETUP,
  74.   WFLG_CLOSEGADGET|WFLG_ACTIVATE|WFLG_GIMMEZEROZERO|WFLG_BORDERLESS,
  75.   NULL,                                 /* First gadget.        */
  76.   NULL,                                 /* Image checkmark.     */
  77.   NULL,                                 /* Title.               */
  78.   NULL,                                 /* Screen pointer.      */
  79.   NULL,                                 /* Custom Bitmap.       */
  80.   120,88,                         /* Min W,H sizes.       */
  81.   641,259,                         /* Max W,H sizes.       */
  82.   CUSTOMSCREEN                          /* Type.                */
  83. };
  84.  
  85. /* This is a general purpose shared window. */
  86. struct NewWindow g_new_window=
  87. {
  88.   0,             /* LeftEdge   */
  89.   1,             /* TopEdge    */
  90.   640,           /* Width      */
  91.   200,           /* Height     */
  92.   2,3,           /* DetailPen,BlockPen */
  93.   IDCMP_CLOSEWINDOW|IDCMP_GADGETDOWN|IDCMP_GADGETUP,
  94.   WFLG_SMART_REFRESH|WFLG_CLOSEGADGET|WFLG_ACTIVATE,
  95.   NULL,          /* FirstGadget */
  96.   NULL,          /* CheckMark   */
  97.   NULL,          /* Title.      */
  98.   NULL,          /* Screen      */
  99.   NULL,          /* BitMap      */
  100.   20,            /* MinWidth    */
  101.   20,            /* MinHeight   */
  102.   641,           /* MaxWidth    */
  103.   259,           /* MaxHeight   */
  104.   CUSTOMSCREEN   /* Type        */
  105. };
  106.  
  107. union printerIO             /* Printer Request Block. */
  108. {
  109.   struct IOStdReq ios;
  110.   struct IODRPReq iodrp;
  111.   struct IOPrtCmdReq iopc;
  112. };
  113.  
  114.  
  115. union printerIO *prt_req=0;
  116. struct MsgPort *prt_reply=0;
  117. UWORD prt_error=TRUE;             /* Printer Device error. */
  118. extern struct Gadget *sl_gadlist; /* Head of 'choose league' Gadget list.*/
  119. struct Window *g_window=0;        /* (Shared Window)   */
  120. struct Window *gfx_window=0;      /* (graphics window) */
  121. struct Screen *my_screen=0;
  122. struct GfxBase *GfxBase=0;
  123. long IntuitionBase=0;
  124. long GadToolsBase=0;
  125.  
  126.   /**** Global Variables Here ****/
  127.  
  128. static WORD font_width, font_height;
  129. char PR_BUF[90]; /* General purpose buffer. */
  130.  
  131. /********************** Rest of code = FUNCTIONS *********************/
  132.  
  133.  /* Start() must be called at beginning; returns window pointer - if 0,
  134.   * program has failed ... everything already cleaned up here, main ()
  135.   * must do its own cleaning up then exit */
  136.  
  137. struct Window *start ()
  138. {
  139.   /* Libraries. */
  140.  if (!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0)))
  141.   goto FAILED;
  142.  if (!(IntuitionBase=(long)OpenLibrary("intuition.library",0)))
  143.   goto FAILED;
  144.         if (!(GadToolsBase=(long)OpenLibrary("gadtools.library", 36))) {
  145.        /*  printf("Could not Open gadtools.library - V36 or Higher!!\n"); */
  146.                 goto FAILED;
  147.                 }
  148.  
  149.    /* Screen */
  150.         if (!(my_screen = (struct Screen *)OpenScreen( &my_new_screen)))
  151.                 goto FAILED;
  152.  
  153.             g_new_window.Screen=my_screen;  /* Attach g Win to Screen.  */
  154.             gfx_new_win.Screen=my_screen;   /* Attach gfx Win to Screen.*/
  155.  
  156.     /* GadTools */
  157.         if (!(Alloc_VisualInfoA())) /* Get Visual info for gad tools. */
  158.                  goto FAILED;
  159.         if (!(Alloc_L_Gadgets()))   /* Alloc, League Gadgets */
  160.                  goto FAILED;
  161.  
  162.             gfx_new_win.FirstGadget = (struct Gadget *)sl_gadlist;
  163.  
  164.     /* Gfx Window. */
  165.        if (!(gfx_window = (struct Window *)OpenWindow(&gfx_new_win))) {
  166.               /* printf("No gfx_window!!\n"); */
  167.                  goto FAILED;
  168.         }
  169.     /* Set up 'my_screen' and 'gfx_window' Graphics. */
  170.         (void)Set_Graphics();
  171.  
  172.        font_width=GfxBase->DefaultFont->tf_XSize; /* May need these. */
  173.        font_height=GfxBase->DefaultFont->tf_YSize;
  174.  
  175.    /* Printer stuff (note: actual printer device only opened when needed) */
  176.         if (!(prt_reply = (struct MsgPort *)CreatePort( NULL, 0))) {
  177.                /* printf("Could not Open - Printer reply port!!\n"); */
  178.                 goto FAILED;
  179.                 }
  180.         if (!(prt_req = (union printerIO *) 
  181.                 CreateExtIO(prt_reply, sizeof(union printerIO)))) {
  182.              /* printf("Could not Create - Printer ExtIO!!\n");  */
  183.                 goto FAILED;
  184.                 }
  185.  
  186.         return (gfx_window);
  187.  
  188. FAILED:
  189.         finish ();
  190.         return (0L);
  191. }
  192.  
  193.  /* Call finish() at end, if start() was successful. */
  194. void finish ()
  195. {
  196.         if (!prt_error)                 /* Close printer device. */
  197.                 CloseDevice( prt_req );
  198.         if (prt_req)
  199.                 DeleteExtIO( prt_req, sizeof(union printerIO) );
  200.         if (prt_reply)
  201.                 DeletePort( prt_reply);
  202.         if (gfx_window)
  203.             CloseWindow (gfx_window);
  204.  
  205.          Close_GWind();      /* Close g window. (Tests for opened).      */
  206.          Free_GT_Gadgets();  /* Free gad memory. (Tests if allocated).   */
  207.          Free_VisualInfo();  /* Free visual memory. (Tests if allocated).*/
  208.  
  209.         if (my_screen)
  210.                 CloseScreen (my_screen);
  211.         if (GadToolsBase)
  212.                 CloseLibrary(GadToolsBase);
  213.  if (GfxBase)
  214.   CloseLibrary (GfxBase);
  215.  if (IntuitionBase)
  216.   CloseLibrary (IntuitionBase);
  217. }
  218.  
  219. /********** START OF PRINTER FUNCTIONS ******************/
  220.  
  221. void PrtError(error) /* Prints the appropriate printer error message.*/
  222. BYTE error;          /* (g_window) result box */
  223. {
  224.   Clear_RBOX();
  225.  
  226.   switch( error ) /* Errors found in 'exec/errors.h' & 'devices/printer.h' */
  227.   {
  228.   case IOERR_ABORTED:
  229.           RB_Msg("ERROR: The printer request was aborted!",0);
  230.           break;
  231.   case IOERR_NOCMD:
  232.           RB_Msg("ERROR: Unknown printer command was sent!",0);
  233.           break;
  234.   case IOERR_BADLENGTH:
  235.           RB_Msg("ERROR: Bad length in the printer CMD - DATA!",0);
  236.           break;
  237.   case PDERR_CANCEL:
  238.           RB_Msg("All Printing Cancelled!",0);
  239.           break;
  240.   case PDERR_NOTGRAPHICS:
  241.           RB_Msg("ERROR: Printer doesn`t support Graphics!.",0);
  242.           break;
  243.   case PDERR_BADDIMENSION:
  244.           RB_Msg("ERROR: Printer dimension is not valid!",0);
  245.           break;
  246.   case PDERR_INTERNALMEMORY:
  247.           RB_Msg("No memory for internal printer functions!",0);
  248.