8 #include <X11/StringDefs.h>
20 static void RealizeWidget(Widget, XtValueMask*, XSetWindowAttributes*);
23 static void DrawWidget(Widget, XEvent*, Region);
24 static Boolean
SetValues(Widget,Widget,Widget,ArgList,Cardinal *);
26 static void EventHandler(Widget,XtPointer,XEvent*,Boolean*);
35 #define athis ((OpenGLAreaWidget)This)->openGLArea
36 #define acur ((OpenGLAreaWidget)a_current)->openGLArea
42 XtOffset(
OpenGLAreaWidget,openGLArea.doubleBufferOn),XtRImmediate,(XtPointer)True},
44 XtOffset(
OpenGLAreaWidget,openGLArea.paintCallback),XtRImmediate,(XtPointer)NULL},
46 XtOffset(
OpenGLAreaWidget,openGLArea.eventCallback),XtRImmediate,(XtPointer)NULL}
52 (WidgetClass) &compositeClassRec,
75 XtInheritSetValuesAlmost,
80 XtInheritTranslations,
81 XtInheritQueryGeometry,
82 XtInheritDisplayAccelerator,
87 XtInheritGeometryManager,
88 XtInheritChangeManaged,
104 static void InitializeWidget(Widget a_request,Widget This,ArgList a_args,Cardinal* a_argn) {
105 if(a_request->core.width<=0) This->core.width = 100;
106 if(a_request->core.height<=0) This->core.height = 100;
109 printf (
"debug : OpenGLArea : InitializeWidget : %s\n",XtName(This));
112 athis.visual = CopyFromParent;
113 athis.installColormap = False;
114 athis.glContext = NULL;
120 display = XtDisplay(This);
121 screen = XtScreen(This);
122 iscreen = XScreenNumberOfScreen(screen);
125 if(glXQueryExtension(display,&error,&event)==0) {
126 CWarn (
"X server does not have OpenGL extensions.\n");
129 if(
athis.doubleBufferOn==True) {
140 vinfo = glXChooseVisual (display,iscreen,atbs);
151 vinfo = glXChooseVisual (display,iscreen,atbs);
163 vinfo = glXChooseVisual (display,iscreen,atbs);
173 vinfo = glXChooseVisual (display,iscreen,atbs);
178 CWarn (
"Can't choose a visual.\n");
180 This->core.depth = vinfo->depth;
181 athis.visual = vinfo->visual;
182 if( (vinfo->depth ==DefaultDepth (display,iscreen)) &&
183 (vinfo->visual==DefaultVisual(display,iscreen)) ) {
184 This->core.colormap = XDefaultColormap (display,iscreen);
185 athis.installColormap = False;
187 This->core.colormap =
188 XCreateColormap (display,XRootWindow(display,iscreen),vinfo->visual, AllocNone);
189 athis.installColormap = True;
191 if(This->core.colormap==0
L) {
192 CWarn (
"Can't get/create a X colormap.\n");
194 athis.glContext = glXCreateContext (display,vinfo,NULL,GL_FALSE);
195 if(
athis.glContext==NULL) {
196 CWarn (
"Can't create a GLX context.\n");
201 XtAddEventHandler(This,ButtonPressMask|ButtonReleaseMask|ButtonMotionMask,0,
EventHandler,NULL);
204 printf(
"debug : OpenGLArea : InitializeWidget : end\n");
213 static void RealizeWidget(Widget This,XtValueMask* a_mask,XSetWindowAttributes* a_watbs) {
215 printf(
"debug : OpenGLArea : RealizeWidget : %s\n",XtName(This));
221 XtCreateWindow(This,(
unsigned int)InputOutput,
athis.visual,*a_mask,a_watbs);
224 if(openGLAreaWidgetClass->core_class.superclass->core_class.realize!=NULL)
225 (openGLAreaWidgetClass->core_class.superclass->core_class.realize)(This, a_mask, a_watbs);
233 printf(
"debug : OpenGLArea : RealizeWidget : end\n");
238 if(
athis.installColormap==True) {
240 athis.installColormap = False;
241 XFreeColormap(XtDisplay(This),This->core.colormap);
243 if(
athis.glContext!=NULL) {
244 glXMakeCurrent(XtDisplay(This),
None,NULL);
245 glXDestroyContext(XtDisplay(This),
athis.glContext);
246 athis.glContext = NULL;
250 #define IFMOD(a_field) if(athis.a_field != acur.a_field)
251 static Boolean
SetValues(Widget a_current,Widget a_request,Widget This,ArgList a_args,Cardinal* a_argn) {
252 IFMOD(doubleBufferOn) {
257 if(XtIsRealized(This) && (
athis.installColormap==True)) {
258 CWarn(
"Can't change buffering after \"realization\" of the widget.\n");
259 athis.doubleBufferOn =
acur.doubleBufferOn;
271 printf(
"debug : OpenGLArea : ChangeWidgetSize : %s\n",XtName(This));
275 if(openGLAreaWidgetClass->core_class.superclass->core_class.resize!=NULL)
276 (openGLAreaWidgetClass->core_class.superclass->core_class.resize)(This);
279 printf(
"debug : OpenGLArea : ChangeWidgetSize : end\n");
283 static void DrawWidget(Widget This,XEvent* a_event,Region a_region) {
285 printf(
"debug : OpenGLArea : DrawWidget : %s\n",XtName(This));
288 if(openGLAreaWidgetClass->core_class.superclass->core_class.expose!=NULL)
289 (openGLAreaWidgetClass->core_class.superclass->core_class.expose)(This,a_event,a_region);
293 printf(
"debug : OpenGLArea : DrawWidget : %s : MakeCurrent ok : call paintCallback...\n",XtName(This));
297 value.
event = a_event;
299 glXSwapBuffers(XtDisplay(This),XtWindow(This));
300 glXMakeCurrent(XtDisplay(This),
None,NULL);
304 printf(
"debug : OpenGLArea : DrawWidget : end\n");
318 if(!XtIsRealized(This))
return;
324 glXSwapBuffers(XtDisplay(This),XtWindow(This));
325 glXMakeCurrent(XtDisplay(This),
None,NULL);
329 #ifdef TOOLS_XT_OPENGLAREA_HAS_GL2PS
331 #include <tools/c_gl2ps.h>
339 static inlib_gl2ps_gl_funcs_t s_OpenGL_funcs = {
352 file = fopen(aFileName,
"w");
355 inlib_c_gl2ps_set_gl_funcs(&s_OpenGL_funcs);
366 vp[2] = This->core.width;
367 vp[3] = This->core.height;
369 inlib_c_gl2psBeginPage(
"title",
"exlib_Xt_OpenGLArea",
379 inlib_c_gl2psEndPage();
381 inlib_c_gl2ps_reset_gl_funcs();
401 if(This==NULL)
return 0;
402 if(!XtIsRealized(This))
return 0;
403 if(
athis.glContext==NULL)
return 0;
404 return (
int)glXMakeCurrent(XtDisplay(This),XtWindow(This),
athis.glContext);
407 static void EventHandler(Widget This,XtPointer a_tag,XEvent* a_event ,Boolean* a_continue) {
410 value.
event = a_event;
425 XWindowAttributes watbs;
428 int wn = 0,found,count;
432 if(This==NULL)
return;
433 if( !XtIsWidget(This) || !XtIsRealized(This) )
return;
435 if(shell==NULL)
return;
436 display = XtDisplay (This);
437 wthis = XtWindow (This);
438 wshell = XtWindow (shell);
439 XGetWMColormapWindows (display,wshell, &ws, &wn);
441 XGetWindowAttributes (display,wthis,&watbs);
442 cmapthis = watbs.colormap;
444 for(count=0;count<wn;count++) {
446 XGetWindowAttributes (display,ws[count],&watbs);
447 cmap = watbs.colormap;
452 if(ws[count]==wshell) {
458 if(ws!=NULL) XFree(ws);
466 ws[wn] = wthis; wn++;
467 ws[wn] = wshell;wn++;
470 ws[wn] = wshell; wn++;
472 if (XSetWMColormapWindows(display,wshell, ws, wn)==0) {
473 CWarnF (
"XWidgetInstallColormap: can't install colormap of %s in %s.\n",XtName(This),XtName(shell));
488 if(This==NULL)
return;
489 if( !XtIsWidget(This) || !XtIsRealized(This) )
return;
491 if(shell==NULL)
return;
492 display = XtDisplay (This);
493 wthis = XtWindow (This);
494 wshell = XtWindow (shell);
495 XGetWMColormapWindows (display,wshell, &ws, &wn);
496 if( (wn==0) || (ws==NULL) )
return;
503 for(count=0;count<wn;count++) {
504 if(ws[count]!=wthis) {
505 nws[nwn] = ws[count];
510 if (XSetWMColormapWindows(display,wshell, nws, nwn)==0) {
511 CWarnF(
"XWidgetUninstallColormap: can't install colormap of %s in %s.\n",XtName(This),XtName(shell));
520 if(This==NULL)
return NULL;
523 if(widget==NULL)
return NULL;
524 if(XtIsShell(widget))
return widget;
525 widget = XtParent(widget);