Changeset 3040 for trunk/gui/src/RpCanvHotspot.c
- Timestamp:
- Jun 13, 2012 8:06:54 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gui/src/RpCanvHotspot.c
r3039 r3040 91 91 * where the item is drawn on the 92 92 * canvas. */ 93 93 double x1, y1, x2, y2; 94 94 /* 95 95 * Configuration settings that are updated by Tk_ConfigureWidget: … … 136 136 ItemSegment *firstPtr, *lastPtr; /* List of hotspot segments. */ 137 137 int width, height; /* Dimension of bounding box. */ 138 ItemSegment *activePtr; /* Active segment. */138 const char *activeValue; /* Active segment. */ 139 139 short int maxImageWidth, maxImageHeight; 140 140 int numLines; … … 152 152 {TK_CONFIG_STRING, "-activeimage", "activeImage", (char*)NULL, (char*)NULL, 153 153 Tk_Offset(HotspotItem, activeImageName), TK_CONFIG_NULL_OK}, 154 {TK_CONFIG_STRING, "-activevalue", "activeValue", (char*)NULL, (char*)NULL, 155 Tk_Offset(HotspotItem, activeValue), TK_CONFIG_NULL_OK}, 154 156 {TK_CONFIG_ANCHOR, "-anchor", "anchor", (char*)NULL, 155 157 "center", Tk_Offset(HotspotItem, anchor), … … 168 170 "blue", Tk_Offset(HotspotItem, activeValueBorder), 0}, 169 171 {TK_CONFIG_FONT, "-valuefont", (char*)NULL, (char*)NULL, 170 " courier -12", Tk_Offset(HotspotItem, valueFont), 0},172 "helvetica -12 bold", Tk_Offset(HotspotItem, valueFont), 0}, 171 173 {TK_CONFIG_COLOR, "-valueforeground", (char*)NULL, (char*)NULL, 172 174 "blue", Tk_Offset(HotspotItem, valueColor), 0}, … … 569 571 segPtr->lineNum = itemPtr->numLines; 570 572 segPtr->type = SEGMENT_TYPE_VARIABLE; 571 memcpy(segPtr->text, varName, length);573 strncpy(segPtr->text, varName, length); 572 574 segPtr->text[length] = '\0'; 573 575 segPtr->length = length; … … 592 594 593 595 static void 594 AddHotspotSegment(HotspotItem *itemPtr )596 AddHotspotSegment(HotspotItem *itemPtr, const char *varName, int length) 595 597 { 596 598 ItemSegment *segPtr; 597 599 598 segPtr = (ItemSegment *)Tcl_Alloc(sizeof(ItemSegment) );600 segPtr = (ItemSegment *)Tcl_Alloc(sizeof(ItemSegment) + length + 1); 599 601 memset(segPtr, 0, sizeof(ItemSegment)); 600 602 segPtr->itemPtr = itemPtr; 601 603 segPtr->lineNum = itemPtr->numLines; 602 604 segPtr->type = SEGMENT_TYPE_HOTSPOT; 603 segPtr->text[0] = '\0'; 604 segPtr->length = 0; 605 strncpy(segPtr->text, varName, length); 606 segPtr->text[length] = '\0'; 607 segPtr->length = length; 605 608 segPtr->x = segPtr->y = 0; 606 609 segPtr->nextPtr = NULL; … … 756 759 itemPtr->width = maxWidth; 757 760 itemPtr->height = y; 761 ComputeBbox(itemPtr); 758 762 } 759 763 … … 784 788 if (*q == '}') { 785 789 AddVariableSegment(itemPtr, varName, q - varName); 786 AddHotspotSegment(itemPtr );790 AddHotspotSegment(itemPtr, varName, q- varName); 787 791 p = q; 788 792 start = p + 1; … … 846 850 } 847 851 852 memset((char *)itemPtr + sizeof(Tk_Item), 0, sizeof(HotspotItem) - sizeof(Tk_Item)); 848 853 /* 849 854 * Carry out initialization that is needed in order to clean up after 850 855 * errors during the the remainder of this procedure. 851 856 */ 852 itemPtr->activeImage = NULL;853 itemPtr->activeImageName = NULL;854 itemPtr->activePtr = NULL;855 857 itemPtr->anchor = TK_ANCHOR_CENTER; 856 itemPtr->border = NULL;857 itemPtr->borderWidth = 0;858 858 itemPtr->canvas = canvas; 859 859 itemPtr->display = Tk_Display(tkwin); 860 itemPtr->firstPtr = NULL;861 itemPtr->font = NULL;862 itemPtr->height = 0;863 itemPtr->image = NULL;864 itemPtr->imageName = NULL;865 860 itemPtr->interp = interp; 866 itemPtr->lastPtr = NULL;867 itemPtr->maxImageWidth = itemPtr->maxImageHeight = 0;868 itemPtr->normalGC = None;869 itemPtr->numLines = 0;870 itemPtr->outlineColor = NULL;871 itemPtr->outlineGC = None;872 861 itemPtr->relief = TK_RELIEF_FLAT; 873 itemPtr->valueColor = NULL; 874 itemPtr->valueFont = NULL; 875 itemPtr->valueGC = None; 876 itemPtr->activeGC = None; 862 itemPtr->tkwin = tkwin; 877 863 itemPtr->valueInterp = interp; 878 itemPtr->text = NULL; 879 itemPtr->textColor = NULL; 880 itemPtr->tkwin = tkwin; 881 itemPtr->width = 0; 882 itemPtr->x = itemPtr->y = 0.0; 864 itemPtr->border = NULL; 865 itemPtr->activeValueBorder = NULL; 883 866 884 867 /* Process the arguments to fill in the item record. */ … … 1178 1161 */ 1179 1162 static void 1180 TranslateAnchor(HotspotItem *itemPtr, int *xPtr, int*yPtr)1181 { 1182 intx, y;1163 TranslateAnchor(HotspotItem *itemPtr, double *xPtr, double *yPtr) 1164 { 1165 double x, y; 1183 1166 1184 1167 x = *xPtr, y = *yPtr; … … 1187 1170 break; 1188 1171 case TK_ANCHOR_W: /* Left center */ 1189 y -= (itemPtr->height / 2);1172 y -= (itemPtr->height * 0.5); 1190 1173 break; 1191 1174 case TK_ANCHOR_SW: /* Lower left corner */ … … 1193 1176 break; 1194 1177 case TK_ANCHOR_N: /* Top center */ 1195 x -= (itemPtr->width / 2);1178 x -= (itemPtr->width * 0.5); 1196 1179 break; 1197 1180 case TK_ANCHOR_CENTER: /* Centered */ 1198 x -= (itemPtr->width / 2);1199 y -= (itemPtr->height / 2);1181 x -= (itemPtr->width * 0.5); 1182 y -= (itemPtr->height * 0.5); 1200 1183 break; 1201 1184 case TK_ANCHOR_S: /* Bottom center */ 1202 x -= (itemPtr->width / 2);1185 x -= (itemPtr->width * 0.5); 1203 1186 y -= itemPtr->height; 1204 1187 break; … … 1208 1191 case TK_ANCHOR_E: /* Right center */ 1209 1192 x -= itemPtr->width; 1210 y -= (itemPtr->height / 2);1193 y -= (itemPtr->height * 0.5); 1211 1194 break; 1212 1195 case TK_ANCHOR_SE: /* Lower right corner */ … … 1242 1225 ComputeBbox(HotspotItem *itemPtr) 1243 1226 { 1244 int x, y; 1245 1246 x = ROUND(itemPtr->x); 1247 y = ROUND(itemPtr->y); 1248 TranslateAnchor(itemPtr,&x, &y); 1249 itemPtr->base.x1 = x; 1250 itemPtr->base.y1 = y; 1251 itemPtr->base.x2 = x + itemPtr->width; 1252 itemPtr->base.y2 = y + itemPtr->height; 1227 double x, y; 1228 1229 x = itemPtr->x, y = itemPtr->y; 1230 TranslateAnchor(itemPtr, &x, &y); 1231 itemPtr->x1 = x; 1232 itemPtr->x2 = x + itemPtr->width; 1233 itemPtr->y1 = y; 1234 itemPtr->y2 = y + itemPtr->height; 1235 itemPtr->base.x1 = ROUND(itemPtr->x1); 1236 itemPtr->base.y1 = ROUND(itemPtr->y1); 1237 itemPtr->base.x2 = ROUND(itemPtr->x2); 1238 itemPtr->base.y2 = ROUND(itemPtr->y2); 1253 1239 } 1254 1240 … … 1264 1250 1265 1251 tkImage = NULL; 1266 if (itemPtr->activePtr == segPtr) { 1252 if ((itemPtr->activeValue != NULL) && 1253 (strcmp(itemPtr->activeValue, segPtr->text) == 0)) { 1267 1254 tkImage = itemPtr->activeImage; 1268 1255 } … … 1305 1292 HotspotItem *itemPtr = (HotspotItem *) canvItemPtr; 1306 1293 ItemSegment *segPtr; 1307 short int cx, cy; 1294 short int drawX, drawY; 1295 double tx, ty; 1308 1296 1309 1297 itemPtr->flags &= ~REDRAW_PENDING; … … 1314 1302 ComputeGeometry(itemPtr); 1315 1303 } 1316 Tk_CanvasDrawableCoords(canvas, itemPtr->x, itemPtr->y, &cx, &cy); 1317 x = cx, y = cy; 1318 TranslateAnchor(itemPtr, &x, &y); 1304 tx = itemPtr->x, ty = itemPtr->y; 1305 TranslateAnchor(itemPtr, &tx, &ty); 1306 1307 /* Convert canvas coordinates to drawable coordinates. */ 1308 Tk_CanvasDrawableCoords(canvas, tx, ty, &drawX, &drawY); 1309 x = drawX, y = drawY; 1319 1310 1320 1311 if (itemPtr->border != NULL) { … … 1402 1393 HotspotItem *itemPtr = (HotspotItem *)canvItemPtr; 1403 1394 1404 /* Compare against bounding box... */ 1405 if ((rectPtr[0] > itemPtr->base.x2) || (rectPtr[1] > itemPtr->base.y2) || 1406 (rectPtr[2] < itemPtr->base.x1) || (rectPtr[3] < itemPtr->base.y1)) { 1407 return -1; /* Completely outside. */ 1408 } 1409 if ((rectPtr[0] >= itemPtr->base.x1) && (rectPtr[1] >= itemPtr->base.y1) && 1410 (rectPtr[2] <= itemPtr->base.x2) && (rectPtr[3] <= itemPtr->base.y2)) { 1411 return 1; /* Completely inside. */ 1412 } 1413 return 0; /* Must be overlapping */ 1395 if ((rectPtr[2] <= itemPtr->base.x1) || 1396 (rectPtr[0] >= itemPtr->base.x2) || 1397 (rectPtr[3] <= itemPtr->base.y1) || 1398 (rectPtr[1] >= itemPtr->base.y2)) { 1399 return -1; 1400 } 1401 if ((rectPtr[0] <= itemPtr->base.x1) && 1402 (rectPtr[1] <= itemPtr->base.y1) && 1403 (rectPtr[2] >= itemPtr->base.x2) && 1404 (rectPtr[3] >= itemPtr->base.y2)) { 1405 return 1; 1406 } 1407 return 0; 1414 1408 } 1415 1409 … … 1437 1431 HotspotItem *itemPtr = (HotspotItem *)canvItemPtr; 1438 1432 1439 itemPtr->x +=scaleX * (itemPtr->x - x);1440 itemPtr->y +=scaleY * (itemPtr->y - y);1433 itemPtr->x = x + scaleX * (itemPtr->x - x); 1434 itemPtr->y = y + scaleY * (itemPtr->y - y); 1441 1435 ComputeBbox(itemPtr); 1442 1436 return; … … 1655 1649 } 1656 1650 1651 static const char * 1652 Identify(Tcl_Interp *interp, HotspotItem *itemPtr, double x, double y) 1653 { 1654 double tx, ty; 1655 short drawX, drawY; 1656 ItemSegment *segPtr; 1657 int xOffset, yOffset; 1658 1659 tx = itemPtr->x, ty = itemPtr->y; 1660 TranslateAnchor(itemPtr, &tx, &ty); 1661 1662 /* Convert canvas coordinates to drawable coordinates. */ 1663 Tk_CanvasDrawableCoords(itemPtr->canvas, tx, ty, &drawX, &drawY); 1664 xOffset = ROUND(tx), yOffset = ROUND(ty); 1665 1666 for (segPtr = itemPtr->firstPtr; segPtr != NULL; segPtr = segPtr->nextPtr) { 1667 int x1, x2, y1, y2; 1668 1669 if (segPtr->type == SEGMENT_TYPE_TEXT) { 1670 continue; 1671 } 1672 x1 = xOffset + segPtr->x; 1673 x2 = xOffset + segPtr->x + segPtr->width; 1674 y1 = yOffset + segPtr->y; 1675 y2 = yOffset + segPtr->y + segPtr->height; 1676 fprintf(stderr, "x=%g y=%g x1=%d x2=%d y1=%d y2=%d xOff=%d yOff=%d ix=%g iy=%g tx=%g ty=%g\n", 1677 x, y, x1, x2, y1, y2, xOffset, yOffset, itemPtr->x, itemPtr->y, tx, ty); 1678 if ((x >= x1) && (x < x2) && (y >= y1) && (y < y2)) { 1679 return segPtr->text; 1680 } 1681 } 1682 return ""; 1683 } 1684 1657 1685 static int 1658 1686 HotspotCmd(ClientData clientData, Tcl_Interp *interp, int objc, … … 1677 1705 } else if ((c == 'd') && (strncmp(string, "deactivate", length) == 0)) { 1678 1706 /* hotspot deactivate .c 0 */ 1679 itemPtr->active Ptr= NULL;1707 itemPtr->activeValue = NULL; 1680 1708 } else if ((c == 'i') && (strncmp(string, "identify", length) == 0)) { 1709 double x, y; 1710 const char *token; 1711 Tcl_Obj *objPtr; 1712 1681 1713 /* hotspot identify .c 0 x y */ 1682 /* Return name of variable identified by x,y coordinate. */ 1714 if ((Tcl_GetDoubleFromObj(interp, objv[4], &x) != TCL_OK) || 1715 (Tcl_GetDoubleFromObj(interp, objv[5], &y) != TCL_OK)) { 1716 return TCL_ERROR; 1717 } 1718 token = Identify(interp, itemPtr, x, y); 1719 objPtr = Tcl_NewStringObj(token, -1); 1720 Tcl_SetObjResult(interp, objPtr); 1683 1721 } else if ((c == 'v') && (strncmp(string, "variables", length) == 0)) { 1722 Tcl_Obj *listObjPtr; 1723 ItemSegment *segPtr; 1724 1684 1725 /* hotspot variables .c 0 */ 1685 /* Return list of variable names. */ 1726 listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **)NULL); 1727 for (segPtr = itemPtr->firstPtr; segPtr != NULL; 1728 segPtr = segPtr->nextPtr) { 1729 if (segPtr->type == SEGMENT_TYPE_VARIABLE) { 1730 Tcl_Obj *objPtr; 1731 1732 objPtr = Tcl_NewStringObj(segPtr->text, -1); 1733 Tcl_ListObjAppendElement(interp, listObjPtr, objPtr); 1734 } 1735 } 1736 Tcl_SetObjResult(interp, listObjPtr); 1686 1737 } else { 1687 1738 Tcl_AppendResult(interp, "unknown hotspot option \"", string, … … 1709 1760 { 1710 1761 Tk_CreateItemType(&hotspotType); 1762 Tcl_CreateObjCommand(interp, "Rappture::hotspot", HotspotCmd, NULL, NULL); 1711 1763 return TCL_OK; 1712 1764 }
Note: See TracChangeset
for help on using the changeset viewer.