]> cvs.zerfleddert.de Git - micropolis/blob - src/tk/tkcvbmap.c
Fixes for compilation with gcc 15
[micropolis] / src / tk / tkcvbmap.c
1 /*
2 * tkCanvBmap.c --
3 *
4 * This file implements bitmap items for canvas widgets.
5 *
6 * Copyright 1992 Regents of the University of California.
7 * Permission to use, copy, modify, and distribute this
8 * software and its documentation for any purpose and without
9 * fee is hereby granted, provided that the above copyright
10 * notice appear in all copies. The University of California
11 * makes no representations about the suitability of this
12 * software for any purpose. It is provided "as is" without
13 * express or implied warranty.
14 */
15
16 #ifndef lint
17 static char rcsid[] = "$Header: /user6/ouster/wish/RCS/tkCanvBmap.c,v 1.4 92/08/24 09:24:11 ouster Exp $ SPRITE (Berkeley)";
18 #endif
19
20 #include <stdio.h>
21 #include <math.h>
22 #include "tkint.h"
23 #include "tkcanvas.h"
24
25 /*
26 * The structure below defines the record for each rectangle/oval item.
27 */
28
29 typedef struct BitmapItem {
30 Tk_Item header; /* Generic stuff that's the same for all
31 * types. MUST BE FIRST IN STRUCTURE. */
32 double x, y; /* Coordinates of positioning point for
33 * bitmap. */
34 Tk_Anchor anchor; /* Where to anchor bitmap relative to
35 * (x,y). */
36 Pixmap bitmap; /* Bitmap to display in window. */
37 XColor *fgColor; /* Foreground color to use for bitmap. */
38 XColor *bgColor; /* Background color to use for bitmap. */
39 GC gc; /* Graphics context to use for drawing
40 * bitmap on screen. */
41 } BitmapItem;
42
43 /*
44 * Information used for parsing configuration specs:
45 */
46
47 static Tk_ConfigSpec configSpecs[] = {
48 {TK_CONFIG_ANCHOR, "-anchor", (char *) NULL, (char *) NULL,
49 "center", Tk_Offset(BitmapItem, anchor), TK_CONFIG_DONT_SET_DEFAULT},
50 {TK_CONFIG_COLOR, "-background", (char *) NULL, (char *) NULL,
51 (char *) NULL, Tk_Offset(BitmapItem, bgColor), TK_CONFIG_NULL_OK},
52 #if defined(USE_XPM3)
53 {TK_CONFIG_PIXMAP, "-bitmap", (char *) NULL, (char *) NULL,
54 (char *) NULL, Tk_Offset(BitmapItem, bitmap), TK_CONFIG_NULL_OK},
55 #else
56 {TK_CONFIG_BITMAP, "-bitmap", (char *) NULL, (char *) NULL,
57 (char *) NULL, Tk_Offset(BitmapItem, bitmap), TK_CONFIG_NULL_OK},
58 #endif
59 {TK_CONFIG_COLOR, "-foreground", (char *) NULL, (char *) NULL,
60 "black", Tk_Offset(BitmapItem, fgColor), 0},
61 {TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,
62 (char *) NULL, 0, TK_CONFIG_NULL_OK, &tkCanvasTagsOption},
63 {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
64 (char *) NULL, 0, 0}
65 };
66
67 /*
68 * Prototypes for procedures defined in this file:
69 */
70
71 static int BitmapCoords _ANSI_ARGS_((Tk_Canvas *canvasPtr,
72 Tk_Item *itemPtr, int argc, char **argv));
73 static int BitmapToArea _ANSI_ARGS_((Tk_Canvas *canvasPtr,
74 Tk_Item *itemPtr, double *rectPtr));
75 static double BitmapToPoint _ANSI_ARGS_((Tk_Canvas *canvasPtr,
76 Tk_Item *itemPtr, double *coordPtr));
77 static void ComputeBitmapBbox _ANSI_ARGS_((Tk_Canvas *canvasPtr,
78 BitmapItem *bmapPtr));
79 static int ConfigureBitmap _ANSI_ARGS_((
80 Tk_Canvas *canvasPtr, Tk_Item *itemPtr, int argc,
81 char **argv, int flags));
82 static int CreateBitmap _ANSI_ARGS_((Tk_Canvas *canvasPtr,
83 struct Tk_Item *itemPtr, int argc, char **argv));
84 static void DeleteBitmap _ANSI_ARGS_((Tk_Item *itemPtr));
85 static void DisplayBitmap _ANSI_ARGS_((Tk_Canvas *canvasPtr,
86 Tk_Item *itemPtr, Drawable dst));
87 static void ScaleBitmap _ANSI_ARGS_((Tk_Canvas *canvasPtr,
88 Tk_Item *itemPtr, double originX, double originY,
89 double scaleX, double scaleY));
90 static void TranslateBitmap _ANSI_ARGS_((Tk_Canvas *canvasPtr,
91 Tk_Item *itemPtr, double deltaX, double deltaY));
92
93 /*
94 * The structures below defines the rectangle and oval item types
95 * by means of procedures that can be invoked by generic item code.
96 */
97
98 Tk_ItemType TkBitmapType = {
99 "bitmap", /* name */
100 sizeof(BitmapItem), /* itemSize */
101 CreateBitmap, /* createProc */
102 configSpecs, /* configSpecs */
103 ConfigureBitmap, /* configureProc */
104 BitmapCoords, /* coordProc */
105 DeleteBitmap, /* deleteProc */
106 DisplayBitmap, /* displayProc */
107 0, /* alwaysRedraw */
108 BitmapToPoint, /* pointProc */
109 BitmapToArea, /* areaProc */
110 (Tk_ItemPostscriptProc *) NULL, /* postscriptProc */
111 ScaleBitmap, /* scaleProc */
112 TranslateBitmap, /* translateProc */
113 (Tk_ItemIndexProc *) NULL, /* indexProc */
114 (Tk_ItemCursorProc *) NULL, /* cursorProc */
115 (Tk_ItemSelectionProc *) NULL, /* selectionProc */
116 (Tk_ItemInsertProc *) NULL, /* insertProc */
117 (Tk_ItemDCharsProc *) NULL, /* dTextProc */
118 (Tk_ItemType *) NULL /* nextPtr */
119 };
120 \f
121 /*
122 *--------------------------------------------------------------
123 *
124 * CreateBitmap --
125 *
126 * This procedure is invoked to create a new bitmap
127 * item in a canvas.
128 *
129 * Results:
130 * A standard Tcl return value. If an error occurred in
131 * creating the item, then an error message is left in
132 * canvasPtr->interp->result; in this case itemPtr is
133 * left uninitialized, so it can be safely freed by the
134 * caller.
135 *
136 * Side effects:
137 * A new bitmap item is created.
138 *
139 *--------------------------------------------------------------
140 */
141
142 static int
143 CreateBitmap (
144 register Tk_Canvas *canvasPtr, /* Canvas to hold new item. */
145 Tk_Item *itemPtr, /* Record to hold new item; header
146 * has been initialized by caller. */
147 int argc, /* Number of arguments in argv. */
148 char **argv /* Arguments describing rectangle. */
149 )
150 {
151 register BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
152
153 if (argc < 2) {
154 Tcl_AppendResult(canvasPtr->interp, "wrong # args: should be \"",
155 Tk_PathName(canvasPtr->tkwin), "\" create ",
156 itemPtr->typePtr->name, " x y ?options?",
157 (char *) NULL);
158 return TCL_ERROR;
159 }
160
161 /*
162 * Initialize item's record.
163 */
164
165 bmapPtr->anchor = TK_ANCHOR_CENTER;
166 bmapPtr->bitmap = None;
167 bmapPtr->fgColor = NULL;
168 bmapPtr->bgColor = NULL;
169 bmapPtr->gc = None;
170
171 /*
172 * Process the arguments to fill in the item record.
173 */
174
175 if ((TkGetCanvasCoord(canvasPtr, argv[0], &bmapPtr->x) != TCL_OK)
176 || (TkGetCanvasCoord(canvasPtr, argv[1],
177 &bmapPtr->y) != TCL_OK)) {
178 return TCL_ERROR;
179 }
180
181 if (ConfigureBitmap(canvasPtr, itemPtr, argc-2, argv+2, 0) != TCL_OK) {
182 DeleteBitmap(itemPtr);
183 return TCL_ERROR;
184 }
185 return TCL_OK;
186 }
187 \f
188 /*
189 *--------------------------------------------------------------
190 *
191 * BitmapCoords --
192 *
193 * This procedure is invoked to process the "coords" widget
194 * command on bitmap items. See the user documentation for
195 * details on what it does.
196 *
197 * Results:
198 * Returns TCL_OK or TCL_ERROR, and sets canvasPtr->interp->result.
199 *
200 * Side effects:
201 * The coordinates for the given item may be changed.
202 *
203 *--------------------------------------------------------------
204 */
205
206 static int
207 BitmapCoords (
208 register Tk_Canvas *canvasPtr, /* Canvas containing item. */
209 Tk_Item *itemPtr, /* Item whose coordinates are to be
210 * read or modified. */
211 int argc, /* Number of coordinates supplied in
212 * argv. */
213 char **argv /* Array of coordinates: x1, y1,
214 * x2, y2, ... */
215 )
216 {
217 register BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
218
219 if (argc == 0) {
220 sprintf(canvasPtr->interp->result, "%g %g", bmapPtr->x, bmapPtr->y);
221 } else if (argc == 2) {
222 if ((TkGetCanvasCoord(canvasPtr, argv[0], &bmapPtr->x) != TCL_OK)
223 || (TkGetCanvasCoord(canvasPtr, argv[1],
224 &bmapPtr->y) != TCL_OK)) {
225 return TCL_ERROR;
226 }
227 ComputeBitmapBbox(canvasPtr, bmapPtr);
228 } else {
229 sprintf(canvasPtr->interp->result,
230 "wrong # coordinates: expected 0 or 2, got %d",
231 argc);
232 return TCL_ERROR;
233 }
234 return TCL_OK;
235 }
236 \f
237 /*
238 *--------------------------------------------------------------
239 *
240 * ConfigureBitmap --
241 *
242 * This procedure is invoked to configure various aspects
243 * of a bitmap item, such as its anchor position.
244 *
245 * Results:
246 * A standard Tcl result code. If an error occurs, then
247 * an error message is left in canvasPtr->interp->result.
248 *
249 * Side effects:
250 * Configuration information may be set for itemPtr.
251 *
252 *--------------------------------------------------------------
253 */
254
255 static int
256 ConfigureBitmap (
257 Tk_Canvas *canvasPtr, /* Canvas containing itemPtr. */
258 Tk_Item *itemPtr, /* Bitmap item to reconfigure. */
259 int argc, /* Number of elements in argv. */
260 char **argv, /* Arguments describing things to configure. */
261 int flags /* Flags to pass to Tk_ConfigureWidget. */
262 )
263 {
264 register BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
265 XGCValues gcValues;
266 GC newGC;
267
268 if (Tk_ConfigureWidget(canvasPtr->interp, canvasPtr->tkwin,
269 configSpecs, argc, argv, (char *) bmapPtr, flags) != TCL_OK) {
270 return TCL_ERROR;
271 }
272
273 /*
274 * A few of the options require additional processing, such as those
275 * that determine the graphics context.
276 */
277
278 gcValues.foreground = bmapPtr->fgColor->pixel;
279 if (bmapPtr->bgColor != NULL) {
280 gcValues.background = bmapPtr->bgColor->pixel;
281 } else {
282 gcValues.background = canvasPtr->bgColor->pixel;
283 }
284 newGC = Tk_GetGC(canvasPtr->tkwin, GCForeground|GCBackground, &gcValues);
285 if (bmapPtr->gc != None) {
286 Tk_FreeGC(bmapPtr->gc);
287 }
288 bmapPtr->gc = newGC;
289
290 ComputeBitmapBbox(canvasPtr, bmapPtr);
291
292 return TCL_OK;
293 }
294 \f
295 /*
296 *--------------------------------------------------------------
297 *
298 * DeleteBitmap --
299 *
300 * This procedure is called to clean up the data structure
301 * associated with a bitmap item.
302 *
303 * Results:
304 * None.
305 *
306 * Side effects:
307 * Resources associated with itemPtr are released.
308 *
309 *--------------------------------------------------------------
310 */
311
312 static void
313 DeleteBitmap (
314 Tk_Item *itemPtr /* Item that is being deleted. */
315 )
316 {
317 register BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
318
319 if (bmapPtr->bitmap != None) {
320 #if defined(USE_XPM3)
321 Tk_FreePixmap(bmapPtr->bitmap);
322 #else
323 Tk_FreeBitmap(bmapPtr->bitmap);
324 #endif
325 }
326 if (bmapPtr->fgColor != NULL) {
327 Tk_FreeColor(bmapPtr->fgColor);
328 }
329 if (bmapPtr->bgColor != NULL) {
330 Tk_FreeColor(bmapPtr->bgColor);
331 }
332 if (bmapPtr->gc != NULL) {
333 Tk_FreeGC(bmapPtr->gc);
334 }
335 }
336 \f
337 /*
338 *--------------------------------------------------------------
339 *
340 * ComputeBitmapBbox --
341 *
342 * This procedure is invoked to compute the bounding box of
343 * all the pixels that may be drawn as part of a bitmap item.
344 * This procedure is where the child bitmap's placement is
345 * computed.
346 *
347 * Results:
348 * None.
349 *
350 * Side effects:
351 * The fields x1, y1, x2, and y2 are updated in the header
352 * for itemPtr.
353 *
354 *--------------------------------------------------------------
355 */
356
357 /* ARGSUSED */
358 static void
359 ComputeBitmapBbox (
360 Tk_Canvas *canvasPtr, /* Canvas that contains item. */
361 register BitmapItem *bmapPtr /* Item whose bbox is to be
362 * recomputed. */
363 )
364 {
365 unsigned int width, height;
366 int x, y;
367
368 x = bmapPtr->x + 0.5;
369 y = bmapPtr->y + 0.5;
370
371 if (bmapPtr->bitmap == None) {
372 bmapPtr->header.x1 = bmapPtr->header.x2 = x;
373 bmapPtr->header.y1 = bmapPtr->header.y2 = y;
374 return;
375 }
376
377 /*
378 * Compute location and size of bitmap, using anchor information.
379 */
380
381 #if defined(USE_XPM3)
382 Tk_SizeOfPixmap(bmapPtr->bitmap, &width, &height);
383 #else
384 Tk_SizeOfBitmap(bmapPtr->bitmap, &width, &height);
385 #endif
386 switch (bmapPtr->anchor) {
387 case TK_ANCHOR_N:
388 x -= width/2;
389 break;
390 case TK_ANCHOR_NE:
391 x -= width;
392 break;
393 case TK_ANCHOR_E:
394 x -= width;
395 y -= height/2;
396 break;
397 case TK_ANCHOR_SE:
398 x -= width;
399 y -= height;
400 break;
401 case TK_ANCHOR_S:
402 x -= width/2;
403 y -= height;
404 break;
405 case TK_ANCHOR_SW:
406 y -= height;
407 break;
408 case TK_ANCHOR_W:
409 y -= height/2;
410 break;
411 case TK_ANCHOR_NW:
412 break;
413 case TK_ANCHOR_CENTER:
414 x -= width/2;
415 y -= height/2;
416 break;
417 }
418
419 /*
420 * Store the information in the item header.
421 */
422
423 bmapPtr->header.x1 = x;
424 bmapPtr->header.y1 = y;
425 bmapPtr->header.x2 = x + width;
426 bmapPtr->header.y2 = y + height;
427 }
428 \f
429 /*
430 *--------------------------------------------------------------
431 *
432 * DisplayBitmap --
433 *
434 * This procedure is invoked to draw a bitmap item in a given
435 * drawable.
436 *
437 * Results:
438 * None.
439 *
440 * Side effects:
441 * ItemPtr is drawn in drawable using the transformation
442 * information in canvasPtr.
443 *
444 *--------------------------------------------------------------
445 */
446
447 static void
448 DisplayBitmap (
449 register Tk_Canvas *canvasPtr, /* Canvas that contains item. */
450 Tk_Item *itemPtr, /* Item to be displayed. */
451 Drawable drawable /* Pixmap or window in which to draw
452 * item. */
453 )
454 {
455 register BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
456
457 if (bmapPtr->bitmap != None) {
458 #if defined(USE_XPM3)
459 XCopyArea(Tk_Display(canvasPtr->tkwin), bmapPtr->bitmap, drawable,
460 bmapPtr->gc, 0, 0,
461 (unsigned int) bmapPtr->header.x2 - bmapPtr->header.x1,
462 (unsigned int) bmapPtr->header.y2 - bmapPtr->header.y1,
463 bmapPtr->header.x1 - canvasPtr->drawableXOrigin,
464 bmapPtr->header.y1 - canvasPtr->drawableYOrigin);
465 #else
466 XCopyPlane(Tk_Display(canvasPtr->tkwin), bmapPtr->bitmap, drawable,
467 bmapPtr->gc, 0, 0,
468 (unsigned int) bmapPtr->header.x2 - bmapPtr->header.x1,
469 (unsigned int) bmapPtr->header.y2 - bmapPtr->header.y1,
470 bmapPtr->header.x1 - canvasPtr->drawableXOrigin,
471 bmapPtr->header.y1 - canvasPtr->drawableYOrigin, 1);
472 #endif
473 }
474 }
475 \f
476 /*
477 *--------------------------------------------------------------
478 *
479 * BitmapToPoint --
480 *
481 * Computes the distance from a given point to a given
482 * rectangle, in canvas units.
483 *
484 * Results:
485 * The return value is 0 if the point whose x and y coordinates
486 * are coordPtr[0] and coordPtr[1] is inside the bitmap. If the
487 * point isn't inside the bitmap then the return value is the
488 * distance from the point to the bitmap.
489 *
490 * Side effects:
491 * None.
492 *
493 *--------------------------------------------------------------
494 */
495
496 /* ARGSUSED */
497 static double
498 BitmapToPoint (
499 Tk_Canvas *canvasPtr, /* Canvas containing item. */
500 Tk_Item *itemPtr, /* Item to check against point. */
501 double *coordPtr /* Pointer to x and y coordinates. */
502 )
503 {
504 register BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
505 double x1, x2, y1, y2, xDiff, yDiff;
506
507 x1 = bmapPtr->header.x1;
508 y1 = bmapPtr->header.y1;
509 x2 = bmapPtr->header.x2;
510 y2 = bmapPtr->header.y2;
511
512 /*
513 * Point is outside rectangle.
514 */
515
516 if (coordPtr[0] < x1) {
517 xDiff = x1 - coordPtr[0];
518 } else if (coordPtr[0] > x2) {
519 xDiff = coordPtr[0] - x2;
520 } else {
521 xDiff = 0;
522 }
523
524 if (coordPtr[1] < y1) {
525 yDiff = y1 - coordPtr[1];
526 } else if (coordPtr[1] > y2) {
527 yDiff = coordPtr[1] - y2;
528 } else {
529 yDiff = 0;
530 }
531
532 return hypot(xDiff, yDiff);
533 }
534 \f
535 /*
536 *--------------------------------------------------------------
537 *
538 * BitmapToArea --
539 *
540 * This procedure is called to determine whether an item
541 * lies entirely inside, entirely outside, or overlapping
542 * a given rectangle.
543 *
544 * Results:
545 * -1 is returned if the item is entirely outside the area
546 * given by rectPtr, 0 if it overlaps, and 1 if it is entirely
547 * inside the given area.
548 *
549 * Side effects:
550 * None.
551 *
552 *--------------------------------------------------------------
553 */
554
555 /* ARGSUSED */
556 static int
557 BitmapToArea (
558 Tk_Canvas *canvasPtr, /* Canvas containing item. */
559 Tk_Item *itemPtr, /* Item to check against rectangle. */
560 double *rectPtr /* Pointer to array of four coordinates
561 * (x1, y1, x2, y2) describing rectangular
562 * area. */
563 )
564 {
565 register BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
566
567 if ((rectPtr[2] <= bmapPtr->header.x1)
568 || (rectPtr[0] >= bmapPtr->header.x2)
569 || (rectPtr[3] <= bmapPtr->header.y1)
570 || (rectPtr[1] >= bmapPtr->header.y2)) {
571 return -1;
572 }
573 if ((rectPtr[0] <= bmapPtr->header.x1)
574 && (rectPtr[1] <= bmapPtr->header.y1)
575 && (rectPtr[2] >= bmapPtr->header.x2)
576 && (rectPtr[3] >= bmapPtr->header.y2)) {
577 return 1;
578 }
579 return 0;
580 }
581 \f
582 /*
583 *--------------------------------------------------------------
584 *
585 * ScaleBitmap --
586 *
587 * This procedure is invoked to rescale a rectangle or oval
588 * item.
589 *
590 * Results:
591 * None.
592 *
593 * Side effects:
594 * The rectangle or oval referred to by itemPtr is rescaled
595 * so that the following transformation is applied to all
596 * point coordinates:
597 * x' = originX + scaleX*(x-originX)
598 * y' = originY + scaleY*(y-originY)
599 *
600 *--------------------------------------------------------------
601 */
602
603 static void
604 ScaleBitmap (
605 Tk_Canvas *canvasPtr, /* Canvas containing rectangle. */
606 Tk_Item *itemPtr, /* Rectangle to be scaled. */
607 double originX,
608 double originY, /* Origin about which to scale rect. */
609 double scaleX, /* Amount to scale in X direction. */
610 double scaleY /* Amount to scale in Y direction. */
611 )
612 {
613 register BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
614
615 bmapPtr->x = originX + scaleX*(bmapPtr->x - originX);
616 bmapPtr->y = originY + scaleY*(bmapPtr->y - originY);
617 ComputeBitmapBbox(canvasPtr, bmapPtr);
618 }
619 \f
620 /*
621 *--------------------------------------------------------------
622 *
623 * TranslateBitmap --
624 *
625 * This procedure is called to move a rectangle or oval by a
626 * given amount.
627 *
628 * Results:
629 * None.
630 *
631 * Side effects:
632 * The position of the rectangle or oval is offset by
633 * (xDelta, yDelta), and the bounding box is updated in the
634 * generic part of the item structure.
635 *
636 *--------------------------------------------------------------
637 */
638
639 static void
640 TranslateBitmap (
641 Tk_Canvas *canvasPtr, /* Canvas containing item. */
642 Tk_Item *itemPtr, /* Item that is being moved. */
643 double deltaX,
644 double deltaY /* Amount by which item is to be
645 * moved. */
646 )
647 {
648 register BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
649
650 bmapPtr->x += deltaX;
651 bmapPtr->y += deltaY;
652 ComputeBitmapBbox(canvasPtr, bmapPtr);
653 }
Impressum, Datenschutz