1 //
2 // "$Id: Fl_visual.cxx 5190 2006-06-09 16:16:34Z mike $"
3 //
4 // Visual support for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
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 #ifdef WIN32
visual(int flags)35 int Fl::visual(int flags) {
36 fl_GetDC(0);
37 if (flags & FL_DOUBLE) return 0;
38 if (!(flags & FL_INDEX) &&
39 GetDeviceCaps(fl_gc,BITSPIXEL) <= 8) return 0;
40 if ((flags & FL_RGB8) && GetDeviceCaps(fl_gc,BITSPIXEL)<24) return 0;
41 return 1;
42 }
43 #elif defined(__APPLE__)
44
45 // \todo Mac : need to implement Visual flags
visual(int flags)46 int Fl::visual(int flags) {
47 (void)flags;
48 return 1;
49 }
50
51 #else
52
53 #if USE_XDBE
54 #include <X11/extensions/Xdbe.h>
55 #endif
56
test_visual(XVisualInfo & v,int flags)57 static int test_visual(XVisualInfo& v, int flags) {
58 if (v.screen != fl_screen) return 0;
59 #if USE_COLORMAP
60 if (!(flags & FL_INDEX)) {
61 if (v.c_class != StaticColor && v.c_class != TrueColor) return 0;
62 if (v.depth <= 8) return 0; // fltk will work better in colormap mode
63 }
64 if (flags & FL_RGB8) {
65 if (v.depth < 24) return 0;
66 }
67 // for now, fltk does not like colormaps of more than 8 bits:
68 if ((v.c_class&1) && v.depth > 8) return 0;
69 #else
70 // simpler if we can't use colormapped visuals at all:
71 if (v.c_class != StaticColor && v.c_class != TrueColor) return 0;
72 #endif
73 #if USE_XDBE
74 if (flags & FL_DOUBLE) {
75 static XdbeScreenVisualInfo *xdbejunk;
76 if (!xdbejunk) {
77 int event_base, error_base;
78 if (!XdbeQueryExtension(fl_display, &event_base, &error_base)) return 0;
79 Drawable root = RootWindow(fl_display,fl_screen);
80 int numscreens = 1;
81 xdbejunk = XdbeGetVisualInfo(fl_display,&root,&numscreens);
82 if (!xdbejunk) return 0;
83 }
84 for (int j = 0; ; j++) {
85 if (j >= xdbejunk->count) return 0;
86 if (xdbejunk->visinfo[j].visual == v.visualid) break;
87 }
88 }
89 #endif
90 return 1;
91 }
92
visual(int flags)93 int Fl::visual(int flags) {
94 #if USE_XDBE == 0
95 if (flags & FL_DOUBLE) return 0;
96 #endif
97 fl_open_display();
98 // always use default if possible:
99 if (test_visual(*fl_visual, flags)) return 1;
100 // get all the visuals:
101 XVisualInfo vTemplate;
102 int num;
103 XVisualInfo *visualList = XGetVisualInfo(fl_display, 0, &vTemplate, &num);
104 // find all matches, use the one with greatest depth:
105 XVisualInfo *found = 0;
106 for (int i=0; i<num; i++) if (test_visual(visualList[i], flags)) {
107 if (!found || found->depth < visualList[i].depth)
108 found = &visualList[i];
109 }
110 if (!found) {XFree((void*)visualList); return 0;}
111 fl_visual = found;
112 fl_colormap = XCreateColormap(fl_display, RootWindow(fl_display,fl_screen),
113 fl_visual->visual, AllocNone);
114 return 1;
115 }
116
117 #endif
118
119 //
120 // End of "$Id: Fl_visual.cxx 5190 2006-06-09 16:16:34Z mike $".
121 //
122