/*
* tkVisual.c --
*
* This file contains library procedures for allocating and
* freeing visuals and colormaps. This code is based on a
* prototype implementation by Paul Mackerras.
*
* Copyright (c) 1994 The Regents of the University of California.
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tkVisual.c 1.18 96/02/15 18:53:08
*/
#include "tkInt.h"
/*
* The table below maps from symbolic names for visual classes
* to the associated X class symbols.
*/
typedef struct VisualDictionary {
* specified for an unambiguous match. */
{"best", 1, 0},
{NULL, 0, 0},
};
/*
* One of the following structures exists for each distinct non-default
* colormap allocated for a display by Tk_GetColormap.
*/
struct TkColormap {
* allocated. */
* outstanding (calls to Tk_GetColormap
* minus calls to Tk_FreeColormap). */
* a call to Tk_GetColormap with "new",
* implying that the window wants it all
* for itself. 1 means that the colormap
* was allocated as a default for a particular
* visual, so it can be shared. */
* or NULL for end of list. */
};
/*
*----------------------------------------------------------------------
*
* Tk_GetVisual --
*
* Given a string identifying a particular kind of visual, this
* procedure returns a visual and depth that matches the specification.
*
* Results:
* The return value is normally a pointer to a visual. If an
* error occurred in looking up the visual, NULL is returned and
* an error message is left in interp->result. The depth of the
* visual is returned to *depthPtr under normal returns. If
* colormapPtr is non-NULL, then this procedure also finds a
* suitable colormap for use with the visual in tkwin, and it
* returns that colormap in *colormapPtr unless an error occurs.
*
* Side effects:
* A new colormap may be allocated.
*
*----------------------------------------------------------------------
*/
Visual *
* reporting. */
* used. */
char *string; /* String describing visual. See
* manual entry for details. */
int *depthPtr; /* The depth of the returned visual
* is stored here. */
* colormap for visual is placed here.
* This colormap must eventually be
* freed by calling Tk_FreeColormap. */
{
long mask;
char *p;
/*
* Parse string and set up a template for use in searching for
* an appropriate visual.
*/
c = string[0];
if (c == '.') {
/*
* The string must be a window name. If the window is on the
* same screen as tkwin, then just use its visual. Otherwise
* use the information about the visual as a template for the
* search.
*/
return NULL;
}
if (colormapPtr != NULL) {
/*
* Use the colormap from the other window too (but be sure
* to increment its reference count if it's one of the ones
* allocated here).
*/
break;
}
}
}
return visual;
}
/*
* Use the default visual for the window's screen.
*/
if (colormapPtr != NULL) {
}
int visualId;
/*
* This is a visual ID.
*/
return NULL;
}
mask = VisualIDMask;
} else {
/*
* Parse the string into a class name (or "best") optionally
* followed by whitespace and a depth.
*/
for (p = string; *p != 0; p++) {
break;
}
}
break;
}
}
}
return NULL;
}
p++;
}
if (*p == 0) {
} else {
return NULL;
}
}
if (c == 'b') {
mask = 0;
} else {
}
}
/*
* Find all visuals that match the template we've just created,
* and return an error if there are none that match.
*/
mask |= VisualScreenMask;
&numVisuals);
if (visInfoList == NULL) {
return NULL;
}
/*
* Search through the visuals that were returned to find the best
* one. The choice is based on the following criteria, in decreasing
* order of importance:
*
* 1. Depth: choose a visual with exactly the desired depth,
* else one with more bits than requested but as few bits
* as possible, else one with fewer bits but as many as
* possible.
* 2. Class: some visual classes are more desirable than others;
* pick the visual with the most desirable class.
* 3. Default: the default visual for the screen gets preference
* over other visuals, all else being equal.
*/
bestPrio = 0;
for (i = 0; i < numVisuals; i++) {
switch (visInfoList[i].class) {
default: prio = 0; break;
}
if (visInfoList[i].visual
prio++;
}
goto newBest;
}
goto newBest;
}
goto newBest;
}
} else {
goto newBest;
}
}
continue;
bestPtr = &visInfoList[i];
}
XFree((char *) visInfoList);
/*
* If we need to find a colormap for this visual, do it now.
* If the visual is the default visual for the screen, then
* use the default colormap. Otherwise search for an existing
* colormap that's shareable. If all else fails, create a new
* colormap.
*/
if (colormapPtr != NULL) {
} else {
goto done;
}
}
}
}
done:
return visual;
}
/*
*----------------------------------------------------------------------
*
* Tk_GetColormap --
*
* Given a string identifying a colormap, this procedure finds
* an appropriate colormap.
*
* Results:
* The return value is normally the X resource identifier for the
* colormap. If an error occurs, None is returned and an error
* message is placed in interp->result.
*
* Side effects:
* A reference count is incremented for the colormap, so
* Tk_FreeColormap must eventually be called exactly once for
* each call to Tk_GetColormap.
*
*----------------------------------------------------------------------
*/
* reporting. */
* used. */
char *string; /* String that identifies colormap:
* either "new" or the name of
* another window. */
{
/*
* Allocate a new colormap, if that's what is wanted.
*/
}
/*
* Use a colormap from an existing window. It must have the same
* visual as tkwin (which means, among other things, that the
* other window must be on the same screen).
*/
return None;
}
": not on same screen", (char *) NULL);
return None;
}
": incompatible visuals", (char *) NULL);
return None;
}
/*
* If the colormap was a special one allocated by code in this file,
* increment its reference count.
*/
}
}
return colormap;
}
/*
*----------------------------------------------------------------------
*
* Tk_FreeColormap --
*
* This procedure is called to release a colormap that was
* previously allocated by Tk_GetColormap.
*
* Results:
* None.
*
* Side effects:
* The colormap's reference count is decremented. If this was the
* last reference to the colormap, then the colormap is freed.
*
*----------------------------------------------------------------------
*/
void
* allocated. */
* Must have been returned by previous
* call to Tk_GetColormap, or
* preserved by a previous call to
* Tk_PreserveColormap. */
{
/*
* Find Tk's information about the display, then see if this
* colormap is a non-default one (if it's a default one, there
* won't be an entry for it in the display's list).
*/
panic("unknown display passed to Tk_FreeColormap");
}
} else {
}
}
return;
}
}
}
/*
*----------------------------------------------------------------------
*
* Tk_PreserveColormap --
*
* This procedure is called to indicate to Tk that the specified
* colormap is being referenced from another location and should
* not be freed until all extra references are eliminated. The
* colormap must have been returned by Tk_GetColormap.
*
* Results:
* None.
*
* Side effects:
* The colormap's reference count is incremented, so
* Tk_FreeColormap must eventually be called exactly once for
* each call to Tk_PreserveColormap.
*
*----------------------------------------------------------------------
*/
void
* allocated. */
* preserved. */
{
/*
* Find Tk's information about the display, then see if this
* colormap is a non-default one (if it's a default one, there
* won't be an entry for it in the display's list).
*/
panic("unknown display passed to Tk_PreserveColormap");
}
return;
}
}
}