4 * This file maintains a database of read-only graphics contexts
5 * for the Tk toolkit, in order to allow GC's to be shared.
7 * Copyright (c) 1990-1994 The Regents of the University of California.
8 * Copyright (c) 1994 Sun Microsystems, Inc.
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
20 * One of the following data structures exists for each GC that is
21 * currently active. The structure is indexed with two hash tables,
22 * one based on the values in the graphics context and the other
23 * based on the display and GC identifier.
27 GC gc; /* Graphics context. */
28 Display *display; /* Display to which gc belongs. */
29 int refCount; /* Number of active uses of gc. */
30 Tcl_HashEntry *valueHashPtr;/* Entry in valueTable (needed when deleting
35 XGCValues values; /* Desired values for GC. */
36 Display *display; /* Display for which GC is valid. */
37 int screenNum; /* screen number of display */
38 int depth; /* and depth for which GC is valid. */
42 * Forward declarations for procedures defined in this file:
45 static void GCInit _ANSI_ARGS_((TkDisplay *dispPtr));
48 *----------------------------------------------------------------------
52 * Given a desired set of values for a graphics context, find
53 * a read-only graphics context with the desired values.
56 * The return value is the X identifer for the desired graphics
57 * context. The caller should never modify this GC, and should
58 * call Tk_FreeGC when the GC is no longer needed.
61 * The GC is added to an internal database with a reference count.
62 * For each call to this procedure, there should eventually be a call
63 * to Tk_FreeGC, so that the database can be cleaned up when GC's
64 * aren't needed anymore.
66 *----------------------------------------------------------------------
70 Tk_GetGC(tkwin, valueMask, valuePtr)
71 Tk_Window tkwin; /* Window in which GC will be used. */
72 register unsigned long valueMask;
73 /* 1 bits correspond to values specified
74 * in *valuesPtr; other values are set
76 register XGCValues *valuePtr;
77 /* Values are specified here for bits set
81 Tcl_HashEntry *valueHashPtr, *idHashPtr;
84 Drawable d, freeDrawable;
85 TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr;
87 if (dispPtr->gcInit <= 0) {
92 * Must zero valueKey at start to clear out pad bytes that may be
93 * part of structure on some systems.
96 memset((VOID *) &valueKey, 0, sizeof(valueKey));
99 * First, check to see if there's already a GC that will work
100 * for this request (exact matches only, sorry).
103 if (valueMask & GCFunction) {
104 valueKey.values.function = valuePtr->function;
106 valueKey.values.function = GXcopy;
108 if (valueMask & GCPlaneMask) {
109 valueKey.values.plane_mask = valuePtr->plane_mask;
111 valueKey.values.plane_mask = (unsigned) ~0;
113 if (valueMask & GCForeground) {
114 valueKey.values.foreground = valuePtr->foreground;
116 valueKey.values.foreground = 0;
118 if (valueMask & GCBackground) {
119 valueKey.values.background = valuePtr->background;
121 valueKey.values.background = 1;
123 if (valueMask & GCLineWidth) {
124 valueKey.values.line_width = valuePtr->line_width;
126 valueKey.values.line_width = 0;
128 if (valueMask & GCLineStyle) {
129 valueKey.values.line_style = valuePtr->line_style;
131 valueKey.values.line_style = LineSolid;
133 if (valueMask & GCCapStyle) {
134 valueKey.values.cap_style = valuePtr->cap_style;
136 valueKey.values.cap_style = CapButt;
138 if (valueMask & GCJoinStyle) {
139 valueKey.values.join_style = valuePtr->join_style;
141 valueKey.values.join_style = JoinMiter;
143 if (valueMask & GCFillStyle) {
144 valueKey.values.fill_style = valuePtr->fill_style;
146 valueKey.values.fill_style = FillSolid;
148 if (valueMask & GCFillRule) {
149 valueKey.values.fill_rule = valuePtr->fill_rule;
151 valueKey.values.fill_rule = EvenOddRule;
153 if (valueMask & GCArcMode) {
154 valueKey.values.arc_mode = valuePtr->arc_mode;
156 valueKey.values.arc_mode = ArcPieSlice;
158 if (valueMask & GCTile) {
159 valueKey.values.tile = valuePtr->tile;
161 valueKey.values.tile = None;
163 if (valueMask & GCStipple) {
164 valueKey.values.stipple = valuePtr->stipple;
166 valueKey.values.stipple = None;
168 if (valueMask & GCTileStipXOrigin) {
169 valueKey.values.ts_x_origin = valuePtr->ts_x_origin;
171 valueKey.values.ts_x_origin = 0;
173 if (valueMask & GCTileStipYOrigin) {
174 valueKey.values.ts_y_origin = valuePtr->ts_y_origin;
176 valueKey.values.ts_y_origin = 0;
178 if (valueMask & GCFont) {
179 valueKey.values.font = valuePtr->font;
181 valueKey.values.font = None;
183 if (valueMask & GCSubwindowMode) {
184 valueKey.values.subwindow_mode = valuePtr->subwindow_mode;
186 valueKey.values.subwindow_mode = ClipByChildren;
188 if (valueMask & GCGraphicsExposures) {
189 valueKey.values.graphics_exposures = valuePtr->graphics_exposures;
191 valueKey.values.graphics_exposures = True;
193 if (valueMask & GCClipXOrigin) {
194 valueKey.values.clip_x_origin = valuePtr->clip_x_origin;
196 valueKey.values.clip_x_origin = 0;
198 if (valueMask & GCClipYOrigin) {
199 valueKey.values.clip_y_origin = valuePtr->clip_y_origin;
201 valueKey.values.clip_y_origin = 0;
203 if (valueMask & GCClipMask) {
204 valueKey.values.clip_mask = valuePtr->clip_mask;
206 valueKey.values.clip_mask = None;
208 if (valueMask & GCDashOffset) {
209 valueKey.values.dash_offset = valuePtr->dash_offset;
211 valueKey.values.dash_offset = 0;
213 if (valueMask & GCDashList) {
214 valueKey.values.dashes = valuePtr->dashes;
216 valueKey.values.dashes = 4;
218 valueKey.display = Tk_Display(tkwin);
219 valueKey.screenNum = Tk_ScreenNumber(tkwin);
220 valueKey.depth = Tk_Depth(tkwin);
221 valueHashPtr = Tcl_CreateHashEntry(&dispPtr->gcValueTable,
222 (char *) &valueKey, &new);
224 gcPtr = (TkGC *) Tcl_GetHashValue(valueHashPtr);
230 * No GC is currently available for this set of values. Allocate a
231 * new GC and add a new structure to the database.
234 gcPtr = (TkGC *) ckalloc(sizeof(TkGC));
237 * Find or make a drawable to use to specify the screen and depth
238 * of the GC. We may have to make a small pixmap, to avoid doing
239 * Tk_MakeWindowExist on the window.
243 if (Tk_WindowId(tkwin) != None) {
244 d = Tk_WindowId(tkwin);
245 } else if (valueKey.depth ==
246 DefaultDepth(valueKey.display, valueKey.screenNum)) {
247 d = RootWindow(valueKey.display, valueKey.screenNum);
249 d = Tk_GetPixmap(valueKey.display,
250 RootWindow(valueKey.display, valueKey.screenNum),
251 1, 1, valueKey.depth);
255 gcPtr->gc = XCreateGC(valueKey.display, d, valueMask, &valueKey.values);
256 gcPtr->display = valueKey.display;
258 gcPtr->valueHashPtr = valueHashPtr;
259 idHashPtr = Tcl_CreateHashEntry(&dispPtr->gcIdTable,
260 (char *) gcPtr->gc, &new);
262 panic("GC already registered in Tk_GetGC");
264 Tcl_SetHashValue(valueHashPtr, gcPtr);
265 Tcl_SetHashValue(idHashPtr, gcPtr);
266 if (freeDrawable != None) {
267 Tk_FreePixmap(valueKey.display, freeDrawable);
274 *----------------------------------------------------------------------
278 * This procedure is called to release a graphics context allocated by
285 * The reference count associated with gc is decremented, and
286 * gc is officially deallocated if no-one is using it anymore.
288 *----------------------------------------------------------------------
292 Tk_FreeGC(display, gc)
293 Display *display; /* Display for which gc was allocated. */
294 GC gc; /* Graphics context to be released. */
296 Tcl_HashEntry *idHashPtr;
297 register TkGC *gcPtr;
298 TkDisplay *dispPtr = TkGetDisplay(display);
300 if (!dispPtr->gcInit) {
301 panic("Tk_FreeGC called before Tk_GetGC");
303 if (dispPtr->gcInit < 0) {
305 * The GCCleanup has been called, and remaining GCs have been
306 * freed. This may still get called by other things shutting
307 * down, but the GCs should no longer be in use.
312 idHashPtr = Tcl_FindHashEntry(&dispPtr->gcIdTable, (char *) gc);
313 if (idHashPtr == NULL) {
314 panic("Tk_FreeGC received unknown gc argument");
316 gcPtr = (TkGC *) Tcl_GetHashValue(idHashPtr);
318 if (gcPtr->refCount == 0) {
319 Tk_FreeXId(gcPtr->display, (XID) XGContextFromGC(gcPtr->gc));
320 XFreeGC(gcPtr->display, gcPtr->gc);
321 Tcl_DeleteHashEntry(gcPtr->valueHashPtr);
322 Tcl_DeleteHashEntry(idHashPtr);
323 ckfree((char *) gcPtr);
328 *----------------------------------------------------------------------
332 * Frees the structures used for GC management.
333 * We need to have it called near the end, when other cleanup that
334 * calls Tk_FreeGC is all done.
340 * GC resources are freed.
342 *----------------------------------------------------------------------
347 TkDisplay *dispPtr; /* display to clean up resources in */
349 Tcl_HashEntry *entryPtr;
350 Tcl_HashSearch search;
353 for (entryPtr = Tcl_FirstHashEntry(&dispPtr->gcIdTable, &search);
355 entryPtr = Tcl_NextHashEntry(&search)) {
356 gcPtr = (TkGC *) Tcl_GetHashValue(entryPtr);
358 * This call is not needed, as it is only used on Unix to restore
359 * the Id to the stack pool, and we don't want to use them anymore.
360 * Tk_FreeXId(gcPtr->display, (XID) XGContextFromGC(gcPtr->gc));
362 XFreeGC(gcPtr->display, gcPtr->gc);
363 Tcl_DeleteHashEntry(gcPtr->valueHashPtr);
364 Tcl_DeleteHashEntry(entryPtr);
365 ckfree((char *) gcPtr);
367 Tcl_DeleteHashTable(&dispPtr->gcValueTable);
368 Tcl_DeleteHashTable(&dispPtr->gcIdTable);
369 dispPtr->gcInit = -1;
373 *----------------------------------------------------------------------
377 * Initialize the structures used for GC management.
385 *----------------------------------------------------------------------
392 if (dispPtr->gcInit < 0) {
393 panic("called GCInit after GCCleanup");
396 Tcl_InitHashTable(&dispPtr->gcValueTable, sizeof(ValueKey)/sizeof(int));
397 Tcl_InitHashTable(&dispPtr->gcIdTable, TCL_ONE_WORD_KEYS);