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.
13
* RCS: @(#) $Id: tkGC.c,v 1.2 1998/09/14 18:23:11 stanton Exp $
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
* Hash table to map from a GC's values to a TkGC structure describing
36
* a GC with those values (used by Tk_GetGC).
39
static Tcl_HashTable valueTable;
41
XGCValues values; /* Desired values for GC. */
42
Display *display; /* Display for which GC is valid. */
43
int screenNum; /* screen number of display */
44
int depth; /* and depth for which GC is valid. */
48
* Hash table for <display + GC> -> TkGC mapping. This table is used by
52
static Tcl_HashTable idTable;
54
Display *display; /* Display for which GC was allocated. */
55
GC gc; /* X's identifier for GC. */
58
static int initialized = 0; /* 0 means static structures haven't been
62
* Forward declarations for procedures defined in this file:
65
static void GCInit _ANSI_ARGS_((void));
68
*----------------------------------------------------------------------
72
* Given a desired set of values for a graphics context, find
73
* a read-only graphics context with the desired values.
76
* The return value is the X identifer for the desired graphics
77
* context. The caller should never modify this GC, and should
78
* call Tk_FreeGC when the GC is no longer needed.
81
* The GC is added to an internal database with a reference count.
82
* For each call to this procedure, there should eventually be a call
83
* to Tk_FreeGC, so that the database can be cleaned up when GC's
84
* aren't needed anymore.
86
*----------------------------------------------------------------------
90
Tk_GetGC(tkwin, valueMask, valuePtr)
91
Tk_Window tkwin; /* Window in which GC will be used. */
92
register unsigned long valueMask;
93
/* 1 bits correspond to values specified
94
* in *valuesPtr; other values are set
96
register XGCValues *valuePtr;
97
/* Values are specified here for bits set
102
Tcl_HashEntry *valueHashPtr, *idHashPtr;
103
register TkGC *gcPtr;
105
Drawable d, freeDrawable;
112
* Must zero valueKey at start to clear out pad bytes that may be
113
* part of structure on some systems.
116
memset((VOID *) &valueKey, 0, sizeof(valueKey));
119
* First, check to see if there's already a GC that will work
120
* for this request (exact matches only, sorry).
123
if (valueMask & GCFunction) {
124
valueKey.values.function = valuePtr->function;
126
valueKey.values.function = GXcopy;
128
if (valueMask & GCPlaneMask) {
129
valueKey.values.plane_mask = valuePtr->plane_mask;
131
valueKey.values.plane_mask = (unsigned) ~0;
133
if (valueMask & GCForeground) {
134
valueKey.values.foreground = valuePtr->foreground;
136
valueKey.values.foreground = 0;
138
if (valueMask & GCBackground) {
139
valueKey.values.background = valuePtr->background;
141
valueKey.values.background = 1;
143
if (valueMask & GCLineWidth) {
144
valueKey.values.line_width = valuePtr->line_width;
146
valueKey.values.line_width = 0;
148
if (valueMask & GCLineStyle) {
149
valueKey.values.line_style = valuePtr->line_style;
151
valueKey.values.line_style = LineSolid;
153
if (valueMask & GCCapStyle) {
154
valueKey.values.cap_style = valuePtr->cap_style;
156
valueKey.values.cap_style = CapButt;
158
if (valueMask & GCJoinStyle) {
159
valueKey.values.join_style = valuePtr->join_style;
161
valueKey.values.join_style = JoinMiter;
163
if (valueMask & GCFillStyle) {
164
valueKey.values.fill_style = valuePtr->fill_style;
166
valueKey.values.fill_style = FillSolid;
168
if (valueMask & GCFillRule) {
169
valueKey.values.fill_rule = valuePtr->fill_rule;
171
valueKey.values.fill_rule = EvenOddRule;
173
if (valueMask & GCArcMode) {
174
valueKey.values.arc_mode = valuePtr->arc_mode;
176
valueKey.values.arc_mode = ArcPieSlice;
178
if (valueMask & GCTile) {
179
valueKey.values.tile = valuePtr->tile;
181
valueKey.values.tile = None;
183
if (valueMask & GCStipple) {
184
valueKey.values.stipple = valuePtr->stipple;
186
valueKey.values.stipple = None;
188
if (valueMask & GCTileStipXOrigin) {
189
valueKey.values.ts_x_origin = valuePtr->ts_x_origin;
191
valueKey.values.ts_x_origin = 0;
193
if (valueMask & GCTileStipYOrigin) {
194
valueKey.values.ts_y_origin = valuePtr->ts_y_origin;
196
valueKey.values.ts_y_origin = 0;
198
if (valueMask & GCFont) {
199
valueKey.values.font = valuePtr->font;
201
valueKey.values.font = None;
203
if (valueMask & GCSubwindowMode) {
204
valueKey.values.subwindow_mode = valuePtr->subwindow_mode;
206
valueKey.values.subwindow_mode = ClipByChildren;
208
if (valueMask & GCGraphicsExposures) {
209
valueKey.values.graphics_exposures = valuePtr->graphics_exposures;
211
valueKey.values.graphics_exposures = True;
213
if (valueMask & GCClipXOrigin) {
214
valueKey.values.clip_x_origin = valuePtr->clip_x_origin;
216
valueKey.values.clip_x_origin = 0;
218
if (valueMask & GCClipYOrigin) {
219
valueKey.values.clip_y_origin = valuePtr->clip_y_origin;
221
valueKey.values.clip_y_origin = 0;
223
if (valueMask & GCClipMask) {
224
valueKey.values.clip_mask = valuePtr->clip_mask;
226
valueKey.values.clip_mask = None;
228
if (valueMask & GCDashOffset) {
229
valueKey.values.dash_offset = valuePtr->dash_offset;
231
valueKey.values.dash_offset = 0;
233
if (valueMask & GCDashList) {
234
valueKey.values.dashes = valuePtr->dashes;
236
valueKey.values.dashes = 4;
238
valueKey.display = Tk_Display(tkwin);
239
valueKey.screenNum = Tk_ScreenNumber(tkwin);
240
valueKey.depth = Tk_Depth(tkwin);
241
valueHashPtr = Tcl_CreateHashEntry(&valueTable, (char *) &valueKey, &new);
243
gcPtr = (TkGC *) Tcl_GetHashValue(valueHashPtr);
249
* No GC is currently available for this set of values. Allocate a
250
* new GC and add a new structure to the database.
253
gcPtr = (TkGC *) ckalloc(sizeof(TkGC));
256
* Find or make a drawable to use to specify the screen and depth
257
* of the GC. We may have to make a small pixmap, to avoid doing
258
* Tk_MakeWindowExist on the window.
262
if (Tk_WindowId(tkwin) != None) {
263
d = Tk_WindowId(tkwin);
264
} else if (valueKey.depth ==
265
DefaultDepth(valueKey.display, valueKey.screenNum)) {
266
d = RootWindow(valueKey.display, valueKey.screenNum);
268
d = Tk_GetPixmap(valueKey.display,
269
RootWindow(valueKey.display, valueKey.screenNum),
270
1, 1, valueKey.depth);
274
gcPtr->gc = XCreateGC(valueKey.display, d, valueMask, &valueKey.values);
275
gcPtr->display = valueKey.display;
277
gcPtr->valueHashPtr = valueHashPtr;
278
idKey.display = valueKey.display;
279
idKey.gc = gcPtr->gc;
280
idHashPtr = Tcl_CreateHashEntry(&idTable, (char *) &idKey, &new);
282
panic("GC already registered in Tk_GetGC");
284
Tcl_SetHashValue(valueHashPtr, gcPtr);
285
Tcl_SetHashValue(idHashPtr, gcPtr);
286
if (freeDrawable != None) {
287
Tk_FreePixmap(valueKey.display, freeDrawable);
294
*----------------------------------------------------------------------
298
* This procedure is called to release a graphics context allocated by
305
* The reference count associated with gc is decremented, and
306
* gc is officially deallocated if no-one is using it anymore.
308
*----------------------------------------------------------------------
312
Tk_FreeGC(display, gc)
313
Display *display; /* Display for which gc was allocated. */
314
GC gc; /* Graphics context to be released. */
317
Tcl_HashEntry *idHashPtr;
318
register TkGC *gcPtr;
321
panic("Tk_FreeGC called before Tk_GetGC");
324
idKey.display = display;
326
idHashPtr = Tcl_FindHashEntry(&idTable, (char *) &idKey);
327
if (idHashPtr == NULL) {
328
panic("Tk_FreeGC received unknown gc argument");
330
gcPtr = (TkGC *) Tcl_GetHashValue(idHashPtr);
332
if (gcPtr->refCount == 0) {
333
Tk_FreeXId(gcPtr->display, (XID) XGContextFromGC(gcPtr->gc));
334
XFreeGC(gcPtr->display, gcPtr->gc);
335
Tcl_DeleteHashEntry(gcPtr->valueHashPtr);
336
Tcl_DeleteHashEntry(idHashPtr);
337
ckfree((char *) gcPtr);
342
*----------------------------------------------------------------------
346
* Initialize the structures used for GC management.
354
*----------------------------------------------------------------------
361
Tcl_InitHashTable(&valueTable, sizeof(ValueKey)/sizeof(int));
362
Tcl_InitHashTable(&idTable, sizeof(IdKey)/sizeof(int));