wxMaxima
Loading...
Searching...
No Matches
TreeUndoManager.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 TREEUNDOMANAGER_H
35#define TREEUNDOMANAGER_H
36
37#include "TreeUndoAction.h"
38
51{
52public:
54 UndoActions &UndoStack() { return m_undoActions; }
55 const UndoActions &UndoStack() const { return m_undoActions; }
56
58 UndoActions &RedoStack() { return m_redoActions; }
59 const UndoActions &RedoStack() const { return m_redoActions; }
60
67 static void DiscardAction(UndoActions *actionList)
68 {
69 if (actionList->empty())
70 return;
71 do {
72 actionList->pop_back();
73 } while (!actionList->empty() && actionList->back().m_partOfAtomicAction);
74 }
75
77 static void AppendAction(UndoActions *actionList)
78 {
79 if (!actionList->empty())
80 actionList->front().m_partOfAtomicAction = true;
81 }
82
84 void AppendAction() { AppendAction(&m_undoActions); }
85
88 {
89 while (!m_redoActions.empty())
90 DiscardAction(&m_redoActions);
91 }
92
95 {
96 while (!m_undoActions.empty())
97 DiscardAction(&m_undoActions);
98 }
99
102 {
105 m_activeCell = nullptr;
106 }
107
112 void LimitUndoBuffer(long undoLimit)
113 {
114 if (undoLimit == 0)
115 return;
116 while (static_cast<long>(m_undoActions.size()) > undoLimit)
117 DiscardAction(&m_undoActions);
118 }
119
127 void CellEntered(GroupCell *group, const wxString &text,
128 long long selStart, long long selEnd)
129 {
130 m_activeCell = group;
131 m_activeCellOldText = text;
132 m_activeCellOldSelStart = selStart;
133 m_activeCellOldSelEnd = selEnd;
134 }
135
148 bool CellLeft(GroupCell *activeCell, const wxString &currentText,
149 long undoLimit)
150 {
151 if (m_activeCell) //-V1051
152 wxASSERT_MSG(m_activeCell == activeCell,
153 _("Bug: Cell left but not entered."));
154
155 // We only can undo a text change if the text has actually changed.
156 if ((m_activeCellOldText.Length() > 1) &&
157 (m_activeCellOldText != currentText) &&
158 (m_activeCellOldText + wxS(";") != currentText)) {
159 m_undoActions.emplace_front(activeCell, m_activeCellOldText,
160 m_activeCellOldSelStart,
161 m_activeCellOldSelEnd);
162 LimitUndoBuffer(undoLimit);
164 return true;
165 }
166 return false;
167 }
168
170 void ForgetActiveCell() { m_activeCell = nullptr; }
171
173 GroupCell *ActiveCell() const { return m_activeCell; }
174
175private:
177 UndoActions m_undoActions;
178
180 UndoActions m_redoActions;
181
188 CellPtr<GroupCell> m_activeCell;
189
191 wxString m_activeCellOldText;
192
194 long long m_activeCellOldSelStart = -1;
195 long long m_activeCellOldSelEnd = -1;
196};
197
198#endif // TREEUNDOMANAGER_H
Declares TreeUndoAction, the immutable record of one cell-tree undo/redo step, and the UndoActions li...
std::list< TreeUndoAction > UndoActions
The type of the list of tree actions that can be undone.
Definition: TreeUndoAction.h:120
A weak non-owning pointer that becomes null whenever the observed object is destroyed.
Definition: CellPtr.h:511
A cell grouping input (and, if there is one, also the output) cell to a foldable item.
Definition: GroupCell.h:87
Owns the cell-tree undo/redo stacks and the active-cell snapshot.
Definition: TreeUndoManager.h:51
GroupCell * ActiveCell() const
The cell the cursor is in, as recorded by CellEntered().
Definition: TreeUndoManager.h:173
bool CellLeft(GroupCell *activeCell, const wxString &currentText, long undoLimit)
The cursor is leaving a cell: record a text-change undo action if needed.
Definition: TreeUndoManager.h:148
void CellEntered(GroupCell *group, const wxString &text, long long selStart, long long selEnd)
The cursor has entered a cell: snapshot its contents.
Definition: TreeUndoManager.h:127
void ClearUndoActionList()
Clear the list of actions that can be undone.
Definition: TreeUndoManager.h:94
void ClearBuffers()
Clear both action lists and forget the active-cell snapshot.
Definition: TreeUndoManager.h:101
UndoActions & UndoStack()
The list of tree actions that can be undone.
Definition: TreeUndoManager.h:54
void ForgetActiveCell()
Forget the active-cell snapshot (e.g. the next change must not be undoable).
Definition: TreeUndoManager.h:170
void LimitUndoBuffer(long undoLimit)
Drop actions from the back of the undo list until it is within the limit.
Definition: TreeUndoManager.h:112
static void DiscardAction(UndoActions *actionList)
Remove one action from an action list.
Definition: TreeUndoManager.h:67
UndoActions & RedoStack()
The list of undone tree actions that can be redone.
Definition: TreeUndoManager.h:58
void ClearRedoActionList()
Clear the list of actions that can be redone.
Definition: TreeUndoManager.h:87
void AppendAction()
Mark the newest undo action as part of the following atomic action.
Definition: TreeUndoManager.h:84
static void AppendAction(UndoActions *actionList)
Mark the newest action of the list as part of the following atomic action.
Definition: TreeUndoManager.h:77