File: visual.c

package info (click to toggle)
xmhtml 1.1.7-7
  • links: PTS
  • area: main
  • in suites: woody
  • size: 6,104 kB
  • ctags: 8,347
  • sloc: ansic: 68,063; makefile: 496; sh: 161; perl: 36
file content (273 lines) | stat: -rw-r--r-- 7,676 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#ifndef lint
static char rcsId[]="$Header: /usr/local/rcs/Newt/XmHTML/RCS/visual.c,v 1.1 1997/05/28 02:01:34 newt Exp newt $";
#endif
/*****
* visual.c : visual & colormap support code for the XmHTML examples.
*
* This file Version	$Revision: 1.1 $
*
* Creation date:		Thu May  1 00:17:12 GMT+0100 1997
* Last modification: 	$Date: 1997/05/28 02:01:34 $
* By:					$Author: newt $
* Current State:		$State: Exp $
*
* Author:				John L. Cwikla
*						X Programmer
*						Wolfram Research Inc.
*						cwikla@wri.com
*
* Copyright (C) 1996 by John C. Cwikla
* Copyright (C) 1997 by Ripley Software Development 
* All Rights Reserved
*
* This file is part of the XmHTML Widget Library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU [Library] General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU [Library] General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*****/
/*****
* ChangeLog 
* $Log: visual.c,v $
* Revision 1.1  1997/05/28 02:01:34  newt
* Initial Revision
*
*****/ 
#include <stdio.h>
#include <stdlib.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>

/*** External Function Prototype Declarations ***/

/*** Public Variable Declarations ***/

/*** Private Datatype Declarations ****/
#define XmNvisualID				"visualID"
#define XmNapplicationDepth		"applicationDepth"
#define XmNvisualClass			"visualClass"
#define XmNusePrivateColormap	"usePrivateColormap"

#define XmCVisualID				"VisualID"
#define XmCApplicationDepth		"ApplicationDepth"
#define XmCVisualClass			"VisualClass"
#define XmCUsePrivateColormap	"UsePrivateColormap"

/*** Private Function Prototype Declarations ****/

/*** Private Variable Declarations ***/
typedef struct _OurResourceStruct
{
	int visualID;
	int applicationDepth;
	int visualClass;
	Boolean usePrivateColormap;
}OurResourceStruct, *OurResourcePtr;

OurResourceStruct ourResources;

#define UNDEFINED_DEFAULT -1
#define TheOffset(a) XtOffset(OurResourcePtr, a)

static XtResource AppResources[] =
{
	{
		XmNvisualID,
		XmCVisualID, XtRInt, sizeof(int),
		TheOffset(visualID),
		XtRImmediate, (XtPointer)UNDEFINED_DEFAULT
	},
	{
		XmNapplicationDepth,
		XmCApplicationDepth, XtRInt, sizeof(int),
		TheOffset(applicationDepth),
		XtRImmediate, (XtPointer)UNDEFINED_DEFAULT
	},
	{
		XmNvisualClass,
		XmCVisualClass, XtRInt, sizeof(int),
		TheOffset(visualClass),
		XtRImmediate, (XtPointer)UNDEFINED_DEFAULT
	},
	{
		XmNusePrivateColormap,
		XmCUsePrivateColormap, XtRBoolean, sizeof(Boolean),
		TheOffset(usePrivateColormap),
		XtRImmediate, (XtPointer)FALSE
	},
};

/*****
* Name:			getStartupVisual
* Return Type:	int
* Description:	pick visual and colormap that are in balance.
* In:
*	shell:		widget for which to pick visual and colormap;
*	visual:		visual picked, updated upon return;
*	colormap:	created colormap (default or privated), updated upon return;
*	depth:		required depth.
* Returns:
*	True when args have been updated, false if not.
* Note:
*	This routine comes from an article of John Cwikla that appeared in the
*	X Advisor Journal, ``Beyond the Default Visual'', dated june 1995. Read it
*	to get the full story and why the resources. You can find this article at:
*	http://www.unx.com/DD/advisor/docs/jun95/jun95.cwikla1.shtml
*****/
int
getStartupVisual(Widget shell, Visual **visual, int *depth,
	Colormap *colormap)
{
	Display *display;
	int success = FALSE, screen;
	int theVisualClass;
	XVisualInfo theVisualInfo;
	static Visual *theVisual;
	static Colormap theColormap;
	static int theApplicationDepth;

	display = XtDisplay(shell);

	XtGetApplicationResources(shell, &ourResources, AppResources,
		XtNumber(AppResources), NULL, 0);

	/* if no resources have been set, just return */
	if(ourResources.visualID == UNDEFINED_DEFAULT &&
		ourResources.applicationDepth == UNDEFINED_DEFAULT &&
		ourResources.visualClass == UNDEFINED_DEFAULT &&
		ourResources.usePrivateColormap == FALSE)
	{
		return(FALSE);
	}

	if(ourResources.visualID != UNDEFINED_DEFAULT)
	{
		XVisualInfo vtemp, *vinfos;
		int vitems;
		vtemp.visualid = ourResources.visualID;
		vinfos = XGetVisualInfo(display, VisualIDMask, &vtemp, &vitems);

		if(vinfos != NULL)
		{
			/* Better only be one match! */
			theVisual = vinfos[0].visual;
			theApplicationDepth = vinfos[0].depth;
			theVisualClass = vinfos[0].class;

			XFree(vinfos);
			success = TRUE;
		}
	}
	screen = DefaultScreen(display);

	if(!success)
	{
		/* Step 2 */
		if((ourResources.applicationDepth == UNDEFINED_DEFAULT) &&
			(ourResources.visualClass == UNDEFINED_DEFAULT))
		{
			theVisual = DefaultVisual(display, screen);
			theApplicationDepth = DefaultDepth(display, screen);
			theVisualClass = theVisual->class;
		}
		else
		{
			/* Step 3 */
			if(ourResources.applicationDepth == UNDEFINED_DEFAULT)
				theApplicationDepth = DefaultDepth(display, screen);
			else
				theApplicationDepth = ourResources.applicationDepth;
			if(ourResources.visualClass == UNDEFINED_DEFAULT)
				theVisualClass = DefaultVisual(display, screen)->class;
			else
				theVisualClass = ourResources.visualClass;

			if(XMatchVisualInfo(display, screen, theApplicationDepth, 
				theVisualClass, &theVisualInfo) != 0)
			{
				theVisual = theVisualInfo.visual;
				theApplicationDepth = theVisualInfo.depth;
				theVisualClass = theVisualInfo.class;
			}
			else
			{
				/* Step 4 */
				XVisualInfo visTemplate;
				XVisualInfo *visReturn;
				int numVis, n;

				visReturn = (XVisualInfo *)NULL;
				n = 0;

				/* See if we can find a visual at the depth they ask for. */
				if(ourResources.applicationDepth != UNDEFINED_DEFAULT)
				{
					visTemplate.depth = ourResources.applicationDepth;
					visReturn = XGetVisualInfo(display, VisualDepthMask, 
						&visTemplate, &numVis);
					/*
					* If numVis > 1 you may want to have it pick your favorite
					* visual. This is not necessary since the user still has
					* finer control by setting XtNvisualClass or XtNvisualID.
					*/
					/* Step 5 */
					if(visReturn == (XVisualInfo *)NULL)
					{
						visTemplate.class = theVisualClass;
						visReturn = XGetVisualInfo(display, VisualClassMask, 
							&visTemplate, &numVis);
						if(visReturn != (XVisualInfo *)NULL)
						{
							int i, d = 0;
							for(i = 1; i < numVis; i++)
							{
								if(d < visReturn[i].depth)
								{
									d = visReturn[i].depth;
									n = i;
								}
							}
						}
					}
					/* Step 6 */
					if(visReturn == (XVisualInfo *)NULL)
					{
						theVisual = DefaultVisual(display, screen);
						theApplicationDepth = DefaultDepth(display, screen);
						theVisualClass = theVisual->class;
					}
					else
					{
						theVisual = visReturn[n].visual;
						theApplicationDepth = visReturn[n].depth;
						theVisualClass = visReturn[n].class;
					}
				}
			}
		}
	}
	if((theVisual->visualid == DefaultVisual(display, screen)->visualid) &&
		!ourResources.usePrivateColormap)
		theColormap = DefaultColormap(display, screen);
	else
		theColormap = XCreateColormap(display, RootWindow(display, screen),
			theVisual, AllocNone);

	/* get everything, set return values */
	*visual   = theVisual;
	*depth    = theApplicationDepth;
	*colormap = theColormap;

	return(True);
}