Merge branch 'master' into gallium-0.2
[mesa.git] / src / glut / os2 / glut_cindex.cpp
1
2 /* Copyright (c) Mark J. Kilgard, 1994, 1996, 1997. */
3
4 /* This program is freely distributable without licensing fees
5 and is provided without guarantee or warrantee expressed or
6 implied. This program is -not- in the public domain. */
7
8 #include <stdlib.h>
9 #include "glutint.h"
10
11 #if defined(__OS2PM__)
12 #define IsWindowVisible WinIsWindowVisible
13 #endif
14
15 #define CLAMP(i) ((i) > 1.0 ? 1.0 : ((i) < 0.0 ? 0.0 : (i)))
16
17 /* CENTRY */
18 void GLUTAPIENTRY
19 glutSetColor(int ndx, GLfloat red, GLfloat green, GLfloat blue)
20 {
21 GLUTcolormap *cmap, *newcmap;
22 XVisualInfo *vis;
23 XColor color;
24 int i;
25
26 if (__glutCurrentWindow->renderWin == __glutCurrentWindow->win) {
27 cmap = __glutCurrentWindow->colormap;
28 vis = __glutCurrentWindow->vis;
29 } else {
30 cmap = __glutCurrentWindow->overlay->colormap;
31 vis = __glutCurrentWindow->overlay->vis;
32 if (ndx == __glutCurrentWindow->overlay->transparentPixel) {
33 __glutWarning(
34 "glutSetColor: cannot set color of overlay transparent index %d\n",
35 ndx);
36 return;
37 }
38 }
39
40 if (!cmap) {
41 __glutWarning("glutSetColor: current window is RGBA");
42 return;
43 }
44 #if defined(_WIN32) || defined(__OS2PM__)
45 if (ndx >= 256 || /* always assume 256 colors on Win32 */
46 #else
47 if (ndx >= vis->visual->map_entries ||
48 #endif
49 ndx < 0) {
50 __glutWarning("glutSetColor: index %d out of range", ndx);
51 return;
52 }
53 if (cmap->refcnt > 1) {
54 newcmap = __glutAssociateNewColormap(vis);
55 cmap->refcnt--;
56 /* Wouldn't it be nice if XCopyColormapAndFree could be
57 told not to free the old colormap's entries! */
58 for (i = cmap->size - 1; i >= 0; i--) {
59 if (i == ndx) {
60 /* We are going to set this cell shortly! */
61 continue;
62 }
63 if (cmap->cells[i].component[GLUT_RED] >= 0.0) {
64 color.pixel = i;
65 newcmap->cells[i].component[GLUT_RED] =
66 cmap->cells[i].component[GLUT_RED];
67 color.red = (GLfloat) 0xffff *
68 cmap->cells[i].component[GLUT_RED];
69 newcmap->cells[i].component[GLUT_GREEN] =
70 cmap->cells[i].component[GLUT_GREEN];
71 color.green = (GLfloat) 0xffff *
72 cmap->cells[i].component[GLUT_GREEN];
73 newcmap->cells[i].component[GLUT_BLUE] =
74 cmap->cells[i].component[GLUT_BLUE];
75 color.blue = (GLfloat) 0xffff *
76 cmap->cells[i].component[GLUT_BLUE];
77 color.flags = DoRed | DoGreen | DoBlue;
78 #if defined(_WIN32) || defined(__OS2PM__)
79 if (IsWindowVisible(__glutCurrentWindow->win)) {
80 XHDC = __glutCurrentWindow->hdc;
81 } else {
82 XHDC = 0;
83 }
84 #endif
85 XStoreColor(__glutDisplay, newcmap->cmap, &color);
86 } else {
87 /* Leave unallocated entries unallocated. */
88 }
89 }
90 cmap = newcmap;
91 if (__glutCurrentWindow->renderWin == __glutCurrentWindow->win) {
92 __glutCurrentWindow->colormap = cmap;
93 __glutCurrentWindow->cmap = cmap->cmap;
94 } else {
95 __glutCurrentWindow->overlay->colormap = cmap;
96 __glutCurrentWindow->overlay->cmap = cmap->cmap;
97 }
98 XSetWindowColormap(__glutDisplay,
99 __glutCurrentWindow->renderWin, cmap->cmap);
100
101 #if !defined(_WIN32) && !defined(__OS2PM__)
102 {
103 GLUTwindow *toplevel;
104
105 toplevel = __glutToplevelOf(__glutCurrentWindow);
106 if (toplevel->cmap != cmap->cmap) {
107 __glutPutOnWorkList(toplevel, GLUT_COLORMAP_WORK);
108 }
109 }
110 #endif
111 }
112 color.pixel = ndx;
113 red = CLAMP(red);
114 cmap->cells[ndx].component[GLUT_RED] = red;
115 color.red = (GLfloat) 0xffff *red;
116 green = CLAMP(green);
117 cmap->cells[ndx].component[GLUT_GREEN] = green;
118 color.green = (GLfloat) 0xffff *green;
119 blue = CLAMP(blue);
120 cmap->cells[ndx].component[GLUT_BLUE] = blue;
121 color.blue = (GLfloat) 0xffff *blue;
122 color.flags = DoRed | DoGreen | DoBlue;
123 #if defined(_WIN32) || defined(__OS2PM__)
124 if (IsWindowVisible(__glutCurrentWindow->win)) {
125 XHDC = __glutCurrentWindow->hdc;
126 } else {
127 XHDC = 0;
128 }
129 #endif
130 XStoreColor(__glutDisplay, cmap->cmap, &color);
131 }
132
133 GLfloat GLUTAPIENTRY
134 glutGetColor(int ndx, int comp)
135 {
136 GLUTcolormap *colormap;
137 XVisualInfo *vis;
138
139 if (__glutCurrentWindow->renderWin == __glutCurrentWindow->win) {
140 colormap = __glutCurrentWindow->colormap;
141 vis = __glutCurrentWindow->vis;
142 } else {
143 colormap = __glutCurrentWindow->overlay->colormap;
144 vis = __glutCurrentWindow->overlay->vis;
145 if (ndx == __glutCurrentWindow->overlay->transparentPixel) {
146 __glutWarning("glutGetColor: requesting overlay transparent index %d\n",
147 ndx);
148 return -1.0;
149 }
150 }
151
152 if (!colormap) {
153 __glutWarning("glutGetColor: current window is RGBA");
154 return -1.0;
155 }
156 #if defined(_WIN32) || defined(__OS2PM__)
157 #define OUT_OF_RANGE_NDX(ndx) (ndx >= 256 || ndx < 0)
158 #else
159 #define OUT_OF_RANGE_NDX(ndx) (ndx >= vis->visual->map_entries || ndx < 0)
160 #endif
161 if (OUT_OF_RANGE_NDX(ndx)) {
162 __glutWarning("glutGetColor: index %d out of range", ndx);
163 return -1.0;
164 }
165 return colormap->cells[ndx].component[comp];
166 }
167
168 void GLUTAPIENTRY
169 glutCopyColormap(int winnum)
170 {
171 GLUTwindow *window = __glutWindowList[winnum - 1];
172 GLUTcolormap *oldcmap, *newcmap;
173 XVisualInfo *dstvis;
174
175 if (__glutCurrentWindow->renderWin == __glutCurrentWindow->win) {
176 oldcmap = __glutCurrentWindow->colormap;
177 dstvis = __glutCurrentWindow->vis;
178 newcmap = window->colormap;
179 } else {
180 oldcmap = __glutCurrentWindow->overlay->colormap;
181 dstvis = __glutCurrentWindow->overlay->vis;
182 if (!window->overlay) {
183 __glutWarning("glutCopyColormap: window %d has no overlay", winnum);
184 return;
185 }
186 newcmap = window->overlay->colormap;
187 }
188
189 if (!oldcmap) {
190 __glutWarning("glutCopyColormap: destination colormap must be color index");
191 return;
192 }
193 if (!newcmap) {
194 __glutWarning(
195 "glutCopyColormap: source colormap of window %d must be color index",
196 winnum);
197 return;
198 }
199 if (newcmap == oldcmap) {
200 /* Source and destination are the same; now copy needed. */
201 return;
202 }
203 #if !defined(_WIN32) && !defined(__OS2PM__)
204 /* Play safe: compare visual IDs, not Visual*'s. */
205 if (newcmap->visual->visualid == oldcmap->visual->visualid) {
206 #endif
207 /* Visuals match! "Copy" by reference... */
208 __glutFreeColormap(oldcmap);
209 newcmap->refcnt++;
210 if (__glutCurrentWindow->renderWin == __glutCurrentWindow->win) {
211 __glutCurrentWindow->colormap = newcmap;
212 __glutCurrentWindow->cmap = newcmap->cmap;
213 } else {
214 __glutCurrentWindow->overlay->colormap = newcmap;
215 __glutCurrentWindow->overlay->cmap = newcmap->cmap;
216 }
217 XSetWindowColormap(__glutDisplay, __glutCurrentWindow->renderWin,
218 newcmap->cmap);
219 #if !defined(_WIN32) && !defined(__OS2PM__)
220 __glutPutOnWorkList(__glutToplevelOf(window), GLUT_COLORMAP_WORK);
221 bla bla bla
222
223 } else {
224 GLUTcolormap *copycmap;
225 XColor color;
226 int i, last;
227
228 /* Visuals different - need a distinct X colormap! */
229 copycmap = __glutAssociateNewColormap(dstvis);
230 /* Wouldn't it be nice if XCopyColormapAndFree could be
231 told not to free the old colormap's entries! */
232 last = newcmap->size;
233 if (last > copycmap->size) {
234 last = copycmap->size;
235 }
236 for (i = last - 1; i >= 0; i--) {
237 if (newcmap->cells[i].component[GLUT_RED] >= 0.0) {
238 color.pixel = i;
239 copycmap->cells[i].component[GLUT_RED] =
240 newcmap->cells[i].component[GLUT_RED];
241 color.red = (GLfloat) 0xffff *
242 newcmap->cells[i].component[GLUT_RED];
243 copycmap->cells[i].component[GLUT_GREEN] =
244 newcmap->cells[i].component[GLUT_GREEN];
245 color.green = (GLfloat) 0xffff *
246 newcmap->cells[i].component[GLUT_GREEN];
247 copycmap->cells[i].component[GLUT_BLUE] =
248 newcmap->cells[i].component[GLUT_BLUE];
249 color.blue = (GLfloat) 0xffff *
250 newcmap->cells[i].component[GLUT_BLUE];
251 color.flags = DoRed | DoGreen | DoBlue;
252 XStoreColor(__glutDisplay, copycmap->cmap, &color);
253 }
254 }
255 }
256 #endif
257 }
258 /* ENDCENTRY */