home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / DisplayFactory.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.9 KB  |  277 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* 
  19.    DisplayFactory.cpp -- class definition for the factory class
  20.    Created: Spence Murray <spence@netscape.com>, 17-Sep-96.
  21.  */
  22.  
  23.  
  24.  
  25. #include "DisplayFactory.h"
  26. #include "xpassert.h"
  27.  
  28. #ifdef DEBUG_toshok
  29. #define D(x) x
  30. #else
  31. #define D(x)
  32. #endif
  33.  
  34. #define PRIVATE_COLORMAP_REALLY_MEANS_PRIVATE_COLORMAP
  35.  
  36. static XFE_DisplayFactory *factoryRunning = NULL;
  37.  
  38. // Internationalization stuff
  39. #include "xpgetstr.h"
  40. extern int XFE_DISPLAY_FACTORY_INSTALL_COLORMAP_ERROR;
  41.  
  42.  
  43. fe_colormap *
  44. XFE_DisplayFactory::getSharedColormap()
  45. {
  46.   // make sure the visual stuff has been dealt with.
  47.   getVisual();
  48.  
  49.   if (m_sharedCmap == NULL)
  50.     {
  51.       if (m_alwaysInstallCmap)
  52.     {
  53.       Colormap cmap = XCreateColormap (m_display, RootWindowOfScreen (m_screen),
  54.                        m_visual, AllocNone);
  55.       m_sharedCmap = fe_NewColormap(m_screen, m_visual, cmap, True);
  56.     }
  57.       else
  58.     {
  59.       Colormap cmap = DefaultColormapOfScreen(m_screen);
  60.       
  61.       m_sharedCmap = fe_NewColormap(m_screen, DefaultVisualOfScreen(m_screen), cmap, False);
  62.     }
  63.     }
  64.  
  65.   return m_sharedCmap;
  66. }
  67.  
  68. fe_colormap *
  69. XFE_DisplayFactory::getPrivateColormap()
  70. {
  71. #ifdef PRIVATE_COLORMAP_REALLY_MEANS_PRIVATE_COLORMAP
  72.  
  73.   if (m_alwaysInstallCmap)
  74.     {
  75.       Colormap cmap;
  76.  
  77.       // make sure the visual stuff has been dealt with.
  78.       getVisual();
  79.  
  80.       cmap = XCreateColormap (m_display, RootWindowOfScreen (m_screen),
  81.                   m_visual, AllocNone);
  82.  
  83.       return fe_NewColormap(m_screen, m_visual, cmap, True);
  84.     }
  85.   else
  86. #endif
  87.     return getSharedColormap();
  88. }
  89.  
  90. Visual *
  91. XFE_DisplayFactory::getVisual()
  92. {
  93.   if (m_visual == NULL)
  94.   {
  95.     m_visual = fe_globalData.default_visual;
  96.  
  97.     if (m_visual == NULL)
  98.       {
  99.         Visual *v;
  100.         String str = 0;
  101.   
  102.         /* "*visualID" is special for a number of reasons... */
  103.         static XtResource getVisual_res = { "visualID", "VisualID",
  104.                   XtRString, sizeof (String),
  105.                   0, XtRString, "default" };
  106.   
  107.         XtGetSubresources (m_toplevel, &str, (char *)fe_progname, "TopLevelShell",
  108.                &getVisual_res, 1, 0, 0);
  109.         v = fe_ParseVisual (m_screen, str);
  110.   
  111.         m_visual =
  112.       fe_globalData.default_visual = v;
  113.       }
  114.   
  115.     /* Don't allow colormap flashing on a deep display */
  116.     if (m_visual != DefaultVisualOfScreen (m_screen))
  117.       m_alwaysInstallCmap = True;
  118.  
  119.     if (!fe_globalData.default_colormap)
  120.       {
  121.     Colormap cmap = DefaultColormapOfScreen (m_screen);
  122.     fe_globalData.default_colormap =
  123.           fe_NewColormap(m_screen,  DefaultVisualOfScreen(m_screen), cmap, False);
  124.       }
  125.   }
  126.  
  127.   return m_visual;
  128. }
  129.  
  130. int
  131. XFE_DisplayFactory::getVisualDepth()
  132. {
  133.   // make sure the visual has been initialized
  134.   getVisual();
  135.  
  136.   if (m_visualDepth == 0)
  137.     {
  138.       XVisualInfo vi_in, *vi_out;
  139.       int out_count;
  140.       vi_in.visualid = XVisualIDFromVisual (m_visual);
  141.       vi_out = XGetVisualInfo (m_display, VisualIDMask,
  142.                    &vi_in, &out_count);
  143.       if (! vi_out) abort ();
  144.  
  145.       m_visualDepth = vi_out [0].depth;
  146.  
  147.       XFree ((char *) vi_out);
  148.     }
  149.  
  150.  
  151.   return m_visualDepth;
  152. }
  153.  
  154. int
  155. XFE_DisplayFactory::getVisualPixmapDepth()
  156. {
  157.   int pixmap_depth;
  158.   int i, pfvc = 0;
  159.   XPixmapFormatValues *pfv;
  160.  
  161.   // make sure the visual stuff has been initialized
  162.   getVisual();
  163.   getVisualDepth();
  164.   
  165.   pixmap_depth = m_visualDepth;
  166.  
  167.   pfv = XListPixmapFormats(m_display, &pfvc);
  168.   /* Return the first matching depth in the pixmap formats.
  169.      If there are no matching pixmap formats (which shouldn't
  170.      be able to happen) return the visual depth instead. */
  171.   for (i = 0; i < pfvc; i++)
  172.     if (pfv[i].depth == m_visualDepth)
  173.       {
  174.     pixmap_depth = pfv[i].bits_per_pixel;
  175.     break;
  176.       }
  177.  
  178.   if (pfv)
  179.     XFree(pfv);
  180.  
  181.   return pixmap_depth;
  182. }
  183.  
  184. XFE_DisplayFactory::XFE_DisplayFactory(Widget toplevel)
  185. {
  186.   m_sharedCmap    = NULL;
  187.   m_visual = NULL;
  188.   m_visualDepth   = 0;
  189.  
  190.   m_toplevel = toplevel;
  191.  
  192.   m_display = XtDisplay(m_toplevel);
  193.   m_screen = XtScreen(m_toplevel);
  194.  
  195.   {
  196.     String str = 0;
  197.     static XtResource res = { "installColormap", XtCString, XtRString,
  198.                   sizeof (String), 0, XtRString, "guess" };
  199.     XtGetApplicationResources (m_toplevel, &str, &res, 1, 0, 0);
  200.     if (!str || !*str || !XP_STRCASECMP (str, "guess"))
  201. #if 0
  202.       /* If the server is capable of installing multiple hardware colormaps
  203.      simultaniously, take one for ourself by default.  Otherwise, don't. */
  204.       m_alwaysInstallCmap = (MaxCmapsOfScreen (m_screen) > 1);
  205. #else
  206.     {
  207.       /* But everybody lies about this value. */
  208.       char *vendor = XServerVendor (m_display);
  209.       m_alwaysInstallCmap =
  210.     !strcmp (vendor, "Silicon Graphics");
  211.     }
  212. #endif
  213.     else if (!XP_STRCASECMP (str, "yes") || !XP_STRCASECMP (str, "true"))
  214.       m_alwaysInstallCmap = True;
  215.     else if (!XP_STRCASECMP (str, "no") || !XP_STRCASECMP (str, "false"))
  216.       m_alwaysInstallCmap = False;
  217.     else
  218.       {
  219.  
  220. //    XP_GetString ( XFE_DISPLAY_FACTORY_INSTALL_COLORMAP_ERROR )
  221.           
  222. //           fprintf (stderr,
  223. //                    "%s: installColormap: %s must be yes, no, or guess.\n",
  224. //                    fe_progname, str);
  225.  
  226.            fprintf(stderr,
  227.                   XP_GetString(XFE_DISPLAY_FACTORY_INSTALL_COLORMAP_ERROR),
  228.                   fe_progname,
  229.                   str);
  230.           
  231.           m_alwaysInstallCmap = False;
  232.       }
  233.   }
  234. }
  235.  
  236. XFE_DisplayFactory::~XFE_DisplayFactory()
  237. {
  238. D(    printf ("in XFE_DisplayFactory::~XFE_DisplayFactory()\n");)
  239.  
  240. D(    printf ("leaving XFE_DisplayFactory::~XFE_DisplayFactory()\n");)
  241. }
  242.  
  243. XFE_DisplayFactory *
  244. XFE_DisplayFactory::theFactory()
  245. {
  246.   XP_ASSERT(factoryRunning);
  247.  
  248.   return factoryRunning;
  249. }
  250.  
  251. void
  252. XFE_DisplayFactory::colormapGoingAway(fe_colormap *colormap)
  253. {
  254.   if (colormap == m_sharedCmap)
  255.     m_sharedCmap = NULL;
  256.   /* we don't free it here, as it's freed inside fe_DisposeColormap */
  257. }
  258.  
  259. void
  260. fe_startDisplayFactory(Widget toplevel)
  261. {
  262.   if (factoryRunning == NULL)
  263.     factoryRunning = new XFE_DisplayFactory(toplevel);
  264. }
  265.  
  266. void
  267. fe_DisplayFactoryColormapGoingAway(fe_colormap *colormap)
  268. {
  269.   XFE_DisplayFactory::theFactory()->colormapGoingAway(colormap);
  270. }
  271.  
  272. fe_colormap * 
  273. fe_GetSharedColormap()
  274. {
  275.     return XFE_DisplayFactory::theFactory()->getSharedColormap();
  276. }
  277.