Blame GLwDrawA.c

Packit 34ce77
/*
Packit 34ce77
 * (c) Copyright 1993, Silicon Graphics, Inc.
Packit 34ce77
 * ALL RIGHTS RESERVED 
Packit 34ce77
 * Permission to use, copy, modify, and distribute this software for 
Packit 34ce77
 * any purpose and without fee is hereby granted, provided that the above
Packit 34ce77
 * copyright notice appear in all copies and that both the copyright notice
Packit 34ce77
 * and this permission notice appear in supporting documentation, and that 
Packit 34ce77
 * the name of Silicon Graphics, Inc. not be used in advertising
Packit 34ce77
 * or publicity pertaining to distribution of the software without specific,
Packit 34ce77
 * written prior permission. 
Packit 34ce77
 *
Packit 34ce77
 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
Packit 34ce77
 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
Packit 34ce77
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
Packit 34ce77
 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
Packit 34ce77
 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
Packit 34ce77
 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
Packit 34ce77
 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
Packit 34ce77
 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
Packit 34ce77
 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
Packit 34ce77
 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
Packit 34ce77
 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
Packit 34ce77
 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
Packit 34ce77
 * 
Packit 34ce77
 * 
Packit 34ce77
 * US Government Users Restricted Rights 
Packit 34ce77
 * Use, duplication, or disclosure by the Government is subject to
Packit 34ce77
 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
Packit 34ce77
 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
Packit 34ce77
 * clause at DFARS 252.227-7013 and/or in similar or successor
Packit 34ce77
 * clauses in the FAR or the DOD or NASA FAR Supplement.
Packit 34ce77
 * Unpublished-- rights reserved under the copyright laws of the
Packit 34ce77
 * United States.  Contractor/manufacturer is Silicon Graphics,
Packit 34ce77
 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
Packit 34ce77
 *
Packit 34ce77
 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
Packit 34ce77
 */
Packit 34ce77
Packit 34ce77
/*
Packit 34ce77
 *
Packit 34ce77
 * This file has been slightly modified from the original for use with Mesa
Packit 34ce77
 *
Packit 34ce77
 *     Jeroen van der Zijp
Packit 34ce77
 *
Packit 34ce77
 *     jvz@cyberia.cfdrc.com
Packit 34ce77
 *
Packit 34ce77
 */
Packit 34ce77
#include <X11/IntrinsicP.h>
Packit 34ce77
#include <X11/StringDefs.h>
Packit 34ce77
#include <GL/glx.h>
Packit 34ce77
#include <GL/gl.h>
Packit 34ce77
#ifdef __GLX_MOTIF
Packit 34ce77
#include <Xm/PrimitiveP.h>
Packit 34ce77
#include "GLwMDrawAP.h"
Packit 34ce77
#else 
Packit 34ce77
#include "GLwDrawAP.h"
Packit 34ce77
#endif 
Packit 34ce77
#include <assert.h>
Packit 34ce77
#include <stdio.h>
Packit 34ce77
Packit 34ce77
#ifdef __GLX_MOTIF
Packit 34ce77
#define GLwDrawingAreaWidget             GLwMDrawingAreaWidget
Packit 34ce77
#define GLwDrawingAreaClassRec           GLwMDrawingAreaClassRec
Packit 34ce77
#define glwDrawingAreaClassRec           glwMDrawingAreaClassRec
Packit 34ce77
#define glwDrawingAreaWidgetClass        glwMDrawingAreaWidgetClass
Packit 34ce77
#define GLwDrawingAreaRec                GLwMDrawingAreaRec
Packit 34ce77
#endif 
Packit 34ce77
Packit 34ce77
#define ATTRIBLIST_SIZE 32
Packit 34ce77
Packit 34ce77
#define offset(field) XtOffset(GLwDrawingAreaWidget,glwDrawingArea.field)
Packit 34ce77
Packit 34ce77
Packit 34ce77
/* forward definitions */
Packit 34ce77
static void createColormap(GLwDrawingAreaWidget w,int offset,XrmValue *value);
Packit 34ce77
static void Initialize(GLwDrawingAreaWidget req,GLwDrawingAreaWidget neww,ArgList args,Cardinal *num_args);
Packit 34ce77
static void Realize(Widget w,Mask *valueMask,XSetWindowAttributes *attributes);
Packit 34ce77
static void Redraw(GLwDrawingAreaWidget w,XEvent *event,Region region);
Packit 34ce77
static void Resize(GLwDrawingAreaWidget glw);
Packit 34ce77
static void Destroy(GLwDrawingAreaWidget glw);
Packit 34ce77
static void glwInput(GLwDrawingAreaWidget glw,XEvent *event,String *params,Cardinal *numParams);
Packit 34ce77
Packit 34ce77
Packit 34ce77
Packit 34ce77
static char defaultTranslations[] =
Packit 34ce77
#ifdef __GLX_MOTIF
Packit 34ce77
     "<Key>osfHelp:PrimitiveHelp() \n"
Packit 34ce77
#endif
Packit 34ce77
    "<KeyDown>:   glwInput() \n\
Packit 34ce77
     <KeyUp>:     glwInput() \n\
Packit 34ce77
     <BtnDown>:   glwInput() \n\
Packit 34ce77
     <BtnUp>:     glwInput() \n\
Packit 34ce77
     <BtnMotion>: glwInput() ";
Packit 34ce77
Packit 34ce77
Packit 34ce77
static XtActionsRec actions[] = {
Packit 34ce77
  {"glwInput",(XtActionProc)glwInput},                /* key or mouse input */
Packit 34ce77
  };
Packit 34ce77
Packit 34ce77
Packit 34ce77
/*
Packit 34ce77
 * There is a bit of unusual handling of the resources here.
Packit 34ce77
 * Because Xt insists on allocating the colormap resource when it is
Packit 34ce77
 * processing the core resources (even if we redeclare the colormap
Packit 34ce77
 * resource here, we need to do a little trick.  When Xt first allocates
Packit 34ce77
 * the colormap, we allow it to allocate the default one, since we have
Packit 34ce77
 * not yet determined the appropriate visual (which is determined from
Packit 34ce77
 * resources parsed after the colormap).  We also let it allocate colors
Packit 34ce77
 * in that default colormap.
Packit 34ce77
 *
Packit 34ce77
 * In the initialize proc we calculate the actual visual.  Then, we
Packit 34ce77
 * reobtain the colormap resource using XtGetApplicationResources in
Packit 34ce77
 * the initialize proc.  If requested, we also reallocate colors in
Packit 34ce77
 * that colormap using the same method.
Packit 34ce77
 */
Packit 34ce77
Packit 34ce77
static XtResource resources[] = {
Packit 34ce77
  /* The GLX attributes.  Add any new attributes here */
Packit 34ce77
Packit 34ce77
  {GLwNbufferSize, GLwCBufferSize, XtRInt, sizeof (int),
Packit 34ce77
       offset(bufferSize), XtRImmediate, (XtPointer) 0},
Packit 34ce77
  
Packit 34ce77
  {GLwNlevel, GLwCLevel, XtRInt, sizeof (int),
Packit 34ce77
       offset(level), XtRImmediate, (XtPointer) 0},
Packit 34ce77
  
Packit 34ce77
  {GLwNrgba, GLwCRgba, XtRBoolean, sizeof (Boolean),
Packit 34ce77
       offset(rgba), XtRImmediate, (XtPointer) FALSE},
Packit 34ce77
  
Packit 34ce77
  {GLwNdoublebuffer, GLwCDoublebuffer, XtRBoolean, sizeof (Boolean),
Packit 34ce77
       offset(doublebuffer), XtRImmediate, (XtPointer) FALSE},
Packit 34ce77
  
Packit 34ce77
  {GLwNstereo, GLwCStereo, XtRBoolean, sizeof (Boolean),
Packit 34ce77
       offset(stereo), XtRImmediate, (XtPointer) FALSE},
Packit 34ce77
  
Packit 34ce77
  {GLwNauxBuffers, GLwCAuxBuffers, XtRInt, sizeof (int),
Packit 34ce77
       offset(auxBuffers), XtRImmediate, (XtPointer) 0},
Packit 34ce77
  
Packit 34ce77
  {GLwNredSize, GLwCColorSize, XtRInt, sizeof (int),
Packit 34ce77
       offset(redSize), XtRImmediate, (XtPointer) 1},
Packit 34ce77
  
Packit 34ce77
  {GLwNgreenSize, GLwCColorSize, XtRInt, sizeof (int),
Packit 34ce77
       offset(greenSize), XtRImmediate, (XtPointer) 1},
Packit 34ce77
  
Packit 34ce77
  {GLwNblueSize, GLwCColorSize, XtRInt, sizeof (int),
Packit 34ce77
       offset(blueSize), XtRImmediate, (XtPointer) 1},
Packit 34ce77
  
Packit 34ce77
  {GLwNalphaSize, GLwCAlphaSize, XtRInt, sizeof (int),
Packit 34ce77
       offset(alphaSize), XtRImmediate, (XtPointer) 0},
Packit 34ce77
  
Packit 34ce77
  {GLwNdepthSize, GLwCDepthSize, XtRInt, sizeof (int),
Packit 34ce77
       offset(depthSize), XtRImmediate, (XtPointer) 0},
Packit 34ce77
  
Packit 34ce77
  {GLwNstencilSize, GLwCStencilSize, XtRInt, sizeof (int),
Packit 34ce77
       offset(stencilSize), XtRImmediate, (XtPointer) 0},
Packit 34ce77
  
Packit 34ce77
  {GLwNaccumRedSize, GLwCAccumColorSize, XtRInt, sizeof (int),
Packit 34ce77
       offset(accumRedSize), XtRImmediate, (XtPointer) 0},
Packit 34ce77
  
Packit 34ce77
  {GLwNaccumGreenSize, GLwCAccumColorSize, XtRInt, sizeof (int),
Packit 34ce77
       offset(accumGreenSize), XtRImmediate, (XtPointer) 0},
Packit 34ce77
  
Packit 34ce77
  {GLwNaccumBlueSize, GLwCAccumColorSize, XtRInt, sizeof (int),
Packit 34ce77
       offset(accumBlueSize), XtRImmediate, (XtPointer) 0},
Packit 34ce77
  
Packit 34ce77
  {GLwNaccumAlphaSize, GLwCAccumAlphaSize, XtRInt, sizeof (int),
Packit 34ce77
       offset(accumAlphaSize), XtRImmediate, (XtPointer) 0},
Packit 34ce77
  
Packit 34ce77
  /* the attribute list */
Packit 34ce77
  {GLwNattribList, GLwCAttribList, XtRPointer, sizeof(int *),
Packit 34ce77
       offset(attribList), XtRImmediate, (XtPointer) NULL},
Packit 34ce77
Packit 34ce77
  /* the visual info */
Packit 34ce77
  {GLwNvisualInfo, GLwCVisualInfo, GLwRVisualInfo, sizeof (XVisualInfo *),
Packit 34ce77
       offset(visualInfo), XtRImmediate, (XtPointer) NULL},
Packit 34ce77
Packit 34ce77
  /* miscellaneous resources */
Packit 34ce77
  {GLwNinstallColormap, GLwCInstallColormap, XtRBoolean, sizeof (Boolean),
Packit 34ce77
       offset(installColormap), XtRImmediate, (XtPointer) TRUE},
Packit 34ce77
Packit 34ce77
  {GLwNallocateBackground, GLwCAllocateColors, XtRBoolean, sizeof (Boolean),
Packit 34ce77
       offset(allocateBackground), XtRImmediate, (XtPointer) FALSE},
Packit 34ce77
Packit 34ce77
  {GLwNallocateOtherColors, GLwCAllocateColors, XtRBoolean, sizeof (Boolean),
Packit 34ce77
       offset(allocateOtherColors), XtRImmediate, (XtPointer) FALSE},
Packit 34ce77
Packit 34ce77
  {GLwNinstallBackground, GLwCInstallBackground, XtRBoolean, sizeof (Boolean),
Packit 34ce77
       offset(installBackground), XtRImmediate, (XtPointer) TRUE},
Packit 34ce77
Packit 34ce77
  {GLwNginitCallback, GLwCCallback, XtRCallback, sizeof (XtCallbackList),
Packit 34ce77
       offset(ginitCallback), XtRImmediate, (XtPointer) NULL},
Packit 34ce77
Packit 34ce77
  {GLwNinputCallback, GLwCCallback, XtRCallback, sizeof (XtCallbackList),
Packit 34ce77
       offset(inputCallback), XtRImmediate, (XtPointer) NULL},
Packit 34ce77
Packit 34ce77
  {GLwNresizeCallback, GLwCCallback, XtRCallback, sizeof (XtCallbackList),
Packit 34ce77
       offset(resizeCallback), XtRImmediate, (XtPointer) NULL},
Packit 34ce77
Packit 34ce77
  {GLwNexposeCallback, GLwCCallback, XtRCallback, sizeof (XtCallbackList),
Packit 34ce77
       offset(exposeCallback), XtRImmediate, (XtPointer) NULL},
Packit 34ce77
Packit 34ce77
  /* Changes to Motif primitive resources */
Packit 34ce77
#ifdef __GLX_MOTIF
Packit 34ce77
  {XmNtraversalOn, XmCTraversalOn, XmRBoolean, sizeof (Boolean),
Packit 34ce77
   XtOffset (GLwDrawingAreaWidget, primitive.traversal_on), XmRImmediate,
Packit 34ce77
   (XtPointer)FALSE},
Packit 34ce77
  
Packit 34ce77
  /* highlighting is normally disabled, as when Motif tries to disable
Packit 34ce77
   * highlighting, it tries to reset the color back to the parent's
Packit 34ce77
   * background (usually Motif blue).  Unfortunately, that is in a
Packit 34ce77
   * different colormap, and doesn't work too well.
Packit 34ce77
   */
Packit 34ce77
  {XmNhighlightOnEnter, XmCHighlightOnEnter, XmRBoolean, sizeof (Boolean),
Packit 34ce77
   XtOffset (GLwDrawingAreaWidget, primitive.highlight_on_enter),
Packit 34ce77
   XmRImmediate, (XtPointer) FALSE},
Packit 34ce77
  
Packit 34ce77
  {XmNhighlightThickness, XmCHighlightThickness, XmRHorizontalDimension,
Packit 34ce77
   sizeof (Dimension),
Packit 34ce77
   XtOffset (GLwDrawingAreaWidget, primitive.highlight_thickness),
Packit 34ce77
   XmRImmediate, (XtPointer) 0},
Packit 34ce77
#endif 
Packit 34ce77
  };
Packit 34ce77
Packit 34ce77
Packit 34ce77
/*
Packit 34ce77
** The following resources are reobtained using XtGetApplicationResources
Packit 34ce77
** in the initialize proc.
Packit 34ce77
*/
Packit 34ce77
Packit 34ce77
/* The colormap */
Packit 34ce77
static XtResource initializeResources[] = {
Packit 34ce77
  /* reobtain the colormap with the new visual */
Packit 34ce77
  {XtNcolormap, XtCColormap, XtRColormap, sizeof(Colormap),
Packit 34ce77
   XtOffset(GLwDrawingAreaWidget, core.colormap),
Packit 34ce77
   XtRCallProc,(XtPointer) createColormap},
Packit 34ce77
  };
Packit 34ce77
Packit 34ce77
Packit 34ce77
/* reallocate any colors we need in the new colormap */
Packit 34ce77
  
Packit 34ce77
/* The background is obtained only if the allocateBackground resource is TRUE*/
Packit 34ce77
static XtResource backgroundResources[] = {
Packit 34ce77
#ifdef __GLX_MOTIF
Packit 34ce77
  {XmNbackground, XmCBackground,XmRPixel, 
Packit 34ce77
   sizeof(Pixel),XtOffset(GLwDrawingAreaWidget,core.background_pixel),
Packit 34ce77
   XmRString,(XtPointer)"lightgrey"},
Packit 34ce77
   /*XmRCallProc,(XtPointer)_XmBackgroundColorDefault},*/
Packit 34ce77
Packit 34ce77
  {XmNbackgroundPixmap,XmCPixmap,XmRXmBackgroundPixmap, 
Packit 34ce77
   sizeof(Pixmap),XtOffset(GLwDrawingAreaWidget,core.background_pixmap),
Packit 34ce77
   XmRImmediate,(XtPointer)XmUNSPECIFIED_PIXMAP},
Packit 34ce77
Packit 34ce77
#else
Packit 34ce77
  {XtNbackground,XtCBackground,XtRPixel,sizeof(Pixel),
Packit 34ce77
   XtOffset(GLwDrawingAreaWidget,core.background_pixel),
Packit 34ce77
   XtRString,(XtPointer)"lightgrey"},
Packit 34ce77
   /*XtRString,(XtPointer)"XtDefaultBackground"},*/
Packit 34ce77
Packit 34ce77
  {XtNbackgroundPixmap, XtCPixmap, XtRPixmap, sizeof(Pixmap),
Packit 34ce77
   XtOffset(GLwDrawingAreaWidget,core.background_pixmap),
Packit 34ce77
   XtRImmediate,(XtPointer)XtUnspecifiedPixmap},
Packit 34ce77
#endif  
Packit 34ce77
  };
Packit 34ce77
Packit 34ce77
Packit 34ce77
Packit 34ce77
/* The other colors such as the foreground are allocated only if
Packit 34ce77
 * allocateOtherColors are set.  These resources only exist in Motif.
Packit 34ce77
 */
Packit 34ce77
#ifdef __GLX_MOTIF
Packit 34ce77
static XtResource otherColorResources[] = {
Packit 34ce77
  {XmNforeground,XmCForeground,XmRPixel, 
Packit 34ce77
   sizeof(Pixel),XtOffset(GLwDrawingAreaWidget,primitive.foreground),
Packit 34ce77
   XmRString,(XtPointer)"lightgrey"},
Packit 34ce77
   /*XmRCallProc, (XtPointer) _XmForegroundColorDefault},*/
Packit 34ce77
Packit 34ce77
  {XmNhighlightColor,XmCHighlightColor,XmRPixel,sizeof(Pixel),
Packit 34ce77
   XtOffset(GLwDrawingAreaWidget,primitive.highlight_color),
Packit 34ce77
   XmRString,(XtPointer)"lightgrey"},
Packit 34ce77
   /*XmRCallProc,(XtPointer)_XmHighlightColorDefault},*/
Packit 34ce77
Packit 34ce77
  {XmNhighlightPixmap,XmCHighlightPixmap,XmRPrimHighlightPixmap,
Packit 34ce77
   sizeof(Pixmap),
Packit 34ce77
   XtOffset(GLwDrawingAreaWidget,primitive.highlight_pixmap),
Packit 34ce77
   XmRImmediate,(XtPointer)XmUNSPECIFIED_PIXMAP},
Packit 34ce77
   /*XmRCallProc,(XtPointer)_XmPrimitiveHighlightPixmapDefault},*/
Packit 34ce77
  };
Packit 34ce77
#endif
Packit 34ce77
Packit 34ce77
Packit 34ce77
#undef offset
Packit 34ce77
Packit 34ce77
Packit 34ce77
GLwDrawingAreaClassRec glwDrawingAreaClassRec = {
Packit 34ce77
  { /* core fields */
Packit 34ce77
#ifdef __GLX_MOTIF
Packit 34ce77
    /* superclass                */        (WidgetClass) &xmPrimitiveClassRec,
Packit 34ce77
    /* class_name                */        "GLwMDrawingArea",
Packit 34ce77
#else /* not __GLX_MOTIF */
Packit 34ce77
    /* superclass                */        (WidgetClass) &widgetClassRec,
Packit 34ce77
    /* class_name                */        "GLwDrawingArea",
Packit 34ce77
#endif /* __GLX_MOTIF */
Packit 34ce77
    /* widget_size               */        sizeof(GLwDrawingAreaRec),
Packit 34ce77
    /* class_initialize          */        NULL,
Packit 34ce77
    /* class_part_initialize     */        NULL,
Packit 34ce77
    /* class_inited              */        FALSE,
Packit 34ce77
    /* initialize                */        (XtInitProc) Initialize,
Packit 34ce77
    /* initialize_hook           */        NULL,
Packit 34ce77
    /* realize                   */        Realize,
Packit 34ce77
    /* actions                   */        actions,
Packit 34ce77
    /* num_actions               */        XtNumber(actions),
Packit 34ce77
    /* resources                 */        resources,
Packit 34ce77
    /* num_resources             */        XtNumber(resources),
Packit 34ce77
    /* xrm_class                 */        NULLQUARK,
Packit 34ce77
    /* compress_motion           */        TRUE,
Packit 34ce77
    /* compress_exposure         */        TRUE,
Packit 34ce77
    /* compress_enterleave       */        TRUE,
Packit 34ce77
    /* visible_interest          */        TRUE,
Packit 34ce77
    /* destroy                   */        (XtWidgetProc) Destroy,
Packit 34ce77
    /* resize                    */        (XtWidgetProc) Resize,
Packit 34ce77
    /* expose                    */        (XtExposeProc) Redraw,
Packit 34ce77
    /* set_values                */        NULL,
Packit 34ce77
    /* set_values_hook           */        NULL,
Packit 34ce77
    /* set_values_almost         */        XtInheritSetValuesAlmost,
Packit 34ce77
    /* get_values_hook           */        NULL,
Packit 34ce77
    /* accept_focus              */        NULL,
Packit 34ce77
    /* version                   */        XtVersion,
Packit 34ce77
    /* callback_private          */        NULL,
Packit 34ce77
    /* tm_table                  */        defaultTranslations,
Packit 34ce77
    /* query_geometry            */        XtInheritQueryGeometry,
Packit 34ce77
    /* display_accelerator       */        XtInheritDisplayAccelerator,
Packit 34ce77
    /* extension                 */        NULL
Packit 34ce77
  },
Packit 34ce77
#ifdef __GLX_MOTIF /* primitive resources */
Packit 34ce77
  {
Packit 34ce77
    /* border_highlight          */        XmInheritBorderHighlight,
Packit 34ce77
    /* border_unhighlight        */        XmInheritBorderUnhighlight,
Packit 34ce77
    /* translations              */        XtInheritTranslations,
Packit 34ce77
    /* arm_and_activate          */        NULL,
Packit 34ce77
    /* get_resources             */        NULL,
Packit 34ce77
    /* num get_resources         */        0,
Packit 34ce77
    /* extension                 */        NULL,                                
Packit 34ce77
  }
Packit 34ce77
#endif 
Packit 34ce77
  };
Packit 34ce77
Packit 34ce77
WidgetClass glwDrawingAreaWidgetClass=(WidgetClass)&glwDrawingAreaClassRec;
Packit 34ce77
Packit 34ce77
Packit 34ce77
Packit 34ce77
static void error(Widget w,char* string){
Packit 34ce77
  char buf[100];
Packit 34ce77
#ifdef __GLX_MOTIF
Packit 34ce77
  sprintf(buf,"GLwMDrawingArea: %s\n",string);
Packit 34ce77
#else
Packit 34ce77
  sprintf(buf,"GLwDrawingArea: %s\n",string);
Packit 34ce77
#endif
Packit 34ce77
  XtAppError(XtWidgetToApplicationContext(w),buf);
Packit 34ce77
  }
Packit 34ce77
Packit 34ce77
Packit 34ce77
static void warning(Widget w,char* string){
Packit 34ce77
  char buf[100];
Packit 34ce77
#ifdef __GLX_MOTIF
Packit 34ce77
  sprintf (buf, "GLwMDraw: %s\n", string);
Packit 34ce77
#else
Packit 34ce77
  sprintf (buf, "GLwDraw: %s\n", string);
Packit 34ce77
#endif
Packit 34ce77
  XtAppWarning(XtWidgetToApplicationContext(w), buf);
Packit 34ce77
  }
Packit 34ce77
Packit 34ce77
Packit 34ce77
Packit 34ce77
/* Initialize the attribList based on the attributes */
Packit 34ce77
static void createAttribList(GLwDrawingAreaWidget w){
Packit 34ce77
  int *ptr;
Packit 34ce77
  w->glwDrawingArea.attribList = (int*)XtMalloc(ATTRIBLIST_SIZE*sizeof(int));
Packit 34ce77
  if(!w->glwDrawingArea.attribList){
Packit 34ce77
    error((Widget)w,"Unable to allocate attribute list");
Packit 34ce77
    }
Packit 34ce77
  ptr = w->glwDrawingArea.attribList;
Packit 34ce77
  *ptr++ = GLX_BUFFER_SIZE;
Packit 34ce77
  *ptr++ = w->glwDrawingArea.bufferSize;
Packit 34ce77
  *ptr++ = GLX_LEVEL;
Packit 34ce77
  *ptr++ = w->glwDrawingArea.level;
Packit 34ce77
  if(w->glwDrawingArea.rgba) *ptr++ = GLX_RGBA;
Packit 34ce77
  if(w->glwDrawingArea.doublebuffer) *ptr++ = GLX_DOUBLEBUFFER;
Packit 34ce77
  if(w->glwDrawingArea.stereo) *ptr++ = GLX_STEREO;
Packit 34ce77
  *ptr++ = GLX_AUX_BUFFERS;
Packit 34ce77
  *ptr++ = w->glwDrawingArea.auxBuffers;
Packit 34ce77
  *ptr++ = GLX_RED_SIZE;
Packit 34ce77
  *ptr++ = w->glwDrawingArea.redSize;
Packit 34ce77
  *ptr++ = GLX_GREEN_SIZE;
Packit 34ce77
  *ptr++ = w->glwDrawingArea.greenSize;
Packit 34ce77
  *ptr++ = GLX_BLUE_SIZE;
Packit 34ce77
  *ptr++ = w->glwDrawingArea.blueSize;
Packit 34ce77
  *ptr++ = GLX_ALPHA_SIZE;
Packit 34ce77
  *ptr++ = w->glwDrawingArea.alphaSize;
Packit 34ce77
  *ptr++ = GLX_DEPTH_SIZE;
Packit 34ce77
  *ptr++ = w->glwDrawingArea.depthSize;
Packit 34ce77
  *ptr++ = GLX_STENCIL_SIZE;
Packit 34ce77
  *ptr++ = w->glwDrawingArea.stencilSize;
Packit 34ce77
  *ptr++ = GLX_ACCUM_RED_SIZE;
Packit 34ce77
  *ptr++ = w->glwDrawingArea.accumRedSize;
Packit 34ce77
  *ptr++ = GLX_ACCUM_GREEN_SIZE;
Packit 34ce77
  *ptr++ = w->glwDrawingArea.accumGreenSize;
Packit 34ce77
  *ptr++ = GLX_ACCUM_BLUE_SIZE;
Packit 34ce77
  *ptr++ = w->glwDrawingArea.accumBlueSize;
Packit 34ce77
  *ptr++ = GLX_ACCUM_ALPHA_SIZE;
Packit 34ce77
  *ptr++ = w->glwDrawingArea.accumAlphaSize;
Packit 34ce77
  *ptr++ = None;
Packit 34ce77
  assert((ptr-w->glwDrawingArea.attribList)
Packit 34ce77
  }
Packit 34ce77
Packit 34ce77
Packit 34ce77
Packit 34ce77
/* Initialize the visualInfo based on the attribute list */
Packit 34ce77
static void createVisualInfo(GLwDrawingAreaWidget w){
Packit 34ce77
  assert(w->glwDrawingArea.attribList);
Packit 34ce77
  w->glwDrawingArea.visualInfo=glXChooseVisual(XtDisplay(w),XScreenNumberOfScreen(XtScreen(w)),w->glwDrawingArea.attribList);
Packit 34ce77
  if(!w->glwDrawingArea.visualInfo) error((Widget)w,"requested visual not supported");
Packit 34ce77
  }
Packit 34ce77
Packit 34ce77
Packit 34ce77
Packit 34ce77
/* Initialize the colormap based on the visual info.
Packit 34ce77
 * This routine maintains a cache of visual-infos to colormaps.  If two
Packit 34ce77
 * widgets share the same visual info, they share the same colormap.
Packit 34ce77
 * This function is called by the callProc of the colormap resource entry.
Packit 34ce77
 */
Packit 34ce77
static void createColormap(GLwDrawingAreaWidget w,int offset,XrmValue *value){
Packit 34ce77
  static struct cmapCache { Visual *visual; Colormap cmap; } *cmapCache;
Packit 34ce77
  static int cacheEntries=0;
Packit 34ce77
  static int cacheMalloced=0;
Packit 34ce77
  register int i;
Packit 34ce77
    
Packit 34ce77
  assert(w->glwDrawingArea.visualInfo);
Packit 34ce77
Packit 34ce77
  /* see if we can find it in the cache */
Packit 34ce77
  for(i=0; i
Packit 34ce77
    if(cmapCache[i].visual==w->glwDrawingArea.visualInfo->visual){
Packit 34ce77
      value->addr=(XtPointer)(&cmapCache[i].cmap);
Packit 34ce77
      return;
Packit 34ce77
      }
Packit 34ce77
    }
Packit 34ce77
Packit 34ce77
  /* not in the cache, create a new entry */
Packit 34ce77
  if(cacheEntries >= cacheMalloced){
Packit 34ce77
    /* need to malloc a new one.  Since we are likely to have only a
Packit 34ce77
     * few colormaps, we allocate one the first time, and double
Packit 34ce77
     * each subsequent time.
Packit 34ce77
     */
Packit 34ce77
    if(cacheMalloced==0){
Packit 34ce77
      cacheMalloced=1;
Packit 34ce77
      cmapCache=(struct cmapCache*)XtMalloc(sizeof(struct cmapCache));
Packit 34ce77
      }
Packit 34ce77
    else{
Packit 34ce77
      cacheMalloced<<=1;
Packit 34ce77
      cmapCache=(struct cmapCache*)XtRealloc((char*)cmapCache,sizeof(struct cmapCache)*cacheMalloced);
Packit 34ce77
      }
Packit 34ce77
    }
Packit 34ce77
       
Packit 34ce77
  cmapCache[cacheEntries].cmap=XCreateColormap(XtDisplay(w),
Packit 34ce77
                                               RootWindow(XtDisplay(w),
Packit 34ce77
                                               w->glwDrawingArea.visualInfo->screen),
Packit 34ce77
                                               w->glwDrawingArea.visualInfo->visual,
Packit 34ce77
                                               AllocNone);
Packit 34ce77
  cmapCache[cacheEntries].visual=w->glwDrawingArea.visualInfo->visual;
Packit 34ce77
  value->addr=(XtPointer)(&cmapCache[cacheEntries++].cmap);
Packit 34ce77
  }
Packit 34ce77
Packit 34ce77
Packit 34ce77
Packit 34ce77
static void Initialize(GLwDrawingAreaWidget req,GLwDrawingAreaWidget neww,ArgList args,Cardinal *num_args){
Packit 34ce77
Packit 34ce77
  /* fix size */
Packit 34ce77
  if(req->core.width==0) neww->core.width=100;
Packit 34ce77
  if(req->core.height==0) neww->core.height=100;
Packit 34ce77
Packit 34ce77
  /* create the attribute list if needed */
Packit 34ce77
  neww->glwDrawingArea.myList=FALSE;
Packit 34ce77
  if(neww->glwDrawingArea.attribList==NULL){
Packit 34ce77
    neww->glwDrawingArea.myList=TRUE;
Packit 34ce77
    createAttribList(neww);
Packit 34ce77
    }
Packit 34ce77
Packit 34ce77
  /* Gotta have it */
Packit 34ce77
  assert(neww->glwDrawingArea.attribList);
Packit 34ce77
Packit 34ce77
  /* determine the visual info if needed */
Packit 34ce77
  neww->glwDrawingArea.myVisual=FALSE;
Packit 34ce77
  if(neww->glwDrawingArea.visualInfo==NULL){
Packit 34ce77
    neww->glwDrawingArea.myVisual=TRUE;
Packit 34ce77
    createVisualInfo(neww);
Packit 34ce77
    }
Packit 34ce77
Packit 34ce77
  /* Gotta have that too */
Packit 34ce77
  assert(neww->glwDrawingArea.visualInfo);
Packit 34ce77
Packit 34ce77
  neww->core.depth=neww->glwDrawingArea.visualInfo->depth;
Packit 34ce77
Packit 34ce77
  /* Reobtain the colormap and colors in it using XtGetApplicationResources*/
Packit 34ce77
  XtGetApplicationResources((Widget)neww,neww,initializeResources,XtNumber(initializeResources),args,*num_args);
Packit 34ce77
Packit 34ce77
  /* obtain the color resources if appropriate */
Packit 34ce77
  if(req->glwDrawingArea.allocateBackground){
Packit 34ce77
    XtGetApplicationResources((Widget)neww,neww,backgroundResources,XtNumber(backgroundResources),args,*num_args);
Packit 34ce77
    }
Packit 34ce77
Packit 34ce77
#ifdef __GLX_MOTIF
Packit 34ce77
  if(req->glwDrawingArea.allocateOtherColors){
Packit 34ce77
    XtGetApplicationResources((Widget)neww,neww,otherColorResources,XtNumber(otherColorResources),args,*num_args);
Packit 34ce77
    }
Packit 34ce77
#endif 
Packit 34ce77
  }
Packit 34ce77
Packit 34ce77
Packit 34ce77
Packit 34ce77
static void Realize(Widget w,Mask *valueMask,XSetWindowAttributes *attributes){
Packit 34ce77
  register GLwDrawingAreaWidget glw=(GLwDrawingAreaWidget)w;
Packit 34ce77
  GLwDrawingAreaCallbackStruct cb;
Packit 34ce77
  Widget parentShell;
Packit 34ce77
  Status status;
Packit 34ce77
  Window windows[2],*windowsReturn,*windowList;
Packit 34ce77
  int countReturn,i;
Packit 34ce77
   
Packit 34ce77
  /* if we haven't requested that the background be both installed and
Packit 34ce77
   * allocated, don't install it.
Packit 34ce77
   */
Packit 34ce77
  if(!(glw->glwDrawingArea.installBackground && glw->glwDrawingArea.allocateBackground)){
Packit 34ce77
    *valueMask&=~CWBackPixel;
Packit 34ce77
    }
Packit 34ce77
 
Packit 34ce77
  XtCreateWindow(w,(unsigned int)InputOutput,glw->glwDrawingArea.visualInfo->visual,*valueMask,attributes);
Packit 34ce77
Packit 34ce77
  /* if appropriate, call XSetWMColormapWindows to install the colormap */
Packit 34ce77
  if(glw->glwDrawingArea.installColormap){
Packit 34ce77
Packit 34ce77
    /* Get parent shell */
Packit 34ce77
    for(parentShell=XtParent(w); parentShell&&!XtIsShell(parentShell); parentShell=XtParent(parentShell));
Packit 34ce77
Packit 34ce77
    if(parentShell && XtWindow(parentShell)){
Packit 34ce77
Packit 34ce77
      /* check to see if there is already a property */
Packit 34ce77
      status=XGetWMColormapWindows(XtDisplay(parentShell),XtWindow(parentShell),&windowsReturn,&countReturn);
Packit 34ce77
            
Packit 34ce77
      /* if no property, just create one */
Packit 34ce77
      if(!status){
Packit 34ce77
        windows[0]=XtWindow(w);
Packit 34ce77
        windows[1]=XtWindow(parentShell);
Packit 34ce77
        XSetWMColormapWindows(XtDisplay(parentShell),XtWindow(parentShell),windows,2);
Packit 34ce77
        }
Packit 34ce77
Packit 34ce77
      /* there was a property, add myself to the beginning */
Packit 34ce77
      else{
Packit 34ce77
        windowList=(Window *)XtMalloc((sizeof(Window))*(countReturn+1));
Packit 34ce77
        windowList[0]=XtWindow(w);
Packit 34ce77
        for(i=0; i
Packit 34ce77
        XSetWMColormapWindows(XtDisplay(parentShell),XtWindow(parentShell),windowList,countReturn+1);
Packit 34ce77
        XtFree((char*)windowList);
Packit 34ce77
        XtFree((char*)windowsReturn);
Packit 34ce77
        }
Packit 34ce77
      }
Packit 34ce77
    else{
Packit 34ce77
      warning(w,"Could not set colormap property on parent shell");
Packit 34ce77
      }
Packit 34ce77
    }
Packit 34ce77
Packit 34ce77
  /* Invoke callbacks */
Packit 34ce77
  cb.reason=GLwCR_GINIT;
Packit 34ce77
  cb.event=NULL;
Packit 34ce77
  cb.width=glw->core.width;
Packit 34ce77
  cb.height=glw->core.height;
Packit 34ce77
  XtCallCallbackList((Widget)glw,glw->glwDrawingArea.ginitCallback,&cb;;
Packit 34ce77
  }
Packit 34ce77
Packit 34ce77
Packit 34ce77
Packit 34ce77
static void Redraw(GLwDrawingAreaWidget w,XEvent *event,Region region){
Packit 34ce77
  GLwDrawingAreaCallbackStruct cb;
Packit 34ce77
  if(!XtIsRealized((Widget)w)) return;
Packit 34ce77
  cb.reason=GLwCR_EXPOSE;
Packit 34ce77
  cb.event=event;
Packit 34ce77
  cb.width=w->core.width;
Packit 34ce77
  cb.height=w->core.height;
Packit 34ce77
  XtCallCallbackList((Widget)w,w->glwDrawingArea.exposeCallback,&cb;;
Packit 34ce77
  }
Packit 34ce77
Packit 34ce77
Packit 34ce77
Packit 34ce77
static void Resize(GLwDrawingAreaWidget glw){
Packit 34ce77
  GLwDrawingAreaCallbackStruct cb;
Packit 34ce77
  if(!XtIsRealized((Widget)glw)) return;
Packit 34ce77
  cb.reason=GLwCR_RESIZE;
Packit 34ce77
  cb.event=NULL;
Packit 34ce77
  cb.width=glw->core.width;
Packit 34ce77
  cb.height=glw->core.height;
Packit 34ce77
  XtCallCallbackList((Widget)glw,glw->glwDrawingArea.resizeCallback,&cb;;
Packit 34ce77
  }
Packit 34ce77
Packit 34ce77
Packit 34ce77
Packit 34ce77
static void Destroy(GLwDrawingAreaWidget glw){
Packit 34ce77
  Window *windowsReturn;
Packit 34ce77
  Widget parentShell;
Packit 34ce77
  Status status;
Packit 34ce77
  int countReturn;
Packit 34ce77
  register int i;
Packit 34ce77
Packit 34ce77
  if(glw->glwDrawingArea.myList && glw->glwDrawingArea.attribList){
Packit 34ce77
    XtFree((XtPointer)glw->glwDrawingArea.attribList);
Packit 34ce77
    }
Packit 34ce77
Packit 34ce77
  if(glw->glwDrawingArea.myVisual && glw->glwDrawingArea.visualInfo){
Packit 34ce77
    XtFree((XtPointer)glw->glwDrawingArea.visualInfo);
Packit 34ce77
    }
Packit 34ce77
Packit 34ce77
  /* if my colormap was installed, remove it */
Packit 34ce77
  if(glw->glwDrawingArea.installColormap){
Packit 34ce77
Packit 34ce77
    /* Get parent shell */
Packit 34ce77
    for(parentShell=XtParent(glw); parentShell&&!XtIsShell(parentShell); parentShell=XtParent(parentShell));
Packit 34ce77
Packit 34ce77
    if(parentShell && XtWindow(parentShell)){
Packit 34ce77
Packit 34ce77
      /* make sure there is a property */
Packit 34ce77
      status=XGetWMColormapWindows(XtDisplay(parentShell),XtWindow(parentShell),&windowsReturn,&countReturn);
Packit 34ce77
            
Packit 34ce77
      /* if no property, just return.  If there was a property, continue */
Packit 34ce77
      if(status){
Packit 34ce77
Packit 34ce77
        /* search for a match */
Packit 34ce77
        for(i=0; i
Packit 34ce77
          if(windowsReturn[i]==XtWindow(glw)){
Packit 34ce77
Packit 34ce77
            /* we found a match, now copy the rest down */
Packit 34ce77
            for(i++; i
Packit 34ce77
Packit 34ce77
            XSetWMColormapWindows(XtDisplay(parentShell),XtWindow(parentShell),windowsReturn,countReturn-1);
Packit 34ce77
            break; 
Packit 34ce77
            }
Packit 34ce77
          }
Packit 34ce77
        XtFree((char *)windowsReturn);
Packit 34ce77
        }
Packit 34ce77
      }
Packit 34ce77
    }
Packit 34ce77
  }
Packit 34ce77
Packit 34ce77
Packit 34ce77
Packit 34ce77
/* Action routine for keyboard and mouse events */
Packit 34ce77
static void glwInput(GLwDrawingAreaWidget glw,XEvent *event,String *params,Cardinal *numParams){
Packit 34ce77
  GLwDrawingAreaCallbackStruct cb;
Packit 34ce77
  cb.reason=GLwCR_INPUT;
Packit 34ce77
  cb.event=event;
Packit 34ce77
  cb.width=glw->core.width;
Packit 34ce77
  cb.height=glw->core.height;
Packit 34ce77
  XtCallCallbackList((Widget)glw,glw->glwDrawingArea.inputCallback,&cb;;
Packit 34ce77
  }
Packit 34ce77
Packit 34ce77
Packit 34ce77
#ifdef __GLX_MOTIF
Packit 34ce77
Packit 34ce77
/* Create routine */
Packit 34ce77
Widget GLwCreateMDrawingArea(Widget parent, char *name,ArgList arglist,Cardinal argcount){
Packit 34ce77
  return XtCreateWidget(name,glwMDrawingAreaWidgetClass, parent, arglist,argcount);
Packit 34ce77
  }
Packit 34ce77
Packit 34ce77
#endif
Packit 34ce77
Packit 34ce77
Packit 34ce77
#ifndef __GLX_MOTIF
Packit 34ce77
Packit 34ce77
/* Make context current */
Packit 34ce77
void GLwDrawingAreaMakeCurrent(Widget w,GLXContext ctx){
Packit 34ce77
  glXMakeCurrent(XtDisplay(w),XtWindow(w),ctx);
Packit 34ce77
  }
Packit 34ce77
Packit 34ce77
Packit 34ce77
/* Swap buffers convenience function */
Packit 34ce77
void GLwDrawingAreaSwapBuffers(Widget w){
Packit 34ce77
  glXSwapBuffers(XtDisplay(w),XtWindow(w));
Packit 34ce77
  }
Packit 34ce77
Packit 34ce77
#endif