wxMaxima
Loading...
Searching...
No Matches
ClipboardContents.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
22#ifndef CLIPBOARDCONTENTS_H
23#define CLIPBOARDCONTENTS_H
24
56#include "precomp.h"
57#include <wx/bitmap.h>
58#include <wx/dataobj.h>
59#include <wx/string.h>
60#if wxUSE_ENH_METAFILE
61#include <wx/metafile.h>
62#endif
63#include <functional>
64#include <memory>
65#include <optional>
66#include <string>
67#include <utility>
68
69class Cell;
70class GroupCell;
71class Configuration;
72
79template <class T> class LazyValue
80{
81public:
82 LazyValue() = default;
83 explicit LazyValue(std::function<T()> renderer) : m_renderer(std::move(renderer)) {}
84
86 bool IsOffered() const { return m_value.has_value() || static_cast<bool>(m_renderer); }
88 bool IsRendered() const { return m_value.has_value(); }
89
91 const T &Get() const {
92 if (!m_value) {
93 if (m_renderer)
94 m_value = m_renderer();
95 else
96 m_value = T();
97 m_renderer = nullptr;
98 }
99 return *m_value;
100 }
101
102private:
103 mutable std::function<T()> m_renderer;
104 mutable std::optional<T> m_value;
105};
106
116{
117public:
122 ClipboardSnapshot(Configuration *configuration, const Cell *cells);
124
126 const Cell *GetCells() const { return m_cells.get(); }
128 std::unique_ptr<Cell> CopyCells() const;
130 Configuration *const *GetConfigurationPointer() const { return &m_configuration; }
131 Configuration *GetConfiguration() const { return m_configuration; }
132
133private:
134 Configuration *m_configuration;
137 std::unique_ptr<GroupCell> m_group;
138 std::unique_ptr<Cell> m_cells;
139};
140
153{
155 wxString wxm;
157 wxString text;
168#if wxUSE_ENH_METAFILE
171#endif
172};
173
179class LazyDataObject final : public wxDataObjectSimple
180{
181public:
182 LazyDataObject(const wxDataFormat &format,
183 std::shared_ptr<const LazyValue<std::string>> data);
184
185 size_t GetDataSize() const override;
186 bool GetDataHere(void *buf) const override;
188 bool SetData(size_t len, const void *buf) override;
189
190 // Don't hide the base class' per-format overloads, which forward to the
191 // ones above.
192 using wxDataObjectSimple::GetDataSize;
193 using wxDataObjectSimple::GetDataHere;
194 using wxDataObjectSimple::SetData;
195
196private:
197 std::shared_ptr<const LazyValue<std::string>> m_data;
198};
199
207class LazyBitmapDataObject final : public wxBitmapDataObject
208{
209public:
210 explicit LazyBitmapDataObject(std::shared_ptr<const LazyValue<wxBitmap>> bitmap);
211
212 wxBitmap GetBitmap() const override;
213 size_t GetDataSize() const override;
214 bool GetDataHere(void *buf) const override;
215 size_t GetDataSize(const wxDataFormat &format) const override;
216 bool GetDataHere(const wxDataFormat &format, void *buf) const override;
217
218private:
220 bool Render() const;
221 std::shared_ptr<const LazyValue<wxBitmap>> m_bitmap;
222 mutable bool m_rendered = false;
223};
224
234class LazyPngDataObject final : public wxDataObjectSimple
235{
236public:
237 explicit LazyPngDataObject(std::shared_ptr<const LazyValue<wxBitmap>> bitmap);
238
240 static wxDataFormat GetDataFormat();
241
242 size_t GetDataSize() const override;
243 bool GetDataHere(void *buf) const override;
245 bool SetData(size_t len, const void *buf) override;
246
247 // Don't hide the base class' per-format overloads, which forward to the
248 // ones above.
249 using wxDataObjectSimple::GetDataSize;
250 using wxDataObjectSimple::GetDataHere;
251 using wxDataObjectSimple::SetData;
252
253private:
256 const std::string &Png() const;
257 std::shared_ptr<const LazyValue<wxBitmap>> m_bitmap;
258 mutable std::optional<std::string> m_png;
259};
260
261#if wxUSE_ENH_METAFILE
263class LazyEnhMetaFileDataObject final : public wxEnhMetaFileDataObject
264{
265public:
266 explicit LazyEnhMetaFileDataObject(
267 std::shared_ptr<const LazyValue<wxEnhMetaFile>> metafile);
268
269 wxEnhMetaFile GetMetafile() const override;
270 size_t GetDataSize(const wxDataFormat &format) const override;
271 bool GetDataHere(const wxDataFormat &format, void *buf) const override;
272
273private:
275 bool Render() const;
276 std::shared_ptr<const LazyValue<wxEnhMetaFile>> m_metafile;
277 mutable bool m_rendered = false;
278};
279#endif
280
281#endif // CLIPBOARDCONTENTS_H
The base class all cell types the worksheet can consist of are derived from.
Definition: Cell.h:148
A private copy of the cells a copy was made from.
Definition: ClipboardContents.h:116
Configuration *const * GetConfigurationPointer() const
The configuration, in the form BitmapOut, Svgout and Emfout want it.
Definition: ClipboardContents.h:130
const Cell * GetCells() const
The snapshot's cells. Renderers that don't consume their input use this.
Definition: ClipboardContents.h:126
std::unique_ptr< Cell > CopyCells() const
A fresh copy of the cells, for renderers that take ownership of theirs.
Definition: ClipboardContents.cpp:41
The configuration storage for the current worksheet.
Definition: Configuration.h:101
A cell grouping input (and, if there is one, also the output) cell to a foldable item.
Definition: GroupCell.h:88
A bitmap that is only drawn when a program asks for it.
Definition: ClipboardContents.h:208
A clipboard format whose bytes are rendered when a program asks for them.
Definition: ClipboardContents.h:180
bool SetData(size_t len, const void *buf) override
Pasting into a LazyDataObject is not supported.
Definition: ClipboardContents.cpp:60
The same bitmap, as PNG bytes, for ports that can only hand over bytes.
Definition: ClipboardContents.h:235
bool SetData(size_t len, const void *buf) override
Pasting into a LazyPngDataObject is not supported.
Definition: ClipboardContents.cpp:143
static wxDataFormat GetDataFormat()
The format this is offered under: image/png.
Definition: ClipboardContents.cpp:111
A value that is computed the first time it is asked for.
Definition: ClipboardContents.h:80
const T & Get() const
The value, computed now if this is the first request.
Definition: ClipboardContents.h:91
bool IsRendered() const
Has the value been computed yet?
Definition: ClipboardContents.h:88
bool IsOffered() const
Is there a value, or a way to compute one?
Definition: ClipboardContents.h:86
Everything one copy operation puts on the clipboard.
Definition: ClipboardContents.h:153
wxString text
The selection as plain text.
Definition: ClipboardContents.h:157
LazyValue< std::string > mathML
A MathML document, UTF-8 encoded.
Definition: ClipboardContents.h:161
LazyValue< wxBitmap > bitmap
A bitmap image. Invalid if it could not be drawn.
Definition: ClipboardContents.h:167
LazyValue< std::string > xml
Copied cells as a self-contained XML document, UTF-8 encoded.
Definition: ClipboardContents.h:159
LazyValue< std::string > svg
A SVG image.
Definition: ClipboardContents.h:165
LazyValue< std::string > rtf
An RTF document with OMML maths, UTF-8 encoded.
Definition: ClipboardContents.h:163
wxString wxm
The selection as .wxm batch code.
Definition: ClipboardContents.h:155