Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libtk/generic/tkCanvImg.c
1810 views
1
/*
2
* tkCanvImg.c --
3
*
4
* This file implements image items for canvas widgets.
5
*
6
* Copyright (c) 1994 The Regents of the University of California.
7
* Copyright (c) 1994-1996 Sun Microsystems, Inc.
8
*
9
* See the file "license.terms" for information on usage and redistribution
10
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11
*
12
* SCCS: @(#) tkCanvImg.c 1.17 96/02/17 17:18:43
13
*/
14
15
#include "tkInt.h"
16
#include "tkCanvas.h"
17
18
/*
19
* The structure below defines the record for each image item.
20
*/
21
22
typedef struct ImageItem {
23
Tk_Item header; /* Generic stuff that's the same for all
24
* types. MUST BE FIRST IN STRUCTURE. */
25
Tk_Canvas canvas; /* Canvas containing the image. */
26
double x, y; /* Coordinates of positioning point for
27
* image. */
28
Tk_Anchor anchor; /* Where to anchor image relative to
29
* (x,y). */
30
char *imageString; /* String describing -image option (malloc-ed).
31
* NULL means no image right now. */
32
Tk_Image image; /* Image to display in window, or NULL if
33
* no image at present. */
34
} ImageItem;
35
36
/*
37
* Information used for parsing configuration specs:
38
*/
39
40
static Tk_CustomOption tagsOption = {Tk_CanvasTagsParseProc,
41
Tk_CanvasTagsPrintProc, (ClientData) NULL
42
};
43
44
static Tk_ConfigSpec configSpecs[] = {
45
{TK_CONFIG_ANCHOR, "-anchor", (char *) NULL, (char *) NULL,
46
"center", Tk_Offset(ImageItem, anchor), TK_CONFIG_DONT_SET_DEFAULT},
47
{TK_CONFIG_STRING, "-image", (char *) NULL, (char *) NULL,
48
(char *) NULL, Tk_Offset(ImageItem, imageString), TK_CONFIG_NULL_OK},
49
{TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,
50
(char *) NULL, 0, TK_CONFIG_NULL_OK, &tagsOption},
51
{TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
52
(char *) NULL, 0, 0}
53
};
54
55
/*
56
* Prototypes for procedures defined in this file:
57
*/
58
59
static void ImageChangedProc _ANSI_ARGS_((ClientData clientData,
60
int x, int y, int width, int height, int imgWidth,
61
int imgHeight));
62
static int ImageCoords _ANSI_ARGS_((Tcl_Interp *interp,
63
Tk_Canvas canvas, Tk_Item *itemPtr, int argc,
64
char **argv));
65
static int ImageToArea _ANSI_ARGS_((Tk_Canvas canvas,
66
Tk_Item *itemPtr, double *rectPtr));
67
static double ImageToPoint _ANSI_ARGS_((Tk_Canvas canvas,
68
Tk_Item *itemPtr, double *coordPtr));
69
static void ComputeImageBbox _ANSI_ARGS_((Tk_Canvas canvas,
70
ImageItem *imgPtr));
71
static int ConfigureImage _ANSI_ARGS_((Tcl_Interp *interp,
72
Tk_Canvas canvas, Tk_Item *itemPtr, int argc,
73
char **argv, int flags));
74
static int CreateImage _ANSI_ARGS_((Tcl_Interp *interp,
75
Tk_Canvas canvas, struct Tk_Item *itemPtr,
76
int argc, char **argv));
77
static void DeleteImage _ANSI_ARGS_((Tk_Canvas canvas,
78
Tk_Item *itemPtr, Display *display));
79
static void DisplayImage _ANSI_ARGS_((Tk_Canvas canvas,
80
Tk_Item *itemPtr, Display *display, Drawable dst,
81
int x, int y, int width, int height));
82
static void ScaleImage _ANSI_ARGS_((Tk_Canvas canvas,
83
Tk_Item *itemPtr, double originX, double originY,
84
double scaleX, double scaleY));
85
static void TranslateImage _ANSI_ARGS_((Tk_Canvas canvas,
86
Tk_Item *itemPtr, double deltaX, double deltaY));
87
88
/*
89
* The structures below defines the image item type in terms of
90
* procedures that can be invoked by generic item code.
91
*/
92
93
Tk_ItemType tkImageType = {
94
"image", /* name */
95
sizeof(ImageItem), /* itemSize */
96
CreateImage, /* createProc */
97
configSpecs, /* configSpecs */
98
ConfigureImage, /* configureProc */
99
ImageCoords, /* coordProc */
100
DeleteImage, /* deleteProc */
101
DisplayImage, /* displayProc */
102
0, /* alwaysRedraw */
103
ImageToPoint, /* pointProc */
104
ImageToArea, /* areaProc */
105
(Tk_ItemPostscriptProc *) NULL, /* postscriptProc */
106
ScaleImage, /* scaleProc */
107
TranslateImage, /* translateProc */
108
(Tk_ItemIndexProc *) NULL, /* indexProc */
109
(Tk_ItemCursorProc *) NULL, /* icursorProc */
110
(Tk_ItemSelectionProc *) NULL, /* selectionProc */
111
(Tk_ItemInsertProc *) NULL, /* insertProc */
112
(Tk_ItemDCharsProc *) NULL, /* dTextProc */
113
(Tk_ItemType *) NULL /* nextPtr */
114
};
115
116
/*
117
*--------------------------------------------------------------
118
*
119
* CreateImage --
120
*
121
* This procedure is invoked to create a new image
122
* item in a canvas.
123
*
124
* Results:
125
* A standard Tcl return value. If an error occurred in
126
* creating the item, then an error message is left in
127
* interp->result; in this case itemPtr is left uninitialized,
128
* so it can be safely freed by the caller.
129
*
130
* Side effects:
131
* A new image item is created.
132
*
133
*--------------------------------------------------------------
134
*/
135
136
static int
137
CreateImage(interp, canvas, itemPtr, argc, argv)
138
Tcl_Interp *interp; /* Interpreter for error reporting. */
139
Tk_Canvas canvas; /* Canvas to hold new item. */
140
Tk_Item *itemPtr; /* Record to hold new item; header
141
* has been initialized by caller. */
142
int argc; /* Number of arguments in argv. */
143
char **argv; /* Arguments describing rectangle. */
144
{
145
ImageItem *imgPtr = (ImageItem *) itemPtr;
146
147
if (argc < 2) {
148
Tcl_AppendResult(interp, "wrong # args: should be \"",
149
Tk_PathName(Tk_CanvasTkwin(canvas)), " create ",
150
itemPtr->typePtr->name, " x y ?options?\"",
151
(char *) NULL);
152
return TCL_ERROR;
153
}
154
155
/*
156
* Initialize item's record.
157
*/
158
159
imgPtr->canvas = canvas;
160
imgPtr->anchor = TK_ANCHOR_CENTER;
161
imgPtr->imageString = NULL;
162
imgPtr->image = NULL;
163
164
/*
165
* Process the arguments to fill in the item record.
166
*/
167
168
if ((Tk_CanvasGetCoord(interp, canvas, argv[0], &imgPtr->x) != TCL_OK)
169
|| (Tk_CanvasGetCoord(interp, canvas, argv[1], &imgPtr->y)
170
!= TCL_OK)) {
171
return TCL_ERROR;
172
}
173
174
if (ConfigureImage(interp, canvas, itemPtr, argc-2, argv+2, 0) != TCL_OK) {
175
DeleteImage(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
176
return TCL_ERROR;
177
}
178
return TCL_OK;
179
}
180
181
/*
182
*--------------------------------------------------------------
183
*
184
* ImageCoords --
185
*
186
* This procedure is invoked to process the "coords" widget
187
* command on image items. See the user documentation for
188
* details on what it does.
189
*
190
* Results:
191
* Returns TCL_OK or TCL_ERROR, and sets interp->result.
192
*
193
* Side effects:
194
* The coordinates for the given item may be changed.
195
*
196
*--------------------------------------------------------------
197
*/
198
199
static int
200
ImageCoords(interp, canvas, itemPtr, argc, argv)
201
Tcl_Interp *interp; /* Used for error reporting. */
202
Tk_Canvas canvas; /* Canvas containing item. */
203
Tk_Item *itemPtr; /* Item whose coordinates are to be
204
* read or modified. */
205
int argc; /* Number of coordinates supplied in
206
* argv. */
207
char **argv; /* Array of coordinates: x1, y1,
208
* x2, y2, ... */
209
{
210
ImageItem *imgPtr = (ImageItem *) itemPtr;
211
char x[TCL_DOUBLE_SPACE], y[TCL_DOUBLE_SPACE];
212
213
if (argc == 0) {
214
Tcl_PrintDouble(interp, imgPtr->x, x);
215
Tcl_PrintDouble(interp, imgPtr->y, y);
216
Tcl_AppendResult(interp, x, " ", y, (char *) NULL);
217
} else if (argc == 2) {
218
if ((Tk_CanvasGetCoord(interp, canvas, argv[0], &imgPtr->x) != TCL_OK)
219
|| (Tk_CanvasGetCoord(interp, canvas, argv[1],
220
&imgPtr->y) != TCL_OK)) {
221
return TCL_ERROR;
222
}
223
ComputeImageBbox(canvas, imgPtr);
224
} else {
225
sprintf(interp->result,
226
"wrong # coordinates: expected 0 or 2, got %d", argc);
227
return TCL_ERROR;
228
}
229
return TCL_OK;
230
}
231
232
/*
233
*--------------------------------------------------------------
234
*
235
* ConfigureImage --
236
*
237
* This procedure is invoked to configure various aspects
238
* of an image item, such as its anchor position.
239
*
240
* Results:
241
* A standard Tcl result code. If an error occurs, then
242
* an error message is left in interp->result.
243
*
244
* Side effects:
245
* Configuration information may be set for itemPtr.
246
*
247
*--------------------------------------------------------------
248
*/
249
250
static int
251
ConfigureImage(interp, canvas, itemPtr, argc, argv, flags)
252
Tcl_Interp *interp; /* Used for error reporting. */
253
Tk_Canvas canvas; /* Canvas containing itemPtr. */
254
Tk_Item *itemPtr; /* Image item to reconfigure. */
255
int argc; /* Number of elements in argv. */
256
char **argv; /* Arguments describing things to configure. */
257
int flags; /* Flags to pass to Tk_ConfigureWidget. */
258
{
259
ImageItem *imgPtr = (ImageItem *) itemPtr;
260
Tk_Window tkwin;
261
Tk_Image image;
262
263
tkwin = Tk_CanvasTkwin(canvas);
264
if (Tk_ConfigureWidget(interp, tkwin, configSpecs, argc,
265
argv, (char *) imgPtr, flags) != TCL_OK) {
266
return TCL_ERROR;
267
}
268
269
/*
270
* Create the image. Save the old image around and don't free it
271
* until after the new one is allocated. This keeps the reference
272
* count from going to zero so the image doesn't have to be recreated
273
* if it hasn't changed.
274
*/
275
276
if (imgPtr->imageString != NULL) {
277
image = Tk_GetImage(interp, tkwin, imgPtr->imageString,
278
ImageChangedProc, (ClientData) imgPtr);
279
if (image == NULL) {
280
return TCL_ERROR;
281
}
282
} else {
283
image = NULL;
284
}
285
if (imgPtr->image != NULL) {
286
Tk_FreeImage(imgPtr->image);
287
}
288
imgPtr->image = image;
289
ComputeImageBbox(canvas, imgPtr);
290
return TCL_OK;
291
}
292
293
/*
294
*--------------------------------------------------------------
295
*
296
* DeleteImage --
297
*
298
* This procedure is called to clean up the data structure
299
* associated with a image item.
300
*
301
* Results:
302
* None.
303
*
304
* Side effects:
305
* Resources associated with itemPtr are released.
306
*
307
*--------------------------------------------------------------
308
*/
309
310
static void
311
DeleteImage(canvas, itemPtr, display)
312
Tk_Canvas canvas; /* Info about overall canvas widget. */
313
Tk_Item *itemPtr; /* Item that is being deleted. */
314
Display *display; /* Display containing window for
315
* canvas. */
316
{
317
ImageItem *imgPtr = (ImageItem *) itemPtr;
318
319
if (imgPtr->imageString != NULL) {
320
ckfree(imgPtr->imageString);
321
}
322
if (imgPtr->image != NULL) {
323
Tk_FreeImage(imgPtr->image);
324
}
325
}
326
327
/*
328
*--------------------------------------------------------------
329
*
330
* ComputeImageBbox --
331
*
332
* This procedure is invoked to compute the bounding box of
333
* all the pixels that may be drawn as part of a image item.
334
* This procedure is where the child image's placement is
335
* computed.
336
*
337
* Results:
338
* None.
339
*
340
* Side effects:
341
* The fields x1, y1, x2, and y2 are updated in the header
342
* for itemPtr.
343
*
344
*--------------------------------------------------------------
345
*/
346
347
/* ARGSUSED */
348
static void
349
ComputeImageBbox(canvas, imgPtr)
350
Tk_Canvas canvas; /* Canvas that contains item. */
351
ImageItem *imgPtr; /* Item whose bbox is to be
352
* recomputed. */
353
{
354
int width, height;
355
int x, y;
356
357
x = imgPtr->x + ((imgPtr->x >= 0) ? 0.5 : - 0.5);
358
y = imgPtr->y + ((imgPtr->y >= 0) ? 0.5 : - 0.5);
359
360
if (imgPtr->image == None) {
361
imgPtr->header.x1 = imgPtr->header.x2 = x;
362
imgPtr->header.y1 = imgPtr->header.y2 = y;
363
return;
364
}
365
366
/*
367
* Compute location and size of image, using anchor information.
368
*/
369
370
Tk_SizeOfImage(imgPtr->image, &width, &height);
371
switch (imgPtr->anchor) {
372
case TK_ANCHOR_N:
373
x -= width/2;
374
break;
375
case TK_ANCHOR_NE:
376
x -= width;
377
break;
378
case TK_ANCHOR_E:
379
x -= width;
380
y -= height/2;
381
break;
382
case TK_ANCHOR_SE:
383
x -= width;
384
y -= height;
385
break;
386
case TK_ANCHOR_S:
387
x -= width/2;
388
y -= height;
389
break;
390
case TK_ANCHOR_SW:
391
y -= height;
392
break;
393
case TK_ANCHOR_W:
394
y -= height/2;
395
break;
396
case TK_ANCHOR_NW:
397
break;
398
case TK_ANCHOR_CENTER:
399
x -= width/2;
400
y -= height/2;
401
break;
402
}
403
404
/*
405
* Store the information in the item header.
406
*/
407
408
imgPtr->header.x1 = x;
409
imgPtr->header.y1 = y;
410
imgPtr->header.x2 = x + width;
411
imgPtr->header.y2 = y + height;
412
}
413
414
/*
415
*--------------------------------------------------------------
416
*
417
* DisplayImage --
418
*
419
* This procedure is invoked to draw a image item in a given
420
* drawable.
421
*
422
* Results:
423
* None.
424
*
425
* Side effects:
426
* ItemPtr is drawn in drawable using the transformation
427
* information in canvas.
428
*
429
*--------------------------------------------------------------
430
*/
431
432
static void
433
DisplayImage(canvas, itemPtr, display, drawable, x, y, width, height)
434
Tk_Canvas canvas; /* Canvas that contains item. */
435
Tk_Item *itemPtr; /* Item to be displayed. */
436
Display *display; /* Display on which to draw item. */
437
Drawable drawable; /* Pixmap or window in which to draw
438
* item. */
439
int x, y, width, height; /* Describes region of canvas that
440
* must be redisplayed (not used). */
441
{
442
ImageItem *imgPtr = (ImageItem *) itemPtr;
443
short drawableX, drawableY;
444
445
if (imgPtr->image == NULL) {
446
return;
447
}
448
449
/*
450
* Translate the coordinates to those of the image, then redisplay it.
451
*/
452
453
Tk_CanvasDrawableCoords(canvas, (double) x, (double) y,
454
&drawableX, &drawableY);
455
Tk_RedrawImage(imgPtr->image, x - imgPtr->header.x1, y - imgPtr->header.y1,
456
width, height, drawable, drawableX, drawableY);
457
}
458
459
/*
460
*--------------------------------------------------------------
461
*
462
* ImageToPoint --
463
*
464
* Computes the distance from a given point to a given
465
* rectangle, in canvas units.
466
*
467
* Results:
468
* The return value is 0 if the point whose x and y coordinates
469
* are coordPtr[0] and coordPtr[1] is inside the image. If the
470
* point isn't inside the image then the return value is the
471
* distance from the point to the image.
472
*
473
* Side effects:
474
* None.
475
*
476
*--------------------------------------------------------------
477
*/
478
479
static double
480
ImageToPoint(canvas, itemPtr, coordPtr)
481
Tk_Canvas canvas; /* Canvas containing item. */
482
Tk_Item *itemPtr; /* Item to check against point. */
483
double *coordPtr; /* Pointer to x and y coordinates. */
484
{
485
ImageItem *imgPtr = (ImageItem *) itemPtr;
486
double x1, x2, y1, y2, xDiff, yDiff;
487
488
x1 = imgPtr->header.x1;
489
y1 = imgPtr->header.y1;
490
x2 = imgPtr->header.x2;
491
y2 = imgPtr->header.y2;
492
493
/*
494
* Point is outside rectangle.
495
*/
496
497
if (coordPtr[0] < x1) {
498
xDiff = x1 - coordPtr[0];
499
} else if (coordPtr[0] > x2) {
500
xDiff = coordPtr[0] - x2;
501
} else {
502
xDiff = 0;
503
}
504
505
if (coordPtr[1] < y1) {
506
yDiff = y1 - coordPtr[1];
507
} else if (coordPtr[1] > y2) {
508
yDiff = coordPtr[1] - y2;
509
} else {
510
yDiff = 0;
511
}
512
513
return hypot(xDiff, yDiff);
514
}
515
516
/*
517
*--------------------------------------------------------------
518
*
519
* ImageToArea --
520
*
521
* This procedure is called to determine whether an item
522
* lies entirely inside, entirely outside, or overlapping
523
* a given rectangle.
524
*
525
* Results:
526
* -1 is returned if the item is entirely outside the area
527
* given by rectPtr, 0 if it overlaps, and 1 if it is entirely
528
* inside the given area.
529
*
530
* Side effects:
531
* None.
532
*
533
*--------------------------------------------------------------
534
*/
535
536
static int
537
ImageToArea(canvas, itemPtr, rectPtr)
538
Tk_Canvas canvas; /* Canvas containing item. */
539
Tk_Item *itemPtr; /* Item to check against rectangle. */
540
double *rectPtr; /* Pointer to array of four coordinates
541
* (x1, y1, x2, y2) describing rectangular
542
* area. */
543
{
544
ImageItem *imgPtr = (ImageItem *) itemPtr;
545
546
if ((rectPtr[2] <= imgPtr->header.x1)
547
|| (rectPtr[0] >= imgPtr->header.x2)
548
|| (rectPtr[3] <= imgPtr->header.y1)
549
|| (rectPtr[1] >= imgPtr->header.y2)) {
550
return -1;
551
}
552
if ((rectPtr[0] <= imgPtr->header.x1)
553
&& (rectPtr[1] <= imgPtr->header.y1)
554
&& (rectPtr[2] >= imgPtr->header.x2)
555
&& (rectPtr[3] >= imgPtr->header.y2)) {
556
return 1;
557
}
558
return 0;
559
}
560
561
/*
562
*--------------------------------------------------------------
563
*
564
* ScaleImage --
565
*
566
* This procedure is invoked to rescale an item.
567
*
568
* Results:
569
* None.
570
*
571
* Side effects:
572
* The item referred to by itemPtr is rescaled so that the
573
* following transformation is applied to all point coordinates:
574
* x' = originX + scaleX*(x-originX)
575
* y' = originY + scaleY*(y-originY)
576
*
577
*--------------------------------------------------------------
578
*/
579
580
static void
581
ScaleImage(canvas, itemPtr, originX, originY, scaleX, scaleY)
582
Tk_Canvas canvas; /* Canvas containing rectangle. */
583
Tk_Item *itemPtr; /* Rectangle to be scaled. */
584
double originX, originY; /* Origin about which to scale rect. */
585
double scaleX; /* Amount to scale in X direction. */
586
double scaleY; /* Amount to scale in Y direction. */
587
{
588
ImageItem *imgPtr = (ImageItem *) itemPtr;
589
590
imgPtr->x = originX + scaleX*(imgPtr->x - originX);
591
imgPtr->y = originY + scaleY*(imgPtr->y - originY);
592
ComputeImageBbox(canvas, imgPtr);
593
}
594
595
/*
596
*--------------------------------------------------------------
597
*
598
* TranslateImage --
599
*
600
* This procedure is called to move an item by a given amount.
601
*
602
* Results:
603
* None.
604
*
605
* Side effects:
606
* The position of the item is offset by (xDelta, yDelta), and
607
* the bounding box is updated in the generic part of the item
608
* structure.
609
*
610
*--------------------------------------------------------------
611
*/
612
613
static void
614
TranslateImage(canvas, itemPtr, deltaX, deltaY)
615
Tk_Canvas canvas; /* Canvas containing item. */
616
Tk_Item *itemPtr; /* Item that is being moved. */
617
double deltaX, deltaY; /* Amount by which item is to be
618
* moved. */
619
{
620
ImageItem *imgPtr = (ImageItem *) itemPtr;
621
622
imgPtr->x += deltaX;
623
imgPtr->y += deltaY;
624
ComputeImageBbox(canvas, imgPtr);
625
}
626
627
/*
628
*----------------------------------------------------------------------
629
*
630
* ImageChangedProc --
631
*
632
* This procedure is invoked by the image code whenever the manager
633
* for an image does something that affects the image's size or
634
* how it is displayed.
635
*
636
* Results:
637
* None.
638
*
639
* Side effects:
640
* Arranges for the canvas to get redisplayed.
641
*
642
*----------------------------------------------------------------------
643
*/
644
645
static void
646
ImageChangedProc(clientData, x, y, width, height, imgWidth, imgHeight)
647
ClientData clientData; /* Pointer to canvas item for image. */
648
int x, y; /* Upper left pixel (within image)
649
* that must be redisplayed. */
650
int width, height; /* Dimensions of area to redisplay
651
* (may be <= 0). */
652
int imgWidth, imgHeight; /* New dimensions of image. */
653
{
654
ImageItem *imgPtr = (ImageItem *) clientData;
655
656
/*
657
* If the image's size changed and it's not anchored at its
658
* northwest corner then just redisplay the entire area of the
659
* image. This is a bit over-conservative, but we need to do
660
* something because a size change also means a position change.
661
*/
662
663
if (((imgPtr->header.x2 - imgPtr->header.x1) != imgWidth)
664
|| ((imgPtr->header.y2 - imgPtr->header.y1) != imgHeight)) {
665
x = y = 0;
666
width = imgWidth;
667
height = imgHeight;
668
Tk_CanvasEventuallyRedraw(imgPtr->canvas, imgPtr->header.x1,
669
imgPtr->header.y1, imgPtr->header.x2, imgPtr->header.y2);
670
}
671
ComputeImageBbox(imgPtr->canvas, imgPtr);
672
Tk_CanvasEventuallyRedraw(imgPtr->canvas, imgPtr->header.x1 + x,
673
imgPtr->header.y1 + y, (int) (imgPtr->header.x1 + x + width),
674
(int) (imgPtr->header.y1 + y + height));
675
}
676
677