wxMaxima
Loading...
Searching...
No Matches
WorksheetSizeMath.h
Go to the documentation of this file.
1// -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2//
3// Copyright (C) 2026 Gunter Königsmann <wxMaxima@physikbuch.de>
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19//
20// SPDX-License-Identifier: GPL-2.0+
21
33#ifndef WORKSHEETSIZEMATH_H
34#define WORKSHEETSIZEMATH_H
35
36#include <algorithm>
37#include <vector>
38
52 const std::vector<int> &cellWidthsWithMargins, int baseIndent) {
53 int width = baseIndent;
54 for (int cellWidth : cellWidthsWithMargins)
55 width = std::max(width, cellWidth);
56 return width;
57}
58
62 bool sizeIsStale = false;
64 int currentY = 0;
66 int maxDrop = 0;
67};
68
90 const std::vector<TrailingGroupGeometry> &trailing, int groupSkip,
91 int baseIndent) {
92 int extraHeight = 0;
93 for (const TrailingGroupGeometry &cell : trailing) {
94 if (cell.sizeIsStale && cell.currentY >= 0)
95 // The anchor: its top position is trusted; add its drop and everything
96 // accumulated below it.
97 return cell.currentY + cell.maxDrop + extraHeight;
98 if (cell.sizeIsStale)
99 // Stale and not yet positioned (currentY < 0): count its own extent.
100 extraHeight += cell.maxDrop + groupSkip;
101 else
102 // Not stale but still unpositioned mid-layout: reserve a nominal height.
103 extraHeight += groupSkip + 20;
104 }
105 return baseIndent + extraHeight;
106}
107
108
112 int width = 40;
114 int height = 40;
116 int scrollUnit = 10;
117};
118
139 int maxPointWidth,
140 int maxPointHeight,
141 int clientHeight,
142 int currentScrollPixelY) {
144 int scrollUnitBasisHeight = 40;
145 if (hasTree) {
146 result.width = maxPointWidth;
147 // Allow scrolling a little past the end of the document.
148 int height = maxPointHeight + clientHeight - clientHeight / 8;
149 // Keep a vertical scrollbar active.
150 result.height = std::max(clientHeight + 10, height);
151 // Never shrink below what the current scroll position requires.
152 result.height =
153 std::max(result.height, currentScrollPixelY + clientHeight + 10);
154 scrollUnitBasisHeight = clientHeight;
155 }
156 result.scrollUnit = std::max(scrollUnitBasisHeight / 30, 10);
157 return result;
158}
159
165 int topOffset = 0;
166};
167
195 int scrollUnit,
196 int topOffset, int shift) {
197 ScrollCompensation result;
198 if (scrollUnit < 1)
199 scrollUnit = 1;
200 // The pixel position the view's top edge must be at, measured in a document
201 // whose top offset is 0.
202 const int target = scrollUnitsY * scrollUnit + shift - topOffset;
203 if (target <= 0) {
204 result.scrollUnitsY = 0;
205 result.topOffset = std::clamp(-target, 0, scrollUnit - 1);
206 return result;
207 }
208 // Round up to a whole scroll unit and shift the document down by the excess.
209 result.scrollUnitsY = (target + scrollUnit - 1) / scrollUnit;
210 result.topOffset = result.scrollUnitsY * scrollUnit - target;
211 return result;
212}
213
227public:
228 virtual ~WorksheetView() = default;
230 virtual void GetViewClientSize(int *width, int *height) const = 0;
232 virtual int GetViewScrollUnitY() const = 0;
234 virtual void SetViewVirtualSize(int width, int height) = 0;
236 virtual void SetViewScrollRate(int rate) = 0;
238 virtual void GetViewPosition(int *x, int *y) const = 0;
240 virtual void ScrollViewToUnitY(int unitY) = 0;
241};
242
245 int lastWidth = -1;
246 int lastHeight = -1;
247};
248
266inline void ApplyWorksheetVirtualSize(WorksheetView &view, bool hasTree,
267 int maxWidth, int maxHeight,
269 int &scrollUnit) {
270 int clientWidth, clientHeight;
271 view.GetViewClientSize(&clientWidth, &clientHeight);
272
273 int currentScrollPixelY = 0;
274 if (hasTree)
275 currentScrollPixelY = view.GetViewScrollUnitY() * scrollUnit;
276
278 hasTree, maxWidth, maxHeight, clientHeight, currentScrollPixelY);
279
280 if ((cache.lastWidth != vs.width || cache.lastHeight != vs.height) &&
281 vs.height > 0) {
282 cache.lastWidth = vs.width;
283 cache.lastHeight = vs.height;
284 view.SetViewVirtualSize(vs.width, vs.height);
285 scrollUnit = vs.scrollUnit;
287 }
288}
289
290#endif
ScrollCompensation ComputeScrollCompensation(int scrollUnitsY, int scrollUnit, int topOffset, int shift)
Compute the scroll position that keeps a point of the worksheet still on screen.
Definition: WorksheetSizeMath.h:194
void ApplyWorksheetVirtualSize(WorksheetView &view, bool hasTree, int maxWidth, int maxHeight, WorksheetVirtualSizeCache &cache, int &scrollUnit)
Measure the view, compute the virtual size and apply it, skipping no-ops.
Definition: WorksheetSizeMath.h:266
WorksheetVirtualSize ComputeWorksheetVirtualSize(bool hasTree, int maxPointWidth, int maxPointHeight, int clientHeight, int currentScrollPixelY)
Compute a worksheet's virtual size and scroll unit.
Definition: WorksheetSizeMath.h:138
int ComputeWorksheetContentWidth(const std::vector< int > &cellWidthsWithMargins, int baseIndent)
Compute the worksheet's content width.
Definition: WorksheetSizeMath.h:51
int ComputeWorksheetContentHeight(const std::vector< TrailingGroupGeometry > &trailing, int groupSkip, int baseIndent)
Compute the worksheet's content height from its trailing cells' geometry.
Definition: WorksheetSizeMath.h:89
The narrow view surface the worksheet's layout pipeline needs.
Definition: WorksheetSizeMath.h:226
virtual void SetViewScrollRate(int rate)=0
Set the scroll granularity (device pixels per scroll unit) on both axes.
virtual int GetViewScrollUnitY() const =0
The current vertical scroll position, in scroll units (not pixels).
virtual void GetViewPosition(int *x, int *y) const =0
The view's position within its parent, in device pixels (wxWindow::GetPosition).
virtual void GetViewClientSize(int *width, int *height) const =0
The visible client area, in device pixels.
virtual void ScrollViewToUnitY(int unitY)=0
Scroll vertically to the given position, in scroll units.
virtual void SetViewVirtualSize(int width, int height)=0
Set the scrollable (virtual) area, in device pixels.
Where to scroll to, and how far to shift the document, to keep a point still.
Definition: WorksheetSizeMath.h:161
int topOffset
The new worksheet top offset (Configuration::GetWorksheetTopOffset), in px.
Definition: WorksheetSizeMath.h:165
int scrollUnitsY
The new vertical scroll position, in scroll units.
Definition: WorksheetSizeMath.h:163
One trailing group cell's geometry, as GetMaxPoint's height walk needs it.
Definition: WorksheetSizeMath.h:60
bool sizeIsStale
Whether the cell's size still needs recomputing (GroupCell::HasStaleSize).
Definition: WorksheetSizeMath.h:62
int maxDrop
The cell's extent below its baseline (GetMaxDrop()).
Definition: WorksheetSizeMath.h:66
int currentY
The cell's laid-out top position (GetCurrentPoint().y); < 0 = not yet set.
Definition: WorksheetSizeMath.h:64
Remembers the last virtual size applied, so an unchanged size is a no-op.
Definition: WorksheetSizeMath.h:244
The scrollable size + scroll granularity computed for a worksheet.
Definition: WorksheetSizeMath.h:110
int height
The virtual (scrollable) height (device px).
Definition: WorksheetSizeMath.h:114
int scrollUnit
The scroll rate (device px per scroll unit).
Definition: WorksheetSizeMath.h:116
int width
The virtual width (device px).
Definition: WorksheetSizeMath.h:112