home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xmu / ShapeWidg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-19  |  5.2 KB  |  179 lines

  1. /* $XConsortium: ShapeWidg.c,v 1.5 90/12/20 11:01:29 converse Exp $ */
  2.  
  3. /* 
  4.  * Copyright 1988 by the Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for any purpose and without fee is hereby granted, provided 
  8.  * that the above copyright notice appear in all copies and that both that 
  9.  * copyright notice and this permission notice appear in supporting 
  10.  * documentation, and that the name of M.I.T. not be used in advertising
  11.  * or publicity pertaining to distribution of the software without specific, 
  12.  * written prior permission. M.I.T. makes no representations about the 
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  */
  17.  
  18. #include <X11/IntrinsicP.h>
  19. #include <X11/extensions/shape.h>
  20. #include "Converters.h"
  21. #include "Drawing.h"
  22.  
  23. static ShapeError();
  24. static  ShapeRectangle(), ShapeOval(), ShapeEllipseOrRoundedRectangle();
  25.  
  26. Boolean XmuReshapeWidget(w, shape_style, corner_width, corner_height)
  27.     Widget w;
  28.     int shape_style;
  29.     int corner_width, corner_height;
  30. {
  31.     switch (shape_style) {
  32.  
  33.       case XmuShapeRectangle:
  34.     ShapeRectangle(w);
  35.     break;
  36.  
  37.       case XmuShapeOval:
  38.     ShapeOval(w);
  39.     break;
  40.  
  41.       case XmuShapeEllipse:
  42.       case XmuShapeRoundedRectangle:
  43.     ShapeEllipseOrRoundedRectangle
  44.         (w,
  45.          ((shape_style == XmuShapeEllipse) ? True : False),
  46.          corner_width,
  47.          corner_height);
  48.     break;
  49.  
  50.       default:
  51.     ShapeError(w);
  52.     return False;
  53.     }
  54.     return True;
  55. }
  56.  
  57. static ShapeError(w)
  58.     Widget w;
  59. {
  60.     String params[1];
  61.     Cardinal num_params = 1;
  62.     params[0] = XtName(w);
  63.     XtAppWarningMsg( XtWidgetToApplicationContext(w),
  64.              "shapeUnknown", "xmuReshapeWidget", "XmuLibrary",
  65.              "Unsupported shape style for Command widget \"%s\"",
  66.              params, &num_params
  67.            );
  68. }
  69.  
  70.  
  71. static ShapeRectangle(w)
  72.     Widget w;
  73. {
  74.     XShapeCombineMask( XtDisplay(w), XtWindow(w),
  75.                ShapeBounding, 0, 0, None, ShapeSet );
  76.     XShapeCombineMask( XtDisplay(w), XtWindow(w),
  77.                ShapeClip, 0, 0, None, ShapeSet );
  78. }
  79.  
  80.  
  81. static ShapeOval(w)
  82.     Widget w;
  83. {
  84.     Display *dpy = XtDisplay(w);
  85.     unsigned width = w->core.width + (w->core.border_width<<1);
  86.     unsigned height = w->core.height + (w->core.border_width<<1);
  87.     Pixmap p = XCreatePixmap( dpy, XtWindow(w), width, height, 1 );
  88.     XGCValues values;
  89.     GC gc;
  90.     int rad;
  91.  
  92.     values.foreground = 0;
  93.     values.background = 1;
  94.     values.cap_style = CapRound;
  95.     values.line_width = height;
  96.     gc = XCreateGC (dpy, p,
  97.             GCForeground | GCBackground | GCLineWidth | GCCapStyle,
  98.             &values);
  99.     XFillRectangle( dpy, p, gc, 0, 0, width, height );
  100.     XSetForeground( dpy, gc, 1 );
  101.     if (width <= height) {
  102.     /* cannot be oval, fall back to ellipse */
  103.     XFillArc( dpy, p, gc, 0, 0, width, height, 0, 360*64 );
  104.     } else {
  105.     rad = height >> 1;
  106.     XDrawLine( dpy, p, gc, rad, rad, (int)width - rad - 1, rad );
  107.     }
  108.     XShapeCombineMask( dpy, XtWindow(w), ShapeBounding, 
  109.                -(w->core.border_width), -(w->core.border_width),
  110.                p, ShapeSet );
  111.     if (w->core.border_width) {
  112.     XSetForeground( dpy, gc, 0 );
  113.     XFillRectangle( dpy, p, gc, 0, 0, width, height );
  114.     values.line_width = w->core.height;
  115.     values.foreground = 1;
  116.     XChangeGC (dpy, gc, GCLineWidth|GCForeground, &values);
  117.     if (w->core.width <= w->core.height) {
  118.         /* cannot be oval, fall back to ellipse */
  119.         XFillArc( dpy, p, gc, 0, 0, w->core.width, w->core.height,
  120.               0, 360*64 );
  121.     } else {
  122.         rad = w->core.height >> 1;
  123.         XDrawLine( dpy, p, gc, rad, rad,
  124.                (int)w->core.width - rad - 1, rad );
  125.     }
  126.     XShapeCombineMask( dpy, XtWindow(w), ShapeClip, 0, 0, p, ShapeSet );
  127.     } else {
  128.     XShapeCombineMask( XtDisplay(w), XtWindow(w),
  129.               ShapeClip, 0, 0, None, ShapeSet );
  130.     }
  131.     XFreePixmap( dpy, p );
  132.     XFreeGC (dpy, gc );
  133. }
  134.  
  135.  
  136. static ShapeEllipseOrRoundedRectangle(w, ellipse, ew, eh)
  137.     Widget w;
  138.     Boolean ellipse;
  139.     int ew, eh;
  140. {
  141.     Display *dpy = XtDisplay(w);
  142.     unsigned width = w->core.width + (w->core.border_width<<1);
  143.     unsigned height = w->core.height + (w->core.border_width<<1);
  144.     Pixmap p = XCreatePixmap( dpy, XtWindow(w), width, height, 1 );
  145.     XGCValues values;
  146.     GC gc;
  147.  
  148.     values.foreground = 0;
  149.     gc = XCreateGC (dpy, p, GCForeground, &values );
  150.     XFillRectangle( dpy, p, gc, 0, 0, width, height );
  151.     XSetForeground (dpy, gc, 1);
  152.     if (!ellipse)
  153.     XmuFillRoundedRectangle( dpy, p, gc, 0, 0, (int)width, (int)height,
  154.                  ew, eh );
  155.     else
  156.     XFillArc( dpy, p, gc, 0, 0, width, height, 0, 360*64 );
  157.     XShapeCombineMask( dpy, XtWindow(w), ShapeBounding, 
  158.                -(w->core.border_width), -(w->core.border_width),
  159.                p, ShapeSet );
  160.     if (w->core.border_width) {
  161.     XSetForeground (dpy, gc, 0);
  162.     XFillRectangle( dpy, p, gc, 0, 0, width, height );
  163.     XSetForeground (dpy, gc, 1);
  164.     if (!ellipse)
  165.         XmuFillRoundedRectangle( dpy, p, gc, 0, 0,
  166.                      (int)w->core.width, (int)w->core.height,
  167.                      ew, eh );
  168.     else
  169.         XFillArc( dpy, p, gc, 0, 0, w->core.width, w->core.height,
  170.               0, 360*64 );
  171.     XShapeCombineMask( dpy, XtWindow(w), ShapeClip, 0, 0, p, ShapeSet );
  172.     } else {
  173.     XShapeCombineMask( XtDisplay(w), XtWindow(w),
  174.                ShapeClip, 0, 0, None, ShapeSet );
  175.     }
  176.     XFreePixmap( dpy, p );
  177.     XFreeGC (dpy, gc);
  178. }
  179.