You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.2KB

  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. static int test_visual(XVisualInfo& v, int flags) {
  79. if (v.screen != fl_screen) return 0;
  80. #if USE_COLORMAP
  81. if (!(flags & FL_INDEX)) {
  82. if (v.c_class != StaticColor && v.c_class != TrueColor) return 0;
  83. if (v.depth <= 8) return 0; // fltk will work better in colormap mode
  84. }
  85. if (flags & FL_RGB8) {
  86. if (v.depth < 24) return 0;
  87. }
  88. // for now, fltk does not like colormaps of more than 8 bits:
  89. if ((v.c_class&1) && v.depth > 8) return 0;
  90. #else
  91. // simpler if we can't use colormapped visuals at all:
  92. if (v.c_class != StaticColor && v.c_class != TrueColor) return 0;
  93. #endif
  94. return 1;
  95. }
  96. int Fl::visual(int flags) {
  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. #endif
  117. //
  118. // End of "$Id: Fl_visual.cxx 7903 2010-11-28 21:06:39Z matt $".
  119. //