wxMaxima
Loading...
Searching...
No Matches
RenderContext.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
34#ifndef RENDERCONTEXT_H
35#define RENDERCONTEXT_H
36
37#include <wx/brush.h>
38#include <wx/dcclient.h>
39#include <wx/gdicmn.h>
40#include <wx/window.h>
41#include <algorithm>
42#include <atomic>
43#include <chrono>
44#include <memory>
45#include <vector>
46
47class Cell;
48
60{
61public:
62 RenderContext() = default;
64 : m_recalcDC(o.m_recalcDC),
65 m_canvasSize(o.m_canvasSize),
66 m_updateRegion(o.m_updateRegion),
67 m_visibleRegion(o.m_visibleRegion),
68 m_worksheetPosition(o.m_worksheetPosition),
69 m_backgroundBrush(o.m_backgroundBrush),
70 m_clipToDrawRegion(o.m_clipToDrawRegion),
71 m_printing(o.m_printing)
72 {
73 }
74 RenderContext &operator=(const RenderContext &) = delete;
75
77 wxDC *GetRecalcDC() const { return m_recalcDC; }
79 void SetRecalcDC(wxDC *dc) { m_recalcDC = dc; }
80
86 void AttachWorksheetDC(wxWindow *worksheet)
87 {
88 m_worksheetDC = std::unique_ptr<wxClientDC>(new wxClientDC(worksheet));
89 m_recalcDC = m_worksheetDC.get();
90 }
93 {
94 if (m_recalcDC == m_worksheetDC.get())
95 m_recalcDC = NULL;
96 m_worksheetDC.reset();
97 }
98
100 wxSize GetCanvasSize() const { return m_canvasSize; }
102 void SetCanvasSize(wxSize siz) { m_canvasSize = siz; }
103
105 wxRect GetUpdateRegion() const { return m_updateRegion; }
107 void SetUpdateRegion(wxRect rect) { m_updateRegion = rect; }
108
110 wxRect GetVisibleRegion() const { return m_visibleRegion; }
112 void SetVisibleRegion(wxRect visibleRegion) { m_visibleRegion = visibleRegion; }
113
115 bool Printing() const { return m_printing; }
117 void SetPrinting(bool printing) { m_printing = printing; }
118
121 {
122 if (!m_cellRedrawTrace)
123 m_cellRedrawTrace.reset(new std::vector<const Cell *>);
124 else
125 m_cellRedrawTrace->clear();
126 }
128 void NotifyOfCellRedraw(const Cell *cell)
129 {
130 // This operation is fast and doesn't allocate after the render context
131 // was used for a few screen redraws.
132 if (m_cellRedrawTrace && cell)
133 m_cellRedrawTrace->push_back(cell);
134 }
141 template <typename Report> void ReportMultipleRedraws(Report report)
142 {
143 if (!m_cellRedrawTrace)
144 return;
145 std::sort(m_cellRedrawTrace->begin(), m_cellRedrawTrace->end());
146 size_t counter = 0;
147 const Cell *prev = nullptr;
148 for (const Cell *cell : *m_cellRedrawTrace) {
149 if (prev != cell) {
150 if (counter > 1)
151 report(counter, prev);
152 prev = cell;
153 counter = 1;
154 } else
155 ++counter;
156 }
157 if (counter > 1)
158 report(counter, prev);
159 }
160
162 bool ClipToDrawRegion() const { return m_clipToDrawRegion; }
164 void ClipToDrawRegion(bool clipToDrawRegion)
165 { m_clipToDrawRegion = clipToDrawRegion; }
166
168 wxPoint GetWorksheetPosition() const { return m_worksheetPosition; }
170 void SetWorksheetPosition(wxPoint worksheetPosition)
171 { m_worksheetPosition = worksheetPosition; }
172
174 wxBrush GetBackgroundBrush() const { return m_backgroundBrush; }
176 void SetBackgroundBrush(const wxBrush &brush) { m_backgroundBrush = brush; }
177
179 void SetLayoutDeadline(int seconds)
180 {
181 m_layoutDeadline = std::chrono::steady_clock::now() +
182 std::chrono::seconds(seconds);
183 m_layoutCancelled.store(false, std::memory_order_relaxed);
184 m_layoutDeadlineActive = true;
185 }
188 {
189 m_layoutDeadlineActive = false;
190 m_layoutCancelled.store(false, std::memory_order_relaxed);
191 }
193 bool IsLayoutCancelled() const
194 {
195 if (m_layoutCancelled.load(std::memory_order_relaxed))
196 return true;
197 if (m_layoutDeadlineActive &&
198 std::chrono::steady_clock::now() >= m_layoutDeadline) {
199 m_layoutCancelled.store(true, std::memory_order_relaxed);
200 return true;
201 }
202 return false;
203 }
204
205private:
207 wxDC *m_recalcDC = nullptr;
209 std::unique_ptr<wxClientDC> m_worksheetDC;
211 wxSize m_canvasSize;
213 wxRect m_updateRegion;
215 wxRect m_visibleRegion;
217 wxPoint m_worksheetPosition;
219 wxBrush m_backgroundBrush;
221 bool m_clipToDrawRegion = true;
223 bool m_printing = false;
230 std::unique_ptr<std::vector<const Cell *>> m_cellRedrawTrace;
232 std::chrono::steady_clock::time_point m_layoutDeadline;
234 bool m_layoutDeadlineActive = false;
236 mutable std::atomic<bool> m_layoutCancelled{false};
237};
238
239#endif // RENDERCONTEXT_H
The base class all cell types the worksheet can consist of are derived from.
Definition: Cell.h:141
The state of the current worksheet render pass.
Definition: RenderContext.h:60
void SetRecalcDC(wxDC *dc)
Tells us which dc to measure text sizes on.
Definition: RenderContext.h:79
void SetWorksheetPosition(wxPoint worksheetPosition)
Sets where the worksheet is to be drawn.
Definition: RenderContext.h:170
void NotifyOfCellRedraw(const Cell *cell)
Records that this cell has been drawn, if tracing is enabled.
Definition: RenderContext.h:128
void DetachWorksheetDC()
Drops the worksheet client DC created by AttachWorksheetDC() again.
Definition: RenderContext.h:92
void ClearLayoutCancelled()
Stops the layout deadline and clears the cancelled flag.
Definition: RenderContext.h:187
wxRect GetUpdateRegion() const
The rectangle of the worksheet that is currently being redrawn.
Definition: RenderContext.h:105
void SetVisibleRegion(wxRect visibleRegion)
Sets the rectangle of the worksheet that is currently visible.
Definition: RenderContext.h:112
void ReportMultipleRedraws(Report report)
Calls report(count, cell) for each cell that was drawn more than once.
Definition: RenderContext.h:141
void SetPrinting(bool printing)
Sets whether we are currently rendering for the printer.
Definition: RenderContext.h:117
bool ClipToDrawRegion() const
Do we want to omit drawing outside the current update region?
Definition: RenderContext.h:162
void ClearAndEnableRedrawTracing()
Starts (or restarts) recording which cells this render pass has drawn.
Definition: RenderContext.h:120
bool Printing() const
Are we currently rendering for the printer rather than the screen?
Definition: RenderContext.h:115
void AttachWorksheetDC(wxWindow *worksheet)
Creates a client DC for the given window and measures text on it.
Definition: RenderContext.h:86
void SetLayoutDeadline(int seconds)
Starts a layout deadline this many seconds in the future.
Definition: RenderContext.h:179
wxBrush GetBackgroundBrush() const
The brush the worksheet's background is painted with.
Definition: RenderContext.h:174
void SetCanvasSize(wxSize siz)
Sets the size of the worksheet's visible window.
Definition: RenderContext.h:102
void ClipToDrawRegion(bool clipToDrawRegion)
Whether to omit drawing outside the current update region.
Definition: RenderContext.h:164
bool IsLayoutCancelled() const
Has the current layout pass exceeded its deadline?
Definition: RenderContext.h:193
wxDC * GetRecalcDC() const
The dc "recalculate" (measuring text) currently draws to.
Definition: RenderContext.h:77
wxPoint GetWorksheetPosition() const
Where is the worksheet to be drawn?
Definition: RenderContext.h:168
wxSize GetCanvasSize() const
The size of the worksheet's visible window.
Definition: RenderContext.h:100
void SetBackgroundBrush(const wxBrush &brush)
Sets the brush the worksheet's background is painted with.
Definition: RenderContext.h:176
wxRect GetVisibleRegion() const
The rectangle of the worksheet that is currently visible.
Definition: RenderContext.h:110
void SetUpdateRegion(wxRect rect)
Sets the rectangle of the worksheet that is currently being redrawn.
Definition: RenderContext.h:107