+/** Set the top of the y range visible in the editor.
+ * Caller should have checked that Editor::pending_visual_change.idle_handler_id is < 0
+ * @param y new editor top in summary coodinates.
+ */
+void
+EditorSummary::set_editor_y (double const y)
+{
+ double y1 = summary_y_to_editor (y);
+ double const eh = _editor->canvas_height() - _editor->get_canvas_timebars_vsize ();
+ double y2 = y1 + eh;
+
+ double const full_editor_height = _editor->full_canvas_height - _editor->get_canvas_timebars_vsize();
+
+ if (y2 > full_editor_height) {
+ y1 -= y2 - full_editor_height;
+ }
+
+ if (y1 < 0) {
+ y1 = 0;
+ }
+
+ if (suspending_editor_updates ()) {
+ double const h = _pending_editor_y.second - _pending_editor_y.first;
+ _pending_editor_y.first = y;
+ _pending_editor_y.second = y + h;
+ _pending_editor_changed = true;
+ set_dirty ();
+ } else {
+ _editor->reset_y_origin (y1);
+ }
+}
+
+/** Set the y range visible in the editor. This is achieved by scaling track heights,
+ * if necessary.
+ * Caller should have checked that Editor::pending_visual_change.idle_handler_id is < 0
+ * @param y new editor range in summary coodinates.
+ */
+void
+EditorSummary::set_editor_y (pair<double, double> const y)
+{
+ if (suspending_editor_updates ()) {
+ _pending_editor_y = y;
+ _pending_editor_changed = true;
+ set_dirty ();
+ return;
+ }
+
+ /* Compute current height of tracks between y.first and y.second. We add up
+ the total height into `total_height' and the height of complete tracks into
+ `scale height'.
+ */
+
+ /* Copy of target range for use below */
+ pair<double, double> yc = y;
+ /* Total height of all tracks */
+ double total_height = 0;
+ /* Height of any parts of tracks that aren't fully in the desired range */
+ double partial_height = 0;
+ /* Height of any tracks that are fully in the desired range */
+ double scale_height = 0;
+
+ _editor->_routes->suspend_redisplay ();
+
+ for (TrackViewList::const_iterator i = _editor->track_views.begin(); i != _editor->track_views.end(); ++i) {
+
+ if ((*i)->hidden()) {
+ continue;
+ }
+
+ double const h = (*i)->effective_height ();
+ total_height += h;
+
+ if (yc.first > 0 && yc.first < _track_height) {
+ partial_height += (_track_height - yc.first) * h / _track_height;
+ } else if (yc.first <= 0 && yc.second >= _track_height) {
+ scale_height += h;
+ } else if (yc.second > 0 && yc.second < _track_height) {
+ partial_height += yc.second * h / _track_height;
+ break;
+ }
+
+ yc.first -= _track_height;
+ yc.second -= _track_height;
+ }
+
+ /* Height that we will use for scaling; use the whole editor height unless there are not
+ enough tracks to fill it.
+ */
+ double const ch = min (total_height, _editor->canvas_height() - _editor->get_canvas_timebars_vsize());
+
+ /* hence required scale factor of the complete tracks to fit the required y range;
+ the amount of space they should take up divided by the amount they currently take up.
+ */
+ double const scale = (ch - partial_height) / scale_height;
+
+ yc = y;
+
+ /* Scale complete tracks within the range to make it fit */
+
+ for (TrackViewList::const_iterator i = _editor->track_views.begin(); i != _editor->track_views.end(); ++i) {
+
+ if ((*i)->hidden()) {
+ continue;
+ }
+
+ if (yc.first <= 0 && yc.second >= _track_height) {
+ (*i)->set_height (max (TimeAxisView::preset_height (HeightSmall), (uint32_t) ((*i)->effective_height() * scale)));
+ }
+
+ yc.first -= _track_height;
+ yc.second -= _track_height;
+ }
+
+ _editor->_routes->resume_redisplay ();
+
+ set_editor_y (y.first);
+}
+