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
149 int maxPointWidth,
150 int maxPointHeight,
151 int clientHeight,
152 int currentScrollPixelY,
153 int groupSkip) {
155 int scrollUnitBasisHeight = 40;
156 if (hasTree)
157 scrollUnitBasisHeight = clientHeight;
158 result.scrollUnit = std::max(scrollUnitBasisHeight / 30, 10);
159 if (hasTree) {
160 result.width = maxPointWidth;
161 // Allow scrolling one scroll step past the room a horizontal cursor below
162 // the last cell needs.
163 int height = maxPointHeight + groupSkip + result.scrollUnit;
164 // Keep a vertical scrollbar active.
165 result.height = std::max(clientHeight + 10, height);
166 // Never shrink below what the current scroll position requires.
167 result.height =
168 std::max(result.height, currentScrollPixelY + clientHeight);
169 }
170 return result;
171}
172
178 int topOffset = 0;
179};
180
208 int scrollUnit,
209 int topOffset, int shift) {
210 ScrollCompensation result;
211 if (scrollUnit < 1)
212 scrollUnit = 1;
213 // The pixel position the view's top edge must be at, measured in a document
214 // whose top offset is 0.
215 const int target = scrollUnitsY * scrollUnit + shift - topOffset;
216 if (target <= 0) {
217 result.scrollUnitsY = 0;
218 result.topOffset = std::clamp(-target, 0, scrollUnit - 1);
219 return result;
220 }
221 // Round up to a whole scroll unit and shift the document down by the excess.
222 result.scrollUnitsY = (target + scrollUnit - 1) / scrollUnit;
223 result.topOffset = result.scrollUnitsY * scrollUnit - target;
224 return result;
225}
226
240public:
241 virtual ~WorksheetView() = default;
243 virtual void GetViewClientSize(int *width, int *height) const = 0;
245 virtual int GetViewScrollUnitY() const = 0;
247 virtual void SetViewVirtualSize(int width, int height) = 0;
249 virtual void SetViewScrollRate(int rate) = 0;
251 virtual void GetViewPosition(int *x, int *y) const = 0;
253 virtual void ScrollViewToUnitY(int unitY) = 0;
254};
255
258 int lastWidth = -1;
259 int lastHeight = -1;
260};
261
280inline void ApplyWorksheetVirtualSize(WorksheetView &view, bool hasTree,
281 int maxWidth, int maxHeight,
282 int groupSkip,
284 int &scrollUnit) {
285 int clientWidth, clientHeight;
286 view.GetViewClientSize(&clientWidth, &clientHeight);
287
288 int currentScrollPixelY = 0;
289 if (hasTree)
290 currentScrollPixelY = view.GetViewScrollUnitY() * scrollUnit;
291
293 hasTree, maxWidth, maxHeight, clientHeight, currentScrollPixelY,
294 groupSkip);
295
296 if ((cache.lastWidth != vs.width || cache.lastHeight != vs.height) &&
297 vs.height > 0) {
298 cache.lastWidth = vs.width;
299 cache.lastHeight = vs.height;
300 view.SetViewVirtualSize(vs.width, vs.height);
301 scrollUnit = vs.scrollUnit;
303 }
304}
305
306#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:207
void ApplyWorksheetVirtualSize(WorksheetView &view, bool hasTree, int maxWidth, int maxHeight, int groupSkip, WorksheetVirtualSizeCache &cache, int &scrollUnit)
Measure the view, compute the virtual size and apply it, skipping no-ops.
Definition: WorksheetSizeMath.h:280
WorksheetVirtualSize ComputeWorksheetVirtualSize(bool hasTree, int maxPointWidth, int maxPointHeight, int clientHeight, int currentScrollPixelY, int groupSkip)
Compute a worksheet's virtual size and scroll unit.
Definition: WorksheetSizeMath.h:148
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:239
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:174
int topOffset
The new worksheet top offset (Configuration::GetWorksheetTopOffset), in px.
Definition: WorksheetSizeMath.h:178
int scrollUnitsY
The new vertical scroll position, in scroll units.
Definition: WorksheetSizeMath.h:176
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:257
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