JAL-147 corrected calculation of vertical scroll position and max
[jalview.git] / src / jalview / viewmodel / ViewportRanges.java
index 7c6b7ab..10cf583 100644 (file)
@@ -563,14 +563,13 @@ public class ViewportRanges extends ViewportProperties
 
   /**
    * Answers the vertical scroll position (0..) to set, given the visible column
-   * that is at top left. Note that if called with the total visible width of
-   * the alignment, this gives the maximum cursor scroll value.
+   * that is at top left.
    * 
    * <pre>
    * Example:
    *    viewport width 40 columns (0-39, 40-79, 80-119...)
    *    column 0 returns scroll position 0
-   *    columns 0-40 return scroll position 1
+   *    columns 1-40 return scroll position 1
    *    columns 41-80 return scroll position 2
    *    etc
    * </pre>
@@ -587,8 +586,35 @@ public class ViewportRanges extends ViewportProperties
      * visible whole widths
      */
     int scroll = topLeftColumn / w;
+
+    /*
+     * add 1 for a part width if there is one
+     */
     scroll += topLeftColumn % w > 0 ? 1 : 0;
 
     return scroll;
   }
+
+  /**
+   * Answers the maximum wrapped vertical scroll value, given the column
+   * position (0..) to show at top left of the visible region.
+   * 
+   * @param topLeftColumn
+   * @return
+   */
+  public int getWrappedMaxScroll(int topLeftColumn)
+  {
+    int scrollPosition = getWrappedScrollPosition(topLeftColumn);
+
+    /*
+     * how many more widths could be drawn after this one?
+     */
+    int columnsRemaining = getVisibleAlignmentWidth() - topLeftColumn;
+    int width = getViewportWidth();
+    int widthsRemaining = columnsRemaining / width
+            + (columnsRemaining % width > 0 ? 1 : 0) - 1;
+    int maxScroll = scrollPosition + widthsRemaining;
+
+    return maxScroll;
+  }
 }