themes: Workaround for bug where a background color of RGB 0,0,0 in Black color schem...
[ntk.git] / src / Fl_Gl_Choice.cxx
blob52a24364ebe77d6dbb8f1ba9664c23631a49abec
1 //
2 // "$Id: Fl_Gl_Choice.cxx 8396 2011-02-07 13:51:17Z manolo $"
3 //
4 // OpenGL visual selection code for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 // USA.
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
28 #include <config.h>
29 #if HAVE_GL
31 # include <FL/Fl.H>
32 # include <FL/x.H>
33 # include <stdlib.h>
34 # include "Fl_Gl_Choice.H"
35 # include <FL/gl_draw.H>
36 # include "flstring.h"
37 # include <FL/fl_utf8.h>
39 # ifdef __APPLE__
40 # include <ApplicationServices/ApplicationServices.h>
41 # include <FL/Fl_Window.H>
42 # endif
44 # ifdef WIN32
45 void fl_save_dc(HWND, HDC);
46 # endif
48 static Fl_Gl_Choice *first;
50 // this assumes one of the two arguments is zero:
51 // We keep the list system in Win32 to stay compatible and interpret
52 // the list later...
53 Fl_Gl_Choice *Fl_Gl_Choice::find(int m, const int *alistp) {
54 Fl_Gl_Choice *g;
56 for (g = first; g; g = g->next)
57 if (g->mode == m && g->alist == alistp)
58 return g;
60 #if defined(USE_X11)
61 const int *blist;
62 int list[32];
64 if (alistp)
65 blist = alistp;
66 else {
67 int n = 0;
68 if (m & FL_INDEX) {
69 list[n++] = GLX_BUFFER_SIZE;
70 list[n++] = 8; // glut tries many sizes, but this should work...
71 } else {
72 list[n++] = GLX_RGBA;
73 list[n++] = GLX_GREEN_SIZE;
74 list[n++] = (m & FL_RGB8) ? 8 : 1;
75 if (m & FL_ALPHA) {
76 list[n++] = GLX_ALPHA_SIZE;
77 list[n++] = (m & FL_RGB8) ? 8 : 1;
79 if (m & FL_ACCUM) {
80 list[n++] = GLX_ACCUM_GREEN_SIZE;
81 list[n++] = 1;
82 if (m & FL_ALPHA) {
83 list[n++] = GLX_ACCUM_ALPHA_SIZE;
84 list[n++] = 1;
88 if (m & FL_DOUBLE) {
89 list[n++] = GLX_DOUBLEBUFFER;
91 if (m & FL_DEPTH) {
92 list[n++] = GLX_DEPTH_SIZE; list[n++] = 1;
94 if (m & FL_STENCIL) {
95 list[n++] = GLX_STENCIL_SIZE; list[n++] = 1;
97 if (m & FL_STEREO) {
98 list[n++] = GLX_STEREO;
100 # if defined(GLX_VERSION_1_1) && defined(GLX_SGIS_multisample)
101 if (m & FL_MULTISAMPLE) {
102 list[n++] = GLX_SAMPLES_SGIS;
103 list[n++] = 4; // value Glut uses
105 # endif
106 list[n] = 0;
107 blist = list;
110 fl_open_display();
111 XVisualInfo *visp = glXChooseVisual(fl_display, fl_screen, (int *)blist);
112 if (!visp) {
113 # if defined(GLX_VERSION_1_1) && defined(GLX_SGIS_multisample)
114 if (m&FL_MULTISAMPLE) return find(m&~FL_MULTISAMPLE,0);
115 # endif
116 return 0;
119 #elif defined(__APPLE_QUARTZ__)
120 // warning: the Quartz version should probably use Core GL (CGL) instead of AGL
121 const int *blist;
122 int list[32];
124 if (alistp)
125 blist = alistp;
126 else {
127 int n = 0;
128 if (m & FL_INDEX) {
129 list[n++] = AGL_BUFFER_SIZE;
130 list[n++] = 8; // glut tries many sizes, but this should work...
131 } else {
132 list[n++] = AGL_RGBA;
133 list[n++] = AGL_GREEN_SIZE;
134 list[n++] = (m & FL_RGB8) ? 8 : 1;
135 if (m & FL_ALPHA) {
136 list[n++] = AGL_ALPHA_SIZE;
137 list[n++] = (m & FL_RGB8) ? 8 : 1;
139 if (m & FL_ACCUM) {
140 list[n++] = AGL_ACCUM_GREEN_SIZE;
141 list[n++] = 1;
142 if (m & FL_ALPHA) {
143 list[n++] = AGL_ACCUM_ALPHA_SIZE;
144 list[n++] = 1;
148 if (m & FL_DOUBLE) {
149 list[n++] = AGL_DOUBLEBUFFER;
151 if (m & FL_DEPTH) {
152 list[n++] = AGL_DEPTH_SIZE; list[n++] = 24;
154 if (m & FL_STENCIL) {
155 list[n++] = AGL_STENCIL_SIZE; list[n++] = 1;
157 # ifdef AGL_STEREO
158 if (m & FL_STEREO) {
159 list[n++] = AGL_STEREO;
161 # endif
162 list[n] = AGL_NONE;
163 blist = list;
165 fl_open_display();
166 AGLPixelFormat fmt = aglChoosePixelFormat(NULL, 0, (GLint*)blist);
167 if (!fmt) return 0;
169 #elif defined(WIN32)
171 // Replacement for ChoosePixelFormat() that finds one with an overlay
172 // if possible:
173 if (!fl_gc) fl_GetDC(0);
174 int pixelformat = 0;
175 PIXELFORMATDESCRIPTOR chosen_pfd;
176 for (int i = 1; ; i++) {
177 PIXELFORMATDESCRIPTOR pfd;
178 if (!DescribePixelFormat(fl_gc, i, sizeof(pfd), &pfd)) break;
179 // continue if it does not satisfy our requirements:
180 if (~pfd.dwFlags & (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL)) continue;
181 if (pfd.iPixelType != ((m&FL_INDEX)?PFD_TYPE_COLORINDEX:PFD_TYPE_RGBA)) continue;
182 if ((m & FL_ALPHA) && !pfd.cAlphaBits) continue;
183 if ((m & FL_ACCUM) && !pfd.cAccumBits) continue;
184 if ((!(m & FL_DOUBLE)) != (!(pfd.dwFlags & PFD_DOUBLEBUFFER))) continue;
185 if ((!(m & FL_STEREO)) != (!(pfd.dwFlags & PFD_STEREO))) continue;
186 if ((m & FL_DEPTH) && !pfd.cDepthBits) continue;
187 if ((m & FL_STENCIL) && !pfd.cStencilBits) continue;
188 // see if better than the one we have already:
189 if (pixelformat) {
190 // offering non-generic rendering is better (read: hardware accelleration)
191 if (!(chosen_pfd.dwFlags & PFD_GENERIC_FORMAT) &&
192 (pfd.dwFlags & PFD_GENERIC_FORMAT)) continue;
193 // offering overlay is better:
194 else if (!(chosen_pfd.bReserved & 15) && (pfd.bReserved & 15)) {}
195 // otherwise more bit planes is better:
196 else if (chosen_pfd.cColorBits > pfd.cColorBits) continue;
197 else if (chosen_pfd.cDepthBits > pfd.cDepthBits) continue;
199 pixelformat = i;
200 chosen_pfd = pfd;
202 //printf("Chosen pixel format is %d\n", pixelformat);
203 if (!pixelformat) return 0;
204 #else
205 # error platform unsupported
206 #endif
208 g = new Fl_Gl_Choice;
209 g->mode = m;
210 g->alist = alistp;
211 g->next = first;
212 first = g;
214 #if defined(USE_X11)
215 g->vis = visp;
217 if (/*MaxCmapsOfScreen(ScreenOfDisplay(fl_display,fl_screen))==1 && */
218 visp->visualid == fl_visual->visualid &&
219 !fl_getenv("MESA_PRIVATE_CMAP"))
220 g->colormap = fl_colormap;
221 else
222 g->colormap = XCreateColormap(fl_display, RootWindow(fl_display,fl_screen),
223 visp->visual, AllocNone);
224 # elif defined(WIN32)
225 g->pixelformat = pixelformat;
226 g->pfd = chosen_pfd;
227 # elif defined(__APPLE_QUARTZ__)
228 // warning: the Quartz version should probably use Core GL (CGL) instead of AGL
229 g->pixelformat = fmt;
230 # else
231 # error unsupported platform
232 # endif
234 return g;
237 static GLContext *context_list = 0;
238 static int nContext = 0, NContext = 0;
240 static void add_context(GLContext ctx) {
241 if (!ctx) return;
242 if (nContext==NContext) {
243 if (!NContext) NContext = 8;
244 NContext *= 2;
245 context_list = (GLContext*)realloc(
246 context_list, NContext*sizeof(GLContext));
248 context_list[nContext++] = ctx;
251 static void del_context(GLContext ctx) {
252 int i;
253 for (i=0; i<nContext; i++) {
254 if (context_list[i]==ctx) {
255 memmove(context_list+i, context_list+i+1,
256 (nContext-i-1) * sizeof(GLContext));
257 context_list[--nContext] = 0;
258 break;
261 if (!nContext) gl_remove_displaylist_fonts();
264 #if defined(USE_X11)
266 GLContext fl_create_gl_context(XVisualInfo* vis) {
267 GLContext shared_ctx = 0;
268 if (context_list && nContext) shared_ctx = context_list[0];
269 GLContext context = glXCreateContext(fl_display, vis, shared_ctx, 1);
270 if (context)
271 add_context(context);
272 return context;
275 #elif defined(WIN32)
277 GLContext fl_create_gl_context(Fl_Window* window, const Fl_Gl_Choice* g, int layer) {
278 Fl_X* i = Fl_X::i(window);
279 HDC hdc = i->private_dc;
280 if (!hdc) {
281 hdc = i->private_dc = GetDCEx(i->xid, 0, DCX_CACHE);
282 fl_save_dc(i->xid, hdc);
283 SetPixelFormat(hdc, g->pixelformat, (PIXELFORMATDESCRIPTOR*)(&g->pfd));
284 # if USE_COLORMAP
285 if (fl_palette) SelectPalette(hdc, fl_palette, FALSE);
286 # endif
288 GLContext context =
289 layer ? wglCreateLayerContext(hdc, layer) : wglCreateContext(hdc);
290 if (context) {
291 if (context_list && nContext)
292 wglShareLists(context_list[0], context);
293 add_context(context);
295 return context;
298 # elif defined(__APPLE_QUARTZ__)
299 #if !(MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 && __LP64__)
300 static CGrafPtr fl_GetWindowPort(WindowRef window)
302 typedef CGrafPtr (*wf)(WindowRef);
303 static wf f = NULL;
304 if ( ! f) f = (wf)Fl_X::get_carbon_function("GetWindowPort");
305 return (*f)(window);
307 #endif
309 // warning: the Quartz version should probably use Core GL (CGL) instead of AGL
310 GLContext fl_create_gl_context(Fl_Window* window, const Fl_Gl_Choice* g, int layer) {
311 GLContext context, shared_ctx = 0;
312 if (context_list && nContext) shared_ctx = context_list[0];
313 context = aglCreateContext( g->pixelformat, shared_ctx);
314 if (!context) return 0;
315 add_context((GLContext)context);
316 if ( window->parent() ) {
317 int H = window->window()->h();
318 GLint rect[] = { window->x(), H-window->h()-window->y(), window->w(), window->h() };
319 aglSetInteger( (GLContext)context, AGL_BUFFER_RECT, rect );
320 aglEnable( (GLContext)context, AGL_BUFFER_RECT );
322 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
323 #if __LP64__
324 // 64 bit version
325 aglSetWindowRef(context, Fl_X::i(window)->window_ref() );
326 #else
327 // 32 bit version >= 10.5
328 if (aglSetWindowRef != NULL)
329 aglSetWindowRef(context, Fl_X::i(window)->window_ref() );
330 else
331 aglSetDrawable( context, fl_GetWindowPort( Fl_X::i(window)->window_ref() ) );
332 #endif
333 #else
334 // 32 bit version < 10.5
335 aglSetDrawable( context, fl_GetWindowPort( Fl_X::i(window)->window_ref() ) );
336 #endif
337 return (context);
339 # else
340 # error unsupported platform
341 # endif
343 static GLContext cached_context;
344 static Fl_Window* cached_window;
346 void fl_set_gl_context(Fl_Window* w, GLContext context) {
347 if (context != cached_context || w != cached_window) {
348 cached_context = context;
349 cached_window = w;
350 # if defined(USE_X11)
351 glXMakeCurrent(fl_display, fl_xid(w), context);
352 # elif defined(WIN32)
353 wglMakeCurrent(Fl_X::i(w)->private_dc, context);
354 # elif defined(__APPLE_QUARTZ__)
355 // warning: the Quartz version should probably use Core GL (CGL) instead of AGL
356 if ( w->parent() ) { //: resize our GL buffer rectangle
357 int H = w->window()->h();
358 GLint rect[] = { w->x(), H-w->h()-w->y(), w->w(), w->h() };
359 aglSetInteger( context, AGL_BUFFER_RECT, rect );
360 aglEnable( context, AGL_BUFFER_RECT );
362 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
363 #if __LP64__
364 // 64 bit version
365 aglSetWindowRef(context, Fl_X::i(w)->window_ref() );
366 #else
367 // 32 bit version >= 10.5
368 if (aglSetWindowRef != NULL)
369 aglSetWindowRef(context, Fl_X::i(w)->window_ref() );
370 else
371 aglSetDrawable( context, fl_GetWindowPort( Fl_X::i(w)->window_ref() ) );
372 #endif
373 #else
374 // 32 bit version < 10.5
375 aglSetDrawable( context, fl_GetWindowPort( Fl_X::i(w)->window_ref() ) );
376 #endif
377 aglSetCurrentContext(context);
378 # else
379 # error unsupported platform
380 # endif
384 void fl_no_gl_context() {
385 cached_context = 0;
386 cached_window = 0;
387 # if defined(USE_X11)
388 glXMakeCurrent(fl_display, 0, 0);
389 # elif defined(WIN32)
390 wglMakeCurrent(0, 0);
391 # elif defined(__APPLE_QUARTZ__)
392 // warning: the Quartz version should probably use Core GL (CGL) instead of AGL
393 AGLContext ctx = aglGetCurrentContext();
394 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
395 if (aglSetWindowRef != NULL)
396 { if(ctx) aglSetWindowRef(ctx, NULL ); }
397 else
398 #endif
399 if(ctx) aglSetDrawable( ctx, NULL );
400 aglSetCurrentContext(0);
401 # else
402 # error unsupported platform
403 # endif
406 void fl_delete_gl_context(GLContext context) {
407 if (cached_context == context) fl_no_gl_context();
408 # if defined(USE_X11)
409 glXDestroyContext(fl_display, context);
410 # elif defined(WIN32)
411 wglDeleteContext(context);
412 # elif defined(__APPLE_QUARTZ__)
413 // warning: the Quartz version should probably use Core GL (CGL) instead of AGL
414 aglDestroyContext( context );
415 # else
416 # error unsupported platform
417 # endif
418 del_context(context);
421 #endif // HAVE_GL
425 // End of "$Id: Fl_Gl_Choice.cxx 8396 2011-02-07 13:51:17Z manolo $".