/*1* tkCanvUtil.c --2*3* This procedure contains a collection of utility procedures4* used by the implementations of various canvas item types.5*6* Copyright (c) 1994 Sun Microsystems, Inc.7* Copyright (c) 1994 Sun Microsystems, Inc.8*9* See the file "license.terms" for information on usage and redistribution10* of this file, and for a DISCLAIMER OF ALL WARRANTIES.11*12* SCCS: @(#) tkCanvUtil.c 1.6 96/02/15 18:53:1013*/1415#include "tkInt.h"16#include "tkCanvas.h"171819/*20*----------------------------------------------------------------------21*22* Tk_CanvasTkwin --23*24* Given a token for a canvas, this procedure returns the25* widget that represents the canvas.26*27* Results:28* The return value is a handle for the widget.29*30* Side effects:31* None.32*33*----------------------------------------------------------------------34*/3536Tk_Window37Tk_CanvasTkwin(canvas)38Tk_Canvas canvas; /* Token for the canvas. */39{40TkCanvas *canvasPtr = (TkCanvas *) canvas;41return canvasPtr->tkwin;42}4344/*45*----------------------------------------------------------------------46*47* Tk_CanvasDrawableCoords --48*49* Given an (x,y) coordinate pair within a canvas, this procedure50* returns the corresponding coordinates at which the point should51* be drawn in the drawable used for display.52*53* Results:54* There is no return value. The values at *drawableXPtr and55* *drawableYPtr are filled in with the coordinates at which56* x and y should be drawn. These coordinates are clipped57* to fit within a "short", since this is what X uses in58* most cases for drawing.59*60* Side effects:61* None.62*63*----------------------------------------------------------------------64*/6566void67Tk_CanvasDrawableCoords(canvas, x, y, drawableXPtr, drawableYPtr)68Tk_Canvas canvas; /* Token for the canvas. */69double x, y; /* Coordinates in canvas space. */70short *drawableXPtr, *drawableYPtr; /* Screen coordinates are stored71* here. */72{73TkCanvas *canvasPtr = (TkCanvas *) canvas;74double tmp;7576tmp = x - canvasPtr->drawableXOrigin;77if (tmp > 0) {78tmp += 0.5;79} else {80tmp -= 0.5;81}82if (tmp > 32767) {83*drawableXPtr = 32767;84} else if (tmp < -32768) {85*drawableXPtr = -32768;86} else {87*drawableXPtr = tmp;88}8990tmp = y - canvasPtr->drawableYOrigin;91if (tmp > 0) {92tmp += 0.5;93} else {94tmp -= 0.5;95}96if (tmp > 32767) {97*drawableYPtr = 32767;98} else if (tmp < -32768) {99*drawableYPtr = -32768;100} else {101*drawableYPtr = tmp;102}103}104105/*106*----------------------------------------------------------------------107*108* Tk_CanvasWindowCoords --109*110* Given an (x,y) coordinate pair within a canvas, this procedure111* returns the corresponding coordinates in the canvas's window.112*113* Results:114* There is no return value. The values at *screenXPtr and115* *screenYPtr are filled in with the coordinates at which116* (x,y) appears in the canvas's window. These coordinates117* are clipped to fit within a "short", since this is what X118* uses in most cases for drawing.119*120* Side effects:121* None.122*123*----------------------------------------------------------------------124*/125126void127Tk_CanvasWindowCoords(canvas, x, y, screenXPtr, screenYPtr)128Tk_Canvas canvas; /* Token for the canvas. */129double x, y; /* Coordinates in canvas space. */130short *screenXPtr, *screenYPtr; /* Screen coordinates are stored131* here. */132{133TkCanvas *canvasPtr = (TkCanvas *) canvas;134double tmp;135136tmp = x - canvasPtr->xOrigin;137if (tmp > 0) {138tmp += 0.5;139} else {140tmp -= 0.5;141}142if (tmp > 32767) {143*screenXPtr = 32767;144} else if (tmp < -32768) {145*screenXPtr = -32768;146} else {147*screenXPtr = tmp;148}149150tmp = y - canvasPtr->yOrigin;151if (tmp > 0) {152tmp += 0.5;153} else {154tmp -= 0.5;155}156if (tmp > 32767) {157*screenYPtr = 32767;158} else if (tmp < -32768) {159*screenYPtr = -32768;160} else {161*screenYPtr = tmp;162}163}164165/*166*--------------------------------------------------------------167*168* Tk_CanvasGetCoord --169*170* Given a string, returns a floating-point canvas coordinate171* corresponding to that string.172*173* Results:174* The return value is a standard Tcl return result. If175* TCL_OK is returned, then everything went well and the176* canvas coordinate is stored at *doublePtr; otherwise177* TCL_ERROR is returned and an error message is left in178* interp->result.179*180* Side effects:181* None.182*183*--------------------------------------------------------------184*/185186int187Tk_CanvasGetCoord(interp, canvas, string, doublePtr)188Tcl_Interp *interp; /* Interpreter for error reporting. */189Tk_Canvas canvas; /* Canvas to which coordinate applies. */190char *string; /* Describes coordinate (any screen191* coordinate form may be used here). */192double *doublePtr; /* Place to store converted coordinate. */193{194TkCanvas *canvasPtr = (TkCanvas *) canvas;195if (Tk_GetScreenMM(canvasPtr->interp, canvasPtr->tkwin, string,196doublePtr) != TCL_OK) {197return TCL_ERROR;198}199*doublePtr *= canvasPtr->pixelsPerMM;200return TCL_OK;201}202203/*204*----------------------------------------------------------------------205*206* Tk_CanvasSetStippleOrigin --207*208* This procedure sets the stipple origin in a graphics context209* so that stipples drawn with the GC will line up with other210* stipples previously drawn in the canvas.211*212* Results:213* None.214*215* Side effects:216* The graphics context is modified.217*218*----------------------------------------------------------------------219*/220221void222Tk_CanvasSetStippleOrigin(canvas, gc)223Tk_Canvas canvas; /* Token for a canvas. */224GC gc; /* Graphics context that is about to be225* used to draw a stippled pattern as226* part of redisplaying the canvas. */227228{229TkCanvas *canvasPtr = (TkCanvas *) canvas;230231XSetTSOrigin(canvasPtr->display, gc, -canvasPtr->drawableXOrigin,232-canvasPtr->drawableYOrigin);233}234235/*236*----------------------------------------------------------------------237*238* Tk_CanvasGetTextInfo --239*240* This procedure returns a pointer to a structure containing241* information about the selection and insertion cursor for242* a canvas widget. Items such as text items save the pointer243* and use it to share access to the information with the generic244* canvas code.245*246* Results:247* The return value is a pointer to the structure holding text248* information for the canvas. Most of the fields should not249* be modified outside the generic canvas code; see the user250* documentation for details.251*252* Side effects:253* None.254*255*----------------------------------------------------------------------256*/257258Tk_CanvasTextInfo *259Tk_CanvasGetTextInfo(canvas)260Tk_Canvas canvas; /* Token for the canvas widget. */261{262return &((TkCanvas *) canvas)->textInfo;263}264265/*266*--------------------------------------------------------------267*268* Tk_CanvasTagsParseProc --269*270* This procedure is invoked during option processing to handle271* "-tags" options for canvas items.272*273* Results:274* A standard Tcl return value.275*276* Side effects:277* The tags for a given item get replaced by those indicated278* in the value argument.279*280*--------------------------------------------------------------281*/282283int284Tk_CanvasTagsParseProc(clientData, interp, tkwin, value, widgRec, offset)285ClientData clientData; /* Not used.*/286Tcl_Interp *interp; /* Used for reporting errors. */287Tk_Window tkwin; /* Window containing canvas widget. */288char *value; /* Value of option (list of tag289* names). */290char *widgRec; /* Pointer to record for item. */291int offset; /* Offset into item (ignored). */292{293register Tk_Item *itemPtr = (Tk_Item *) widgRec;294int argc, i;295char **argv;296Tk_Uid *newPtr;297298/*299* Break the value up into the individual tag names.300*/301302if (Tcl_SplitList(interp, value, &argc, &argv) != TCL_OK) {303return TCL_ERROR;304}305306/*307* Make sure that there's enough space in the item to hold the308* tag names.309*/310311if (itemPtr->tagSpace < argc) {312newPtr = (Tk_Uid *) ckalloc((unsigned) (argc * sizeof(Tk_Uid)));313for (i = itemPtr->numTags-1; i >= 0; i--) {314newPtr[i] = itemPtr->tagPtr[i];315}316if (itemPtr->tagPtr != itemPtr->staticTagSpace) {317ckfree((char *) itemPtr->tagPtr);318}319itemPtr->tagPtr = newPtr;320itemPtr->tagSpace = argc;321}322itemPtr->numTags = argc;323for (i = 0; i < argc; i++) {324itemPtr->tagPtr[i] = Tk_GetUid(argv[i]);325}326ckfree((char *) argv);327return TCL_OK;328}329330/*331*--------------------------------------------------------------332*333* Tk_CanvasTagsPrintProc --334*335* This procedure is invoked by the Tk configuration code336* to produce a printable string for the "-tags" configuration337* option for canvas items.338*339* Results:340* The return value is a string describing all the tags for341* the item referred to by "widgRec". In addition, *freeProcPtr342* is filled in with the address of a procedure to call to free343* the result string when it's no longer needed (or NULL to344* indicate that the string doesn't need to be freed).345*346* Side effects:347* None.348*349*--------------------------------------------------------------350*/351352char *353Tk_CanvasTagsPrintProc(clientData, tkwin, widgRec, offset, freeProcPtr)354ClientData clientData; /* Ignored. */355Tk_Window tkwin; /* Window containing canvas widget. */356char *widgRec; /* Pointer to record for item. */357int offset; /* Ignored. */358Tcl_FreeProc **freeProcPtr; /* Pointer to variable to fill in with359* information about how to reclaim360* storage for return string. */361{362register Tk_Item *itemPtr = (Tk_Item *) widgRec;363364if (itemPtr->numTags == 0) {365*freeProcPtr = (Tcl_FreeProc *) NULL;366return "";367}368if (itemPtr->numTags == 1) {369*freeProcPtr = (Tcl_FreeProc *) NULL;370return (char *) itemPtr->tagPtr[0];371}372*freeProcPtr = TCL_DYNAMIC;373return Tcl_Merge(itemPtr->numTags, (char **) itemPtr->tagPtr);374}375376377