Fl_visual.cxx 4.6 KB

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