themes: Workaround for bug where a background color of RGB 0,0,0 in Black color schem...
[ntk.git] / src / Fl_visual.cxx
blob34c3c222ec84e56aa43ba9c44e4c7f6f0e7a3c86
1 //
2 // "$Id: Fl_visual.cxx 7903 2010-11-28 21:06:39Z matt $"
3 //
4 // Visual support 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 // Set the default visual according to passed switches:
30 #include <config.h>
31 #include <FL/Fl.H>
32 #include <FL/x.H>
34 /** \fn Fl::visual(int flags)
35 Selects a visual so that your graphics are drawn correctly. This is
36 only allowed before you call show() on any windows. This does nothing
37 if the default visual satisfies the capabilities, or if no visual
38 satisfies the capabilities, or on systems that don't have such
39 brain-dead notions.
41 <P>Only the following combinations do anything useful:
43 <UL>
44 <LI>Fl::visual(FL_RGB)
45 <BR>Full/true color (if there are several depths FLTK chooses the
46 largest). Do this if you use fl_draw_image
47 for much better (non-dithered) output.
48 <BR>&nbsp; </LI>
49 <LI>Fl::visual(FL_RGB8)
50 <BR>Full color with at least 24 bits of color. FL_RGB will
51 always pick this if available, but if not it will happily return a
52 less-than-24 bit deep visual. This call fails if 24 bits are not
53 available.
54 <BR>&nbsp; </LI>
55 <LI>Fl::visual(FL_DOUBLE|FL_INDEX)
56 <BR>Hardware double buffering. Call this if you are going to use
57 Fl_Double_Window.
58 <BR>&nbsp; </LI>
59 <LI>Fl::visual(FL_DOUBLE|FL_RGB)</LI>
60 <LI>Fl::visual(FL_DOUBLE|FL_RGB8)
61 <BR>Hardware double buffering and full color.
62 </UL>
64 <P>This returns true if the system has the capabilities by default or
65 FLTK suceeded in turing them on. Your program will still work even if
66 this returns false (it just won't look as good).
68 #ifdef WIN32
69 int Fl::visual(int flags) {
70 fl_GetDC(0);
71 if (flags & FL_DOUBLE) return 0;
72 if (!(flags & FL_INDEX) &&
73 GetDeviceCaps(fl_gc,BITSPIXEL) <= 8) return 0;
74 if ((flags & FL_RGB8) && GetDeviceCaps(fl_gc,BITSPIXEL)<24) return 0;
75 return 1;
77 #elif defined(__APPLE__)
79 // \todo Mac : need to implement Visual flags
80 int Fl::visual(int flags) {
81 (void)flags;
82 return 1;
85 #else
87 static int test_visual(XVisualInfo& v, int flags) {
88 if (v.screen != fl_screen) return 0;
89 #if USE_COLORMAP
90 if (!(flags & FL_INDEX)) {
91 if (v.c_class != StaticColor && v.c_class != TrueColor) return 0;
92 if (v.depth <= 8) return 0; // fltk will work better in colormap mode
94 if (flags & FL_RGB8) {
95 if (v.depth < 24) return 0;
97 // for now, fltk does not like colormaps of more than 8 bits:
98 if ((v.c_class&1) && v.depth > 8) return 0;
99 #else
100 // simpler if we can't use colormapped visuals at all:
101 if (v.c_class != StaticColor && v.c_class != TrueColor) return 0;
102 #endif
103 return 1;
106 int Fl::visual(int flags) {
107 fl_open_display();
108 // always use default if possible:
109 if (test_visual(*fl_visual, flags)) return 1;
110 // get all the visuals:
111 XVisualInfo vTemplate;
112 int num;
113 XVisualInfo *visualList = XGetVisualInfo(fl_display, 0, &vTemplate, &num);
114 // find all matches, use the one with greatest depth:
115 XVisualInfo *found = 0;
116 for (int i=0; i<num; i++) if (test_visual(visualList[i], flags)) {
117 if (!found || found->depth < visualList[i].depth)
118 found = &visualList[i];
120 if (!found) {XFree((void*)visualList); return 0;}
121 fl_visual = found;
122 fl_colormap = XCreateColormap(fl_display, RootWindow(fl_display,fl_screen),
123 fl_visual->visual, AllocNone);
124 return 1;
127 #endif
130 // End of "$Id: Fl_visual.cxx 7903 2010-11-28 21:06:39Z matt $".