Fl_visual.cxx 4.9 KB

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