home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377b.lha / devices / printer / wd / windowdump.c next >
Encoding:
C/C++ Source or Header  |  1980-02-03  |  8.4 KB  |  287 lines

  1. /* Copyright (c) 1990 Commodore-Amiga, Inc.
  2.  *
  3.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  4.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  5.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  6.  * information on the correct usage of the techniques and operating system
  7.  * functions presented in this example.  The source and executable code of
  8.  * this example may only be distributed in free electronic form, via bulletin
  9.  * board or as part of a fully non-commercial and freely redistributable
  10.  * diskette.  Both the source and executable code (including comments) must
  11.  * be included, without modification, in any copy.  This example may not be
  12.  * published in printed form or distributed with any commercial product.
  13.  * However, the programming techniques and support routines set forth in
  14.  * this example may be used in the development of original executable
  15.  * software products for Commodore Amiga computers.
  16.  * All other rights reserved.
  17.  * This example is provided "as-is" and is subject to change; no warranties
  18.  * are made.  All use is at your own risk.  No liability or responsibility
  19.  * is assumed.
  20.  */
  21.  
  22. /* WindowDump.c 10/89 EC
  23.  * Compiled with Lattice C 5.04: LC -b1 -cfist -v -y 
  24.  * Linkage: lib:c.o+WindowDump.o to WindowDump Library lib:lc.lib+lib:amiga.lib
  25.  */
  26.  
  27. #include <exec/types.h>
  28. #include <devices/printer.h>
  29. #include <exec/io.h>
  30. #include <graphics/display.h>
  31. #include <graphics/gfxbase.h>
  32. #include <graphics/rastport.h>
  33. #include <graphics/view.h>
  34. #include <intuition/intuition.h>
  35. #include <libraries/dos.h>
  36. #include <libraries/dosextens.h>
  37.  
  38. #ifdef LATTICE
  39. #include <proto/all.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #endif
  43.  
  44. void main(void);
  45. union printerIO *CreatePrtReq(void);
  46. void DeletePrtReq(union printerIO *);
  47. int OpenPrinter (union printerIO *);
  48. void ClosePrinter(union printerIO *);
  49. void DumpRPort(union printerIO *, struct RastPort *, struct ColorMap *,
  50.                       ULONG, UWORD, UWORD, UWORD, UWORD, LONG, LONG, UWORD);
  51. void openLibraries(void);
  52. struct Window *doWindow(SHORT, SHORT);
  53. void cleanexit(UBYTE *);
  54.  
  55. union printerIO {
  56.     struct IOStdReq    ios;
  57.     struct IODRPReq    iodrp;
  58.     struct IOPrtCmdReq iopc;
  59. };
  60.  
  61. static UBYTE *prtErrorText[] = {" EVERYTHING'S FINE", 
  62.                                 " USER CANCELED DUMP", 
  63.                                 " NOT A GRAPHICS PRINTER",
  64.                                 " SHOULDN'T GET THIS ONE",
  65.                                 " ILLEGAL DIMENSIONS", 
  66.                                 " SHOULDN'T GET THIS ONE", 
  67.                                 " NO MEMORY FOR VARIABLES", 
  68.                                 " NO MEMORY FOR BUFFER"};
  69.  
  70. struct IntuitionBase *IntuitionBase = NULL;
  71. struct GfxBase *GfxBase = NULL;
  72. struct Window *window;
  73. union printerIO *printerReq;
  74. BOOL PDOPEN = FALSE;
  75.  
  76. void
  77. main()
  78. {
  79.     struct IntuiMessage *msg;
  80.     struct MsgPort *port;
  81.     struct RastPort *rp;
  82.     struct ViewPort *vp;
  83.  
  84.     ULONG usersig, printersig, signal;
  85.     BOOL P_ABORT = FALSE;
  86.     BOOL U_ABORT = FALSE;
  87.     
  88.     openLibraries();
  89.     window = doWindow(320, 100);
  90.  
  91.     if (!(printerReq = CreatePrtReq()))
  92.         cleanexit("Can't create printer request");
  93.  
  94.     if (OpenPrinter(printerReq))
  95.         cleanexit("Can't open printer device\n");
  96.  
  97.     PDOPEN = TRUE;
  98.  
  99.     port = printerReq->ios.io_Message.mn_ReplyPort;
  100.  
  101.     usersig = 1 << window->UserPort->mp_SigBit;
  102.     printersig = 1 << port->mp_SigBit;
  103.  
  104.     rp = window->RPort;
  105.     vp = &window->WScreen->ViewPort;
  106.  
  107.     DumpRPort(printerReq, rp, vp->ColorMap, vp->Modes,
  108.               (UWORD)window->BorderLeft, (UWORD)window->BorderTop,
  109.               (UWORD)(window->Width - window->BorderRight - window->BorderLeft),
  110.               (UWORD)(window->Height - window->BorderBottom - window->BorderTop),
  111.               0L, 0L,
  112.               SPECIAL_ASPECT|SPECIAL_FULLROWS|SPECIAL_TRUSTME);
  113.  
  114.     FOREVER {
  115.         signal = Wait(usersig | printersig);
  116.         if (signal & usersig){ 
  117.             while(msg = (struct IntuiMessage *)GetMsg(window->UserPort)) {
  118.                 if(msg->Class == CLOSEWINDOW) U_ABORT = TRUE;
  119.                 ReplyMsg((struct Message *)msg);
  120.             }
  121.         }
  122.         if (signal & printersig) {
  123.             if (printerReq->iodrp.io_Error != 0){
  124.                   SetWindowTitles(window, 
  125.                                   prtErrorText[printerReq->iodrp.io_Error], NULL);
  126.                 Delay(150);
  127.         }
  128.             P_ABORT = TRUE;
  129.             while((struct MsgPort *)GetMsg(port));
  130.         }
  131.  
  132.         if (U_ABORT) {
  133.             AbortIO((struct IORequest *)printerReq);
  134.             WaitIO((struct IORequest *)printerReq);    
  135.     }
  136.         if (P_ABORT | U_ABORT)
  137.            cleanexit("");
  138.     }
  139. }
  140.  
  141. /* open and fill a window on the Workbench screen */
  142.  
  143. struct Window *doWindow(width, height)
  144. SHORT width, height;
  145. {
  146.  
  147.     struct NewWindow nw = {
  148.         0,0,0,0,0,1,CLOSEWINDOW,ACTIVATE|WINDOWCLOSE|WINDOWDEPTH|SMART_REFRESH|
  149.         WINDOWDRAG|RMBTRAP,NULL,NULL,"<- Close To Abort Dump",
  150.         NULL,NULL,-1,-1,-1,-1,WBENCHSCREEN
  151.     };
  152.  
  153.     struct RastPort *rp;
  154.     struct Window   *wn;
  155.     register COUNT n;
  156.     register SHORT mc, mr;
  157.  
  158.     nw.Height = height;
  159.     nw.Width  = width;
  160.  
  161.     if(!(wn = (struct Window *)OpenWindow(&nw)))
  162.         cleanexit("Can't open window\n");
  163.     
  164.     rp = wn->RPort;
  165.     mc = wn->Width - (wn->BorderRight+1);
  166.     mr = wn->Height - (wn->BorderBottom+1);
  167.  
  168.     SetDrMd(rp,0);
  169.     SetAPen(rp, 1);
  170.     RectFill(rp, wn->BorderLeft, wn->BorderTop, mc, mr);
  171.     for (n = wn->BorderLeft; n < mc; n+=4) {
  172.         Move(rp, (mc / 2) + wn->BorderLeft + 1, (mr / 2) + wn->BorderBottom + 1);
  173.         SetAPen(rp, 0);
  174.         Draw(rp, n, wn->BorderTop);
  175.         Move(rp, (mc / 2) + wn->BorderLeft + 1, (mr / 2) + wn->BorderBottom + 1);
  176.         SetAPen(rp, 3);
  177.         Draw(rp, n, mr);
  178.     }
  179.     for (n = wn->BorderTop; n < mr; n+=4) {
  180.         Move(rp, (mc / 2) + wn->BorderLeft + 1, (mr / 2) + wn->BorderBottom + 1);
  181.         SetAPen(rp, 2);
  182.         Draw(rp, wn->BorderLeft, n);
  183.         Move(rp, (mc / 2) + wn->BorderLeft + 1, (mr / 2) + wn->BorderBottom + 1);
  184.         Draw(rp, mc, n);
  185.     }
  186.  
  187.     return(wn);
  188. }
  189.  
  190.  
  191. void openLibraries()
  192. {
  193.     if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",33)))
  194.         cleanexit("Can't open graphics.lib\n");
  195.     if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 33)))
  196.         cleanexit("Can't open intuition.lib\n");
  197. }
  198.  
  199. void cleanexit(s)
  200. UBYTE *s;
  201. {
  202.     struct IntuiMessage *msg;
  203.     SHORT c;
  204.  
  205.     if (PDOPEN)
  206.         CloseDevice((struct IORequest *)printerReq);
  207.  
  208.     if (printerReq)
  209.         DeletePrtReq(printerReq);
  210.  
  211.     if(window) {
  212.         while(msg = (struct IntuiMessage *)GetMsg(window->UserPort))
  213.             ReplyMsg((struct Message *)msg);
  214.         CloseWindow(window);
  215.     }
  216.  
  217.     if(GfxBase) CloseLibrary((struct Library *)GfxBase);
  218.     if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  219.  
  220.     if (*s) {
  221.         printf("%s\n", s);
  222.         printf("<<< Hit Return to continue >>>\n"); /* just in case we're */
  223.         c = getchar();                              /* run from Workbench */
  224.         exit(RETURN_FAIL);
  225.     }
  226.     exit(RETURN_OK);
  227. }
  228.  
  229.  
  230. /* the printer i/o routines we use */
  231.  
  232. union printerIO *CreatePrtReq()
  233. {
  234.     struct MsgPort *prtport;
  235.     union printerIO *request;
  236.  
  237.     if (!(prtport = CreatePort(NULL,0)))
  238.         return(0);
  239.     if (!(request = (union printerIO *)CreateExtIO(prtport, 
  240.                         sizeof(union printerIO)))){
  241.         DeletePort(prtport);
  242.         return(0);
  243.     }
  244.     return(request);
  245. }
  246.  
  247. void DeletePrtReq(request)
  248. union printerIO *request;
  249. {
  250.     struct MsgPort *prtport;
  251.  
  252.     prtport = request->ios.io_Message.mn_ReplyPort;
  253.     DeleteExtIO((struct IORequest *)request);
  254.     DeletePort(prtport);
  255. }
  256.     
  257. int
  258. OpenPrinter(request)
  259. union printerIO *request;
  260. {
  261.     return((int)OpenDevice("printer.device",0,(struct IORequest *)request,0));
  262. }
  263.  
  264. void
  265. DumpRPort(request,rastPort, colorMap, modes, sx,sy, sw,sh, dc,dr, s)
  266. union printerIO *request;
  267. struct RastPort *rastPort;
  268. struct ColorMap *colorMap;
  269. ULONG modes;
  270. UWORD sx, sy, sw, sh;
  271. LONG dc, dr;
  272. UWORD s;
  273. {
  274.     request->iodrp.io_Command = PRD_DUMPRPORT;
  275.     request->iodrp.io_RastPort = rastPort;
  276.     request->iodrp.io_ColorMap = colorMap;
  277.     request->iodrp.io_Modes = modes;
  278.     request->iodrp.io_SrcX = sx;
  279.     request->iodrp.io_SrcY = sy;
  280.     request->iodrp.io_SrcWidth = sw;
  281.     request->iodrp.io_SrcHeight = sh;
  282.     request->iodrp.io_DestCols = dc;
  283.     request->iodrp.io_DestRows = dr;
  284.     request->iodrp.io_Special = s;
  285.     SendIO((struct IORequest *)request);
  286. }
  287.