Menu

#2411 TkRoundToResolution doesn't account for -from

obsolete: 8.5.1
open
5
2008-02-21
2008-02-21
No

If you run the following script you get a scale that can be set to the values of -1, 0, 1, and 2. I would argue that for consistency the values should be at -1.5, -.5, .5, and 1.5.

toplevel .test
pack [scale .test.s -orient horizontal]
.test.s configure -resolution 1 -from -1.5 -to 1.5

This behavior is in both 8.4.x and 8.5.x.

The problem is that TkRoundToResolution function doesn't take into account the -from value.

The following patch provides the expected behavior. It rounds to the resolution value in terms of units relative to the -from value:

RCS file: /projects/cvs/slicer/tk/generic/tkScale.c,v
retrieving revision 1.1.1.1
diff -r1.1.1.1 tkScale.c
1148a1149
> value = value + scalePtr->fromValue;
1160a1162
> new = new - scalePtr->fromValue;

The entire function, including the patch, is here:

double
TkRoundToResolution(scalePtr, value)
TkScale *scalePtr; /* Information about scale widget. */
double value; /* Value to round. */
{
double rem, new, tick;

if (scalePtr->resolution <= 0) {
return value;
}
value = value + scalePtr->fromValue;
tick = floor(value/scalePtr->resolution);
new = scalePtr->resolution * tick;
rem = value - new;
if (rem < 0) {
if (rem <= -scalePtr->resolution/2) {
new = (tick - 1.0) * scalePtr->resolution;
}
} else {
if (rem >= scalePtr->resolution/2) {
new = (tick + 1.0) * scalePtr->resolution;
}
}
new = new - scalePtr->fromValue;
return new;
}

Discussion

MongoDB Logo MongoDB