Tk Source Code

View Ticket
Login
Ticket UUID: 1899040
Title: TkRoundToResolution doesn't account for -from
Type: Bug Version: obsolete: 8.5.1
Submitter: pieper Created on: 2008-02-21 19:06:37
Subsystem: 15. [scale] Assigned To: fvogel
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2019-01-26 13:35:11
Resolution: Duplicate Closed By: fvogel
    Closed on: 2019-01-26 13:35:11
Description:
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;
}
User Comments: fvogel added on 2019-01-26 13:35:11:

The fix for [3003895fff] was werged to core-8-6-branch and trunk. Closing now.


fvogel added on 2019-01-05 21:02:25:

Actually the fix you proposed was not fully correct in my opinion.

I have committed a better fix in a bugfix branch, and I have added two new tests.

This all passes scale.test 100% OK (tested on Windows).

Follow up in the duplicate ticket [3003895fff].