1 | /* |
---|
2 | * (c) Copyright 1993, Silicon Graphics, Inc. |
---|
3 | * ALL RIGHTS RESERVED |
---|
4 | * Permission to use, copy, modify, and distribute this software for |
---|
5 | * any purpose and without fee is hereby granted, provided that the above |
---|
6 | * copyright notice appear in all copies and that both the copyright notice |
---|
7 | * and this permission notice appear in supporting documentation, and that |
---|
8 | * the name of Silicon Graphics, Inc. not be used in advertising |
---|
9 | * or publicity pertaining to distribution of the software without specific, |
---|
10 | * written prior permission. |
---|
11 | * |
---|
12 | * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" |
---|
13 | * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, |
---|
14 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR |
---|
15 | * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON |
---|
16 | * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, |
---|
17 | * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY |
---|
18 | * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, |
---|
19 | * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF |
---|
20 | * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN |
---|
21 | * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON |
---|
22 | * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE |
---|
23 | * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
24 | * |
---|
25 | * |
---|
26 | * US Government Users Restricted Rights |
---|
27 | * Use, duplication, or disclosure by the Government is subject to |
---|
28 | * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph |
---|
29 | * (c)(1)(ii) of the Rights in Technical Data and Computer Software |
---|
30 | * clause at DFARS 252.227-7013 and/or in similar or successor |
---|
31 | * clauses in the FAR or the DOD or NASA FAR Supplement. |
---|
32 | * Unpublished-- rights reserved under the copyright laws of the |
---|
33 | * United States. Contractor/manufacturer is Silicon Graphics, |
---|
34 | * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311. |
---|
35 | * |
---|
36 | * OpenGL(TM) is a trademark of Silicon Graphics, Inc. |
---|
37 | */ |
---|
38 | |
---|
39 | /* This file has been slightly modified from the original for use with Mesa |
---|
40 | * Jeroen van der Zijp (jvz@cyberia.cfdrc.com) |
---|
41 | */ |
---|
42 | |
---|
43 | /* This file has been modified for use with ARB */ |
---|
44 | |
---|
45 | #ifndef __GLX_MOTIF |
---|
46 | #error ARB only needs the Motif flavour of this file |
---|
47 | #endif |
---|
48 | #define USE(x) (x)=(x) |
---|
49 | |
---|
50 | #include <X11/IntrinsicP.h> |
---|
51 | #include <X11/StringDefs.h> |
---|
52 | /* #include <GL/glx.h> */ |
---|
53 | /* #include <GL/gl.h> */ |
---|
54 | #ifdef __GLX_MOTIF |
---|
55 | #include <Xm/PrimitiveP.h> |
---|
56 | #include "GLwMDrawAP.h" |
---|
57 | #else |
---|
58 | #include "GLwDrawAP.h" |
---|
59 | #endif |
---|
60 | #include <assert.h> |
---|
61 | #include <stdio.h> |
---|
62 | |
---|
63 | #ifdef __GLX_MOTIF |
---|
64 | #define GLwDrawingAreaWidget GLwMDrawingAreaWidget |
---|
65 | #define GLwDrawingAreaClassRec GLwMDrawingAreaClassRec |
---|
66 | #define glwDrawingAreaClassRec glwMDrawingAreaClassRec |
---|
67 | #define glwDrawingAreaWidgetClass glwMDrawingAreaWidgetClass |
---|
68 | #define GLwDrawingAreaRec GLwMDrawingAreaRec |
---|
69 | #endif |
---|
70 | |
---|
71 | #define ATTRIBLIST_SIZE 32 |
---|
72 | |
---|
73 | #define offset(field) XtOffset(GLwDrawingAreaWidget,glwDrawingArea.field) |
---|
74 | |
---|
75 | |
---|
76 | /* forward definitions */ |
---|
77 | static void createColormap(GLwDrawingAreaWidget w,int offset,XrmValue *value); |
---|
78 | static void Initialize(GLwDrawingAreaWidget req,GLwDrawingAreaWidget neww,ArgList args,Cardinal *num_args); |
---|
79 | static void Realize(Widget w,Mask *valueMask,XSetWindowAttributes *attributes); |
---|
80 | static void Redraw(GLwDrawingAreaWidget w,XEvent *event,Region region); |
---|
81 | static void Resize(GLwDrawingAreaWidget glw); |
---|
82 | static void Destroy(GLwDrawingAreaWidget glw); |
---|
83 | static void glwInput(GLwDrawingAreaWidget glw,XEvent *event,String *params,Cardinal *numParams); |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | static char defaultTranslations[] = |
---|
88 | #ifdef __GLX_MOTIF |
---|
89 | "<Key>osfHelp:PrimitiveHelp() \n" |
---|
90 | #endif |
---|
91 | "<KeyDown>: glwInput() \n\ |
---|
92 | <KeyUp>: glwInput() \n\ |
---|
93 | <BtnDown>: glwInput() \n\ |
---|
94 | <BtnUp>: glwInput() \n\ |
---|
95 | <BtnMotion>: glwInput() "; |
---|
96 | |
---|
97 | |
---|
98 | static XtActionsRec actions[] = { |
---|
99 | {(String)"glwInput",(XtActionProc)glwInput}, /* key or mouse input */ |
---|
100 | }; |
---|
101 | |
---|
102 | |
---|
103 | /* |
---|
104 | * There is a bit of unusual handling of the resources here. |
---|
105 | * Because Xt insists on allocating the colormap resource when it is |
---|
106 | * processing the core resources (even if we redeclare the colormap |
---|
107 | * resource here, we need to do a little trick. When Xt first allocates |
---|
108 | * the colormap, we allow it to allocate the default one, since we have |
---|
109 | * not yet determined the appropriate visual (which is determined from |
---|
110 | * resources parsed after the colormap). We also let it allocate colors |
---|
111 | * in that default colormap. |
---|
112 | * |
---|
113 | * In the initialize proc we calculate the actual visual. Then, we |
---|
114 | * reobtain the colormap resource using XtGetApplicationResources in |
---|
115 | * the initialize proc. If requested, we also reallocate colors in |
---|
116 | * that colormap using the same method. |
---|
117 | */ |
---|
118 | |
---|
119 | static XtResource resources[] = { |
---|
120 | /* The GLX attributes. Add any new attributes here */ |
---|
121 | |
---|
122 | {(String)GLwNbufferSize, (String)GLwCBufferSize, XtRInt, sizeof(int) , offset(bufferSize) , XtRImmediate, (XtPointer)0}, |
---|
123 | {(String)GLwNlevel, (String)GLwCLevel, XtRInt, sizeof(int) , offset(level) , XtRImmediate, (XtPointer)0}, |
---|
124 | {(String)GLwNrgba, (String)GLwCRgba, XtRBoolean, sizeof(Boolean), offset(rgba) , XtRImmediate, (XtPointer)FALSE}, |
---|
125 | {(String)GLwNdoublebuffer, (String)GLwCDoublebuffer, XtRBoolean, sizeof(Boolean), offset(doublebuffer) , XtRImmediate, (XtPointer)FALSE}, |
---|
126 | {(String)GLwNstereo, (String)GLwCStereo, XtRBoolean, sizeof(Boolean), offset(stereo) , XtRImmediate, (XtPointer)FALSE}, |
---|
127 | {(String)GLwNauxBuffers, (String)GLwCAuxBuffers, XtRInt, sizeof(int) , offset(auxBuffers) , XtRImmediate, (XtPointer)0}, |
---|
128 | {(String)GLwNredSize, (String)GLwCColorSize, XtRInt, sizeof(int) , offset(redSize) , XtRImmediate, (XtPointer)1}, |
---|
129 | {(String)GLwNgreenSize, (String)GLwCColorSize, XtRInt, sizeof(int) , offset(greenSize) , XtRImmediate, (XtPointer)1}, |
---|
130 | {(String)GLwNblueSize, (String)GLwCColorSize, XtRInt, sizeof(int) , offset(blueSize) , XtRImmediate, (XtPointer)1}, |
---|
131 | {(String)GLwNalphaSize, (String)GLwCAlphaSize, XtRInt, sizeof(int) , offset(alphaSize) , XtRImmediate, (XtPointer)0}, |
---|
132 | {(String)GLwNdepthSize, (String)GLwCDepthSize, XtRInt, sizeof(int) , offset(depthSize) , XtRImmediate, (XtPointer)0}, |
---|
133 | {(String)GLwNstencilSize, (String)GLwCStencilSize, XtRInt, sizeof(int) , offset(stencilSize) , XtRImmediate, (XtPointer)0}, |
---|
134 | {(String)GLwNaccumRedSize, (String)GLwCAccumColorSize, XtRInt, sizeof(int) , offset(accumRedSize) , XtRImmediate, (XtPointer)0}, |
---|
135 | {(String)GLwNaccumGreenSize, (String)GLwCAccumColorSize, XtRInt, sizeof(int) , offset(accumGreenSize), XtRImmediate, (XtPointer)0}, |
---|
136 | {(String)GLwNaccumBlueSize, (String)GLwCAccumColorSize, XtRInt, sizeof(int) , offset(accumBlueSize) , XtRImmediate, (XtPointer)0}, |
---|
137 | {(String)GLwNaccumAlphaSize, (String)GLwCAccumAlphaSize, XtRInt, sizeof(int) , offset(accumAlphaSize), XtRImmediate, (XtPointer)0}, |
---|
138 | |
---|
139 | /* the attribute list */ |
---|
140 | {(String)GLwNattribList, (String)GLwCAttribList, XtRPointer, sizeof(int *), offset(attribList), XtRImmediate, (XtPointer) NULL}, |
---|
141 | |
---|
142 | /* the visual info */ |
---|
143 | {(String)GLwNvisualInfo, (String)GLwCVisualInfo, (String)GLwRVisualInfo, sizeof (XVisualInfo *), offset(visualInfo), XtRImmediate, (XtPointer) NULL}, |
---|
144 | |
---|
145 | /* miscellaneous resources */ |
---|
146 | {(String)GLwNinstallColormap, (String)GLwCInstallColormap, XtRBoolean, sizeof(Boolean) , offset(installColormap) , XtRImmediate, (XtPointer)TRUE}, |
---|
147 | {(String)GLwNallocateBackground, (String)GLwCAllocateColors, XtRBoolean, sizeof(Boolean) , offset(allocateBackground) , XtRImmediate, (XtPointer)FALSE}, |
---|
148 | {(String)GLwNallocateOtherColors, (String)GLwCAllocateColors, XtRBoolean, sizeof(Boolean) , offset(allocateOtherColors), XtRImmediate, (XtPointer)FALSE}, |
---|
149 | {(String)GLwNinstallBackground, (String)GLwCInstallBackground, XtRBoolean, sizeof(Boolean) , offset(installBackground) , XtRImmediate, (XtPointer)TRUE}, |
---|
150 | {(String)GLwNginitCallback, (String)GLwCCallback, XtRCallback, sizeof(XtCallbackList), offset(ginitCallback) , XtRImmediate, (XtPointer)NULL}, |
---|
151 | {(String)GLwNinputCallback, (String)GLwCCallback, XtRCallback, sizeof(XtCallbackList), offset(inputCallback) , XtRImmediate, (XtPointer)NULL}, |
---|
152 | {(String)GLwNresizeCallback, (String)GLwCCallback, XtRCallback, sizeof(XtCallbackList), offset(resizeCallback) , XtRImmediate, (XtPointer)NULL}, |
---|
153 | {(String)GLwNexposeCallback, (String)GLwCCallback, XtRCallback, sizeof(XtCallbackList), offset(exposeCallback) , XtRImmediate, (XtPointer)NULL}, |
---|
154 | |
---|
155 | /* Changes to Motif primitive resources */ |
---|
156 | #ifdef __GLX_MOTIF |
---|
157 | {(String)XmNtraversalOn, (String)XmCTraversalOn, XmRBoolean, sizeof (Boolean), XtOffset (GLwDrawingAreaWidget, primitive.traversal_on), XmRImmediate, (XtPointer)FALSE}, |
---|
158 | |
---|
159 | /* highlighting is normally disabled, as when Motif tries to disable |
---|
160 | * highlighting, it tries to reset the color back to the parent's |
---|
161 | * background (usually Motif blue). Unfortunately, that is in a |
---|
162 | * different colormap, and doesn't work too well. |
---|
163 | */ |
---|
164 | {(String)XmNhighlightOnEnter, (String)XmCHighlightOnEnter, XmRBoolean, sizeof(Boolean) , XtOffset(GLwDrawingAreaWidget, primitive.highlight_on_enter) , XmRImmediate, (XtPointer)FALSE}, |
---|
165 | {(String)XmNhighlightThickness, (String)XmCHighlightThickness, XmRHorizontalDimension, sizeof(Dimension), XtOffset(GLwDrawingAreaWidget, primitive.highlight_thickness), XmRImmediate, (XtPointer)0}, |
---|
166 | #endif |
---|
167 | }; |
---|
168 | |
---|
169 | |
---|
170 | /* |
---|
171 | ** The following resources are reobtained using XtGetApplicationResources |
---|
172 | ** in the initialize proc. |
---|
173 | */ |
---|
174 | |
---|
175 | /* The colormap */ |
---|
176 | static XtResource initializeResources[] = { |
---|
177 | /* reobtain the colormap with the new visual */ |
---|
178 | {XtNcolormap, XtCColormap, XtRColormap, sizeof(Colormap), |
---|
179 | XtOffset(GLwDrawingAreaWidget, core.colormap), |
---|
180 | XtRCallProc,(XtPointer) createColormap}, |
---|
181 | }; |
---|
182 | |
---|
183 | |
---|
184 | /* reallocate any colors we need in the new colormap */ |
---|
185 | |
---|
186 | /* The background is obtained only if the allocateBackground resource is TRUE*/ |
---|
187 | static XtResource backgroundResources[] = { |
---|
188 | #ifdef __GLX_MOTIF |
---|
189 | {XmNbackground, XmCBackground,XmRPixel, |
---|
190 | sizeof(Pixel),XtOffset(GLwDrawingAreaWidget,core.background_pixel), |
---|
191 | XmRString,(XtPointer)"lightgrey"}, |
---|
192 | /*XmRCallProc,(XtPointer)_XmBackgroundColorDefault},*/ |
---|
193 | |
---|
194 | {XmNbackgroundPixmap,XmCPixmap,XmRXmBackgroundPixmap, |
---|
195 | sizeof(Pixmap),XtOffset(GLwDrawingAreaWidget,core.background_pixmap), |
---|
196 | XmRImmediate,(XtPointer)XmUNSPECIFIED_PIXMAP}, |
---|
197 | |
---|
198 | #else |
---|
199 | {XtNbackground,XtCBackground,XtRPixel,sizeof(Pixel), |
---|
200 | XtOffset(GLwDrawingAreaWidget,core.background_pixel), |
---|
201 | XtRString,(XtPointer)"lightgrey"}, |
---|
202 | /*XtRString,(XtPointer)"XtDefaultBackground"},*/ |
---|
203 | |
---|
204 | {XtNbackgroundPixmap, XtCPixmap, XtRPixmap, sizeof(Pixmap), |
---|
205 | XtOffset(GLwDrawingAreaWidget,core.background_pixmap), |
---|
206 | XtRImmediate,(XtPointer)XtUnspecifiedPixmap}, |
---|
207 | #endif |
---|
208 | }; |
---|
209 | |
---|
210 | |
---|
211 | |
---|
212 | /* The other colors such as the foreground are allocated only if |
---|
213 | * allocateOtherColors are set. These resources only exist in Motif. |
---|
214 | */ |
---|
215 | #ifdef __GLX_MOTIF |
---|
216 | static XtResource otherColorResources[] = { |
---|
217 | {XmNforeground,XmCForeground,XmRPixel, |
---|
218 | sizeof(Pixel),XtOffset(GLwDrawingAreaWidget,primitive.foreground), |
---|
219 | XmRString,(XtPointer)"lighgrey"}, |
---|
220 | /*XmRCallProc, (XtPointer) _XmForegroundColorDefault},*/ |
---|
221 | |
---|
222 | {XmNhighlightColor,XmCHighlightColor,XmRPixel,sizeof(Pixel), |
---|
223 | XtOffset(GLwDrawingAreaWidget,primitive.highlight_color), |
---|
224 | XmRString,(XtPointer)"lightgrey"}, |
---|
225 | /*XmRCallProc,(XtPointer)_XmHighlightColorDefault},*/ |
---|
226 | |
---|
227 | {XmNhighlightPixmap,XmCHighlightPixmap,XmRPrimHighlightPixmap, |
---|
228 | sizeof(Pixmap), |
---|
229 | XtOffset(GLwDrawingAreaWidget,primitive.highlight_pixmap), |
---|
230 | XmRImmediate,(XtPointer)XmUNSPECIFIED_PIXMAP}, |
---|
231 | /*XmRCallProc,(XtPointer)_XmPrimitiveHighlightPixmapDefault},*/ |
---|
232 | }; |
---|
233 | #endif |
---|
234 | |
---|
235 | |
---|
236 | #undef offset |
---|
237 | |
---|
238 | |
---|
239 | GLwDrawingAreaClassRec glwDrawingAreaClassRec = { |
---|
240 | { /* core fields */ |
---|
241 | #ifdef __GLX_MOTIF |
---|
242 | /* superclass */ (WidgetClass) &xmPrimitiveClassRec, |
---|
243 | /* class_name */ (String)"GLwMDrawingArea", |
---|
244 | #else /* not __GLX_MOTIF */ |
---|
245 | /* superclass */ (WidgetClass) &widgetClassRec, |
---|
246 | /* class_name */ (String)"GLwDrawingArea", |
---|
247 | #endif /* __GLX_MOTIF */ |
---|
248 | /* widget_size */ sizeof(GLwDrawingAreaRec), |
---|
249 | /* class_initialize */ NULL, |
---|
250 | /* class_part_initialize */ NULL, |
---|
251 | /* class_inited */ FALSE, |
---|
252 | /* initialize */ (XtInitProc) Initialize, |
---|
253 | /* initialize_hook */ NULL, |
---|
254 | /* realize */ Realize, |
---|
255 | /* actions */ actions, |
---|
256 | /* num_actions */ XtNumber(actions), |
---|
257 | /* resources */ resources, |
---|
258 | /* num_resources */ XtNumber(resources), |
---|
259 | /* xrm_class */ NULLQUARK, |
---|
260 | /* compress_motion */ TRUE, |
---|
261 | /* compress_exposure */ TRUE, |
---|
262 | /* compress_enterleave */ TRUE, |
---|
263 | /* visible_interest */ TRUE, |
---|
264 | /* destroy */ (XtWidgetProc) Destroy, |
---|
265 | /* resize */ (XtWidgetProc) Resize, |
---|
266 | /* expose */ (XtExposeProc) Redraw, |
---|
267 | /* set_values */ NULL, |
---|
268 | /* set_values_hook */ NULL, |
---|
269 | /* set_values_almost */ XtInheritSetValuesAlmost, |
---|
270 | /* get_values_hook */ NULL, |
---|
271 | /* accept_focus */ NULL, |
---|
272 | /* version */ XtVersion, |
---|
273 | /* callback_private */ NULL, |
---|
274 | /* tm_table */ defaultTranslations, |
---|
275 | /* query_geometry */ XtInheritQueryGeometry, |
---|
276 | /* display_accelerator */ XtInheritDisplayAccelerator, |
---|
277 | /* extension */ NULL |
---|
278 | }, |
---|
279 | #ifdef __GLX_MOTIF /* primitive resources (XmPrimitiveClassPart) */ |
---|
280 | { |
---|
281 | /* border_highlight */ XmInheritBorderHighlight, |
---|
282 | /* border_unhighlight */ XmInheritBorderUnhighlight, |
---|
283 | /* translations */ XtInheritTranslations, |
---|
284 | /* arm_and_activate */ NULL, |
---|
285 | /* get_resources */ NULL, |
---|
286 | /* num get_resources */ 0, |
---|
287 | /* extension */ NULL, |
---|
288 | }, |
---|
289 | #endif |
---|
290 | { NULL }, |
---|
291 | }; |
---|
292 | |
---|
293 | WidgetClass glwDrawingAreaWidgetClass=(WidgetClass)&glwDrawingAreaClassRec; |
---|
294 | |
---|
295 | |
---|
296 | |
---|
297 | static void error(Widget w,const char* string){ |
---|
298 | char buf[100]; |
---|
299 | #ifdef __GLX_MOTIF |
---|
300 | sprintf(buf,"GLwMDrawingArea: %s\n",string); |
---|
301 | #else |
---|
302 | sprintf(buf,"GLwDrawingArea: %s\n",string); |
---|
303 | #endif |
---|
304 | XtAppError(XtWidgetToApplicationContext(w),buf); |
---|
305 | } |
---|
306 | |
---|
307 | |
---|
308 | static void warning(Widget w,const char* string){ |
---|
309 | char buf[100]; |
---|
310 | #ifdef __GLX_MOTIF |
---|
311 | sprintf (buf, "GLwMDraw: %s\n", string); |
---|
312 | #else |
---|
313 | sprintf (buf, "GLwDraw: %s\n", string); |
---|
314 | #endif |
---|
315 | XtAppWarning(XtWidgetToApplicationContext(w), buf); |
---|
316 | } |
---|
317 | |
---|
318 | |
---|
319 | |
---|
320 | /* Initialize the attribList based on the attributes */ |
---|
321 | static void createAttribList(GLwDrawingAreaWidget w){ |
---|
322 | int *ptr; |
---|
323 | w->glwDrawingArea.attribList = (int*)XtMalloc(ATTRIBLIST_SIZE*sizeof(int)); |
---|
324 | if(!w->glwDrawingArea.attribList){ |
---|
325 | error((Widget)w,"Unable to allocate attribute list"); |
---|
326 | } |
---|
327 | ptr = w->glwDrawingArea.attribList; |
---|
328 | *ptr++ = GLX_BUFFER_SIZE; |
---|
329 | *ptr++ = w->glwDrawingArea.bufferSize; |
---|
330 | *ptr++ = GLX_LEVEL; |
---|
331 | *ptr++ = w->glwDrawingArea.level; |
---|
332 | if(w->glwDrawingArea.rgba) *ptr++ = GLX_RGBA; |
---|
333 | if(w->glwDrawingArea.doublebuffer) *ptr++ = GLX_DOUBLEBUFFER; |
---|
334 | if(w->glwDrawingArea.stereo) *ptr++ = GLX_STEREO; |
---|
335 | *ptr++ = GLX_AUX_BUFFERS; |
---|
336 | *ptr++ = w->glwDrawingArea.auxBuffers; |
---|
337 | *ptr++ = GLX_RED_SIZE; |
---|
338 | *ptr++ = w->glwDrawingArea.redSize; |
---|
339 | *ptr++ = GLX_GREEN_SIZE; |
---|
340 | *ptr++ = w->glwDrawingArea.greenSize; |
---|
341 | *ptr++ = GLX_BLUE_SIZE; |
---|
342 | *ptr++ = w->glwDrawingArea.blueSize; |
---|
343 | *ptr++ = GLX_ALPHA_SIZE; |
---|
344 | *ptr++ = w->glwDrawingArea.alphaSize; |
---|
345 | *ptr++ = GLX_DEPTH_SIZE; |
---|
346 | *ptr++ = w->glwDrawingArea.depthSize; |
---|
347 | *ptr++ = GLX_STENCIL_SIZE; |
---|
348 | *ptr++ = w->glwDrawingArea.stencilSize; |
---|
349 | *ptr++ = GLX_ACCUM_RED_SIZE; |
---|
350 | *ptr++ = w->glwDrawingArea.accumRedSize; |
---|
351 | *ptr++ = GLX_ACCUM_GREEN_SIZE; |
---|
352 | *ptr++ = w->glwDrawingArea.accumGreenSize; |
---|
353 | *ptr++ = GLX_ACCUM_BLUE_SIZE; |
---|
354 | *ptr++ = w->glwDrawingArea.accumBlueSize; |
---|
355 | *ptr++ = GLX_ACCUM_ALPHA_SIZE; |
---|
356 | *ptr++ = w->glwDrawingArea.accumAlphaSize; |
---|
357 | *ptr++ = None; |
---|
358 | assert((ptr-w->glwDrawingArea.attribList)<ATTRIBLIST_SIZE); |
---|
359 | } |
---|
360 | |
---|
361 | |
---|
362 | |
---|
363 | /* Initialize the visualInfo based on the attribute list */ |
---|
364 | static void createVisualInfo(GLwDrawingAreaWidget w){ |
---|
365 | assert(w->glwDrawingArea.attribList); |
---|
366 | w->glwDrawingArea.visualInfo=glXChooseVisual(XtDisplay(w),XScreenNumberOfScreen(XtScreen(w)),w->glwDrawingArea.attribList); |
---|
367 | if(!w->glwDrawingArea.visualInfo) error((Widget)w,"requested visual not supported"); |
---|
368 | } |
---|
369 | |
---|
370 | |
---|
371 | |
---|
372 | /* Initialize the colormap based on the visual info. |
---|
373 | * This routine maintains a cache of visual-infos to colormaps. If two |
---|
374 | * widgets share the same visual info, they share the same colormap. |
---|
375 | * This function is called by the callProc of the colormap resource entry. |
---|
376 | */ |
---|
377 | static void createColormap(GLwDrawingAreaWidget w,int offset,XrmValue *value){ |
---|
378 | static struct cmapCache { Visual *visual; Colormap cmap; } *cmapCache; |
---|
379 | static int cacheEntries=0; |
---|
380 | static int cacheMalloced=0; |
---|
381 | int i; |
---|
382 | |
---|
383 | USE(offset); |
---|
384 | |
---|
385 | assert(w->glwDrawingArea.visualInfo); |
---|
386 | |
---|
387 | /* see if we can find it in the cache */ |
---|
388 | for(i=0; i<cacheEntries; i++){ |
---|
389 | if(cmapCache[i].visual==w->glwDrawingArea.visualInfo->visual){ |
---|
390 | value->addr=(XtPointer)(&cmapCache[i].cmap); |
---|
391 | return; |
---|
392 | } |
---|
393 | } |
---|
394 | |
---|
395 | /* not in the cache, create a new entry */ |
---|
396 | if(cacheEntries >= cacheMalloced){ |
---|
397 | /* need to malloc a new one. Since we are likely to have only a |
---|
398 | * few colormaps, we allocate one the first time, and double |
---|
399 | * each subsequent time. |
---|
400 | */ |
---|
401 | if(cacheMalloced==0){ |
---|
402 | cacheMalloced=1; |
---|
403 | cmapCache=(struct cmapCache*)XtMalloc(sizeof(struct cmapCache)); |
---|
404 | } |
---|
405 | else{ |
---|
406 | cacheMalloced<<=1; |
---|
407 | cmapCache=(struct cmapCache*)XtRealloc((char*)cmapCache,sizeof(struct cmapCache)*cacheMalloced); |
---|
408 | } |
---|
409 | } |
---|
410 | |
---|
411 | cmapCache[cacheEntries].cmap=XCreateColormap(XtDisplay(w), |
---|
412 | RootWindow(XtDisplay(w), |
---|
413 | w->glwDrawingArea.visualInfo->screen), |
---|
414 | w->glwDrawingArea.visualInfo->visual, |
---|
415 | AllocNone); |
---|
416 | cmapCache[cacheEntries].visual=w->glwDrawingArea.visualInfo->visual; |
---|
417 | value->addr=(XtPointer)(&cmapCache[cacheEntries++].cmap); |
---|
418 | } |
---|
419 | |
---|
420 | |
---|
421 | |
---|
422 | static void Initialize(GLwDrawingAreaWidget req,GLwDrawingAreaWidget neww,ArgList args,Cardinal *num_args){ |
---|
423 | |
---|
424 | /* fix size */ |
---|
425 | if(req->core.width==0) neww->core.width=100; |
---|
426 | if(req->core.height==0) neww->core.width=100; |
---|
427 | |
---|
428 | /* create the attribute list if needed */ |
---|
429 | neww->glwDrawingArea.myList=FALSE; |
---|
430 | if(neww->glwDrawingArea.attribList==NULL){ |
---|
431 | neww->glwDrawingArea.myList=TRUE; |
---|
432 | createAttribList(neww); |
---|
433 | } |
---|
434 | |
---|
435 | /* Gotta have it */ |
---|
436 | assert(neww->glwDrawingArea.attribList); |
---|
437 | |
---|
438 | /* determine the visual info if needed */ |
---|
439 | neww->glwDrawingArea.myVisual=FALSE; |
---|
440 | if(neww->glwDrawingArea.visualInfo==NULL){ |
---|
441 | neww->glwDrawingArea.myVisual=TRUE; |
---|
442 | createVisualInfo(neww); |
---|
443 | } |
---|
444 | |
---|
445 | /* Gotta have that too */ |
---|
446 | assert(neww->glwDrawingArea.visualInfo); |
---|
447 | |
---|
448 | neww->core.depth=neww->glwDrawingArea.visualInfo->depth; |
---|
449 | |
---|
450 | /* Reobtain the colormap and colors in it using XtGetApplicationResources*/ |
---|
451 | XtGetApplicationResources((Widget)neww,neww,initializeResources,XtNumber(initializeResources),args,*num_args); |
---|
452 | |
---|
453 | /* obtain the color resources if appropriate */ |
---|
454 | if(req->glwDrawingArea.allocateBackground){ |
---|
455 | XtGetApplicationResources((Widget)neww,neww,backgroundResources,XtNumber(backgroundResources),args,*num_args); |
---|
456 | } |
---|
457 | |
---|
458 | #ifdef __GLX_MOTIF |
---|
459 | if(req->glwDrawingArea.allocateOtherColors){ |
---|
460 | XtGetApplicationResources((Widget)neww,neww,otherColorResources,XtNumber(otherColorResources),args,*num_args); |
---|
461 | } |
---|
462 | #endif |
---|
463 | } |
---|
464 | |
---|
465 | |
---|
466 | |
---|
467 | static void Realize(Widget w,Mask *valueMask,XSetWindowAttributes *attributes){ |
---|
468 | GLwDrawingAreaWidget glw=(GLwDrawingAreaWidget)w; |
---|
469 | GLwDrawingAreaCallbackStruct cb; |
---|
470 | Widget parentShell; |
---|
471 | Status status; |
---|
472 | Window windows[2],*windowsReturn,*windowList; |
---|
473 | int countReturn,i; |
---|
474 | |
---|
475 | /* if we haven't requested that the background be both installed and |
---|
476 | * allocated, don't install it. |
---|
477 | */ |
---|
478 | if(!(glw->glwDrawingArea.installBackground && glw->glwDrawingArea.allocateBackground)){ |
---|
479 | *valueMask&=~CWBackPixel; |
---|
480 | } |
---|
481 | |
---|
482 | XtCreateWindow(w,(unsigned int)InputOutput,glw->glwDrawingArea.visualInfo->visual,*valueMask,attributes); |
---|
483 | |
---|
484 | /* if appropriate, call XSetWMColormapWindows to install the colormap */ |
---|
485 | if(glw->glwDrawingArea.installColormap){ |
---|
486 | |
---|
487 | /* Get parent shell */ |
---|
488 | for(parentShell=XtParent(w); parentShell&&!XtIsShell(parentShell); parentShell=XtParent(parentShell)); |
---|
489 | |
---|
490 | if(parentShell && XtWindow(parentShell)){ |
---|
491 | |
---|
492 | /* check to see if there is already a property */ |
---|
493 | status=XGetWMColormapWindows(XtDisplay(parentShell),XtWindow(parentShell),&windowsReturn,&countReturn); |
---|
494 | |
---|
495 | /* if no property, just create one */ |
---|
496 | if(!status){ |
---|
497 | windows[0]=XtWindow(w); |
---|
498 | windows[1]=XtWindow(parentShell); |
---|
499 | XSetWMColormapWindows(XtDisplay(parentShell),XtWindow(parentShell),windows,2); |
---|
500 | } |
---|
501 | |
---|
502 | /* there was a property, add myself to the beginning */ |
---|
503 | else{ |
---|
504 | windowList=(Window *)XtMalloc((sizeof(Window))*(countReturn+1)); |
---|
505 | windowList[0]=XtWindow(w); |
---|
506 | for(i=0; i<countReturn; i++) windowList[i+1]=windowsReturn[i]; |
---|
507 | XSetWMColormapWindows(XtDisplay(parentShell),XtWindow(parentShell),windowList,countReturn+1); |
---|
508 | XtFree((char*)windowList); |
---|
509 | XtFree((char*)windowsReturn); |
---|
510 | } |
---|
511 | } |
---|
512 | else{ |
---|
513 | warning(w,"Could not set colormap property on parent shell"); |
---|
514 | } |
---|
515 | } |
---|
516 | |
---|
517 | /* Invoke callbacks */ |
---|
518 | cb.reason=GLwCR_GINIT; |
---|
519 | cb.event=NULL; |
---|
520 | cb.width=glw->core.width; |
---|
521 | cb.height=glw->core.height; |
---|
522 | XtCallCallbackList((Widget)glw,glw->glwDrawingArea.ginitCallback,&cb); |
---|
523 | } |
---|
524 | |
---|
525 | |
---|
526 | |
---|
527 | static void Redraw(GLwDrawingAreaWidget w,XEvent *event,Region region){ |
---|
528 | GLwDrawingAreaCallbackStruct cb; |
---|
529 | USE(region); |
---|
530 | if(!XtIsRealized((Widget)w)) return; |
---|
531 | cb.reason = GLwCR_EXPOSE; |
---|
532 | cb.event = event; |
---|
533 | cb.width = w->core.width; |
---|
534 | cb.height = w->core.height; |
---|
535 | XtCallCallbackList((Widget)w,w->glwDrawingArea.exposeCallback,&cb); |
---|
536 | } |
---|
537 | |
---|
538 | |
---|
539 | |
---|
540 | static void Resize(GLwDrawingAreaWidget glw){ |
---|
541 | GLwDrawingAreaCallbackStruct cb; |
---|
542 | if(!XtIsRealized((Widget)glw)) return; |
---|
543 | cb.reason=GLwCR_RESIZE; |
---|
544 | cb.event=NULL; |
---|
545 | cb.width=glw->core.width; |
---|
546 | cb.height=glw->core.height; |
---|
547 | XtCallCallbackList((Widget)glw,glw->glwDrawingArea.resizeCallback,&cb); |
---|
548 | } |
---|
549 | |
---|
550 | |
---|
551 | |
---|
552 | static void Destroy(GLwDrawingAreaWidget glw){ |
---|
553 | Window *windowsReturn; |
---|
554 | Widget parentShell; |
---|
555 | Status status; |
---|
556 | int countReturn; |
---|
557 | int i; |
---|
558 | |
---|
559 | if(glw->glwDrawingArea.myList && glw->glwDrawingArea.attribList){ |
---|
560 | XtFree((XtPointer)glw->glwDrawingArea.attribList); |
---|
561 | } |
---|
562 | |
---|
563 | if(glw->glwDrawingArea.myVisual && glw->glwDrawingArea.visualInfo){ |
---|
564 | XtFree((XtPointer)glw->glwDrawingArea.visualInfo); |
---|
565 | } |
---|
566 | |
---|
567 | /* if my colormap was installed, remove it */ |
---|
568 | if(glw->glwDrawingArea.installColormap){ |
---|
569 | |
---|
570 | /* Get parent shell */ |
---|
571 | for(parentShell=XtParent(glw); parentShell&&!XtIsShell(parentShell); parentShell=XtParent(parentShell)); |
---|
572 | |
---|
573 | if(parentShell && XtWindow(parentShell)){ |
---|
574 | |
---|
575 | /* make sure there is a property */ |
---|
576 | status=XGetWMColormapWindows(XtDisplay(parentShell),XtWindow(parentShell),&windowsReturn,&countReturn); |
---|
577 | |
---|
578 | /* if no property, just return. If there was a property, continue */ |
---|
579 | if(status){ |
---|
580 | |
---|
581 | /* search for a match */ |
---|
582 | for(i=0; i<countReturn; i++){ |
---|
583 | if(windowsReturn[i]==XtWindow(glw)){ |
---|
584 | |
---|
585 | /* we found a match, now copy the rest down */ |
---|
586 | for(i++; i<countReturn; i++){ windowsReturn[i-1]=windowsReturn[i]; } |
---|
587 | |
---|
588 | XSetWMColormapWindows(XtDisplay(parentShell),XtWindow(parentShell),windowsReturn,countReturn-1); |
---|
589 | break; |
---|
590 | } |
---|
591 | } |
---|
592 | XtFree((char *)windowsReturn); |
---|
593 | } |
---|
594 | } |
---|
595 | } |
---|
596 | } |
---|
597 | |
---|
598 | |
---|
599 | |
---|
600 | /* Action routine for keyboard and mouse events */ |
---|
601 | static void glwInput(GLwDrawingAreaWidget glw,XEvent *event,String *params,Cardinal *numParams){ |
---|
602 | GLwDrawingAreaCallbackStruct cb; |
---|
603 | USE(params); |
---|
604 | USE(numParams); |
---|
605 | cb.reason = GLwCR_INPUT; |
---|
606 | cb.event = event; |
---|
607 | cb.width = glw->core.width; |
---|
608 | cb.height = glw->core.height; |
---|
609 | XtCallCallbackList((Widget)glw,glw->glwDrawingArea.inputCallback,&cb); |
---|
610 | } |
---|
611 | |
---|
612 | |
---|
613 | #ifdef __GLX_MOTIF |
---|
614 | |
---|
615 | /* Create routine */ |
---|
616 | Widget GLwCreateMDrawingArea(Widget parent, char *name,ArgList arglist,Cardinal argcount){ |
---|
617 | return XtCreateWidget(name,glwMDrawingAreaWidgetClass, parent, arglist,argcount); |
---|
618 | } |
---|
619 | |
---|
620 | #endif |
---|
621 | |
---|
622 | |
---|
623 | #ifndef __GLX_MOTIF |
---|
624 | |
---|
625 | /* Make context current */ |
---|
626 | void GLwDrawingAreaMakeCurrent(Widget w,GLXContext ctx){ |
---|
627 | glXMakeCurrent(XtDisplay(w),XtWindow(w),ctx); |
---|
628 | } |
---|
629 | |
---|
630 | |
---|
631 | /* Swap buffers convenience function */ |
---|
632 | void GLwDrawingAreaSwapBuffers(Widget w){ |
---|
633 | glXSwapBuffers(XtDisplay(w),XtWindow(w)); |
---|
634 | } |
---|
635 | |
---|
636 | #endif |
---|