8#include <X11/StringDefs.h>
18static void InitializeClass(
void);
19static void InitializeWidget(Widget, Widget,ArgList,Cardinal*);
20static void RealizeWidget(Widget, XtValueMask*, XSetWindowAttributes*);
21static void DestroyWidget(Widget);
22static void ChangeWidgetSize(Widget);
23static void DrawWidget(Widget, XEvent*, Region);
24static Boolean SetValues(Widget,Widget,Widget,ArgList,Cardinal *);
26static void EventHandler(Widget,XtPointer,XEvent*,Boolean*);
27static int MakeCurrent(Widget);
28static void XWidgetInstallColormap(Widget);
29static void XWidgetUninstallColormap(Widget);
30static Widget XWidgetGetShell(Widget);
35#define athis ((OpenGLAreaWidget)This)->openGLArea
36#define acur ((OpenGLAreaWidget)a_current)->openGLArea
40static XtResource resources [] = {
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,
102static void InitializeClass(
void) {}
104static 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==0L) {
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");
213static 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);
228 if(
athis.installColormap==True) XWidgetInstallColormap(This);
233 printf(
"debug : OpenGLArea : RealizeWidget : end\n");
237static void DestroyWidget(Widget This) {
238 if(
athis.installColormap==True) {
239 XWidgetUninstallColormap (This);
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)
251static 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;
269static void ChangeWidgetSize(Widget This) {
271 printf(
"debug : OpenGLArea : ChangeWidgetSize : %s\n",XtName(This));
279 printf(
"debug : OpenGLArea : ChangeWidgetSize : end\n");
283static void DrawWidget(Widget This,XEvent* a_event,Region a_region) {
285 printf(
"debug : OpenGLArea : DrawWidget : %s\n",XtName(This));
291 if(MakeCurrent(This)==1) {
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;
319 if(MakeCurrent(This)==1) {
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();
400static int MakeCurrent(Widget This) {
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);
407static void EventHandler(Widget This,XtPointer a_tag,XEvent* a_event ,Boolean* a_continue) {
410 value.
event = a_event;
419static void XWidgetInstallColormap(Widget This) {
425 XWindowAttributes watbs;
428 int wn = 0,found,count;
432 if(This==NULL)
return;
433 if( !XtIsWidget(This) || !XtIsRealized(This) )
return;
434 shell = XWidgetGetShell (This);
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);
459 ws = (Window*)malloc ( 2 *
sizeof(Window));
461 ws = (Window*)realloc (ws,(wn + 2) *
sizeof(Window));
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));
478static void XWidgetUninstallColormap(Widget This) {
488 if(This==NULL)
return;
489 if( !XtIsWidget(This) || !XtIsRealized(This) )
return;
490 shell = XWidgetGetShell (This);
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;
497 nws = (Window*)malloc ( wn *
sizeof(Window));
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));
518Widget XWidgetGetShell(Widget This) {
520 if(This==NULL)
return NULL;
523 if(widget==NULL)
return NULL;
524 if(XtIsShell(widget))
return widget;
525 widget = XtParent(widget);
OpenGLAreaClassRec openGLAreaClassRec
int OpenGLAreaWrite_gl2ps(Widget w, const char *f, const char *o)
WidgetClass openGLAreaWidgetClass
void OpenGLAreaPaint(Widget This)
#define XoNdoubleBufferOn
#define TOOLS_GL2PS_OCCLUSION_CULL
#define TOOLS_GL2PS_BEST_ROOT
#define TOOLS_GL2PS_BSP_SORT
#define TOOLS_GL2PS_DRAW_BACKGROUND
#define TOOLS_GL2PS_SILENT