/*1* tk3d.c --2*3* This module provides procedures to draw borders in4* the three-dimensional Motif style.5*6* Copyright (c) 1990-1994 The Regents of the University of California.7* Copyright (c) 1994-1995 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: @(#) tk3d.c 1.53 96/06/27 08:15:3513*/1415#include "tkInt.h"1617/*18* One of the following data structures is allocated for19* each 3-D border currently in use. Structures of this20* type are indexed by borderTable, so that a single21* structure can be shared for several uses.22*/2324typedef struct {25Screen *screen; /* Screen on which the border will be used. */26Visual *visual; /* Visual for all windows and pixmaps using27* the border. */28int depth; /* Number of bits per pixel of drawables where29* the border will be used. */30Colormap colormap; /* Colormap out of which pixels are31* allocated. */32int refCount; /* Number of different users of33* this border. */34XColor *bgColorPtr; /* Background color (intensity35* between lightColorPtr and36* darkColorPtr). */37XColor *darkColorPtr; /* Color for darker areas (must free when38* deleting structure). NULL means shadows39* haven't been allocated yet.*/40XColor *lightColorPtr; /* Color used for lighter areas of border41* (must free this when deleting structure).42* NULL means shadows haven't been allocated43* yet. */44Pixmap shadow; /* Stipple pattern to use for drawing45* shadows areas. Used for displays with46* <= 64 colors or where colormap has filled47* up. */48GC bgGC; /* Used (if necessary) to draw areas in49* the background color. */50GC darkGC; /* Used to draw darker parts of the51* border. None means the shadow colors52* haven't been allocated yet.*/53GC lightGC; /* Used to draw lighter parts of54* the border. None means the shadow colors55* haven't been allocated yet. */56Tcl_HashEntry *hashPtr; /* Entry in borderTable (needed in57* order to delete structure). */58} Border;5960/*61* Hash table to map from a border's values (color, etc.) to a62* Border structure for those values.63*/6465static Tcl_HashTable borderTable;66typedef struct {67Tk_Uid colorName; /* Color for border. */68Colormap colormap; /* Colormap used for allocating border69* colors. */70Screen *screen; /* Screen on which border will be drawn. */71} BorderKey;7273/*74* Maximum intensity for a color:75*/7677#define MAX_INTENSITY 65535787980static int initialized = 0; /* 0 means static structures haven't81* been initialized yet. */8283/*84* Forward declarations for procedures defined in this file:85*/8687static void BorderInit _ANSI_ARGS_((void));88static void GetShadows _ANSI_ARGS_((Border *borderPtr,89Tk_Window tkwin));90static int Intersect _ANSI_ARGS_((XPoint *a1Ptr, XPoint *a2Ptr,91XPoint *b1Ptr, XPoint *b2Ptr, XPoint *iPtr));92static void ShiftLine _ANSI_ARGS_((XPoint *p1Ptr, XPoint *p2Ptr,93int distance, XPoint *p3Ptr));9495/*96*--------------------------------------------------------------97*98* Tk_Get3DBorder --99*100* Create a data structure for displaying a 3-D border.101*102* Results:103* The return value is a token for a data structure104* describing a 3-D border. This token may be passed105* to Tk_Draw3DRectangle and Tk_Free3DBorder. If an106* error prevented the border from being created then107* NULL is returned and an error message will be left108* in interp->result.109*110* Side effects:111* Data structures, graphics contexts, etc. are allocated.112* It is the caller's responsibility to eventually call113* Tk_Free3DBorder to release the resources.114*115*--------------------------------------------------------------116*/117118Tk_3DBorder119Tk_Get3DBorder(interp, tkwin, colorName)120Tcl_Interp *interp; /* Place to store an error message. */121Tk_Window tkwin; /* Token for window in which border will122* be drawn. */123Tk_Uid colorName; /* String giving name of color124* for window background. */125{126BorderKey key;127Tcl_HashEntry *hashPtr;128register Border *borderPtr;129int new;130XGCValues gcValues;131132if (!initialized) {133BorderInit();134}135136/*137* First, check to see if there's already a border that will work138* for this request.139*/140141key.colorName = colorName;142key.colormap = Tk_Colormap(tkwin);143key.screen = Tk_Screen(tkwin);144145hashPtr = Tcl_CreateHashEntry(&borderTable, (char *) &key, &new);146if (!new) {147borderPtr = (Border *) Tcl_GetHashValue(hashPtr);148borderPtr->refCount++;149} else {150151/*152* No satisfactory border exists yet. Initialize a new one.153*/154155borderPtr = (Border *) ckalloc(sizeof(Border));156borderPtr->screen = Tk_Screen(tkwin);157borderPtr->visual = Tk_Visual(tkwin);158borderPtr->depth = Tk_Depth(tkwin);159borderPtr->colormap = key.colormap;160borderPtr->refCount = 1;161borderPtr->bgColorPtr = NULL;162borderPtr->darkColorPtr = NULL;163borderPtr->lightColorPtr = NULL;164borderPtr->shadow = None;165borderPtr->bgGC = None;166borderPtr->darkGC = None;167borderPtr->lightGC = None;168borderPtr->hashPtr = hashPtr;169Tcl_SetHashValue(hashPtr, borderPtr);170171/*172* Create the information for displaying the background color,173* but delay the allocation of shadows until they are actually174* needed for drawing.175*/176177borderPtr->bgColorPtr = Tk_GetColor(interp, tkwin, colorName);178if (borderPtr->bgColorPtr == NULL) {179goto error;180}181gcValues.foreground = borderPtr->bgColorPtr->pixel;182borderPtr->bgGC = Tk_GetGC(tkwin, GCForeground, &gcValues);183}184return (Tk_3DBorder) borderPtr;185186error:187Tk_Free3DBorder((Tk_3DBorder) borderPtr);188return NULL;189}190191/*192*--------------------------------------------------------------193*194* Tk_3DVerticalBevel --195*196* This procedure draws a vertical bevel along one side of197* an object. The bevel is always rectangular in shape:198* |||199* |||200* |||201* |||202* |||203* |||204* An appropriate shadow color is chosen for the bevel based205* on the leftBevel and relief arguments. Normally this206* procedure is called first, then Tk_3DHorizontalBevel is207* called next to draw neat corners.208*209* Results:210* None.211*212* Side effects:213* Graphics are drawn in drawable.214*215*--------------------------------------------------------------216*/217218void219Tk_3DVerticalBevel(tkwin, drawable, border, x, y, width, height,220leftBevel, relief)221Tk_Window tkwin; /* Window for which border was allocated. */222Drawable drawable; /* X window or pixmap in which to draw. */223Tk_3DBorder border; /* Token for border to draw. */224int x, y, width, height; /* Area of vertical bevel. */225int leftBevel; /* Non-zero means this bevel forms the226* left side of the object; 0 means it227* forms the right side. */228int relief; /* Kind of bevel to draw. For example,229* TK_RELIEF_RAISED means interior of230* object should appear higher than231* exterior. */232{233Border *borderPtr = (Border *) border;234GC left, right;235Display *display = Tk_Display(tkwin);236237if ((borderPtr->lightGC == None) && (relief != TK_RELIEF_FLAT)) {238GetShadows(borderPtr, tkwin);239}240if (relief == TK_RELIEF_RAISED) {241XFillRectangle(display, drawable,242(leftBevel) ? borderPtr->lightGC : borderPtr->darkGC,243x, y, (unsigned) width, (unsigned) height);244} else if (relief == TK_RELIEF_SUNKEN) {245XFillRectangle(display, drawable,246(leftBevel) ? borderPtr->darkGC : borderPtr->lightGC,247x, y, (unsigned) width, (unsigned) height);248} else if (relief == TK_RELIEF_RIDGE) {249int half;250251left = borderPtr->lightGC;252right = borderPtr->darkGC;253ridgeGroove:254half = width/2;255if (!leftBevel && (width & 1)) {256half++;257}258XFillRectangle(display, drawable, left, x, y, (unsigned) half,259(unsigned) height);260XFillRectangle(display, drawable, right, x+half, y,261(unsigned) (width-half), (unsigned) height);262} else if (relief == TK_RELIEF_GROOVE) {263left = borderPtr->darkGC;264right = borderPtr->lightGC;265goto ridgeGroove;266} else if (relief == TK_RELIEF_FLAT) {267XFillRectangle(display, drawable, borderPtr->bgGC, x, y,268(unsigned) width, (unsigned) height);269}270}271272/*273*--------------------------------------------------------------274*275* Tk_3DHorizontalBevel --276*277* This procedure draws a horizontal bevel along one side of278* an object. The bevel has mitered corners (depending on279* leftIn and rightIn arguments).280*281* Results:282* None.283*284* Side effects:285* None.286*287*--------------------------------------------------------------288*/289290void291Tk_3DHorizontalBevel(tkwin, drawable, border, x, y, width, height,292leftIn, rightIn, topBevel, relief)293Tk_Window tkwin; /* Window for which border was allocated. */294Drawable drawable; /* X window or pixmap in which to draw. */295Tk_3DBorder border; /* Token for border to draw. */296int x, y, width, height; /* Bounding box of area of bevel. Height297* gives width of border. */298int leftIn, rightIn; /* Describes whether the left and right299* edges of the bevel angle in or out as300* they go down. For example, if "leftIn"301* is true, the left side of the bevel302* looks like this:303* ___________304* __________305* _________306* ________307*/308int topBevel; /* Non-zero means this bevel forms the309* top side of the object; 0 means it310* forms the bottom side. */311int relief; /* Kind of bevel to draw. For example,312* TK_RELIEF_RAISED means interior of313* object should appear higher than314* exterior. */315{316Border *borderPtr = (Border *) border;317Display *display = Tk_Display(tkwin);318int bottom, halfway, x1, x2, x1Delta, x2Delta;319GC topGC = None, bottomGC = None;320/* Initializations needed only to prevent321* compiler warnings. */322323if ((borderPtr->lightGC == None) && (relief != TK_RELIEF_FLAT)) {324GetShadows(borderPtr, tkwin);325}326327/*328* Compute a GC for the top half of the bevel and a GC for the329* bottom half (they're the same in many cases).330*/331332switch (relief) {333case TK_RELIEF_RAISED:334topGC = bottomGC =335(topBevel) ? borderPtr->lightGC : borderPtr->darkGC;336break;337case TK_RELIEF_SUNKEN:338topGC = bottomGC =339(topBevel) ? borderPtr->darkGC : borderPtr->lightGC;340break;341case TK_RELIEF_RIDGE:342topGC = borderPtr->lightGC;343bottomGC = borderPtr->darkGC;344break;345case TK_RELIEF_GROOVE:346topGC = borderPtr->darkGC;347bottomGC = borderPtr->lightGC;348break;349case TK_RELIEF_FLAT:350topGC = bottomGC = borderPtr->bgGC;351break;352}353354/*355* Compute various other geometry-related stuff.356*/357358x1 = x;359if (!leftIn) {360x1 += height;361}362x2 = x+width;363if (!rightIn) {364x2 -= height;365}366x1Delta = (leftIn) ? 1 : -1;367x2Delta = (rightIn) ? -1 : 1;368halfway = y + height/2;369if (!topBevel && (height & 1)) {370halfway++;371}372bottom = y + height;373374/*375* Draw one line for each y-coordinate covered by the bevel.376*/377378for ( ; y < bottom; y++) {379/*380* In some weird cases (such as large border widths for skinny381* rectangles) x1 can be >= x2. Don't draw the lines382* in these cases.383*/384385if (x1 < x2) {386XFillRectangle(display, drawable,387(y < halfway) ? topGC : bottomGC, x1, y,388(unsigned) (x2-x1), (unsigned) 1);389}390x1 += x1Delta;391x2 += x2Delta;392}393}394395/*396*--------------------------------------------------------------397*398* Tk_Draw3DRectangle --399*400* Draw a 3-D border at a given place in a given window.401*402* Results:403* None.404*405* Side effects:406* A 3-D border will be drawn in the indicated drawable.407* The outside edges of the border will be determined by x,408* y, width, and height. The inside edges of the border409* will be determined by the borderWidth argument.410*411*--------------------------------------------------------------412*/413414void415Tk_Draw3DRectangle(tkwin, drawable, border, x, y, width, height,416borderWidth, relief)417Tk_Window tkwin; /* Window for which border was allocated. */418Drawable drawable; /* X window or pixmap in which to draw. */419Tk_3DBorder border; /* Token for border to draw. */420int x, y, width, height; /* Outside area of region in421* which border will be drawn. */422int borderWidth; /* Desired width for border, in423* pixels. */424int relief; /* Type of relief: TK_RELIEF_RAISED,425* TK_RELIEF_SUNKEN, TK_RELIEF_GROOVE, etc. */426{427if (width < 2*borderWidth) {428borderWidth = width/2;429}430if (height < 2*borderWidth) {431borderWidth = height/2;432}433Tk_3DVerticalBevel(tkwin, drawable, border, x, y, borderWidth, height,4341, relief);435Tk_3DVerticalBevel(tkwin, drawable, border, x+width-borderWidth, y,436borderWidth, height, 0, relief);437Tk_3DHorizontalBevel(tkwin, drawable, border, x, y, width, borderWidth,4381, 1, 1, relief);439Tk_3DHorizontalBevel(tkwin, drawable, border, x, y+height-borderWidth,440width, borderWidth, 0, 0, 0, relief);441}442443/*444*--------------------------------------------------------------445*446* Tk_NameOf3DBorder --447*448* Given a border, return a textual string identifying the449* border's color.450*451* Results:452* The return value is the string that was used to create453* the border.454*455* Side effects:456* None.457*458*--------------------------------------------------------------459*/460461char *462Tk_NameOf3DBorder(border)463Tk_3DBorder border; /* Token for border. */464{465Border *borderPtr = (Border *) border;466void *ptr = borderPtr->hashPtr->key.words;467468return ((BorderKey *) ptr)->colorName;469}470471/*472*--------------------------------------------------------------------473*474* Tk_3DBorderColor --475*476* Given a 3D border, return the X color used for the "flat"477* surfaces.478*479* Results:480* Returns the color used drawing flat surfaces with the border.481*482* Side effects:483* None.484*485*--------------------------------------------------------------------486*/487XColor *488Tk_3DBorderColor(border)489Tk_3DBorder border; /* Border whose color is wanted. */490{491return(((Border *) border)->bgColorPtr);492}493494/*495*--------------------------------------------------------------------496*497* Tk_3DBorderGC --498*499* Given a 3D border, returns one of the graphics contexts used to500* draw the border.501*502* Results:503* Returns the graphics context given by the "which" argument.504*505* Side effects:506* None.507*508*--------------------------------------------------------------------509*/510GC511Tk_3DBorderGC(tkwin, border, which)512Tk_Window tkwin; /* Window for which border was allocated. */513Tk_3DBorder border; /* Border whose GC is wanted. */514int which; /* Selects one of the border's 3 GC's:515* TK_3D_FLAT_GC, TK_3D_LIGHT_GC, or516* TK_3D_DARK_GC. */517{518Border * borderPtr = (Border *) border;519520if ((borderPtr->lightGC == None) && (which != TK_3D_FLAT_GC)) {521GetShadows(borderPtr, tkwin);522}523if (which == TK_3D_FLAT_GC) {524return borderPtr->bgGC;525} else if (which == TK_3D_LIGHT_GC) {526return borderPtr->lightGC;527} else if (which == TK_3D_DARK_GC){528return borderPtr->darkGC;529}530panic("bogus \"which\" value in Tk_3DBorderGC");531532/*533* The code below will never be executed, but it's needed to534* keep compilers happy.535*/536537return (GC) None;538}539540/*541*--------------------------------------------------------------542*543* Tk_Free3DBorder --544*545* This procedure is called when a 3D border is no longer546* needed. It frees the resources associated with the547* border. After this call, the caller should never again548* use the "border" token.549*550* Results:551* None.552*553* Side effects:554* Resources are freed.555*556*--------------------------------------------------------------557*/558559void560Tk_Free3DBorder(border)561Tk_3DBorder border; /* Token for border to be released. */562{563register Border *borderPtr = (Border *) border;564Display *display = DisplayOfScreen(borderPtr->screen);565566borderPtr->refCount--;567if (borderPtr->refCount == 0) {568if (borderPtr->bgColorPtr != NULL) {569Tk_FreeColor(borderPtr->bgColorPtr);570}571if (borderPtr->darkColorPtr != NULL) {572Tk_FreeColor(borderPtr->darkColorPtr);573}574if (borderPtr->lightColorPtr != NULL) {575Tk_FreeColor(borderPtr->lightColorPtr);576}577if (borderPtr->shadow != None) {578Tk_FreeBitmap(display, borderPtr->shadow);579}580if (borderPtr->bgGC != None) {581Tk_FreeGC(display, borderPtr->bgGC);582}583if (borderPtr->darkGC != None) {584Tk_FreeGC(display, borderPtr->darkGC);585}586if (borderPtr->lightGC != None) {587Tk_FreeGC(display, borderPtr->lightGC);588}589Tcl_DeleteHashEntry(borderPtr->hashPtr);590ckfree((char *) borderPtr);591}592}593594/*595*----------------------------------------------------------------------596*597* Tk_SetBackgroundFromBorder --598*599* Change the background of a window to one appropriate for a given600* 3-D border.601*602* Results:603* None.604*605* Side effects:606* Tkwin's background gets modified.607*608*----------------------------------------------------------------------609*/610611void612Tk_SetBackgroundFromBorder(tkwin, border)613Tk_Window tkwin; /* Window whose background is to be set. */614Tk_3DBorder border; /* Token for border. */615{616register Border *borderPtr = (Border *) border;617618Tk_SetWindowBackground(tkwin, borderPtr->bgColorPtr->pixel);619}620621/*622*----------------------------------------------------------------------623*624* Tk_GetRelief --625*626* Parse a relief description and return the corresponding627* relief value, or an error.628*629* Results:630* A standard Tcl return value. If all goes well then631* *reliefPtr is filled in with one of the values632* TK_RELIEF_RAISED, TK_RELIEF_FLAT, or TK_RELIEF_SUNKEN.633*634* Side effects:635* None.636*637*----------------------------------------------------------------------638*/639640int641Tk_GetRelief(interp, name, reliefPtr)642Tcl_Interp *interp; /* For error messages. */643char *name; /* Name of a relief type. */644int *reliefPtr; /* Where to store converted relief. */645{646char c;647size_t length;648649c = name[0];650length = strlen(name);651if ((c == 'f') && (strncmp(name, "flat", length) == 0)) {652*reliefPtr = TK_RELIEF_FLAT;653} else if ((c == 'g') && (strncmp(name, "groove", length) == 0)654&& (length >= 2)) {655*reliefPtr = TK_RELIEF_GROOVE;656} else if ((c == 'r') && (strncmp(name, "raised", length) == 0)657&& (length >= 2)) {658*reliefPtr = TK_RELIEF_RAISED;659} else if ((c == 'r') && (strncmp(name, "ridge", length) == 0)) {660*reliefPtr = TK_RELIEF_RIDGE;661} else if ((c == 's') && (strncmp(name, "sunken", length) == 0)) {662*reliefPtr = TK_RELIEF_SUNKEN;663} else {664sprintf(interp->result, "bad relief type \"%.50s\": must be %s",665name, "flat, groove, raised, ridge, or sunken");666return TCL_ERROR;667}668return TCL_OK;669}670671/*672*--------------------------------------------------------------673*674* Tk_NameOfRelief --675*676* Given a relief value, produce a string describing that677* relief value.678*679* Results:680* The return value is a static string that is equivalent681* to relief.682*683* Side effects:684* None.685*686*--------------------------------------------------------------687*/688689char *690Tk_NameOfRelief(relief)691int relief; /* One of TK_RELIEF_FLAT, TK_RELIEF_RAISED,692* or TK_RELIEF_SUNKEN. */693{694if (relief == TK_RELIEF_FLAT) {695return "flat";696} else if (relief == TK_RELIEF_SUNKEN) {697return "sunken";698} else if (relief == TK_RELIEF_RAISED) {699return "raised";700} else if (relief == TK_RELIEF_GROOVE) {701return "groove";702} else if (relief == TK_RELIEF_RIDGE) {703return "ridge";704} else {705return "unknown relief";706}707}708709/*710*--------------------------------------------------------------711*712* Tk_Draw3DPolygon --713*714* Draw a border with 3-D appearance around the edge of a715* given polygon.716*717* Results:718* None.719*720* Side effects:721* Information is drawn in "drawable" in the form of a722* 3-D border borderWidth units width wide on the left723* of the trajectory given by pointPtr and numPoints (or724* -borderWidth units wide on the right side, if borderWidth725* is negative).726*727*--------------------------------------------------------------728*/729730void731Tk_Draw3DPolygon(tkwin, drawable, border, pointPtr, numPoints,732borderWidth, leftRelief)733Tk_Window tkwin; /* Window for which border was allocated. */734Drawable drawable; /* X window or pixmap in which to draw. */735Tk_3DBorder border; /* Token for border to draw. */736XPoint *pointPtr; /* Array of points describing737* polygon. All points must be738* absolute (CoordModeOrigin). */739int numPoints; /* Number of points at *pointPtr. */740int borderWidth; /* Width of border, measured in741* pixels to the left of the polygon's742* trajectory. May be negative. */743int leftRelief; /* TK_RELIEF_RAISED or744* TK_RELIEF_SUNKEN: indicates how745* stuff to left of trajectory looks746* relative to stuff on right. */747{748XPoint poly[4], b1, b2, newB1, newB2;749XPoint perp, c, shift1, shift2; /* Used for handling parallel lines. */750register XPoint *p1Ptr, *p2Ptr;751Border *borderPtr = (Border *) border;752GC gc;753int i, lightOnLeft, dx, dy, parallel, pointsSeen;754Display *display = Tk_Display(tkwin);755756if (borderPtr->lightGC == None) {757GetShadows(borderPtr, tkwin);758}759760/*761* Handle grooves and ridges with recursive calls.762*/763764if ((leftRelief == TK_RELIEF_GROOVE) || (leftRelief == TK_RELIEF_RIDGE)) {765int halfWidth;766767halfWidth = borderWidth/2;768Tk_Draw3DPolygon(tkwin, drawable, border, pointPtr, numPoints,769halfWidth, (leftRelief == TK_RELIEF_GROOVE) ? TK_RELIEF_RAISED770: TK_RELIEF_SUNKEN);771Tk_Draw3DPolygon(tkwin, drawable, border, pointPtr, numPoints,772-halfWidth, (leftRelief == TK_RELIEF_GROOVE) ? TK_RELIEF_SUNKEN773: TK_RELIEF_RAISED);774return;775}776777/*778* If the polygon is already closed, drop the last point from it779* (we'll close it automatically).780*/781782p1Ptr = &pointPtr[numPoints-1];783p2Ptr = &pointPtr[0];784if ((p1Ptr->x == p2Ptr->x) && (p1Ptr->y == p2Ptr->y)) {785numPoints--;786}787788/*789* The loop below is executed once for each vertex in the polgon.790* At the beginning of each iteration things look like this:791*792* poly[1] /793* * /794* | /795* b1 * poly[0] (pointPtr[i-1])796* | |797* | |798* | |799* | |800* | |801* | | *p1Ptr *p2Ptr802* b2 *--------------------*803* |804* |805* x-------------------------806*807* The job of this iteration is to do the following:808* (a) Compute x (the border corner corresponding to809* pointPtr[i]) and put it in poly[2]. As part of810* this, compute a new b1 and b2 value for the next811* side of the polygon.812* (b) Put pointPtr[i] into poly[3].813* (c) Draw the polygon given by poly[0..3].814* (d) Advance poly[0], poly[1], b1, and b2 for the815* next side of the polygon.816*/817818/*819* The above situation doesn't first come into existence until820* two points have been processed; the first two points are821* used to "prime the pump", so some parts of the processing822* are ommitted for these points. The variable "pointsSeen"823* keeps track of the priming process; it has to be separate824* from i in order to be able to ignore duplicate points in the825* polygon.826*/827828pointsSeen = 0;829for (i = -2, p1Ptr = &pointPtr[numPoints-2], p2Ptr = p1Ptr+1;830i < numPoints; i++, p1Ptr = p2Ptr, p2Ptr++) {831if ((i == -1) || (i == numPoints-1)) {832p2Ptr = pointPtr;833}834if ((p2Ptr->x == p1Ptr->x) && (p2Ptr->y == p1Ptr->y)) {835/*836* Ignore duplicate points (they'd cause core dumps in837* ShiftLine calls below).838*/839continue;840}841ShiftLine(p1Ptr, p2Ptr, borderWidth, &newB1);842newB2.x = newB1.x + (p2Ptr->x - p1Ptr->x);843newB2.y = newB1.y + (p2Ptr->y - p1Ptr->y);844poly[3] = *p1Ptr;845parallel = 0;846if (pointsSeen >= 1) {847parallel = Intersect(&newB1, &newB2, &b1, &b2, &poly[2]);848849/*850* If two consecutive segments of the polygon are parallel,851* then things get more complex. Consider the following852* diagram:853*854* poly[1]855* *----b1-----------b2------a856* \857* \858* *---------*----------* b859* poly[0] *p2Ptr *p1Ptr /860* /861* --*--------*----c862* newB1 newB2863*864* Instead of using x and *p1Ptr for poly[2] and poly[3], as865* in the original diagram, use a and b as above. Then instead866* of using x and *p1Ptr for the new poly[0] and poly[1], use867* b and c as above.868*869* Do the computation in three stages:870* 1. Compute a point "perp" such that the line p1Ptr-perp871* is perpendicular to p1Ptr-p2Ptr.872* 2. Compute the points a and c by intersecting the lines873* b1-b2 and newB1-newB2 with p1Ptr-perp.874* 3. Compute b by shifting p1Ptr-perp to the right and875* intersecting it with p1Ptr-p2Ptr.876*/877878if (parallel) {879perp.x = p1Ptr->x + (p2Ptr->y - p1Ptr->y);880perp.y = p1Ptr->y - (p2Ptr->x - p1Ptr->x);881(void) Intersect(p1Ptr, &perp, &b1, &b2, &poly[2]);882(void) Intersect(p1Ptr, &perp, &newB1, &newB2, &c);883ShiftLine(p1Ptr, &perp, borderWidth, &shift1);884shift2.x = shift1.x + (perp.x - p1Ptr->x);885shift2.y = shift1.y + (perp.y - p1Ptr->y);886(void) Intersect(p1Ptr, p2Ptr, &shift1, &shift2, &poly[3]);887}888}889if (pointsSeen >= 2) {890dx = poly[3].x - poly[0].x;891dy = poly[3].y - poly[0].y;892if (dx > 0) {893lightOnLeft = (dy <= dx);894} else {895lightOnLeft = (dy < dx);896}897if (lightOnLeft ^ (leftRelief == TK_RELIEF_RAISED)) {898gc = borderPtr->lightGC;899} else {900gc = borderPtr->darkGC;901}902XFillPolygon(display, drawable, gc, poly, 4, Convex,903CoordModeOrigin);904}905b1.x = newB1.x;906b1.y = newB1.y;907b2.x = newB2.x;908b2.y = newB2.y;909poly[0].x = poly[3].x;910poly[0].y = poly[3].y;911if (parallel) {912poly[1].x = c.x;913poly[1].y = c.y;914} else if (pointsSeen >= 1) {915poly[1].x = poly[2].x;916poly[1].y = poly[2].y;917}918pointsSeen++;919}920}921922/*923*----------------------------------------------------------------------924*925* Tk_Fill3DRectangle --926*927* Fill a rectangular area, supplying a 3D border if desired.928*929* Results:930* None.931*932* Side effects:933* Information gets drawn on the screen.934*935*----------------------------------------------------------------------936*/937938void939Tk_Fill3DRectangle(tkwin, drawable, border, x, y, width,940height, borderWidth, relief)941Tk_Window tkwin; /* Window for which border was allocated. */942Drawable drawable; /* X window or pixmap in which to draw. */943Tk_3DBorder border; /* Token for border to draw. */944int x, y, width, height; /* Outside area of rectangular region. */945int borderWidth; /* Desired width for border, in946* pixels. Border will be *inside* region. */947int relief; /* Indicates 3D effect: TK_RELIEF_FLAT,948* TK_RELIEF_RAISED, or TK_RELIEF_SUNKEN. */949{950register Border *borderPtr = (Border *) border;951952XFillRectangle(Tk_Display(tkwin), drawable, borderPtr->bgGC,953x, y, (unsigned int) width, (unsigned int) height);954if (relief != TK_RELIEF_FLAT) {955Tk_Draw3DRectangle(tkwin, drawable, border, x, y, width,956height, borderWidth, relief);957}958}959960/*961*----------------------------------------------------------------------962*963* Tk_Fill3DPolygon --964*965* Fill a polygonal area, supplying a 3D border if desired.966*967* Results:968* None.969*970* Side effects:971* Information gets drawn on the screen.972*973*----------------------------------------------------------------------974*/975976void977Tk_Fill3DPolygon(tkwin, drawable, border, pointPtr, numPoints,978borderWidth, leftRelief)979Tk_Window tkwin; /* Window for which border was allocated. */980Drawable drawable; /* X window or pixmap in which to draw. */981Tk_3DBorder border; /* Token for border to draw. */982XPoint *pointPtr; /* Array of points describing983* polygon. All points must be984* absolute (CoordModeOrigin). */985int numPoints; /* Number of points at *pointPtr. */986int borderWidth; /* Width of border, measured in987* pixels to the left of the polygon's988* trajectory. May be negative. */989int leftRelief; /* Indicates 3D effect of left side of990* trajectory relative to right:991* TK_RELIEF_FLAT, TK_RELIEF_RAISED,992* or TK_RELIEF_SUNKEN. */993{994register Border *borderPtr = (Border *) border;995996XFillPolygon(Tk_Display(tkwin), drawable, borderPtr->bgGC,997pointPtr, numPoints, Complex, CoordModeOrigin);998if (leftRelief != TK_RELIEF_FLAT) {999Tk_Draw3DPolygon(tkwin, drawable, border, pointPtr, numPoints,1000borderWidth, leftRelief);1001}1002}10031004/*1005*--------------------------------------------------------------1006*1007* BorderInit --1008*1009* Initialize the structures used for border management.1010*1011* Results:1012* None.1013*1014* Side effects:1015* Read the code.1016*1017*-------------------------------------------------------------1018*/10191020static void1021BorderInit()1022{1023initialized = 1;1024Tcl_InitHashTable(&borderTable, sizeof(BorderKey)/sizeof(int));1025}10261027/*1028*--------------------------------------------------------------1029*1030* ShiftLine --1031*1032* Given two points on a line, compute a point on a1033* new line that is parallel to the given line and1034* a given distance away from it.1035*1036* Results:1037* None.1038*1039* Side effects:1040* None.1041*1042*--------------------------------------------------------------1043*/10441045static void1046ShiftLine(p1Ptr, p2Ptr, distance, p3Ptr)1047XPoint *p1Ptr; /* First point on line. */1048XPoint *p2Ptr; /* Second point on line. */1049int distance; /* New line is to be this many1050* units to the left of original1051* line, when looking from p1 to1052* p2. May be negative. */1053XPoint *p3Ptr; /* Store coords of point on new1054* line here. */1055{1056int dx, dy, dxNeg, dyNeg;10571058/*1059* The table below is used for a quick approximation in1060* computing the new point. An index into the table1061* is 128 times the slope of the original line (the slope1062* must always be between 0 and 1). The value of the table1063* entry is 128 times the amount to displace the new line1064* in y for each unit of perpendicular distance. In other1065* words, the table maps from the tangent of an angle to1066* the inverse of its cosine. If the slope of the original1067* line is greater than 1, then the displacement is done in1068* x rather than in y.1069*/10701071static int shiftTable[129];10721073/*1074* Initialize the table if this is the first time it is1075* used.1076*/10771078if (shiftTable[0] == 0) {1079int i;1080double tangent, cosine;10811082for (i = 0; i <= 128; i++) {1083tangent = i/128.0;1084cosine = 128/cos(atan(tangent)) + .5;1085shiftTable[i] = cosine;1086}1087}10881089*p3Ptr = *p1Ptr;1090dx = p2Ptr->x - p1Ptr->x;1091dy = p2Ptr->y - p1Ptr->y;1092if (dy < 0) {1093dyNeg = 1;1094dy = -dy;1095} else {1096dyNeg = 0;1097}1098if (dx < 0) {1099dxNeg = 1;1100dx = -dx;1101} else {1102dxNeg = 0;1103}1104if (dy <= dx) {1105dy = ((distance * shiftTable[(dy<<7)/dx]) + 64) >> 7;1106if (!dxNeg) {1107dy = -dy;1108}1109p3Ptr->y += dy;1110} else {1111dx = ((distance * shiftTable[(dx<<7)/dy]) + 64) >> 7;1112if (dyNeg) {1113dx = -dx;1114}1115p3Ptr->x += dx;1116}1117}11181119/*1120*--------------------------------------------------------------1121*1122* Intersect --1123*1124* Find the intersection point between two lines.1125*1126* Results:1127* Under normal conditions 0 is returned and the point1128* at *iPtr is filled in with the intersection between1129* the two lines. If the two lines are parallel, then1130* -1 is returned and *iPtr isn't modified.1131*1132* Side effects:1133* None.1134*1135*--------------------------------------------------------------1136*/11371138static int1139Intersect(a1Ptr, a2Ptr, b1Ptr, b2Ptr, iPtr)1140XPoint *a1Ptr; /* First point of first line. */1141XPoint *a2Ptr; /* Second point of first line. */1142XPoint *b1Ptr; /* First point of second line. */1143XPoint *b2Ptr; /* Second point of second line. */1144XPoint *iPtr; /* Filled in with intersection point. */1145{1146int dxadyb, dxbdya, dxadxb, dyadyb, p, q;11471148/*1149* The code below is just a straightforward manipulation of two1150* equations of the form y = (x-x1)*(y2-y1)/(x2-x1) + y1 to solve1151* for the x-coordinate of intersection, then the y-coordinate.1152*/11531154dxadyb = (a2Ptr->x - a1Ptr->x)*(b2Ptr->y - b1Ptr->y);1155dxbdya = (b2Ptr->x - b1Ptr->x)*(a2Ptr->y - a1Ptr->y);1156dxadxb = (a2Ptr->x - a1Ptr->x)*(b2Ptr->x - b1Ptr->x);1157dyadyb = (a2Ptr->y - a1Ptr->y)*(b2Ptr->y - b1Ptr->y);11581159if (dxadyb == dxbdya) {1160return -1;1161}1162p = (a1Ptr->x*dxbdya - b1Ptr->x*dxadyb + (b1Ptr->y - a1Ptr->y)*dxadxb);1163q = dxbdya - dxadyb;1164if (q < 0) {1165p = -p;1166q = -q;1167}1168if (p < 0) {1169iPtr->x = - ((-p + q/2)/q);1170} else {1171iPtr->x = (p + q/2)/q;1172}1173p = (a1Ptr->y*dxadyb - b1Ptr->y*dxbdya + (b1Ptr->x - a1Ptr->x)*dyadyb);1174q = dxadyb - dxbdya;1175if (q < 0) {1176p = -p;1177q = -q;1178}1179if (p < 0) {1180iPtr->y = - ((-p + q/2)/q);1181} else {1182iPtr->y = (p + q/2)/q;1183}1184return 0;1185}11861187/*1188*----------------------------------------------------------------------1189*1190* GetShadows --1191*1192* This procedure computes the shadow colors for a 3-D border1193* and fills in the corresponding fields of the Border structure.1194* It's called lazily, so that the colors aren't allocated until1195* something is actually drawn with them. That way, if a border1196* is only used for flat backgrounds the shadow colors will1197* never be allocated.1198*1199* Results:1200* None.1201*1202* Side effects:1203* The lightGC and darkGC fields in borderPtr get filled in,1204* if they weren't already.1205*1206*----------------------------------------------------------------------1207*/12081209static void1210GetShadows(borderPtr, tkwin)1211Border *borderPtr; /* Information about border. */1212Tk_Window tkwin; /* Window where border will be used for1213* drawing. */1214{1215XColor lightColor, darkColor;1216int stressed, tmp1, tmp2;1217XGCValues gcValues;12181219if (borderPtr->lightGC != None) {1220return;1221}1222stressed = TkCmapStressed(tkwin, borderPtr->colormap);12231224/*1225* First, handle the case of a color display with lots of colors.1226* The shadow colors get computed using whichever formula results1227* in the greatest change in color:1228* 1. Lighter shadow is half-way to white, darker shadow is half1229* way to dark.1230* 2. Lighter shadow is 40% brighter than background, darker shadow1231* is 40% darker than background.1232*/12331234if (!stressed && (Tk_Depth(tkwin) >= 6)) {1235/*1236* This is a color display with lots of colors. For the dark1237* shadow, cut 40% from each of the background color components.1238* For the light shadow, boost each component by 40% or half-way1239* to white, whichever is greater (the first approach works1240* better for unsaturated colors, the second for saturated ones).1241*/12421243darkColor.red = (60 * (int) borderPtr->bgColorPtr->red)/100;1244darkColor.green = (60 * (int) borderPtr->bgColorPtr->green)/100;1245darkColor.blue = (60 * (int) borderPtr->bgColorPtr->blue)/100;1246borderPtr->darkColorPtr = Tk_GetColorByValue(tkwin, &darkColor);1247gcValues.foreground = borderPtr->darkColorPtr->pixel;1248borderPtr->darkGC = Tk_GetGC(tkwin, GCForeground, &gcValues);12491250/*1251* Compute the colors using integers, not using lightColor.red1252* etc.: these are shorts and may have problems with integer1253* overflow.1254*/12551256tmp1 = (14 * (int) borderPtr->bgColorPtr->red)/10;1257if (tmp1 > MAX_INTENSITY) {1258tmp1 = MAX_INTENSITY;1259}1260tmp2 = (MAX_INTENSITY + (int) borderPtr->bgColorPtr->red)/2;1261lightColor.red = (tmp1 > tmp2) ? tmp1 : tmp2;1262tmp1 = (14 * (int) borderPtr->bgColorPtr->green)/10;1263if (tmp1 > MAX_INTENSITY) {1264tmp1 = MAX_INTENSITY;1265}1266tmp2 = (MAX_INTENSITY + (int) borderPtr->bgColorPtr->green)/2;1267lightColor.green = (tmp1 > tmp2) ? tmp1 : tmp2;1268tmp1 = (14 * (int) borderPtr->bgColorPtr->blue)/10;1269if (tmp1 > MAX_INTENSITY) {1270tmp1 = MAX_INTENSITY;1271}1272tmp2 = (MAX_INTENSITY + (int) borderPtr->bgColorPtr->blue)/2;1273lightColor.blue = (tmp1 > tmp2) ? tmp1 : tmp2;1274borderPtr->lightColorPtr = Tk_GetColorByValue(tkwin, &lightColor);1275gcValues.foreground = borderPtr->lightColorPtr->pixel;1276borderPtr->lightGC = Tk_GetGC(tkwin, GCForeground, &gcValues);1277return;1278}12791280if (borderPtr->shadow == None) {1281borderPtr->shadow = Tk_GetBitmap((Tcl_Interp *) NULL, tkwin,1282Tk_GetUid("gray50"));1283if (borderPtr->shadow == None) {1284panic("GetShadows couldn't allocate bitmap for border");1285}1286}1287if (borderPtr->visual->map_entries > 2) {1288/*1289* This isn't a monochrome display, but the colormap either1290* ran out of entries or didn't have very many to begin with.1291* Generate the light shadows with a white stipple and the1292* dark shadows with a black stipple.1293*/12941295gcValues.foreground = borderPtr->bgColorPtr->pixel;1296gcValues.background = BlackPixelOfScreen(borderPtr->screen);1297gcValues.stipple = borderPtr->shadow;1298gcValues.fill_style = FillOpaqueStippled;1299borderPtr->darkGC = Tk_GetGC(tkwin,1300GCForeground|GCBackground|GCStipple|GCFillStyle, &gcValues);1301gcValues.background = WhitePixelOfScreen(borderPtr->screen);1302borderPtr->lightGC = Tk_GetGC(tkwin,1303GCForeground|GCBackground|GCStipple|GCFillStyle, &gcValues);1304return;1305}13061307/*1308* This is just a measly monochrome display, hardly even worth its1309* existence on this earth. Make one shadow a 50% stipple and the1310* other the opposite of the background.1311*/13121313gcValues.foreground = WhitePixelOfScreen(borderPtr->screen);1314gcValues.background = BlackPixelOfScreen(borderPtr->screen);1315gcValues.stipple = borderPtr->shadow;1316gcValues.fill_style = FillOpaqueStippled;1317borderPtr->lightGC = Tk_GetGC(tkwin,1318GCForeground|GCBackground|GCStipple|GCFillStyle, &gcValues);1319if (borderPtr->bgColorPtr->pixel1320== WhitePixelOfScreen(borderPtr->screen)) {1321gcValues.foreground = BlackPixelOfScreen(borderPtr->screen);1322borderPtr->darkGC = Tk_GetGC(tkwin, GCForeground, &gcValues);1323} else {1324borderPtr->darkGC = borderPtr->lightGC;1325borderPtr->lightGC = Tk_GetGC(tkwin, GCForeground, &gcValues);1326}1327}132813291330