home *** CD-ROM | disk | FTP | other *** search
/ Shareware 1 2 the Maxx / sw_1.zip / sw_1 / PROGRAM / CBGRX100.ZIP / CONTRIB / LIBGRX / SRC / CONTEXT.C < prev    next >
Text File  |  1992-04-10  |  4KB  |  154 lines

  1. /** 
  2.  ** CONTEXT.C 
  3.  **
  4.  **  Copyright (C) 1992, Csaba Biegl
  5.  **    820 Stirrup Dr, Nashville, TN, 37221
  6.  **    csaba@vuse.vanderbilt.edu
  7.  **
  8.  **  This file is distributed under the terms listed in the document
  9.  **  "copying.cb", available from the author at the address above.
  10.  **  A copy of "copying.cb" should accompany this file; if not, a copy
  11.  **  should be available from where this file was obtained.  This file
  12.  **  may not be distributed without a verbatim copy of "copying.cb".
  13.  **  You should also have received a copy of the GNU General Public
  14.  **  License along with this program (it is in the file "copying");
  15.  **  if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16.  **  Cambridge, MA 02139, USA.
  17.  **
  18.  **  This program is distributed in the hope that it will be useful,
  19.  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  **  GNU General Public License for more details.
  22.  **/
  23.  
  24. #include "grx.h"
  25. #include "libgrx.h"
  26. #include "clipping.h"
  27. #include "gmalloc.h"
  28.  
  29. #include <string.h>
  30.  
  31. GrContext *GrCreateContext(int w,int h,char far *memory,GrContext *where)
  32. {
  33.     long size   = GrContextSize(w,h);
  34.     long psize  = GrPlaneSize(w,h);
  35.     int  offset = GrLineOffset(w);
  36.     int  flags  = 0;
  37.  
  38.     if(size == 0L) return(NULL);
  39. #ifdef _MAXMEMPLANESIZE
  40.     if(psize > _MAXMEMPLANESIZE) return(NULL);
  41. #endif
  42.     if(where == NULL) {
  43.         where = _GrMalloc(sizeof(GrContext));
  44.         if(where == NULL) return(NULL);
  45.         flags |= GCM_MYCONTEXT;
  46.     }
  47.     memset(where,0,sizeof(GrContext));
  48.     if(memory == (char far *)NULL) {
  49.         memory = _GrFarMalloc(size);
  50.         if(memory == (char far *)NULL)
  51.         { if(flags) _GrFree(where); return(NULL); }
  52.         flags |= GCM_MYMEMORY;
  53.     }
  54.     where->gc_baseaddr    = memory;
  55.     where->gc_lineoffset  = offset;
  56.     where->gc_frameaddr   = BASE_ADDRESS(where);
  57.     where->gc_memflags    = flags;
  58.     where->gc_planeoffset =
  59.         (long)((char huge *)where->gc_planeoffset + psize);
  60.     where->gc_xmax = where->gc_xcliphi = w - 1;
  61.     where->gc_ymax = where->gc_ycliphi = h - 1;
  62.     return(where);
  63. }
  64.  
  65. #undef  WHEN_OUTSIDE
  66. #define WHEN_OUTSIDE    return(NULL)
  67.  
  68. GrContext *GrCreateSubContext
  69.       (int x1,int y1,int x2,int y2,GrContext *parent,GrContext *where)
  70. {
  71.     int flags = 0;
  72.  
  73.     if(parent == NULL) parent = CURC;
  74.     if(parent->gc_root != NULL) {
  75.         x1 += parent->gc_xoffset;
  76.         y1 += parent->gc_yoffset;
  77.         x2 += parent->gc_xoffset;
  78.         y2 += parent->gc_yoffset;
  79.         parent = parent->gc_root;
  80.     }
  81.     CLIPBOXTOCONTEXT(parent,x1,y1,x2,y2);
  82.     if(where == NULL) {
  83.         where = _GrMalloc(sizeof(GrContext));
  84.         if(where == NULL) return(NULL);
  85.         flags |= GCM_MYCONTEXT;
  86.     }
  87.     *where = *parent;
  88.     where->gc_frameaddr = PIX_ADDR(parent,x1,y1);
  89.     where->gc_memflags  = flags;
  90.     where->gc_xoffset   = x1;
  91.     where->gc_yoffset   = y1;
  92.     where->gc_xcliphi   = where->gc_xmax = x2 - x1;
  93.     where->gc_ycliphi   = where->gc_ymax = y2 - y1;
  94.     where->gc_xcliplo   = 0;
  95.     where->gc_ycliplo   = 0;
  96.     where->gc_root        = parent;
  97.     return(where);
  98. }
  99.  
  100. #undef  WHEN_OUTSIDE
  101. #define WHEN_OUTSIDE    return
  102.  
  103. void GrResizeSubContext(GrContext *context,int x1,int y1,int x2,int y2)
  104. {
  105.     GrContext *parent;
  106.  
  107.     if((parent = context->gc_root) == NULL) return;
  108.     x1 += context->gc_xoffset;
  109.     y1 += context->gc_yoffset;
  110.     x2 += context->gc_xoffset;
  111.     y2 += context->gc_yoffset;
  112.     CLIPBOXTOCONTEXT(parent,x1,y1,x2,y2);
  113.     context->gc_frameaddr = PIX_ADDR(parent,x1,y1);
  114.     context->gc_xoffset   = x1;
  115.     context->gc_yoffset   = y1;
  116.     context->gc_xcliphi   = context->gc_xmax = x2 - x1;
  117.     context->gc_ycliphi   = context->gc_ymax = y2 - y1;
  118.     context->gc_xcliplo   = 0;
  119.     context->gc_ycliplo   = 0;
  120. }
  121.  
  122. void GrDestroyContext(GrContext *cxt)
  123. {
  124.     if((cxt != NULL) && (cxt != CURC) && (cxt != SCREEN)) {
  125.         if(cxt->gc_memflags & GCM_MYMEMORY)  _GrFarFree(cxt->gc_baseaddr);
  126.         if(cxt->gc_memflags & GCM_MYCONTEXT) _GrFree(cxt);
  127.     }
  128. }
  129.  
  130. void GrSetContext(GrContext *context)
  131. {
  132.     if(context == NULL) context = SCREEN;
  133.     _GrContext = *context;
  134.     _GrMouseCheck =
  135.         (_GrMouseDrawn && (CURC->gc_baseaddr == SCREEN->gc_baseaddr)) ?
  136.         TRUE :
  137.         FALSE;
  138. }
  139.  
  140. GrContext *GrSaveContext(GrContext *where)
  141. {
  142.     int flags = 0;
  143.  
  144.     if(where == NULL) {
  145.         where = _GrMalloc(sizeof(GrContext));
  146.         if(where == NULL) return(NULL);
  147.         flags |= GCM_MYCONTEXT;
  148.     }
  149.     *where = _GrContext;
  150.     where->gc_memflags = flags;
  151.     return(where);
  152. }
  153.  
  154.