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
173public:
174 virtual ~WorksheetView() = default;
176 virtual void GetViewClientSize(int *width, int *height) const = 0;
178 virtual int GetViewScrollUnitY() const = 0;
180 virtual void SetViewVirtualSize(int width, int height) = 0;
182 virtual void SetViewScrollRate(int rate) = 0;
184 virtual void GetViewPosition(int *x, int *y) const = 0;
185};
186
189 int lastWidth = -1;
190 int lastHeight = -1;
191};
192
210inline void ApplyWorksheetVirtualSize(WorksheetView &view, bool hasTree,
211 int maxWidth, int maxHeight,
213 int &scrollUnit) {
214 int clientWidth, clientHeight;
215 view.GetViewClientSize(&clientWidth, &clientHeight);
216
217 int currentScrollPixelY = 0;
218 if (hasTree)
219 currentScrollPixelY = view.GetViewScrollUnitY() * scrollUnit;
220
222 hasTree, maxWidth, maxHeight, clientHeight, currentScrollPixelY);
223
224 if ((cache.lastWidth != vs.width || cache.lastHeight != vs.height) &&
225 vs.height > 0) {
226 cache.lastWidth = vs.width;
227 cache.lastHeight = vs.height;
228 view.SetViewVirtualSize(vs.width, vs.height);
229 scrollUnit = vs.scrollUnit;
231 }
232}
233
234#endif
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:210
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:172
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 SetViewVirtualSize(int width, int height)=0
Set the scrollable (virtual) area, in device pixels.
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:188
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