home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / XfeWidgets / Xfe / Draw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  12.4 KB  |  521 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /*-----------------------------------------*/
  19. /*                                                                        */
  20. /* Name:        <Xfe/Draw.c>                                            */
  21. /* Description:    Xfe widgets drawing functions header file.                */
  22. /* Author:        Ramiro Estrugo <ramiro@netscape.com>                    */
  23. /*                                                                        */
  24. /*----------------------------------------------------------------------*/
  25.  
  26.  
  27. #include <Xfe/XfeP.h>
  28. #include <Xfe/ManagerP.h>
  29.  
  30. /*----------------------------------------------------------------------*/
  31. /*                                                                        */
  32. /* Drawing functions                                                    */
  33. /*                                                                        */
  34. /*----------------------------------------------------------------------*/
  35. /* extern */ void
  36. XfeDrawRectangle(Display *        dpy,
  37.                  Drawable        d,
  38.                  GC                gc,
  39.                  Position        x,
  40.                  Position        y,
  41.                  Dimension        width,
  42.                  Dimension        height,
  43.                  Dimension        thickness)
  44. {
  45.     static XSegment *    segs = NULL;
  46.     static Cardinal    nsegs = 0;
  47.     XSegment *        sp;
  48.     Cardinal        i;
  49.     Cardinal        count;
  50.    
  51.     assert(dpy != NULL);
  52.     assert(d != None);
  53.     assert(gc != NULL);
  54.  
  55.     /* Make sure a thickness is given */
  56.     if (!thickness || !width || !height) return;
  57.  
  58.     /* Number of segments to draw */
  59.     count = thickness * 4;
  60.  
  61.     /* Make sure we have enough segments */
  62.     if (nsegs < count)
  63.     {
  64.         nsegs = count;
  65.         segs = (XSegment *) XtRealloc((char *) segs,sizeof(XSegment) * nsegs);
  66.     }
  67.  
  68.     for (i = 0; i < thickness; i++)
  69.     {
  70.         /* top */
  71.         sp = segs + i;
  72.  
  73.         sp->x1 = x;
  74.         sp->y1 = sp->y2 = y + i;
  75.         sp->x2 = x + width - 0;
  76.  
  77.         /* bottom */
  78.         sp = segs + i + thickness;
  79.  
  80.         sp->x1 = x;
  81.         sp->y1 = sp->y2 = y + height - i - 1;
  82.         sp->x2 = x + width - 0;
  83.  
  84.         /* left */
  85.         sp = segs + i + 2 * thickness;
  86.  
  87.         sp->x1 = sp->x2 = x + i;
  88.         sp->y1 = y + thickness;
  89.         sp->y2 = y + height - thickness - 0;
  90.  
  91.         /* right */
  92.         sp = segs + i + 3 * thickness;
  93.  
  94.         sp->x1 = sp->x2 = x + width - i - 1;
  95.         sp->y1 = y + thickness;
  96.         sp->y2 = y + height - thickness - 0;
  97.     }
  98.  
  99.     XDrawSegments(dpy,d,gc,segs,count);
  100. }
  101. /*----------------------------------------------------------------------*/
  102. /* extern */ void
  103. XfeClearRectangle(Display *    dpy,
  104.                   Drawable    d,
  105.                   Position    x,
  106.                   Position    y,
  107.                   Dimension    width,
  108.                   Dimension    height,
  109.                   Dimension    thickness,
  110.                   Boolean    exposures)
  111. {
  112.     Dimension rw;
  113.     
  114.     assert(dpy != NULL);
  115.     assert(d != None);
  116.     
  117.     /* Make sure a thickness is given */
  118.     if (!thickness || !width || !height) return;
  119.     
  120.     rw = width - (thickness / 2);
  121.     
  122.     /* Left */
  123.     XClearArea(dpy,d,x,y,thickness,height,exposures);
  124.     
  125.     /* Right */
  126.     XClearArea(dpy,d,x + width - thickness,y,thickness,height,exposures);
  127.     
  128.     /* Top */
  129.     XClearArea(dpy,d,x + thickness,y,rw,thickness,exposures);
  130.     
  131.     /* Bottom */
  132.     XClearArea(dpy,d,x+thickness,y + height - thickness,rw,thickness,exposures);
  133. }
  134. /*----------------------------------------------------------------------*/
  135. /* extern */ void
  136. XfeDrawCross(Display *    dpy,
  137.              Drawable    d,
  138.              GC        gc,
  139.              Position    x,
  140.              Position    y,
  141.              Dimension    width,
  142.              Dimension    height,
  143.              Dimension    thickness)
  144. {
  145.     static XPoint    pts[17];
  146.  
  147.     int        w = width;
  148.     int        h = height;
  149.     int        w2 = width / 2;
  150.     int        h2 = height / 2;
  151.     float    rad2 = 1.41421356237;
  152.     int        c = (int) ((float) thickness / rad2);
  153.  
  154.     /* Make sure c is at least 1 */
  155.     if (c < 1)
  156.     {
  157.         c = 1;
  158.     }
  159.  
  160.     assert(dpy != NULL);
  161.     assert(d != None);
  162.     assert(gc != NULL);
  163.  
  164.     /* Make sure a thickness is given */
  165.     if (!thickness || !width || !height) return;
  166.  
  167.     pts[0].x = x + 0;
  168.     pts[0].y = y + 0;
  169.  
  170.     pts[1].x = x + c;
  171.     pts[1].y = y + 0;
  172.  
  173.     pts[2].x = x + w2;
  174.     pts[2].y = y + h2 - c;
  175.  
  176.     pts[3].x = x + w - c;
  177.     pts[3].y = y + 0;
  178.  
  179.     pts[4].x = x + w;
  180.     pts[4].y = y + 0;
  181.  
  182.     pts[5].x = x + w;
  183.     pts[5].y = y + c;
  184.  
  185.     pts[6].x = x + w2 + c;
  186.     pts[6].y = y + h2;
  187.  
  188.     pts[7].x = x + w;
  189.     pts[7].y = y + h - c;
  190.  
  191.     pts[8].x = x + w;
  192.     pts[8].y = y + h;
  193.  
  194.     pts[9].x = x + w - c;
  195.     pts[9].y = y + h;
  196.  
  197.     pts[10].x = x + w2;
  198.     pts[10].y = y + h2 + c;
  199.  
  200.     pts[11].x = x + c;
  201.     pts[11].y = y + h;
  202.  
  203.     pts[12].x = x + 0;
  204.     pts[12].y = y + h;
  205.  
  206.     pts[13].x = x + 0;
  207.     pts[13].y = y + h - c;
  208.  
  209.     pts[14].x = x + w2 - c;
  210.     pts[14].y = y + h2;
  211.  
  212.     pts[15].x = x + 0;
  213.     pts[15].y = y + c;
  214.  
  215.     pts[16].x = x + 0;
  216.     pts[16].y = y + 0;
  217.  
  218.     XFillPolygon(dpy,d,gc,pts,17,CoordModeOrigin,Complex);
  219. }
  220. /*----------------------------------------------------------------------*/
  221. /* extern */ void
  222. XfeDrawArrow(Display *        dpy,
  223.              Drawable        d,
  224.              GC                gc,
  225.              Position        x,
  226.              Position        y,
  227.              Dimension        width,
  228.              Dimension        height,
  229.              unsigned char    direction)
  230. {
  231.     static XPoint    pts[3];
  232.     Dimension        dx;
  233.     Dimension        dy;
  234.     
  235.     assert( dpy != NULL );
  236.     assert( d != None );
  237.     assert( gc != NULL );
  238.     
  239.     /* Make sure a thickness is given */
  240.     if (!width || !height) return;
  241.     
  242.     dx = width / 2;
  243.     dy = height / 2;
  244.     
  245.     switch (direction)
  246.     {
  247.     case XmARROW_UP:
  248.         XfePointSet(&pts[0],x,y + height);
  249.         XfePointSet(&pts[1],x + width,y + height);
  250.         XfePointSet(&pts[2],x + dx,y);
  251.         break;
  252.     
  253.     case XmARROW_DOWN:
  254.         XfePointSet(&pts[0],x,y);
  255.         XfePointSet(&pts[1],x + dx,y + height);
  256.         XfePointSet(&pts[2],x + width,y);
  257.         break;
  258.     
  259.     case XmARROW_RIGHT:
  260.         XfePointSet(&pts[0],x,y);
  261.         XfePointSet(&pts[1],x,y + height);
  262.         XfePointSet(&pts[2],x + width,y + dy);
  263.         break;
  264.     
  265.     case XmARROW_LEFT:
  266.         XfePointSet(&pts[0],x + width,y);
  267.         XfePointSet(&pts[1],x + width,y + height);
  268.         XfePointSet(&pts[2],x,y + dy);
  269.         break;
  270.     }
  271.     
  272.     /* Fill the Arrow */
  273.     XFillPolygon(dpy,d,gc,pts,3,Convex,CoordModeOrigin);
  274. }
  275. /*----------------------------------------------------------------------*/
  276. /* extern */ void
  277. XfeDrawMotifArrow(Display *        dpy,
  278.                   Drawable        d,
  279.                   GC            top_gc,
  280.                   GC            bottom_gc,
  281.                   GC            center_gc,
  282.                   Position        x,
  283.                   Position        y,
  284.                   Dimension        width,
  285.                   Dimension        height,
  286.                   unsigned char    direction,
  287.                   Dimension        shadow_thickness,
  288.                   Boolean        swap)
  289. {
  290.  
  291.     assert( dpy != NULL );
  292.     assert( d != None );
  293. /*     assert( top_gc != NULL ); */
  294. /*     assert( bottom_gc != NULL ); */
  295.     assert( center_gc != NULL );
  296.  
  297.     if (swap) 
  298.     {
  299.         GC tmp_gc = bottom_gc;
  300.         bottom_gc = top_gc;
  301.         top_gc = tmp_gc;
  302.     }
  303.     
  304.     _XmDrawArrow(dpy,d,
  305.                  top_gc,bottom_gc,center_gc,
  306.                  x,y,
  307.                  width,height,
  308.                  shadow_thickness,
  309.                  direction);
  310. }
  311. /*----------------------------------------------------------------------*/
  312. /* extern */ void
  313. XfeDrawVerticalLine(Display *        dpy,
  314.                     Drawable        d,
  315.                     GC                gc,
  316.                     Position        x,
  317.                     Position        y,
  318.                     Dimension        height,
  319.                     Dimension        thickness)
  320. {
  321.     static XSegment *    segs = NULL;
  322.     static Cardinal    nsegs = 0;
  323.     XSegment *        sp;
  324.     Cardinal        i;
  325.     Cardinal        count;
  326.    
  327.     assert(dpy != NULL);
  328.     assert(d != None);
  329.     assert(gc != NULL);
  330.  
  331.     /* Make sure a thickness is given */
  332.     if (!thickness || !height) return;
  333.  
  334.     /* Number of segments to draw */
  335.     count = thickness;
  336.  
  337.     /* Make sure we have enough segments */
  338.     if (nsegs < count)
  339.     {
  340.         nsegs = count;
  341.         segs = (XSegment *) XtRealloc((char *) segs,sizeof(XSegment) * nsegs);
  342.     }
  343.  
  344.     for (i = 0; i < thickness; i++)
  345.     {
  346.         sp = segs + i;
  347.  
  348.         sp->x1 = sp->x2 = x + i;
  349.         sp->y1 = y + thickness;
  350.         sp->y2 = y + height - thickness - 1;
  351.     }
  352.  
  353.     XDrawSegments(dpy,d,gc,segs,count);
  354. }
  355. /*----------------------------------------------------------------------*/
  356. /* extern */ void
  357. XfeDrawHorizontalLine(Display *        dpy,
  358.                       Drawable        d,
  359.                       GC            gc,
  360.                       Position        x,
  361.                       Position        y,
  362.                       Dimension        width,
  363.                       Dimension        thickness)
  364. {
  365.     static XSegment *    segs = NULL;
  366.     static Cardinal    nsegs = 0;
  367.     XSegment *        sp;
  368.     Cardinal        i;
  369.     Cardinal        count;
  370.    
  371.     assert(dpy != NULL);
  372.     assert(d != None);
  373.     assert(gc != NULL);
  374.  
  375.     /* Make sure a thickness is given */
  376.     if (!thickness || !width) return;
  377.  
  378.     /* Number of segments to draw */
  379.     count = thickness;
  380.  
  381.     /* Make sure we have enough segments */
  382.     if (nsegs < count)
  383.     {
  384.         nsegs = count;
  385.         segs = (XSegment *) XtRealloc((char *) segs,sizeof(XSegment) * nsegs);
  386.     }
  387.  
  388.     for (i = 0; i < thickness; i++)
  389.     {
  390.         sp = segs + i;
  391.  
  392.         sp->x1 = x;
  393.         sp->y1 = sp->y2 = y + i;
  394.         sp->x2 = x + width - 1;
  395.     }
  396.  
  397.     XDrawSegments(dpy,d,gc,segs,count);
  398. }
  399. /*----------------------------------------------------------------------*/
  400. /* extern */ void
  401. XfeStippleRectangle(Display *    dpy,
  402.                     Drawable    d,
  403.                     GC            gc,
  404.                     Position    x,
  405.                     Position    y,
  406.                     Dimension    width,
  407.                     Dimension    height,
  408.                     Dimension    step)
  409. {
  410.     int            i;
  411.     int            j;
  412.     Dimension    half_step;
  413.  
  414.     assert( dpy != NULL );
  415.     assert( d != None );
  416.     assert( gc != NULL );
  417.     assert( width > 0 );
  418.     assert( height > 0 );
  419.     assert( step > 1 );
  420.     assert( XfeEven(step) );
  421.     
  422.     /* Make sure a step is given */
  423.     if (!step || !width || !height) return;
  424.  
  425.     half_step = step / 2;
  426.  
  427.     /* Vertical */
  428.     for (i = y; i <= (y + height); i += step)
  429.     {
  430.         for (j = x; j <= (x + width); j += step)
  431.         {
  432.             XFillRectangle(dpy,d,gc,j,i,half_step,half_step);
  433.         }
  434.     }
  435.  
  436.     /* Horizontal */
  437.     for (i = (y + half_step); i <= (y + height - half_step); i += step)
  438.     {
  439.         for (j = (x + half_step); j <= (x + width - half_step); j += step)
  440.         {
  441.             XFillRectangle(dpy,d,gc,j,i,half_step,half_step);
  442.         }
  443.     }
  444. }
  445. /*----------------------------------------------------------------------*/
  446.  
  447. /*----------------------------------------------------------------------*/
  448. /*                                                                        */
  449. /* Drawable functions                                                    */
  450. /*                                                                        */
  451. /*----------------------------------------------------------------------*/
  452. /* extern */ Boolean
  453. XfeDrawableGetGeometry(Display *    dpy,
  454.                        Drawable        d,
  455.                        Position *    x_out,
  456.                        Position *    y_out,
  457.                        Dimension *    width_out,
  458.                        Dimension *    height_out)
  459. {
  460.     int                x = 0;
  461.     int                y = 0;
  462.     unsigned int    width = 0;
  463.     unsigned int    height = 0;
  464.     unsigned int    border;
  465.     unsigned int    depth;
  466.     Window            root;
  467.     Boolean            result = False;
  468.  
  469.     assert( dpy != NULL );
  470.     assert( d != None );
  471.     
  472.     result = XGetGeometry(dpy,d,&root,&x,&y,&width,&height,&border,&depth);
  473.  
  474.     if (x_out)
  475.         *x_out = (Position) x;
  476.     
  477.     if (y_out)
  478.         *y_out = (Position) y;
  479.     
  480.     if (width_out)
  481.         *width_out = (Dimension) width;
  482.     
  483.     if (height_out)
  484.         *height_out = (Dimension) height;
  485.     
  486.     return True;
  487. }
  488. /*----------------------------------------------------------------------*/
  489.  
  490. /*----------------------------------------------------------------------*/
  491. /*                                                                        */
  492. /* Shadow functions                                                        */
  493. /*                                                                        */
  494. /*----------------------------------------------------------------------*/
  495. /* extern */ void
  496. XfeDrawShadowsAroundWidget(Widget            parent,
  497.                            Widget            child,
  498.                            GC                top_gc,
  499.                            GC                bottom_gc,
  500.                            Dimension        offset,
  501.                            Dimension        shadow_thickness,
  502.                            unsigned char    shadow_type)
  503. {
  504.     assert( _XfeIsAlive(parent) );
  505.     assert( _XfeIsAlive(child) );
  506.     assert( shadow_thickness > 0 );
  507.     assert( XmIsManager(parent) );
  508.  
  509.     _XmDrawShadows(XtDisplay(parent),
  510.                    _XfeWindow(parent),
  511.                    _XfemTopShadowGC(parent),
  512.                    _XfemBottomShadowGC(parent),
  513.                    _XfeX(child) - shadow_thickness - offset,
  514.                    _XfeY(child) - shadow_thickness - offset,
  515.                    _XfeWidth(child) + 2 * shadow_thickness + 2 * offset,
  516.                    _XfeHeight(child) + 2 * shadow_thickness + 2 * offset,
  517.                    shadow_thickness,
  518.                    shadow_type);
  519. }
  520. /*----------------------------------------------------------------------*/
  521.