diff options
author | jan.nijtmans <nijtmans@users.sourceforge.net> | 2022-01-25 16:12:11 (GMT) |
---|---|---|
committer | jan.nijtmans <nijtmans@users.sourceforge.net> | 2022-01-25 16:12:11 (GMT) |
commit | 186f5c8cca88c9e09ac1273f16161b40da91d5dc (patch) | |
tree | 37426ce59d4e31e5b918f2ceff67655158699418 | |
parent | 2863744760a2d204f11ad4d334f7ba3eb0af8d15 (diff) | |
download | tk-186f5c8cca88c9e09ac1273f16161b40da91d5dc.zip tk-186f5c8cca88c9e09ac1273f16161b40da91d5dc.tar.gz tk-186f5c8cca88c9e09ac1273f16161b40da91d5dc.tar.bz2 |
Implement TK_OPTION_NULL_OK for TK_OPTION_INT (working the same as for TK_OPTION_DOUBLE). Better error-messages.
-rw-r--r-- | doc/SetOptions.3 | 15 | ||||
-rw-r--r-- | generic/tkConfig.c | 21 | ||||
-rw-r--r-- | tests/config.test | 2 |
3 files changed, 28 insertions, 10 deletions
diff --git a/doc/SetOptions.3 b/doc/SetOptions.3 index 8eee22a..0daedee 100644 --- a/doc/SetOptions.3 +++ b/doc/SetOptions.3 @@ -345,9 +345,11 @@ such as \fBTk_SetOptions\fR, and it supports the \fBTK_OPTION_NULL_OK\fR flag. .TP \fBTK_OPTION_BOOLEAN\fR The value must be a standard boolean value such as \fBtrue\fR or -\fBno\fR. The internal form is an integer with value 0 or 1. This option -type supports the \fBTK_OPTION_NULL_OK\fR flag; if a NULL value is set, the -internal representation is set to -1. +\fBno\fR. The internal form is an integer with value 0 or 1. Note: if the +\fIobjOffset\fR field is not used then information about the original value +of this option will be lost. This option type supports the +\fBTK_OPTION_NULL_OK\fR flag; if a NULL value is set, the internal +representation is set to -1. .TP \fBTK_OPTION_BORDER\fR The value must be a standard color name such as \fBred\fR or \fB#ff8080\fR. @@ -403,7 +405,8 @@ such as \fBTk_SetOptions\fR, and it supports the \fBTK_OPTION_NULL_OK\fR flag. The string value must be an integer in the format accepted by \fBstrtol\fR (e.g. \fB0\fR and \fB0x\fR prefixes may be used to specify octal or hexadecimal numbers, respectively). The internal -form is a C \fBint\fR value. +form is a C \fBint\fR value. This option type supports the \fBTK_OPTION_NULL_OK\fR +flag; if a NULL value is set, the internal representation is set to 0.0. .TP \fBTK_OPTION_JUSTIFY\fR The value must be a standard justification value such as \fBleft\fR. @@ -426,8 +429,8 @@ internal representation is set to zero. The value must be standard relief such as \fBraised\fR. The internal form is an integer relief value such as \fBTK_RELIEF_RAISED\fR. This option type supports the \fBTK_OPTION_NULL_OK\fR -flag; if the empty string is specified as the value for the option, -the integer relief value is set to \fBTK_RELIEF_NULL\fR. +flag; if a NULL value is set, the internal representation is set to +\fBTK_RELIEF_NULL\fR. .TP \fBTK_OPTION_STRING\fR The value may be any string. The internal form is a (char *) pointer diff --git a/generic/tkConfig.c b/generic/tkConfig.c index 533ff99..34dc12e 100644 --- a/generic/tkConfig.c +++ b/generic/tkConfig.c @@ -617,7 +617,11 @@ DoObjConfig( if (nullOK && ObjectIsEmpty(valuePtr)) { valuePtr = NULL; newBool = -1; - } else if (Tcl_GetBooleanFromObj(interp, valuePtr, &newBool) != TCL_OK) { + } else if (Tcl_GetBooleanFromObj(nullOK ? NULL : interp, valuePtr, &newBool) != TCL_OK) { + if (nullOK && interp) { + Tcl_AppendResult(interp, "expected boolean value or \"\" but got \"", + Tcl_GetString(valuePtr), "\"", NULL); + } return TCL_ERROR; } if (internalPtr != NULL) { @@ -629,7 +633,14 @@ DoObjConfig( case TK_OPTION_INT: { int newInt; - if (Tcl_GetIntFromObj(interp, valuePtr, &newInt) != TCL_OK) { + if (nullOK && ObjectIsEmpty(valuePtr)) { + valuePtr = NULL; + newInt = 0; + } else if (Tcl_GetIntFromObj(nullOK ? NULL : interp, valuePtr, &newInt) != TCL_OK) { + if (nullOK && interp) { + Tcl_AppendResult(interp, "expected integer or \"\" but got \"", + Tcl_GetString(valuePtr), "\"", NULL); + } return TCL_ERROR; } if (internalPtr != NULL) { @@ -666,7 +677,11 @@ DoObjConfig( valuePtr = NULL; newDbl = 0; } else { - if (Tcl_GetDoubleFromObj(interp, valuePtr, &newDbl) != TCL_OK) { + if (Tcl_GetDoubleFromObj(nullOK ? NULL : interp, valuePtr, &newDbl) != TCL_OK) { + if (nullOK && interp) { + Tcl_AppendResult(interp, "expected floating-point number or \"\" but got \"", + Tcl_GetString(valuePtr), "\"", NULL); + } return TCL_ERROR; } } diff --git a/tests/config.test b/tests/config.test index b4d1879..cfda857 100644 --- a/tests/config.test +++ b/tests/config.test @@ -277,7 +277,7 @@ test config-4.7 {DoObjConfig - invalid boolean} -constraints { testobjconfig alltypes .foo -boolean foo } -cleanup { killTables -} -returnCodes error -result {expected boolean value but got "foo"} +} -returnCodes error -result {expected boolean value or "" but got "foo"} test config-4.8 {DoObjConfig - boolean internal value} -constraints { testobjconfig } -setup { |