Debian Codegraph @

pTk/mTk/generic/tkCanvBmap.c

raw symbols SCIP
1 /*
2 * tkCanvBmap.c --
3 *
4 * This file implements bitmap items for canvas widgets.
5 *
6 * Copyright (c) 1992-1994 The Regents of the University of California.
7 * Copyright (c) 1994-1997 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 * RCS: @(#) $Id: tkCanvBmap.c,v 1.7.2.1 2003/04/18 21:56:47 hobbs Exp $
13 */
14
15 #include "tkInt.h"
16 #include "tkPort.h"
17 #include "tkCanvases.h"
18
19 /*
20 * The structure below defines the record for each bitmap item.
21 */
22
23 typedef struct BitmapItem {
24 Tk_Item header; /* Generic stuff that's the same for all
25 * types. MUST BE FIRST IN STRUCTURE. */
26 double x, y; /* Coordinates of positioning point for
27 * bitmap. */
28 Tk_Anchor anchor; /* Where to anchor bitmap relative to
29 * (x,y). */
30 Pixmap bitmap; /* Bitmap to display in window. */
31 Pixmap activeBitmap; /* Bitmap to display in window. */
32 Pixmap disabledBitmap; /* Bitmap to display in window. */
33 XColor *fgColor; /* Foreground color to use for bitmap. */
34 XColor *activeFgColor; /* Foreground color to use for bitmap. */
35 XColor *disabledFgColor; /* Foreground color to use for bitmap. */
36 XColor *bgColor; /* Background color to use for bitmap. */
37 XColor *activeBgColor; /* Background color to use for bitmap. */
38 XColor *disabledBgColor; /* 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_CustomOption stateOption = {
48 TkStateParseProc,
49 TkStatePrintProc, (ClientData) 2
50 };
51 static Tk_CustomOption tagsOption = {
52 Tk_CanvasTagsParseProc,
53 Tk_CanvasTagsPrintProc, (ClientData) NULL
54 };
55
56 static Tk_ConfigSpec configSpecs[] = {
57 {TK_CONFIG_COLOR, "-activebackground", (char *) NULL, (char *) NULL,
58 (char *) NULL, Tk_Offset(BitmapItem, activeBgColor), TK_CONFIG_NULL_OK},
59 {TK_CONFIG_BITMAP, "-activebitmap", (char *) NULL, (char *) NULL,
60 (char *) NULL, Tk_Offset(BitmapItem, activeBitmap), TK_CONFIG_NULL_OK},
61 {TK_CONFIG_COLOR, "-activeforeground", (char *) NULL, (char *) NULL,
62 (char *) NULL, Tk_Offset(BitmapItem, activeFgColor), TK_CONFIG_NULL_OK},
63 {TK_CONFIG_ANCHOR, "-anchor", (char *) NULL, (char *) NULL,
64 "center", Tk_Offset(BitmapItem, anchor), TK_CONFIG_DONT_SET_DEFAULT},
65 {TK_CONFIG_COLOR, "-background", (char *) NULL, (char *) NULL,
66 (char *) NULL, Tk_Offset(BitmapItem, bgColor), TK_CONFIG_NULL_OK},
67 {TK_CONFIG_BITMAP, "-bitmap", (char *) NULL, (char *) NULL,
68 (char *) NULL, Tk_Offset(BitmapItem, bitmap), TK_CONFIG_NULL_OK},
69 {TK_CONFIG_COLOR, "-disabledbackground", (char *) NULL, (char *) NULL,
70 (char *) NULL, Tk_Offset(BitmapItem, disabledBgColor),
71 TK_CONFIG_NULL_OK},
72 {TK_CONFIG_BITMAP, "-disabledbitmap", (char *) NULL, (char *) NULL,
73 (char *) NULL, Tk_Offset(BitmapItem, disabledBitmap),
74 TK_CONFIG_NULL_OK},
75 {TK_CONFIG_COLOR, "-disabledforeground", (char *) NULL, (char *) NULL,
76 (char *) NULL, Tk_Offset(BitmapItem, disabledFgColor),
77 TK_CONFIG_NULL_OK},
78 {TK_CONFIG_COLOR, "-foreground", (char *) NULL, (char *) NULL,
79 "black", Tk_Offset(BitmapItem, fgColor), 0},
80 {TK_CONFIG_CUSTOM, "-state", (char *) NULL, (char *) NULL,
81 (char *) NULL, Tk_Offset(Tk_Item, state), TK_CONFIG_NULL_OK,
82 &stateOption},
83 {TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,
84 (char *) NULL, 0, TK_CONFIG_NULL_OK, &tagsOption},
85 {TK_CONFIG_CALLBACK, "-updatecommand", (char *) NULL, (char *) NULL,
86 (char *) NULL, Tk_Offset(Tk_Item, updateCmd), TK_CONFIG_NULL_OK},
87 {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
88 (char *) NULL, 0, 0}
89 };
90
91 /*
92 * Prototypes for procedures defined in this file:
93 */
94
95 static int BitmapCoords _ANSI_ARGS_((Tcl_Interp *interp,
96 Tk_Canvas canvas, Tk_Item *itemPtr, int objc,
97 Tcl_Obj *CONST objv[]));
98 static int BitmapToArea _ANSI_ARGS_((Tk_Canvas canvas,
99 Tk_Item *itemPtr, double *rectPtr));
100 static double BitmapToPoint _ANSI_ARGS_((Tk_Canvas canvas,
101 Tk_Item *itemPtr, double *coordPtr));
102 static int BitmapToPostscript _ANSI_ARGS_((Tcl_Interp *interp,
103 Tk_Canvas canvas, Tk_Item *itemPtr, int prepass));
104 static void ComputeBitmapBbox _ANSI_ARGS_((Tk_Canvas canvas,
105 BitmapItem *bmapPtr));
106 static int ConfigureBitmap _ANSI_ARGS_((Tcl_Interp *interp,
107 Tk_Canvas canvas, Tk_Item *itemPtr, int objc,
108 Tcl_Obj *CONST objv[], int flags));
109 static int TkcCreateBitmap _ANSI_ARGS_((Tcl_Interp *interp,
110 Tk_Canvas canvas, struct Tk_Item *itemPtr,
111 int objc, Tcl_Obj *CONST objv[]));
112 static void DeleteBitmap _ANSI_ARGS_((Tk_Canvas canvas,
113 Tk_Item *itemPtr, Display *display));
114 static void DisplayBitmap _ANSI_ARGS_((Tk_Canvas canvas,
115 Tk_Item *itemPtr, Display *display, Drawable dst,
116 int x, int y, int width, int height));
117 static void ScaleBitmap _ANSI_ARGS_((Tk_Canvas canvas,
118 Tk_Item *itemPtr, double originX, double originY,
119 double scaleX, double scaleY));
120 static void TranslateBitmap _ANSI_ARGS_((Tk_Canvas canvas,
121 Tk_Item *itemPtr, double deltaX, double deltaY));
122
123 /*
124 * The structures below defines the bitmap item type in terms of
125 * procedures that can be invoked by generic item code.
126 */
127
128 Tk_ItemType tkBitmapType = {
129 "bitmap", /* name */
130 sizeof(BitmapItem), /* itemSize */
131 TkcCreateBitmap, /* createProc */
132 configSpecs, /* configSpecs */
133 ConfigureBitmap, /* configureProc */
134 BitmapCoords, /* coordProc */
135 DeleteBitmap, /* deleteProc */
136 DisplayBitmap, /* displayProc */
137 TK_CONFIG_OBJS, /* flags */
138 BitmapToPoint, /* pointProc */
139 BitmapToArea, /* areaProc */
140 BitmapToPostscript, /* postscriptProc */
141 ScaleBitmap, /* scaleProc */
142 TranslateBitmap, /* translateProc */
143 (Tk_ItemIndexProc *) NULL, /* indexProc */
144 (Tk_ItemCursorProc *) NULL, /* icursorProc */
145 (Tk_ItemSelectionProc *) NULL, /* selectionProc */
146 (Tk_ItemInsertProc *) NULL, /* insertProc */
147 (Tk_ItemDCharsProc *) NULL, /* dTextProc */
148 (Tk_ItemType *) NULL, /* nextPtr */
149 };
150
151 /*
152 *--------------------------------------------------------------
153 *
154 * TkcCreateBitmap --
155 *
156 * This procedure is invoked to create a new bitmap
157 * item in a canvas.
158 *
159 * Results:
160 * A standard Tcl return value. If an error occurred in
161 * creating the item, then an error message is left in
162 * the interp's result; in this case itemPtr is left uninitialized,
163 * so it can be safely freed by the caller.
164 *
165 * Side effects:
166 * A new bitmap item is created.
167 *
168 *--------------------------------------------------------------
169 */
170
171 static int
172 TkcCreateBitmap(interp, canvas, itemPtr, objc, objv)
173 Tcl_Interp *interp; /* Interpreter for error reporting. */
174 Tk_Canvas canvas; /* Canvas to hold new item. */
175 Tk_Item *itemPtr; /* Record to hold new item; header
176 * has been initialized by caller. */
177 int objc; /* Number of arguments in objv. */
178 Tcl_Obj *CONST objv[]; /* Arguments describing rectangle. */
179 {
180 BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
181 int i;
182
183 if (objc == 0) {
184 panic("canvas did not pass any coords\n");
185 }
186
187 /*
188 * Initialize item's record.
189 */
190
191 bmapPtr->anchor = TK_ANCHOR_CENTER;
192 bmapPtr->bitmap = None;
193 bmapPtr->activeBitmap = None;
194 bmapPtr->disabledBitmap = None;
195 bmapPtr->fgColor = NULL;
196 bmapPtr->activeFgColor = NULL;
197 bmapPtr->disabledFgColor = NULL;
198 bmapPtr->bgColor = NULL;
199 bmapPtr->activeBgColor = NULL;
200 bmapPtr->disabledBgColor = NULL;
201 bmapPtr->gc = None;
202
203 /*
204 * Process the arguments to fill in the item record.
205 * Only 1 (list) or 2 (x y) coords are allowed.
206 */
207
208 if (objc == 1) {
209 i = 1;
210 } else {
211 char *arg = Tcl_GetString(objv[1]);
212 i = 2;
213 if ((arg[0] == '-') && (arg[1] >= 'a') && (arg[1] <= 'z')) {
214 i = 1;
215 }
216 }
217 if (BitmapCoords(interp, canvas, itemPtr, i, objv) != TCL_OK) {
218 goto error;
219 }
220 if (ConfigureBitmap(interp, canvas, itemPtr, objc-i, objv+i, 0)
221 == TCL_OK) {
222 return TCL_OK;
223 }
224
225 error:
226 DeleteBitmap(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
227 return TCL_ERROR;
228 }
229
230 /*
231 *--------------------------------------------------------------
232 *
233 * BitmapCoords --
234 *
235 * This procedure is invoked to process the "coords" widget
236 * command on bitmap items. See the user documentation for
237 * details on what it does.
238 *
239 * Results:
240 * Returns TCL_OK or TCL_ERROR, and sets the interp's result.
241 *
242 * Side effects:
243 * The coordinates for the given item may be changed.
244 *
245 *--------------------------------------------------------------
246 */
247
248 static int
249 BitmapCoords(interp, canvas, itemPtr, objc, objv)
250 Tcl_Interp *interp; /* Used for error reporting. */
251 Tk_Canvas canvas; /* Canvas containing item. */
252 Tk_Item *itemPtr; /* Item whose coordinates are to be
253 * read or modified. */
254 int objc; /* Number of coordinates supplied in
255 * objv. */
256 Tcl_Obj *CONST objv[]; /* Array of coordinates: x1, y1,
257 * x2, y2, ... */
258 {
259 BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
260
261 if (objc == 0) {
262 Tcl_Obj *obj = Tcl_NewObj();
263 Tcl_Obj *subobj = Tcl_NewDoubleObj(bmapPtr->x);
264 Tcl_ListObjAppendElement(interp, obj, subobj);
265 subobj = Tcl_NewDoubleObj(bmapPtr->y);
266 Tcl_ListObjAppendElement(interp, obj, subobj);
267 Tcl_SetObjResult(interp, obj);
268 } else if (objc < 3) {
269 if (objc == 1) {
270 if (Tcl_ListObjGetElements(interp, objv[0], &objc,
271 (Tcl_Obj ***) &objv) != TCL_OK) {
272 return TCL_ERROR;
273 } else if (objc != 2) {
274 char buf[64 + TCL_INTEGER_SPACE];
275
276 sprintf(buf, "wrong # coordinates: expected 2, got %d", objc);
277 Tcl_SetResult(interp, buf, TCL_VOLATILE);
278 return TCL_ERROR;
279 }
280 }
281 if ((Tk_CanvasGetCoordFromObj(interp, canvas, objv[0],
282 &bmapPtr->x) != TCL_OK)
283 || (Tk_CanvasGetCoordFromObj(interp, canvas, objv[1],
284 &bmapPtr->y) != TCL_OK)) {
285 return TCL_ERROR;
286 }
287 ComputeBitmapBbox(canvas, bmapPtr);
288 } else {
289 char buf[64 + TCL_INTEGER_SPACE];
290
291 sprintf(buf, "wrong # coordinates: expected 0 or 2, got %d", objc);
292 Tcl_SetResult(interp, buf, TCL_VOLATILE);
293 return TCL_ERROR;
294 }
295 return TCL_OK;
296 }
297
298 /*
299 *--------------------------------------------------------------
300 *
301 * ConfigureBitmap --
302 *
303 * This procedure is invoked to configure various aspects
304 * of a bitmap item, such as its anchor position.
305 *
306 * Results:
307 * A standard Tcl result code. If an error occurs, then
308 * an error message is left in the interp's result.
309 *
310 * Side effects:
311 * Configuration information may be set for itemPtr.
312 *
313 *--------------------------------------------------------------
314 */
315
316 static int
317 ConfigureBitmap(interp, canvas, itemPtr, objc, objv, flags)
318 Tcl_Interp *interp; /* Used for error reporting. */
319 Tk_Canvas canvas; /* Canvas containing itemPtr. */
320 Tk_Item *itemPtr; /* Bitmap item to reconfigure. */
321 int objc; /* Number of elements in objv. */
322 Tcl_Obj *CONST objv[]; /* Arguments describing things to configure. */
323 int flags; /* Flags to pass to Tk_ConfigureWidget. */
324 {
325 BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
326 XGCValues gcValues;
327 GC newGC;
328 Tk_Window tkwin;
329 unsigned long mask;
330 XColor *fgColor;
331 XColor *bgColor;
332 Pixmap bitmap;
333 Tk_State state;
334 int width, height;
335 int width1, height1;
336
337 tkwin = Tk_CanvasTkwin(canvas);
338 if (TCL_OK != Tk_ConfigureWidget(interp, tkwin, configSpecs, objc,
339 objv, (char *) bmapPtr, flags|TK_CONFIG_OBJS)) {
340 return TCL_ERROR;
341 }
342
343 /*
344 * A few of the options require additional processing, such as those
345 * that determine the graphics context.
346 */
347
348 state = Tk_GetItemState(canvas, itemPtr);
349
350 if (bmapPtr->activeFgColor!=NULL ||
351 bmapPtr->activeBgColor!=NULL ||
352 bmapPtr->activeBitmap!=None) {
353 itemPtr->redraw_flags |= TK_ITEM_STATE_DEPENDANT;
354 } else {
355 itemPtr->redraw_flags &= ~TK_ITEM_STATE_DEPENDANT;
356 }
357
358 /* Check that bitmaps have same width and height. This avoids problems
359 due to wrong position relative to the anchor point, displaying only
360 part of one of the bitmaps, etc.
361 */
362 Tk_SizeOfBitmap(Tk_Display(Tk_CanvasTkwin(canvas)), bmapPtr->bitmap,
363 &width, &height);
364 if (bmapPtr->activeBitmap!=None) {
365 Tk_SizeOfBitmap(Tk_Display(Tk_CanvasTkwin(canvas)),
366 bmapPtr->activeBitmap,
367 &width1, &height1);
368 if (width != width1 || height != height1) {
369 Tcl_SetResult(interp, "active bitmap dimensions differ", TCL_STATIC);
370 return TCL_ERROR;
371 }
372 }
373 if (bmapPtr->disabledBitmap!=None) {
374 Tk_SizeOfBitmap(Tk_Display(Tk_CanvasTkwin(canvas)),
375 bmapPtr->disabledBitmap,
376 &width1, &height1);
377 if (width != width1 || height != height1) {
378 Tcl_SetResult(interp, "disabled bitmap dimensions differ", TCL_STATIC);
379 return TCL_ERROR;
380 }
381 }
382
383 if (state==TK_STATE_HIDDEN) {
384 ComputeBitmapBbox(canvas, bmapPtr);
385 return TCL_OK;
386 }
387 fgColor = bmapPtr->fgColor;
388 bgColor = bmapPtr->bgColor;
389 bitmap = bmapPtr->bitmap;
390 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
391 if (bmapPtr->activeFgColor!=NULL) {
392 fgColor = bmapPtr->activeFgColor;
393 }
394 if (bmapPtr->activeBgColor!=NULL) {
395 bgColor = bmapPtr->activeBgColor;
396 }
397 if (bmapPtr->activeBitmap!=None) {
398 bitmap = bmapPtr->activeBitmap;
399 }
400 } else if (state==TK_STATE_DISABLED) {
401 if (bmapPtr->disabledFgColor!=NULL) {
402 fgColor = bmapPtr->disabledFgColor;
403 }
404 if (bmapPtr->disabledBgColor!=NULL) {
405 bgColor = bmapPtr->disabledBgColor;
406 }
407 if (bmapPtr->disabledBitmap!=None) {
408 bitmap = bmapPtr->disabledBitmap;
409 }
410 }
411
412 if (bitmap == None) {
413 ComputeBitmapBbox(canvas, bmapPtr);
414 return TCL_OK;
415 }
416
417 gcValues.foreground = fgColor->pixel;
418 mask = GCForeground;
419 if (bgColor != NULL) {
420 gcValues.background = bgColor->pixel;
421 mask |= GCBackground;
422 } else {
423 gcValues.clip_mask = bitmap;
424 mask |= GCClipMask;
425 }
426 if (bitmap == None) {
427 newGC = None;
428 } else {
429 newGC = Tk_GetGC(tkwin, mask, &gcValues);
430 }
431 if (bmapPtr->gc != None) {
432 Tk_FreeGC(Tk_Display(tkwin), bmapPtr->gc);
433 }
434 bmapPtr->gc = newGC;
435
436 ComputeBitmapBbox(canvas, bmapPtr);
437
438 return TCL_OK;
439 }
440
441 /*
442 *--------------------------------------------------------------
443 *
444 * DeleteBitmap --
445 *
446 * This procedure is called to clean up the data structure
447 * associated with a bitmap item.
448 *
449 * Results:
450 * None.
451 *
452 * Side effects:
453 * Resources associated with itemPtr are released.
454 *
455 *--------------------------------------------------------------
456 */
457
458 static void
459 DeleteBitmap(canvas, itemPtr, display)
460 Tk_Canvas canvas; /* Info about overall canvas widget. */
461 Tk_Item *itemPtr; /* Item that is being deleted. */
462 Display *display; /* Display containing window for
463 * canvas. */
464 {
465 BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
466
467 if (bmapPtr->bitmap != None) {
468 Tk_FreeBitmap(display, bmapPtr->bitmap);
469 }
470 if (bmapPtr->activeBitmap != None) {
471 Tk_FreeBitmap(display, bmapPtr->activeBitmap);
472 }
473 if (bmapPtr->disabledBitmap != None) {
474 Tk_FreeBitmap(display, bmapPtr->disabledBitmap);
475 }
476 if (bmapPtr->fgColor != NULL) {
477 Tk_FreeColor(bmapPtr->fgColor);
478 }
479 if (bmapPtr->activeFgColor != NULL) {
480 Tk_FreeColor(bmapPtr->activeFgColor);
481 }
482 if (bmapPtr->disabledFgColor != NULL) {
483 Tk_FreeColor(bmapPtr->disabledFgColor);
484 }
485 if (bmapPtr->bgColor != NULL) {
486 Tk_FreeColor(bmapPtr->bgColor);
487 }
488 if (bmapPtr->activeBgColor != NULL) {
489 Tk_FreeColor(bmapPtr->activeBgColor);
490 }
491 if (bmapPtr->disabledBgColor != NULL) {
492 Tk_FreeColor(bmapPtr->disabledBgColor);
493 }
494 if (bmapPtr->gc != NULL) {
495 Tk_FreeGC(display, bmapPtr->gc);
496 }
497 }
498
499 /*
500 *--------------------------------------------------------------
501 *
502 * ComputeBitmapBbox --
503 *
504 * This procedure is invoked to compute the bounding box of
505 * all the pixels that may be drawn as part of a bitmap item.
506 * This procedure is where the child bitmap's placement is
507 * computed.
508 *
509 * Results:
510 * None.
511 *
512 * Side effects:
513 * The fields x1, y1, x2, and y2 are updated in the header
514 * for itemPtr.
515 *
516 *--------------------------------------------------------------
517 */
518
519 /* ARGSUSED */
520 static void
521 ComputeBitmapBbox(canvas, bmapPtr)
522 Tk_Canvas canvas; /* Canvas that contains item. */
523 BitmapItem *bmapPtr; /* Item whose bbox is to be
524 * recomputed. */
525 {
526 int width, height;
527 int x, y;
528 Pixmap bitmap;
529 Tk_State state = Tk_GetItemState(canvas, &bmapPtr->header);
530
531 bitmap = bmapPtr->bitmap;
532 if (((TkCanvas *)canvas)->currentItemPtr == (Tk_Item *)bmapPtr) {
533 if (bmapPtr->activeBitmap!=None) {
534 bitmap = bmapPtr->activeBitmap;
535 }
536 } else if (state==TK_STATE_DISABLED) {
537 if (bmapPtr->disabledBitmap!=None) {
538 bitmap = bmapPtr->disabledBitmap;
539 }
540 }
541
542 x = (int) (bmapPtr->x + ((bmapPtr->x >= 0) ? 0.5 : - 0.5));
543 y = (int) (bmapPtr->y + ((bmapPtr->y >= 0) ? 0.5 : - 0.5));
544
545 if (state==TK_STATE_HIDDEN || bitmap == None) {
546 bmapPtr->header.x1 = bmapPtr->header.x2 = x;
547 bmapPtr->header.y1 = bmapPtr->header.y2 = y;
548 return;
549 }
550
551 /*
552 * Compute location and size of bitmap, using anchor information.
553 */
554
555 Tk_SizeOfBitmap(Tk_Display(Tk_CanvasTkwin(canvas)), bmapPtr->bitmap,
556 &width, &height);
557 switch (bmapPtr->anchor) {
558 case TK_ANCHOR_N:
559 x -= width/2;
560 break;
561 case TK_ANCHOR_NE:
562 x -= width;
563 break;
564 case TK_ANCHOR_E:
565 x -= width;
566 y -= height/2;
567 break;
568 case TK_ANCHOR_SE:
569 x -= width;
570 y -= height;
571 break;
572 case TK_ANCHOR_S:
573 x -= width/2;
574 y -= height;
575 break;
576 case TK_ANCHOR_SW:
577 y -= height;
578 break;
579 case TK_ANCHOR_W:
580 y -= height/2;
581 break;
582 case TK_ANCHOR_NW:
583 break;
584 case TK_ANCHOR_CENTER:
585 x -= width/2;
586 y -= height/2;
587 break;
588 }
589
590 /*
591 * Store the information in the item header.
592 */
593
594 bmapPtr->header.x1 = x;
595 bmapPtr->header.y1 = y;
596 bmapPtr->header.x2 = x + width;
597 bmapPtr->header.y2 = y + height;
598 }
599
600 /*
601 *--------------------------------------------------------------
602 *
603 * DisplayBitmap --
604 *
605 * This procedure is invoked to draw a bitmap item in a given
606 * drawable.
607 *
608 * Results:
609 * None.
610 *
611 * Side effects:
612 * ItemPtr is drawn in drawable using the transformation
613 * information in canvas.
614 *
615 *--------------------------------------------------------------
616 */
617
618 static void
619 DisplayBitmap(canvas, itemPtr, display, drawable, x, y, width, height)
620 Tk_Canvas canvas; /* Canvas that contains item. */
621 Tk_Item *itemPtr; /* Item to be displayed. */
622 Display *display; /* Display on which to draw item. */
623 Drawable drawable; /* Pixmap or window in which to draw
624 * item. */
625 int x, y, width, height; /* Describes region of canvas that
626 * must be redisplayed (not used). */
627 {
628 BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
629 int bmapX, bmapY, bmapWidth, bmapHeight;
630 short drawableX, drawableY;
631 XColor *fgColor;
632 XColor *bgColor;
633 Pixmap bitmap;
634 Tk_State state = Tk_GetItemState(canvas, itemPtr);
635
636 /*
637 * If the area being displayed doesn't cover the whole bitmap,
638 * then only redisplay the part of the bitmap that needs
639 * redisplay.
640 */
641
642 fgColor = bmapPtr->fgColor;
643 bgColor = bmapPtr->bgColor;
644 bitmap = bmapPtr->bitmap;
645 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
646 if (bmapPtr->activeFgColor!=NULL) {
647 fgColor = bmapPtr->activeFgColor;
648 }
649 if (bmapPtr->activeBgColor!=NULL) {
650 bgColor = bmapPtr->activeBgColor;
651 }
652 if (bmapPtr->activeBitmap!=None) {
653 bitmap = bmapPtr->activeBitmap;
654 }
655 } else if (state==TK_STATE_DISABLED) {
656 if (bmapPtr->disabledFgColor!=NULL) {
657 fgColor = bmapPtr->disabledFgColor;
658 }
659 if (bmapPtr->disabledBgColor!=NULL) {
660 bgColor = bmapPtr->disabledBgColor;
661 }
662 if (bmapPtr->disabledBitmap!=None) {
663 bitmap = bmapPtr->disabledBitmap;
664 }
665 }
666
667 if (bitmap != None) {
668 if (x > bmapPtr->header.x1) {
669 bmapX = x - bmapPtr->header.x1;
670 bmapWidth = bmapPtr->header.x2 - x;
671 } else {
672 bmapX = 0;
673 if ((x+width) < bmapPtr->header.x2) {
674 bmapWidth = x + width - bmapPtr->header.x1;
675 } else {
676 bmapWidth = bmapPtr->header.x2 - bmapPtr->header.x1;
677 }
678 }
679 if (y > bmapPtr->header.y1) {
680 bmapY = y - bmapPtr->header.y1;
681 bmapHeight = bmapPtr->header.y2 - y;
682 } else {
683 bmapY = 0;
684 if ((y+height) < bmapPtr->header.y2) {
685 bmapHeight = y + height - bmapPtr->header.y1;
686 } else {
687 bmapHeight = bmapPtr->header.y2 - bmapPtr->header.y1;
688 }
689 }
690 Tk_CanvasDrawableCoords(canvas,
691 (double) (bmapPtr->header.x1 + bmapX),
692 (double) (bmapPtr->header.y1 + bmapY),
693 &drawableX, &drawableY);
694
695 /*
696 * Must modify the mask origin within the graphics context
697 * to line up with the bitmap's origin (in order to make
698 * bitmaps with "-background {}" work right).
699 */
700
701 XSetClipOrigin(display, bmapPtr->gc, drawableX - bmapX,
702 drawableY - bmapY);
703 XCopyPlane(display, bitmap, drawable,
704 bmapPtr->gc, bmapX, bmapY, (unsigned int) bmapWidth,
705 (unsigned int) bmapHeight, drawableX, drawableY, 1);
706 XSetClipOrigin(display, bmapPtr->gc, 0, 0);
707 }
708 }
709
710 /*
711 *--------------------------------------------------------------
712 *
713 * BitmapToPoint --
714 *
715 * Computes the distance from a given point to a given
716 * rectangle, in canvas units.
717 *
718 * Results:
719 * The return value is 0 if the point whose x and y coordinates
720 * are coordPtr[0] and coordPtr[1] is inside the bitmap. If the
721 * point isn't inside the bitmap then the return value is the
722 * distance from the point to the bitmap.
723 *
724 * Side effects:
725 * None.
726 *
727 *--------------------------------------------------------------
728 */
729
730 /* ARGSUSED */
731 static double
732 BitmapToPoint(canvas, itemPtr, coordPtr)
733 Tk_Canvas canvas; /* Canvas containing item. */
734 Tk_Item *itemPtr; /* Item to check against point. */
735 double *coordPtr; /* Pointer to x and y coordinates. */
736 {
737 BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
738 double x1, x2, y1, y2, xDiff, yDiff;
739
740 x1 = bmapPtr->header.x1;
741 y1 = bmapPtr->header.y1;
742 x2 = bmapPtr->header.x2;
743 y2 = bmapPtr->header.y2;
744
745 /*
746 * Point is outside rectangle.
747 */
748
749 if (coordPtr[0] < x1) {
750 xDiff = x1 - coordPtr[0];
751 } else if (coordPtr[0] > x2) {
752 xDiff = coordPtr[0] - x2;
753 } else {
754 xDiff = 0;
755 }
756
757 if (coordPtr[1] < y1) {
758 yDiff = y1 - coordPtr[1];
759 } else if (coordPtr[1] > y2) {
760 yDiff = coordPtr[1] - y2;
761 } else {
762 yDiff = 0;
763 }
764
765 return hypot(xDiff, yDiff);
766 }
767
768 /*
769 *--------------------------------------------------------------
770 *
771 * BitmapToArea --
772 *
773 * This procedure is called to determine whether an item
774 * lies entirely inside, entirely outside, or overlapping
775 * a given rectangle.
776 *
777 * Results:
778 * -1 is returned if the item is entirely outside the area
779 * given by rectPtr, 0 if it overlaps, and 1 if it is entirely
780 * inside the given area.
781 *
782 * Side effects:
783 * None.
784 *
785 *--------------------------------------------------------------
786 */
787
788 /* ARGSUSED */
789 static int
790 BitmapToArea(canvas, itemPtr, rectPtr)
791 Tk_Canvas canvas; /* Canvas containing item. */
792 Tk_Item *itemPtr; /* Item to check against rectangle. */
793 double *rectPtr; /* Pointer to array of four coordinates
794 * (x1, y1, x2, y2) describing rectangular
795 * area. */
796 {
797 BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
798
799 if ((rectPtr[2] <= bmapPtr->header.x1)
800 || (rectPtr[0] >= bmapPtr->header.x2)
801 || (rectPtr[3] <= bmapPtr->header.y1)
802 || (rectPtr[1] >= bmapPtr->header.y2)) {
803 return -1;
804 }
805 if ((rectPtr[0] <= bmapPtr->header.x1)
806 && (rectPtr[1] <= bmapPtr->header.y1)
807 && (rectPtr[2] >= bmapPtr->header.x2)
808 && (rectPtr[3] >= bmapPtr->header.y2)) {
809 return 1;
810 }
811 return 0;
812 }
813
814 /*
815 *--------------------------------------------------------------
816 *
817 * ScaleBitmap --
818 *
819 * This procedure is invoked to rescale a bitmap item in a
820 * canvas. It is one of the standard item procedures for
821 * bitmap items, and is invoked by the generic canvas code.
822 *
823 * Results:
824 * None.
825 *
826 * Side effects:
827 * The item referred to by itemPtr is rescaled so that the
828 * following transformation is applied to all point coordinates:
829 * x' = originX + scaleX*(x-originX)
830 * y' = originY + scaleY*(y-originY)
831 *
832 *--------------------------------------------------------------
833 */
834
835 static void
836 ScaleBitmap(canvas, itemPtr, originX, originY, scaleX, scaleY)
837 Tk_Canvas canvas; /* Canvas containing rectangle. */
838 Tk_Item *itemPtr; /* Rectangle to be scaled. */
839 double originX, originY; /* Origin about which to scale item. */
840 double scaleX; /* Amount to scale in X direction. */
841 double scaleY; /* Amount to scale in Y direction. */
842 {
843 BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
844
845 bmapPtr->x = originX + scaleX*(bmapPtr->x - originX);
846 bmapPtr->y = originY + scaleY*(bmapPtr->y - originY);
847 ComputeBitmapBbox(canvas, bmapPtr);
848 }
849
850 /*
851 *--------------------------------------------------------------
852 *
853 * TranslateBitmap --
854 *
855 * This procedure is called to move an item by a given amount.
856 *
857 * Results:
858 * None.
859 *
860 * Side effects:
861 * The position of the item is offset by (xDelta, yDelta), and
862 * the bounding box is updated in the generic part of the item
863 * structure.
864 *
865 *--------------------------------------------------------------
866 */
867
868 static void
869 TranslateBitmap(canvas, itemPtr, deltaX, deltaY)
870 Tk_Canvas canvas; /* Canvas containing item. */
871 Tk_Item *itemPtr; /* Item that is being moved. */
872 double deltaX, deltaY; /* Amount by which item is to be
873 * moved. */
874 {
875 BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
876
877 bmapPtr->x += deltaX;
878 bmapPtr->y += deltaY;
879 ComputeBitmapBbox(canvas, bmapPtr);
880 }
881
882 /*
883 *--------------------------------------------------------------
884 *
885 * BitmapToPostscript --
886 *
887 * This procedure is called to generate Postscript for
888 * bitmap items.
889 *
890 * Results:
891 * The return value is a standard Tcl result. If an error
892 * occurs in generating Postscript then an error message is
893 * left in the interp's result, replacing whatever used to be there.
894 * If no error occurs, then Postscript for the item is appended
895 * to the result.
896 *
897 * Side effects:
898 * None.
899 *
900 *--------------------------------------------------------------
901 */
902
903 static int
904 BitmapToPostscript(interp, canvas, itemPtr, prepass)
905 Tcl_Interp *interp; /* Leave Postscript or error message
906 * here. */
907 Tk_Canvas canvas; /* Information about overall canvas. */
908 Tk_Item *itemPtr; /* Item for which Postscript is
909 * wanted. */
910 int prepass; /* 1 means this is a prepass to
911 * collect font information; 0 means
912 * final Postscript is being created. */
913 {
914 BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
915 double x, y;
916 int width, height, rowsAtOnce, rowsThisTime;
917 int curRow;
918 char buffer[100 + TCL_DOUBLE_SPACE * 2 + TCL_INTEGER_SPACE * 4];
919
920 if (bmapPtr->bitmap == None) {
921 return TCL_OK;
922 }
923
924 /*
925 * Compute the coordinates of the lower-left corner of the bitmap,
926 * taking into account the anchor position for the bitmp.
927 */
928
929 x = bmapPtr->x;
930 y = Tk_CanvasPsY(canvas, bmapPtr->y);
931 Tk_SizeOfBitmap(Tk_Display(Tk_CanvasTkwin(canvas)), bmapPtr->bitmap,
932 &width, &height);
933 switch (bmapPtr->anchor) {
934 case TK_ANCHOR_NW: y -= height; break;
935 case TK_ANCHOR_N: x -= width/2.0; y -= height; break;
936 case TK_ANCHOR_NE: x -= width; y -= height; break;
937 case TK_ANCHOR_E: x -= width; y -= height/2.0; break;
938 case TK_ANCHOR_SE: x -= width; break;
939 case TK_ANCHOR_S: x -= width/2.0; break;
940 case TK_ANCHOR_SW: break;
941 case TK_ANCHOR_W: y -= height/2.0; break;
942 case TK_ANCHOR_CENTER: x -= width/2.0; y -= height/2.0; break;
943 }
944
945 /*
946 * Color the background, if there is one.
947 */
948
949 if (bmapPtr->bgColor != NULL) {
950 sprintf(buffer,
951 "%.15g %.15g moveto %d 0 rlineto 0 %d rlineto %d %s\n",
952 x, y, width, height, -width, "0 rlineto closepath");
953 Tcl_AppendResult(interp, buffer, (char *) NULL);
954 if (Tk_CanvasPsColor(interp, canvas, bmapPtr->bgColor) != TCL_OK) {
955 return TCL_ERROR;
956 }
957 Tcl_AppendResult(interp, "fill\n", (char *) NULL);
958 }
959
960 /*
961 * Draw the bitmap, if there is a foreground color. If the bitmap
962 * is very large, then chop it up into multiple bitmaps, each
963 * consisting of one or more rows. This is needed because Postscript
964 * can't handle single strings longer than 64 KBytes long.
965 */
966
967 if (bmapPtr->fgColor != NULL) {
968 if (Tk_CanvasPsColor(interp, canvas, bmapPtr->fgColor) != TCL_OK) {
969 return TCL_ERROR;
970 }
971 if (width > 60000) {
972 Tcl_ResetResult(interp);
973 Tcl_AppendResult(interp, "can't generate Postscript",
974 " for bitmaps more than 60000 pixels wide",
975 (char *) NULL);
976 return TCL_ERROR;
977 }
978 rowsAtOnce = 60000/width;
979 if (rowsAtOnce < 1) {
980 rowsAtOnce = 1;
981 }
982 sprintf(buffer, "%.15g %.15g translate\n", x, y+height);
983 Tcl_AppendResult(interp, buffer, (char *) NULL);
984 for (curRow = 0; curRow < height; curRow += rowsAtOnce) {
985 rowsThisTime = rowsAtOnce;
986 if (rowsThisTime > (height - curRow)) {
987 rowsThisTime = height - curRow;
988 }
989 sprintf(buffer, "0 -%.15g translate\n%d %d true matrix {\n",
990 (double) rowsThisTime, width, rowsThisTime);
991 Tcl_AppendResult(interp, buffer, (char *) NULL);
992 if (Tk_CanvasPsBitmap(interp, canvas, bmapPtr->bitmap,
993 0, curRow, width, rowsThisTime) != TCL_OK) {
994 return TCL_ERROR;
995 }
996 Tcl_AppendResult(interp, "\n} imagemask\n", (char *) NULL);
997 }
998 }
999 return TCL_OK;
1000 }
1001