Skip to content

Commit

Permalink
"canvas" almost done. Only missing are tkCanvPs and tkCanvWind
Browse files Browse the repository at this point in the history
,
  • Loading branch information
jan.nijtmans committed Sep 6, 2024
1 parent 607e4d6 commit 07b9686
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 48 deletions.
6 changes: 3 additions & 3 deletions generic/tk.h
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ typedef struct Tk_ItemType {
typedef struct Tk_CanvasTextInfo {
Tk_3DBorder selBorder; /* Border and background for selected
* characters. Read-only to items.*/
int selBorderWidth; /* Width of border around selection. Read-only
Tcl_Obj *selBorderWidthObj; /* Width of border around selection. Read-only
* to items. */
XColor *selFgColorPtr; /* Foreground color for selected text.
* Read-only to items. */
Expand All @@ -1144,9 +1144,9 @@ typedef struct Tk_CanvasTextInfo {
* items. */
Tk_3DBorder insertBorder; /* Used to draw vertical bar for insertion
* cursor. Read-only to items. */
int insertWidth; /* Total width of insertion cursor. Read-only
Tcl_Obj *insertWidthObj; /* Total width of insertion cursor. Read-only
* to items. */
int insertBorderWidth; /* Width of 3-D border around insert cursor.
Tcl_Obj *insertBorderWidthObj; /* Width of 3-D border around insert cursor.
* Read-only to items. */
Tk_Item *focusItemPtr; /* Item that currently has the input focus, or
* NULL if no such item. Read-only to items. */
Expand Down
28 changes: 18 additions & 10 deletions generic/tkCanvText.c
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ ComputeTextBbox(
TextItem *textPtr) /* Item whose bbox is to be recomputed. */
{
Tk_CanvasTextInfo *textInfoPtr;
int width, height, fudge, i;
int width, height, fudge, i, insertWidth, selBorderWidth;
Tk_State state = textPtr->header.state;
double x[4], y[4], dx[4], dy[4], sinA, cosA, tmp;

Expand Down Expand Up @@ -799,9 +799,11 @@ ComputeTextBbox(
*/

textInfoPtr = textPtr->textInfoPtr;
fudge = (textInfoPtr->insertWidth + 1) / 2;
if (textInfoPtr->selBorderWidth > fudge) {
fudge = textInfoPtr->selBorderWidth;
Tk_GetPixelsFromObj(NULL, Tk_CanvasTkwin(canvas), textInfoPtr->insertWidthObj, &insertWidth);
Tk_GetPixelsFromObj(NULL, Tk_CanvasTkwin(canvas), textInfoPtr->selBorderWidthObj, &selBorderWidth);
fudge = (insertWidth + 1) / 2;
if (selBorderWidth > fudge) {
fudge = selBorderWidth;
}

/*
Expand Down Expand Up @@ -928,6 +930,7 @@ DisplayCanvText(
if ((selFirstChar >= 0) && (selFirstChar <= selLastChar)) {
int xFirst, yFirst, hFirst;
int xLast, yLast, wLast;
int selBorderWidth;

/*
* Draw a special background under the selection.
Expand All @@ -947,6 +950,7 @@ DisplayCanvText(

x = xFirst;
height = hFirst;
Tk_GetPixelsFromObj(NULL, Tk_CanvasTkwin(canvas), textInfoPtr->selBorderWidthObj, &selBorderWidth);
for (y = yFirst ; y <= yLast; y += height) {
int dx1, dy1, dx2, dy2;
double s = textPtr->sine, c = textPtr->cosine;
Expand All @@ -957,9 +961,9 @@ DisplayCanvText(
} else {
width = textPtr->actualWidth - x;
}
dx1 = x - textInfoPtr->selBorderWidth;
dx1 = x - selBorderWidth;
dy1 = y;
dx2 = width + 2 * textInfoPtr->selBorderWidth;
dx2 = width + 2 * selBorderWidth;
dy2 = height;
points[0].x = (short)(drawableX + dx1*c + dy1*s);
points[0].y = (short)(drawableY + dy1*c - dx1*s);
Expand All @@ -971,7 +975,7 @@ DisplayCanvText(
points[3].y = (short)(drawableY + (dy1+dy2)*c - dx1*s);
Tk_Fill3DPolygon(Tk_CanvasTkwin(canvas), drawable,
textInfoPtr->selBorder, points, 4,
textInfoPtr->selBorderWidth, TK_RELIEF_RAISED);
selBorderWidth, TK_RELIEF_RAISED);
x = 0;
}
}
Expand All @@ -992,10 +996,12 @@ DisplayCanvText(
int dx1, dy1, dx2, dy2;
double s = textPtr->sine, c = textPtr->cosine;
XPoint points[4];
int insertWidth;

dx1 = x - (textInfoPtr->insertWidth / 2);
Tk_GetPixelsFromObj(NULL, Tk_CanvasTkwin(canvas), textInfoPtr->insertWidthObj, &insertWidth);
dx1 = x - (insertWidth / 2);
dy1 = y;
dx2 = textInfoPtr->insertWidth;
dx2 = insertWidth;
dy2 = height;
points[0].x = (short)(drawableX + dx1*c + dy1*s);
points[0].y = (short)(drawableY + dy1*c - dx1*s);
Expand All @@ -1009,9 +1015,11 @@ DisplayCanvText(
Tk_SetCaretPos(Tk_CanvasTkwin(canvas), points[0].x, points[0].y,
height);
if (textInfoPtr->cursorOn) {
int insertBorderWidth;
Tk_GetPixelsFromObj(NULL, Tk_CanvasTkwin(canvas), textInfoPtr->insertBorderWidthObj, &insertBorderWidth);
Tk_Fill3DPolygon(Tk_CanvasTkwin(canvas), drawable,
textInfoPtr->insertBorder, points, 4,
textInfoPtr->insertBorderWidth, TK_RELIEF_RAISED);
insertBorderWidth, TK_RELIEF_RAISED);
} else if (textPtr->cursorOffGC != NULL) {
/*
* Redraw the background over the area of the cursor, even
Expand Down
77 changes: 45 additions & 32 deletions generic/tkCanvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,17 @@ static const Tk_ConfigSpec configSpecs[] = {
{TK_CONFIG_BORDER, "-insertbackground", "insertBackground", "Foreground",
DEF_CANVAS_INSERT_BG, offsetof(TkCanvas, textInfo.insertBorder), 0, NULL},
{TK_CONFIG_PIXELS, "-insertborderwidth", "insertBorderWidth", "BorderWidth",
DEF_CANVAS_INSERT_BD_COLOR,
offsetof(TkCanvas, textInfo.insertBorderWidth), TK_CONFIG_COLOR_ONLY, NULL},
DEF_CANVAS_INSERT_BD_COLOR, offsetof(TkCanvas, textInfo.insertBorderWidthObj),
TK_CONFIG_OBJS|TK_CONFIG_COLOR_ONLY, NULL},
{TK_CONFIG_PIXELS, "-insertborderwidth", "insertBorderWidth", "BorderWidth",
DEF_CANVAS_INSERT_BD_MONO,
offsetof(TkCanvas, textInfo.insertBorderWidth), TK_CONFIG_MONO_ONLY, NULL},
DEF_CANVAS_INSERT_BD_MONO, offsetof(TkCanvas, textInfo.insertBorderWidthObj),
TK_CONFIG_OBJS|TK_CONFIG_MONO_ONLY, NULL},
{TK_CONFIG_INT, "-insertofftime", "insertOffTime", "OffTime",
DEF_CANVAS_INSERT_OFF_TIME, offsetof(TkCanvas, insertOffTime), 0, NULL},
{TK_CONFIG_INT, "-insertontime", "insertOnTime", "OnTime",
DEF_CANVAS_INSERT_ON_TIME, offsetof(TkCanvas, insertOnTime), 0, NULL},
{TK_CONFIG_PIXELS, "-insertwidth", "insertWidth", "InsertWidth",
DEF_CANVAS_INSERT_WIDTH, offsetof(TkCanvas, textInfo.insertWidth), 0, NULL},
DEF_CANVAS_INSERT_WIDTH, offsetof(TkCanvas, textInfo.insertWidthObj), TK_CONFIG_OBJS, NULL},
{TK_CONFIG_CUSTOM, "-offset", "offset", "Offset", "0,0",
offsetof(TkCanvas, tsoffset),TK_CONFIG_DONT_SET_DEFAULT,
&offsetOption},
Expand All @@ -147,11 +147,11 @@ static const Tk_ConfigSpec configSpecs[] = {
DEF_CANVAS_SELECT_MONO, offsetof(TkCanvas, textInfo.selBorder),
TK_CONFIG_MONO_ONLY, NULL},
{TK_CONFIG_PIXELS, "-selectborderwidth", "selectBorderWidth", "BorderWidth",
DEF_CANVAS_SELECT_BD_COLOR,
offsetof(TkCanvas, textInfo.selBorderWidth), TK_CONFIG_COLOR_ONLY, NULL},
DEF_CANVAS_SELECT_BD_COLOR, offsetof(TkCanvas, textInfo.selBorderWidthObj),
TK_CONFIG_OBJS|TK_CONFIG_COLOR_ONLY, NULL},
{TK_CONFIG_PIXELS, "-selectborderwidth", "selectBorderWidth", "BorderWidth",
DEF_CANVAS_SELECT_BD_MONO, offsetof(TkCanvas, textInfo.selBorderWidth),
TK_CONFIG_MONO_ONLY, NULL},
DEF_CANVAS_SELECT_BD_MONO, offsetof(TkCanvas, textInfo.selBorderWidthObj),
TK_CONFIG_OBJS|TK_CONFIG_MONO_ONLY, NULL},
{TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
DEF_CANVAS_SELECT_FG_COLOR, offsetof(TkCanvas, textInfo.selFgColorPtr),
TK_CONFIG_COLOR_ONLY|TK_CONFIG_NULL_OK, NULL},
Expand All @@ -170,16 +170,14 @@ static const Tk_ConfigSpec configSpecs[] = {
DEF_CANVAS_X_SCROLL_CMD, offsetof(TkCanvas, xScrollCmd),
TK_CONFIG_NULL_OK, NULL},
{TK_CONFIG_PIXELS, "-xscrollincrement", "xScrollIncrement",
"ScrollIncrement",
DEF_CANVAS_X_SCROLL_INCREMENT, offsetof(TkCanvas, xScrollIncrementObj),
TK_CONFIG_OBJS, NULL},
"ScrollIncrement", DEF_CANVAS_X_SCROLL_INCREMENT,
offsetof(TkCanvas, xScrollIncrementObj), TK_CONFIG_OBJS, NULL},
{TK_CONFIG_STRING, "-yscrollcommand", "yScrollCommand", "ScrollCommand",
DEF_CANVAS_Y_SCROLL_CMD, offsetof(TkCanvas, yScrollCmd),
TK_CONFIG_NULL_OK, NULL},
{TK_CONFIG_PIXELS, "-yscrollincrement", "yScrollIncrement",
"ScrollIncrement",
DEF_CANVAS_Y_SCROLL_INCREMENT, offsetof(TkCanvas, yScrollIncrementObj),
TK_CONFIG_OBJS, NULL},
"ScrollIncrement", DEF_CANVAS_Y_SCROLL_INCREMENT,
offsetof(TkCanvas, yScrollIncrementObj), TK_CONFIG_OBJS, NULL},
{TK_CONFIG_END, NULL, NULL, NULL, NULL, 0, 0, NULL}
};

Expand Down Expand Up @@ -690,16 +688,16 @@ Tk_CanvasObjCmd(
canvasPtr->heightObj = NULL;
canvasPtr->confine = 0;
canvasPtr->textInfo.selBorder = NULL;
canvasPtr->textInfo.selBorderWidth = 0;
canvasPtr->textInfo.selBorderWidthObj = NULL;
canvasPtr->textInfo.selFgColorPtr = NULL;
canvasPtr->textInfo.selItemPtr = NULL;
canvasPtr->textInfo.selectFirst = TCL_INDEX_NONE;
canvasPtr->textInfo.selectLast = TCL_INDEX_NONE;
canvasPtr->textInfo.anchorItemPtr = NULL;
canvasPtr->textInfo.selectAnchor = 0;
canvasPtr->textInfo.insertBorder = NULL;
canvasPtr->textInfo.insertWidth = 0;
canvasPtr->textInfo.insertBorderWidth = 0;
canvasPtr->textInfo.insertWidthObj = NULL;
canvasPtr->textInfo.insertBorderWidthObj = NULL;
canvasPtr->textInfo.focusItemPtr = NULL;
canvasPtr->textInfo.gotFocus = 0;
canvasPtr->textInfo.cursorOn = 0;
Expand Down Expand Up @@ -2267,6 +2265,7 @@ ConfigureCanvas(
Tk_State old_canvas_state=canvasPtr->canvas_state;
int width, height, borderWidth, highlightWidth;
int xScrollIncrement, yScrollIncrement;
int insertBorderWidth, insertWidth;

if (Tk_ConfigureWidget(interp, canvasPtr->tkwin, configSpecs,
objc, objv, canvasPtr,
Expand All @@ -2281,36 +2280,50 @@ ConfigureCanvas(

Tk_SetBackgroundFromBorder(canvasPtr->tkwin, canvasPtr->bgBorder);

Tk_GetPixelsFromObj(NULL, canvasPtr->tkwin, canvasPtr->heightObj, &height);
Tk_GetPixelsFromObj(NULL, canvasPtr->tkwin, canvasPtr->widthObj, &width);
Tk_GetPixelsFromObj(NULL, canvasPtr->tkwin, canvasPtr->borderWidthObj, &borderWidth);
Tk_GetPixelsFromObj(NULL, canvasPtr->tkwin, canvasPtr->heightObj, &height);
Tk_GetPixelsFromObj(NULL, canvasPtr->tkwin, canvasPtr->highlightWidthObj, &highlightWidth);
Tk_GetPixelsFromObj(NULL, canvasPtr->tkwin, canvasPtr->textInfo.insertBorderWidthObj, &insertBorderWidth);
Tk_GetPixelsFromObj(NULL, canvasPtr->tkwin, canvasPtr->textInfo.insertWidthObj, &insertWidth);
Tk_GetPixelsFromObj(NULL, canvasPtr->tkwin, canvasPtr->widthObj, &width);
Tk_GetPixelsFromObj(NULL, canvasPtr->tkwin, canvasPtr->xScrollIncrementObj, &xScrollIncrement);
Tk_GetPixelsFromObj(NULL, canvasPtr->tkwin, canvasPtr->yScrollIncrementObj, &yScrollIncrement);
if (height < 0) {
height = 0;
Tcl_DecrRefCount(canvasPtr->heightObj);
canvasPtr->heightObj = Tcl_NewIntObj(0);
Tcl_IncrRefCount(canvasPtr->heightObj);
}
if (width < 0) {
width = 0;
Tcl_DecrRefCount(canvasPtr->widthObj);
canvasPtr->widthObj = Tcl_NewIntObj(0);
Tcl_IncrRefCount(canvasPtr->widthObj);
}
if (borderWidth < 0) {
borderWidth = 0;
Tcl_DecrRefCount(canvasPtr->borderWidthObj);
canvasPtr->borderWidthObj = Tcl_NewIntObj(0);
Tcl_IncrRefCount(canvasPtr->borderWidthObj);
}
if (height < 0) {
height = 0;
Tcl_DecrRefCount(canvasPtr->heightObj);
canvasPtr->heightObj = Tcl_NewIntObj(0);
Tcl_IncrRefCount(canvasPtr->heightObj);
}
if (highlightWidth < 0) {
highlightWidth = 0;
Tcl_DecrRefCount(canvasPtr->highlightWidthObj);
canvasPtr->highlightWidthObj = Tcl_NewIntObj(0);
Tcl_IncrRefCount(canvasPtr->highlightWidthObj);
}
if (insertBorderWidth < 0) {
insertBorderWidth = 0;
Tcl_DecrRefCount(canvasPtr->textInfo.insertBorderWidthObj);
canvasPtr->textInfo.insertBorderWidthObj = Tcl_NewIntObj(0);
Tcl_IncrRefCount(canvasPtr->textInfo.insertBorderWidthObj);
}
if (insertWidth < 0) {
insertWidth = 0;
Tcl_DecrRefCount(canvasPtr->textInfo.insertWidthObj);
canvasPtr->textInfo.insertWidthObj = Tcl_NewIntObj(0);
Tcl_IncrRefCount(canvasPtr->textInfo.insertWidthObj);
}
if (width < 0) {
width = 0;
Tcl_DecrRefCount(canvasPtr->widthObj);
canvasPtr->widthObj = Tcl_NewIntObj(0);
Tcl_IncrRefCount(canvasPtr->widthObj);
}
if (xScrollIncrement < 0) {
xScrollIncrement = 0;
Tcl_DecrRefCount(canvasPtr->xScrollIncrementObj);
Expand Down
6 changes: 3 additions & 3 deletions tests/canvas.test
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ test canvas-1.24 {configuration options: bad value for "insertbackground"} -body
test canvas-1.25 {configuration options: good value for "insertborderwidth"} -body {
.c configure -insertborderwidth 1.3
.c cget -insertborderwidth
} -result 1
} -result 1.3
test canvas-1.26 {configuration options: bad value for "insertborderwidth"} -body {
.c configure -insertborderwidth 2.6x
} -returnCodes error -result {expected screen distance but got "2.6x"}
Expand All @@ -128,7 +128,7 @@ test canvas-1.30 {configuration options: bad value for "insertontime"} -body {
test canvas-1.31 {configuration options: good value for "insertwidth"} -body {
.c configure -insertwidth 1.3
.c cget -insertwidth
} -result 1
} -result 1.3
test canvas-1.32 {configuration options: bad value for "insertwidth"} -body {
.c configure -insertwidth 6x
} -returnCodes error -result {expected screen distance but got "6x"}
Expand All @@ -149,7 +149,7 @@ test canvas-1.36 {configuration options: bad value for "selectbackground"} -body
test canvas-1.37 {configuration options: good value for "selectborderwidth"} -body {
.c configure -selectborderwidth 1.3
.c cget -selectborderwidth
} -result 1
} -result 1.3
test canvas-1.38 {configuration options: bad value for "selectborderwidth"} -body {
.c configure -selectborderwidth badValue
} -returnCodes error -result {expected screen distance but got "badValue"}
Expand Down

0 comments on commit 07b9686

Please sign in to comment.