blob: 98c3c3a92aa025aae5683ef2eb3255a155730372 [file] [log] [blame]
DRC2ff39b82011-07-28 08:38:59 +00001//
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.
12//
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.
17//
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.
22//
23// Please report all bugs and problems on the following page:
24//
25// http://www.fltk.org/str.php
26//
27
28// Set the default visual according to passed switches:
29
30#include <config.h>
31#include <FL/Fl.H>
32#include <FL/x.H>
33
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.
40
41 <P>Only the following combinations do anything useful:
42
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>
63
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).
67*/
68#ifdef WIN32
69int 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;
76}
77#elif defined(__APPLE__)
78
79// \todo Mac : need to implement Visual flags
80int Fl::visual(int flags) {
81 (void)flags;
82 return 1;
83}
84
85#else
86
87#if USE_XDBE
88#include <X11/extensions/Xdbe.h>
89#endif
90
91static int test_visual(XVisualInfo& v, int flags) {
92 if (v.screen != fl_screen) return 0;
93#if USE_COLORMAP
94 if (!(flags & FL_INDEX)) {
95 if (v.c_class != StaticColor && v.c_class != TrueColor) return 0;
96 if (v.depth <= 8) return 0; // fltk will work better in colormap mode
97 }
98 if (flags & FL_RGB8) {
99 if (v.depth < 24) return 0;
100 }
101 // for now, fltk does not like colormaps of more than 8 bits:
102 if ((v.c_class&1) && v.depth > 8) return 0;
103#else
104 // simpler if we can't use colormapped visuals at all:
105 if (v.c_class != StaticColor && v.c_class != TrueColor) return 0;
106#endif
107#if USE_XDBE
108 if (flags & FL_DOUBLE) {
109 static XdbeScreenVisualInfo *xdbejunk;
110 if (!xdbejunk) {
111 int event_base, error_base;
112 if (!XdbeQueryExtension(fl_display, &event_base, &error_base)) return 0;
113 Drawable root = RootWindow(fl_display,fl_screen);
114 int numscreens = 1;
115 xdbejunk = XdbeGetVisualInfo(fl_display,&root,&numscreens);
116 if (!xdbejunk) return 0;
117 }
118 for (int j = 0; ; j++) {
119 if (j >= xdbejunk->count) return 0;
120 if (xdbejunk->visinfo[j].visual == v.visualid) break;
121 }
122 }
123#endif
124 return 1;
125}
126
127int Fl::visual(int flags) {
128#if USE_XDBE == 0
129 if (flags & FL_DOUBLE) return 0;
130#endif
131 fl_open_display();
132 // always use default if possible:
133 if (test_visual(*fl_visual, flags)) return 1;
134 // get all the visuals:
135 XVisualInfo vTemplate;
136 int num;
137 XVisualInfo *visualList = XGetVisualInfo(fl_display, 0, &vTemplate, &num);
138 // find all matches, use the one with greatest depth:
139 XVisualInfo *found = 0;
140 for (int i=0; i<num; i++) if (test_visual(visualList[i], flags)) {
141 if (!found || found->depth < visualList[i].depth)
142 found = &visualList[i];
143 }
144 if (!found) {XFree((void*)visualList); return 0;}
145 fl_visual = found;
146 fl_colormap = XCreateColormap(fl_display, RootWindow(fl_display,fl_screen),
147 fl_visual->visual, AllocNone);
148 return 1;
149}
150
151#endif
152
153//
154// End of "$Id: Fl_visual.cxx 7903 2010-11-28 21:06:39Z matt $".
155//