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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
|
/*
* tkCanvImg.c --
*
* This file implements image items for canvas widgets.
*
* Copyright (c) 1994 The Regents of the University of California.
* Copyright (c) 1994-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tkCanvImg.c 1.19 97/11/07 21:14:48
*/
#include <stdio.h>
#include "tkInt.h"
#include "tkPort.h"
#include "tkCanvas.h"
/*
* The structure below defines the record for each image item.
*/
typedef struct ImageItem {
Tk_Item header; /* Generic stuff that's the same for all
* types. MUST BE FIRST IN STRUCTURE. */
Tk_Canvas canvas; /* Canvas containing the image. */
double x, y; /* Coordinates of positioning point for
* image. */
Tk_Anchor anchor; /* Where to anchor image relative to
* (x,y). */
char *imageString; /* String describing -image option (malloc-ed).
* NULL means no image right now. */
Tk_Image image; /* Image to display in window, or NULL if
* no image at present. */
} ImageItem;
/*
* Information used for parsing configuration specs:
*/
static Tk_CustomOption tagsOption = {Tk_CanvasTagsParseProc,
Tk_CanvasTagsPrintProc, (ClientData) NULL
};
static Tk_ConfigSpec configSpecs[] = {
{TK_CONFIG_ANCHOR, "-anchor", (char *) NULL, (char *) NULL,
"center", Tk_Offset(ImageItem, anchor), TK_CONFIG_DONT_SET_DEFAULT},
{TK_CONFIG_STRING, "-image", (char *) NULL, (char *) NULL,
(char *) NULL, Tk_Offset(ImageItem, imageString), TK_CONFIG_NULL_OK},
{TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,
(char *) NULL, 0, TK_CONFIG_NULL_OK, &tagsOption},
{TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
(char *) NULL, 0, 0}
};
/*
* Prototypes for procedures defined in this file:
*/
static void ImageChangedProc _ANSI_ARGS_((ClientData clientData,
int x, int y, int width, int height, int imgWidth,
int imgHeight));
static int ImageCoords _ANSI_ARGS_((Tcl_Interp *interp,
Tk_Canvas canvas, Tk_Item *itemPtr, int argc,
char **argv));
static int ImageToArea _ANSI_ARGS_((Tk_Canvas canvas,
Tk_Item *itemPtr, double *rectPtr));
static double ImageToPoint _ANSI_ARGS_((Tk_Canvas canvas,
Tk_Item *itemPtr, double *coordPtr));
static void ComputeImageBbox _ANSI_ARGS_((Tk_Canvas canvas,
ImageItem *imgPtr));
static int ConfigureImage _ANSI_ARGS_((Tcl_Interp *interp,
Tk_Canvas canvas, Tk_Item *itemPtr, int argc,
char **argv, int flags));
static int CreateImage _ANSI_ARGS_((Tcl_Interp *interp,
Tk_Canvas canvas, struct Tk_Item *itemPtr,
int argc, char **argv));
static void DeleteImage _ANSI_ARGS_((Tk_Canvas canvas,
Tk_Item *itemPtr, Display *display));
static void DisplayImage _ANSI_ARGS_((Tk_Canvas canvas,
Tk_Item *itemPtr, Display *display, Drawable dst,
int x, int y, int width, int height));
static void ScaleImage _ANSI_ARGS_((Tk_Canvas canvas,
Tk_Item *itemPtr, double originX, double originY,
double scaleX, double scaleY));
static void TranslateImage _ANSI_ARGS_((Tk_Canvas canvas,
Tk_Item *itemPtr, double deltaX, double deltaY));
/*
* The structures below defines the image item type in terms of
* procedures that can be invoked by generic item code.
*/
Tk_ItemType tkImageType = {
"image", /* name */
sizeof(ImageItem), /* itemSize */
CreateImage, /* createProc */
configSpecs, /* configSpecs */
ConfigureImage, /* configureProc */
ImageCoords, /* coordProc */
DeleteImage, /* deleteProc */
DisplayImage, /* displayProc */
0, /* alwaysRedraw */
ImageToPoint, /* pointProc */
ImageToArea, /* areaProc */
(Tk_ItemPostscriptProc *) NULL, /* postscriptProc */
ScaleImage, /* scaleProc */
TranslateImage, /* translateProc */
(Tk_ItemIndexProc *) NULL, /* indexProc */
(Tk_ItemCursorProc *) NULL, /* icursorProc */
(Tk_ItemSelectionProc *) NULL, /* selectionProc */
(Tk_ItemInsertProc *) NULL, /* insertProc */
(Tk_ItemDCharsProc *) NULL, /* dTextProc */
(Tk_ItemType *) NULL /* nextPtr */
};
/*
*--------------------------------------------------------------
*
* CreateImage --
*
* This procedure is invoked to create a new image
* item in a canvas.
*
* Results:
* A standard Tcl return value. If an error occurred in
* creating the item, then an error message is left in
* the interp's result; in this case itemPtr is left uninitialized,
* so it can be safely freed by the caller.
*
* Side effects:
* A new image item is created.
*
*--------------------------------------------------------------
*/
static int
CreateImage(interp, canvas, itemPtr, argc, argv)
Tcl_Interp *interp; /* Interpreter for error reporting. */
Tk_Canvas canvas; /* Canvas to hold new item. */
Tk_Item *itemPtr; /* Record to hold new item; header
* has been initialized by caller. */
int argc; /* Number of arguments in argv. */
char **argv; /* Arguments describing rectangle. */
{
ImageItem *imgPtr = (ImageItem *) itemPtr;
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
Tk_PathName(Tk_CanvasTkwin(canvas)), " create ",
itemPtr->typePtr->name, " x y ?options?\"",
(char *) NULL);
return TCL_ERROR;
}
/*
* Initialize item's record.
*/
imgPtr->canvas = canvas;
imgPtr->anchor = TK_ANCHOR_CENTER;
imgPtr->imageString = NULL;
imgPtr->image = NULL;
/*
* Process the arguments to fill in the item record.
*/
if ((Tk_CanvasGetCoord(interp, canvas, argv[0], &imgPtr->x) != TCL_OK)
|| (Tk_CanvasGetCoord(interp, canvas, argv[1], &imgPtr->y)
!= TCL_OK)) {
return TCL_ERROR;
}
if (ConfigureImage(interp, canvas, itemPtr, argc-2, argv+2, 0) != TCL_OK) {
DeleteImage(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
return TCL_ERROR;
}
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* ImageCoords --
*
* This procedure is invoked to process the "coords" widget
* command on image items. See the user documentation for
* details on what it does.
*
* Results:
* Returns TCL_OK or TCL_ERROR, and sets the interp's result.
*
* Side effects:
* The coordinates for the given item may be changed.
*
*--------------------------------------------------------------
*/
static int
ImageCoords(interp, canvas, itemPtr, argc, argv)
Tcl_Interp *interp; /* Used for error reporting. */
Tk_Canvas canvas; /* Canvas containing item. */
Tk_Item *itemPtr; /* Item whose coordinates are to be
* read or modified. */
int argc; /* Number of coordinates supplied in
* argv. */
char **argv; /* Array of coordinates: x1, y1,
* x2, y2, ... */
{
ImageItem *imgPtr = (ImageItem *) itemPtr;
char x[TCL_DOUBLE_SPACE], y[TCL_DOUBLE_SPACE];
if (argc == 0) {
Tcl_PrintDouble(interp, imgPtr->x, x);
Tcl_PrintDouble(interp, imgPtr->y, y);
Tcl_AppendResult(interp, x, " ", y, (char *) NULL);
} else if (argc == 2) {
if ((Tk_CanvasGetCoord(interp, canvas, argv[0], &imgPtr->x) != TCL_OK)
|| (Tk_CanvasGetCoord(interp, canvas, argv[1],
&imgPtr->y) != TCL_OK)) {
return TCL_ERROR;
}
ComputeImageBbox(canvas, imgPtr);
} else {
char buf[64];
sprintf(buf, "wrong # coordinates: expected 0 or 2, got %d", argc);
Tcl_SetResult(interp, buf, TCL_VOLATILE);
return TCL_ERROR;
}
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* ConfigureImage --
*
* This procedure is invoked to configure various aspects
* of an image item, such as its anchor position.
*
* Results:
* A standard Tcl result code. If an error occurs, then
* an error message is left in the interp's result.
*
* Side effects:
* Configuration information may be set for itemPtr.
*
*--------------------------------------------------------------
*/
static int
ConfigureImage(interp, canvas, itemPtr, argc, argv, flags)
Tcl_Interp *interp; /* Used for error reporting. */
Tk_Canvas canvas; /* Canvas containing itemPtr. */
Tk_Item *itemPtr; /* Image item to reconfigure. */
int argc; /* Number of elements in argv. */
char **argv; /* Arguments describing things to configure. */
int flags; /* Flags to pass to Tk_ConfigureWidget. */
{
ImageItem *imgPtr = (ImageItem *) itemPtr;
Tk_Window tkwin;
Tk_Image image;
tkwin = Tk_CanvasTkwin(canvas);
if (Tk_ConfigureWidget(interp, tkwin, configSpecs, argc,
argv, (char *) imgPtr, flags) != TCL_OK) {
return TCL_ERROR;
}
/*
* Create the image. Save the old image around and don't free it
* until after the new one is allocated. This keeps the reference
* count from going to zero so the image doesn't have to be recreated
* if it hasn't changed.
*/
if (imgPtr->imageString != NULL) {
image = Tk_GetImage(interp, tkwin, imgPtr->imageString,
ImageChangedProc, (ClientData) imgPtr);
if (image == NULL) {
return TCL_ERROR;
}
} else {
image = NULL;
}
if (imgPtr->image != NULL) {
Tk_FreeImage(imgPtr->image);
}
imgPtr->image = image;
ComputeImageBbox(canvas, imgPtr);
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* DeleteImage --
*
* This procedure is called to clean up the data structure
* associated with a image item.
*
* Results:
* None.
*
* Side effects:
* Resources associated with itemPtr are released.
*
*--------------------------------------------------------------
*/
static void
DeleteImage(canvas, itemPtr, display)
Tk_Canvas canvas; /* Info about overall canvas widget. */
Tk_Item *itemPtr; /* Item that is being deleted. */
Display *display; /* Display containing window for
* canvas. */
{
ImageItem *imgPtr = (ImageItem *) itemPtr;
if (imgPtr->imageString != NULL) {
ckfree(imgPtr->imageString);
}
if (imgPtr->image != NULL) {
Tk_FreeImage(imgPtr->image);
}
}
/*
*--------------------------------------------------------------
*
* ComputeImageBbox --
*
* This procedure is invoked to compute the bounding box of
* all the pixels that may be drawn as part of a image item.
* This procedure is where the child image's placement is
* computed.
*
* Results:
* None.
*
* Side effects:
* The fields x1, y1, x2, and y2 are updated in the header
* for itemPtr.
*
*--------------------------------------------------------------
*/
/* ARGSUSED */
static void
ComputeImageBbox(canvas, imgPtr)
Tk_Canvas canvas; /* Canvas that contains item. */
ImageItem *imgPtr; /* Item whose bbox is to be
* recomputed. */
{
int width, height;
int x, y;
x = (int) (imgPtr->x + ((imgPtr->x >= 0) ? 0.5 : - 0.5));
y = (int) (imgPtr->y + ((imgPtr->y >= 0) ? 0.5 : - 0.5));
if (imgPtr->image == None) {
imgPtr->header.x1 = imgPtr->header.x2 = x;
imgPtr->header.y1 = imgPtr->header.y2 = y;
return;
}
/*
* Compute location and size of image, using anchor information.
*/
Tk_SizeOfImage(imgPtr->image, &width, &height);
switch (imgPtr->anchor) {
case TK_ANCHOR_N:
x -= width/2;
break;
case TK_ANCHOR_NE:
x -= width;
break;
case TK_ANCHOR_E:
x -= width;
y -= height/2;
break;
case TK_ANCHOR_SE:
x -= width;
y -= height;
break;
case TK_ANCHOR_S:
x -= width/2;
y -= height;
break;
case TK_ANCHOR_SW:
y -= height;
break;
case TK_ANCHOR_W:
y -= height/2;
break;
case TK_ANCHOR_NW:
break;
case TK_ANCHOR_CENTER:
x -= width/2;
y -= height/2;
break;
}
/*
* Store the information in the item header.
*/
imgPtr->header.x1 = x;
imgPtr->header.y1 = y;
imgPtr->header.x2 = x + width;
imgPtr->header.y2 = y + height;
}
/*
*--------------------------------------------------------------
*
* DisplayImage --
*
* This procedure is invoked to draw a image item in a given
* drawable.
*
* Results:
* None.
*
* Side effects:
* ItemPtr is drawn in drawable using the transformation
* information in canvas.
*
*--------------------------------------------------------------
*/
static void
DisplayImage(canvas, itemPtr, display, drawable, x, y, width, height)
Tk_Canvas canvas; /* Canvas that contains item. */
Tk_Item *itemPtr; /* Item to be displayed. */
Display *display; /* Display on which to draw item. */
Drawable drawable; /* Pixmap or window in which to draw
* item. */
int x, y, width, height; /* Describes region of canvas that
* must be redisplayed (not used). */
{
ImageItem *imgPtr = (ImageItem *) itemPtr;
short drawableX, drawableY;
if (imgPtr->image == NULL) {
return;
}
/*
* Translate the coordinates to those of the image, then redisplay it.
*/
Tk_CanvasDrawableCoords(canvas, (double) x, (double) y,
&drawableX, &drawableY);
Tk_RedrawImage(imgPtr->image, x - imgPtr->header.x1, y - imgPtr->header.y1,
width, height, drawable, drawableX, drawableY);
}
/*
*--------------------------------------------------------------
*
* ImageToPoint --
*
* Computes the distance from a given point to a given
* rectangle, in canvas units.
*
* Results:
* The return value is 0 if the point whose x and y coordinates
* are coordPtr[0] and coordPtr[1] is inside the image. If the
* point isn't inside the image then the return value is the
* distance from the point to the image.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
static double
ImageToPoint(canvas, itemPtr, coordPtr)
Tk_Canvas canvas; /* Canvas containing item. */
Tk_Item *itemPtr; /* Item to check against point. */
double *coordPtr; /* Pointer to x and y coordinates. */
{
ImageItem *imgPtr = (ImageItem *) itemPtr;
double x1, x2, y1, y2, xDiff, yDiff;
x1 = imgPtr->header.x1;
y1 = imgPtr->header.y1;
x2 = imgPtr->header.x2;
y2 = imgPtr->header.y2;
/*
* Point is outside rectangle.
*/
if (coordPtr[0] < x1) {
xDiff = x1 - coordPtr[0];
} else if (coordPtr[0] > x2) {
xDiff = coordPtr[0] - x2;
} else {
xDiff = 0;
}
if (coordPtr[1] < y1) {
yDiff = y1 - coordPtr[1];
} else if (coordPtr[1] > y2) {
yDiff = coordPtr[1] - y2;
} else {
yDiff = 0;
}
return hypot(xDiff, yDiff);
}
/*
*--------------------------------------------------------------
*
* ImageToArea --
*
* This procedure is called to determine whether an item
* lies entirely inside, entirely outside, or overlapping
* a given rectangle.
*
* Results:
* -1 is returned if the item is entirely outside the area
* given by rectPtr, 0 if it overlaps, and 1 if it is entirely
* inside the given area.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
static int
ImageToArea(canvas, itemPtr, rectPtr)
Tk_Canvas canvas; /* Canvas containing item. */
Tk_Item *itemPtr; /* Item to check against rectangle. */
double *rectPtr; /* Pointer to array of four coordinates
* (x1, y1, x2, y2) describing rectangular
* area. */
{
ImageItem *imgPtr = (ImageItem *) itemPtr;
if ((rectPtr[2] <= imgPtr->header.x1)
|| (rectPtr[0] >= imgPtr->header.x2)
|| (rectPtr[3] <= imgPtr->header.y1)
|| (rectPtr[1] >= imgPtr->header.y2)) {
return -1;
}
if ((rectPtr[0] <= imgPtr->header.x1)
&& (rectPtr[1] <= imgPtr->header.y1)
&& (rectPtr[2] >= imgPtr->header.x2)
&& (rectPtr[3] >= imgPtr->header.y2)) {
return 1;
}
return 0;
}
/*
*--------------------------------------------------------------
*
* ScaleImage --
*
* This procedure is invoked to rescale an item.
*
* Results:
* None.
*
* Side effects:
* The item referred to by itemPtr is rescaled so that the
* following transformation is applied to all point coordinates:
* x' = originX + scaleX*(x-originX)
* y' = originY + scaleY*(y-originY)
*
*--------------------------------------------------------------
*/
static void
ScaleImage(canvas, itemPtr, originX, originY, scaleX, scaleY)
Tk_Canvas canvas; /* Canvas containing rectangle. */
Tk_Item *itemPtr; /* Rectangle to be scaled. */
double originX, originY; /* Origin about which to scale rect. */
double scaleX; /* Amount to scale in X direction. */
double scaleY; /* Amount to scale in Y direction. */
{
ImageItem *imgPtr = (ImageItem *) itemPtr;
imgPtr->x = originX + scaleX*(imgPtr->x - originX);
imgPtr->y = originY + scaleY*(imgPtr->y - originY);
ComputeImageBbox(canvas, imgPtr);
}
/*
*--------------------------------------------------------------
*
* TranslateImage --
*
* This procedure is called to move an item by a given amount.
*
* Results:
* None.
*
* Side effects:
* The position of the item is offset by (xDelta, yDelta), and
* the bounding box is updated in the generic part of the item
* structure.
*
*--------------------------------------------------------------
*/
static void
TranslateImage(canvas, itemPtr, deltaX, deltaY)
Tk_Canvas canvas; /* Canvas containing item. */
Tk_Item *itemPtr; /* Item that is being moved. */
double deltaX, deltaY; /* Amount by which item is to be
* moved. */
{
ImageItem *imgPtr = (ImageItem *) itemPtr;
imgPtr->x += deltaX;
imgPtr->y += deltaY;
ComputeImageBbox(canvas, imgPtr);
}
/*
*----------------------------------------------------------------------
*
* ImageChangedProc --
*
* This procedure is invoked by the image code whenever the manager
* for an image does something that affects the image's size or
* how it is displayed.
*
* Results:
* None.
*
* Side effects:
* Arranges for the canvas to get redisplayed.
*
*----------------------------------------------------------------------
*/
static void
ImageChangedProc(clientData, x, y, width, height, imgWidth, imgHeight)
ClientData clientData; /* Pointer to canvas item for image. */
int x, y; /* Upper left pixel (within image)
* that must be redisplayed. */
int width, height; /* Dimensions of area to redisplay
* (may be <= 0). */
int imgWidth, imgHeight; /* New dimensions of image. */
{
ImageItem *imgPtr = (ImageItem *) clientData;
/*
* If the image's size changed and it's not anchored at its
* northwest corner then just redisplay the entire area of the
* image. This is a bit over-conservative, but we need to do
* something because a size change also means a position change.
*/
if (((imgPtr->header.x2 - imgPtr->header.x1) != imgWidth)
|| ((imgPtr->header.y2 - imgPtr->header.y1) != imgHeight)) {
x = y = 0;
width = imgWidth;
height = imgHeight;
Tk_CanvasEventuallyRedraw(imgPtr->canvas, imgPtr->header.x1,
imgPtr->header.y1, imgPtr->header.x2, imgPtr->header.y2);
}
ComputeImageBbox(imgPtr->canvas, imgPtr);
Tk_CanvasEventuallyRedraw(imgPtr->canvas, imgPtr->header.x1 + x,
imgPtr->header.y1 + y, (int) (imgPtr->header.x1 + x + width),
(int) (imgPtr->header.y1 + y + height));
}
|