Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libtk/generic/tkCanvUtil.c
1810 views
1
/*
2
* tkCanvUtil.c --
3
*
4
* This procedure contains a collection of utility procedures
5
* used by the implementations of various canvas item types.
6
*
7
* Copyright (c) 1994 Sun Microsystems, Inc.
8
* Copyright (c) 1994 Sun Microsystems, Inc.
9
*
10
* See the file "license.terms" for information on usage and redistribution
11
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12
*
13
* SCCS: @(#) tkCanvUtil.c 1.6 96/02/15 18:53:10
14
*/
15
16
#include "tkInt.h"
17
#include "tkCanvas.h"
18
19
20
/*
21
*----------------------------------------------------------------------
22
*
23
* Tk_CanvasTkwin --
24
*
25
* Given a token for a canvas, this procedure returns the
26
* widget that represents the canvas.
27
*
28
* Results:
29
* The return value is a handle for the widget.
30
*
31
* Side effects:
32
* None.
33
*
34
*----------------------------------------------------------------------
35
*/
36
37
Tk_Window
38
Tk_CanvasTkwin(canvas)
39
Tk_Canvas canvas; /* Token for the canvas. */
40
{
41
TkCanvas *canvasPtr = (TkCanvas *) canvas;
42
return canvasPtr->tkwin;
43
}
44
45
/*
46
*----------------------------------------------------------------------
47
*
48
* Tk_CanvasDrawableCoords --
49
*
50
* Given an (x,y) coordinate pair within a canvas, this procedure
51
* returns the corresponding coordinates at which the point should
52
* be drawn in the drawable used for display.
53
*
54
* Results:
55
* There is no return value. The values at *drawableXPtr and
56
* *drawableYPtr are filled in with the coordinates at which
57
* x and y should be drawn. These coordinates are clipped
58
* to fit within a "short", since this is what X uses in
59
* most cases for drawing.
60
*
61
* Side effects:
62
* None.
63
*
64
*----------------------------------------------------------------------
65
*/
66
67
void
68
Tk_CanvasDrawableCoords(canvas, x, y, drawableXPtr, drawableYPtr)
69
Tk_Canvas canvas; /* Token for the canvas. */
70
double x, y; /* Coordinates in canvas space. */
71
short *drawableXPtr, *drawableYPtr; /* Screen coordinates are stored
72
* here. */
73
{
74
TkCanvas *canvasPtr = (TkCanvas *) canvas;
75
double tmp;
76
77
tmp = x - canvasPtr->drawableXOrigin;
78
if (tmp > 0) {
79
tmp += 0.5;
80
} else {
81
tmp -= 0.5;
82
}
83
if (tmp > 32767) {
84
*drawableXPtr = 32767;
85
} else if (tmp < -32768) {
86
*drawableXPtr = -32768;
87
} else {
88
*drawableXPtr = tmp;
89
}
90
91
tmp = y - canvasPtr->drawableYOrigin;
92
if (tmp > 0) {
93
tmp += 0.5;
94
} else {
95
tmp -= 0.5;
96
}
97
if (tmp > 32767) {
98
*drawableYPtr = 32767;
99
} else if (tmp < -32768) {
100
*drawableYPtr = -32768;
101
} else {
102
*drawableYPtr = tmp;
103
}
104
}
105
106
/*
107
*----------------------------------------------------------------------
108
*
109
* Tk_CanvasWindowCoords --
110
*
111
* Given an (x,y) coordinate pair within a canvas, this procedure
112
* returns the corresponding coordinates in the canvas's window.
113
*
114
* Results:
115
* There is no return value. The values at *screenXPtr and
116
* *screenYPtr are filled in with the coordinates at which
117
* (x,y) appears in the canvas's window. These coordinates
118
* are clipped to fit within a "short", since this is what X
119
* uses in most cases for drawing.
120
*
121
* Side effects:
122
* None.
123
*
124
*----------------------------------------------------------------------
125
*/
126
127
void
128
Tk_CanvasWindowCoords(canvas, x, y, screenXPtr, screenYPtr)
129
Tk_Canvas canvas; /* Token for the canvas. */
130
double x, y; /* Coordinates in canvas space. */
131
short *screenXPtr, *screenYPtr; /* Screen coordinates are stored
132
* here. */
133
{
134
TkCanvas *canvasPtr = (TkCanvas *) canvas;
135
double tmp;
136
137
tmp = x - canvasPtr->xOrigin;
138
if (tmp > 0) {
139
tmp += 0.5;
140
} else {
141
tmp -= 0.5;
142
}
143
if (tmp > 32767) {
144
*screenXPtr = 32767;
145
} else if (tmp < -32768) {
146
*screenXPtr = -32768;
147
} else {
148
*screenXPtr = tmp;
149
}
150
151
tmp = y - canvasPtr->yOrigin;
152
if (tmp > 0) {
153
tmp += 0.5;
154
} else {
155
tmp -= 0.5;
156
}
157
if (tmp > 32767) {
158
*screenYPtr = 32767;
159
} else if (tmp < -32768) {
160
*screenYPtr = -32768;
161
} else {
162
*screenYPtr = tmp;
163
}
164
}
165
166
/*
167
*--------------------------------------------------------------
168
*
169
* Tk_CanvasGetCoord --
170
*
171
* Given a string, returns a floating-point canvas coordinate
172
* corresponding to that string.
173
*
174
* Results:
175
* The return value is a standard Tcl return result. If
176
* TCL_OK is returned, then everything went well and the
177
* canvas coordinate is stored at *doublePtr; otherwise
178
* TCL_ERROR is returned and an error message is left in
179
* interp->result.
180
*
181
* Side effects:
182
* None.
183
*
184
*--------------------------------------------------------------
185
*/
186
187
int
188
Tk_CanvasGetCoord(interp, canvas, string, doublePtr)
189
Tcl_Interp *interp; /* Interpreter for error reporting. */
190
Tk_Canvas canvas; /* Canvas to which coordinate applies. */
191
char *string; /* Describes coordinate (any screen
192
* coordinate form may be used here). */
193
double *doublePtr; /* Place to store converted coordinate. */
194
{
195
TkCanvas *canvasPtr = (TkCanvas *) canvas;
196
if (Tk_GetScreenMM(canvasPtr->interp, canvasPtr->tkwin, string,
197
doublePtr) != TCL_OK) {
198
return TCL_ERROR;
199
}
200
*doublePtr *= canvasPtr->pixelsPerMM;
201
return TCL_OK;
202
}
203
204
/*
205
*----------------------------------------------------------------------
206
*
207
* Tk_CanvasSetStippleOrigin --
208
*
209
* This procedure sets the stipple origin in a graphics context
210
* so that stipples drawn with the GC will line up with other
211
* stipples previously drawn in the canvas.
212
*
213
* Results:
214
* None.
215
*
216
* Side effects:
217
* The graphics context is modified.
218
*
219
*----------------------------------------------------------------------
220
*/
221
222
void
223
Tk_CanvasSetStippleOrigin(canvas, gc)
224
Tk_Canvas canvas; /* Token for a canvas. */
225
GC gc; /* Graphics context that is about to be
226
* used to draw a stippled pattern as
227
* part of redisplaying the canvas. */
228
229
{
230
TkCanvas *canvasPtr = (TkCanvas *) canvas;
231
232
XSetTSOrigin(canvasPtr->display, gc, -canvasPtr->drawableXOrigin,
233
-canvasPtr->drawableYOrigin);
234
}
235
236
/*
237
*----------------------------------------------------------------------
238
*
239
* Tk_CanvasGetTextInfo --
240
*
241
* This procedure returns a pointer to a structure containing
242
* information about the selection and insertion cursor for
243
* a canvas widget. Items such as text items save the pointer
244
* and use it to share access to the information with the generic
245
* canvas code.
246
*
247
* Results:
248
* The return value is a pointer to the structure holding text
249
* information for the canvas. Most of the fields should not
250
* be modified outside the generic canvas code; see the user
251
* documentation for details.
252
*
253
* Side effects:
254
* None.
255
*
256
*----------------------------------------------------------------------
257
*/
258
259
Tk_CanvasTextInfo *
260
Tk_CanvasGetTextInfo(canvas)
261
Tk_Canvas canvas; /* Token for the canvas widget. */
262
{
263
return &((TkCanvas *) canvas)->textInfo;
264
}
265
266
/*
267
*--------------------------------------------------------------
268
*
269
* Tk_CanvasTagsParseProc --
270
*
271
* This procedure is invoked during option processing to handle
272
* "-tags" options for canvas items.
273
*
274
* Results:
275
* A standard Tcl return value.
276
*
277
* Side effects:
278
* The tags for a given item get replaced by those indicated
279
* in the value argument.
280
*
281
*--------------------------------------------------------------
282
*/
283
284
int
285
Tk_CanvasTagsParseProc(clientData, interp, tkwin, value, widgRec, offset)
286
ClientData clientData; /* Not used.*/
287
Tcl_Interp *interp; /* Used for reporting errors. */
288
Tk_Window tkwin; /* Window containing canvas widget. */
289
char *value; /* Value of option (list of tag
290
* names). */
291
char *widgRec; /* Pointer to record for item. */
292
int offset; /* Offset into item (ignored). */
293
{
294
register Tk_Item *itemPtr = (Tk_Item *) widgRec;
295
int argc, i;
296
char **argv;
297
Tk_Uid *newPtr;
298
299
/*
300
* Break the value up into the individual tag names.
301
*/
302
303
if (Tcl_SplitList(interp, value, &argc, &argv) != TCL_OK) {
304
return TCL_ERROR;
305
}
306
307
/*
308
* Make sure that there's enough space in the item to hold the
309
* tag names.
310
*/
311
312
if (itemPtr->tagSpace < argc) {
313
newPtr = (Tk_Uid *) ckalloc((unsigned) (argc * sizeof(Tk_Uid)));
314
for (i = itemPtr->numTags-1; i >= 0; i--) {
315
newPtr[i] = itemPtr->tagPtr[i];
316
}
317
if (itemPtr->tagPtr != itemPtr->staticTagSpace) {
318
ckfree((char *) itemPtr->tagPtr);
319
}
320
itemPtr->tagPtr = newPtr;
321
itemPtr->tagSpace = argc;
322
}
323
itemPtr->numTags = argc;
324
for (i = 0; i < argc; i++) {
325
itemPtr->tagPtr[i] = Tk_GetUid(argv[i]);
326
}
327
ckfree((char *) argv);
328
return TCL_OK;
329
}
330
331
/*
332
*--------------------------------------------------------------
333
*
334
* Tk_CanvasTagsPrintProc --
335
*
336
* This procedure is invoked by the Tk configuration code
337
* to produce a printable string for the "-tags" configuration
338
* option for canvas items.
339
*
340
* Results:
341
* The return value is a string describing all the tags for
342
* the item referred to by "widgRec". In addition, *freeProcPtr
343
* is filled in with the address of a procedure to call to free
344
* the result string when it's no longer needed (or NULL to
345
* indicate that the string doesn't need to be freed).
346
*
347
* Side effects:
348
* None.
349
*
350
*--------------------------------------------------------------
351
*/
352
353
char *
354
Tk_CanvasTagsPrintProc(clientData, tkwin, widgRec, offset, freeProcPtr)
355
ClientData clientData; /* Ignored. */
356
Tk_Window tkwin; /* Window containing canvas widget. */
357
char *widgRec; /* Pointer to record for item. */
358
int offset; /* Ignored. */
359
Tcl_FreeProc **freeProcPtr; /* Pointer to variable to fill in with
360
* information about how to reclaim
361
* storage for return string. */
362
{
363
register Tk_Item *itemPtr = (Tk_Item *) widgRec;
364
365
if (itemPtr->numTags == 0) {
366
*freeProcPtr = (Tcl_FreeProc *) NULL;
367
return "";
368
}
369
if (itemPtr->numTags == 1) {
370
*freeProcPtr = (Tcl_FreeProc *) NULL;
371
return (char *) itemPtr->tagPtr[0];
372
}
373
*freeProcPtr = TCL_DYNAMIC;
374
return Tcl_Merge(itemPtr->numTags, (char **) itemPtr->tagPtr);
375
}
376
377