home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xaw / Logo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-22  |  7.9 KB  |  282 lines

  1. /* $XConsortium: Logo.c,v 1.25 91/05/22 16:56:51 converse Exp $ */
  2.  
  3. /*
  4. Copyright 1988 by the Massachusetts Institute of Technology
  5.  
  6. Permission to use, copy, modify, and distribute this
  7. software and its documentation for any purpose and without
  8. fee is hereby granted, provided that the above copyright
  9. notice appear in all copies and that both that copyright
  10. notice and this permission notice appear in supporting
  11. documentation, and that the name of M.I.T. not be used in
  12. advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.
  14. M.I.T. makes no representations about the suitability of
  15. this software for any purpose.  It is provided "as is"
  16. without express or implied warranty.
  17. */
  18.  
  19. #include <X11/StringDefs.h>
  20. #include <X11/IntrinsicP.h>
  21. #include <X11/Xaw/XawInit.h>
  22. #include <X11/Xaw/LogoP.h>
  23. #include <X11/extensions/shape.h>
  24.  
  25. static XtResource resources[] = {
  26.     {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
  27.         XtOffsetOf(LogoRec,logo.fgpixel), XtRString,
  28.        (XtPointer) XtDefaultForeground},
  29.     {XtNshapeWindow, XtCShapeWindow, XtRBoolean, sizeof (Boolean),
  30.        XtOffsetOf(LogoRec,logo.shape_window), XtRImmediate, 
  31.        (XtPointer) FALSE},
  32. };
  33.  
  34. static void Initialize(), Realize(), Destroy(), Redisplay(), Resize();
  35. static Boolean SetValues();
  36.  
  37. LogoClassRec logoClassRec = {
  38.     { /* core fields */
  39.     /* superclass        */    (WidgetClass) &simpleClassRec,
  40.     /* class_name        */    "Logo",
  41.     /* widget_size        */    sizeof(LogoRec),
  42.     /* class_initialize        */    XawInitializeWidgetSet,
  43.     /* class_part_initialize    */    NULL,
  44.     /* class_inited        */    FALSE,
  45.     /* initialize        */    Initialize,
  46.     /* initialize_hook        */    NULL,
  47.     /* realize            */    Realize,
  48.     /* actions            */    NULL,
  49.     /* num_actions        */    0,
  50.     /* resources        */    resources,
  51.     /* resource_count        */    XtNumber(resources),
  52.     /* xrm_class        */    NULLQUARK,
  53.     /* compress_motion        */    TRUE,
  54.     /* compress_exposure    */    TRUE,
  55.     /* compress_enterleave    */    TRUE,
  56.     /* visible_interest        */    FALSE,
  57.     /* destroy            */    Destroy,
  58.     /* resize            */    Resize,
  59.     /* expose            */    Redisplay,
  60.     /* set_values        */    SetValues,
  61.     /* set_values_hook        */    NULL,
  62.     /* set_values_almost    */    XtInheritSetValuesAlmost,
  63.     /* get_values_hook        */    NULL,
  64.     /* accept_focus        */    NULL,
  65.     /* version            */    XtVersion,
  66.     /* callback_private        */    NULL,
  67.     /* tm_table            */    NULL,
  68.     /* query_geometry        */    XtInheritQueryGeometry,
  69.     /* display_accelerator    */    XtInheritDisplayAccelerator,
  70.     /* extension        */    NULL
  71.     },
  72.     { /* simple fields */
  73.     /* change_sensitive         */      XtInheritChangeSensitive
  74.     },
  75.     { /* logo fields */
  76.     /* ignore                   */      0
  77.     }
  78. };
  79.  
  80. WidgetClass logoWidgetClass = (WidgetClass) &logoClassRec;
  81.  
  82.  
  83. /*****************************************************************************
  84.  *                                         *
  85.  *               private utility routines                 *
  86.  *                                         *
  87.  *****************************************************************************/
  88.  
  89. static void create_gcs (w)
  90.     LogoWidget w;
  91. {
  92.     XGCValues v;
  93.  
  94.     v.foreground = w->logo.fgpixel;
  95.     w->logo.foreGC = XtGetGC ((Widget) w, GCForeground, &v);
  96.     v.foreground = w->core.background_pixel;
  97.     w->logo.backGC = XtGetGC ((Widget) w, GCForeground, &v);
  98. }
  99.  
  100. static void check_shape (w)
  101.     LogoWidget w;
  102. {
  103.     if (w->logo.shape_window) {
  104.     int event_base, error_base;
  105.  
  106.     if (!XShapeQueryExtension (XtDisplay (w), &event_base, &error_base))
  107.       w->logo.shape_window = FALSE;
  108.     }
  109. }
  110.  
  111. /* ARGSUSED */
  112. static void unset_shape (w)
  113.     LogoWidget w;
  114. {
  115.     XSetWindowAttributes attr;
  116.     unsigned long mask;
  117.     Display *dpy = XtDisplay ((Widget) w);
  118.     Window win = XtWindow ((Widget) w);
  119.  
  120.     if (win == None) return;
  121.  
  122.     if (w->core.background_pixmap != None && 
  123.     w->core.background_pixmap != XtUnspecifiedPixmap) {
  124.     attr.background_pixmap = w->core.background_pixmap;
  125.     mask = CWBackPixmap;
  126.     } else {
  127.     attr.background_pixel = w->core.background_pixel;
  128.     mask = CWBackPixel;
  129.     }
  130.     XChangeWindowAttributes (dpy, win, mask, &attr);
  131.     XShapeCombineMask (dpy, win, ShapeBounding, 0, 0, None, ShapeSet);
  132.     if (!w->logo.foreGC) create_gcs (w);
  133.     w->logo.need_shaping = w->logo.shape_window;
  134. }
  135.  
  136. static void set_shape (w)
  137.     LogoWidget w;
  138. {
  139.     GC ones, zeros;
  140.     Display *dpy = XtDisplay ((Widget) w);
  141.     Window win = XtWindow ((Widget) w);
  142.     unsigned int width = (unsigned int) w->core.width;
  143.     unsigned int height = (unsigned int) w->core.height;
  144.     Pixmap pm = XCreatePixmap (dpy, win, width, height, (unsigned int) 1);
  145.     XGCValues v;
  146.  
  147.     v.foreground = (Pixel) 1;
  148.     v.background = (Pixel) 0;
  149.     ones = XCreateGC (dpy, pm, (GCForeground | GCBackground), &v);
  150.     v.foreground = (Pixel) 0;
  151.     v.background = (Pixel) 1;
  152.     zeros = XCreateGC (dpy, pm, (GCForeground | GCBackground), &v);
  153.  
  154.     if (pm && ones && zeros) {
  155.     int x = 0, y = 0;
  156.     Widget parent;
  157.  
  158.     XmuDrawLogo (dpy, pm, ones, zeros, 0, 0, width, height);
  159.     for (parent = (Widget) w; XtParent(parent);
  160.          parent = XtParent(parent)) {
  161.         x += parent->core.x + parent->core.border_width;
  162.         y += parent->core.y + parent->core.border_width;
  163.     }
  164.     XShapeCombineMask (dpy, XtWindow (parent), ShapeBounding,
  165.                x, y, pm, ShapeSet);
  166.     w->logo.need_shaping = FALSE;
  167.     } else {
  168.     unset_shape (w);
  169.     }
  170.     if (ones) XFreeGC (dpy, ones);
  171.     if (zeros) XFreeGC (dpy, zeros);
  172.     if (pm) XFreePixmap (dpy, pm);
  173. }
  174.  
  175.  
  176. /*****************************************************************************
  177.  *                                         *
  178.  *                 class methods                     *
  179.  *                                         *
  180.  *****************************************************************************/
  181.  
  182. /* ARGSUSED */
  183. static void Initialize (request, new)
  184.     Widget request, new;
  185. {
  186.     LogoWidget w = (LogoWidget)new;
  187.  
  188.     if (w->core.width < 1) w->core.width = 100;
  189.     if (w->core.height < 1) w->core.height = 100;
  190.  
  191.     w->logo.foreGC = (GC) NULL;
  192.     w->logo.backGC = (GC) NULL;
  193.     check_shape (w);
  194.     w->logo.need_shaping = w->logo.shape_window;
  195. }
  196.  
  197. static void Destroy (gw)
  198.     Widget gw;
  199. {
  200.     LogoWidget w = (LogoWidget) gw;
  201.     if (w->logo.foreGC) {
  202.     XtReleaseGC (gw, w->logo.foreGC);
  203.     w->logo.foreGC = (GC) NULL;
  204.     }
  205.     if (w->logo.backGC) {
  206.     XtReleaseGC (gw, w->logo.backGC);
  207.     w->logo.backGC = (GC) NULL;
  208.     }
  209. }
  210.  
  211. static void Realize (gw, valuemaskp, attr)
  212.     Widget gw;
  213.     XtValueMask *valuemaskp;
  214.     XSetWindowAttributes *attr;
  215. {
  216.     LogoWidget w = (LogoWidget) gw;
  217.  
  218.     if (w->logo.shape_window) {
  219.     attr->background_pixel = w->logo.fgpixel;  /* going to shape */
  220.     *valuemaskp |= CWBackPixel;
  221.     } else
  222.       create_gcs (w);
  223.     (*logoWidgetClass->core_class.superclass->core_class.realize)
  224.     (gw, valuemaskp, attr);
  225. }
  226.  
  227. static void Resize (gw)
  228.     Widget gw;
  229. {
  230.     LogoWidget w = (LogoWidget) gw;
  231.  
  232.     if (w->logo.shape_window) set_shape (w);
  233. }
  234.  
  235. /* ARGSUSED */
  236. static void Redisplay (gw, event, region)
  237.     Widget gw;
  238.     XEvent *event;        /* unused */
  239.     Region region;        /* unused */
  240. {
  241.     LogoWidget w = (LogoWidget) gw;
  242.  
  243.     if (w->logo.shape_window) {
  244.     if (w->logo.need_shaping) set_shape (w);  /* may change shape flag */
  245.     }
  246.     if (!w->logo.shape_window)
  247.       XmuDrawLogo(XtDisplay(w), XtWindow(w), w->logo.foreGC, w->logo.backGC,
  248.           0, 0, (unsigned int) w->core.width,
  249.           (unsigned int) w->core.height);
  250. }
  251.  
  252. /* ARGSUSED */
  253. static Boolean SetValues (gcurrent, grequest, gnew)
  254.     Widget gcurrent, grequest, gnew;
  255. {
  256.     LogoWidget current = (LogoWidget) gcurrent;
  257.     LogoWidget new = (LogoWidget) gnew;
  258.     Boolean redisplay = FALSE;
  259.  
  260.    check_shape (new);            /* validate shape_window */
  261.  
  262.     if ((new->logo.fgpixel != current->logo.fgpixel) ||
  263.     (new->core.background_pixel != current->core.background_pixel)) {
  264.     Destroy (gnew);
  265.     if (!new->logo.shape_window) create_gcs (new);
  266.     redisplay = TRUE;
  267.     }
  268.    
  269.    if (new->logo.shape_window != current->logo.shape_window) {
  270.        if (new->logo.shape_window) {
  271.        Destroy (gnew);
  272.        set_shape (new);
  273.        redisplay = FALSE;
  274.        } else {
  275.        unset_shape (new);        /* creates new GCs */
  276.        redisplay = TRUE;
  277.        }
  278.    }
  279.  
  280.    return (redisplay);
  281. }
  282.