diff options
author | jan.nijtmans <nijtmans@users.sourceforge.net> | 2020-03-18 16:20:07 (GMT) |
---|---|---|
committer | jan.nijtmans <nijtmans@users.sourceforge.net> | 2020-03-18 16:20:07 (GMT) |
commit | 1c9d56a0f8ddd1cdc87789648eb7032671b2607e (patch) | |
tree | 5b3b3bd718bbfd7fa5662f158d77f1205b072c5a | |
parent | beb7fb810b843573cb513577c6ed9f7c56c4741e (diff) | |
download | tk-1c9d56a0f8ddd1cdc87789648eb7032671b2607e.zip tk-1c9d56a0f8ddd1cdc87789648eb7032671b2607e.tar.gz tk-1c9d56a0f8ddd1cdc87789648eb7032671b2607e.tar.bz2 |
More internal use of TkGetIntForIndex() function.
-rw-r--r-- | generic/tkCanvLine.c | 50 | ||||
-rw-r--r-- | generic/tkCanvPoly.c | 51 | ||||
-rw-r--r-- | generic/tkCanvText.c | 25 | ||||
-rw-r--r-- | generic/tkEntry.c | 45 | ||||
-rw-r--r-- | generic/tkListbox.c | 31 | ||||
-rw-r--r-- | generic/ttk/ttkEntry.c | 79 |
6 files changed, 120 insertions, 161 deletions
diff --git a/generic/tkCanvLine.c b/generic/tkCanvLine.c index 980d170..647ddfa 100644 --- a/generic/tkCanvLine.c +++ b/generic/tkCanvLine.c @@ -1749,17 +1749,26 @@ GetLineIndex( * itemPtr's line. */ int *indexPtr) /* Where to store converted index. */ { + TkSizeT idx, length; LineItem *linePtr = (LineItem *) itemPtr; - const char *string = Tcl_GetString(obj); + const char *string; (void)canvas; - if (string[0] == 'e') { - if (strncmp(string, "end", obj->length) == 0) { - *indexPtr = 2*linePtr->numPoints; + if (TCL_OK == TkGetIntForIndex(obj, 2*linePtr->numPoints - 1, &idx)) { + if (idx == TCL_INDEX_NONE) { + idx = 0; + } else if (idx > (2*(TkSizeT)linePtr->numPoints)) { + idx = 2*linePtr->numPoints; } else { - goto badIndex; + idx &= (TkSizeT)-2; /* If index is odd, make it even. */ } - } else if (string[0] == '@') { + *indexPtr = (int)idx; + return TCL_OK; + } + + string = TkGetStringFromObj(obj, &length); + + if (string[0] == '@') { int i; double x, y, bestDist, dist, *coordPtr; char *end; @@ -1787,27 +1796,18 @@ GetLineIndex( coordPtr += 2; } } else { - if (Tcl_GetIntFromObj(NULL, obj, indexPtr) != TCL_OK) { - goto badIndex; - } - *indexPtr &= -2; /* If index is odd, make it even. */ - if (*indexPtr < 0){ - *indexPtr = 0; - } else if (*indexPtr > (2*linePtr->numPoints)) { - *indexPtr = (2*linePtr->numPoints); - } - } - return TCL_OK; - /* - * Some of the paths here leave messages in interp->result, so we have to - * clear it out before storing our own message. - */ + /* + * Some of the paths here leave messages in interp->result, so we have to + * clear it out before storing our own message. + */ - badIndex: - Tcl_SetObjResult(interp, Tcl_ObjPrintf("bad index \"%s\"", string)); - Tcl_SetErrorCode(interp, "TK", "CANVAS", "ITEM_INDEX", "LINE", NULL); - return TCL_ERROR; + badIndex: + Tcl_SetObjResult(interp, Tcl_ObjPrintf("bad index \"%s\"", string)); + Tcl_SetErrorCode(interp, "TK", "CANVAS", "ITEM_INDEX", "LINE", NULL); + return TCL_ERROR; + } + return TCL_OK; } /* diff --git a/generic/tkCanvPoly.c b/generic/tkCanvPoly.c index 19c5f6d..1fd71bc 100644 --- a/generic/tkCanvPoly.c +++ b/generic/tkCanvPoly.c @@ -1679,16 +1679,25 @@ GetPolygonIndex( * itemPtr's line. */ int *indexPtr) /* Where to store converted index. */ { + TkSizeT length, idx; PolygonItem *polyPtr = (PolygonItem *) itemPtr; - const char *string = Tcl_GetString(obj); + const char *string; (void)canvas; + TkSizeT count = 2*(polyPtr->numPoints - polyPtr->autoClosed); - if (string[0] == 'e') { - if (strncmp(string, "end", obj->length) != 0) { - goto badIndex; + if (TCL_OK == TkGetIntForIndex(obj, (INT_MAX) - ((INT_MAX) % count), &idx)) { + if (idx == TCL_INDEX_NONE) { + idx = 0; + } else { + idx = (idx & (TkSizeT)-2) % count; } - *indexPtr = 2*(polyPtr->numPoints - polyPtr->autoClosed); - } else if (string[0] == '@') { + *indexPtr = (int)idx; + return TCL_OK; + } + + string = TkGetStringFromObj(obj, &length); + + if (string[0] == '@') { int i; double x, y, bestDist, dist, *coordPtr; char *end; @@ -1716,31 +1725,17 @@ GetPolygonIndex( coordPtr += 2; } } else { - int count = 2*(polyPtr->numPoints - polyPtr->autoClosed); + /* + * Some of the paths here leave messages in interp->result, so we have to + * clear it out before storing our own message. + */ - if (Tcl_GetIntFromObj(interp, obj, indexPtr) != TCL_OK) { - goto badIndex; - } - *indexPtr &= -2; /* if odd, make it even */ - if (!count) { - *indexPtr = 0; - } else if (*indexPtr > 0) { - *indexPtr = ((*indexPtr - 2) % count) + 2; - } else { - *indexPtr = -((-(*indexPtr)) % count); - } + badIndex: + Tcl_SetObjResult(interp, Tcl_ObjPrintf("bad index \"%s\"", string)); + Tcl_SetErrorCode(interp, "TK", "CANVAS", "ITEM_INDEX", "POLY", NULL); + return TCL_ERROR; } return TCL_OK; - - /* - * Some of the paths here leave messages in interp->result, so we have to - * clear it out before storing our own message. - */ - - badIndex: - Tcl_SetObjResult(interp, Tcl_ObjPrintf("bad index \"%s\"", string)); - Tcl_SetErrorCode(interp, "TK", "CANVAS", "ITEM_INDEX", "POLY", NULL); - return TCL_ERROR; } /* diff --git a/generic/tkCanvText.c b/generic/tkCanvText.c index baf85df..bf6befc 100644 --- a/generic/tkCanvText.c +++ b/generic/tkCanvText.c @@ -1380,17 +1380,26 @@ GetTextIndex( * index. */ { TextItem *textPtr = (TextItem *) itemPtr; - TkSizeT length; + TkSizeT length, idx; int c; Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr; - const char *string = TkGetStringFromObj(obj, &length); + const char *string; (void)canvas; + if (TCL_OK == TkGetIntForIndex(obj, textPtr->numChars, &idx)) { + if (idx == TCL_INDEX_NONE) { + idx = 0; + } else if (idx > (TkSizeT)textPtr->numChars) { + idx = textPtr->numChars; + } + *indexPtr = (int)idx; + return TCL_OK; + } + + string = TkGetStringFromObj(obj, &length); c = string[0]; - if ((c == 'e') && (strncmp(string, "end", length) == 0)) { - *indexPtr = textPtr->numChars; - } else if ((c == 'i') + if ((c == 'i') && (strncmp(string, "insert", length) == 0)) { *indexPtr = textPtr->insertPos; } else if ((c == 's') && (length >= 5) @@ -1433,12 +1442,6 @@ GetTextIndex( y -= (int) textPtr->drawOrigin[1]; *indexPtr = Tk_PointToChar(textPtr->textLayout, (int) (x*c - y*s), (int) (y*c + x*s)); - } else if (Tcl_GetIntFromObj(NULL, obj, indexPtr) == TCL_OK) { - if (*indexPtr < 0) { - *indexPtr = 0; - } else if (*indexPtr > textPtr->numChars) { - *indexPtr = textPtr->numChars; - } } else { /* * Some of the paths here leave messages in the interp's result, so we diff --git a/generic/tkEntry.c b/generic/tkEntry.c index a5e09bd..2281a61 100644 --- a/generic/tkEntry.c +++ b/generic/tkEntry.c @@ -2651,8 +2651,20 @@ GetEntryIndex( Tcl_Obj *indexObj, /* Specifies character in entryPtr. */ int *indexPtr) /* Where to store converted character index */ { - const char *string = Tcl_GetString(indexObj); - size_t length = indexObj->length; + TkSizeT length, idx; + const char *string; + + if (TCL_OK == TkGetIntForIndex(indexObj, entryPtr->numChars, &idx)) { + if (idx == TCL_INDEX_NONE) { + idx = 0; + } else if (idx > (TkSizeT)entryPtr->numChars) { + idx = (TkSizeT)entryPtr->numChars; + } + *indexPtr = (int)idx; + return TCL_OK; + } + + string = TkGetStringFromObj(indexObj, &length); switch (string[0]) { case 'a': @@ -2661,12 +2673,6 @@ GetEntryIndex( } *indexPtr = entryPtr->selectAnchor; break; - case 'e': - if (strncmp(string, "end", length) != 0) { - goto badIndex; - } - *indexPtr = entryPtr->numChars; - break; case 'i': if (strncmp(string, "insert", length) != 0) { goto badIndex; @@ -2727,24 +2733,15 @@ GetEntryIndex( break; } default: - if (Tcl_GetIntFromObj(NULL, indexObj, indexPtr) != TCL_OK) { - goto badIndex; - } - if (*indexPtr < 0){ - *indexPtr = 0; - } else if (*indexPtr > entryPtr->numChars) { - *indexPtr = entryPtr->numChars; - } + badIndex: + Tcl_SetObjResult(interp, Tcl_ObjPrintf("bad %s index \"%s\"", + (entryPtr->type == TK_ENTRY) ? "entry" : "spinbox", string)); + Tcl_SetErrorCode(interp, "TK", + (entryPtr->type == TK_ENTRY) ? "ENTRY" : "SPINBOX", + "BAD_INDEX", NULL); + return TCL_ERROR; } return TCL_OK; - - badIndex: - Tcl_SetObjResult(interp, Tcl_ObjPrintf("bad %s index \"%s\"", - (entryPtr->type == TK_ENTRY) ? "entry" : "spinbox", string)); - Tcl_SetErrorCode(interp, "TK", - (entryPtr->type == TK_ENTRY) ? "ENTRY" : "SPINBOX", - "BAD_INDEX", NULL); - return TCL_ERROR; } /* diff --git a/generic/tkListbox.c b/generic/tkListbox.c index d456a13..bdff469 100644 --- a/generic/tkListbox.c +++ b/generic/tkListbox.c @@ -377,10 +377,10 @@ enum scancommand { }; static const char *const indexNames[] = { - "active", "anchor", "end", NULL + "active", "anchor", NULL }; enum indices { - INDEX_ACTIVE, INDEX_ANCHOR, INDEX_END + INDEX_ACTIVE, INDEX_ANCHOR }; /* @@ -2737,8 +2737,18 @@ GetListboxIndex( int *indexPtr) /* Where to store converted index. */ { int result, index; + TkSizeT idx; const char *stringRep; + result = TkGetIntForIndex(indexObj, listPtr->nElements - (endIsSize ? 0 : 1), &idx); + if (result == TCL_OK) { + if (idx + 1 > (TkSizeT)listPtr->nElements + 1) { + idx = listPtr->nElements; + } + *indexPtr = (int)idx; + return TCL_OK; + } + /* * First see if the index is one of the named indices. */ @@ -2754,10 +2764,6 @@ GetListboxIndex( /* "anchor" index */ *indexPtr = listPtr->selectAnchor; break; - case INDEX_END: - /* "end" index */ - *indexPtr = listPtr->nElements - (endIsSize ? 0 : 1); - break; } return TCL_OK; } @@ -2792,19 +2798,6 @@ GetListboxIndex( } /* - * Maybe the index is just an integer. - */ - - if (Tcl_GetIntFromObj(NULL, indexObj, indexPtr) == TCL_OK) { - if (*indexPtr < -1) { - *indexPtr = -1; - } else if (*indexPtr > listPtr->nElements) { - *indexPtr = listPtr->nElements; - } - return TCL_OK; - } - - /* * Everything failed, nothing matched. Throw up an error message. */ diff --git a/generic/ttk/ttkEntry.c b/generic/ttk/ttkEntry.c index 3f28445..8abcd9b 100644 --- a/generic/ttk/ttkEntry.c +++ b/generic/ttk/ttkEntry.c @@ -1369,12 +1369,20 @@ EntryIndex( int *indexPtr) /* Return value */ { # define EntryWidth(e) (Tk_Width(entryPtr->core.tkwin)) /* Not Right */ - TkSizeT length; - const char *string = TkGetStringFromObj(indexObj, &length); + TkSizeT length, idx; + const char *string; + + if (TCL_OK == TkGetIntForIndex(indexObj, entryPtr->entry.numChars, &idx)) { + if (idx + 1 > (TkSizeT)entryPtr->entry.numChars + 1) { + idx = (TkSizeT)entryPtr->entry.numChars; + } + *indexPtr = (int)idx; + return TCL_OK; + } - if (strncmp(string, "end", length) == 0) { - *indexPtr = entryPtr->entry.numChars; - } else if (strncmp(string, "insert", length) == 0) { + string = TkGetStringFromObj(indexObj, &length); + + if (strncmp(string, "insert", length) == 0) { *indexPtr = entryPtr->entry.insertPos; } else if (strncmp(string, "left", length) == 0) { /* for debugging */ *indexPtr = entryPtr->entry.xscroll.first; @@ -1426,14 +1434,7 @@ EntryIndex( *indexPtr += 1; } } else { - if (Tcl_GetIntFromObj(NULL, indexObj, indexPtr) != TCL_OK) { - goto badIndex; - } - if (*indexPtr < 0) { - *indexPtr = 0; - } else if (*indexPtr > entryPtr->entry.numChars) { - *indexPtr = entryPtr->entry.numChars; - } + goto badIndex; } return TCL_OK; @@ -1749,16 +1750,6 @@ static const WidgetSpec EntryWidgetSpec = { }; /*------------------------------------------------------------------------ - * Named indices for the combobox "current" command - */ -static const char *const comboboxCurrentIndexNames[] = { - "end", NULL -}; -enum comboboxCurrentIndices { - INDEX_END -}; - -/*------------------------------------------------------------------------ * +++ Combobox widget record. */ @@ -1859,42 +1850,22 @@ static int ComboboxCurrentCommand( Tcl_SetObjResult(interp, Tcl_NewWideIntObj(currentIndex)); return TCL_OK; } else if (objc == 3) { - int result, index; - - result = Tcl_GetIndexFromObj(NULL, objv[2], comboboxCurrentIndexNames, - "", 0, &index); - if (result == TCL_OK) { + TkSizeT idx; - /* - * The index is one of the named indices. - */ - - switch (index) { - case INDEX_END: - /* "end" index */ - currentIndex = nValues - 1; - break; - } - } else { - - /* - * The index should be just an integer. - */ - - if (Tcl_GetIntFromObj(NULL, objv[2], ¤tIndex) != TCL_OK) { + if (TCL_OK == TkGetIntForIndex(objv[2], nValues - 1, &idx)) { + if (idx == TCL_INDEX_NONE || idx > (TkSizeT)nValues) { Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "Incorrect index %s", Tcl_GetString(objv[2]))); - Tcl_SetErrorCode(interp, "TTK", "COMBOBOX", "IDX_VALUE", NULL); - return TCL_ERROR; - } - - if (currentIndex < 0 || currentIndex >= nValues) { - Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "Index %s out of range", Tcl_GetString(objv[2]))); + "index \"%s\" out of range", Tcl_GetString(objv[2]))); Tcl_SetErrorCode(interp, "TTK", "COMBOBOX", "IDX_RANGE", NULL); return TCL_ERROR; } - } + currentIndex = (int)idx; + } else { + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "Incorrect index %s", Tcl_GetString(objv[2]))); + Tcl_SetErrorCode(interp, "TTK", "COMBOBOX", "IDX_VALUE", NULL); + return TCL_ERROR; + } cbPtr->combobox.currentIndex = currentIndex; |