Skip to content

Commit 591f68c

Browse files
committed
Fix [3003895fff] and [1899040fff]: TkRoundToResolution doesn't account for -from
2 parents 08e04d9 + 303f111 commit 591f68c

File tree

4 files changed

+81
-32
lines changed

4 files changed

+81
-32
lines changed

generic/tkScale.c

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ ConfigureScale(
611611
TCL_GLOBAL_ONLY);
612612
if ((valuePtr != NULL) &&
613613
(Tcl_GetDoubleFromObj(NULL, valuePtr, &value) == TCL_OK)) {
614-
scalePtr->value = TkRoundToResolution(scalePtr, value);
614+
scalePtr->value = TkRoundValueToResolution(scalePtr, value);
615615
}
616616
}
617617

@@ -620,10 +620,10 @@ ConfigureScale(
620620
* orientation and creating GCs.
621621
*/
622622

623-
scalePtr->fromValue = TkRoundToResolution(scalePtr,
623+
scalePtr->fromValue = TkRoundValueToResolution(scalePtr,
624624
scalePtr->fromValue);
625-
scalePtr->toValue = TkRoundToResolution(scalePtr, scalePtr->toValue);
626-
scalePtr->tickInterval = TkRoundToResolution(scalePtr,
625+
scalePtr->toValue = TkRoundValueToResolution(scalePtr, scalePtr->toValue);
626+
scalePtr->tickInterval = TkRoundIntervalToResolution(scalePtr,
627627
scalePtr->tickInterval);
628628

629629
/*
@@ -1119,10 +1119,14 @@ TkEventuallyRedrawScale(
11191119
/*
11201120
*--------------------------------------------------------------
11211121
*
1122-
* TkRoundToResolution --
1122+
* TkRoundValueToResolution, TkRoundIntervalToResolution --
11231123
*
11241124
* Round a given floating-point value to the nearest multiple of the
11251125
* scale's resolution.
1126+
* TkRoundValueToResolution rounds an absolute value based on the from
1127+
* value as a reference.
1128+
* TkRoundIntervalToResolution rounds a relative value without
1129+
* reference, i.e. it rounds an interval.
11261130
*
11271131
* Results:
11281132
* The return value is the rounded result.
@@ -1134,7 +1138,16 @@ TkEventuallyRedrawScale(
11341138
*/
11351139

11361140
double
1137-
TkRoundToResolution(
1141+
TkRoundValueToResolution(
1142+
TkScale *scalePtr, /* Information about scale widget. */
1143+
double value) /* Value to round. */
1144+
{
1145+
return TkRoundIntervalToResolution(scalePtr, value - scalePtr->fromValue)
1146+
+ scalePtr->fromValue;
1147+
}
1148+
1149+
double
1150+
TkRoundIntervalToResolution(
11381151
TkScale *scalePtr, /* Information about scale widget. */
11391152
double value) /* Value to round. */
11401153
{
@@ -1147,13 +1160,13 @@ TkRoundToResolution(
11471160
rounded = scalePtr->resolution * tick;
11481161
rem = value - rounded;
11491162
if (rem < 0) {
1150-
if (rem <= -scalePtr->resolution/2) {
1151-
rounded = (tick - 1.0) * scalePtr->resolution;
1152-
}
1163+
if (rem <= -scalePtr->resolution/2) {
1164+
rounded = (tick - 1.0) * scalePtr->resolution;
1165+
}
11531166
} else {
1154-
if (rem >= scalePtr->resolution/2) {
1155-
rounded = (tick + 1.0) * scalePtr->resolution;
1156-
}
1167+
if (rem >= scalePtr->resolution/2) {
1168+
rounded = (tick + 1.0) * scalePtr->resolution;
1169+
}
11571170
}
11581171
return rounded;
11591172
}
@@ -1238,7 +1251,7 @@ ScaleVarProc(
12381251
resultStr = "can't assign non-numeric value to scale variable";
12391252
ScaleSetVariable(scalePtr);
12401253
} else {
1241-
scalePtr->value = TkRoundToResolution(scalePtr, value);
1254+
scalePtr->value = TkRoundValueToResolution(scalePtr, value);
12421255

12431256
/*
12441257
* This code is a bit tricky because it sets the scale's value before
@@ -1282,7 +1295,7 @@ TkScaleSetValue(
12821295
int invokeCommand) /* Non-zero means invoked -command option to
12831296
* notify of new value, 0 means don't. */
12841297
{
1285-
value = TkRoundToResolution(scalePtr, value);
1298+
value = TkRoundValueToResolution(scalePtr, value);
12861299
if ((value < scalePtr->fromValue)
12871300
^ (scalePtr->toValue < scalePtr->fromValue)) {
12881301
value = scalePtr->fromValue;
@@ -1402,7 +1415,7 @@ TkScalePixelToValue(
14021415
}
14031416
value = scalePtr->fromValue +
14041417
value * (scalePtr->toValue - scalePtr->fromValue);
1405-
return TkRoundToResolution(scalePtr, value);
1418+
return TkRoundValueToResolution(scalePtr, value);
14061419
}
14071420

14081421
/*

generic/tkScale.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ typedef struct TkScale {
219219
*/
220220

221221
MODULE_SCOPE void TkEventuallyRedrawScale(TkScale *scalePtr, int what);
222-
MODULE_SCOPE double TkRoundToResolution(TkScale *scalePtr, double value);
222+
MODULE_SCOPE double TkRoundValueToResolution(TkScale *scalePtr, double value);
223+
MODULE_SCOPE double TkRoundIntervalToResolution(TkScale *scalePtr, double value);
223224
MODULE_SCOPE TkScale * TkpCreateScale(Tk_Window tkwin);
224225
MODULE_SCOPE void TkpDestroyScale(TkScale *scalePtr);
225226
MODULE_SCOPE void TkpDisplayScale(ClientData clientData);

tests/scale.test

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,85 +1104,120 @@ test scale-13.6 {SetScaleValue procedure} -body {
11041104
destroy .s
11051105
pack [scale .s]
11061106
update
1107-
test scale-14.1 {RoundToResolution procedure} -body {
1107+
test scale-14.1 {RoundValueToResolution procedure} -body {
11081108
.s configure -from 0 -to 100 -sliderlength 10 -length 114 -bd 2 \
11091109
-orient horizontal -resolution 4.0
11101110
update
11111111
.s get 84 152
11121112
} -result 72
1113-
test scale-14.2 {RoundToResolution procedure} -body {
1113+
test scale-14.2 {RoundValueToResolution procedure} -body {
11141114
.s configure -from 0 -to 100 -sliderlength 10 -length 114 -bd 2 \
11151115
-orient horizontal -resolution 4.0
11161116
update
11171117
.s get 86 152
11181118
} -result 76
11191119

1120-
test scale-14.3 {RoundToResolution procedure} -body {
1120+
test scale-14.3 {RoundValueToResolution procedure} -body {
11211121
.s configure -from 100 -to 0 -sliderlength 10 -length 114 -bd 2 \
11221122
-orient horizontal -resolution 4.0
11231123
update
11241124
.s get 84 152
11251125
} -result 28
1126-
test scale-14.4 {RoundToResolution procedure} -body {
1126+
test scale-14.4 {RoundValueToResolution procedure} -body {
11271127
.s configure -from 100 -to 0 -sliderlength 10 -length 114 -bd 2 \
11281128
-orient horizontal -resolution 4.0
11291129
update
11301130
.s get 86 152
11311131
} -result 24
11321132

1133-
test scale-14.5 {RoundToResolution procedure} -body {
1133+
test scale-14.5 {RoundValueToResolution procedure} -body {
11341134
.s configure -from -100 -to 0 -sliderlength 10 -length 114 -bd 2 \
11351135
-orient horizontal -resolution 4.0
11361136
update
11371137
.s get 84 152
11381138
} -result {-28}
1139-
test scale-14.6 {RoundToResolution procedure} -body {
1139+
test scale-14.6 {RoundValueToResolution procedure} -body {
11401140
.s configure -from -100 -to 0 -sliderlength 10 -length 114 -bd 2 \
11411141
-orient horizontal -resolution 4.0
11421142
update
11431143
.s get 86 152
11441144
} -result {-24}
11451145

1146-
test scale-14.7 {RoundToResolution procedure} -body {
1146+
test scale-14.7 {RoundValueToResolution procedure} -body {
11471147
.s configure -from 0 -to -100 -sliderlength 10 -length 114 -bd 2 \
11481148
-orient horizontal -resolution 4.0
11491149
update
11501150
.s get 84 152
11511151
} -result {-72}
1152-
test scale-14.8 {RoundToResolution procedure} -body {
1152+
test scale-14.8 {RoundValueToResolution procedure} -body {
11531153
.s configure -from 0 -to -100 -sliderlength 10 -length 114 -bd 2 \
11541154
-orient horizontal -resolution 4.0
11551155
update
11561156
.s get 86 152
11571157
} -result {-76}
11581158

1159-
test scale-14.9 {RoundToResolution procedure} -body {
1159+
test scale-14.9 {RoundValueToResolution procedure} -body {
11601160
.s configure -from 0 -to 2.25 -sliderlength 10 -length 114 -bd 2 \
11611161
-orient horizontal -resolution 0
11621162
update
11631163
.s get 84 152
11641164
} -result {1.64}
1165-
test scale-14.10 {RoundToResolution procedure} -body {
1165+
test scale-14.10 {RoundValueToResolution procedure} -body {
11661166
.s configure -from 0 -to 2.25 -sliderlength 10 -length 114 -bd 2 \
11671167
-orient horizontal -resolution 0
11681168
update
11691169
.s get 86 152
11701170
} -result {1.69}
11711171

1172-
test scale-14.11 {RoundToResolution procedure} -body {
1172+
test scale-14.11 {RoundValueToResolution procedure} -body {
11731173
.s configure -from 0 -to 225 -sliderlength 10 -length 114 -bd 2 \
11741174
-orient horizontal -resolution 0 -digits 5
11751175
update
11761176
.s get 84 152
11771177
} -result {164.25}
1178-
test scale-14.12 {RoundToResolution procedure} -body {
1178+
test scale-14.12 {RoundValueToResolution procedure} -body {
11791179
.s configure -from 0 -to 225 -sliderlength 10 -length 114 -bd 2 \
11801180
-orient horizontal -resolution 0 -digits 5
11811181
update
11821182
.s get 86 152
11831183
} -result {168.75}
11841184
destroy .s
11851185

1186+
test scale-14.13 {RoundValueToResolution procedure, round-off errors} -setup {
1187+
# see [220665ffff], and duplicates [220265ffff] and [779559ffff]
1188+
set x NotSet
1189+
pack [scale .s -orient horizontal -resolution .1 -from -180 -to 180 -command "set x"]
1190+
update
1191+
} -body {
1192+
.s configure -background red
1193+
update
1194+
set x
1195+
} -cleanup {
1196+
destroy .s
1197+
} -result {NotSet}
1198+
1199+
test scale-14a.1 {RoundValueToResolution, RoundIntervalToResolution procedures} -setup {
1200+
pack [scale .s -orient horizontal]
1201+
update
1202+
} -body {
1203+
.s configure -length 400 -bd 0 -from 1 -to 9 -resolution 2 -tickinterval 1
1204+
update
1205+
.s get 200 0
1206+
} -cleanup {
1207+
destroy .s
1208+
} -result {5}
1209+
test scale-14a.2 {RoundValueToResolution, RoundIntervalToResolution procedures} -setup {
1210+
pack [scale .s -orient horizontal]
1211+
update
1212+
} -body {
1213+
.s configure -length 400 -bd 0 -from -1.5 -to 1.5 -resolution 1 \
1214+
-tickinterval 1 -digits 2
1215+
update
1216+
.s get 250 0
1217+
} -cleanup {
1218+
destroy .s
1219+
} -result {0.5}
1220+
11861221

11871222
test scale-15.1 {ScaleVarProc procedure} -setup {
11881223
deleteWindows

unix/tkUnixScale.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ DisplayVerticalScale(
150150
for (tickValue = scalePtr->fromValue; ;
151151
tickValue += tickInterval) {
152152
/*
153-
* The TkRoundToResolution call gets rid of accumulated
153+
* The TkRoundValueToResolution call gets rid of accumulated
154154
* round-off errors, if any.
155155
*/
156156

157-
tickValue = TkRoundToResolution(scalePtr, tickValue);
157+
tickValue = TkRoundValueToResolution(scalePtr, tickValue);
158158
if (scalePtr->toValue >= scalePtr->fromValue) {
159159
if (tickValue > scalePtr->toValue) {
160160
break;
@@ -370,11 +370,11 @@ DisplayHorizontalScale(
370370
for (tickValue = scalePtr->fromValue; ;
371371
tickValue += tickInterval) {
372372
/*
373-
* The TkRoundToResolution call gets rid of accumulated
373+
* The TkRoundValueToResolution call gets rid of accumulated
374374
* round-off errors, if any.
375375
*/
376376

377-
tickValue = TkRoundToResolution(scalePtr, tickValue);
377+
tickValue = TkRoundValueToResolution(scalePtr, tickValue);
378378
if (scalePtr->toValue >= scalePtr->fromValue) {
379379
if (tickValue > scalePtr->toValue) {
380380
break;

0 commit comments

Comments
 (0)