home *** CD-ROM | disk | FTP | other *** search
/ PC Shareware 1999 March / PCShareware-3-99.iso / IMPLE / DJGPP.RAR / DJGPP2 / XLIB-SR0.ZIP / SRC / XLIBEMU / DRAW.C < prev    next >
C/C++ Source or Header  |  1994-12-03  |  11KB  |  522 lines

  1. /* $Id: draw.c 1.4 1994/02/20 19:58:26 ulrich Exp $ */
  2. /*
  3.  * draw.c
  4.  *
  5.  * X library drawing functions.
  6.  */
  7. #include "Xlibemu.h"
  8.  
  9. #ifdef DEBUG
  10. #include <stdio.h>
  11. #endif
  12.  
  13. /* Active drawing window context */
  14.  
  15. Drawable    _DrawDrawable = None;
  16. Region        _DrawRegion;
  17. BoxRec        _DrawClip;
  18. int        _DrawRegionXOffset;
  19. int        _DrawRegionYOffset;
  20. REGION        _TempRegion;
  21.  
  22. int
  23. _WDrawScreenContext (Drawable d)
  24. {
  25.   GrSetContext (NULL);
  26.   GrResetClipBox ();
  27.  
  28.   if (d->type == 2) {
  29.     _DrawRegion = ((Window) d)->visible_region;
  30.     _DrawClip   = ((Window) d)->border_port;
  31.   }
  32.   else {
  33.     _TempRegion.rects = &_TempRegion.extents;
  34.     _TempRegion.numRects = 1;
  35.     _TempRegion.extents.x1 = 0;
  36.     _TempRegion.extents.y1 = 0;
  37.     _TempRegion.extents.x2 = GrSizeX();
  38.     _TempRegion.extents.y2 = GrSizeY();
  39.     _TempRegion.size = 1;
  40.     _DrawRegion = &_TempRegion;
  41.     _DrawClip   = _TempRegion.extents;
  42.   }
  43.   _DrawDrawable = None;
  44.   _DrawRegionXOffset = 0;
  45.   _DrawRegionYOffset = 0;
  46.   return 1;
  47. }
  48.  
  49. /* This functions must by called by XFreePixmap and XDestroyWindow */
  50.  
  51. void
  52. _WDrawInvalid (Drawable d)
  53. {
  54.   if (d == _DrawDrawable) _DrawDrawable = None;
  55. }
  56.  
  57. int
  58. _WDrawContext (Drawable d, GC gc)
  59. {
  60.   if (d == _DrawDrawable)
  61.     return 1;
  62.   if (d == None)
  63.     return 0;
  64.  
  65.   switch (d->type) {
  66.   case 0:
  67.   case 1:
  68.     return 0;
  69.   case 2:
  70.     {
  71.       Window w = (Window) d;
  72.       if (w->viewable == 0 ||
  73.       w->visibility == VisibilityFullyObscured)
  74.     return 0;
  75.       GrSetContext (w->context);
  76.       _DrawRegionXOffset = w->window_port.x1;
  77.       _DrawRegionYOffset = w->window_port.y1;
  78.       if (gc != None &&
  79.       gc->values.subwindow_mode == IncludeInferiors) {
  80.     _TempRegion.rects = &_TempRegion.extents;
  81.     _TempRegion.numRects = 1;
  82.     _TempRegion.extents = w->window_port;
  83.     _TempRegion.size = 1;
  84.     _DrawRegion = &_TempRegion;
  85.       }
  86.       else {
  87.     _DrawRegion = w->visible_region;
  88.       }
  89.       _DrawClip = w->window_port;
  90.       break;
  91.     }
  92.   case 3:
  93.     GrSetContext (((Pixmap) d)->context);
  94.     _DrawRegionXOffset = 0;
  95.     _DrawRegionYOffset = 0;
  96.     _TempRegion.rects = &_TempRegion.extents;
  97.     _TempRegion.numRects = 1;
  98.     _TempRegion.extents.x1 = 0;
  99.     _TempRegion.extents.y1 = 0;
  100.     _TempRegion.extents.x2 = GrSizeX();
  101.     _TempRegion.extents.y2 = GrSizeY();
  102.     _TempRegion.size = 1;
  103.     _DrawRegion = &_TempRegion;
  104.     _DrawClip = _TempRegion.extents;
  105.     break;
  106.   default:
  107.     exit (1);
  108.   }
  109.   _DrawDrawable = d;
  110.   return 1;
  111. }
  112.  
  113. void
  114. _WDrawRegion (void (*primitive)(),
  115.           long a1, long a2, long a3, long a4, long a5)
  116. {
  117.   BoxPtr pbox;
  118.   int i;
  119.  
  120. #if 0
  121.   _XPrintRegion (_DrawRegion);
  122. #endif
  123.  
  124.   pbox = _DrawRegion->rects;
  125.   for (i = _DrawRegion->numRects; --i >= 0; pbox++) {
  126.     if (! EXTENTCHECK (pbox, &_DrawClip)) continue;
  127.     GrSetClipBox (pbox->x1 - _DrawRegionXOffset,
  128.           pbox->y1 - _DrawRegionYOffset,
  129.           pbox->x2 - _DrawRegionXOffset - 1,
  130.           pbox->y2 - _DrawRegionYOffset - 1);
  131.     (*primitive) (a1, a2, a3, a4, a5);
  132.   }
  133. }
  134.  
  135. void
  136. _WDrawRegion7 (void (*primitive)(),
  137.            long a1, long a2, long a3, long a4, long a5, long a6, long a7)
  138. {
  139.   BoxPtr pbox;
  140.   int i;
  141.  
  142. #if 0
  143.   _XPrintRegion (_DrawRegion);
  144. #endif
  145.  
  146.   pbox = _DrawRegion->rects;
  147.   for (i = _DrawRegion->numRects; --i >= 0; pbox++) {
  148.     if (! EXTENTCHECK (pbox, &_DrawClip)) continue;
  149.     GrSetClipBox (pbox->x1 - _DrawRegionXOffset,
  150.           pbox->y1 - _DrawRegionYOffset,
  151.           pbox->x2 - _DrawRegionXOffset - 1,
  152.           pbox->y2 - _DrawRegionYOffset - 1);
  153.     (*primitive) (a1, a2, a3, a4, a5, a6, a7);
  154.   }
  155. }
  156.  
  157. int
  158. XDrawLine
  159. (Display*    display,
  160.  Drawable    drawable,
  161.  GC        gc,
  162.  int        x1,
  163.  int        y1,
  164.  int        x2,
  165.  int        y2)
  166. {
  167.   WGC ext = (WGC) gc;
  168.  
  169.   if (_WDrawContext (drawable, gc) == 0)
  170.     return 0;
  171.   FlushGC(display, gc);
  172.   _WDrawRegion (ext->Line, x1, y1, x2, y2, ext->line_arg);
  173.   return 1;
  174. }
  175.  
  176. int
  177. XDrawLines
  178. (Display*    display,
  179.  Drawable    drawable,
  180.  GC        gc,
  181.  XPoint*    points,
  182.  int        npoints,
  183.  int        mode)
  184. {
  185.   int i, x1, y1, x2, y2;
  186.   WGC ext = (WGC) gc;
  187.  
  188.   if (_WDrawContext (drawable, gc) == 0)
  189.     return 0;
  190.   FlushGC(display, gc);  
  191.   x1 = points->x;
  192.   y1 = points->y;
  193.   points++;
  194.  
  195.   if (mode == CoordModePrevious) {
  196.     for (; --npoints >= 1; points++) {
  197.       x2 = x1 + points->x;
  198.       y2 = y1 + points->y;
  199.       _WDrawRegion (ext->Line, x1, y1, x2, y2, ext->line_arg);
  200.       x1 = x2;
  201.       y1 = y2;
  202.     }
  203.   }
  204.   else /* if (mode == CoordModeOrigin) */ {
  205.     for (; --npoints >= 1; points++) {
  206.       x2 = points->x;
  207.       y2 = points->y;
  208.       _WDrawRegion (ext->Line, x1, y1, x2, y2, ext->line_arg);
  209.       x1 = x2;
  210.       y1 = y2;
  211.     }
  212.   }
  213.  
  214.   return 1;
  215. }
  216.  
  217. int
  218. XDrawSegments
  219. (Display*    display,
  220.  Drawable    drawable,
  221.  GC        gc,
  222.  XSegment*    segments,
  223.  int        nsegments)
  224. {
  225.   WGC ext = (WGC) gc;
  226.  
  227.   if (_WDrawContext (drawable, gc) == 0)
  228.     return 0;
  229.   FlushGC(display, gc);  
  230.   for (; --nsegments >= 0; segments++) {
  231.     _WDrawRegion (ext->Line,
  232.           (int)segments->x1, (int)segments->y1,
  233.           (int)segments->x2, (int)segments->y2, ext->line_arg);
  234.   }
  235.   return 1;
  236. }
  237.  
  238. int
  239. XDrawPoint(
  240.     Display*        display,
  241.     Drawable        d,
  242.     GC            gc,
  243.     int            x,
  244.     int            y)
  245. {
  246.   WGC ext = (WGC) gc;
  247.  
  248.   if (_WDrawContext (d, gc) == 0)
  249.     return 0;
  250.   FlushGC(display, gc);
  251.   _WDrawRegion (ext->Plot, x, y, ext->line_arg, 0, 0);
  252.   return 1;
  253. }
  254.  
  255. int
  256. XDrawPoints(
  257.     Display*        display,
  258.     Drawable        d,
  259.     GC            gc,
  260.     XPoint*        points,
  261.     int            npoints,
  262.     int            mode)
  263. {
  264.   int x, y;
  265.   WGC ext = (WGC) gc;
  266.  
  267.   if (_WDrawContext (d, gc) == 0)
  268.     return 0;
  269.   FlushGC(display, gc);  
  270.   if (mode == CoordModePrevious) {
  271.     x = y = 0;
  272.     for (; --npoints >= 0; points++) {
  273.       x += points->x;
  274.       y += points->y;
  275.       _WDrawRegion (ext->Plot, x, y, ext->line_arg, 0, 0);
  276.     }
  277.   }
  278.   else /* if (mode == CoordModeOrigin) */ {
  279.     for (; --npoints >= 0; points++) {
  280.       x = points->x;
  281.       y = points->y;
  282.       _WDrawRegion (ext->Plot, x, y, ext->line_arg, 0, 0);
  283.     }
  284.   }
  285.   return Success;
  286. }
  287.  
  288. int
  289. XDrawRectangle(
  290.     Display*        display,
  291.     Drawable        d,
  292.     GC            gc,
  293.     int            x,
  294.     int            y,
  295.     unsigned int    width,
  296.     unsigned int    height)
  297. {
  298.   WGC ext = (WGC) gc;
  299.  
  300.   if (_WDrawContext (d, gc) == 0)
  301.     return 0;
  302.   FlushGC(display, gc);
  303.   _WDrawRegion (ext->Box, x, y, x + width, y + height, ext->line_arg);
  304.   return 1;
  305. }
  306.  
  307. int
  308. XDrawRectangles(
  309.     Display*        display,
  310.     Drawable        d,
  311.     GC            gc,
  312.     XRectangle*        rectangles,
  313.     int            nrectangles)
  314. {
  315.   int x1, y1, x2, y2;
  316.   WGC ext = (WGC) gc;
  317.  
  318.   if (_WDrawContext (d, gc) == 0)
  319.     return 0;
  320.   FlushGC(display, gc);  
  321.   for (; --nrectangles >= 0; rectangles++) {
  322.     x1 = rectangles->x;
  323.     y1 = rectangles->y;
  324.     x2 = x1 + rectangles->width;
  325.     y2 = y1 + rectangles->height; 
  326.     _WDrawRegion (ext->Box, x1, y1, x2, y2, ext->line_arg);
  327.   }
  328.   return 1;
  329. }
  330.  
  331. int XDrawArc(
  332.     Display*        display,
  333.     Drawable        d,
  334.     GC            gc,
  335.     int            x,
  336.     int            y,
  337.     unsigned int    width,
  338.     unsigned int    height,
  339.     int            angle1,
  340.     int            angle2)
  341. {
  342.   XArc arc;
  343.  
  344.   arc.x = x;
  345.   arc.y = y;
  346.   arc.width = width;
  347.   arc.height = height;
  348.   arc.angle1 = angle1;
  349.   arc.angle2 = angle2;
  350.   return XDrawArcs (display, d, gc, &arc, 1);
  351. }
  352.  
  353. int XDrawArcs(
  354.     Display*        display,
  355.     Drawable        d,
  356.     GC            gc,
  357.     XArc*        arcs,
  358.     int            narcs)
  359. {
  360.   int xc, yc, xa, ya, start, end;
  361.   WGC ext = (WGC) gc;
  362.  
  363.   if (_WDrawContext (d, gc) == 0)
  364.     return 0;
  365.   FlushGC(display, gc);
  366.   for (; --narcs >= 0; arcs++) {
  367.     xa = arcs->width / 2;
  368.     ya = arcs->height / 2;
  369.     xc = arcs->x + xa;
  370.     yc = arcs->y + ya;
  371.     start = (arcs->angle1 * 10) / 64;
  372.     end = start + (arcs->angle2 * 10) / 64;
  373.     if (end < start) { int temp = end; end = start; start = temp; };
  374.     _WDrawRegion7 (ext->EllipseArc, xc, yc, xa, ya, start, end, ext->line_arg);
  375.   }
  376.   return 0;
  377. }
  378.  
  379. int
  380. XFillArc(
  381.     Display*        display,
  382.     Drawable        d,
  383.     GC            gc,
  384.     int            x,
  385.     int            y,
  386.     unsigned int    width,
  387.     unsigned int    height,
  388.     int            angle1,
  389.     int            angle2
  390. )
  391. {
  392.   XArc arc;
  393.  
  394.   arc.x = x;
  395.   arc.y = y;
  396.   arc.width = width;
  397.   arc.height = height;
  398.   arc.angle1 = angle1;
  399.   arc.angle2 = angle2;
  400.   return XFillArcs (display, d, gc, &arc, 1);
  401. }
  402.  
  403. int
  404. XFillArcs(
  405.     Display*        display,
  406.     Drawable        d,
  407.     GC            gc,
  408.     XArc*        arcs,
  409.     int            narcs)
  410. {
  411.   int xc, yc, xa, ya, start, end;
  412.   WGC ext = (WGC) gc;
  413.  
  414.   if (_WDrawContext (d, gc) == 0)
  415.     return 0;
  416.   FlushGC(display, gc);
  417.   for (; --narcs >= 0; arcs++) {
  418.     xa = arcs->width / 2;
  419.     ya = arcs->height / 2;
  420.     xc = arcs->x + xa;
  421.     yc = arcs->y + ya;
  422.     start = (arcs->angle1 * 10) / 64;
  423.     end = start + (arcs->angle2 * 10) / 64;
  424.     if (end < start) { int temp = end; end = start; start = temp; };
  425.     _WDrawRegion7 (ext->FilledEllipseArc, xc, yc, xa, ya, start, end, ext->fill_arg);
  426.   }
  427.   return 0;
  428. }
  429.  
  430. int
  431. XFillPolygon(
  432.     Display*        display,
  433.     Drawable        d,
  434.     GC            gc,
  435.     XPoint*        points,
  436.     int            npoints,
  437.     int            shape,
  438.     int            mode)
  439. {
  440.   int i, x, y;
  441.   int *opoints = (int *) alloca (npoints * 2 * sizeof(int));
  442.   WGC ext = (WGC) gc;
  443.  
  444.   if (_WDrawContext (d, gc) == 0)
  445.     return 0;
  446.   FlushGC(display, gc);
  447.   if (mode == CoordModePrevious) {
  448.     x = y = 0;
  449.     for (i = 0; i < npoints; i++) {
  450.       x += points->x;
  451.       y += points->y;
  452.       points++;
  453.       opoints[2*i+0] = x;
  454.       opoints[2*i+1] = y;
  455.     }
  456.   }
  457.   else /* if (mode == CoordModeOrigin) */ {
  458.     for (i = 0; i < npoints; i++) {
  459.       x = points->x;
  460.       y = points->y;
  461.       points++;
  462.       opoints[2*i+0] = x;
  463.       opoints[2*i+1] = y;
  464.     }
  465.   }
  466.   _WDrawRegion (ext->FilledPolygon,
  467.         npoints, (long) opoints, ext->fill_arg, 0, 0);
  468.   return 1;
  469. }
  470.  
  471. int
  472. XFillRectangle(
  473.     Display*        display,
  474.     Drawable        d,
  475.     GC            gc,
  476.     int            x,
  477.     int            y,
  478.     unsigned int    width,
  479.     unsigned int    height)
  480. {
  481.   WGC ext = (WGC) gc;
  482.  
  483.   if (_WDrawContext (d, gc) == 0)
  484.     return 0;
  485.   FlushGC(display, gc);
  486.   _WDrawRegion (ext->FilledBox,    x, y, x + width - 1, y + height - 1, ext->fill_arg);
  487.   return 1;
  488. }
  489.  
  490. int
  491. XFillRectangles(
  492.     Display*        display,
  493.     Drawable        d,
  494.     GC            gc,
  495.     XRectangle*        rectangles,
  496.     int            nrectangles)
  497. {
  498.   int x1, y1, x2, y2;
  499.   WGC ext = (WGC) gc;
  500.  
  501.   if (_WDrawContext (d, gc) == 0)
  502.     return 0;
  503.   FlushGC(display, gc);
  504.  
  505.   for (; --nrectangles >= 0; rectangles++) {
  506.     x1 = rectangles->x;
  507.     y1 = rectangles->y;
  508.     x2 = x1 + rectangles->width - 1;
  509.     y2 = y1 + rectangles->height - 1;
  510.     _WDrawRegion (ext->FilledBox, x1, y1, x2, y2, ext->fill_arg);
  511.   }
  512.   return 1;
  513. }
  514.  
  515. int
  516. XFlush(
  517.     Display*        display
  518. )
  519. {
  520.   return 1;
  521. }
  522.