34 #include <libFreeWRL.h>
36 #include "../vrml_parser/Structs.h"
37 #include "../main/headers.h"
38 #include "../vrml_parser/CParseGeneral.h"
39 #include "../scenegraph/Vector.h"
40 #include "../vrml_parser/CFieldDecls.h"
41 #include "../vrml_parser/CParseParser.h"
42 #include "../vrml_parser/CParseLexer.h"
43 #include "../vrml_parser/CParse.h"
47 #include "../x3d_parser/Bindable.h"
48 #include "../scenegraph/Collision.h"
49 #include "../scenegraph/quaternion.h"
50 #include "../scenegraph/Viewer.h"
102 #include <X11/IntrinsicP.h>
103 #include <X11/StringDefs.h>
105 #include "GLwDrawAP.h"
107 #define ATTRIBLIST_SIZE 32
109 #define offset(field) XtOffset(GLwDrawingAreaWidget,glwDrawingArea.field)
115 static void Realize(Widget w,Mask *valueMask,XSetWindowAttributes *attributes);
119 static void glwInput(
GLwDrawingAreaWidget glw,XEvent *event,String *params,Cardinal *numParams);
123 static char defaultTranslations[] =
124 "<KeyDown>: glwInput() \n\
125 <KeyUp>: glwInput() \n\
126 <BtnDown>: glwInput() \n\
127 <BtnUp>: glwInput() \n\
128 <BtnMotion>: glwInput() ";
131 static XtActionsRec actions[] = {
132 {
"glwInput",(XtActionProc)glwInput},
152 static XtResource resources[] = {
155 {GLwNbufferSize, GLwCBufferSize, XtRInt,
sizeof (int),
156 offset(bufferSize), XtRImmediate, (XtPointer) 0},
158 {GLwNlevel, GLwCLevel, XtRInt,
sizeof (int),
159 offset(level), XtRImmediate, (XtPointer) 0},
161 {GLwNrgba, GLwCRgba, XtRBoolean,
sizeof (Boolean),
162 offset(rgba), XtRImmediate, (XtPointer) FALSE},
164 {GLwNdoublebuffer, GLwCDoublebuffer, XtRBoolean,
sizeof (Boolean),
165 offset(doublebuffer), XtRImmediate, (XtPointer) FALSE},
167 {GLwNstereo, GLwCStereo, XtRBoolean,
sizeof (Boolean),
168 offset(stereo), XtRImmediate, (XtPointer) FALSE},
170 {GLwNauxBuffers, GLwCAuxBuffers, XtRInt,
sizeof (int),
171 offset(auxBuffers), XtRImmediate, (XtPointer) 0},
173 {GLwNredSize, GLwCColorSize, XtRInt,
sizeof (int),
174 offset(redSize), XtRImmediate, (XtPointer) 1},
176 {GLwNgreenSize, GLwCColorSize, XtRInt,
sizeof (int),
177 offset(greenSize), XtRImmediate, (XtPointer) 1},
179 {GLwNblueSize, GLwCColorSize, XtRInt,
sizeof (int),
180 offset(blueSize), XtRImmediate, (XtPointer) 1},
182 {GLwNalphaSize, GLwCAlphaSize, XtRInt,
sizeof (int),
183 offset(alphaSize), XtRImmediate, (XtPointer) 0},
185 {GLwNdepthSize, GLwCDepthSize, XtRInt,
sizeof (int),
186 offset(depthSize), XtRImmediate, (XtPointer) 0},
188 {GLwNstencilSize, GLwCStencilSize, XtRInt,
sizeof (int),
189 offset(stencilSize), XtRImmediate, (XtPointer) 0},
191 {GLwNaccumRedSize, GLwCAccumColorSize, XtRInt,
sizeof (int),
192 offset(accumRedSize), XtRImmediate, (XtPointer) 0},
194 {GLwNaccumGreenSize, GLwCAccumColorSize, XtRInt,
sizeof (int),
195 offset(accumGreenSize), XtRImmediate, (XtPointer) 0},
197 {GLwNaccumBlueSize, GLwCAccumColorSize, XtRInt,
sizeof (int),
198 offset(accumBlueSize), XtRImmediate, (XtPointer) 0},
200 {GLwNaccumAlphaSize, GLwCAccumAlphaSize, XtRInt,
sizeof (int),
201 offset(accumAlphaSize), XtRImmediate, (XtPointer) 0},
204 {GLwNattribList, GLwCAttribList, XtRPointer,
sizeof(
int *),
205 offset(attribList), XtRImmediate, (XtPointer) NULL},
208 {GLwNvisualInfo, GLwCVisualInfo, GLwRVisualInfo,
sizeof (XVisualInfo *),
209 offset(visualInfo), XtRImmediate, (XtPointer) NULL},
212 {GLwNinstallColormap, GLwCInstallColormap, XtRBoolean,
sizeof (Boolean),
213 offset(installColormap), XtRImmediate, (XtPointer) TRUE},
215 {GLwNallocateBackground, GLwCAllocateColors, XtRBoolean,
sizeof (Boolean),
216 offset(allocateBackground), XtRImmediate, (XtPointer) FALSE},
218 {GLwNallocateOtherColors, GLwCAllocateColors, XtRBoolean,
sizeof (Boolean),
219 offset(allocateOtherColors), XtRImmediate, (XtPointer) FALSE},
221 {GLwNinstallBackground, GLwCInstallBackground, XtRBoolean,
sizeof (Boolean),
222 offset(installBackground), XtRImmediate, (XtPointer) TRUE},
224 {GLwNginitCallback, GLwCCallback, XtRCallback,
sizeof (XtCallbackList),
225 offset(ginitCallback), XtRImmediate, (XtPointer) NULL},
227 {GLwNinputCallback, GLwCCallback, XtRCallback,
sizeof (XtCallbackList),
228 offset(inputCallback), XtRImmediate, (XtPointer) NULL},
230 {GLwNresizeCallback, GLwCCallback, XtRCallback,
sizeof (XtCallbackList),
231 offset(resizeCallback), XtRImmediate, (XtPointer) NULL},
233 {GLwNexposeCallback, GLwCCallback, XtRCallback,
sizeof (XtCallbackList),
234 offset(exposeCallback), XtRImmediate, (XtPointer) NULL},
244 static XtResource initializeResources[] = {
246 {XtNcolormap, XtCColormap, XtRColormap,
sizeof(Colormap),
248 XtRCallProc,(XtPointer) createColormap},
255 static XtResource backgroundResources[] = {
256 {XtNbackground,XtCBackground,XtRPixel,
sizeof(Pixel),
258 XtRString,(XtPointer)
"lightgrey"},
261 {XtNbackgroundPixmap, XtCPixmap, XtRPixmap,
sizeof(Pixmap),
263 XtRImmediate,(XtPointer)XtUnspecifiedPixmap},
271 (WidgetClass) &widgetClassRec,
277 (XtInitProc) Initialize,
289 (XtWidgetProc) Destroy,
290 (XtWidgetProc) Resize,
291 (XtExposeProc) Redraw,
294 XtInheritSetValuesAlmost,
300 XtInheritQueryGeometry,
301 XtInheritDisplayAccelerator,
306 WidgetClass glwDrawingAreaWidgetClass=(WidgetClass)&glwDrawingAreaClassRec;
310 static void error(Widget w,
char*
string){
312 sprintf(buf,
"GLwDrawingArea: %s\n",
string);
313 XtAppError(XtWidgetToApplicationContext(w),buf);
317 static void warning(Widget w,
char*
string){
319 sprintf (buf,
"GLwDraw: %s\n",
string);
320 XtAppWarning(XtWidgetToApplicationContext(w), buf);
328 w->glwDrawingArea.attribList = (
int*)XtMalloc(ATTRIBLIST_SIZE*
sizeof(
int));
329 if(!w->glwDrawingArea.attribList){
330 error((Widget)w,
"Unable to allocate attribute list");
332 ptr = w->glwDrawingArea.attribList;
333 *ptr++ = GLX_BUFFER_SIZE;
334 *ptr++ = w->glwDrawingArea.bufferSize;
336 *ptr++ = w->glwDrawingArea.level;
337 if(w->glwDrawingArea.rgba) *ptr++ = GLX_RGBA;
338 if(w->glwDrawingArea.doublebuffer) *ptr++ = GLX_DOUBLEBUFFER;
339 if(w->glwDrawingArea.stereo) *ptr++ = GLX_STEREO;
340 *ptr++ = GLX_AUX_BUFFERS;
341 *ptr++ = w->glwDrawingArea.auxBuffers;
342 *ptr++ = GLX_RED_SIZE;
343 *ptr++ = w->glwDrawingArea.redSize;
344 *ptr++ = GLX_GREEN_SIZE;
345 *ptr++ = w->glwDrawingArea.greenSize;
346 *ptr++ = GLX_BLUE_SIZE;
347 *ptr++ = w->glwDrawingArea.blueSize;
348 *ptr++ = GLX_ALPHA_SIZE;
349 *ptr++ = w->glwDrawingArea.alphaSize;
350 *ptr++ = GLX_DEPTH_SIZE;
351 *ptr++ = w->glwDrawingArea.depthSize;
352 *ptr++ = GLX_STENCIL_SIZE;
353 *ptr++ = w->glwDrawingArea.stencilSize;
354 *ptr++ = GLX_ACCUM_RED_SIZE;
355 *ptr++ = w->glwDrawingArea.accumRedSize;
356 *ptr++ = GLX_ACCUM_GREEN_SIZE;
357 *ptr++ = w->glwDrawingArea.accumGreenSize;
358 *ptr++ = GLX_ACCUM_BLUE_SIZE;
359 *ptr++ = w->glwDrawingArea.accumBlueSize;
360 *ptr++ = GLX_ACCUM_ALPHA_SIZE;
361 *ptr++ = w->glwDrawingArea.accumAlphaSize;
363 assert((ptr-w->glwDrawingArea.attribList)<ATTRIBLIST_SIZE);
370 assert(w->glwDrawingArea.attribList);
371 w->glwDrawingArea.visualInfo=glXChooseVisual(XtDisplay(w),XScreenNumberOfScreen(XtScreen(w)),w->glwDrawingArea.attribList);
372 if(!w->glwDrawingArea.visualInfo) error((Widget)w,
"requested visual not supported");
383 static struct cmapCache { Visual *visual; Colormap cmap; } *cmapCache;
384 static int cacheEntries=0;
385 static int cacheMalloced=0;
388 assert(w->glwDrawingArea.visualInfo);
391 for(i=0; i<cacheEntries; i++){
392 if(cmapCache[i].visual==w->glwDrawingArea.visualInfo->visual){
393 value->addr=(XtPointer)(&cmapCache[i].cmap);
399 if(cacheEntries >= cacheMalloced){
404 if(cacheMalloced==0){
406 cmapCache=(
struct cmapCache*)XtMalloc(
sizeof(
struct cmapCache));
410 cmapCache=(
struct cmapCache*)XtRealloc((
char*)cmapCache,
sizeof(
struct cmapCache)*cacheMalloced);
414 cmapCache[cacheEntries].cmap=XCreateColormap(XtDisplay(w),
415 RootWindow(XtDisplay(w),
416 w->glwDrawingArea.visualInfo->screen),
417 w->glwDrawingArea.visualInfo->visual,
419 cmapCache[cacheEntries].visual=w->glwDrawingArea.visualInfo->visual;
420 value->addr=(XtPointer)(&cmapCache[cacheEntries++].cmap);
428 if(req->core.width==0) neww->core.width=100;
429 if(req->core.height==0) neww->core.width=100;
432 neww->glwDrawingArea.myList=FALSE;
433 if(neww->glwDrawingArea.attribList==NULL){
434 neww->glwDrawingArea.myList=TRUE;
435 createAttribList(neww);
439 assert(neww->glwDrawingArea.attribList);
442 neww->glwDrawingArea.myVisual=FALSE;
443 if(neww->glwDrawingArea.visualInfo==NULL){
444 neww->glwDrawingArea.myVisual=TRUE;
445 createVisualInfo(neww);
449 assert(neww->glwDrawingArea.visualInfo);
451 neww->core.depth=neww->glwDrawingArea.visualInfo->depth;
454 XtGetApplicationResources((Widget)neww,neww,initializeResources,XtNumber(initializeResources),args,*num_args);
457 if(req->glwDrawingArea.allocateBackground){
458 XtGetApplicationResources((Widget)neww,neww,backgroundResources,XtNumber(backgroundResources),args,*num_args);
464 static void Realize(Widget w,Mask *valueMask,XSetWindowAttributes *attributes){
469 Window windows[2],*windowsReturn,*windowList;
475 if(!(glw->glwDrawingArea.installBackground && glw->glwDrawingArea.allocateBackground)){
476 *valueMask&=~CWBackPixel;
479 XtCreateWindow(w,(
unsigned int)InputOutput,glw->glwDrawingArea.visualInfo->visual,*valueMask,attributes);
482 if(glw->glwDrawingArea.installColormap){
485 for(parentShell=XtParent(w); parentShell&&!XtIsShell(parentShell); parentShell=XtParent(parentShell));
487 if(parentShell && XtWindow(parentShell)){
490 status=XGetWMColormapWindows(XtDisplay(parentShell),XtWindow(parentShell),&windowsReturn,&countReturn);
494 windows[0]=XtWindow(w);
495 windows[1]=XtWindow(parentShell);
496 XSetWMColormapWindows(XtDisplay(parentShell),XtWindow(parentShell),windows,2);
501 windowList=(Window *)XtMalloc((
sizeof(Window))*(countReturn+1));
502 windowList[0]=XtWindow(w);
503 for(i=0; i<countReturn; i++) windowList[i+1]=windowsReturn[i];
504 XSetWMColormapWindows(XtDisplay(parentShell),XtWindow(parentShell),windowList,countReturn+1);
505 XtFree((
char*)windowList);
506 XtFree((
char*)windowsReturn);
510 warning(w,
"Could not set colormap property on parent shell");
515 cb.reason=GLwCR_GINIT;
517 cb.width=glw->core.width;
518 cb.height=glw->core.height;
519 XtCallCallbackList((Widget)glw,glw->glwDrawingArea.ginitCallback,&cb);
526 if(!XtIsRealized((Widget)w))
return;
527 cb.reason=GLwCR_EXPOSE;
529 cb.width=w->core.width;
530 cb.height=w->core.height;
531 XtCallCallbackList((Widget)w,w->glwDrawingArea.exposeCallback,&cb);
538 if(!XtIsRealized((Widget)glw))
return;
539 cb.reason=GLwCR_RESIZE;
541 cb.width=glw->core.width;
542 cb.height=glw->core.height;
543 XtCallCallbackList((Widget)glw,glw->glwDrawingArea.resizeCallback,&cb);
549 Window *windowsReturn;
555 if(glw->glwDrawingArea.myList && glw->glwDrawingArea.attribList){
556 XtFree((XtPointer)glw->glwDrawingArea.attribList);
559 if(glw->glwDrawingArea.myVisual && glw->glwDrawingArea.visualInfo){
560 XtFree((XtPointer)glw->glwDrawingArea.visualInfo);
564 if(glw->glwDrawingArea.installColormap){
567 for(parentShell=XtParent(glw); parentShell&&!XtIsShell(parentShell); parentShell=XtParent(parentShell));
569 if(parentShell && XtWindow(parentShell)){
572 status=XGetWMColormapWindows(XtDisplay(parentShell),XtWindow(parentShell),&windowsReturn,&countReturn);
578 for(i=0; i<countReturn; i++){
579 if(windowsReturn[i]==XtWindow(glw)){
582 for(i++; i<countReturn; i++){ windowsReturn[i-1]=windowsReturn[i]; }
584 XSetWMColormapWindows(XtDisplay(parentShell),XtWindow(parentShell),windowsReturn,countReturn-1);
588 XtFree((
char *)windowsReturn);
597 static void glwInput(
GLwDrawingAreaWidget glw,XEvent *event,String *params,Cardinal *numParams){
599 cb.reason=GLwCR_INPUT;
601 cb.width=glw->core.width;
602 cb.height=glw->core.height;
603 XtCallCallbackList((Widget)glw,glw->glwDrawingArea.inputCallback,&cb);