home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Telnet 2.7b5 / source / ICR / vrrgmac.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-18  |  9.2 KB  |  400 lines  |  [TEXT/CWIE]

  1. /*
  2.  *
  3.  *      Virtual Graphics Kernel Macintosh Real Graphics Interface
  4.  *                          (rgmac.c)
  5.  *
  6.  *   National Center for Supercomputing Applications
  7.  *      by Gaige B. Paulsen
  8.  *
  9.  *    This file contains the macintosh real screen calls for the NCSA
  10.  *  Virtual Graphics Kernel.
  11.  *
  12.  *    Following are the Per Device calls:
  13.  *
  14.  *   MacRGraster( p,x1,y1,x2,y2,wid)- Plot raster in rect @(x1,y1,x2,y2) with wid @ p
  15.  *   MacRGcopy( x1,y1,x2,y2,x3,y3,x4,y4)- 
  16.  *   MacRGmap( offset,count,data)   - 
  17.  *
  18.  *
  19.  *  WARNING, WARNING!
  20.  *  Gaige has this cute idea about how to do "subwindows" of real windows by shifting
  21.  *  the window number by 4 bits (MAC_WINDOW_SHIFT).  Then, the remainder is the
  22.  *  sub-window number.  It will probably work, but you MUST keep the shifted and
  23.  *  non-shifted numbers straight.  For example, MacRGdestroy() and MacRGremove() take
  24.  *  different uses of the window number right now.  
  25.  *
  26.  *
  27.  *  Macintosh only Routines:
  28.  *
  29.  *      Version Date    Notes
  30.  *      ------- ------  ---------------------------------------------------
  31.  *        0.5     880912    Initial Coding -GBP
  32.  *      1.0     890216  Minor fixes for 1.0 - TKK
  33.  */
  34.  
  35. #ifdef MPW
  36. #pragma segment ICR
  37. #endif
  38.  
  39.  
  40. #define __ALLNU__
  41.  
  42.  
  43. #include "maclook.proto.h"
  44. #include "vdevice.h"
  45. #include "vdevice.proto.h"
  46. #include "vr.h"
  47. #include "InternalEvents.h"
  48.  
  49. #define MAX_MAC_RGS    8
  50. #define MAX_MAC_SUB 16
  51. #define MAC_WINDOW_SHIFT    4        /* Bits shifted */
  52. #define    MAC_SUB_MASK    0xf            /* Bits maksed */
  53.  
  54. #include "vrrgmac.proto.h"
  55. #include "errors.proto.h"
  56. #include "telneterrors.h"
  57.  
  58.  
  59.  
  60. typedef struct MacWindow {
  61.     VDevice            vdev;            /* virtual device to draw in, has its own colors */
  62.     WindowPtr         window;            /* My Window  (0L if not in use ) */
  63.     PaletteHandle     palette;        /* My Palette */
  64.     char            title[256];        /* Title string */
  65.     
  66.     
  67.     Point            size;            /* My height and width */
  68.     Rect            subs[MAX_MAC_SUB];/* Rectangles of my subwindows [0,0,0,0] if not in use */
  69.     } MacWindow;
  70.  
  71. MacWindow *MacRGs;
  72.  
  73. short RGwn=0;                            /* Window number in use */
  74. short RGsub=0;                        /* Sub-Window Number in use */
  75.  
  76. void MacRGinit(void)
  77. {
  78.     short i;
  79.     
  80.     MacRGs= (MacWindow *)myNewPtr( MAX_MAC_RGS * sizeof(MacWindow));
  81.     for (i=0;i<MAX_MAC_RGS;i++)
  82.         MacRGs[i].window=NULL;
  83. }
  84.  
  85. /************************************************************
  86.  * MacRGnewwindow( name, x1, y1, x2, y2)        -            *
  87.  *        make a new subwindow to wn                            *
  88.  ************************************************************/
  89.  
  90. short MacRGnewwindow
  91.   (
  92.     char *name,
  93.     short x1,
  94.     short y1,
  95.     short x2,
  96.     short y2
  97.   )
  98. {
  99.     short w,i ;
  100.     Rect wDims;
  101.     RGBColor curcol;
  102.  
  103.     
  104.     for (w=0; w<MAX_MAC_RGS && MacRGs[w].window; w++)
  105.         /* loop */;
  106.     
  107.     if (w>= MAX_MAC_RGS)
  108.         return( -1);
  109.         
  110.     if ((x2 - x1) & 1)                        /* odd width, must be even */
  111.         x2++;
  112.     
  113.     SetRect( &wDims,  x1+40, y1+40, x2+40, y2+40);
  114.         
  115.     strcpy( MacRGs[w].title, name);                        /* Copy of the name */
  116.     
  117.     if (!TelInfo->haveColorQuickDraw)        /* Borrow from RS */
  118.         return(-1);
  119.     else 
  120.     {
  121.         short i;
  122.         Str255    scratchPstring;
  123.         strcpy((char *)scratchPstring, name);
  124.         CtoPstr((char *)scratchPstring);
  125.         
  126.         MacRGs[w].window=NewCWindow(NULL, &wDims, scratchPstring, TRUE, noGrowDocProc, kInFront,    /* BYU LSC */
  127.             TRUE, (long) w);
  128.         
  129.         if (!MacRGs[w].window)
  130.         {
  131.             DoError(108 | MEMORY_ERRORCLASS, LEVEL2, NULL);        
  132.             return(-1);
  133.         }
  134.         MacRGs[w].vdev.bounds = &MacRGs[w].window->portRect;
  135.  
  136.         
  137.         MacRGs[w].palette = NewPalette( 256, NULL, pmTolerant, 0);
  138.         if (!MacRGs[w].palette)
  139.         {
  140.             DisposeWindow(MacRGs[w].window);
  141.             DoError(108 | MEMORY_ERRORCLASS, LEVEL2, NULL);        
  142.             return(-1);
  143.         }
  144.         for (i=0; i<256; i++) {                /* load with grey-scale */
  145.             curcol.red = i<<8;
  146.             curcol.green = i<<8;
  147.             curcol.blue = i<<8;
  148.             SetEntryColor( MacRGs[w].palette, i, &curcol);
  149.         }
  150.         if (InitVDevice(&MacRGs[w].vdev,MacRGs[w].palette))        /* get vdevice going */
  151.         {
  152.             DisposePalette(MacRGs[w].palette);
  153.             DisposeWindow(MacRGs[w].window);
  154.             DoError(108 | MEMORY_ERRORCLASS, LEVEL2, NULL);        
  155.             return(-1);
  156.         }
  157.     }
  158.         
  159.     ((WindowPeek)MacRGs[w].window)->windowKind = WIN_ICRG;    
  160.     MacRGs[w].size.h = x2-x1;
  161.     MacRGs[w].size.v = y2-y1;
  162.     for (i=0; i<MAX_MAC_SUB; i++)
  163.         SetRect( &MacRGs[w].subs[i], 0,0,0,0);            /* Reset the subs */
  164.     RGwn = w;
  165.     RGsub= 0;
  166.     return (w << MAC_WINDOW_SHIFT);
  167. }
  168.  
  169. /****************************************
  170.  * MacRGsubwindow(wn)        -            *
  171.  *        make a new subwindow to wn        *
  172.  ****************************************/
  173.  
  174. short MacRGsubwindow( short wn)
  175. {
  176.     return (wn);
  177. }
  178.  
  179. /****************************************
  180.  * MacRGsetwindow(wn)        -            *
  181.  *        set the drawing window to wn    *
  182.  ****************************************/
  183. void MacRGsetwindow( short wn)
  184. {
  185.     short w = wn >> MAC_WINDOW_SHIFT;
  186.     
  187.     if (!MacRGs[w].window)
  188.         return;
  189.     
  190.     SetPort( MacRGs[w].window);
  191.     RGwn = w;
  192.     RGsub= wn & MAC_SUB_MASK;
  193.     
  194.     /* Optionally set the clip region */
  195. }
  196.  
  197. /****************************************
  198.  * MacRGdestroy(wn)        -                *
  199.  *        destroy window wn                *
  200.  ****************************************/
  201. void MacRGdestroy(short wn)
  202. {
  203.     
  204. //    sprintf((char *) tempspot,"destroy: %d", wn); putln((char *) tempspot);    /* BYU LSC */
  205.     if (!MacRGs[wn].window)
  206.         return;
  207.     
  208.     VRdestroybyName((char *) &MacRGs[wn].title);
  209. }
  210.     
  211. /****************************************
  212.  * MacRGremove(wn)        -                *
  213.  *        destroy window wn                *
  214.  ****************************************/
  215. void MacRGremove
  216.   (
  217.     short wn
  218.   )
  219. {
  220.     CGrafPtr cgp;
  221.     short w = wn>> MAC_WINDOW_SHIFT;
  222.     
  223.     if (!MacRGs[w].window)
  224.         return;
  225.     
  226.     TrashVDevice(&MacRGs[w].vdev);
  227.     
  228.     
  229.     cgp = (CGrafPtr) MacRGs[w].window;                    /* unseed window color table */
  230.     (*(*(cgp->portPixMap))->pmTable)->ctSeed = GetCTSeed();
  231.     
  232.     DisposeWindow( MacRGs[w].window);        /* Get rid of the actual window */
  233.  
  234.     if (MacRGs[w].palette)
  235.         DisposePalette( MacRGs[w].palette);
  236.         
  237.     MacRGs[w].palette = NULL;
  238.     MacRGs[w].window = NULL;
  239. //    sprintf((char *) tempspot,"take away: %d", w); putln((char *) tempspot);    /* BYU LSC */
  240. }
  241.  
  242. short MacRGfindwind(WindowPtr wind)
  243. {
  244.     short i=0;
  245.     
  246.     if (!wind)
  247.         return(-2);
  248.     
  249.     while (i<MAX_MAC_RGS && wind != MacRGs[i].window)
  250.         i++;
  251.     if (i==MAX_MAC_RGS)
  252.         return(-1);
  253.  
  254.     return( i);
  255. }
  256.  
  257. /************************************************************************************/
  258. /* MacRGcopy
  259. *  Copybits the image window into the clipboard.
  260. *  
  261. */
  262. RGBColor    icrwhite = { 0xffff,0xffff,0xffff },
  263.             icrblack = { 0,0,0}; 
  264.             
  265. void MacRGcopy(WindowPtr wind)
  266. {
  267.     Rect copysize,copyfrom;
  268.     long len,wn;
  269.     PicHandle picture;
  270.     PixMapHandle hidep;
  271.         
  272.     if (( wn= MacRGfindwind( wind)) <0)
  273.         return;                        /* Couldn't do it */
  274.  
  275.     hidep = MacRGs[wn].vdev.whichWorld->portPixMap;
  276.     copyfrom = *MacRGs[wn].vdev.bounds;
  277.             
  278.     SetPort(wind);
  279.     
  280.     copysize = copyfrom;                        /* boundary of drawing area */
  281.  
  282.     picture= OpenPicture(©size);
  283.  
  284.     ClipRect(©size);
  285.     
  286. /*    RGBBackColor(&icrwhite);
  287.     RGBForeColor(&icrblack); */
  288.  
  289.     ForeColor( blackColor);
  290.     BackColor( whiteColor);
  291.     LockPixels(hidep);
  292.     CopyBits((BitMap *) (*hidep), &wind->portBits,
  293.             ©from, ©size, srcCopy, NULL); 
  294.             
  295.     UnlockPixels(hidep);
  296.  
  297.     ClosePicture();
  298.     
  299. /*    put the PICT into the scrap manager */
  300.     len = GetHandleSize((Handle) picture);
  301.     HLock((Handle) picture);
  302.     ZeroScrap();
  303.     PutScrap( len, 'PICT', (Ptr) *picture);
  304.     HUnlock((Handle) picture);
  305.     KillPicture(picture);
  306.  
  307. }
  308.  
  309. short MacRGupdate( WindowPtr wind)
  310. {
  311.     short wn;
  312.     GWorldPtr theWorld;
  313.     if (( wn= MacRGfindwind( wind)) <0)
  314.         return(-1);                        /* Couldn't do it */
  315.  
  316.      theWorld = MacRGs[wn].vdev.whichWorld;
  317.      SetPort(wind);
  318.     ForeColor( blackColor);
  319.     BackColor( whiteColor);
  320.     BeginUpdate(wind);
  321.  
  322.     LockPixels(theWorld->portPixMap);
  323.     CopyBits(&((GrafPtr)theWorld)->portBits,&wind->portBits,&theWorld->portRect,
  324.                     &wind->portRect, srcCopy, NULL);
  325.     UnlockPixels(theWorld->portPixMap);
  326.     EndUpdate( wind);
  327.     return(0);
  328. }
  329.  
  330. /**************************** Hereafter lie the graphics routines ************************/
  331. short    MacRGraster(char *data, short x1, short y1, short x2, short y2, short rowbytes)
  332. {
  333.     Rect tr;
  334.     char *p,*baseOfThisRun;
  335.     short i;
  336.     PixMapHandle pix;
  337.     CGrafPtr origPort;
  338.     GDHandle origDev;
  339.     GWorldPtr theOffscreenWorld;
  340.     long temp;
  341.     if (!MacRGs[RGwn].window)
  342.         return(-1);
  343.     theOffscreenWorld = MacRGs[RGwn].vdev.whichWorld;
  344.     
  345.     GetGWorld(&origPort,&origDev); //save old settings
  346.     
  347.     SetGWorld(theOffscreenWorld,NULL);
  348.     pix = GetGWorldPixMap(MacRGs[RGwn].vdev.whichWorld);
  349.     LockPixels(pix);
  350.     baseOfThisRun = (char *)GetPixBaseAddr(pix);
  351.     temp = (long)((*pix)->rowBytes & 0x3fff);
  352.     baseOfThisRun += temp*y1 + x1; 
  353.     p = baseOfThisRun;                                
  354.     for (i=0; i<rowbytes; i++)
  355.         *p++ = *data++;
  356.     UnlockPixels(pix);
  357.     SetRect(&tr, x1,y1, x2+1,y2+1);
  358.     SetGWorld(origPort,origDev);
  359.     InvalRect(&tr);
  360.     return(0);
  361. }
  362.  
  363. short    MacRGcopyrgn(short x1, short y1, short x2, short y2, short x3, short y3, short x4, short y4)
  364. {
  365.     UNUSED_ARG(x1)
  366.     UNUSED_ARG(y1)
  367.     UNUSED_ARG(x2)
  368.     UNUSED_ARG(y2)
  369.     UNUSED_ARG(x3)
  370.     UNUSED_ARG(y3)
  371.     UNUSED_ARG(x4)
  372.     UNUSED_ARG(y4)
  373.     /* copy one region to another within wn */
  374.     return 0;
  375. }
  376.  
  377. short    MacRGmap(short start, short length, char *data)
  378. {
  379.     short i;
  380.     RGBColor curcol;
  381.     
  382.     for (i=start; i<start+length; i++) {        
  383.         curcol.red = (*data++)<<8;
  384.         curcol.green = (*data++)<<8;
  385.         curcol.blue = (*data++)<<8;
  386.         SetEntryColor( MacRGs[RGwn].palette, i, &curcol);
  387.     }
  388.     
  389.     SetPalette( MacRGs[RGwn].window, MacRGs[RGwn].palette, TRUE);
  390.     ActivatePalette( MacRGs[RGwn].window);
  391.  
  392.     ColorVDevice(&MacRGs[RGwn].vdev,MacRGs[RGwn].palette);
  393. #if 0                                                            /* BYU LSC */
  394.     sprintf((char *) tempspot, "Palette[%d,%d]",start,length);    /* BYU LSC */
  395.     putln((char *) tempspot);                                    /* BYU LSC */
  396. #endif
  397.     return 0;
  398. }
  399.         
  400.